User File #4013288162007150

Upload All User Files

#4013288162007150 - Sonic the Hedgehog series timer

sonictime.lua
1313 downloads
Uploaded 1/19/2013 5:18 PM by oneeighthundred (see all 3)
This script adds a more accurate in-game timer overlay to Sonic the Hedgehog series games. It displays the subsecond time on the main clock as well as an additional display of the time it actually took to go from the timer starting to the post-stage score screen.
This includes a breakdown of frames added from lag, pausing the game, and most importantly, post-stage actions that delayed the score screen.
Works with the entire main Sonic series.
local levelTimerActive = false
local stoppedPostLevel = false

local prevFrameTotal = 0
local extraFrames = 0
local lagFrames = 0
local pausedFrames = 0

gens.registerafter(function()
	local minutes = memory.readbyte('0x00fffe23')
	local seconds = memory.readbyte('0x00fffe24')
	local ticks = memory.readbyte('0x00fffe25')

	local totalFrames = minutes*360 + seconds*60 + ticks

	if totalFrames ~= prevFrameTotal then
		levelTimerActive = true
		stoppedPostLevel = false
		extraFrames = 0
		if totalFrames == 1 then
			lagFrames = 0
			pausedFrames = 0
		end
	else
		if stoppedPostLevel == false then
			if gens.lagged() then
				lagFrames = lagFrames + 1
			elseif memory.readbyte('0x00fff63b') == 1 then
				pausedFrames = pausedFrames + 1
			else
				local timebonus = memory.readbyte('0x00fff7d2') * 256 + memory.readbyte('0x00fff7d3')
				if timebonus ~= 0 then
					stoppedPostLevel = true
				else
					extraFrames = extraFrames + 1
				end
			end
		end
	end

	prevFrameTotal = totalFrames

	local timerTag
	if ticks < 10 then
		timerTag = ': 0'..ticks
	else
		timerTag = ': '..ticks
	end
	if extraFrames > 1 or lagFrames ~= 0 or pausedFrames ~= 0 then
		ticks = ticks + extraFrames + lagFrames + pausedFrames
		while ticks >= 60 do
			ticks = ticks - 60
			seconds = seconds + 1
		end
		while seconds >= 60 do
			seconds = seconds - 60
			minutes = minutes + 1
		end

		if seconds < 10 then
			seconds = "0"..seconds
		end
		if ticks < 10 then
			ticks = "0"..ticks
		end
		timerTag = timerTag.."  (+"..(lagFrames+extraFrames+pausedFrames).." = "..minutes..":"..seconds..":"..ticks..")"

		local breakdownTag = ""
		if lagFrames ~= 0 then
			breakdownTag = breakdownTag..lagFrames.." Lag "
		end
		if pausedFrames ~= 0 then
			breakdownTag = breakdownTag..pausedFrames.." Pause "
		end
		if extraFrames > 1 then
			breakdownTag = breakdownTag..extraFrames.." After "
		end
		gui.drawtext(122, 36, breakdownTag)
	end
	gui.drawtext(90, 28, timerTag)
end)