Advertisement
Guest User

Untitled

a guest
Feb 25th, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.63 KB | None | 0 0
  1. --Copyright (c) 2013~2016, Byrthnoth
  2. --All rights reserved.
  3.  
  4. --Redistribution and use in source and binary forms, with or without
  5. --modification, are permitted provided that the following conditions are met:
  6.  
  7. -- * Redistributions of source code must retain the above copyright
  8. -- notice, this list of conditions and the following disclaimer.
  9. -- * Redistributions in binary form must reproduce the above copyright
  10. -- notice, this list of conditions and the following disclaimer in the
  11. -- documentation and/or other materials provided with the distribution.
  12. -- * Neither the name of <addon name> nor the
  13. -- names of its contributors may be used to endorse or promote products
  14. -- derived from this software without specific prior written permission.
  15.  
  16. --THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. --ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. --WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. --DISCLAIMED. IN NO EVENT SHALL <your name> BE LIABLE FOR ANY
  20. --DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. --(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. --LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. --ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. --(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. --SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  
  27.  
  28. -- Functions that are directly exposed to users --
  29.  
  30.  
  31. function set_language(lang)
  32. if _global.current_event ~= 'get_sets' then
  33. error('\nGearSwap: set_language() is only valid in the get_sets function', 2)
  34. return
  35. end
  36. if lang and type(lang) == 'string' and (lang == 'english' or lang == 'japanese') then
  37. rawset(_G,'language',lang)
  38. refresh_globals()
  39. else
  40. error('\nGearSwap: set_language() was passed an invalid value ('..tostring(lang)..'). (must be a string)', 2)
  41. end
  42. end
  43.  
  44. function debug_mode(boolean)
  45. if type(boolean) == "boolean" then _settings.debug_mode = boolean
  46. elseif boolean == nil then
  47. _settings.debug_mode = true
  48. else
  49. error('\nGearSwap: show_swaps() was passed an invalid value ('..tostring(boolean)..'). (true/no value/nil=on, false=off)', 2)
  50. end
  51. end
  52.  
  53. function show_swaps(boolean)
  54. if type(boolean) == "boolean" then _settings.show_swaps = boolean
  55. elseif boolean == nil then
  56. _settings.show_swaps = true
  57. else
  58. error('\nGearSwap: show_swaps() was passed an invalid value ('..tostring(boolean)..'). (true/no value/nil=on, false=off)', 2)
  59. end
  60. end
  61.  
  62. function cancel_spell(boolean)
  63. if _global.current_event ~= 'precast' and _global.current_event ~= 'pretarget' and _global.current_event ~= 'filtered_action' then
  64. error('\nGearSwap: cancel_spell() is only valid in the precast, pretarget, or filtered_action functions', 2)
  65. return
  66. end
  67. if type(boolean) == "boolean" then _global.cancel_spell = boolean
  68. elseif boolean == nil then
  69. _global.cancel_spell = true
  70. else
  71. error('\nGearSwap: cancel_spell() was passed an invalid value ('..tostring(boolean)..'). (true/no value/nil=Cancel the spell, false=do not cancel the spell)', 2)
  72. end
  73. end
  74.  
  75. function move_spell_target(position_table)
  76. if _global.current_event ~= 'precast' then
  77. error('\nGearSwap: move_spell_target() is only valid in the precast function', 2)
  78. return
  79. end
  80.  
  81. if type(position_table) == 'table' and type(position_table.x or position_table.X) == 'number' and
  82. type(position_table.y or position_table.Y) == 'number' and
  83. type(position_table.z or positino_table.Z) == 'number' then
  84. _global.target_arrow.x = position_table.x or position_table.X
  85. _global.target_arrow.y = position_table.y or position_table.Y
  86. _global.target_arrow.z = position_table.z or position_table.Z
  87. print_set(_global.target_arrow)
  88. else
  89. error('\nGearSwap: move_spell_target() was passed an invalid value ('..tostring(position_table)..'). Should be a table with x, y, and z keys (offset from target)', 2)
  90. end
  91. end
  92.  
  93. function change_target(name)
  94. if _global.current_event ~= 'pretarget' then
  95. error('\nGearSwap: change_target() is only valid in the pretarget function', 2)
  96. return
  97. end
  98. if name and type(name)=='string' then
  99. if valid_target(name) then
  100. _,_global.new_target = valid_target(name)
  101. else
  102. error('\nGearSwap: change_target() was passed an invalid value ('..tostring(name)..'). (must be a valid target)', 2)
  103. end
  104. else
  105. error('\nGearSwap: change_target() was passed an invalid value ('..tostring(name)..'). (must be a string)', 2)
  106. end
  107. end
  108.  
  109. function cast_delay(delay)
  110. if _global.current_event ~= 'precast' and _global.current_event ~= 'pretarget' then
  111. error('\nGearSwap: cast_delay() is only valid in the precast and pretarget functions', 2)
  112. return
  113. end
  114. if tonumber(delay) then
  115. _global[_global.current_event.."_cast_delay"] = tonumber(delay)
  116. else
  117. error('\nGearSwap: cast_delay() was passed an invalid value ('..tostring(delay)..'). (cast delay must be a number of seconds)', 2)
  118. end
  119. end
  120.  
  121. -- Combines the provided gear sets into a new set. Returns the result.
  122. function set_combine(...)
  123. return set_merge(false,{}, ...)
  124. end
  125.  
  126. -- Combines the provided gear sets into the equip_list set.
  127. function equip(...)
  128. set_merge(true,equip_list, ...)
  129. end
  130.  
  131. function disable(...)
  132. local disable_tab = {...}
  133. if type(disable_tab[1]) == 'table' then
  134. disable_tab = disable_tab[1] -- Compensates for people passing a table instead of a series of strings.
  135. end
  136. for i,v in pairs(disable_tab) do
  137. if slot_map[v] then
  138. rawset(disable_table,slot_map[v],true)
  139. else
  140. error('\nGearSwap: disable error, passed an unrecognized slot name. ('..tostring(v)..')',2)
  141. end
  142. end
  143. end
  144.  
  145. function enable(...)
  146. local enable_tab = {...}
  147. if type(enable_tab[1]) == 'table' then
  148. enable_tab = enable_tab[1] -- Compensates for people passing a table instead of a series of strings.
  149. end
  150. local sending_table = {}
  151. for i,v in pairs(enable_tab) do
  152. local local_slot = get_default_slot(v)
  153. if local_slot then
  154. disable_table[toslotid(v)] = false
  155. if not_sent_out_equip[local_slot] then
  156. sending_table[local_slot] = not_sent_out_equip[local_slot]
  157. not_sent_out_equip[local_slot] = nil
  158. end
  159. else
  160. error('\nGearSwap: enable error, passed an unrecognized slot name. ('..tostring(v)..')',2)
  161. end
  162. end
  163.  
  164. return sending_table
  165. end
  166.  
  167. function user_enable(...)
  168. local sending_table = enable(...)
  169.  
  170. if table.length(sending_table) > 0 then
  171. equip(sending_table)
  172. end
  173. return sending_table
  174. end
  175.  
  176. function command_enable(...)
  177. local sending_table = enable(...)
  178.  
  179. if table.length(sending_table) > 0 then
  180. refresh_globals()
  181. equip_sets('equip_command',nil,sending_table)
  182. end
  183. end
  184.  
  185. function print_set(set,title)
  186. if not set then
  187. if title then
  188. error('\nGearSwap: print_set error, '..windower.to_shift_jis(tostring(title))..' set is nil.', 2)
  189. else
  190. error('\nGearSwap: print_set error, set is nil.', 2)
  191. end
  192. return
  193. elseif type(set) ~= 'table' then
  194. if title then
  195. error('\nGearSwap: print_set error, '..windower.to_shift_jis(tostring(title))..' set is not a table.', 2)
  196. else
  197. error('\nGearSwap: print_set error, set is not a table.', 2)
  198. end
  199. end
  200. if table.length(set) == 0 then
  201. if title then
  202. msg.add_to_chat(1,'------------------'.. windower.to_shift_jis(tostring(title))..' -- Empty Table -----------------')
  203. else
  204. msg.add_to_chat(1,'-------------------------- Empty Table -------------------------')
  205. end
  206. return
  207. elseif title then
  208. msg.add_to_chat(1,'------------------------- '..windower.to_shift_jis(tostring(title))..' -------------------------')
  209. else
  210. msg.add_to_chat(1,'----------------------------------------------------------------')
  211. end
  212. local function print_element(key,value)
  213. if type(value) == 'table' and value.name then
  214. msg.add_to_chat(8,windower.to_shift_jis(tostring(key))..' '..windower.to_shift_jis(tostring(value.name))..' (Adv.)')
  215. else
  216. msg.add_to_chat(8,windower.to_shift_jis(tostring(key))..' '..windower.to_shift_jis(tostring(value)))
  217. end
  218. end
  219. local function cmp_key(key,tab)
  220. for k in pairs(tab) do
  221. if k:lower() == key:lower() then
  222. return k
  223. end
  224. end
  225. end
  226.  
  227. if #set == table.length(set) then -- If it is a list (keyed by continuous whole number starting at 1), then print it out in order
  228. for key,value in ipairs(set) do
  229. print_element(key,value)
  230. end
  231. else -- Otherwise, try to print out the gear in order and then everything else.
  232. for _,key in ipairs({'main','sub','ranged','range','ammo','head','neck','lear','ear1','learring','left_ear','rear','ear2','rearring','right_ear','body','hands','lring','ring1','left_ring','rring','ring2','right_ring','back','waist','legs','feet'}) do
  233. local k = cmp_key(key,set)
  234. if k then
  235. print_element(k,set[k])
  236. end
  237. end
  238. for key,value in pairs(set) do
  239. if not slot_map[key] then
  240. print_element(key,set[key])
  241. end
  242. end
  243. end
  244. msg.add_to_chat(1,'----------------------------------------------------------------')
  245. end
  246.  
  247. function send_cmd_user(command)
  248. local com
  249. if string.byte(1) ~= 0x40 then --i do not understand what this is for
  250. com = '@'..command
  251. end
  252. windower.send_command(com)
  253. end
  254.  
  255. function register_event_user(str,func)
  256. if type(func)~='function' then
  257. error('\nGearSwap: windower.register_event() was passed an invalid value ('..tostring(func)..'). (must be a function)', 2)
  258. elseif type(str) ~= 'string' then
  259. error('\nGearSwap: windower.register_event() was passed an invalid value ('..tostring(str)..'). (must be a string)', 2)
  260. end
  261. local id = windower.register_event(str,user_equip_sets(func))
  262. registered_user_events[id] = true
  263. return id
  264. end
  265.  
  266. function raw_register_event_user(str,func)
  267. if type(func)~='function' then
  268. error('\nGearSwap: windower.register_event() was passed an invalid value ('..tostring(func)..'). (must be a function)', 2)
  269. elseif type(str) ~= 'string' then
  270. error('\nGearSwap: windower.register_event() was passed an invalid value ('..tostring(str)..'). (must be a string)', 2)
  271. end
  272. local id = windower.register_event(str,setfenv(func,user_env))
  273. registered_user_events[id] = true
  274. return id
  275. end
  276.  
  277. function unregister_event_user(id)
  278. if type(id)~='number' then
  279. error('\nGearSwap: windower.unregister_event() was passed an invalid value ('..tostring(id)..'). (must be a number)', 2)
  280. end
  281. windower.unregister_event(id)
  282. registered_user_events[id] = nil
  283. end
  284.  
  285. function user_equip_sets(func)
  286. return setfenv(function(...)
  287. if not gearswap.gearswap_disabled then
  288. gearswap.refresh_globals(true)
  289. return gearswap.equip_sets(func,nil,...)
  290. end
  291. end,user_env)
  292. end
  293.  
  294. function user_unhandled_command(func)
  295. if type(func) ~= 'function' then
  296. error('\nGearSwap: unhandled_command was passed an invalid value ('..tostring(func)..'). (must be a function)', 2)
  297. end
  298. unhandled_command_events[#unhandled_command_events+1] = setfenv(func,user_env)
  299. end
  300.  
  301. function include_user(str, load_include_in_this_table)
  302. if not (type(str) == 'string') then
  303. error('\nGearSwap: include() was passed an invalid value ('..tostring(str)..'). (must be a string)', 2)
  304. end
  305.  
  306. str = str:lower()
  307. if type(package.loaded[str]) == 'table' then
  308. return package.loaded[str]
  309. elseif T{'pack'}:contains(str) then
  310. return
  311. end
  312.  
  313. if str:sub(-4)~='.lua' then str = str..'.lua' end
  314. local path, loaded_values = pathsearch({str})
  315.  
  316. if not path then
  317. error('\nGearSwap: Cannot find the include file ('..tostring(str)..').', 2)
  318. end
  319.  
  320. local f, err = loadfile(path)
  321. if f and not err then
  322. if load_include_in_this_table and type(load_include_in_this_table) == 'table' then
  323. setmetatable(load_include_in_this_table, {__index=user_env._G})
  324. setfenv(f, load_include_in_this_table)
  325. pcall(f, load_include_in_this_table)
  326. return load_include_in_this_table
  327. else
  328. setfenv(f,user_env)
  329. return f()
  330. end
  331. else
  332. error('\nGearSwap: Error loading file ('..tostring(str)..'): '..err, 2)
  333. end
  334. end
  335.  
  336. -- Allow the user to set a path subdirectory to check when searching for included files.
  337. -- This path is checked as a subdirectory to each fixed path, before the fixed path itself is checked.
  338. -- Path argument can only be a string; otherwise this is set to nil.
  339. function user_include_path(path)
  340. if type(path) == 'string' then
  341. include_user_path = path
  342. else
  343. include_user_path = nil
  344. end
  345. end
  346.  
  347.  
  348. function user_midaction(bool)
  349. if bool == false then
  350. for i,v in pairs(command_registry) do
  351. if v.midaction then
  352. command_registry[i].midaction = false
  353. end
  354. end
  355. end
  356.  
  357. for i,v in pairs(command_registry) do
  358. if type(v) == 'table' and v.midaction then
  359. return true, v.spell
  360. end
  361. end
  362.  
  363. return false
  364. end
  365.  
  366. function user_pet_midaction(bool)
  367. if bool == false then
  368. for i,v in pairs(command_registry) do
  369. if v.pet_midaction then
  370. command_registry.pet_midaction = false
  371. end
  372. end
  373. end
  374.  
  375. for i,v in pairs(command_registry) do
  376. if v.pet_midaction then
  377. return true, v.spell
  378. end
  379. end
  380.  
  381. return false
  382. end
  383.  
  384. function add_to_chat_user(num,str)
  385. local backup_str
  386. if type(num) == 'string' then
  387. -- It was passed a string as the first argument.
  388. str = not tonumber(str) and str or num
  389. num = 8
  390. elseif not num and str and type(str) == 'string' then
  391. -- It only needs the number.
  392. num=8
  393. end
  394.  
  395. if language == 'japanese' then
  396. msg.add_to_chat(num,windower.to_shift_jis(str))
  397. else
  398. msg.add_to_chat(num,str)
  399. end
  400. end
  401.  
  402.  
  403. function user_sleep(delay)
  404. if not delay then
  405. error('\nGearSwap: coroutine.sleep() not passed a delay value', 2)
  406. elseif type(delay) ~= 'number' or delay < 0 then
  407. error('\nGearSwap: coroutine.sleep() was passed an invalid value ('..tostring(delay)..'). (must be a number >= 0)', 2)
  408. else
  409. coroutine.yield('sleep',delay)
  410. end
  411. end
  412.  
  413. function user_yield()
  414. coroutine.yield('yield')
  415. end
  416.  
  417.  
  418. -- Define the user windower functions.
  419. user_windower = {register_event = register_event_user, raw_register_event = raw_register_event_user,
  420. unregister_event = unregister_event_user, send_command = send_cmd_user,add_to_chat=add_to_chat_user}
  421. user_coroutine = coroutine
  422. user_coroutine.sleep = user_sleep
  423. user_coroutine.yield = user_yield
  424. setmetatable(user_windower,{__index=windower})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement