Skip to content

Commit

Permalink
Merge pull request #301 from TruncateGame/fix/npc-winning
Browse files Browse the repository at this point in the history
Fix bug where NPC couldn't win by touching an artifact on WASM builds
  • Loading branch information
bglw authored Mar 1, 2025
2 parents d5c82a9 + dfc769e commit 6cdad10
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 38 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions truncate_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ impl WebHandle {
}
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn backchannel_setup() {
use web_sys::console;

// Make sure panics are logged using `console.error`.
console_error_panic_hook::set_once();

// Redirect tracing to console.log and friends:
tracing_wasm::set_as_global_default();
}

// Functions used in the web worker
// Used to evaluate games as the NPC using a separate thread,
// preventing the UI from hanging during computation.
Expand Down
6 changes: 1 addition & 5 deletions truncate_client/src/regions/native_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ pub fn render_native_menu_if_required(
}
ui.text_edit_singleline(room_code);
if ui.button("Join Game").clicked() {
send_to_server(PlayerMessage::JoinGame(
room_code.clone(),
outer.name.clone(),
token.clone(),
));
outer.launched_code = Some(room_code.clone());
return Some(GameStatus::PendingJoin(room_code.clone()));
}
if let Some(existing_token) = token {
Expand Down
1 change: 1 addition & 0 deletions truncate_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ chksum-hash-sha2 = { version = "0.0.0", default-features = false, features = [
"256",
] }
noise = "0.8"
tracing = "0.1.41"

[dev-dependencies]
insta = { version = "1.29.0", features = ["yaml"] }
Expand Down
19 changes: 8 additions & 11 deletions truncate_core/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,18 +567,15 @@ impl Game {
}

let neighbors = self.board.neighbouring_squares(position);
if !neighbors.iter().any(|&(_, square)| match square {
Square::Occupied { player: p, .. } => p == player,
Square::Artifact { player: p, .. } => p == player,
_ => false,
}) {
if neighbors.iter().any(|&(_, square)| matches!(square, Square::Artifact { player: p, .. } if p != player)) {
return Err(GamePlayError::OpponentStartPlace);
}

if self.turn_count == 0 && neighbors.iter().any(|&(_, square)| matches!(square, Square::Artifact { player: p, .. } if p != player)) {
return Err(GamePlayError::OpponentStartPlace);
}

if !neighbors.iter().any(
|&(_, square)| match square {
Square::Occupied { player: p, .. } => p == player,
Square::Artifact { player: p, .. } => p == player,
_ => false,
},
) {
return Err(GamePlayError::NonAdjacentPlace);
}

Expand Down
2 changes: 1 addition & 1 deletion truncate_core/src/moves/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ mod tests {
players,
player_turn_count: vec![0, 0],
judge: short_dict(),
turn_count: 1, // any non zero value will do to avoid hitting OpponentStartPlace error
turn_count: 0,
..Game::new_legacy(3, 1, None, GameRules::generation(0))
};
game.start();
Expand Down
19 changes: 4 additions & 15 deletions truncate_core/src/npc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ impl Game {
let evaluation_player = game
.next_player
.expect("Minimax only works in non-periodic playmodes");

let mut internal_arborist = if npc_params.pruning {
Arborist::pruning()
} else {
Expand Down Expand Up @@ -140,15 +139,15 @@ impl Game {
};

if log {
println!(
tracing::debug!(
"Bot checked {} boards, going to a depth of {looked}",
arborist.assessed()
);
println!("Bot has the hand: {}", game.players[evaluation_player].hand);
tracing::debug!("Bot has the hand: {}", game.players[evaluation_player].hand);

println!("Chosen tree has the score {best_score:#?}");
tracing::debug!("Chosen tree has the score {best_score:#?}");
if let Some(board) = &best_score.board {
println!("Bot is aiming for the board {board}");
tracing::debug!("Bot is aiming for the board {board}");
}
}

Expand Down Expand Up @@ -717,10 +716,8 @@ mod tests {
initial_board: &'a str,
depth: usize,
dict: &WordDict,
turn_count: u32,
) -> (&'a str, String) {
let mut game = test_game(initial_board, hand);
game.turn_count = turn_count;

let (best_move, pruned_checks, total_checks) = best_test_move(&game, &dict, depth);

Expand Down Expand Up @@ -989,7 +986,6 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1031,7 +1027,6 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1073,7 +1068,6 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1115,7 +1109,6 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1157,7 +1150,6 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1202,7 +1194,6 @@ mod tests {
"###,
3,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1250,7 +1241,6 @@ mod tests {
"###,
4,
&dict,
0,
);

insta::with_settings!({
Expand Down Expand Up @@ -1304,7 +1294,6 @@ mod tests {
"###,
2,
&dict,
3, // any non zero turn count value will do
);

insta::with_settings!({
Expand Down
2 changes: 2 additions & 0 deletions web_client/src/static/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ self.onmessage = function (e) {
console.log("[WORKER] Successfully loaded Truncate");

loadedWasm = true;
wasm_bindgen.backchannel_setup();

}

function on_wasm_error(error) {
Expand Down

0 comments on commit 6cdad10

Please sign in to comment.