User File #638548877920087482

Upload All User Files

#638548877920087482 - monopoly_7cpus_starting.lua

monopoly_7cpus_starting.lua
Game: Monopoly ( NES, see all files )
32 downloads
Uploaded 6/25/2024 4:49 AM by FractalFusion (see all 87)
Script for finding delays to RNG-manipulate first part of 7 CPUs TAS.
console.clear()


local baserng=0x3E2AD2
local max_rng_index=200000
local rngtable={}


local mscore=45 --maximum allowable delay
local init_index=1186+56+30
local init_index2=0+7


local delay_max={mscore}
--delay_max[0]=mscore

local delay={0}
--delay[0]=0

local rng_index={init_index}
--rng_index[0]=init_index

local rng_index2={init_index2}
--rng_index2[0]=init_index2

local number_of_results=0
local depth=1
local cur_index = 0
local cur_index2 = 0
local dice1=1
local dice2=1

local floor = math.floor

local function nextrng(rng,n)
	for i=1,n do	
		local bool1= ((rng%0x400000)>=0x200000)
		local bool2= ((rng%0x200000)>=0x100000)
		
		if bool1~=bool2 then
		  nextbit=1
		else
		  nextbit=0
		end
	
		rng=2*rng+nextbit
		rng=rng%0x1000000
	end
	return rng
end

local function xor(a,b)
	local pow=1
	local res=0
	for i=0,7 do
		local bool1 = (a%(2*pow)>=pow)
		local bool2 = (b%(2*pow)>=pow)
		if bool1~=bool2 then
			res=res+pow
		end
		
		pow=pow*2
	end
	return res
end

--create lookup table for rng values up to max_rng_index
local cur_rng=baserng
for i=1,max_rng_index do
	cur_rng=nextrng(cur_rng,1)
	rngtable[i]=cur_rng%256
end


local function advance_depth()
	delay[depth+1]=-1  --always followed by try_next() to increment this variable
	delay_max[depth+1]=delay_max[depth]-delay[depth]
	rng_index[depth+1]=cur_index
	rng_index2[depth+1]=cur_index2
	depth=depth+1
end

local function try_next()
	while true do
		delay[depth]=delay[depth]+1
		if delay[depth]<=delay_max[depth] then break end
		depth=depth-1
		if depth<=0 then break end
	end
end


local function load_indices()
	cur_index=rng_index[depth]
	cur_index2=rng_index2[depth]
end

local function player_dice1()
	cur_index = cur_index + delay[depth-2] + (6+delay[depth-1])*8 + (2+delay[depth])*16 + floor(((3+delay[depth-1])%4+(2+delay[depth]))/4)*2 + 27*6 + 5
	cur_index2 = cur_index2 + (6+delay[depth-1]) + (2+delay[depth])*3 + 27
	dice1 = xor(rngtable[cur_index],(cur_index2*35)%256) % 6 + 1	
end

local function player_dice1_after_cpu()
	cur_index = cur_index + (6+delay[depth-1])*8 + (2+delay[depth])*16 + floor(((3+delay[depth-1])%4+(2+delay[depth]))/4)*2 + 27*6 + 5
	cur_index2 = cur_index2 + (6+delay[depth-1]) + (2+delay[depth])*3 + 27
	dice1 = xor(rngtable[cur_index],(cur_index2*35)%256) % 6 + 1	
end

local function player_dice1_double()
	cur_index = cur_index + delay[depth-2] + (6+delay[depth-1])*6 + (2+delay[depth])*16 + floor(((3+delay[depth-1])%4+(2+delay[depth]))/4)*2 + 27*6 + 5
	cur_index2 = cur_index2 + (6+delay[depth-1]) + (2+delay[depth])*3 + 27
	dice1 = xor(rngtable[cur_index],(cur_index2*35)%256) % 6 + 1	
end

local function cpu_dice1_delay()
	cur_index = cur_index + delay[depth] + 40*8 + 23*16 + 6*2 + 27*6 + 5
	cur_index2 = cur_index2 + 40 + 23*3 + 27
	dice1 = xor(rngtable[cur_index],(cur_index2*35)%256) % 6 + 1	
end

local function cpu_dice1_unusual_delay()
	cur_index = cur_index + delay[depth-1] + 8*delay[depth] + 40*8 + 23*16 + 6*2 + 27*6 + 5
	cur_index2 = cur_index2 + delay[depth] + 40 + 23*3 + 27
	dice1 = xor(rngtable[cur_index],(cur_index2*35)%256) % 6 + 1	
end

local function cpu_dice1_double_delay()
	cur_index = cur_index + delay[depth] + 40*6 + 23*16 + 6*2 + 27*6 + 5
	cur_index2 = cur_index2 + 40 + 23*3 + 27
	dice1 = xor(rngtable[cur_index],(cur_index2*35)%256) % 6 + 1	
end

local function cpu_dice1_nodelay()
	cur_index = cur_index + 40*8 + 23*16 + 6*2 + 27*6 + 5
	cur_index2 = cur_index2 + 40 + 23*3 + 27
	dice1 = xor(rngtable[cur_index],(cur_index2*35)%256) % 6 + 1	
end

