User File #45103536094459113

Upload All User Files

#45103536094459113 - Pokemon Gen 4 loadlines lua with autodetection

loadlines.lua
1209 downloads
Uploaded 2/13/2018 5:35 AM by Fortranm (see all 102)
So I decided to merge MKDasher's 3 loadlines luas and added a auto-detection function based on the one from his DPP RNG lua. US versions of all Gen 4 Pokemon games and JP versions of Diamond/Pearl are supported.
local game = 1 
local gamename = ""
-- 1 = HG/SS, 2 = Platinum, 3 = D/P
local pointerAddr = 0
local XPositionAddrOffset = {0x32DD2, 0x308B4, 0x31AA6}
local YPositionAddrOffset = {0x32DDA, 0x308BC, 0x31AAE}
local ZPositionAddrOffset = {0x32DD6, 0x308B8, 0x31AAA}
local movementXAddrOffset = {0x32DD0, 0x308CC, 0x31AA4}
local movementYAddrOffset = {0x32DD8, 0x308D4, 0x31AAC}
local mapOffset = 0x31146
local map = 0
local pointer = 0
local XPos = {0,0,0,0,0,0}
local YPos = {0,0,0,0,0,0}
local ZPos = {0,0,0,0,0,0}
local movementX = {0,0,0,0,0,0}
local movementY = {0,0,0,0,0,0}
local colour = "blue"

local bnd,br,bxr=bit.band,bit.bor,bit.bxor
local rshift, lshift=bit.rshift, bit.lshift

function drawLoadLines()
	xmiddle = 128
	ystart = -192
	xstartLoop = -112 - rshift(movementX[1],12)
	xloadX = -(XPos[1] % 32) + 23
	xloadY = -(YPos[1] % 32) + 25
	gui.box(127 - rshift(movementX[1],12),-83 - rshift(movementY[1],12),144 - rshift(movementX[1],12),-95 - rshift(movementY[1],12), "#88FFFFA0", "#0000FFFF")
	for i = -1,18,1 do
		colour = "blue"
		if i == xloadX then colour = "red"
		end
		gui.line((xstartLoop+i*16)*0.89+xmiddle,-192,(xstartLoop+i*16)*1.14+xmiddle,0, colour)
		if i == xloadX then
			gui.line((xstartLoop+i*16)*0.89+xmiddle-1,-192,(xstartLoop+i*16)*1.14+xmiddle-1,0, colour)
			gui.line((xstartLoop+i*16)*0.89+xmiddle+1,-192,(xstartLoop+i*16)*1.14+xmiddle+1,0, colour)
		end
	end
	ystart = ystart + 8 - rshift(movementY[1],12)
	for i = 1,16 do
		colour = "blue"
		if i == xloadY then colour = "red"
		end
		auxy = {4,13,23,33,43,54,65,77,89,101,114,127,141,155,169,184}
		ynum = auxy[i]
		gui.line(0,ynum + ystart, 255, ynum + ystart, colour)
		if i == xloadY then
			gui.line(0,ynum-1 + ystart, 255, ynum-1 + ystart, colour)
			gui.line(0,ynum+1 + ystart, 255, ynum+1 + ystart, colour)
		end
	end
end

function fn()
	gameIDAddr = memory.readdword(0x23FFE0C)
	if gameIDAddr == 0x45415041 then
		game = 3
		gamename = "Pearl (U)"
		pointerAddr = 0x02106FAC
	elseif gameIDAddr == 0x45414441 then
		game = 3
		gamename = "Diamond (U)"
		pointerAddr = 0x02106FAC
	elseif gameIDAddr == 0x4A415041 then
		game = 3
		gamename = "Pearl (J)"
		pointerAddr = 0x02108804
	elseif gameIDAddr == 0x4A414441 then
		game = 3
		gamename = "Diamond (J)"
		pointerAddr = 0x02108804
	elseif gameIDAddr == 0x45555043 then
		game = 2
		gamename = "Platinum (U)"
		pointerAddr = 0x02101D2C
	elseif gameIDAddr == 0x454B5049 then
		game = 1
		gamename = "HeartGold (U)"
		pointerAddr = 0x0211186C
	elseif gameIDAddr == 0x45475049 then
		game = 1
		gamename = "SoulSilver (U)"
		pointerAddr = 0x0211186C
	else
		game = -1
		gamename = "Invalid game"
	end
	pointer = memory.readdword(pointerAddr)
	gui.text(5,140,"Game: " .. gamename,"#FFFFFFB0")
	gui.text(20,5,bit.tohex(pointer) .. " " .. bit.tohex(pointer + movementXAddrOffset[game]) .. " (Pt)", "yellow")
	gui.text(20,15,bit.tohex(pointer) .. " " .. bit.tohex(ZPositionAddrOffset[game] + pointer) .. " (HG/SS)", "yellow")
	gui.text(5,150,"Map: " .. map, "yellow")
	gui.text(5,160,"X position: " .. XPos[5], "yellow")
	gui.text(5,170,"Y position: " .. YPos[5], "yellow")
	gui.text(5,180,"Z position: " .. ZPos[5], "yellow")
	drawLoadLines()
end
gui.register(fn)

function delayRAMread()
	for i = 1,4 do
		movementX[i] = movementX[i+1]
		movementY[i] = movementY[i+1]
		XPos[i] = XPos[i+1]
		YPos[i] = YPos[i+1]
		ZPos[i] = ZPos[i+1]
	end
	movementX[5] = memory.readword(pointer + movementXAddrOffset[game])
	movementY[5] = memory.readword(pointer + movementYAddrOffset[game])
	XPos[5] = memory.readword(XPositionAddrOffset[game] + pointer)
	YPos[5] = memory.readword(YPositionAddrOffset[game] + pointer)
	ZPos[5] = memory.readword(ZPositionAddrOffset[game] + pointer)
	map = memory.readword(mapOffset + pointer)
end
emu.registerafter(delayRAMread)