1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-05-17 21:23:22 -07:00

move actor iteration funcs to their own file

This commit is contained in:
Connor Olding 2015-12-28 21:48:55 -08:00
parent bc6037092e
commit 2e42819b99
2 changed files with 119 additions and 115 deletions

View File

@ -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",

115
Lua/actors.lua Normal file
View File

@ -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