All pastes #1409109 Raw Copy code Copy link Edit

gocha

public unlisted lua v1 · immutable
#1409109 ·published 2009-05-01 02:23 UTC
rendered paste body
--[[  Densetsu no Stafy: Views & Cheats r1 written by gocha, for vba-rerecording r77  leftclick : drag and throw rightclick: summon ]]--local guiOpacityMax = 0.68local drawCursorInGame = false-- cheatslocal disableCheats = falselocal dragdrop = truelocal infiniteSpin = true-- cheat optionslocal throwPowerRate = 1.68-- [ initial check, etc. ] -----------------------------------------------------if emu then error("Unknown EmuLua host") endemu = vbaif not emu then error("Unknown EmuLua host") endif disableCheats then	dragdrop = false	infiniteSpin = falseend-- require("bit")local bit = {}bit.band = ANDbit.bor  = ORbit.bxor = XORfunction bit.lshift(num, shift) return SHIFT(num, -shift) endfunction bit.rshift(num, shift) return SHIFT(num,  shift) endRAM = {	stafyXPos = 0x3006458, stafyYPos = 0x300645c,	stafyXVel = 0x3006460, stafyYVel = 0x3006464,	stafySpinTimer = 0x3000f4e, stafySpinCount = 0x3000f50,	stafyInvincibility = 0x3000fb8, stafyActive = 0x3000eb0,	bgScrollX = 0x3000ec6, bgScrollY = 0x3000ed6, -- readonly :(	gameState = 0x3000d6c}function gbaROMName()	local tROMName = memory.readbyterange(0x80000a0, 12)	local tROMId = memory.readbyterange(0x80000ac, 4)	-- truncate redundant null chars from tROMName	for i = 1, 12 do		if tROMName[i] == 0 then			tROMName = { unpack(tROMName, 1, i - 1) }			break		end	end	-- process tROMId as well	for i = 1, 4 do		if tROMId[i] == 0 then			tROMId = { unpack(tROMId, 1, i - 1) }			break		end	end	-- convert them into strings	return string.char(unpack(tROMName)), string.char(unpack(tROMId))endfunction romIsValid()	local romName, romId = gbaROMName()	if romName == "STAFY" and romId == "ASTJ" then		return true	else		return false	endendif not romIsValid() then	error("Unsupported ROM possibly")end-- [ utility functions ] --------------------------------------------------------- xx seconds => h:mm:ss.ssfunction formatseconds(seconds)    local h, m, s    s = seconds % 60    m = math.floor(seconds / 60)    h = math.floor(m / 60)    m = m % 60    return string.format("%d:%02d:%05.2f", h, m, s)end-- Lua version of win32 PtInRectfunction ptInRect(rect, pt)	if pt.x >= rect.left and pt.x <= rect.right and pt.y >= rect.top and pt.y <= rect.bottom then		return true	else		return false	endend-- draw traditional arrow cursor (for video logging) ;)function gui.drawarrowcursor(x, y, color, outlineColor)	if color == nil then color = "white" end	if outlineColor == nil then outlineColor = "black" end	gui.line(x, y, x, y+16, outlineColor)	gui.line(x+1, y+1, x+11, y+11, outlineColor)	gui.line(x+1, y+15, x+3, y+13, outlineColor)	gui.line(x+7, y+11, x+10, y+11, outlineColor)	gui.line(x+4, y+12, x+7, y+19, outlineColor)	gui.line(x+7, y+12, x+10, y+19, outlineColor)	gui.line(x+8, y+20, x+9, y+20, outlineColor)	gui.line(x+1, y+2, x+1, y+14, color)	gui.line(x+2, y+3, x+2, y+13, color)	gui.line(x+3, y+4, x+3, y+12, color)	gui.line(x+4, y+5, x+4, y+11, color)	gui.line(x+5, y+6, x+5, y+13, color)	gui.line(x+6, y+7, x+6, y+15, color)	gui.line(x+7, y+8, x+7, y+10, color)	gui.line(x+8, y+9, x+8, y+10, color)	gui.pixel(x+9, y+10, color)	gui.line(x+7, y+14, x+7, y+17, color)	gui.line(x+8, y+16, x+8, y+19, color)	gui.line(x+9, y+18, x+9, y+19, color)endfunction gui.drawarrowcursorwithshadow(x, y, color, outlineColour)	gui.drawarrowcursor(x+1, y+1, "#00000080", "#00000080")	gui.drawarrowcursor(x, y, color, outlineColour)end-- [ subroutines ] -------------------------------------------------------------local allkeys = {	shift = 1, control = 1, alt = 1, capslock = 1, numlock = 1, scrolllock = 1,	["0"] = 1, ["1"] = 1, ["2"] = 1, ["3"] = 1, ["4"] = 1, ["5"] = 1, ["6"] = 1, ["7"] = 1, ["8"] = 1, ["9"] = 1,	A = 1, B = 1, C = 1, D = 1, E = 1, F = 1, G = 1, H = 1, I = 1, J = 1, K = 1, L = 1, M = 1, N = 1, O = 1, P = 1, Q = 1, R = 1, S = 1, T = 1, U = 1, V = 1, W = 1, X = 1, Y = 1, Z = 1,	F1 = 1, F2 = 1, F3 = 1, F4 = 1, F5 = 1, F6 = 1, F7 = 1, F8 = 1, F9 = 1, F10 = 1, F11 = 1, F12 = 1,	F13 = 1, F14 = 1, F15 = 1, F16 = 1, F17 = 1, F18 = 1, F19 = 1, F20 = 1, F21 = 1, F22 = 1, F23 = 1, F24 = 1,	backspace = 1, tab = 1, enter = 1, pause = 1, escape = 1, space = 1,	pageup = 1, pagedown = 1, ["end"] = 1, home = 1, insert = 1, delete = 1,	left = 1, up = 1, right = 1, down = 1,	numpad0 = 1, numpad1 = 1, numpad2 = 1, numpad3 = 1, numpad4 = 1, numpad5 = 1, numpad6 = 1, numpad7 = 1, numpad8 = 1, numpad9 = 1,	["numpad*"] = 1, ["numpad+"] = 1, ["numpad-"] = 1, ["numpad."] = 1, ["numpad/"] = 1,	tilde = 1, plus = 1, minus = 1, leftbracket = 1, rightbracket = 1,	semicolon = 1, quote = 1, comma = 1, period = 1, slash = 1, backslash = 1,	leftclick = 1, rightclick = 1, middleclick = 1}local allpads = { left = 1, up = 1, right = 1, down = 1, A = 1, B = 1, X = 1, Y = 1, L = 1, R = 1, start = 1, select = 1 }local key, keyOld = {}, {}local keyDown, keyUp = {}, {}local keyTime = {}local mouse, mouseOld = {}, {}for k in pairs(allkeys) do keyTime[k] = 0 endfunction stafy1StafyInfo()	local stafy = {}	stafy.x = memory.readdwordsigned(RAM.stafyXPos)	stafy.y = memory.readdwordsigned(RAM.stafyYPos)	stafy.xv = memory.readdwordsigned(RAM.stafyXVel)	stafy.yv = memory.readdwordsigned(RAM.stafyYVel)	stafy.xpx = math.floor(stafy.x / 0x10000)	stafy.ypx = math.floor(stafy.y / 0x10000)	stafy.xsubpx = stafy.x % 0x10000	stafy.ysubpx = stafy.y % 0x10000	stafy.xdisp = stafy.xpx - memory.readword(RAM.bgScrollX)	stafy.ydisp = stafy.ypx - memory.readword(RAM.bgScrollY)	stafy.hitboxdisp = { left = stafy.xdisp - 6, top = stafy.ydisp - 12, right = stafy.xdisp + 6, bottom = stafy.ydisp } -- possible hitbox	stafy.invincibility = memory.readword(RAM.stafyInvincibility)	return stafyend-- [ core routines ] -----------------------------------------------------------local dragStarted = falseemu.registerbefore( function()	key = input.get()	mouse = { x = key.xmouse, y = key.ymouse }	-- triggered input	key.xmouse, key.ymouse = nil, nil	keyDown, keyUp = {}, {}	for k in pairs(allkeys) do		if key[k] then			if not keyOld[k] then keyDown[k] = 1 end			keyTime[k] = keyTime[k] + 1		else			if keyOld[k] then keyUp[k] = 1 end			keyTime[k] = 0		end	end	if memory.readbyte(RAM.gameState) == 1 then		local stafy = stafy1StafyInfo()		if dragdrop then			if keyDown.leftclick then				if ptInRect(stafy.hitboxdisp, { x = mouse.x, y = mouse.y }) then					dragStarted = true				end			elseif key.leftclick and dragStarted then				-- drag (crappy)				memory.writedword(RAM.stafyXPos, (memory.readword(RAM.bgScrollX) + mouse.x) * 0x10000)				memory.writedword(RAM.stafyYPos, (memory.readword(RAM.bgScrollY) + mouse.y) * 0x10000)				memory.writedword(RAM.stafyXVel, 0)				memory.writedword(RAM.stafyYVel, 0)				memory.writeword(RAM.stafyInvincibility, 120)			end			if not key.leftclick then				if keyDown.rightclick then					-- summon					memory.writedword(RAM.stafyXPos, (memory.readword(RAM.bgScrollX) + mouse.x) * 0x10000)					memory.writedword(RAM.stafyYPos, (memory.readword(RAM.bgScrollY) + mouse.y) * 0x10000)					memory.writedword(RAM.stafyXVel, 0)					memory.writedword(RAM.stafyYVel, 0)					memory.writeword(RAM.stafyInvincibility, 120)				elseif keyUp.leftclick and dragStarted then					-- drag finish (throw)					local xv, yv = mouse.x - mouseOld.x, mouse.y - mouseOld.y					memory.writedword(RAM.stafyXVel, xv * 0x10000 * throwPowerRate)					memory.writedword(RAM.stafyYVel, yv * 0x10000 * throwPowerRate)				end				dragStarted = false			end		end		if infiniteSpin then			memory.writeword(RAM.stafySpinCount, 0)		end	endend)emu.registerafter( function()	keyOld = key	mouseOld = mouseend)gui.register( function()	gui.opacity(guiOpacityMax)	if memory.readbyte(RAM.gameState) == 1 then		local stafy = stafy1StafyInfo()		gui.text(1, 1, string.format("STAFY (%04X.%04X,%04X.%04X)\nSPEED ( %08.4f, %08.4f) px/f",			bit.band(stafy.xpx, 0xffff), bit.band(stafy.xsubpx, 0xffff),			bit.band(stafy.ypx, 0xffff), bit.band(stafy.ysubpx, 0xffff),			stafy.xv / 0x10000, stafy.yv / 0x10000), "yellow", "#333333")		local stafyText = ""		if memory.readword(RAM.stafySpinCount) ~= 0 or memory.readword(RAM.stafySpinTimer) ~= 0 then			if stafyText ~= "" then stafyText = stafyText .. " " end			stafyText = stafyText .. string.format("%d/%d", memory.readword(RAM.stafySpinCount), memory.readword(RAM.stafySpinTimer))		end		if stafy.invincibility > 0 then			if stafyText ~= "" then stafyText = stafyText .. " " end			stafyText = stafyText .. string.format("%.2f", (stafy.invincibility / 60.0) + 0.005)		end		gui.text(stafy.xdisp - (#stafyText * 2) + 1, stafy.ydisp, stafyText)		if dragdrop then			gui.box(stafy.hitboxdisp.left, stafy.hitboxdisp.top, stafy.hitboxdisp.right, stafy.hitboxdisp.bottom, "#00cc00")		end	end	if drawCursorInGame then		if key.leftclick then			gui.drawarrowcursor(mouse.x + 1, mouse.y + 1, "red")		elseif key.rightclick then			gui.drawarrowcursor(mouse.x + 1, mouse.y + 1, "yellow")		else			gui.drawarrowcursorwithshadow(mouse.x, mouse.y)		end	endend)while romIsValid() do	emu.frameadvance()end