User File #21849958098105961

Upload All User Files

#21849958098105961 - Mega Man ZX RNG Lua

Mega Man ZX RNG.lua
1108 downloads
Uploaded 4/3/2015 12:01 AM by ALAKTORN (see all 34)
Bug fixes.
“Added an experimental automated ROM version detection system. Compatible with USA, JPN, and EUR. Also added an input toggle for 3 different modes (as suggested by Flameberger), it defaults to key “D”, but you can change it to whatever you want, it’s the very first function in the script (writing “tab” makes it work with the Tab key). The current mode is written on the bottom screen. The modes are: Complete, Moderate, and Nothing. Complete writes all advances, Moderate only those bigger than 1 step, Nothing writes none. Feedback, error reports, feature requests and such all appreciated.”
-- Custom commands
local lastInput = {}
function executeCustomCommands()
	local Input = input.get()
	
	if Input.D and not lastInput.D then
		printMode = (printMode + 1) % 3
	end
	lastInput = Input
end

-- Determine game version
function detectVersion()
	local value = memory.readword(0x0200000E)
	if value == 0x333B then
		return "USA"
	elseif value == 0x0B91 then
		return "JPN"
	elseif value == 0xF87A then
		return "EUR"
	else
		print("Couldn't detect ROM version.")
	end
end

-- RNG address
version = detectVersion()
function readRNG()
	if version == "USA" then
		return memory.readdword(0x02108B90)
	elseif version == "JPN" then
		return memory.readdword(0x02108790)
	elseif version == "EUR" then
		return memory.readdword(0x0210AD98)
	end
end

-- RNG calculations
function RNGStep(iRNG)
	local High1 = math.floor(iRNG / 65536)
	local Low1 = math.floor(iRNG % 65536)
	local High2 = math.floor(214013 / 65536)
	local Low2 = math.floor(214013 % 65536)
	 
	local NewHigh = (High1 * Low2 + High2 * Low1) % 65536;
	local NewLow = Low1 * Low2
	 
	return (NewHigh * 65536 + NewLow + 2531011) % 2147483648;
	--return (214013 * iRNG + 2531011) % 2147483648
end

function RNGSteps(iRNG, steps)
	for i = 1, steps do
		iRNG = RNGStep(iRNG)
	end
	return iRNG
end

function getNumSteps(oldRNG, newRNG, maxSearch)
	if oldRNG == -1 then
		return -2
	end
	if oldRNG == newRNG then
		return 0
	end
	for i = 1, maxSearch do
		oldRNG = RNGStep(oldRNG)
		if oldRNG == newRNG then
			return i
		end
	end
	return -1;
end

