-- Port of Salanewt's DD camera control AR code to Bizhawk Lua
-- https://docs.google.com/document/d/1gdVf59_w6COzJI6VI2dPqctewHCdcx8eh6DRWrlN5Xg/edit
local function write_words(ptr, xs)
for i, x in ipairs(xs) do
memory.write_u32_le(ptr+(i-1)*4, x, "Main RAM")
end
end
local function write_nops(ptr, n)
for i = 0, n-1 do
memory.write_u32_le(ptr+i*4, 0, "Main RAM")
end
end
local function increment(ptr, delta)
local x = memory.read_u32_le(ptr, "Main RAM")
memory.write_u32_le(ptr, x + delta, "Main RAM")
end
local P = 0x142D18
local Delta = 2408
while true do
local btns = joypad.get();
if btns.Select and btns.Start then -- Restore default camera
write_words(P, {0xE8850005, 0xE5851008, 0xE5858018, 0xE585701C, 0xE5853020})
elseif btns.Select and btns.A then -- Move Left
write_nops(P, 5)
increment(0x079304, -Delta)
increment(0x0792E8, -Delta)
elseif btns.Select and btns.B then -- Move Right
write_nops(P, 5)
increment(0x079304, Delta)
increment(0x0792E8, Delta)
elseif btns.Start and btns.B then -- Move South
write_nops(P, 5)
increment(0x07930C, Delta)
increment(0x0792F0, Delta)
elseif btns.Start and btns.A then -- Move North
write_nops(P, 5)
increment(0x07930C, -Delta)
increment(0x0792F0, -Delta)
elseif btns.Start and btns.L then -- Move Up
write_nops(P, 5)
increment(0x079308, Delta)
increment(0x0792EC, Delta)
elseif btns.Start and btns.R then -- Move Down
write_nops(P, 5)
increment(0x079308, -Delta)
increment(0x0792EC, -Delta)
elseif btns.A and btns.L then -- Rotate Left Only
write_nops(P, 5)
increment(0x0792EC, Delta)
elseif btns.A and btns.R then -- Rotate Right Only
write_nops(P, 5)
increment(0x0792EC, -Delta)
elseif btns.B and btns.L then -- Rotate North Only
write_nops(P, 5)
increment(0x0792F4, -Delta)
elseif btns.B and btns.R then -- Rotate South Only
write_nops(P, 5)
increment(0x0792F4, Delta)
elseif btns.Select and btns.L then -- Rotate Up Only
write_nops(P, 5)
increment(0x0792F0, -Delta)
elseif btns.Select and btns.R then -- Rotate Down Only
write_nops(P, 5)
increment(0x0792F0, -Delta)
end
emu.frameadvance()
end