-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.js
528 lines (414 loc) · 17.5 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
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
519
520
521
522
523
524
525
526
527
528
const SKILL_RETRY_MS = 50, /* Desync reduction (0 = disabled) - Setting this too high may cause skills to go off twice,
and may cause desync compensation to fail.
*/
SKILL_DELAY_NEXT = true, // Desync compensation
DEBUG = false,
DEBUG_LOC = false,
DEBUG_GLYPH = false
const sysmsg = require('tera-data').sysmsg,
AbnormalityPrediction = require('./abnormalities'),
skills = require('./config/skills')
module.exports = function SkillPrediction(dispatch) {
const abnormality = new AbnormalityPrediction(dispatch)
let skillsCache = null,
cid = null,
model = 0,
job = -1,
aspd = 1,
currentGlyphs = null,
currentStamina = 0,
delayNext = false,
delayNextEnd = 0,
delayNextTimeout = null,
actionNumber = 0x80000000,
currentLocation = null,
currentAction = null,
serverAction = null,
lastEndType = 0,
lastEmulatedEnd = 0,
stageTimeout = null,
debugActionTime = 0
dispatch.hook('sLogin', event => {
skillsCache = {}
;({cid, model} = event)
job = (model - 10101) % 100
if(DEBUG) console.log('Class', job)
})
dispatch.hook('sLoadTopo', () => {
actionNumber = 0x8000000
currentAction = null
serverAction = null
lastEndType = 0
lastEmulatedEnd = 0
clearTimeout(stageTimeout)
})
dispatch.hook('sPlayerStatUpdate', event => {
// Newer classes use a different speed algorithm
aspd = (event.baseAttackSpeed + event.bonusAttackSpeed) / (job >= 8 ? 100 : event.baseAttackSpeed)
})
dispatch.hook('sCrestInfo', event => {
currentGlyphs = {}
for(let glyph of event.glyphs)
currentGlyphs[glyph.id] = glyph.enabled
})
dispatch.hook('sCrestApply', event => {
if(DEBUG_GLYPH) console.log('Glyph', event.id, event.enabled)
currentGlyphs[event.id] = event.enabled
})
dispatch.hook('sPlayerChangeStamina', event => { currentStamina = event.currentRe })
dispatch.hook('cPlayerLocation', event => {
if(DEBUG_LOC) console.log('Location %d %d (%d %d %d %d) > (%d %d %d)', event.type, event.speed, Math.round(event.x1), Math.round(event.y1), Math.round(event.z1), event.w, Math.round(event.x2), Math.round(event.y2), Math.round(event.z2))
if(currentAction) {
let info = skillInfo(currentAction.skill)
if(info && info.distance) return false
}
currentLocation = {
// This is not correct, but the midpoint location seems to be "close enough" for the client to not teleport the player
x: (event.x1 / 2) + (event.x2 / 2),
y: (event.y1 / 2) + (event.y2 / 2),
z: (event.z1 / 2) + (event.z2 / 2),
w: event.w,
walking: event.type == 0
}
})
dispatch.hook('cNotifyLocationInAction', event => {
if(DEBUG_LOC) console.log('NotifyLocation %s %d (%d %d %d %d)', skillId(event.skill), event.stage, Math.round(event.x), Math.round(event.y), Math.round(event.z), event.w)
currentLocation = {
x: event.x,
y: event.y,
z: event.z,
w: event.w,
inAction: true
}
let info = skillInfo(event.skill)
if(info && SKILL_RETRY_MS && !info.noRetry)
setTimeout(() => {
if(currentAction && currentAction.skill == event.skill && (!serverAction || serverAction.skill != event.skill))
dispatch.toServer('cNotifyLocationInAction', event)
}, SKILL_RETRY_MS)
})
dispatch.hook('cStartSkill', startSkill.bind(null, 'cStartSkill'))
dispatch.hook('cStartTargetedSkill', startSkill.bind(null, 'cStartTargetedSkill'))
dispatch.hook('cStartComboInstantSkill', startSkill.bind(null, 'cStartComboInstantSkill'))
dispatch.hook('cStartInstanceSkill', startSkill.bind(null, 'cStartInstanceSkill'))
dispatch.hook('cPressSkill', startSkill.bind(null, 'cPressSkill'))
function startSkill(type, event) {
let delayed = delayNext && delayNextEnd >= Date.now()
if(DEBUG)
if(DEBUG_LOC) {
if(type == 'cStartSkill' || type == 'cStartTargetedSkill') console.log('-> %s %s %d\xb0 (%d %d %d) > (%d %d %d)', type, skillId(event.skill), event.w, Math.round(event.x1), Math.round(event.y1), Math.round(event.z1), Math.round(event.x2), Math.round(event.y2), Math.round(event.z2), delayed ? 'Delayed' : '')
else if(type == 'cPressSkill') console.log('-> %s %s %d\xb0 (%d %d %d)', type, skillId(event.skill), event.start, event.w, Math.round(event.x), Math.round(event.y), Math.round(event.z), delayed ? 'Delayed' : '')
else console.log('-> %s %s %d %d\xb0 (%d %d %d)', type, skillId(event.skill), event.w, Math.round(event.x), Math.round(event.y), Math.round(event.z), delayed ? 'Delayed' : '')
}
else if(type == 'cPressSkill') console.log('->', type, skillId(event.skill), event.start, delayed ? 'Delayed' : '')
else console.log('->', type, skillId(event.skill), delayed ? 'Delayed' : '')
if(delayed) {
clearTimeout(delayNextTimeout)
delayNextTimeout = setTimeout(sendStartSkill, SKILL_RETRY_MS, type, event, true)
return false
}
return sendStartSkill(type, event)
}
function sendStartSkill(type, event, send) {
delayNext = false
let info = skillInfo(event.skill)
if(!info) {
if(type != 'cPressSkill' || event.start)
// Sometimes invalid (if this skill can't be used, but we have no way of knowing that)
updateLocation(event, false, type == 'cStartSkill' || type == 'cStartTargetedSkill')
if(send) dispatch.toServer(type, event)
return
}
let skill = event.skill,
skillBase = Math.floor((skill - 0x4000000) / 10000),
interruptType = 0
if(type == 'cPressSkill' && !event.start) {
if(info.instantPressAndHold && currentAction && currentAction.skill == skill) {
updateLocation(event)
sendActionEnd(10)
}
if(send) dispatch.toServer(type, event)
return
}
if(currentAction) {
interruptType = info.chainType || 6
let currentSkill = currentAction.skill - 0x4000000,
currentSkillBase = Math.floor(currentSkill / 10000),
currentSkillSub = currentSkill % 100
// Some skills are bugged clientside and can interrupt the wrong skills, so they need to be flagged manually
if(info.noInterrupt && (info.noInterrupt.includes(currentSkillBase) || info.noInterrupt.includes(currentSkillBase + '-' + currentSkillSub)))
return false
if(currentSkill == 68209065 || currentSkill == 68209066) return false // Stagger, Knockdown
let chain = get(info, 'chains', currentSkillBase + '-' + currentSkillSub) || get(info, 'chains', currentSkillBase)
if(chain) {
skill += chain - ((skill - 0x4000000) % 100)
interruptType = info.chainType || 4
}
}
// Skill override (chain)
if(skill != event.skill) {
info = skillInfo(skill)
if(!info) {
updateLocation(event, false, type == 'cStartSkill' || type == 'cStartTargetedSkill')
if(send) dispatch.toServer(type, event)
return
}
}
// TODO: System Message
if(info.requiredBuff && !abnormality.exists(info.requiredBuff)) return false
let walking = currentLocation.walking,
abnormalSpeed = 1,
distanceMult = 1
updateLocation(event, false, type == 'cStartSkill' || type == 'cStartTargetedSkill')
if(info.abnormals)
for(let id in info.abnormals)
if(abnormality.exists(id)) {
let abnormal = info.abnormals[id]
if(abnormal.speed) abnormalSpeed *= abnormal.speed
if(abnormal.chain) skill += abnormal.chain - ((skill - 0x4000000) % 100)
if(abnormal.skill) skill = 0x4000000 + abnormal.skill
}
// Skill override (abnormal)
if(skill != event.skill) {
info = skillInfo(skill)
if(!info) {
if(send) dispatch.toServer(type, event)
return
}
}
if(interruptType) sendActionEnd(interruptType)
// Finish calculations and send the final skill
let speed = info.fixedSpeed || aspd * (info.speed || 1) * abnormalSpeed,
stamina = info.stamina
if(info.glyphs)
for(let id in info.glyphs)
if(currentGlyphs[id]) {
let glyph = info.glyphs[id]
if(glyph.speed) speed *= glyph.speed
if(glyph.distance) distanceMult *= glyph.distance
if(glyph.stamina) stamina += glyph.stamina
}
if(stamina) {
if(currentStamina < stamina) {
//dispatch.toClient('sSystemMessage', { message: '@' + sysmsg.map.name['smtBattleSkillFailLowStamina'] })
return false
}
if(info.instantStamina) currentStamina -= stamina
}
sendActionStage(skill, info, 0, speed, 0, distanceMult, walking)
if(send) dispatch.toServer(type, event)
// Normally the user can press the skill button again if it doesn't go off
// However, once the animation starts this is no longer possible, so instead we simulate retrying each skill
if(SKILL_RETRY_MS && !info.noRetry)
setTimeout(() => {
// Note: May fail with high ping and casting the same skill multiple times in very quick succession (ie. Lancer Combo Attack)
if(currentAction && currentAction.skill == skill && (!serverAction || serverAction.skill != skill))
dispatch.toServer(type, event)
}, SKILL_RETRY_MS)
}
dispatch.hook('cCancelSkill', event => {
if(DEBUG) console.log('-> cCancelSkill', skillId(event.skill), event.type)
if(currentAction) {
let info = skillInfo(currentAction.skill) // event.skill can be wrong, so use the known current skill instead
if(info && info.canCancel) sendActionEnd(event.type)
}
})
dispatch.hook('sActionStage', event => {
if(event.source.equals(cid)) {
if(DEBUG) {
let effects = ''
if(event.effects) {
effects = []
for(let e of event.effects)
effects.push(e.duration + ' ' + e.speed + ' ' + e.unk + ' ' + e.distance)
effects = '(' + effects.join(', ') + ')'
}
if(DEBUG_LOC) {
if(serverAction) console.log('<- sActionStage %s %d %dx %d\xb0 %du %dms (%dms)', skillId(event.skill), event.stage, Math.round(event.speed * 1000) / 1000, event.w, Math.round(Math.sqrt(Math.pow(event.x - serverAction.x, 2) + Math.pow(event.y - serverAction.y, 2)) * 1000) / 1000, Date.now() - debugActionTime, Math.round((Date.now() - debugActionTime) * serverAction.speed), event.unk, event.unk1, event.toX, event.toY, event.toZ, event.unk2, event.unk3, effects, skillInfo(event.skill) ? 'X' : '')
else console.log('<- sActionStage %s %d %dx %d\xb0', skillId(event.skill), event.stage, Math.round(event.speed * 1000) / 1000, event.w, event.unk, event.unk1, event.toX, event.toY, event.toZ, event.unk2, event.unk3, effects, skillInfo(event.skill) ? 'X' : '')
}
else if(serverAction) console.log('<- sActionStage %s %d %dx %du %dms (%dms)', skillId(event.skill), event.stage, Math.round(event.speed * 1000) / 1000, Math.round(Math.sqrt(Math.pow(event.x - serverAction.x, 2) + Math.pow(event.y - serverAction.y, 2)) * 1000) / 1000, Date.now() - debugActionTime, Math.round((Date.now() - debugActionTime) * serverAction.speed), event.unk, event.unk1, event.toX, event.toY, event.toZ, event.unk2, event.unk3, effects, skillInfo(event.skill) ? 'X' : '')
else console.log('<- sActionStage %s %d %dx', skillId(event.skill), event.stage, Math.round(event.speed * 1000) / 1000, event.unk, event.unk1, event.toX, event.toY, event.toZ, event.unk2, event.unk3, effects, skillInfo(event.skill) ? 'X' : '')
debugActionTime = Date.now()
}
if(skillInfo(event.skill)) {
// If the server sends 2 sActionStage in a row without a sActionEnd between them and the last one is an emulated skill,
// this stops your character from being stuck in the first animation (although slight desync will occur)
if(serverAction && serverAction == currentAction && !skillInfo(currentAction.skill)) sendActionEnd(6)
serverAction = event
return false
}
if(currentAction && skillInfo(currentAction.skill)) sendActionEnd(lastEndType)
serverAction = currentAction = event
lastEndType = 6
updateLocation()
}
})
dispatch.hook('sActionEnd', event => {
if(event.source.equals(cid)) {
if(DEBUG) {
if(DEBUG_LOC) console.log('<- sActionEnd %s %d %d\xb0 %du %dms (%dms)', skillId(event.skill), event.type, event.w, Math.round(Math.sqrt(Math.pow(event.x - serverAction.x, 2) + Math.pow(event.y - serverAction.y, 2)) * 1000) / 1000, Date.now() - debugActionTime, Math.round((Date.now() - debugActionTime) * serverAction.speed), (event.id == lastEmulatedEnd || skillInfo(event.skill)) ? 'X' : '')
else console.log('<- sActionEnd %s %d %du %dms (%dms)', skillId(event.skill), event.type, Math.round(Math.sqrt(Math.pow(event.x - serverAction.x, 2) + Math.pow(event.y - serverAction.y, 2)) * 1000) / 1000, Date.now() - debugActionTime, Math.round((Date.now() - debugActionTime) * serverAction.speed), (event.id == lastEmulatedEnd || skillInfo(event.skill)) ? 'X' : '')
}
serverAction = null
lastEndType = event.type
if(event.id == lastEmulatedEnd) {
lastEmulatedEnd = 0
return false
}
if(skillInfo(event.skill)) {
// Skills that may only be cancelled during part of the animation are hard to emulate, so we use server response instead
// This may cause bugs with very high ping and casting the same skill multiple times
if(currentAction && event.skill == currentAction.skill && event.type == 2) sendActionEnd(2)
return false
}
if(!currentAction)
console.log('[SkillPrediction] sActionEnd: currentAction is null', skillId(event.skill), event.id)
else if(event.skill != currentAction.skill)
console.log('[SkillPrediction] sActionEnd: skill mismatch', skillId(currentAction.skill), skillId(event.skill), currentAction.id, event.id)
currentAction = null
}
})
dispatch.hook('sEachSkillResult', event => {
if(event.target.equals(cid) && event.setTargetAction) {
if(currentAction && skillInfo(currentAction.skill)) sendActionEnd(lastEndType)
currentAction = serverAction = {
x: event.targetX,
y: event.targetY,
z: event.targetZ,
w: event.targetW,
skill: event.targetAction,
stage: event.targetStage,
id: event.targetId
}
updateLocation()
}
})
dispatch.hook('sCannotStartSkill', event => {
if(DEBUG) console.log('<- sCannotStartSkill', skillId(event.skill, true))
if(skillInfo(event.skill, true)) {
if(SKILL_DELAY_NEXT && SKILL_RETRY_MS && currentAction && (!serverAction || currentAction.skill != serverAction.skill) && event.skill == currentAction.skill - 0x4000000)
delayNext = true
return false
}
})
function sendActionStage(skill, info, stage, speed, distance, distanceMult, walking) {
movePlayer(distance * distanceMult)
let movement = null
if(Array.isArray(info.length))
movement = !walking && get(info, 'inPlace', 'movement', stage) || get(info, 'movement', stage) || []
else
movement = !walking && get(info, 'inPlace', 'movement') || info.movement || []
dispatch.toClient('sActionStage', currentAction = {
source: cid,
x: currentLocation.x,
y: currentLocation.y,
z: currentLocation.z,
w: currentLocation.w,
model,
skill,
stage,
speed,
id: actionNumber,
unk: 1,
unk1: 0,
toX: 0,
toY: 0,
toZ: 0,
unk2: 0,
unk3: 0,
movement
})
if(info.instantPressAndHold) return
let length = 0,
nextDistance = 0
if(Array.isArray(info.length)) {
length = info.length[stage] / speed
nextDistance = get(info, 'distance', stage) || 0
if(!walking) {
let inPlaceDistance = get(info, 'inPlace', 'distance', stage)
if(inPlaceDistance !== undefined) nextDistance = inPlaceDistance
}
if(stage + 1 < info.length.length) {
delayNextEnd = Date.now() + length + SKILL_RETRY_MS
stageTimeout = setTimeout(sendActionStage, length, skill, info, stage + 1, speed, nextDistance, distanceMult, walking)
return
}
}
else {
length = info.length / speed
nextDistance = info.distance || 0
if(!walking) {
let inPlaceDistance = get(info, 'inPlace', 'distance')
if(inPlaceDistance !== undefined) nextDistance = inPlaceDistance
}
}
delayNextEnd = Date.now() + length + SKILL_RETRY_MS
stageTimeout = setTimeout(sendActionEnd, length, 0, nextDistance, distanceMult)
}
function sendActionEnd(type, distance, distanceMult) {
clearTimeout(stageTimeout)
if(!currentAction) return
if(DEBUG) console.log('<* sActionEnd %s %d %d\xb0 %du', skillId(currentAction.skill), type || 0, currentLocation.w, distance || 0)
movePlayer(distance * distanceMult)
dispatch.toClient('sActionEnd', {
source: cid,
x: currentLocation.x,
y: currentLocation.y,
z: currentLocation.z,
w: currentLocation.w,
model,
skill: currentAction.skill,
type: type || 0,
id: currentAction.id
})
if(currentAction.id != actionNumber) lastEmulatedEnd = currentAction.id
actionNumber++
currentAction = null
}
function updateLocation(event, inAction, special) {
event = event || currentAction
currentLocation = special ? {
x: event.x1,
y: event.y1,
z: event.z1,
w: event.w || currentLocation.w, // Should be a skill flag maybe?
inAction
} : {
x: event.x,
y: event.y,
z: event.z,
w: event.w,
inAction
}
}
// The real server uses loaded maps and a physics engine for skill movement, which would be costly to simulate
// However the client avoids teleporting the player if the sent position is close enough, so we can simply approximate it instead
function movePlayer(distance) {
if(distance && !currentLocation.inAction) {
let r = (currentLocation.w / 0x8000) * Math.PI
currentLocation.x += Math.cos(r) * distance
currentLocation.y += Math.sin(r) * distance
}
}
function skillId(id, local) {
if(!local) id -= 0x4000000
return [Math.floor(id / 10000), Math.floor(id / 100) % 100, id % 100].join('-')
}
function skillInfo(id, local) {
if(!local) id -= 0x4000000
if(skillsCache[id] !== undefined) return skillsCache[id]
let group = Math.floor(id / 10000),
sub = id % 100,
info = [get(skills, job, '*'), get(skills, job, group, '*'), get(skills, job, group, sub)]
if(info[info.length - 1]) return skillsCache[id] = Object.assign({}, ...info)
return skillsCache[id] = null
}
function get(obj, ...keys) {
if(obj === undefined) return
for(let key of keys)
if((obj = obj[key]) === undefined)
return
return obj
}
}