This repository was archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
478 lines (440 loc) · 16.3 KB
/
index.js
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
/*----------
TO DO:
Add debug stuff
Fix combo attack
Add more in-game commands
Add blocking support
----------*/
module.exports = function PingCompensation(dispatch) {
//----------
// Constants
//----------
const config = require('./config/config.json')
const preset = require('./config/preset.js')
const skills = require('./config/data/skills.js')
const Command = require('command')
const path = require('path')
const Ping = (!config.spCompatible) ? require('./lib/ping.js') : require(path.join(config.spDirectory, 'lib', 'ping.js'))
const CDR = (!config.spCompatible) ? require('./lib/cooldowns.js') : false
const command = Command(dispatch)
const ping = Ping(dispatch)
//----------
// Variables
//----------
let gameId = null,
templateId = null,
skillsCache = null,
job = -1,
race = -1,
timeouts = {},
startTime = false,
alive = false,
//inBlock = false,
//mounted = false,
queuedPacket = false,
currentAction = false,
enabled = config.enabled
//----------
// Commands
//----------
command.add(['PC', 'pingComp', 'PingCompensation'], (option) => {
if (option) {
// ping
if (option.toLowerCase() == 'ping') {
command.message(`Ping: Min=${ping.min} Avg=${Math.floor(ping.avg)} Max=${ping.max} Jitter=${ping.max - ping.min} Samples=${ping.history.length}`)
return
}
// on
if (option.toLowerCase() == 'on') {
command.message('Ping Compensation enabled.')
enabled = true
return
}
// off
if (option.toLowerCase() == 'off') {
command.message('Ping Compensation disabled.')
enabled = false
return
}
// debug
if (option.toLowerCase() == 'debug') {
config.debug = !config.debug
command.message(`Ping Compensation debug ${config.debug ? 'enabled.' : 'disabled.'}`)
return
}
}
command.message('Ping Compensation command input missing. Input options are "ping", "on", "off", or "debug".')
})
//----------
// Functions
//----------
//Load info about skill - credit: Pinkie Pie + Salty Monkey
function skillInfo(id, local) {
if (!local) id -= 0x4000000;
let cached = skillsCache[id];
if (cached !== undefined) return cached;
let group = Math.floor(id / 10000),
level = (Math.floor(id / 100) % 100) - 1,
sub = id % 100
// preset.js support
if (!get(preset, job, "enabled") || !get(preset, job, group))
return skillsCache[id] = null
let info = [ // Ordered by least specific < most specific
get(skills, job, '*'),
get(skills, job, '*', 'level', level),
get(skills, job, '*', 'race', race),
get(skills, job, '*', 'race', race, 'level', level),
get(skills, job, group, '*'),
get(skills, job, group, '*', 'level', level),
get(skills, job, group, '*', 'race', race),
get(skills, job, group, '*', 'race', race, 'level', level),
get(skills, job, group, sub),
get(skills, job, group, sub, 'level', level),
get(skills, job, group, sub, 'race', race),
get(skills, job, group, sub, 'race', race, 'level', level)
];
// Note: Exact skill (group, sub) must be specified for prediction to be enabled. This helps to avoid breakage in future patches
if (info[8]) {
cached = skillsCache[id] = Object.assign({}, ...info);
// Sanitize to reduce memory usage
delete cached.race;
delete cached.level;
return cached
}
return skillsCache[id] = null
}
// read skill data - credit: Pinkie Pie
function get(obj, ...keys) {
if (obj === undefined) return;
for (let key of keys)
if ((obj = obj[key]) === undefined)
return;
return obj
}
// endSkill
function endSkill(event) {
if (alive && enabled && event) {
timeouts[event.id] = false
dispatch.toClient('S_ACTION_END', 2, event)
if (config.debug) {console.log('sActionEnd Ping-Compensation')}
}
}
// updateCoord
function updateCoord(event) {
for (let coord of ["x", "y", "z", "w"]) {
// if in fake skill
if (currentAction && timeouts[currentAction.id]) {
// update end location
currentAction[coord] = event[coord]
}
}
}
// skillHook
function skillHook(event) {
let info = skillInfo(event.skill)
if (!info || Array.isArray(info.length)) {
startTime = false
return null
}
startTime = Date.now()
}
//----------
// Hooks
//----------
// S_LOGIN
dispatch.hook('S_LOGIN', 9, event => {
gameId = event.gameId
templateId = event.templateId
race = Math.floor((templateId - 10101) / 100)
job = (templateId - 10101) % 100
skillsCache = {}
})
// C_PRESS_SKILL
dispatch.hook('C_PRESS_SKILL', 1, {order: 10, filter: {fake: null}}, event => {
updateCoord(event)
/*
// if blocking, end immediately
if (inBlock && event.start == 0) {
inBlock = false
endSkill(currentAction)
}
*/
})
// C_PLAYER_LOCATION
dispatch.hook('C_PLAYER_LOCATION', 2, {order: 10, filter: {fake: false}}, event => {
updateCoord(event)
})
// C_PLAYER_LOCATION
dispatch.hook('C_PLAYER_LOCATION', 'raw', {order: 10}, (code, data, fromServer, fake) => {
if (!fake) {
// if between fake and real S_ACTION_END
if (currentAction && !timeouts[currentAction.id]) {
queuedPacket = data
// block location packets
return false
}
}
})
// C_NOTIFY_LOCATION_IN_ACTION
dispatch.hook('C_NOTIFY_LOCATION_IN_ACTION', 1, {order: 10, filter: {fake: null}}, event => {
updateCoord(event)
})
// C_NOTIFY_LOCATION_IN_DASH
dispatch.hook('C_NOTIFY_LOCATION_IN_DASH', 1, {order: 10, filter: {fake: null}}, event => {
updateCoord(event)
})
// S_INSTANT_DASH
dispatch.hook('S_INSTANT_DASH', 2, {order: 10, filter: {fake: null}}, event => {
if (event.source.equals(gameId)){
updateCoord(event)
}
})
// S_INSTANT_MOVE
dispatch.hook('S_INSTANT_MOVE', 1, {order: 10, filter: {fake: null}}, event => {
if (event.id.equals(gameId)){
updateCoord(event)
}
})
// skill packets, get current ping
for(let packet of [
['C_START_SKILL', 3],
['C_START_TARGETED_SKILL', 3],
['C_START_COMBO_INSTANT_SKILL', 1],
['C_START_INSTANCE_SKILL', 1],
['C_START_INSTANCE_SKILL_EX', 2],
//['C_PRESS_SKILL', 1],
['C_NOTIMELINE_SKILL', 1], //not sure about this one
//['C_CAN_LOCKON_TARGET', 1],
]) dispatch.hook(packet[0], packet[1], { /*filter: { fake: false, modified: false },*/ order: 1000 }, skillHook);
// S_CANNOT_START_SKILL
dispatch.hook('S_CANNOT_START_SKILL', 'raw', {order: 10}, () => {
startTime = false
})
// C_CANCEL_SKILL
dispatch.hook('C_CANCEL_SKILL', 'raw', {order: 10}, () => {
startTime = false
});
// S_ACTION_STAGE
dispatch.hook('S_ACTION_STAGE', 2, {order: 10, filter: {fake: false}}, event => {
// if character is your character
if (event.gameId.equals(gameId)) {
// get skill id
let info = skillInfo(event.skill)
// if skill is in config
if (config.debug && enabled) {console.log('sActionStage: info?', info ? true : false)}
if (alive && enabled && info) {
/*
// if block, enable fast release
if (info.type == 'holdInfinite') {
inBlock = true
currentAction = {
gameId: event.gameId,
x: event.x,
y: event.y,
z: event.z,
w: event.w,
templateId: event.templateId,
skill: event.skill,
type: 10,
id: event.id
}
return
}
*/
// get length and distance
let multistage = Array.isArray(info.length),
length = multistage ? info.length[event.stage] : info.length,
distance = multistage ? info.distance[event.stage] : info.distance,
currentPing = Math.max(ping.min, multistage ? 0 : startTime ? Date.now() - startTime : 0)
if (length && length > 0) {
// change animation speed
if (currentPing < length) {
if (config.debug) {console.log(`Ping Compensation: skill=${event.skill - 0x4000000} compensation=${currentPing}`)}
event.speed = event.speed * length / (length - currentPing)
}
else {
if (config.debug) {console.log(`Ping Compensation skill=${event.skill - 0x4000000} compensation=${length}`)}
length = 1
}
// if server sends distance
if (event.movement[0]) {
distance = 0
// get total distance
for (let stage of event.movement) {
distance += stage.distance
}
}
// get coordinates
let x,y
if (distance && distance * distance > 0) {
let r = (event.w / 0x8000) * Math.PI
x = event.x + Math.cos(r) * distance
y = event.y + Math.sin(r) * distance
}
// if skill type charging or lockon
if (['charging','lockon'].includes(info.type)) {
return true
}
// if multi-stage and not last stage
if (multistage && event.stage < info.length.length - 1) {
return true
}
// get end type ???
// TO DO
// send sActionEnd early
currentAction = {
gameId: event.gameId,
x: (x ? x : event.x),
y: (y ? y : event.y),
z: event.z,
w: event.w,
templateId: event.templateId,
skill: event.skill,
type: 0,
id: event.id
}
timeouts[event.id] = setTimeout(endSkill, length / event.speed, currentAction)
return true
}
}
else {
if (currentAction && timeouts[currentAction.id]) {
// disable fake endSkill
clearTimeout(timeouts[currentAction.id])
timeouts[currentAction.id] = false
}
currentAction = false
queuedPacket = false
}
startTime = false
}
})
// S_ACTION_END
dispatch.hook('S_ACTION_END', 2, {order: 10, filter: {fake: false}}, event => {
// if character is your character
if (event.gameId.equals(gameId)) {
// if modded skill
if (alive && enabled && currentAction && currentAction.id == event.id) {
// if not fake ended
if (timeouts[event.id] /*|| inBlock*/) {
// disable fake endSkill
clearTimeout(timeouts[event.id])
timeouts[event.id] = false
if (config.debug) {console.log('sActionEnd Server')}
}
// if fake ended
else {
// if location emulated wrong
if (Math.sqrt((currentAction.x - event.x)*(currentAction.x - event.x)
+ (currentAction.y - event.y)*(currentAction.y - event.y)) > 100
|| (currentAction.z - event.z)*(currentAction.z - event.z) > 2500) {
// teleport to correct location
if (config.debug) {console.log('S_INSTANT_MOVE correction')}
dispatch.toClient('S_INSTANT_MOVE', 1, {
id: gameId,
x: event.x,
y: event.y,
z: event.z,
w: event.w
})
}
else if (queuedPacket) {
dispatch.toServer(queuedPacket)
}
queuedPacket = false
currentAction = false
// hide this sActionEnd
return false
}
}
queuedPacket = false
currentAction = false
}
})
// S_ACTION_END SP Compatibility
dispatch.hook('S_ACTION_END', 2, {order: 10, filter: {fake: true}}, event => {
// if character is your character
if (event.gameId.equals(gameId)) {
// if modded skill
if (alive && enabled && currentAction && currentAction.id == event.id) {
// if not fake ended
if (timeouts[event.id] /*|| inBlock*/) {
// disable fake endSkill
clearTimeout(timeouts[event.id])
timeouts[event.id] = false
if (config.debug) {console.log('sActionEnd Skill-Prediction')}
}
}
queuedPacket = false
currentAction = false
}
})
// S_EACH_SKILL_RESULT
dispatch.hook('S_EACH_SKILL_RESULT', 4, event => {
if (gameId.equals(event.target)) {
if (event.setTargetAction == 1) {
clearTimeout(timeouts[currentAction.id])
timeouts[currentAction.id] = false
queuedPacket = false
currentAction = false
}
}
})
// S_SPAWN_ME
dispatch.hook('S_SPAWN_ME', 1, event => {
alive = event.alive
if (!alive) {
clearTimeout(timeouts[currentAction.id])
timeouts[currentAction.id] = false
queuedPacket = false
currentAction = false
}
})
// S_CREATURE_LIFE
dispatch.hook('S_CREATURE_LIFE', 1, event => {
if (gameId.equals(event.target)) {
alive = event.alive
if (!alive) {
clearTimeout(timeouts[currentAction.id])
timeouts[currentAction.id] = false
queuedPacket = false
currentAction = false
}
}
})
// S_LOAD_TOPO
dispatch.hook('S_LOAD_TOPO', 1, event => {
if (currentAction) {
clearTimeout(timeouts[currentAction.id])
timeouts[currentAction.id] = false
queuedPacket = false
currentAction = false
}
})
/*
// S_MOUNT_VEHICLE
dispatch.hook('S_MOUNT_VEHICLE', 1, event => {
if (gameId.equals(event.target)) {
mounted = true
}
})
// S_UNMOUNT_VEHICLE
dispatch.hook('S_UNMOUNT_VEHICLE', 1, event => {
if (gameId.equals(event.target)) {
mounted = false
}
})
// S_MOUNT_VEHICLE_EX
dispatch.hook('S_MOUNT_VEHICLE_EX', 1, event => {
if (gameId.equals(event.target)) {
mounted = true
}
})
// S_UNMOUNT_VEHICLE_EX
dispatch.hook('S_UNMOUNT_VEHICLE_EX', 1, event => {
if (gameId.equals(event.target)) {
mounted = false
}
})
*/
}