User File #73828299040239151

Upload All User Files

#73828299040239151 - DK64 sync-collect.lua

dk64-sync-collect.lua
Game: Donkey Kong 64 ( N64, see all files )
181 downloads
Uploaded 8/29/2021 8:40 PM by Zinfidel (see all 12)
Sync script modified to work on very old versions of BizHawk, with code specific to syncing DK64.
-- This script is for collecting "sync data" for a game that consists of some sort of canary data
-- that is very likely to indicate a desync happening. Good choices for this kind of data are transformation
-- matrices for models, or manually collected position data like x,y,z,pitch,yaw,roll,etc.

-- Change this to some amount of frames on which data will be flushed. 200 means flush to file every 200 frames.
FLUSH_FRAMES = 200

-- Check if a file exists on the filesystem
local function file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end

-- Create a "log.txt" file for the data, but add a number so we don't accidentally overwrite previous results.
local name = "log.txt"
local i = 1
while (file_exists(name)) do
	name = "log" .. tostring(i) .. ".txt"
	i = i + 1
end
LogFile = io.open(name, "w")

-- Flush and close the log file when the script closes.
local onExitEvent = event.onexit(
	function()
		LogFile:close()
		console.log("")
		console.log("Script stopping. Closing log file.")
	end
	, "Exit Event" );

local function fl(val)
	return mainmemory.readfloat(val, true)
end

u32 = mainmemory.read_u32_be

local function sigfig(value, figs)
	str = string.format("%f", value)
	return str:sub(1, figs+1) -- account for '.' character
end

-- Collect sync data
console.write("Flushing to file ")
while true do
	-- Custom data collection for the game goes in here. The example code below is for Star Wars Episode 1: Racer,
	-- it finds the player's position matrix and records each matrix entry as a float to 5 decimals. It writes these
	-- data to a comma-separated line.
	---------------------------------------------------------------------------------------------------------------

	-- get pointers
	local player = u32(0x7FBB4C)
	local camera = u32(0x7FB968)
	
	-- the actual values are floats
	-- but let's just use their raw hex view for simplicity
	if player >= 0x80000000 and player < 0x80800000 then
		player = player - 0x80000000
		pos = string.format("%s,%s,%s",
			sigfig(fl(player + 0x7C), 8), -- x
			sigfig(fl(player + 0x80), 8), -- y
			sigfig(fl(player + 0x84), 8)) -- z
	else
		pos = "00000000,00000000,00000000"
	end
	
	if camera >= 0x80000000 and camera < 0x80800000 then
		camera = camera - 0x80000000
		cam = string.format("%s,%s,%s",
			sigfig(fl(camera + 0x1FC), 8), -- x
			sigfig(fl(camera + 0x200), 8), -- y
			sigfig(fl(camera + 0x204), 8)) -- z
	else
		cam = "00000000,00000000,00000000"
	end
	
	-- conbined string
	LogFile:write(pos ..  "," .. cam)

	---------------------------------------------------------------------------------------------------------------
	-- End custom collection code

	-- New line, and if we are at a flush boundary, flush the file.
	LogFile:write('\n')
	if (emu.framecount() % FLUSH_FRAMES) == 0 then
		LogFile:flush()
		console.write('.')
	end

	emu.frameadvance()
end