Blazephlozard

Tetris (Nintendo) Lua script

Aug 8th, 2020 (edited)
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. LINE21 = 0x04C8
  2. LINENEG1 = 0x04F6
  3.  
  4. GAMESTATE = memory.readbyte(0x0048)
  5. states = { [0] = "copyright", "moving", "locking", "checking", "clearing", "updating", "updating", "updating", "spawning", "x", "x", "x", "x", "x" }
  6.  
  7. RNG = memory.read_u16_be(0x0017)
  8. SPAWNID = memory.readbyte(0x0019)
  9. SPAWNCOUNT = memory.readbyte(0x001A)
  10. --the in-game connection between the number at SPAWNID and the shape it gives
  11. SPAWNTABLE = { [0] = 0x02, 0x07, 0x08, 0x0A, 0x0B, 0x0E, 0x12 }
  12. --1-7 table of possible pieces
  13. letters = { "T", "J", "Z", "O", "S", "L", "I" }
  14. --convert spawn table number to string
  15. names = { [0x02] = "T", [0x07] = "J", [0x08] = "Z", [0x0A] = "O", [0x0B] = "S", [0x0E] = "L", [0x12] = "I" }
  16. --convert string to spawn table number
  17. ids = { T = 0x02, J = 0x07, Z = 0x08, O = 0x0A, S = 0x0B, L = 0x0E, I = 0x12 }
  18. --constants for writing sequences in easily
  19. T = "T"; J = "J"; Z = "Z"; O = "O"; S = "S"; L = "L"; I = "I";
  20.  
  21. --transparency constants
  22. transpercent = 75
  23. trans = math.ceil(0xFF * transpercent/100) * 0x1000000
  24.  
  25. --color constants
  26. lineColor = { [0x7B] = trans + 0x4240fe, [0x7C] = trans + 0x000000, [0x7D] = trans + 0x000000 }
  27. fillColor = { [0x7B] = trans + 0xffffff, [0x7C] = trans + 0xb53220, [0x7D] = trans + 0x4240fe }
  28.  
  29. gui.defaultPixelFont("fceux")
  30.  
  31. --Keyboard states last frame and this frame
  32. prevks = input.get()
  33. curks = input.get()
  34. curcheat = 0
  35. cheatseq = {}
  36. doubledip = false
  37. startingSpawnCount = 0
  38.  
  39. function update()
  40.     RNG = memory.read_u16_be(0x0017)
  41.     SPAWNID = memory.readbyte(0x0019)
  42.     SPAWNCOUNT = memory.readbyte(0x001A)
  43. end
  44.  
  45. function advanceRNG()
  46.     local temp = 0
  47.     if (bit.check(RNG,1) ~= bit.check(RNG,9)) then temp = 1 end
  48.     if (temp == 1) then RNG = bit.set(RNG,16) end
  49.     RNG = bit.rshift(RNG, 1)
  50.    
  51.     return RNG
  52. end
  53.  
  54. function pickPiece()
  55.     --RNG = memory.read_u16_be(0x0017)
  56.     tempRNG = RNG
  57.     advanceRNG()
  58.     SPAWNID = memory.readbyte(0x0019)
  59.     SPAWNCOUNT = memory.readbyte(0x001A) + 1
  60.     local index = bit.rshift(RNG,8) + SPAWNCOUNT
  61.     index = index % 8
  62.     if (index ~= 7 and SPAWNTABLE[index] ~= SPAWNID) then
  63.         doubledip = false
  64.         SPAWNID = SPAWNTABLE[index]
  65.     else
  66.         doubledip = true
  67.         advanceRNG()
  68.         index = bit.rshift(RNG,8)
  69.         index = ((index % 8) + SPAWNID) % 7
  70.         SPAWNID = SPAWNTABLE[index]
  71.     end
  72.     RNG = tempRNG
  73.     return SPAWNID
  74. end
  75.  
  76. function weLoaded(branchIndex)
  77.     theBranch = tastudio.getbranches()[branchIndex]
  78.    
  79.     startingSpawnCount = memory.readbyte(0x001A)
  80.     seqString = theBranch["Text"]
  81.     cheatseq = {}
  82.    
  83.     for i = 1, string.len(seqString), 2 do
  84.         thePiece = string.char(string.byte(seqString,i))
  85.         --encounter illegal character in the branch name? quit out
  86.         if (ids[thePiece] == null) then
  87.             startingSpawnCount = 0
  88.             return
  89.         end
  90.         cheatseq[#cheatseq+1] = thePiece
  91.     end
  92.     --Forcibly clear the greenzone
  93.     tastudio.submitinsertframes(emu.framecount()-1,1)
  94.     tastudio.submitdeleteframes(emu.framecount()-1,1)
  95.     tastudio.applyinputchanges()
  96.     tastudio.setplayback(emu.framecount()-1)
  97.    
  98.    
  99. end
  100.  
  101. tastudio.onbranchload(weLoaded)
  102.  
  103. while true do
  104.     curks = input.get()
  105.     update()
  106.    
  107.     --you can cheat specific pieces by using the keyboard
  108.     for i = 1, #letters do
  109.         if (curks[letters[i]] and not prevks[letters[i]]) then
  110.             if (curcheat == ids[letters[i]]) then curcheat = 0
  111.             else curcheat = ids[letters[i]] end
  112.         end
  113.     end
  114.    
  115.     if (startingSpawnCount ~= 0) then
  116.         advancedSpawns = SPAWNCOUNT - startingSpawnCount
  117.         if (advancedSpawns+1 <= #cheatseq and advancedSpawns >= 0) then
  118.             curcheat = ids[cheatseq[advancedSpawns+1]]
  119.         else
  120.             curcheat = 0
  121.         end
  122.     end
  123.    
  124.     if (curcheat ~= 0) then
  125.         memory.writebyte(0x00BF, curcheat)
  126.         gui.pixelText(87, 217, "CHEATING " .. names[curcheat] .. "-PIECE", "red")
  127.     end
  128.    
  129.     for i = 5, 0, -1 do
  130.         for j = 1, 10 do
  131.             theBlock = memory.readbyte(LINENEG1 - (i*10) + (j-1))
  132.             if (theBlock ~= 0xEF) then
  133.                 gui.drawRectangle(95+((j-1)*8),((6-i-1)*8)-1,8,8,lineColor[theBlock],fillColor[theBlock])
  134.             end
  135.         end
  136.     end
  137.    
  138.     GAMESTATE = memory.readbyte(0x0048)
  139.     --do not run during copyright screen
  140.     if (GAMESTATE ~= 0) then
  141.         color = "lightcoral"
  142.         if (GAMESTATE == 1) then color = "lightgreen" end
  143.         gui.pixelText(185, 190, GAMESTATE .. ": " .. states[GAMESTATE], color,"black")
  144.        
  145.         gui.pixelText(190, 146, "Next: " .. names[SPAWNID],"white","black")
  146.        
  147.         gui.drawBox(11,7,84,236,"black","black")
  148.  
  149.         for i = 0, 24 do
  150.             nextPieceString = (emu.framecount()+i+1) .. ": " .. names[pickPiece()]
  151.             if (doubledip) then nextPieceString = nextPieceString .. " (" .. "2" .. ")" end
  152.             gui.pixelText(12, 8+(9*i), nextPieceString)
  153.             advanceRNG()
  154.         end
  155.     end
  156.    
  157.     prevks = curks
  158.     emu.frameadvance()
  159. end
Add Comment
Please, Sign In to add comment