make more stuff pub, add clone_chunks functions, add material list to pws

This commit is contained in:
bgkillas 2025-07-08 10:24:37 -04:00
parent 7b54962824
commit 97e816888c
6 changed files with 49 additions and 135 deletions

View file

@ -29,5 +29,11 @@ function OnWorldInitialized()
local world_info = np.GetWorldInfo()
local construct_cell = tonumber(ffi.cast("intptr_t", world_info.construct_cell))
local remove_cell = tonumber(ffi.cast("intptr_t", world_info.remove_cell))
blob_guy.init_particle_world_state(grid_world, material_list, construct_cell, remove_cell)
local mat_len = 0
local name = CellFactory_GetName(mat_len)
while name ~= "unknown" do
mat_len = mat_len + 1
name = CellFactory_GetName(mat_len)
end
blob_guy.init_particle_world_state(grid_world, material_list, mat_len, construct_cell, remove_cell)
end

View file

@ -42,7 +42,7 @@ impl State {
}
}
if self.blobs.is_empty() {
self.blobs.push(Blob::new(256.0, -64.0 - 32.0));
self.blobs.push(Blob::new(256.0, -(64.0 + 32.0)));
}
'upper: for blob in self.blobs.iter_mut() {
blob.update_pos()?;

View file

@ -48,14 +48,19 @@ fn init_particle_world_state(lua: LuaState) -> eyre::Result<()> {
let chunk_map_ptr = unsafe { world_ptr.as_ref().unwrap() }.chunk_map.cell_array;
let chunk_map = unsafe { std::slice::from_raw_parts(chunk_map_ptr, 512 * 512) };
let material_list_ptr = lua.to_integer(2) as *const noita::ntypes::CellData;
let construct_ptr = lua.to_integer(3) as *const c_void;
let remove_ptr = lua.to_integer(4) as *const c_void;
let mat_len = lua.to_integer(3);
let material_list =
unsafe { std::slice::from_raw_parts(material_list_ptr, mat_len as usize) };
let construct_ptr = lua.to_integer(4) as *const c_void;
let remove_ptr = lua.to_integer(5) as *const c_void;
let blob_guy = noita_api::raw::cell_factory_get_type("blob_guy".into())? as u16;
state.blob_guy = blob_guy;
let pws = ParticleWorldState {
world_ptr,
chunk_map,
blob_ptr: unsafe { material_list_ptr.offset(blob_guy as isize) },
material_list_ptr,
material_list,
construct_ptr,
remove_ptr,
};

View file

@ -3,19 +3,20 @@ use crate::chunk::{CellType, Chunk};
#[cfg(target_arch = "x86")]
use std::arch::asm;
use std::{ffi::c_void, mem, ptr};
pub(crate) mod ntypes;
//pub(crate) mod pixel;
pub mod ntypes;
#[derive(Default)]
pub(crate) struct ParticleWorldState {
pub(crate) world_ptr: *const ntypes::GridWorld,
pub(crate) chunk_map: &'static [*mut &'static mut [*const ntypes::Cell; 512 * 512]],
//pub(crate) material_list_ptr: *const ntypes::CellData,
pub(crate) blob_ptr: *const ntypes::CellData,
pub(crate) construct_ptr: *const c_void,
pub(crate) remove_ptr: *const c_void,
pub struct ParticleWorldState {
pub world_ptr: *const ntypes::GridWorld,
pub chunk_map: &'static [*mut &'static mut [*const ntypes::Cell; 512 * 512]],
pub material_list_ptr: *const ntypes::CellData,
pub material_list: &'static [ntypes::CellData],
pub blob_ptr: *const ntypes::CellData,
pub construct_ptr: *const c_void,
pub remove_ptr: *const c_void,
}
unsafe impl Sync for ParticleWorldState {}
unsafe impl Send for ParticleWorldState {}
#[allow(clippy::result_unit_err)]
impl ParticleWorldState {
fn create_cell(
&self,
@ -111,31 +112,25 @@ impl ParticleWorldState {
unsafe { pixel.as_ref() }
}
/*fn get_cell_raw_mut<'a>(
pub fn get_cell_raw_mut<'a>(
&self,
x: isize,
y: isize,
pixel_array:&'a mut &'a mut [*const ntypes::Cell; 512 * 512],
pixel_array: &'a mut &'a mut [*const ntypes::Cell; 512 * 512],
) -> &'a mut *const ntypes::Cell {
let index = (y << 9) | x;
&mut pixel_array[index as usize]
}
fn get_cell_material_id(&self, cell: &ntypes::Cell) -> u16 {
let mat_ptr = cell.material_ptr();
let offset = unsafe { mat_ptr.offset_from(self.material_list_ptr) };
pub fn get_cell_material_id(&self, cell: &ntypes::Cell) -> u16 {
let offset = unsafe { cell.material_ptr.offset_from(self.material_list_ptr) };
offset as u16
}*/
}
fn get_cell_type(&self, cell: &ntypes::Cell) -> Option<ntypes::CellType> {
unsafe { Some(cell.material_ptr.as_ref()?.cell_type) }
}
pub(crate) unsafe fn encode_area(
&self,
x: isize,
y: isize,
chunk: &mut Chunk,
) -> Result<(), ()> {
///# Safety
pub unsafe fn encode_area(&self, x: isize, y: isize, chunk: &mut Chunk) -> Result<(), ()> {
let (shift_x, shift_y, pixel_array) = self.set_chunk(x, y)?;
let pixel_array = unsafe { pixel_array.as_mut() }.unwrap();
let mut modified = false;
@ -173,7 +168,8 @@ impl ParticleWorldState {
chunk.modified = modified;
Ok(())
}
pub(crate) unsafe fn decode_area(&self, x: isize, y: isize, chunk: &Chunk) -> Result<(), ()> {
///# Safety
pub unsafe fn decode_area(&self, x: isize, y: isize, chunk: &Chunk) -> Result<(), ()> {
if !chunk.modified {
return Ok(());
}
@ -223,4 +219,18 @@ impl ParticleWorldState {
}
Ok(())
}
///# Safety
pub unsafe fn clone_chunks(&self) -> Vec<(usize, usize, [Option<ntypes::Cell>; 512 * 512])> {
self.chunk_map
.iter()
.enumerate()
.filter_map(|(i, c)| {
unsafe { c.as_ref() }.map(|c| {
let x = i % 512;
let y = i / 512;
(x, y, c.map(|p| unsafe { p.as_ref() }.cloned()))
})
})
.collect::<Vec<_>>()
}
}

View file

@ -17,34 +17,6 @@ pub struct Colour {
a: u8,
}
/*impl Debug for CellArrayPtr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", unsafe { self.0.as_ref() })
}
}
impl Debug for CellPtr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", unsafe { self.0.as_ref() })
}
}
impl Debug for CellDataPtr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", unsafe { self.0.as_ref() })
}
}
#[repr(C)]
pub(crate) struct CellDataPtr(pub *const CellData);
#[repr(C)]
pub(crate) struct CellPtr(pub *const Cell);
#[repr(C)]
pub(crate) struct CellArrayPtr(pub *mut CellPtr);
#[derive(Debug)]
#[repr(C)]
pub(crate) struct ChunkArray(pub *mut CellArrayPtr);*/
#[repr(C)]
#[derive(Debug)]
pub struct ChunkMap {
@ -125,7 +97,6 @@ impl Debug for StdString {
}
#[repr(u32)]
#[derive(Debug, PartialEq, Clone, Copy)]
#[expect(dead_code)]
pub enum CellType {
None = 0,
Liquid = 1,
@ -483,7 +454,7 @@ impl CellVTable {
}
#[repr(C)]
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Cell {
pub vtable: *const CellVTable,

View file

@ -1,78 +0,0 @@
#[repr(C, packed)]
pub(crate) struct NoitaPixelRun {
pub(crate) length: u16,
pub(crate) material: u16,
pub(crate) flags: u8,
}
/// Copied from proxy.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct RawPixel {
pub material: u16,
pub flags: u8,
}
/// Copied from proxy.
/// Stores a run of pixels.
/// Not specific to Noita side - length is an actual length
#[derive(Debug)]
pub(crate) struct PixelRun<Pixel> {
pub length: u32,
pub data: Pixel,
}
/// Copied from proxy.
/// Converts a normal sequence of pixels to a run-length-encoded one.
pub(crate) struct PixelRunner<Pixel> {
pub(crate) current_pixel: Option<Pixel>,
pub(crate) current_run_len: u32,
pub(crate) runs: Vec<PixelRun<Pixel>>,
}
impl<Pixel: Eq + Copy> Default for PixelRunner<Pixel> {
fn default() -> Self {
Self::new()
}
}
impl<Pixel: Eq + Copy> PixelRunner<Pixel> {
pub(crate) fn new() -> Self {
Self {
current_pixel: None,
current_run_len: 0,
runs: Vec::new(),
}
}
pub(crate) fn put_pixel(&mut self, pixel: Pixel) {
if let Some(current) = self.current_pixel {
if pixel != current {
self.runs.push(PixelRun {
length: self.current_run_len,
data: current,
});
self.current_pixel = Some(pixel);
self.current_run_len = 1;
} else {
self.current_run_len += 1;
}
} else {
self.current_pixel = Some(pixel);
self.current_run_len = 1;
}
}
pub(crate) fn build(&mut self) -> &[PixelRun<Pixel>] {
if self.current_run_len > 0 {
self.runs.push(PixelRun {
length: self.current_run_len,
data: self.current_pixel.expect("has current pixel"),
});
}
&mut self.runs
}
pub(crate) fn clear(&mut self) {
self.current_pixel = None;
self.current_run_len = 0;
self.runs.clear();
}
}