]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_onslaught.qc
Replace all player/bot/spectator classname checks with macros
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_onslaught.qc
1 float autocvar_g_onslaught_spawn_at_controlpoints;
2 float autocvar_g_onslaught_spawn_at_generator;
3 float autocvar_g_onslaught_controlpoints_proxycap;
4 float autocvar_g_onslaught_controlpoints_proxycap_distance = 512;
5 float autocvar_g_onslaught_controlpoints_proxycap_dps = 100;
6
7 void onslaught_generator_updatesprite(entity e);
8 void onslaught_controlpoint_updatesprite(entity e);
9 void onslaught_link_checkupdate();
10
11 .entity sprite;
12 .string target2;
13 .float iscaptured;
14 .float islinked;
15 .float isgenneighbor_red;
16 .float isgenneighbor_blue;
17 .float iscpneighbor_red;
18 .float iscpneighbor_blue;
19 .float isshielded;
20 .float lasthealth;
21 .float lastteam;
22 .float lastshielded;
23 .float lastcaptured;
24
25 .string model1, model2, model3;
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 == COLOR_TEAM1)
167                                         l.enemy.isgenneighbor_red = TRUE;
168                                 else if(l.goalentity.team == COLOR_TEAM2)
169                                         l.enemy.isgenneighbor_blue = TRUE;
170                         }
171                         else
172                         {
173                                 if(l.goalentity.team == COLOR_TEAM1)
174                                         l.enemy.iscpneighbor_red = TRUE;
175                                 else if(l.goalentity.team == COLOR_TEAM2)
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 == COLOR_TEAM1)
189                                         l.goalentity.isgenneighbor_red = TRUE;
190                                 else if(l.enemy.team == COLOR_TEAM2)
191                                         l.goalentity.isgenneighbor_blue = TRUE;
192                         }
193                         else
194                         {
195                                 if(l.enemy.team == COLOR_TEAM1)
196                                         l.goalentity.iscpneighbor_red = TRUE;
197                                 else if(l.enemy.team == COLOR_TEAM2)
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 == COLOR_TEAM1) t1 = 1;
258                         if (l.team == COLOR_TEAM2) t2 = 1;
259                         if (l.team == COLOR_TEAM3) t3 = 1;
260                         if (l.team == COLOR_TEAM4) 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 == COLOR_TEAM1)
275         {
276                 if(cp.isgenneighbor_red)
277                         return 2;
278                 if(cp.iscpneighbor_red)
279                         return 1;
280         }
281         else if(t == COLOR_TEAM2)
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, COLOR_TEAM1 + COLOR_TEAM2 - 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, COLOR_TEAM1 + COLOR_TEAM2 - 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                                 FOR_EACH_PLAYER(e)
397                                         centerprint(e, "^3Now playing ^1OVERTIME^3!\n^3Generators start now to decay.\n^3The more control points your team holds,\n^3the faster the enemy generator decays.");
398                                 overtime_msg_time = time;
399                         }
400                         // self.max_health / 300 gives 5 minutes of overtime.
401                         // control points reduce the overtime duration.
402                         sound(self, CH_TRIGGER, "onslaught/generator_decay.wav", VOL_BASE, ATTN_NORM);
403                         d = 1;
404                         e = findchain(classname, "onslaught_controlpoint");
405                         while (e)
406                         {
407                                 if (e.team != self.team)
408                                         if (e.islinked)
409                                                 d = d + 1;
410                                 e = e.chain;
411                         }
412                         
413                         if(autocvar_g_campaign && autocvar__campaign_testrun)
414                                 d = d * self.max_health;
415                         else
416                                 d = d * self.max_health / max(30, 60 * autocvar_timelimit_suddendeath);
417                         
418                         Damage(self, self, self, d, DEATH_HURTTRIGGER, self.origin, '0 0 0');
419                 }
420                 else if (overtime_msg_time)
421                         overtime_msg_time = 0;
422         
423         if(!self.isshielded && self.wait < time)
424         {
425             self.wait = time + 5;
426             FOR_EACH_PLAYER(e)
427             {
428                 if(e.team == self.team)
429                 {
430                     centerprint(e, "^1Your generator is NOT shielded!\n^7Re-capture controlpoints to shield it!");
431                     soundto(MSG_ONE, e, CHAN_AUTO, "kh/alarm.wav", VOL_BASE, ATTN_NONE);    // FIXME: Uniqe sound?
432                 }                                   
433             }                                               
434         }    
435         }
436 }
437
438 void onslaught_generator_ring_spawn(vector org)
439 {
440         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);
441 }
442
443 void onslaught_generator_ray_think()
444 {
445         self.nextthink = time + 0.05;
446         if(self.count > 10)
447         {
448                 self.think = SUB_Remove;
449                 return;
450         }
451
452         if(self.count > 5)
453                 self.alpha -= 0.1;
454         else
455                 self.alpha += 0.1;
456
457         self.scale += 0.2;
458         self.count +=1;
459 }
460
461 void onslaught_generator_ray_spawn(vector org)
462 {
463         entity e;
464         e = spawn();
465         setmodel(e, "models/onslaught/ons_ray.md3");
466         setorigin(e, org);
467         e.angles = randomvec() * 360;
468         e.alpha = 0;
469         e.scale = random() * 5 + 8;
470         e.think = onslaught_generator_ray_think;
471         e.nextthink = time + 0.05;
472 }
473
474 void onslaught_generator_shockwave_spawn(vector org)
475 {
476         shockwave_spawn("models/onslaught/shockwave.md3", org, -64, 0.75, 0.5);
477 }
478
479 void onslaught_generator_damage_think()
480 {
481         if(self.owner.health < 0)
482         {
483                 self.think = SUB_Remove;
484                 return;
485         }
486         self.nextthink = time+0.1;
487
488         // damaged fx (less probable the more damaged is the generator)
489         if(random() < 0.9 - self.owner.health / self.owner.max_health)
490                 if(random() < 0.01)
491                 {
492                         pointparticles(particleeffectnum("electro_ballexplode"), self.origin + randompos('-50 -50 -20', '50 50 50'), '0 0 0', 1);
493                         sound(self, CH_TRIGGER, "onslaught/electricity_explode.wav", VOL_BASE, ATTN_NORM);
494                 }
495                 else
496                         pointparticles(particleeffectnum("torch_small"), self.origin + randompos('-60 -60 -20', '60 60 60'), '0 0 0', 1);
497 }
498
499 void onslaught_generator_damage_spawn(entity gd_owner)
500 {
501         entity e;
502         e = spawn();
503         e.owner = gd_owner;
504         e.health = self.owner.health;
505         setorigin(e, gd_owner.origin);
506         e.think = onslaught_generator_damage_think;
507         e.nextthink = time+1;
508 }
509
510 void onslaught_generator_deaththink()
511 {
512         vector org;
513         float i;
514
515         if not (self.count)
516                 self.count = 40;
517
518         // White shockwave
519         if(self.count==40||self.count==20)
520         {
521                 onslaught_generator_ring_spawn(self.origin);
522                 sound(self, CH_TRIGGER, "onslaught/shockwave.wav", VOL_BASE, ATTN_NORM);
523         }
524
525         // Throw some gibs
526         if(random() < 0.3)
527         {
528                 i = random();
529                 if(i < 0.3)
530                         ons_throwgib(self.origin + '0 0 40', (100 * randomvec() - '1 1 1') * 11 + '0 0 20', "models/onslaught/gen_gib1.md3", 6, TRUE);
531                 else if(i > 0.7)
532                         ons_throwgib(self.origin + '0 0 40', (100 * randomvec() - '1 1 1') * 12 + '0 0 20', "models/onslaught/gen_gib2.md3", 6, TRUE);
533                 else
534                         ons_throwgib(self.origin + '0 0 40', (100 * randomvec() - '1 1 1') * 13 + '0 0 20', "models/onslaught/gen_gib3.md3", 6, TRUE);
535         }
536
537         // Spawn fire balls
538         for(i=0;i < 10;++i)
539         {
540                 org = self.origin + randompos('-30 -30 -30' * i + '0 0 -20', '30 30 30' * i + '0 0 20');
541                 pointparticles(particleeffectnum("onslaught_generator_gib_explode"), org, '0 0 0', 1);
542         }
543
544         // Short explosion sound + small explosion
545         if(random() < 0.25)
546         {
547                 te_explosion(self.origin);
548                 sound(self, CH_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
549         }
550
551         // Particles
552         org = self.origin + randompos(self.mins + '8 8 8', self.maxs + '-8 -8 -8');
553         pointparticles(particleeffectnum("onslaught_generator_smallexplosion"), org, '0 0 0', 1);
554
555         // rays
556         if(random() > 0.25 )
557         {
558                 onslaught_generator_ray_spawn(self.origin);
559         }
560
561         // Final explosion
562         if(self.count==1)
563         {
564                 org = self.origin;
565                 te_explosion(org);
566                 onslaught_generator_shockwave_spawn(org);
567                 pointparticles(particleeffectnum("onslaught_generator_finalexplosion"), org, '0 0 0', 1);
568                 sound(self, CH_TRIGGER, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
569         }
570         else
571                 self.nextthink = time + 0.05;
572
573         self.count = self.count - 1;
574 }
575
576 void onslaught_generator_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
577 {
578         float i;
579         if (damage <= 0)
580                 return;
581         if(inWarmupStage)
582                 return;
583         if (attacker != self)
584         {
585                 if (self.isshielded)
586                 {
587                         // this is protected by a shield, so ignore the damage
588                         if (time > self.pain_finished)
589                                 if (IS_PLAYER(attacker))
590                                 {
591                                         play2(attacker, "onslaught/damageblockedbyshield.wav");
592                                         self.pain_finished = time + 1;
593                                 }
594                         return;
595                 }
596                 if (time > self.pain_finished)
597                 {
598                         self.pain_finished = time + 10;
599                         bprint(ColoredTeamName(self.team), " generator under attack!\n");
600                         play2team(self.team, "onslaught/generator_underattack.wav");
601                 }
602         }
603         self.health = self.health - damage;
604         WaypointSprite_UpdateHealth(self.sprite, self.health);
605         // choose an animation frame based on health
606         self.frame = 10 * bound(0, (1 - self.health / self.max_health), 1);
607         // see if the generator is still functional, or dying
608         if (self.health > 0)
609         {
610 #ifdef ONSLAUGHT_SPAM
611                 float h, lh;
612                 lh = ceil(self.lasthealth / 100) * 100;
613                 h = ceil(self.health / 100) * 100;
614                 if(lh != h)
615                         bprint(ColoredTeamName(self.team), " generator has less than ", ftos(h), " health remaining\n");
616 #endif
617                 self.lasthealth = self.health;
618         }
619         else if not(inWarmupStage)
620         {
621                 if (attacker == self)
622                         bprint(ColoredTeamName(self.team), " generator spontaneously exploded due to overtime!\n");
623                 else
624                 {
625                         string t;
626                         t = ColoredTeamName(attacker.team);
627                         bprint(ColoredTeamName(self.team), " generator destroyed by ", t, "!\n");
628                 }
629                 self.iscaptured = FALSE;
630                 self.islinked = FALSE;
631                 self.isshielded = FALSE;
632                 self.takedamage = DAMAGE_NO; // can't be hurt anymore
633                 self.event_damage = func_null; // won't do anything if hurt
634                 self.count = 0; // reset counter
635                 self.think = onslaught_generator_deaththink; // explosion sequence
636                 self.nextthink = time; // start exploding immediately
637                 self.think(); // do the first explosion now
638
639                 WaypointSprite_UpdateMaxHealth(self.sprite, 0);
640
641                 onslaught_updatelinks();
642         }
643
644         if(self.health <= 0)
645                 setmodel(self, "models/onslaught/generator_dead.md3");
646         else if(self.health < self.max_health * 0.10)
647                 setmodel(self, "models/onslaught/generator_dmg9.md3");
648         else if(self.health < self.max_health * 0.20)
649                 setmodel(self, "models/onslaught/generator_dmg8.md3");
650         else if(self.health < self.max_health * 0.30)
651                 setmodel(self, "models/onslaught/generator_dmg7.md3");
652         else if(self.health < self.max_health * 0.40)
653                 setmodel(self, "models/onslaught/generator_dmg6.md3");
654         else if(self.health < self.max_health * 0.50)
655                 setmodel(self, "models/onslaught/generator_dmg5.md3");
656         else if(self.health < self.max_health * 0.60)
657                 setmodel(self, "models/onslaught/generator_dmg4.md3");
658         else if(self.health < self.max_health * 0.70)
659                 setmodel(self, "models/onslaught/generator_dmg3.md3");
660         else if(self.health < self.max_health * 0.80)
661                 setmodel(self, "models/onslaught/generator_dmg2.md3");
662         else if(self.health < self.max_health * 0.90)
663                 setmodel(self, "models/onslaught/generator_dmg1.md3");
664         setsize(self, '-52 -52 -14', '52 52 75');
665
666         // Throw some flaming gibs on damage, more damage = more chance for gib
667         if(random() < damage/220)
668         {
669                 sound(self, CH_TRIGGER, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
670                 i = random();
671                 if(i < 0.3)
672                         ons_throwgib(hitloc + '0 0 20', force * -1, "models/onslaught/gen_gib1.md3", 5, TRUE);
673                 else if(i > 0.7)
674                         ons_throwgib(hitloc + '0 0 20', force * -1, "models/onslaught/gen_gib2.md3", 5, TRUE);
675                 else
676                         ons_throwgib(hitloc + '0 0 20', force * -1, "models/onslaught/gen_gib3.md3", 5, TRUE);
677         }
678         else
679         {
680                 // particles on every hit
681                 pointparticles(particleeffectnum("sparks"), hitloc, force * -1, 1);
682
683                 //sound on every hit
684                 if (random() < 0.5)
685                         sound(self, CH_TRIGGER, "onslaught/ons_hit1.wav", VOL_BASE, ATTN_NORM);
686                 else
687                         sound(self, CH_TRIGGER, "onslaught/ons_hit2.wav", VOL_BASE, ATTN_NORM);
688         }
689
690         //throw some gibs on damage
691         if(random() < damage/200+0.2)
692                 if(random() < 0.5)
693                         ons_throwgib(hitloc + '0 0 20', randomvec()*360, "models/onslaught/gen_gib1.md3", 5, FALSE);
694 }
695
696 // update links after a delay
697 void onslaught_generator_delayed()
698 {
699         onslaught_updatelinks();
700         // now begin normal thinking
701         self.think = onslaught_generator_think;
702         self.nextthink = time;
703 }
704
705 string onslaught_generator_waypointsprite_for_team(entity e, float t)
706 {
707         if(t == e.team)
708         {
709                 if(e.team == COLOR_TEAM1)
710                         return "ons-gen-red";
711                 else if(e.team == COLOR_TEAM2)
712                         return "ons-gen-blue";
713         }
714         if(e.isshielded)
715                 return "ons-gen-shielded";
716         if(e.team == COLOR_TEAM1)
717                 return "ons-gen-red";
718         else if(e.team == COLOR_TEAM2)
719                 return "ons-gen-blue";
720         return "";
721 }
722
723 void onslaught_generator_updatesprite(entity e)
724 {
725         string s1, s2, s3;
726         s1 = onslaught_generator_waypointsprite_for_team(e, COLOR_TEAM1);
727         s2 = onslaught_generator_waypointsprite_for_team(e, COLOR_TEAM2);
728         s3 = onslaught_generator_waypointsprite_for_team(e, -1);
729         WaypointSprite_UpdateSprites(e.sprite, s1, s2, s3);
730
731         if(e.lastteam != e.team + 2 || e.lastshielded != e.isshielded)
732         {
733                 e.lastteam = e.team + 2;
734                 e.lastshielded = e.isshielded;
735                 if(e.lastshielded)
736                 {
737                         if(e.team == COLOR_TEAM1 || e.team == COLOR_TEAM2)
738                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, 0.5 * colormapPaletteColor(e.team - 1, FALSE));
739                         else
740                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.5 0.5 0.5');
741                 }
742                 else
743                 {
744                         if(e.team == COLOR_TEAM1 || e.team == COLOR_TEAM2)
745                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, colormapPaletteColor(e.team - 1, FALSE));
746                         else
747                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.75 0.75 0.75');
748                 }
749                 WaypointSprite_Ping(e.sprite);
750         }
751 }
752
753 string onslaught_controlpoint_waypointsprite_for_team(entity e, float t)
754 {
755         float a;
756         if(t != -1)
757         {
758                 a = onslaught_controlpoint_attackable(e, t);
759                 if(a == 3 || a == 4) // ATTACK/TOUCH THIS ONE NOW
760                 {
761                         if(e.team == COLOR_TEAM1)
762                                 return "ons-cp-atck-red";
763                         else if(e.team == COLOR_TEAM2)
764                                 return "ons-cp-atck-blue";
765                         else
766                                 return "ons-cp-atck-neut";
767                 }
768                 else if(a == -2) // DEFEND THIS ONE NOW
769                 {
770                         if(e.team == COLOR_TEAM1)
771                                 return "ons-cp-dfnd-red";
772                         else if(e.team == COLOR_TEAM2)
773                                 return "ons-cp-dfnd-blue";
774                 }
775                 else if(e.team == t || a == -1 || a == 1) // own point, or fire at it
776                 {
777                         if(e.team == COLOR_TEAM1)
778                                 return "ons-cp-red";
779                         else if(e.team == COLOR_TEAM2)
780                                 return "ons-cp-blue";
781                 }
782                 else if(a == 2) // touch it
783                         return "ons-cp-neut";
784         }
785         else
786         {
787                 if(e.team == COLOR_TEAM1)
788                         return "ons-cp-red";
789                 else if(e.team == COLOR_TEAM2)
790                         return "ons-cp-blue";
791                 else
792                         return "ons-cp-neut";
793         }
794         return "";
795 }
796
797 void onslaught_controlpoint_updatesprite(entity e)
798 {
799         string s1, s2, s3;
800         s1 = onslaught_controlpoint_waypointsprite_for_team(e, COLOR_TEAM1);
801         s2 = onslaught_controlpoint_waypointsprite_for_team(e, COLOR_TEAM2);
802         s3 = onslaught_controlpoint_waypointsprite_for_team(e, -1);
803         WaypointSprite_UpdateSprites(e.sprite, s1, s2, s3);
804
805         float sh;
806         sh = !(onslaught_controlpoint_can_be_linked(e, COLOR_TEAM1) || onslaught_controlpoint_can_be_linked(e, COLOR_TEAM2));
807
808         if(e.lastteam != e.team + 2 || e.lastshielded != sh || e.iscaptured != e.lastcaptured)
809         {
810                 if(e.iscaptured) // don't mess up build bars!
811                 {
812                         if(sh)
813                         {
814                                 WaypointSprite_UpdateMaxHealth(e.sprite, 0);
815                         }
816                         else
817                         {
818                                 WaypointSprite_UpdateMaxHealth(e.sprite, e.goalentity.max_health);
819                                 WaypointSprite_UpdateHealth(e.sprite, e.goalentity.health);
820                         }
821                 }
822                 if(e.lastshielded)
823                 {
824                         if(e.team == COLOR_TEAM1 || e.team == COLOR_TEAM2)
825                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, 0.5 * colormapPaletteColor(e.team - 1, FALSE));
826                         else
827                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.5 0.5 0.5');
828                 }
829                 else
830                 {
831                         if(e.team == COLOR_TEAM1 || e.team == COLOR_TEAM2)
832                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, colormapPaletteColor(e.team - 1, FALSE));
833                         else
834                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.75 0.75 0.75');
835                 }
836                 WaypointSprite_Ping(e.sprite);
837
838                 e.lastteam = e.team + 2;
839                 e.lastshielded = sh;
840                 e.lastcaptured = e.iscaptured;
841         }
842 }
843
844 void onslaught_generator_reset()
845 {
846         self.team = self.team_saved;
847         self.lasthealth = self.max_health = self.health = autocvar_g_onslaught_gen_health;
848         self.takedamage = DAMAGE_AIM;
849         self.bot_attack = TRUE;
850         self.iscaptured = TRUE;
851         self.islinked = TRUE;
852         self.isshielded = TRUE;
853         self.enemy.solid = SOLID_NOT;
854         self.think = onslaught_generator_delayed;
855         self.nextthink = time + 0.2;
856         setmodel(self, "models/onslaught/generator.md3");
857         setsize(self, '-52 -52 -14', '52 52 75');
858
859         if (!self.noalign)
860         droptofloor();
861
862         WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
863         WaypointSprite_UpdateHealth(self.sprite, self.health);
864 }
865
866 /*QUAKED spawnfunc_onslaught_generator (0 .5 .8) (-32 -32 -24) (32 32 64)
867   Base generator.
868
869   spawnfunc_onslaught_link entities can target this.
870
871 keys:
872 "team" - team that owns this generator (5 = red, 14 = blue, etc), MUST BE SET.
873 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
874  */
875 void spawnfunc_onslaught_generator()
876 {
877         if (!g_onslaught)
878         {
879                 remove(self);
880                 return;
881         }
882
883         //entity e;
884         precache_model("models/onslaught/generator.md3");
885         precache_model("models/onslaught/generator_shield.md3");
886         precache_model("models/onslaught/generator_dmg1.md3");
887         precache_model("models/onslaught/generator_dmg2.md3");
888         precache_model("models/onslaught/generator_dmg3.md3");
889         precache_model("models/onslaught/generator_dmg4.md3");
890         precache_model("models/onslaught/generator_dmg5.md3");
891         precache_model("models/onslaught/generator_dmg6.md3");
892         precache_model("models/onslaught/generator_dmg7.md3");
893         precache_model("models/onslaught/generator_dmg8.md3");
894         precache_model("models/onslaught/generator_dmg9.md3");
895         precache_model("models/onslaught/generator_dead.md3");
896         precache_model("models/onslaught/shockwave.md3");
897         precache_model("models/onslaught/shockwavetransring.md3");
898         precache_model("models/onslaught/gen_gib1.md3");
899         precache_model("models/onslaught/gen_gib2.md3");
900         precache_model("models/onslaught/gen_gib3.md3");
901         precache_model("models/onslaught/ons_ray.md3");
902         precache_sound("onslaught/generator_decay.wav");
903         precache_sound("weapons/grenade_impact.wav");
904         precache_sound("weapons/rocket_impact.wav");
905         precache_sound("onslaught/generator_underattack.wav");
906         precache_sound("onslaught/shockwave.wav");
907         precache_sound("onslaught/ons_hit1.wav");
908         precache_sound("onslaught/ons_hit2.wav");
909         precache_sound("onslaught/electricity_explode.wav");
910         if (!self.team)
911                 objerror("team must be set");
912         
913         if(self.team == COLOR_TEAM1)
914         ons_red_generator = self;
915
916         if(self.team == COLOR_TEAM2)
917         ons_blue_generator = self;
918         
919         self.team_saved = self.team;
920         self.colormap = 1024 + (self.team - 1) * 17;
921         self.solid = SOLID_BBOX;
922         self.movetype = MOVETYPE_NONE;
923         self.lasthealth = self.max_health = self.health = autocvar_g_onslaught_gen_health;
924         setmodel(self, "models/onslaught/generator.md3");
925         setsize(self, '-52 -52 -14', '52 52 75');
926         setorigin(self, self.origin);
927         self.takedamage = DAMAGE_AIM;
928         self.bot_attack = TRUE;
929         self.event_damage = onslaught_generator_damage;
930         self.iscaptured = TRUE;
931         self.islinked = TRUE;
932         self.isshielded = TRUE;
933         // helper entity that create fx when generator is damaged
934         onslaught_generator_damage_spawn(self);
935         // spawn shield model which indicates whether this can be damaged
936         self.enemy = spawn();
937         setattachment(self.enemy , self, "");
938         self.enemy.classname = "onslaught_generator_shield";
939         self.enemy.solid = SOLID_NOT;
940         self.enemy.movetype = MOVETYPE_NONE;
941         self.enemy.effects = EF_ADDITIVE;
942         setmodel(self.enemy, "models/onslaught/generator_shield.md3");
943         //setorigin(e, self.origin);
944         self.enemy.colormap = self.colormap;
945         self.enemy.team = self.team;
946         //self.think = onslaught_generator_delayed;
947         //self.nextthink = time + 0.2;
948         InitializeEntity(self, onslaught_generator_delayed, INITPRIO_LAST);
949
950         WaypointSprite_SpawnFixed(string_null, self.origin + '0 0 128', self, sprite, RADARICON_NONE, '0 0 0');
951         WaypointSprite_UpdateRule(self.sprite, COLOR_TEAM2, SPRITERULE_TEAMPLAY);
952         WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
953         WaypointSprite_UpdateHealth(self.sprite, self.health);
954
955         waypoint_spawnforitem(self);
956
957         onslaught_updatelinks();
958         
959         self.reset = onslaught_generator_reset;
960 }
961
962 .float waslinked;
963 .float cp_bob_spd;
964 .vector cp_origin, cp_bob_origin, cp_bob_dmg;
965
966 float ons_notification_time_team1;
967 float ons_notification_time_team2;
968
969 void onslaught_controlpoint_icon_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
970 {
971         entity oself;
972         float nag;
973
974         if (damage <= 0)
975                 return;
976         if (self.owner.isshielded)
977         {
978                 // this is protected by a shield, so ignore the damage
979                 if (time > self.pain_finished)
980                         if (IS_PLAYER(attacker))
981                         {
982                                 play2(attacker, "onslaught/damageblockedbyshield.wav");
983                                 self.pain_finished = time + 1;
984                         }
985                 return;
986         }
987
988         if (IS_PLAYER(attacker))
989         {
990                 nag = FALSE;
991                 if(self.team == COLOR_TEAM1)
992                 {
993                         if(time - ons_notification_time_team1 > 10)
994                         {
995                                 nag = TRUE;
996                                 ons_notification_time_team1 = time;
997                         }
998                 }
999                 else if(self.team == COLOR_TEAM2)
1000                 {
1001                         if(time - ons_notification_time_team2 > 10)
1002                         {
1003                                 nag = TRUE;
1004                                 ons_notification_time_team2 = time;
1005                         }
1006                 }
1007                 else
1008                         nag = TRUE;
1009
1010                 if(nag)
1011                         play2team(self.team, "onslaught/controlpoint_underattack.wav");
1012         }
1013
1014         self.health = self.health - damage;
1015         if(self.owner.iscaptured)
1016                 WaypointSprite_UpdateHealth(self.owner.sprite, self.health);
1017         else
1018                 WaypointSprite_UpdateBuildFinished(self.owner.sprite, time + (self.max_health - self.health) / (self.count / sys_frametime));
1019         self.pain_finished = time + 1;
1020         self.punchangle = (2 * randomvec() - '1 1 1') * 45;
1021         self.cp_bob_dmg_z = (2 * random() - 1) * 15;
1022         // colormod flash when shot
1023         self.colormod = '2 2 2';
1024         // particles on every hit
1025         pointparticles(particleeffectnum("sparks"), hitloc, force*-1, 1);
1026         //sound on every hit
1027         if (random() < 0.5)
1028                 sound(self, CH_TRIGGER, "onslaught/ons_hit1.wav", VOL_BASE+0.3, ATTN_NORM);
1029         else
1030                 sound(self, CH_TRIGGER, "onslaught/ons_hit2.wav", VOL_BASE+0.3, ATTN_NORM);
1031
1032         if (self.health < 0)
1033         {
1034                 sound(self, CH_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
1035                 pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
1036                 {
1037                         string t;
1038                         t = ColoredTeamName(attacker.team);
1039                         bprint(ColoredTeamName(self.team), " ", self.message, " control point destroyed by ", t, "\n");
1040                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 25, "models/onslaught/controlpoint_icon_gib1.md3", 3, FALSE);
1041                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 45, "models/onslaught/controlpoint_icon_gib2.md3", 3, FALSE);
1042                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 45, "models/onslaught/controlpoint_icon_gib2.md3", 3, FALSE);
1043                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 75, "models/onslaught/controlpoint_icon_gib4.md3", 3, FALSE);
1044                         ons_throwgib(self.origin, (2 * randomvec() - '1 1 1') * 75, "models/onslaught/controlpoint_icon_gib4.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                 }
1048                 self.owner.goalentity = world;
1049                 self.owner.islinked = FALSE;
1050                 self.owner.iscaptured = FALSE;
1051                 self.owner.team = 0;
1052                 self.owner.colormap = 1024;
1053
1054                 WaypointSprite_UpdateMaxHealth(self.owner.sprite, 0);
1055
1056                 onslaught_updatelinks();
1057
1058                 // Use targets now (somebody make sure this is in the right place..)
1059                 oself = self;
1060                 self = self.owner;
1061                 activator = self;
1062                 SUB_UseTargets ();
1063                 self = oself;
1064
1065
1066                 self.owner.waslinked = self.owner.islinked;
1067                 if(self.owner.model != "models/onslaught/controlpoint_pad.md3")
1068                         setmodel(self.owner, "models/onslaught/controlpoint_pad.md3");
1069                 //setsize(self, '-32 -32 0', '32 32 8');
1070
1071                 remove(self);
1072         }
1073 }
1074
1075 void onslaught_controlpoint_icon_think()
1076 {
1077         entity oself;
1078         self.nextthink = time + sys_frametime;
1079         
1080         if(autocvar_g_onslaught_controlpoints_proxycap)
1081         {        
1082         float _enemy_count = 0;
1083         float _friendly_count = 0;
1084         float _dist;
1085         entity _player;
1086         
1087         FOR_EACH_PLAYER(_player)
1088         {
1089             if(!_player.deadflag)
1090             {
1091                 _dist = vlen(_player.origin - self.origin);
1092                 if(_dist < autocvar_g_onslaught_controlpoints_proxycap_distance)
1093                 {
1094                     if(_player.team == self.team)
1095                         ++_friendly_count;
1096                     else
1097                         ++_enemy_count;
1098                 }
1099             }
1100         }
1101
1102         _friendly_count = _friendly_count * (autocvar_g_onslaught_controlpoints_proxycap_dps * sys_frametime);
1103         _enemy_count = _enemy_count * (autocvar_g_onslaught_controlpoints_proxycap_dps * sys_frametime);
1104         
1105         self.health = bound(0, self.health + (_friendly_count - _enemy_count), self.max_health);
1106         if(self.health <= 0)
1107         {
1108             onslaught_controlpoint_icon_damage(self, self, 1, 0, self.origin, '0 0 0');
1109             return;
1110         }
1111     }
1112     
1113         if (time > self.pain_finished + 5)
1114         {
1115                 if(self.health < self.max_health)
1116                 {
1117                         self.health = self.health + self.count;
1118                         if (self.health >= self.max_health)
1119                                 self.health = self.max_health;
1120                         WaypointSprite_UpdateHealth(self.owner.sprite, self.health);
1121                 }
1122         }
1123         if (self.health < self.max_health * 0.25)
1124                 setmodel(self, "models/onslaught/controlpoint_icon_dmg3.md3");
1125         else if (self.health < self.max_health * 0.50)
1126                 setmodel(self, "models/onslaught/controlpoint_icon_dmg2.md3");
1127         else if (self.health < self.max_health * 0.75)
1128                 setmodel(self, "models/onslaught/controlpoint_icon_dmg1.md3");
1129         else if (self.health < self.max_health * 0.90)
1130                 setmodel(self, "models/onslaught/controlpoint_icon.md3");
1131         // colormod flash when shot
1132         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
1133
1134         if(self.owner.islinked != self.owner.waslinked)
1135         {
1136                 // unteam the spawnpoint if needed
1137                 float t;
1138                 t = self.owner.team;
1139                 if(!self.owner.islinked)
1140                         self.owner.team = 0;
1141
1142                 oself = self;
1143                 self = self.owner;
1144                 activator = self;
1145                 SUB_UseTargets ();
1146                 self = oself;
1147
1148                 self.owner.team = t;
1149
1150                 self.owner.waslinked = self.owner.islinked;
1151         }
1152
1153         if (self.punchangle_x > 0)
1154         {
1155                 self.punchangle_x = self.punchangle_x - 60 * sys_frametime;
1156                 if (self.punchangle_x < 0)
1157                         self.punchangle_x = 0;
1158         }
1159         else if (self.punchangle_x < 0)
1160         {
1161                 self.punchangle_x = self.punchangle_x + 60 * sys_frametime;
1162                 if (self.punchangle_x > 0)
1163                         self.punchangle_x = 0;
1164         }
1165
1166         if (self.punchangle_y > 0)
1167         {
1168                 self.punchangle_y = self.punchangle_y - 60 * sys_frametime;
1169                 if (self.punchangle_y < 0)
1170                         self.punchangle_y = 0;
1171         }
1172         else if (self.punchangle_y < 0)
1173         {
1174                 self.punchangle_y = self.punchangle_y + 60 * sys_frametime;
1175                 if (self.punchangle_y > 0)
1176                         self.punchangle_y = 0;
1177         }
1178
1179         if (self.punchangle_z > 0)
1180         {
1181                 self.punchangle_z = self.punchangle_z - 60 * sys_frametime;
1182                 if (self.punchangle_z < 0)
1183                         self.punchangle_z = 0;
1184         }
1185         else if (self.punchangle_z < 0)
1186         {
1187                 self.punchangle_z = self.punchangle_z + 60 * sys_frametime;
1188                 if (self.punchangle_z > 0)
1189                         self.punchangle_z = 0;
1190         }
1191
1192         self.angles_x = self.punchangle_x;
1193         self.angles_y = self.punchangle_y + self.mangle_y;
1194         self.angles_z = self.punchangle_z;
1195         self.mangle_y = self.mangle_y + 45 * sys_frametime;
1196
1197         self.cp_bob_origin_z = 4 * PI * (1 - cos(self.cp_bob_spd));
1198         self.cp_bob_spd = self.cp_bob_spd + 1.875 * sys_frametime;
1199         if(self.cp_bob_dmg_z > 0)
1200                 self.cp_bob_dmg_z = self.cp_bob_dmg_z - 3 * sys_frametime;
1201         else
1202                 self.cp_bob_dmg_z = 0;
1203         setorigin(self,self.cp_origin + self.cp_bob_origin + self.cp_bob_dmg);
1204
1205         // damaged fx
1206         if(random() < 0.6 - self.health / self.max_health)
1207         {
1208                 pointparticles(particleeffectnum("electricity_sparks"), self.origin + randompos('-10 -10 -20', '10 10 20'), '0 0 0', 1);
1209
1210                 if(random() > 0.8)
1211                         sound(self, CH_PAIN, "onslaught/ons_spark1.wav", VOL_BASE, ATTN_NORM);
1212                 else if (random() > 0.5)
1213                         sound(self, CH_PAIN, "onslaught/ons_spark2.wav", VOL_BASE, ATTN_NORM);
1214         }
1215 }
1216
1217 void onslaught_controlpoint_icon_buildthink()
1218 {
1219         entity oself;
1220         float a;
1221
1222         self.nextthink = time + sys_frametime;
1223
1224         // only do this if there is power
1225         a = onslaught_controlpoint_can_be_linked(self.owner, self.owner.team);
1226         if(!a)
1227                 return;
1228     
1229         self.health = self.health + self.count;
1230
1231         if (self.health >= self.max_health)
1232         {
1233                 self.health = self.max_health;
1234                 self.count = autocvar_g_onslaught_cp_regen * sys_frametime; // slow repair rate from now on
1235                 self.think = onslaught_controlpoint_icon_think;
1236                 sound(self, CH_TRIGGER, "onslaught/controlpoint_built.wav", VOL_BASE, ATTN_NORM);
1237                 bprint(ColoredTeamName(self.team), " captured ", self.owner.message, " control point\n");
1238                 self.owner.iscaptured = TRUE;
1239
1240                 WaypointSprite_UpdateMaxHealth(self.owner.sprite, self.max_health);
1241                 WaypointSprite_UpdateHealth(self.owner.sprite, self.health);
1242
1243                 onslaught_updatelinks();
1244
1245                 // Use targets now (somebody make sure this is in the right place..)
1246                 oself = self;
1247                 self = self.owner;
1248                 activator = self;
1249                 SUB_UseTargets ();
1250                 self = oself;
1251                 self.cp_origin = self.origin;
1252                 self.cp_bob_origin = '0 0 0.1';
1253                 self.cp_bob_spd = 0;
1254         }
1255         self.alpha = self.health / self.max_health;
1256         // colormod flash when shot
1257         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
1258         if(self.owner.model != "models/onslaught/controlpoint_pad2.md3")
1259                 setmodel(self.owner, "models/onslaught/controlpoint_pad2.md3");
1260         //setsize(self, '-32 -32 0', '32 32 8');
1261
1262         if(random() < 0.9 - self.health / self.max_health)
1263                 pointparticles(particleeffectnum("rage"), self.origin + 10 * randomvec(), '0 0 -1', 1);
1264 }
1265
1266
1267
1268
1269 void onslaught_controlpoint_touch()
1270 {
1271         entity e;
1272         float a;
1273         if not(IS_PLAYER(other))
1274                 return;
1275         a = onslaught_controlpoint_attackable(self, other.team);
1276         if(a != 2 && a != 4)
1277                 return;
1278         // we've verified that this player has a legitimate claim to this point,
1279         // so start building the captured point icon (which only captures this
1280         // point if it successfully builds without being destroyed first)
1281         self.goalentity = e = spawn();
1282         e.classname = "onslaught_controlpoint_icon";
1283         e.owner = self;
1284         e.max_health = autocvar_g_onslaught_cp_health;
1285         e.health = autocvar_g_onslaught_cp_buildhealth;
1286         e.solid = SOLID_BBOX;
1287         e.movetype = MOVETYPE_NONE;
1288         setmodel(e, "models/onslaught/controlpoint_icon.md3");
1289         setsize(e, '-32 -32 -32', '32 32 32');
1290         setorigin(e, self.origin + '0 0 96');
1291         e.takedamage = DAMAGE_AIM;
1292         e.bot_attack = TRUE;
1293         e.event_damage = onslaught_controlpoint_icon_damage;
1294         e.team = other.team;
1295         e.colormap = 1024 + (e.team - 1) * 17;
1296         e.think = onslaught_controlpoint_icon_buildthink;
1297         e.nextthink = time + sys_frametime;
1298         e.count = (e.max_health - e.health) * sys_frametime / autocvar_g_onslaught_cp_buildtime; // how long it takes to build
1299         sound(e, CH_TRIGGER, "onslaught/controlpoint_build.wav", VOL_BASE, ATTN_NORM);
1300         self.team = e.team;
1301         self.colormap = e.colormap;
1302         WaypointSprite_UpdateBuildFinished(self.sprite, time + (e.max_health - e.health) / (e.count / sys_frametime));
1303         onslaught_updatelinks();
1304 }
1305
1306 void onslaught_controlpoint_reset()
1307 {
1308         if(self.goalentity && self.goalentity != world)
1309                 remove(self.goalentity);
1310         self.goalentity = world;
1311         self.team = 0;
1312         self.colormap = 1024;
1313         self.iscaptured = FALSE;
1314         self.islinked = FALSE;
1315         self.isshielded = TRUE;
1316         self.enemy.solid = SOLID_NOT;
1317         self.enemy.colormap = self.colormap;
1318         self.think = self.enemy.think = func_null;
1319         self.nextthink = 0; // don't like func_null :P
1320         setmodel(self, "models/onslaught/controlpoint_pad.md3");
1321         //setsize(self, '-32 -32 0', '32 32 8');
1322
1323         WaypointSprite_UpdateMaxHealth(self.sprite, 0);
1324
1325         onslaught_updatelinks();
1326
1327         activator = self;
1328         SUB_UseTargets(); // to reset the structures, playerspawns etc.
1329 }
1330
1331 /*QUAKED spawnfunc_onslaught_controlpoint (0 .5 .8) (-32 -32 0) (32 32 128)
1332   Control point. Be sure to give this enough clearance so that the shootable part has room to exist
1333
1334   This should link to an spawnfunc_onslaught_controlpoint entity or spawnfunc_onslaught_generator entity.
1335
1336 keys:
1337 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
1338 "target" - target any entities that are tied to this control point, such as vehicles and buildable structure entities.
1339 "message" - name of this control point (should reflect the location in the map, such as "center bridge", "north tower", etc)
1340  */
1341  
1342  /*
1343 void onslaught_controlpoint_think()
1344 {
1345     self.nextthink = time;
1346         //if(autocvar_g_onslaught_controlpoints_proxycap)
1347                     
1348     float _enemy_count;
1349     float _friendly_count;
1350     float _dist;
1351     entity _player;
1352     
1353     FOR_EACH_PLAYER(_player)
1354     {
1355         if(!_player.deadflag)
1356         {
1357             _dist = vlen(_player.origin - self.origin);
1358             if(_dist < autocvar_g_onslaught_controlpoints_proxycap_distance)
1359             {
1360                 if(_player.team == self.team)
1361                     ++_friendly_count;
1362                 else
1363                     ++_enemy_count;
1364             }
1365         }
1366     }
1367
1368     _friendly_count = _friendly_count * (autocvar_g_onslaught_controlpoints_proxycap_dps * sys_frametime);
1369     _enemy_count = _enemy_count * (autocvar_g_onslaught_controlpoints_proxycap_dps * sys_frametime);
1370     
1371     self.health = bound(0, self.health + (_friendly_count - _enemy_count), self.max_health);
1372     if(self.health <= 0)
1373     {
1374         onslaught_controlpoint_icon_damage(self, self, 1, 0, self.origin, '0 0 0');
1375         return;
1376     }
1377     
1378     if(self.health == max_health)
1379         {
1380             
1381         }
1382 }
1383 */
1384
1385 void spawnfunc_onslaught_controlpoint()
1386 {
1387         //entity e;
1388         if (!g_onslaught)
1389         {
1390                 remove(self);
1391                 return;
1392         }
1393         precache_model("models/onslaught/controlpoint_pad.md3");
1394         precache_model("models/onslaught/controlpoint_pad2.md3");
1395         precache_model("models/onslaught/controlpoint_shield.md3");
1396         precache_model("models/onslaught/controlpoint_icon.md3");
1397         precache_model("models/onslaught/controlpoint_icon_dmg1.md3");
1398         precache_model("models/onslaught/controlpoint_icon_dmg2.md3");
1399         precache_model("models/onslaught/controlpoint_icon_dmg3.md3");
1400         precache_model("models/onslaught/controlpoint_icon_gib1.md3");
1401         precache_model("models/onslaught/controlpoint_icon_gib2.md3");
1402         precache_model("models/onslaught/controlpoint_icon_gib4.md3");
1403         precache_sound("onslaught/controlpoint_build.wav");
1404         precache_sound("onslaught/controlpoint_built.wav");
1405         precache_sound("weapons/grenade_impact.wav");
1406         precache_sound("onslaught/damageblockedbyshield.wav");
1407         precache_sound("onslaught/controlpoint_underattack.wav");
1408         precache_sound("onslaught/ons_spark1.wav");
1409         precache_sound("onslaught/ons_spark2.wav");
1410         self.solid = SOLID_BBOX;
1411         self.movetype = MOVETYPE_NONE;
1412         setmodel(self, "models/onslaught/controlpoint_pad.md3");
1413         //setsize(self, '-32 -32 0', '32 32 8');
1414         if (!self.noalign)
1415         droptofloor();
1416         
1417         setorigin(self, self.origin);
1418         self.touch = onslaught_controlpoint_touch;
1419         self.team = 0;
1420         self.colormap = 1024;
1421         self.iscaptured = FALSE;
1422         self.islinked = FALSE;
1423         self.isshielded = TRUE;
1424         // spawn shield model which indicates whether this can be damaged
1425         self.enemy = spawn();
1426         self.enemy.classname = "onslaught_controlpoint_shield";
1427         self.enemy.solid = SOLID_NOT;
1428         self.enemy.movetype = MOVETYPE_NONE;
1429         self.enemy.effects = EF_ADDITIVE;
1430         setmodel(self.enemy , "models/onslaught/controlpoint_shield.md3");
1431         
1432         setattachment(self.enemy , self, "");
1433         //setsize(e, '-32 -32 0', '32 32 128');
1434
1435         //setorigin(e, self.origin);
1436         self.enemy.colormap = self.colormap;
1437
1438         waypoint_spawnforitem(self);
1439
1440         WaypointSprite_SpawnFixed(string_null, self.origin + '0 0 128', self, sprite, RADARICON_NONE, '0 0 0');
1441         WaypointSprite_UpdateRule(self.sprite, COLOR_TEAM2, SPRITERULE_TEAMPLAY);
1442
1443         onslaught_updatelinks();
1444         
1445         self.reset = onslaught_controlpoint_reset;
1446 }
1447
1448 float onslaught_link_send(entity to, float sendflags)
1449 {
1450         WriteByte(MSG_ENTITY, ENT_CLIENT_RADARLINK);
1451         WriteByte(MSG_ENTITY, sendflags);
1452         if(sendflags & 1)
1453         {
1454                 WriteCoord(MSG_ENTITY, self.goalentity.origin_x);
1455                 WriteCoord(MSG_ENTITY, self.goalentity.origin_y);
1456                 WriteCoord(MSG_ENTITY, self.goalentity.origin_z);
1457         }
1458         if(sendflags & 2)
1459         {
1460                 WriteCoord(MSG_ENTITY, self.enemy.origin_x);
1461                 WriteCoord(MSG_ENTITY, self.enemy.origin_y);
1462                 WriteCoord(MSG_ENTITY, self.enemy.origin_z);
1463         }
1464         if(sendflags & 4)
1465         {
1466                 WriteByte(MSG_ENTITY, self.clientcolors); // which is goalentity's color + enemy's color * 16
1467         }
1468         return TRUE;
1469 }
1470
1471 void onslaught_link_checkupdate()
1472 {
1473         // TODO check if the two sides have moved (currently they won't move anyway)
1474         float redpower, bluepower;
1475
1476         redpower = bluepower = 0;
1477         if(self.goalentity.islinked)
1478         {
1479                 if(self.goalentity.team == COLOR_TEAM1)
1480                         redpower = 1;
1481                 else if(self.goalentity.team == COLOR_TEAM2)
1482                         bluepower = 1;
1483         }
1484         if(self.enemy.islinked)
1485         {
1486                 if(self.enemy.team == COLOR_TEAM1)
1487                         redpower = 2;
1488                 else if(self.enemy.team == COLOR_TEAM2)
1489                         bluepower = 2;
1490         }
1491
1492         float cc;
1493         if(redpower == 1 && bluepower == 2)
1494                 cc = (COLOR_TEAM1 - 1) * 0x01 + (COLOR_TEAM2 - 1) * 0x10;
1495         else if(redpower == 2 && bluepower == 1)
1496                 cc = (COLOR_TEAM1 - 1) * 0x10 + (COLOR_TEAM2 - 1) * 0x01;
1497         else if(redpower)
1498                 cc = (COLOR_TEAM1 - 1) * 0x11;
1499         else if(bluepower)
1500                 cc = (COLOR_TEAM2 - 1) * 0x11;
1501         else
1502                 cc = 0;
1503
1504         //print(etos(self), " rp=", ftos(redpower), " bp=", ftos(bluepower), " ");
1505         //print("cc=", ftos(cc), "\n");
1506
1507         if(cc != self.clientcolors)
1508         {
1509                 self.clientcolors = cc;
1510                 self.SendFlags |= 4;
1511         }
1512
1513         self.nextthink = time;
1514 }
1515
1516 void onslaught_link_delayed()
1517 {
1518         self.goalentity = find(world, targetname, self.target);
1519         self.enemy = find(world, targetname, self.target2);
1520         if (!self.goalentity)
1521                 objerror("can not find target\n");
1522         if (!self.enemy)
1523                 objerror("can not find target2\n");
1524         dprint(etos(self.goalentity), " linked with ", etos(self.enemy), "\n");
1525         self.SendFlags |= 3;
1526         self.think = onslaught_link_checkupdate;
1527         self.nextthink = time;
1528 }
1529
1530 /*QUAKED spawnfunc_onslaught_link (0 .5 .8) (-16 -16 -16) (16 16 16)
1531   Link between control points.
1532
1533   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.
1534
1535 keys:
1536 "target" - first control point.
1537 "target2" - second control point.
1538  */
1539 void spawnfunc_onslaught_link()
1540 {
1541         if (!g_onslaught)
1542         {
1543                 remove(self);
1544                 return;
1545         }
1546         if (self.target == "" || self.target2 == "")
1547                 objerror("target and target2 must be set\n");
1548         InitializeEntity(self, onslaught_link_delayed, INITPRIO_FINDTARGET);
1549         Net_LinkEntity(self, FALSE, 0, onslaught_link_send);
1550 }
1551
1552 MUTATOR_HOOKFUNCTION(ons_BuildMutatorsString)
1553 {
1554         ret_string = strcat(ret_string, ":ONS");
1555         return 0;
1556 }
1557
1558 MUTATOR_HOOKFUNCTION(ons_BuildMutatorsPrettyString)
1559 {
1560         ret_string = strcat(ret_string, ", Onslught");
1561         return 0;
1562 }
1563
1564 MUTATOR_HOOKFUNCTION(ons_Spawn_Score)
1565 {
1566     
1567     /*
1568     float _neer_home = (random() > 0.5 ? TRUE : FALSE);
1569     
1570         RandomSelection_Init();
1571         
1572         if(self.team == COLOR_TEAM1)
1573         RandomSelection_Add(ons_red_generator, 0, string_null, 1, 1);
1574         
1575         if(self.team == COLOR_TEAM2)
1576         RandomSelection_Add(ons_blue_generator, 0, string_null, 1, 1);
1577         
1578         entity _cp = findchain(classname, "onslaught_controlpoint"):
1579         while _cp;
1580         {
1581             if(_cp.team == self.team)            
1582             RandomSelection_Add(_cp, 0, string_null, 1, 1);
1583                 
1584                 _cp = _cp.chain;
1585         }
1586
1587         if(RandomSelection_chosen_ent)
1588         {
1589                 self.tur_head = RandomSelection_chosen_ent;
1590                 spawn_score_x += SPAWN_PRIO_NEAR_TEAMMATE_FOUND;
1591         }
1592         else if(self.team == spawn_spot.team)
1593                 spawn_score_x += SPAWN_PRIO_NEAR_TEAMMATE_SAMETEAM; // prefer same team, if we can't find a spawn near teammate
1594     
1595     */
1596     
1597         return 0;
1598 }
1599
1600 MUTATOR_HOOKFUNCTION(ons_PlayerSpawn)
1601 {
1602     if(!autocvar_g_onslaught_spawn_at_controlpoints)
1603         return 0;
1604         
1605     if(random() < 0.5)  // 50/50 chane to use default spawnsystem.
1606         return 0;
1607     
1608     float _close_to_home = ((random() > 0.5) ? TRUE : FALSE);
1609     entity _best = world, _trg_gen = world;
1610     float _score, _best_score = MAX_SHOT_DISTANCE;
1611     
1612         RandomSelection_Init();
1613     
1614         if(self.team == COLOR_TEAM1)
1615         {
1616             if(!_close_to_home)
1617             _trg_gen = ons_blue_generator;
1618         else    
1619             _trg_gen  = ons_red_generator;        
1620         }
1621         
1622         if(self.team == COLOR_TEAM2)
1623         {
1624             if(_close_to_home)
1625             _trg_gen = ons_blue_generator;
1626         else    
1627             _trg_gen  = ons_red_generator;        
1628         }
1629         
1630         entity _cp = findchain(classname, "onslaught_controlpoint");
1631         while(_cp)
1632         {
1633             if(_cp.team == self.team)            
1634         {            
1635             _score = vlen(_trg_gen.origin - _cp.origin);
1636             if(_score < _best_score)
1637             {
1638                 _best = _cp;
1639                 _best_score = _score;            
1640             }
1641         }               
1642                 _cp = _cp.chain;
1643         }
1644         
1645     vector _loc;        
1646     float i;    
1647     if(_best)
1648     {
1649         for(i = 0; i < 10; ++i)
1650         {
1651             _loc = _best.origin + '0 0 96';
1652             _loc += ('0 1 0' * random()) * 128; 
1653             tracebox(_loc, PL_MIN, PL_MAX, _loc, MOVE_NORMAL, self);
1654             if(trace_fraction == 1.0 && !trace_startsolid)
1655             {
1656                 setorigin(self, _loc);
1657                 self.angles = normalize(_loc - _best.origin) * RAD2DEG;
1658                 return 0;
1659             }
1660         }
1661     }
1662     else
1663     {
1664         if(!autocvar_g_onslaught_spawn_at_generator)
1665             return 0;
1666         
1667         _trg_gen = ((self.team == COLOR_TEAM1) ? ons_red_generator : ons_blue_generator);
1668         
1669         for(i = 0; i < 10; ++i)
1670         {
1671             _loc = _trg_gen.origin + '0 0 96';
1672             _loc += ('0 1 0' * random()) * 128; 
1673             tracebox(_loc, PL_MIN, PL_MAX, _loc, MOVE_NORMAL, self);
1674             if(trace_fraction == 1.0 && !trace_startsolid)
1675             {
1676                 setorigin(self, _loc);
1677                 self.angles = normalize(_loc - _trg_gen.origin) * RAD2DEG;
1678                 return 0;
1679             }
1680         }
1681     }
1682     
1683     return 0;
1684 }
1685
1686 MUTATOR_DEFINITION(gamemode_onslaught)
1687 {
1688         //MUTATOR_HOOK(PlayerDies, nexball_BallDrop, CBC_ORDER_ANY);
1689         //MUTATOR_HOOK(MakePlayerObserver, nexball_BallDrop, CBC_ORDER_ANY);
1690         //MUTATOR_HOOK(ClientDisconnect, nexball_BallDrop, CBC_ORDER_ANY);
1691         //MUTATOR_HOOK(PlayerPreThink, nexball_PlayerPreThink, CBC_ORDER_ANY);
1692         MUTATOR_HOOK(BuildMutatorsPrettyString, ons_BuildMutatorsPrettyString, CBC_ORDER_ANY);
1693         MUTATOR_HOOK(BuildMutatorsString, ons_BuildMutatorsString, CBC_ORDER_ANY);
1694         MUTATOR_HOOK(PlayerSpawn, ons_PlayerSpawn, CBC_ORDER_ANY);
1695         //MUTATOR_HOOK(Spawn_Score, ons_Spawn_Score, CBC_ORDER_ANY);
1696         
1697         MUTATOR_ONADD
1698         {
1699                 if(time > 1) // game loads at time 1
1700                         error("This is a game type and it cannot be added at runtime.");
1701         }
1702
1703         MUTATOR_ONREMOVE
1704         {
1705                 print("This is a game type and it cannot be removed at runtime.");
1706                 return -1;
1707         }
1708
1709         return 0;
1710 }