From d83517d31b831189e2255de7fd991640a7947914 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 26 Mar 2015 16:36:44 -0700 Subject: [PATCH] pointer boilerplate this also fixes a subtle bug with Z targets, since `(not 0) == false` in Lua --- Lua/actor lister.lua | 25 +++++++++++-------------- Lua/boilerplate.lua | 8 ++++++++ Lua/room debug.lua | 11 +---------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/Lua/actor lister.lua b/Lua/actor lister.lua index d639d49..bb846fc 100755 --- a/Lua/actor lister.lua +++ b/Lua/actor lister.lua @@ -35,20 +35,16 @@ function T_BL(x, y, s, color) T(x, y, s, color, "bottomleft") end function T_TL(x, y, s, color) T(x, y, s, color, "topleft") end function T_TR(x, y, s, color) T(x, y, s, color, "topright") end -function pmask(p) - return bit.band(p, 0x7FFFFFFF) -end - function get_actor_count(i) return R4(addrs.actor_counts[i].addr) end function get_first_actor(i) - return pmask(R4(addrs.actor_firsts[i].addr)) + return deref(R4(addrs.actor_firsts[i].addr)) end function get_next_actor(addr) - return pmask(R4(addr + actor_t.next.addr)) + return deref(R4(addr + actor_t.next.addr)) end function count_actors() @@ -78,10 +74,10 @@ function iter_actors(counts) addr = get_first_actor(at) else addr = get_next_actor(addr) - if addr == 0 then - T_TR(0, 0, "no actor found", "yellow") - return nil - end + end + if not addr then + T_TR(0, 0, "no actor found", "yellow") + return nil end return at, ai, addr @@ -234,12 +230,13 @@ local function run() local color = name:sub(1,1) == "?" and "red" or "orange" T_BL(0, 7, name, color) - local dmg = pmask(R4(addr + actor_t.damage_table.addr)) - if dmg > 0 then + local dmg = deref(R4(addr + actor_t.damage_table.addr)) + if dmg then for i = 0, 31 do local name = damage_names[i] local str = ('%9s: %02X'):format(name, R1(dmg + i)) local pos = 'topleft' + if i >= 16 then i = i - 16; pos = 'topright' end T(0, i, str, nil, pos) end @@ -283,8 +280,8 @@ local function run() T_BR(0, 0, ("unique:%3i"):format(#seen_strs_sorted)) if any > 0 then - local cursor = pmask(addrs.z_cursor_actor()) - local target = pmask(addrs.z_target_actor()) + local cursor = deref(addrs.z_cursor_actor()) + local target = deref(addrs.z_target_actor()) local z = target or cursor if z then local num = R2(z) diff --git a/Lua/boilerplate.lua b/Lua/boilerplate.lua index d6c003f..c1cbf87 100755 --- a/Lua/boilerplate.lua +++ b/Lua/boilerplate.lua @@ -58,6 +58,14 @@ function printf(fmt, ...) print(fmt:format(...)) end +function is_ptr(ptr) + return bit.band(0xFF800000, ptr) == 0x80000000 +end + +function deref(ptr) + return is_ptr(ptr) and ptr - 0x80000000 +end + function asciize(bytes) local str = "" local seq = false diff --git a/Lua/room debug.lua b/Lua/room debug.lua index 024015f..0d71dbd 100755 --- a/Lua/room debug.lua +++ b/Lua/room debug.lua @@ -6,15 +6,6 @@ function gs2(addr, value) W2(addr, value) end -function is_ptr(ptr) - local head = bit.band(0xFF000000, ptr) - return head == 0x80000000 -end - -function deref(ptr) - return is_ptr(ptr) and ptr - 0x80000000 -end - function dump_half_row(addr) printf("%04X %04X %04X %04X", R2(addr), R2(addr+2), R2(addr+4), R2(addr+6)) end @@ -172,7 +163,7 @@ end local last_addr while true do - local addr = deref(addrs.room_ptr()) + local addr = deref(addrs.room_pointer()) if addr and addr ~= last_addr then console.clear() print('# setup: 00')