- rename sys_ticrate global to sys_frametime, and premultiply it with slowmo (fixes...
[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 .float lastcaptured;
18
19 .string model1, model2, model3;
20
21 void ons_gib_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
22 {
23         self.velocity = self.velocity + vforce;
24 }
25
26 .float giblifetime;
27 void ons_throwgib_think()
28 {
29         float d;
30
31         self.nextthink = time + 0.05;
32
33         d = self.giblifetime - time;
34
35         if(d<0)
36         {
37                 self.think = SUB_Remove;
38                 return;
39         }
40         if(d<1)
41                 self.alpha = d;
42
43         if(d>2)
44         if(random()<0.6)
45                 pointparticles(particleeffectnum("onslaught_generator_gib_flame"), self.origin, '0 0 0', 1);
46 };
47
48 void ons_throwgib(vector v_from, vector v_to, string smodel, float f_lifetime, float b_burn)
49 {
50         local entity gib;
51
52         gib = spawn();
53
54         setmodel(gib, smodel);
55         setorigin(gib, v_from);
56         gib.solid = SOLID_BBOX;
57         gib.movetype = MOVETYPE_BOUNCE;
58         gib.takedamage = DAMAGE_YES;
59         gib.event_damage = ons_gib_damage;
60         gib.health = -1;
61         gib.effects = EF_LOWPRECISION;
62         gib.flags = FL_NOTARGET;
63         gib.velocity = v_to;
64         gib.giblifetime = time + f_lifetime;
65
66         if (b_burn)
67         {
68                 gib.think = ons_throwgib_think;
69                 gib.nextthink = time + 0.05;
70         }
71         else
72                 SUB_SetFade(gib, gib.giblifetime, 2);
73 };
74
75 void onslaught_updatelinks()
76 {
77         local entity l, links;
78         local float stop, t1, t2, t3, t4;
79         // first check if the game has ended
80         dprint("--- updatelinks ---\n");
81         links = findchain(classname, "onslaught_link");
82         // mark generators as being shielded and networked
83         l = findchain(classname, "onslaught_generator");
84         while (l)
85         {
86                 if (l.iscaptured)
87                         dprint(etos(l), " (generator) belongs to team ", ftos(l.team), "\n");
88                 else
89                         dprint(etos(l), " (generator) is destroyed\n");
90                 l.islinked = l.iscaptured;
91                 l.isshielded = l.iscaptured;
92                 l = l.chain;
93         }
94         // mark points as shielded and not networked
95         l = findchain(classname, "onslaught_controlpoint");
96         while (l)
97         {
98                 l.islinked = FALSE;
99                 l.isshielded = TRUE;
100                 l.isgenneighbor_red = FALSE;
101                 l.isgenneighbor_blue = FALSE;
102                 l.iscpneighbor_red = FALSE;
103                 l.iscpneighbor_blue = FALSE;
104                 dprint(etos(l), " (point) belongs to team ", ftos(l.team), "\n");
105                 l = l.chain;
106         }
107         // flow power outward from the generators through the network
108         l = links;
109         while (l)
110         {
111                 dprint(etos(l), " (link) connects ", etos(l.goalentity), " with ", etos(l.enemy), "\n");
112                 l = l.chain;
113         }
114         stop = FALSE;
115         while (!stop)
116         {
117                 stop = TRUE;
118                 l = links;
119                 while (l)
120                 {
121                         // if both points are captured by the same team, and only one of
122                         // them is powered, mark the other one as powered as well
123                         if (l.enemy.iscaptured && l.goalentity.iscaptured)
124                                 if (l.enemy.islinked != l.goalentity.islinked)
125                                         if (l.enemy.team == l.goalentity.team)
126                                         {
127                                                 if (!l.goalentity.islinked)
128                                                 {
129                                                         stop = FALSE;
130                                                         l.goalentity.islinked = TRUE;
131                                                         dprint(etos(l), " (link) is marking ", etos(l.goalentity), " (point) because its team matches ", etos(l.enemy), " (point)\n");
132                                                 }
133                                                 else if (!l.enemy.islinked)
134                                                 {
135                                                         stop = FALSE;
136                                                         l.enemy.islinked = TRUE;
137                                                         dprint(etos(l), " (link) is marking ", etos(l.enemy), " (point) because its team matches ", etos(l.goalentity), " (point)\n");
138                                                 }
139                                         }
140                         l = l.chain;
141                 }
142         }
143         // now that we know which points are powered we can mark their neighbors
144         // as unshielded if team differs
145         l = links;
146         while (l)
147         {
148                 if (l.goalentity.islinked)
149                 {
150                         if (l.goalentity.team != l.enemy.team)
151                         {
152                                 dprint(etos(l), " (link) is unshielding ", etos(l.enemy), " (point) because its team does not match ", etos(l.goalentity), " (point)\n");
153                                 l.enemy.isshielded = FALSE;
154                         }
155                         if(l.goalentity.classname == "onslaught_generator")
156                         {
157                                 if(l.goalentity.team == COLOR_TEAM1)
158                                         l.enemy.isgenneighbor_red = TRUE;
159                                 else if(l.goalentity.team == COLOR_TEAM2)
160                                         l.enemy.isgenneighbor_blue = TRUE;
161                         }
162                         else
163                         {
164                                 if(l.goalentity.team == COLOR_TEAM1)
165                                         l.enemy.iscpneighbor_red = TRUE;
166                                 else if(l.goalentity.team == COLOR_TEAM2)
167                                         l.enemy.iscpneighbor_blue = TRUE;
168                         }
169                 }
170                 if (l.enemy.islinked)
171                 {
172                         if (l.goalentity.team != l.enemy.team)
173                         {
174                                 dprint(etos(l), " (link) is unshielding ", etos(l.goalentity), " (point) because its team does not match ", etos(l.enemy), " (point)\n");
175                                 l.goalentity.isshielded = FALSE;
176                         }
177                         if(l.enemy.classname == "onslaught_generator")
178                         {
179                                 if(l.enemy.team == COLOR_TEAM1)
180                                         l.goalentity.isgenneighbor_red = TRUE;
181                                 else if(l.enemy.team == COLOR_TEAM2)
182                                         l.goalentity.isgenneighbor_blue = TRUE;
183                         }
184                         else
185                         {
186                                 if(l.enemy.team == COLOR_TEAM1)
187                                         l.goalentity.iscpneighbor_red = TRUE;
188                                 else if(l.enemy.team == COLOR_TEAM2)
189                                         l.goalentity.iscpneighbor_blue = TRUE;
190                         }
191                 }
192                 l = l.chain;
193         }
194         // now update the takedamage and alpha variables on generator shields
195         l = findchain(classname, "onslaught_generator");
196         while (l)
197         {
198                 if (l.isshielded)
199                 {
200                         dprint(etos(l), " (generator) is shielded\n");
201                         l.enemy.alpha = 1;
202                         l.takedamage = DAMAGE_NO;
203                         l.bot_attack = FALSE;
204                 }
205                 else
206                 {
207                         dprint(etos(l), " (generator) is not shielded\n");
208                         l.enemy.alpha = -1;
209                         l.takedamage = DAMAGE_AIM;
210                         l.bot_attack = TRUE;
211                 }
212                 l = l.chain;
213         }
214         // now update the takedamage and alpha variables on control point icons
215         l = findchain(classname, "onslaught_controlpoint");
216         while (l)
217         {
218                 if (l.isshielded)
219                 {
220                         dprint(etos(l), " (point) is shielded\n");
221                         l.enemy.alpha = 1;
222                         if (l.goalentity)
223                         {
224                                 l.goalentity.takedamage = DAMAGE_NO;
225                                 l.goalentity.bot_attack = FALSE;
226                         }
227                 }
228                 else
229                 {
230                         dprint(etos(l), " (point) is not shielded\n");
231                         l.enemy.alpha = -1;
232                         if (l.goalentity)
233                         {
234                                 l.goalentity.takedamage = DAMAGE_AIM;
235                                 l.goalentity.bot_attack = TRUE;
236                         }
237                 }
238                 onslaught_controlpoint_updatesprite(l);
239                 l = l.chain;
240         }
241         // count generators owned by each team
242         t1 = t2 = t3 = t4 = 0;
243         l = findchain(classname, "onslaught_generator");
244         while (l)
245         {
246                 if (l.iscaptured)
247                 {
248                         if (l.team == COLOR_TEAM1) t1 = 1;
249                         if (l.team == COLOR_TEAM2) t2 = 1;
250                         if (l.team == COLOR_TEAM3) t3 = 1;
251                         if (l.team == COLOR_TEAM4) t4 = 1;
252                 }
253                 onslaught_generator_updatesprite(l);
254                 l = l.chain;
255         }
256         // see if multiple teams remain (if not, it's game over)
257         if (t1 + t2 + t3 + t4 < 2)
258                 dprint("--- game over ---\n");
259         else
260                 dprint("--- done updating links ---\n");
261 };
262
263 float onslaught_controlpoint_can_be_linked(entity cp, float t)
264 {
265         if(t == COLOR_TEAM1)
266         {
267                 if(cp.isgenneighbor_red)
268                         return 2;
269                 if(cp.iscpneighbor_red)
270                         return 1;
271         }
272         else if(t == COLOR_TEAM2)
273         {
274                 if(cp.isgenneighbor_blue)
275                         return 2;
276                 if(cp.iscpneighbor_blue)
277                         return 1;
278         }
279         return 0;
280         /*
281            entity e;
282         // check to see if this player has a legitimate claim to capture this
283         // control point - more specifically that there is a captured path of
284         // points leading back to the team generator
285         e = findchain(classname, "onslaught_link");
286         while (e)
287         {
288         if (e.goalentity == cp)
289         {
290         dprint(etos(e), " (link) connects to ", etos(e.enemy), " (point)");
291         if (e.enemy.islinked)
292         {
293         dprint(" which is linked");
294         if (e.enemy.team == t)
295         {
296         dprint(" and has the correct team!\n");
297         return 1;
298         }
299         else
300         dprint(" but has the wrong team\n");
301         }
302         else
303         dprint("\n");
304         }
305         else if (e.enemy == cp)
306         {
307         dprint(etos(e), " (link) connects to ", etos(e.goalentity), " (point)");
308         if (e.goalentity.islinked)
309         {
310         dprint(" which is linked");
311         if (e.goalentity.team == t)
312         {
313         dprint(" and has a team!\n");
314         return 1;
315         }
316         else
317         dprint(" but has the wrong team\n");
318         }
319         else
320         dprint("\n");
321         }
322         e = e.chain;
323         }
324         return 0;
325          */
326 }
327
328 float onslaught_controlpoint_attackable(entity cp, float t)
329         // -2: SAME TEAM, attackable by enemy!
330         // -1: SAME TEAM!
331         // 0: off limits
332         // 1: attack it
333         // 2: touch it
334         // 3: attack it (HIGH PRIO)
335         // 4: touch it (HIGH PRIO)
336 {
337         float a;
338
339         if(cp.isshielded)
340         {
341                 return 0;
342         }
343         else if(cp.goalentity)
344         {
345                 // if there's already an icon built, nothing happens
346                 if(cp.team == t)
347                 {
348                         a = onslaught_controlpoint_can_be_linked(cp, COLOR_TEAM1 + COLOR_TEAM2 - t);
349                         if(a) // attackable by enemy?
350                                 return -2; // EMERGENCY!
351                         return -1;
352                 }
353                 // we know it can be linked, so no need to check
354                 // but...
355                 a = onslaught_controlpoint_can_be_linked(cp, t);
356                 if(a == 2) // near our generator?
357                         return 3; // EMERGENCY!
358                 return 1;
359         }
360         else
361         {
362                 // free point
363                 if(onslaught_controlpoint_can_be_linked(cp, t))
364                 {
365                         a = onslaught_controlpoint_can_be_linked(cp, COLOR_TEAM1 + COLOR_TEAM2 - t);
366                         if(a == 2)
367                                 return 4; // GET THIS ONE NOW!
368                         else
369                                 return 2; // TOUCH ME
370                 }
371         }
372         return 0;
373 }
374
375 void onslaught_generator_think()
376 {
377         local float d;
378         local entity e;
379         self.nextthink = ceil(time + 1);
380         if (!gameover)
381         {
382                 if (cvar("timelimit"))
383                 if (time > game_starttime + cvar("timelimit") * 60)
384                 {
385                         // self.max_health / 300 gives 5 minutes of overtime.
386                         // control points reduce the overtime duration.
387                         sound(self, CHAN_TRIGGER, "onslaught/generator_decay.wav", VOL_BASE, ATTN_NORM);
388                         d = 1;
389                         e = findchain(classname, "onslaught_controlpoint");
390                         while (e)
391                         {
392                                 if (e.team != self.team)
393                                         if (e.islinked)
394                                                 d = d + 1;
395                                 e = e.chain;
396                         }
397                         d = d * self.max_health / 300;
398                         Damage(self, self, self, d, DEATH_HURTTRIGGER, self.origin, '0 0 0');
399                 }
400         }
401 };
402
403 void onslaught_generator_ring_spawn(vector org)
404 {
405         modeleffect_spawn("models/onslaught/shockwavetransring.md3", 0, 0, org, '0 0 0', '0 0 0', '0 0 0', 0, -16, 0.1, 1.25, 0.25);
406 };
407
408 void onslaught_generator_ray_think()
409 {
410         self.nextthink = time + 0.05;
411         if(self.count > 10)
412         {
413                 self.think = SUB_Remove;
414                 return;
415         }
416
417         if(self.count > 5)
418                 self.alpha -= 0.1;
419         else
420                 self.alpha += 0.1;
421
422         self.scale += 0.2;
423         self.count +=1;
424 };
425
426 void onslaught_generator_ray_spawn(vector org)
427 {
428         entity e;
429         e = spawn();
430         setmodel(e, "models/onslaught/ons_ray.md3");
431         setorigin(e, org);
432         e.angles = randomvec() * 360;
433         e.alpha = 0;
434         e.scale = random() * 5 + 8;
435         e.think = onslaught_generator_ray_think;
436         e.nextthink = time + 0.05;
437 };
438
439 void onslaught_generator_shockwave_spawn(vector org)
440 {
441         shockwave_spawn("models/onslaught/shockwave.md3", org, -64, 0.75, 0.5);
442 };
443
444 void onslaught_generator_damage_think()
445 {
446         if(self.owner.health < 0)
447         {
448                 self.think = SUB_Remove;
449                 return;
450         }
451         self.nextthink = time+0.1;
452
453         // damaged fx (less probable the more damaged is the generator)
454         if(random() < 0.9 - self.owner.health / self.owner.max_health)
455                 if(random() < 0.01)
456                 {
457                         pointparticles(particleeffectnum("electro_ballexplode"), self.origin + randompos('-50 -50 -20', '50 50 50'), '0 0 0', 1);
458                         sound(self, CHAN_TRIGGER, "onslaught/electricity_explode.wav", VOL_BASE, ATTN_NORM);
459                 }
460                 else
461                         pointparticles(particleeffectnum("torch_small"), self.origin + randompos('-60 -60 -20', '60 60 60'), '0 0 0', 1);
462 };
463
464 void onslaught_generator_damage_spawn(entity gd_owner)
465 {
466         entity e;
467         e = spawn();
468         e.owner = gd_owner;
469         e.health = self.owner.health;
470         setorigin(e, gd_owner.origin);
471         e.think = onslaught_generator_damage_think;
472         e.nextthink = time+1;
473 };
474
475 void onslaught_generator_deaththink()
476 {
477         local vector org;
478         local float i;
479
480         if not (self.count)
481                 self.count = 40;
482
483         // White shockwave
484         if(self.count==40||self.count==20)
485         {
486                 onslaught_generator_ring_spawn(self.origin);
487                 sound(self, CHAN_TRIGGER, "onslaught/shockwave.wav", VOL_BASE, ATTN_NORM);
488         }
489
490         // Throw some gibs
491         if(random() < 0.3)
492         {
493                 i = random();
494                 if(i < 0.3)
495                         ons_throwgib(self.origin + '0 0 40', (100 * randomvec() - '1 1 1') * 11 + '0 0 20', "models/onslaught/gen_gib1.md3", 6, TRUE);
496                 else if(i > 0.7)
497                         ons_throwgib(self.origin + '0 0 40', (100 * randomvec() - '1 1 1') * 12 + '0 0 20', "models/onslaught/gen_gib2.md3", 6, TRUE);
498                 else
499                         ons_throwgib(self.origin + '0 0 40', (100 * randomvec() - '1 1 1') * 13 + '0 0 20', "models/onslaught/gen_gib3.md3", 6, TRUE);
500         }
501
502         // Spawn fire balls
503         for(i=0;i < 10;++i)
504         {
505                 org = self.origin + randompos('-30 -30 -30' * i + '0 0 -20', '30 30 30' * i + '0 0 20');
506                 pointparticles(particleeffectnum("onslaught_generator_gib_explode"), org, '0 0 0', 1);
507         }
508
509         // Short explosion sound + small explosion
510         if(random() < 0.25)
511         {
512                 te_explosion(self.origin);
513                 sound(self, CHAN_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
514         }
515
516         // Particles
517         org = self.origin + randompos(self.mins + '8 8 8', self.maxs + '-8 -8 -8');
518         pointparticles(particleeffectnum("onslaught_generator_smallexplosion"), org, '0 0 0', 1);
519
520         // rays
521         if(random() > 0.25 )
522         {
523                 onslaught_generator_ray_spawn(self.origin);
524         }
525
526         // Final explosion
527         if(self.count==1)
528         {
529                 org = self.origin;
530                 te_explosion(org);
531                 onslaught_generator_shockwave_spawn(org);
532                 pointparticles(particleeffectnum("onslaught_generator_finalexplosion"), org, '0 0 0', 1);
533                 sound(self, CHAN_TRIGGER, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
534         }
535         else
536                 self.nextthink = time + 0.05;
537
538         self.count = self.count - 1;
539 };
540
541 void onslaught_generator_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
542 {
543         local float i;
544         if (damage <= 0)
545                 return;
546         if(inWarmupStage)
547                 return;
548         if (attacker != self)
549         {
550                 if (self.isshielded)
551                 {
552                         // this is protected by a shield, so ignore the damage
553                         if (time > self.pain_finished)
554                                 if (attacker.classname == "player")
555                                 {
556                                         play2(attacker, "onslaught/damageblockedbyshield.wav");
557                                         self.pain_finished = time + 1;
558                                 }
559                         return;
560                 }
561                 if (time > self.pain_finished)
562                 {
563                         self.pain_finished = time + 10;
564                         bprint(ColoredTeamName(self.team), " generator under attack!\n");
565                         play2team(self.team, "onslaught/generator_underattack.wav");
566                 }
567         }
568         self.health = self.health - damage;
569         WaypointSprite_UpdateHealth(self.sprite, self.health);
570         // choose an animation frame based on health
571         self.frame = 10 * bound(0, (1 - self.health / self.max_health), 1);
572         // see if the generator is still functional, or dying
573         if (self.health > 0)
574         {
575 #ifdef ONSLAUGHT_SPAM
576                 float h, lh;
577                 lh = ceil(self.lasthealth / 100) * 100;
578                 h = ceil(self.health / 100) * 100;
579                 if(lh != h)
580                         bprint(ColoredTeamName(self.team), " generator has less than ", ftos(h), " health remaining\n");
581 #endif
582                 self.lasthealth = self.health;
583         }
584         else if not(inWarmupStage)
585         {
586                 if (attacker == self)
587                         bprint(ColoredTeamName(self.team), " generator spontaneously exploded due to overtime!\n");
588                 else
589                 {
590                         string t;
591                         t = ColoredTeamName(attacker.team);
592                         bprint(ColoredTeamName(self.team), " generator destroyed by ", t, "!\n");
593                 }
594                 self.iscaptured = FALSE;
595                 self.islinked = FALSE;
596                 self.isshielded = FALSE;
597                 self.takedamage = DAMAGE_NO; // can't be hurt anymore
598                 self.event_damage = SUB_Null; // won't do anything if hurt
599                 self.count = 0; // reset counter
600                 self.think = onslaught_generator_deaththink; // explosion sequence
601                 self.nextthink = time; // start exploding immediately
602                 self.think(); // do the first explosion now
603
604                 WaypointSprite_UpdateMaxHealth(self.sprite, 0);
605
606                 onslaught_updatelinks();
607         }
608
609         if(self.health <= 0)
610                 setmodel(self, "models/onslaught/generator_dead.md3");
611         else if(self.health < self.max_health * 0.10)
612                 setmodel(self, "models/onslaught/generator_dmg9.md3");
613         else if(self.health < self.max_health * 0.20)
614                 setmodel(self, "models/onslaught/generator_dmg8.md3");
615         else if(self.health < self.max_health * 0.30)
616                 setmodel(self, "models/onslaught/generator_dmg7.md3");
617         else if(self.health < self.max_health * 0.40)
618                 setmodel(self, "models/onslaught/generator_dmg6.md3");
619         else if(self.health < self.max_health * 0.50)
620                 setmodel(self, "models/onslaught/generator_dmg5.md3");
621         else if(self.health < self.max_health * 0.60)
622                 setmodel(self, "models/onslaught/generator_dmg4.md3");
623         else if(self.health < self.max_health * 0.70)
624                 setmodel(self, "models/onslaught/generator_dmg3.md3");
625         else if(self.health < self.max_health * 0.80)
626                 setmodel(self, "models/onslaught/generator_dmg2.md3");
627         else if(self.health < self.max_health * 0.90)
628                 setmodel(self, "models/onslaught/generator_dmg1.md3");
629         setsize(self, '-52 -52 -14', '52 52 75');
630
631         // Throw some flaming gibs on damage, more damage = more chance for gib
632         if(random() < damage/220)
633         {
634                 sound(self, CHAN_TRIGGER, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
635                 i = random();
636                 if(i < 0.3)
637                         ons_throwgib(hitloc + '0 0 20', force * -1, "models/onslaught/gen_gib1.md3", 5, TRUE);
638                 else if(i > 0.7)
639                         ons_throwgib(hitloc + '0 0 20', force * -1, "models/onslaught/gen_gib2.md3", 5, TRUE);
640                 else
641                         ons_throwgib(hitloc + '0 0 20', force * -1, "models/onslaught/gen_gib3.md3", 5, TRUE);
642         }
643         else
644         {
645                 // particles on every hit
646                 pointparticles(particleeffectnum("sparks"), hitloc, force * -1, 1);
647
648                 //sound on every hit
649                 if (random() < 0.5)
650                         sound(self, CHAN_TRIGGER, "onslaught/ons_hit1.wav", VOL_BASE, ATTN_NORM);
651                 else
652                         sound(self, CHAN_TRIGGER, "onslaught/ons_hit2.wav", VOL_BASE, ATTN_NORM);
653         }
654
655         //throw some gibs on damage
656         if(random() < damage/200+0.2)
657                 if(random() < 0.5)
658                         ons_throwgib(hitloc + '0 0 20', randomvec()*360, "models/onslaught/gen_gib1.md3", 5, FALSE);
659 };
660
661 // update links after a delay
662 void onslaught_generator_delayed()
663 {
664         onslaught_updatelinks();
665         // now begin normal thinking
666         self.think = onslaught_generator_think;
667         self.nextthink = time;
668 };
669
670 string onslaught_generator_waypointsprite_for_team(entity e, float t)
671 {
672         if(t == e.team)
673         {
674                 if(e.team == COLOR_TEAM1)
675                         return "ons-gen-red";
676                 else if(e.team == COLOR_TEAM2)
677                         return "ons-gen-blue";
678         }
679         if(e.isshielded)
680                 return "ons-gen-shielded";
681         if(e.team == COLOR_TEAM1)
682                 return "ons-gen-red";
683         else if(e.team == COLOR_TEAM2)
684                 return "ons-gen-blue";
685         return "";
686 }
687
688 void onslaught_generator_updatesprite(entity e)
689 {
690         string s1, s2, s3;
691         s1 = onslaught_generator_waypointsprite_for_team(e, COLOR_TEAM1);
692         s2 = onslaught_generator_waypointsprite_for_team(e, COLOR_TEAM2);
693         s3 = onslaught_generator_waypointsprite_for_team(e, -1);
694         WaypointSprite_UpdateSprites(e.sprite, s1, s2, s3);
695
696         if(e.lastteam != e.team + 2 || e.lastshielded != e.isshielded)
697         {
698                 e.lastteam = e.team + 2;
699                 e.lastshielded = e.isshielded;
700                 if(e.lastshielded)
701                 {
702                         if(e.team == COLOR_TEAM1 || e.team == COLOR_TEAM2)
703                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, 0.5 * colormapPaletteColor(e.team - 1, FALSE));
704                         else
705                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.5 0.5 0.5');
706                 }
707                 else
708                 {
709                         if(e.team == COLOR_TEAM1 || e.team == COLOR_TEAM2)
710                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, colormapPaletteColor(e.team - 1, FALSE));
711                         else
712                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.75 0.75 0.75');
713                 }
714                 WaypointSprite_Ping(e.sprite);
715         }
716 }
717
718 string onslaught_controlpoint_waypointsprite_for_team(entity e, float t)
719 {
720         float a;
721         if(t != -1)
722         {
723                 a = onslaught_controlpoint_attackable(e, t);
724                 if(a == 3 || a == 4) // ATTACK/TOUCH THIS ONE NOW
725                 {
726                         if(e.team == COLOR_TEAM1)
727                                 return "ons-cp-atck-red";
728                         else if(e.team == COLOR_TEAM2)
729                                 return "ons-cp-atck-blue";
730                         else
731                                 return "ons-cp-atck-neut";
732                 }
733                 else if(a == -2) // DEFEND THIS ONE NOW
734                 {
735                         if(e.team == COLOR_TEAM1)
736                                 return "ons-cp-dfnd-red";
737                         else if(e.team == COLOR_TEAM2)
738                                 return "ons-cp-dfnd-blue";
739                 }
740                 else if(e.team == t || a == -1 || a == 1) // own point, or fire at it
741                 {
742                         if(e.team == COLOR_TEAM1)
743                                 return "ons-cp-red";
744                         else if(e.team == COLOR_TEAM2)
745                                 return "ons-cp-blue";
746                 }
747                 else if(a == 2) // touch it
748                         return "ons-cp-neut";
749         }
750         else
751         {
752                 if(e.team == COLOR_TEAM1)
753                         return "ons-cp-red";
754                 else if(e.team == COLOR_TEAM2)
755                         return "ons-cp-blue";
756                 else
757                         return "ons-cp-neut";
758         }
759         return "";
760 }
761
762 void onslaught_controlpoint_updatesprite(entity e)
763 {
764         string s1, s2, s3;
765         s1 = onslaught_controlpoint_waypointsprite_for_team(e, COLOR_TEAM1);
766         s2 = onslaught_controlpoint_waypointsprite_for_team(e, COLOR_TEAM2);
767         s3 = onslaught_controlpoint_waypointsprite_for_team(e, -1);
768         WaypointSprite_UpdateSprites(e.sprite, s1, s2, s3);
769
770         float sh;
771         sh = !(onslaught_controlpoint_can_be_linked(e, COLOR_TEAM1) || onslaught_controlpoint_can_be_linked(e, COLOR_TEAM2));
772
773         if(e.lastteam != e.team + 2 || e.lastshielded != sh || e.iscaptured != e.lastcaptured)
774         {
775                 if(e.iscaptured) // don't mess up build bars!
776                 {
777                         if(sh)
778                         {
779                                 WaypointSprite_UpdateMaxHealth(e.sprite, 0);
780                         }
781                         else
782                         {
783                                 WaypointSprite_UpdateMaxHealth(e.sprite, e.goalentity.max_health);
784                                 WaypointSprite_UpdateHealth(e.sprite, e.goalentity.health);
785                         }
786                 }
787                 if(e.lastshielded)
788                 {
789                         if(e.team == COLOR_TEAM1 || e.team == COLOR_TEAM2)
790                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, 0.5 * colormapPaletteColor(e.team - 1, FALSE));
791                         else
792                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.5 0.5 0.5');
793                 }
794                 else
795                 {
796                         if(e.team == COLOR_TEAM1 || e.team == COLOR_TEAM2)
797                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, colormapPaletteColor(e.team - 1, FALSE));
798                         else
799                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.75 0.75 0.75');
800                 }
801                 WaypointSprite_Ping(e.sprite);
802
803                 e.lastteam = e.team + 2;
804                 e.lastshielded = sh;
805                 e.lastcaptured = e.iscaptured;
806         }
807 }
808
809 void onslaught_generator_reset()
810 {
811         self.team = self.team_saved;
812         self.lasthealth = self.max_health = self.health = cvar("g_onslaught_gen_health");
813         self.takedamage = DAMAGE_AIM;
814         self.bot_attack = TRUE;
815         self.iscaptured = TRUE;
816         self.islinked = TRUE;
817         self.isshielded = TRUE;
818         self.enemy.solid = SOLID_NOT;
819         self.think = onslaught_generator_delayed;
820         self.nextthink = time + 0.2;
821         setmodel(self, "models/onslaught/generator.md3");
822
823         WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
824         WaypointSprite_UpdateHealth(self.sprite, self.health);
825 }
826
827 /*QUAKED spawnfunc_onslaught_generator (0 .5 .8) (-32 -32 -24) (32 32 64)
828   Base generator.
829
830   spawnfunc_onslaught_link entities can target this.
831
832 keys:
833 "team" - team that owns this generator (5 = red, 14 = blue, etc), MUST BE SET.
834 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
835  */
836 void spawnfunc_onslaught_generator()
837 {
838         if (!g_onslaught)
839         {
840                 remove(self);
841                 return;
842         }
843
844         local entity e;
845         precache_model("models/onslaught/generator.md3");
846         precache_model("models/onslaught/generator_shield.md3");
847         precache_model("models/onslaught/generator_dmg1.md3");
848         precache_model("models/onslaught/generator_dmg2.md3");
849         precache_model("models/onslaught/generator_dmg3.md3");
850         precache_model("models/onslaught/generator_dmg4.md3");
851         precache_model("models/onslaught/generator_dmg5.md3");
852         precache_model("models/onslaught/generator_dmg6.md3");
853         precache_model("models/onslaught/generator_dmg7.md3");
854         precache_model("models/onslaught/generator_dmg8.md3");
855         precache_model("models/onslaught/generator_dmg9.md3");
856         precache_model("models/onslaught/generator_dead.md3");
857         precache_model("models/onslaught/shockwave.md3");
858         precache_model("models/onslaught/shockwavetransring.md3");
859         precache_model("models/onslaught/gen_gib1.md3");
860         precache_model("models/onslaught/gen_gib2.md3");
861         precache_model("models/onslaught/gen_gib3.md3");
862         precache_model("models/onslaught/ons_ray.md3");
863         precache_sound("onslaught/generator_decay.wav");
864         precache_sound("weapons/grenade_impact.wav");
865         precache_sound("weapons/rocket_impact.wav");
866         precache_sound("onslaught/generator_underattack.wav");
867         precache_sound("onslaught/shockwave.wav");
868         precache_sound("onslaught/ons_hit1.wav");
869         precache_sound("onslaught/ons_hit2.wav");
870         precache_sound("onslaught/electricity_explode.wav");
871         if (!self.team)
872                 objerror("team must be set");
873         self.team_saved = self.team;
874         self.colormap = 1024 + (self.team - 1) * 17;
875         self.solid = SOLID_BBOX;
876         self.movetype = MOVETYPE_NONE;
877         self.lasthealth = self.max_health = self.health = cvar("g_onslaught_gen_health");
878         setmodel(self, "models/onslaught/generator.md3");
879         setsize(self, '-52 -52 -14', '52 52 75');
880         setorigin(self, self.origin);
881         self.takedamage = DAMAGE_AIM;
882         self.bot_attack = TRUE;
883         self.event_damage = onslaught_generator_damage;
884         self.iscaptured = TRUE;
885         self.islinked = TRUE;
886         self.isshielded = TRUE;
887         // helper entity that create fx when generator is damaged
888         onslaught_generator_damage_spawn(self);
889         // spawn shield model which indicates whether this can be damaged
890         self.enemy = e = spawn();
891         e.classname = "onslaught_generator_shield";
892         e.solid = SOLID_NOT;
893         e.movetype = MOVETYPE_NONE;
894         e.effects = EF_ADDITIVE;
895         setmodel(e, "models/onslaught/generator_shield.md3");
896         setorigin(e, self.origin);
897         e.colormap = self.colormap;
898         e.team = self.team;
899         self.think = onslaught_generator_delayed;
900         self.nextthink = time + 0.2;
901         InitializeEntity(self, onslaught_generator_delayed, INITPRIO_LAST);
902
903         WaypointSprite_SpawnFixed(string_null, e.origin + '0 0 1' * e.maxs_z, self, sprite);
904         WaypointSprite_UpdateRule(self.sprite, COLOR_TEAM2, SPRITERULE_TEAMPLAY);
905         WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
906         WaypointSprite_UpdateHealth(self.sprite, self.health);
907
908         waypoint_spawnforitem(self);
909
910         onslaught_updatelinks();
911
912         self.reset = onslaught_generator_reset;
913 };
914
915 .float waslinked;
916 .float cp_bob_spd;
917 .vector cp_origin, cp_bob_origin, cp_bob_dmg;
918
919 float ons_notification_time_team1;
920 float ons_notification_time_team2;
921
922 void onslaught_controlpoint_icon_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
923 {
924         entity oself;
925         float nag;
926
927         if (damage <= 0)
928                 return;
929         if (self.owner.isshielded)
930         {
931                 // this is protected by a shield, so ignore the damage
932                 if (time > self.pain_finished)
933                         if (attacker.classname == "player")
934                         {
935                                 play2(attacker, "onslaught/damageblockedbyshield.wav");
936                                 self.pain_finished = time + 1;
937                         }
938                 return;
939         }
940
941         if (attacker.classname == "player")
942         {
943                 if(self.team == COLOR_TEAM1)
944                 {
945                         if(time - ons_notification_time_team1 > 10)
946                         {
947                                 nag = TRUE;
948                                 ons_notification_time_team1 = time;
949                         }
950                 }
951                 else if(self.team == COLOR_TEAM2)
952                 {
953                         if(time - ons_notification_time_team2 > 10)
954                         {
955                                 nag = TRUE;
956                                 ons_notification_time_team2 = time;
957                         }
958                 }
959                 else
960                         nag = TRUE;
961
962                 if(nag)
963                         play2team(self.team, "onslaught/controlpoint_underattack.wav");
964         }
965
966         self.health = self.health - damage;
967         if(self.owner.iscaptured)
968                 WaypointSprite_UpdateHealth(self.owner.sprite, self.health);
969         else
970                 WaypointSprite_UpdateBuildFinished(self.owner.sprite, time + (self.max_health - self.health) / (self.count / sys_frametime));
971         self.pain_finished = time + 1;
972         self.punchangle = (2 * randomvec() - '1 1 1') * 45;
973         self.cp_bob_dmg_z = (2 * random() - 1) * 15;
974         // colormod flash when shot
975         self.colormod = '2 2 2';
976         // particles on every hit
977         pointparticles(particleeffectnum("sparks"), hitloc, force*-1, 1);
978         //sound on every hit
979         if (random() < 0.5)
980                 sound(self, CHAN_TRIGGER, "onslaught/ons_hit1.wav", VOL_BASE+0.3, ATTN_NORM);
981         else
982                 sound(self, CHAN_TRIGGER, "onslaught/ons_hit2.wav", VOL_BASE+0.3, ATTN_NORM);
983
984         if (self.health < 0)
985         {
986                 sound(self, CHAN_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
987                 pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
988                 {
989                         string t;
990                         t = ColoredTeamName(attacker.team);
991                         bprint(ColoredTeamName(self.team), " ", self.message, " control point destroyed by ", t, "\n");
992                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 25, "models/onslaught/controlpoint_icon_gib1.md3", 3, FALSE);
993                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 45, "models/onslaught/controlpoint_icon_gib2.md3", 3, FALSE);
994                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 45, "models/onslaught/controlpoint_icon_gib2.md3", 3, FALSE);
995                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 75, "models/onslaught/controlpoint_icon_gib4.md3", 3, FALSE);
996                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 75, "models/onslaught/controlpoint_icon_gib4.md3", 3, FALSE);
997                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 75, "models/onslaught/controlpoint_icon_gib4.md3", 3, FALSE);
998                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 75, "models/onslaught/controlpoint_icon_gib4.md3", 3, FALSE);
999                 }
1000                 self.owner.goalentity = world;
1001                 self.owner.islinked = FALSE;
1002                 self.owner.iscaptured = FALSE;
1003                 self.owner.team = 0;
1004                 self.owner.colormap = 1024;
1005
1006                 WaypointSprite_UpdateMaxHealth(self.owner.sprite, 0);
1007
1008                 onslaught_updatelinks();
1009
1010                 // Use targets now (somebody make sure this is in the right place..)
1011                 oself = self;
1012                 self = self.owner;
1013                 activator = self;
1014                 SUB_UseTargets ();
1015                 self = oself;
1016
1017
1018                 self.owner.waslinked = self.owner.islinked;
1019                 if(self.owner.model != "models/onslaught/controlpoint_pad.md3")
1020                         setmodel(self.owner, "models/onslaught/controlpoint_pad.md3");
1021                 //setsize(self, '-32 -32 0', '32 32 8');
1022
1023                 remove(self);
1024         }
1025 };
1026
1027 void onslaught_controlpoint_icon_think()
1028 {
1029         entity oself;
1030         self.nextthink = time + sys_frametime;
1031         if (time > self.pain_finished + 5)
1032         {
1033                 if(self.health < self.max_health)
1034                 {
1035                         self.health = self.health + self.count;
1036                         if (self.health >= self.max_health)
1037                                 self.health = self.max_health;
1038                         WaypointSprite_UpdateHealth(self.owner.sprite, self.health);
1039                 }
1040         }
1041         if (self.health < self.max_health * 0.25)
1042                 setmodel(self, "models/onslaught/controlpoint_icon_dmg3.md3");
1043         else if (self.health < self.max_health * 0.50)
1044                 setmodel(self, "models/onslaught/controlpoint_icon_dmg2.md3");
1045         else if (self.health < self.max_health * 0.75)
1046                 setmodel(self, "models/onslaught/controlpoint_icon_dmg1.md3");
1047         else if (self.health < self.max_health * 0.90)
1048                 setmodel(self, "models/onslaught/controlpoint_icon.md3");
1049         // colormod flash when shot
1050         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
1051
1052         if(self.owner.islinked != self.owner.waslinked)
1053         {
1054                 // unteam the spawnpoint if needed
1055                 float t;
1056                 t = self.owner.team;
1057                 if(!self.owner.islinked)
1058                         self.owner.team = 0;
1059
1060                 oself = self;
1061                 self = self.owner;
1062                 activator = self;
1063                 SUB_UseTargets ();
1064                 self = oself;
1065
1066                 self.owner.team = t;
1067
1068                 self.owner.waslinked = self.owner.islinked;
1069         }
1070         if (self.punchangle_x > 2)
1071                 self.punchangle_x = self.punchangle_x - 2;
1072         else if (self.punchangle_x < -2)
1073                 self.punchangle_x = self.punchangle_x + 2;
1074         else
1075                 self.punchangle_x = 0;
1076         if (self.punchangle_y > 2)
1077                 self.punchangle_y = self.punchangle_y - 2;
1078         else if (self.punchangle_y < -2)
1079                 self.punchangle_y = self.punchangle_y + 2;
1080         else
1081                 self.punchangle_y = 0;
1082         if (self.punchangle_z > 2)
1083                 self.punchangle_z = self.punchangle_z - 2;
1084         else if (self.punchangle_z < -2)
1085                 self.punchangle_z = self.punchangle_z + 2;
1086         else
1087                 self.punchangle_z = 0;
1088         self.angles_x = self.punchangle_x;
1089         self.angles_y = self.punchangle_y + self.mangle_y;
1090         self.angles_z = self.punchangle_z;
1091         self.mangle_y = self.mangle_y + 1.5;
1092
1093         self.cp_bob_origin_z = 4 * PI * (1 - cos(self.cp_bob_spd / 8));
1094         self.cp_bob_spd = self.cp_bob_spd + 0.5;
1095         if(self.cp_bob_dmg_z > 0)
1096                 self.cp_bob_dmg_z = self.cp_bob_dmg_z - 0.1;
1097         else
1098                 self.cp_bob_dmg_z = 0;
1099         setorigin(self,self.cp_origin + self.cp_bob_origin + self.cp_bob_dmg);
1100
1101         // damaged fx
1102         if(random() < 0.6 - self.health / self.max_health)
1103         {
1104                 pointparticles(particleeffectnum("electricity_sparks"), self.origin + randompos('-10 -10 -20', '10 10 20'), '0 0 0', 1);
1105
1106                 if(random() > 0.8)
1107                         sound(self, CHAN_PAIN, "onslaught/ons_spark1.wav", VOL_BASE, ATTN_NORM);
1108                 else if (random() > 0.5)
1109                         sound(self, CHAN_PAIN, "onslaught/ons_spark2.wav", VOL_BASE, ATTN_NORM);
1110         }
1111 };
1112
1113 void onslaught_controlpoint_icon_buildthink()
1114 {
1115         local entity oself;
1116         float a;
1117
1118         self.nextthink = time + sys_frametime;
1119
1120         // only do this if there is power
1121         a = onslaught_controlpoint_can_be_linked(self.owner, self.owner.team);
1122         if(!a)
1123                 return;
1124
1125         self.health = self.health + self.count;
1126
1127         if (self.health >= self.max_health)
1128         {
1129                 self.health = self.max_health;
1130                 self.count = cvar("g_onslaught_cp_regen") * sys_frametime; // slow repair rate from now on
1131                 self.think = onslaught_controlpoint_icon_think;
1132                 sound(self, CHAN_TRIGGER, "onslaught/controlpoint_built.wav", VOL_BASE, ATTN_NORM);
1133                 bprint(ColoredTeamName(self.team), " captured ", self.owner.message, " control point\n");
1134                 self.owner.iscaptured = TRUE;
1135
1136                 WaypointSprite_UpdateMaxHealth(self.owner.sprite, self.max_health);
1137                 WaypointSprite_UpdateHealth(self.owner.sprite, self.health);
1138
1139                 onslaught_updatelinks();
1140
1141                 // Use targets now (somebody make sure this is in the right place..)
1142                 oself = self;
1143                 self = self.owner;
1144                 activator = self;
1145                 SUB_UseTargets ();
1146                 self = oself;
1147                 self.cp_origin = self.origin;
1148                 self.cp_bob_origin = '0 0 0.1';
1149                 self.cp_bob_spd = 0;
1150         }
1151         self.alpha = self.health / self.max_health;
1152         // colormod flash when shot
1153         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
1154         if(self.owner.model != "models/onslaught/controlpoint_pad2.md3")
1155                 setmodel(self.owner, "models/onslaught/controlpoint_pad2.md3");
1156         //setsize(self, '-32 -32 0', '32 32 8');
1157
1158         if(random() < 0.9 - self.health / self.max_health)
1159                 pointparticles(particleeffectnum("rage"), self.origin + 10 * randomvec(), '0 0 -1', 1);
1160 };
1161
1162
1163
1164
1165 void onslaught_controlpoint_touch()
1166 {
1167         local entity e;
1168         float a;
1169         if (other.classname != "player")
1170                 return;
1171         a = onslaught_controlpoint_attackable(self, other.team);
1172         if(a != 2 && a != 4)
1173                 return;
1174         // we've verified that this player has a legitimate claim to this point,
1175         // so start building the captured point icon (which only captures this
1176         // point if it successfully builds without being destroyed first)
1177         self.goalentity = e = spawn();
1178         e.classname = "onslaught_controlpoint_icon";
1179         e.owner = self;
1180         e.max_health = cvar("g_onslaught_cp_health");
1181         e.health = cvar("g_onslaught_cp_buildhealth");
1182         e.solid = SOLID_BBOX;
1183         e.movetype = MOVETYPE_NONE;
1184         setmodel(e, "models/onslaught/controlpoint_icon.md3");
1185         setsize(e, '-32 -32 -32', '32 32 32');
1186         setorigin(e, self.origin + '0 0 96');
1187         e.takedamage = DAMAGE_AIM;
1188         e.bot_attack = TRUE;
1189         e.event_damage = onslaught_controlpoint_icon_damage;
1190         e.team = other.team;
1191         e.colormap = 1024 + (e.team - 1) * 17;
1192         e.think = onslaught_controlpoint_icon_buildthink;
1193         e.nextthink = time + sys_frametime;
1194         e.count = (e.max_health - e.health) * sys_frametime / cvar("g_onslaught_cp_buildtime"); // how long it takes to build
1195         sound(e, CHAN_TRIGGER, "onslaught/controlpoint_build.wav", VOL_BASE, ATTN_NORM);
1196         self.team = e.team;
1197         self.colormap = e.colormap;
1198         WaypointSprite_UpdateBuildFinished(self.sprite, time + (e.max_health - e.health) / (e.count / sys_frametime));
1199         onslaught_updatelinks();
1200 };
1201
1202 void onslaught_controlpoint_reset()
1203 {
1204         if(self.goalentity && self.goalentity != world)
1205                 remove(self.goalentity);
1206         self.goalentity = world;
1207         self.team = 0;
1208         self.colormap = 1024;
1209         self.iscaptured = FALSE;
1210         self.islinked = FALSE;
1211         self.isshielded = TRUE;
1212         self.enemy.solid = SOLID_NOT;
1213         self.enemy.colormap = self.colormap;
1214         self.think = self.enemy.think = SUB_Null;
1215         self.nextthink = 0; // don't like SUB_Null :P
1216         setmodel(self, "models/onslaught/controlpoint_pad.md3");
1217         //setsize(self, '-32 -32 0', '32 32 8');
1218
1219         WaypointSprite_UpdateMaxHealth(self.sprite, 0);
1220
1221         onslaught_updatelinks();
1222
1223         activator = self;
1224         SUB_UseTargets(); // to reset the structures, playerspawns etc.
1225 }
1226
1227 /*QUAKED spawnfunc_onslaught_controlpoint (0 .5 .8) (-32 -32 0) (32 32 128)
1228   Control point. Be sure to give this enough clearance so that the shootable part has room to exist
1229
1230   This should link to an spawnfunc_onslaught_controlpoint entity or spawnfunc_onslaught_generator entity.
1231
1232 keys:
1233 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
1234 "target" - target any entities that are tied to this control point, such as vehicles and buildable structure entities.
1235 "message" - name of this control point (should reflect the location in the map, such as "center bridge", "north tower", etc)
1236  */
1237 void spawnfunc_onslaught_controlpoint()
1238 {
1239         local entity e;
1240         if (!g_onslaught)
1241         {
1242                 remove(self);
1243                 return;
1244         }
1245         precache_model("models/onslaught/controlpoint_pad.md3");
1246         precache_model("models/onslaught/controlpoint_pad2.md3");
1247         precache_model("models/onslaught/controlpoint_shield.md3");
1248         precache_model("models/onslaught/controlpoint_icon.md3");
1249         precache_model("models/onslaught/controlpoint_icon_dmg1.md3");
1250         precache_model("models/onslaught/controlpoint_icon_dmg2.md3");
1251         precache_model("models/onslaught/controlpoint_icon_dmg3.md3");
1252         precache_model("models/onslaught/controlpoint_icon_gib1.md3");
1253         precache_model("models/onslaught/controlpoint_icon_gib2.md3");
1254         precache_model("models/onslaught/controlpoint_icon_gib4.md3");
1255         precache_sound("onslaught/controlpoint_build.wav");
1256         precache_sound("onslaught/controlpoint_built.wav");
1257         precache_sound("weapons/grenade_impact.wav");
1258         precache_sound("onslaught/damageblockedbyshield.wav");
1259         precache_sound("onslaught/controlpoint_underattack.wav");
1260         precache_sound("onslaught/ons_spark1.wav");
1261         precache_sound("onslaught/ons_spark2.wav");
1262         self.solid = SOLID_BBOX;
1263         self.movetype = MOVETYPE_NONE;
1264         setmodel(self, "models/onslaught/controlpoint_pad.md3");
1265         //setsize(self, '-32 -32 0', '32 32 8');
1266         setorigin(self, self.origin);
1267         self.touch = onslaught_controlpoint_touch;
1268         self.team = 0;
1269         self.colormap = 1024;
1270         self.iscaptured = FALSE;
1271         self.islinked = FALSE;
1272         self.isshielded = TRUE;
1273         // spawn shield model which indicates whether this can be damaged
1274         self.enemy = e = spawn();
1275         e.classname = "onslaught_controlpoint_shield";
1276         e.solid = SOLID_NOT;
1277         e.movetype = MOVETYPE_NONE;
1278         e.effects = EF_ADDITIVE;
1279         setmodel(e, "models/onslaught/controlpoint_shield.md3");
1280         //setsize(e, '-32 -32 0', '32 32 128');
1281         setorigin(e, self.origin);
1282         e.colormap = self.colormap;
1283
1284         waypoint_spawnforitem(self);
1285
1286         WaypointSprite_SpawnFixed(string_null, e.origin + '0 0 1' * e.maxs_z, self, sprite);
1287         WaypointSprite_UpdateRule(self.sprite, COLOR_TEAM2, SPRITERULE_TEAMPLAY);
1288
1289         onslaught_updatelinks();
1290
1291         self.reset = onslaught_controlpoint_reset;
1292 };
1293
1294 float onslaught_link_send(entity to, float sendflags)
1295 {
1296         WriteByte(MSG_ENTITY, ENT_CLIENT_RADARLINK);
1297         WriteByte(MSG_ENTITY, sendflags);
1298         if(sendflags & 1)
1299         {
1300                 WriteCoord(MSG_ENTITY, self.goalentity.origin_x);
1301                 WriteCoord(MSG_ENTITY, self.goalentity.origin_y);
1302                 WriteCoord(MSG_ENTITY, self.goalentity.origin_z);
1303         }
1304         if(sendflags & 2)
1305         {
1306                 WriteCoord(MSG_ENTITY, self.enemy.origin_x);
1307                 WriteCoord(MSG_ENTITY, self.enemy.origin_y);
1308                 WriteCoord(MSG_ENTITY, self.enemy.origin_z);
1309         }
1310         if(sendflags & 4)
1311         {
1312                 WriteByte(MSG_ENTITY, self.clientcolors); // which is goalentity's color + enemy's color * 16
1313         }
1314         return TRUE;
1315 }
1316
1317 void onslaught_link_checkupdate()
1318 {
1319         // TODO check if the two sides have moved (currently they won't move anyway)
1320         float redpower, bluepower;
1321
1322         redpower = bluepower = 0;
1323         if(self.goalentity.islinked)
1324         {
1325                 if(self.goalentity.team == COLOR_TEAM1)
1326                         redpower = 1;
1327                 else if(self.goalentity.team == COLOR_TEAM2)
1328                         bluepower = 1;
1329         }
1330         if(self.enemy.islinked)
1331         {
1332                 if(self.enemy.team == COLOR_TEAM1)
1333                         redpower = 2;
1334                 else if(self.enemy.team == COLOR_TEAM2)
1335                         bluepower = 2;
1336         }
1337
1338         float cc;
1339         if(redpower == 1 && bluepower == 2)
1340                 cc = (COLOR_TEAM1 - 1) * 0x01 + (COLOR_TEAM2 - 1) * 0x10;
1341         else if(redpower == 2 && bluepower == 1)
1342                 cc = (COLOR_TEAM1 - 1) * 0x10 + (COLOR_TEAM2 - 1) * 0x01;
1343         else if(redpower)
1344                 cc = (COLOR_TEAM1 - 1) * 0x11;
1345         else if(bluepower)
1346                 cc = (COLOR_TEAM2 - 1) * 0x11;
1347         else
1348                 cc = 0;
1349
1350         //print(etos(self), " rp=", ftos(redpower), " bp=", ftos(bluepower), " ");
1351         //print("cc=", ftos(cc), "\n");
1352
1353         if(cc != self.clientcolors)
1354         {
1355                 self.clientcolors = cc;
1356                 self.SendFlags |= 4;
1357         }
1358
1359         self.nextthink = time;
1360 }
1361
1362 void onslaught_link_delayed()
1363 {
1364         self.goalentity = find(world, targetname, self.target);
1365         self.enemy = find(world, targetname, self.target2);
1366         if (!self.goalentity)
1367                 objerror("can not find target\n");
1368         if (!self.enemy)
1369                 objerror("can not find target2\n");
1370         dprint(etos(self.goalentity), " linked with ", etos(self.enemy), "\n");
1371         self.SendFlags |= 3;
1372         self.think = onslaught_link_checkupdate;
1373         self.nextthink = time;
1374 }
1375
1376 /*QUAKED spawnfunc_onslaught_link (0 .5 .8) (-16 -16 -16) (16 16 16)
1377   Link between control points.
1378
1379   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.
1380
1381 keys:
1382 "target" - first control point.
1383 "target2" - second control point.
1384  */
1385 void spawnfunc_onslaught_link()
1386 {
1387         if (!g_onslaught)
1388         {
1389                 remove(self);
1390                 return;
1391         }
1392         if (self.target == "" || self.target2 == "")
1393                 objerror("target and target2 must be set\n");
1394         InitializeEntity(self, onslaught_link_delayed, INITPRIO_FINDTARGET);
1395         Net_LinkEntity(self, FALSE, 0, onslaught_link_send);
1396 };