User File #51620369168057608

Upload All User Files

#51620369168057608 - Donald Land lua

Donald Land.lua
Game: Donald Land ( NES, see all files )
690 downloads
Uploaded 12/3/2018 5:17 PM by TASeditor (see all 188)
Output logging stuff
function GetColorFromValue(value, maxValue, index)

	if value ~= nil
	then if math.abs(value) < (maxValue/2)
		 then red = 255
			  green = math.floor((255/(maxValue/2))*math.abs(value))
			  blue = 0
		 else red = math.floor(-(255/(maxValue/2))*math.abs(value)+510)
			  green = 255
			  blue = 0
		 end
		 if (index >= ug)
		 then red = math.floor((255-red)/120*(200-240)+255)
			  green = math.floor((255-green)/120*(200-240)+255)
			  blue = math.floor((255-blue)/120*(200-240)+255)
		 end
		 return red*256*256+green*256+blue
	end
end	

function Color(index, column)
	
	if column == "xSpdCol" and xSpdTable[index] ~= nil
	then color = GetColorFromValue(xSpdTable[index], 1280, index)
		 return color
	elseif column == "dxCol" and xTable[index] ~= nil and xTable[index-1] ~= nil and xSubTable[index] ~= nil and xSubTable[index-1] ~= nil
		then color = GetColorFromValue((xTable[index]*256+xSubTable[index])-(xTable[index-1]*256+xSubTable[index-1]), 1280, index)
			 return color
	elseif column == "ySpdCol" and ySpdTable[index] ~= nil
		then color = GetColorFromValue(ySpdTable[index], 1024, index)
			 return color
	elseif column == "P1 A" and groundTable[index] ~= nil
		then if groundTable[index] == 0
			 then if index >= ug
				  then return 0xAAECDD
				  else return 0x00C8FF
				  end
			 end
	elseif column == "P1 B" and appleTable[index] ~= nil
		then if appleTable[index] >= 2 and appleTable[index] <= 3
			 then if index >= ug
				  then return 0xAAECFF
				  else return 0x00C8FF
				  end
			 elseif appleTable[index] >= 4
			 then if index >= ug
				  then return 0xAADDFF
				  else return 0x0096FF
				  end
			 end
	end

end

function Text(index, column)

	if column == "xSpdCol" and xSpdTable[index] ~= nil
	then return tostring(xSpdTable[index])
	elseif column == "dxCol" and xTable[index] ~= nil and xTable[index-1] ~= nil and xSubTable[index] ~= nil and xSubTable[index-1] ~= nil
		then return tostring((xTable[index]*256+xSubTable[index])-(xTable[index-1]*256+xSubTable[index-1]))
	elseif column == "ySpdCol" and ySpdTable[index] ~= nil
		then return tostring(ySpdTable[index])
	end

end

function Ungreen(index)

	if ug > index
	then ug = index
	end

end

function BranchLoad(index)
	--print("load "..index)
	lastBranchAction = "load"
	--save backup file
	if index >= 0
	then local file = io.open(movie.filename()..tostring(-1)..".txt", "w+")
	
		 for i = 0, emu.framecount(), 1 do
			 file:write(tostring(xSpdTable[i])..";"..
					 tostring(ySpdTable[i])..";"..
					 tostring(groundTable[i])..";"..
					 tostring(appleTable[i])..";"..
					 tostring(xTable[i])..";"..
					 tostring(xSubTable[i])..";"..
					 tostring(yTable[i])..";"..
					 tostring(ySubTable[i]).."\n")
		 end
		 file:close()
		 --print("backup file created")
	end
	
	local file = io.open(movie.filename()..tostring(index)..".txt", "r")
	local num = 0
	
	if file ~= nil
	then for i in file:lines(1) do
			 local str = {}
			 str = bizstring.split(i, ";")
		
			 xSpdTable[num] = tonumber(str[1])
			 ySpdTable[num] = tonumber(str[2])
			 groundTable[num] = tonumber(str[3])
			 appleTable[num] = tonumber(str[4])
			 xTable[num] = tonumber(str[5])
			 xSubTable[num] = tonumber(str[6])
			 yTable[num] = tonumber(str[7])
			 ySubTable[num] = tonumber(str[8])
			 
			 num = num + 1
		 end
		 file:close()
	end
	
