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