Skip to content

Commit

Permalink
Tuck bombs under WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 5, 2024
1 parent 565f395 commit 0b1b731
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 38 deletions.
14 changes: 7 additions & 7 deletions client.c3
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions client.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -236,7 +236,7 @@ async function instantiateWasmClient(url: string): Promise<WasmClient> {
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,
Expand Down Expand Up @@ -315,7 +315,7 @@ async function createGame(): Promise<Game> {
}
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;
}
Expand All @@ -328,7 +328,7 @@ async function createGame(): Promise<Game> {
}

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);
Expand All @@ -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) => {
Expand Down
Binary file modified client.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions common.c3
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ struct Bomb {
}

def Bombs = Bomb[20];
Bombs bombs;

fn Bombs* allocate_bombs() @extern("allocate_bombs") @wasm {
return mem::new(Bombs);
Expand Down
4 changes: 1 addition & 3 deletions common.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions common.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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,
}
}
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions server.c3
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,15 @@ 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);
bytes_received_within_tick += message.size;
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 {
Expand All @@ -598,16 +598,16 @@ 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;

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;
Expand Down
4 changes: 2 additions & 2 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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() {
Expand Down
12 changes: 6 additions & 6 deletions server.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -104,16 +104,16 @@ 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));
}

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 {
Expand Down Expand Up @@ -154,8 +154,8 @@ async function instantiateWasmServer(path: string): Promise<WasmServer> {
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,
};
}

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

0 comments on commit 0b1b731

Please sign in to comment.