Advertisement
Guest User

Crash HA speed hud v2.2

a guest
Mar 10th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. --Position and speed hud values v2.2 by ThunderAxe31 for GBA Crash Bandicoot - The Huge Adventure
  2. --If the script encounters a speed amount that goes over the known limit, it will show it in blue. Otherwise:
  3. local x_max    =  1224 --initial slide speed
  4. local x_middle =   512 --target walking speed
  5. local y_max    =  1536 --highest falling speed
  6. local y_low    = -1229 --initial jumping speed
  7.  
  8. local player_x_addr = 0x010A14
  9. local player_y_addr = 0x010A18
  10. local framecount = -1
  11. local player_x = 0
  12. local player_x_old = 0
  13. local speed_x = 0
  14. local player_y = 0
  15. local player_y_old = 0
  16. local speed_y = 0
  17. local speed_x_list = {}
  18. local speed_y_list = {}
  19. local pos_x_all = ""
  20. local pos_y_all = ""
  21.  
  22. function decide_color_horizontal(speed, middle, maxi)
  23.     if speed == nil then
  24.         return
  25.     end
  26.    
  27.     speed = math.abs(speed)
  28.     maxi = math.abs(maxi)
  29.    
  30.     if speed > maxi then --in case we broke the limit, use blue color
  31.         color = 0x000000FF
  32.     elseif speed < middle then --if 0: red; if middle: white
  33.         local value = math.floor(0xFF*(speed/middle))
  34.         color = 0xFF0000 + value*0x101
  35.     else --if middle: white; if maxi: green
  36.         local value = math.floor(0xFF*(1-(speed-middle)/(maxi-middle)))
  37.         color = 0x00FF00 + value*0x10001
  38.     end
  39.     return color+0xFF000000
  40. end
  41.  
  42. function decide_color_vertical(speed, low, maxi)
  43.     if speed == nil then
  44.         return
  45.     end
  46.    
  47.     speed = tonumber(speed)
  48.    
  49.     if speed > y_max or speed < y_low then --in case we broke the limit, use blue color
  50.         color = 0x000000FF
  51.     elseif speed >= 0 then -- if 0: white; if low: green
  52.         local value = math.floor(0xFF*(1-speed/maxi))
  53.         color = 0x00FF00 + value*0x10001
  54.     else -- if 0: white; if maxi: green
  55.         local value = math.floor(0xFF*(1-(speed*-1)/(low*-1)))
  56.         color = 0x00FF00 + value*0x10001
  57.     end
  58.     return color+0xFF000000
  59. end
  60.  
  61. if memory.usememorydomain("EWRAM") then
  62. --  console.log("Starting script at frame: " .. emu.framecount() .. " with X: " .. player_x .. ", Y: " .. player_y)
  63.    
  64.     while true do
  65.         if framecount ~= emu.framecount()-1 then
  66.             pos_x_all = ""
  67.             pos_y_all = ""
  68.             speed_x_list = {}
  69.             speed_y_list = {}
  70.         end
  71.        
  72.         player_x_old = player_x
  73.         player_x = memory.read_u32_le(player_x_addr)
  74.         player_y_old = player_y
  75.         player_y = memory.read_u32_le(player_y_addr)
  76.        
  77.         pos_x_all = player_x .. "\n" .. pos_x_all
  78.         pos_y_all = player_y .. "\n" .. pos_y_all
  79.        
  80.         speed_x = player_x -player_x_old
  81.         speed_y = player_y -player_y_old
  82.        
  83.         for i=21, 1, -1 do
  84.             speed_x_list[i]=speed_x_list[i-1]
  85.             speed_y_list[i]=speed_y_list[i-1]
  86.            
  87.             gui.pixelText( 33, 7+i*7, speed_x_list[i], decide_color_horizontal(speed_x_list[i], x_middle, x_max))
  88.             gui.pixelText(219, 7+i*7, speed_y_list[i], decide_color_vertical(speed_y_list[i], y_low, y_max))
  89.         end
  90.        
  91.         last_found = 0
  92.         occurences = 0
  93.         while last_found ~= nil do
  94.             occurences = occurences + 1
  95.             if occurences > 22 then
  96.                 pos_x_all = string.sub(pos_x_all, 0, last_found)
  97.             end
  98.             last_found = string.find(pos_x_all, "\n", last_found+1)
  99.         end
  100.        
  101.         last_found = 0
  102.         occurences = 0
  103.         while last_found ~= nil do
  104.             occurences = occurences + 1
  105.             if occurences > 22 then
  106.                 pos_y_all = string.sub(pos_y_all, 0, last_found)
  107.             end
  108.             last_found = string.find(pos_y_all, "\n", last_found+1)
  109.         end
  110.        
  111.         if framecount == emu.framecount()-1 then
  112.             speed_x_list[0] = speed_x
  113.             speed_y_list[0] = speed_y
  114.         end
  115.        
  116.         gui.pixelText(  0, 0, "X Pos   Speed")
  117.         gui.pixelText(187, 0, "Y Pos   Speed")
  118.         gui.pixelText(  0, 7, pos_x_all)
  119.         gui.pixelText(186, 7, pos_y_all)
  120.         gui.pixelText( 33, 7, speed_x_list[0], decide_color_horizontal(speed_x_list[0], x_middle, x_max))
  121.         gui.pixelText(219, 7, speed_y_list[0], decide_color_vertical(speed_y_list[0], y_low, y_max))
  122.        
  123.         framecount = emu.framecount()
  124.        
  125.         emu.frameadvance()
  126.     end
  127. else
  128.     console.log("Error: failed to set EWRAM memory domain")
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement