Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --How many frames ahead of the "action" to start savestates
- preFrames = 45
- --Game-specific RAM addresses
- RAM = {
- bytes = {
- dead = 0x7e0D86, --101 if dead
- inGame = 0x7e0040 --32 if in game
- }
- }
- --Locations to output text
- output = {
- time = {
- x = 0,
- y = 190
- },
- gameInfo = {
- x = 0,
- y = 200
- }
- }
- --Tells the script when to do nothing regarding savestates
- function DoNothing()
- if not inGame() then
- return true
- end
- return false
- end
- function inGame()
- local gameVal = memory.readbyte(RAM.bytes.inGame)
- if gameVal == 32 then
- return true
- end
- return false
- end
- function GameActive()
- if memory.readbyte(RAM.bytes.dead) ~= 101 and inGame() then
- return true
- end
- return false
- end
- --Tells the script that the attempt should continue
- function ContinueAttempt()
- if GameActive() then
- return true
- end
- return false
- end
- --Tells the script that the last segment was okay (i.e. you didn't die)
- function SegmentOk()
- if memory.readbyte(RAM.bytes.dead) ~= 101 then
- return true
- end
- return false
- end
- --Handles all the hotkey stuff
- function HandleKeys()
- keys = input.get()
- if press('Q') then
- segment.done = true
- end
- if press('R') then
- segment.failed = true
- end
- if press('C') then
- segment.failed = true
- segment.bestTime = 999999
- end
- if press('B') then
- segment.done = true
- segment.rollback = true
- end
- last_keys = keys
- end
- --Nothing below this point should have to change between games (though it might anyway)
- segment = {
- prevStart = savestate.create(),
- prevBest = savestate.create(),
- start = savestate.create(),
- best = savestate.create(),
- bestTime = 999999,
- lastTime = 0,
- curTime = 0,
- done = false,
- actionStart = 0,
- failed = false,
- rollback = false
- }
- firstSegment = true
- segment.curTime = 0
- function press(button)
- if keys[button] and not last_keys[button] then
- return true
- end
- return false
- end
- function StartSegment()
- if firstSegment then
- savestate.save(segment.prevStart)
- firstSegment = false
- end
- savestate.save(segment.start)
- segment.done = false
- segment.rollback = false
- segment.bestTime = 999999
- segment.lastTime = 0
- segment.curTime = movie.framecount()
- end
- function OutputTime()
- gui.text(output.time.x,output.time.y,string.format("Last: %d Best: %d", segment.lastTime, segment.bestTime))
- end
- while true do
- StartSegment()
- local closerStart = false
- while not segment.done do
- segment.failed = false
- savestate.load(segment.start)
- while not segment.done and not segment.failed and (DoNothing()) do
- HandleKeys()
- snes9x.frameadvance()
- --get a save state that's still in the transition, but close to the action for player control
- if movie.framecount() == segment.actionStart - preFrames then
- savestate.save(segment.start)
- closerStart = true
- end
- end
- if not closerStart then
- segment.actionStart = movie.framecount()
- end
- while not segment.done and not segment.failed and ContinueAttempt() do
- snes9x.frameadvance()
- OutputTime()
- HandleKeys()
- end
- segment.lastTime = (movie.framecount() - segment.curTime)
- if (segment.bestTime > segment.lastTime) and (segment.lastTime > 10) and not segment.failed and not segment.done and SegmentOk() then
- segment.bestTime = segment.lastTime
- savestate.save(segment.best)
- end
- end
- if not segment.rollback then
- savestate.save(segment.prevBest)
- savestate.load(segment.best)
- savestate.save(segment.prevStart)
- segment.prevStart, segment.start = segment.start, segment.prevStart
- else
- savestate.load(segment.prevBest)
- savestate.save(segment.best)
- savestate.load(segment.prevStart)
- savestate.save(segment.start)
- end
- end
Add Comment
Please, Sign In to add comment