Skip to content

Commit f4f2020

Browse files
committed
fix ewext test and make some code nicer
1 parent b39be10 commit f4f2020

File tree

2 files changed

+81
-77
lines changed

2 files changed

+81
-77
lines changed

ewext/src/modules/world_sync.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ impl Module for WorldSync {
2828
.filter_map(|i| {
2929
let dx = i % 3;
3030
let dy = i / 3;
31-
let cx = x as i32 / CHUNK_SIZE as i32 - 1 + dx;
32-
let cy = y as i32 / CHUNK_SIZE as i32 - 1 + dy;
31+
let cx = (x as i32).div_euclid(CHUNK_SIZE as i32) - 1 + dx;
32+
let cy = (y as i32).div_euclid(CHUNK_SIZE as i32) - 1 + dy;
3333
let mut update = NoitaWorldUpdate {
3434
coord: ChunkCoord(cx, cy),
3535
pixels: std::array::from_fn(|_| Pixel::default()),
@@ -271,39 +271,48 @@ pub fn test_world() {
271271
.chunk_map
272272
.insert(1, 1, chunk);
273273
}
274-
let mut data = [Pixel::default(); CHUNK_SIZE * CHUNK_SIZE];
274+
let mut upd = NoitaWorldUpdate {
275+
coord: ChunkCoord(5, 5),
276+
pixels: [Pixel::default(); CHUNK_SIZE * CHUNK_SIZE],
277+
};
275278
unsafe {
276-
assert!(pws.encode_world(ChunkCoord(5, 5), &mut data).is_ok());
279+
assert!(pws.encode_world(&mut upd).is_ok());
277280
}
278281
assert_eq!(
279-
data[0..128].iter().map(|a| a.mat()).collect::<Vec<_>>(),
282+
upd.pixels[0..128]
283+
.iter()
284+
.map(|a| a.mat())
285+
.collect::<Vec<_>>(),
280286
vec![0; 128]
281287
);
282288
let tmr = std::time::Instant::now();
289+
upd.coord = ChunkCoord(0, 0);
283290
unsafe {
284-
assert!(pws.encode_world(ChunkCoord(0, 0), &mut data).is_ok());
291+
assert!(pws.encode_world(&mut upd).is_ok());
285292
}
286293
println!("{}", tmr.elapsed().as_nanos());
287294
assert_eq!(
288-
data[0..128].iter().map(|a| a.mat()).collect::<Vec<_>>(),
295+
upd.pixels[0..128]
296+
.iter()
297+
.map(|a| a.mat())
298+
.collect::<Vec<_>>(),
289299
list[0..128].iter().map(|a| *a as u16).collect::<Vec<_>>()
290300
);
291301
let tmr = std::time::Instant::now();
302+
upd.coord = ChunkCoord(5, 5);
292303
unsafe {
293-
assert!(
294-
pws.decode_world(NoitaWorldUpdate {
295-
coord: ChunkCoord(5, 5),
296-
pixels: data
297-
})
298-
.is_ok()
299-
);
304+
assert!(pws.decode_world(upd.clone()).is_ok());
300305
}
301306
println!("{}", tmr.elapsed().as_nanos());
307+
upd.coord = ChunkCoord(0, 0);
302308
unsafe {
303-
assert!(pws.encode_world(ChunkCoord(0, 0), &mut data).is_ok());
309+
assert!(pws.encode_world(&mut upd).is_ok());
304310
}
305311
assert_eq!(
306-
data[0..128].iter().map(|a| a.mat()).collect::<Vec<_>>(),
312+
upd.pixels[0..128]
313+
.iter()
314+
.map(|a| a.mat())
315+
.collect::<Vec<_>>(),
307316
list[0..128].iter().map(|a| *a as u16).collect::<Vec<_>>()
308317
);
309318
}

noita-proxy/src/net/world.rs

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub(crate) enum WorldNetMessage {
6868
// Tell how to update a chunk storage
6969
UpdateStorage {
7070
chunk: ChunkCoord,
71-
chunk_data: Option<ChunkData>,
71+
chunk_data: ChunkData,
7272
world_num: u8,
7373
priority: Option<u8>,
7474
},
@@ -318,13 +318,7 @@ impl WorldManager {
318318
self.chunk_storage.clone()
319319
}
320320

321-
fn chunk_updated_locally(
322-
&mut self,
323-
chunk: ChunkCoord,
324-
priority: u8,
325-
pos: Option<(i32, i32, i32, i32, bool)>,
326-
world_num: u8,
327-
) -> Vec<(OmniPeerId, u8)> {
321+
fn update_world_info(&mut self, pos: Option<(i32, i32, i32, i32, bool)>, world_num: u8) {
328322
if let Some((px, py, cx, cy, is_not)) = pos {
329323
self.my_pos = (px, py);
330324
self.cam_pos = (cx, cy);
@@ -337,6 +331,9 @@ impl WorldManager {
337331
self.world_num = world_num;
338332
self.reset();
339333
}
334+
}
335+
336+
fn chunk_updated_locally(&mut self, chunk: ChunkCoord, priority: u8) -> Vec<(OmniPeerId, u8)> {
340337
let entry = self.chunk_state.entry(chunk).or_insert_with(|| {
341338
debug!("Created entry for {chunk:?}");
342339
ChunkState::RequestAuthority {
@@ -400,9 +397,6 @@ impl WorldManager {
400397
new_authority,
401398
stop_sending,
402399
} => {
403-
let Some(delta) = self.outbound_model.get_chunk_delta(chunk, false) else {
404-
return Vec::new();
405-
};
406400
if *pri != priority {
407401
*pri = priority;
408402
emit_queue.push((
@@ -429,6 +423,11 @@ impl WorldManager {
429423
new_auth_got = true
430424
}
431425
if take_auth {
426+
let Some(delta) = self.outbound_model.get_chunk_delta(chunk, false)
427+
else {
428+
return Vec::new();
429+
};
430+
emit_queue.retain(|(a, _)| matches!(a, Destination::Host));
432431
emit_queue.push((
433432
Destination::Peer(listener),
434433
WorldNetMessage::ListenUpdate {
@@ -437,7 +436,8 @@ impl WorldManager {
437436
take_auth,
438437
},
439438
));
440-
chunks_to_send = Vec::new()
439+
chunks_to_send = Vec::new();
440+
break;
441441
} else {
442442
chunks_to_send.push((listener, priority));
443443
}
@@ -712,15 +712,19 @@ impl WorldManager {
712712
}
713713
}
714714
}
715-
WorldNetMessage::GetChunk { chunk, priority } => self.emit_msg(
716-
Destination::Host,
717-
WorldNetMessage::UpdateStorage {
718-
chunk,
719-
chunk_data: self.outbound_model.get_chunk_data(chunk),
720-
world_num: self.world_num,
721-
priority: Some(priority),
722-
},
723-
),
715+
WorldNetMessage::GetChunk { chunk, priority } => {
716+
if let Some(chunk_data) = self.outbound_model.get_chunk_data(chunk) {
717+
self.emit_msg(
718+
Destination::Host,
719+
WorldNetMessage::UpdateStorage {
720+
chunk,
721+
chunk_data,
722+
world_num: self.world_num,
723+
priority: Some(priority),
724+
},
725+
)
726+
}
727+
}
724728
WorldNetMessage::AskForAuthority { chunk, priority } => {
725729
self.emit_msg(
726730
Destination::Host,
@@ -786,12 +790,12 @@ impl WorldManager {
786790
if let Some(chunk_data) = chunk_data {
787791
self.inbound_model.apply_chunk_data(chunk, &chunk_data);
788792
self.outbound_model.apply_chunk_data(chunk, &chunk_data);
789-
} else {
793+
} else if let Some(chunk_data) = self.outbound_model.get_chunk_data(chunk) {
790794
self.emit_msg(
791795
Destination::Host,
792796
WorldNetMessage::UpdateStorage {
793797
chunk,
794-
chunk_data: self.outbound_model.get_chunk_data(chunk),
798+
chunk_data,
795799
world_num: self.world_num,
796800
priority: None,
797801
},
@@ -811,15 +815,11 @@ impl WorldManager {
811815
if world_num != self.world_num {
812816
return;
813817
}
814-
if let Some(chunk_data) = chunk_data {
815-
let _ = self.tx.send((chunk, chunk_data.clone()));
816-
self.chunk_storage.insert(chunk, chunk_data);
817-
if let Some(p) = priority {
818-
self.cut_through_world_explosion_chunk(chunk);
819-
self.emit_got_authority(chunk, source, p)
820-
}
821-
} else if priority.is_some() {
822-
warn!("{} sent give auth without chunk", source)
818+
let _ = self.tx.send((chunk, chunk_data.clone()));
819+
self.chunk_storage.insert(chunk, chunk_data);
820+
if let Some(p) = priority {
821+
self.cut_through_world_explosion_chunk(chunk);
822+
self.emit_got_authority(chunk, source, p)
823823
}
824824
}
825825
WorldNetMessage::RelinquishAuthority {
@@ -1040,16 +1040,17 @@ impl WorldManager {
10401040
},
10411041
);
10421042
self.chunk_state.insert(chunk, ChunkState::UnloadPending);
1043-
let chunk_data = self.outbound_model.get_chunk_data(chunk);
1044-
self.emit_msg(
1045-
Destination::Host,
1046-
WorldNetMessage::UpdateStorage {
1047-
chunk,
1048-
chunk_data,
1049-
world_num: self.world_num,
1050-
priority: None,
1051-
},
1052-
);
1043+
if let Some(chunk_data) = self.outbound_model.get_chunk_data(chunk) {
1044+
self.emit_msg(
1045+
Destination::Host,
1046+
WorldNetMessage::UpdateStorage {
1047+
chunk,
1048+
chunk_data,
1049+
world_num: self.world_num,
1050+
priority: None,
1051+
},
1052+
);
1053+
}
10531054
} else {
10541055
self.emit_msg(
10551056
Destination::Peer(source),
@@ -3344,36 +3345,30 @@ impl WorldManager {
33443345
}
33453346
}
33463347
WorldSyncToProxy::End(pos, priority, world_num) => {
3347-
let updated_chunks = self
3348-
.outbound_model
3349-
.updated_chunks()
3350-
.iter()
3351-
.copied()
3352-
.collect::<Vec<_>>();
3348+
let updated_chunks = self.outbound_model.updated_chunks().clone();
33533349
self.current_update += 1;
3354-
let chunks_to_send: Vec<Vec<(OmniPeerId, u8)>> = updated_chunks
3355-
.iter()
3356-
.map(|chunk| self.chunk_updated_locally(*chunk, priority, pos, world_num))
3357-
.collect();
33583350
let mut chunk_packet: HashMap<OmniPeerId, Vec<(ChunkDelta, u8)>> = HashMap::new();
3359-
for (chunk, who_sending) in updated_chunks.iter().zip(chunks_to_send.iter()) {
3360-
let Some(delta) = self.outbound_model.get_chunk_delta(*chunk, false) else {
3351+
self.update_world_info(pos, world_num);
3352+
for chunk in updated_chunks {
3353+
// who sending may be better to be after the let some
3354+
// but im too lazy to figure out for sure
3355+
let who_sending = self.chunk_updated_locally(chunk, priority);
3356+
let Some(delta) = self.outbound_model.get_chunk_delta(chunk, false) else {
33613357
continue;
33623358
};
33633359
for (peer, pri) in who_sending {
33643360
chunk_packet
3365-
.entry(*peer)
3361+
.entry(peer)
33663362
.or_default()
3367-
.push((delta.clone(), *pri));
3363+
.push((delta.clone(), pri));
33683364
}
33693365
}
3370-
let mut emit_queue = Vec::new();
3371-
for (peer, chunkpacket) in chunk_packet {
3372-
emit_queue.push((
3366+
let emit_queue = chunk_packet.into_iter().map(|(peer, chunkpacket)| {
3367+
(
33733368
Destination::Peer(peer),
33743369
WorldNetMessage::ChunkPacket { chunkpacket },
3375-
));
3376-
}
3370+
)
3371+
});
33773372
for (dst, msg) in emit_queue {
33783373
self.emit_msg(dst, msg)
33793374
}

0 commit comments

Comments
 (0)