-
Notifications
You must be signed in to change notification settings - Fork 5
/
d_net.pas
1039 lines (891 loc) · 26.8 KB
/
d_net.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
//
// FPCDoom - Port of Doom to Free Pascal Compiler
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 2004-2007 by Jim Valavanis
// Copyright (C) 2017-2022 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//------------------------------------------------------------------------------
// E-Mail: [email protected]
// Site : https://sourceforge.net/projects/fpcdoom/
//------------------------------------------------------------------------------
{$I FPCDoom.inc}
unit d_net;
interface
uses
d_fpc,
doomdef,
d_player,
d_ticcmd;
const
// Networking and tick handling related.
BACKUPTICS = 12;
type
Tcmds = packed array[0..BACKUPTICS - 1] of ticcmd_t;
//
// Network packet data.
//
doomdata_t = record
// High bit is retransmit request.
checksum: LongWord;
// Only valid if NCMD_RETRANSMIT.
retransmitfrom: byte;
starttic: byte;
player: byte;
numtics: byte;
flags: longword;
cmds: Tcmds;
end;
Pdoomdata_t = ^doomdata_t;
var
netbuffer: Pdoomdata_t; // points inside doomcom
//
// Network play related stuff.
// There is a data struct that stores network
// communication related stuff, and another
// one that defines the actual packets to
// be transmitted.
//
const
DOOMCOM_ID = $12345678;
// Max computers/players in a game.
MAXNETNODES = 8;
CMD_SEND = 1;
CMD_GET = 2;
type
doomcom_t = record
// Supposed to be DOOMCOM_ID?
id: integer;
// DOOM executes an int to execute commands.
intnum: smallint;
// Communication between DOOM and the driver.
// Is CMD_SEND or CMD_GET.
command: smallint;
// Is dest for send, set by get (-1 = no packet).
remotenode: smallint;
// Number of bytes in doomdata to be sent
datalength: smallint;
// Info common to all nodes.
// Console is allways node 0.
numnodes: smallint;
// Flag: 1 = no duplication, 2-5 = dup for slow nets.
ticdup: smallint;
// Flag: 1 = send a backup tic in every packet.
extratics: smallint;
// Flag: 1 = deathmatch.
deathmatch: smallint;
// Flag: -1 = new game, 0-5 = load savegame
savegame: smallint;
episode: smallint; // 1-3
map: smallint; // 1-9
skill: smallint; // 1-5
// Info specific to this node.
consoleplayer: smallint;
numplayers: smallint;
// These are related to the 3-display mode,
// in which two drones looking left and right
// were used to render two additional views
// on two additional computers.
// Probably not operational anymore.
// 1 = left, 0 = center, -1 = right
angleoffset: smallint;
// 1 = drone
drone: smallint;
// The packet data to be sent.
data: doomdata_t;
end;
Pdoomcom_t = ^doomcom_t;
{ Create any new ticcmds and broadcast to other players. }
//==============================================================================
//
// NetUpdate
//
//==============================================================================
procedure NetUpdate;
{ Broadcasts special packets to other players }
{ to notify of game exit }
//==============================================================================
//
// D_QuitNetGame
//
//==============================================================================
procedure D_QuitNetGame;
{? how many ticks to run? }
//==============================================================================
//
// D_RunMultipleTicks
//
//==============================================================================
procedure D_RunMultipleTicks;
//==============================================================================
//
// D_RunSingleTick
//
//==============================================================================
procedure D_RunSingleTick;
//==============================================================================
//
// D_CheckNetGame
//
//==============================================================================
procedure D_CheckNetGame;
var
netcmds: array[0..MAXPLAYERS - 1] of array[0..BACKUPTICS - 1] of ticcmd_t;
maketic: integer;
doomcom: Pdoomcom_t;
ticdup: integer;
var
isinterpolateddisplay: boolean;
firstinterpolation: boolean;
implementation
uses
m_menu,
m_fixed,
e_endoom,
c_con,
i_system,
i_net,
i_io,
d_main,
d_event,
r_intrpl,
r_main,
p_mobj_h,
p_terrain,
g_game;
const
NCMD_EXIT = $80000000;
NCMD_RETRANSMIT = $40000000;
NCMD_SETUP = $20000000;
NCMD_KILL = $10000000; // kill game
NCMD_CHECKSUM = $0fffffff;
//
// NETWORKING
//
// gametic is the tic about to (or currently being) run
// maketic is the tick that hasn't had control made for it yet
// nettics[] has the maketics for all players
//
// a gametic cannot be run until nettics[] > gametic for all players
//
const
CRESENDCOUNT = 10;
PL_DRONE = $80; // bit flag in doomdata->player
var
localcmds: array[0..BACKUPTICS - 1] of ticcmd_t;
nettics: array[0..MAXNETNODES - 1] of integer;
nodeingame: array[0..MAXNETNODES - 1] of boolean; // set false as nodes leave game
remoteresend: array[0..MAXNETNODES - 1] of boolean; // set when local needs tics
resendto: array[0..MAXNETNODES - 1] of integer; // set when remote needs tics
resendcount: array[0..MAXNETNODES - 1] of integer;
nodeforplayer: array[0..MAXPLAYERS - 1] of integer;
skiptics: integer;
maxsend: integer; // BACKUPTICS/(2*ticdup)-1
var
reboundpacket: boolean;
reboundstore: doomdata_t;
//==============================================================================
// NetbufferSize
//
//==============================================================================
function NetbufferSize: integer;
begin
result := integer(@Pdoomdata_t(0).cmds[netbuffer.numtics]);
end;
//==============================================================================
// NetbufferChecksum
//
// Checksum
//
//==============================================================================
function NetbufferChecksum: LongWord;
var
i: integer;
p: PLongWordArray;
begin
result := $1234567;
p := PLongWordArray(netbuffer);
// Hack: first position of doomdata_t is checksum (longword)
for i := 1 to NetbufferSize div 4 do
result := result + p[i];
result := result and NCMD_CHECKSUM;
end;
//==============================================================================
//
// ExpandTics
//
//==============================================================================
function ExpandTics(low: integer): integer;
var
delta: integer;
begin
delta := low - (maketic and $ff);
if (delta >= -64) and (delta <= 64) then
begin
result := (maketic and not $ff) + low;
exit;
end;
if delta > 64 then
begin
result := (maketic and not $ff) - 256 + low;
exit;
end;
if delta < -64 then
begin
result := (maketic and not $ff) + 256 + low;
exit;
end;
I_Error('ExpandTics(): strange value %d at maketic %d', [low, maketic]);
result := 0;
end;
//==============================================================================
//
// HSendPacket
//
//==============================================================================
procedure HSendPacket(node: integer; flags: LongWord);
var
i: integer;
realretrans: integer;
p: PByteArray;
begin
netbuffer.checksum := NetbufferChecksum or flags;
if node = 0 then
begin
reboundstore := netbuffer^;
reboundpacket := true;
exit;
end;
if demoplayback then
exit;
if not netgame then
I_Error('HSendPacket(): Tried to transmit to another node');
doomcom.command := CMD_SEND;
doomcom.remotenode := node;
doomcom.datalength := NetbufferSize;
if devparm and (debugfile <> nil) then
begin
if netbuffer.checksum and NCMD_RETRANSMIT <> 0 then
realretrans := ExpandTics(netbuffer.retransmitfrom)
else
realretrans := -1;
fprintf(debugfile, 'send (%d(starttic) + %d(numtics), R %d(realretrans)) [%d(datalength)] ',
[ExpandTics(netbuffer.starttic), netbuffer.numtics,
realretrans, doomcom.datalength]);
p := PByteArray(netbuffer);
for i := 0 to doomcom.datalength - 1 do
fprintf(debugfile, '%d ', [p[i]]);
fprintf(debugfile, #13#10);
end;
I_NetCmd;
end;
//==============================================================================
//
// HGetPacket
// Returns false if no packet is waiting
//
//==============================================================================
function HGetPacket: boolean;
var
realretrans: integer;
i: integer;
p: PByteArray;
begin
if reboundpacket then
begin
netbuffer^ := reboundstore;
doomcom.remotenode := 0;
reboundpacket := false;
result := true;
exit;
end;
if not netgame then
begin
result := false;
exit;
end;
if demoplayback then
begin
result := false;
exit;
end;
doomcom.command := CMD_GET;
I_NetCmd;
if doomcom.remotenode = -1 then
begin
result := false;
exit;
end;
if doomcom.datalength <> NetbufferSize then
begin
if devparm and (debugfile <> nil) then
fprintf(debugfile, 'bad packet length %d'#13#10, [doomcom.datalength]);
result := false;
exit;
end;
if NetbufferChecksum <> (netbuffer.checksum and NCMD_CHECKSUM) then
begin
if devparm and (debugfile <> nil) then
fprintf(debugfile, 'bad packet checksum'#13#10);
result := false;
exit;
end;
if devparm and (debugfile <> nil) then
begin
if netbuffer.checksum and NCMD_SETUP <> 0 then
fprintf(debugfile, 'setup packet'#13#10)
else
begin
if netbuffer.checksum and NCMD_RETRANSMIT <> 0 then
realretrans := ExpandTics(netbuffer.retransmitfrom)
else
realretrans := -1;
fprintf(debugfile, 'get %d = (%d + %d, R %d)[%d] ',
[doomcom.remotenode, ExpandTics(netbuffer.starttic),
netbuffer.numtics, realretrans, doomcom.datalength]);
p := PByteArray(netbuffer);
for i := 0 to doomcom.datalength - 1 do
fprintf(debugfile, '%d ', [p[i]]);
fprintf(debugfile, #13#10);
end;
end;
result := true;
end;
//==============================================================================
//
// GetPackets
//
//==============================================================================
procedure GetPackets;
var
netconsole: integer;
netnode: integer;
src, dest: Pticcmd_t;
realend: integer;
realstart: integer;
start: integer;
exitmsg: string;
begin
while HGetPacket do
begin
if netbuffer.checksum and NCMD_SETUP <> 0 then
continue; // extra setup packet
netconsole := netbuffer.player and not PL_DRONE;
netnode := doomcom.remotenode;
// to save bytes, only the low byte of tic numbers are sent
// Figure out what the rest of the bytes are
realstart := ExpandTics(netbuffer.starttic);
realend := realstart + netbuffer.numtics;
// check for exiting the game
if netbuffer.checksum and NCMD_EXIT <> 0 then
begin
if not nodeingame[netnode] then
continue;
nodeingame[netnode] := false;
playeringame[netconsole] := false;
exitmsg := 'Player 1 left the game';
exitmsg[7] := chr(Ord(exitmsg[7]) + netconsole);
players[consoleplayer]._message := exitmsg;
if demorecording then
G_CheckDemoStatus;
continue;
end;
// check for a remote game kill
if netbuffer.checksum and NCMD_KILL <> 0 then
I_Error('GetPackets(): Killed by network driver');
nodeforplayer[netconsole] := netnode;
// check for retransmit request
if (resendcount[netnode] <= 0) and ((netbuffer.checksum and NCMD_RETRANSMIT) <> 0) then
begin
resendto[netnode] := ExpandTics(netbuffer.retransmitfrom);
if devparm and (debugfile <> nil) then
fprintf(debugfile, 'retransmit from %d'#13#10, [resendto[netnode]]);
resendcount[netnode] := CRESENDCOUNT;
end
else
resendcount[netnode] := resendcount[netnode] - 1;
// check for out of order / duplicated packet
if realend = nettics[netnode] then
continue;
if realend < nettics[netnode] then
begin
if devparm and (debugfile <> nil) then
fprintf(debugfile, 'out of order packet (%d + %d)'#13#10,
[realstart, netbuffer.numtics]);
continue;
end;
// check for a missed packet
if realstart > nettics[netnode] then
begin
// stop processing until the other system resends the missed tics
if devparm and (debugfile <> nil) then
fprintf(debugfile, 'missed tics from %d (%d - %d)'#13#10,
[netnode, realstart, nettics[netnode]]);
remoteresend[netnode] := true;
continue;
end;
// update command store from the packet
remoteresend[netnode] := false;
start := nettics[netnode] - realstart;
src := @netbuffer.cmds[start];
while nettics[netnode] < realend do
begin
dest := @netcmds[netconsole][nettics[netnode] mod BACKUPTICS];
nettics[netnode] := nettics[netnode] + 1;
dest^ := src^;
inc(src);
end;
end;
end;
//
// NetUpdate
// Builds ticcmds for console player,
// sends out a packet
//
var
gametime: integer;
//==============================================================================
//
// NetUpdate
//
//==============================================================================
procedure NetUpdate;
var
nowtime: integer;
newtics: integer;
i, j: integer;
realstart: integer;
gameticdiv: integer;
begin
// check time
nowtime := I_GetTime div ticdup;
newtics := nowtime - gametime;
gametime := nowtime;
if newtics <= 0 then // nothing new to update
begin
GetPackets;
exit;
end;
if skiptics <= newtics then
begin
newtics := newtics - skiptics;
skiptics := 0;
end
else
begin
skiptics := skiptics - newtics;
newtics := 0;
end;
netbuffer.player := consoleplayer;
// build new ticcmds for console player
gameticdiv := gametic div ticdup;
for i := 0 to newtics - 1 do
begin
I_StartTic;
D_ProcessEvents;
if I_GameFinished then
exit;
if maketic - gameticdiv >= BACKUPTICS div 2 - 1 then
break; // can't hold any more
G_BuildTiccmd(@localcmds[maketic mod BACKUPTICS]);
inc(maketic);
end;
if singletics then
exit; // singletic update is syncronous
// send the packet to the other nodes
for i := 0 to doomcom.numnodes - 1 do
if nodeingame[i] then
begin
netbuffer.starttic := resendto[i];
realstart := resendto[i];
netbuffer.numtics := maketic - realstart;
if netbuffer.numtics > BACKUPTICS then
I_Error('NetUpdate(): netbuffer.numtics > BACKUPTICS');
resendto[i] := maketic - doomcom.extratics;
for j := 0 to netbuffer.numtics - 1 do
netbuffer.cmds[j] :=
localcmds[(realstart + j) mod BACKUPTICS];
if remoteresend[i] then
begin
netbuffer.retransmitfrom := nettics[i];
HSendPacket(i, NCMD_RETRANSMIT);
end
else
begin
netbuffer.retransmitfrom := 0;
HSendPacket(i, 0);
end;
end;
// listen for other packets
GetPackets;
end;
//==============================================================================
//
// D_CheckAbort
//
//==============================================================================
procedure D_CheckAbort;
var
ev: Pevent_t;
stoptic: integer;
begin
stoptic := I_GetTime + 2;
while I_GetTime < stoptic do
begin
I_WaitVBL(1);
I_StartTic;
I_ProcessWindows;
end;
I_StartTic;
repeat
ev := @events[eventtail];
if (ev._type = ev_keydown) and (ev.data1 = KEY_ESCAPE) then
I_Error('D_CheckAbort(): Network game synchronization aborted.');
eventtail := (eventtail + 1) and (MAXEVENTS - 1);
until eventtail = eventhead;
end;
//==============================================================================
//
// D_ArbitrateNetStart
//
//==============================================================================
procedure D_ArbitrateNetStart;
var
i: integer;
gotinfo: array[0..MAXNETNODES - 1] of boolean;
begin
autostart := true;
if doomcom.consoleplayer <> 0 then
begin
// listen for setup info from key player
printf('listening for network start info...'#13#10);
while true do
begin
D_CheckAbort;
if not HGetPacket then
continue;
if netbuffer.checksum and NCMD_SETUP <> 0 then
begin
if netbuffer.player <> VERSION then
I_Error('D_ArbitrateNetStart(): Different DOOM versions cannot play a net game!');
startskill := skill_t(netbuffer.retransmitfrom and 15);
deathmatch := _SHR((netbuffer.retransmitfrom and $C0), 6);
nomonsters := (netbuffer.retransmitfrom and $20) > 0;
respawnparm := (netbuffer.retransmitfrom and $10) > 0;
startmap := netbuffer.starttic and $3f;
startepisode := _SHR(netbuffer.starttic, 6);
allowplayerjumps := netbuffer.flags and 1 <> 0;
majorbossdeathendsdoom1level := netbuffer.flags and 2 <> 0;
spawnrandommonsters := netbuffer.flags and 4 <> 0;
allowterrainsplashes := netbuffer.flags and 8 <> 0;
exit;
end;
end // while
end
else
begin
ZeroMemory(@gotinfo, SizeOf(gotinfo));
// key player, send the setup info
printf('sending network start info...'#13#10);
repeat
D_CheckAbort;
for i := 0 to doomcom.numnodes - 1 do
begin
netbuffer.retransmitfrom := Ord(startskill);
if deathmatch <> 0 then
netbuffer.retransmitfrom := netbuffer.retransmitfrom or _SHL(deathmatch, 6);
if nomonsters then
netbuffer.retransmitfrom := netbuffer.retransmitfrom or $20;
if respawnparm then
netbuffer.retransmitfrom := netbuffer.retransmitfrom or $10;
netbuffer.starttic := startepisode * 64 + startmap;
netbuffer.player := VERSION;
netbuffer.numtics := 0;
netbuffer.flags := 0;
if allowplayerjumps then
netbuffer.flags := netbuffer.flags or 1;
if majorbossdeathendsdoom1level then
netbuffer.flags := netbuffer.flags or 2;
if spawnrandommonsters then
netbuffer.flags := netbuffer.flags or 4;
if allowterrainsplashes then
netbuffer.flags := netbuffer.flags or 8;
HSendPacket(i, NCMD_SETUP);
end;
i := 10;
while (i > 0) and HGetPacket do
begin
if (netbuffer.player and $7f) < MAXNETNODES then
gotinfo[netbuffer.player and $7f] := true;
dec(i);
end;
i := 1;
while i < doomcom.numnodes do
begin
if not gotinfo[i] then
break;
inc(i);
end;
until i >= doomcom.numnodes;
end;
end;
//==============================================================================
//
// D_CheckNetGame
// Works out player numbers among the net participants
//
//==============================================================================
procedure D_CheckNetGame;
var
i: integer;
begin
for i := 0 to MAXNETNODES - 1 do
begin
nodeingame[i] := false;
nettics[i] := 0;
remoteresend[i] := false; // set when local needs tics
resendto[i] := 0; // which tic to start sending
end;
// I_InitNetwork sets doomcom and netgame
I_InitNetwork;
if doomcom.id <> DOOMCOM_ID then
I_Error('D_CheckNetGame(): Doomcom buffer invalid!');
netbuffer := @doomcom.data;
consoleplayer := doomcom.consoleplayer;
displayplayer := doomcom.consoleplayer;
if netgame then
D_ArbitrateNetStart;
printf(' startskill %d deathmatch: %d startmap: %d startepisode: %d'#13#10,
[Ord(startskill), deathmatch, startmap, startepisode]);
// read values out of doomcom
ticdup := doomcom.ticdup;
maxsend := BACKUPTICS div (2 * ticdup) - 1;
if maxsend < 1 then
maxsend := 1;
for i := 0 to doomcom.numplayers - 1 do
playeringame[i] := true;
for i := 0 to doomcom.numnodes - 1 do
nodeingame[i] := true;
printf(' player %d of %d (%d nodes)'#13#10,
[consoleplayer + 1, doomcom.numplayers, doomcom.numnodes]);
end;
//==============================================================================
//
// D_QuitNetGame
// Called before quitting to leave a net game
// without hanging the other players
//
//==============================================================================
procedure D_QuitNetGame;
var
i, j: integer;
begin
if not netgame or not usergame or (consoleplayer = -1) or demoplayback then
exit;
// send a bunch of packets for security
netbuffer.player := consoleplayer;
netbuffer.numtics := 0;
for i := 0 to 3 do
begin
for j := 1 to doomcom.numnodes - 1 do
if nodeingame[j] then
HSendPacket(j, NCMD_EXIT);
I_WaitVBL(1);
end;
end;
//
// D_RunMultipleTicks
//
var
frameon: integer = 0;
frameskip: array[0..3] of boolean;
oldnettics: integer;
oldentertics: integer = 0;
//==============================================================================
//
// D_RunMultipleTicks
//
//==============================================================================
procedure D_RunMultipleTicks;
var
i, j: integer;
lowtic: integer;
entertic: integer;
realtics: integer;
availabletics: integer;
counts: integer;
cmd: Pticcmd_t;
buf: integer;
entertime: integer;
begin
// get real tics
entertime := I_GetTime;
entertic := entertime div ticdup;
realtics := entertic - oldentertics;
oldentertics := entertic;
// get available tics
NetUpdate;
if I_GameFinished then
exit;
lowtic := MAXINT;
for i := 0 to doomcom.numnodes - 1 do
begin
if nodeingame[i] then
begin
if nettics[i] < lowtic then
lowtic := nettics[i];
end;
end;
availabletics := lowtic - gametic div ticdup;
// decide how many tics to run
if realtics < availabletics - 1 then
counts := realtics + 1
else if realtics < availabletics then
counts := realtics
else
counts := availabletics;
if counts < 1 then
counts := 1;
inc(frameon);
if not demoplayback then
begin
// ideally nettics[0] should be 1 - 3 tics above lowtic
// if we are consistantly slower, speed up time
i := 0;
while (i < MAXPLAYERS) and not playeringame[i] do
inc(i);
if consoleplayer = i then
begin
// the key player does not adapt
end
else
begin
if nettics[0] <= nettics[nodeforplayer[i]] then
begin
dec(gametime);
// printf ('-');
end;
frameskip[frameon and 3] := (oldnettics > nettics[nodeforplayer[i]]);
oldnettics := nettics[0];
if (frameskip[0] and frameskip[1] and frameskip[2] and frameskip[3]) then
begin
skiptics := 1;
// printf ("+");
end;
end;
end; // demoplayback
didinterpolations := false;
isinterpolateddisplay := true;
firstinterpolation := true;
// wait for new tics if needed
repeat
NetUpdate;
lowtic := MAXINT;
for i := 0 to doomcom.numnodes - 1 do
if nodeingame[i] and (nettics[i] < lowtic) then
lowtic := nettics[i];
if lowtic < gametic div ticdup then
I_Error('D_RunMultipleTicks(): lowtic < gametic');
// don't stay in here forever -- give the menu a chance to work
// JVAL 16/2/2011 - We insist while demo playback or recording!
if not demoplayback and not demorecording then
if I_GetTime div ticdup - entertic >= 10 then
begin
E_Ticker;
M_Ticker;
exit;
end;
if interpolate and (gamestate = GS_LEVEL) and (oldgamestate = Ord(GS_LEVEL)) then
begin
if not didinterpolations then
R_StoreInterpolationData;
if R_Interpolate then
begin
didinterpolations := true;
D_Display;
firstinterpolation := false;
end;
end;
until lowtic >= gametic div ticdup + counts;
if didinterpolations then
R_RestoreInterpolationData;
ticfrac := 0;
isinterpolateddisplay := false;
// run the count * ticdup dics
while counts <> 0 do
begin
dec(counts);
for i := 0 to ticdup - 1 do
begin
if gametic div ticdup > lowtic then
I_Error('D_RunMultipleTicks(): gametic > lowtic');
if advancedemo then
D_DoAdvanceDemo;
R_Ticker;
E_Ticker;
M_Ticker;
C_Ticker;
interpolationstoretime := I_GetTime * FRACUNIT;
G_Ticker;
inc(gametic);
// modify command for duplicated tics
if i <> ticdup - 1 then
begin
buf := (gametic div ticdup) mod BACKUPTICS;
for j := 0 to MAXPLAYERS - 1 do
begin
cmd := @netcmds[j][buf];
cmd.chatchar := 0;
cmd.commands := 0;
if cmd.buttons and BT_SPECIAL <> 0 then
cmd.buttons := 0;