Skip to content

Instantly share code, notes, and snippets.

@adituv
Last active October 24, 2019 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adituv/c45b80bb74dbe4815f737def015ff4fb to your computer and use it in GitHub Desktop.
Save adituv/c45b80bb74dbe4815f737def015ff4fb to your computer and use it in GitHub Desktop.
Pokemon Emerald auto textbox
local printers = {
main = 0x020201B0,
shop = 0x02020264
};
local offsets = {
active = 0x1B,
state = 0x1C
};
local isInBattle = function()
local gMainFlags = memory.readbyte(0x030026F9, "System Bus");
return bit.band(gMainFlags, 2) ~= 0;
end
local setTastudioRecording = function(value)
if tastudio.engaged() then
tastudio.setrecording(value);
end
end
local globalEnable = true;
-- Creating events within a function closure to encapsulate the otherwise-global state
local setupMainAutotext = function ()
local oldActive = nil;
local active = nil;
local state = nil;
local activeAddr = printers.main + offsets.active;
local stateAddr = printers.main + offsets.state;
local delaySpeedUp = -1;
local delayExitText = -1;
event.unregisterbyname("autotext_main");
event.unregisterbyname("autotext_main_onload");
event.onframeend(function()
if not globalEnable then
return
end
if isInBattle() then
return
end
oldActive = active;
active = memory.read_u8(activeAddr, "System Bus");
state = memory.read_u8(stateAddr, "System Bus");
if not emu.islagged() then
if delaySpeedUp > 0 then
delaySpeedUp = delaySpeedUp - 1;
end
if delayExitText > -1 then
delayExitText = delayExitText - 1;
end
end
if oldActive == 0 and active == 1 then
delaySpeedUp = 2;
setTastudioRecording(true);
elseif oldActive == 1 and active == 0 then
delayExitText = 3;
end
if active == 1 and delaySpeedUp == 0 then
if state == 2 or state == 3 then
joypad.set({ A = true, B = false });
else
joypad.set({ B = true, A = false });
end
end
if active == 0 and delayExitText == 1 then
joypad.set({A = true});
end
if delayExitText == 0 then
setTastudioRecording(false);
end
end, "autotext_main");
event.onloadstate(function()
-- Invalidate the loaded state of *everything*.
oldActive = nil;
active = nil;
state = nil;
delaySpeedUp = -1;
delayExitText = -1;
end, "autotext_main_onload");
end;
local setupBattleAutotext = function ()
local oldActive = nil;
local active = nil;
local state = nil;
local activeAddr = printers.main + offsets.active;
local stateAddr = printers.main + offsets.state;
local justExited = false;
event.unregisterbyname("autotext_battle");
event.unregisterbyname("autotext_battle_onload");
event.onframeend(function()
if not globalEnable then
return
end
if not isInBattle() then
return
end
oldActive = active;
active = memory.read_u8(activeAddr, "System Bus");
state = memory.read_u8(stateAddr, "System Bus");
if justExited and not emu.islagged() then
justExited = false;
setTastudioRecording(false);
end
if oldActive == 0 and active == 1 then
setTastudioRecording(true);
elseif oldActive == 1 and active == 0 then
joypad.set({A = true});
justExited = true;
end
if active == 1 then
if state == 2 or state == 3 then
joypad.set({ A = true, B = false });
else
joypad.set({ B = true, A = false });
end
end
end, "autotext_battle");
event.onloadstate(function()
-- Invalidate the loaded state of *everything*.
oldActive = nil;
active = nil;
state = nil;
end, "autotext_battle_onload");
end;
local oldInput = {};
local curInput = {};
setupMainAutotext();
setupBattleAutotext();
console.log("Autotext enabled: true");
while true do
oldInput = curInput;
curInput = input.get();
if oldInput["K"] and not curInput["K"] then
globalEnable = not globalEnable;
console.log(string.format("Autotext enabled: %s", globalEnable and "true" or "false"));
end
emu.yield();
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment