prob closer to what im meant to do

This commit is contained in:
bgkillas 2025-07-03 18:05:41 -04:00
parent c905ef4de1
commit 434acff594
4 changed files with 31 additions and 51 deletions

View file

@ -33,7 +33,7 @@ impl<'a> State<'a> {
(y.floor() as isize).rem_euclid(CHUNK_SIZE as isize),
) {
noita_api::print(format!("{cell:?}"));
noita_api::print(format!("{:?}", cell.material_ptr.as_ref()));
noita_api::print(format!("{:?}", unsafe { cell.material_ptr.0.as_ref() }));
} else {
noita_api::print("mat nil");
}

View file

@ -67,7 +67,7 @@ fn init_particle_world_state(lua: LuaState) -> eyre::Result<()> {
state.blob_guy = blob_guy;
let pws = ParticleWorldState {
world_ptr,
chunk_arr: unsafe { ptr.chunk_map.cell_array.read() },
chunk_arr: ntypes::ChunkArray(ptr.chunk_map.cell_array.0),
material_list,
blob_guy,
pixel_array: Default::default(),

View file

@ -9,11 +9,11 @@ pub(crate) mod ntypes;
//pub(crate) mod pixel;
#[derive(Debug)]
pub(crate) struct ParticleWorldState<'a> {
pub(crate) world_ptr: *const ntypes::GridWorld<'a>,
pub(crate) chunk_arr: ntypes::ChunkArray<'a>,
pub(crate) world_ptr: *const ntypes::GridWorld,
pub(crate) chunk_arr: ntypes::ChunkArray,
pub(crate) material_list: &'a [ntypes::CellData],
pub(crate) blob_guy: u16,
pub(crate) pixel_array: usize,
pub(crate) pixel_array: &'a mut [ntypes::CellPtr],
pub(crate) construct_ptr: *const c_void,
pub(crate) remove_ptr: *const c_void,
pub(crate) shift_x: isize,
@ -25,23 +25,22 @@ impl<'a> ParticleWorldState<'a> {
self.shift_x = (x * CHUNK_SIZE as isize).rem_euclid(512);
self.shift_y = (y * CHUNK_SIZE as isize).rem_euclid(512);
let chunk_index = ((((y >> SCALE) - 256) & 511) << 9) | (((x >> SCALE) - 256) & 511);
if self.chunk_arr[chunk_index as usize].0.is_null() {
let array = self.chunk_arr.index(chunk_index);
if array.0.is_null() {
return Err(eyre!(format!("cant find chunk index {}", chunk_index)));
}
self.pixel_array = chunk_index as usize;
self.pixel_array = unsafe { std::slice::from_raw_parts_mut(array.0, 512 * 512) };
Ok(())
}
pub fn get_cell_raw(&self, x: isize, y: isize) -> Option<&ntypes::Cell> {
let x = x + self.shift_x;
let y = y + self.shift_y;
let index = ((y & 511) << 9) | (x & 511);
let pixel = &unsafe { self.chunk_arr[self.pixel_array].0.as_ref()? }[index as usize];
if pixel.is_null() {
noita_api::print("b");
let pixel = &self.pixel_array[index as usize];
if pixel.0.is_null() {
return None;
}
pixel.as_ref()
unsafe { pixel.0.as_ref() }
}
fn get_cell_material_id(&self, cell: &ntypes::Cell) -> u16 {
let mat_ptr = cell.material_ptr();
@ -50,7 +49,7 @@ impl<'a> ParticleWorldState<'a> {
}
fn get_cell_type(&self, cell: &ntypes::Cell) -> Option<ntypes::CellType> {
Some(cell.material_ptr().as_ref()?.cell_type)
Some(unsafe { cell.material_ptr().0.as_ref()? }.cell_type)
}
pub(crate) unsafe fn encode_area(
@ -111,8 +110,7 @@ impl<'a> ParticleWorldState<'a> {
let x = x + self.shift_x;
let y = y + self.shift_y;
let index = ((y & 511) << 9) | (x & 511);
let array = self.chunk_arr[self.pixel_array].0.as_mut().unwrap();
&mut array[index as usize]
&mut self.pixel_array[index as usize]
}};
}
match pixel {
@ -121,7 +119,7 @@ impl<'a> ParticleWorldState<'a> {
let y = y + j;
unsafe {
let cell = get_cell_raw_mut!(i, j);
if !(*cell).is_null() {
if !cell.0.is_null() {
remove_cell(self.world_ptr, self.remove_ptr, cell.0, x, y);
cell.0 = ptr::null_mut();
}
@ -143,12 +141,10 @@ impl<'a> ParticleWorldState<'a> {
CellType::Remove => {
let x = x + i;
let y = y + j;
unsafe {
let cell = get_cell_raw_mut!(i, j);
if !(*cell).is_null() {
remove_cell(self.world_ptr, self.remove_ptr, cell.0, x, y);
cell.0 = ptr::null_mut();
}
let cell = get_cell_raw_mut!(i, j);
if !cell.0.is_null() {
remove_cell(self.world_ptr, self.remove_ptr, cell.0, x, y);
cell.0 = ptr::null_mut();
}
}
_ => {}

View file

@ -7,7 +7,6 @@ use std::ffi::{c_char, c_void};
#[cfg(target_arch = "x86")]
use std::arch::asm;
use std::fmt::{Debug, Display, Formatter};
use std::ops::Index;
#[repr(C)]
#[derive(Debug, Clone)]
pub struct Colour {
@ -32,44 +31,29 @@ impl Debug for CellDataPtr {
write!(f, "{:?}", unsafe { self.0.as_ref() })
}
}
impl CellPtr {
pub fn is_null(&self) -> bool {
self.0.is_null()
}
pub fn as_ref(&self) -> Option<&Cell> {
unsafe { self.0.as_ref() }
}
}
impl CellDataPtr {
pub fn as_ref(&self) -> Option<&CellData> {
unsafe { self.0.as_ref() }
}
}
impl<'a> Index<usize> for ChunkArray<'a> {
type Output = CellArrayPtr;
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
impl ChunkArray {
pub fn index(&self, index: isize) -> CellArrayPtr {
unsafe { self.0.offset(index).read() }
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub(crate) struct CellPtr(pub *const Cell);
#[repr(C)]
#[derive(Copy, Clone)]
pub(crate) struct CellDataPtr(pub *const CellData);
#[repr(C)]
pub(crate) struct CellArrayPtr(pub *mut [CellPtr; 512 * 512]);
pub(crate) struct CellArrayPtr(pub *mut CellPtr);
#[derive(Debug)]
#[repr(C)]
pub(crate) struct ChunkArray<'a>(pub &'a [CellArrayPtr; 512 * 512]);
pub(crate) struct ChunkArray(pub *mut CellArrayPtr);
#[repr(C)]
pub struct ChunkMap<'a> {
pub struct ChunkMap {
unknown: [isize; 2],
pub cell_array: *const ChunkArray<'a>,
pub cell_array: ChunkArray,
unknown2: [isize; 8],
}
@ -81,7 +65,7 @@ pub struct GridWorldVtable {
}
#[allow(dead_code)]
impl GridWorldVtable {
pub fn get_chunk_map(&self) -> *const ChunkMap<'_> {
pub fn get_chunk_map(&self) -> *const ChunkMap {
#[cfg(target_arch = "x86")]
unsafe {
let ret: *const ChunkMap;
@ -101,11 +85,11 @@ impl GridWorldVtable {
}
}
#[repr(C)]
pub struct GridWorld<'a> {
pub struct GridWorld {
vtable: *const GridWorldVtable,
unknown: [isize; 318],
world_update_count: isize,
pub chunk_map: ChunkMap<'a>,
pub chunk_map: ChunkMap,
unknown2: [isize; 41],
m_thread_impl: *const c_void,
}
@ -527,7 +511,7 @@ pub(crate) struct LiquidCell {
}
impl Cell {
pub(crate) fn material_ptr(&self) -> CellDataPtr {
self.material_ptr
pub(crate) fn material_ptr(&self) -> &CellDataPtr {
&self.material_ptr
}
}