revert some things

This commit is contained in:
bgkillas 2025-07-04 17:03:49 -04:00
parent dcd7c049ea
commit 2fed3cc807
5 changed files with 58 additions and 61 deletions

View file

@ -25,16 +25,12 @@ end
function OnWorldInitialized() function OnWorldInitialized()
started = 60 started = 60
local grid_world = world_ffi.get_grid_world() local grid_world = world_ffi.get_grid_world()
local chunk_map = grid_world.vtable.get_chunk_map(grid_world)
grid_world = tonumber(ffi.cast("intptr_t", grid_world)) grid_world = tonumber(ffi.cast("intptr_t", grid_world))
chunk_map = tonumber(ffi.cast("intptr_t", chunk_map))
local material_list = tonumber(ffi.cast("intptr_t", world_ffi.get_material_ptr(0))) local material_list = tonumber(ffi.cast("intptr_t", world_ffi.get_material_ptr(0)))
local world_info = np.GetWorldInfo() local world_info = np.GetWorldInfo()
local construct_cell = tonumber(ffi.cast("intptr_t", world_info.construct_cell)) local construct_cell = tonumber(ffi.cast("intptr_t", world_info.construct_cell))
local remove_cell = tonumber(ffi.cast("intptr_t", world_info.remove_cell)) local remove_cell = tonumber(ffi.cast("intptr_t", world_info.remove_cell))
local i = 0 blob_guy.init_particle_world_state(grid_world, chunk_map, material_list, construct_cell, remove_cell)
local name = CellFactory_GetName(i)
while name ~= "unknown" do
i = i + 1
name = CellFactory_GetName(i)
end
blob_guy.init_particle_world_state(grid_world, material_list, i, construct_cell, remove_cell)
end end

View file

