-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcommon.c3
465 lines (396 loc) · 13.8 KB
/
common.c3
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
module common;
import std::math;
import std::io;
import std::collections::list;
import std::collections::map;
import std::hash::fnv32a;
def ShortString = char[64];
struct Asset {
String filename;
usz offset;
usz width;
usz height;
}
def Assets = List(<Asset>);
const float BOMB_GRAVITY = 10;
const float BOMB_LIFETIME = 2;
const float BOMB_THROW_VELOCITY = 5;
const float BOMB_DAMP = 0.8;
const float BOMB_SCALE = 0.25;
const float PLAYER_SIZE = 0.5;
const float PLAYER_RADIUS = 0.5;
const float PLAYER_SPEED = 2;
def Vector2 = float[<2>];
def IVector2 = int[<2>];
fn uint IVector2.hash(IVector2 self) => fnv32a::encode(@as_char_view(self));
def Vector3 = float[<3>];
def Vector4 = float[<4>];
macro Vector2.angle(self) => platform::atan2f(self.y, self.x);
// // TODO: math::atan2 is broken. Does not work with sprite_angle_index() properly.
// // Investigate what's up and potentially report to C3.
// macro Vector2.angle(self) => math::atan2(self.y, self.x);
fn Vector2 from_polar(float angle, float len) {
return {math::cos(angle)*len, math::sin(angle)*len};
}
// It's such mod that proper_mod(-1, 100) === 99
macro proper_mod(a, b) => (a%b + b)%b;
/// Messages //////////////////////////////
enum MessageKind: char {
HELLO,
PLAYER_JOINED,
PLAYER_LEFT,
PLAYER_MOVING,
AMMA_MOVING,
AMMA_THROWING,
PING,
PONG,
ITEM_SPAWNED,
ITEM_COLLECTED,
BOMB_SPAWNED,
BOMB_EXPLODED,
}
fault MessageFault {
INVALID
}
struct Message @packed {
uint byte_length;
char[*] bytes;
}
/// Scenea //////////////////////////////
struct Scene {
HashMap(<IVector2, bool>) walls;
}
Scene scene;
fn void load_default_scene() {
bool[*][*] default_walls = {
{ false, false, true, true, true, false, false},
{ false, false, false, false, false, true, false},
{ true, false, false, false, false, true, false},
{ true, false, false, false, false, true, false},
{ true, false, false, false, false, false, false},
{ false, true, true, true, false, false, false},
{ false, false, false, false, false, false, false},
};
usz width = default_walls[0].len;
usz height = default_walls.len;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (default_walls[y][x]) {
scene.walls.set({x, y}, default_walls[y][x]);
}
}
}
}
fn bool Scene.get_tile(&scene, Vector2 p) {
if (try tile = scene.walls.get((IVector2)math::floor(p))) {
return tile;
}
return false;
}
fn bool Scene.can_rectangle_fit_here(&scene, float px, float py, float sx, float sy) {
int x1 = (int)math::floor(px - sx*0.5f);
int x2 = (int)math::floor(px + sx*0.5f);
int y1 = (int)math::floor(py - sy*0.5f);
int y2 = (int)math::floor(py + sy*0.5f);
for (int x = x1; x <= x2; ++x) {
for (int y = y1; y <= y2; ++y) {
if (scene.get_tile({x, y})) {
return false;
}
}
}
return true;
}
/// Items //////////////////////////////
enum ItemKind: char {
KEY,
BOMB,
}
struct Item {
ItemKind kind;
bool alive;
Vector2 position;
}
Item[] items = {
{
.kind = ItemKind.BOMB,
.position = {1.5, 3.5},
.alive = true,
},
{
.kind = ItemKind.KEY,
.position = {2.5, 1.5},
.alive = true,
},
{
.kind = ItemKind.KEY,
.position = {3, 1.5},
.alive = true,
},
{
.kind = ItemKind.KEY,
.position = {3.5, 1.5},
.alive = true,
},
{
.kind = ItemKind.KEY,
.position = {4.0, 1.5},
.alive = true,
},
{
.kind = ItemKind.KEY,
.position = {4.5, 1.5},
.alive = true,
},
};
fn bool collect_item(Player player, Item *item) {
if (!item.alive) return false;
if (player.position.distance(item.position) >= PLAYER_RADIUS) return false;
item.alive = false;
return true;
}
struct ItemSpawned @packed {
ItemKind itemKind;
uint itemIndex;
float x;
float y;
}
def ItemsSpawnedBatchMessage = BatchMessage(<ItemSpawned, MessageKind.ITEM_SPAWNED>);
def verify_items_spawned_batch_message = msg::batch::verify(<ItemSpawned, MessageKind.ITEM_SPAWNED>);
def alloc_items_spawned_batch_message = msg::batch::alloc(<ItemSpawned, MessageKind.ITEM_SPAWNED>);
fn ItemsSpawnedBatchMessage* reconstruct_state_of_items(Item[] *items) {
usz itemsCount = 0;
foreach (&item: *items) {
if (item.alive) itemsCount += 1;
}
if (itemsCount == 0) return null;
ItemsSpawnedBatchMessage *message = alloc_items_spawned_batch_message(itemsCount);
usz index = 0;
foreach (itemIndex, item: *items) {
if (item.alive) {
message.payload[index] = {
.itemKind = item.kind,
.itemIndex = (int)itemIndex,
.x = item.position.x,
.y = item.position.y,
};
index += 1;
}
}
return message;
}
def ItemsCollectedBatchMessage = BatchMessage(<int, MessageKind.ITEM_COLLECTED>);
def verify_items_collected_batch_message = msg::batch::verify(<int, MessageKind.ITEM_COLLECTED>);
def alloc_items_collected_batch_message = msg::batch::alloc(<int, MessageKind.ITEM_COLLECTED>);
/// Bombs //////////////////////////////
struct Bomb {
Vector2 position;
float position_z;
Vector2 velocity;
float velocity_z;
float lifetime;
}
def Bombs = Bomb[20];
Bombs bombs;
fn int throw_bomb(Vector2 position, float direction, Bombs *bombs) {
foreach (index, &bomb: *bombs) {
if (bomb.lifetime <= 0) {
bomb.lifetime = BOMB_LIFETIME;
bomb.position = position;
bomb.position_z = 0.6;
bomb.velocity = from_polar(direction, 1.0f);
bomb.velocity_z = 0.5;
bomb.velocity *= BOMB_THROW_VELOCITY;
bomb.velocity_z *= BOMB_THROW_VELOCITY;
return (int)index;
}
}
return -1;
}
fn bool update_bomb(Bomb *bomb, Scene* scene, float delta_time) {
bool collided = false;
bomb.lifetime -= delta_time;
bomb.velocity_z -= BOMB_GRAVITY*delta_time;
float nx = bomb.position.x + bomb.velocity.x*delta_time;
float ny = bomb.position.y + bomb.velocity.y*delta_time;
if (scene.get_tile({nx, ny})) {
float dx = math::abs(math::floor(bomb.position.x) - math::floor(nx));
float dy = math::abs(math::floor(bomb.position.y) - math::floor(ny));
if (dx > 0) bomb.velocity.x *= -1;
if (dy > 0) bomb.velocity.y *= -1;
bomb.velocity *= BOMB_DAMP;
bomb.velocity_z *= BOMB_DAMP;
if (Vector3{bomb.velocity, bomb.velocity_z}.length() > 1) collided = true; // Wall collision
} else {
bomb.position.x = nx;
bomb.position.y = ny;
}
float nz = bomb.position_z + bomb.velocity_z*delta_time;
if (nz < BOMB_SCALE || nz > 1.0) {
bomb.velocity_z *= -1*BOMB_DAMP;
bomb.velocity *= BOMB_DAMP;
if (Vector3{bomb.velocity, bomb.velocity_z}.length() > 1) collided = true; // Floor collision
} else {
bomb.position_z = nz;
}
return collided;
}
struct BombSpawned @packed {
uint bombIndex;
float x;
float y;
float z;
float dx;
float dy;
float dz;
float lifetime;
}
def BombsSpawnedBatchMessage = BatchMessage(<BombSpawned, MessageKind.BOMB_SPAWNED>);
def verify_bombs_spawned_batch_message = msg::batch::verify(<BombSpawned, MessageKind.BOMB_SPAWNED>);
def alloc_bombs_spawned_batch_message = msg::batch::alloc(<BombSpawned, MessageKind.BOMB_SPAWNED>);
struct BombExploded @packed {
uint bombIndex;
float x;
float y;
float z;
}
def BombsExplodedBatchMessage = BatchMessage(<BombExploded, MessageKind.BOMB_EXPLODED>);
def verify_bombs_exploded_batch_message = msg::batch::verify(<BombExploded, MessageKind.BOMB_EXPLODED>);
def alloc_bombs_exploded_batch_message = msg::batch::alloc(<BombExploded, MessageKind.BOMB_EXPLODED>);
/// Player //////////////////////////////
enum Moving: char {
MOVING_FORWARD,
MOVING_BACKWARD,
TURNING_LEFT,
TURNING_RIGHT,
COUNT,
}
struct Player {
uint id;
Vector2 position;
float direction;
char moving;
char hue;
}
// NOTE: this struct intended to be part of the binary protocol to communicate the state of the player.
// This is why it is @packed. Do not confuse it with struct Player which is used to track the state of the player.
struct PlayerStruct @packed {
uint id;
float x;
float y;
float direction;
char hue;
char moving;
}
def PlayersJoinedBatchMessage = BatchMessage(<PlayerStruct, MessageKind.PLAYER_JOINED>);
def verify_players_joined_batch_message = msg::batch::verify(<PlayerStruct, MessageKind.PLAYER_JOINED>);
def alloc_players_joined_batch_message = msg::batch::alloc(<PlayerStruct, MessageKind.PLAYER_JOINED>);
def PlayersLeftBatchMessage = BatchMessage(<uint, MessageKind.PLAYER_LEFT>);
def verify_players_left_batch_message = msg::batch::verify(<uint, MessageKind.PLAYER_LEFT>);
def alloc_players_left_batch_message = msg::batch::alloc(<uint, MessageKind.PLAYER_LEFT>);
def PlayersMovingBatchMessage = BatchMessage(<PlayerStruct, MessageKind.PLAYER_MOVING>);
def verify_players_moving_batch_message = msg::batch::verify(<PlayerStruct, MessageKind.PLAYER_MOVING>);
def alloc_players_moving_batch_message = msg::batch::alloc(<PlayerStruct, MessageKind.PLAYER_MOVING>);
struct HelloPlayer @packed {
uint id;
float x;
float y;
float direction;
char hue;
}
def HelloMessage = SingleMessage(<HelloPlayer, MessageKind.HELLO>);
def verify_hello_message = msg::single::verify(<HelloPlayer, MessageKind.HELLO>);
def PongMessage = SingleMessage(<uint, MessageKind.PONG>);
def verify_pong_message = msg::single::verify(<uint, MessageKind.PONG>);
struct AmmaMoving @packed {
Moving direction;
char start;
}
def AmmaMovingMessage = SingleMessage(<AmmaMoving, MessageKind.AMMA_MOVING>);
def verify_amma_moving_message = msg::single::verify(<AmmaMoving, MessageKind.AMMA_MOVING>);
// TODO: EmptyMessage does not need Payload type, but since passing void is forbidden in C3, we must provide something.
// Providing uint in here is a completely arbitrary decision. Investigate how such things should be made properly.
def AmmaThrowingMessage = EmptyMessage(<MessageKind.AMMA_THROWING>);
def verify_amma_throwing_message = msg::empty::verify(<MessageKind.AMMA_THROWING>);
def PingMessage = SingleMessage(<uint, MessageKind.PING>);
def verify_ping_message = msg::single::verify(<uint, MessageKind.PING>);
fn void update_player(Player *player, Scene *scene, float delta_time) {
Vector2 control_velocity = {0, 0};
float angular_velocity = 0.0;
if ((player.moving>>(uint)Moving.MOVING_FORWARD)&1) {
control_velocity += from_polar(player.direction, PLAYER_SPEED);
}
if ((player.moving>>(uint)Moving.MOVING_BACKWARD)&1) {
control_velocity -= from_polar(player.direction, PLAYER_SPEED);
}
if ((player.moving>>(uint)Moving.TURNING_LEFT)&1) {
angular_velocity -= math::PI;
}
if ((player.moving>>(uint)Moving.TURNING_RIGHT)&1) {
angular_velocity += math::PI;
}
player.direction = (player.direction + angular_velocity*delta_time)%(2*(float)math::PI);
float nx = player.position.x + control_velocity.x*delta_time;
if (scene.can_rectangle_fit_here(nx, player.position.y, PLAYER_SIZE, PLAYER_SIZE)) {
player.position.x = nx;
}
float ny = player.position.y + control_velocity.y*delta_time;
if (scene.can_rectangle_fit_here(player.position.x, ny, PLAYER_SIZE, PLAYER_SIZE)) {
player.position.y = ny;
}
}
/// Temporary Memory //////////////////////////////
usz temp_mark = 0;
fn void reset_temp_mark() {
allocator::temp().reset(temp_mark);
}
fn void* allocate_temporary_buffer(usz size) @extern("allocate_temporary_buffer") @wasm {
return mem::tcalloc(size);
}
module common::msg::empty(<KIND>);
struct EmptyMessage @packed {
uint byte_length;
MessageKind kind;
}
fn EmptyMessage*! verify(Message *message) {
if (message.byte_length != EmptyMessage.sizeof) return MessageFault.INVALID?;
EmptyMessage *empty_message = (EmptyMessage*)message;
if (empty_message.kind != KIND) return MessageFault.INVALID?;
return empty_message;
}
module common::msg::single(<Payload, KIND>);
struct SingleMessage @packed {
uint byte_length;
MessageKind kind;
Payload payload;
}
fn SingleMessage*! verify(Message *message) {
if (message.byte_length != SingleMessage.sizeof) return MessageFault.INVALID?;
SingleMessage* single_message = (SingleMessage*)message;
if (single_message.kind != (MessageKind)KIND) return MessageFault.INVALID?;
return single_message;
}
module common::msg::batch(<Payload, KIND>);
struct BatchMessage @packed {
uint byte_length;
MessageKind kind;
Payload[*] payload;
}
macro uint BatchMessage.count(self) => (self.byte_length - BatchMessage.sizeof)/Payload.sizeof;
fn BatchMessage *alloc(usz count = 1, Allocator allocator = allocator::temp(), usz alignment = 0) {
usz byte_length = BatchMessage.sizeof + Payload.sizeof*count;
BatchMessage *message = allocator.acquire(byte_length, ZERO, alignment)!!;
message.byte_length = (int)byte_length;
message.kind = KIND;
return message;
}
fn BatchMessage*! verify(Message *message) {
if (message.byte_length < BatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - BatchMessage.sizeof)%Payload.sizeof != 0) return MessageFault.INVALID?;
BatchMessage* batch_message = (BatchMessage*)message;
if (batch_message.kind != (MessageKind)KIND) return MessageFault.INVALID?;
return batch_message;
}
module common::platform;
extern fn void write(void *buffer, usz buffer_sz) @extern("platform_write");
extern fn float atan2f(float y, float x) @extern("platform_atan2f");