end

function BranchSave(index)
	--print("save "..index)
	lastBranchAction = "save"
	local file = io.open(movie.filename()..tostring(index)..".txt", "w+")
	
	for i = 0, ug, 1 do
		file:write(tostring(xSpdTable[i])..";"..
				   tostring(ySpdTable[i])..";"..
				   tostring(groundTable[i])..";"..
				   tostring(appleTable[i])..";"..
				   tostring(xTable[i])..";"..
				   tostring(xSubTable[i])..";"..
				   tostring(yTable[i])..";"..
				   tostring(ySubTable[i]).."\n")
	end
	file:close()
end

function BranchRemove(index)
	--print("remove "..index)
	lastBranchAction = "remove"
	local branches = tastudio.getbranches()
	
	os.remove(movie.filename()..tostring(index)..".txt")
	--print("b"..table.getn(branches))
	for i = index, table.getn(branches), 1 do
		os.rename(movie.filename()..tostring(i+1)..".txt", movie.filename()..tostring(i)..".txt")
		--print("renamed "..tostring(i+1).." to "..tostring(i))
	end

end

function Exit()

	local file = io.open(movie.filename().."c.txt", "w+")
	
	for i = 0, ug, 1 do
		file:write(tostring(xSpdTable[i])..";"..
				   tostring(ySpdTable[i])..";"..
				   tostring(groundTable[i])..";"..
				   tostring(appleTable[i])..";"..
				   tostring(xTable[i])..";"..
				   tostring(xSubTable[i])..";"..
				   tostring(yTable[i])..";"..
				   tostring(ySubTable[i]).."\n")

	end
	file:close()
end

function PlayerData()

	xCam = memory.read_u16_le(0x00CC)
	
	xPos = memory.read_u16_be(0x0617) 
	
	xOnS = memory.read_u8(0x0091)
	xSub = memory.read_u8(0x0092)
	yOnS = memory.read_u8(0x0093)
	ySub = memory.read_u8(0x0094)
	yOnS_ = memory.read_u8(0x0095)
	
	xSpd = memory.read_s16_be(0x0098)
	xSpd_ = memory.read_s16_be(0x009A)
	ySpd = memory.read_s16_be(0x009C)

	xSpdMax = memory.read_u16_be(0x009E)
	
	blockState = memory.read_u8(0x00A8)
	
	jumpState = memory.read_u8(0x00AB) -- 0: ground, 1: jump, 2: fall
	ascendFrames = memory.read_u8(0x00AC) --?
	
	apple1Active = memory.read_u8(0x04F9)
	apple2Active = memory.read_u8(0x04FA)
	
	dx = 0
	dy = 0
	
	onGround = bit.band(blockState, 33)
	
	if onGround > 1 then onGround = 1 end
	
	if xTable[f] ~= nil and xSubTable[f]
	then dx = (((xOnS+xCam)*256+xSub)-(xTable[f]*256+xSubTable[f]))/256	 
	end
	
	if yTable[f] ~= nil and ySubTable[f]
	then dy = (((yOnS)*256+ySub)-(yTable[f]*256+ySubTable[f]))/256	 
	end
	
	
	xSpdTable[f] = xSpd_
	ySpdTable[f] = ySpd
	--jumpTable[f] = jumpState
	groundTable[f] = onGround
	appleTable[f] = apple1Active + apple2Active
	xTable[f] = xOnS+xCam
	xSubTable[f] = xSub
	yTable[f] = yOnS
	ySubTable[f] = ySub
	
end

