User File #638548878630620764

Upload All User Files

#638548878630620764 - monopoly_7cpus_ending.lua

monopoly_7cpus_ending.lua
Game: Monopoly ( NES, see all files )
5 downloads
Uploaded 3 days ago by FractalFusion (see all 83)
Script for finding possible RNGa and RNGb values for an ending to the 7 CPUs TAS.
console.clear()


local baserng=0x3E2AD2
local start_rng_index=17000 --start range for initial rng
local end_rng_index=25000 --end range for initial rng
local max_rng_index=200000
local rngtable={}


local mscore=45 --maximum allowable delay
local init_index=984
--local init_index=1241
local init_index2=0


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()


local sum=1
local rec1,rec2,rec3,rec4,rec5,rec6,rec7

	for init_index=start_rng_index, end_rng_index do
		for init_index2=0,255 do
			cur_index=init_index
			cur_index2=init_index2
			
			cpu_dice1_nodelay()
			get_dice2()
			sum=dice1+dice2
			if sum==7 or sum==9 or sum==10 or sum==11 then  --cpu1
				rec1=sum
				complete_turn()
				cur_index = cur_index + 439 --bankrupt1
				
				cpu_dice1_nodelay()
				get_dice2()
				sum=dice1+dice2				
				if sum==7 or sum==9 or sum==10 or sum==11 then --cpu2
					rec2=sum
					complete_turn()
					cur_index = cur_index + 439 --bankrupt2
					
					cpu_dice1_nodelay()
					get_dice2()
					sum=dice1+dice2			
					if sum==7 or sum==9 or sum==10 or sum==11 then --cpu3
						rec3=sum
						complete_turn()
						cur_index = cur_index + 439 --bankrupt3
						
						cpu_dice1_nodelay()
						get_dice2()
						sum=dice1+dice2			
						if sum==7 or sum==9 or sum==10 or sum==11 then --cpu4
							rec4=sum
							complete_turn()
							cur_index = cur_index + 439 --bankrupt4
							
							cpu_dice1_nodelay()
							get_dice2()
							sum=dice1+dice2			
							if sum==7 or sum==9 or sum==10 or sum==11 then --cpu5
								rec5=sum
								complete_turn()
								cur_index = cur_index + 439 --bankrupt5
								
								cpu_dice1_nodelay()
								get_dice2()
								sum=dice1+dice2			
								if sum==7 or sum==9 or sum==10 or sum==11 then --cpu6
									rec6=sum
									complete_turn()
									cur_index = cur_index + 439 --bankrupt6
									
									cpu_dice1_nodelay()
									get_dice2()
									sum=dice1+dice2
									if dice1==dice2 and (sum==2 or sum==4) then --cpu7_roll_1
										rec7=sum
										complete_turn()
										cur_index = cur_index + 120
										
										cpu_dice1_double_nodelay()
										get_dice2()
										sum=dice1+dice2
										if sum==2 or sum==3 then --cpu7_roll_2
											--success
											
											local fle=io.open("monopoly_result.txt","a")
											fle:write(string.format("RNGa: %d, RNGb: %d, sum1: %d, sum2: %d, sum3: %d, sum4: %d, sum5: %d, sum6: %d, sum7: %d\n",init_index, init_index2, rec1, rec2, rec3, rec4, rec5, rec6, rec7))

											fle:close()									
										
											number_of_results=number_of_results+1
										end --7_2
							
									end --7_1
								end --6									

							end --5								
					
						end --4							
				
					end --3						
			
				end --2			
			
			end --1
		end
	end


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