Advertisement
Blazephlozard

nintendo tetris cheating lua

Jun 17th, 2020
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. local RNG1 = memory.readbyte(0x0017)
  2. local RNG2 = memory.readbyte(0x0018)
  3.  
  4. local RNG = memory.read_u16_be(0x0017)
  5. local SPAWNID = memory.readbyte(0x0019)
  6. local SPAWNCOUNT = memory.readbyte(0x001A)
  7. --the in-game connection between the number at SPAWNID and the shape it gives
  8. local SPAWNTABLE = { [0] = 0x02, 0x07, 0x08, 0x0A, 0x0B, 0x0E, 0x12 }
  9. --1-7 table of possible pieces
  10. local letters = { "T", "J", "Z", "O", "S", "L", "I" }
  11. --convert spawn table number to string
  12. local names = { [0x02] = "T", [0x07] = "J", [0x08] = "Z", [0x0A] = "O", [0x0B] = "S", [0x0E] = "L", [0x12] = "I" }
  13. --convert string to spawn table number
  14. local 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.  
  19. --ORDER OF PIECES CHEATED WHEN YOU PRESS SPACE for frame timing documentation
  20. cheatsequence = { I, J, Z, J, I, I }
  21.  
  22.  
  23. --Keyboard states last frame and this frame
  24. prevks = input.get()
  25. curks = input.get()
  26. prevspawncount = memory.readbyte(0x001A)
  27. curcheat = 0
  28.  
  29. curseq = 0
  30.  
  31. function update()
  32.     RNG1 = memory.readbyte(0x0017)
  33.     RNG2 = memory.readbyte(0x0018)
  34.     RNG = memory.read_u16_be(0x0017)
  35.     SPAWNID = memory.readbyte(0x0019)
  36.     SPAWNCOUNT = memory.readbyte(0x001A)
  37. end
  38.  
  39. function advanceRNG()
  40.     local temp = 0
  41.     if (bit.check(RNG,1) ~= bit.check(RNG,9)) then temp = 1 end
  42.     if (temp == 1) then RNG = bit.set(RNG,16) end
  43.     RNG = bit.rshift(RNG, 1)
  44.    
  45.     return RNG
  46. end
  47.  
  48. function pickPiece()
  49.     --RNG = memory.read_u16_be(0x0017)
  50.     tempRNG = RNG
  51.     SPAWNID = memory.readbyte(0x0019)
  52.     SPAWNCOUNT = memory.readbyte(0x001A) + 1
  53.     local index = bit.rshift(RNG,8) + SPAWNCOUNT
  54.     print(RNG)
  55.     print(index)
  56.     index = index % 8
  57.     if (index ~= 7 and SPAWNTABLE[index] ~= SPAWNID) then
  58.         SPAWNID = SPAWNTABLE[index]
  59.     else
  60.         advanceRNG()
  61.         index = bit.rshift(RNG,8)
  62.         index = ((index % 8) + SPAWNID) % 7
  63.         SPAWNID = SPAWNTABLE[index]
  64.     end
  65.     RNG = tempRNG
  66.     return SPAWNID
  67. end
  68.  
  69. while true do
  70.     curks = input.get()
  71.     update()
  72.    
  73.     for i = 1, #letters do
  74.         if (curks[letters[i]] and not prevks[letters[i]]) then
  75.             curcheat = ids[letters[i]]
  76.         end
  77.     end
  78.    
  79.     if (curks["Space"] and not prevks["Space"]) then
  80.         if (curseq == 0) then
  81.             curseq = 1
  82.             curcheat = ids[cheatsequence[curseq]]
  83.             prevspawncount = SPAWNCOUNT
  84.         else
  85.             curseq = 0
  86.             curcheat = 0
  87.         end
  88.     end
  89.    
  90.     if (curseq ~= 0) then
  91.         if (SPAWNCOUNT > prevspawncount) then
  92.             prevspawncount = SPAWNCOUNT
  93.             curseq = curseq + 1
  94.             curcheat = ids[cheatsequence[curseq]]
  95.             if (curseq > #cheatsequence) then
  96.                 curseq = 0
  97.                 curcheat = 0
  98.             end
  99.         end
  100.     end
  101.    
  102.     if (curcheat ~= 0) then
  103.         memory.writebyte(0x00BF, curcheat)
  104.     end
  105.    
  106.    
  107.    
  108.    
  109.     --advanceRNG()
  110.     --print(names[pickPiece()])
  111.    
  112.     prevks = curks
  113.     emu.frameadvance()
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement