aboutsummaryrefslogtreecommitdiff
path: root/util.c
blob: cedd89e1f8fba231c6829b27009c583bec3f58a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <u.h>
#include <libc.h>
#include <mp.h>
#include <libsec.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
#include <geometry.h>
#include "dat.h"
#include "fns.h"

static char rowtab[] = "abcdefghijklmnopq";	/* |rowtab| = MAPH */
static int shiplentab[] = {
 [Scarrier]	5,
 [Sbattleship]	4,
 [Scruiser]	3,
 [Ssubmarine]	3,
 [Sdestroyer]	2,
};
static char *shipnametab[] = {
 [Scarrier]	"carrier",
 [Sbattleship]	"battleship",
 [Scruiser]	"cruiser",
 [Ssubmarine]	"submarine",
 [Sdestroyer]	"destroyer",
};
static char *statenametab[] = {
 [Waiting0]	"Waiting0",
 [Watching]	"Watching",
 [Ready]	"Ready",
 [Outlaying]	"Outlaying",
 [Waiting]	"Waiting",
 [Playing]	"Playing",
};


int
isoob(Point2 cell)
{
	return cell.x < 0 || cell.x >= MAPW ||
		cell.y < 0 || cell.y >= MAPH;
}

int
cell2coords(char *buf, ulong len, Point2 cell)
{
	assert(len >= 3+1 && !isoob(cell));
	return snprint(buf, len, "%c%d", rowtab[(int)cell.y], (int)cell.x);
}

Point2
coords2cell(char *s)
{
	Point2 cell;
	char *p;

	assert(s[0] >= 'a' && s[0] <= 'q');

	cell = Pt2(0,0,1);
	p = strchr(rowtab, s[0]);
	cell.y = p-rowtab;
	cell.x = strtol(s+1, nil, 10);

	assert(!isoob(cell));

	return cell;
}

int
gettile(Map *m, Point2 cell)
{
	return m->map[(int)cell.x][(int)cell.y];
}

void
settile(Map *m, Point2 cell, int type)
{
	assert(!isoob(cell));
	m->map[(int)cell.x][(int)cell.y] = type;
}

void
settiles(Map *m, Point2 cell, int o, int ncells, int type)
{
	Point2 sv;

	assert(o == OH || o == OV);
	sv = o == OH? Vec2(1,0): Vec2(0,1);

	while(ncells-- > 0){
		assert(!isoob(cell));
		settile(m, cell, type);
		cell = addpt2(cell, sv);
	}
}

void
fprintmap(int fd, Map *m)
{
	int i, j;

	for(i = 0; i < MAPH; i++){
		fprint(fd, "\t");
		for(j = 0; j < MAPW; j++)
			switch(m->map[j][i]){
			case Twater: fprint(fd, "W"); break;
			case Tship: fprint(fd, "S"); break;
			case Thit: fprint(fd, "X"); break;
			case Tmiss: fprint(fd, "O"); break;
			default: fprint(fd, "?"); break;
			}
		fprint(fd, "\n");
	}
}

int
countshipcells(Map *m)
{
	int i, j, n;

	n = 0;
	for(i = 0; i < MAPW; i++)
		for(j = 0; j < MAPH; j++)
			if(gettile(m, Pt2(i,j,1)) == Tship)
				n++;
	return n;
}

int
shiplen(int stype)
{
	if(stype < 0 || stype >= nelem(shiplentab))
		return -1;
	return shiplentab[stype];
}

char *
shipname(int stype)
{
	if(stype < 0 || stype >= nelem(shipnametab))
		return nil;
	return shipnametab[stype];
}

char *
statename(int state)
{
	if(state < 0 || state > nelem(statenametab))
		return nil;
	return statenametab[state];
}

int
min(int a, int b)
{
	return a < b? a: b;
}

int
max(int a, int b)
{
	return a > b? a: b;
}

int
bitpackmap(uchar *buf, ulong len, Map *m)
{
	int i, j, off, n;

	assert(len >= BY2MAP);

	off = n = 0;
	*buf = 0;
	for(i = 0; i < MAPW; i++)
		for(j = 0; j < MAPH; j++){
			if(off >= 8){
				buf[++n] = 0;
				off = 0;
			}
			buf[n] |= (m->map[i][j] & TMASK) << off;
			off += TBITS;
		}
	return n+1;
}

int
bitunpackmap(Map *m, uchar *buf, ulong len)
{
	int i, j, off, n;

	assert(len >= BY2MAP);

	off = n = 0;
	for(i = 0; i < MAPW; i++)
		for(j = 0; j < MAPH; j++){
			if(off >= 8){
				n++;
				off = 0;
			}
			m->map[i][j] = buf[n] >> off & TMASK;
			off += TBITS;
		}
	return n+1;
}

int
chanvprint(Channel *c, char *fmt, va_list arg)
{
	char *p;
	int n;

	p = vsmprint(fmt, arg);
	if(p == nil)
		sysfatal("vsmprint failed: %r");
	n = sendp(c, p);
	yield();	/* let recipient handle message immediately */
	return n;
}

ulong
getrand(ulong max)
{
	mpint *n, *r;
	ulong c;

	n = uitomp(max, nil);
	r = mpnrand(n, genrandom, nil);
	c = mptoui(r);
	mpfree(n);
	mpfree(r);
	return c;
}