@ -18,7 +18,7 @@ impl Pos {
} }
} }
const OFFSET: isize = CHUNK_AMOUNT as isize / 2; const OFFSET: isize = CHUNK_AMOUNT as isize / 2;
impl<'a> State<'a> { impl State {
pub fn update(&mut self) -> eyre::Result<()> { pub fn update(&mut self) -> eyre::Result<()> {
if noita_api::raw::input_is_mouse_button_just_down(1)? { if noita_api::raw::input_is_mouse_button_just_down(1)? {
let (x, y) = noita_api::raw::debug_get_mouse_world()?; let (x, y) = noita_api::raw::debug_get_mouse_world()?;

View file

@ -4,7 +4,6 @@ pub mod noita;
use crate::blob_guy::Blob; use crate::blob_guy::Blob;
use crate::chunk::Chunk; use crate::chunk::Chunk;
use crate::noita::{ParticleWorldState, ntypes}; use crate::noita::{ParticleWorldState, ntypes};
use eyre::eyre;
use noita_api::add_lua_fn; use noita_api::add_lua_fn;
use noita_api::lua::LUA; use noita_api::lua::LUA;
use noita_api::lua::LuaState; use noita_api::lua::LuaState;
@ -17,14 +16,14 @@ use std::mem::MaybeUninit;
use std::sync::LazyLock; use std::sync::LazyLock;
pub const CHUNK_SIZE: usize = 128; pub const CHUNK_SIZE: usize = 128;
pub const CHUNK_AMOUNT: usize = 3; pub const CHUNK_AMOUNT: usize = 3;
struct State<'a> { struct State {
particle_world_state: MaybeUninit<ParticleWorldState<'a>>, particle_world_state: MaybeUninit<ParticleWorldState>,
blobs: SmallVec<[Blob; 8]>, blobs: SmallVec<[Blob; 8]>,
world: [Chunk; CHUNK_AMOUNT * CHUNK_AMOUNT], world: [Chunk; CHUNK_AMOUNT * CHUNK_AMOUNT],
blob_guy: u16, blob_guy: u16,
} }
thread_local! { thread_local! {
static STATE: LazyCell<RefCell<State<'static>>> = LazyCell::new(|| { static STATE: LazyCell<RefCell<State>> = LazyCell::new(|| {
State { State {
particle_world_state: MaybeUninit::uninit(),blobs: Default::default(),world: Default::default(),blob_guy: 0,} particle_world_state: MaybeUninit::uninit(),blobs: Default::default(),world: Default::default(),blob_guy: 0,}
}.into()); }.into());
@ -54,22 +53,18 @@ fn init_particle_world_state(lua: LuaState) -> eyre::Result<()> {
STATE.with(|state| { STATE.with(|state| {
let mut state = state.borrow_mut(); let mut state = state.borrow_mut();
let world_ptr = lua.to_integer(1) as *const ntypes::GridWorld; let world_ptr = lua.to_integer(1) as *const ntypes::GridWorld;
let Some(ptr) = (unsafe { world_ptr.as_ref() }) else { let chunk_map_ptr = unsafe { (lua.to_integer(2) as *mut c_void).offset(8) };
return Err(eyre!("world ptr borken")); let material_list_ptr = lua.to_integer(3) as *const ntypes::CellData;
}; let construct_ptr = lua.to_integer(5) as *mut c_void;
let material_list_ptr = lua.to_integer(2) as *const ntypes::CellData; let remove_ptr = lua.to_integer(6) as *mut c_void;
let material_list_len = lua.to_integer(3) as usize;
let material_list =
unsafe { std::slice::from_raw_parts(material_list_ptr, material_list_len) };
let construct_ptr = lua.to_integer(4) as *mut c_void;
let remove_ptr = lua.to_integer(5) as *mut c_void;
let blob_guy = noita_api::raw::cell_factory_get_type("blob_guy".into())? as u16; let blob_guy = noita_api::raw::cell_factory_get_type("blob_guy".into())? as u16;
state.blob_guy = blob_guy; state.blob_guy = blob_guy;
let pws = ParticleWorldState { let pws = ParticleWorldState {
world_ptr, world_ptr,
chunk_arr: ntypes::ChunkArray(ptr.chunk_map.cell_array.0), chunk_map_ptr,
material_list, material_list_ptr,
blob_guy, blob_guy,
blob_ptr: unsafe { material_list_ptr.offset(blob_guy as isize) },
pixel_array: Default::default(), pixel_array: Default::default(),
construct_ptr, construct_ptr,
remove_ptr, remove_ptr,

View file

@ -8,43 +8,54 @@ use std::{mem, ptr};
pub(crate) mod ntypes; pub(crate) mod ntypes;
//pub(crate) mod pixel; //pub(crate) mod pixel;
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct ParticleWorldState<'a> { pub(crate) struct ParticleWorldState {
pub(crate) world_ptr: *const ntypes::GridWorld, pub(crate) world_ptr: *const ntypes::GridWorld,
pub(crate) chunk_arr: ntypes::ChunkArray, pub(crate) chunk_map_ptr: *const c_void,
pub(crate) material_list: &'a [ntypes::CellData], pub(crate) material_list_ptr: *const ntypes::CellData,
pub(crate) blob_guy: u16, pub(crate) blob_guy: u16,
pub(crate) pixel_array: &'a mut [ntypes::CellPtr], pub(crate) blob_ptr: *const ntypes::CellData,
pub(crate) pixel_array: *const c_void,
pub(crate) construct_ptr: *const c_void, pub(crate) construct_ptr: *const c_void,
pub(crate) remove_ptr: *const c_void, pub(crate) remove_ptr: *const c_void,
pub(crate) shift_x: isize, pub(crate) shift_x: isize,
pub(crate) shift_y: isize, pub(crate) shift_y: isize,
} }
impl<'a> ParticleWorldState<'a> { impl ParticleWorldState {
pub fn set_chunk(&mut self, x: isize, y: isize) -> eyre::Result<()> { pub fn set_chunk(&mut self, x: isize, y: isize) -> eyre::Result<()> {
const SCALE: isize = (512 / CHUNK_SIZE as isize).ilog2() as isize; const SCALE: isize = (512 / CHUNK_SIZE as isize).ilog2() as isize;
self.shift_x = (x * CHUNK_SIZE as isize).rem_euclid(512); self.shift_x = (x * CHUNK_SIZE as isize).rem_euclid(512);
self.shift_y = (y * 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); let chunk_index = ((((y >> SCALE) - 256) & 511) << 9) | (((x >> SCALE) - 256) & 511);
let array = self.chunk_arr.index(chunk_index); // Deref 1/3
if array.0.is_null() { let chunk_arr = unsafe { self.chunk_map_ptr.cast::<*const c_void>().read() };
return Err(eyre!(format!("cant find chunk index {}", chunk_index))); // Deref 2/3
let chunk = unsafe {
chunk_arr
.offset(chunk_index * 4)
.cast::<*const c_void>()
.read()
};
if chunk.is_null() {
return Err(eyre!("could not find chunk {}", chunk_index));
} }
self.pixel_array = unsafe { std::slice::from_raw_parts_mut(array.0, 512 * 512) }; // Deref 3/3
let pixel_array = unsafe { chunk.cast::<*const c_void>().read() };
self.pixel_array = pixel_array;
Ok(()) Ok(())
} }
pub fn get_cell_raw(&self, x: isize, y: isize) -> Option<&ntypes::Cell> { pub fn get_cell_raw(&self, x: isize, y: isize) -> Option<&ntypes::Cell> {
let x = x + self.shift_x; let x = x + self.shift_x;
let y = y + self.shift_y; let y = y + self.shift_y;
let index = ((y & 511) << 9) | (x & 511); let index = ((y & 511) << 9) | (x & 511);
let pixel = &self.pixel_array[index as usize]; let pixel = unsafe { self.pixel_array.offset(index * 4) };
if pixel.0.is_null() { if pixel.is_null() {
return None; return None;
} }
unsafe { pixel.0.as_ref() } unsafe { pixel.cast::<*const ntypes::Cell>().read().as_ref() }
} }
fn get_cell_material_id(&self, cell: &ntypes::Cell) -> u16 { fn get_cell_material_id(&self, cell: &ntypes::Cell) -> u16 {
let mat_ptr = cell.material_ptr(); let mat_ptr = cell.material_ptr();
let offset = unsafe { mat_ptr.0.offset_from(self.material_list.as_ptr()) }; let offset = unsafe { mat_ptr.0.offset_from(self.material_list_ptr) };
offset as u16 offset as u16
} }
@ -110,41 +121,41 @@ impl<'a> ParticleWorldState<'a> {
let x = x + self.shift_x; let x = x + self.shift_x;
let y = y + self.shift_y; let y = y + self.shift_y;
let index = ((y & 511) << 9) | (x & 511); let index = ((y & 511) << 9) | (x & 511);
&mut self.pixel_array[index as usize] let pixel = unsafe { self.pixel_array.offset(index * 4) };
pixel as *mut *mut ntypes::Cell
}}; }};
} }
match pixel { match pixel {
CellType::Blob => { CellType::Blob => {
noita_api::game_print("a");
let x = x + i; let x = x + i;
let y = y + j; let y = y + j;
let cell = get_cell_raw_mut!(i, j);
unsafe { 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, x, y);
remove_cell(self.world_ptr, self.remove_ptr, cell.0, x, y); *cell = ptr::null_mut();
cell.0 = ptr::null_mut();
} }
let src = create_cell( let src =
self.world_ptr, create_cell(self.world_ptr, self.construct_ptr, x, y, self.blob_ptr);
self.construct_ptr,
x,
y,
&self.material_list[self.blob_guy as usize],
);
if !src.is_null() { if !src.is_null() {
let liquid: &mut ntypes::LiquidCell = let liquid: &mut ntypes::LiquidCell =
&mut *src.cast::<ntypes::LiquidCell>(); &mut *src.cast::<ntypes::LiquidCell>();
liquid.is_static = true; liquid.is_static = true;
cell.0 = src; *cell = src;
} }
} }
} }
CellType::Remove => { CellType::Remove => {
noita_api::game_print("b");
let x = x + i; let x = x + i;
let y = y + j; let y = y + j;
let cell = get_cell_raw_mut!(i, j); let cell = get_cell_raw_mut!(i, j);
if !cell.0.is_null() { unsafe {
remove_cell(self.world_ptr, self.remove_ptr, cell.0, x, y); if !(*cell).is_null() {
cell.0 = ptr::null_mut(); remove_cell(self.world_ptr, self.remove_ptr, *cell, x, y);
*cell = ptr::null_mut();
}
} }
} }
_ => {} _ => {}
@ -158,7 +169,7 @@ fn create_cell(
construct_ptr: *const c_void, construct_ptr: *const c_void,
x: isize, x: isize,
y: isize, y: isize,
material: &ntypes::CellData, material: *const ntypes::CellData,
//_memory: *const c_void, //_memory: *const c_void,
) -> *mut ntypes::Cell { ) -> *mut ntypes::Cell {
#[cfg(target_arch = "x86")] #[cfg(target_arch = "x86")]
@ -174,7 +185,7 @@ fn create_cell(
world = in(reg) world_ptr, world = in(reg) world_ptr,
x = in(reg) x, x = in(reg) x,
y = in(reg) y, y = in(reg) y,
material = in(reg) material as *const ntypes::CellData, material = in(reg) material,
construct = in(reg) construct_ptr, construct = in(reg) construct_ptr,
clobber_abi("C"), clobber_abi("C"),
out("eax") cell_ptr, out("eax") cell_ptr,
@ -216,4 +227,4 @@ fn remove_cell(
std::hint::black_box((x, y, cell, world_ptr, remove_ptr)); std::hint::black_box((x, y, cell, world_ptr, remove_ptr));
unreachable!() unreachable!()
} }
} }

View file

@ -31,11 +31,6 @@ impl Debug for CellDataPtr {
write!(f, "{:?}", unsafe { self.0.as_ref() }) write!(f, "{:?}", unsafe { self.0.as_ref() })
} }
} }
impl ChunkArray {
pub fn index(&self, index: isize) -> CellArrayPtr {
unsafe { self.0.offset(index).read() }
}
}
#[repr(C)] #[repr(C)]
pub(crate) struct CellPtr(pub *const Cell); pub(crate) struct CellPtr(pub *const Cell);
@ -139,7 +134,7 @@ pub enum CellType {
#[repr(C)] #[repr(C)]
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct CellData { pub(crate) struct CellData {
name: StdString, pub name: StdString,
ui_name: StdString, ui_name: StdString,
material_type: isize, material_type: isize,
id_2: isize, id_2: isize,