Advertisement
Blazephlozard

tetris brute force wip

Jul 10th, 2020
1,488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.77 KB | None | 0 0
  1. RNG1 = memory.readbyte(0x0017)
  2. RNG2 = memory.readbyte(0x0018)
  3.  
  4. RNG = memory.read_u16_be(0x0017)
  5. SPAWNID = memory.readbyte(0x0019)
  6. SPAWNCOUNT = memory.readbyte(0x001A)
  7. --the in-game connection between the number at SPAWNID and the shape it gives
  8. SPAWNTABLE = { [0] = 0x02, 0x07, 0x08, 0x0A, 0x0B, 0x0E, 0x12 }
  9. --1-7 table of possible pieces
  10. letters = { "T", "J", "Z", "O", "S", "L", "I" }
  11. --convert spawn table number to string
  12. names = { [0x02] = "T", [0x07] = "J", [0x08] = "Z", [0x0A] = "O", [0x0B] = "S", [0x0E] = "L", [0x12] = "I" }
  13. --convert string to spawn table number
  14. ids = { T = 0x02, J = 0x07, Z = 0x08, O = 0x0A, S = 0x0B, L = 0x0E, I = 0x12 }
  15. --constants for writing sequences in easily
  16. T = "T"; J = "J"; Z = "Z"; O = "O"; S = "S"; L = "L"; I = "I";
  17.  
  18. L0 = {}; L1 = {}; R0 = {}; R1 = {}; E = {};
  19.  
  20.  
  21. --hole location
  22. for i=1,10 do
  23.     L0[i] = {}; L1[i] = {}; R0[i] = {}; R1[i] = {}; E[i] = {};
  24. end
  25.  
  26. n = 6
  27.  
  28. L0[n][1] = { {I, L, L, O, O}, { 27, 27, 24, 24, 21 } }
  29. E[n][1] = { J }
  30. R1[n][1] = { { L, Z, Z, T }, { 28, 24, 21, 25 } }
  31.  
  32.  
  33. --code for printing the table to file from http://lua-users.org/wiki/TableUtils
  34. --plus table copy code. just minimize these functions
  35. function table.val_to_str ( v )
  36.   if "string" == type( v ) then
  37.     v = string.gsub( v, "\n", "\\n" )
  38.     if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
  39.       return "'" .. v .. "'"
  40.     end
  41.     return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
  42.   else
  43.     return "table" == type( v ) and table.tostring( v ) or
  44.       tostring( v )
  45.   end
  46. end
  47. function table.key_to_str ( k )
  48.   if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
  49.     return k
  50.   else
  51.     return "[" .. table.val_to_str( k ) .. "]"
  52.   end
  53. end
  54. function table.tostring( tbl )
  55.   local result, done = {}, {}
  56.   for k, v in ipairs( tbl ) do
  57.     table.insert( result, table.val_to_str( v ) )
  58.     done[ k ] = true
  59.   end
  60.   for k, v in pairs( tbl ) do
  61.     if not done[ k ] then
  62.       table.insert( result,
  63.         table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
  64.     end
  65.   end
  66.   return "{" .. table.concat( result, "," ) .. "}"
  67. end
  68. function exportToFile(m, filename)
  69.     file = io.open(filename,"a")
  70.     file:write(table.tostring(m) .. "\n")
  71.     file:flush()
  72.     file:close()
  73. end
  74. function shallowcopy(orig)
  75.     local orig_type = type(orig)
  76.     local copy
  77.     if orig_type == 'table' then
  78.         copy = {}
  79.         for orig_key, orig_value in pairs(orig) do
  80.             copy[orig_key] = orig_value
  81.         end
  82.     else -- number, string, boolean, etc
  83.         copy = orig
  84.     end
  85.     return copy
  86. end
  87.  
  88.  
  89.  
  90. function beginSearch(initDelay, column)
  91.     --check every combination of potential left and right side builds
  92.     for i = 1, #R1[column] do
  93.         for j = 1, #L0[column] do
  94.             --check every order of getting the left pieces and the right pieces
  95.             --endings are currently tied to the Left fills
  96.             combi(1,1,L0[column][j],R1[column][i],E[column][j])
  97.         end
  98.     end
  99. end
  100.  
  101. function combi(Lspot, Rspot, Larray, Rarray, Earray, Pseq, Fseq)
  102.     Pseq = Pseq or {}
  103.     Fseq = Fseq or {}
  104.     --print(#Larray[1])
  105.    
  106.     if (Lspot > #Larray[1] and Rspot > #Rarray[1]) then
  107.         Pseq[#Pseq+1] = E[6][1][1]
  108.         Fseq[#Fseq+1] = 0
  109.         findSequence(Pseq,Fseq)
  110.         return
  111.     end
  112.    
  113.     tempP = shallowcopy(Pseq)
  114.     tempF = shallowcopy(Fseq)
  115.    
  116.     if (Lspot <= #Larray[1]) then
  117.         tempP[#tempP+1] = Larray[1][Lspot];
  118.         tempF[#tempF+1] = Larray[2][Lspot];
  119.        
  120.         combi(Lspot+1,Rspot,Larray,Rarray,Earray,tempP,tempF)
  121.     end
  122.    
  123.     tempP = shallowcopy(Pseq)
  124.     tempF = shallowcopy(Fseq)
  125.     if (Rspot <= #Rarray[1]) then
  126.         tempP[#tempP+1] = Rarray[1][Rspot];
  127.         tempF[#tempF+1] = Rarray[2][Rspot];
  128.        
  129.         combi(Lspot,Rspot+1,Larray,Rarray,Earray,tempP,tempF)
  130.     end
  131.    
  132. end
  133.  
  134. fastestTotal = 9999
  135. fastestSeq = {}
  136. fastestWaits = {}
  137. initDelay = 4
  138. secondDelay = 42
  139.  
  140. function findSequence(Pseq, Fseq)
  141.     tempRNG = RNG
  142.     tempID = SPAWNID
  143.     tempCount = SPAWNCOUNT
  144.    
  145.     totalLoss = 0
  146.     waitList = {}
  147.     Fseq[0] = secondDelay
  148.    
  149.     for i = 1, initDelay do
  150.         tempRNG = advanceRNG(tempRNG)
  151.     end
  152.     file = io.open("results.txt","a")
  153.     --file:write(tempRNG .. " " .. names[pickPiece(tempRNG, tempID, tempCount)] .. "\n")
  154.     file:flush()
  155.     file:close()
  156.     for i = 1, #Pseq do
  157.         framesLost = 0
  158.         nextP = pickPiece(tempRNG, tempID, tempCount)
  159.         while names[nextP] ~= Pseq[i] do
  160.             framesLost = framesLost+1
  161.             if (framesLost > 15) then return end
  162.             tempRNG = advanceRNG(tempRNG)
  163.             nextP = pickPiece(tempRNG, tempID, tempCount)
  164.         end
  165.         waitList[#waitList+1] = framesLost
  166.         totalLoss = totalLoss + framesLost
  167.        
  168.         if (doubledip == true) then tempRNG = advanceRNG(tempRNG) end
  169.         for i = 1, Fseq[i-1] do
  170.             tempRNG = advanceRNG(tempRNG)
  171.         end
  172.         tempID = ids[Pseq[i]]
  173.         tempCount = tempCount + 1
  174.     end
  175.    
  176.    
  177.     file = io.open("results.txt","a")
  178.     file:write(table.tostring(Pseq) .. ": " .. totalLoss .. " (" .. table.tostring(waitList) .. ")\n")
  179.     file:flush()
  180.     file:close()
  181. end
  182.  
  183. function findNext(initRNG, targetPiece)
  184.     local frameCount = 0
  185.     while true do
  186.         if (names[pickPiece(initRNG)] == targetPiece) then return frameCount end
  187.         frameCount = frameCount + 1
  188.         initRNG = advanceRNG(initRNG)
  189.     end
  190. end
  191.  
  192. function advanceRNG(aRNG)
  193.     local temp = 0
  194.     if (bit.check(aRNG,1) ~= bit.check(aRNG,9)) then temp = 1 end
  195.     if (temp == 1) then aRNG = bit.set(aRNG,16) end
  196.     aRNG = bit.rshift(aRNG, 1)
  197.    
  198.     return aRNG
  199. end
  200.  
  201. doubledip = false
  202. function pickPiece(theRNG, theID, theCount)
  203.     theRNG = advanceRNG(theRNG)
  204.     theCount = theCount + 1
  205.     local index = bit.rshift(theRNG,8) + theCount
  206.     index = index % 8
  207.     if (index ~= 7 and SPAWNTABLE[index] ~= theID) then
  208.         doubledip = false
  209.         theID = SPAWNTABLE[index]
  210.     else
  211.         doubledip = true
  212.         theRNG = advanceRNG(theRNG)
  213.         index = bit.rshift(theRNG,8)
  214.         index = ((index % 8) + theID) % 7
  215.         theID = SPAWNTABLE[index]
  216.     end
  217.     return theID
  218. end
  219.  
  220. beginSearch(0,6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement