Skip to content

Commit

Permalink
fix: Set random seed in online from matchmaker
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxCWhitehead committed Oct 7, 2024
1 parent e25141e commit 6252a0b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/core/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ pub fn plugin(session: &mut Session) {
#[type_data(SchemaLuaEcsRefMetatable(lua_metatable))]
pub struct GlobalRng(AtomicRng);

pub const DEFAULT_RANDOM_SEED: u32 = 7;

impl Default for GlobalRng {
fn default() -> Self {
Self(AtomicRng::with_seed(7))
Self(AtomicRng::with_seed(DEFAULT_RANDOM_SEED as u64))
}
}

Expand Down
24 changes: 16 additions & 8 deletions src/ui/main_menu/map_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::prelude::*;

use crate::ui::map_select::{map_select_menu, MapSelectAction};

#[cfg(not(target_arch = "wasm32"))]
use crate::ui::network_game::NetworkGameState;

use super::player_select::PlayerSelectState;
use super::MenuPage;

Expand Down Expand Up @@ -101,14 +104,19 @@ pub fn widget(

#[cfg(not(target_arch = "wasm32"))]
let session_runner: Box<dyn SessionRunner> = match network_socket {
Some(socket) => Box::new(GgrsSessionRunner::<NetworkInputConfig>::new(
FPS,
GgrsSessionRunnerInfo::new(
socket.ggrs_socket(),
Some(meta.network.max_prediction_window),
Some(meta.network.local_input_delay),
),
)),
Some(socket) => {
let random_seed = ui.ctx().get_state::<NetworkGameState>().random_seed();

Box::new(GgrsSessionRunner::<NetworkInputConfig>::new(
Some(FPS),
GgrsSessionRunnerInfo::new(
socket.ggrs_socket(),
Some(meta.network.max_prediction_window),
Some(meta.network.local_input_delay),
random_seed,
),
))
}
None => Box::<JumpyDefaultMatchRunner>::default(),
};
#[cfg(target_arch = "wasm32")]
Expand Down
13 changes: 11 additions & 2 deletions src/ui/network_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub struct NetworkGameState {
joined_players: usize,
lan_servers: Vec<lan::ServerInfo>,
ping_update_timer: Timer,
random_seed: u64,
}

impl Default for NetworkGameState {
Expand All @@ -97,10 +98,17 @@ impl Default for NetworkGameState {
lan_servers: default(),
joined_players: default(),
ping_update_timer: Timer::new(Duration::from_secs(1), TimerMode::Repeating),
random_seed: DEFAULT_RANDOM_SEED as u64,
}
}
}

impl NetworkGameState {
pub fn random_seed(&self) -> u64 {
self.random_seed
}
}

pub fn network_game_menu(
world: &World,
// commands: &mut Commands,
Expand Down Expand Up @@ -247,6 +255,7 @@ pub fn network_game_menu(
status,
ping_update_timer,
joined_players,
random_seed
} = &mut state;

ui.separator();
Expand Down Expand Up @@ -504,11 +513,11 @@ pub fn network_game_menu(
// Player idx is on socket - don't need it here atm
player_idx: _,
player_count: _,
// random seed currently unused - currently GlobalRng has a fixed seed
random_seed,
random_seed: new_random_seed,
} => {
world.resources.insert(socket);
*status = NetworkGameStatus::default();
*random_seed = new_random_seed;
ui.ctx().set_state(MenuPage::PlayerSelect);
info!("Matchmaking complete, going to player select.");
},
Expand Down

0 comments on commit 6252a0b

Please sign in to comment.