aboutsummaryrefslogtreecommitdiff
path: root/dat.h
blob: 3e8e7039bf793253b723abb4d629e7d59ef98f9d (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
typedef enum {
	K↑,
	K↺,
	K↻,
	Kfire,
	Khyper,
	Ksay,
	Kquit,
	NKEYOPS
} KeyOp;

typedef enum {
	NEEDLE,
	WEDGE
} Kind;

typedef enum {
	NCSDisconnected,
	NCSConnecting,
	NCSConnected
} NCState;

enum {
	SCRW	= 640,
	SCRH	= 480,
	SCRWB	= SCRW+2*Borderwidth,
	SCRHB	= SCRH+2*Borderwidth
};

enum {
	NChi		= 10,	/* C wants to connect */
	NShi,			/* S accepts. sends P and G for DHX */
	NCdhx		= 12,	/* C shares pubkey */
	NSdhx,			/* S shares pubkey */
	NCnudge		= 16,
	NSnudge,		/* check the pulse of the line */

	NCinput		= 20,	/* C sends player input state */
	NSsimstate,		/* S sends current simulation state */

	NCbuhbye	= 30,
	NSbuhbye,

	NSerror 	= 66	/* report an error */
};

enum {
	ProtocolID	= 0x5753554d,	/* MUSW */
	Framehdrsize	= 4+1+4+4+2+MD5dlen,
	MTU		= 1024,
	ConnTimeout	= 10000		/* in ms */
};

enum {
	THRUST = 10
};

typedef struct VModel VModel;
typedef struct Sprite Sprite;
typedef struct Keymap Keymap;
typedef struct State State;
typedef struct Particle Particle;
typedef struct Bullet Bullet;
typedef struct Ship Ship;
typedef struct Star Star;
typedef struct Universe Universe;
typedef struct Derivative Derivative;

typedef struct Frame Frame;
typedef struct DHparams DHparams;
typedef struct NetConn NetConn;
typedef struct Player Player;
typedef struct Playerq Playerq;
typedef struct Party Party;

/*
 * Vector model - made out of lines and curves
 */
struct VModel
{
	Point2 *pts;
	usize npts;
	/* WIP
	 * l(ine) → takes 2 points
	 * c(urve) → takes 3 points
	 */
	char *strokefmt;
};

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);
};

struct Keymap
{
	Rune key;
	KeyOp op;
};

struct State
{
	char *name;

	State *(*δ)(State*, void*); /* transition fn */
};

struct Particle
{
	Point2 p, v;
	double θ, ω;
	double mass; /* in kg */
};

struct Bullet
{
	Particle;
	double ttl; /* in s */
	int fired; /* XXX: |v| != 0 */
};

struct Ship
{
	Particle;
	Kind kind;
	int fuel;
	Bullet rounds[10];
	VModel *mdl;
	Matrix mdlxform;

	void (*forward)(Ship*, double);
	void (*rotate)(Ship*, int, double);
	void (*hyperjump)(Ship*);
	void (*fire)(Ship*);
};

struct Star
{
	Particle;
	Sprite *spr;
};

struct Universe
{
	Ship ships[2];
	Star star;
	double t, timeacc;

	void (*step)(Universe*, double);
	void (*collide)(Universe*);
	void (*reset)(Universe*);
};

struct Derivative
{
	Point2 dx; /* v */
	Point2 dv; /* a */
};

struct Frame
{
	Udphdr udp;
	u32int id;	/* ProtocolID */
	u8int type;
	u32int seq;
	u32int ack;
	u16int len;
	uchar sig[MD5dlen];
	uchar data[];
};

struct DHparams
{
	ulong p, g, pub, sec, priv;
};

struct NetConn
{
	Udphdr udp;
	DHparams dh;
	NCState state;
	u32int lastseq;
	u32int lastack;
	ulong lastrecvts;	/* last time a packet was received (in ms) */
	ulong lastnudgets;	/* last time a nudge was sent (in ms) */
	Player *player;
};

struct Player
{
	char *name;
	NetConn *conn;
	ulong oldkdown, kdown;
	Player *next;
};

struct Playerq
{
	Player *head, *tail;
	usize len;

	void (*put)(Playerq*, Player*);
	Player *(*get)(Playerq*);
	void (*del)(Playerq*, Player*);
};

struct Party
{
	Player *players[2];	/* the needle and the wedge */
	Universe *u;
	Party *prev, *next;
};

#pragma varargck type "Φ" Frame*