Skip to content

Commit

Permalink
Store scene walls in a hash table
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 13, 2024
1 parent 880ed5e commit d845646
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
11 changes: 5 additions & 6 deletions client.c3
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,6 @@ fn void render_other_players(SpritePool *sprite_pool, Image *player_image) {
fn void key_down(uint key_code) @extern("key_down") @wasm {
foreach (control: CONTROL_KEYS) {
if (control.key_code == key_code) {
io::printn(string::tformat("key_down %f %f", me.position.x, me.position.y));
Moving direction = control.moving;

if (!platform::is_offline_mode()) {
Expand Down Expand Up @@ -782,15 +781,15 @@ fn void render_game(float delta_time, float time) @extern("render_game") @wasm {
assert(asset);
Image player_image = {asset.width, asset.height, (Color*)&pack[asset.offset]};

update_all_players(common::scene, delta_time);
update_all_players(&common::scene, delta_time);
update_items(&sprite_pool, time, &common::items, &key_image, &bomb_image);
update_bombs_on_client_side(&sprite_pool, &particle_pool, &bomb_image, common::scene, delta_time, &common::bombs);
update_particles(&particle_image, &sprite_pool, delta_time, common::scene, &particle_pool);
update_bombs_on_client_side(&sprite_pool, &particle_pool, &bomb_image, &common::scene, delta_time, &common::bombs);
update_particles(&particle_image, &sprite_pool, delta_time, &common::scene, &particle_pool);

render_other_players(&sprite_pool, &player_image);

render_floor_and_ceiling(&display.image);
render_walls(&display.image, display.zbuffer, &wall_image, common::scene);
render_walls(&display.image, display.zbuffer, &wall_image, &common::scene);
cull_and_sort_sprites(&sprite_pool);
render_sprites(&display.image, display.zbuffer, &sprite_pool);

Expand Down Expand Up @@ -897,7 +896,7 @@ fn void entry() @init(2048) @private {
return buffer.len;
};
common::temp_mark = allocator::temp().used;
common::scene = common::allocate_default_scene();
common::load_default_scene();
}

module client::platform;
Expand Down
Binary file modified client.wasm
Binary file not shown.
44 changes: 18 additions & 26 deletions common.c3
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module common;
import std::math;
import std::io;
import std::collections::list;
import std::collections::map;
import std::hash::fnv32a;

def ShortString = char[64];

Expand All @@ -25,6 +27,7 @@ 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>];

Expand Down Expand Up @@ -66,50 +69,39 @@ struct Message @packed {
char[*] bytes;
}

/// Scene //////////////////////////////
/// Scenea //////////////////////////////

struct Scene {
usz width;
usz height;
bool[*] walls;
}

fn Scene *allocate_scene(usz width, usz height) {
Scene *scene = mem::calloc(Scene.sizeof + bool.sizeof*width*height);
scene.width = width;
scene.height = height;
for (usz i = 0; i < width*height; ++i) scene.walls[i] = false;
return scene;
HashMap(<IVector2, bool>) walls;
}

Scene *scene = null;

fn Scene *allocate_default_scene() {
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},
{ 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;
Scene *scene = allocate_scene(width, height);
for (usz y = 0; y < height; ++y) {
for (usz x = 0; x < width; ++x) {
scene.walls[y*width + x] = default_walls[y][x];
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]);
}
}
}
return scene;
}

fn bool Scene.get_tile(&scene, Vector2 p) {
int x = (int)math::floor(p.x);
int y = (int)math::floor(p.y);
if (!(0 <= x && x < scene.width && 0 <= y && y < scene.height)) return false;
return scene.walls[y*scene.width + x];
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) {
Expand Down
4 changes: 2 additions & 2 deletions server.c3
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ fn uint tick() @extern("tick") @wasm {
process_left_players();
process_moving_players();
process_thrown_bombs(&common::bombs);
process_world_simulation(&common::items, common::scene, &common::bombs, delta_time);
process_world_simulation(&common::items, &common::scene, &common::bombs, delta_time);
process_pings();

uint tickTime = platform::now_msecs() - timestamp;
Expand Down Expand Up @@ -468,7 +468,7 @@ fn void entry() @init(2048) @private {
};
stats::stats[StatEntry.UPTIME].timer.started_at = platform::now_secs();
common::temp_mark = allocator::temp().used;
common::scene = common::allocate_default_scene();
common::load_default_scene();
previous_timestamp = platform::now_msecs();
}

Expand Down
Binary file modified server.wasm
Binary file not shown.

0 comments on commit d845646

Please sign in to comment.