Post subject: Desync in v22 (Final Fantasy Legend)
Player (79)
Joined: 8/5/2007
Posts: 865
I have tried four or five times to start a demonstration run of Final Fantasy Legend, but it desyncs every time, usually about 4000 frames in. My partner in crime, FatRatKnight, has had no trouble whatsoever, although he's using a very different strategy. I suspect it has something to do with my Lua script, which I'll post here:
movie.rerecordcounting(false)

local state1 = savestate.create()
local state2 = savestate.create()

local H,S,D,A,M,X,Y= "hp","st","df","ag","mn","AB","!!"
local _ = "__"

local Growth={[0]=H,
    _,_,D,X,_,M,_,A,_,_,X,_,X,_,_,
  _,_,X,M,A,_,_,M,_,_,_,_,M,X,_,M,
  _,_,_,_,_,_,A,_,_,_,X,M,X,H,X,_,
  _,_,_,X,_,_,H,X,A,_,_,S,A,M,S,S,
  H,_,X,_,A,_,_,_,X,_,_,_,_,A,_,M,
  X,_,D,H,X,S,_,H,M,_,H,H,_,Y,H,_,
  S,_,_,_,M,M,X,_,_,_,S,_,S,_,M,X,
  _,S,H,A,_,H,_,S,_,S,_,X,X,_,_,_,
  _,A,A,A,_,X,_,A,_,M,M,_,H,_,M,H,
  A,M,_,_,D,X,X,_,H,H,M,H,_,_,H,_,
  X,H,M,H,H,X,Y,H,H,X,_,_,_,_,_,A,
  M,X,M,_,H,H,_,_,D,X,A,S,_,_,S,S,
  _,X,_,X,_,M,H,X,_,H,H,M,H,D,X,_,
  _,H,S,_,_,S,A,_,_,M,_,_,H,A,_,H,
  S,M,H,M,_,_,_,M,M,A,D,X,S,M,_,_,
  _,S,_,Y,M,_,M,D,X,D,X,_,_,H,X,X
}

local FrameTable={}
local FTI = 1

savestate.save(state1)

for i = 1, 300 do
	emu.frameadvance()
	savestate.save(state2)

	frame = emu.framecount()

	for k = 1, 8 do
		joypad.set(1, {A = 1, B = 1, select = 1, start = 1})
		emu.frameadvance()
	end

	local rng = memory.readbyte(0xC30B)
	-- Uncomment for Agl. boost +5
	--if rng == 130 or rng == 56 or rng == 221 then
	-- Uncomment for TELEPOR
	if rng == 235 then
		FrameTable[FTI]=frame
		FTI = FTI+1
	end
	savestate.load(state2)
end

savestate.load(state1)
for i = 1, #FrameTable do
	gui.text(100, 9*i - 8 ,FrameTable[i])
end
emu.pause()
The part I'm most concerned about is that loop near the bottom, the one that says "for i = 1, 300 do". In that loop, you can see that it saves and loads the game 300 times in an effort to divine when I should restart the game (the game's RNG can be manipulated by restarting at different frames, but that's not too important to know). Basically, I'm asking the emulator for accuracy that's exact to the frame while loading and saving 300 times. You can see why this might cause a desync. But I'm not absolutely certain. I thought it might have something to do with the rewind feature, but I just tried a run and it desynced with rewind disabled. I suppose it could also be my run-of-the-mill savestates, but it would be very discouraging if I couldn't even count on those. I'm getting sick of failed runs of this game, so I'm counting on some advice from more experienced players. Can I modify my program to at least cut down on desyncs? Is there some option I can toggle that will help? What exactly might be causing these desyncs? (I read the article on Desync Help, but I need some contextual help at the moment.) Basically, what would you guys do if you were in my shoes? Thanks!
Editor, Emulator Coder, Expert player (2107)
Joined: 5/22/2007
Posts: 1134
Location: Glitchvania
There appears to be an emulator bug with either emu.frameadvance() or emu.pause(), but I don't know if it's relevent as I am never into the Lua implementation of VBA. You may have better chance when someone like Gocha (who actually implemented Lua for VBA) more experienced in Lua than me shows up.
<klmz> it reminds me of that people used to keep quoting adelikat's IRC statements in the old good days <adelikat> no doubt <adelikat> klmz, they still do
Player (79)
Joined: 8/5/2007
Posts: 865
Any thoughts? Anyone? Hello? Well, I've done a little bit of debugging on my own. I wrote up the following Lua script:
local state1 = savestate.create()
local state2 = savestate.create()

savestate.save(state1)

for i = 1, 300 do
	emu.frameadvance()
	savestate.save(state2)

	for k = 1, 8 do
		joypad.set(1, {A = 1, B = 1, select = 1, start = 1})
		emu.frameadvance()
	end

	savestate.load(state2)
end

savestate.load(state1)

emu.pause()
As you can see, this script is essentially a stripped-down version of the script in my previous post. I ran two versions of this script. In the first version, I commented out the restart sequence (the "joypad.set..." line). I made a 25,000 frame movie with it and it didn't desync. In the second version, the restart sequence was included. The movie I created to test that script desynced about 7,000 frames in. So in summary, it appears that the restart sequence is causing it. I don't have a clue why, but it is. Hopefully some more experienced runners can provide their insights. My next steps will be to fiddle with parameters like the duration of each loop and what buttons are pressed (is it the restart sequence specifically, or will pressing any buttons cause a desync?). Edit: Just ran another test with the restart sequence line included. It remained synced through 17,000 frames. Grrrr...