Branch comparision not working properly for some reason
function Draw()
gui.drawRectangle(0, 167, 255, 56, 0x88FFFFFF, 0x88000000)
gui.drawLine(128, 168, 128, 222, 0x88FFFFFF)
gui.drawText(2, 168, "X: "..kirby.position.x[frame]..":"..kirby.subpixel.x[ frame], 0xFFFFFFFF, 0x44FF0000)
gui.drawText(2, 180, "Y: "..kirby.position.y[frame]..":"..kirby.subpixel.y[frame], 0xFFFFFFFF, 0x4400FF00)
gui.drawText(2, 192, "Xv: "..kirby.velocity.x[frame], 0xFFFFFFFF, 0x44FF0000)
gui.drawText(2, 204, "Yv: "..kirby.velocity.y[frame], 0xFFFFFFFF, 0x4400FF00)
end
function sgn(x)
if x > 0
then return 1
elseif x < 0
then return -1
else return 0
end
end
function Color(index, color1, color2)
if index >= ungreenframe
then return color1
else return color2
end
end
function ColorGradient(value, maxvalue, index)
if value < 0
then red = 255
green = 0
blue = 0
elseif value < maxvalue/2
then red = 255
green = math.floor((255/(maxvalue-maxvalue/2))*math.abs(value))
blue = 0
elseif math.abs(value) < maxvalue
then red = math.floor(-(255/(maxvalue-maxvalue/2))*math.abs(value)+510)
green = 255
blue = 0
else red = 0
green = 255
blue = 0
end
if (index >= ungreenframe)
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
alpha = 255
return alpha*256*256*256+red*256*256+green*256+blue
end
function TAStudioText(index, column)
if kirby.velocity.x[index] ~= nil and column == "Xv"
then return tostring(kirby.velocity.x[index])
elseif kirby.velocity.y[index] ~= nil and column == "Yv"
then return tostring(kirby.velocity.y[index])
elseif branches[selectedbranch] ~= nil and branches[selectedbranch].position.x[index] ~= nil and column == "Dx" and kirby.position.x[index] ~= nil and kirby.subpixel.x[index] ~= nil
then local d = ((kirby.position.x[index]*256 + kirby.subpixel.x[index])
- (branches[selectedbranch].position.x[index]* 256 + branches[selectedbranch].subpixel.x[index]))
* sgn(branches[selectedbranch].velocity.x[index])
return tostring(math.floor(d / 256) ..":" .. d % 256)
elseif branches[selectedbranch] ~= nil and branches[selectedbranch].position.y[index] ~= nil and column == "Dy" and kirby.position.y[index] ~= nil and kirby.subpixel.y[index] ~= nil
then local d = ((kirby.position.y[index]*256 + kirby.subpixel.y[index])
- (branches[selectedbranch].position.y[index]* 256 + branches[selectedbranch].subpixel.y[index]))
* sgn(branches[selectedbranch].velocity.y[index])
return tostring(math.floor(d / 256) ..":" .. d % 256)
end
if column == "Xv" or column == "Yv" or column == "Dx" or column == "Dy"
then return ""
end
end
function TAStudioColor(index, column)
if kirby.velocity.x[index] ~= nil and column == "Xv"
then return ColorGradient(math.abs(kirby.velocity.x[index]), 704, index)
elseif kirby.velocity.y[index] ~= nil and column == "Yv"
then return ColorGradient(math.abs(kirby.velocity.y[index]), 880, index)
elseif branches[selectedbranch] ~= nil and branches[selectedbranch].position.x[index] ~= nil and column == "Dx" and kirby.position.x[index] ~= nil and kirby.subpixel.x[index] ~= nil
then return ColorGradient(((kirby.position.x[index]*256 + kirby.subpixel.x[index])
- (branches[selectedbranch].position.x[index]* 256 + branches[selectedbranch].subpixel.x[index]))
* sgn(branches[selectedbranch].velocity.x[index]), 256, index)
elseif branches[selectedbranch] ~= nil and branches[selectedbranch].position.y[index] ~= nil and column == "Dy" and kirby.position.y[index] ~= nil and kirby.subpixel.y[index] ~= nil
then return ColorGradient(((kirby.position.y[index]*256 + kirby.subpixel.y[index])
- (branches[selectedbranch].position.y[index]* 256 + branches[selectedbranch].subpixel.y[index]))
* sgn(branches[selectedbranch].velocity.y[index]), 256, index)
end
end
function Ungreen(index)
if ungreenframe > index
then ungreenframe = index
end
end
function LoadFile(index)
--Backup file for undo branch load
SaveFile(-1)
local file = io.open(movie.filename()..tostring(index)..".txt", "r")
local i = 0
if file ~= nil
then for line in file:lines(1) do
local s = {}
s = bizstring.split(line, ";")
--Use the same order as saving
kirby.position.x[i] = tonumber(s[1])
kirby.position.y[i] = tonumber(s[2])
kirby.subpixel.x[i] = tonumber(s[3])
kirby.subpixel.y[i] = tonumber(s[4])
kirby.velocity.x[i] = tonumber(s[5])
kirby.velocity.y[i] = tonumber(s[6])
i = i + 1
end
file:close()
end
end
function SaveFile(index)
local file = io.open(movie.filename()..tostring(index)..".txt", "w+")
for i = 0, movie.length(), 1 do
file:write(tostring(kirby.position.x[i])..";"..
tostring(kirby.position.y[i])..";"..
tostring(kirby.subpixel.x[i])..";"..
tostring(kirby.subpixel.y[i])..";"..
tostring(kirby.velocity.x[i])..";"..
tostring(kirby.velocity.y[i]).."\n")
end
file:close()
end
function BranchLoad(index)
LoadFile(index)
end
function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function BranchSave(index)
if index >= 0
then kirby_ = {position = {x = {}, y = {}},
subpixel = {x = {}, y = {}},
velocity = {x = {}, y = {}},
}
kirby_ = deepcopy(players)
branches[index+1] = kirby_
end
SaveFile(index)
end
function BranchRemove(index)
table.remove(branches, index+1)
local branches = tastudio.getbranches()
os.remove(movie.filename()..tostring(index)..".txt")
for i = index, table.getn(branches), 1 do
os.rename(movie.filename()..tostring(i+1)..".txt", movie.filename()..tostring(i)..".txt")
end
end
function LogData()
kirby.position.x[frame] = memory.read_u8(0x0083) + memory.read_u8(0x0095)*256
kirby.position.y[frame] = memory.read_u8(0x00B9) + memory.read_u8(0x00CB)*256
kirby.subpixel.x[frame] = memory.read_u8(0x0071)
kirby.subpixel.y[frame] = memory.read_u8(0x00A7)
kirby.velocity.x[frame] = memory.read_s16_le(0x05B9)
kirby.velocity.y[frame] = memory.read_s16_le(0x05BD)
--print("ASD");
end
function Exit()
SaveFile(-2)
end
function FrameEnd()
LogData()
Draw()
end
memory.usememorydomain("RAM")
kirby = {position = {x = {}, y = {}},
subpixel = {x = {}, y = {}},
velocity = {x = {}, y = {}}
}
branches = {}
tastudio.addcolumn("Xv", "Xv", 40)
tastudio.addcolumn("Yv", "Yv", 40)
tastudio.addcolumn("Dx", "Dx", 60)
tastudio.addcolumn("Dy", "Dy", 60)
ungreenframe = 0
tastudio.onqueryitemtext(TAStudioText)
tastudio.onqueryitembg(TAStudioColor)
tastudio.ongreenzoneinvalidated(Ungreen)
tastudio.onbranchsave(BranchSave)
tastudio.onbranchload(BranchLoad)
tastudio.onbranchremove(BranchRemove)
event.onexit(Exit)
event.onframeend(FrameEnd)
selectedbranch = 1
for index = 1, table.getn(tastudio.getbranches())+1, 1 do
kirby_ = {position = {x = {}, y = {}},
subpixel = {x = {}, y = {}},
velocity = {x = {}, y = {}},
}
local file = io.open(movie.filename()..tostring(index-1)..".txt", "r")
local i = 0
if file ~= nil
then for line in file:lines(1) do
local s = {}
s = bizstring.split(line, ";")
--Use the same order as saving
kirby_.position.x[i] = tonumber(s[1])
kirby_.position.y[i] = tonumber(s[2])
kirby_.subpixel.x[i] = tonumber(s[3])
kirby_.subpixel.y[i] = tonumber(s[4])
kirby_.velocity.x[i] = tonumber(s[5])
kirby_.velocity.y[i] = tonumber(s[6])
i = i + 1
end
branches[index] = kirby_
file:close()
end
end
LoadFile(-2)
while true do
frame = emu.framecount()
if frame > ungreenframe
then ungreenframe = frame
end
wasinp = inp
inp = input.get()
if inp["P"] and wasinp["P"] == nil and selectedbranch <= table.getn(tastudio.getbranches())
then selectedbranch = selectedbranch + 1
elseif inp["M"] and wasinp["M"] == nil and selectedbranch > 1
then selectedbranch = selectedbranch - 1
end
lag = memory.read_u8(0x01a5)
if (lag ==1)
then emu.setislagged()
tastudio.setlag(emu.framecount(), true)
else emu.setislagged(false)
tastudio.setlag(emu.framecount(), false)
end
gui.pixelText(0, 160, "B:"..selectedbranch)
LogData()
Draw()
emu.yield()
end