local battletimer = 0; local menutimer = 0; local shoptimer = 0; local u8 = memory.read_u8; function PrintTimes(battletimer, menutimer, shoptimer) console.clear(); print(string.format('Time in battle: %s', SecondsToClock(battletimer))); print(string.format('Time in menu: %s', SecondsToClock(menutimer))); print(string.format('Time in shops: %s', SecondsToClock(shoptimer))); end function SecondsToClock(duration) if duration <= 0 then return '00:00:00'; end local hours = 0; local minutes = 0; local seconds = duration; if duration >= 60 then while seconds >= 60 do minutes = minutes + 1 seconds = seconds - 60 end while minutes >= 60 do hours = hours + 1 minutes = minutes - 60 end end return string.format('%02.f:%02.f:%02.f', hours, minutes, seconds); end local function TimeDelta(condition) if condition() then local time_before = os.clock(); while (condition()) do if u8(0x6B86) == 255 and u8(0x6C92) == 4 and u8(0x006A) == 123 then break end emu.frameadvance(); end local time_after = os.clock(); return time_after - time_before, true; end return 0, false; end local function InBattle() return mainmemory.readbyte(0x00F2) == 8; end local function InMenu() return mainmemory.readbyte(0x00F2) == 6; end local function InShop() return mainmemory.readbyte(0x00F2) == 5; end PrintTimes(battletimer, menutimer, shoptimer) while true do local advanced = false; local any_advanced = false; local timer = 0; timer, advanced = TimeDelta(InBattle); if advanced then any_advanced = true; battletimer = battletimer + timer; end timer, advanced = TimeDelta(InMenu); if advanced then any_advanced = true; menutimer = menutimer + timer; end timer, advanced = TimeDelta(InShop); if advanced then any_advanced = true; shoptimer = shoptimer + timer; end if u8(0x6B86) == 255 and u8(0x6C92) == 4 and u8(0x006A) == 123 then break end if (any_advanced) then PrintTimes(battletimer, menutimer, shoptimer); end emu.frameadvance(); end PrintTimes(battletimer, menutimer, shoptimer); print('Final Times! Congratulations!');