Advertisement
Zanoab

TWEWY_Battle.lua

Aug 27th, 2011
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.74 KB | None | 0 0
  1. --  Displays information for all objects on the bottom screen
  2. --  and top screen and GUI elements.
  3. --  This is completely safe to leaving running outside battles
  4. --  because the linked list contains 0 items and safely stop.
  5. --  I added the gameplay timer, battle timer, frame count, and
  6. --  lag count because DeSmuME's frame and lag counter would
  7. --  get in the way of the top screen object data.
  8. --  All displayed objects have their address listed for people
  9. --  interested in trying to break down their structs.
  10. local headOBAdr = 0x0235406C
  11. local tailOBAdr = 0x0235408C
  12. local headOTAdr = 0x0235C0F8
  13. local tailOTAdr = 0x0235C118
  14.  
  15. local headGAdr = 0x02364184
  16. local tailGAdr = 0x023641A4
  17.  
  18. local timeAdr = 0x02073AA4
  19. local timeBattleAdr = 0x02074E98
  20.  
  21. local puckPassAdr = 0x023918CD
  22.  
  23. function DEC_HEX(IN,i)
  24.     local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
  25.     while IN>0 do
  26.         I=I+1
  27.         IN,D=math.floor(IN/B),math.mod(IN,B)+1
  28.         OUT=string.sub(K,D,D)..OUT
  29.     end
  30.     while i and i > string.len(OUT) do OUT = "0" .. OUT end
  31.     return OUT
  32. end
  33.  
  34. function readString(stringAdr)
  35.     local index = 0
  36.     local newString = ""
  37.     local charBuffer
  38.     while true do
  39.         charBuffer = string.char(memory.readbyteunsigned(stringAdr + index))
  40.         if charBuffer == "\0" then
  41.             return newString
  42.         else
  43.             newString = newString .. charBuffer
  44.             index = index + 1
  45.         end
  46.     end
  47. end
  48.  
  49. function getGameplayTime()
  50.     return memory.readdwordunsigned(timeAdr)
  51. end
  52. function getBattleTime()
  53.     return memory.readdwordunsigned(timeBattleAdr)
  54. end
  55.  
  56. -- Struct Functions
  57. function getAssetName(currPtr)
  58.     return readString(memory.readdwordunsigned(currPtr+0x004))
  59. end
  60. function getIdent(currPtr)
  61.     return memory.readdwordunsigned(currPtr+0x008)
  62. end
  63. function getIsValid(currPtr)
  64.     return memory.readdwordunsigned(currPtr+0x00C) ~= 0xFFFFFFFF
  65. end
  66. function getNextPtr(currPtr)
  67.     return memory.readdwordunsigned(currPtr+0x018)
  68. end
  69.  
  70. -- Enemy Functions
  71. function getEnemyCurrHp(currPtr)
  72.     return memory.readwordunsigned(currPtr+0x08C)
  73. end
  74. function setEnemyCurrHp(currPtr,hp)
  75.     memory.writewordunsigned(currPtr+0x08C,hp)
  76. end
  77. function getEnemyMaxHp(currPtr)
  78.     return memory.readwordunsigned(currPtr+0x08E)
  79. end
  80. function getEnemyInvuln(currPtr)
  81.     return memory.readwordunsigned(currPtr+0x07A)
  82. end
  83.  
  84. -- Pin Functions
  85. function getPinBootTime(currPtr)
  86.     return memory.readwordunsigned(currPtr+0x068)
  87. end
  88. function getPinUses(currPtr)
  89.     return memory.readwordunsigned(currPtr+0x064)
  90. end
  91. function getPinUsesMax(currPtr)
  92.     return memory.readwordunsigned(currPtr+0x066)
  93. end
  94.  
  95. -- Puck Functions
  96. function getPuckTravelTime(currPtr)
  97.     return memory.readwordsigned(currPtr+0x7BC)
  98. end
  99. function getPuckDecay(currPtr)
  100.     return memory.readdwordunsigned(currPtr+0x7CC)
  101. end
  102. function getPuckMultiplier(currPtr)
  103.     return memory.readwordsigned(currPtr+0x7D0)
  104. end
  105. function getPuckHoldTime(currPtr)
  106.     return memory.readwordsigned(currPtr+0x7D4)
  107. end
  108. function getPuckMultiplierChange(currPtr)
  109.     local ratio = getPuckHoldTime(currPtr) / (930 - (memory.readbyteunsigned(puckPassAdr) * 30))
  110.     if ratio >= 0.98 then
  111.         return 50
  112.     end
  113.     if ratio >= 0.92 then
  114.         return 20
  115.     end
  116.     if ratio >= 0.70 then
  117.         return 10
  118.     end
  119.     return -100
  120. end
  121.  
  122. function Data_Update_Bottom()
  123.     local count
  124.     local printStr = "Gameplay Time: " .. getGameplayTime()
  125.         .. "\n" .. "Battle Time: " .. getBattleTime()
  126.         .. "\n\n"
  127.     local newStr
  128.     -- Objects Bottom
  129.     local currPtr = headOBAdr
  130.     newStr = ""
  131.     count = 0
  132.     while memory.readdwordunsigned(currPtr) == 0x51342403 do
  133.         currPtr = getNextPtr(currPtr)   -- Skips Head
  134.         local structName = getAssetName(currPtr)
  135.         local continue = true
  136.         if currPtr ~= tailOBAdr and getIsValid(currPtr) then
  137.             if continue and structName == "EasyTask TCB" then
  138.                 -- May contain useful information to display later
  139.                 continue = false
  140.             end
  141.             if continue and structName == "_free_" then
  142.                 -- Allocated block that is no longer in use (garbage)
  143.                 continue = false
  144.             end
  145.             if continue and structName == "Tsk_BtlObs" then
  146.                 -- An obstacle on the field that can be moved/thrown
  147.                 continue = false
  148.             end
  149.             if continue and structName == "Tsk_BtlPlayer00" then
  150.                 -- Controllable(?) Neku on screen
  151.                 newStr = newStr
  152.                     .. DEC_HEX(currPtr,8)
  153.                     .. " - Neku"
  154.                     .. "\n"
  155.                 continue = false
  156.             end
  157.             if continue and structName == "Tsk_BtlPrizeItem" then
  158.                 newStr = newStr
  159.                     .. DEC_HEX(currPtr,8)
  160.                     .. " - Receive in: " .. getEnemyMaxHp(currPtr)
  161.                     .. "\n" .. DEC_HEX(getIdent(currPtr))
  162.                     .. "\t" .. structName
  163.                     .. "\n"
  164.                 continue = false
  165.             end
  166.             if continue then
  167.                 newStr = newStr
  168.                     .. DEC_HEX(currPtr,8)
  169.                     .. (
  170.                         (getEnemyInvuln(currPtr) > 0 and (" - Invuln: " .. getEnemyInvuln(currPtr)))
  171.                         or (" - HP: " .. getEnemyCurrHp(currPtr))
  172.                         )
  173.                     .. "\n" .. DEC_HEX(getIdent(currPtr))
  174.                     .. "\t" .. structName
  175.                     .. "\n"
  176.                 continue = false
  177.             end
  178.         else
  179.             break
  180.         end
  181.         count = count + 1
  182.     end
  183.     if count > 0 then
  184.         printStr = printStr .. "Object Count : " .. count .. "\n" .. newStr .. "\n"
  185.     end
  186.    
  187.     -- GUI Bottom
  188.     local currPtr = headGAdr
  189.     newStr = ""
  190.     count = 0
  191.     while memory.readdwordunsigned(currPtr) == 0x51342403 and getNextPtr(currPtr) ~= tailGAdr do
  192.         currPtr = getNextPtr(currPtr)   -- Skips Head
  193.         continue = true
  194.         if getIsValid(currPtr) then
  195.             if continue and getIdent(currPtr) == 0x000002C0 then
  196.                 -- A rebootable pin
  197.                 -- There are occasional false positives
  198.                 newStr = newStr
  199.                     .. DEC_HEX(currPtr,8)
  200.                     .. " - Pin"
  201.                     .. "\n-> " .. (
  202.                         (getPinUses(currPtr) > 0 and "Uses: " .. getPinUses(currPtr) .. "\t/ " .. getPinUsesMax(currPtr))
  203.                         or (getPinBootTime(currPtr) ~= 0xFFFF and "Boot:\t" .. getPinBootTime(currPtr))
  204.                         or "Booting Now..."
  205.                         )
  206.                     .. "\n"
  207.                 continue = false
  208.             end
  209.             if continue and getIdent(currPtr) == 0x000007E0 then
  210.                 -- Contains puck information
  211.                 newStr = newStr
  212.                     .. DEC_HEX(currPtr,8)
  213.                     .. " - Puck"
  214.                     .. "\n-> " .. (
  215.                         (getPuckTravelTime(currPtr) > 0 and "Travel: " .. getPuckTravelTime(currPtr))
  216.                         or "Hold: " .. getPuckHoldTime(currPtr) .. " (" .. (
  217.                             (getPuckMultiplierChange(currPtr) > 0 and "+" .. getPuckMultiplierChange(currPtr))
  218.                             or getPuckMultiplierChange(currPtr)
  219.                             )
  220.                         ) .. ")"
  221.                     .. "\n-> " .. "Multiplier: " .. getPuckMultiplier(currPtr)
  222.                     .. "\n"
  223.                 continue = false
  224.             end
  225.             if false and continue then
  226.                 newStr = newStr
  227.                     .. DEC_HEX(currPtr,8)
  228.                     .. "\tIdent:\t" .. DEC_HEX(getIdent(currPtr))
  229.                     .. "\n"
  230.                 continue = false
  231.             end
  232.         else
  233.             break
  234.         end
  235.         count = count + 1
  236.     end
  237.     if count > 0 then
  238.         printStr = printStr .. "GUI Count: " .. count .. "\n" .. newStr .. "\n"
  239.     end
  240.     -- Return Result
  241.     return printStr
  242. end
  243.  
  244. function Data_Update_Top()
  245.     local count
  246.     local printStr = "Frame: " .. emu.framecount() .. "\n"
  247.         .. "Lag:" .. string.rep(" ",3+string.len(emu.framecount())-string.len(emu.lagcount())) .. emu.lagcount() .. "\n\n"
  248.     local newStr
  249.     -- Objects Bottom
  250.     local currPtr = headOTAdr
  251.     newStr = ""
  252.     count = 0
  253.     while memory.readdwordunsigned(currPtr) == 0x51342403 do
  254.         currPtr = getNextPtr(currPtr)   -- Skips Head
  255.         local structName = getAssetName(currPtr)
  256.         local continue = true
  257.         if currPtr ~= tailOTAdr and getIsValid(currPtr) then
  258.             if continue and structName == "EasyTask TCB" then
  259.                 -- May contain useful information to display later
  260.                 continue = false
  261.             end
  262.             if continue and structName == "_free_" then
  263.                 -- Allocated block that is no longer in use (garbage)
  264.                 continue = false
  265.             end
  266.             if continue and structName == "Tsk_BtlObs" then
  267.                 -- An obstacle on the field that can be moved/thrown
  268.                 continue = false
  269.             end
  270.             if continue and structName == "Tsk_BtlPlayer01" then
  271.                 -- Controllable(?) Shiki on screen?
  272.                 newStr = newStr
  273.                     .. DEC_HEX(currPtr,8)
  274.                     .. " - Shiki"
  275.                     .. "\n"
  276.                 continue = false
  277.             end
  278.             if continue and structName == "Tsk_BtlPrizeItem" then
  279.                 newStr = newStr
  280.                     .. DEC_HEX(currPtr,8)
  281.                     .. " - Receive in: " .. getEnemyMaxHp(currPtr)
  282.                     .. "\n" .. DEC_HEX(getIdent(currPtr))
  283.                     .. "\t" .. structName
  284.                     .. "\n"
  285.                 continue = false
  286.             end
  287.             if continue then
  288.                 newStr = newStr
  289.                     .. DEC_HEX(currPtr,8)
  290.                     .. (
  291.                         (getEnemyInvuln(currPtr) > 0 and (" - Invuln: " .. getEnemyInvuln(currPtr)))
  292.                         or (" - HP: " .. getEnemyCurrHp(currPtr))
  293.                         )
  294.                     .. "\n" .. DEC_HEX(getIdent(currPtr))
  295.                     .. "\t" .. structName
  296.                     .. "\n"
  297.                 continue = false
  298.             end
  299.         else
  300.             break
  301.         end
  302.         count = count + 1
  303.     end
  304.     if count > 0 then
  305.         printStr = printStr .. "Object Count : " .. count .. "\n" .. newStr .. "\n"
  306.     end
  307.     -- Return Result
  308.     return printStr
  309. end
  310.  
  311. function Data_Draw()
  312.     gui.text(2,2,Data_Update_Bottom())
  313.     gui.text(2,-190,Data_Update_Top())
  314. end
  315.  
  316. gui.register(Data_Draw)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement