User File #27870229146523304

Upload All User Files

#27870229146523304 - Advance Wars: Dual Strike (USA) RNG Lua

AWDS simple RNG.lua
838 downloads
Uploaded 12/30/2015 3:01 AM by ALAKTORN (see all 34)
local function Multiply(int1, int2)
	local High1 = math.floor(int1 / 32768)
	local Low1 = math.floor(int1 % 32768)
	local High2 = math.floor(int2 / 32768)
	local Low2 = math.floor(int2 % 32768)
	
	local NewHigh = (High1 * Low2 + High2 * Low1) % 32768;
	local NewLow = Low1 * Low2
	
	return (NewHigh * 32768 + NewLow) % 1073741824;
end

function RNGStep(i)
	local i4 = (i * 4 + 1) % 1073741824
	return Multiply(i4, i + 1)
end

local function RNGSteps(iRNG, iTimes)
	while iTimes > 0 do
		iRNG = RNGStep(iRNG)
		iTimes = iTimes - 1
	end
	return iRNG
end

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

local function RNGFrameAdv(iRNG, GameTime)
	local x = GameTime + RNGStep(iRNG)
	local newRNG = RNGStep(x)
	newRNG = newRNG + (math.floor(newRNG / 2) % 2)
	return newRNG
end

local function RNGMenuAdv(iRNG)
	for i = 1, 80 do
		iRNG = RNGStep(iRNG)
		iRNG = iRNG + (math.floor(iRNG / 2) % 2)
	end
	return iRNG
end

local MapTimerAddr = 0x2189394
MapTimer = -1
MapTimerFrame = -1
function readMapTimer()
	local newMapTime = memory.readdword(MapTimerAddr)
	if MapTimer == newMapTime then
		MapTimerFrame = MapTimerFrame + 1
	else
		MapTimer = newMapTime
		MapTimerFrame = 1
		-- Player map timer increased? Then AI Day is certainly over
		AIDayActive = false
	end
end

local GameTimerAddr = 0x27C0738
GameTimer = -1
GameTimerFrame = -1
function readGameTimer()
	local newGameTime = memory.readdword(GameTimerAddr)
	if GameTimer == newGameTime then
		GameTimerFrame = GameTimerFrame + 1
	else
		GameTimer = newGameTime
		GameTimerFrame = 1
	end
end

local RNGAddr = 0x216E2B4
RNG = -1
TotalNumSteps = 0
function readRNG(printChanges)
	newRNG = memory.readdword(RNGAddr)
	if RNG ~= newRNG then
		printText = ''
		if RNG ~= -1 then
			local numSteps = getNumSteps(RNG, newRNG)
			if numSteps ~= -1 then
				TotalNumSteps = TotalNumSteps + numSteps
				printText = newRNG .. ' (' .. numSteps .. ')' .. ' Total: ' .. TotalNumSteps .. ' at ' .. GameTimer
			else
				TotalNumSteps = 0
				-- FrameBasedAdvance! AI Day started
				if newRNG == RNGFrameAdv(RNG, GameTimer) then
					printText = newRNG .. ' (Frame ' .. GameTimer .. ')'
					AIDayActive = true
				else
					local menuRNG = RNG
					for i = 1, 10 do
						menuRNG = RNGMenuAdv(menuRNG)
						if newRNG == menuRNG then
							printText = newRNG .. ' (Menu * ' .. i .. ' at ' .. GameTimer .. ')'
							AIDayActive = false
						end
					end
				end
			end
		end
		if printChanges then
			if printText ~= '' then
				print(printText)
			else
				print(newRNG)
			end
		end
		RNG = newRNG
	end
end

function main()
	readMapTimer()
	readGameTimer()
	readRNG(true)
end

emu.registerbefore(main)