summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/lua-addons/addons/EmpyPopTracker/EmpyPopTracker.lua
blob: a6ae2aa5493b7ea6d78625a72fd3c0995b4e9dcf (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
--[[
Copyright © 2020, Dean James (Xurion of Bismarck)
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 Empy Pop Tracker 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 Dean James (Xurion of Bismarck) 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.
]]

_addon.name = 'Empy Pop Tracker'
_addon.author = 'Dean James (Xurion of Bismarck)'
_addon.commands = { 'ept', 'empypoptracker' }
_addon.version = '2.5.0'

config = require('config')
res = require('resources')
nm_data = require('nms/index')

active = false

local EmpyPopTracker = {}

local defaults = {}
defaults.text = {}
defaults.text.pos = {}
defaults.text.pos.x = 0
defaults.text.pos.y = 0
defaults.text.bg = {}
defaults.text.bg.alpha = 150
defaults.text.bg.blue = 0
defaults.text.bg.green = 0
defaults.text.bg.red = 0
defaults.text.bg.visible = true
defaults.text.padding = 8
defaults.text.text = {}
defaults.text.text.font = 'Consolas'
defaults.text.text.size = 10
defaults.tracking = 'briareus'
defaults.visible = true
defaults.add_to_chat_mode = 8
defaults.colors = {}
defaults.colors.needed = {}
defaults.colors.needed.red = 255
defaults.colors.needed.green = 50
defaults.colors.needed.blue = 50
defaults.colors.obtained = {}
defaults.colors.obtained.red = 100
defaults.colors.obtained.green = 255
defaults.colors.obtained.blue = 100
defaults.colors.pool = {}
defaults.colors.pool.red = 255
defaults.colors.pool.green = 170
defaults.colors.pool.blue = 0
defaults.colors.bg = {}
defaults.colors.bg.red = 0
defaults.colors.bg.green = 0
defaults.colors.bg.blue = 0
defaults.colors.bgall = {}
defaults.colors.bgall.red = 0
defaults.colors.bgall.green = 75
defaults.colors.bgall.blue = 0
defaults.collectables = true
defaults.expanded = true

EmpyPopTracker.settings = config.load(defaults)
EmpyPopTracker.text = require('texts').new(EmpyPopTracker.settings.text, EmpyPopTracker.settings)

function start_color(color)
    return '\\cs(' .. EmpyPopTracker.settings.colors[color].red .. ',' .. EmpyPopTracker.settings.colors[color].green .. ',' .. EmpyPopTracker.settings.colors[color].blue .. ')'
end

function owns_item(id, items)
    for _, bag in pairs(items) do
        if type(bag) == 'table' then
            for _, item in ipairs(bag) do
                if item.id == id then
                    return true
                end
            end
        end
    end

    return false
end

function get_item_count(id, items)
    local count = 0
    for _, bag in pairs(items) do
        if type(bag) == 'table' then
            for _, item in ipairs(bag) do
                if item.id == id then
                    count = count + item.count
                end
            end
        end
    end

    return count
end

function owns_key_item(id, items)
    local owned = false

    for _, item_id in pairs(items) do
        if item_id == id then
            owned = true
            break
        end
    end

    return owned
end

function item_treasure_pool_count(id, treasure)
    local count = 0

    for _, item in pairs(treasure) do
        if item.item_id == id then
            count = count + 1
        end
    end

    return count
end

function ucwords(str)
    local result = string.gsub(str, '(%a)([%w_\']*)', function(first, rest)
        return first:upper() .. rest:lower()
    end)

    return result
end

function get_indent(depth)
    return string.rep('  ', depth)
end

function generate_text(data, key_items, items, depth)
    local text = depth == 1 and data.name or ''
    for _, pop in pairs(data.pops) do
        local resource
        local item_scope
        local owns_pop
        local in_pool_count = 0
        local item_identifier = ''

        if pop.type == 'key item' then
            resource = res.key_items[pop.id]
            owns_pop = owns_key_item(pop.id, key_items)
            item_identifier = 'Ж '
        else
            resource = res.items[pop.id]
            owns_pop = owns_item(pop.id, items)
            in_pool_count = item_treasure_pool_count(pop.id, items.treasure)
        end

        local pop_name = 'Unknown pop'
        if resource then
            pop_name = ucwords(resource.name)
        end

        if depth == 1 and EmpyPopTracker.settings.expanded then
            text = text .. '\n'
        end

        local item_colour = start_color(owns_pop and 'obtained' or 'needed')
        local pool_notification = ''
        if in_pool_count > 0 then
            pool_notification = start_color('pool') .. ' [' .. in_pool_count .. ']' .. '\\cr'
        end

        local name_color = ''
        local name_color_end = ''
        if not EmpyPopTracker.settings.expanded and owns_pop then
            name_color = item_colour
            name_color_end = '\\cr'
        end

        text = text .. '\n' .. get_indent(depth) .. name_color .. pop.dropped_from.name .. name_color_end

        if EmpyPopTracker.settings.expanded then
            text = text .. '\n' .. get_indent(depth) .. ' >> ' .. item_colour .. item_identifier .. pop_name .. '\\cr' .. pool_notification
        end

        if pop.dropped_from.pops then
            text = text .. generate_text(pop.dropped_from, key_items, items, depth + 1)
        end
    end

    if data.collectable and EmpyPopTracker.settings.collectables then
        local count = get_item_count(data.collectable, items)
        local start = ''
        local finish = ''
        if count >= data.collectable_target_count then
            start = start_color('obtained')
            finish = '\\cr'
        end

        text = text .. '\n\n' .. start .. res.items[data.collectable].name .. ': ' .. count .. '/' .. data.collectable_target_count .. finish
    end

    return text
end

EmpyPopTracker.generate_info = function(nm, key_items, items)
    return {
        has_all_pops = not nm.pops or T(nm.pops):all(function(item)
            return item.type == 'item' and owns_item(item.id, items) or owns_key_item(item.id, key_items)
        end),
        text = generate_text(nm, key_items, items, 1)
    }
end

function find_nms(pattern)
    local matching_nms = {}
    local lower_pattern = pattern:lower()
    for _, nm in pairs(nm_data) do
        local nm_name = nm.name:lower()
        local result = windower.wc_match(nm_name, lower_pattern)
        if result then
            table.insert(matching_nms, nm_name)
        end
    end

    return matching_nms
end

windower.register_event('addon command', function(command, ...)
    command = command and command:lower() or 'help'

    if commands[command] then
        commands[command](...)
    else
        commands.help()
    end
end)

commands = {}

commands.track = function(...)
    local nm_search_pattern = table.concat({...}, ' ')
    local matching_nm_names = find_nms(nm_search_pattern)

    if #matching_nm_names == 0 then
        windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, 'Unable to find a NM using: "' .. nm_search_pattern .. '"')
    elseif #matching_nm_names > 1 then
        windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '"' .. nm_search_pattern .. '" matches ' .. #matching_nm_names .. ' NMs. Please be more explicit:')
        for key, matching_file_name in pairs(matching_nm_names) do
            windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '  Match ' .. key .. ': ' .. ucwords(matching_file_name))
        end
    else
        active = true
        windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, 'Now tracking: ' .. ucwords(matching_nm_names[1]))
        EmpyPopTracker.settings.tracking = matching_nm_names[1]
        EmpyPopTracker.update()
        commands.show()
    end
