Advertisement
Nightfyre

AzureSets

Jul 10th, 2016
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.73 KB | None | 0 0
  1. --[[
  2. Copyright (c) 2013, Ricky Gall
  3. All rights reserved.
  4.  
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7.  
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * Neither the name of azureSets nor the
  14. names of its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16.  
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL The Addon's Contributors BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. ]]
  28.  
  29.  
  30. _addon.name = 'AzureSets'
  31. _addon.version = '1.25'
  32. _addon.author = 'Nitrous (Shiva)'
  33. _addon.commands = {'aset','azuresets','asets'}
  34.  
  35. require('tables')
  36. require('strings')
  37. require('logger')
  38. config = require('config')
  39. files = require('files')
  40. res = require('resources')
  41. chat = require('chat')
  42.  
  43. defaults = {}
  44. defaults.setmode = 'PreserveTraits'
  45. defaults.setspeed = 0.65
  46. defaults.spellsets = {}
  47. defaults.spellsets.default = T{}
  48. defaults.spellsets.vw1 = T{slot01='Firespit', slot02='Heat Breath', slot03='Thermal Pulse', slot04='Blastbomb',
  49. slot05='Infrasonics', slot06='Frost Breath', slot07='Ice Break', slot08='Cold Wave',
  50. slot09='Sandspin', slot10='Magnetite Cloud', slot11='Cimicine Discharge', slot12='Bad Breath',
  51. slot13='Acrid Stream', slot14='Maelstrom', slot15='Corrosive Ooze', slot16='Cursed Sphere',
  52. slot17='Awful Eye'
  53. }
  54. defaults.spellsets.vw2 = T{slot01='Hecatomb Wave', slot02='Mysterious Light', slot03='Leafstorm', slot04='Reaving Wind',
  55. slot05='Temporal Shift', slot06='Mind Blast', slot07='Blitzstrahl', slot08='Charged Whisker',
  56. slot09='Blank Gaze', slot10='Radiant Breath', slot11='Light of Penance', slot12='Actinic Burst',
  57. slot13='Death Ray', slot14='Eyes On Me', slot15='Sandspray'
  58. }
  59.  
  60. settings = config.load(defaults)
  61. config.save(settings)
  62.  
  63. modes = {traits = "PreserveTraits", slots = "PreserveSlots", clear = "ClearFirst"}
  64.  
  65. function initialize()
  66.     spells = res.spells:type('BlueMagic')
  67.     get_current_spellset()
  68. end
  69.  
  70. windower.register_event('load', initialize:cond(function() return windower.ffxi.get_info().logged_in end))
  71.  
  72. windower.register_event('login', initialize)
  73.  
  74. windower.register_event('job change', initialize:cond(function(job) return job == 16 end))
  75.  
  76. function set_spells(spellset, setmode)
  77.     if windower.ffxi.get_player()['main_job_id'] ~= 16 --[[and windower.ffxi.get_player()['sub_job_id'] ~= 16]] then
  78.         error('Main job not set to Blue Mage.')
  79.         return
  80.     end
  81.     if settings.spellsets[spellset] == nil then
  82.         error('Set not defined: '..spellset)
  83.         return
  84.     end
  85.     if is_spellset_equipped(settings.spellsets[spellset]) then
  86.         log(spellset..' was already equipped.')
  87.         return
  88.     end
  89.  
  90.     log('Starting to set '..spellset..'.')
  91.     if setmode:lower() == 'clearfirst' then
  92.         remove_all_spells()
  93.         set_spells_from_spellset:schedule(settings.setspeed, spellset, 'add')
  94.     elseif setmode:lower() == 'preservetraits' or setmode:lower() == 'preserveslots' then
  95.         set_spells_from_spellset(spellset, 'remove')
  96.     else
  97.         error('Unexpected setmode: '..setmode)
  98.     end
  99. end
  100.  
  101. function is_spellset_equipped(spellset)
  102.     return S(spellset):map(string.lower) == S(get_current_spellset())
  103. end
  104.  
  105. function set_spells_from_spellset(spellset, setPhase)
  106.     local setToSet = settings.spellsets[spellset]
  107.     local currentSet = get_current_spellset()
  108.     local clearSlots = S{}
  109.    
  110.     for k,v in pairs(setToSet) do
  111.         setToSet[k] = v:lower()
  112.     end
  113.  
  114.     if setPhase == 'remove' then
  115.         -- Remove Phase
  116.         for k,v in pairs(currentSet) do
  117.             if not setToSet:contains(v:lower()) or (settings.setmode:lower() == 'preserveslots' and (not setToSet[k] or setToSet[k]:lower() ~= v:lower())) then
  118.                 local setSlot = k
  119.                 local slotToRemove = tonumber(k:sub(5, k:len()))
  120.  
  121.                 windower.ffxi.remove_blue_magic_spell(slotToRemove)
  122.                 --log('Removed spell: '..v..' at #'..slotToRemove)
  123.                 set_spells_from_spellset:schedule(settings.setspeed, spellset, 'remove')
  124.                 return
  125.             end
  126.         end
  127.     end
  128.  
  129.     -- Did not find spell to remove. Start set phase
  130.     -- Find empty slot:
  131.     local slotToSetTo
  132.     for i = 1, 20 do
  133.         local slotName = 'slot%02u':format(i)
  134.         if currentSet[slotName] == nil and not (settings.setmode:lower() == 'preserveslots' and not setToSet[slotName]) then
  135.             slotToSetTo = i
  136.             break
  137.         end
  138.     end
  139.  
  140.     if slotToSetTo ~= nil then
  141.         -- We found an empty slot. Find a spell to set.
  142.         for i=1,20 do
  143.             local slotName = 'slot%02u':format(i)
  144.             if setToSet[slotName] then
  145.                 if not currentSet:contains(setToSet[slotName]:lower()) then
  146.                     if setToSet[slotName] ~= nil then
  147.                         local spellID = find_spell_id_by_name(setToSet[slotName])
  148.                         if spellID ~= nil then
  149.                             windower.ffxi.set_blue_magic_spell(spellID, tonumber(slotToSetTo))
  150.                             set_spells_from_spellset:schedule(settings.setspeed, spellset, 'add')
  151.                             return
  152.                         end
  153.                     end
  154.                 end
  155.             end
  156.         end
  157.     end
  158.  
  159.     -- Unable to find any spells to set. Must be complete.
  160.     log(spellset..' has been equipped.')
  161.     windower.send_command('@timers c "Blue Magic Cooldown" 60 up')
  162. end
  163.  
  164. function find_spell_id_by_name(spellname)
  165.     for spell in spells:it() do
  166.         if spell['english']:lower() == spellname:lower() then
  167.             return spell['id']
  168.         end
  169.     end
  170.     return nil
  171. end
  172.  
  173. function set_single_spell(setspell,slot)
  174.     if windower.ffxi.get_player()['main_job_id'] ~= 16 --[[and windower.ffxi.get_player()['sub_job_id'] ~= 16]] then return nil end
  175.  
  176.     local tmpTable = T(get_current_spellset())
  177.     for key,val in pairs(tmpTable) do
  178.         if tmpTable[key]:lower() == setspell then
  179.             error('That spell is already set.')
  180.             return
  181.         end
  182.     end
  183.     if tonumber(slot) < 10 then slot = '0'..slot end
  184.     --insert spell add code here
  185.     for spell in spells:it() do
  186.         if spell['english']:lower() == setspell then
  187.             --This is where single spell setting code goes.
  188.             --Need to set by spell id rather than name.
  189.             windower.ffxi.set_blue_magic_spell(spell['id'], tonumber(slot))
  190.             windower.send_command('@timers c "Blue Magic Cooldown" 60 up')
  191.             tmpTable['slot'..slot] = setspell
  192.         end
  193.     end
  194.     tmpTable = nil
  195. end
  196.  
  197. function get_current_spellset()
  198.     if windower.ffxi.get_player().main_job_id ~= 16 then return nil end
  199.     return T(windower.ffxi.get_mjob_data().spells)
  200.     -- Returns all values but 512
  201.     :filter(function(id) return id ~= 512 end)
  202.     -- Transforms them from IDs to lowercase English names
  203.     :map(function(id) return spells[id].english:lower() end)
  204.     -- Transform the keys from numeric x or xx to string 'slot0x' or 'slotxx'
  205.     :key_map(function(slot) return 'slot%02u':format(slot) end)
  206. end
  207.  
  208. function remove_all_spells(trigger)
  209.     windower.ffxi.reset_blue_magic_spells()
  210.     notice('All spells removed.')
  211. end
  212.  
  213. function save_set(setname)
  214.     if setname == 'default' then
  215.         error('Please choose a name other than default.')
  216.         return
  217.     end
  218.     local curSpells = T(get_current_spellset())
  219.     settings.spellsets[setname] = curSpells
  220.     settings:save('all')
  221.     notice('Set '..setname..' saved.')
  222. end
  223.  
  224. function get_spellset_list()
  225.     log("Listing sets:")
  226.     for key,_ in pairs(settings.spellsets) do
  227.         if key ~= 'default' then
  228.             local it = 0
  229.             for i = 1, #settings.spellsets[key] do
  230.                 it = it + 1
  231.             end
  232.             log("\t"..key..' '..settings.spellsets[key]:length()..' spells.')
  233.         end
  234.     end
  235. end
  236.  
  237. function get_spellset_content(spellset)
  238.     log('Getting '..spellset..'\'s spell list:')
  239.     settings.spellsets[spellset]:print()
  240. end
  241.  
  242. windower.register_event('addon command', function(...)
  243.     if windower.ffxi.get_player()['main_job_id'] ~= 16 --[[and windower.ffxi.get_player()['sub_job_id'] ~= 16]] then
  244.         error('You are not on (main) Blue Mage.')
  245.         return nil
  246.     end
  247.     local args = T{...}
  248.     if args ~= nil then
  249.         local comm = table.remove(args,1):lower()
  250.         if comm == 'removeall' then
  251.             remove_all_spells('trigger')
  252.         elseif comm == 'add' then
  253.             if args[2] ~= nil then
  254.                 local slot = table.remove(args,1)
  255.                 local spell = args:sconcat()
  256.                 set_single_spell(spell:lower(),slot)
  257.             end
  258.         elseif comm == 'save' then
  259.             if args[1] ~= nil then
  260.                 save_set(args[1])
  261.             end
  262.  
  263.         elseif comm == 'spellset' or comm == 'set' then
  264.             if args[1] ~= nil then
  265.                 set_spells(args[1], args[2] or settings.setmode)
  266.             end
  267.         elseif comm == 'mode' then
  268.             if args[1] and modes[args[1]] then
  269.                 settings.setmode = modes[args[1]]
  270.             elseif args[1] then
  271.                 log('Mode not found. Valid modes: traits | slots | clear')
  272.                 return
  273.             elseif settings.setmode == "PreserveTraits" then
  274.                 settings.setmode = "ClearFirst"
  275.             elseif settings.setmode == "ClearFirst" then
  276.                 settings.setmode = "PreserveSlots"
  277.             else
  278.                 settings.setmode = "PreserveTraits"
  279.             end
  280.             log('Mode changed to '..settings.setmode)
  281.         elseif comm == 'currentlist' then
  282.             get_current_spellset():print()
  283.         elseif comm == 'setlist' then
  284.             get_spellset_list()
  285.         elseif comm == 'spelllist' then
  286.             if args[1] ~= nil then
  287.                 get_spellset_content(args[1])
  288.             end
  289.         elseif comm == 'help' then
  290.             local helptext = [[AzureSets - Command List:')
  291.            1. removeall - Unsets all spells.
  292.            2. spellset <setname> [ClearFirst|PreserveSlots|PreserveTraits] -- Set (setname)'s spells,
  293.                      optional parameter: ClearFirst or PreserveSlots or PreserveTraits:
  294.                      overrides setting to clear spells first or remove individually,
  295.                      preserving matching slots or traits where possible.
  296.                      Default: use settings or preservetraits if settings not configured.
  297.             3. set <setname> (clearfirst|preserveslots|preservetraits) -- Same as spellset
  298.             4. mode [ClearFirst|PreserveSlots|PreserveTraits] - cycle settings for set parameters,
  299.                     optional parameter: ClearFirst or PreserveSlots or PreserveTraits
  300.             5. add <slot> <spell> -- Set (spell) to slot (slot (number)).
  301.             6. save <setname> -- Saves current spellset as (setname).
  302.             7. currentlist -- Lists currently set spells.
  303.             8. setlist -- Lists all spellsets.
  304.             9. spelllist <setname> -- List spells in (setname)
  305.             10. help --Shows this menu.]]
  306.             for _, line in ipairs(helptext:split('\n')) do
  307.                 windower.add_to_chat(207, line..chat.controls.reset)
  308.             end
  309.         end
  310.     end
  311. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement