Advertisement
Guest User

terranigma_exp_timer.lua

a guest
Jun 27th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. -- Convert EXP info from HEX 2 DEC
  2. local function loadExp(address)
  3.   local exp = 0
  4.   for n=0,2 do
  5.     local expByte = memory.readbyte(address+n)
  6.     exp = exp + (AND(expByte,0xf) + AND(expByte,0xf0)/0x10*10) * math.pow(100,n)
  7.   end
  8.   return exp
  9. end
  10.  
  11. -- Detect ROM version
  12. local expTable = 0;
  13. local fps = 60;
  14. local expTableEU = 0x8db92a
  15. local expTableJPN = 0x8dba61--0x8db92a--
  16. if loadExp(expTableEU + 11*49) == 802435 then
  17.     -- European ROM
  18.     expTable = expTableEU
  19.     fps = 50
  20. elseif loadExp(expTableJPN + 11*49) == 802435 then
  21.     -- Japanese ROM
  22.     expTable = expTableJPN
  23.     fps = 60
  24. else
  25.     error(string.format("Could not detect valid Terranigma ROM: %s",loadExp(expTableJPN + 11*1)))
  26. end
  27.  
  28. -- Create exp timer
  29.  
  30. --exp.xpos      = 92
  31. --exp.ypos      = 6
  32. local boxcolor          = 0x00000080
  33. local activetextcolor   = 0xFFFFFFFF
  34. local inactivetextcolor = 0x808080FF
  35.  
  36. local timerIsActive = false
  37. local startFrame    = 0
  38. local totalFrames   = 0
  39. local startExp      = 0
  40. local totalExp      = 0
  41. local previousFrame = 0
  42. local levelUp        = false
  43. local lostControl    = false
  44.  
  45. local smallHeight = 10
  46. local fullHeight = 2+8*4
  47.  
  48. local currentHeight = fullHeight
  49. local targetHeight = fullHeight
  50.  
  51. local xpos = 92
  52. local ypos = 2
  53.  
  54. local hidden = false
  55.  
  56. -- Function to render the timer
  57. local function my_display()
  58.  
  59.   if(currentHeight < targetHeight) then
  60.     currentHeight = currentHeight + 1
  61.   elseif(currentHeight > targetHeight) then
  62.     currentHeight = currentHeight - 1
  63.   end
  64.  
  65.   local level      = memory.readbyte(0x7e0656)
  66.   local currentExp = loadExp(0x7e0690)
  67.   local targetExp  = loadExp(expTable + 11*level)
  68.   local currentFrame = emu.framecount()
  69.  
  70.   if(currentExp>targetExp and not levelUp) then
  71.     levelUp         = true
  72.     lostControl     = false
  73.   end
  74.  
  75.   if levelUp then
  76.     if lostControl then
  77.       lostControl = memory.readbyte(0x7e097d)==0x80
  78.       levelUp = lostControl
  79.       if levelUp and timerIsActive and not (currentFrame==previousFrame) then
  80.         startFrame = startFrame + 1
  81.       end
  82.     else
  83.       lostControl = memory.readbyte(0x7e097d)==0x80
  84.     end
  85.   end
  86.  
  87.   if timerIsActive then
  88.     totalExp    = currentExp  -startExp
  89.     totalFrames = currentFrame-startFrame
  90.   end
  91.  
  92.   local expSpeed
  93.   if totalFrames==0 then
  94.     expSpeed = 0
  95.   else
  96.     expSpeed = totalExp/totalFrames*fps
  97.   end
  98.  
  99.   if hidden then
  100.     return
  101.   end
  102.  
  103.   gui.box(xpos,ypos,xpos+(17+1)*4,ypos+currentHeight,boxcolor)
  104.  
  105.   gui.text(xpos+3,ypos+2+8*0,string.format("EXP %13s", string.format("%i / %i",currentExp,targetExp)),textcolor)
  106.  
  107.   local timerTextColor
  108.   if timerIsActive then timerTextColor = activetextcolor else timerTextColor = inactivetextcolor end
  109.  
  110.   if currentHeight>=16 then
  111.     gui.text(xpos+3,ypos+2+8*1,string.format("GAIN %12s", string.format("%6i exp",totalExp)),timerTextColor)
  112.   end
  113.   if currentHeight>=24 then
  114.     gui.text(xpos+3,ypos+2+8*2,string.format("TIME %12s", string.format("%6.1f sec",totalFrames/50)),timerTextColor)
  115.   end
  116.   if currentHeight>=32 then
  117.     gui.text(xpos+3,ypos+2+8*3,string.format("SPEED %11s",string.format("%6.1f x/s",expSpeed)),timerTextColor)
  118.   end
  119.  
  120.   previousFrame = currentFrame
  121. end
  122.  
  123. -- Hook the display function to the GUI update
  124. local on_gui_update_old = gui.register()
  125. local function on_gui_update_new()
  126.   if on_gui_update_old then
  127.     on_gui_update_old()
  128.   end
  129.   my_display()
  130. end
  131. gui.register(on_gui_update_new)
  132.  
  133. -- Public functions to control the timer
  134. exp_timer = {}
  135.  
  136. function exp_timer.show() hidden = false end
  137. function exp_timer.hide() hidden = true  end
  138.  
  139. function exp_timer.start()
  140.   totalExp      = 0
  141.   startExp      = loadExp(0x7e0690)
  142.   startFrame    = emu.framecount()
  143.   totalFrames   = 0
  144.   timerIsActive = true
  145. end
  146.  
  147. function exp_timer.stop()
  148.   timerIsActive = false
  149. end
  150.  
  151. function exp_timer.toggle()
  152.   if timerIsActive then
  153.     exp_timer.stop()
  154.   else
  155.     exp_timer.start()
  156.   end
  157. end
  158.  
  159. function exp_timer.clear()
  160.   totalExp    = 0
  161.   totalFrames = 0
  162. end
  163.  
  164. function exp_timer.collapse()
  165.   targetHeight = smallHeight
  166. end
  167.  
  168. function exp_timer.expand()
  169.   targetHeight = fullHeight
  170. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement