Advertisement
FractalFusion

Lua script for Pokemon 3rd gen.

Dec 29th, 2012
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.55 KB | None | 0 0
  1. local game=6 --see below
  2. local startvalue=0x00001337 --insert the first value of RNG
  3.  
  4. -- These are all the possible key names: [keys]
  5. -- backspace, tab, enter, shift, control, alt, pause, capslock, escape,
  6. -- space, pageup, pagedown, end, home, left, up, right, down, insert, delete,
  7. -- 0 .. 9, A .. Z, numpad0 .. numpad9, numpad*, numpad+, numpad-, numpad., numpad/,
  8. -- F1 .. F24, numlock, scrolllock, semicolon, plus, comma, minus, period, slash, tilde,
  9. -- leftbracket, backslash, rightbracket, quote.
  10. -- [/keys]
  11. -- Key names must be in quotes.
  12. -- Key names are case sensitive.
  13. local key={"9", "8", "7"}
  14.  
  15. -- It is not necessary to change anything beyond this point.
  16.  
  17. --for different display modes
  18. local status=1
  19. local substatus={1,1,1}
  20.  
  21. local tabl={}
  22. local prev={}
  23.  
  24. local xfix=0 --x position of display handle
  25. local yfix=60 --y position of display handle
  26. local k
  27.  
  28.  
  29.  
  30. --for different game versions
  31. --1: Ruby/Sapphire U
  32. --2: Emerald U
  33. --3: FireRed/LeafGreen U
  34. --4: Ruby/Sapphire J
  35. --5: Emerald J (TODO)
  36. --6: FireRed/LeafGreen J (1360)
  37.  
  38. local gamename={"Ruby/Sapphire U", "Emerald U", "FireRed/LeafGreen U", "Ruby/Sapphire J", "Emerald J", "FireRed/LeafGreen J (1360)"}
  39.  
  40. local pstats={0x3004360, 0x20244EC, 0x2024284, 0x3004290, 0x2024190, 0x20241E4}
  41. local estats={0x30045C0, 0x2024744, 0x202402C, 0x30044F0, 0x0000000, 0x2023F8C}
  42. local rng   ={0x3004818, 0x3005D80, 0x3005000, 0x3004748, 0x0000000, 0x3005040} --0X3004FA0
  43. local rng2  ={0x0000000, 0x0000000, 0x20386D0, 0x0000000, 0x0000000, 0x203861C}
  44.  
  45. --HP, Atk, Def, Spd, SpAtk, SpDef
  46. local statcolor = {"yellow", "red", "blue", "green", "magenta", "cyan"}
  47.  
  48. local flag=0
  49. local i,j
  50. local last=0
  51. local cur
  52. local test
  53. local test2
  54. local counter=0
  55. local indexfind
  56. local index
  57. local clr
  58. local randvalue
  59. local prev
  60. local tid
  61. local modd
  62.  
  63. local start
  64.  
  65. local personality
  66. local trainerid
  67. local magicword
  68. local growthoffset
  69. local miscoffset
  70. local effortoffset
  71. local i
  72. local species
  73. local holditem
  74. local pokerus
  75. local ivs
  76. local evs1
  77. local evs2
  78. local hpiv
  79. local atkiv
  80. local defiv
  81. local spdiv
  82. local spatkiv
  83. local spdefiv
  84. local nature
  85. local natinc
  86. local natdec
  87. local hidpowbase
  88. local hidpowtype
  89.  
  90. local bnd,br,bxr=bit.band,bit.bor,bit.bxor
  91. local rshift, lshift=bit.rshift, bit.lshift
  92. local mdword=memory.readdwordunsigned
  93. local mword=memory.readwordunsigned
  94. local mbyte=memory.readbyteunsigned
  95.  
  96. --these 32-value tables are for fast indexing of RNG values
  97. --the RNG is a linear congruential generator with modulus 2^32
  98. --x<-0x41C64E6D*x+0x6073
  99. --because the modulus is a power of two
  100. --it is very easy to find the index
  101. --only takes O(log n) time
  102. --in general, it is unreasonable to calculate the index of an RNG without lookup tables
  103. --one can use lookup tables to find indices in O(1) time
  104. --however for this RNG, it takes up way too much space (16GB)
  105.  
  106. local multspa={
  107.  0x41C64E6D, 0xC2A29A69, 0xEE067F11, 0xCFDDDF21,
  108.  0x5F748241, 0x8B2E1481, 0x76006901, 0x1711D201,
  109.  0xBE67A401, 0xDDDF4801, 0x3FFE9001, 0x90FD2001,
  110.  0x65FA4001, 0xDBF48001, 0xF7E90001, 0xEFD20001,
  111.  0xDFA40001, 0xBF480001, 0x7E900001, 0xFD200001,
  112.  0xFA400001, 0xF4800001, 0xE9000001, 0xD2000001,
  113.  0xA4000001, 0x48000001, 0x90000001, 0x20000001,
  114.  0x40000001, 0x80000001, 0x00000001, 0x00000001}
  115.  
  116. local multspb={
  117.  0x00006073, 0xE97E7B6A, 0x31B0DDE4, 0x67DBB608,
  118.  0xCBA72510, 0x1D29AE20, 0xBA84EC40, 0x79F01880,
  119.  0x08793100, 0x6B566200, 0x803CC400, 0xA6B98800,
  120.  0xE6731000, 0x30E62000, 0xF1CC4000, 0x23988000,
  121.  0x47310000, 0x8E620000, 0x1CC40000, 0x39880000,
  122.  0x73100000, 0xE6200000, 0xCC400000, 0x98800000,
  123.  0x31000000, 0x62000000, 0xC4000000, 0x88000000,
  124.  0x10000000, 0x20000000, 0x40000000, 0x80000000}
  125.  
  126. local multspc={
  127.  0x00003039, 0xD3DC167E, 0xD6651C2C, 0xCD1DCF18,
  128.  0x65136930, 0x642B7E60, 0x1935ACC0, 0xB6461980,
  129.  0x1EF73300, 0x1F9A6600, 0x85E4CC00, 0x26899800,
  130.  0xB8133000, 0x1C266000, 0xE84CC000, 0x90998000,
  131.  0x21330000, 0x42660000, 0x84CC0000, 0x09980000,
  132.  0x13300000, 0x26600000, 0x4CC00000, 0x99800000,
  133.  0x33000000, 0x66000000, 0xCC000000, 0x98000000,
  134.  0x30000000, 0x60000000, 0xC0000000, 0x80000000}
  135.  
  136. local natureorder={"Atk","Def","Spd","SpAtk","SpDef"}
  137. local naturename={
  138.  "Hardy","Lonely","Brave","Adamant","Naughty",
  139.  "Bold","Docile","Relaxed","Impish","Lax",
  140.  "Timid","Hasty","Serious","Jolly","Naive",
  141.  "Modest","Mild","Quiet","Bashful","Rash",
  142.  "Calm","Gentle","Sassy","Careful","Quirky"}
  143. local typeorder={
  144.  "Fighting","Flying","Poison","Ground",
  145.  "Rock","Bug","Ghost","Steel",
  146.  "Fire","Water","Grass","Electric",
  147.  "Psychic","Ice","Dragon","Dark"}
  148.  
  149. --a 32-bit, b bit position bottom, d size
  150. function getbits(a,b,d)
  151.  return rshift(a,b)%lshift(1,d)
  152. end
  153.  
  154.  
  155. function gettop(a)
  156.  return(rshift(a,16))
  157. end
  158.  
  159.  
  160. --does 32-bit multiplication
  161. --necessary because Lua does not allow 32-bit integer definitions
  162. --so one cannot do 32-bit arithmetic
  163. --furthermore, precision loss occurs at around 10^10
  164. --so numbers must be broken into parts
  165. --may be improved using bitop library exclusively
  166. function mult32(a,b)
  167.  local c=rshift(a,16)
  168.  local d=a%0x10000
  169.  local e=rshift(b,16)
  170.  local f=b%0x10000
  171.  local g=(c*f+d*e)%0x10000
  172.  local h=d*f
  173.  local i=g*0x10000+h
  174.  return i
  175. end
  176.  
  177. -- draws a 3x3 square with x position a, y position b, and color c
  178. function drawsquare(a,b,c)
  179.  gui.box(a,b,a+2,b+2,c)
  180. end
  181.  
  182. -- draws a down arrow, x position a, y position b, and color c
  183. -- this arrow marks the square for the current RNG value
  184. function drawarrow(a,b,c)
  185.  gui.line(a,b,a-2,b-2,c)
  186.  gui.line(a,b,a+2,b-2,c)
  187.  gui.line(a,b,a,b-6,c)
  188. end
  189.  
  190.  
  191.  
  192.  
  193. --a press is when input is registered on one frame but not on the previous
  194. --that's why the previous input is used as well
  195. prev=input.get()
  196. function fn()
  197. --*********
  198.  tabl=input.get()
  199.  
  200.  if tabl[key[1]] and not prev[key[1]] then
  201.   status=status+1
  202.   if status==4 then
  203.    status=1
  204.   end
  205.  end
  206.  
  207.  if tabl[key[2]] and not prev[key[2]] then
  208.   substatus[status]=substatus[status]+1
  209.   if substatus[status]==9 then
  210.    substatus[status]=1
  211.   end
  212.  end
  213.  
  214.  if tabl[key[3]] and not prev[key[3]] then
  215.   substatus[status]=substatus[status]-1
  216.   if substatus[status]==0 then
  217.    substatus[status]=8
  218.   end
  219.  end
  220.  
  221.  gui.text(200,0,status)
  222.  gui.text(200,10,substatus[1])
  223.  
  224.  gui.text(200,20,substatus[2])
  225.  gui.text(200,30,substatus[3])
  226.  
  227.  prev=tabl
  228.  
  229. -- now for display
  230.  if status==1 or status==2 then --status 1 or 2
  231.  
  232.     if status==1 then
  233.       start=pstats[game]+100*(substatus[1]-1)
  234.     else
  235.      start=estats[game]+100*(substatus[2]-1)
  236.     end
  237.  
  238.     personality=mdword(start)
  239.     trainerid=mdword(start+4)
  240.     magicword=bxr(personality, trainerid)
  241.  
  242.     i=personality%24
  243.  
  244.     if i<=5 then
  245.      growthoffset=0
  246.     elseif i%6<=1 then
  247.      growthoffset=12
  248.     elseif i%2==0 then
  249.      growthoffset=24
  250.     else
  251.      growthoffset=36
  252.     end
  253.  
  254.     if i>=18 then
  255.      miscoffset=0
  256.     elseif i%6>=4 then
  257.      miscoffset=12
  258.     elseif i%2==1 then
  259.      miscoffset=24
  260.     else
  261.      miscoffset=36
  262.     end
  263.  
  264.     if i>=12 and i<=17 then
  265.      effortoffset=0
  266.     elseif i==2 or i==3 or i==8 or i==9 or i==22 or i==23 then
  267.      effortoffset=12
  268.     elseif i==0 or i==5 or i==6 or i==11 or i==19 or i==21 then
  269.      effortoffset=24
  270.     else
  271.      effortoffset=36
  272.     end
  273.  
  274.     species=getbits(bxr(mdword(start+32+growthoffset),magicword),0,16)
  275.  
  276.     holditem=getbits(bxr(mdword(start+32+growthoffset),magicword),16,16)
  277.  
  278.     pokerus=getbits(bxr(mdword(start+32+miscoffset),magicword),0,8)
  279.  
  280.     ivs=bxr(mdword(start+32+miscoffset+4),magicword)
  281.  
  282.     evs1=bxr(mdword(start+32+effortoffset),magicword)
  283.     evs2=bxr(mdword(start+32+effortoffset+4),magicword)
  284.  
  285.     hpiv=getbits(ivs,0,5)
  286.     atkiv=getbits(ivs,5,5)
  287.     defiv=getbits(ivs,10,5)
  288.     spdiv=getbits(ivs,15,5)
  289.     spatkiv=getbits(ivs,20,5)
  290.     spdefiv=getbits(ivs,25,5)
  291.  
  292.     nature=personality%25
  293.     natinc=math.floor(nature/5)
  294.     natdec=nature%5
  295.  
  296.     hidpowtype=math.floor(((hpiv%2 + 2*(atkiv%2) + 4*(defiv%2) + 8*(spdiv%2) + 16*(spatkiv%2) + 32*(spdefiv%2))*15)/63)
  297.     hidpowbase=math.floor((( getbits(hpiv,1,1) + 2*getbits(atkiv,1,1) + 4*getbits(defiv,1,1) + 8*getbits(spdiv,1,1) + 16*getbits(spatkiv,1,1) + 32*getbits(spdefiv,1,1))*40)/63 + 30)
  298.  
  299.     gui.text(xfix+15,yfix-8, "Stat")
  300.     gui.text(xfix+40,yfix-8, "IV")
  301.     gui.text(xfix+60,yfix-8, "EV")
  302.     gui.text(xfix+80,yfix-8, "Nat")
  303.  
  304.     gui.text(xfix,yfix-16, "CurHP: "..mword(start+86).."/"..mword(start+88), "yellow")
  305.     if status==2 then
  306.      gui.text(xfix,yfix-24, "Enemy "..substatus[2])
  307.     elseif status==1 then
  308.      gui.text(xfix,yfix-24, "Player "..substatus[1])
  309.     end
  310.  
  311.     gui.text(xfix,yfix+0,"HPT", "yellow")
  312.     gui.text(xfix,yfix+8,"ATK", "red")
  313.     gui.text(xfix,yfix+16,"DEF", "blue")
  314.     gui.text(xfix,yfix+24,"SPE", "green")
  315.     gui.text(xfix,yfix+32,"SAT", "magenta")
  316.     gui.text(xfix,yfix+40,"SDF", "cyan")
  317.  
  318.     gui.text(xfix+20,yfix, mword(start+88), "yellow")
  319.     gui.text(xfix+20,yfix+8, mword(start+90), "red")
  320.     gui.text(xfix+20,yfix+16, mword(start+92), "blue")
  321.     gui.text(xfix+20,yfix+24, mword(start+94), "green")
  322.     gui.text(xfix+20,yfix+32, mword(start+96), "magenta")
  323.     gui.text(xfix+20,yfix+40, mword(start+98), "cyan")
  324.  
  325.     gui.text(xfix+40,yfix, hpiv, "yellow")
  326.     gui.text(xfix+40,yfix+8, atkiv, "red")
  327.     gui.text(xfix+40,yfix+16, defiv, "blue")
  328.     gui.text(xfix+40,yfix+24, spdiv, "green")
  329.     gui.text(xfix+40,yfix+32, spatkiv, "magenta")
  330.     gui.text(xfix+40,yfix+40, spdefiv, "cyan")
  331.  
  332.     gui.text(xfix+60,yfix, getbits(evs1, 0, 8), "yellow")
  333.     gui.text(xfix+60,yfix+8, getbits(evs1, 8, 8), "red")
  334.     gui.text(xfix+60,yfix+16, getbits(evs1, 16, 8), "blue")
  335.     gui.text(xfix+60,yfix+24, getbits(evs1, 24, 8), "green")
  336.     gui.text(xfix+60,yfix+32, getbits(evs2, 0, 8), "magenta")
  337.     gui.text(xfix+60,yfix+40, getbits(evs2, 8, 8), "cyan")
  338.  
  339.     if natinc~=natdec then
  340.      gui.text(xfix+80,yfix+8*(natinc+1), "+", statcolor[natinc+2])
  341.      gui.text(xfix+80,yfix+8*(natdec+1), "-", statcolor[natdec+2])
  342.     else
  343.      gui.text(xfix+80,yfix+8*(natinc+1), "+-", "grey")
  344.     end
  345.  end --status 1 or 2
  346.  
  347.  --gui.text(0,30,"Species "..species)
  348.  --gui.text(0,40,"Nature: "..naturename[nature+1])
  349.  --gui.text(0,50,natureorder[natinc+1].."+ "..natureorder[natdec+1].."-")
  350.  --gui.text(0,60,"Hidden Power: "..typeorder[hidpowtype+1].." "..hidpowbase)
  351.  --gui.text(0,70,"Hold Item "..holditem)
  352.  --gui.text(0,80,"Pokerus Status "..pokerus)
  353.  if status==3 then
  354.     i=0
  355.     cur=memory.readdword(rng[game])
  356.     test=last
  357.     while bit.tohex(cur)~=bit.tohex(test) and i<=100 do
  358.      test=mult32(test,0x41C64E6D) + 0x6073
  359.      i=i+1
  360.     end
  361.     gui.text(120,20,"Last RNG value: "..bit.tohex(last))
  362.     last=cur
  363.     gui.text(120,0,"Current RNG value: "..bit.tohex(cur))
  364.     if i<=100 then
  365.      gui.text(120,10,"RNG distance since last: "..i)
  366.     else
  367.      gui.text(120,10,"RNG distance since last: >100")
  368.     end
  369.    
  370.  
  371.    
  372.    
  373.     --math
  374.     indexfind=startvalue
  375.     index=0
  376.     for j=0,31,1 do
  377.      if getbits(cur,j,1)~=getbits(indexfind,j,1) then
  378.       indexfind=mult32(indexfind,multspa[j+1])+multspb[j+1]
  379.       index=index+bit.lshift(1,j)
  380.       if j==31 then
  381.        index=index+0x100000000
  382.       end
  383.      end
  384.     end
  385.     gui.text(120,30,index)
  386.    
  387.    
  388.     if substatus[3]>=5 and substatus[3]<=8 then
  389.      modd=2
  390.     else
  391.      modd=3
  392.     end
  393.    
  394.     if i>modd and i<=100 then
  395.      gui.box(3,30,17,44, "red")
  396.      gui.box(5,32,15,42, "black")
  397.     end
  398.    
  399.     if substatus[3]%4==1 then
  400.        gui.text(10,45, "Critical Hit/Max Damage")
  401.     elseif substatus[3]%4==2 then
  402.        gui.text(10,45, "Move Miss (95%)")
  403.     elseif substatus[3]%4==3 then
  404.        gui.text(10,45, "Move Miss (90%)")
  405.     else
  406.        gui.text(10,45, "Quick Claw")
  407.     end
  408.        
  409.      
  410.     drawarrow(3,52, "#FF0000FF")
  411.     test=cur
  412.     -- i row j column
  413.     for i=0,13,1 do
  414.      for j=0,17,1 do
  415.       if j%modd==1 then
  416.        clr="#C0C0C0FF"
  417.       else
  418.        clr="#808080FF"
  419.       end
  420.       randvalue=gettop(test)
  421.       if substatus[3]%4==1 then
  422.        if randvalue%16==0 then
  423.         test2=test
  424.         for k=1,7,1 do
  425.          test2=mult32(test2,0x41C64E6D) + 0x6073
  426.         end
  427.         clr={r=255, g=0x10*(gettop(test2)%16), b=0, a=255}
  428.        end
  429.       end
  430.      
  431.       if substatus[3]%4==2 then
  432.        if randvalue%100>=95 then
  433.         clr="#0000FFFF"
  434.        end
  435.       end
  436.      
  437.       if substatus[3]%4==3 then
  438.        if randvalue%100>=90 then
  439.         clr="#000080FF"
  440.        end
  441.       end
  442.  
  443.       if substatus[3]%4==0 then
  444.        if randvalue<0x3333 then
  445.         clr="#00FF00FF"
  446.        end
  447.       end    
  448.      
  449.       drawsquare(2+4*j,54+4*i, clr)
  450.    
  451.  
  452.       test=mult32(test,0x41C64E6D) + 0x6073
  453.      end
  454.     end
  455.    
  456.    
  457.    
  458.  end
  459.  
  460. gui.text(0,0,emu.framecount())
  461.    
  462. --*********
  463. end
  464. gui.register(fn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement