User File #33506096746575394

Upload All User Files

#33506096746575394 - RNG script for TearRing Saga

rng_script_v3.lua
851 downloads
Uploaded 9/8/2016 10:31 PM by lapogne36 (see all 25)
For those who would like to understand how I determine the RNs based on the RAM values
--------------------------------Parameters to fix----------------------------------
--The game make an unknow update on the ram after map 9 (first split)

nb=624*30  -- Number of RN values to predict in addition to the 624 RN currently on the RAM (minimum 1)
-- around 21 boucles

--op = {101,9,66,101,9} 
op = {101,9,67,5,101,9}
--op2 = {"<","<",">","<","<"} 
op2 = {"<","<","<",">","<","<"}

--------------------------------------------------------------------------------------------------------------------------------------------------------

client.setwindowsize(2)

rngvalues={
{1;8;10;12;13;14;15;16};
{17;18;19;20;21;21;22;23};
{24;24;25;25;26;27;28;29};
{30;30;31;32;33;34;35;35};
{36;37;38;39;40;40;41;42};
{43;44;45;45;46;47;48;49};
{50;50;51;52;53;54;55;55};
{56;57;58;59;60;60;61;62};
{63;64;65;65;66;67;68;69};
{70;70;71;72;73;74;75;75};
{76;77;77;78;79;80;80;81};
{82;83;84;85;86;87;88;89};
{91;93;95;98;255;255;255;255};
{255;255;255;255;255;255;255;255};
{255;255;255;255;255;255;255;255};
{255;255;255;255;255;255;255;255}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------

while true do



byte={}
for i=1, 2496 do
byte[i]=memory.readbyte(0x000E9847+i)
end

for i=624, 623+nb do -- Start of Loop 1 to predict the bytes
j=i*4
	byte[j+1]=0
	byte[j+2]=0
	byte[j+3]=0
	byte[j+4]=0

k=(i-623)*4
l=(i-227)*4
dw2 = {byte[k+1],byte[k+2],byte[k+3],byte[k+4]}
dw398 = {byte[l+1],byte[l+2],byte[l+3],byte[l+4]}

-----------------------------------Byte 1-----------------------------------

c1 = math.floor(byte[k+1]/2)
c2 = byte[l+1]
coef = 1
calc = c1 + c2

if byte[k+1] % 2 == 1 then
	coef = -1
	calc = -calc
	byte[j+1] = byte[j+1] - 33
	if byte[l+1] % 64 > 31 then byte[j+1] = byte[j+1] + 64 end
	if byte[k+1] % 128 > 63 then calc = calc + 64 end
end

	if c1 % 2 == 1 and c2 % 2 == 1 then calc = calc - 2*coef end
	if c1 % 4 > 1 and c2 % 4 > 1 then calc = calc - 4*coef end
	if c1 % 8 > 3 and c2 % 8 > 3 then calc = calc - 8*coef end
	if c1 % 16 > 7 and c2 % 16 > 7 then calc = calc - 16*coef end
	if c1 % 32 > 15 and c2 % 32 > 15 then calc = calc - 32*coef end
	if c1 % 64 > 31 and c2 % 64 > 31 then calc = calc - 64 end
	if c1 % 128 > 63 and c2 % 128 > 63 then calc = calc - 128*coef end

byte[j+1] = byte[j+1] + calc

-----------------------------------Byte 2-----------------------------------

c1 = math.floor(byte[k+2]/2)
c2 = byte[l+2]
calc = c1 + c2

if byte[k+1] % 2 == 1 then
	calc = c1 + c2
	byte[j+2] = byte[j+2] - 80
	if byte[l+2] % 32 > 15 then byte[j+2] = byte[j+2] - 32 end
	if byte[l+2] % 64 > 31 then byte[j+2] = byte[j+2] - 64 end
	if byte[k+2] % 64 > 31 then calc = calc - 32 end
	if byte[k+2] % 128 > 63 then calc = calc - 64 end

	if c1 % 2 == 1 and c2 % 2 == 1 then calc = calc - 2 end
	if c1 % 4 > 1 and c2 % 4 > 1 then calc = calc - 4 end
	if c1 % 8 > 3 and c2 % 8 > 3 then calc = calc - 8 end
	if c1 % 16 > 7 and c2 % 16 > 7 then calc = calc - 16 end
	if c1 % 32 > 15 and c2 % 32 > 15 then calc = calc + 32 end
	if c1 % 64 > 31 and c2 % 64 > 31 then calc = calc + 64 end
	if c1 % 128 > 63 and c2 % 128 > 63 then calc = calc - 128 end
end

if byte[k+1] % 2 == 0 then
	if c1 % 2 == 1 and c2 % 2 == 1 then calc = calc - 2 end
	if c1 % 4 > 1 and c2 % 4 > 1 then calc = calc - 4 end
	if c1 % 8 > 3 and c2 % 8 > 3 then calc = calc - 8 end
	if c1 % 16 > 7 and c2 % 16 > 7 then calc = calc - 16 end
	if c1 % 32 > 15 and c2 % 32 > 15 then calc = calc - 32 end
	if c1 % 64 > 31 and c2 % 64 > 31 then calc = calc - 64 end
	if c1 % 128 > 63 and c2 % 128 > 63 then calc = calc - 128 end
end

byte[j+2] = byte[j+2] + calc

-----------------------------------Byte 3-----------------------------------

c1 = math.floor(byte[k+3]/2)
c2 = byte[l+3]
calc = c1 + c2

if byte[k+1] % 2 == 1 then
	calc = c1 + c2
	byte[j+3] = byte[j+3] + 8
	if byte[l+3] % 16 > 7 then byte[j+3] = byte[j+3] - 16 end
	if byte[k+3] % 32 > 15 then calc = calc - 16 end

	if c1 % 2 == 1 and c2 % 2 == 1 then calc = calc - 2 end
	if c1 % 4 > 1 and c2 % 4 > 1 then calc = calc - 4 end
	if c1 % 8 > 3 and c2 % 8 > 3 then calc = calc - 8 end
	if c1 % 16 > 7 and c2 % 16 > 7 then calc = calc + 16 end
	if c1 % 32 > 15 and c2 % 32 > 15 then calc = calc - 32 end
	if c1 % 64 > 31 and c2 % 64 > 31 then calc = calc - 64 end
	if c1 % 128 > 63 and c2 % 128 > 63 then calc = calc - 128 end
end

if byte[k+1] % 2 == 0 then
	if c1 % 2 == 1 and c2 % 2 == 1 then calc = calc - 2 end
	if c1 % 4 > 1 and c2 % 4 > 1 then calc = calc - 4 end
	if c1 % 8 > 3 and c2 % 8 > 3 then calc = calc - 8 end
	if c1 % 16 > 7 and c2 % 16 > 7 then calc = calc - 16 end
	if c1 % 32 > 15 and c2 % 32 > 15 then calc = calc - 32 end
	if c1 % 64 > 31 and c2 % 64 > 31 then calc = calc - 64 end
	if c1 % 128 > 63 and c2 % 128 > 63 then calc = calc - 128 end
end

byte[j+3] = byte[j+3] + calc

-----------------------------------Byte 4-----------------------------------

c1 = math.floor(byte[k+4]/2)
c2 = byte[l+4]
calc = c1 + c2

if byte[k+1] % 2 == 1 then
	calc = c1 + c2
	byte[j+4] = byte[j+4] - 103
	if byte[l+4] % 2 == 1 then byte[j+4] = byte[j+4] - 2 end
	if byte[l+4] % 16 > 7 then byte[j+4] = byte[j+4] - 16 end
	if byte[l+4] % 32 > 15 then byte[j+4] = byte[j+4] - 32 end
	if byte[k+4] % 4 > 1 then calc = calc - 2 end
	if byte[k+4] % 32 > 15 then calc = calc - 16 end
	if byte[k+4] % 64 > 31 then calc = calc - 32 end
	if byte[k+4] > 127 then calc = calc - 64 end

	if c1 % 2 == 1 and c2 % 2 == 1 then calc = calc + 2 end
	if c1 % 4 > 1 and c2 % 4 > 1 then calc = calc - 4 end
	if c1 % 8 > 3 and c2 % 8 > 3 then calc = calc - 8 end
	if c1 % 16 > 7 and c2 % 16 > 7 then calc = calc + 16 end
	if c1 % 32 > 15 and c2 % 32 > 15 then calc = calc + 32 end
	if c1 % 64 > 31 and c2 % 64 > 31 then calc = calc - 64 end
end

if byte[k+1] % 2 == 0 then
	if c1 % 2 == 1 and c2 % 2 == 1 then calc = calc - 2 end
	if c1 % 4 > 1 and c2 % 4 > 1 then calc = calc - 4 end
	if c1 % 8 > 3 and c2 % 8 > 3 then calc = calc - 8 end
	if c1 % 16 > 7 and c2 % 16 > 7 then calc = calc - 16 end
	if c1 % 32 > 15 and c2 % 32 > 15 then calc = calc - 32 end
	if c1 % 64 > 31 and c2 % 64 > 31 then calc = calc - 64 end
	if byte[k+4] > 127 then calc = calc - 64 end
end

byte[j+4] = byte[j+4] + calc

----------------------------Extra calculations-----------------------------------

if byte[k+2] % 2 == 1 then byte[j+1] = byte[j+1] + 128 end

if byte[k+3] % 2 == 1 then byte[j+2] = byte[j+2] + 128 end

if byte[k+4] % 2 == 1 then byte[j+3] = byte[j+3] + 128 end

if byte[k] > 127 then 
	byte[j+4] = byte[j+4] + 64
	if byte[l+4] % 128 > 63 then byte[j+4] = byte[j+4] - 128 end
end

byte[j+1] = byte[j+1] % 256
byte[j+2] = byte[j+2] % 256
byte[j+3] = byte[j+3] % 256
byte[j+4] = byte[j+4] % 256

end -- End of Loop 1

--------------------------------------------------------------------------------------------------------------------------------------------------------



predict={}


for i=0, 623+nb do -- Start of Loop 2 to predict the RN values

byte1 = ((byte[i*4+1] + 128 ) % 256) - 128
byte2 = ((byte[i*4+2] + 128 ) % 256) - 128
byte3 = ((byte[i*4+3] + 128 ) % 256) - 128
byte4 = ((byte[i*4+4] + 128 ) % 256) - 128

line=1
col=1
pol=0

----------------------------byte1 column----------------------------

if byte1 % 64 > 31 then col=col+4 end
if byte1 % 16 > 7 then pol=pol+1 end

----------------------------byte1 line----------------------------

if byte1 % 256 > 127 then line=line+2 end

----------------------------byte2 column----------------------------

C=2*math.floor((byte2 % 32)/8)
D=1
col=col+D*C

if byte2 % 8 > 3 then pol=pol+1 end
if byte2 % 128 > 63 then pol=pol+1 end

----------------------------byte2 line----------------------------

line=((line-1) % 16) + 1
F=line
G=2*math.floor(byte2/64)

if byte1 % 256 > 127 then G=-G end

line=line+G

if (line-1) % 16 > 7 then
	if (F-1) % 16 < 8 then line=line+8 end
else	if (F-1) % 16 > 7 then line=line+8 end
end

----------------------------byte3----------------------------

if byte3 % 2 == 1 then
	line=line+8
	col=col+4
end

if byte3 % 8 > 3 then
	F=2
	if byte2 % 128 > 63 then F=-F end
	if byte1 % 256 > 127 then F=-F end
	line=line+F				
end

if byte3 % 64 > 31 then
	if line % 2 == 1 then 
		line=line+1
		else line=line-1
	end
	pol=pol+1
end

A=2*math.floor((byte3 % 32)/8)

if byte2 % 16 > 7 then A=-A end
if byte3 % 128 > 63 then A=-A end

col=col+A
Q=2*math.floor(byte3/64)

if byte2 % 16 > 7 then Q=-Q end

col=col+Q

----------------------------byte4 column----------------------------

M=2*math.floor(byte4/64)
A=2*math.floor(byte4/4) 

if byte4 % 128 > 63 then A=-A end

if byte2 % 16 > 7 then
	A=-A
	M=-M
end

if byte3 % 16 > 7 then
	A=-A
	M=-M
end

if byte3 % 128 > 63 then
	A=-A
	M=-M
end

col=col+A+M		

if byte4 % 4 > 1 then pol=pol+1 end

----------------------------byte4 column----------------------------

if byte4 % 2 == 1 then line=line+8 end

J=2*math.floor(byte4/32)

if byte3 % 8 > 3 then J=-J end
if byte2 % 128 > 63 then J=-J end
if byte1 % 256 > 127 then J=-J end

if byte4 % 32 > 15 then
	if line % 2 == 1 then line=line+1
	else line=line-1
	end
end

line=line+J
K=2*math.floor(byte4/2)

if byte3 % 8 > 3 then K=-K end
if byte2 % 128 > 63 then K=-K end
if byte1 % 256 > 127 then K=-K end
if byte4 % 64 > 31 then K=-K end

line=line+K
L=1

if byte4 % 64 > 31 then L=-L end
if byte4 % 128 > 63 then L=-L end
if byte3 % 8 > 3 then L=-L end
if byte2 % 256 > 127 then L=-L end
if byte2 % 128 > 63 then L=-L end
if byte1 % 256 > 127 then L=-L end
if (L==-1) and byte4 % 8 > 3 then line=line+8 end

N=1

if byte1 % 256 > 127 then N=-N end
if byte2 % 256 > 127 then N=-N end
if byte2 % 128 > 63 then N=-N end
if byte3 % 8 > 3 then N=-N end
if N==-1 and (byte4 % 128 > 63) then line=line+8 end

-------------------------------------------------------------

pol=pol % 2
if pol == 1 then col=col+1 end
line=((line-1) % 16)+1
col=((col-1) % 8)+1

predict[i+1]=rngvalues[line][col]

end -- End of Loop 2



local file = io.open("rng2.txt", "w")
--for i=2205, 2215 do
for i=279, 370 do
--	if predict[i] < 9 then
--		file:write(i," ",predict[i],"\n")
--	end
end
file:close()



--for i=1, table.getn(byte) do
--	file:write(byte[i*624+4]," ",byte[i*624+3]," ",byte[i*624+2]," ",byte[i*624+1], "\n")
--	file:write(byte[i],"\n")
--end




rng=memory.read_u16_le(0x000CC270)
rnindex = rng-3
loop = 1



dummy = 0
start = 00000
i = rnindex + 3 + start
--i = start
hit = {0,0,0}

while dummy < table.getn(hit) and i < math.min(rng + 100001, table.getn(predict)-1-table.getn(op)) do
	ibis = i
	a=0
	cnt=0
	if predict[ibis-1] ~= 255 then
		while cnt < table.getn(op) and ibis < table.getn(predict) do
			if predict[ibis] ~= 255 then
				if op2[cnt+1] == "<" then
					if predict[ibis] < op[cnt+1] then
						a = a + 1
					end
				elseif op2[cnt+1] == ">" then
					if predict[ibis] > op[cnt+1] then
						a = a + 1
					end
				end
				cnt = cnt + 1
			end
			ibis = ibis + 1
		end
	end
	if a == table.getn(op) then
		dummy = dummy + 1
		hit[dummy] = i
	end
	i = i + 1
end


--if dummy > 0 then
--	hit = i
--end







while true do

ref=rng

res={}
for i=1, 25 do
res[i]=0
end
beg=rnindex+4
beg2=beg
while predict[beg2]==255 do
beg2=beg2+1
end
res[9]=predict[beg2]

for i=-8, -1 do
beg=beg-1
out=0
	while out==0 do
		if beg<=0 then break end
		while predict[beg]==255 do
			beg=beg-1
		end
		out=1
	end
	if beg>0 then res[-i]=predict[beg] end
end

for i=10, 31 do
	beg2=beg2+1
	while predict[beg2]==255 do
		beg2=beg2+1
	end
	res[i]=predict[beg2]
end

while rng==ref do
	if res[1]>0 then gui.drawText(775,06,res[1]) end
	if res[2]>0 then gui.drawText(775,21,res[2]) end
	if res[3]>0 then gui.drawText(775,36,res[3]) end
	if res[4]>0 then gui.drawText(775,51,res[4]) end
	if res[5]>0 then gui.drawText(775,66,res[5]) end
	if res[6]>0 then gui.drawText(775,81,res[6]) end
	if res[7]>0 then gui.drawText(775,96,res[7]) end
	if res[8]>0 then gui.drawText(775,111,res[8]) end
	gui.drawText(775,126,res[9])
	gui.drawText(775,141,res[10])
	gui.drawText(775,156,res[11])
	gui.drawText(775,171,res[12])
	gui.drawText(775,186,res[13])
	gui.drawText(775,201,res[14])
	gui.drawText(775,216,res[15])
	gui.drawText(775,231,res[16])
	gui.drawText(775,246,res[17])
	gui.drawText(775,261,res[18])
	gui.drawText(775,276,res[19])
	gui.drawText(775,291,res[20])
	gui.drawText(775,306,res[21])
	gui.drawText(775,321,res[22])
	gui.drawText(775,336,res[23])
	gui.drawText(775,351,res[24])
	gui.drawText(775,366,res[25])
	gui.drawText(775,381,res[26])
	gui.drawText(775,396,res[27])
	gui.drawText(775,414,res[28])
	gui.drawText(775,429,res[29])
	gui.drawText(775,444,res[30])
	gui.drawText(775,459,res[31])
	gui.drawText(760,126,"->")
	gui.drawText(725,88,rng,"blue")
	gui.drawText(725,103,loop,"blue")
	gui.drawText(725,118,"Next")
	gui.drawText(730,133,"RN")
	f1 = emu.framecount()
	for i=1, table.getn(hit), 1 do
		if 0<=(hit[i]-4-rnindex) then 
			gui.drawText(725,141+15*i,hit[i]-4-rnindex,"red")
			if (hit[i]-4-rnindex)<=31 then
				gui.drawText(760,126+15*(hit[i]-4-rnindex),"->","red") 
			end
		end
	end
	gui.drawText(725,318,client.borderheight())
	gui.drawText(725,333,client.borderwidth())
	gui.drawText(725,348,client.bufferheight())
	gui.drawText(725,363,client.bufferwidth())
	gui.drawText(725,378,client.screenheight())
	gui.drawText(725,393,client.screenwidth())

	emu.frameadvance();
	rng=memory.read_u16_le(0x000CC270)
end

f2 = emu.framecount()
if f2 ~= f1 + 1 then
	dummy = 0
	i = 0
	dw = memory.read_u32_le(0x000E9848)
	while dummy == 0 and i < math.floor(nb/624+1) do
		if 256*256*256*byte[624*i*4+4] + 256*256*byte[624*i*4+3] + 256*byte[624*i*4+2] + byte[624*i*4+1] == dw then
			dummy = 1
		end
		i = i + 1
	end
	if dummy == 1 then
		loop = i
		rnindex = (loop-1)*624 + rng - 3
		else break
	end
	else
	if rng == 624 then
		if byte[1]==0 and byte[2]==0 and byte[3]==0 and byte[4]==0 then
			break
		end
	end
	if rng < ref then 
		loop = loop + 1 
	end
	rnindex = rnindex + ((rng - ref) % 624)
end

if (f2 ~= f1 + 1) or (hit[1]-4-rnindex) < 0 then --Calculate the new 3 closest RN
dummy = 0
--i = start
i = rnindex + 3
hit = {0,0,0}

while dummy < table.getn(hit) and i < math.min(rng + 100001, table.getn(predict)-1-table.getn(op)) do
	ibis = i
	a=0
	cnt=0
	if predict[ibis-1] ~= 255 then
		while cnt < table.getn(op) and ibis < table.getn(predict) do
			if predict[ibis] ~= 255 then
				if op2[cnt+1] == "<" then
					if predict[ibis] < op[cnt+1] then
						a = a + 1
					end
				elseif op2[cnt+1] == ">" then
					if predict[ibis] > op[cnt+1] then
						a = a + 1
					end
				end
				cnt = cnt + 1
			end
			ibis = ibis + 1
		end
	end
	if a == table.getn(op) then
		dummy = dummy + 1
		hit[dummy] = i
	end
	i = i + 1
end
end

end

end --Restart the whole script