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()
started = 60
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))
chunk_map = tonumber(ffi.cast("intptr_t", chunk_map))
local material_list = tonumber(ffi.cast("intptr_t", world_ffi.get_material_ptr(0)))
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))
local i = 0
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)
blob_guy.init_particle_world_state(grid_world, chunk_map, material_list, construct_cell, remove_cell)
end

View file

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

View file

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

View file

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