forked from Dukilles/pathlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathlog.lua
518 lines (427 loc) · 17.5 KB
/
pathlog.lua
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
_addon.name = 'pathlog'
_addon.author = 'Duke'
_addon.version = '1.0'
_addon.commands = {'pathlog', 'pl'}
resources = require('resources')
config = require('config')
packets = require('packets')
files = require('files')
require('strings')
require('lists')
defaults = {}
defaults.messageColor = 219
defaults.logPath = false
defaults.mode = 'target'
defaults.TimestampFormat = '-- %H:%M:%S'
defaults.AddTimestamp = true
defaults.tableEachPoint = false
defaults.defineCoordinates = false
defaults.all = false
defaults.cumulativeDiff = 4
defaults.xDiff = 3
defaults.yDiff = 0.5
defaults.zDiff = 3
settings = config.load(defaults)
local pathlog = {}
pathlog.trackList = L{}
windower.register_event('incoming chunk', function(id, data, modified, injected, blocked)
if not windower.ffxi.get_info().logged_in or not settings.logPath or not id == 0x0E then return end
local packet = packets.parse('incoming', data)
local npc = {}
local pos = {}
npc.index = packet['Index']
npc.id = packet['NPC']
npc.look = packet['look']
npc.polutils = packet['polutils_name']
if pathlog.willScan(npc.look, npc.polutils) and npc.id then
-- pos.rot = packet['Rotation']
-- walkCount = packet['Walk Count'] -- on the shelf until v2
pos.x = packet['X']
pos.y = packet['Z'] -- Windower has Z and Y axis swapped
pos.z = packet['Y']
if settings.mode == 'target' then
pathlog.logNpcByTarget(npc.id, pos.x, pos.y, pos.z)
elseif settings.mode == 'list' then
pathlog.logNpcByList(npc.id, pos.x, pos.y, pos.z)
end
end
end)
windower.register_event('outgoing chunk', function(id, data, modified, injected, blocked)
if not windower.ffxi.get_info().logged_in or not settings.logPath then return end
if settings.mode == 'target' then
pathlog.logSelfByTarget()
end
end)
windower.register_event('zone change', function(new, old)
pathlog.trackList:clear()
end)
pathlog.caluclateCoordLen = function(isY)
local len = 8
if isY then
len = len - 1
end
return len
end
pathlog.padLeft = function(str, length, char)
local padded = string.rep(char or ' ', length - #str) .. str
return padded
end
pathlog.padCoords = function(coord, isY)
local padding = pathlog.caluclateCoordLen(isY)
return pathlog.padLeft(coord, padding)
end
function pathlog.logNpcByTarget(npcID, x, y, z)
local player = windower.ffxi.get_player()
local target = windower.ffxi.get_mob_by_target('t')
local zone = resources.zones[windower.ffxi.get_info().zone].name
if target and npcID == target.id then
local playerName = player.name
local targetName = target.name
local id = target.id
local index = target.index
local logFile = files.new('data/pathlogs/'..playerName..'/'..zone..'/'..targetName..'/'..id..'.log')
local timestamp = settings.AddTimestamp and os.date(settings.TimestampFormat, os.time()) or ''
local openBracket = settings.tableEachPoint and '{ ' or ''
local closeBracket = settings.tableEachPoint and ' }' or ''
local defineX = settings.defineCoordinates and 'x = ' or ''
local defineY = settings.defineCoordinates and 'y = ' or ''
local defineZ = settings.defineCoordinates and 'z = ' or ''
if not logFile:exists() then
logFile:create()
end
if pathlog.shouldLogPoint(logFile, x, y, z) then
local x = string.format('%.3f', x)
local y = string.format('%.3f', y)
local z = string.format('%.3f', z)
x = pathlog.padCoords(x)
y = pathlog.padCoords(y, true)
z = pathlog.padCoords(z)
logFile:append(string.format("%s%s%s, %s%s, %s%s%s, %s\n", openBracket, defineX, x, defineY, y, defineZ, z, closeBracket, timestamp))
end
end
end
function pathlog.logNpcByList(npcID, x, y, z)
local player = windower.ffxi.get_player()
local zone = resources.zones[windower.ffxi.get_info().zone].name
local trackList = pathlog.trackList
if #trackList <= 0 then return end
for entry = 1, #trackList do
local listID = trackList[entry]
local target = windower.ffxi.get_mob_by_id(listID)
if target and npcID == target.id then
local playerName = player.name
local targetName = target.name
local id = target.id
local index = target.index
local logFile = files.new('data/pathlogs/'..playerName..'/'..zone..'/'..targetName..'/'..id..'.log')
local timestamp = settings.AddTimestamp and os.date(settings.TimestampFormat, os.time()) or ''
local openBracket = settings.tableEachPoint and '{ ' or ''
local closeBracket = settings.tableEachPoint and ' }' or ''
local defineX = settings.defineCoordinates and 'x = ' or ''
local defineY = settings.defineCoordinates and 'y = ' or ''
local defineZ = settings.defineCoordinates and 'z = ' or ''
if not logFile:exists() then
logFile:create()
end
if pathlog.shouldLogPoint(logFile, x, y, z) then
local x = string.format('%.3f', x)
local y = string.format('%.3f', y)
local z = string.format('%.3f', z)
x = pathlog.padCoords(x)
y = pathlog.padCoords(y, true)
z = pathlog.padCoords(z)
logFile:append(string.format("%s%s%s, %s%s, %s%s%s, %s\n", openBracket, defineX, x, defineY, y, defineZ, z, closeBracket, timestamp))
end
end
end
end
function pathlog.logSelfByTarget()
local player = windower.ffxi.get_player()
local target = windower.ffxi.get_mob_by_target('t')
local zone = resources.zones[windower.ffxi.get_info().zone].name
if target and target.index == player.index then
local playerName = player.name
local targetName = target.name
local logFile = files.new('data/pathlogs/'..playerName..'/'..zone..'/'..targetName..'.log')
local timestamp = settings.AddTimestamp and os.date(settings.TimestampFormat, os.time()) or ''
local openBracket = settings.tableEachPoint and '{ ' or ''
local closeBracket = settings.tableEachPoint and ' }' or ''
local defineX = settings.defineCoordinates and 'x = ' or ''
local defineY = settings.defineCoordinates and 'y = ' or ''
local defineZ = settings.defineCoordinates and 'z = ' or ''
local x = target.x
local y = target.z -- Windower has Z and Y axis swapped
local z = target.y
if not logFile:exists() then
logFile:create()
end
if pathlog.shouldLogPoint(logFile, x, y, z) then
local x = string.format('%.3f', x)
local y = string.format('%.3f', y)
local z = string.format('%.3f', z)
x = pathlog.padCoords(x)
y = pathlog.padCoords(y, true)
z = pathlog.padCoords(z)
logFile:append(string.format("%s%s%s, %s%s, %s%s%s, %s\n", openBracket, defineX, x, defineY, y, defineZ, z, closeBracket, timestamp))
end
end
end
function pathlog.logPointWithComment(comment)
local player = windower.ffxi.get_player()
local target = windower.ffxi.get_mob_by_target('me')
local zone = resources.zones[windower.ffxi.get_info().zone].name
if target then
local playerName = player.name
local targetName = target.name
local logFile = files.new('data/pathlogs/'..playerName..'/'..zone..'/'..targetName..'.log')
local timestamp = settings.AddTimestamp and os.date(settings.TimestampFormat, os.time()) or ''
local openBracket = settings.tableEachPoint and '{ ' or ''
local closeBracket = settings.tableEachPoint and ' }' or ''
local defineX = settings.defineCoordinates and 'x = ' or ''
local defineY = settings.defineCoordinates and ' y = ' or ''
local defineZ = settings.defineCoordinates and ' z = ' or ''
local x = string.format('%.3f', target.x)
local y = string.format('%.3f', target.z) -- Windower has Z and Y axis swapped
local z = string.format('%.3f', target.y)
if not comment then
comment = ''
else
comment = table.sconcat(comment)
end
if not logFile:exists() then
logFile:create()
end
x = pathlog.padCoords(x)
y = pathlog.padCoords(y, true)
z = pathlog.padCoords(z)
logFile:append(string.format("%s%s%s, %s%s, %s%s%s, %s -- %s\n", openBracket, defineX, x, defineY, y, defineZ, z, closeBracket, timestamp, comment))
end
end
function pathlog.shouldLogPoint(logFile, x, y, z)
local readLines = files.readlines(logFile)
local lastLine = readLines[#readLines - 1]
local chars = '}{xyz= '
if x == 0 and y == 0 and z == 0 then
return false
end
if not readLines or not lastLine or settings.all then
return true
end
local lastX = lastLine:split(',')[1]:stripchars(chars)
local lastY = lastLine:split(',')[2]:stripchars(chars)
local lastZ = lastLine:split(',')[3]:stripchars(chars)
local xDiff = math.abs(lastX - x)
local yDiff = math.abs(lastY - y)
local zDiff = math.abs(lastZ - z)
local cumulativeDiff = xDiff + yDiff + zDiff
if cumulativeDiff >= settings.cumulativeDiff or xDiff >= settings.xDiff or yDiff >= settings.yDiff or zDiff >= settings.zDiff then
return true
end
return false
end
function pathlog.willScan(look, polutils)
if look == '00003400' or polutils == 'NPC' then
return false
end
return true
end
local commands = {}
commands.start = function()
settings.logPath = true
windower.add_to_chat(settings.messageColor, 'Path logging ON')
end
commands.st = function()
return commands.start()
end
commands.stop = function()
settings.logPath = false
windower.add_to_chat(settings.messageColor, 'Path logging OFF')
end
commands.sp = function()
return commands.stop()
end
commands.mode = function(args)
local newMode = args:remove(1)
if newMode == 't' then
newMode = 'target'
elseif newMode == 'l' then
newMode = 'list'
end
if newMode == 'target' or newMode == 'list' then
settings.mode = newMode
windower.add_to_chat(settings.messageColor, 'Pathlog mode: '..settings.mode)
else
commands.help()
end
settings:save()
end
commands.m = function(args)
return commands.mode(args)
end
commands.list = function(args)
local option = args:remove(1)
local id = tonumber(args:concat(' '))
local target = windower.ffxi.get_mob_by_target('t')
local player = windower.ffxi.get_mob_by_target('me')
if option == 'show' or option == 's' then
local trackList = pathlog.trackList
if #trackList > 0 then
windower.add_to_chat(settings.messageColor, 'Pathlog Tracklist:')
for entry = 1, #trackList do
windower.add_to_chat(settings.messageColor, ''..trackList[entry])
end
else
windower.add_to_chat(settings.messageColor, 'Pathlog Tracklist is Empty')
end
return
end
if id then -- Look for explicitly desired ID first
if type(id) ~= "number" or id == player.id then -- Make sure its a not players id number
commands.help()
return
end
elseif not id then -- Look for cursor target next
if target then
if target.name == player.name or target.index == player.index then
windower.add_to_chat(settings.messageColor, 'Cannot target player in list mode. Switch to target mode to log player path')
else
id = target.id
end
end
else
commands.help()
return
end
if option == 'add' or option == 'a' then
if id and not pathlog.trackList:contains(id) then
pathlog.trackList:append(id)
windower.add_to_chat(settings.messageColor, 'Added '..id.. ' to tracking list. ')
else
windower.add_to_chat(settings.messageColor, 'Must provivde an ID or target an entity to add to tracking list.')
end
elseif option == 'remove' or option == 'r' then
if id and pathlog.trackList:contains(id) then
pathlog.trackList:remove(id)
windower.add_to_chat(settings.messageColor, 'Removed '..id.. ' from tracking list. ')
else
windower.add_to_chat(settings.messageColor, 'Must provivde an ID or target an entity to remove from tracking list.')
end
elseif option == 'clear' or option == 'c' then
if not pathlog.trackList:empty() then
pathlog.trackList:clear()
windower.add_to_chat(settings.messageColor, 'Cleared tracking list! ')
else
windower.add_to_chat(settings.messageColor, 'Trackling list is empty.')
end
else
commands.help()
return
end
end
commands.l = function(args)
return commands.list(args)
end
commands.all = function()
if settings.all == true then
settings.all = false
windower.add_to_chat(settings.messageColor, 'Log all = FALSE')
elseif settings.all == false then
settings.all = true
windower.add_to_chat(settings.messageColor, 'Log all = TRUE')
end
settings:save()
end
commands.a = function()
return commands.all()
end
commands.diff = function(args)
local option = args:remove(1)
local value = tonumber(args:concat(' '))
if option == 'cumulative' or option == 'c' then
settings.cumulativeDiff = value
windower.add_to_chat(settings.messageColor, 'Cumulative Diff = '..settings.cumulativeDiff)
elseif option == 'x' then
settings.xDiff = value
windower.add_to_chat(settings.messageColor, 'x Diff = '..settings.xDiff)
elseif option == 'y' then
settings.yDiff = value
windower.add_to_chat(settings.messageColor, 'y Diff = '..settings.yDiff)
elseif option == 'z' then
settings.zDiff = value
windower.add_to_chat(settings.messageColor, 'z Diff = '..settings.zDiff)
else
windower.add_to_chat(settings.messageColor, 'Valid arguments are cumulative(c), x, y, or z followed by a number')
return
end
settings:save()
end
commands.d = function(args)
return commands.diff(args)
end
commands.timestamp = function()
if settings.AddTimestamp == true then
settings.AddTimestamp = false
windower.add_to_chat(settings.messageColor, 'Add timestamp to logs = FALSE')
elseif settings.AddTimestamp == false then
settings.AddTimestamp = true
windower.add_to_chat(settings.messageColor, 'Add timestamp to logs = TRUE')
end
settings:save()
end
commands.ts = function()
return commands.timestamp()
end
commands.tablepoints = function()
if settings.tableEachPoint == true then
settings.tableEachPoint = false
windower.add_to_chat(settings.messageColor, 'Table each point = FALSE')
elseif settings.tableEachPoint == false then
settings.tableEachPoint = true
windower.add_to_chat(settings.messageColor, 'Table each point = TRUE')
end
settings:save()
end
commands.tp = function()
return commands.tablepoints()
end
commands.definecoordinates = function()
if settings.defineCoordinates == true then
settings.defineCoordinates = false
windower.add_to_chat(settings.messageColor, 'Define coordinates = FALSE')
elseif settings.defineCoordinates == false then
settings.defineCoordinates = true
windower.add_to_chat(settings.messageColor, 'Define coordinates = TRUE')
end
settings:save()
end
commands.dc = function()
return commands.definecoordinates()
end
commands.point = function(args)
pathlog.logPointWithComment(args)
windower.add_to_chat(settings.messageColor, 'Point added to logs')
end
commands.p = function(args)
return commands.point(args)
end
commands.help = function()
windower.add_to_chat(settings.messageColor, 'pathlog (or //pl)')
windower.add_to_chat(settings.messageColor, '//pathlog start(st) - Begin logging targeted entity\'s path')
windower.add_to_chat(settings.messageColor, '//pathlog stop(sp) - Stop logging targeted entity\'s path')
windower.add_to_chat(settings.messageColor, '//pathlog mode(m) target(t)|list(l) - change tracking mode from cursor target to a set list (default target))')
windower.add_to_chat(settings.messageColor, '//pathlog list(l) add(a)|remove(r)|show(s) ID - In list mode, add/remove ID|target to/from tracking list.')
windower.add_to_chat(settings.messageColor, '//pathlog all(a) - log all positions without difference filtering (default FALSE)')
windower.add_to_chat(settings.messageColor, '//pathlog diff(d) cumulative(c)|x|y|z (value)- set diff required between points to log (default cumulative 4, x = 3, y = 0.5, z = 3)')
windower.add_to_chat(settings.messageColor, '//pathlog timestamp(ts) - toggle timestamp in log (default TRUE)')
windower.add_to_chat(settings.messageColor, '//pathlog tablepoints(tp) - toggle table each path point (default FALSE)')
windower.add_to_chat(settings.messageColor, '//pathlog definecoordinates(dc) - toggle define coordinates (x = #) (default FALSE)')
windower.add_to_chat(settings.messageColor, '//pathlog point(p) \'...\' - will add a point and anything typed after to a comment in the logs')
end
windower.register_event('addon command', function(command, ...)
command = command and command:lower()
if commands[command] then
commands[command](L{...})
else
commands.help()
end
end)