I just have to remember to call the selector from the right table - I kept bumping into bugs where I was calling the entire table instead of the value at i
Also updated script below:
Language: lua
--[[
General scripts for Metroid II: Return of Samus for Gameboy/GB
]]
enemy = {"indicator","health","maxhealth","frozen","lifeperc","isdrop","isupgrade"}
enemy.indicator = {}
enemy.health = {}
enemy.maxhealth = {}
enemy.frozen = {}
enemy.lifeperc = {}
enemy.isdrop = {}
enemy.isupgrade = {}
samus = {"basehp","maxhp","perchp"}
bombs = { }
bombperc = { }
ubite = memory.read_u8
sbite = memory.read_s8
circ = gui.drawPie
rekt = gui.drawRectangle
bawx = gui.drawBox
function core()
getSamusLife()
getEnemyStats()
updateBombTimers()
calcPercents()
drawBombTimers()
drawHealthBars()
end
function getSamusLife()
za = ubite(0x1051)
upda = string.format("%2x",za)
za = tonumber(upda,10)
zb = ubite(0x1050)
zc = ubite(0x1052)
samus.maxhp = 99 + (zb*99)
samus.basehp = za + (zc*99)
end
function getEnemyStats()
root = 0x0600
for i = 0,7 do
enemy.indicator[i] = root + i*32
enemy.health[i] = ubite(enemy.indicator[i] + 12)
enemy.maxhealth[i] = ubite(enemy.indicator[i] + 17)
enemy.frozen[i] = ubite(enemy.indicator[i] + 11)
enemy.isdrop[i] = sbite(enemy.indicator[i] + 13)
enemy.isupgrade[i] = ubite(enemy.indicator[i] + 4)
enemy.indicator[i] = sbite(enemy.indicator[i])
if enemy.maxhealth[i] >= 250 then
enemy.health[i] = 0
end
if enemy.maxhealth[i] == 0 then
enemy.maxhealth[i] = 1
enemy.health[i] = 0
end
end
end
function updateBombTimers()
bombs[1] = ubite(0x1d31)
bombs[2] = ubite(0x1d41)
bombs[3] = ubite(0x1d51)
end
function calcPercents()
-- first, calc percents for the bombs since it's a simple loop with a bound check
for i, v in ipairs(bombs) do
if (bombs[i] ~= 255) then
bombperc[i] = bombs[i] / 95
else
bombperc[i] = 0
end
end
samus.perchp = samus.basehp / samus.maxhp
for i = 0, 7 do
enemy.lifeperc[i] = enemy.health[i] / enemy.maxhealth[i]
end
end
function drawBombTimers()
for i, v in ipairs(bombperc) do
circ(i*10,8,8,8,-90,bombperc[i]*360,"Green","Green")
end
end
function drawHealthBars()
bawx(0,136,64,143,0xFF666666,0xFF666666)
rekt(1,137,samus.perchp*62,5,0xFF333333,0xFF333333)
for i = 0,7 do
if (enemy.indicator[i] ~= -1) then -- Is the enemy slot active?
if (enemy.isdrop[i] == 0) then -- Make sure it's not an item
if (enemy.frozen[i] == 0) then -- If it's not frozen, use red
rekt(150+i,0,1,100*enemy.lifeperc[i],0xFFFF0000,0xFFFF0000)
else -- otherwise, use cyan
rekt(150+i,0,1,100*enemy.lifeperc[i],0xFF00FFFF,0xFF00FFFF)
end
end
end
end
end
event.onframeend(core)
Still need to figure out how to handle doors and the spring ball enemy, but it no longer shows health bars for projectiles and Gamma Metroid barriers.
EDIT: Removed one check since it was ultimately redundant.
EDITWO: Added a sanity check to getEnemyStats()