From 414e775e61e63aa2410d0eab5b0fce131d05cc15 Mon Sep 17 00:00:00 2001 From: IQuant Date: Mon, 13 May 2024 21:00:00 +0300 Subject: [PATCH] Proxy updates --- files/src/net_handling.lua | 4 ++++ init.lua | 17 ++++++++++++----- noita-proxy/Cargo.lock | 1 + noita-proxy/Cargo.toml | 1 + noita-proxy/src/lib.rs | 15 +++++++++++++++ noita-proxy/src/net.rs | 27 +++++++++++++++++++++++++-- noita-proxy/tangled/src/lib.rs | 4 ++++ todo.txt | 7 ++++--- 8 files changed, 66 insertions(+), 10 deletions(-) diff --git a/files/src/net_handling.lua b/files/src/net_handling.lua index ea51dc6b..efde292c 100644 --- a/files/src/net_handling.lua +++ b/files/src/net_handling.lua @@ -27,6 +27,10 @@ function net_handling.proxy.peer_id(_, value) ctx.is_host = ctx.my_id == ctx.host_id end +function net_handling.proxy.debug(_, value) + ctx.debug = value == "true" +end + function net_handling.mod.player(peer_id, value) local input_data = value.i local pos_data = value.p diff --git a/init.lua b/init.lua index d0c76015..3ef26e71 100755 --- a/init.lua +++ b/init.lua @@ -128,11 +128,13 @@ function OnPlayerSpawned( player_entity ) -- This runs when player entity has be EntityAddComponent2(player_entity, "LuaComponent", {script_wand_fired = "mods/quant.ew/files/cbs/count_times_wand_fired.lua"}) - dofile_once("data/scripts/perks/perk.lua") - local x, y = EntityGetFirstHitboxCenter(player_entity) - perk_spawn(x, y, "LASER_AIM", true) - perk_spawn(x-50, y, "GLASS_CANNON", true) - perk_spawn(x-25, y, "EDIT_WANDS_EVERYWHERE", true) + if ctx.debug then + dofile_once("data/scripts/perks/perk.lua") + local x, y = EntityGetFirstHitboxCenter(player_entity) + perk_spawn(x, y, "LASER_AIM", true) + perk_spawn(x-50, y, "GLASS_CANNON", true) + perk_spawn(x-25, y, "EDIT_WANDS_EVERYWHERE", true) + end end local function on_world_pre_update_inner() @@ -154,6 +156,11 @@ local function on_world_pre_update_inner() if hp == 0 then EntityInflictDamage(my_player.entity, 10000000, "DAMAGE_CURSE", "Out of shared health", "NONE", 0, 0, GameGetWorldStateEntity()) end + if not ctx.run_ended then + GamePrint("Notifying of run end") + net.proxy_notify_game_over() + ctx.run_ended = true + end end -- Item sync diff --git a/noita-proxy/Cargo.lock b/noita-proxy/Cargo.lock index 969d74e8..6767f5c4 100644 --- a/noita-proxy/Cargo.lock +++ b/noita-proxy/Cargo.lock @@ -1227,6 +1227,7 @@ dependencies = [ "bitcode", "eframe", "lz4_flex", + "rand", "serde", "tangled", "tracing", diff --git a/noita-proxy/Cargo.toml b/noita-proxy/Cargo.toml index fbc27217..d3f933d9 100644 --- a/noita-proxy/Cargo.toml +++ b/noita-proxy/Cargo.toml @@ -17,6 +17,7 @@ tangled = { path = "tangled" } serde = { version = "1.0.199", features = ["serde_derive"] } bitcode = "0.6.0" lz4_flex = { version = "0.11.3", default_features = false, features = ["std"]} +rand = "0.8.5" [profile.dev] opt-level = 1 diff --git a/noita-proxy/src/lib.rs b/noita-proxy/src/lib.rs index 2d34cb11..ed8cd502 100644 --- a/noita-proxy/src/lib.rs +++ b/noita-proxy/src/lib.rs @@ -9,6 +9,7 @@ pub mod messages; #[derive(Debug, Decode, Encode, Clone)] pub struct GameSettings { seed: u64, + debug_mode: bool, } pub mod net; @@ -21,6 +22,8 @@ enum AppState { pub struct App { state: AppState, addr: String, + debug_mode: bool, + use_constant_seed: bool, } impl App { @@ -28,6 +31,13 @@ impl App { let bind_addr = "0.0.0.0:5123".parse().unwrap(); let peer = Peer::host(bind_addr, None).unwrap(); let netman = net::NetManager::new(peer); + { + let mut settings = netman.settings.lock().unwrap(); + settings.debug_mode = self.debug_mode; + if !self.use_constant_seed { + settings.seed = rand::random(); + } + } netman .accept_local .store(true, std::sync::atomic::Ordering::SeqCst); @@ -47,6 +57,8 @@ impl Default for App { Self { state: AppState::Init, addr: "192.168.1.168:5123".to_string(), + debug_mode: false, + use_constant_seed: false, } } } @@ -58,9 +70,12 @@ impl eframe::App for App { AppState::Init => { egui::CentralPanel::default().show(ctx, |ui| { ui.heading("Noita Entangled Worlds proxy"); + ui.checkbox(&mut self.debug_mode, "Debug mode"); + ui.checkbox(&mut self.use_constant_seed, "Use specified seed"); if ui.button("Host").clicked() { self.start_server(); } + ui.separator(); ui.text_edit_singleline(&mut self.addr); let addr = self.addr.parse(); ui.add_enabled_ui(addr.is_ok(), |ui| { diff --git a/noita-proxy/src/net.rs b/noita-proxy/src/net.rs index efc4b5c3..8cc65a11 100644 --- a/noita-proxy/src/net.rs +++ b/noita-proxy/src/net.rs @@ -58,7 +58,10 @@ impl NetManager { pub fn new(peer: tangled::Peer) -> Arc { Self { peer, - settings: Mutex::new(GameSettings { seed: 1663107061 }), + settings: Mutex::new(GameSettings { + seed: 1663107061, + debug_mode: false, + }), continue_running: AtomicBool::new(true), accept_local: AtomicBool::new(false), local_connected: AtomicBool::new(false), @@ -114,6 +117,10 @@ impl NetManager { self.peer.my_id().expect("Has peer id at this point"), )); state.try_ws_write(ws_encode_proxy("name", "test_name")); + state.try_ws_write(ws_encode_proxy( + "debug", + if settings.debug_mode { "true" } else { "false" }, + )); state.try_ws_write(ws_encode_proxy("ready", "")); // TODO? those are currently ignored by mod for id in self.peer.iter_peer_ids() { @@ -237,12 +244,28 @@ impl NetManager { thread::spawn(move || self.start_inner()); } + fn resend_game_settings(&self) { + let settings = self.settings.lock().unwrap().clone(); + self.broadcast(&NetMsg::StartGame { settings }, Reliability::Reliable); + } + + fn is_host(&self) -> bool { + self.peer.my_id() == Some(PeerId::HOST) + } + pub(crate) fn handle_message_to_proxy(&self, msg: &[u8]) { let msg = String::from_utf8_lossy(msg); let mut msg = msg.split_ascii_whitespace(); let key = msg.next(); match key { - Some("game_over") => {} + Some("game_over") => { + if self.is_host() { + info!("Game over, resending game settings"); + self.settings.lock().unwrap().seed += 1; + info!("New seed: {}", self.settings.lock().unwrap().seed); + self.resend_game_settings(); + } + } key => { error!("Unknown msg from mod: {:?}", key) } diff --git a/noita-proxy/tangled/src/lib.rs b/noita-proxy/tangled/src/lib.rs index 5a3df9b9..51e8647b 100644 --- a/noita-proxy/tangled/src/lib.rs +++ b/noita-proxy/tangled/src/lib.rs @@ -37,6 +37,10 @@ struct Datagram { #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] pub struct PeerId(pub u16); +impl PeerId { + pub const HOST: PeerId = PeerId(0); +} + impl Display for PeerId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) diff --git a/todo.txt b/todo.txt index 40e21c28..5f3cab4f 100644 --- a/todo.txt +++ b/todo.txt @@ -23,11 +23,12 @@ k Лимит на длину сообщения - Улучшеная синхронизация инвентаря и перков + ...и текущего предмета - - reliability + + reliability + Сжатие пакетов - - Перекидывание предметов + + Перекидывание предметов ? Фикс наведения на клиентов - fungal shift-ы - orbs sync - test tangled better - - poly on clients \ No newline at end of file + - poly on clients + - proxy: stop receiving connections before another start_game on game over, noita: try to reconnect to proxy. \ No newline at end of file