local function cpu_dice1_double_nodelay()
	cur_index = cur_index + 40*6 + 23*16 + 6*2 + 27*6 + 5
	cur_index2 = cur_index2 + 40 + 23*3 + 27
	dice1 = xor(rngtable[cur_index],(cur_index2*35)%256) % 6 + 1	
end

local function get_dice2()
	cur_index = cur_index + 5
	dice2 = (rngtable[cur_index]) % 6 + 1
end

local function complete_turn()
	cur_index = cur_index + (31+(dice1+dice2)*6)*6
	cur_index2 = cur_index2 + 31+(dice1+dice2)*6	
end


local function add_result()
	
	local total_delay=0
	for j=1,depth do
		total_delay=total_delay+delay[j]
	end
	
	local fle=io.open("monopoly_result.txt","a")
	fle:write(string.format("Delay %d: ",total_delay))
		for j=1,depth do
			fle:write(string.format("%d ",delay[j]))
		end
		fle:write("\n")
	fle:close()
	
	number_of_results=number_of_results+1
end

local fle=io.open("monopoly_result.txt","a")
fle:write("----\n")
fle:close()






--cur_index=init_index
--cur_index2=init_index2

local adice1
local adice2
local bdice1
local bdice2

while true do

	load_indices()
	


	if depth==1 then
		advance_depth()
	elseif depth==2 then   --cpu 9
		cpu_dice1_unusual_delay()
		get_dice2()
		if dice1+dice2==9 then
			complete_turn()
			cur_index = cur_index + 120 + 54 + 325
			cur_index2 = cur_index2 + 7
			advance_depth()
		end
	elseif depth==3 then
		advance_depth()
	elseif depth==4 then
		advance_depth()
	elseif depth==5 then   --need 3+3
		player_dice1()
		if dice1==3 then
			get_dice2()
			if dice2==3 then
				complete_turn()
				cur_index = cur_index + 83
				advance_depth()
			end
		end
	elseif depth==6 then
		advance_depth()
	elseif depth==7 then
		advance_depth()
	elseif depth==8 then   --need 1+1
		player_dice1_double()
		if dice1==1 then
			get_dice2()
			if dice2==1 then
				complete_turn()
				cur_index = cur_index + 81
				advance_depth()
			end
		end
	elseif depth==9 then
		advance_depth()
	elseif depth==10 then
		advance_depth()
	elseif depth==11 then   --need nd8
		player_dice1_double()
		if dice1==2 or dice1==3 or dice1==5 or dice1==6 then
			get_dice2()
			if dice1+dice2==8 then
				complete_turn()
				cur_index = cur_index + 79 + 56 + 699
				cur_index2 = cur_index2 + 7
				advance_depth()
			end
		end
	elseif depth==12 then
		advance_depth()		
	elseif depth==13 then  -- (9 9 8 9) 9 9 3
		cpu_dice1_unusual_delay()		
		get_dice2()
		if dice1+dice2==9 then
			complete_turn()
			cur_index = cur_index + 120
			
			cpu_dice1_nodelay()
			get_dice2()
			if dice1+dice2==9 then
				complete_turn()
				cur_index = cur_index + 120
				
				cpu_dice1_nodelay()
				get_dice2()
				if dice1+dice2==8 and dice1~=4 then
					complete_turn()
					cur_index = cur_index + 120
					
					cpu_dice1_nodelay()
					get_dice2()
					if dice1+dice2==9 then	
							complete_turn()
							cur_index = cur_index + 120 + 56 + 12
							cur_index2 = cur_index2 + 7
							advance_depth()									
					end				
				
				end
			end
		end
	elseif depth==14 then 	-- 9 9 8 9 (9 9) 3	
		advance_depth()
	elseif depth==15 then
		cpu_dice1_unusual_delay()			
		get_dice2()
		if dice1+dice2==9 then
			complete_turn()
			cur_index = cur_index + 120
			
			cpu_dice1_nodelay()
			get_dice2()
			if dice1+dice2==9 then	
					complete_turn()
					cur_index = cur_index + 120 + 56 + 12
					cur_index2 = cur_index2 + 7
					advance_depth()	
			end
		end
		
		
		
		
	elseif depth==16 then 	-- 9 9 8 9 9 9 (3)	
		advance_depth()
	elseif depth==17 then
		cpu_dice1_unusual_delay()			
		get_dice2()
		if dice1+dice2==5 then	
				complete_turn()		
				cur_index = cur_index + 112
				advance_depth()
		end
	elseif depth==18 then
		advance_depth()
	elseif depth==19 then -- need 1+1
		player_dice1_after_cpu()
		if dice1==1 then
			get_dice2()
			if dice2==1 then
				complete_turn()
				cur_index = cur_index + 73
				advance_depth()
			end
		end		

	elseif depth==20 then
		advance_depth()		
	elseif depth==21 then
		advance_depth()
	elseif depth==22 then -- need nd4
		player_dice1_double()
		if dice1==1 or dice1==3 then
			get_dice2()
			if dice1+dice2==4 then
				add_result()
			end
		end				
	end
	
	try_next()
	if depth<=0 then break end
	
end


console.write("Done. # results: "..tostring(number_of_results))