Skip to content

Commit

Permalink
[server] tuck thrownBombs under server.wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 1, 2024
1 parent 1ad0bff commit 44d70df
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 @@ -216,25 +216,21 @@ fn ItemsCollectedBatchMessage *collected_items_as_batch_message(Item[]* items) @

/// Bombs //////////////////////////////

def ThrownBombs = List(<usz>);
List(<usz>) thrown_bombs;

fn ThrownBombs *allocate_thrown_bombs() @extern("allocate_thrown_bombs") @wasm {
return mem::new(ThrownBombs);
}

fn void throw_bomb_on_server_side(float player_position_x, float player_position_y, float player_direction, Bombs *bombs, ThrownBombs *thrown_bombs) @extern("throw_bomb_on_server_side") @wasm {
fn void throw_bomb_on_server_side(float player_position_x, float player_position_y, float player_direction, Bombs *bombs) @extern("throw_bomb_on_server_side") @wasm {
int index = common::throw_bomb(player_position_x, player_position_y, player_direction, bombs);
if (index >= 0) thrown_bombs.push(index);
}

fn BombsSpawnedBatchMessage *thrown_bombs_as_batch_message(Bombs *bombs, ThrownBombs *thrown_bombs) @extern("thrown_bombs_as_batch_message") @wasm {
fn BombsSpawnedBatchMessage *thrown_bombs_as_batch_message(Bombs *bombs) @extern("thrown_bombs_as_batch_message") @wasm {
if (thrown_bombs.size == 0) return null;
int size = BombsSpawnedBatchMessage.sizeof + BombSpawned.sizeof*thrown_bombs.size;
BombsSpawnedBatchMessage *message = mem::tcalloc(size);
message.size = size;
message.kind = MessageKind.BOMB_SPAWNED;

foreach (index, bombIndex: *thrown_bombs) {
foreach (index, bombIndex: thrown_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 thrownBombsPtr = wasmServer.allocate_thrown_bombs();
const explodedBombsPtr = wasmServer.allocate_exploded_bombs();
wss.on("connection", (ws, req) => {
ws.binaryType = 'arraybuffer';
Expand Down Expand Up @@ -104,7 +103,7 @@ wss.on("connection", (ws, req) => {
}
}
else if (common.AmmaThrowingStruct.verify(view)) {
wasmServer.throw_bomb_on_server_side(player.position.x, player.position.y, player.direction, level.bombsPtr, thrownBombsPtr);
wasmServer.throw_bomb_on_server_side(player.position.x, player.position.y, player.direction, level.bombsPtr);
}
else if (common.PingStruct.verify(view)) {
pingIds.set(id, common.PingStruct.timestamp.read(view));
Expand Down Expand Up @@ -254,7 +253,7 @@ function tick() {
}
}
const bufferBombsThrown = (() => {
const message = wasmServer.thrown_bombs_as_batch_message(level.bombsPtr, thrownBombsPtr);
const message = wasmServer.thrown_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 @@ -351,7 +350,6 @@ async function instantiateWasmServer(path) {
reconstruct_state_of_items: wasm.instance.exports.reconstruct_state_of_items,
collect_items_by_player_at: wasm.instance.exports.collect_items_by_player_at,
collected_items_as_batch_message: wasm.instance.exports.collected_items_as_batch_message,
allocate_thrown_bombs: wasm.instance.exports.allocate_thrown_bombs,
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,
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 thrownBombsPtr = wasmServer.allocate_thrown_bombs();
const explodedBombsPtr = wasmServer.allocate_exploded_bombs();

wss.on("connection", (ws, req) => {
Expand Down Expand Up @@ -123,7 +122,7 @@ wss.on("connection", (ws, req) => {
player.newMoving &= ~(1<<direction);
}
} else if (common.AmmaThrowingStruct.verify(view)) {
wasmServer.throw_bomb_on_server_side(player.position.x, player.position.y, player.direction, level.bombsPtr, thrownBombsPtr);
wasmServer.throw_bomb_on_server_side(player.position.x, player.position.y, player.direction, level.bombsPtr);
} else if (common.PingStruct.verify(view)) {
pingIds.set(id, common.PingStruct.timestamp.read(view));
} else {
Expand Down Expand Up @@ -299,7 +298,7 @@ function tick() {

// Notifying about thrown bombs
const bufferBombsThrown = (() => {
const message = wasmServer.thrown_bombs_as_batch_message(level.bombsPtr, thrownBombsPtr);
const message = wasmServer.thrown_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 @@ -395,10 +394,9 @@ interface WasmServer extends common.WasmCommon {
reconstruct_state_of_items: (items: number) => number,
collect_items_by_player_at: (player_position_x: number, player_position_y: number, items: number) => void,
collected_items_as_batch_message: (items: number) => number,
allocate_thrown_bombs: () => number,
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, thrown_bombs: number) => number,
thrown_bombs_as_batch_message: (bombs: number, thrown_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,
Expand Down Expand Up @@ -429,10 +427,9 @@ async function instantiateWasmServer(path: string): Promise<WasmServer> {
reconstruct_state_of_items: wasm.instance.exports.reconstruct_state_of_items as (items: number) => number,
collect_items_by_player_at: wasm.instance.exports.collect_items_by_player_at as (player_position_x: number, player_position_y: number, items: number) => void,
collected_items_as_batch_message: wasm.instance.exports.collected_items_as_batch_message as (items: number) => number,
allocate_thrown_bombs: wasm.instance.exports.allocate_thrown_bombs as () => number,
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, thrown_bombs: number) => number,
thrown_bombs_as_batch_message: wasm.instance.exports.thrown_bombs_as_batch_message as (bombs: number, thrown_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
Expand Down
Binary file modified server.wasm
Binary file not shown.

0 comments on commit 44d70df

Please sign in to comment.