ent_cs: cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / ent_cs.qc
1 #include "ent_cs.qh"
2 #include "_all.qh"
3
4 float entcs_customize()
5 {
6         SELFPARAM();
7         entity o = self.owner;
8         if(o.deadflag != DEAD_NO)
9                 return false;
10         if (!IS_PLAYER(o))
11                 return false;
12         if(other == o)
13                 return false;
14         if((IS_PLAYER(other)) || other.caplayer)
15                 if(!teamplay || o.team != other.team)
16                         if (!radar_showennemies)
17                                 return false;
18         return true;
19 }
20
21 bool entcs_send(entity this, entity to, int sf)
22 {
23         WriteByte(MSG_ENTITY, ENT_CLIENT_ENTCS);
24         WriteByte(MSG_ENTITY, sf);
25         if(sf & BIT(0))
26                 WriteByte(MSG_ENTITY, num_for_edict(self.owner) - 1);
27         if(sf & BIT(1))
28         {
29                 WriteShort(MSG_ENTITY, self.origin.x);
30                 WriteShort(MSG_ENTITY, self.origin.y);
31                 WriteShort(MSG_ENTITY, self.origin.z);
32         }
33         if(sf & BIT(2))
34                 WriteByte(MSG_ENTITY, self.angles.y * 256.0 / 360);
35         if(sf & BIT(3))
36                 WriteByte(MSG_ENTITY, self.health / 10); // FIXME use a better scale?
37         if(sf & BIT(4))
38                 WriteByte(MSG_ENTITY, self.armorvalue / 10); // FIXME use a better scale?
39         return true;
40 }
41
42 void entcs_think()
43 {
44         SELFPARAM();
45         self.nextthink = time + 0.033333333333; // increase this to like 0.15 once the client can do smoothing
46         entity o = self.owner;
47         if (o.origin != self.origin)
48         {
49                 setorigin(self, o.origin);
50                 self.SendFlags |= BIT(1);
51         }
52         if (o.angles.y != self.angles.y)
53         {
54                 self.angles = o.angles;
55                 self.SendFlags |= BIT(2);
56         }
57         if (o.health != self.health)
58         {
59                 self.health = o.health;
60                 self.SendFlags |= BIT(3);
61         }
62         if (o.armorvalue != self.armorvalue)
63         {
64                 self.armorvalue = o.armorvalue;
65                 self.SendFlags |= BIT(4);
66         }
67 }
68
69 entity attach_entcs(entity e)
70 {
71         entity ent = e.entcs = new(entcs_sender);
72         ent.owner = e;
73         ent.think = entcs_think;
74         ent.nextthink = time;
75
76         Net_LinkEntity(ent, false, 0, entcs_send);
77         ent.customizeentityforclient = entcs_customize;
78
79         return ent;
80 }
81
82 void detach_entcs(entity e)
83 {
84         if (!e.entcs) return;
85         remove(e.entcs);
86         e.entcs = NULL;
87 }