Advertisement
Motenten

Rune tracking

Sep 3rd, 2014
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.51 KB | None | 0 0
  1. ------------------------------------------------------------------
  2. -- Initialization
  3. ------------------------------------------------------------------
  4.  
  5. function get_sets()
  6.     -- Table of entries
  7.     rune_timers = T{}
  8.     -- entry = rune, index, expires
  9.    
  10.     if player.main_job_level >= 65 then
  11.         max_runes = 3
  12.     elseif player.main_job_level >= 35 then
  13.         max_runes = 2
  14.     elseif player.main_job_level >= 5 then
  15.         max_runes = 1
  16.     else
  17.         max_runes = 0
  18.     end
  19. end
  20.  
  21. ------------------------------------------------------------------
  22. -- Action events
  23. ------------------------------------------------------------------
  24.  
  25. function aftercast(spell)
  26.     if not spell.interrupted then
  27.         if spell.type == 'Rune' then
  28.             update_timers(spell)
  29.         elseif spell.name == "Lunge" or spell.name == "Gambit" or spell.name == "Rayke" then
  30.             reset_timers()
  31.         elseif spell.name == "Swipe" then
  32.             send_command(trim(1))
  33.         end
  34.     end
  35. end
  36.  
  37. ------------------------------------------------------------------
  38. -- Timer manipulation
  39. ------------------------------------------------------------------
  40.  
  41. -- Add a new run to the custom timers, and update index values for existing timers.
  42. function update_timers(spell)
  43.     local expires_time = os.time() + 300
  44.     local entry_index = rune_count(spell.name) + 1
  45.  
  46.     local entry = {rune=spell.name, index=entry_index, expires=expires_time}
  47.  
  48.     rune_timers:append(entry)
  49.     local cmd_queue = create_timer(entry).. ';wait 0.05;'
  50.    
  51.     cmd_queue = cmd_queue .. trim()
  52.  
  53.     --add_to_chat(123,'cmd_queue='..cmd_queue)
  54.  
  55.     send_command(cmd_queue)
  56. end
  57.  
  58. -- Get the command string to create a custom timer for the provided entry.
  59. function create_timer(entry)
  60.     local timer_name = '"Rune: ' .. entry.rune .. '-' .. tostring(entry.index) .. '"'
  61.     local duration = entry.expires - os.time()
  62.     return 'timers c ' .. timer_name .. ' ' .. tostring(duration) .. ' down'
  63. end
  64.  
  65. -- Get the command string to delete a custom timer for the provided entry.
  66. function delete_timer(entry)
  67.     local timer_name = '"Rune: ' .. entry.rune .. '-' .. tostring(entry.index) .. '"'
  68.     return 'timers d ' .. timer_name .. ''
  69. end
  70.  
  71. -- Reset all timers
  72. function reset_timers()
  73.     local cmd_queue = ''
  74.     for index,entry in pairs(rune_timers) do
  75.         cmd_queue = cmd_queue .. delete_timer(entry) .. ';wait 0.05;'
  76.     end
  77.     rune_timers:clear()
  78.     send_command(cmd_queue)
  79. end
  80.  
  81. -- Get a count of the number of runes of a given type
  82. function rune_count(rune)
  83.     local count = 0
  84.     for _,entry in pairs(rune_timers) do
  85.         if entry.rune == rune then
  86.             count = count + 1
  87.         end
  88.     end
  89.     return count
  90. end
  91.  
  92. -- Remove the oldest rune(s) from the table, until we're below the max_runes limit.
  93. -- If given a value n, remove n runes from the table.
  94. function trim(n)
  95.     local cmd_queue = ''
  96.  
  97.     local to_remove = n or (rune_timers:length() - max_runes)
  98.  
  99.     while to_remove > 0 and rune_timers:length() > 0 do
  100.         local oldest
  101.         for index,entry in pairs(rune_timers) do
  102.             if oldest == nil or entry.expires < rune_timers[oldest].expires then
  103.                 oldest = index
  104.             end
  105.         end
  106.        
  107.         cmd_queue = cmd_queue .. prune(rune_timers[oldest].rune)
  108.         to_remove = to_remove - 1
  109.     end
  110.    
  111.     return cmd_queue
  112. end
  113.  
  114. -- Drop the index of all runes of a given type.
  115. -- If the index drops to 0, it is removed from the table.
  116. function prune(rune)
  117.     local cmd_queue = ''
  118.    
  119.     for index,entry in pairs(rune_timers) do
  120.         if entry.rune == rune then
  121.             cmd_queue = cmd_queue .. delete_timer(entry) .. ';wait 0.05;'
  122.             entry.index = entry.index - 1
  123.         end
  124.     end
  125.  
  126.     for index,entry in pairs(rune_timers) do
  127.         if entry.rune == rune then
  128.             if entry.index == 0 then
  129.                 rune_timers[index] = nil
  130.             else
  131.                 cmd_queue = cmd_queue .. create_timer(entry) .. ';wait 0.05;'
  132.             end
  133.         end
  134.     end
  135.    
  136.     return cmd_queue
  137. end
  138.  
  139. ------------------------------------------------------------------
  140. -- Reset events
  141. ------------------------------------------------------------------
  142.  
  143. windower.raw_register_event('zone change',reset_timers)
  144. windower.raw_register_event('logout',reset_timers)
  145. windower.raw_register_event('status change',function(new, old)
  146.     if gearswap.res.statuses[new].english == 'Dead' then
  147.         reset_timers()
  148.     end
  149. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement