summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/lua-addons/addons/SpellBook/SpellBook.lua
blob: 311f287e06cd94833390a8acf36fb478ba8c251d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
_addon.name     = 'SpellBook'
_addon.author   = 'SigilBaram'
_addon.version  = '1.0.1'
_addon.commands = {'spellbook','spbk'}

require('tables')
res = require('resources')

spell_types = {
    whitemagic  = { type = 'WhiteMagic',    readable = 'White Magic spells' },
    blackmagic  = { type = 'BlackMagic',    readable = 'Black Magic spells' },
    songs       = { type = 'BardSong',      readable = 'Bard songs' },
    ninjutsu    = { type = 'Ninjutsu',      readable = 'Ninjutsu' },
    summoning   = { type = 'SummonerPact',  readable = 'Summoning spells' },
    bluemagic   = { type = 'BlueMagic',     readable = 'Blue Magic spells' },
    geomancy    = { type = 'Geomancy',      readable = 'Geomancy spells' },
    trusts      = { type = 'Trust',         readable = 'Trusts'},
    all         = { type = 'all',           readable = 'spells of all types'}
}

windower.register_event('addon command', function (command, ...)
    local args = L{...}
    local jobs = build_job_list()

    command = command and command:lower() or 'current'

    if command == 'help' then
        display_help()
    elseif command == 'current' then
        local player = windower.ffxi.get_player()
        spells_by_current(player)
    elseif command == 'main' then
        local player = windower.ffxi.get_player()
        local level = player.main_job_level
        local job_points = player.job_points[player.main_job:lower()].jp_spent
        if job_points > 99 then
            level = job_points
        end
        level = args[1] or level
        if level == 'all' then
            level = 1500
        end
        level = tonumber(level)
        if level then
            spells_by_job(player.main_job_id, level)
        else
            invalid_input()
        end
    elseif command == 'sub' then
        local player = windower.ffxi.get_player()
        if not player.sub_job then
            windower.add_to_chat(7, "You don't have a subjob equipped.")
            return
        end
        local level = args[1] or player.sub_job_level
        if level == 'all' then
            level = 1500
        end
        level = tonumber(level)
        if level then
            spells_by_job(player.sub_job_id, level)
        else
            invalid_input()
        end
    elseif spell_types[command] then
        if args[1] == 'all' then
            local player = windower.ffxi.get_player()
            spells_by_type(player, spell_types[command], false)
        elseif args[1] == nil then
            local player = windower.ffxi.get_player()
            spells_by_type(player, spell_types[command], true)
        else
            invalid_input()
        end
    elseif jobs[command] then
        local job = jobs[command]
        local player = windower.ffxi.get_player()
        local level = args[1] or player.jobs[res.jobs[job].english_short]
        if level == 'all' then
            level = 1500
        end
        level = tonumber(level)
        if level then
            spells_by_job(job, level)
        else
            invalid_input()
        end
    else
        invalid_input()
    end
end)

--------------------------------------------------------------------------------
--Name: build_job_list
--------------------------------------------------------------------------------
--Returns:
---- (table) list of jobs with short name as the key and id as the value
--------------------------------------------------------------------------------
function build_job_list()
    local jobs = {}
    for id,val in pairs(res.jobs) do
        jobs[val.english_short:lower()] = id
    end
    return jobs
end

--------------------------------------------------------------------------------
--Name: invalid_input
---- Display an error message for invalid input.
--------------------------------------------------------------------------------
function invalid_input()
    windower.add_to_chat(7, 'Invalid input. See //spbk help.')
end

--------------------------------------------------------------------------------
--Name: display_help
-- Display help text for the addon.
--------------------------------------------------------------------------------
function display_help()
    windower.add_to_chat(7, _addon.name .. ' version ' .. _addon.version)
    windower.add_to_chat(7, 'Spent jp can be specified for spells learned from Gifts by entering a value of 100-1500.')
    windower.add_to_chat(7, 'Spells are never given as Gifts for less than 100 jp and values under 100 are treated as level.')
    windower.add_to_chat(7, '//spbk help -- Show this help text.')
    windower.add_to_chat(7, '//spbk [current] -- Show learnable spells based on current main and sub job and level/jp.')
    windower.add_to_chat(7, '//spbk <main|sub> [<level|spent jp|all>] -- Show missing spells for current main or sub job. Defaults to the job\'s current level/jp.')
    windower.add_to_chat(7, '//spbk <job> [<level|spent jp|all>] -- Show missings spells for specified job and level. Defaults to the job\'s level/jp.')
    windower.add_to_chat(7, '//spbk <category> [all] -- Show learnable spells by category. Limited to spells which are learnable, unless all is added after the category.')
    windower.add_to_chat(7, 'Categories: whitemagic, blackmagic, songs, ninjustu, summoning, bluemagic, geomancy, trusts, all (Trusts are not included in all)')
