diff --git a/client.c3 b/client.c3 index bb29582..606d8df 100644 --- a/client.c3 +++ b/client.c3 @@ -863,7 +863,7 @@ fn void render_other_players(SpritePool *sprite_pool, Image *player_image) { }; } -fn void key_down(uint key_code, Bombs *bombs) @extern("key_down") @wasm { +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)); @@ -891,7 +891,7 @@ fn void key_down(uint key_code, Bombs *bombs) @extern("key_down") @wasm { message.kind = AMMA_THROWING; platform_send_message(message); } else { - common::throw_bomb(me.position.x, me.position.y, me.direction, bombs); + common::throw_bomb(me.position.x, me.position.y, me.direction, &common::bombs); } } } @@ -916,10 +916,10 @@ fn void key_up(uint key_code) @extern("key_up") @wasm { } } -fn void render_game(Image *display, float *zbuffer, SpritePool *sprite_pool, ParticlePool *particle_pool, Scene *scene, Bombs *bombs, Image *key_image, Image *bomb_image, Image *particle_image, Image *wall_image, Image *player_image, float delta_time, float time) @extern("render_game") @wasm { +fn void render_game(Image *display, float *zbuffer, SpritePool *sprite_pool, ParticlePool *particle_pool, Scene *scene, Image *key_image, Image *bomb_image, Image *particle_image, Image *wall_image, Image *player_image, float delta_time, float time) @extern("render_game") @wasm { update_all_players(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, scene, delta_time, bombs); + update_bombs_on_client_side(sprite_pool, particle_pool, bomb_image, scene, delta_time, &common::bombs); update_particles(particle_image, sprite_pool, delta_time, scene, particle_pool); render_other_players(sprite_pool, player_image); @@ -962,7 +962,7 @@ fn uint ping_msecs() @extern("ping_msecs") @wasm { return ping; } -fn bool process_message(Message *message, Bombs *bombs, ParticlePool *particle_pool) @extern("process_message") @wasm { +fn bool process_message(Message *message, ParticlePool *particle_pool) @extern("process_message") @wasm { // TODO: verify_* functions should return the typed message with exception, so we can turn this into sequence of if-try-s if (verify_hello_message(message)) { apply_hello_message_to_me((HelloMessage*)message, &common::items); @@ -985,11 +985,11 @@ fn bool process_message(Message *message, Bombs *bombs, ParticlePool *particle_p return false; } } else if (verify_bombs_spawned_batch_message(message)) { - if (!apply_bombs_spawned_batch_message_to_level_items((BombsSpawnedBatchMessage*)message, bombs)) { + if (!apply_bombs_spawned_batch_message_to_level_items((BombsSpawnedBatchMessage*)message, &common::bombs)) { return false; } } else if (verify_bombs_exploded_batch_message(message)) { - if (!apply_bombs_exploded_batch_message_to_level_items((BombsExplodedBatchMessage*)message, bombs, particle_pool)) { + if (!apply_bombs_exploded_batch_message_to_level_items((BombsExplodedBatchMessage*)message, &common::bombs, particle_pool)) { return false; } } else { diff --git a/client.mjs b/client.mjs index 7b8251b..f1693be 100644 --- a/client.mjs +++ b/client.mjs @@ -231,7 +231,7 @@ async function createGame() { return; } const eventDataPtr = common.arrayBufferAsMessageInWasm(wasmClient, event.data); - if (!game.wasmClient.process_message(eventDataPtr, game.level.bombsPtr, game.particlesPtr)) { + if (!game.wasmClient.process_message(eventDataPtr, game.particlesPtr)) { ws?.close(); return; } @@ -242,7 +242,7 @@ async function createGame() { return game; } function renderGame(display, deltaTime, time, game) { - game.wasmClient.render_game(display.backImagePtr, display.zBufferPtr, game.spritePoolPtr, game.particlesPtr, game.level.scenePtr, game.level.bombsPtr, game.assets.keyImagePtr, game.assets.bombImagePtr, game.assets.particleImagePtr, game.assets.wallImagePtr, game.assets.playerImagePtr, deltaTime, time); + game.wasmClient.render_game(display.backImagePtr, display.zBufferPtr, game.spritePoolPtr, game.particlesPtr, game.level.scenePtr, game.assets.keyImagePtr, game.assets.bombImagePtr, game.assets.particleImagePtr, game.assets.wallImagePtr, game.assets.playerImagePtr, deltaTime, time); displaySwapBackImageData(display, game.wasmClient); if (MINIMAP) game.wasmClient.render_minimap(display.minimapPtr, game.level.scenePtr, game.spritePoolPtr); @@ -252,7 +252,7 @@ function renderGame(display, deltaTime, time, game) { game = await createGame(); window.addEventListener("keydown", (e) => { if (!e.repeat) - game.wasmClient.key_down(e.keyCode, game.level.bombsPtr); + game.wasmClient.key_down(e.keyCode); }); window.addEventListener("keyup", (e) => { if (!e.repeat) diff --git a/client.mts b/client.mts index 8185ec3..77d8df5 100644 --- a/client.mts +++ b/client.mts @@ -68,12 +68,12 @@ interface WasmClient extends common.WasmCommon { image_pixels: (image: number) => number, players_count: () => number, unregister_all_other_players: () => void, - key_down: (key_code: number, bombs: number) => void, + key_down: (key_code: number) => void, key_up: (key_code: number) => void, - render_game: (display: number, zbuffer: number, sprite_pool: number, particle_pool: number, scene: number, bombs: number, key_image: number, bomb_image: number, particle_image: number, wall_image: number, player_image: number, delta_time: number, time: number) => void, + render_game: (display: number, zbuffer: number, sprite_pool: number, particle_pool: number, scene: number, key_image: number, bomb_image: number, particle_image: number, wall_image: number, player_image: number, delta_time: number, time: number) => void, ping_server_if_needed: () => void, ping_msecs: () => number, - process_message: (message: number, bombs: number, particle_pool: number) => boolean, + process_message: (message: number, particle_pool: number) => boolean, } function createDisplay(wasmClient: WasmClient, backImageWidth: number, backImageHeight: number): Display { @@ -236,7 +236,7 @@ async function instantiateWasmClient(url: string): Promise { unregister_all_other_players: wasm.instance.exports.unregister_all_other_players as () => void, key_down: wasm.instance.exports.key_down as (key_code: number) => void, key_up: wasm.instance.exports.key_up as (key_code: number) => void, - render_game: wasm.instance.exports.render_game as (display: number, zbuffer: number, sprite_pool: number, particle_pool: number, scene: number, bombs: number, key_image: number, bomb_image: number, particle_image: number, wall_image: number, player_image: number, delta_time: number, time: number) => void, + render_game: wasm.instance.exports.render_game as (display: number, zbuffer: number, sprite_pool: number, particle_pool: number, scene: number, key_image: number, bomb_image: number, particle_image: number, wall_image: number, player_image: number, delta_time: number, time: number) => void, ping_server_if_needed: wasm.instance.exports.ping_server_if_needed as () => void, ping_msecs: wasm.instance.exports.ping_msecs as () => number, process_message: wasm.instance.exports.process_message as (message: number) => boolean, @@ -315,7 +315,7 @@ async function createGame(): Promise { } const eventDataPtr = common.arrayBufferAsMessageInWasm(wasmClient, event.data); // console.log(`Received message from server`, new Uint8ClampedArray(event.data)); - if (!game.wasmClient.process_message(eventDataPtr, game.level.bombsPtr, game.particlesPtr)) { + if (!game.wasmClient.process_message(eventDataPtr, game.particlesPtr)) { ws?.close(); return; } @@ -328,7 +328,7 @@ async function createGame(): Promise { } function renderGame(display: Display, deltaTime: number, time: number, game: Game) { - game.wasmClient.render_game(display.backImagePtr, display.zBufferPtr, game.spritePoolPtr, game.particlesPtr, game.level.scenePtr, game.level.bombsPtr, game.assets.keyImagePtr, game.assets.bombImagePtr, game.assets.particleImagePtr, game.assets.wallImagePtr, game.assets.playerImagePtr, deltaTime, time); + game.wasmClient.render_game(display.backImagePtr, display.zBufferPtr, game.spritePoolPtr, game.particlesPtr, game.level.scenePtr, game.assets.keyImagePtr, game.assets.bombImagePtr, game.assets.particleImagePtr, game.assets.wallImagePtr, game.assets.playerImagePtr, deltaTime, time); displaySwapBackImageData(display, game.wasmClient); if (MINIMAP) game.wasmClient.render_minimap(display.minimapPtr, game.level.scenePtr, game.spritePoolPtr); @@ -339,7 +339,7 @@ function renderGame(display: Display, deltaTime: number, time: number, game: Gam game = await createGame(); window.addEventListener("keydown", (e) => { - if (!e.repeat) game.wasmClient.key_down(e.keyCode, game.level.bombsPtr); + if (!e.repeat) game.wasmClient.key_down(e.keyCode); }); // TODO: When the window loses the focus, reset all the controls window.addEventListener("keyup", (e) => { diff --git a/client.wasm b/client.wasm index 0bb17ac..974e163 100755 Binary files a/client.wasm and b/client.wasm differ diff --git a/common.c3 b/common.c3 index 49cdaad..820e6e1 100644 --- a/common.c3 +++ b/common.c3 @@ -222,6 +222,7 @@ struct Bomb { } def Bombs = Bomb[20]; +Bombs bombs; fn Bombs* allocate_bombs() @extern("allocate_bombs") @wasm { return mem::new(Bombs); diff --git a/common.mjs b/common.mjs index 32515d0..277f8b5 100644 --- a/common.mjs +++ b/common.mjs @@ -7,14 +7,12 @@ export function makeWasmCommon(wasm) { _initialize: wasm.instance.exports._initialize, reset_temp_mark: wasm.instance.exports.reset_temp_mark, allocate_temporary_buffer: wasm.instance.exports.allocate_temporary_buffer, - allocate_bombs: wasm.instance.exports.allocate_bombs, allocate_default_scene: wasm.instance.exports.allocate_default_scene, }; } export function createLevel(wasmCommon) { const scenePtr = wasmCommon.allocate_default_scene(); - const bombsPtr = wasmCommon.allocate_bombs(); - return { scenePtr, bombsPtr }; + return { scenePtr }; } export function arrayBufferAsMessageInWasm(wasmCommon, buffer) { const wasmBufferSize = buffer.byteLength + UINT32_SIZE; diff --git a/common.mts b/common.mts index 1e9389c..eab02ec 100644 --- a/common.mts +++ b/common.mts @@ -7,7 +7,6 @@ export interface WasmCommon { _initialize: () => void, reset_temp_mark: () => void, allocate_temporary_buffer: (size: number) => number, - allocate_bombs: () => number, allocate_default_scene: () => number, } @@ -18,7 +17,6 @@ export function makeWasmCommon(wasm: WebAssembly.WebAssemblyInstantiatedSource): _initialize: wasm.instance.exports._initialize as () => void, reset_temp_mark: wasm.instance.exports.reset_temp_mark as () => void, allocate_temporary_buffer: wasm.instance.exports.allocate_temporary_buffer as (size: number) => number, - allocate_bombs: wasm.instance.exports.allocate_bombs as () => number, allocate_default_scene: wasm.instance.exports.allocate_default_scene as () => number, } } @@ -27,13 +25,11 @@ export function makeWasmCommon(wasm: WebAssembly.WebAssemblyInstantiatedSource): // between Client and Server and constantly synced over the network. export interface Level { scenePtr: number, - bombsPtr: number, } export function createLevel(wasmCommon: WasmCommon): Level { const scenePtr = wasmCommon.allocate_default_scene(); - const bombsPtr = wasmCommon.allocate_bombs(); - return {scenePtr, bombsPtr}; + return {scenePtr}; } export function arrayBufferAsMessageInWasm(wasmCommon: WasmCommon, buffer: ArrayBuffer): number { diff --git a/server.c3 b/server.c3 index aabde9f..f62d4aa 100644 --- a/server.c3 +++ b/server.c3 @@ -578,7 +578,7 @@ fn bool verify_amma_moving_message(Message *message) { return true; } -fn bool process_message_on_server(uint id, Message *message, Bombs *bombs) @extern("process_message_on_server") @wasm { +fn bool process_message_on_server(uint id, Message *message) @extern("process_message_on_server") @wasm { stats_inc_counter(MESSAGES_RECEIVED, 1); messages_recieved_within_tick += 1; stats_inc_counter(BYTES_RECEIVED, message.size); @@ -586,7 +586,7 @@ fn bool process_message_on_server(uint id, Message *message, Bombs *bombs) @exte if (verify_amma_moving_message(message)) { player_update_moving(id, (AmmaMovingMessage*)message); } else if (verify_amma_throwing_message(message)) { - throw_bomb_on_server_side(id, bombs); + throw_bomb_on_server_side(id, &common::bombs); } else if (verify_ping_message(message)) { schedule_ping_for_player(id, (PingMessage*)message); } else { @@ -598,7 +598,7 @@ fn bool process_message_on_server(uint id, Message *message, Bombs *bombs) @exte } uint previous_timestamp = 0; -fn uint tick(Bombs *bombs, Scene *scene) @extern("tick") @wasm { +fn uint tick(Scene *scene) @extern("tick") @wasm { uint timestamp = platform_now_msecs(); float delta_time = (float)(timestamp - previous_timestamp)/1000.0f; previous_timestamp = timestamp; @@ -606,8 +606,8 @@ fn uint tick(Bombs *bombs, Scene *scene) @extern("tick") @wasm { process_joined_players(&common::items); process_left_players(); process_moving_players(); - process_thrown_bombs(bombs); - process_world_simulation(&common::items, scene, bombs, delta_time); + process_thrown_bombs(&common::bombs); + process_world_simulation(&common::items, scene, &common::bombs, delta_time); process_pings(); uint tickTime = platform_now_msecs() - timestamp; diff --git a/server.mjs b/server.mjs index 9d1cdf8..7d824d7 100644 --- a/server.mjs +++ b/server.mjs @@ -65,7 +65,7 @@ wss.on("connection", (ws, req) => { return; } const eventDataPtr = common.arrayBufferAsMessageInWasm(wasmServer, event.data); - if (!wasmServer.process_message_on_server(id, eventDataPtr, level.bombsPtr)) { + if (!wasmServer.process_message_on_server(id, eventDataPtr)) { ws.close(); return; } @@ -85,7 +85,7 @@ wss.on("connection", (ws, req) => { }); }); function tick() { - const tickTime = wasmServer.tick(level.bombsPtr, level.scenePtr); + const tickTime = wasmServer.tick(level.scenePtr); setTimeout(tick, Math.max(0, 1000 / SERVER_FPS - tickTime)); } function platform_now_secs() { diff --git a/server.mts b/server.mts index 61849f8..48a0e8c 100644 --- a/server.mts +++ b/server.mts @@ -83,7 +83,7 @@ wss.on("connection", (ws, req) => { } const eventDataPtr = common.arrayBufferAsMessageInWasm(wasmServer, event.data); // console.log(`Received message from player ${id}`, new Uint8ClampedArray(event.data)); - if (!wasmServer.process_message_on_server(id, eventDataPtr, level.bombsPtr)) { + if (!wasmServer.process_message_on_server(id, eventDataPtr)) { ws.close(); return; } @@ -104,7 +104,7 @@ wss.on("connection", (ws, req) => { }) function tick() { - const tickTime = wasmServer.tick(level.bombsPtr, level.scenePtr); + const tickTime = wasmServer.tick(level.scenePtr); setTimeout(tick, Math.max(0, 1000/SERVER_FPS - tickTime)); } @@ -112,8 +112,8 @@ interface WasmServer extends common.WasmCommon { stats_inc_counter: (entry: number, delta: number) => void, register_new_player: (id: number, x: number, y: number, hue: number) => void, unregister_player: (id: number) => void, - process_message_on_server: (id: number, message: number, bombs: number) => boolean, - tick: (bombs: number, scene: number) => number, + process_message_on_server: (id: number, message: number) => boolean, + tick: (scene: number) => number, } function platform_now_secs(): number { @@ -154,8 +154,8 @@ async function instantiateWasmServer(path: string): Promise { stats_inc_counter: wasm.instance.exports.stats_inc_counter as (entry: number) => void, register_new_player: wasm.instance.exports.register_new_player as (id: number, x: number, y: number, hue: number) => void, unregister_player: wasm.instance.exports.unregister_player as (id: number) => void, - process_message_on_server: wasm.instance.exports.process_message_on_server as (id: number, message: number, bombs: number) => boolean, - tick: wasm.instance.exports.tick as (bombs: number, scene: number) => number, + process_message_on_server: wasm.instance.exports.process_message_on_server as (id: number, message: number) => boolean, + tick: wasm.instance.exports.tick as (scene: number) => number, }; } diff --git a/server.wasm b/server.wasm index ad129c5..92e11a2 100755 Binary files a/server.wasm and b/server.wasm differ