Skip to content

Commit

Permalink
[server] tuck exploded bombs under server.wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 1, 2024
1 parent 44d70df commit 46c431a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
12 changes: 4 additions & 8 deletions server.c3
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,9 @@ fn BombsSpawnedBatchMessage *thrown_bombs_as_batch_message(Bombs *bombs) @extern
return message;
}

def ExplodedBombs = List(<usz>);
List(<usz>) exploded_bombs;

fn ExplodedBombs *allocate_exploded_bombs() @extern("allocate_exploded_bombs") @wasm {
return mem::new(ExplodedBombs);
}

fn void update_bombs_on_server_side(Scene *scene, float delta_time, ExplodedBombs *exploded_bombs, Bombs *bombs) @extern("update_bombs_on_server_side") @wasm {
fn void update_bombs_on_server_side(Scene *scene, float delta_time, Bombs *bombs) @extern("update_bombs_on_server_side") @wasm {
foreach (bombIndex, &bomb: *bombs) {
if (bomb.lifetime > 0) {
common::update_bomb(bomb, scene, delta_time);
Expand All @@ -262,14 +258,14 @@ fn void update_bombs_on_server_side(Scene *scene, float delta_time, ExplodedBomb
}
}

fn BombsExplodedBatchMessage* exploded_bombs_as_batch_message(ExplodedBombs* exploded_bombs, Bombs* bombs) @extern("exploded_bombs_as_batch_message") @wasm {
fn BombsExplodedBatchMessage* exploded_bombs_as_batch_message(Bombs* bombs) @extern("exploded_bombs_as_batch_message") @wasm {
if (exploded_bombs.size == 0) return null;
int size = BombsExplodedBatchMessage.sizeof + BombExploded.sizeof*exploded_bombs.size;
BombsExplodedBatchMessage *message = mem::tcalloc(size);
message.size = size;
message.kind = MessageKind.BOMB_EXPLODED;

foreach (index, bombIndex: *exploded_bombs) {
foreach (index, bombIndex: exploded_bombs) {
Bomb bomb = (*bombs)[bombIndex];
message.bombs[index].bombIndex = bombIndex;
message.bombs[index].x = bomb.position.x;
Expand Down
6 changes: 2 additions & 4 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const joinedIds = new Set();
const leftIds = new Set();
const pingIds = new Map();
const level = common.createLevel(wasmServer);
const explodedBombsPtr = wasmServer.allocate_exploded_bombs();
wss.on("connection", (ws, req) => {
ws.binaryType = 'arraybuffer';
if (players.size >= SERVER_TOTAL_LIMIT) {
Expand Down Expand Up @@ -285,9 +284,9 @@ function tick() {
messageSentCounter += 1;
});
}
wasmServer.update_bombs_on_server_side(level.scenePtr, deltaTime, explodedBombsPtr, level.bombsPtr);
wasmServer.update_bombs_on_server_side(level.scenePtr, deltaTime, level.bombsPtr);
const bufferBombsExploded = (() => {
const message = wasmServer.exploded_bombs_as_batch_message(explodedBombsPtr, level.bombsPtr);
const message = wasmServer.exploded_bombs_as_batch_message(level.bombsPtr);
if (message === 0)
return null;
const size = new DataView(wasmServer.memory.buffer, message, common.UINT32_SIZE).getUint32(0, true);
Expand Down Expand Up @@ -353,7 +352,6 @@ async function instantiateWasmServer(path) {
throw_bomb: wasm.instance.exports.throw_bomb,
throw_bomb_on_server_side: wasm.instance.exports.throw_bomb_on_server_side,
thrown_bombs_as_batch_message: wasm.instance.exports.thrown_bombs_as_batch_message,
allocate_exploded_bombs: wasm.instance.exports.allocate_exploded_bombs,
update_bombs_on_server_side: wasm.instance.exports.update_bombs_on_server_side,
exploded_bombs_as_batch_message: wasm.instance.exports.exploded_bombs_as_batch_message
};
Expand Down
15 changes: 6 additions & 9 deletions server.mts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const joinedIds = new Set<number>()
const leftIds = new Set<number>()
const pingIds = new Map<number, number>()
const level = common.createLevel(wasmServer);
const explodedBombsPtr = wasmServer.allocate_exploded_bombs();

wss.on("connection", (ws, req) => {
ws.binaryType = 'arraybuffer';
Expand Down Expand Up @@ -334,9 +333,9 @@ function tick() {
});
}

wasmServer.update_bombs_on_server_side(level.scenePtr, deltaTime, explodedBombsPtr, level.bombsPtr);
wasmServer.update_bombs_on_server_side(level.scenePtr, deltaTime, level.bombsPtr);
const bufferBombsExploded = (() => {
const message = wasmServer.exploded_bombs_as_batch_message(explodedBombsPtr, level.bombsPtr);
const message = wasmServer.exploded_bombs_as_batch_message(level.bombsPtr);
if (message === 0) return null;
const size = new DataView(wasmServer.memory.buffer, message, common.UINT32_SIZE).getUint32(0, true);
return new Uint8ClampedArray(wasmServer.memory.buffer, message + common.UINT32_SIZE, size - common.UINT32_SIZE);
Expand Down Expand Up @@ -397,9 +396,8 @@ interface WasmServer extends common.WasmCommon {
throw_bomb: (player_position_x: number, player_position_y: number, player_direction: number, bombs: number) => number,
throw_bomb_on_server_side: (player_position_x: number, player_position_y: number, player_direction: number, bombs: number) => number,
thrown_bombs_as_batch_message: (bombs: number) => number,
allocate_exploded_bombs: () => number,
update_bombs_on_server_side: (scene: number, delta_time: number, exploded_bombs: number, bombs: number) => void,
exploded_bombs_as_batch_message: (exploded_bombs: number, bombs: number) => number,
update_bombs_on_server_side: (scene: number, delta_time: number, bombs: number) => void,
exploded_bombs_as_batch_message: (bombs: number) => number,
}

function platform_now_secs(): number {
Expand Down Expand Up @@ -430,9 +428,8 @@ async function instantiateWasmServer(path: string): Promise<WasmServer> {
throw_bomb: wasm.instance.exports.throw_bomb as (player_position_x: number, player_position_y: number, player_direction: number, bombs: number) => number,
throw_bomb_on_server_side: wasm.instance.exports.throw_bomb_on_server_side as (player_position_x: number, player_position_y: number, player_direction: number, bombs: number) => number,
thrown_bombs_as_batch_message: wasm.instance.exports.thrown_bombs_as_batch_message as (bombs: number) => number,
allocate_exploded_bombs: wasm.instance.exports.allocate_exploded_bombs as () => number,
update_bombs_on_server_side: wasm.instance.exports.update_bombs_on_server_side as (scene: number, delta_time: number, exploded_bombs: number, bombs: number) => void,
exploded_bombs_as_batch_message: wasm.instance.exports.exploded_bombs_as_batch_message as (exploded_bombs: number, bombs: number) => number
update_bombs_on_server_side: wasm.instance.exports.update_bombs_on_server_side as (scene: number, delta_time: number, bombs: number) => void,
exploded_bombs_as_batch_message: wasm.instance.exports.exploded_bombs_as_batch_message as (bombs: number) => number
};
}

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

0 comments on commit 46c431a

Please sign in to comment.