]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/ent_cs.qc
using the clientinfo entities for the ping times
[divverent/nexuiz.git] / data / qcsrc / server / ent_cs.qc
1 /**
2  * The point of these entities is to avoid the problems
3  * with clientprediction.
4  * If you add SendEntity to players, the engine will not
5  * do any prediction anymore, and you'd have to write the whole
6  * prediction code in CSQC, you want that? :P
7  * Data can depend on gamemode. For now, it serves as GPS entities
8  * in onslaught... YAY ;)
9  */
10
11 // Beware: do not redefine those in other files
12 // and NO, you cannot use ".version", which already exists (at least
13 // it did when I added this) But you have to use .Version
14 // Capital V
15
16 .float(entity to) SendEntity;
17 .float Version;
18
19 .float pingtime;
20
21 entity entcs_start;
22
23 void entcs_init()
24 {
25         print("Initializing ClientSide information entities\n");
26         entcs_start = spawn();
27         entcs_start.solid = SOLID_NOT;
28         entcs_start.chain = world;
29 };
30
31 entity get_entcs_ent(float num)
32 {
33         entity entcs;
34         entcs = spawn();
35         entcs.chain = entcs_start.chain;
36         entcs_start.chain = entcs;
37         return entcs;
38 };
39
40 void entcs_ons(entity to)
41 {
42         if(to == self.owner || self.team != to.team)
43                 return;
44         if(self.owner.classname == "observer" || to.classname == "observer")
45                 return;
46         WriteByte(MSG_ENTITY, ENTCS_MSG_ONS);
47         WriteCoord(MSG_ENTITY, self.owner.origin_x);
48         WriteCoord(MSG_ENTITY, self.owner.origin_y);
49         WriteCoord(MSG_ENTITY, self.owner.angles_y);
50 }
51
52 void entcs_common_self()
53 {
54         entity pl;
55         if(self.pingtime < time)
56         {
57                 self.pingtime = time + 9; // keep it 1 below the non-ons update intervall
58                 // just to be safe... (blah)
59                 WriteByte(MSG_ENTITY, ENTCS_MSG_PING);
60                 FOR_EACH_REALCLIENT(pl)
61                 {
62                         WriteByte(MSG_ENTITY, num_for_edict(pl));
63                         WriteShort(MSG_ENTITY, pl.ping);
64                 }
65                 WriteByte(MSG_ENTITY, 0);
66         }
67 }
68
69 float entcs_send(entity to)
70 {
71         WriteByte(MSG_ENTITY, ENT_CLIENT_ENTCS);
72         WriteByte(MSG_ENTITY, self.health); // serves as entitynumber
73         
74         if(to == self.owner)
75         {
76                 entcs_common_self();
77         }
78         
79         if(game == GAME_ONSLAUGHT)
80                 entcs_ons(to);
81         
82         WriteByte(MSG_ENTITY, ENTCS_MSG_END);
83         return TRUE;
84 };
85
86 void entcs_think()
87 {
88         self.team = self.owner.team;
89         self.Version++;
90         setorigin(self, self.owner.origin);
91         if(game == GAME_ONSLAUGHT)
92                 self.nextthink = time + 0.1;
93         else
94                 self.nextthink = time + 10; // update pings every 10 seconds
95 };
96
97 void attach_entcs()
98 {
99         local float num;
100         local entity ent;
101
102         print("Attaching ENTCS entity\n");
103
104         num = num_for_edict(self);
105         ent = get_entcs_ent(num);
106
107         ent.classname = "entcs_sender";
108         ent.health = num;
109         setorigin(ent, self.origin);
110         ent.owner = self;
111         ent.think = entcs_think;
112         ent.nextthink = time;
113         ent.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
114         ent.model = "entcs_sender";
115         ent.modelindex = 1;
116         setsize(ent, '0 0 0', '0 0 0');
117
118         ent.pingtime = time + 5;
119                 
120         ent.SendEntity = entcs_send;
121 };
122
123 void detach_entcs()
124 {
125         local float num;
126         local entity ent;
127         num = num_for_edict(self);
128         for(ent = entcs_start; ent.chain.owner != self && ent.chain != world; ent = ent.chain);
129         if(ent.chain != world && ent.chain.owner == self)
130         {
131                 remove(ent.chain);
132                 ent.chain = ent.chain.chain;
133         }
134 };