/addons/autocontrol/autocontrol.lua

https://github.com/Ibuprophen/Lua · Lua · 290 lines · 245 code · 18 blank · 27 comment · 93 complexity · 3797f88cf2763480f79981d14a8ddef4 MD5 · raw file

  1. --[[
  2. Copyright (c) 2013, Ricky Gall
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of <addon name> nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL <your name> BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. ]]
  25. _addon.name = 'autocontrol'
  26. _addon.version = '1.01'
  27. _addon.author = 'Nitrous (Shiva)'
  28. _addon.commands = {'autocontrol','acon'}
  29. require('tables')
  30. require('strings')
  31. require('logger')
  32. require('sets')
  33. res = require('resources')
  34. config = require('config')
  35. files = require('files')
  36. chat = require('chat')
  37. defaults = {}
  38. defaults.bg = {}
  39. defaults.bg.red = 0
  40. defaults.bg.blue = 0
  41. defaults.bg.green = 0
  42. defaults.pos = {}
  43. defaults.pos.x = 400
  44. defaults.pos.y = 300
  45. defaults.text = {}
  46. defaults.text.red = 255
  47. defaults.text.green = 255
  48. defaults.text.blue = 255
  49. defaults.text.font = 'Consolas'
  50. defaults.text.size = 10
  51. defaults.autosets = T{}
  52. defaults.autosets.default = T{ }
  53. settings = config.load(defaults)
  54. require('maneuver') -- has to be loaded after settings are parsed.
  55. petlessZones = S{50,235,234,224,284,233,70,257,251,14,242,250,226,245,
  56. 237,249,131,53,252,231,236,246,232,240,247,243,223,248,230,
  57. 26,71,244,239,238,241,256,257}
  58. function initialize()
  59. local player = windower.ffxi.get_player()
  60. if not player then
  61. windower.send_command('@wait 5;lua i autocontrol initialize')
  62. return
  63. end
  64. mjob_id = player.main_job_id
  65. atts = res.items:category('Automaton')
  66. decay = 1
  67. for key,_ in pairs(heat) do
  68. heat[key] = 0
  69. Burden_tb[key] = 0
  70. Burden_tb['time'..key] = 0
  71. end
  72. if mjob_id == 18 then
  73. if player.pet_index then
  74. running = 1
  75. text_update_loop('start')
  76. Burden_tb:show()
  77. end
  78. end
  79. end
  80. windower.register_event('load', 'login', initialize)
  81. windower.register_event('logout', 'unload', function()
  82. text_update_loop('stop')
  83. end)
  84. function attach_set(autoset)
  85. if windower.ffxi.get_player().main_job_id ~= 18 then return nil end
  86. if settings.autosets[autoset] == nil then return end
  87. if settings.autosets[autoset]:map(string.lower):equals(get_current_autoset():map(string.lower)) then
  88. log('The '..autoset..' set is already equipped.')
  89. return
  90. end
  91. if windower.ffxi.get_mob_by_id(windower.ffxi.get_player().id).pet_index
  92. and windower.ffxi.get_mob_by_id(windower.ffxi.get_player().id).pet_index ~= 0 then
  93. if windower.ffxi.get_ability_recasts()[208] == 0 then
  94. windower.send_command('input /pet "Deactivate" <me>')
  95. log('Deactivating '..windower.ffxi.get_mjob_data()['name']..'.')
  96. windower.send_command('@wait 2;lua i autocontrol attach_set '..autoset)
  97. else
  98. local var = windower.ffxi.get_ability_recasts()[208]
  99. if var ~= nil then
  100. error('Deactivate on cooldown wait '..((var * 1) / 60)..' seconds and try again')
  101. end
  102. end
  103. else
  104. windower.ffxi.reset_attachments()
  105. log('Starting to equip '..autoset..' to '..windower.ffxi.get_mjob_data().name..'.')
  106. set_attachments_from_autoset(autoset, 'head')
  107. end
  108. end
  109. function set_attachments_from_autoset(autoset,slot)
  110. if slot == 'head' then
  111. local tempHead = settings.autosets[autoset].head:lower()
  112. if tempHead ~= nil then
  113. for att in atts:it() do
  114. if att.name:lower() == tempHead and att.id >5000 then
  115. windower.ffxi.set_attachment(att.id)
  116. break
  117. end
  118. end
  119. end
  120. windower.send_command('@wait .5;lua i autocontrol set_attachments_from_autoset '..autoset..' frame')
  121. elseif slot == 'frame' then
  122. local tempFrame = settings.autosets[autoset].frame:lower()
  123. if tempFrame ~= nil then
  124. for att in atts:it() do
  125. if att.name:lower() == tempFrame and att.id >5000 then
  126. windower.ffxi.set_attachment(att.id)
  127. break
  128. end
  129. end
  130. end
  131. windower.send_command('@wait .5;lua i autocontrol set_attachments_from_autoset '..autoset..' 1')
  132. else
  133. local islot
  134. if tonumber(slot) < 10 then
  135. islot = '0'..slot
  136. else islot = slot end
  137. local tempname = settings.autosets[autoset]['slot'..islot]:lower()
  138. if tempname ~= nil then
  139. for att in atts:it() do
  140. if att.name:lower() == tempname and att.id >5000 then
  141. windower.ffxi.set_attachment(att.id, tonumber(slot))
  142. break
  143. end
  144. end
  145. end
  146. if tonumber(slot) < 12 then
  147. windower.send_command('@wait .5;lua i autocontrol set_attachments_from_autoset '..autoset..' '..slot+1)
  148. else
  149. log(windower.ffxi.get_mjob_data().name..' has been equipped with the '..autoset..' set.')
  150. if petlessZones:contains(windower.ffxi.get_info().zone) then
  151. return
  152. else
  153. if windower.ffxi.get_ability_recasts()[205] == 0 then
  154. windower.send_command('input /ja "Activate" <me>')
  155. else
  156. log('Unable to reactivate. Activate timer was not ready.')
  157. end
  158. end
  159. end
  160. end
  161. end
  162. function get_current_autoset()
  163. if windower.ffxi.get_player().main_job_id == 18 then
  164. local autoTable = T{}
  165. local tmpTable = T{}
  166. local tmpTable = T(windower.ffxi.get_mjob_data().attachments)
  167. local i
  168. for i = 1, 12 do
  169. local t = ''
  170. if tmpTable[i] then
  171. if i < 10 then t = '0' end
  172. autoTable['slot'..t..i] = atts[tonumber(tmpTable[i])].name:lower()
  173. end
  174. end
  175. local headnum = windower.ffxi.get_mjob_data().head
  176. local framenum = windower.ffxi.get_mjob_data().frame
  177. autoTable.head = atts[headnum].name:lower()
  178. autoTable.frame = atts[framenum].name:lower()
  179. return autoTable
  180. end
  181. end
  182. function save_set(setname)
  183. if setname == 'default' then
  184. error('Please choose a name other than default.')
  185. return
  186. end
  187. local curAuto = T(get_current_autoset())
  188. settings.autosets[setname] = curAuto
  189. config.save(settings, 'all')
  190. notice('Set '..setname..' saved.')
  191. end
  192. function get_autoset_list()
  193. log("Listing sets:")
  194. for key,_ in pairs(settings.autosets) do
  195. if key ~= 'default' then
  196. local it = 0
  197. for i = 1, #settings.autosets[key] do
  198. it = it + 1
  199. end
  200. log("\t"..key..': '..(settings.autosets[key]:length()-2)..' attachments.')
  201. end
  202. end
  203. end
  204. function get_autoset_content(autoset)
  205. log('Getting '..autoset..'\'s attachment list:')
  206. settings.autosets[autoset]:vprint()
  207. end
  208. windower.register_event("addon command", function(comm, ...)
  209. if windower.ffxi.get_player().main_job_id ~= 18 then
  210. error('You are not on Puppetmaster.')
  211. return nil
  212. end
  213. local args = L{...}
  214. comm = comm or 'help'
  215. if comm == 'saveset' then
  216. if args[1] then
  217. save_set(args[1])
  218. end
  219. elseif comm == 'add' then
  220. if args[2] then
  221. local slot = table.remove(args,1)
  222. local attach = args:sconcat()
  223. add_attachment(attach,slot)
  224. end
  225. elseif comm == 'equipset' then
  226. if args[1] then
  227. attach_set(args[1])
  228. end
  229. elseif comm == 'setlist' then
  230. get_autoset_list()
  231. elseif comm == 'attlist' then
  232. if args[1] then
  233. get_autoset_content(args[1])
  234. end
  235. elseif comm == 'list' then
  236. get_current_autoset():vprint()
  237. elseif S{'fonttype','fontsize','pos','bgcolor','txtcolor'}:contains(comm) then
  238. if comm == 'fonttype' then Burden_tb:font(args[1])
  239. elseif comm == 'fontsize' then Burden_tb:size(args[1])
  240. elseif comm == 'pos' then Burden_tb:pos(args[1], args[2])
  241. elseif comm == 'bgcolor' then Burden_tb:bgcolor(args[1], args[2], args[3])
  242. elseif comm == 'txtcolor' then Burden_tb:color(args[1], args[2], args[3])
  243. end
  244. config.save(settings, 'all')
  245. elseif comm == 'show' then Burden_tb:show()
  246. elseif comm == 'hide' then Burden_tb:hide()
  247. elseif comm == 'settings' then
  248. log('BG: R: '..settings.bg.red..' G: '..settings.bg.green..' B: '..settings.bg.blue)
  249. log('Font: '..settings.text.font..' Size: '..settings.text.size)
  250. log('Text: R: '..settings.text.red..' G: '..settings.text.green..' B: '..settings.text.blue)
  251. log('Position: X: '..settings.pos.x..' Y: '..settings.pos.y)
  252. else
  253. local helptext = [[Autosets command list:
  254. 1. help - Brings up this menu.
  255. 2. setlist - list all saved automaton sets.
  256. 3. saveset <setname> - saves <setname> to your settings.
  257. 4. equipset <setname> - equips <setname> to your automaton.
  258. 5. attlist <setname> - gets the attachment list for <setname>
  259. 6. list - gets the list of currently equipped attachments.
  260. The following all correspond to the burden tracker:
  261. fonttype <name> | fontsize <size> | pos <x> <y>
  262. bgcolor <r> <g> <b> | txtcolor <r> <g> <b>
  263. settings - shows current settings
  264. show/hide - toggles visibility of the tracker so you can make changes.]]
  265. for _, line in ipairs(helptext:split('\n')) do
  266. windower.add_to_chat(207, line..chat.controls.reset)
  267. end
  268. end
  269. end)