-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSVEdict.pas
524 lines (457 loc) · 11.7 KB
/
SVEdict.pas
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
unit SVEdict;
{$I HLDS.inc}
interface
uses Default, SDK;
procedure SetMinMaxSize(var E: TEdict; const MinS, MaxS: TVec3);
function SV_ModelIndex(Name: PLChar): UInt;
procedure SV_LoadEntities;
procedure SV_ClearEntities;
procedure SV_GetPlayerHulls;
procedure SV_FreePMSimulator;
procedure SV_AllocatePMSimulator;
procedure SV_CreateBaseline;
procedure SV_WriteEntitiesToClient(var C: TClient; var SB: TSizeBuf);
procedure SV_CleanupEnts;
var
sv_instancedbaseline: TCVar = (Name: 'sv_instancedbaseline'; Data: '1');
LocalModels: array[0..MAX_MODELS - 1] of array[1..16] of LChar;
implementation
uses Common, Console, Delta, Edict, GameLib, Host, MathLib, Memory, MsgBuf, PMove, SVClient, SVDelta, SVEvent, SVMain, SVPhys, SVWorld, SysMain;
var
SVPlayerModel: Int; // signed
SVLastNum: UInt;
SVInstanceBaselines: TServerBaseline;
DeltaCallback: record
EntNumber: PUInt32; // +0
Index: UInt; // +4
NoDelta, Custom: Boolean; // +8, +12
HasES: Boolean; // +16
ES: UInt; // +20
HasBestBaseline: Boolean; // +24
BaselineIndex: Int; // +28
end;
procedure SetMinMaxSize(var E: TEdict; const MinS, MaxS: TVec3);
begin
if (MaxS[0] < MinS[0]) or (MaxS[1] < MinS[1]) or (MaxS[2] < MinS[2]) then
Host_Error(['SetMinMaxSize: Backwards mins/maxs on "', PLChar(PRStrings + E.V.ClassName), '".']);
E.V.MinS := MinS;
E.V.MaxS := MaxS;
VectorSubtract(MaxS, MinS, E.V.Size);
SV_LinkEdict(E, False);
end;
function SV_ModelIndex(Name: PLChar): UInt;
var
I: UInt;
begin
if (Name <> nil) and (Name^ > #0) then
begin
for I := 0 to MAX_MODELS - 1 do
if SV.PrecachedModelNames[I] = nil then
Break
else
if StrIComp(Name, SV.PrecachedModelNames[I]) = 0 then
begin
Result := I;
Exit;
end;
Sys_Error(['SV_ModelIndex: Model "', Name, '" is not precached.']);
end;
Result := 0;
end;
procedure SV_LoadEntities;
begin
ED_LoadFromFile(SV.WorldModel.Entities);
end;
procedure SV_ClearEntities;
var
I: Int;
begin
for I := 0 to SV.NumEdicts - 1 do
if SV.Edicts[I].Free = 0 then
ReleaseEntityDLLFields(SV.Edicts[I]);
end;
procedure SV_FindModelNumbers;
var
I: UInt;
S: PLChar;
begin
SVPlayerModel := -1;
for I := 0 to MAX_MODELS - 1 do
begin
S := SV.PrecachedModelNames[I];
if S = nil then
Exit
else
if (StrIComp(S, 'models\player.mdl') = 0) or (StrIComp(S, 'models/player.mdl') = 0) then
SVPlayerModel := I;
end;
end;
procedure SV_CreateBaseline;
var
B: Boolean;
OS: TEntityState;
NS: PEntityState;
I: Int;
E: PEdict;
D: PDelta;
begin
SV.Baseline := @SVInstanceBaselines;
SV_FindModelNumbers;
for I := 0 to SV.NumEdicts - 1 do
begin
E := @SV.Edicts[I];
B := (I >= 1) and (UInt(I) <= SVS.MaxClients);
if (E.Free = 0) and (B or (E.V.ModelIndex <> 0)) then
begin
NS := @SV.EntityState[I];
NS.Number := I;
if (E.V.Flags and FL_CUSTOMENTITY) > 0 then
NS.EntityType := ENTITY_BEAM
else
NS.EntityType := ENTITY_NORMAL;
DLLFunctions.CreateBaseline(Int(B), I, NS, E^, SVPlayerModel, @PlayerMinS[0], @PlayerMaxS[0], 0, 0, 1, 0);
if B then
begin
NS.MinS := PVec3(@PlayerMinS)^;
NS.MaxS := PVec3(@PlayerMaxS)^;
end;
SVLastNum := I;
end;
end;
DLLFunctions.CreateInstancedBaselines;
MemSet(OS, SizeOf(OS), 0);
MSG_WriteByte(SV.Signon, SVC_SPAWNBASELINE);
MSG_StartBitWriting(SV.Signon);
for I := 0 to SV.NumEdicts - 1 do
begin
E := @SV.Edicts[I];
B := (I >= 1) and (UInt(I) <= SVS.MaxClients);
if (E.Free = 0) and (B or (E.V.ModelIndex <> 0)) then
begin
MSG_WriteBits(I, 11);
NS := @SV.EntityState[I];
MSG_WriteBits(NS.EntityType, 2);
if NS.EntityType = ENTITY_BEAM then
D := CustomEntityDelta
else
if B then
D := PlayerDelta
else
D := EntityDelta;
Delta_WriteDelta(@OS, NS, True, D^, nil);
end;
end;
MSG_WriteBits(65535, 16);
MSG_WriteBits(SV.Baseline.NumEnts, 6);
for I := 0 to SV.Baseline.NumEnts - 1 do
Delta_WriteDelta(@OS, @SV.Baseline.ES[I], True, EntityDelta^, nil);
MSG_EndBitWriting;
end;
procedure SV_GetPlayerHulls;
var
I: UInt;
begin
for I := 0 to 3 do
if DLLFunctions.GetHullBounds(I, PlayerMinS[I], PlayerMaxS[I]) = 0 then
Break;
end;
procedure SV_FreePMSimulator;
begin
if MovedEdict <> nil then
Mem_FreeAndNil(MovedEdict);
if MovedFrom <> nil then
Mem_FreeAndNil(MovedFrom);
end;
procedure SV_AllocatePMSimulator;
begin
if MovedEdict <> nil then
Mem_FreeAndNil(MovedEdict);
if MovedFrom <> nil then
Mem_FreeAndNil(MovedFrom);
if SV.MaxEdicts > 0 then
begin
MovedEdict := Mem_ZeroAlloc(SizeOf(PEdict) * SV.MaxEdicts);
MovedFrom := Mem_ZeroAlloc(SizeOf(TVec3) * SV.MaxEdicts);
end
else
DPrint('SV_ReallocateDynamicData: No edicts.');
end;
procedure SV_SetCallback(Index: UInt; NoDelta, Custom: Boolean; EntNumber: PUInt32; HasBestBaseline: Boolean; BaselineIndex: Int);
begin
DeltaCallback.EntNumber := EntNumber;
DeltaCallback.Index := Index;
DeltaCallback.NoDelta := NoDelta;
DeltaCallback.Custom := Custom;
DeltaCallback.HasES := False;
DeltaCallback.ES := 0;
DeltaCallback.HasBestBaseline := HasBestBaseline;
DeltaCallback.BaselineIndex := BaselineIndex;
end;
procedure SV_SetNewInfo(ES: UInt);
begin
DeltaCallback.HasES := True;
DeltaCallback.ES := ES;
end;
procedure SV_WriteDeltaHeader(Index: UInt; NoDelta, Custom: Boolean; EntNumber: PUInt32; HasES: Boolean; ES: UInt; HasBestBaseline: Boolean; BaselineIndex: Int);
var
ID: Int;
B: Boolean;
begin
ID := Index - EntNumber^;
B := False;
if not HasBestBaseline then
MSG_WriteBits(UInt(NoDelta), 1)
else
if ID <> 1 then
MSG_WriteBits(0, 1)
else
begin
MSG_WriteBits(1, 1);
B := True;
end;
if not B then
if (ID > 0) and (ID < MAX_BASELINES) then
begin
MSG_WriteBits(0, 1);
MSG_WriteBits(ID, 6);
end
else
begin
MSG_WriteBits(1, 1);
MSG_WriteBits(Index, 11);
end;
EntNumber^ := Index;
if not NoDelta then
begin
MSG_WriteBits(UInt(Custom), 1);
if SV.Baseline.NumEnts > 0 then
if HasES then
begin
MSG_WriteBits(1, 1);
MSG_WriteBits(ES, 6);
end
else
MSG_WriteBits(0, 1);
if not HasES and HasBestBaseline then
if BaselineIndex <> 0 then
begin
MSG_WriteBits(1, 1);
MSG_WriteBits(BaselineIndex, 6);
end
else
MSG_WriteBits(0, 1);
end;
end;
procedure SV_InvokeCallback;
begin
SV_WriteDeltaHeader(DeltaCallback.Index, DeltaCallback.NoDelta, DeltaCallback.Custom, DeltaCallback.EntNumber,
DeltaCallback.HasES, DeltaCallback.ES, DeltaCallback.HasBestBaseline, DeltaCallback.BaselineIndex);
end;
function SV_FindBestBaseline(PackNum: Int; var Best: PEntityState; ESPack: PEntityStateArray; EntIndex: UInt; Custom: Boolean): Int;
var
D: PDelta;
OS, NS: PEntityState;
BestIndex, BestScore, I, K, J: Int;
begin
if Custom then
D := CustomEntityDelta
else
if SV_IsPlayerIndex(EntIndex) then
D := PlayerDelta
else
D := EntityDelta;
OS := Best;
NS := @ESPack[PackNum];
BestScore := Delta_TestDelta(OS, NS, D^) - 6; // how many bits should be sent
BestIndex := PackNum;
J := 1;
for I := PackNum - 1 downto 0 do
begin
if (BestScore <= 0) or (J >= MAX_BASELINES - 1) then
Break;
OS := @ESPack[I];
if OS.EntityType = NS.EntityType then
begin
K := Delta_TestDelta(OS, NS, D^);
if K < BestScore then
begin
BestScore := K;
BestIndex := I;
end;
end;
Inc(J);
end;
Result := PackNum - BestIndex;
if PackNum <> BestIndex then
Best := @ESPack[BestIndex];
end;
procedure SV_CreatePacketEntities(DeltaCompression: Boolean; var C: TClient; var DstPack: TPacketEntities; var SB: TSizeBuf);
var
SrcPack: PPacketEntities;
SrcNumEnts, I, J, DstEntNum, SrcEntNum: UInt;
K: Int;
B: Boolean;
ESIndex: UInt32;
D: PDelta;
DstEdict: PEdict;
Best: PEntityState;
begin
if DeltaCompression then
begin
SrcPack := @C.Frames[SVUpdateMask and C.UpdateMask].Pack;
SrcNumEnts := SrcPack.NumEnts;
MSG_WriteByte(SB, SVC_DELTAPACKETENTITIES);
MSG_WriteShort(SB, DstPack.NumEnts);
MSG_WriteByte(SB, C.UpdateMask);
end
else
begin
SrcPack := nil;
SrcNumEnts := 0;
MSG_WriteByte(SB, SVC_PACKETENTITIES);
MSG_WriteShort(SB, DstPack.NumEnts);
end;
MSG_StartBitWriting(SB);
I := 0;
J := 0;
while (I < DstPack.NumEnts) or (J < SrcNumEnts) do
begin
if I < DstPack.NumEnts then
DstEntNum := DstPack.Ents[I].Number
else
DstEntNum := 9999;
if J < SrcNumEnts then
SrcEntNum := SrcPack.Ents[J].Number
else
SrcEntNum := 9999;
if SrcEntNum = DstEntNum then
begin
B := DstPack.Ents[I].EntityType = ENTITY_BEAM;
SV_SetCallback(DstEntNum, False, B, @ESIndex, False, 0);
if B then
D := CustomEntityDelta
else
if SV_IsPlayerIndex(DstEntNum) then
D := PlayerDelta
else
D := EntityDelta;
Delta_WriteDelta(@SrcPack.Ents[J], @DstPack.Ents[I], False, D^, SV_InvokeCallback);
Inc(J);
Inc(I);
end
else
if SrcEntNum < DstEntNum then
begin
SV_WriteDeltaHeader(SrcEntNum, True, False, @ESIndex, False, 0, False, 0);
Inc(J);
Continue;
end
else
begin
B := DstPack.Ents[I].EntityType = ENTITY_BEAM;
SV_SetCallback(DstEntNum, False, B, @ESIndex, SrcPack = nil, 0);
Best := @SV.EntityState[DstEntNum];
if (sv_instancedbaseline.Value = 0) or (SV.Baseline.NumEnts = 0) or (SVLastNum >= DstEntNum) then
if SrcPack = nil then
begin
K := SV_FindBestBaseline(I, Best, DstPack.Ents, DstEntNum, B);
if K <> 0 then
SV_SetCallback(DstEntNum, False, B, @ESIndex, True, K);
end
else
else
begin
DstEdict := @SV.Edicts[DstEntNum];
for K := 0 to SV.Baseline.NumEnts - 1 do
if SV.Baseline.Classnames[K] = DstEdict.V.ClassName then
begin
SV_SetNewInfo(K);
Best := @SV.Baseline.ES[K];
Break;
end;
end;
if B then
D := CustomEntityDelta
else
if SV_IsPlayerIndex(DstEntNum) then
D := PlayerDelta
else
D := EntityDelta;
Delta_WriteDelta(Best, @DstPack.Ents[I], True, D^, SV_InvokeCallback);
Inc(I);
end;
end;
MSG_WriteBits(0, 16);
MSG_EndBitWriting;
end;
procedure SV_EmitPacketEntities(var C: TClient; var Dst: TPacketEntities; var SB: TSizeBuf);
begin
SV_CreatePacketEntities(C.UpdateMask <> -1, C, Dst, SB);
end;
procedure SV_WriteEntitiesToClient(var C: TClient; var SB: TSizeBuf);
var
Frame: PClientFrame;
PVS, PAS: PByte;
IsPlayer: Boolean;
I: Int;
InPack: UInt;
P: PClient;
ES: array[0..MAX_PACKET_ENTITIES - 1] of TEntityState;
A: PEntityStateArray;
begin
Frame := @C.Frames[SVUpdateMask and C.Netchan.OutgoingSequence];
PVS := nil;
PAS := nil;
InPack := 0;
DLLFunctions.SetupVisibility(C.Target^, C.Entity^, PVS, PAS);
SV_ClearPacketEntities(Frame^, False);
if (sv_keepframes.Value <> 0) and (Frame.Pack.Ents <> nil) then
A := Frame.Pack.Ents
else
A := @ES;
for I := 1 to SV.NumEdicts - 1 do
begin
IsPlayer := UInt(I) <= SVS.MaxClients;
if IsPlayer then
begin
P := @SVS.Clients[I - 1];
if (not P.Active and not P.Spawned) or P.HLTV then
Continue;
end;
if InPack >= MAX_PACKET_ENTITIES then
begin
DPrint('Too many entities in visible packet list.');
Break;
end
else
if DLLFunctions.AddToFullPack(A[InPack], I, SV.Edicts[I], C.Entity^, UInt(C.LW), UInt(IsPlayer), PVS) <> 0 then
Inc(InPack);
end;
if A = @ES then
begin
SV_AllocPacketEntities(Frame^, InPack);
if Frame.Pack.NumEnts > 0 then
Move(ES, Frame.Pack.Ents^, SizeOf(TEntityState) * Frame.Pack.NumEnts);
end
else
Frame.Pack.NumEnts := InPack;
SV_EmitPacketEntities(C, Frame.Pack, SB);
SV_EmitEvents(C, Frame.Pack, SB);
if SV_ShouldUpdatePing(C) then
begin
SV_EmitPings(SB);
C.NextPingTime := RealTime + sv_pinginterval.Value;
end;
end;
procedure SV_CleanupEnts;
var
I: Int;
E: PEdict;
begin
for I := 1 to SV.NumEdicts - 1 do
begin
E := @SV.Edicts[I];
E.V.Effects := E.V.Effects and not (EF_MUZZLEFLASH or EF_NOINTERP);
end;
end;
end.