]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/mode_onslaught.qc
use InitializeEntity for onslaught too
[divverent/nexuiz.git] / data / qcsrc / server / mode_onslaught.qc
1 void onslaught_generator_updatesprite(entity e);
2 void onslaught_controlpoint_updatesprite(entity e);
3 void onslaught_link_checkupdate();
4
5 .entity sprite;
6 .string target2;
7 .float iscaptured;
8 .float islinked;
9 .float isgenneighbor_red;
10 .float isgenneighbor_blue;
11 .float iscpneighbor_red;
12 .float iscpneighbor_blue;
13 .float isshielded;
14 .float lasthealth;
15 .float lastteam;
16 .float lastshielded;
17
18 .string model1, model2, model3;
19
20 void onslaught_updatelinks()
21 {
22         local entity l, links;
23         local float stop, t1, t2, t3, t4;
24         // first check if the game has ended
25         dprint("--- updatelinks ---\n");
26         links = findchain(classname, "onslaught_link");
27         // mark generators as being shielded and networked
28         l = findchain(classname, "onslaught_generator");
29         while (l)
30         {
31                 if (l.iscaptured)
32                         dprint(etos(l), " (generator) belongs to team ", ftos(l.team), "\n");
33                 else
34                         dprint(etos(l), " (generator) is destroyed\n");
35                 l.islinked = l.iscaptured;
36                 l.isshielded = l.iscaptured;
37                 l = l.chain;
38         }
39         // mark points as shielded and not networked
40         l = findchain(classname, "onslaught_controlpoint");
41         while (l)
42         {
43                 l.islinked = FALSE;
44                 l.isshielded = TRUE;
45                 l.isgenneighbor_red = FALSE;
46                 l.isgenneighbor_blue = FALSE;
47                 l.iscpneighbor_red = FALSE;
48                 l.iscpneighbor_blue = FALSE;
49                 dprint(etos(l), " (point) belongs to team ", ftos(l.team), "\n");
50                 l = l.chain;
51         }
52         // flow power outward from the generators through the network
53         l = links;
54         while (l)
55         {
56                 dprint(etos(l), " (link) connects ", etos(l.goalentity), " with ", etos(l.enemy), "\n");
57                 l = l.chain;
58         }
59         stop = FALSE;
60         while (!stop)
61         {
62                 stop = TRUE;
63                 l = links;
64                 while (l)
65                 {
66                         // if both points are captured by the same team, and only one of
67                         // them is powered, mark the other one as powered as well
68                         if (l.enemy.iscaptured && l.goalentity.iscaptured)
69                         if (l.enemy.islinked != l.goalentity.islinked)
70                         if (l.enemy.team == l.goalentity.team)
71                         {
72                                 if (!l.goalentity.islinked)
73                                 {
74                                         stop = FALSE;
75                                         l.goalentity.islinked = TRUE;
76                                         dprint(etos(l), " (link) is marking ", etos(l.goalentity), " (point) because its team matches ", etos(l.enemy), " (point)\n");
77                                 }
78                                 else if (!l.enemy.islinked)
79                                 {
80                                         stop = FALSE;
81                                         l.enemy.islinked = TRUE;
82                                         dprint(etos(l), " (link) is marking ", etos(l.enemy), " (point) because its team matches ", etos(l.goalentity), " (point)\n");
83                                 }
84                         }
85                         l = l.chain;
86                 }
87         }
88         // now that we know which points are powered we can mark their neighbors
89         // as unshielded if team differs
90         l = links;
91         while (l)
92         {
93                 if (l.goalentity.team != l.enemy.team)
94                 {
95                         if (l.goalentity.islinked)
96                         {
97                                 dprint(etos(l), " (link) is unshielding ", etos(l.enemy), " (point) because its team does not match ", etos(l.goalentity), " (point)\n");
98                                 l.enemy.isshielded = FALSE;
99                                 if(l.goalentity.classname == "onslaught_generator")
100                                 {
101                                         if(l.goalentity.team == COLOR_TEAM1)
102                                                 l.enemy.isgenneighbor_red = TRUE;
103                                         else if(l.goalentity.team == COLOR_TEAM2)
104                                                 l.enemy.isgenneighbor_blue = TRUE;
105                                 }
106                                 else
107                                 {
108                                         if(l.goalentity.team == COLOR_TEAM1)
109                                                 l.enemy.iscpneighbor_red = TRUE;
110                                         else if(l.goalentity.team == COLOR_TEAM2)
111                                                 l.enemy.iscpneighbor_blue = TRUE;
112                                 }
113                         }
114                         if (l.enemy.islinked)
115                         {
116                                 dprint(etos(l), " (link) is unshielding ", etos(l.goalentity), " (point) because its team does not match ", etos(l.enemy), " (point)\n");
117                                 l.goalentity.isshielded = FALSE;
118                                 if(l.enemy.classname == "onslaught_generator")
119                                 {
120                                         if(l.enemy.team == COLOR_TEAM1)
121                                                 l.goalentity.isgenneighbor_red = TRUE;
122                                         else if(l.enemy.team == COLOR_TEAM2)
123                                                 l.goalentity.isgenneighbor_blue = TRUE;
124                                 }
125                                 else
126                                 {
127                                         if(l.enemy.team == COLOR_TEAM1)
128                                                 l.goalentity.iscpneighbor_red = TRUE;
129                                         else if(l.enemy.team == COLOR_TEAM2)
130                                                 l.goalentity.iscpneighbor_blue = TRUE;
131                                 }
132                         }
133                 }
134                 l = l.chain;
135         }
136         // now update the takedamage and alpha variables on generator shields
137         l = findchain(classname, "onslaught_generator");
138         while (l)
139         {
140                 if (l.isshielded)
141                 {
142                         dprint(etos(l), " (generator) is shielded\n");
143                         l.enemy.alpha = 1;
144                         l.takedamage = DAMAGE_NO;
145                         l.bot_attack = FALSE;
146                 }
147                 else
148                 {
149                         dprint(etos(l), " (generator) is not shielded\n");
150                         l.enemy.alpha = -1;
151                         l.takedamage = DAMAGE_AIM;
152                         l.bot_attack = TRUE;
153                 }
154                 l = l.chain;
155         }
156         // now update the takedamage and alpha variables on control point icons
157         l = findchain(classname, "onslaught_controlpoint");
158         while (l)
159         {
160                 if (l.isshielded)
161                 {
162                         dprint(etos(l), " (point) is shielded\n");
163                         l.enemy.alpha = 1;
164                         if (l.goalentity)
165                         {
166                                 l.goalentity.takedamage = DAMAGE_NO;
167                                 l.goalentity.bot_attack = FALSE;
168                         }
169                 }
170                 else
171                 {
172                         dprint(etos(l), " (point) is not shielded\n");
173                         l.enemy.alpha = -1;
174                         if (l.goalentity)
175                         {
176                                 l.goalentity.takedamage = DAMAGE_AIM;
177                                 l.goalentity.bot_attack = TRUE;
178                         }
179                 }
180                 onslaught_controlpoint_updatesprite(l);
181                 l = l.chain;
182         }
183         // count generators owned by each team
184         t1 = t2 = t3 = t4 = 0;
185         l = findchain(classname, "onslaught_generator");
186         while (l)
187         {
188                 if (l.iscaptured)
189                 {
190                         if (l.team == COLOR_TEAM1) t1 = 1;
191                         if (l.team == COLOR_TEAM2) t2 = 1;
192                         if (l.team == COLOR_TEAM3) t3 = 1;
193                         if (l.team == COLOR_TEAM4) t4 = 1;
194                 }
195                 onslaught_generator_updatesprite(l);
196                 l = l.chain;
197         }
198         // see if multiple teams remain (if not, it's game over)
199         if (t1 + t2 + t3 + t4 < 2)
200                 dprint("--- game over ---\n");
201         else
202                 dprint("--- done updating links ---\n");
203 };
204
205 float onslaught_controlpoint_can_be_linked(entity cp, float t)
206 {
207         if(t == COLOR_TEAM1)
208         {
209                 if(cp.isgenneighbor_red)
210                         return 2;
211                 if(cp.iscpneighbor_red)
212                         return 1;
213         }
214         else if(t == COLOR_TEAM2)
215         {
216                 if(cp.isgenneighbor_blue)
217                         return 2;
218                 if(cp.iscpneighbor_blue)
219                         return 1;
220         }
221         return 0;
222 /*
223         entity e;
224         // check to see if this player has a legitimate claim to capture this
225         // control point - more specifically that there is a captured path of
226         // points leading back to the team generator
227         e = findchain(classname, "onslaught_link");
228         while (e)
229         {
230                 if (e.goalentity == cp)
231                 {
232                         dprint(etos(e), " (link) connects to ", etos(e.enemy), " (point)");
233                         if (e.enemy.islinked)
234                         {
235                                 dprint(" which is linked");
236                                 if (e.enemy.team == t)
237                                 {
238                                         dprint(" and has the correct team!\n");
239                                         return 1;
240                                 }
241                                 else
242                                         dprint(" but has the wrong team\n");
243                         }
244                         else
245                                 dprint("\n");
246                 }
247                 else if (e.enemy == cp)
248                 {
249                         dprint(etos(e), " (link) connects to ", etos(e.goalentity), " (point)");
250                         if (e.goalentity.islinked)
251                         {
252                                 dprint(" which is linked");
253                                 if (e.goalentity.team == t)
254                                 {
255                                         dprint(" and has a team!\n");
256                                         return 1;
257                                 }
258                                 else
259                                         dprint(" but has the wrong team\n");
260                         }
261                         else
262                                 dprint("\n");
263                 }
264                 e = e.chain;
265         }
266         return 0;
267 */
268 }
269
270 float onslaught_controlpoint_attackable(entity cp, float t)
271 // -2: SAME TEAM, attackable by enemy!
272 // -1: SAME TEAM!
273 // 0:  off limits
274 // 1:  attack it
275 // 2:  touch it
276 // 3:  attack it (HIGH PRIO)
277 // 4:  touch it (HIGH PRIO)
278 {
279         float a;
280
281         if(cp.isshielded)
282         {
283                 return 0;
284         }
285         else if(cp.goalentity)
286         {
287                 // if there's already an icon built, nothing happens
288                 if(cp.team == t)
289                 {
290                         a = onslaught_controlpoint_can_be_linked(cp, COLOR_TEAM1 + COLOR_TEAM2 - t);
291                         if(a) // attackable by enemy?
292                                 return -2; // EMERGENCY!
293                         return -1;
294                 }
295                 // we know it can be linked, so no need to check
296                 // but...
297                 a = onslaught_controlpoint_can_be_linked(cp, t);
298                 if(a == 2) // near our generator?
299                         return 3; // EMERGENCY!
300                 return 1;
301         }
302         else
303         {
304                 // free point
305                 if(onslaught_controlpoint_can_be_linked(cp, t))
306                 {
307                         a = onslaught_controlpoint_can_be_linked(cp, COLOR_TEAM1 + COLOR_TEAM2 - t);
308                         if(a == 2)
309                                 return 4; // GET THIS ONE NOW!
310                         else
311                                 return 2; // TOUCH ME
312                 }
313         }
314         return 0;
315 }
316
317 void onslaught_generator_think()
318 {
319         local float d;
320         local entity e;
321         self.nextthink = ceil(time + 1);
322         if (cvar("timelimit"))
323         if (time > cvar("timelimit") * 60 - 60)
324         {
325                 // self.max_health / 300 gives 5 minutes of overtime.
326                 // control points reduce the overtime duration.
327                 sound(self, CHAN_TRIGGER, "onslaught/generator_decay.wav", VOL_BASE, ATTN_NORM);
328                 d = 1;
329                 e = findchain(classname, "onslaught_controlpoint");
330                 while (e)
331                 {
332                         if (e.team != self.team)
333                         if (e.islinked)
334                                 d = d + 1;
335                         e = e.chain;
336                 }
337                 d = d * self.max_health / 300;
338                 Damage(self, self, self, d, DEATH_HURTTRIGGER, self.origin, '0 0 0');
339         }
340 };
341
342 void onslaught_generator_deaththink()
343 {
344         local vector org;
345         if (self.count > 0)
346         {
347                 self.nextthink = time + 0.1;
348                 self.count = self.count - 1;
349                 org = randompos(self.origin + self.mins + '8 8 8', self.origin + self.maxs + '-8 -8 -8');
350                 pointparticles(particleeffectnum("onslaught_generator_smallexplosion"), org, '0 0 0', 1);
351                 sound(self, CHAN_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
352         }
353         else
354         {
355                 org = self.origin;
356                 pointparticles(particleeffectnum("onslaught_generator_finalexplosion"), org, '0 0 0', 1);
357                 sound(self, CHAN_TRIGGER, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
358         }
359 };
360
361 void onslaught_generator_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
362 {
363         if (damage <= 0)
364                 return;
365         if (attacker != self)
366         {
367                 if (self.isshielded)
368                 {
369                         // this is protected by a shield, so ignore the damage
370                         if (time > self.pain_finished)
371                         if (attacker.classname == "player")
372                         {
373                                 play2(attacker, "onslaught/damageblockedbyshield.wav");
374                                 self.pain_finished = time + 1;
375                         }
376                         return;
377                 }
378                 if (time > self.pain_finished)
379                 {
380                         self.pain_finished = time + 5;
381                         bprint(ColoredTeamName(self.team), " generator under attack!\n");
382                         play2team(self.team, "onslaught/generator_underattack.wav");
383                 }
384         }
385         self.health = self.health - damage;
386         // choose an animation frame based on health
387         self.frame = 10 * bound(0, (1 - self.health / self.max_health), 1);
388         // see if the generator is still functional, or dying
389         if (self.health > 0)
390         {
391                 float h, lh;
392                 lh = ceil(self.lasthealth / 100) * 100;
393                 h = ceil(self.health / 100) * 100;
394                 if(lh != h)
395                         bprint(ColoredTeamName(self.team), " generator has less than ", ftos(h), " health remaining\n");
396                 self.lasthealth = self.health;
397         }
398         else
399         {
400                 if (attacker == self)
401                         bprint(ColoredTeamName(self.team), " generator spontaneously exploded due to overtime!\n");
402                 else
403                 {
404                         string t;
405                         t = ColoredTeamName(attacker.team);
406                         bprint(ColoredTeamName(self.team), " generator destroyed by ", t, "!\n");
407                 }
408                 self.iscaptured = FALSE;
409                 self.islinked = FALSE;
410                 self.isshielded = FALSE;
411                 self.takedamage = DAMAGE_NO; // can't be hurt anymore
412                 self.event_damage = SUB_Null; // won't do anything if hurt
413                 self.count = 30; // 30 explosions
414                 self.think = onslaught_generator_deaththink; // explosion sequence
415                 self.nextthink = time; // start exploding immediately
416                 self.think(); // do the first explosion now
417                 onslaught_updatelinks();
418         }
419 };
420
421 // update links after a delay
422 void onslaught_generator_delayed()
423 {
424         onslaught_updatelinks();
425         // now begin normal thinking
426         self.think = onslaught_generator_think;
427         self.nextthink = time;
428 };
429
430 string onslaught_generator_waypointsprite_for_team(entity e, float t)
431 {
432         if(t == e.team)
433         {
434                 if(e.team == COLOR_TEAM1)
435                         return "ons-gen-red";
436                 else if(e.team == COLOR_TEAM2)
437                         return "ons-gen-blue";
438         }
439         if(e.isshielded)
440                 return "ons-gen-shielded";
441         if(e.team == COLOR_TEAM1)
442                 return "ons-gen-red";
443         else if(e.team == COLOR_TEAM2)
444                 return "ons-gen-blue";
445         return "";
446 }
447
448 void onslaught_generator_updatesprite(entity e)
449 {
450         string s1, s2, s3;
451         s1 = onslaught_generator_waypointsprite_for_team(e, COLOR_TEAM1);
452         s2 = onslaught_generator_waypointsprite_for_team(e, COLOR_TEAM2);
453         s3 = onslaught_generator_waypointsprite_for_team(e, -1);
454         WaypointSprite_UpdateSprites(e.sprite, s1, s2, s3);
455
456         if(e.lastteam != e.team + 2 || e.lastshielded != e.isshielded)
457         {
458                 e.lastteam = e.team + 2;
459                 e.lastshielded = e.isshielded;
460                 if(e.lastshielded)
461                 {
462                         if(e.team == COLOR_TEAM1)
463                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.5 0 0');
464                         else if(e.team == COLOR_TEAM2)
465                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0 0 0.5');
466                         else
467                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.5 0.5 0.5');
468                 }
469                 else
470                 {
471                         if(e.team == COLOR_TEAM1)
472                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '1 0 0');
473                         else if(e.team == COLOR_TEAM2)
474                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0 0 1');
475                         else
476                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.75 0.75 0.75');
477                 }
478                 WaypointSprite_Ping(e.sprite);
479         }
480 }
481
482 string onslaught_controlpoint_waypointsprite_for_team(entity e, float t)
483 {
484         float a;
485         if(t != -1)
486         {
487                 a = onslaught_controlpoint_attackable(e, t);
488                 if(a == 3 || a == 4) // ATTACK/TOUCH THIS ONE NOW
489                 {
490                         if(e.team == COLOR_TEAM1)
491                                 return "ons-cp-atck-red";
492                         else if(e.team == COLOR_TEAM2)
493                                 return "ons-cp-atck-blue";
494                         else
495                                 return "ons-cp-atck-neut";
496                 }
497                 else if(a == -2) // DEFEND THIS ONE NOW
498                 {
499                         if(e.team == COLOR_TEAM1)
500                                 return "ons-cp-dfnd-red";
501                         else if(e.team == COLOR_TEAM2)
502                                 return "ons-cp-dfnd-blue";
503                 }
504                 else if(e.team == t || a == -1 || a == 1) // own point, or fire at it
505                 {
506                         if(e.team == COLOR_TEAM1)
507                                 return "ons-cp-red";
508                         else if(e.team == COLOR_TEAM2)
509                                 return "ons-cp-blue";
510                 }
511                 else if(a == 2) // touch it
512                         return "ons-cp-neut";
513         }
514         else
515         {
516                 if(e.team == COLOR_TEAM1)
517                         return "ons-cp-red";
518                 else if(e.team == COLOR_TEAM2)
519                         return "ons-cp-blue";
520                 else
521                         return "ons-cp-neut";
522         }
523         return "";
524 }
525
526 void onslaught_controlpoint_updatesprite(entity e)
527 {
528         string s1, s2, s3;
529         s1 = onslaught_controlpoint_waypointsprite_for_team(e, COLOR_TEAM1);
530         s2 = onslaught_controlpoint_waypointsprite_for_team(e, COLOR_TEAM2);
531         s3 = onslaught_controlpoint_waypointsprite_for_team(e, -1);
532         WaypointSprite_UpdateSprites(e.sprite, s1, s2, s3);
533
534         float sh;
535         sh = !(onslaught_controlpoint_can_be_linked(e, COLOR_TEAM1) || onslaught_controlpoint_can_be_linked(e, COLOR_TEAM2));
536
537         if(e.lastteam != e.team + 2 || e.lastshielded != sh)
538         {
539                 e.lastteam = e.team + 2;
540                 e.lastshielded = sh;
541                 if(e.lastshielded)
542                 {
543                         if(e.team == COLOR_TEAM1)
544                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.5 0 0');
545                         else if(e.team == COLOR_TEAM2)
546                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0 0 0.5');
547                         else
548                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.5 0.5 0.5');
549                 }
550                 else
551                 {
552                         if(e.team == COLOR_TEAM1)
553                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '1 0 0');
554                         else if(e.team == COLOR_TEAM2)
555                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0 0 1');
556                         else
557                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.75 0.75 0.75');
558                 }
559                 WaypointSprite_Ping(e.sprite);
560         }
561 }
562
563 void onslaught_generator_reset()
564 {
565         self.team = self.team_saved;
566         self.lasthealth = self.max_health = self.health = cvar("g_onslaught_gen_health");
567         self.takedamage = DAMAGE_AIM;
568         self.bot_attack = TRUE;
569         self.iscaptured = TRUE;
570         self.islinked = TRUE;
571         self.isshielded = TRUE;
572         self.enemy.solid = SOLID_NOT;
573         self.think = onslaught_generator_delayed;
574         self.nextthink = time + 0.2;
575 }
576
577 /*QUAKED spawnfunc_onslaught_generator (0 .5 .8) (-32 -32 -24) (32 32 64)
578 Base generator.
579
580 spawnfunc_onslaught_link entities can target this.
581
582 keys:
583 "team" - team that owns this generator (5 = red, 14 = blue, etc), MUST BE SET.
584 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
585 */
586 void spawnfunc_onslaught_generator()
587 {
588         if (!g_onslaught)
589         {
590                 remove(self);
591                 return;
592         }
593
594         local entity e;
595         precache_model("models/onslaught/generator.md3");
596         precache_model("models/onslaught/generator_shield.md3");
597         precache_sound("onslaught/generator_decay.wav");
598         precache_sound("weapons/grenade_impact.wav");
599         precache_sound("weapons/rocket_impact.wav");
600         precache_sound("onslaught/generator_underattack.wav");
601         if (!self.team)
602                 objerror("team must be set");
603         self.team_saved = self.team;
604         self.colormap = 1024 + (self.team - 1) * 17;
605         self.solid = SOLID_BSP;
606         self.movetype = MOVETYPE_NONE;
607         self.lasthealth = self.max_health = self.health = cvar("g_onslaught_gen_health");
608         setmodel(self, "models/onslaught/generator.md3");
609         //setsize(self, '-32 -32 -24', '32 32 64');
610         setorigin(self, self.origin);
611         self.takedamage = DAMAGE_AIM;
612         self.bot_attack = TRUE;
613         self.event_damage = onslaught_generator_damage;
614         self.iscaptured = TRUE;
615         self.islinked = TRUE;
616         self.isshielded = TRUE;
617         // spawn shield model which indicates whether this can be damaged
618         self.enemy = e = spawn();
619         e.classname = "onslaught_generator_shield";
620         e.solid = SOLID_NOT;
621         e.movetype = MOVETYPE_NONE;
622         e.effects = EF_ADDITIVE;
623         setmodel(e, "models/onslaught/generator_shield.md3");
624         //setsize(e, '-32 -32 0', '32 32 128');
625         setorigin(e, self.origin);
626         e.colormap = self.colormap;
627         e.team = self.team;
628         self.think = onslaught_generator_delayed;
629         self.nextthink = time + 0.2;
630         InitializeEntity(self, onslaught_generator_delayed, INITPRIO_LAST);
631
632         WaypointSprite_SpawnFixed(string_null, e.origin + '0 0 1' * e.maxs_z, self, sprite);
633         WaypointSprite_UpdateRule(self.sprite, COLOR_TEAM2, SPRITERULE_TEAMPLAY);
634
635         onslaught_updatelinks();
636
637         self.reset = onslaught_generator_reset;
638 };
639
640 .float waslinked;
641 void onslaught_controlpoint_icon_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
642 {
643         entity oself;
644         if (damage <= 0)
645                 return;
646         if (self.owner.isshielded)
647         {
648                 // this is protected by a shield, so ignore the damage
649                 if (time > self.pain_finished)
650                 if (attacker.classname == "player")
651                 {
652                         play2(attacker, "onslaught/damageblockedbyshield.wav");
653                         self.pain_finished = time + 1;
654                 }
655                 return;
656         }
657         if (time > self.pain_finished)
658         if (attacker.classname == "player")
659         {
660                 play2team(self.team, "onslaught/controlpoint_underattack.wav");
661                 self.pain_finished = time + 5;
662         }
663         self.health = self.health - damage;
664         self.alpha = self.health / self.max_health;
665         self.pain_finished = time + 1;
666         // colormod flash when shot
667         self.colormod = '2 2 2';
668         if (self.health < 0)
669         {
670                 sound(self, CHAN_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
671                 pointparticles(particleeffectnum("onslaught_controlpoint_explosion"), self.origin, '0 0 0', 1);
672                 {
673                         string t;
674                         t = ColoredTeamName(attacker.team);
675                         bprint(ColoredTeamName(self.team), " ", self.message, " control point destroyed by ", t, "\n");
676                 }
677                 self.owner.goalentity = world;
678                 self.owner.islinked = FALSE;
679                 self.owner.iscaptured = FALSE;
680                 self.owner.team = 0;
681                 self.owner.colormap = 1024;
682                 onslaught_updatelinks();
683
684                 // Use targets now (somebody make sure this is in the right place..)
685                 oself = self;
686                 self = self.owner;
687                 activator = self;
688                 SUB_UseTargets ();
689                 self = oself;
690
691                 self.owner.waslinked = self.owner.islinked;
692
693                 remove(self);
694         }
695 };
696
697 void onslaught_controlpoint_icon_think()
698 {
699         entity oself;
700         self.nextthink = time + 0.1;
701         if (time > self.pain_finished + 1)
702         {
703                 self.health = self.health + self.count;
704                 if (self.health >= self.max_health)
705                         self.health = self.max_health;
706         }
707         self.alpha = self.health / self.max_health;
708         // colormod flash when shot
709         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
710
711         if(self.owner.islinked != self.owner.waslinked)
712         {
713                 // unteam the spawnpoint if needed
714                 float t;
715                 t = self.owner.team;
716                 if(!self.owner.islinked)
717                         self.owner.team = 0;
718
719                 oself = self;
720                 self = self.owner;
721                 activator = self;
722                 SUB_UseTargets ();
723                 self = oself;
724
725                 self.owner.team = t;
726
727                 self.owner.waslinked = self.owner.islinked;
728         }
729 };
730
731 void onslaught_controlpoint_icon_buildthink()
732 {
733         local entity oself;
734         float a;
735
736         self.nextthink = time + 0.1;
737
738         // only do this if there is power
739         a = onslaught_controlpoint_can_be_linked(self.owner, self.owner.team);
740         if(!a)
741                 return;
742
743         self.health = self.health + self.count;
744
745         if (self.health >= self.max_health)
746         {
747                 self.health = self.max_health;
748                 self.count = self.count * 0.2; // slow repair rate from now on
749                 self.think = onslaught_controlpoint_icon_think;
750                 sound(self, CHAN_TRIGGER, "onslaught/controlpoint_built.wav", VOL_BASE, ATTN_NORM);
751                 bprint(ColoredTeamName(self.team), " captured ", self.owner.message, " control point\n");
752                 self.owner.iscaptured = TRUE;
753                 onslaught_updatelinks();
754
755                 // Use targets now (somebody make sure this is in the right place..)
756                 oself = self;
757                 self = self.owner;
758                 activator = self;
759                 SUB_UseTargets ();
760                 self = oself;
761         }
762         self.alpha = self.health / self.max_health;
763         // colormod flash when shot
764         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
765 };
766
767 void onslaught_controlpoint_touch()
768 {
769         local entity e;
770         float a;
771         if (other.classname != "player")
772                 return;
773         a = onslaught_controlpoint_attackable(self, other.team);
774         if(a != 2 && a != 4)
775                 return;
776         // we've verified that this player has a legitimate claim to this point,
777         // so start building the captured point icon (which only captures this
778         // point if it successfully builds without being destroyed first)
779         self.goalentity = e = spawn();
780         e.classname = "onslaught_controlpoint_icon";
781         e.owner = self;
782         e.max_health = cvar("g_onslaught_cp_health");
783         e.health = e.max_health * 0.1;
784         e.alpha = e.health / e.max_health;
785         e.solid = SOLID_BBOX;
786         e.movetype = MOVETYPE_NONE;
787         setmodel(e, "models/onslaught/controlpoint_icon.md3");
788         setsize(e, '-32 -32 -32', '32 32 32');
789         setorigin(e, self.origin + '0 0 96');
790         e.takedamage = DAMAGE_AIM;
791         e.bot_attack = TRUE;
792         e.event_damage = onslaught_controlpoint_icon_damage;
793         e.team = other.team;
794         e.colormap = 1024 + (e.team - 1) * 17;
795         e.think = onslaught_controlpoint_icon_buildthink;
796         e.nextthink = time + 0.1;
797         e.count = e.max_health / 50; // how long it takes to build
798         sound(e, CHAN_TRIGGER, "onslaught/controlpoint_build.wav", VOL_BASE, ATTN_NORM);
799         self.team = e.team;
800         self.colormap = e.colormap;
801 };
802
803 void onslaught_controlpoint_reset()
804 {
805         if(self.goalentity && self.goalentity != world)
806                 remove(self.goalentity);
807         self.goalentity = world;
808         self.team = 0;
809         self.colormap = 1024;
810         self.iscaptured = FALSE;
811         self.islinked = FALSE;
812         self.isshielded = TRUE;
813         self.enemy.solid = SOLID_NOT;
814         self.enemy.colormap = self.colormap;
815         self.think = self.enemy.think = SUB_Null;
816         self.nextthink = 0; // don't like SUB_Null :P
817         
818         onslaught_updatelinks();
819
820         activator = self;
821         SUB_UseTargets(); // to reset the structures, playerspawns etc.
822 }
823
824 /*QUAKED spawnfunc_onslaught_controlpoint (0 .5 .8) (-32 -32 0) (32 32 128)
825 Control point.  Be sure to give this enough clearance so that the shootable part has room to exist
826
827 This should link to an spawnfunc_onslaught_controlpoint entity or spawnfunc_onslaught_generator entity.
828
829 keys:
830 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
831 "target" - target any entities that are tied to this control point, such as vehicles and buildable structure entities.
832 "message" - name of this control point (should reflect the location in the map, such as "center bridge", "north tower", etc)
833 */
834 void spawnfunc_onslaught_controlpoint()
835 {
836         local entity e;
837         if (!g_onslaught)
838         {
839                 remove(self);
840                 return;
841         }
842         precache_model("models/onslaught/controlpoint_pad.md3");
843         precache_model("models/onslaught/controlpoint_shield.md3");
844         precache_model("models/onslaught/controlpoint_icon.md3");
845         precache_sound("onslaught/controlpoint_build.wav");
846         precache_sound("onslaught/controlpoint_built.wav");
847         precache_sound("weapons/grenade_impact.wav");
848         precache_sound("onslaught/damageblockedbyshield.wav");
849         precache_sound("onslaught/controlpoint_underattack.wav");
850         self.solid = SOLID_BSP;
851         self.movetype = MOVETYPE_NONE;
852         setmodel(self, "models/onslaught/controlpoint_pad.md3");
853         //setsize(self, '-32 -32 0', '32 32 8');
854         setorigin(self, self.origin);
855         self.touch = onslaught_controlpoint_touch;
856         self.team = 0;
857         self.colormap = 1024;
858         self.iscaptured = FALSE;
859         self.islinked = FALSE;
860         self.isshielded = TRUE;
861         // spawn shield model which indicates whether this can be damaged
862         self.enemy = e = spawn();
863         e.classname = "onslaught_controlpoint_shield";
864         e.solid = SOLID_NOT;
865         e.movetype = MOVETYPE_NONE;
866         e.effects = EF_ADDITIVE;
867         setmodel(e, "models/onslaught/controlpoint_shield.md3");
868         //setsize(e, '-32 -32 0', '32 32 128');
869         setorigin(e, self.origin);
870         e.colormap = self.colormap;
871
872         waypoint_spawnforitem(self);
873
874         WaypointSprite_SpawnFixed(string_null, e.origin + '0 0 1' * e.maxs_z, self, sprite);
875         WaypointSprite_UpdateRule(self.sprite, COLOR_TEAM2, SPRITERULE_TEAMPLAY);
876
877         onslaught_updatelinks();
878
879         self.reset = onslaught_controlpoint_reset;
880 };
881
882 float onslaught_link_send(entity to, float sendflags)
883 {
884         WriteByte(MSG_ENTITY, ENT_CLIENT_RADARLINK);
885         WriteByte(MSG_ENTITY, sendflags);
886         if(sendflags & 1)
887         {
888                 WriteCoord(MSG_ENTITY, self.goalentity.origin_x);
889                 WriteCoord(MSG_ENTITY, self.goalentity.origin_y);
890                 WriteCoord(MSG_ENTITY, self.goalentity.origin_z);
891         }
892         if(sendflags & 2)
893         {
894                 WriteCoord(MSG_ENTITY, self.enemy.origin_x);
895                 WriteCoord(MSG_ENTITY, self.enemy.origin_y);
896                 WriteCoord(MSG_ENTITY, self.enemy.origin_z);
897         }
898         if(sendflags & 4)
899         {
900                 WriteByte(MSG_ENTITY, self.clientcolors); // which is goalentity's color + enemy's color * 16
901         }
902         return TRUE;
903 }
904
905 void onslaught_link_checkupdate()
906 {
907         // TODO check if the two sides have moved (currently they won't move anyway)
908         float redpower, bluepower;
909
910         redpower = bluepower = 0;
911         if(self.goalentity.islinked)
912         {
913                 if(self.goalentity.team == COLOR_TEAM1)
914                         redpower = 1;
915                 else if(self.goalentity.team == COLOR_TEAM2)
916                         bluepower = 1;
917         }
918         if(self.enemy.islinked)
919         {
920                 if(self.enemy.team == COLOR_TEAM1)
921                         redpower = 2;
922                 else if(self.enemy.team == COLOR_TEAM2)
923                         bluepower = 2;
924         }
925
926         float cc;
927         if(redpower == 1 && bluepower == 2)
928                 cc = (COLOR_TEAM1 - 1) * 0x01 + (COLOR_TEAM2 - 1) * 0x10;
929         else if(redpower == 2 && bluepower == 1)
930                 cc = (COLOR_TEAM1 - 1) * 0x10 + (COLOR_TEAM2 - 1) * 0x01;
931         else if(redpower)
932                 cc = (COLOR_TEAM1 - 1) * 0x11;
933         else if(bluepower)
934                 cc = (COLOR_TEAM2 - 1) * 0x11;
935         else
936                 cc = 0;
937
938         if(cc != self.clientcolors)
939         {
940                 self.clientcolors = cc;
941                 self.SendFlags |= 4;
942         }
943
944         self.nextthink = time;
945 }
946
947 void onslaught_link_delayed()
948 {
949         self.goalentity = find(world, targetname, self.target);
950         self.enemy = find(world, targetname, self.target2);
951         if (!self.goalentity)
952                 objerror("can not find target\n");
953         if (!self.enemy)
954                 objerror("can not find target2\n");
955         dprint(etos(self.goalentity), " linked with ", etos(self.enemy), "\n");
956
957         self.SendFlags |= 3;
958         self.think = onslaught_link_checkupdate;
959         self.nextthink = time;
960 }
961
962 /*QUAKED spawnfunc_onslaught_link (0 .5 .8) (-16 -16 -16) (16 16 16)
963 Link between control points.
964
965 This entity targets two different spawnfunc_onslaught_controlpoint or spawnfunc_onslaught_generator entities, and suppresses shielding on both if they are owned by different teams.
966
967 keys:
968 "target" - first control point.
969 "target2" - second control point.
970 */
971 void spawnfunc_onslaught_link()
972 {
973         if (!g_onslaught)
974         {
975                 remove(self);
976                 return;
977         }
978         if (self.target == "" || self.target2 == "")
979                 objerror("target and target2 must be set\n");
980         InitializeEntity(self, onslaught_link_delayed, INITPRIO_FINDTARGET);
981         Net_LinkEntity(self, FALSE, 0, onslaught_link_send);
982 };