a83260b1049c7dfcabcb04926e9b6c817fe1dd23
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / controlpoint.qc
1 #include "controlpoint.qh"
2 #include "gibs.qh"
3 #include "teamradar.qh"
4 #include "../common/movetypes/movetypes.qh"
5
6 .float alpha;
7 .int count;
8 .float pain_finished;
9
10 .bool iscaptured;
11
12 .vector cp_origin, cp_bob_origin;
13 .float cp_bob_spd;
14
15 .vector cp_bob_dmg;
16
17 .vector punchangle;
18
19 .float max_health;
20
21 .entity icon_realmodel;
22
23 void cpicon_draw()
24 {SELFPARAM();
25         if(time < self.move_time) { return; }
26
27         if(self.cp_bob_dmg_z > 0)
28                 self.cp_bob_dmg_z = self.cp_bob_dmg_z - 3 * frametime;
29         else
30                 self.cp_bob_dmg_z = 0;
31         self.cp_bob_origin_z = 4 * PI * (1 - cos(self.cp_bob_spd));
32         self.cp_bob_spd = self.cp_bob_spd + 1.875 * frametime;
33         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
34
35         if(!self.iscaptured) self.alpha = self.health / self.max_health;
36
37         if(self.iscaptured)
38         {
39                 if (self.punchangle_x > 0)
40                 {
41                         self.punchangle_x = self.punchangle_x - 60 * frametime;
42                         if (self.punchangle_x < 0)
43                                 self.punchangle_x = 0;
44                 }
45                 else if (self.punchangle_x < 0)
46                 {
47                         self.punchangle_x = self.punchangle_x + 60 * frametime;
48                         if (self.punchangle_x > 0)
49                                 self.punchangle_x = 0;
50                 }
51
52                 if (self.punchangle_y > 0)
53                 {
54                         self.punchangle_y = self.punchangle_y - 60 * frametime;
55                         if (self.punchangle_y < 0)
56                                 self.punchangle_y = 0;
57                 }
58                 else if (self.punchangle_y < 0)
59                 {
60                         self.punchangle_y = self.punchangle_y + 60 * frametime;
61                         if (self.punchangle_y > 0)
62                                 self.punchangle_y = 0;
63                 }
64
65                 if (self.punchangle_z > 0)
66                 {
67                         self.punchangle_z = self.punchangle_z - 60 * frametime;
68                         if (self.punchangle_z < 0)
69                                 self.punchangle_z = 0;
70                 }
71                 else if (self.punchangle_z < 0)
72                 {
73                         self.punchangle_z = self.punchangle_z + 60 * frametime;
74                         if (self.punchangle_z > 0)
75                                 self.punchangle_z = 0;
76                 }
77
78                 self.angles_x = self.punchangle_x;
79                 self.angles_y = self.punchangle_y + self.move_angles_y;
80                 self.angles_z = self.punchangle_z;
81                 self.move_angles_y = self.move_angles_y + 45 * frametime;
82         }
83
84         setorigin(self, self.cp_origin + self.cp_bob_origin + self.cp_bob_dmg);
85 }
86
87 void cpicon_damage(float hp)
88 {SELFPARAM();
89         if(!self.iscaptured) { return; }
90
91         if(hp < self.max_health * 0.25)
92                 setmodel(self, MDL_ONS_CP3);
93         else if(hp < self.max_health * 0.50)
94                 setmodel(self, MDL_ONS_CP2);
95         else if(hp < self.max_health * 0.75)
96                 setmodel(self, MDL_ONS_CP1);
97         else if(hp <= self.max_health || hp >= self.max_health)
98                 setmodel(self, MDL_ONS_CP);
99
100         self.punchangle = (2 * randomvec() - '1 1 1') * 45;
101
102         self.cp_bob_dmg_z = (2 * random() - 1) * 15;
103         self.pain_finished = time + 1;
104         self.colormod = '2 2 2';
105
106         setsize(self, CPICON_MIN, CPICON_MAX);
107 }
108
109 void cpicon_construct()
110 {SELFPARAM();
111         self.netname = "Control Point Icon";
112
113         setmodel(self, MDL_ONS_CP);
114         setsize(self, CPICON_MIN, CPICON_MAX);
115
116         if(self.icon_realmodel == world)
117         {
118                 self.icon_realmodel = spawn();
119                 setmodel(self.icon_realmodel, MDL_Null);
120                 setorigin(self.icon_realmodel, self.origin);
121                 setsize(self.icon_realmodel, CPICON_MIN, CPICON_MAX);
122                 self.icon_realmodel.movetype = MOVETYPE_NOCLIP;
123                 self.icon_realmodel.solid = SOLID_NOT;
124                 self.icon_realmodel.move_origin = self.icon_realmodel.origin;
125         }
126
127         if(self.iscaptured) { self.icon_realmodel.solid = SOLID_BBOX; }
128
129         self.move_movetype      = MOVETYPE_NOCLIP;
130         self.solid                      = SOLID_NOT;
131         self.movetype           = MOVETYPE_NOCLIP;
132         self.move_origin        = self.origin;
133         self.move_time          = time;
134         self.drawmask           = MASK_NORMAL;
135         self.alpha                      = 1;
136         self.draw                       = cpicon_draw;
137         self.cp_origin          = self.origin;
138         self.cp_bob_origin      = '0 0 0.1';
139         self.cp_bob_spd         = 0;
140 }
141
142 .vector glowmod;
143 void cpicon_changeteam()
144 {SELFPARAM();
145         if(self.team)
146         {
147                 self.glowmod = Team_ColorRGB(self.team - 1);
148                 self.teamradar_color = Team_ColorRGB(self.team - 1);
149                 self.colormap = 1024 + (self.team - 1) * 17;
150         }
151         else
152         {
153                 self.colormap = 1024;
154                 self.glowmod = '1 1 0';
155                 self.teamradar_color = '1 1 0';
156         }
157 }
158
159 void ent_cpicon()
160 {SELFPARAM();
161         int sf = ReadByte();
162
163         if(sf & CPSF_SETUP)
164         {
165                 self.origin_x = ReadCoord();
166                 self.origin_y = ReadCoord();
167                 self.origin_z = ReadCoord();
168                 setorigin(self, self.origin);
169
170                 self.health = ReadByte();
171                 self.max_health = ReadByte();
172                 self.count = ReadByte();
173                 self.team = ReadByte();
174                 self.iscaptured = ReadByte();
175
176                 if(!self.count)
177                         self.count = (self.health - self.max_health) * frametime;
178
179                 cpicon_changeteam();
180                 cpicon_construct();
181         }
182
183         if(sf & CPSF_STATUS)
184         {
185                 int _tmp = ReadByte();
186                 if(_tmp != self.team)
187                 {
188                         self.team = _tmp;
189                         cpicon_changeteam();
190                 }
191
192                 _tmp = ReadByte();
193
194                 if(_tmp != self.health)
195                         cpicon_damage(_tmp);
196
197                 self.health = _tmp;
198         }
199 }