function DisplayLogFromBranch()

	if not dontDraw
	then for i = f-60, f+60, 1 do
			 if _xTable[i] ~= nil and _yTable[i] ~=nil and _xSpdTable[i] ~= nil
			 then if _xTable[i] >= xCam and _xTable[i] <= xCam+256 
				  then if i >= f
					   then gui.drawAxis(_xTable[i]-xCam, _yTable[i], 1, 0xFF3333BB)--;print("help")--+GetColorFromValue(_xSpdTable[i], 1280, 0)) --TODO: Better coloring
					   else gui.drawAxis(_xTable[i]-xCam, _yTable[i], 1, 0xFF6666FF)
					   end
				  end
			 end
		 end
		 
		 if xTable[f] ~= nil and xSubTable[f] ~= nil and yTable[f] ~= nil and ySubTable[f] ~= nil and _xTable[f] ~= nil and _xSubTable[f] ~= nil and _yTable[f] ~= nil and _ySubTable[f] ~= nil
		 then local xDiff = (xTable[f]*256+xSubTable[f])-(_xTable[f]*256+_xSubTable[f])
			  local yDiff = (yTable[f]*256+ySubTable[f])-(_yTable[f]*256+_ySubTable[f])
			  
			  local xPosDiff = xDiff / 256 -- floor division // doesn't work
			  --local xSubDiff = xDiff % 256 
			  local yPosDiff = yDiff / 256
			  --local ySubDiff = yDiff % 256
			  
			  gui.pixelText(2, 92, "dx':"..xPosDiff, 0xFFFFFFFF, 0x77990000, "fceux")
			  gui.pixelText(2, 100, "dy':"..yPosDiff, 0xFFFFFFFF, 0x77009900, "fceux")
		 end
	end
end

function Player()
	
	for i = f-60, f+60, 1 do
		if xTable[i] ~= nil and yTable[i] ~=nil and xSpdTable[i] ~= nil
		then if xTable[i] >= xCam and xTable[i] <= xCam+256 
			 then if i >= f
				  then gui.drawAxis(xTable[i]-xCam, yTable[i], 2, 0xFFBB3333)--+GetColorFromValue(xSpdTable[i], 1280, 0))
				  else gui.drawAxis(xTable[i]-xCam, yTable[i], 2, 0xFFFF6666)--+GetColorFromValue(xSpdTable[i], 1280, 0))
				  end
			 end
		end
	end
	
	--_xOnS = xOnS
	--_xSub = xSub
	--_yOnS = yOnS
	--_ySub = ySub
		
	gui.pixelText(2, 36, "Xp:"..xOnS+xCam..":"..xSub, 0xFFFFFFFF, 0x77990000, "fceux");
	gui.pixelText(2, 44, "Yp:"..yOnS..":"..ySub, 0xFFFFFFFF, 0x77009900, "fceux");
	gui.pixelText(2, 52, "Xv:"..xSpd..", "..xSpd_, 0xFFFFFFFF, 0x77990000, "fceux");
	gui.pixelText(2, 60, "Yv:"..ySpd.." ", 0xFFFFFFFF, 0x77009900, "fceux");
	gui.pixelText(2, 68, "dx:"..dx, 0xFFFFFFFF, 0x77990000, "fceux")
	gui.pixelText(2, 76, "dy:"..dy, 0xFFFFFFFF, 0x77009900, "fceux")
	gui.pixelText(2, 84, "up:"..ascendFrames, 0xFFFFFFFF, 0x77009900, "fceux")
end

function Objects()

	local xCam = memory.read_u16_le(0x00CC)
	
	--TODO: Hitboxes
	for i = 0, 7, 1 do
		
		local xPos = memory.read_u8(0x054B+i)+256*memory.read_u8(0x0543+i)
		local yPos = memory.read_u8(0x0563+i)
		
		local xSpd = 256*memory.read_s8(0x0583+i)+memory.read_u8(0x058B+i)
		local ySpd = 256*memory.read_s8(0x0593+i)
		--gui.drawAxis(xPos-xCam, yPos, 5)
		gui.pixelText(xPos-xCam, yPos, xSpd, nil, nil, "fceux")
		--gui.drawText(0, 40+16*i, xSpd..","..ySpd)
		
	end

end

function FrameEnd()


	
	local lagFlag = memory.read_u8(0x007F)
	
	if lagFlag == 1
	then tastudio.setlag(f, true)
	else --tastudio.setlag(f, false)
	end
	
	PlayerData()
	Objects()
	DisplayLogFromBranch()
	Player()

