User File #51259610045391444

Upload All User Files

#51259610045391444 (unlisted) - Scripts for QoL

lttp scripts.lua
445 downloads
Uploaded 11/17/2018 11:22 AM by fmp (see all 10)
local running = true
local input_requested = false
local pad = nil

local scripts

-- function here prevents input from being asked for multiple times by different functions on the same frame
local function request_input()
	if input_requested then
		return
	end
	pad = joypad.get(1)
	input_requested = true
end

-- function here prevents input from being asked for multiple times by different functions on the same frame
local memory_cache = {}

local function request_memory(address, func, domain)
	func = func or 'readbyte'
	domain = domain or 'WRAM'
	local cache_name = string.format('%06x-%s-%s', address, func, domain)

	if not memory_cache[cache_name] then
		memory_cache[cache_name] = memory[func](address, domain)
	end

	return memory_cache[cache_name]
end


local text_pressed = false

-- mash L and R for idle activity, then X to close
local function do_text_mash()
	local countdown = request_memory(0x1CE9)
	if countdown == 0 and not text_pressed then
		joypad.set({ X = true }, 1)
		text_pressed = true
		client.pause()
	elseif countdown <= 20 and not text_pressed then
		if countdown % 2 == 0 then
			joypad.set({ R = true }, 1)
		else
			joypad.set({ L = true }, 1)
		end
	elseif countdown == 28 and text_pressed then
		text_pressed = false
	end
end

-- end stuff
local function quit_this()
	running = false
end

-- skip transitions and such by looking at submodule
local function do_transition()
	local submod = request_memory(0x11)
	if submod == 0 then
		client.pause()
		scripts.transition_skip.active = false
		gui.addmessage('Transition done')
	end
end

local function hit_transition()
	local submod = request_memory(0x11)
	if submod ~= 0 then
		client.clearautohold()
		client.pause()
		scripts.transition_go.active = false
		gui.addmessage('Transition hit')
	end
end

-- wait for link state to reset
local function wait_state()
	local state = request_memory(0x5D)
	local move = request_memory(0x2E4)

	if state <= 1 and move == 0 then
		client.pause()
		scripts.state_wait.active = false
		gui.addmessage('State wait over')
	end
end

local function f_savestate()
	local frame = emu.framecount()
	local rr = movie.getrerecordcount()

	savestate.save(string.format('../SNES/State/100 v3/%s.%s.state', frame, rr))
	scripts.f_savestate.active = false
end

-- turn off all scripts
local function all_off()
	for _, v in pairs(scripts) do
		v.active = false
	end
end

-- table of scripts and meta data
scripts = {
	text = {
		name = "Textbox mashing",
		active = false,
		func = do_text_mash,
		key = 'M',
		key_held = false
	},
	transition_skip = {
		name = "Transition skip",
		active = false,
		func = do_transition,
		key = 'N',
		key_held = false
	},
	transition_go = {
		name = "Transition hit",
		active = false,
		func = hit_transition,
		key = 'J',
		key_held = false
	},
	state_wait = {
		name = "State wait",
		active = false,
		func = wait_state,
		key = 'B',
		key_held = false
	},
	f_savestate = {
		name = "Frame savestate",
		active = false,
		func = f_savestate,
		key = 'F2',
		key_held = false
	},
	clear = {
		name = "Script clearing",
		active = false,
		func = all_off,
		key = 'Z',
		key_held = false
	},
	quit = {
		name = "Exit mode",
		active = false,
		func = quit_this,
		key = 'X',
		key_held = false
	}
}

while running do
	emu.yield()

	local keys = input.get()
	gui.cleartext()
	local message_i = 0

	for _, v in pairs(scripts) do
		if keys[v.key] then
			if not v.key_held then
				v.key_held = true
				v.active = not v.active
				gui.addmessage(string.format('%s %s', v.name, v.active and 'activated' or 'deactivated'))
			end
		else
			v.key_held = false
		end

		if v.active then
			gui.text(0, 30 + 15 * message_i, v.name .. ' active', nil, 'topright')
			message_i = message_i + 1
			v.func()
		end
	end

	input_requested = false
	memory_cache = {}

	gui.text(0, 15, "Scripts script active", nil, 'topright')
end