Skip to content

Instantly share code, notes, and snippets.

@taotao54321 taotao54321/Vice-hud.lua

Last active Dec 18, 2016
Embed
What would you like to do?
NES Vice - Project Doom (U) HUD script for FCEUX
----------------------------------------------------------------------
-- NES Vice - Project Doom (U) HUD script for FCEUX
--
-- displays position, velocity, acceleration, and HPs.
----------------------------------------------------------------------
----------------------------------------------------------------------
-- util
----------------------------------------------------------------------
local function num_stou(n, size)
if n < 0 then
n = n + 2^(8*size)
end
return n
end
local function num_utos(n, size)
if n >= 2^(8*size-1) then
n = n - 2^(8*size)
end
return n
end
local function numdec_s_le(buf, size)
local n = numdec_u_le(buf, size)
return num_utos(n, size)
end
local function numdec_u_le(buf, size)
local n = 0
for i = 1, size do
n = n + 2^(8*(i-1)) * buf[i]
end
return n
end
local function numdec_s_be(buf, size)
local n = numdec_u_be(buf, size)
return num_utos(n, size)
end
local function numdec_u_be(buf, size)
local n = 0
for i = 1, size do
n = n + 2^(8*(size-i)) * buf[i]
end
return n
end
local function numdec_s32_le(buf)
return numdec_s_le(buf, 4)
end
local function numdec_u32_le(buf)
return numdec_u_le(buf, 4)
end
local function numdec_s24_le(buf)
return numdec_s_le(buf, 3)
end
local function numdec_u24_le(buf)
return numdec_u_le(buf, 3)
end
local function numdec_s16_le(buf)
return numdec_s_le(buf, 2)
end
local function numdec_u16_le(buf)
return numdec_u_le(buf, 2)
end
local function numdec_s32_be(buf)
return numdec_s_be(buf, 4)
end
local function numdec_u32_be(buf)
return numdec_u_be(buf, 4)
end
local function numdec_s24_be(buf)
return numdec_s_be(buf, 3)
end
local function numdec_u24_be(buf)
return numdec_u_be(buf, 3)
end
local function numdec_s16_be(buf)
return numdec_s_be(buf, 2)
end
local function numdec_u16_be(buf)
return numdec_u_be(buf, 2)
end
local function mem_read_bytes(addr, len)
local buf = {}
for i = 1, len do
buf[i] = memory.readbyte(addr+(i-1))
end
return buf
end
local function mem_read_s_le(addr, size)
local buf = mem_read_bytes(addr, size)
return numdec_s_le(buf, size)
end
local function mem_read_u_le(addr, size)
local buf = mem_read_bytes(addr, size)
return numdec_u_le(buf, size)
end
local function mem_read_s_be(addr, size)
local buf = mem_read_bytes(addr, size)
return numdec_s_be(buf, size)
end
local function mem_read_u_be(addr, size)
local buf = mem_read_bytes(addr, size)
return numdec_u_be(buf, size)
end
local function mem_read_s32_le(addr)
return mem_read_s_le(addr, 4)
end
local function mem_read_u32_le(addr)
return mem_read_u_le(addr, 4)
end
local function mem_read_s24_le(addr)
return mem_read_s_le(addr, 3)
end
local function mem_read_u24_le(addr)
return mem_read_u_le(addr, 3)
end
local function mem_read_s16_le(addr)
return mem_read_s_le(addr, 2)
end
local function mem_read_u16_le(addr)
return mem_read_u_le(addr, 2)
end
local function mem_read_s32_be(addr)
return mem_read_s_be(addr, 4)
end
local function mem_read_u32_be(addr)
return mem_read_u_be(addr, 4)
end
local function mem_read_s24_be(addr)
return mem_read_s_be(addr, 3)
end
local function mem_read_u24_be(addr)
return mem_read_u_be(addr, 3)
end
local function mem_read_s16_be(addr)
return mem_read_s_be(addr, 2)
end
local function mem_read_u16_be(addr)
return mem_read_u_be(addr, 2)
end
local function mem_read_s8(addr)
return num_utos(mem_read_u8(addr), 1)
end
local function mem_read_u8(addr)
return memory.readbyte(addr)
end
----------------------------------------------------------------------
-- main
----------------------------------------------------------------------
local fmt = string.format
local function signmag8(n)
local val = bit.band(n, 0x7F)
if bit.band(n, 0x80) ~= 0 then val = -val end
return val
end
local function get_objs()
local objs = {}
for i = 0, 15 do
local obj = {}
obj.hp = mem_read_u8(0x0280+i)
obj.invin = bit.band(mem_read_u8(0x02B0+i), 0x7F)
obj.x = 0x100 * mem_read_u8(0x0200+i) + mem_read_u8(0x01F0+i)
obj.y = 0x100 * mem_read_u8(0x0220+i) + mem_read_u8(0x0210+i)
obj.x_sub = mem_read_u8(0x01E0+i)
obj.y_sub = mem_read_u8(0x01B0+i)
obj.vx = signmag8(mem_read_u8(0x01D0+i))
obj.vy = signmag8(mem_read_u8(0x01A0+i))
obj.ax = signmag8(mem_read_u8(0x01C0+i))
obj.ay = signmag8(mem_read_u8(0x0190+i))
objs[i+1] = obj
end
return objs
end
local function get_info()
local info = {}
info.objs = get_objs()
info.scroll_x = mem_read_u16_le(0x0B)
info.scroll_y = mem_read_u8(0x0D)
info.rand_idx = mem_read_u8(0x94)
return info
end
local function text(x, y, str)
gui.text(x, y, str)
end
local function draw_hp(objs)
local strs = {}
for i = 1, #objs do
strs[i] = fmt("%d%s",
objs[i].hp,
objs[i].invin ~= 0 and fmt("(%d)", objs[i].invin) or ""
)
end
text(56, 224, "H:" .. table.concat(strs, ","))
end
local function draw_seg()
local seg_frame = -1
if taseditor.engaged() then
local f_current = emu.framecount()
for f = f_current, 0, -1 do
if taseditor.markedframe(f) or f == 0 then
seg_frame = f_current - f
break
end
end
text(232, 192, fmt("%4d", seg_frame))
end
end
local function draw()
local info = get_info()
local objs = info.objs
local hero = objs[1]
local x = hero.x + hero.x_sub / 16.0
local y = hero.y + hero.y_sub / 16.0
text(0, 8, fmt(
"P:(%.2f,%.2f) V:(%.2f,%.2f) A:(%.2f,%.2f)",
x, y,
hero.vx / 16.0, hero.vy / 16.0,
hero.ax / 16.0, hero.ay / 16.0
))
text(0, 16, fmt(
"S:(%d,%d)",
info.scroll_x, info.scroll_y
))
draw_hp(objs)
text(200, 192, fmt("R:%3d", info.rand_idx))
draw_seg()
end
local function main()
gui.opacity(0.7)
emu.registerafter(draw)
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.