fix shifting

This commit is contained in:
bgkillas 2025-06-29 22:50:21 -04:00
parent 04c13e8641
commit a024afceae
3 changed files with 25 additions and 20 deletions

View file

@ -7,19 +7,23 @@ local world_ffi = require("noitapatcher.nsew.world_ffi")
local ffi = require("ffi")
--local nxml = dofile_once("mods/blob_guy/nxml.lua")
ModMaterialsFileAdd("mods/blob_guy/materials.xml")
local started = false
local started = -1
function OnWorldPostUpdate()
if not started then
if started == -1 then
return
end
local start_time = GameGetRealWorldTimeSinceStarted()
if started > 0 then
started = started - 1
return
end
--local start_time = GameGetRealWorldTimeSinceStarted()
blob_guy.update()
local end_time = GameGetRealWorldTimeSinceStarted()
local delta = (end_time - start_time) * 1000000
GamePrint(math.floor(delta + 0.5))
--local end_time = GameGetRealWorldTimeSinceStarted()
--local delta = (end_time - start_time) * 1000000
--GamePrint(math.floor(delta + 0.5))
end
function OnWorldInitialized()
started = true
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))

View file

@ -21,7 +21,8 @@ const OFFSET: isize = CHUNK_AMOUNT as isize / 2;
impl State {
pub fn update(&mut self) -> eyre::Result<()> {
if self.blobs.is_empty() {
self.blobs.push(Blob::new(128.0 + 16.0, -128.0 - 16.0));
self.blobs
.push(Blob::new(128.0 + 16.0, -(128.0 + 64.0 + 16.0)));
}
'upper: for blob in self.blobs.iter_mut() {
let c = blob.pos.to_chunk();
@ -54,14 +55,14 @@ impl State {
const SIZE: usize = 8;
pub struct Blob {
pub pos: Pos,
pixels: [Pos; SIZE * SIZE],
pixels: [Option<Pos>; SIZE * SIZE],
}
impl Blob {
pub fn update(&mut self, map: &mut [Chunk; 9]) {
let mut last = ChunkPos::new(isize::MAX, isize::MAX);
let mut k = 0;
let start = self.pos.to_chunk();
for p in self.pixels.iter_mut() {
for p in self.pixels.iter_mut().flatten() {
//p.y += 1.0;
let c = p.to_chunk();
if c != last {
@ -84,7 +85,7 @@ impl Blob {
pixels: std::array::from_fn(|i| {
let a = (i / SIZE) as f64 - SIZE as f64 / 2.0 + 0.5;
let b = (i % SIZE) as f64 - SIZE as f64 / 2.0 + 0.5;
Pos::new(x + a, y + b)
Some(Pos::new(x + a, y + b))
}),
}
}

View file

@ -25,7 +25,6 @@ impl ParticleWorldState {
material: *const c_void,
_memory: *const c_void,
) -> *mut ntypes::Cell {
//construct_cell(self.world_ptr, x, y, material, self.blob_guy).unwrap_or_default()
unsafe {
let cell_ptr: *mut ntypes::Cell;
asm!(
@ -47,7 +46,6 @@ impl ParticleWorldState {
}
}
fn remove_cell(&mut self, cell: *mut ntypes::Cell, x: isize, y: isize) {
//let _ = remove_cell(self.world_ptr, cell, x, y);
unsafe {
asm!(
"mov ecx, {world}",
@ -67,8 +65,10 @@ impl ParticleWorldState {
}
fn set_chunk(&mut self, x: isize, y: isize) -> bool {
const SCALE: isize = (512 / CHUNK_SIZE as isize).ilog2() as isize;
self.shift_x = CHUNK_SIZE as isize * x.rem_euclid(SCALE);
self.shift_y = CHUNK_SIZE as isize * y.rem_euclid(SCALE);
self.shift_x =
CHUNK_SIZE as isize * ((x * CHUNK_SIZE as isize).rem_euclid(512) / CHUNK_SIZE as isize);
self.shift_y =
CHUNK_SIZE as isize * ((y * CHUNK_SIZE as isize).rem_euclid(512) / CHUNK_SIZE as isize);
let chunk_index = (((((y >> SCALE) - 256) & 511) << 9) | (((x >> SCALE) - 256) & 511)) * 4;
// Deref 1/3
let chunk_arr = unsafe { self.chunk_map_ptr.cast::<*const c_void>().read() };
@ -85,17 +85,17 @@ impl ParticleWorldState {
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 pixel = unsafe { self.pixel_array.offset(((y << 9) | x) * 4) };
let pixel = unsafe { self.pixel_array.offset((((y & 511) << 9) | (x & 511)) * 4) };
if pixel.is_null() {
return None;
}
unsafe { pixel.cast::<*const ntypes::Cell>().read().as_ref() }
}
fn get_cell_raw_mut_nil(&mut self, x: isize, y: isize) -> *mut *mut ntypes::Cell {
fn get_cell_raw_mut(&mut self, x: isize, y: isize) -> *mut *mut ntypes::Cell {
let x = x + self.shift_x;
let y = y + self.shift_y;
let pixel = unsafe { self.pixel_array.offset(((y << 9) | x) * 4) };
let pixel = unsafe { self.pixel_array.offset((((y & 511) << 9) | (x & 511)) * 4) };
pixel as *mut *mut ntypes::Cell
}
fn get_cell_material_id(&self, cell: &ntypes::Cell) -> u16 {
@ -150,7 +150,7 @@ impl ParticleWorldState {
let x = x + i;
let y = y + j;
unsafe {
let cell = self.get_cell_raw_mut_nil(i, j);
let cell = self.get_cell_raw_mut(i, j);
if !(*cell).is_null() {
self.remove_cell(*cell, x, y);
*cell = ptr::null_mut();
@ -168,7 +168,7 @@ impl ParticleWorldState {
let x = x + i;
let y = y + j;
unsafe {
let cell = self.get_cell_raw_mut_nil(i, j);
let cell = self.get_cell_raw_mut(i, j);
if !(*cell).is_null() {
self.remove_cell(*cell, x, y);
*cell = ptr::null_mut();