From 2e42819b9968b3281284122ff517f57e4ee771bc Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 28 Dec 2015 21:48:55 -0800 Subject: [PATCH] move actor iteration funcs to their own file --- Lua/actor lister.lua | 119 ++----------------------------------------- Lua/actors.lua | 115 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 115 deletions(-) create mode 100644 Lua/actors.lua diff --git a/Lua/actor lister.lua b/Lua/actor lister.lua index 39bbd1c..02d0bb0 100755 --- a/Lua/actor lister.lua +++ b/Lua/actor lister.lua @@ -3,12 +3,14 @@ require "boilerplate" require "addrs.init" require "messages" require "classes" +require "actors" local suffix = oot and " oot" or "" local damage_names = require("data.damage names"..suffix) --- check for errors in the actor linked lists -local validate = false +-- creating an object every time is a bit slow, so +-- using a template to offset from will do for now. +local actor_t = Actor(0) -- for figuring out actor variables local debug_mode = false @@ -36,97 +38,6 @@ local debug_watch = mm and { {'unk_78', '%9.3f'}, } or {} --- creating an object every time is a bit slow, so --- using a template to offset from will do for now. -local actor_t = Actor(0) - -function sort_by_key(t) - local sorted = {} - local i = 1 - for k, v in pairs(t) do - sorted[i] = {k=k, v=v} - i = i + 1 - end - table.sort(sorted, function(a, b) return a.k < b.k end) - return sorted -end - -function get_actor_count(i) - return R4(addrs.actor_counts[i].addr) -end - -function get_first_actor(i) - return deref(R4(addrs.actor_firsts[i].addr)) -end - -function get_next_actor(addr) - return deref(R4(addr + actor_t.next.addr)) -end - -function get_prev_actor(addr) - return deref(R4(addr + actor_t.prev.addr)) -end - -function count_actors() - local counts = {} - for i = 0, 11 do - counts[i] = get_actor_count(i) - end - return counts -end - -function iter_actors(counts) - local at, ai = 0, 0 - local addr - - local y = 1 - local complain = function(s) - s = s..(" (%2i:%3i)"):format(at, ai) - T_TR(0, y, "yellow", s) - y = y + 1 - end - - local iterate - iterate = function() - if ai == 0 then - addr = get_first_actor(at) - if validate and addr and get_prev_actor(addr) then - complain("item before first") - end - else - local prev = addr - addr = get_next_actor(addr) - if validate then - if addr and prev ~= get_prev_actor(addr) then - complain("previous mismatch") - end - end - end - - if not addr then - if validate then - if ai < counts[at] then - -- known case: romani ranch on first/third night - complain("list ended early") - elseif ai > counts[at] then - complain("list ended late") - end - end - - ai = 0 - at = at + 1 - if at == 12 then return nil end - return iterate() - else - local temp = ai - ai = ai + 1 - return at, temp, addr - end - end - - return iterate -end - function longbinary(x) return ('%032s'):format(bizstring.binary(x)) end @@ -197,28 +108,6 @@ function focus(actor, dump) end end -function collect_actors() - local game_counts = count_actors() - local any = 0 - for i = 0, 11 do - any = any + game_counts[i] - --FIXME: T_BR(0, 13 - i, nil, "#%2i: %2i", i, game_counts[i]) - end - --FIXME: T_BR(0, 1, nil, "sum:%3i", any) - - local actors_by_type = {[0]={},{},{},{},{},{},{},{},{},{},{},{}} -- 12 - local new_counts = {[0]=0,0,0,0,0,0,0,0,0,0,0,0} -- 12 - if any > 0 then - any = 0 - for at, ai, addr in iter_actors(game_counts) do - actors_by_type[at][ai] = addr - new_counts[at] = new_counts[at] + 1 - any = any + 1 - end - end - return any > 0, actors_by_type, new_counts -end - local input_handler = InputHandler{ enter = "P1 L", up = "P1 DPad U", diff --git a/Lua/actors.lua b/Lua/actors.lua new file mode 100644 index 0000000..311499d --- /dev/null +++ b/Lua/actors.lua @@ -0,0 +1,115 @@ +-- check for errors in the actor linked lists +local validate = false + +-- creating an object every time is a bit slow, so +-- using a template to offset from will do for now. +local actor_t = Actor(0) + +function sort_by_key(t) + local sorted = {} + local i = 1 + for k, v in pairs(t) do + sorted[i] = {k=k, v=v} + i = i + 1 + end + table.sort(sorted, function(a, b) return a.k < b.k end) + return sorted +end + +function get_actor_count(i) + return R4(addrs.actor_counts[i].addr) +end + +function get_first_actor(i) + return deref(R4(addrs.actor_firsts[i].addr)) +end + +function get_next_actor(addr) + return deref(R4(addr + actor_t.next.addr)) +end + +function get_prev_actor(addr) + return deref(R4(addr + actor_t.prev.addr)) +end + +function count_actors() + local counts = {} + for i = 0, 11 do + counts[i] = get_actor_count(i) + end + return counts +end + +function iter_actors(counts) + local at, ai = 0, 0 + local addr + + local y = 1 + local complain = function(s) + s = s..(" (%2i:%3i)"):format(at, ai) + T_TR(0, y, "yellow", s) + y = y + 1 + end + + local iterate + iterate = function() + if ai == 0 then + addr = get_first_actor(at) + if validate and addr and get_prev_actor(addr) then + complain("item before first") + end + else + local prev = addr + addr = get_next_actor(addr) + if validate then + if addr and prev ~= get_prev_actor(addr) then + complain("previous mismatch") + end + end + end + + if not addr then + if validate then + if ai < counts[at] then + -- known case: romani ranch on first/third night + complain("list ended early") + elseif ai > counts[at] then + complain("list ended late") + end + end + + ai = 0 + at = at + 1 + if at == 12 then return nil end + return iterate() + else + local temp = ai + ai = ai + 1 + return at, temp, addr + end + end + + return iterate +end + +function collect_actors() + local game_counts = count_actors() + local any = 0 + for i = 0, 11 do + any = any + game_counts[i] + --FIXME: T_BR(0, 13 - i, nil, "#%2i: %2i", i, game_counts[i]) + end + --FIXME: T_BR(0, 1, nil, "sum:%3i", any) + + local actors_by_type = {[0]={},{},{},{},{},{},{},{},{},{},{},{}} -- 12 + local new_counts = {[0]=0,0,0,0,0,0,0,0,0,0,0,0} -- 12 + if any > 0 then + any = 0 + for at, ai, addr in iter_actors(game_counts) do + actors_by_type[at][ai] = addr + new_counts[at] = new_counts[at] + 1 + any = any + 1 + end + end + return any > 0, actors_by_type, new_counts +end