Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RNG1 = memory.readbyte(0x0017)
- RNG2 = memory.readbyte(0x0018)
- RNG = memory.read_u16_be(0x0017)
- SPAWNID = memory.readbyte(0x0019)
- SPAWNCOUNT = memory.readbyte(0x001A)
- --the in-game connection between the number at SPAWNID and the shape it gives
- SPAWNTABLE = { [0] = 0x02, 0x07, 0x08, 0x0A, 0x0B, 0x0E, 0x12 }
- --1-7 table of possible pieces
- letters = { "T", "J", "Z", "O", "S", "L", "I" }
- --convert spawn table number to string
- names = { [0x02] = "T", [0x07] = "J", [0x08] = "Z", [0x0A] = "O", [0x0B] = "S", [0x0E] = "L", [0x12] = "I" }
- --convert string to spawn table number
- ids = { T = 0x02, J = 0x07, Z = 0x08, O = 0x0A, S = 0x0B, L = 0x0E, I = 0x12 }
- --constants for writing sequences in easily
- T = "T"; J = "J"; Z = "Z"; O = "O"; S = "S"; L = "L"; I = "I";
- L0 = {}; L1 = {}; R0 = {}; R1 = {}; E = {};
- --hole location
- for i=1,10 do
- L0[i] = {}; L1[i] = {}; R0[i] = {}; R1[i] = {}; E[i] = {};
- end
- n = 6
- L0[n][1] = { {I, L, L, O, O}, { 27, 27, 24, 24, 21 } }
- E[n][1] = { J }
- R1[n][1] = { { L, Z, Z, T }, { 28, 24, 21, 25 } }
- --code for printing the table to file from http://lua-users.org/wiki/TableUtils
- --plus table copy code. just minimize these functions
- function table.val_to_str ( v )
- if "string" == type( v ) then
- v = string.gsub( v, "\n", "\\n" )
- if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
- return "'" .. v .. "'"
- end
- return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
- else
- return "table" == type( v ) and table.tostring( v ) or
- tostring( v )
- end
- end
- function table.key_to_str ( k )
- if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
- return k
- else
- return "[" .. table.val_to_str( k ) .. "]"
- end
- end
- function table.tostring( tbl )
- local result, done = {}, {}
- for k, v in ipairs( tbl ) do
- table.insert( result, table.val_to_str( v ) )
- done[ k ] = true
- end
- for k, v in pairs( tbl ) do
- if not done[ k ] then
- table.insert( result,
- table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
- end
- end
- return "{" .. table.concat( result, "," ) .. "}"
- end
- function exportToFile(m, filename)
- file = io.open(filename,"a")
- file:write(table.tostring(m) .. "\n")
- file:flush()
- file:close()
- end
- function shallowcopy(orig)
- local orig_type = type(orig)
- local copy
- if orig_type == 'table' then
- copy = {}
- for orig_key, orig_value in pairs(orig) do
- copy[orig_key] = orig_value
- end
- else -- number, string, boolean, etc
- copy = orig
- end
- return copy
- end
- function beginSearch(initDelay, column)
- --check every combination of potential left and right side builds
- for i = 1, #R1[column] do
- for j = 1, #L0[column] do
- --check every order of getting the left pieces and the right pieces
- --endings are currently tied to the Left fills
- combi(1,1,L0[column][j],R1[column][i],E[column][j])
- end
- end
- end
- function combi(Lspot, Rspot, Larray, Rarray, Earray, Pseq, Fseq)
- Pseq = Pseq or {}
- Fseq = Fseq or {}
- --print(#Larray[1])
- if (Lspot > #Larray[1] and Rspot > #Rarray[1]) then
- Pseq[#Pseq+1] = E[6][1][1]
- Fseq[#Fseq+1] = 0
- findSequence(Pseq,Fseq)
- return
- end
- tempP = shallowcopy(Pseq)
- tempF = shallowcopy(Fseq)
- if (Lspot <= #Larray[1]) then
- tempP[#tempP+1] = Larray[1][Lspot];
- tempF[#tempF+1] = Larray[2][Lspot];
- combi(Lspot+1,Rspot,Larray,Rarray,Earray,tempP,tempF)
- end
- tempP = shallowcopy(Pseq)
- tempF = shallowcopy(Fseq)
- if (Rspot <= #Rarray[1]) then
- tempP[#tempP+1] = Rarray[1][Rspot];
- tempF[#tempF+1] = Rarray[2][Rspot];
- combi(Lspot,Rspot+1,Larray,Rarray,Earray,tempP,tempF)
- end
- end
- fastestTotal = 9999
- fastestSeq = {}
- fastestWaits = {}
- initDelay = 4
- secondDelay = 42
- function findSequence(Pseq, Fseq)
- tempRNG = RNG
- tempID = SPAWNID
- tempCount = SPAWNCOUNT
- totalLoss = 0
- waitList = {}
- Fseq[0] = secondDelay
- for i = 1, initDelay do
- tempRNG = advanceRNG(tempRNG)
- end
- file = io.open("results.txt","a")
- --file:write(tempRNG .. " " .. names[pickPiece(tempRNG, tempID, tempCount)] .. "\n")
- file:flush()
- file:close()
- for i = 1, #Pseq do
- framesLost = 0
- nextP = pickPiece(tempRNG, tempID, tempCount)
- while names[nextP] ~= Pseq[i] do
- framesLost = framesLost+1
- if (framesLost > 15) then return end
- tempRNG = advanceRNG(tempRNG)
- nextP = pickPiece(tempRNG, tempID, tempCount)
- end
- waitList[#waitList+1] = framesLost
- totalLoss = totalLoss + framesLost
- if (doubledip == true) then tempRNG = advanceRNG(tempRNG) end
- for i = 1, Fseq[i-1] do
- tempRNG = advanceRNG(tempRNG)
- end
- tempID = ids[Pseq[i]]
- tempCount = tempCount + 1
- end
- file = io.open("results.txt","a")
- file:write(table.tostring(Pseq) .. ": " .. totalLoss .. " (" .. table.tostring(waitList) .. ")\n")
- file:flush()
- file:close()
- end
- function findNext(initRNG, targetPiece)
- local frameCount = 0
- while true do
- if (names[pickPiece(initRNG)] == targetPiece) then return frameCount end
- frameCount = frameCount + 1
- initRNG = advanceRNG(initRNG)
- end
- end
- function advanceRNG(aRNG)
- local temp = 0
- if (bit.check(aRNG,1) ~= bit.check(aRNG,9)) then temp = 1 end
- if (temp == 1) then aRNG = bit.set(aRNG,16) end
- aRNG = bit.rshift(aRNG, 1)
- return aRNG
- end
- doubledip = false
- function pickPiece(theRNG, theID, theCount)
- theRNG = advanceRNG(theRNG)
- theCount = theCount + 1
- local index = bit.rshift(theRNG,8) + theCount
- index = index % 8
- if (index ~= 7 and SPAWNTABLE[index] ~= theID) then
- doubledip = false
- theID = SPAWNTABLE[index]
- else
- doubledip = true
- theRNG = advanceRNG(theRNG)
- index = bit.rshift(theRNG,8)
- index = ((index % 8) + theID) % 7
- theID = SPAWNTABLE[index]
- end
- return theID
- end
- beginSearch(0,6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement