actually send chunks to proxy again

This commit is contained in:
bgkillas 2025-08-30 13:48:04 -04:00
parent 785e945790
commit b39be102a7
5 changed files with 38 additions and 46 deletions

View file

@ -1,7 +1,7 @@
use crate::modules::{Module, ModuleCtx};
use crate::{WorldSync, my_peer_id};
use eyre::{ContextCompat, eyre};
use noita_api::noita::types::{CellType, FireCell, GasCell, LiquidCell};
use noita_api::noita::types::{CellType, FireCell, GasCell, LiquidCell, Vec2i};
use noita_api::noita::world::ParticleWorldState;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use shared::NoitaOutbound;
@ -23,13 +23,13 @@ impl Module for WorldSync {
return Ok(());
};
let (x, y) = (ent.transform.pos.x, ent.transform.pos.y);
let updates = (0..1)
//.into_par_iter()
.map(|i| {
let updates = (0..9)
.into_par_iter()
.filter_map(|i| {
let dx = i % 3;
let dy = i / 3;
let cx = x as i32 / CHUNK_SIZE as i32 + dx;
let cy = y as i32 / CHUNK_SIZE as i32 + dy;
let cx = x as i32 / CHUNK_SIZE as i32 - 1 + dx;
let cy = y as i32 / CHUNK_SIZE as i32 - 1 + dy;
let mut update = NoitaWorldUpdate {
coord: ChunkCoord(cx, cy),
pixels: std::array::from_fn(|_| Pixel::default()),
@ -37,7 +37,7 @@ impl Module for WorldSync {
if unsafe {
self.particle_world_state
.assume_init_ref()
.encode_world(update.coord, &mut update.pixels)
.encode_world(&mut update)
}
.is_ok()
{
@ -49,6 +49,19 @@ impl Module for WorldSync {
.collect::<Vec<_>>();
let msg = NoitaOutbound::WorldSyncToProxy(WorldSyncToProxy::Updates(updates));
ctx.net.send(&msg)?;
let Vec2i { x: cx, y: cy } = ctx.globals.game_global.m_grid_world.cam_pos;
let msg = NoitaOutbound::WorldSyncToProxy(WorldSyncToProxy::End(
Some((
x.div_euclid(CHUNK_SIZE as f32) as i32,
y.div_euclid(CHUNK_SIZE as f32) as i32,
cx.div_euclid(CHUNK_SIZE as isize) as i32,
cy.div_euclid(CHUNK_SIZE as isize) as i32,
false,
)),
1,
self.world_num,
));
ctx.net.send(&msg)?;
Ok(())
}
}
@ -70,20 +83,14 @@ impl WorldSync {
pub const SCALE: isize = (512 / CHUNK_SIZE as isize).ilog2() as isize;
#[allow(unused)]
trait WorldData {
unsafe fn encode_world(
&self,
coord: ChunkCoord,
chunk: &mut [Pixel; CHUNK_SIZE * CHUNK_SIZE],
) -> eyre::Result<()>;
unsafe fn encode_world(&self, chunk: &mut NoitaWorldUpdate) -> eyre::Result<()>;
unsafe fn decode_world(&self, chunk: NoitaWorldUpdate) -> eyre::Result<()>;
}
impl WorldData for ParticleWorldState {
unsafe fn encode_world(
&self,
coord: ChunkCoord,
chunk: &mut [Pixel; CHUNK_SIZE * CHUNK_SIZE],
) -> eyre::Result<()> {
let (cx, cy) = (coord.0 as isize, coord.1 as isize);
unsafe fn encode_world(&self, chunk: &mut NoitaWorldUpdate) -> eyre::Result<()> {
let ChunkCoord(cx, cy) = chunk.coord;
let (cx, cy) = (cx as isize, cy as isize);
let chunk = &mut chunk.pixels;
let Some(pixel_array) = unsafe { self.world_ptr.as_mut() }
.wrap_err("no world")?
.chunk_map

View file

@ -3338,7 +3338,7 @@ impl WorldManager {
pub fn handle_noita_msg(&mut self, _: OmniPeerId, msg: WorldSyncToProxy) {
match msg {
WorldSyncToProxy::Updates(updates) => {
for update in updates.into_iter().flatten() {
for update in updates {
self.outbound_model
.apply_noita_update(update, &mut self.is_storage_recent)
}

View file

@ -92,13 +92,10 @@ impl ChunkData {
}
impl WorldModel {
fn get_chunk_coords(x: i32, y: i32) -> (ChunkCoord, usize) {
let chunk_x = x.div_euclid(CHUNK_SIZE as i32);
let chunk_y = y.div_euclid(CHUNK_SIZE as i32);
fn get_chunk_offset(x: i32, y: i32) -> usize {
let x = x.rem_euclid(CHUNK_SIZE as i32) as usize;
let y = y.rem_euclid(CHUNK_SIZE as i32) as usize;
let offset = x + y * CHUNK_SIZE;
(ChunkCoord(chunk_x, chunk_y), offset)
x + y * CHUNK_SIZE
}
/*fn set_pixel(&mut self, x: i32, y: i32, pixel: Pixel) {
@ -124,36 +121,21 @@ impl WorldModel {
update: NoitaWorldUpdate,
changed: &mut FxHashSet<ChunkCoord>,
) {
fn set_pixel(pixel: Pixel, chunk: &mut Chunk, offset: usize) -> bool {
let current = chunk.pixel(offset);
if current != pixel {
chunk.set_pixel(offset, pixel);
true
} else {
false
}
}
let (start_x, start_y) = (
update.coord.0 * CHUNK_SIZE as i32,
update.coord.1 * CHUNK_SIZE as i32,
);
let mut chunk_coord = update.coord;
let mut chunk = self.chunks.entry(update.coord).or_default();
let chunk_coord = update.coord;
let chunk = self.chunks.entry(update.coord).or_default();
for (i, pixel) in update.pixels.into_iter().enumerate() {
let x = (i % CHUNK_SIZE) as i32;
let y = (i / CHUNK_SIZE) as i32;
let xs = start_x + x;
let ys = start_y + y;
let (new_chunk_coord, offset) = Self::get_chunk_coords(xs, ys);
if chunk_coord != new_chunk_coord {
chunk_coord = new_chunk_coord;
chunk = self.chunks.entry(chunk_coord).or_default();
}
if set_pixel(pixel, chunk, offset) {
let offset = Self::get_chunk_offset(xs, ys);
if chunk.set_pixel(offset, pixel) {
self.updated_chunks.insert(chunk_coord);
if changed.contains(&chunk_coord) {
changed.remove(&chunk_coord);
}
changed.remove(&chunk_coord);
}
}
}

View file

@ -66,10 +66,13 @@ impl Chunk {
self.pixels[offset]
}
pub fn set_pixel(&mut self, offset: usize, pixel: Pixel) {
pub fn set_pixel(&mut self, offset: usize, pixel: Pixel) -> bool {
if self.pixels[offset] != pixel {
self.pixels[offset] = pixel;
self.mark_changed(offset);
true
} else {
false
}
}

View file

@ -66,7 +66,7 @@ impl Pixel {
#[derive(Debug, Encode, Decode, Clone)]
pub enum WorldSyncToProxy {
Updates(Vec<Option<NoitaWorldUpdate>>),
Updates(Vec<NoitaWorldUpdate>),
End(Option<(i32, i32, i32, i32, bool)>, u8, u8),
}
#[derive(Debug, Encode, Decode, Clone)]