-- 2 means print all, 1 don't print 1 step advances, 0 print nothing
printMode = 2
RNG = -1
function printRNGChanges()
	newRNG = readRNG()
	if RNG ~= newRNG then
		numSteps = getNumSteps(RNG, newRNG, 1000)
		if numSteps ~= -1 then
			if printMode >= 2 or (printMode >= 1 and numSteps > 1) then
				print(newRNG .. " (" .. numSteps .. ")")
			end
		elseif printMode >= 1 then
			-- Lua code from PHP here
			if newRNG == 1627459585 then print(newRNG .. " (Area A-1 reset)")
			elseif newRNG == 1627463682 then print(newRNG .. " (Area A-2 reset)")
			elseif newRNG == 1627467779 then print(newRNG .. " (Area A-3 reset)")
			elseif newRNG == 1627471876 then print(newRNG .. " (Area A-4 reset)")
			elseif newRNG == 1644302341 then print(newRNG .. " (Area B-1 reset)")
			elseif newRNG == 1644306438 then print(newRNG .. " (Area B-2 reset)")
			elseif newRNG == 1644310535 then print(newRNG .. " (Area B-3 reset)")
			elseif newRNG == 1644314632 then print(newRNG .. " (Area B-4 reset)")
			elseif newRNG == 1661145097 then print(newRNG .. " (Area C-1 reset)")
			elseif newRNG == 1661149194 then print(newRNG .. " (Area C-2 reset)")
			elseif newRNG == 1661153291 then print(newRNG .. " (Area C-3 reset)")
			elseif newRNG == 1677987855 then print(newRNG .. " (Area D-1 reset)")
			elseif newRNG == 1677991952 then print(newRNG .. " (Area D-2 reset)")
			elseif newRNG == 1677996049 then print(newRNG .. " (Area D-3 reset)")
			elseif newRNG == 1678000146 then print(newRNG .. " (Area D-4 reset)")
			elseif newRNG == 1678004243 then print(newRNG .. " (Area D-5 reset)")
			elseif newRNG == 1694830612 then print(newRNG .. " (Area E-1 reset)")
			elseif newRNG == 1694834709 then print(newRNG .. " (Area E-2 reset)")
			elseif newRNG == 1694838806 then print(newRNG .. " (Area E-3 reset)")
			elseif newRNG == 1694842903 then print(newRNG .. " (Area E-4 reset)")
			elseif newRNG == 1694847000 then print(newRNG .. " (Area E-5 reset)")
			elseif newRNG == 1694851097 then print(newRNG .. " (Area E-6 reset)")
			elseif newRNG == 1694855194 then print(newRNG .. " (Area E-7 reset)")
			elseif newRNG == 1694859291 then print(newRNG .. " (Area E-8 reset)")
			elseif newRNG == 1711673372 then print(newRNG .. " (Area F-1 reset)")
			elseif newRNG == 1711677469 then print(newRNG .. " (Area F-2 reset)")
			elseif newRNG == 1711681566 then print(newRNG .. " (Area F-3 reset)")
			elseif newRNG == 1711685663 then print(newRNG .. " (Area F-4 reset)")
			elseif newRNG == 1711689760 then print(newRNG .. " (Area F-5 reset)")
			elseif newRNG == 1728516129 then print(newRNG .. " (Area G-1 reset)")
			elseif newRNG == 1728520226 then print(newRNG .. " (Area G-2 reset)")
			elseif newRNG == 1728524323 then print(newRNG .. " (Area G-3 reset)")
			elseif newRNG == 1728528420 then print(newRNG .. " (Area G-4 reset)")
			elseif newRNG == 1728532517 then print(newRNG .. " (Area G-5 reset)")
			elseif newRNG == 1745358886 then print(newRNG .. " (Area H-1 reset)")
			elseif newRNG == 1745362983 then print(newRNG .. " (Area H-2 reset)")
			elseif newRNG == 1745367080 then print(newRNG .. " (Area H-3 reset)")
			elseif newRNG == 1745371177 then print(newRNG .. " (Area H-4 reset)")
			elseif newRNG == 1762201642 then print(newRNG .. " (Area I-1 reset)")
			elseif newRNG == 1762205739 then print(newRNG .. " (Area I-2 reset)")
			elseif newRNG == 1762209836 then print(newRNG .. " (Area I-3 reset)")
			elseif newRNG == 1762213933 then print(newRNG .. " (Area I-4 reset)")
			elseif newRNG == 1762218030 then print(newRNG .. " (Area I-5 reset)")
			elseif newRNG == 1779044399 then print(newRNG .. " (Area J-1 reset)")
			elseif newRNG == 1779048496 then print(newRNG .. " (Area J-2 reset)")
			elseif newRNG == 1779052593 then print(newRNG .. " (Area J-3 reset)")
			elseif newRNG == 1779056690 then print(newRNG .. " (Area J-4 reset)")
			elseif newRNG == 1779060787 then print(newRNG .. " (Area J-5 reset)")
			elseif newRNG == 1795887156 then print(newRNG .. " (Area K-1 reset)")
			elseif newRNG == 1795891253 then print(newRNG .. " (Area K-2 reset)")
			elseif newRNG == 1795895350 then print(newRNG .. " (Area K-3 reset)")
			elseif newRNG == 1795899447 then print(newRNG .. " (Area K-4 reset)")
			elseif newRNG == 1812729913 then print(newRNG .. " (Area L-1 reset)")
			elseif newRNG == 1812734010 then print(newRNG .. " (Area L-2 reset)")
			elseif newRNG == 1812738107 then print(newRNG .. " (Area L-3 reset)")
			elseif newRNG == 1812742204 then print(newRNG .. " (Area L-4 reset)")
			elseif newRNG == 1829572669 then print(newRNG .. " (Area M-1 reset)")
			elseif newRNG == 1829576766 then print(newRNG .. " (Area M-2 reset)")
			elseif newRNG == 1829580863 then print(newRNG .. " (Area M-3 reset)")
			elseif newRNG == 1846415424 then print(newRNG .. " (Area N-1 reset)")
			elseif newRNG == 1863258177 then print(newRNG .. " (Area O-1 reset)")
			elseif newRNG == 1863262274 then print(newRNG .. " (Area O-2 reset)")
			elseif newRNG == 2014318659 then print(newRNG .. " (Area X-1 reset)")
			elseif newRNG == 2014322756 then print(newRNG .. " (Area X-2 reset)")
			elseif newRNG == 2014326853 then print(newRNG .. " (Area X-3 reset)")
			elseif newRNG == 2047938630 then print(newRNG .. " (Transerver room reset)")
			elseif newRNG == 2047942727 then print(newRNG .. " (Boss room reset)")
			-- Unknown advance
			else print(newRNG)
			end
		end
		RNG = newRNG
	end
end

-- GUI text
local x = 1
local y = 1
function drawGUI()
	if printMode == 0 then
		gui.text(x, y, "Nothing", "#FF0000")
	elseif printMode == 1 then
		gui.text(x, y, "Moderate", "#FFFF00")
	elseif printMode == 2 then
		gui.text(x, y, "Complete", "#00FF00")
	end
end

-- Main loop
function main()
	executeCustomCommands()
	printRNGChanges()
end

emu.registerbefore(main)
gui.register(drawGUI)