end

--------------------------------------------------------------------------------
--Name: is_learnable
--Args:
---- player (table): player object from windower.ffxi.get_player()
---- spell (table): a spell from resources.spells
--------------------------------------------------------------------------------
--Returns:
---- (bool) true if player has a job that is high enough level to learn spell
--------------------------------------------------------------------------------
function is_learnable(player, spell)
    local player_levels = player.jobs
    for job,level in pairs(spell.levels) do
        if player_levels[res.jobs[job].english_short] >= level then
            return true
        end
    end
    return false
end

--------------------------------------------------------------------------------
--Name: format_spell
-- Formats a spell as the spell's name followed by a list of jobs and levels
-- which would qualify to learn that spell.
--Args:
---- spell (table): a spell from resources.spells
--------------------------------------------------------------------------------
--Returns:
---- (string) the formatted string
--------------------------------------------------------------------------------
function format_spell(spell)
    local format

    if spell.type ~= 'Trust' then
        local jobs = T{}
        local levels = T{}
        for job_id,_ in pairs(spell.levels) do
            jobs:append(job_id)
        end
        jobs:sort()
        for _,job_id in ipairs(jobs) do
            if spell.levels[job_id] <= 99 then
                levels:append(res.jobs[job_id].english_short .. ' Lv.' ..
                    tostring(spell.levels[job_id]))
            else
                levels:append(res.jobs[job_id].english_short .. ' Jp.' ..
                    tostring(spell.levels[job_id]))
            end
        end
        format = levels:concat(' / ')
    else
        format = ' ( Trust )'
    end
    return string.format('%-20s %s', spell.english, format)
end

--------------------------------------------------------------------------------
--Name: spells_by_type
-- List spells of a given type, i.e. white magic.
--Args:
---- player (T): player object from windower.ffxi.get_player()
---- spell_type (table): one of the types from spell_types global
---- learnable_only (bool): if true then the output is limited to spell for
----     which the player has a job that is high enough level
--------------------------------------------------------------------------------
function spells_by_type(player, spell_type, learnable_only)
    local missing_spells = T{}
    local player_spells = windower.ffxi.get_spells()
    local spell_count = 0

    for spell_id,spell in pairs(res.spells) do
        if ((spell_type.type == 'all' and spell.type ~= 'Trust') or
            spell.type == spell_type.type) and
            not table.empty(spell.levels) and
            not player_spells[spell_id] and
            (is_learnable(player, spell) or not learnable_only) and
            not (spell_type.type == 'Trust' and spell.name:match('.*%(UC%)')) and
            not spell.unlearnable then

            missing_spells:append(format_spell(spell))
            spell_count = spell_count + 1
        end
    end

    if learnable_only then
        windower.add_to_chat(7, string.format('Showing learnable %s.',
            spell_type.readable))
    else
        windower.add_to_chat(7, string.format('Showing all missing %s.',
            spell_type.readable))
    end

    if not missing_spells:empty() then
        missing_spells:sort()
        for _,spell in ipairs(missing_spells) do
            windower.add_to_chat(7, spell)
        end
        if learnable_only then
            windower.add_to_chat(7,string.format(
                'List Complete. You are missing %d learnable %s.',
                spell_count, spell_type.readable))
        else
            windower.add_to_chat(7,string.format(
                'List Complete. You are missing %d %s.',
                spell_count, spell_type.readable))
        end
    else
        if learnable_only then
            windower.add_to_chat(7,string.format(
                'Congratulations! You know all currently learnable %s.',
                spell_type.readable))
        else
            windower.add_to_chat(7,string.format(
                'Congratulations! You know all %s.',
                spell_type.readable))
        end
    end
end

