Advertisement
Guest User

DKKoS speed hud v1.1

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