/
sv_pool.cpp
2898 lines (2512 loc) · 82.6 KB
/
sv_pool.cpp
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
/** @file sv_pool.cpp Delta Pools
* @ingroup server
*
* Delta Pools use PU_MAP, which means all the memory allocated for them
* is deallocated when the map changes. Sv_InitPools() is called in
* R_SetupMap() to clear out all the old data.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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, see:
* http://www.gnu.org/licenses</small>
*/
#include <math.h>
#include <de/mathutil.h>
#include <de/vector1.h>
#include "de_base.h"
#include "de_console.h"
#include "de_network.h"
#include "de_play.h"
#include "audio/s_main.h"
#include "world/thinkers.h"
#include "server/sv_pool.h"
using namespace de;
#define DEFAULT_DELTA_BASE_SCORE 10000
#define REG_MOBJ_HASH_SIZE 1024
#define REG_MOBJ_HASH_FUNCTION_MASK 0x3ff
// Maximum difference in plane height where the absolute height doesn't
// need to be sent.
#define PLANE_SKIP_LIMIT (40)
typedef struct reg_mobj_s {
// Links to next and prev mobj in the register hash.
struct reg_mobj_s* next, *prev;
// The tic when the mobj state was last sent.
dt_mobj_t mo; // The state of the mobj.
} reg_mobj_t;
typedef struct mobjhash_s {
reg_mobj_t* first, *last;
} mobjhash_t;
/**
* One cregister_t holds the state of the entire world.
*/
typedef struct cregister_s {
// The time the register was last updated.
int gametic;
// True if this register contains a read-only copy of the initial state
// of the world.
boolean isInitial;
// The mobjs are stored in a hash for efficiency (ID is the key).
mobjhash_t mobjs[REG_MOBJ_HASH_SIZE];
dt_player_t ddPlayers[DDMAXPLAYERS];
dt_sector_t* sectors;
dt_side_t* sides;
dt_poly_t* polyObjs;
} cregister_t;
void Sv_RegisterWorld(cregister_t* reg, boolean isInitial);
void Sv_NewDelta(void* deltaPtr, deltatype_t type, uint id);
boolean Sv_IsVoidDelta(const void* delta);
void Sv_PoolQueueClear(pool_t* pool);
void Sv_GenerateNewDeltas(cregister_t* reg, int clientNumber,
boolean doUpdate);
// The register contains the previous state of the world.
cregister_t worldRegister;
// The initial register is used when generating deltas for a new client.
cregister_t initialRegister;
// Each client has its own pool for deltas.
pool_t pools[DDMAXPLAYERS];
static float deltaBaseScores[NUM_DELTA_TYPES];
// Keep this zeroed out. Used if the register doesn't have data for
// the mobj being compared.
static dt_mobj_t dummyZeroMobj;
/**
* Called once for each map, from R_SetupMap(). Initialize the world
* register and drain all pools.
*/
void Sv_InitPools(void)
{
de::Time startedAt;
uint i;
// Clients don't register anything.
if(isClient) return;
LOG_AS("Sv_InitPools");
// Set base priority scores for all the delta types.
for(i = 0; i < NUM_DELTA_TYPES; ++i)
{
deltaBaseScores[i] = DEFAULT_DELTA_BASE_SCORE;
}
// Priorities for all deltas that will be sent out by the server.
// No priorities need to be declared for obsolete delta types.
deltaBaseScores[DT_MOBJ] = 1000;
deltaBaseScores[DT_PLAYER] = 1000;
deltaBaseScores[DT_SECTOR] = 2000;
deltaBaseScores[DT_SIDE] = 800;
deltaBaseScores[DT_POLY] = 2000;
deltaBaseScores[DT_LUMP] = 0;
deltaBaseScores[DT_SOUND] = 2000;
deltaBaseScores[DT_MOBJ_SOUND] = 3000;
deltaBaseScores[DT_SECTOR_SOUND] = 5000;
deltaBaseScores[DT_SIDE_SOUND] = 5500;
deltaBaseScores[DT_POLY_SOUND] = 5000;
// Since the map has changed, PU_MAP memory has been freed.
// Reset all pools (set numbers are kept, though).
for(i = 0; i < DDMAXPLAYERS; ++i)
{
pools[i].owner = i;
pools[i].resendDealer = 1;
de::zap(pools[i].hash);
de::zap(pools[i].misHash);
pools[i].queueSize = 0;
pools[i].allocatedSize = 0;
pools[i].queue = NULL;
// This will be set to false when a frame is sent.
pools[i].isFirst = true;
}
// Store the current state of the world into both the registers.
Sv_RegisterWorld(&worldRegister, false);
Sv_RegisterWorld(&initialRegister, true);
// How much time did we spend?
LOG_MAP_VERBOSE("World registered in %.2f seconds") << startedAt.since();
}
/**
* Called during server shutdown (when shutting down the engine).
*/
void Sv_ShutdownPools(void)
{
// Nothing to do.
}
/**
* Called when a client joins the game.
*/
void Sv_InitPoolForClient(uint clientNumber)
{
// Free everything that might exist in the pool.
Sv_DrainPool(clientNumber);
// Generate deltas by comparing against the initial state of the world.
// The initial register remains unmodified.
Sv_GenerateNewDeltas(&initialRegister, clientNumber, false);
// No frames have yet been sent for this client.
// The first frame is processed a bit more thoroughly than the others
// (e.g. *all* sides are compared, not just a portion).
pools[clientNumber].isFirst = true;
}
/**
* @return Pointer to the console's delta pool.
*/
pool_t* Sv_GetPool(uint consoleNumber)
{
return &pools[consoleNumber];
}
/**
* The hash function for the register mobj hash.
*/
uint Sv_RegisterHashFunction(thid_t id)
{
return (uint) id & REG_MOBJ_HASH_FUNCTION_MASK;
}
/**
* @return Pointer to the register-mobj, if it already exists.
*/
reg_mobj_t* Sv_RegisterFindMobj(cregister_t* reg, thid_t id)
{
mobjhash_t* hash = ®->mobjs[Sv_RegisterHashFunction(id)];
reg_mobj_t* iter;
// See if there already is a register-mobj for this id.
for(iter = hash->first; iter; iter = iter->next)
{
// Is this the one?
if(iter->mo.thinker.id == id)
{
return iter;
}
}
return NULL;
}
/**
* Adds a new reg_mobj_t to the register's mobj hash.
*/
reg_mobj_t* Sv_RegisterAddMobj(cregister_t* reg, thid_t id)
{
mobjhash_t* hash = ®->mobjs[Sv_RegisterHashFunction(id)];
reg_mobj_t* newRegMo;
// Try to find an existing register-mobj.
if((newRegMo = Sv_RegisterFindMobj(reg, id)) != NULL)
{
return newRegMo;
}
// Allocate the new register-mobj.
newRegMo = (reg_mobj_t *) Z_Calloc(sizeof(reg_mobj_t), PU_MAP, 0);
// Link it to the end of the hash list.
if(hash->last)
{
hash->last->next = newRegMo;
newRegMo->prev = hash->last;
}
hash->last = newRegMo;
if(!hash->first)
{
hash->first = newRegMo;
}
return newRegMo;
}
/**
* Removes a reg_mobj_t from the register's mobj hash.
*/
void Sv_RegisterRemoveMobj(cregister_t* reg, reg_mobj_t* regMo)
{
mobjhash_t* hash =
®->mobjs[Sv_RegisterHashFunction(regMo->mo.thinker.id)];
// Update the first and last links.
if(hash->last == regMo)
{
hash->last = regMo->prev;
}
if(hash->first == regMo)
{
hash->first = regMo->next;
}
// Link out of the list.
if(regMo->next)
{
regMo->next->prev = regMo->prev;
}
if(regMo->prev)
{
regMo->prev->next = regMo->next;
}
// Destroy the register-mobj.
Z_Free(regMo);
}
/**
* @return If the mobj is on the floor; @c MININT.
* If the mobj is touching the ceiling; @c MAXINT.
* Otherwise returns the Z coordinate.
*/
float Sv_GetMaxedMobjZ(const mobj_t* mo)
{
// No maxing for now.
/*
if(mo->origin[VZ] == mo->floorZ)
{
return DDMINFLOAT;
}
if(mo->origin[VZ] + mo->height == mo->ceilingZ)
{
return DDMAXFLOAT;
}
*/
return mo->origin[VZ];
}
/**
* Store the state of the mobj into the register-mobj.
* Called at register init and after each delta generation cycle.
*/
void Sv_RegisterMobj(dt_mobj_t *reg, mobj_t const *mo)
{
// (dt_mobj_t <=> mobj_t)
// Just copy the data we need.
reg->thinker.id = mo->thinker.id;
reg->type = mo->type;
reg->dPlayer = mo->dPlayer;
reg->_bspLeaf = mo->_bspLeaf;
reg->origin[VX] = mo->origin[VX];
reg->origin[VY] = mo->origin[VY];
reg->origin[VZ] = Sv_GetMaxedMobjZ(mo);
reg->floorZ = mo->floorZ;
reg->ceilingZ = mo->ceilingZ;
reg->mom[MX] = mo->mom[MX];
reg->mom[MY] = mo->mom[MY];
reg->mom[MZ] = mo->mom[MZ];
reg->angle = mo->angle;
reg->selector = mo->selector;
reg->state = mo->state;
reg->radius = mo->radius;
reg->height = mo->height;
reg->ddFlags = mo->ddFlags;
reg->flags = mo->flags;
reg->flags2 = mo->flags2;
reg->flags3 = mo->flags3;
reg->health = mo->health;
reg->floorClip = mo->floorClip;
reg->translucency = mo->translucency;
reg->visTarget = mo->visTarget;
}
/**
* Reset the data of the registered mobj to reasonable defaults.
* In effect, forces a resend of the zeroed entries as deltas.
*/
void Sv_RegisterResetMobj(dt_mobj_t* reg)
{
reg->origin[VX] = DDMINFLOAT;
reg->origin[VY] = DDMINFLOAT;
reg->origin[VZ] = -1000000;
reg->angle = 0;
reg->type = -1;
reg->selector = 0;
reg->state = 0;
reg->radius = -1;
reg->height = -1;
reg->ddFlags = 0;
reg->flags = 0;
reg->flags2 = 0;
reg->flags3 = 0;
reg->health = 0;
reg->floorClip = 0;
reg->translucency = 0;
reg->visTarget = 0;
}
/**
* Store the state of the player into the register-player.
* Called at register init and after each delta generation cycle.
*/
void Sv_RegisterPlayer(dt_player_t* reg, uint number)
{
#define FMAKERGBA(r,g,b,a) ( (byte)(0xff*r) + ((byte)(0xff*g)<<8) + ((byte)(0xff*b)<<16) + ((byte)(0xff*a)<<24) )
player_t* plr = &ddPlayers[number];
ddplayer_t* ddpl = &plr->shared;
//client_t* c = &clients[number];
reg->mobj = (ddpl->mo ? ddpl->mo->thinker.id : 0);
reg->forwardMove = 0;
reg->sideMove = 0;
reg->angle = (ddpl->mo ? ddpl->mo->angle : 0);
reg->turnDelta = (ddpl->mo ? ddpl->mo->angle - ddpl->lastAngle : 0);
reg->friction = ddpl->mo &&
(gx.MobjFriction ? gx.MobjFriction(ddpl->mo) : DEFAULT_FRICTION);
reg->extraLight = ddpl->extraLight;
reg->fixedColorMap = ddpl->fixedColorMap;
if(ddpl->flags & DDPF_VIEW_FILTER)
{
reg->filter = FMAKERGBA(ddpl->filterColor[CR],
ddpl->filterColor[CG],
ddpl->filterColor[CB],
ddpl->filterColor[CA]);
}
else
{
reg->filter = 0;
}
reg->clYaw = (ddpl->mo ? ddpl->mo->angle : 0);
reg->clPitch = ddpl->lookDir;
memcpy(reg->psp, ddpl->pSprites, sizeof(ddpsprite_t) * 2);
#undef FMAKERGBA
}
/**
* Store the state of the sector into the register-sector.
* Called at register init and after each delta generation.
*
* @param reg The sector register to be initialized.
* @param number The world sector number to be registered.
*/
void Sv_RegisterSector(dt_sector_t *reg, int number)
{
Sector *sector = App_World().map().sectors().at(number);
reg->lightLevel = sector->lightLevel();
for(int i = 0; i < 3; ++i)
reg->rgb[i] = sector->lightColor()[i];
// @todo $nplanes
for(int i = 0; i < 2; ++i) // number of planes in sector.
{
Plane const &plane = sector->plane(i);
// Plane properties
reg->planes[i].height = plane.height();
reg->planes[i].target = plane.targetHeight();
reg->planes[i].speed = plane.speed();
// Surface properties.
Surface const &surface = plane.surface();
Vector3f const &tintColor = surface.tintColor();
for(int c = 0; c < 3; ++c)
{
reg->planes[i].surface.rgba[c] = tintColor[c];
}
reg->planes[i].surface.rgba[CA] = surface.opacity();
reg->planes[i].surface.material = surface.materialPtr();
}
}
/**
* Store the state of the side into the register-side.
* Called at register init and after each delta generation.
*/
void Sv_RegisterSide(dt_side_t *reg, int number)
{
DENG2_ASSERT(reg != 0);
LineSide *side = App_World().map().sideByIndex(number);
if(side->hasSections())
{
reg->top.material = side->top().materialPtr();
reg->middle.material = side->middle().materialPtr();
reg->bottom.material = side->bottom().materialPtr();
for(int c = 0; c < 3; ++c)
{
reg->middle.rgba[c] = side->middle().tintColor()[c];
reg->bottom.rgba[c] = side->bottom().tintColor()[c];
reg->top.rgba[c] = side->top().tintColor()[c];
}
// Only middle sections support blending.
reg->middle.rgba[CA] = side->middle().opacity();
reg->middle.blendMode = side->middle().blendMode();
}
reg->lineFlags = side->line().flags() & 0xff;
reg->flags = side->flags() & 0xff;
}
/**
* Store the state of the polyobj into the register-poly.
* Called at register init and after each delta generation.
*/
void Sv_RegisterPoly(dt_poly_t *reg, uint number)
{
DENG_ASSERT(reg != 0);
Polyobj *poly = App_World().map().polyobjs().at(number);
reg->dest[VX] = poly->dest[VX];
reg->dest[VY] = poly->dest[VY];
reg->speed = poly->speed;
reg->destAngle = poly->destAngle;
reg->angleSpeed = poly->angleSpeed;
}
/**
* @return @c true if the result is not void.
*/
boolean Sv_RegisterCompareMobj(cregister_t *reg, mobj_t const *s, mobjdelta_t *d)
{
int df;
reg_mobj_t *regMo = 0;
dt_mobj_t const *r = &dummyZeroMobj;
if((regMo = Sv_RegisterFindMobj(reg, s->thinker.id)) != NULL)
{
// Use the registered data.
r = ®Mo->mo;
df = 0;
}
else
{
// This didn't exist in the register, so it's a new mobj.
df = MDFC_CREATE | MDF_EVERYTHING | MDFC_TYPE;
}
if(r->origin[VX] != s->origin[VX])
df |= MDF_ORIGIN_X;
if(r->origin[VY] != s->origin[VY])
df |= MDF_ORIGIN_Y;
if(r->origin[VZ] != Sv_GetMaxedMobjZ(s) || r->floorZ != s->floorZ || r->ceilingZ != s->ceilingZ)
{
df |= MDF_ORIGIN_Z;
if(!(df & MDFC_CREATE) && s->origin[VZ] <= s->floorZ)
{
// It is currently on the floor. The client will place it on its
// clientside floor and disregard the Z coordinate.
df |= MDFC_ON_FLOOR;
}
}
if(r->mom[MX] != s->mom[MX])
df |= MDF_MOM_X;
if(r->mom[MY] != s->mom[MY])
df |= MDF_MOM_Y;
if(r->mom[MZ] != s->mom[MZ])
df |= MDF_MOM_Z;
if(r->angle != s->angle)
df |= MDF_ANGLE;
if(r->selector != s->selector)
df |= MDF_SELECTOR;
if(r->translucency != s->translucency)
df |= MDFC_TRANSLUCENCY;
if(r->visTarget != s->visTarget)
df |= MDFC_FADETARGET;
if(r->type != s->type)
df |= MDFC_TYPE;
// Mobj state sent periodically, if the sequence keeps changing.
if(regMo && !Def_SameStateSequence(s->state, r->state))
{
df |= MDF_STATE;
if(s->state == NULL)
{
// No valid comparison can be generated because the mobj is gone.
return false;
}
}
if(r->radius != s->radius)
df |= MDF_RADIUS;
if(r->height != s->height)
df |= MDF_HEIGHT;
if((r->ddFlags & DDMF_PACK_MASK) != (s->ddFlags & DDMF_PACK_MASK) ||
r->flags != s->flags || r->flags2 != s->flags2 || r->flags3 != s->flags3)
{
df |= MDF_FLAGS;
}
if(r->health != s->health)
df |= MDF_HEALTH;
if(r->floorClip != s->floorClip)
df |= MDF_FLOORCLIP;
if(df)
{
// Init the delta with current data.
Sv_NewDelta(d, DT_MOBJ, s->thinker.id);
Sv_RegisterMobj(&d->mo, s);
}
d->delta.flags = df;
return !Sv_IsVoidDelta(d);
}
/**
* @return @c true, if the result is not void.
*/
boolean Sv_RegisterComparePlayer(cregister_t* reg, uint number,
playerdelta_t* d)
{
const dt_player_t* r = ®->ddPlayers[number];
dt_player_t* s = &d->player;
int df = 0;
// Init the delta with current data.
Sv_NewDelta(d, DT_PLAYER, number);
Sv_RegisterPlayer(&d->player, number);
// Determine which data is different.
if(r->mobj != s->mobj)
df |= PDF_MOBJ;
if(r->forwardMove != s->forwardMove)
df |= PDF_FORWARDMOVE;
if(r->sideMove != s->sideMove)
df |= PDF_SIDEMOVE;
if(r->turnDelta != s->turnDelta)
df |= PDF_TURNDELTA;
if(r->friction != s->friction)
df |= PDF_FRICTION;
if(r->extraLight != s->extraLight || r->fixedColorMap != s->fixedColorMap)
df |= PDF_EXTRALIGHT;
if(r->filter != s->filter)
df |= PDF_FILTER;
d->delta.flags = df;
return !Sv_IsVoidDelta(d);
}
/**
* @return @c true, if the result is not void.
*/
boolean Sv_RegisterCompareSector(cregister_t *reg, int number,
sectordelta_t *d, byte doUpdate)
{
dt_sector_t *r = ®->sectors[number];
Sector const *s = App_World().map().sectors().at(number);
int df = 0;
// Determine which data is different.
if(s->floorSurface().materialPtr() != r->planes[PLN_FLOOR].surface.material)
df |= SDF_FLOOR_MATERIAL;
if(s->ceilingSurface().materialPtr() != r->planes[PLN_CEILING].surface.material)
df |= SDF_CEILING_MATERIAL;
if(r->lightLevel != s->lightLevel())
df |= SDF_LIGHT;
if(r->rgb[0] != s->lightColor().x)
df |= SDF_COLOR_RED;
if(r->rgb[1] != s->lightColor().y)
df |= SDF_COLOR_GREEN;
if(r->rgb[2] != s->lightColor().z)
df |= SDF_COLOR_BLUE;
if(r->planes[PLN_FLOOR].surface.rgba[0] != s->floorSurface().tintColor().x)
df |= SDF_FLOOR_COLOR_RED;
if(r->planes[PLN_FLOOR].surface.rgba[1] != s->floorSurface().tintColor().y)
df |= SDF_FLOOR_COLOR_GREEN;
if(r->planes[PLN_FLOOR].surface.rgba[2] != s->floorSurface().tintColor().z)
df |= SDF_FLOOR_COLOR_BLUE;
if(r->planes[PLN_CEILING].surface.rgba[0] != s->ceilingSurface().tintColor().x)
df |= SDF_CEIL_COLOR_RED;
if(r->planes[PLN_CEILING].surface.rgba[1] != s->ceilingSurface().tintColor().y)
df |= SDF_CEIL_COLOR_GREEN;
if(r->planes[PLN_CEILING].surface.rgba[2] != s->ceilingSurface().tintColor().z)
df |= SDF_CEIL_COLOR_BLUE;
// The cases where an immediate change to a plane's height is needed:
// 1) Plane is not moving, but the heights are different. This means
// the plane's height was changed unpredictably.
// 2) Plane is moving, but there is a large difference in the heights.
// The clientside height should be fixed.
// Should we make an immediate change in floor height?
if(FEQUAL(r->planes[PLN_FLOOR].speed, 0) && FEQUAL(s->floor().speed(), 0))
{
if(!FEQUAL(r->planes[PLN_FLOOR].height, s->floor().height()))
df |= SDF_FLOOR_HEIGHT;
}
else
{
if(fabs(r->planes[PLN_FLOOR].height - s->floor().height()) > PLANE_SKIP_LIMIT)
df |= SDF_FLOOR_HEIGHT;
}
// How about the ceiling?
if(FEQUAL(r->planes[PLN_CEILING].speed, 0) && FEQUAL(s->ceiling().speed(), 0))
{
if(!FEQUAL(r->planes[PLN_CEILING].height, s->ceiling().height()))
df |= SDF_CEILING_HEIGHT;
}
else
{
if(fabs(r->planes[PLN_CEILING].height - s->ceiling().height()) > PLANE_SKIP_LIMIT)
df |= SDF_CEILING_HEIGHT;
}
// Check planes, too.
if(!FEQUAL(r->planes[PLN_FLOOR].target, s->floor().targetHeight()))
{
// Target and speed are always sent together.
df |= SDF_FLOOR_TARGET | SDF_FLOOR_SPEED;
}
if(!FEQUAL(r->planes[PLN_FLOOR].speed, s->floor().speed()))
{
// Target and speed are always sent together.
df |= SDF_FLOOR_SPEED | SDF_FLOOR_TARGET;
}
if(!FEQUAL(r->planes[PLN_CEILING].target, s->ceiling().targetHeight()))
{
// Target and speed are always sent together.
df |= SDF_CEILING_TARGET | SDF_CEILING_SPEED;
}
if(!FEQUAL(r->planes[PLN_CEILING].speed, s->ceiling().speed()))
{
// Target and speed are always sent together.
df |= SDF_CEILING_SPEED | SDF_CEILING_TARGET;
}
#ifdef _DEBUG
if(df & (SDF_CEILING_HEIGHT | SDF_CEILING_SPEED | SDF_CEILING_TARGET))
{
LOGDEV_NET_XVERBOSE("Sector %i: ceiling state change noted (target = %f)")
<< number << s->ceiling().targetHeight();
}
#endif
// Only do a delta when something changes.
if(df)
{
// Init the delta with current data.
Sv_NewDelta(d, DT_SECTOR, number);
Sv_RegisterSector(&d->sector, number);
if(doUpdate)
{
Sv_RegisterSector(r, number);
}
}
if(doUpdate)
{
// The plane heights should be tracked regardless of the
// change flags.
r->planes[PLN_FLOOR].height = s->floor().height();
r->planes[PLN_CEILING].height = s->ceiling().height();
}
d->delta.flags = df;
return !Sv_IsVoidDelta(d);
}
/**
* @return @c true= the result is not void.
*/
boolean Sv_RegisterCompareSide(cregister_t *reg, uint number,
sidedelta_t *d, byte doUpdate)
{
LineSide const *side = App_World().map().sideByIndex(number);
dt_side_t *r = ®->sides[number];
byte lineFlags = side->line().flags() & 0xff;
byte sideFlags = side->flags() & 0xff;
int df = 0;
if(side->hasSections())
{
if(!side->top().hasFixMaterial() && r->top.material != side->top().materialPtr())
{
df |= SIDF_TOP_MATERIAL;
if(doUpdate)
r->top.material = side->top().materialPtr();
}
if(!side->middle().hasFixMaterial() && r->middle.material != side->middle().materialPtr())
{
df |= SIDF_MID_MATERIAL;
if(doUpdate)
r->middle.material = side->middle().materialPtr();
}
if(!side->bottom().hasFixMaterial() && r->bottom.material != side->bottom().materialPtr())
{
df |= SIDF_BOTTOM_MATERIAL;
if(doUpdate)
r->bottom.material = side->bottom().materialPtr();
}
if(r->top.rgba[0] != side->top().tintColor().x)
{
df |= SIDF_TOP_COLOR_RED;
if(doUpdate)
r->top.rgba[0] = side->top().tintColor().x;
}
if(r->top.rgba[1] != side->top().tintColor().y)
{
df |= SIDF_TOP_COLOR_GREEN;
if(doUpdate)
r->top.rgba[1] = side->top().tintColor().y;
}
if(r->top.rgba[2] != side->top().tintColor().z)
{
df |= SIDF_TOP_COLOR_BLUE;
if(doUpdate)
r->top.rgba[3] = side->top().tintColor().z;
}
if(r->middle.rgba[0] != side->middle().tintColor().x)
{
df |= SIDF_MID_COLOR_RED;
if(doUpdate)
r->middle.rgba[0] = side->middle().tintColor().x;
}
if(r->middle.rgba[1] != side->middle().tintColor().y)
{
df |= SIDF_MID_COLOR_GREEN;
if(doUpdate)
r->middle.rgba[1] = side->middle().tintColor().y;
}
if(r->middle.rgba[2] != side->middle().tintColor().z)
{
df |= SIDF_MID_COLOR_BLUE;
if(doUpdate)
r->middle.rgba[3] = side->middle().tintColor().z;
}
if(r->middle.rgba[3] != side->middle().opacity())
{
df |= SIDF_MID_COLOR_ALPHA;
if(doUpdate)
r->middle.rgba[3] = side->middle().opacity();
}
if(r->bottom.rgba[0] != side->bottom().tintColor().x)
{
df |= SIDF_BOTTOM_COLOR_RED;
if(doUpdate)
r->bottom.rgba[0] = side->bottom().tintColor().x;
}
if(r->bottom.rgba[1] != side->bottom().tintColor().y)
{
df |= SIDF_BOTTOM_COLOR_GREEN;
if(doUpdate)
r->bottom.rgba[1] = side->bottom().tintColor().y;
}
if(r->bottom.rgba[2] != side->bottom().tintColor().z)
{
df |= SIDF_BOTTOM_COLOR_BLUE;
if(doUpdate)
r->bottom.rgba[3] = side->bottom().tintColor().z;
}
if(r->middle.blendMode != side->middle().blendMode())
{
df |= SIDF_MID_BLENDMODE;
if(doUpdate)
r->middle.blendMode = side->middle().blendMode();
}
}
if(r->lineFlags != lineFlags)
{
df |= SIDF_LINE_FLAGS;
if(doUpdate)
r->lineFlags = lineFlags;
}
if(r->flags != sideFlags)
{
df |= SIDF_FLAGS;
if(doUpdate)
r->flags = sideFlags;
}
// Was there any change?
if(df)
{
// This happens quite rarely.
// Init the delta with current data.
Sv_NewDelta(d, DT_SIDE, number);
Sv_RegisterSide(&d->side, number);
}
d->delta.flags = df;
return !Sv_IsVoidDelta(d);
}
/**
* @return @c true, if the result is not void.
*/
boolean Sv_RegisterComparePoly(cregister_t* reg, int number,
polydelta_t *d)
{
const dt_poly_t* r = ®->polyObjs[number];
dt_poly_t* s = &d->po;
int df = 0;
// Init the delta with current data.
Sv_NewDelta(d, DT_POLY, number);
Sv_RegisterPoly(&d->po, number);
// What is different?
if(r->dest[VX] != s->dest[VX])
df |= PODF_DEST_X;
if(r->dest[VY] != s->dest[VY])
df |= PODF_DEST_Y;
if(r->speed != s->speed)
df |= PODF_SPEED;
if(r->destAngle != s->destAngle)
df |= PODF_DEST_ANGLE;
if(r->angleSpeed != s->angleSpeed)
df |= PODF_ANGSPEED;
d->delta.flags = df;
return !Sv_IsVoidDelta(d);
}
/**
* @return @c true, if the mobj can be excluded from delta
* processing.
*/
boolean Sv_IsMobjIgnored(mobj_t* mo)
{
return (mo->ddFlags & DDMF_LOCAL) != 0;
}
/**
* @return @c true, if the player can be excluded from delta
* processing.
*/
boolean Sv_IsPlayerIgnored(int plrNum)
{
return !ddPlayers[plrNum].shared.inGame;
}
/**
* Initialize the register with the current state of the world.
* The arrays are allocated and the data is copied, nothing else is done.
*
* An initial register doesn't contain any mobjs. When new clients enter,
* they know nothing about any mobjs. If the mobjs were included in the
* initial register, clients wouldn't receive much info from mobjs that
* haven't moved since the beginning.
*/
void Sv_RegisterWorld(cregister_t *reg, boolean isInitial)
{
DENG_ASSERT(reg != 0);
Map &map = App_World().map();
de::zapPtr(reg);
reg->gametic = SECONDS_TO_TICKS(gameTime);
// Is this the initial state?
reg->isInitial = isInitial;
// Init sectors.
reg->sectors = (dt_sector_t *) Z_Calloc(sizeof(*reg->sectors) * map.sectorCount(), PU_MAP, 0);
for(int i = 0; i < map.sectorCount(); ++i)
{
Sv_RegisterSector(®->sectors[i], i);
}
// Init sides.
reg->sides = (dt_side_t *) Z_Calloc(sizeof(*reg->sides) * map.sideCount(), PU_MAP, 0);
for(int i = 0; i < map.sideCount(); ++i)
{
Sv_RegisterSide(®->sides[i], i);
}
// Init polyobjs.
int numPolyobjs = map.polyobjCount();
if(numPolyobjs)
{
reg->polyObjs = (dt_poly_t *) Z_Calloc(sizeof(*reg->polyObjs) * map.polyobjCount(), PU_MAP, 0);
for(int i = 0; i < numPolyobjs; ++i)
{
Sv_RegisterPoly(®->polyObjs[i], i);
}
}
else
{
reg->polyObjs = NULL;
}
}
/**
* Update the pool owner's info.
*/
void Sv_UpdateOwnerInfo(pool_t *pool)
{
player_t *plr = &ddPlayers[pool->owner];
ownerinfo_t *info = &pool->ownerInfo;
de::zapPtr(info);
// Pointer to the owner's pool.
info->pool = pool;
if(plr->shared.mo)