Proxy updates

This commit is contained in:
IQuant 2024-05-13 21:00:00 +03:00
parent c98a74dc17
commit 414e775e61
8 changed files with 66 additions and 10 deletions

View file

@ -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

View file

@ -128,12 +128,14 @@ 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"})
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()
if my_player == nil then return end
@ -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

View file

@ -1227,6 +1227,7 @@ dependencies = [
"bitcode",
"eframe",
"lz4_flex",
"rand",
"serde",
"tangled",
"tracing",

View file

@ -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

View file

@ -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| {

View file

@ -58,7 +58,10 @@ impl NetManager {
pub fn new(peer: tangled::Peer) -> Arc<Self> {
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)
}

View file

@ -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)

View file

@ -23,11 +23,12 @@
k Лимит на длину сообщения
- Улучшеная синхронизация инвентаря и перков
+ ...и текущего предмета
- reliability
+ reliability
+ Сжатие пакетов
- Перекидывание предметов
+ Перекидывание предметов
? Фикс наведения на клиентов
- fungal shift-ы
- orbs sync
- test tangled better
- poly on clients
- proxy: stop receiving connections before another start_game on game over, noita: try to reconnect to proxy.