Cleanup a bit.

This commit is contained in:
IQuant 2024-09-27 17:46:42 +03:00
parent fb4a392b97
commit b9291ba212
4 changed files with 7 additions and 93 deletions

View file

@ -36,7 +36,7 @@ extern "C" fn init_particle_world_state(lua: *mut lua_State) -> c_int {
STATE.with(|state| {
state.borrow_mut().particle_world_state = Some(ParticleWorldState {
world_ptr: world_pointer as *mut c_void,
_world_ptr: world_pointer as *mut c_void,
chunk_map_ptr: chunk_map_pointer as *mut c_void,
material_list_ptr: material_list_pointer as _,
});
@ -44,19 +44,6 @@ extern "C" fn init_particle_world_state(lua: *mut lua_State) -> c_int {
0
}
extern "C" fn get_pixel_pointer(lua: *mut lua_State) -> c_int {
let x = unsafe { LUA.lua_tointeger(lua, 1) } as i32;
let y = unsafe { LUA.lua_tointeger(lua, 2) } as i32;
STATE.with(|state| {
let state = state.borrow_mut();
let pws = state.particle_world_state.as_ref().unwrap();
// let pixel_pointer = pws.get_cell_raw(x, y);
// unsafe { LUA.lua_pushinteger(lua, pixel_pointer as isize) };
});
1
}
extern "C" fn encode_area(lua: *mut lua_State) -> c_int {
let start_x = unsafe { LUA.lua_tointeger(lua, 1) } as i32;
let start_y = unsafe { LUA.lua_tointeger(lua, 2) } as i32;
@ -81,8 +68,6 @@ pub extern "C" fn luaopen_ewext(lua: *mut lua_State) -> c_int {
LUA.lua_pushcclosure(lua, Some(init_particle_world_state), 0);
LUA.lua_setfield(lua, -2, c"init_particle_world_state".as_ptr());
// LUA.lua_pushcclosure(lua, Some(get_pixel_pointer), 0);
// LUA.lua_setfield(lua, -2, c"get_pixel_pointer".as_ptr());
LUA.lua_pushcclosure(lua, Some(encode_area), 0);
LUA.lua_setfield(lua, -2, c"encode_area".as_ptr());
}

View file

@ -1,4 +1,4 @@
use std::{ffi::c_void, mem, ptr::null};
use std::{ffi::c_void, mem};
mod ntypes;
@ -76,7 +76,7 @@ impl<Pixel: Eq + Copy> PixelRunner<Pixel> {
}
pub(crate) struct ParticleWorldState {
pub(crate) world_ptr: *mut c_void,
pub(crate) _world_ptr: *mut c_void,
pub(crate) chunk_map_ptr: *mut c_void,
pub(crate) material_list_ptr: *const c_void,
}

View file

@ -1,6 +1,6 @@
// Type defs borrowed from NoitaPatcher.
use std::ffi::{c_char, c_void};
use std::ffi::c_char;
pub(crate) const CELLDATA_SIZE: isize = 0x290;
@ -15,6 +15,7 @@ pub(crate) struct StdString {
#[repr(u32)]
#[derive(Debug, PartialEq, Clone, Copy)]
#[expect(dead_code)]
pub(crate) enum CellType {
None = 0,
Liquid = 1,

View file

@ -84,8 +84,8 @@ function world.encode_area(chunk_map, start_x_ini, start_y_ini, end_x_ini, end_y
return nil
end
if width > 256 or height > 256 then
print("Invalid world part, dimension greater than 256")
if width > 128 or height > 128 then
print("Invalid world part, dimension greater than 128")
return nil
end
@ -95,78 +95,6 @@ function world.encode_area(chunk_map, start_x_ini, start_y_ini, end_x_ini, end_y
encoded_area.header.height = height - 1
encoded_area.header.pixel_run_count = ewext.encode_area(start_x_ini, start_y_ini, end_x_ini, end_y_ini, tonumber(ffi.cast("intptr_t", encoded_area.pixel_runs)))
if true then
return encoded_area
end
local run_count = 1
local current_run = encoded_area.pixel_runs[0]
local run_length = 0
local current_material = 0
local current_flags = 0
local y = start_y
while y < end_y do
local x = start_x
while x < end_x do
local material_number = 0
local flags = 0
local ppixel = world_ffi.get_cell(chunk_map, x, y)
local pixel = ppixel[0]
if pixel ~= nil then
local cell_type = pixel.vtable.get_cell_type(pixel)
if cell_type ~= C.CELL_TYPE_SOLID then
local material_ptr = pixel.vtable.get_material(pixel)
material_number = world_ffi.get_material_id(material_ptr)
end
if cell_type == C.CELL_TYPE_LIQUID then
local liquid_cell = ffi.cast(pliquid_cell, pixel)
if liquid_cell.is_static then
flags = bit.bor(flags, C.LIQUID_FLAG_STATIC)
end
end
end
if x == start_x and y == start_y then
-- Initial run
current_material = material_number
current_flags = flags
elseif current_material ~= material_number or current_flags ~= flags then
-- Next run
current_run.length = run_length - 1
current_run.material = current_material
current_run.flags = current_flags
if run_count == C.PIXEL_RUN_MAX then
print("Area too complicated to encode")
return nil
end
current_run = encoded_area.pixel_runs[run_count]
run_count = run_count + 1
run_length = 0
current_material = material_number
current_flags = flags
end
run_length = run_length + 1
x = x + 1
end
y = y + 1
end
current_run.length = run_length - 1
current_run.material = current_material
current_run.flags = current_flags
encoded_area.header.pixel_run_count = run_count
return encoded_area
end