Skip to content

Commit efd2a65

Browse files
committed
[common] get rid of the HashMap in the scene implementation
1 parent 2d1ed7b commit efd2a65

7 files changed

Lines changed: 54 additions & 70 deletions

File tree

client.wasm

-3.19 KB
Binary file not shown.

src/client.c3

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -278,29 +278,29 @@ fn Vector2 ray_step(Vector2 p1, Vector2 p2) {
278278
return p3;
279279
}
280280

281-
fn Vector2 cast_ray(Scene *scene, Vector2 p1, Vector2 p2) {
281+
fn Vector2 cast_ray(Vector2 p1, Vector2 p2) {
282282
Vector2 start = p1;
283283
while (vector2::distance(start, p1) < FAR_CLIPPING_PLANE) {
284284
Vector2 c = hitting_cell(p1, p2);
285-
if (scene.get_tile(c)) break;
285+
if (common::scene_get_tile(c)) break;
286286
Vector2 p3 = ray_step(p1, p2);
287287
p1 = p2;
288288
p2 = p3;
289289
}
290290
return p2;
291291
}
292292

293-
fn void render_walls(Image *display, float *zbuffer, Image *wall, Scene *scene) {
293+
fn void render_walls(Image *display, float *zbuffer, Image *wall) {
294294
Camera camera = { .position = {me.position.x, me.position.y}, .direction = me.direction };
295295
camera.update();
296296

297297
Vector2 d = vector2::from_polar(camera.direction, 1.0f);
298298
for (int x = 0; x < display.width; ++x) {
299-
Vector2 p = cast_ray(scene, camera.position, vector2::lerp(camera.fovLeft, camera.fovRight, (float)x/display.width));
299+
Vector2 p = cast_ray(camera.position, vector2::lerp(camera.fovLeft, camera.fovRight, (float)x/display.width));
300300
Vector2 c = hitting_cell(camera.position, p);
301301
Vector2 v = vector2::sub(p, camera.position);
302302
zbuffer[x] = vector2::dot(v, d);
303-
if (scene.get_tile(c)) render_column_of_wall(display, zbuffer, wall, x, p, c);
303+
if (common::scene_get_tile(c)) render_column_of_wall(display, zbuffer, wall, x, p, c);
304304
}
305305
}
306306

@@ -414,14 +414,14 @@ fn void emit_particle(Vector3 source, ParticlePool *particle_pool) {
414414
}
415415
}
416416

417-
fn void update_particles(Image *image, SpritePool *sprite_pool, float deltaTime, Scene *scene, ParticlePool *particle_pool) {
417+
fn void update_particles(Image *image, SpritePool *sprite_pool, float deltaTime, ParticlePool *particle_pool) {
418418
foreach (&particle: particle_pool.items) {
419419
if (particle.lifetime > 0) {
420420
particle.lifetime -= deltaTime;
421421
particle.velocity_z -= common::BOMB_GRAVITY*deltaTime;
422422

423423
Vector2 new_position = vector2::add(particle.position, vector2::mul(particle.velocity, vector2::xx(deltaTime)));
424-
if (scene.get_tile(new_position)) {
424+
if (common::scene_get_tile(new_position)) {
425425
float dx = math::abs(math::floor(particle.position.x) - math::floor(new_position.x));
426426
float dy = math::abs(math::floor(particle.position.y) - math::floor(new_position.y));
427427

@@ -539,12 +539,12 @@ fn void explode_bomb(Vector3 bomb_position, Vector2 player_position, ParticlePoo
539539
}
540540
}
541541

542-
fn void update_bombs_on_client_side(SpritePool *sprite_pool, ParticlePool *particle_pool, Image *bomb_image, Scene *scene, float delta_time, Bombs *bombs) {
542+
fn void update_bombs_on_client_side(SpritePool *sprite_pool, ParticlePool *particle_pool, Image *bomb_image, float delta_time, Bombs *bombs) {
543543
foreach (&bomb: *bombs) {
544544
if (bomb.lifetime > 0) {
545545
push_sprite(sprite_pool, bomb_image, {bomb.position.x, bomb.position.y, bomb.position_z}, common::BOMB_SCALE, {0, 0}, {bomb_image.width, bomb_image.height});
546546

547-
if (common::update_bomb(bomb, scene, delta_time)) {
547+
if (common::update_bomb(bomb, delta_time)) {
548548
platform::play_sound(BOMB_RICOCHET, me.position.x, me.position.y, bomb.position.x, bomb.position.y);
549549
}
550550

@@ -699,11 +699,11 @@ fn uint sprite_angle_index(Vector2 camera_position, Player entity) {
699699
return (uint)math::floor(common::proper_mod(common::proper_mod(entity.direction, TAU) - common::proper_mod(vector2::sub(entity.position, camera_position).angle_fixed(), TAU) - PI + PI/8, TAU)/TAU*SPRITE_ANGLES_COUNT);
700700
}
701701

702-
fn void update_all_players(Scene *scene, float delta_time) {
702+
fn void update_all_players(float delta_time) {
703703
other_players.@each_entry(; OtherPlayersEntry *entry) {
704-
common::update_player(&entry.value, scene, delta_time);
704+
common::update_player(&entry.value, delta_time);
705705
};
706-
common::update_player(&me, scene, delta_time);
706+
common::update_player(&me, delta_time);
707707
}
708708

709709
fn void render_other_players(SpritePool *sprite_pool, Image *player_image) {
@@ -801,15 +801,15 @@ fn void render_game(float delta_time, float time) @extern("render_game") @wasm {
801801
assert(asset);
802802
Image player_image = {asset.width, asset.height, (Color*)&pack[asset.offset]};
803803

804-
update_all_players(&common::scene, delta_time);
804+
update_all_players(delta_time);
805805
update_items(&sprite_pool, time, common::items_ptr(), common::items_len(), &key_image, &bomb_image);
806-
update_bombs_on_client_side(&sprite_pool, &particle_pool, &bomb_image, &common::scene, delta_time, &common::bombs);
807-
update_particles(&particle_image, &sprite_pool, delta_time, &common::scene, &particle_pool);
806+
update_bombs_on_client_side(&sprite_pool, &particle_pool, &bomb_image, delta_time, &common::bombs);
807+
update_particles(&particle_image, &sprite_pool, delta_time, &particle_pool);
808808

809809
render_other_players(&sprite_pool, &player_image);
810810

811811
render_floor_and_ceiling(&display.image);
812-
render_walls(&display.image, display.zbuffer, &wall_image, &common::scene);
812+
render_walls(&display.image, display.zbuffer, &wall_image);
813813
cull_and_sort_sprites(&sprite_pool);
814814
render_sprites(&display.image, display.zbuffer, &sprite_pool);
815815

@@ -906,7 +906,6 @@ fn void entry() @init(2048) @private {
906906
return buffer.len;
907907
};
908908
common::temp_mark = allocator::temp().used;
909-
common::load_default_scene();
910909
}
911910

912911
module client::platform;

src/common.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ int throw_bomb(Vector2 position, float direction, Bombs *bombs) {
161161

162162
// Player //////////////////////////////
163163

164-
void update_player(Player *player, Scene *scene, float delta_time) {
164+
void update_player(Player *player, float delta_time) {
165165
Vector2 control_velocity = {0, 0};
166166
float angular_velocity = 0.0;
167167
if ((player->moving>>(uint32_t)MOVING_FORWARD)&1) {
@@ -179,11 +179,11 @@ void update_player(Player *player, Scene *scene, float delta_time) {
179179
player->direction = __builtin_fmodf(player->direction + angular_velocity*delta_time, 2*PI);
180180

181181
float nx = player->position.x + control_velocity.x*delta_time;
182-
if (scene_can_rectangle_fit_here(scene, nx, player->position.y, PLAYER_SIZE, PLAYER_SIZE)) {
182+
if (scene_can_rectangle_fit_here(nx, player->position.y, PLAYER_SIZE, PLAYER_SIZE)) {
183183
player->position.x = nx;
184184
}
185185
float ny = player->position.y + control_velocity.y*delta_time;
186-
if (scene_can_rectangle_fit_here(scene, player->position.x, ny, PLAYER_SIZE, PLAYER_SIZE)) {
186+
if (scene_can_rectangle_fit_here(player->position.x, ny, PLAYER_SIZE, PLAYER_SIZE)) {
187187
player->position.y = ny;
188188
}
189189
}

src/common.c3

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -57,47 +57,33 @@ struct Message @packed {
5757

5858
/// Scene //////////////////////////////
5959

60-
struct Scene {
61-
HashMap(<IVector2, bool>) walls;
62-
}
63-
64-
Scene scene;
65-
fn void load_default_scene() {
66-
bool[*][*] default_walls = {
67-
{ false, false, true, true, true, false, false},
68-
{ false, false, false, false, false, true, false},
69-
{ true, false, false, false, false, true, false},
70-
{ true, false, false, false, false, true, false},
71-
{ true, false, false, false, false, false, false},
72-
{ false, true, true, true, false, false, false},
73-
{ false, false, false, false, false, false, false},
74-
};
75-
usz width = default_walls[0].len;
76-
usz height = default_walls.len;
77-
for (int y = 0; y < height; ++y) {
78-
for (int x = 0; x < width; ++x) {
79-
if (default_walls[y][x]) {
80-
scene.walls.set({x, y}, default_walls[y][x]);
81-
}
82-
}
83-
}
84-
}
85-
86-
fn bool Scene.get_tile(&scene, Vector2 p) {
87-
if (try tile = scene.walls.get(ivector2::from_vector2(vector2::floor(p)))) {
88-
return tile;
89-
}
90-
return false;
91-
}
92-
93-
fn bool scene_can_rectangle_fit_here(Scene *scene, float px, float py, float sx, float sy) @export("scene_can_rectangle_fit_here") {
60+
const usz WALLS_WIDTH = 7;
61+
const usz WALLS_HEIGHT = 7;
62+
bool[WALLS_HEIGHT][WALLS_WIDTH] walls = {
63+
{ false, false, true, true, true, false, false},
64+
{ false, false, false, false, false, true, false},
65+
{ true, false, false, false, false, true, false},
66+
{ true, false, false, false, false, true, false},
67+
{ true, false, false, false, false, false, false},
68+
{ false, true, true, true, false, false, false},
69+
{ false, false, false, false, false, false, false},
70+
};
71+
72+
fn bool scene_get_tile(Vector2 p) {
73+
IVector2 ip = ivector2::from_vector2(vector2::floor(p));
74+
if (!(0 <= ip.x && ip.x < WALLS_WIDTH)) return false;
75+
if (!(0 <= ip.y && ip.y < WALLS_HEIGHT)) return false;
76+
return walls[ip.y][ip.x];
77+
}
78+
79+
fn bool scene_can_rectangle_fit_here(float px, float py, float sx, float sy) @export("scene_can_rectangle_fit_here") {
9480
int x1 = (int)math::floor(px - sx*0.5f);
9581
int x2 = (int)math::floor(px + sx*0.5f);
9682
int y1 = (int)math::floor(py - sy*0.5f);
9783
int y2 = (int)math::floor(py + sy*0.5f);
9884
for (int x = x1; x <= x2; ++x) {
9985
for (int y = y1; y <= y2; ++y) {
100-
if (scene.get_tile({x, y})) {
86+
if (scene_get_tile({x, y})) {
10187
return false;
10288
}
10389
}
@@ -177,14 +163,14 @@ fn int throw_bomb(Vector2 position, float direction, Bombs *bombs) {
177163
return -1;
178164
}
179165

180-
fn bool update_bomb(Bomb *bomb, Scene* scene, float delta_time) @export("update_bomb") {
166+
fn bool update_bomb(Bomb *bomb, float delta_time) @export("update_bomb") {
181167
bool collided = false;
182168
bomb.lifetime -= delta_time;
183169
bomb.velocity_z -= BOMB_GRAVITY*delta_time;
184170

185171
float nx = bomb.position.x + bomb.velocity.x*delta_time;
186172
float ny = bomb.position.y + bomb.velocity.y*delta_time;
187-
if (scene.get_tile({nx, ny})) {
173+
if (scene_get_tile({nx, ny})) {
188174
float dx = math::abs(math::floor(bomb.position.x) - math::floor(nx));
189175
float dy = math::abs(math::floor(bomb.position.y) - math::floor(ny));
190176

@@ -345,7 +331,7 @@ struct PingMessage @packed {
345331
}
346332
macro verify_ping_message(message) => msg::batch::verify(MessageKind.PING, message, uint.sizeof);
347333

348-
extern fn void update_player(Player *player, Scene *scene, float delta_time) @extern("update_player");
334+
extern fn void update_player(Player *player, float delta_time) @extern("update_player");
349335

350336
/// Temporary Memory //////////////////////////////
351337

src/common.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef struct {
5858

5959
typedef void Scene;
6060

61-
bool scene_can_rectangle_fit_here(Scene *scene, float px, float py, float sx, float sy); // Implemented in C3
61+
bool scene_can_rectangle_fit_here(float px, float py, float sx, float sy); // Implemented in C3
6262

6363
// Player //////////////////////////////
6464

@@ -78,7 +78,7 @@ typedef struct {
7878
uint8_t hue;
7979
} Player;
8080

81-
void update_player(Player *player, Scene *scene, float delta_time);
81+
void update_player(Player *player, float delta_time);
8282

8383
// Items //////////////////////////////
8484

@@ -116,7 +116,7 @@ typedef struct {
116116
extern Bombs bombs; // Implemented in C3
117117

118118
int throw_bomb(Vector2 position, float direction, Bombs *bombs);
119-
bool update_bomb(Bomb *bomb, Scene *scene, float delta_time); // Implemented in C3
119+
bool update_bomb(Bomb *bomb, float delta_time); // Implemented in C3
120120

121121
// Messages //////////////////////////////
122122

src/server.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,11 @@ BombsSpawnedBatchMessage *thrown_bombs_as_batch_message(Bombs *bombs) {
385385

386386
Indices exploded_bombs = {0};
387387

388-
void update_bombs_on_server_side(Scene *scene, float delta_time, Bombs *bombs) {
388+
void update_bombs_on_server_side(float delta_time, Bombs *bombs) {
389389
for (size_t bombIndex = 0; bombIndex < BOMBS_CAPACITY; ++bombIndex) {
390390
Bomb *bomb = &bombs->items[bombIndex];
391391
if (bomb->lifetime > 0) {
392-
update_bomb(bomb, scene, delta_time);
392+
update_bomb(bomb, delta_time);
393393
if (bomb->lifetime <= 0) {
394394
da_append(&exploded_bombs, bombIndex);
395395
}
@@ -426,11 +426,11 @@ void process_thrown_bombs(Bombs *bombs) {
426426

427427
// World //////////////////////////////
428428

429-
void process_world_simulation(Item *items, size_t items_len, Scene *scene, Bombs *bombs, float delta_time) {
429+
void process_world_simulation(Item *items, size_t items_len, Bombs *bombs, float delta_time) {
430430
// Simulating the world for one server tick.
431431
for (ptrdiff_t i = 0; i < hmlen(players); ++i) {
432432
PlayerOnServerEntry* entry = &players[i];
433-
update_player(&entry->value.player, scene, delta_time);
433+
update_player(&entry->value.player, delta_time);
434434
collect_items_by_player(entry->value.player, items, items_len);
435435
}
436436

@@ -442,7 +442,7 @@ void process_world_simulation(Item *items, size_t items_len, Scene *scene, Bombs
442442
}
443443
}
444444

445-
update_bombs_on_server_side(scene, delta_time, bombs);
445+
update_bombs_on_server_side(delta_time, bombs);
446446
BombsExplodedBatchMessage *bombs_exploded_batch_message = exploded_bombs_as_batch_message(bombs);
447447
if (bombs_exploded_batch_message) {
448448
for (ptrdiff_t i = 0; i < hmlen(players); ++i) {

src/server.c3

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern fn ItemsCollectedBatchMessage *collected_items_as_batch_message() @extern
3333

3434
extern fn void throw_bomb_on_server_side(uint player_id, Bombs *bombs) @extern("throw_bomb_on_server_side");
3535
extern fn BombsSpawnedBatchMessage *thrown_bombs_as_batch_message(Bombs *bombs) @extern("thrown_bombs_as_batch_message");
36-
extern fn void update_bombs_on_server_side(Scene *scene, float delta_time, Bombs *bombs) @extern("update_bombs_on_server_side");
36+
extern fn void update_bombs_on_server_side(float delta_time, Bombs *bombs) @extern("update_bombs_on_server_side");
3737
extern fn BombsExplodedBatchMessage* exploded_bombs_as_batch_message(Bombs* bombs) @extern("exploded_bombs_as_batch_message");
3838

3939
/// Player //////////////////////////////
@@ -70,7 +70,7 @@ extern fn void process_left_players() @extern("process_left_players");
7070
extern fn void process_moving_players() @extern("process_moving_players");
7171
extern fn void process_thrown_bombs(Bombs *bombs) @extern("process_thrown_bombs");
7272

73-
extern fn void process_world_simulation(Item *items, usz items_len, Scene *scene, Bombs *bombs, float delta_time) @extern("process_world_simulation");
73+
extern fn void process_world_simulation(Item *items, usz items_len, Bombs *bombs, float delta_time) @extern("process_world_simulation");
7474

7575
extern fn void process_pings() @extern("process_pings");
7676
extern fn void clear_intermediate_ids() @extern("clear_intermediate_ids");
@@ -85,7 +85,7 @@ fn uint tick() @extern("tick") @wasm {
8585
process_left_players();
8686
process_moving_players();
8787
process_thrown_bombs(&common::bombs);
88-
process_world_simulation(common::items_ptr(), common::items_len(), &common::scene, &common::bombs, delta_time);
88+
process_world_simulation(common::items_ptr(), common::items_len(), &common::bombs, delta_time);
8989
process_pings();
9090

9191
uint tickTime = now_msecs() - timestamp;
@@ -125,7 +125,6 @@ fn int main() {
125125

126126
stats::start_timer_at(StatEntry.UPTIME, now_msecs());
127127
common::temp_mark = allocator::temp().used;
128-
common::load_default_scene();
129128
server::previous_timestamp = now_msecs();
130129

131130
const String HOST = "0.0.0.0";

0 commit comments

Comments
 (0)