aboutsummaryrefslogtreecommitdiff
path: root/dat.h
blob: d645d6f94fbd07627a65e13a06badfc5da08c6b7 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#define SEC	1000000000ULL

enum {
	Twater,
	Tship,
	Thit,
	Tmiss,
	NTILES,

	TBITS = 2,	/* ceil(log(NTILES)/log(2)) */
	TMASK = (1<<TBITS) - 1,

	Scarrier = 0,
	Sbattleship,
	Scruiser,
	Ssubmarine,
	Sdestroyer,
	NSHIPS,

	VFXHit = 0,
	VFXMiss,
	NVFX,

	OH = 0,		/* horizontal */
	OV,		/* vertical */

	GMPvP = 0,
	GMPvAI,

	Waiting0 = 0,
	Watching,
	Ready,
	Outlaying,
	Waiting,
	Playing,

	ASearching = 0,
	ACalibrating,
	ABombing,

	BRest = 0,
	BHover,

	Boardmargin = 50,
	Btnborder = 4,
	Btnpadding = 2,
	TW = 16,
	TH = TW,
	MAPW = 17,
	MAPH = MAPW,
	SCRW = Boardmargin + Borderwidth+MAPW*TW+Borderwidth + Boardmargin,
	SCRH = Boardmargin+
		Borderwidth+MAPH*TH+Borderwidth+
		TH+
		Borderwidth+MAPH*TH+Borderwidth+
		Boardmargin,

	SBG0 = 0,
	SBG1,
	SBG2,
	SCANNON,
	SWATER,
	SVICTORY,
	SDEFEAT,
	NSOUNDS,

	KB = 1024,
	BY2MAP = (TBITS*MAPW*MAPH+7)/8,
};

typedef struct Ship Ship;
typedef struct Map Map;
typedef struct Board Board;
typedef struct Chanpipe Chanpipe;
typedef struct Player Player;
typedef struct Andy Andy;
typedef struct Match Match;
typedef struct Msg Msg;
typedef struct Stands Stands;
typedef struct MatchInfo MatchInfo;

struct Ship
{
	Point2 p;	/* board cell */
	Rectangle bbox;
	int orient;
	int ncells;
	int *hit;	/* |hit| = ncells and hitᵢ ∈ {0,1} */
};

struct Map
{
	char map[MAPW][MAPH];
};

struct Board
{
	RFrame;
	Map;
	Rectangle bbox;
};

struct Chanpipe
{
	Channel *in;
	Channel *out;
	Channel *ctl;
	int fd;
};

struct Player
{
	Map;
	char name[8+1];
	int state;
	int gamemode;
	Match *battle;
	NetConnInfo *nci;
	Chanpipe io;
	Channel *ctl;
};

struct Andy
{
	Map;		/* of the enemy */
	Player *ego;
	int state;
	Point2 lastshot;
	Point2 firsthit;
	Point2 passdir;	/* direction of current pass */
	int ntries;	/* attempts left to find the direction */
	int passes;	/* remaining passes (one per direction) */

	void (*layout)(Andy*, Msg*);
	void (*shoot)(Andy*, Msg*);
	void (*engage)(Andy*);
	void (*disengage)(Andy*);
	void (*registerhit)(Andy*);
	void (*registermiss)(Andy*);
};

struct Match
{
	RWLock;
	int id;
	Player *pl[2];
	Channel *data;
	Channel *ctl;
	Match *prev;
	Match *next;
};

struct Msg
{
	Player *from;
	char *body;
};

struct Stands
{
	Player **seats;
	ulong nused;
	ulong cap;
};

struct MatchInfo
{
	int id;
	struct {
		char uid[8+1];
		int state;
	} pl[2];
	Board *bl[2];
	char conclusion[16];
};

typedef struct Button Button;

struct Button
{
	char *label;
	Rectangle r;
	int status;
	void (*handler)(Button*);
};

typedef struct Sprite Sprite;
typedef struct Vfx Vfx;

struct Sprite
{
	Image *sheet;
	Point sp;
	Rectangle r;
	int nframes;
	int curframe;
	ulong period;
	ulong elapsed;

	void (*step)(Sprite*, ulong);
	void (*draw)(Sprite*, Image*, Point);
	Sprite *(*clone)(Sprite*);
};

struct Vfx
{
	Sprite *a;	/* animation */
	Point p;
	int times;	/* to repeat. -1 loops forever */
	Vfx *prev, *next;

	void (*step)(Vfx*, ulong);
	void (*draw)(Vfx*, Image*);
};

typedef struct Mentry Mentry;
typedef struct Mlist Mlist;
typedef struct Menulist Menulist;

struct Mentry
{
	int id;
	char *title;
};

struct Mlist
{
	Mentry *entries;
	int nentries;
	int filling;	/* lock-alike */
};

struct Menulist
{
	Mlist;
	char *title;
	Rectangle r, sr;	/* content and scroll rects */
	int high;		/* [-1,nitems) where -1 is none */
	int off;		/* entry offset ∈ [0, nitems-Maxvisitems] */

	void (*add)(Menulist*, int, char*);
	void (*clear)(Menulist*);
	int (*update)(Menulist*, Mousectl*, Channel*);
	void (*draw)(Menulist*, Image*);
};

/*
 * Kernel-style command parser
 */
typedef struct Cmdbuf Cmdbuf;
typedef struct Cmdtab Cmdtab;

struct Cmdbuf
{
	char	*buf;
	char	**f;
	int	nf;
};

struct Cmdtab
{
	int	index;	/* used by client to switch on result */
	char	*cmd;	/* command name */
	int	narg;	/* expected #args; 0 ==> variadic */
};