end
commands.t = commands.track

commands.hide = function()
    active = false
    EmpyPopTracker.text:visible(false)
    EmpyPopTracker.settings.visible = false
    EmpyPopTracker.settings:save()
end

commands.show = function()
    active = true
    EmpyPopTracker.text:visible(true)
    EmpyPopTracker.settings.visible = true
    EmpyPopTracker.settings:save()
    EmpyPopTracker.update()
end

commands.help = function()
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '---Empy Pop Tracker---')
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, 'Available commands:')
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '//ept track briareus - tracks Briareus pops (search patterns such as apadem* work too!)')
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '//ept hide - hides the UI')
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '//ept show - shows the UI')
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '//ept list - lists all trackable NMs')
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '//ept mini - toggles mini/expanded mode')
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '//ept collectables - toggles the collectable item')
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '//ept help - displays this help')
end

commands.list = function()
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, '---Empy Pop Tracker---')
    windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, 'Trackable NMs:')
    for _, nm in pairs(nm_data) do
        windower.add_to_chat(EmpyPopTracker.settings.add_to_chat_mode, ucwords(nm.name))
    end
end

commands.bg = function()
    local tracking_nm = nm_data[EmpyPopTracker.settings.tracking]
    local url = 'https://www.bg-wiki.com/bg/' .. tracking_nm.name
    windower.open_url(url)
end

commands.collectables = function()
    EmpyPopTracker.settings.collectables = not EmpyPopTracker.settings.collectables
    EmpyPopTracker.settings:save()
    EmpyPopTracker.update()
end

commands.mini = function()
    EmpyPopTracker.settings.expanded = not EmpyPopTracker.settings.expanded
    EmpyPopTracker.settings:save()
    EmpyPopTracker.update()
end

EmpyPopTracker.update = function()
    local key_items = windower.ffxi.get_key_items()
    local items = windower.ffxi.get_items()
    local tracked_nm_data = nm_data[EmpyPopTracker.settings.tracking]
    local generated_info = EmpyPopTracker.generate_info(tracked_nm_data, key_items, items)
    EmpyPopTracker.text:text(generated_info.text)
    if generated_info.has_all_pops then
        EmpyPopTracker.text:bg_color(EmpyPopTracker.settings.colors.bgall.red, EmpyPopTracker.settings.colors.bgall.green, EmpyPopTracker.settings.colors.bgall.blue)
    else
        EmpyPopTracker.text:bg_color(EmpyPopTracker.settings.colors.bg.red, EmpyPopTracker.settings.colors.bg.green, EmpyPopTracker.settings.colors.bg.blue)
    end
    if EmpyPopTracker.settings.visible then
        EmpyPopTracker.text:visible(true)
    end
end

windower.register_event('load', function()
    if windower.ffxi.get_info().logged_in and EmpyPopTracker.settings.visible then
        active = true
        EmpyPopTracker.update()
    end
end)

windower.register_event('add item', 'remove item', function()
    if active then
        EmpyPopTracker.update()
    end
end)

windower.register_event('incoming chunk', function(id)
    --0x055: KI update
    --0x0D2: Treasure pool addition
    --0x0D3: Treasure pool lot/drop
    if active and id == 0x055 or id == 0x0D2 or id == 0x0D3 then
        EmpyPopTracker.update()
    end
end)

windower.register_event('login', function()
    if EmpyPopTracker.settings.visible then
        EmpyPopTracker.text:visible(true)
        active = true
    end
end)

windower.register_event('logout', function()
    EmpyPopTracker.text:visible(false)
    active = false
end)

return EmpyPopTracker