Advertisement
warmCabin

Pictionary RNG script v1.0

Mar 22nd, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. local slot0 = savestate.object(1)
  2. local start_frame = 0
  3. local test_frame = 0
  4. local inited = false
  5. local testing = false
  6. local prev = nil
  7. local sum = 0
  8. local index = 0
  9. local lagCheck = true
  10. local freeFrame = 0
  11. local lagsChecked = 0
  12. local best = 123456789
  13. local worst = 0
  14.  
  15. emu.speedmode("turbo") --It seems to work better when you click turbo from the menu.
  16. emu.setrenderplanes(false,false)
  17. emu.unpause()
  18.  
  19. local function init()
  20.    
  21.     savestate.load(slot0)
  22.     start_frame = emu.framecount()
  23.     prev = "................"
  24.     sum = 0
  25.     index = 0
  26.     testing = false
  27.     lagCheck = false
  28.     lagsChecked = 0
  29.     freeFrame = 0
  30.     inited = true
  31.    
  32. end
  33.  
  34. local function spacelessLen(str)
  35.     local ans = 0
  36.     for i=1,#str do
  37.         if str:sub(i,i)~=" " then
  38.             ans = ans + 1
  39.         end
  40.     end
  41.     return ans
  42. end
  43.  
  44. local function clampRight(str)
  45.     local length = 0
  46.     for i=1,#str do
  47.         if str:sub(i,i) ~= ' ' then
  48.             length = i
  49.         end
  50.     end
  51.     return str:sub(1,length)
  52. end
  53.  
  54. local function readWord()
  55.     local word = memory.readbyterange(0x0448, 16)
  56.     local ret  = ""
  57.     for i=1,#word do
  58.         local c = word:sub(i,i):byte()
  59.         if c == 0x28 then
  60.             --emu.print(" ")
  61.             ret = ret.." "
  62.         elseif c>=0x0A and c<= 0x23 then
  63.             --emu.print(string.char(c-0x0A+65))
  64.             ret = ret..string.char(c-0x0A+65)
  65.         else
  66.             ret = ret.."." --dot for unknown characters, just like the hex editor
  67.         end
  68.         --emu.print(word:sub(i,i):byte()-0x0A+65)
  69.         --emu.print(word:sub(i,i):byte())
  70.     end
  71.     --emu.print(ret)
  72.     return clampRight(ret)
  73. end
  74.  
  75. local function post_frame()
  76.    
  77.     if not inited then init() end
  78.    
  79.     if emu.framecount() - start_frame == test_frame then
  80.         emu.print("Testing frame "..emu.framecount().."...")
  81.         testing = true
  82.         joypad.set(1,{start=true})
  83.         return
  84.     end
  85.    
  86.     if not testing then return end
  87.    
  88.     if freeFrame==0 and not lagCheck and emu.lagged() then
  89.         lagCheck = true
  90.         lagsChecked = lagsChecked + 1
  91.     end
  92.    
  93.     if freeFrame==0 and lagCheck and not emu.lagged() then
  94.         lagCheck = false
  95.         if lagsChecked == 5 then
  96.             --emu.print("Escaped lag at frame "..emu.framecount())
  97.             freeFrame = emu.framecount()
  98.         end
  99.     end
  100.    
  101.     if AND(emu.framecount(),1) == 0 then
  102.         joypad.set(1,{start=true})
  103.     end
  104.    
  105.     local word = readWord()
  106.    
  107.     if word ~= prev then
  108.         emu.print(word)
  109.         sum = sum + spacelessLen(word)
  110.         index = index + 1
  111.         if index==6 then
  112.             test_frame = test_frame + 1
  113.             inited = false
  114.             emu.print(sum.." characters + "..(freeFrame-1182).." frames of waiting.")
  115.             if sum < best then
  116.                 emu.print("!!BEST SO FAR!!")
  117.                 best = sum
  118.             elseif sum > worst then
  119.                 emu.print("...WORST SO FAR...")
  120.                 worst = sum
  121.             end
  122.             emu.print("________________________")
  123.         end
  124.     end
  125.    
  126.     prev = word
  127.    
  128. end
  129. emu.registerafter(post_frame)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement