User File #41561075890549511

Upload All User Files

#41561075890549511 - Zook Man ZX4 - Allocation Viewer

ZM_Allocation_Display.lua
879 downloads
Uploaded 9/6/2017 4:44 PM by ThunderAxe31 (see all 111)
Scripts that displays on screen the allocation of sprite memory allocation. Ultra-light version by ThunderAxe31, based on the basic script by FatRatKnight.
Colors:
Black - Free
Cyan - Player bullet
Green - Item
Yellow - Enemy (w/ 0+ HP reported)
Magenta - Enemy (w/ negative HP reported)
Red - No detected object -- Memory leak likely
--Zook Man ZX4 - Allocation Viewer
--Ultra-light version by ThunderAxe31, based on the basic script by FatRatKnight.

AllocationArrayStart   = 0x7C44
ZookBulletsArrayStart  = 0x226C
OtherObjectsArrayStart = 0x2B7C

AllocationArrayLenght   = 192
ZookBulletsArrayLenght  =  20
OtherObjectsArrayLenght =  30

ObjectAllocationOffset = 0x0E
ObjectAmountOffset     = 0x40

IsFreeSpace  = 0xFF000000 --black
IsZookBullet = 0xFF00FFFF --cyan
IsItemDrop   = 0xFF00FF00 --green
IsEnemy      = 0xFFFFFF00 --yellow
IsOther      = 0xFFFF00FF --magenta
IsLeaked     = 0xFFFF0000 --red

local AllocationArrayColor = {}

function ReadAllocation()
	for i = 0, AllocationArrayLenght-1 do
		if memory.read_u8(AllocationArrayStart+i, "IWRAM") == 0 then
			AllocationArrayColor[i] = IsFreeSpace
		else
			AllocationArrayColor[i] = IsLeaked
		end
	end
end

function SetArrayChunckColor(Color, Start, Lenght)
	for i = Start, Start+Lenght-1 do
		AllocationArrayColor[i] = Color
	end
end

function ReadZookBullets()
	for i = 0, ZookBulletsArrayLenght-1 do
		local ObjectAddress = ZookBulletsArrayStart + i*0x74
	
		if memory.read_s16_le(ObjectAddress+0x08, "IWRAM") ~= 0 then --If does exist
			local AllocationAddressStart  = math.floor((memory.read_u16_le(ObjectAddress+0x0E)-256)/4)
			local AllocationAddressLenght = memory.read_u16_le(ObjectAddress+0x40)
			SetArrayChunckColor(IsZookBullet, AllocationAddressStart, AllocationAddressLenght)
		end
	end
end

function ReadOtherObjects()
	for i = 0, OtherObjectsArrayLenght-1 do
		local ObjectAddress = OtherObjectsArrayStart + i*0x74
		local ObjectColor = 0
		
		if memory.read_s16_le(ObjectAddress+0x3C, "IWRAM") == 2 then --Is an item
			ObjectColor = IsItemDrop
		elseif memory.read_s16_le(ObjectAddress+0x2C, "IWRAM") ~= -1 then --Has HP, so it's an enemy
			ObjectColor = IsEnemy
		else --Can't be damaged, like enemy bullets and destroyed enemy parts splatting around
			ObjectColor = IsOther
		end
		
		local AllocationAddressStart  = math.floor((memory.read_u16_le(ObjectAddress+0x0E)-256)/4)
		local AllocationAddressLenght = memory.read_u16_le(ObjectAddress+0x40)
		SetArrayChunckColor(ObjectColor, AllocationAddressStart, AllocationAddressLenght)
	end
end

function SetAllocationColors()
	ReadAllocation()
	ReadZookBullets()
	ReadOtherObjects()
end

function DrawAllocationBar()
	local Last = AllocationArrayColor[0]
	local ChunkLenght = 1
	
	for i = 1, AllocationArrayLenght-1 do
		if Last ~= AllocationArrayColor[i] or i == AllocationArrayLenght-1 then
			gui.drawRectangle(i-ChunkLenght,0,ChunkLenght,3,AllocationArrayColor[i-1],AllocationArrayColor[i-1])
			ChunkLenght = 1
		else
			ChunkLenght = ChunkLenght + 1
		end
		Last = AllocationArrayColor[i]
	end
end

while true do
	SetAllocationColors()
	DrawAllocationBar()
	emu.frameadvance()
end