--[[ Leeland Kirwan (FatRatKnight), 2014
Allows user input to "mix" with TAS Editor using XOR logic.
If Recording is checked, this script will do its intended job.
Tap a P1 key to toggle input. If "All" is selected, will use other player keys
for related player. If an individual player is selected, uses P1 controls. You
can hold down a key to affect multiple frames, toggling the key on each frame.
For reference, Superimpose uses OR logic, and does not allow user input to
clear stored input. A great inconvience for those wishing to use the keyboard
to toggle individual input rather than mouse.
]]--
local btnstr= {A = "A", B = "B", select= "S", start= "T",
up= "U", down= "D", left = "L", right= "R"}
local btnval= {A = 0x01, B = 0x02, select= 0x04, start= 0x08,
up= 0x10, down= 0x20, left = 0x40, right= 0x80}
local plval= {["1P"]= 1, ["2P"]= 2, ["3P"]= 3, ["4P"]= 4}
local OldJoy= {{},{},{},{}}
--*****************************************************************************
local function InputToggle()
--*****************************************************************************
--Needs readonly mode to work, but won't interfere too much otherwise.
--Toggles inputs recorded in TAS Editor.
if not taseditor.engaged() then return end -- Escape
taseditor.clearinputchanges() -- Guessing something horrible w/o this.
local frame= emu.framecount()
local plSel= plval[taseditor.getrecordermode()]
local plLow = plSel or 1 -- Get the for loop range!
local plHigh= plSel or 4 -- Hardcoded 4 doesn't appear harmful...
local P1joy= nil -- If one player, get P1.
if plSel then P1joy= joypad.getimmediate(1) end -- Otherwise, get all.
local changed= false -- Flag, so I don't interfere too much
local History= "" -- Information for TASer in History list
for pl= plLow, plHigh do
local pad= taseditor.getinput(frame,pl)
local joy= P1joy or joypad.getimmediate(pl) -- Pick out the joypad
local mask= 0 -- Convert messy table to numbers
for btn,pressed in pairs(joy) do
if pressed and not OldJoy[pl][btn] then
mask= bit.bor(mask,btnval[btn])
History= History .. pl .. btnstr[btn]
end
end
taseditor.submitinputchange(frame,pl,bit.bxor(pad,mask))
OldJoy[pl]= joy -- To avoid repeated triggers
changed= changed or (mask ~= 0) -- If the bitmask did something, apply!
end
if changed then taseditor.applyinputchanges(History) end
end
taseditor.registerauto(InputToggle)
--#############################################################################
--#############################################################################
--If you need some other code and other registers, I recommend them here.
--If you need the "while true do" loop, that's down below.
--#############################################################################
--#############################################################################
-- Ugh, need this loop on the very bottom...
--*****************************************************************************
while true do
--*****************************************************************************
-- Exists to detect frame advances, and thus clear the OldJoy array.
for i= 1, 4 do OldJoy[i]= {} end -- Retrigger keys
-- Any code that needs this loop can go here.
emu.frameadvance()
end