-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlagrecord.lua
More file actions
472 lines (361 loc) · 14 KB
/
lagrecord.lua
File metadata and controls
472 lines (361 loc) · 14 KB
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
-- Variables
local sv_maxunlag = cvar.sv_maxunlag
local host_frameticks = ffi.cast('uint32_t*', utils.opcode_scan('engine.dll', '03 05 ? ? ? ? 83 CF 10', 0x2))
local host_currentframetick = ffi.cast('uint32_t*', utils.opcode_scan('engine.dll', '2B 05 ? ? ? ? 03 05 ? ? ? ? 83 CF 10', 0x2))
-- Functions
local new_class = function()
local mt, mt_data, this_mt = { }, { }
mt.__metatable = false
mt_data.struct = function(self, name)
assert(type(name) == 'string', 'invalid class name')
assert(rawget(self, name) == nil, 'cannot overwrite subclass')
return function(data)
assert(type(data) == 'table', 'invalid class data')
rawset(self, name, setmetatable(data, {
__metatable = false,
__index = function(self, key)
return
rawget(mt, key) or
rawget(this_mt, key)
end
}))
return this_mt
end
end
this_mt = setmetatable(mt_data, mt)
return this_mt
end
local insert = function(tbl, new_value)
local new_tbl = {}
new_tbl[#new_tbl+1] = new_value
for _, value in pairs(tbl) do
if value ~= nil then
new_tbl[#new_tbl+1] = value
end
end
return new_tbl
end
local evnt do (function()
local c_list = { }
local function register_callback(fn)
assert(type(fn) == 'function', 'callback has to be a function')
local already_exists = false
for _, this in pairs(c_list) do
if this == fn then
already_exists = true
break
end
end
if already_exists then
error('the function callback is already registered', 3)
end
table.insert(c_list, fn)
end
local function unregister_callback(fn)
assert(type(fn) == 'function', 'callback has to be a function')
for index, this in pairs(c_list) do
if this == fn then
table.remove(c_list, index)
return true
end
end
return false
end
local function get_list()
return c_list
end
local function fire_callback(...)
local output = false
for idx, callback in ipairs(c_list) do
local success, result = pcall(callback, ...)
if success == true and result == true then
output = true
break
end
end
return output
end
evnt = {
register = register_callback,
unregister = unregister_callback,
fire_callback = fire_callback,
get_list = get_list
}
end)() end
-- Global Class
local ctx = new_class()
:struct 'lagrecord' {
data = { },
estimated_tickbase = 0,
local_player_tickbase = 0,
purge = function(self, player)
if player == nil then
self.estimated_tickbase = 0
self.local_player_tickbase = 0
self.data = { }
return
end
self.data[player:get_index()] = { }
end,
track_time = function(self, cmd)
self.estimated_tickbase = globals.estimated_tickbase
if cmd.choked_commands == 0 then
self.local_player_tickbase = entity.get_local_player().m_nTickBase
end
end,
get_server_time = function(self, as_ticks)
local predicted_server_tick = globals.client_tick + globals.clock_offset
if host_frameticks ~= nil and host_currentframetick ~= nil then
local delta = host_frameticks[0] - host_currentframetick[0]
local max_delta_for_tick_rate = math.floor(1 / globals.tickinterval) / 8
if delta > 0 and delta < max_delta_for_tick_rate then
predicted_server_tick = predicted_server_tick + delta
end
end
return as_ticks ~= true and to_time(predicted_server_tick) or predicted_server_tick
end,
get_player_time = function(self, player, as_tick)
assert(player ~= nil and player.get_simulation_time ~= nil, 'invalid player')
if player == entity.get_local_player() then
local m_nTickBase = self.local_player_tickbase -- player.m_nTickBase
return as_tick ~= true and to_time(m_nTickBase) or m_nTickBase
end
local simulation_time = player:get_simulation_time().current
return as_tick == true and
self:to_ticks(simulation_time) or simulation_time
end,
get_dead_time = function(self, as_tick)
local sv_maxunlag = sv_maxunlag:float()
local outgoing_latency = utils.net_channel().latency[0]
local dead_time = to_time(self.estimated_tickbase) - outgoing_latency - sv_maxunlag
return as_tick == true and to_ticks(dead_time) or dead_time
end,
verify_records = function(self, userptr, dead_time, is_alive)
if userptr == nil or
userptr.records == nil or userptr.localdata == nil then
return
end
-- make sure we dont keep old records if those become invalid
local records, localdata = userptr.records, userptr.localdata
local first_rec_origin = records[1] and records[1].origin
local allow_updates = localdata.allow_updates
for idx, this in ipairs(records) do
local c_idx = idx ~= 1
if allow_updates == false then
c_idx = true
end
if is_alive == false then
rawset(records, idx, nil)
elseif c_idx == true and first_rec_origin then
if this.simulation_time <= dead_time then
-- purge current record if simulation time is too old
rawset(records, idx, nil)
elseif first_rec_origin:distsqr(this.origin) > 4096 then
-- purge records if teleport distance is too big
for i=2, #records do
rawset(records, i, nil)
end
break
end
end
end
end,
on_net_update = function(self, player, tick, dead_time)
assert(player ~= nil and player.get_simulation_time ~= nil, 'invalid player')
local index = player:get_index()
local origin = player:get_origin()
local is_alive = player:is_alive()
self.data[index] = self.data[index] or new_class()
:struct 'records' { }
:struct 'localdata' {
allow_updates = false,
updated_this_frame = false,
last_animated_simulation = 0,
no_entry = vector(),
cycle = 0
}
-- preserve data
local user = self.data[index]
local records, localdata = user.records, user.localdata
local simulation_time = self:get_player_time(player)
-- set update state to false
localdata.allow_updates = evnt.fire_callback(player)
localdata.updated_this_frame = false
if localdata.allow_updates == false or
is_alive == false or player:is_dormant() == true then
goto verify_records
end
do
local shifted_forwards = records[1] and
math.max(0, to_ticks(records[1].simulation_time - simulation_time)) or 0
if shifted_forwards > 0 and localdata.no_entry.x == 0 then
localdata.no_entry.y = shifted_forwards
elseif shifted_forwards <= 0 then
localdata.no_entry.y = 0
end
localdata.cycle = records[1] and math.max(0, tick - records[1].tick - 1) or 0
localdata.no_entry.x = shifted_forwards
localdata.last_animated_simulation = simulation_time
if records[1] and simulation_time <= records[1].simulation_time then
goto verify_records
end
-- STAGE: PLAYER_UPDATE
localdata.updated_this_frame = true
rawset(user, 'records', insert(records, {
tick = tick,
shifting = to_ticks(simulation_time) - tick - 1,
elapsed = math.clamp(records[1] and (tick - records[1].tick - 1) or 0, 0, 72),
choked = math.clamp(records[1] and (to_ticks(simulation_time - records[1].simulation_time) - 1) or 0, 0, 72),
origin = origin,
origin_old = records[1] and records[1].origin or origin,
simulation_time = simulation_time,
simulation_time_old = records[1] and records[1].simulation_time or simulation_time,
angles = player:get_angles(),
eye_position = player:get_eye_position(),
volume = { player.m_vecMins, player.m_vecMaxs }
}))
-- invoke entity update callback
events.entity_update:call {
tick = tick,
index = index,
entity = player
}
end
::verify_records::
self:verify_records(user, dead_time, is_alive)
end
}
:struct 'output' {
get_player_idx = function(self, ...)
local va = { ... }
if #va == 0 then
local me = entity.get_local_player()
if me == nil then
return
end
return me:get_index()
end
local va = va[1]
local va_type = type(va)
if va == nil or va_type == 'nil' then
return
end
if va_type == 'userdata' and va.get_index then
return va:get_index()
end
if va_type == 'userdata' or va_type == 'cdata' or va_type == 'number' then
local player = entity.get(va)
if player == nil then
return
end
return player:get_index()
end
return nil
end,
get_player_data = function(self, ...)
local index = self:get_player_idx(...)
if index == nil then
return
end
local data = self.lagrecord.data[index]
if data == nil or data.localdata == nil or data.records == nil then
return
end
return data
end,
get_all = function(self, ...)
local data = self:get_player_data(...)
if data == nil then
return
end
return data.records
end,
get_record = function(self, ...)
local data = self:get_player_data(...)
if data == nil then
return
end
return data.records[({ ... })[2] or 1]
end,
get_snapshot = function(self, ...)
local data = self:get_player_data(...)
if data == nil then
return
end
local record_at = ({ ... })[2] or 1
local record = data.records[record_at]
if record == nil then
return
end
return {
id = record_at,
tick = record.tick,
updated_this_frame = data.localdata.updated_this_frame,
origin = {
angles = record.angles,
volume = record.volume,
current = record.origin,
previous = record.origin_old,
change = record.origin:distsqr(record.origin_old)
},
simulation_time = {
animated = data.localdata.last_animated_simulation,
current = record.simulation_time,
previous = record.simulation_time_old,
change = record.simulation_time - record.simulation_time_old
},
command = {
elapsed = record.elapsed,
choke = record.choked,
cycle = data.localdata.cycle,
shifting = record.shifting,
no_entry = data.localdata.no_entry,
}
}, record
end,
get_server_time = function(self, ...)
return self.lagrecord:get_server_time(...)
end
}
-- Callbacks
events.level_init:set(function() ctx.lagrecord:purge() end)
events.createmove:set(function(cmd) ctx.lagrecord:track_time(cmd) end)
events.net_update_end:set(function()
local lagrecord = ctx.lagrecord
local me = entity.get_local_player()
local tick = lagrecord:get_server_time(true)
local dead_time = lagrecord:get_dead_time(false)
if me == nil or globals.is_in_game == false then
lagrecord:purge()
return
end
if me:is_alive() == false then
lagrecord.estimated_tickbase = globals.client_tick + globals.clock_offset
end
entity.get_players(false, true, function(player)
lagrecord:on_net_update(player, tick, dead_time)
end)
end)
return {
set_update_callback = function(...)
return evnt.register(...)
end,
unset_update_callback = function(...)
return evnt.unregister(...)
end,
get_player_data = function(...)
return ctx.output:get_player_data(...)
end,
get_all = function(...)
return ctx.output:get_all(...)
end,
get_record = function(...)
return ctx.output:get_record(...)
end,
get_snapshot = function(...)
return ctx.output:get_snapshot(...)
end,
get_server_time = function(...)
return ctx.output:get_server_time(...)
end
}