end

memory.usememorydomain("RAM")

ug = 0 -- ungreenzone frame
dontDraw = true
drawBranch = 0
lastBranchAction = nil

--_xOnS = 0
--_xSub = 0
--_yOnS = 0
--_ySub = 0

xSpdTable = {}
ySpdTable = {}
--jumpTable = {}
groundTable = {}
appleTable = {}
xTable = {}
xSubTable = {}
yTable = {}
ySubTable = {}

_xSpdTable = {}
_ySpdTable = {}
_groundTable = {}
_appleTable = {}
_xTable = {}
_xSubTable = {}
_yTable = {}
_ySubTable = {}

tastudio.addcolumn("xSpdCol", "X Speed", 60)
tastudio.addcolumn("dxCol", "X Diff", 60)
tastudio.addcolumn("ySpdCol", "Y Speed", 60)

tastudio.ongreenzoneinvalidated(Ungreen)
tastudio.onqueryitembg(Color)
tastudio.onqueryitemtext(Text)
tastudio.onbranchload(BranchLoad)
tastudio.onbranchsave(BranchSave)
tastudio.onbranchremove(BranchRemove)
event.onframeend(FrameEnd)
event.onexit(Exit)

local file = io.open(movie.filename().."c.txt", "r")
if file ~= nil
then local num = 0
	
	 for i in file:lines(1) do
		 local str = {}
		 str = bizstring.split(i, ";")
		
		 xSpdTable[num] = tonumber(str[1])
		 ySpdTable[num] = tonumber(str[2])
		 groundTable[num] = tonumber(str[3])
		 appleTable[num] = tonumber(str[4])
		 xTable[num] = tonumber(str[5])
		 xSubTable[num] = tonumber(str[6])
		 yTable[num] = tonumber(str[7])
		 ySubTable[num] = tonumber(str[8])
		
		 num = num + 1
	 end
	 file:close()
end

while true do
	
	f = emu.framecount()
	
	if f > ug
	then ug = f
	end
	
	local inp = input.get()
	--TODO: There could be more thatn 10 branches
	if inp["NumberPad0"] then drawBranch = 0; dontDraw = false   
	elseif inp["NumberPad1"] then drawBranch = 1; dontDraw = false
	elseif inp["NumberPad2"] then drawBranch = 2; dontDraw = false
	elseif inp["NumberPad3"] then drawBranch = 3; dontDraw = false
	elseif inp["NumberPad4"] then drawBranch = 4; dontDraw = false
	elseif inp["NumberPad5"] then drawBranch = 5; dontDraw = false
	elseif inp["NumberPad6"] then drawBranch = 6; dontDraw = false
	elseif inp["NumberPad7"] then drawBranch = 7; dontDraw = false
	elseif inp["NumberPad8"] then drawBranch = 8; dontDraw = false
	elseif inp["NumberPad9"] then drawBranch = 9; dontDraw = false
	elseif inp["NumberPadMinus"] then dontDraw = true
	end
	
	if drawBranch ~= oldDrawBranch
	then local file = io.open(movie.filename()..tostring(drawBranch)..".txt", "r")
		 local num = 0
		 
		 if file ~= nil
		 then for i in file:lines(1) do
				 local str = {}
				 str = bizstring.split(i, ";")
			 
				 _xSpdTable[num] = tonumber(str[1])
				 _ySpdTable[num] = tonumber(str[2])
				 _groundTable[num] = tonumber(str[3])
				 _appleTable[num] = tonumber(str[4])
				 _xTable[num] = tonumber(str[5])
				 _xSubTable[num] = tonumber(str[6])
				 _yTable[num] = tonumber(str[7])
				 _ySubTable[num] = tonumber(str[8])
				 
				 num = num + 1
			 end
			 file:close()
		 end
		 PlayerData()
		 Objects()
		 DisplayLogFromBranch()
		 Player()
		 gui.DrawFinish() 
	end
	
	oldDrawBranch = drawBranch
	
	emu.yield();
end