vm0d

Mote-Include

Apr 25th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 39.03 KB | None | 0 0
  1. -------------------------------------------------------------------------------------------------------------------
  2. -- Common variables and functions to be included in job scripts, for general default handling.
  3. --
  4. -- Include this file in the get_sets() function with the command:
  5. -- include('Mote-Include.lua')
  6. --
  7. -- It will then automatically run its own init_include() function.
  8. --
  9. -- IMPORTANT: This include requires supporting include files:
  10. -- Mote-Utility
  11. -- Mote-Mappings
  12. -- Mote-SelfCommands
  13. -- Mote-Globals
  14. --
  15. -- Place the include() directive at the start of a job's get_sets() function.
  16. --
  17. -- Included variables and functions are considered to be at the same scope level as
  18. -- the job script itself, and can be used as such.
  19. -- Modified JUN 17 2018
  20. -------------------------------------------------------------------------------------------------------------------
  21.  
  22. -------------------------------------------------------------------------------------------------------------------
  23. -- Initialization function that defines variables to be used.
  24. -- These are accessible at the including job lua script's scope.
  25. --
  26. -- Auto-initialize after defining this function.
  27. -------------------------------------------------------------------------------------------------------------------
  28.  
  29. current_mote_include_version = 2
  30.  
  31. function init_include()
  32.     -- Used to define various types of data mappings.  These may be used in the initialization, so load it up front.
  33.     include('Mote-Mappings')
  34.  
  35.     -- Modes is the include for a mode-tracking variable class.  Used for state vars, below.
  36.     include('Modes')
  37.  
  38.     -- Var for tracking state values
  39.     state = {}
  40.  
  41.     -- General melee offense/defense modes, allowing for hybrid set builds, as well as idle/resting/weaponskill.
  42.     -- This just defines the vars and sets the descriptions.  List modes with no values automatically get assigned a 'Normal' default value.
  43.     state.OffenseMode           = M{['description'] = 'Offense Mode'}
  44.     state.HybridMode            = M{['description'] = 'Hybrid Mode'}
  45.     state.RangedMode            = M{['description'] = 'Ranged Mode'}
  46.     state.WeaponskillMode       = M{['description'] = 'Weaponskill Mode'}
  47.     state.CastingMode           = M{['description'] = 'Casting Mode'}
  48.     state.IdleMode              = M{['description'] = 'Idle Mode'}
  49.     state.RestingMode           = M{['description'] = 'Resting Mode'}
  50.  
  51.     state.DefenseMode           = M{['description'] = 'Defense Mode', 'None', 'Physical', 'Magical'}
  52.     state.PhysicalDefenseMode   = M{['description'] = 'Physical Defense Mode', 'PDT'}
  53.     state.MagicalDefenseMode    = M{['description'] = 'Magical Defense Mode', 'MDT'}
  54.  
  55.     state.CP                    = M(false, 'CP')
  56.     state.Kiting                = M(false, 'Kiting')
  57.     state.SelectNPCTargets      = M(false, 'Select NPC Targets')
  58.     state.PCTargetMode          = M{['description'] = 'PC Target Mode', 'default', 'stpt', 'stal', 'stpc'}
  59.  
  60.     state.EquipStop             = M{['description'] = 'Stop Equipping Gear', 'off', 'precast', 'midcast', 'pet_midcast'}
  61.  
  62.     state.CombatWeapon          = M{['description']='Combat Weapon', ['string']=''}
  63.     state.CombatForm            = M{['description']='Combat Form', ['string']=''}
  64.  
  65.     -- Non-mode vars that are used for state tracking.
  66.     state.MaxWeaponskillDistance = 0
  67.     state.Buff = {}
  68.  
  69.     -- Classes describe a 'type' of action.  They are similar to state, but
  70.     -- may have any free-form value, or describe an entire table of mapped values.
  71.     classes = {}
  72.     -- Basic spell mappings are based on common spell series.
  73.     -- EG: 'Cure' for Cure, Cure II, Cure III, Cure IV, Cure V, or Cure VI.
  74.     classes.SpellMaps = spell_maps
  75.     -- List of spells and spell maps that don't benefit from greater skill (though
  76.     -- they may benefit from spell-specific augments, such as improved regen or refresh).
  77.     -- Spells that fall under this category will be skipped when searching for
  78.     -- spell.skill sets.
  79.     classes.NoSkillSpells = no_skill_spells_list
  80.     classes.SkipSkillCheck = false
  81.     -- Custom, job-defined class, like the generic spell mappings.
  82.     -- Takes precedence over default spell maps.
  83.     -- Is reset at the end of each spell casting cycle (ie: at the end of aftercast).
  84.     classes.JAMode = nil
  85.     classes.CustomClass = nil
  86.     -- Custom groups used for defining melee and idle sets.  Persists long-term.
  87.     classes.CustomMeleeGroups = L{}
  88.     classes.CustomRangedGroups = L{}
  89.     classes.CustomIdleGroups = L{}
  90.     classes.CustomDefenseGroups = L{}
  91.  
  92.     -- Class variables for time-based flags
  93.     classes.Daytime = false
  94.     classes.DuskToDawn = false
  95.  
  96.  
  97.     -- Var for tracking misc info
  98.     info = {}
  99.     options = {}
  100.  
  101.     -- Special control flags.
  102.     mote_vars = {}
  103.     mote_vars.set_breadcrumbs = L{}
  104.     mote_vars.res_buffs = S{}
  105.     for index,struct in pairs(gearswap.res.buffs) do
  106.         mote_vars.res_buffs:add(struct.en)
  107.     end
  108.  
  109.     -- Sub-tables within the sets table that we expect to exist, and are annoying to have to
  110.     -- define within each individual job file.  We can define them here to make sure we don't
  111.     -- have to check for existence.  The job file should be including this before defining
  112.     -- any sets, so any changes it makes will override these anyway.
  113.     sets.precast = {}
  114.     sets.precast.FC = {}
  115.     sets.precast.JA = {}
  116.     sets.precast.WS = {}
  117.     sets.precast.RA = {}
  118.     sets.midcast = {}
  119.     sets.midcast.RA = {}
  120.     sets.midcast.Pet = {}
  121.     sets.idle = {}
  122.     sets.resting = {}
  123.     sets.engaged = {}
  124.     sets.defense = {}
  125.     sets.buff = {}
  126.  
  127.     gear = {}
  128.     gear.default = {}
  129.  
  130.     gear.ElementalGorget = {name=""}
  131.     gear.ElementalBelt = {name=""}
  132.     gear.ElementalObi = {name=""}
  133.     gear.ElementalCape = {name=""}
  134.     gear.ElementalRing = {name=""}
  135.     gear.FastcastStaff = {name=""}
  136.     gear.RecastStaff = {name=""}
  137.  
  138.  
  139.     -- Load externally-defined information (info that we don't want to change every time this file is updated).
  140.  
  141.     -- Used to define misc utility functions that may be useful for this include or any job files.
  142.     include('Mote-Utility')
  143.  
  144.     -- Used for all self-command handling.
  145.     include('Mote-SelfCommands')
  146.  
  147.     -- Include general user globals, such as custom binds or gear tables.
  148.     -- Load Mote-Globals first, followed by User-Globals, followed by <character>-Globals.
  149.     -- Any functions re-defined in the later includes will overwrite the earlier versions.
  150.     include('Mote-Globals')
  151.     optional_include({'user-globals.lua'})
  152.     optional_include({player.name..'-globals.lua'})
  153.  
  154.     -- *-globals.lua may define additional sets to be added to the local ones.
  155.     if define_global_sets then
  156.         define_global_sets()
  157.     end
  158.  
  159.     -- Global default binds (either from Mote-Globals or user-globals)
  160.     (binds_on_load or global_on_load)()
  161.  
  162.     -- Load a sidecar file for the job (if it exists) that may re-define init_gear_sets and file_unload.
  163.     load_sidecar(player.main_job)
  164.  
  165.     -- General var initialization and setup.
  166.     if job_setup then
  167.         job_setup()
  168.     end
  169.  
  170.     -- User-specific var initialization and setup.
  171.     if user_setup then
  172.         user_setup()
  173.     end
  174.  
  175.     -- Load up all the gear sets.
  176.     init_gear_sets()
  177. end
  178.  
  179. if not mote_include_version or mote_include_version < current_mote_include_version then
  180.     add_to_chat(123,'Warning: Your job file is out of date.  Please update to the latest repository baseline.')
  181.     add_to_chat(123,'For details, visit https://github.com/Kinematics/GearSwap-Jobs/wiki/Upgrading')
  182.     rev = mote_include_version or 1
  183.     include_path('rev' .. tostring(rev))
  184.     include('Mote-Include')
  185.     return
  186. end
  187.  
  188. -- Auto-initialize the include
  189. init_include()
  190.  
  191. -- Called when this job file is unloaded (eg: job change)
  192. -- Conditional definition so that it doesn't overwrite explicit user
  193. -- versions of this function.
  194. if not file_unload then
  195.     file_unload = function()
  196.         if user_unload then
  197.             user_unload()
  198.         elseif job_file_unload then
  199.             job_file_unload()
  200.         end
  201.         _G[(binds_on_unload and 'binds_on_unload') or 'global_on_unload']()
  202.     end
  203. end
  204.  
  205.  
  206. -------------------------------------------------------------------------------------------------------------------
  207. -- Generalized functions for handling precast/midcast/aftercast for player-initiated actions.
  208. -- This depends on proper set naming.
  209. -- Global hooks can be written as user_xxx() to override functions at a global level.
  210. -- Each job can override any of these general functions using job_xxx() hooks.
  211. -------------------------------------------------------------------------------------------------------------------
  212.  
  213. ------------------------------------------------------------------------
  214. -- Generic function to map a set processing order to all action events.
  215. ------------------------------------------------------------------------
  216.  
  217.  
  218. -- Process actions in a specific order of events:
  219. -- Filter  - filter_xxx() functions determine whether to run any of the code for this action.
  220. -- Global  - user_xxx() functions get called first.  Define in Mote-Globals or User-Globals.
  221. -- Local   - job_xxx() functions get called next. Define in JOB.lua file.
  222. -- Default - default_xxx() functions get called next. Defined in this file.
  223. -- Cleanup - cleanup_xxx() functions always get called before exiting.
  224. --
  225. -- Parameters:
  226. -- spell - standard spell table passed in by GearSwap
  227. -- action - string defining the function mapping to use (precast, midcast, etc)
  228. function handle_actions(spell, action)
  229.     -- Init an eventArgs that allows cancelling.
  230.     local eventArgs = {handled = false, cancel = false}
  231.    
  232.     mote_vars.set_breadcrumbs:clear()
  233.  
  234.     -- Get the spell mapping, since we'll be passing it to various functions and checks.
  235.     local spellMap = get_spell_map(spell)
  236.  
  237.     -- General filter checks to see whether this function should be run.
  238.     -- If eventArgs.cancel is set, cancels this function, not the spell.
  239.     if _G['filter_'..action] then
  240.         _G['filter_'..action](spell, spellMap, eventArgs)
  241.     end
  242.  
  243.     -- If filter didn't cancel it, process user and default actions.
  244.     if not eventArgs.cancel then
  245.         -- Global user handling of this action
  246.         if _G['user_'..action] then
  247.             _G['user_'..action](spell, action, spellMap, eventArgs)
  248.            
  249.             if eventArgs.cancel then
  250.                 cancel_spell()
  251.             end
  252.         end
  253.        
  254.         -- Job-specific handling of this action
  255.         if not eventArgs.cancel and not eventArgs.handled and _G['job_'..action] then
  256.             _G['job_'..action](spell, action, spellMap, eventArgs)
  257.            
  258.             if eventArgs.cancel then
  259.                 cancel_spell()
  260.             end
  261.         end
  262.    
  263.         -- Default handling of this action
  264.         if not eventArgs.cancel and not eventArgs.handled and _G['default_'..action] then
  265.             _G['default_'..action](spell, spellMap)
  266.             display_breadcrumbs(spell, spellMap, action)
  267.         end
  268.        
  269.         -- Global post-handling of this action
  270.         if not eventArgs.cancel and _G['user_post_'..action] then
  271.             _G['user_post_'..action](spell, action, spellMap, eventArgs)
  272.         end
  273.  
  274.         -- Job-specific post-handling of this action
  275.         if not eventArgs.cancel and _G['job_post_'..action] then
  276.             _G['job_post_'..action](spell, action, spellMap, eventArgs)
  277.         end
  278.     end
  279.  
  280.     -- Cleanup once this action is done
  281.     if _G['cleanup_'..action] then
  282.         _G['cleanup_'..action](spell, spellMap, eventArgs)
  283.     end
  284. end
  285.  
  286.  
  287. --------------------------------------
  288. -- Action hooks called by GearSwap.
  289. --------------------------------------
  290.  
  291. function pretarget(spell)
  292.     handle_actions(spell, 'pretarget')
  293. end
  294.  
  295. function precast(spell)
  296.     if state.Buff[spell.english] ~= nil then
  297.         state.Buff[spell.english] = true
  298.     end
  299.     handle_actions(spell, 'precast')
  300. end
  301.  
  302. function midcast(spell)
  303.     handle_actions(spell, 'midcast')
  304. end
  305.  
  306. function aftercast(spell)
  307.     if state.Buff[spell.english] ~= nil then
  308.         state.Buff[spell.english] = not spell.interrupted or buffactive[spell.english] or false
  309.     end
  310.     handle_actions(spell, 'aftercast')
  311. end
  312.  
  313. function pet_midcast(spell)
  314.     handle_actions(spell, 'pet_midcast')
  315. end
  316.  
  317. function pet_aftercast(spell)
  318.     handle_actions(spell, 'pet_aftercast')
  319. end
  320.  
  321. --------------------------------------
  322. -- Default code for each action.
  323. --------------------------------------
  324.  
  325. function default_pretarget(spell, spellMap)
  326.     auto_change_target(spell, spellMap)
  327. end
  328.  
  329. function default_precast(spell, spellMap)
  330.     equip(get_precast_set(spell, spellMap))
  331. end
  332.  
  333. function default_midcast(spell, spellMap)
  334.     equip(get_midcast_set(spell, spellMap))
  335. end
  336.  
  337. function default_aftercast(spell, spellMap)
  338.     if not pet_midaction() then
  339.         handle_equipping_gear(player.status)
  340.     end
  341. end
  342.  
  343. function default_pet_midcast(spell, spellMap)
  344.     equip(get_pet_midcast_set(spell, spellMap))
  345. end
  346.  
  347. function default_pet_aftercast(spell, spellMap)
  348.     handle_equipping_gear(player.status)
  349. end
  350.  
  351. --------------------------------------
  352. -- Filters for each action.
  353. -- Set eventArgs.cancel to true to stop further processing.
  354. -- May show notification messages, but should not do any processing here.
  355. --------------------------------------
  356.  
  357. function filter_midcast(spell, spellMap, eventArgs)
  358.     if state.EquipStop.value == 'precast' then
  359.         eventArgs.cancel = true
  360.     end
  361. end
  362.  
  363. function filter_aftercast(spell, spellMap, eventArgs)
  364.     if state.EquipStop.value == 'precast' or state.EquipStop.value == 'midcast' or state.EquipStop.value == 'pet_midcast' then
  365.         eventArgs.cancel = true
  366.     elseif spell.name == 'Unknown Interrupt' then
  367.         eventArgs.cancel = true
  368.     end
  369. end
  370.  
  371. function filter_pet_midcast(spell, spellMap, eventArgs)
  372.     -- If we have show_set active for precast or midcast, don't try to equip pet midcast gear.
  373.     if state.EquipStop.value == 'precast' or state.EquipStop.value == 'midcast' then
  374.         add_to_chat(104, 'Show Sets: Pet midcast not equipped.')
  375.         eventArgs.cancel = true
  376.     end
  377. end
  378.  
  379. function filter_pet_aftercast(spell, spellMap, eventArgs)
  380.     -- If show_set is flagged for precast or midcast, don't try to equip aftercast gear.
  381.     if state.EquipStop.value == 'precast' or state.EquipStop.value == 'midcast' or state.EquipStop.value == 'pet_midcast' then
  382.         eventArgs.cancel = true
  383.     end
  384. end
  385.  
  386. --------------------------------------
  387. -- Cleanup code for each action.
  388. --------------------------------------
  389.  
  390. function cleanup_precast(spell, spellMap, eventArgs)
  391.     -- If show_set is flagged for precast, notify that we won't try to equip later gear.
  392.     if state.EquipStop.value == 'precast' then
  393.         add_to_chat(104, 'Show Sets: Stopping at precast.')
  394.     end
  395. end
  396.  
  397. function cleanup_midcast(spell, spellMap, eventArgs)
  398.     -- If show_set is flagged for midcast, notify that we won't try to equip later gear.
  399.     if state.EquipStop.value == 'midcast' then
  400.         add_to_chat(104, 'Show Sets: Stopping at midcast.')
  401.     end
  402. end
  403.  
  404. function cleanup_aftercast(spell, spellMap, eventArgs)
  405.     -- Reset custom classes after all possible precast/midcast/aftercast/job-specific usage of the value.
  406.     -- If we're in the middle of a pet action, pet_aftercast will handle clearing it.
  407.     if not pet_midaction() then
  408.         reset_transitory_classes()
  409.     end
  410. end
  411.  
  412. function cleanup_pet_midcast(spell, spellMap, eventArgs)
  413.     -- If show_set is flagged for pet midcast, notify that we won't try to equip later gear.
  414.     if state.EquipStop.value == 'pet_midcast' then
  415.         add_to_chat(104, 'Show Sets: Stopping at pet midcast.')
  416.     end
  417. end
  418.  
  419. function cleanup_pet_aftercast(spell, spellMap, eventArgs)
  420.     -- Reset custom classes after all possible precast/midcast/aftercast/job-specific usage of the value.
  421.     reset_transitory_classes()
  422. end
  423.  
  424.  
  425. -- Clears the values from classes that only exist til the action is complete.
  426. function reset_transitory_classes()
  427.     classes.CustomClass = nil
  428.     classes.JAMode = nil
  429. end
  430.  
  431.  
  432.  
  433. -------------------------------------------------------------------------------------------------------------------
  434. -- High-level functions for selecting and equipping gear sets.
  435. -------------------------------------------------------------------------------------------------------------------
  436.  
  437. -- Central point to call to equip gear based on status.
  438. -- Status - Player status that we're using to define what gear to equip.
  439. function handle_equipping_gear(playerStatus, petStatus)
  440.     -- init a new eventArgs
  441.     local eventArgs = {handled = false}
  442.  
  443.     -- Allow jobs to override this code
  444.     if job_handle_equipping_gear then
  445.         job_handle_equipping_gear(playerStatus, eventArgs)
  446.     end
  447.  
  448.     -- Equip default gear if job didn't handle it.
  449.     if not eventArgs.handled then
  450.         equip_gear_by_status(playerStatus, petStatus)
  451.     end
  452. end
  453.  
  454.  
  455. -- Function to wrap logic for equipping gear on aftercast, status change, or user update.
  456. -- @param status : The current or new player status that determines what sort of gear to equip.
  457. function equip_gear_by_status(playerStatus, petStatus)
  458.     if _global.debug_mode then add_to_chat(123,'Debug: Equip gear for status ['..tostring(status)..'], HP='..tostring(player.hp)) end
  459.  
  460.     playerStatus = playerStatus or player.status or 'Idle'
  461.    
  462.     -- If status not defined, treat as idle.
  463.     -- Be sure to check for positive HP to make sure they're not dead.
  464.     if (playerStatus == 'Idle' or playerStatus == '') and player.hp > 0 then
  465.         equip(get_idle_set(petStatus))
  466.     elseif playerStatus == 'Engaged' then
  467.         equip(get_melee_set(petStatus))
  468.     elseif playerStatus == 'Resting' then
  469.         equip(get_resting_set(petStatus))
  470.     end
  471. end
  472.  
  473.  
  474. -------------------------------------------------------------------------------------------------------------------
  475. -- Functions for constructing default gear sets based on status.
  476. -------------------------------------------------------------------------------------------------------------------
  477.  
  478. -- Returns the appropriate idle set based on current state values and location.
  479. -- Set construction order (all of which are optional):
  480. --   sets.idle[idleScope][state.IdleMode][Pet[Engaged]][CustomIdleGroups]
  481. --
  482. -- Params:
  483. -- petStatus - Optional explicit definition of pet status.
  484. function get_idle_set(petStatus)
  485.  
  486.     local idleSet = sets.idle
  487.    
  488.     if not idleSet then
  489.         return {}
  490.     end
  491.    
  492.     mote_vars.set_breadcrumbs:append('sets')
  493.     mote_vars.set_breadcrumbs:append('idle')
  494.    
  495.     local idleScope
  496.  
  497.     if buffactive.weakness then
  498.         idleScope = 'Weak'
  499.     elseif areas.Cities:contains(world.area) then
  500.         idleScope = 'Town'
  501.     else
  502.         idleScope = 'Field'
  503.     end
  504.  
  505.     if idleSet[idleScope] then
  506.         idleSet = idleSet[idleScope]
  507.         mote_vars.set_breadcrumbs:append(idleScope)
  508.     end
  509.  
  510.     if idleSet[state.IdleMode.current] then
  511.         idleSet = idleSet[state.IdleMode.current]
  512.         mote_vars.set_breadcrumbs:append(state.IdleMode.current)
  513.     end
  514.  
  515.     if (pet.isvalid or state.Buff.Pet) and idleSet.Pet then
  516.         idleSet = idleSet.Pet
  517.         petStatus = petStatus or pet.status
  518.         mote_vars.set_breadcrumbs:append('Pet')
  519.  
  520.         if petStatus == 'Engaged' and idleSet.Engaged then
  521.             idleSet = idleSet.Engaged
  522.             mote_vars.set_breadcrumbs:append('Engaged')
  523.         end
  524.     end
  525.  
  526.     for _,group in ipairs(classes.CustomIdleGroups) do
  527.         if idleSet[group] then
  528.             idleSet = idleSet[group]
  529.             mote_vars.set_breadcrumbs:append(group)
  530.         end
  531.     end
  532.  
  533.     idleSet = apply_defense(idleSet)
  534.     idleSet = apply_kiting(idleSet)
  535.  
  536.     if user_customize_idle_set then
  537.         idleSet = user_customize_idle_set(idleSet)
  538.     end
  539.  
  540.     if customize_idle_set then
  541.         idleSet = customize_idle_set(idleSet)
  542.     end
  543.  
  544.     return idleSet
  545. end
  546.  
  547.  
  548. -- Returns the appropriate melee set based on current state values.
  549. -- Set construction order (all sets after sets.engaged are optional):
  550. --   sets.engaged[state.CombatForm][state.CombatWeapon][state.OffenseMode][state.DefenseMode][classes.CustomMeleeGroups (any number)]
  551. function get_melee_set()
  552.     local meleeSet = sets.engaged
  553.    
  554.     if not meleeSet then
  555.         return {}
  556.     end
  557.    
  558.     mote_vars.set_breadcrumbs:append('sets')
  559.     mote_vars.set_breadcrumbs:append('engaged')
  560.  
  561.     if state.CombatForm.has_value and meleeSet[state.CombatForm.value] then
  562.         meleeSet = meleeSet[state.CombatForm.value]
  563.         mote_vars.set_breadcrumbs:append(state.CombatForm.value)
  564.     end
  565.  
  566.     if state.CombatWeapon.has_value and meleeSet[state.CombatWeapon.value] then
  567.         meleeSet = meleeSet[state.CombatWeapon.value]
  568.         mote_vars.set_breadcrumbs:append(state.CombatWeapon.value)
  569.     end
  570.  
  571.     if meleeSet[state.OffenseMode.current] then
  572.         meleeSet = meleeSet[state.OffenseMode.current]
  573.         mote_vars.set_breadcrumbs:append(state.OffenseMode.current)
  574.     end
  575.  
  576.     if meleeSet[state.HybridMode.current] then
  577.         meleeSet = meleeSet[state.HybridMode.current]
  578.         mote_vars.set_breadcrumbs:append(state.HybridMode.current)
  579.     end
  580.  
  581.     for _,group in ipairs(classes.CustomMeleeGroups) do
  582.         if meleeSet[group] then
  583.             meleeSet = meleeSet[group]
  584.             mote_vars.set_breadcrumbs:append(group)
  585.         end
  586.     end
  587.  
  588.     meleeSet = apply_defense(meleeSet)
  589.     meleeSet = apply_kiting(meleeSet)
  590.  
  591.     if customize_melee_set then
  592.         meleeSet = customize_melee_set(meleeSet)
  593.     end
  594.  
  595.     if user_customize_melee_set then
  596.         meleeSet = user_customize_melee_set(meleeSet)
  597.     end
  598.  
  599.     return meleeSet
  600. end
  601.  
  602.  
  603. -- Returns the appropriate resting set based on current state values.
  604. -- Set construction order:
  605. --   sets.resting[state.RestingMode]
  606. function get_resting_set()
  607.     local restingSet = sets.resting
  608.  
  609.     if not restingSet then
  610.         return {}
  611.     end
  612.  
  613.     mote_vars.set_breadcrumbs:append('sets')
  614.     mote_vars.set_breadcrumbs:append('resting')
  615.  
  616.     if restingSet[state.RestingMode.current] then
  617.         restingSet = restingSet[state.RestingMode.current]
  618.         mote_vars.set_breadcrumbs:append(state.RestingMode.current)
  619.     end
  620.  
  621.     return restingSet
  622. end
  623.  
  624.  
  625. -------------------------------------------------------------------------------------------------------------------
  626. -- Functions for constructing default gear sets based on action.
  627. -------------------------------------------------------------------------------------------------------------------
  628.  
  629. -- Get the default precast gear set.
  630. function get_precast_set(spell, spellMap)
  631.     -- If there are no precast sets defined, bail out.
  632.     if not sets.precast then
  633.         return {}
  634.     end
  635.  
  636.     local equipSet = sets.precast
  637.  
  638.     mote_vars.set_breadcrumbs:append('sets')
  639.     mote_vars.set_breadcrumbs:append('precast')
  640.    
  641.     -- Determine base sub-table from type of action being performed.
  642.    
  643.     local cat
  644.    
  645.     if spell.action_type == 'Magic' then
  646.         cat = 'FC'
  647.     elseif spell.action_type == 'Ranged Attack' then
  648.         cat = (sets.precast.RangedAttack and 'RangedAttack') or 'RA'
  649.     elseif spell.action_type == 'Ability' then
  650.         if spell.type == 'WeaponSkill' then
  651.             cat = 'WS'
  652.         elseif spell.type == 'JobAbility' then
  653.             cat = 'JA'
  654.         else
  655.             -- Allow fallback to .JA table if spell.type isn't found, for all non-weaponskill abilities.
  656.             cat = (sets.precast[spell.type] and spell.type) or 'JA'
  657.         end
  658.     elseif spell.action_type == 'Item' then
  659.         cat = 'Item'
  660.     end
  661.    
  662.     -- If no proper sub-category is defined in the job file, bail out.
  663.     if cat then
  664.         if equipSet[cat] then
  665.             equipSet = equipSet[cat]
  666.             mote_vars.set_breadcrumbs:append(cat)
  667.         else
  668.             mote_vars.set_breadcrumbs:clear()
  669.             return {}
  670.         end
  671.     end
  672.  
  673.     classes.SkipSkillCheck = false
  674.     -- Handle automatic selection of set based on spell class/name/map/skill/type.
  675.     equipSet = select_specific_set(equipSet, spell, spellMap)
  676.  
  677.    
  678.     -- Once we have a named base set, do checks for specialized modes (casting mode, weaponskill mode, etc).
  679.    
  680.     if spell.action_type == 'Magic' then
  681.         if equipSet[state.CastingMode.current] then
  682.             equipSet = equipSet[state.CastingMode.current]
  683.             mote_vars.set_breadcrumbs:append(state.CastingMode.current)
  684.         end
  685.     elseif spell.type == 'WeaponSkill' then
  686.         equipSet = get_weaponskill_set(equipSet, spell, spellMap)
  687.     elseif spell.action_type == 'Ability' then
  688.         if classes.JAMode and equipSet[classes.JAMode] then
  689.             equipSet = equipSet[classes.JAMode]
  690.             mote_vars.set_breadcrumbs:append(classes.JAMode)
  691.         end
  692.     elseif spell.action_type == 'Ranged Attack' then
  693.         equipSet = get_ranged_set(equipSet, spell, spellMap)
  694.     end
  695.  
  696.     -- Update defintions for element-specific gear that may be used.
  697.     set_elemental_gear(spell)
  698.    
  699.     -- Return whatever we've constructed.
  700.     return equipSet
  701. end
  702.  
  703.  
  704.  
  705. -- Get the default midcast gear set.
  706. -- This builds on sets.midcast.
  707. function get_midcast_set(spell, spellMap)
  708.     -- If there are no midcast sets defined, bail out.
  709.     if not sets.midcast then
  710.         return {}
  711.     end
  712.    
  713.     local equipSet = sets.midcast
  714.  
  715.     mote_vars.set_breadcrumbs:append('sets')
  716.     mote_vars.set_breadcrumbs:append('midcast')
  717.    
  718.     -- Determine base sub-table from type of action being performed.
  719.     -- Only ranged attacks and items get specific sub-categories here.
  720.    
  721.     local cat
  722.  
  723.     if spell.action_type == 'Ranged Attack' then
  724.         cat = (sets.precast.RangedAttack and 'RangedAttack') or 'RA'
  725.     elseif spell.action_type == 'Item' then
  726.         cat = 'Item'
  727.     end
  728.    
  729.     -- If no proper sub-category is defined in the job file, bail out.
  730.     if cat then
  731.         if equipSet[cat] then
  732.             equipSet = equipSet[cat]
  733.             mote_vars.set_breadcrumbs:append(cat)
  734.         else
  735.             mote_vars.set_breadcrumbs:clear()
  736.             return {}
  737.         end
  738.     end
  739.    
  740.     classes.SkipSkillCheck = classes.NoSkillSpells:contains(spell.english)
  741.     -- Handle automatic selection of set based on spell class/name/map/skill/type.
  742.     equipSet = select_specific_set(equipSet, spell, spellMap)
  743.    
  744.     -- After the default checks, do checks for specialized modes (casting mode, etc).
  745.    
  746.     if spell.action_type == 'Magic' then
  747.         if equipSet[state.CastingMode.current] then
  748.             equipSet = equipSet[state.CastingMode.current]
  749.             mote_vars.set_breadcrumbs:append(state.CastingMode.current)
  750.         end
  751.     elseif spell.action_type == 'Ranged Attack' then
  752.         equipSet = get_ranged_set(equipSet, spell, spellMap)
  753.     end
  754.  
  755.     -- Return whatever we've constructed.
  756.     return equipSet
  757. end
  758.  
  759.  
  760. -- Get the default pet midcast gear set.
  761. -- This is built in sets.midcast.Pet.
  762. function get_pet_midcast_set(spell, spellMap)
  763.     -- If there are no midcast sets defined, bail out.
  764.     if not sets.midcast or not sets.midcast.Pet then
  765.         return {}
  766.     end
  767.  
  768.     local equipSet = sets.midcast.Pet
  769.  
  770.     mote_vars.set_breadcrumbs:append('sets')
  771.     mote_vars.set_breadcrumbs:append('midcast')
  772.     mote_vars.set_breadcrumbs:append('Pet')
  773.  
  774.     if sets.midcast and sets.midcast.Pet then
  775.         classes.SkipSkillCheck = false
  776.         equipSet = select_specific_set(equipSet, spell, spellMap)
  777.  
  778.         -- We can only generally be certain about whether the pet's action is
  779.         -- Magic (ie: it cast a spell of its own volition) or Ability (it performed
  780.         -- an action at the request of the player).  Allow CastinMode and
  781.         -- OffenseMode to refine whatever set was selected above.
  782.         if spell.action_type == 'Magic' then
  783.             if equipSet[state.CastingMode.current] then
  784.                 equipSet = equipSet[state.CastingMode.current]
  785.                 mote_vars.set_breadcrumbs:append(state.CastingMode.current)
  786.             end
  787.         elseif spell.action_type == 'Ability' then
  788.             if equipSet[state.OffenseMode.current] then
  789.                 equipSet = equipSet[state.OffenseMode.current]
  790.                 mote_vars.set_breadcrumbs:append(state.OffenseMode.current)
  791.             end
  792.         end
  793.     end
  794.  
  795.     return equipSet
  796. end
  797.  
  798.  
  799. -- Function to handle the logic of selecting the proper weaponskill set.
  800. function get_weaponskill_set(equipSet, spell, spellMap)
  801.     -- Custom handling for weaponskills
  802.     local ws_mode = state.WeaponskillMode.current
  803.    
  804.     if ws_mode == 'Normal' then
  805.         -- If a particular weaponskill mode isn't specified, see if we have a weaponskill mode
  806.         -- corresponding to the current offense mode.  If so, use that.
  807.         if spell.skill == 'Archery' or spell.skill == 'Marksmanship' then
  808.             if state.RangedMode.current ~= 'Normal' and state.WeaponskillMode:contains(state.RangedMode.current) then
  809.                 ws_mode = state.RangedMode.current
  810.             end
  811.         else
  812.             if state.OffenseMode.current ~= 'Normal' and state.WeaponskillMode:contains(state.OffenseMode.current) then
  813.                 ws_mode = state.OffenseMode.current
  814.             end
  815.         end
  816.     end
  817.  
  818.     local custom_wsmode
  819.  
  820.     -- Allow the job file to specify a preferred weaponskill mode
  821.     if get_custom_wsmode then
  822.         custom_wsmode = get_custom_wsmode(spell, spellMap, ws_mode)
  823.     end
  824.  
  825.     -- If the job file returned a weaponskill mode, use that.
  826.     if custom_wsmode then
  827.         ws_mode = custom_wsmode
  828.     end
  829.  
  830.     if equipSet[ws_mode] then
  831.         equipSet = equipSet[ws_mode]
  832.         mote_vars.set_breadcrumbs:append(ws_mode)
  833.     end
  834.    
  835.     return equipSet
  836. end
  837.  
  838.  
  839. -- Function to handle the logic of selecting the proper ranged set.
  840. function get_ranged_set(equipSet, spell, spellMap)
  841.     -- Attach Combat Form and Combat Weapon to set checks
  842.     if state.CombatForm.has_value and equipSet[state.CombatForm.value] then
  843.         equipSet = equipSet[state.CombatForm.value]
  844.         mote_vars.set_breadcrumbs:append(state.CombatForm.value)
  845.     end
  846.  
  847.     if state.CombatWeapon.has_value and equipSet[state.CombatWeapon.value] then
  848.         equipSet = equipSet[state.CombatWeapon.value]
  849.         mote_vars.set_breadcrumbs:append(state.CombatWeapon.value)
  850.     end
  851.  
  852.     -- Check for specific mode for ranged attacks (eg: Acc, Att, etc)
  853.     if equipSet[state.RangedMode.current] then
  854.         equipSet = equipSet[state.RangedMode.current]
  855.         mote_vars.set_breadcrumbs:append(state.RangedMode.current)
  856.     end
  857.  
  858.     -- Tack on any additionally specified custom groups, if the sets are defined.
  859.     for _,group in ipairs(classes.CustomRangedGroups) do
  860.         if equipSet[group] then
  861.             equipSet = equipSet[group]
  862.             mote_vars.set_breadcrumbs:append(group)
  863.         end
  864.     end
  865.  
  866.     return equipSet
  867. end
  868.  
  869.  
  870. -------------------------------------------------------------------------------------------------------------------
  871. -- Functions for optional supplemental gear overriding the default sets defined above.
  872. -------------------------------------------------------------------------------------------------------------------
  873.  
  874. -- Function to apply any active defense set on top of the supplied set
  875. -- @param baseSet : The set that any currently active defense set will be applied on top of. (gear set table)
  876. function apply_defense(baseSet)
  877.     if state.DefenseMode.current ~= 'None' then
  878.         local defenseSet = sets.defense
  879.  
  880.         defenseSet = sets.defense[state[state.DefenseMode.current .. 'DefenseMode'].current] or defenseSet
  881.  
  882.         for _,group in ipairs(classes.CustomDefenseGroups) do
  883.             defenseSet = defenseSet[group] or defenseSet
  884.         end
  885.  
  886.         if customize_defense_set then
  887.             defenseSet = customize_defense_set(defenseSet)
  888.         end
  889.  
  890.         baseSet = set_combine(baseSet, defenseSet)
  891.     end
  892.  
  893.     return baseSet
  894. end
  895.  
  896.  
  897. -- Function to add kiting gear on top of the base set if kiting state is true.
  898. -- @param baseSet : The gear set that the kiting gear will be applied on top of.
  899. function apply_kiting(baseSet)
  900.     if state.Kiting.value then
  901.         if sets.Kiting then
  902.             baseSet = set_combine(baseSet, sets.Kiting)
  903.         end
  904.     end
  905.  
  906.     return baseSet
  907. end
  908.  
  909.  
  910. -------------------------------------------------------------------------------------------------------------------
  911. -- Utility functions for constructing default gear sets.
  912. -------------------------------------------------------------------------------------------------------------------
  913.  
  914. -- Get a spell mapping for the spell.
  915. function get_spell_map(spell)
  916.     local defaultSpellMap = classes.SpellMaps[spell.english]
  917.     local jobSpellMap
  918.    
  919.     if job_get_spell_map then
  920.         jobSpellMap = job_get_spell_map(spell, defaultSpellMap)
  921.     end
  922.  
  923.     return jobSpellMap or defaultSpellMap
  924. end
  925.  
  926.  
  927. -- Select the equipment set to equip from a given starting table, based on standard
  928. -- selection order: custom class, spell name, spell map, spell skill, and spell type.
  929. -- Spell skill and spell type may further refine their selections based on
  930. -- custom class, spell name and spell map.
  931. function select_specific_set(equipSet, spell, spellMap)
  932.     -- Take the determined base equipment set and try to get the simple naming extensions that
  933.     -- may apply to it (class, spell name, spell map).
  934.     local namedSet = get_named_set(equipSet, spell, spellMap)
  935.    
  936.     -- If no simple naming sub-tables were found, and we simply got back the original equip set,
  937.     -- check for spell.skill and spell.type, then check the simple naming extensions again.
  938.     if namedSet == equipSet then
  939.         if spell.skill and equipSet[spell.skill] and not classes.SkipSkillCheck then
  940.             namedSet = equipSet[spell.skill]
  941.             mote_vars.set_breadcrumbs:append(spell.skill)
  942.         elseif spell.type and equipSet[spell.type] then
  943.             namedSet = equipSet[spell.type]
  944.             mote_vars.set_breadcrumbs:append(spell.type)
  945.         else
  946.             return equipSet
  947.         end
  948.        
  949.         namedSet = get_named_set(namedSet, spell, spellMap)
  950.     end
  951.  
  952.     return namedSet or equipSet
  953. end
  954.  
  955.  
  956. -- Simple utility function to handle a portion of the equipment set determination.
  957. -- It attempts to select a sub-table of the provided equipment set based on the
  958. -- standard search order of custom class, spell name, and spell map.
  959. -- If no such set is found, it returns the original base set (equipSet) provided.
  960. function get_named_set(equipSet, spell, spellMap)
  961.     if equipSet then
  962.         if classes.CustomClass and equipSet[classes.CustomClass] then
  963.             mote_vars.set_breadcrumbs:append(classes.CustomClass)
  964.             return equipSet[classes.CustomClass]
  965.         elseif equipSet[spell.english] then
  966.             mote_vars.set_breadcrumbs:append(spell.english)
  967.             return equipSet[spell.english]
  968.         elseif spellMap and equipSet[spellMap] then
  969.             mote_vars.set_breadcrumbs:append(spellMap)
  970.             return equipSet[spellMap]
  971.         else
  972.             return equipSet
  973.         end
  974.     end
  975. end
  976.  
  977.  
  978. -------------------------------------------------------------------------------------------------------------------
  979. -- Hooks for other events.
  980. -------------------------------------------------------------------------------------------------------------------
  981.  
  982. -- Called when the player's subjob changes.
  983. function sub_job_change(newSubjob, oldSubjob)
  984.     if user_setup then
  985.         user_setup()
  986.     end
  987.    
  988.     if job_sub_job_change then
  989.         job_sub_job_change(newSubjob, oldSubjob)
  990.     end
  991.    
  992.     send_command('gs c update')
  993. end
  994.  
  995.  
  996. -- Called when the player's status changes.
  997. function status_change(newStatus, oldStatus)
  998.     -- init a new eventArgs
  999.     local eventArgs = {handled = false}
  1000.     mote_vars.set_breadcrumbs:clear()
  1001.  
  1002.     -- Allow a global function to be called on status change.
  1003.     if user_status_change then
  1004.         user_status_change(newStatus, oldStatus, eventArgs)
  1005.     end
  1006.  
  1007.     -- Then call individual jobs to handle status change events.
  1008.     if not eventArgs.handled then
  1009.         if job_status_change then
  1010.             job_status_change(newStatus, oldStatus, eventArgs)
  1011.         end
  1012.     end
  1013.  
  1014.     -- Handle equipping default gear if the job didn't mark this as handled.
  1015.     if not eventArgs.handled then
  1016.         handle_equipping_gear(newStatus)
  1017.         display_breadcrumbs()
  1018.     end
  1019. end
  1020.  
  1021.  
  1022. -- Called when a player gains or loses a buff.
  1023. -- buff == buff gained or lost
  1024. -- gain == true if the buff was gained, false if it was lost.
  1025. function buff_change(buff, gain)
  1026.     -- Init a new eventArgs
  1027.     local eventArgs = {handled = false}
  1028.  
  1029.     if state.Buff[buff] ~= nil then
  1030.         state.Buff[buff] = gain
  1031.     end
  1032.  
  1033.     -- Allow a global function to be called on buff change.
  1034.     if user_buff_change then
  1035.         user_buff_change(buff, gain, eventArgs)
  1036.     end
  1037.  
  1038.     -- Allow jobs to handle buff change events.
  1039.     if not eventArgs.handled then
  1040.         if job_buff_change then
  1041.             job_buff_change(buff, gain, eventArgs)
  1042.         end
  1043.     end
  1044. end
  1045.  
  1046.  
  1047. -- Called when a player gains or loses a pet.
  1048. -- pet == pet gained or lost
  1049. -- gain == true if the pet was gained, false if it was lost.
  1050. function pet_change(pet, gain)
  1051.     -- Init a new eventArgs
  1052.     local eventArgs = {handled = false}
  1053.  
  1054.     -- Allow jobs to handle pet change events.
  1055.     if job_pet_change then
  1056.         job_pet_change(pet, gain, eventArgs)
  1057.     end
  1058.  
  1059.     -- Equip default gear if not handled by the job.
  1060.     if not eventArgs.handled then
  1061.         handle_equipping_gear(player.status)
  1062.     end
  1063. end
  1064.  
  1065.  
  1066. -- Called when the player's pet's status changes.
  1067. -- Note that this is also called after pet_change when the pet is released.
  1068. -- As such, don't automatically handle gear equips.  Only do so if directed
  1069. -- to do so by the job.
  1070. function pet_status_change(newStatus, oldStatus)
  1071.     -- Init a new eventArgs
  1072.     local eventArgs = {handled = false}
  1073.  
  1074.     -- Allow jobs to override this code
  1075.     if job_pet_status_change then
  1076.         job_pet_status_change(newStatus, oldStatus, eventArgs)
  1077.     end
  1078. end
  1079.  
  1080.  
  1081. -------------------------------------------------------------------------------------------------------------------
  1082. -- Debugging functions.
  1083. -------------------------------------------------------------------------------------------------------------------
  1084.  
  1085. -- This is a debugging function that will print the accumulated set selection
  1086. -- breadcrumbs for the default selected set for any given action stage.
  1087. function display_breadcrumbs(spell, spellMap, action)
  1088.     if not _settings.debug_mode then
  1089.         return
  1090.     end
  1091.    
  1092.     local msg = 'Default '
  1093.    
  1094.     if action and spell then
  1095.         msg = msg .. action .. ' set selection for ' .. spell.name
  1096.     end
  1097.    
  1098.     if spellMap then
  1099.         msg = msg .. ' (' .. spellMap .. ')'
  1100.     end
  1101.     msg = msg .. ' : '
  1102.    
  1103.     local cons
  1104.    
  1105.     for _,name in ipairs(mote_vars.set_breadcrumbs) do
  1106.         if not cons then
  1107.             cons = name
  1108.         else
  1109.             if name:contains(' ') or name:contains("'") then
  1110.                 cons = cons .. '["' .. name .. '"]'
  1111.             else
  1112.                 cons = cons .. '.' .. name
  1113.             end
  1114.         end
  1115.     end
  1116.  
  1117.     if cons then
  1118.         if action and cons == ('sets.' .. action) then
  1119.             msg = msg .. "None"
  1120.         else
  1121.             msg = msg .. tostring(cons)
  1122.         end
  1123.         add_to_chat(123, msg)
  1124.     end
  1125. end
Advertisement
Add Comment
Please, Sign In to add comment