User File #19987242045801795

Upload All User Files

#19987242045801795 - [lua]Macro in BizHawk

KeyToSequence.lua
964 downloads
Uploaded 1/9/2015 2:42 AM by Dica (see all 7)
Just a little lua script that allows you to use macros in BizHawk
sequences = {J = {{"A"},
				  {},
				  {"B"},
				  {},
				  {"B"},}}


-- Just a function to copy a table to another one,
-- srly lua no built-in function for that ?!
function deepcopy(orig)
    local orig_type = type(orig)
    local copy
    if orig_type == 'table' then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
            copy[deepcopy(orig_key)] = deepcopy(orig_value)
        end
        setmetatable(copy, deepcopy(getmetatable(orig)))
    else -- number, string, boolean, etc
        copy = orig
    end
    return copy
end

-- send a sequence of input to the emulator
function send_sequence(seq)
	local joypad_seq = {}
	for i, seq_frame in pairs(seq) do
		joypad_seq = {}
		for _, key in pairs(seq_frame) do
			joypad_seq[key] = "True"
		end
		joypad.set(joypad_seq)
		if i == (#seq) then
			gui.addmessage("End of sequence")
			-- Ne pas avoir de emu.frameadvance() sur le dernier element de
			-- la sequence permet de faire en sorte que la boucle principale
			-- reprenne le controle et donc de pouvoir directement entrer une
			-- nouvelle séquence sans avoir à attendre une frame
		else
			emu.frameadvance()
		end
	end
	
end

-- check if keys in `sequences` are pressed and then call send_sequence
function check_mapped_keys()
	local inp = input.get()
	for key, _ in pairs(inp) do
		if not (sequences[key] == nil) then
			send_sequence(sequences[key])
		end
	end
end


while true do
	emu.frameadvance()
	check_mapped_keys()
end