--------------------------------------------------------------------------------
--Name: spells_by_job
-- List unknown spells by job.
--Args:
---- job (int): the job's id
---- level_cap (int): the max level/jp required by listed spells
--------------------------------------------------------------------------------
function spells_by_job(job, level_cap)
    local missing_spells = T{}
    local player_spells = windower.ffxi.get_spells()
    local spell_count = 0

    for spell_id,spell in pairs(res.spells) do
        local spell_level = spell.levels[job]
        if spell_level and spell_level <= level_cap and
            spell.type ~= 'Trust' and not player_spells[spell_id] and
            not spell.unlearnable then

            missing_spells[spell_level] = missing_spells[spell_level] or T{}
            missing_spells[spell_level]:append(spell.english)
            spell_count = spell_count + 1
        end
    end

    if not missing_spells:empty() then
        if level_cap > 99 then
            windower.add_to_chat(7, string.format(
                'Showing missing spells for %s up to %d spent job points.',
                res.jobs[job].en, level_cap))
        else
            windower.add_to_chat(7, string.format(
                'Showing missing spells for %s up to level %d.',
                res.jobs[job].en, level_cap))
        end

        for level=1,level_cap do
            if missing_spells[level] then
                missing_spells[level]:sort()
                if level > 99 then
                    windower.add_to_chat(7,
                        level .. 'jp: ' .. missing_spells[level]:concat(', '))
                else
                    windower.add_to_chat(7,
                        level .. ': ' .. missing_spells[level]:concat(', '))
                end
            end
        end
        if level_cap > 99 then
            windower.add_to_chat(7, string.format(
                'List Complete. You are missing %d %s spells up to %d spent job points.',
                spell_count, res.jobs[job].en, level_cap))
        else
            windower.add_to_chat(7, string.format(
                'List Complete. You are missing %d %s spells up to level %d.',
                spell_count, res.jobs[job].en, level_cap))
        end
    else
        if level_cap >= 1500 then
            windower.add_to_chat(7,string.format(
                'Congratulations! You know all spells for %s.',
                res.jobs[job].en))
        elseif level_cap > 99 then
            windower.add_to_chat(7,string.format(
                'Congratulations! You know all spells for %s up to %d spent job points!',
                res.jobs[job].en, level_cap))
        else
            windower.add_to_chat(7,string.format(
                'Congratulations! You know all spells for %s up to level %d!',
                res.jobs[job].en, level_cap))
        end
    end
end

--------------------------------------------------------------------------------
--Name: spells_by_current
-- Show missing spells for the current main and sub jobs.
--Args:
---- player (T): player object from windower.ffxi.get_player()
--------------------------------------------------------------------------------
function spells_by_current(player)
    local missing_spells = T{}
    local player_spells = windower.ffxi.get_spells()
    local spell_count = 0


    local main_job_level = player.main_job_level
    local main_job_jp = player.job_points[player.main_job:lower()].jp_spent

    local main_level
    local sub_level

    -- If the player has over 99 spend jp, then switch to treating JP as level.
    if main_job_jp > 99 then
        main_job_level = main_job_jp
    end

    for spell_id,spell in pairs(res.spells) do
        local main_level = spell.levels[player.main_job_id]
        local sub_level = spell.levels[player.sub_job_id]
        local spell_level = nil


        if main_level and main_level > main_job_level then
            main_level = nil
        end
        if sub_level and sub_level > player.sub_job_level then
            sub_level = nil
        end

        if main_level and sub_level then
            spell_level = math.min(main_level, sub_level)
        else
            spell_level = spell_level or main_level or sub_level
        end

        if spell_level and spell.type ~= 'Trust' and
            not player_spells[spell_id] and not spell.unlearnable then

            missing_spells:append(format_spell(spell))
            spell_count = spell_count + 1
        end
    end

    if not missing_spells:empty() then
        if main_job_jp > 0 then
            if player.sub_job then
                windower.add_to_chat(7, string.format('Showing learnable spells for %s%d with %d spent job points and %s%d.',
                    player.main_job, player.main_job_level,
                    player.job_points[player.main_job:lower()].jp_spent,
                    player.sub_job, player.sub_job_level))
            else
                windower.add_to_chat(7, string.format('Showing learnable spells for %s%d with %d spent job points.',
                    player.main_job, player.main_job_level,
                    player.job_points[player.main_job:lower()].jp_spent))
            end
        else
            if player.sub_job then
                windower.add_to_chat(7, string.format('Showing learnable spells for %s%d/%s%d.',
                    player.main_job, player.main_job_level,
                    player.sub_job, player.sub_job_level))
            else
                windower.add_to_chat(7, string.format('Showing learnable spells for %s%d.',
                    player.main_job, player.main_job_level))
            end
        end

        missing_spells:sort()
        for _,spell in ipairs(missing_spells) do
            windower.add_to_chat(7, spell)
        end
        if player.sub_job then
            windower.add_to_chat(7, string.format('List Complete. You are missing %d learnable spells for %s%d/%s%d.',
                spell_count,
                player.main_job, player.main_job_level,
                player.sub_job, player.sub_job_level))
        else
            windower.add_to_chat(7, string.format('List Complete. You are missing %d learnable spells for %s%d.',
                spell_count,
                player.main_job, player.main_job_level))
        end
    else
        if player.sub_job then
            windower.add_to_chat(7,  string.format('Congratulations! You know all learnable spells for %s%d/%s%d!',
                player.main_job, player.main_job_level,
                player.sub_job, player.sub_job_level))
        else
            windower.add_to_chat(7,  string.format('Congratulations! You know all learnable spells for %s%d!',
                player.main_job, player.main_job_level))
        end
    end
end

--[[
Copyright © 2018, John S Hobart
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of SpellBook nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL John S Hobart BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--]]