]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_onslaught.qc
Merge branch 'TimePath/experiments/csqc_prediction' into Mario/qc_physics
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_onslaught.qc
1 #include "../../common/triggers/subs.qh"
2
3 float autocvar_g_onslaught_spawn_at_controlpoints;
4 float autocvar_g_onslaught_spawn_at_generator;
5 float autocvar_g_onslaught_cp_proxydecap;
6 float autocvar_g_onslaught_cp_proxydecap_distance = 512;
7 float autocvar_g_onslaught_cp_proxydecap_dps = 100;
8
9 void onslaught_generator_updatesprite(entity e);
10 void onslaught_controlpoint_updatesprite(entity e);
11 void onslaught_link_checkupdate();
12
13 .entity sprite;
14 .string target2;
15 .float iscaptured;
16 .float islinked;
17 .float isgenneighbor_red;
18 .float isgenneighbor_blue;
19 .float iscpneighbor_red;
20 .float iscpneighbor_blue;
21 .float isshielded;
22 .float lasthealth;
23 .float lastteam;
24 .float lastshielded;
25 .float lastcaptured;
26
27 entity ons_red_generator;
28 entity ons_blue_generator;
29
30 void ons_gib_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
31 {
32         self.velocity = self.velocity + vforce;
33 }
34
35 .float giblifetime;
36 void ons_throwgib_think()
37 {
38         float d;
39
40         self.nextthink = time + 0.05;
41
42         d = self.giblifetime - time;
43
44         if(d<0)
45         {
46                 self.think = SUB_Remove;
47                 return;
48         }
49         if(d<1)
50                 self.alpha = d;
51
52         if(d>2)
53         if(random()<0.6)
54                 pointparticles(particleeffectnum("onslaught_generator_gib_flame"), self.origin, '0 0 0', 1);
55 }
56
57 void ons_throwgib(vector v_from, vector v_to, string smodel, float f_lifetime, float b_burn)
58 {
59         entity gib;
60
61         gib = spawn();
62
63         setmodel(gib, smodel);
64         setorigin(gib, v_from);
65         gib.solid = SOLID_BBOX;
66         gib.movetype = MOVETYPE_BOUNCE;
67         gib.takedamage = DAMAGE_YES;
68         gib.event_damage = ons_gib_damage;
69         gib.health = -1;
70         gib.effects = EF_LOWPRECISION;
71         gib.flags = FL_NOTARGET;
72         gib.velocity = v_to;
73         gib.giblifetime = time + f_lifetime;
74
75         if (b_burn)
76         {
77                 gib.think = ons_throwgib_think;
78                 gib.nextthink = time + 0.05;
79         }
80         else
81                 SUB_SetFade(gib, gib.giblifetime, 2);
82 }
83
84 void onslaught_updatelinks()
85 {
86         entity l, links;
87         float stop, t1, t2, t3, t4;
88         // first check if the game has ended
89         dprint("--- updatelinks ---\n");
90         links = findchain(classname, "onslaught_link");
91         // mark generators as being shielded and networked
92         l = findchain(classname, "onslaught_generator");
93         while (l)
94         {
95                 if (l.iscaptured)
96                         dprint(etos(l), " (generator) belongs to team ", ftos(l.team), "\n");
97                 else
98                         dprint(etos(l), " (generator) is destroyed\n");
99                 l.islinked = l.iscaptured;
100                 l.isshielded = l.iscaptured;
101                 l = l.chain;
102         }
103         // mark points as shielded and not networked
104         l = findchain(classname, "onslaught_controlpoint");
105         while (l)
106         {
107                 l.islinked = false;
108                 l.isshielded = true;
109                 l.isgenneighbor_red = false;
110                 l.isgenneighbor_blue = false;
111                 l.iscpneighbor_red = false;
112                 l.iscpneighbor_blue = false;
113                 dprint(etos(l), " (point) belongs to team ", ftos(l.team), "\n");
114                 l = l.chain;
115         }
116         // flow power outward from the generators through the network
117         l = links;
118         while (l)
119         {
120                 dprint(etos(l), " (link) connects ", etos(l.goalentity), " with ", etos(l.enemy), "\n");
121                 l = l.chain;
122         }
123         stop = false;
124         while (!stop)
125         {
126                 stop = true;
127                 l = links;
128                 while (l)
129                 {
130                         // if both points are captured by the same team, and only one of
131                         // them is powered, mark the other one as powered as well
132                         if (l.enemy.iscaptured && l.goalentity.iscaptured)
133                                 if (l.enemy.islinked != l.goalentity.islinked)
134                                         if (l.enemy.team == l.goalentity.team)
135                                         {
136                                                 if (!l.goalentity.islinked)
137                                                 {
138                                                         stop = false;
139                                                         l.goalentity.islinked = true;
140                                                         dprint(etos(l), " (link) is marking ", etos(l.goalentity), " (point) because its team matches ", etos(l.enemy), " (point)\n");
141                                                 }
142                                                 else if (!l.enemy.islinked)
143                                                 {
144                                                         stop = false;
145                                                         l.enemy.islinked = true;
146                                                         dprint(etos(l), " (link) is marking ", etos(l.enemy), " (point) because its team matches ", etos(l.goalentity), " (point)\n");
147                                                 }
148                                         }
149                         l = l.chain;
150                 }
151         }
152         // now that we know which points are powered we can mark their neighbors
153         // as unshielded if team differs
154         l = links;
155         while (l)
156         {
157                 if (l.goalentity.islinked)
158                 {
159                         if (l.goalentity.team != l.enemy.team)
160                         {
161                                 dprint(etos(l), " (link) is unshielding ", etos(l.enemy), " (point) because its team does not match ", etos(l.goalentity), " (point)\n");
162                                 l.enemy.isshielded = false;
163                         }
164                         if(l.goalentity.classname == "onslaught_generator")
165                         {
166                                 if(l.goalentity.team == NUM_TEAM_1)
167                                         l.enemy.isgenneighbor_red = true;
168                                 else if(l.goalentity.team == NUM_TEAM_2)
169                                         l.enemy.isgenneighbor_blue = true;
170                         }
171                         else
172                         {
173                                 if(l.goalentity.team == NUM_TEAM_1)
174                                         l.enemy.iscpneighbor_red = true;
175                                 else if(l.goalentity.team == NUM_TEAM_2)
176                                         l.enemy.iscpneighbor_blue = true;
177                         }
178                 }
179                 if (l.enemy.islinked)
180                 {
181                         if (l.goalentity.team != l.enemy.team)
182                         {
183                                 dprint(etos(l), " (link) is unshielding ", etos(l.goalentity), " (point) because its team does not match ", etos(l.enemy), " (point)\n");
184                                 l.goalentity.isshielded = false;
185                         }
186                         if(l.enemy.classname == "onslaught_generator")
187                         {
188                                 if(l.enemy.team == NUM_TEAM_1)
189                                         l.goalentity.isgenneighbor_red = true;
190                                 else if(l.enemy.team == NUM_TEAM_2)
191                                         l.goalentity.isgenneighbor_blue = true;
192                         }
193                         else
194                         {
195                                 if(l.enemy.team == NUM_TEAM_1)
196                                         l.goalentity.iscpneighbor_red = true;
197                                 else if(l.enemy.team == NUM_TEAM_2)
198                                         l.goalentity.iscpneighbor_blue = true;
199                         }
200                 }
201                 l = l.chain;
202         }
203         // now update the takedamage and alpha variables on generator shields
204         l = findchain(classname, "onslaught_generator");
205         while (l)
206         {
207                 if (l.isshielded)
208                 {
209                         dprint(etos(l), " (generator) is shielded\n");
210                         l.enemy.alpha = 1;
211                         l.takedamage = DAMAGE_NO;
212                         l.bot_attack = false;
213                 }
214                 else
215                 {
216                         dprint(etos(l), " (generator) is not shielded\n");
217                         l.enemy.alpha = -1;
218                         l.takedamage = DAMAGE_AIM;
219                         l.bot_attack = true;
220                 }
221                 l = l.chain;
222         }
223         // now update the takedamage and alpha variables on control point icons
224         l = findchain(classname, "onslaught_controlpoint");
225         while (l)
226         {
227                 if (l.isshielded)
228                 {
229                         dprint(etos(l), " (point) is shielded\n");
230                         l.enemy.alpha = 1;
231                         if (l.goalentity)
232                         {
233                                 l.goalentity.takedamage = DAMAGE_NO;
234                                 l.goalentity.bot_attack = false;
235                         }
236                 }
237                 else
238                 {
239                         dprint(etos(l), " (point) is not shielded\n");
240                         l.enemy.alpha = -1;
241                         if (l.goalentity)
242                         {
243                                 l.goalentity.takedamage = DAMAGE_AIM;
244                                 l.goalentity.bot_attack = true;
245                         }
246                 }
247                 onslaught_controlpoint_updatesprite(l);
248                 l = l.chain;
249         }
250         // count generators owned by each team
251         t1 = t2 = t3 = t4 = 0;
252         l = findchain(classname, "onslaught_generator");
253         while (l)
254         {
255                 if (l.iscaptured)
256                 {
257                         if (l.team == NUM_TEAM_1) t1 = 1;
258                         if (l.team == NUM_TEAM_2) t2 = 1;
259                         if (l.team == NUM_TEAM_3) t3 = 1;
260                         if (l.team == NUM_TEAM_4) t4 = 1;
261                 }
262                 onslaught_generator_updatesprite(l);
263                 l = l.chain;
264         }
265         // see if multiple teams remain (if not, it's game over)
266         if (t1 + t2 + t3 + t4 < 2)
267                 dprint("--- game over ---\n");
268         else
269                 dprint("--- done updating links ---\n");
270 }
271
272 float onslaught_controlpoint_can_be_linked(entity cp, float t)
273 {
274         if(t == NUM_TEAM_1)
275         {
276                 if(cp.isgenneighbor_red)
277                         return 2;
278                 if(cp.iscpneighbor_red)
279                         return 1;
280         }
281         else if(t == NUM_TEAM_2)
282         {
283                 if(cp.isgenneighbor_blue)
284                         return 2;
285                 if(cp.iscpneighbor_blue)
286                         return 1;
287         }
288         return 0;
289         /*
290            entity e;
291         // check to see if this player has a legitimate claim to capture this
292         // control point - more specifically that there is a captured path of
293         // points leading back to the team generator
294         e = findchain(classname, "onslaught_link");
295         while (e)
296         {
297         if (e.goalentity == cp)
298         {
299         dprint(etos(e), " (link) connects to ", etos(e.enemy), " (point)");
300         if (e.enemy.islinked)
301         {
302         dprint(" which is linked");
303         if (e.enemy.team == t)
304         {
305         dprint(" and has the correct team!\n");
306         return 1;
307         }
308         else
309         dprint(" but has the wrong team\n");
310         }
311         else
312         dprint("\n");
313         }
314         else if (e.enemy == cp)
315         {
316         dprint(etos(e), " (link) connects to ", etos(e.goalentity), " (point)");
317         if (e.goalentity.islinked)
318         {
319         dprint(" which is linked");
320         if (e.goalentity.team == t)
321         {
322         dprint(" and has a team!\n");
323         return 1;
324         }
325         else
326         dprint(" but has the wrong team\n");
327         }
328         else
329         dprint("\n");
330         }
331         e = e.chain;
332         }
333         return 0;
334          */
335 }
336
337 float onslaught_controlpoint_attackable(entity cp, float t)
338         // -2: SAME TEAM, attackable by enemy!
339         // -1: SAME TEAM!
340         // 0: off limits
341         // 1: attack it
342         // 2: touch it
343         // 3: attack it (HIGH PRIO)
344         // 4: touch it (HIGH PRIO)
345 {
346         float a;
347
348         if(cp.isshielded)
349         {
350                 return 0;
351         }
352         else if(cp.goalentity)
353         {
354                 // if there's already an icon built, nothing happens
355                 if(cp.team == t)
356                 {
357                         a = onslaught_controlpoint_can_be_linked(cp, NUM_TEAM_1 + NUM_TEAM_2 - t);
358                         if(a) // attackable by enemy?
359                                 return -2; // EMERGENCY!
360                         return -1;
361                 }
362                 // we know it can be linked, so no need to check
363                 // but...
364                 a = onslaught_controlpoint_can_be_linked(cp, t);
365                 if(a == 2) // near our generator?
366                         return 3; // EMERGENCY!
367                 return 1;
368         }
369         else
370         {
371                 // free point
372                 if(onslaught_controlpoint_can_be_linked(cp, t))
373                 {
374                         a = onslaught_controlpoint_can_be_linked(cp, NUM_TEAM_1 + NUM_TEAM_2 - t);
375                         if(a == 2)
376                                 return 4; // GET THIS ONE NOW!
377                         else
378                                 return 2; // TOUCH ME
379                 }
380         }
381         return 0;
382 }
383
384 float overtime_msg_time;
385 void onslaught_generator_think()
386 {
387         float d;
388         entity e;
389         self.nextthink = ceil(time + 1);
390         if (!gameover)
391         {
392                 if (autocvar_timelimit && time > game_starttime + autocvar_timelimit * 60)
393                 {
394                         if (!overtime_msg_time)
395                         {
396                                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_OVERTIME_CONTROLPOINT);
397                                 overtime_msg_time = time;
398                         }
399                         // self.max_health / 300 gives 5 minutes of overtime.
400                         // control points reduce the overtime duration.
401                         sound(self, CH_TRIGGER, "onslaught/generator_decay.wav", VOL_BASE, ATTEN_NORM);
402                         d = 1;
403                         e = findchain(classname, "onslaught_controlpoint");
404                         while (e)
405                         {
406                                 if (e.team != self.team)
407                                         if (e.islinked)
408                                                 d = d + 1;
409                                 e = e.chain;
410                         }
411
412                         if(autocvar_g_campaign && autocvar__campaign_testrun)
413                                 d = d * self.max_health;
414                         else
415                                 d = d * self.max_health / max(30, 60 * autocvar_timelimit_suddendeath);
416
417                         Damage(self, self, self, d, DEATH_HURTTRIGGER, self.origin, '0 0 0');
418                 }
419                 else if (overtime_msg_time)
420                         overtime_msg_time = 0;
421
422         if(!self.isshielded && self.wait < time)
423         {
424             self.wait = time + 5;
425             FOR_EACH_REALPLAYER(e)
426             {
427                                 if(SAME_TEAM(e, self))
428                 {
429                                         Send_Notification(NOTIF_ONE, e, MSG_CENTER, CENTER_ONS_NOTSHIELDED);
430                     soundto(MSG_ONE, e, CHAN_AUTO, "kh/alarm.wav", VOL_BASE, ATTEN_NONE);    // FIXME: Uniqe sound?
431                 }
432             }
433         }
434         }
435 }
436
437 void onslaught_generator_ring_spawn(vector org)
438 {
439         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);
440 }
441
442 void onslaught_generator_ray_think()
443 {
444         self.nextthink = time + 0.05;
445         if(self.count > 10)
446         {
447                 self.think = SUB_Remove;
448                 return;
449         }
450
451         if(self.count > 5)
452                 self.alpha -= 0.1;
453         else
454                 self.alpha += 0.1;
455
456         self.scale += 0.2;
457         self.count +=1;
458 }
459
460 void onslaught_generator_ray_spawn(vector org)
461 {
462         entity e;
463         e = spawn();
464         setmodel(e, "models/onslaught/ons_ray.md3");
465         setorigin(e, org);
466         e.angles = randomvec() * 360;
467         e.alpha = 0;
468         e.scale = random() * 5 + 8;
469         e.think = onslaught_generator_ray_think;
470         e.nextthink = time + 0.05;
471 }
472
473 void onslaught_generator_shockwave_spawn(vector org)
474 {
475         shockwave_spawn("models/onslaught/shockwave.md3", org, -64, 0.75, 0.5);
476 }
477
478 void onslaught_generator_damage_think()
479 {
480         if(self.owner.health < 0)
481         {
482                 self.think = SUB_Remove;
483                 return;
484         }
485         self.nextthink = time+0.1;
486
487         // damaged fx (less probable the more damaged is the generator)
488         if(random() < 0.9 - self.owner.health / self.owner.max_health)
489                 if(random() < 0.01)
490                 {
491                         pointparticles(particleeffectnum("electro_ballexplode"), self.origin + randompos('-50 -50 -20', '50 50 50'), '0 0 0', 1);
492                         sound(self, CH_TRIGGER, "onslaught/electricity_explode.wav", VOL_BASE, ATTEN_NORM);
493                 }
494                 else
495                         pointparticles(particleeffectnum("torch_small"), self.origin + randompos('-60 -60 -20', '60 60 60'), '0 0 0', 1);
496 }
497
498 void onslaught_generator_damage_spawn(entity gd_owner)
499 {
500         entity e;
501         e = spawn();
502         e.owner = gd_owner;
503         e.health = self.owner.health;
504         setorigin(e, gd_owner.origin);
505         e.think = onslaught_generator_damage_think;
506         e.nextthink = time+1;
507 }
508
509 void onslaught_generator_deaththink()
510 {
511         vector org;
512         float i;
513
514         if (!self.count)
515                 self.count = 40;
516
517         // White shockwave
518         if(self.count==40||self.count==20)
519         {
520                 onslaught_generator_ring_spawn(self.origin);
521                 sound(self, CH_TRIGGER, "onslaught/shockwave.wav", VOL_BASE, ATTEN_NORM);
522         }
523
524         // Throw some gibs
525         if(random() < 0.3)
526         {
527                 i = random();
528                 if(i < 0.3)
529                         ons_throwgib(self.origin + '0 0 40', (100 * randomvec() - '1 1 1') * 11 + '0 0 20', "models/onslaught/gen_gib1.md3", 6, true);
530                 else if(i > 0.7)
531                         ons_throwgib(self.origin + '0 0 40', (100 * randomvec() - '1 1 1') * 12 + '0 0 20', "models/onslaught/gen_gib2.md3", 6, true);
532                 else
533                         ons_throwgib(self.origin + '0 0 40', (100 * randomvec() - '1 1 1') * 13 + '0 0 20', "models/onslaught/gen_gib3.md3", 6, true);
534         }
535
536         // Spawn fire balls
537         for(i=0;i < 10;++i)
538         {
539                 org = self.origin + randompos('-30 -30 -30' * i + '0 0 -20', '30 30 30' * i + '0 0 20');
540                 pointparticles(particleeffectnum("onslaught_generator_gib_explode"), org, '0 0 0', 1);
541         }
542
543         // Short explosion sound + small explosion
544         if(random() < 0.25)
545         {
546                 te_explosion(self.origin);
547                 sound(self, CH_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTEN_NORM);
548         }
549
550         // Particles
551         org = self.origin + randompos(self.mins + '8 8 8', self.maxs + '-8 -8 -8');
552         pointparticles(particleeffectnum("onslaught_generator_smallexplosion"), org, '0 0 0', 1);
553
554         // rays
555         if(random() > 0.25 )
556         {
557                 onslaught_generator_ray_spawn(self.origin);
558         }
559
560         // Final explosion
561         if(self.count==1)
562         {
563                 org = self.origin;
564                 te_explosion(org);
565                 onslaught_generator_shockwave_spawn(org);
566                 pointparticles(particleeffectnum("onslaught_generator_finalexplosion"), org, '0 0 0', 1);
567                 sound(self, CH_TRIGGER, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
568         }
569         else
570                 self.nextthink = time + 0.05;
571
572         self.count = self.count - 1;
573 }
574
575 void onslaught_generator_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
576 {
577         float i;
578         if (damage <= 0)
579                 return;
580         if(warmup_stage)
581                 return;
582         if (attacker != self)
583         {
584                 if (self.isshielded)
585                 {
586                         // this is protected by a shield, so ignore the damage
587                         if (time > self.pain_finished)
588                                 if (IS_PLAYER(attacker))
589                                 {
590                                         play2(attacker, "onslaught/damageblockedbyshield.wav");
591                                         self.pain_finished = time + 1;
592                                 }
593                         return;
594                 }
595                 if (time > self.pain_finished)
596                 {
597                         self.pain_finished = time + 10;
598                         bprint(Team_ColoredFullName(self.team), " generator under attack!\n");
599                         play2team(self.team, "onslaught/generator_underattack.wav");
600                 }
601         }
602         self.health = self.health - damage;
603         WaypointSprite_UpdateHealth(self.sprite, self.health);
604         // choose an animation frame based on health
605         self.frame = 10 * bound(0, (1 - self.health / self.max_health), 1);
606         // see if the generator is still functional, or dying
607         if (self.health > 0)
608         {
609 #ifdef ONSLAUGHT_SPAM
610                 float h, lh;
611                 lh = ceil(self.lasthealth / 100) * 100;
612                 h = ceil(self.health / 100) * 100;
613                 if(lh != h)
614                         bprint(Team_ColoredFullName(self.team), " generator has less than ", ftos(h), " health remaining\n");
615 #endif
616                 self.lasthealth = self.health;
617         }
618         else if (!warmup_stage)
619         {
620                 if (attacker == self)
621                         bprint(Team_ColoredFullName(self.team), " generator spontaneously exploded due to overtime!\n");
622                 else
623                 {
624                         string t;
625                         t = Team_ColoredFullName(attacker.team);
626                         bprint(Team_ColoredFullName(self.team), " generator destroyed by ", t, "!\n");
627                 }
628                 self.iscaptured = false;
629                 self.islinked = false;
630                 self.isshielded = false;
631                 self.takedamage = DAMAGE_NO; // can't be hurt anymore
632                 self.event_damage = func_null; // won't do anything if hurt
633                 self.count = 0; // reset counter
634                 self.think = onslaught_generator_deaththink; // explosion sequence
635                 self.nextthink = time; // start exploding immediately
636                 self.think(); // do the first explosion now
637
638                 WaypointSprite_UpdateMaxHealth(self.sprite, 0);
639
640                 onslaught_updatelinks();
641         }
642
643         if(self.health <= 0)
644                 setmodel(self, "models/onslaught/generator_dead.md3");
645         else if(self.health < self.max_health * 0.10)
646                 setmodel(self, "models/onslaught/generator_dmg9.md3");
647         else if(self.health < self.max_health * 0.20)
648                 setmodel(self, "models/onslaught/generator_dmg8.md3");
649         else if(self.health < self.max_health * 0.30)
650                 setmodel(self, "models/onslaught/generator_dmg7.md3");
651         else if(self.health < self.max_health * 0.40)
652                 setmodel(self, "models/onslaught/generator_dmg6.md3");
653         else if(self.health < self.max_health * 0.50)
654                 setmodel(self, "models/onslaught/generator_dmg5.md3");
655         else if(self.health < self.max_health * 0.60)
656                 setmodel(self, "models/onslaught/generator_dmg4.md3");
657         else if(self.health < self.max_health * 0.70)
658                 setmodel(self, "models/onslaught/generator_dmg3.md3");
659         else if(self.health < self.max_health * 0.80)
660                 setmodel(self, "models/onslaught/generator_dmg2.md3");
661         else if(self.health < self.max_health * 0.90)
662                 setmodel(self, "models/onslaught/generator_dmg1.md3");
663         setsize(self, '-52 -52 -14', '52 52 75');
664
665         // Throw some flaming gibs on damage, more damage = more chance for gib
666         if(random() < damage/220)
667         {
668                 sound(self, CH_TRIGGER, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
669                 i = random();
670                 if(i < 0.3)
671                         ons_throwgib(hitloc + '0 0 20', force * -1, "models/onslaught/gen_gib1.md3", 5, true);
672                 else if(i > 0.7)
673                         ons_throwgib(hitloc + '0 0 20', force * -1, "models/onslaught/gen_gib2.md3", 5, true);
674                 else
675                         ons_throwgib(hitloc + '0 0 20', force * -1, "models/onslaught/gen_gib3.md3", 5, true);
676         }
677         else
678         {
679                 // particles on every hit
680                 pointparticles(particleeffectnum("sparks"), hitloc, force * -1, 1);
681
682                 //sound on every hit
683                 if (random() < 0.5)
684                         sound(self, CH_TRIGGER, "onslaught/ons_hit1.wav", VOL_BASE, ATTEN_NORM);
685                 else
686                         sound(self, CH_TRIGGER, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM);
687         }
688
689         //throw some gibs on damage
690         if(random() < damage/200+0.2)
691                 if(random() < 0.5)
692                         ons_throwgib(hitloc + '0 0 20', randomvec()*360, "models/onslaught/gen_gib1.md3", 5, false);
693 }
694
695 // update links after a delay
696 void onslaught_generator_delayed()
697 {
698         onslaught_updatelinks();
699         // now begin normal thinking
700         self.think = onslaught_generator_think;
701         self.nextthink = time;
702 }
703
704 string onslaught_generator_waypointsprite_for_team(entity e, float t)
705 {
706         if(t == e.team)
707         {
708                 if(e.team == NUM_TEAM_1)
709                         return "ons-gen-red";
710                 else if(e.team == NUM_TEAM_2)
711                         return "ons-gen-blue";
712         }
713         if(e.isshielded)
714                 return "ons-gen-shielded";
715         if(e.team == NUM_TEAM_1)
716                 return "ons-gen-red";
717         else if(e.team == NUM_TEAM_2)
718                 return "ons-gen-blue";
719         return "";
720 }
721
722 void onslaught_generator_updatesprite(entity e)
723 {
724         string s1, s2, s3;
725         s1 = onslaught_generator_waypointsprite_for_team(e, NUM_TEAM_1);
726         s2 = onslaught_generator_waypointsprite_for_team(e, NUM_TEAM_2);
727         s3 = onslaught_generator_waypointsprite_for_team(e, -1);
728         WaypointSprite_UpdateSprites(e.sprite, s1, s2, s3);
729
730         if(e.lastteam != e.team + 2 || e.lastshielded != e.isshielded)
731         {
732                 e.lastteam = e.team + 2;
733                 e.lastshielded = e.isshielded;
734                 if(e.lastshielded)
735                 {
736                         if(e.team == NUM_TEAM_1 || e.team == NUM_TEAM_2)
737                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, 0.5 * colormapPaletteColor(e.team - 1, false));
738                         else
739                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.5 0.5 0.5');
740                 }
741                 else
742                 {
743                         if(e.team == NUM_TEAM_1 || e.team == NUM_TEAM_2)
744                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, colormapPaletteColor(e.team - 1, false));
745                         else
746                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.75 0.75 0.75');
747                 }
748                 WaypointSprite_Ping(e.sprite);
749         }
750 }
751
752 string onslaught_controlpoint_waypointsprite_for_team(entity e, float t)
753 {
754         float a;
755         if(t != -1)
756         {
757                 a = onslaught_controlpoint_attackable(e, t);
758                 if(a == 3 || a == 4) // ATTACK/TOUCH THIS ONE NOW
759                 {
760                         if(e.team == NUM_TEAM_1)
761                                 return "ons-cp-atck-red";
762                         else if(e.team == NUM_TEAM_2)
763                                 return "ons-cp-atck-blue";
764                         else
765                                 return "ons-cp-atck-neut";
766                 }
767                 else if(a == -2) // DEFEND THIS ONE NOW
768                 {
769                         if(e.team == NUM_TEAM_1)
770                                 return "ons-cp-dfnd-red";
771                         else if(e.team == NUM_TEAM_2)
772                                 return "ons-cp-dfnd-blue";
773                 }
774                 else if(e.team == t || a == -1 || a == 1) // own point, or fire at it
775                 {
776                         if(e.team == NUM_TEAM_1)
777                                 return "ons-cp-red";
778                         else if(e.team == NUM_TEAM_2)
779                                 return "ons-cp-blue";
780                 }
781                 else if(a == 2) // touch it
782                         return "ons-cp-neut";
783         }
784         else
785         {
786                 if(e.team == NUM_TEAM_1)
787                         return "ons-cp-red";
788                 else if(e.team == NUM_TEAM_2)
789                         return "ons-cp-blue";
790                 else
791                         return "ons-cp-neut";
792         }
793         return "";
794 }
795
796 void onslaught_controlpoint_updatesprite(entity e)
797 {
798         string s1, s2, s3;
799         s1 = onslaught_controlpoint_waypointsprite_for_team(e, NUM_TEAM_1);
800         s2 = onslaught_controlpoint_waypointsprite_for_team(e, NUM_TEAM_2);
801         s3 = onslaught_controlpoint_waypointsprite_for_team(e, -1);
802         WaypointSprite_UpdateSprites(e.sprite, s1, s2, s3);
803
804         float sh;
805         sh = !(onslaught_controlpoint_can_be_linked(e, NUM_TEAM_1) || onslaught_controlpoint_can_be_linked(e, NUM_TEAM_2));
806
807         if(e.lastteam != e.team + 2 || e.lastshielded != sh || e.iscaptured != e.lastcaptured)
808         {
809                 if(e.iscaptured) // don't mess up build bars!
810                 {
811                         if(sh)
812                         {
813                                 WaypointSprite_UpdateMaxHealth(e.sprite, 0);
814                         }
815                         else
816                         {
817                                 WaypointSprite_UpdateMaxHealth(e.sprite, e.goalentity.max_health);
818                                 WaypointSprite_UpdateHealth(e.sprite, e.goalentity.health);
819                         }
820                 }
821                 if(e.lastshielded)
822                 {
823                         if(e.team == NUM_TEAM_1 || e.team == NUM_TEAM_2)
824                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, 0.5 * colormapPaletteColor(e.team - 1, false));
825                         else
826                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.5 0.5 0.5');
827                 }
828                 else
829                 {
830                         if(e.team == NUM_TEAM_1 || e.team == NUM_TEAM_2)
831                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, colormapPaletteColor(e.team - 1, false));
832                         else
833                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.75 0.75 0.75');
834                 }
835                 WaypointSprite_Ping(e.sprite);
836
837                 e.lastteam = e.team + 2;
838                 e.lastshielded = sh;
839                 e.lastcaptured = e.iscaptured;
840         }
841 }
842
843 void onslaught_generator_reset()
844 {
845         self.team = self.team_saved;
846         self.lasthealth = self.max_health = self.health = autocvar_g_onslaught_gen_health;
847         self.takedamage = DAMAGE_AIM;
848         self.bot_attack = true;
849         self.iscaptured = true;
850         self.islinked = true;
851         self.isshielded = true;
852         self.enemy.solid = SOLID_NOT;
853         self.think = onslaught_generator_delayed;
854         self.nextthink = time + 0.2;
855         setmodel(self, "models/onslaught/generator.md3");
856         setsize(self, '-52 -52 -14', '52 52 75');
857
858         if(!self.noalign)
859         {
860                 setorigin(self, self.origin + '0 0 20');
861                 droptofloor();
862         }
863
864         WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
865         WaypointSprite_UpdateHealth(self.sprite, self.health);
866 }
867
868 /*QUAKED spawnfunc_onslaught_generator (0 .5 .8) (-32 -32 -24) (32 32 64)
869   Base generator.
870
871   spawnfunc_onslaught_link entities can target this.
872
873 keys:
874 "team" - team that owns this generator (5 = red, 14 = blue, etc), MUST BE SET.
875 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
876  */
877 void spawnfunc_onslaught_generator()
878 {
879         if (!g_onslaught)
880         {
881                 remove(self);
882                 return;
883         }
884
885         //entity e;
886         precache_model("models/onslaught/generator.md3");
887         precache_model("models/onslaught/generator_shield.md3");
888         precache_model("models/onslaught/generator_dmg1.md3");
889         precache_model("models/onslaught/generator_dmg2.md3");
890         precache_model("models/onslaught/generator_dmg3.md3");
891         precache_model("models/onslaught/generator_dmg4.md3");
892         precache_model("models/onslaught/generator_dmg5.md3");
893         precache_model("models/onslaught/generator_dmg6.md3");
894         precache_model("models/onslaught/generator_dmg7.md3");
895         precache_model("models/onslaught/generator_dmg8.md3");
896         precache_model("models/onslaught/generator_dmg9.md3");
897         precache_model("models/onslaught/generator_dead.md3");
898         precache_model("models/onslaught/shockwave.md3");
899         precache_model("models/onslaught/shockwavetransring.md3");
900         precache_model("models/onslaught/gen_gib1.md3");
901         precache_model("models/onslaught/gen_gib2.md3");
902         precache_model("models/onslaught/gen_gib3.md3");
903         precache_model("models/onslaught/ons_ray.md3");
904         precache_sound("onslaught/generator_decay.wav");
905         precache_sound("weapons/grenade_impact.wav");
906         precache_sound("weapons/rocket_impact.wav");
907         precache_sound("onslaught/generator_underattack.wav");
908         precache_sound("onslaught/shockwave.wav");
909         precache_sound("onslaught/ons_hit1.wav");
910         precache_sound("onslaught/ons_hit2.wav");
911         precache_sound("onslaught/electricity_explode.wav");
912         if (!self.team)
913                 objerror("team must be set");
914
915         if(self.team == NUM_TEAM_1)
916         ons_red_generator = self;
917
918         if(self.team == NUM_TEAM_2)
919         ons_blue_generator = self;
920
921         self.team_saved = self.team;
922         self.colormap = 1024 + (self.team - 1) * 17;
923         self.solid = SOLID_BBOX;
924         self.movetype = MOVETYPE_NONE;
925         self.lasthealth = self.max_health = self.health = autocvar_g_onslaught_gen_health;
926         setmodel(self, "models/onslaught/generator.md3");
927         setsize(self, '-52 -52 -14', '52 52 75');
928         setorigin(self, self.origin);
929         self.takedamage = DAMAGE_AIM;
930         self.bot_attack = true;
931         self.event_damage = onslaught_generator_damage;
932         self.iscaptured = true;
933         self.islinked = true;
934         self.isshielded = true;
935         // helper entity that create fx when generator is damaged
936         onslaught_generator_damage_spawn(self);
937         // spawn shield model which indicates whether this can be damaged
938         self.enemy = spawn();
939         setattachment(self.enemy , self, "");
940         self.enemy.classname = "onslaught_generator_shield";
941         self.enemy.solid = SOLID_NOT;
942         self.enemy.movetype = MOVETYPE_NONE;
943         self.enemy.effects = EF_ADDITIVE;
944         setmodel(self.enemy, "models/onslaught/generator_shield.md3");
945         //setorigin(e, self.origin);
946         self.enemy.colormap = self.colormap;
947         self.enemy.team = self.team;
948         //self.think = onslaught_generator_delayed;
949         //self.nextthink = time + 0.2;
950         InitializeEntity(self, onslaught_generator_delayed, INITPRIO_LAST);
951
952         WaypointSprite_SpawnFixed(string_null, self.origin + '0 0 128', self, sprite, RADARICON_NONE, '0 0 0');
953         WaypointSprite_UpdateRule(self.sprite, NUM_TEAM_2, SPRITERULE_TEAMPLAY);
954         WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
955         WaypointSprite_UpdateHealth(self.sprite, self.health);
956
957         waypoint_spawnforitem(self);
958
959         onslaught_updatelinks();
960
961         self.reset = onslaught_generator_reset;
962 }
963
964 .float waslinked;
965 .float cp_bob_spd;
966 .vector cp_origin, cp_bob_origin, cp_bob_dmg;
967
968 float ons_notification_time_team1;
969 float ons_notification_time_team2;
970
971 void onslaught_controlpoint_icon_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
972 {
973         entity oself;
974         float nag;
975
976         if (damage <= 0)
977                 return;
978         if (self.owner.isshielded)
979         {
980                 // this is protected by a shield, so ignore the damage
981                 if (time > self.pain_finished)
982                         if (IS_PLAYER(attacker))
983                         {
984                                 play2(attacker, "onslaught/damageblockedbyshield.wav");
985                                 self.pain_finished = time + 1;
986                         }
987                 return;
988         }
989
990         if (IS_PLAYER(attacker))
991         {
992                 nag = false;
993                 if(self.team == NUM_TEAM_1)
994                 {
995                         if(time - ons_notification_time_team1 > 10)
996                         {
997                                 nag = true;
998                                 ons_notification_time_team1 = time;
999                         }
1000                 }
1001                 else if(self.team == NUM_TEAM_2)
1002                 {
1003                         if(time - ons_notification_time_team2 > 10)
1004                         {
1005                                 nag = true;
1006                                 ons_notification_time_team2 = time;
1007                         }
1008                 }
1009                 else
1010                         nag = true;
1011
1012                 if(nag)
1013                         play2team(self.team, "onslaught/controlpoint_underattack.wav");
1014         }
1015
1016         self.health = self.health - damage;
1017         if(self.owner.iscaptured)
1018                 WaypointSprite_UpdateHealth(self.owner.sprite, self.health);
1019         else
1020                 WaypointSprite_UpdateBuildFinished(self.owner.sprite, time + (self.max_health - self.health) / (self.count / sys_frametime));
1021         self.pain_finished = time + 1;
1022         self.punchangle = (2 * randomvec() - '1 1 1') * 45;
1023         self.cp_bob_dmg_z = (2 * random() - 1) * 15;
1024         // colormod flash when shot
1025         self.colormod = '2 2 2';
1026         // particles on every hit
1027         pointparticles(particleeffectnum("sparks"), hitloc, force*-1, 1);
1028         //sound on every hit
1029         if (random() < 0.5)
1030                 sound(self, CH_TRIGGER, "onslaught/ons_hit1.wav", VOL_BASE+0.3, ATTEN_NORM);
1031         else
1032                 sound(self, CH_TRIGGER, "onslaught/ons_hit2.wav", VOL_BASE+0.3, ATTEN_NORM);
1033
1034         if (self.health < 0)
1035         {
1036                 sound(self, CH_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTEN_NORM);
1037                 pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
1038                 {
1039                         string t;
1040                         t = Team_ColoredFullName(attacker.team);
1041                         bprint(Team_ColoredFullName(self.team), " ", self.message, " control point destroyed by ", t, "\n");
1042                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 25, "models/onslaught/controlpoint_icon_gib1.md3", 3, false);
1043                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 45, "models/onslaught/controlpoint_icon_gib2.md3", 3, false);
1044                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 45, "models/onslaught/controlpoint_icon_gib2.md3", 3, false);
1045                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 75, "models/onslaught/controlpoint_icon_gib4.md3", 3, false);
1046                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 75, "models/onslaught/controlpoint_icon_gib4.md3", 3, false);
1047                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 75, "models/onslaught/controlpoint_icon_gib4.md3", 3, false);
1048                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 75, "models/onslaught/controlpoint_icon_gib4.md3", 3, false);
1049                 }
1050                 self.owner.goalentity = world;
1051                 self.owner.islinked = false;
1052                 self.owner.iscaptured = false;
1053                 self.owner.team = 0;
1054                 self.owner.colormap = 1024;
1055
1056                 WaypointSprite_UpdateMaxHealth(self.owner.sprite, 0);
1057
1058                 onslaught_updatelinks();
1059
1060                 // Use targets now (somebody make sure this is in the right place..)
1061                 oself = self;
1062                 self = self.owner;
1063                 activator = self;
1064                 SUB_UseTargets ();
1065                 self = oself;
1066
1067
1068                 self.owner.waslinked = self.owner.islinked;
1069                 if(self.owner.model != "models/onslaught/controlpoint_pad.md3")
1070                         setmodel(self.owner, "models/onslaught/controlpoint_pad.md3");
1071                 //setsize(self, '-32 -32 0', '32 32 8');
1072
1073                 remove(self);
1074         }
1075 }
1076
1077 void onslaught_controlpoint_icon_think()
1078 {
1079         entity oself;
1080         self.nextthink = time + sys_frametime;
1081
1082         if(autocvar_g_onslaught_cp_proxydecap)
1083         {
1084         float _enemy_count = 0;
1085         float _friendly_count = 0;
1086         float _dist;
1087         entity _player;
1088
1089         FOR_EACH_PLAYER(_player)
1090         {
1091             if(!_player.deadflag)
1092             {
1093                 _dist = vlen(_player.origin - self.origin);
1094                 if(_dist < autocvar_g_onslaught_cp_proxydecap_distance)
1095                 {
1096                     if(_player.team == self.team)
1097                         ++_friendly_count;
1098                     else
1099                         ++_enemy_count;
1100                 }
1101             }
1102         }
1103
1104         _friendly_count = _friendly_count * (autocvar_g_onslaught_cp_proxydecap_dps * sys_frametime);
1105         _enemy_count = _enemy_count * (autocvar_g_onslaught_cp_proxydecap_dps * sys_frametime);
1106
1107         self.health = bound(0, self.health + (_friendly_count - _enemy_count), self.max_health);
1108         if(self.health <= 0)
1109         {
1110             onslaught_controlpoint_icon_damage(self, self, 1, 0, self.origin, '0 0 0');
1111             return;
1112         }
1113     }
1114
1115         if (time > self.pain_finished + 5)
1116         {
1117                 if(self.health < self.max_health)
1118                 {
1119                         self.health = self.health + self.count;
1120                         if (self.health >= self.max_health)
1121                                 self.health = self.max_health;
1122                         WaypointSprite_UpdateHealth(self.owner.sprite, self.health);
1123                 }
1124         }
1125         if (self.health < self.max_health * 0.25)
1126                 setmodel(self, "models/onslaught/controlpoint_icon_dmg3.md3");
1127         else if (self.health < self.max_health * 0.50)
1128                 setmodel(self, "models/onslaught/controlpoint_icon_dmg2.md3");
1129         else if (self.health < self.max_health * 0.75)
1130                 setmodel(self, "models/onslaught/controlpoint_icon_dmg1.md3");
1131         else if (self.health < self.max_health * 0.90)
1132                 setmodel(self, "models/onslaught/controlpoint_icon.md3");
1133         // colormod flash when shot
1134         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
1135
1136         if(self.owner.islinked != self.owner.waslinked)
1137         {
1138                 // unteam the spawnpoint if needed
1139                 float t;
1140                 t = self.owner.team;
1141                 if(!self.owner.islinked)
1142                         self.owner.team = 0;
1143
1144                 oself = self;
1145                 self = self.owner;
1146                 activator = self;
1147                 SUB_UseTargets ();
1148                 self = oself;
1149
1150                 self.owner.team = t;
1151
1152                 self.owner.waslinked = self.owner.islinked;
1153         }
1154
1155         if (self.punchangle.x > 0)
1156         {
1157                 self.punchangle_x = self.punchangle.x - 60 * sys_frametime;
1158                 if (self.punchangle.x < 0)
1159                         self.punchangle_x = 0;
1160         }
1161         else if (self.punchangle.x < 0)
1162         {
1163                 self.punchangle_x = self.punchangle.x + 60 * sys_frametime;
1164                 if (self.punchangle.x > 0)
1165                         self.punchangle_x = 0;
1166         }
1167
1168         if (self.punchangle.y > 0)
1169         {
1170                 self.punchangle_y = self.punchangle.y - 60 * sys_frametime;
1171                 if (self.punchangle.y < 0)
1172                         self.punchangle_y = 0;
1173         }
1174         else if (self.punchangle.y < 0)
1175         {
1176                 self.punchangle_y = self.punchangle.y + 60 * sys_frametime;
1177                 if (self.punchangle.y > 0)
1178                         self.punchangle_y = 0;
1179         }
1180
1181         if (self.punchangle.z > 0)
1182         {
1183                 self.punchangle_z = self.punchangle.z - 60 * sys_frametime;
1184                 if (self.punchangle.z < 0)
1185                         self.punchangle_z = 0;
1186         }
1187         else if (self.punchangle.z < 0)
1188         {
1189                 self.punchangle_z = self.punchangle.z + 60 * sys_frametime;
1190                 if (self.punchangle.z > 0)
1191                         self.punchangle_z = 0;
1192         }
1193
1194         self.angles_x = self.punchangle.x;
1195         self.angles_y = self.punchangle.y + self.mangle.y;
1196         self.angles_z = self.punchangle.z;
1197         self.mangle_y = self.mangle.y + 45 * sys_frametime;
1198
1199         self.cp_bob_origin_z = 4 * PI * (1 - cos(self.cp_bob_spd));
1200         self.cp_bob_spd = self.cp_bob_spd + 1.875 * sys_frametime;
1201         if(self.cp_bob_dmg.z > 0)
1202                 self.cp_bob_dmg_z = self.cp_bob_dmg.z - 3 * sys_frametime;
1203         else
1204                 self.cp_bob_dmg_z = 0;
1205         setorigin(self,self.cp_origin + self.cp_bob_origin + self.cp_bob_dmg);
1206
1207         // damaged fx
1208         if(random() < 0.6 - self.health / self.max_health)
1209         {
1210                 pointparticles(particleeffectnum("electricity_sparks"), self.origin + randompos('-10 -10 -20', '10 10 20'), '0 0 0', 1);
1211
1212                 if(random() > 0.8)
1213                         sound(self, CH_PAIN, "onslaught/ons_spark1.wav", VOL_BASE, ATTEN_NORM);
1214                 else if (random() > 0.5)
1215                         sound(self, CH_PAIN, "onslaught/ons_spark2.wav", VOL_BASE, ATTEN_NORM);
1216         }
1217 }
1218
1219 void onslaught_controlpoint_icon_buildthink()
1220 {
1221         entity oself;
1222         float a;
1223
1224         self.nextthink = time + sys_frametime;
1225
1226         // only do this if there is power
1227         a = onslaught_controlpoint_can_be_linked(self.owner, self.owner.team);
1228         if(!a)
1229                 return;
1230
1231         self.health = self.health + self.count;
1232
1233         if (self.health >= self.max_health)
1234         {
1235                 self.health = self.max_health;
1236                 self.count = autocvar_g_onslaught_cp_regen * sys_frametime; // slow repair rate from now on
1237                 self.think = onslaught_controlpoint_icon_think;
1238                 sound(self, CH_TRIGGER, "onslaught/controlpoint_built.wav", VOL_BASE, ATTEN_NORM);
1239                 bprint(Team_ColoredFullName(self.team), " captured ", self.owner.message, " control point\n");
1240                 self.owner.iscaptured = true;
1241
1242                 WaypointSprite_UpdateMaxHealth(self.owner.sprite, self.max_health);
1243                 WaypointSprite_UpdateHealth(self.owner.sprite, self.health);
1244
1245                 onslaught_updatelinks();
1246
1247                 // Use targets now (somebody make sure this is in the right place..)
1248                 oself = self;
1249                 self = self.owner;
1250                 activator = self;
1251                 SUB_UseTargets ();
1252                 self = oself;
1253                 self.cp_origin = self.origin;
1254                 self.cp_bob_origin = '0 0 0.1';
1255                 self.cp_bob_spd = 0;
1256         }
1257         self.alpha = self.health / self.max_health;
1258         // colormod flash when shot
1259         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
1260         if(self.owner.model != "models/onslaught/controlpoint_pad2.md3")
1261                 setmodel(self.owner, "models/onslaught/controlpoint_pad2.md3");
1262         //setsize(self, '-32 -32 0', '32 32 8');
1263
1264         if(random() < 0.9 - self.health / self.max_health)
1265                 pointparticles(particleeffectnum("rage"), self.origin + 10 * randomvec(), '0 0 -1', 1);
1266 }
1267
1268
1269
1270
1271 void onslaught_controlpoint_touch()
1272 {
1273         entity e;
1274         float a;
1275         if (!IS_PLAYER(other))
1276                 return;
1277         a = onslaught_controlpoint_attackable(self, other.team);
1278         if(a != 2 && a != 4)
1279                 return;
1280         // we've verified that this player has a legitimate claim to this point,
1281         // so start building the captured point icon (which only captures this
1282         // point if it successfully builds without being destroyed first)
1283         self.goalentity = e = spawn();
1284         e.classname = "onslaught_controlpoint_icon";
1285         e.owner = self;
1286         e.max_health = autocvar_g_onslaught_cp_health;
1287         e.health = autocvar_g_onslaught_cp_buildhealth;
1288         e.solid = SOLID_BBOX;
1289         e.movetype = MOVETYPE_NONE;
1290         setmodel(e, "models/onslaught/controlpoint_icon.md3");
1291         setsize(e, '-32 -32 -32', '32 32 32');
1292         setorigin(e, self.origin + '0 0 96');
1293         e.takedamage = DAMAGE_AIM;
1294         e.bot_attack = true;
1295         e.event_damage = onslaught_controlpoint_icon_damage;
1296         e.team = other.team;
1297         e.colormap = 1024 + (e.team - 1) * 17;
1298         e.think = onslaught_controlpoint_icon_buildthink;
1299         e.nextthink = time + sys_frametime;
1300         e.count = (e.max_health - e.health) * sys_frametime / autocvar_g_onslaught_cp_buildtime; // how long it takes to build
1301         sound(e, CH_TRIGGER, "onslaught/controlpoint_build.wav", VOL_BASE, ATTEN_NORM);
1302         self.team = e.team;
1303         self.colormap = e.colormap;
1304         WaypointSprite_UpdateBuildFinished(self.sprite, time + (e.max_health - e.health) / (e.count / sys_frametime));
1305         onslaught_updatelinks();
1306 }
1307
1308 void onslaught_controlpoint_think()
1309 {
1310         self.nextthink = time;
1311         CSQCMODEL_AUTOUPDATE();
1312 }
1313
1314 void onslaught_controlpoint_reset()
1315 {
1316         if(self.goalentity && self.goalentity != world)
1317                 remove(self.goalentity);
1318         self.goalentity = world;
1319         self.team = 0;
1320         self.colormap = 1024;
1321         self.iscaptured = false;
1322         self.islinked = false;
1323         self.isshielded = true;
1324         self.enemy.solid = SOLID_NOT;
1325         self.enemy.colormap = self.colormap;
1326         self.think = onslaught_controlpoint_think;
1327         self.enemy.think = func_null;
1328         self.nextthink = time; // don't like func_null :P
1329         setmodel(self, "models/onslaught/controlpoint_pad.md3");
1330         //setsize(self, '-32 -32 0', '32 32 8');
1331
1332         WaypointSprite_UpdateMaxHealth(self.sprite, 0);
1333
1334         onslaught_updatelinks();
1335
1336         activator = self;
1337         SUB_UseTargets(); // to reset the structures, playerspawns etc.
1338
1339         CSQCMODEL_AUTOUPDATE();
1340 }
1341
1342 /*QUAKED spawnfunc_onslaught_controlpoint (0 .5 .8) (-32 -32 0) (32 32 128)
1343   Control point. Be sure to give this enough clearance so that the shootable part has room to exist
1344
1345   This should link to an spawnfunc_onslaught_controlpoint entity or spawnfunc_onslaught_generator entity.
1346
1347 keys:
1348 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
1349 "target" - target any entities that are tied to this control point, such as vehicles and buildable structure entities.
1350 "message" - name of this control point (should reflect the location in the map, such as "center bridge", "north tower", etc)
1351  */
1352
1353 void spawnfunc_onslaught_controlpoint()
1354 {
1355         //entity e;
1356         if (!g_onslaught)
1357         {
1358                 remove(self);
1359                 return;
1360         }
1361         precache_model("models/onslaught/controlpoint_pad.md3");
1362         precache_model("models/onslaught/controlpoint_pad2.md3");
1363         precache_model("models/onslaught/controlpoint_shield.md3");
1364         precache_model("models/onslaught/controlpoint_icon.md3");
1365         precache_model("models/onslaught/controlpoint_icon_dmg1.md3");
1366         precache_model("models/onslaught/controlpoint_icon_dmg2.md3");
1367         precache_model("models/onslaught/controlpoint_icon_dmg3.md3");
1368         precache_model("models/onslaught/controlpoint_icon_gib1.md3");
1369         precache_model("models/onslaught/controlpoint_icon_gib2.md3");
1370         precache_model("models/onslaught/controlpoint_icon_gib4.md3");
1371         precache_sound("onslaught/controlpoint_build.wav");
1372         precache_sound("onslaught/controlpoint_built.wav");
1373         precache_sound("weapons/grenade_impact.wav");
1374         precache_sound("onslaught/damageblockedbyshield.wav");
1375         precache_sound("onslaught/controlpoint_underattack.wav");
1376         precache_sound("onslaught/ons_spark1.wav");
1377         precache_sound("onslaught/ons_spark2.wav");
1378
1379         self.solid = SOLID_BBOX;
1380         self.movetype = MOVETYPE_NONE;
1381         setmodel(self, "models/onslaught/controlpoint_pad.md3");
1382         //setsize(self, '-32 -32 0', '32 32 8');
1383         if(!self.noalign)
1384         {
1385                 setorigin(self, self.origin + '0 0 20');
1386                 droptofloor();
1387         }
1388         self.touch = onslaught_controlpoint_touch;
1389         self.team = 0;
1390         self.colormap = 1024;
1391         self.iscaptured = false;
1392         self.islinked = false;
1393         self.isshielded = true;
1394
1395         // spawn shield model which indicates whether this can be damaged
1396         self.enemy = spawn();
1397         self.enemy.classname = "onslaught_controlpoint_shield";
1398         self.enemy.solid = SOLID_NOT;
1399         self.enemy.movetype = MOVETYPE_NONE;
1400         self.enemy.effects = EF_ADDITIVE;
1401         setmodel(self.enemy , "models/onslaught/controlpoint_shield.md3");
1402
1403         setattachment(self.enemy , self, "");
1404         //setsize(e, '-32 -32 0', '32 32 128');
1405
1406         //setorigin(e, self.origin);
1407         self.enemy.colormap = self.colormap;
1408
1409         waypoint_spawnforitem(self);
1410
1411         self.think = onslaught_controlpoint_think;
1412         self.nextthink = time;
1413
1414         WaypointSprite_SpawnFixed(string_null, self.origin + '0 0 128', self, sprite, RADARICON_NONE, '0 0 0');
1415         WaypointSprite_UpdateRule(self.sprite, NUM_TEAM_2, SPRITERULE_TEAMPLAY);
1416
1417         onslaught_updatelinks();
1418
1419         self.reset = onslaught_controlpoint_reset;
1420
1421         CSQCMODEL_AUTOINIT();
1422 }
1423
1424 float onslaught_link_send(entity to, float sendflags)
1425 {
1426         WriteByte(MSG_ENTITY, ENT_CLIENT_RADARLINK);
1427         WriteByte(MSG_ENTITY, sendflags);
1428         if(sendflags & 1)
1429         {
1430                 WriteCoord(MSG_ENTITY, self.goalentity.origin.x);
1431                 WriteCoord(MSG_ENTITY, self.goalentity.origin.y);
1432                 WriteCoord(MSG_ENTITY, self.goalentity.origin.z);
1433         }
1434         if(sendflags & 2)
1435         {
1436                 WriteCoord(MSG_ENTITY, self.enemy.origin.x);
1437                 WriteCoord(MSG_ENTITY, self.enemy.origin.y);
1438                 WriteCoord(MSG_ENTITY, self.enemy.origin.z);
1439         }
1440         if(sendflags & 4)
1441         {
1442                 WriteByte(MSG_ENTITY, self.clientcolors); // which is goalentity's color + enemy's color * 16
1443         }
1444         return true;
1445 }
1446
1447 void onslaught_link_checkupdate()
1448 {
1449         // TODO check if the two sides have moved (currently they won't move anyway)
1450         float redpower, bluepower;
1451
1452         redpower = bluepower = 0;
1453         if(self.goalentity.islinked)
1454         {
1455                 if(self.goalentity.team == NUM_TEAM_1)
1456                         redpower = 1;
1457                 else if(self.goalentity.team == NUM_TEAM_2)
1458                         bluepower = 1;
1459         }
1460         if(self.enemy.islinked)
1461         {
1462                 if(self.enemy.team == NUM_TEAM_1)
1463                         redpower = 2;
1464                 else if(self.enemy.team == NUM_TEAM_2)
1465                         bluepower = 2;
1466         }
1467
1468         float cc;
1469         if(redpower == 1 && bluepower == 2)
1470                 cc = (NUM_TEAM_1 - 1) * 0x01 + (NUM_TEAM_2 - 1) * 0x10;
1471         else if(redpower == 2 && bluepower == 1)
1472                 cc = (NUM_TEAM_1 - 1) * 0x10 + (NUM_TEAM_2 - 1) * 0x01;
1473         else if(redpower)
1474                 cc = (NUM_TEAM_1 - 1) * 0x11;
1475         else if(bluepower)
1476                 cc = (NUM_TEAM_2 - 1) * 0x11;
1477         else
1478                 cc = 0;
1479
1480         //print(etos(self), " rp=", ftos(redpower), " bp=", ftos(bluepower), " ");
1481         //print("cc=", ftos(cc), "\n");
1482
1483         if(cc != self.clientcolors)
1484         {
1485                 self.clientcolors = cc;
1486                 self.SendFlags |= 4;
1487         }
1488
1489         self.nextthink = time;
1490 }
1491
1492 void onslaught_link_delayed()
1493 {
1494         self.goalentity = find(world, targetname, self.target);
1495         self.enemy = find(world, targetname, self.target2);
1496         if (!self.goalentity)
1497                 objerror("can not find target\n");
1498         if (!self.enemy)
1499                 objerror("can not find target2\n");
1500         dprint(etos(self.goalentity), " linked with ", etos(self.enemy), "\n");
1501         self.SendFlags |= 3;
1502         self.think = onslaught_link_checkupdate;
1503         self.nextthink = time;
1504 }
1505
1506 /*QUAKED spawnfunc_onslaught_link (0 .5 .8) (-16 -16 -16) (16 16 16)
1507   Link between control points.
1508
1509   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.
1510
1511 keys:
1512 "target" - first control point.
1513 "target2" - second control point.
1514  */
1515 void spawnfunc_onslaught_link()
1516 {
1517         if (!g_onslaught)
1518         {
1519                 remove(self);
1520                 return;
1521         }
1522         if (self.target == "" || self.target2 == "")
1523                 objerror("target and target2 must be set\n");
1524         InitializeEntity(self, onslaught_link_delayed, INITPRIO_FINDTARGET);
1525         Net_LinkEntity(self, false, 0, onslaught_link_send);
1526 }
1527
1528 MUTATOR_HOOKFUNCTION(ons_BuildMutatorsString)
1529 {
1530         ret_string = strcat(ret_string, ":ONS");
1531         return 0;
1532 }
1533
1534 MUTATOR_HOOKFUNCTION(ons_BuildMutatorsPrettyString)
1535 {
1536         ret_string = strcat(ret_string, ", Onslaught");
1537         return 0;
1538 }
1539
1540 MUTATOR_HOOKFUNCTION(ons_Spawn_Score)
1541 {
1542
1543     /*
1544     float _neer_home = (random() > 0.5 ? true : false);
1545
1546         RandomSelection_Init();
1547
1548         if(self.team == NUM_TEAM_1)
1549         RandomSelection_Add(ons_red_generator, 0, string_null, 1, 1);
1550
1551         if(self.team == NUM_TEAM_2)
1552         RandomSelection_Add(ons_blue_generator, 0, string_null, 1, 1);
1553
1554         entity _cp = findchain(classname, "onslaught_controlpoint"):
1555         while _cp;
1556         {
1557             if(_cp.team == self.team)
1558             RandomSelection_Add(_cp, 0, string_null, 1, 1);
1559
1560                 _cp = _cp.chain;
1561         }
1562
1563         if(RandomSelection_chosen_ent)
1564         {
1565                 self.tur_head = RandomSelection_chosen_ent;
1566                 spawn_score_x += SPAWN_PRIO_NEAR_TEAMMATE_FOUND;
1567         }
1568         else if(self.team == spawn_spot.team)
1569                 spawn_score_x += SPAWN_PRIO_NEAR_TEAMMATE_SAMETEAM; // prefer same team, if we can't find a spawn near teammate
1570
1571     */
1572
1573         return 0;
1574 }
1575
1576 MUTATOR_HOOKFUNCTION(ons_PlayerSpawn)
1577 {
1578     if(!autocvar_g_onslaught_spawn_at_controlpoints)
1579         return 0;
1580
1581     if(random() < 0.5)  // 50/50 chane to use default spawnsystem.
1582         return 0;
1583
1584     float _close_to_home = ((random() > 0.5) ? true : false);
1585     entity _best = world, _trg_gen = world;
1586     float _score, _best_score = MAX_SHOT_DISTANCE;
1587
1588         RandomSelection_Init();
1589
1590         if(self.team == NUM_TEAM_1)
1591         {
1592             if(!_close_to_home)
1593             _trg_gen = ons_blue_generator;
1594         else
1595             _trg_gen  = ons_red_generator;
1596         }
1597
1598         if(self.team == NUM_TEAM_2)
1599         {
1600             if(_close_to_home)
1601             _trg_gen = ons_blue_generator;
1602         else
1603             _trg_gen  = ons_red_generator;
1604         }
1605
1606         entity _cp = findchain(classname, "onslaught_controlpoint");
1607         while(_cp)
1608         {
1609             if(_cp.team == self.team)
1610         {
1611             _score = vlen(_trg_gen.origin - _cp.origin);
1612             if(_score < _best_score)
1613             {
1614                 _best = _cp;
1615                 _best_score = _score;
1616             }
1617         }
1618                 _cp = _cp.chain;
1619         }
1620
1621     vector _loc;
1622     float i;
1623     if(_best)
1624     {
1625         for(i = 0; i < 10; ++i)
1626         {
1627             _loc = _best.origin + '0 0 96';
1628             _loc += ('0 1 0' * random()) * 128;
1629             tracebox(_loc, PL_MIN, PL_MAX, _loc, MOVE_NORMAL, self);
1630             if(trace_fraction == 1.0 && !trace_startsolid)
1631             {
1632                 setorigin(self, _loc);
1633                 self.angles = normalize(_loc - _best.origin) * RAD2DEG;
1634                 return 0;
1635             }
1636         }
1637     }
1638     else
1639     {
1640         if(!autocvar_g_onslaught_spawn_at_generator)
1641             return 0;
1642
1643         _trg_gen = ((self.team == NUM_TEAM_1) ? ons_red_generator : ons_blue_generator);
1644
1645         for(i = 0; i < 10; ++i)
1646         {
1647             _loc = _trg_gen.origin + '0 0 96';
1648             _loc += ('0 1 0' * random()) * 128;
1649             tracebox(_loc, PL_MIN, PL_MAX, _loc, MOVE_NORMAL, self);
1650             if(trace_fraction == 1.0 && !trace_startsolid)
1651             {
1652                 setorigin(self, _loc);
1653                 self.angles = normalize(_loc - _trg_gen.origin) * RAD2DEG;
1654                 return 0;
1655             }
1656         }
1657     }
1658
1659     return 0;
1660 }
1661
1662 MUTATOR_HOOKFUNCTION(ons_MonsterThink)
1663 {
1664         entity e = find(world, targetname, self.target);
1665         if (e != world)
1666                 self.team = e.team;
1667
1668         return false;
1669 }
1670
1671 MUTATOR_HOOKFUNCTION(ons_MonsterSpawn)
1672 {
1673         entity e, ee = world;
1674
1675         if(self.targetname)
1676         {
1677                 e = find(world,target,self.targetname);
1678                 if(e != world)
1679                 {
1680                         self.team = e.team;
1681                         ee = e;
1682                 }
1683         }
1684
1685         if(ee)
1686         {
1687         activator = ee;
1688         self.use();
1689     }
1690
1691         return false;
1692 }
1693
1694 MUTATOR_DEFINITION(gamemode_onslaught)
1695 {
1696         MUTATOR_HOOK(BuildMutatorsPrettyString, ons_BuildMutatorsPrettyString, CBC_ORDER_ANY);
1697         MUTATOR_HOOK(BuildMutatorsString, ons_BuildMutatorsString, CBC_ORDER_ANY);
1698         MUTATOR_HOOK(PlayerSpawn, ons_PlayerSpawn, CBC_ORDER_ANY);
1699         MUTATOR_HOOK(MonsterMove, ons_MonsterThink, CBC_ORDER_ANY);
1700         MUTATOR_HOOK(MonsterSpawn, ons_MonsterSpawn, CBC_ORDER_ANY);
1701         //MUTATOR_HOOK(Spawn_Score, ons_Spawn_Score, CBC_ORDER_ANY);
1702
1703         MUTATOR_ONADD
1704         {
1705                 if(time > 1) // game loads at time 1
1706                         error("This is a game type and it cannot be added at runtime.");
1707         }
1708
1709         MUTATOR_ONREMOVE
1710         {
1711                 print("This is a game type and it cannot be removed at runtime.");
1712                 return -1;
1713         }
1714
1715         return 0;
1716 }