]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_onslaught.qc
Merge branch 'master' into Mario/notifications
[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_cp_proxydecap;
4 var float autocvar_g_onslaught_cp_proxydecap_distance = 512;
5 var float autocvar_g_onslaught_cp_proxydecap_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 entity ons_red_generator;
26 entity ons_blue_generator;
27
28 void ons_gib_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
29 {
30         self.velocity = self.velocity + vforce;
31 }
32
33 .float giblifetime;
34 void ons_throwgib_think()
35 {
36         float d;
37
38         self.nextthink = time + 0.05;
39
40         d = self.giblifetime - time;
41
42         if(d<0)
43         {
44                 self.think = SUB_Remove;
45                 return;
46         }
47         if(d<1)
48                 self.alpha = d;
49
50         if(d>2)
51         if(random()<0.6)
52                 pointparticles(particleeffectnum("onslaught_generator_gib_flame"), self.origin, '0 0 0', 1);
53 }
54
55 void ons_throwgib(vector v_from, vector v_to, string smodel, float f_lifetime, float b_burn)
56 {
57         entity gib;
58
59         gib = spawn();
60
61         setmodel(gib, smodel);
62         setorigin(gib, v_from);
63         gib.solid = SOLID_BBOX;
64         gib.movetype = MOVETYPE_BOUNCE;
65         gib.takedamage = DAMAGE_YES;
66         gib.event_damage = ons_gib_damage;
67         gib.health = -1;
68         gib.effects = EF_LOWPRECISION;
69         gib.flags = FL_NOTARGET;
70         gib.velocity = v_to;
71         gib.giblifetime = time + f_lifetime;
72
73         if (b_burn)
74         {
75                 gib.think = ons_throwgib_think;
76                 gib.nextthink = time + 0.05;
77         }
78         else
79                 SUB_SetFade(gib, gib.giblifetime, 2);
80 }
81
82 void onslaught_updatelinks()
83 {
84         entity l, links;
85         float stop, t1, t2, t3, t4;
86         // first check if the game has ended
87         dprint("--- updatelinks ---\n");
88         links = findchain(classname, "onslaught_link");
89         // mark generators as being shielded and networked
90         l = findchain(classname, "onslaught_generator");
91         while (l)
92         {
93                 if (l.iscaptured)
94                         dprint(etos(l), " (generator) belongs to team ", ftos(l.team), "\n");
95                 else
96                         dprint(etos(l), " (generator) is destroyed\n");
97                 l.islinked = l.iscaptured;
98                 l.isshielded = l.iscaptured;
99                 l = l.chain;
100         }
101         // mark points as shielded and not networked
102         l = findchain(classname, "onslaught_controlpoint");
103         while (l)
104         {
105                 l.islinked = FALSE;
106                 l.isshielded = TRUE;
107                 l.isgenneighbor_red = FALSE;
108                 l.isgenneighbor_blue = FALSE;
109                 l.iscpneighbor_red = FALSE;
110                 l.iscpneighbor_blue = FALSE;
111                 dprint(etos(l), " (point) belongs to team ", ftos(l.team), "\n");
112                 l = l.chain;
113         }
114         // flow power outward from the generators through the network
115         l = links;
116         while (l)
117         {
118                 dprint(etos(l), " (link) connects ", etos(l.goalentity), " with ", etos(l.enemy), "\n");
119                 l = l.chain;
120         }
121         stop = FALSE;
122         while (!stop)
123         {
124                 stop = TRUE;
125                 l = links;
126                 while (l)
127                 {
128                         // if both points are captured by the same team, and only one of
129                         // them is powered, mark the other one as powered as well
130                         if (l.enemy.iscaptured && l.goalentity.iscaptured)
131                                 if (l.enemy.islinked != l.goalentity.islinked)
132                                         if (l.enemy.team == l.goalentity.team)
133                                         {
134                                                 if (!l.goalentity.islinked)
135                                                 {
136                                                         stop = FALSE;
137                                                         l.goalentity.islinked = TRUE;
138                                                         dprint(etos(l), " (link) is marking ", etos(l.goalentity), " (point) because its team matches ", etos(l.enemy), " (point)\n");
139                                                 }
140                                                 else if (!l.enemy.islinked)
141                                                 {
142                                                         stop = FALSE;
143                                                         l.enemy.islinked = TRUE;
144                                                         dprint(etos(l), " (link) is marking ", etos(l.enemy), " (point) because its team matches ", etos(l.goalentity), " (point)\n");
145                                                 }
146                                         }
147                         l = l.chain;
148                 }
149         }
150         // now that we know which points are powered we can mark their neighbors
151         // as unshielded if team differs
152         l = links;
153         while (l)
154         {
155                 if (l.goalentity.islinked)
156                 {
157                         if (l.goalentity.team != l.enemy.team)
158                         {
159                                 dprint(etos(l), " (link) is unshielding ", etos(l.enemy), " (point) because its team does not match ", etos(l.goalentity), " (point)\n");
160                                 l.enemy.isshielded = FALSE;
161                         }
162                         if(l.goalentity.classname == "onslaught_generator")
163                         {
164                                 if(l.goalentity.team == NUM_TEAM_1)
165                                         l.enemy.isgenneighbor_red = TRUE;
166                                 else if(l.goalentity.team == NUM_TEAM_2)
167                                         l.enemy.isgenneighbor_blue = TRUE;
168                         }
169                         else
170                         {
171                                 if(l.goalentity.team == NUM_TEAM_1)
172                                         l.enemy.iscpneighbor_red = TRUE;
173                                 else if(l.goalentity.team == NUM_TEAM_2)
174                                         l.enemy.iscpneighbor_blue = TRUE;
175                         }
176                 }
177                 if (l.enemy.islinked)
178                 {
179                         if (l.goalentity.team != l.enemy.team)
180                         {
181                                 dprint(etos(l), " (link) is unshielding ", etos(l.goalentity), " (point) because its team does not match ", etos(l.enemy), " (point)\n");
182                                 l.goalentity.isshielded = FALSE;
183                         }
184                         if(l.enemy.classname == "onslaught_generator")
185                         {
186                                 if(l.enemy.team == NUM_TEAM_1)
187                                         l.goalentity.isgenneighbor_red = TRUE;
188                                 else if(l.enemy.team == NUM_TEAM_2)
189                                         l.goalentity.isgenneighbor_blue = TRUE;
190                         }
191                         else
192                         {
193                                 if(l.enemy.team == NUM_TEAM_1)
194                                         l.goalentity.iscpneighbor_red = TRUE;
195                                 else if(l.enemy.team == NUM_TEAM_2)
196                                         l.goalentity.iscpneighbor_blue = TRUE;
197                         }
198                 }
199                 l = l.chain;
200         }
201         // now update the takedamage and alpha variables on generator shields
202         l = findchain(classname, "onslaught_generator");
203         while (l)
204         {
205                 if (l.isshielded)
206                 {
207                         dprint(etos(l), " (generator) is shielded\n");
208                         l.enemy.alpha = 1;
209                         l.takedamage = DAMAGE_NO;
210                         l.bot_attack = FALSE;
211                 }
212                 else
213                 {
214                         dprint(etos(l), " (generator) is not shielded\n");
215                         l.enemy.alpha = -1;
216                         l.takedamage = DAMAGE_AIM;
217                         l.bot_attack = TRUE;
218                 }
219                 l = l.chain;
220         }
221         // now update the takedamage and alpha variables on control point icons
222         l = findchain(classname, "onslaught_controlpoint");
223         while (l)
224         {
225                 if (l.isshielded)
226                 {
227                         dprint(etos(l), " (point) is shielded\n");
228                         l.enemy.alpha = 1;
229                         if (l.goalentity)
230                         {
231                                 l.goalentity.takedamage = DAMAGE_NO;
232                                 l.goalentity.bot_attack = FALSE;
233                         }
234                 }
235                 else
236                 {
237                         dprint(etos(l), " (point) is not shielded\n");
238                         l.enemy.alpha = -1;
239                         if (l.goalentity)
240                         {
241                                 l.goalentity.takedamage = DAMAGE_AIM;
242                                 l.goalentity.bot_attack = TRUE;
243                         }
244                 }
245                 onslaught_controlpoint_updatesprite(l);
246                 l = l.chain;
247         }
248         // count generators owned by each team
249         t1 = t2 = t3 = t4 = 0;
250         l = findchain(classname, "onslaught_generator");
251         while (l)
252         {
253                 if (l.iscaptured)
254                 {
255                         if (l.team == NUM_TEAM_1) t1 = 1;
256                         if (l.team == NUM_TEAM_2) t2 = 1;
257                         if (l.team == NUM_TEAM_3) t3 = 1;
258                         if (l.team == NUM_TEAM_4) t4 = 1;
259                 }
260                 onslaught_generator_updatesprite(l);
261                 l = l.chain;
262         }
263         // see if multiple teams remain (if not, it's game over)
264         if (t1 + t2 + t3 + t4 < 2)
265                 dprint("--- game over ---\n");
266         else
267                 dprint("--- done updating links ---\n");
268 }
269
270 float onslaught_controlpoint_can_be_linked(entity cp, float t)
271 {
272         if(t == NUM_TEAM_1)
273         {
274                 if(cp.isgenneighbor_red)
275                         return 2;
276                 if(cp.iscpneighbor_red)
277                         return 1;
278         }
279         else if(t == NUM_TEAM_2)
280         {
281                 if(cp.isgenneighbor_blue)
282                         return 2;
283                 if(cp.iscpneighbor_blue)
284                         return 1;
285         }
286         return 0;
287         /*
288            entity e;
289         // check to see if this player has a legitimate claim to capture this
290         // control point - more specifically that there is a captured path of
291         // points leading back to the team generator
292         e = findchain(classname, "onslaught_link");
293         while (e)
294         {
295         if (e.goalentity == cp)
296         {
297         dprint(etos(e), " (link) connects to ", etos(e.enemy), " (point)");
298         if (e.enemy.islinked)
299         {
300         dprint(" which is linked");
301         if (e.enemy.team == t)
302         {
303         dprint(" and has the correct team!\n");
304         return 1;
305         }
306         else
307         dprint(" but has the wrong team\n");
308         }
309         else
310         dprint("\n");
311         }
312         else if (e.enemy == cp)
313         {
314         dprint(etos(e), " (link) connects to ", etos(e.goalentity), " (point)");
315         if (e.goalentity.islinked)
316         {
317         dprint(" which is linked");
318         if (e.goalentity.team == t)
319         {
320         dprint(" and has a team!\n");
321         return 1;
322         }
323         else
324         dprint(" but has the wrong team\n");
325         }
326         else
327         dprint("\n");
328         }
329         e = e.chain;
330         }
331         return 0;
332          */
333 }
334
335 float onslaught_controlpoint_attackable(entity cp, float t)
336         // -2: SAME TEAM, attackable by enemy!
337         // -1: SAME TEAM!
338         // 0: off limits
339         // 1: attack it
340         // 2: touch it
341         // 3: attack it (HIGH PRIO)
342         // 4: touch it (HIGH PRIO)
343 {
344         float a;
345
346         if(cp.isshielded)
347         {
348                 return 0;
349         }
350         else if(cp.goalentity)
351         {
352                 // if there's already an icon built, nothing happens
353                 if(cp.team == t)
354                 {
355                         a = onslaught_controlpoint_can_be_linked(cp, NUM_TEAM_1 + NUM_TEAM_2 - t);
356                         if(a) // attackable by enemy?
357                                 return -2; // EMERGENCY!
358                         return -1;
359                 }
360                 // we know it can be linked, so no need to check
361                 // but...
362                 a = onslaught_controlpoint_can_be_linked(cp, t);
363                 if(a == 2) // near our generator?
364                         return 3; // EMERGENCY!
365                 return 1;
366         }
367         else
368         {
369                 // free point
370                 if(onslaught_controlpoint_can_be_linked(cp, t))
371                 {
372                         a = onslaught_controlpoint_can_be_linked(cp, NUM_TEAM_1 + NUM_TEAM_2 - t);
373                         if(a == 2)
374                                 return 4; // GET THIS ONE NOW!
375                         else
376                                 return 2; // TOUCH ME
377                 }
378         }
379         return 0;
380 }
381
382 float overtime_msg_time;
383 void onslaught_generator_think()
384 {
385         float d;
386         entity e;
387         self.nextthink = ceil(time + 1);
388         if (!gameover)
389         {
390                 if (autocvar_timelimit && time > game_starttime + autocvar_timelimit * 60)
391                 {
392                         if (!overtime_msg_time)
393                         {
394                                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_OVERTIME_CONTROLPOINT);
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, ATTEN_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                         }
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
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;
419
420         if(!self.isshielded && self.wait < time)
421         {
422             self.wait = time + 5;
423             FOR_EACH_REALPLAYER(e)
424             {
425                                 if(SAME_TEAM(e, self))
426                 {
427                                         Send_Notification(NOTIF_ONE, e, MSG_CENTER, CENTER_ONS_NOTSHIELDED);
428                     soundto(MSG_ONE, e, CHAN_AUTO, "kh/alarm.wav", VOL_BASE, ATTEN_NONE);    // FIXME: Uniqe sound?
429                 }
430             }
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, ATTEN_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 (!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, ATTEN_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, ATTEN_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, ATTEN_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(warmup_stage)
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 (IS_PLAYER(attacker))
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(Team_ColoredFullName(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(Team_ColoredFullName(self.team), " generator has less than ", ftos(h), " health remaining\n");
613 #endif
614                 self.lasthealth = self.health;
615         }
616         else if (!warmup_stage)
617         {
618                 if (attacker == self)
619                         bprint(Team_ColoredFullName(self.team), " generator spontaneously exploded due to overtime!\n");
620                 else
621                 {
622                         string t;
623                         t = Team_ColoredFullName(attacker.team);
624                         bprint(Team_ColoredFullName(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 = func_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, ATTEN_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, ATTEN_NORM);
683                 else
684                         sound(self, CH_TRIGGER, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_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 == NUM_TEAM_1)
707                         return "ons-gen-red";
708                 else if(e.team == NUM_TEAM_2)
709                         return "ons-gen-blue";
710         }
711         if(e.isshielded)
712                 return "ons-gen-shielded";
713         if(e.team == NUM_TEAM_1)
714                 return "ons-gen-red";
715         else if(e.team == NUM_TEAM_2)
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, NUM_TEAM_1);
724         s2 = onslaught_generator_waypointsprite_for_team(e, NUM_TEAM_2);
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 == NUM_TEAM_1 || e.team == NUM_TEAM_2)
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 == NUM_TEAM_1 || e.team == NUM_TEAM_2)
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 == NUM_TEAM_1)
759                                 return "ons-cp-atck-red";
760                         else if(e.team == NUM_TEAM_2)
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 == NUM_TEAM_1)
768                                 return "ons-cp-dfnd-red";
769                         else if(e.team == NUM_TEAM_2)
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 == NUM_TEAM_1)
775                                 return "ons-cp-red";
776                         else if(e.team == NUM_TEAM_2)
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 == NUM_TEAM_1)
785                         return "ons-cp-red";
786                 else if(e.team == NUM_TEAM_2)
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, NUM_TEAM_1);
798         s2 = onslaught_controlpoint_waypointsprite_for_team(e, NUM_TEAM_2);
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, NUM_TEAM_1) || onslaught_controlpoint_can_be_linked(e, NUM_TEAM_2));
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 == NUM_TEAM_1 || e.team == NUM_TEAM_2)
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 == NUM_TEAM_1 || e.team == NUM_TEAM_2)
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
856         if(!self.noalign)
857         {
858                 setorigin(self, self.origin + '0 0 20');
859                 droptofloor();
860         }
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 == NUM_TEAM_1)
914         ons_red_generator = self;
915
916         if(self.team == NUM_TEAM_2)
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, NUM_TEAM_2, 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 == NUM_TEAM_1)
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 == NUM_TEAM_2)
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, ATTEN_NORM);
1029         else
1030                 sound(self, CH_TRIGGER, "onslaught/ons_hit2.wav", VOL_BASE+0.3, ATTEN_NORM);
1031
1032         if (self.health < 0)
1033         {
1034                 sound(self, CH_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTEN_NORM);
1035                 pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
1036                 {
1037                         string t;
1038                         t = Team_ColoredFullName(attacker.team);
1039                         bprint(Team_ColoredFullName(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_cp_proxydecap)
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_cp_proxydecap_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_cp_proxydecap_dps * sys_frametime);
1103         _enemy_count = _enemy_count * (autocvar_g_onslaught_cp_proxydecap_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, ATTEN_NORM);
1212                 else if (random() > 0.5)
1213                         sound(self, CH_PAIN, "onslaught/ons_spark2.wav", VOL_BASE, ATTEN_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, ATTEN_NORM);
1237                 bprint(Team_ColoredFullName(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 (!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, ATTEN_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_think()
1307 {
1308         self.nextthink = time;
1309         CSQCMODEL_AUTOUPDATE();
1310 }
1311
1312 void onslaught_controlpoint_reset()
1313 {
1314         if(self.goalentity && self.goalentity != world)
1315                 remove(self.goalentity);
1316         self.goalentity = world;
1317         self.team = 0;
1318         self.colormap = 1024;
1319         self.iscaptured = FALSE;
1320         self.islinked = FALSE;
1321         self.isshielded = TRUE;
1322         self.enemy.solid = SOLID_NOT;
1323         self.enemy.colormap = self.colormap;
1324         self.think = onslaught_controlpoint_think;
1325         self.enemy.think = func_null;
1326         self.nextthink = time; // don't like func_null :P
1327         setmodel(self, "models/onslaught/controlpoint_pad.md3");
1328         //setsize(self, '-32 -32 0', '32 32 8');
1329
1330         WaypointSprite_UpdateMaxHealth(self.sprite, 0);
1331
1332         onslaught_updatelinks();
1333
1334         activator = self;
1335         SUB_UseTargets(); // to reset the structures, playerspawns etc.
1336         
1337         CSQCMODEL_AUTOUPDATE();
1338 }
1339
1340 /*QUAKED spawnfunc_onslaught_controlpoint (0 .5 .8) (-32 -32 0) (32 32 128)
1341   Control point. Be sure to give this enough clearance so that the shootable part has room to exist
1342
1343   This should link to an spawnfunc_onslaught_controlpoint entity or spawnfunc_onslaught_generator entity.
1344
1345 keys:
1346 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
1347 "target" - target any entities that are tied to this control point, such as vehicles and buildable structure entities.
1348 "message" - name of this control point (should reflect the location in the map, such as "center bridge", "north tower", etc)
1349  */
1350
1351 void spawnfunc_onslaught_controlpoint()
1352 {
1353         //entity e;
1354         if (!g_onslaught)
1355         {
1356                 remove(self);
1357                 return;
1358         }
1359         precache_model("models/onslaught/controlpoint_pad.md3");
1360         precache_model("models/onslaught/controlpoint_pad2.md3");
1361         precache_model("models/onslaught/controlpoint_shield.md3");
1362         precache_model("models/onslaught/controlpoint_icon.md3");
1363         precache_model("models/onslaught/controlpoint_icon_dmg1.md3");
1364         precache_model("models/onslaught/controlpoint_icon_dmg2.md3");
1365         precache_model("models/onslaught/controlpoint_icon_dmg3.md3");
1366         precache_model("models/onslaught/controlpoint_icon_gib1.md3");
1367         precache_model("models/onslaught/controlpoint_icon_gib2.md3");
1368         precache_model("models/onslaught/controlpoint_icon_gib4.md3");
1369         precache_sound("onslaught/controlpoint_build.wav");
1370         precache_sound("onslaught/controlpoint_built.wav");
1371         precache_sound("weapons/grenade_impact.wav");
1372         precache_sound("onslaught/damageblockedbyshield.wav");
1373         precache_sound("onslaught/controlpoint_underattack.wav");
1374         precache_sound("onslaught/ons_spark1.wav");
1375         precache_sound("onslaught/ons_spark2.wav");
1376
1377         self.solid = SOLID_BBOX;
1378         self.movetype = MOVETYPE_NONE;
1379         setmodel(self, "models/onslaught/controlpoint_pad.md3");
1380         //setsize(self, '-32 -32 0', '32 32 8');
1381         if(!self.noalign)
1382         {
1383                 setorigin(self, self.origin + '0 0 20');
1384                 droptofloor();
1385         }
1386         self.touch = onslaught_controlpoint_touch;
1387         self.team = 0;
1388         self.colormap = 1024;
1389         self.iscaptured = FALSE;
1390         self.islinked = FALSE;
1391         self.isshielded = TRUE;
1392
1393         // spawn shield model which indicates whether this can be damaged
1394         self.enemy = spawn();
1395         self.enemy.classname = "onslaught_controlpoint_shield";
1396         self.enemy.solid = SOLID_NOT;
1397         self.enemy.movetype = MOVETYPE_NONE;
1398         self.enemy.effects = EF_ADDITIVE;
1399         setmodel(self.enemy , "models/onslaught/controlpoint_shield.md3");
1400
1401         setattachment(self.enemy , self, "");
1402         //setsize(e, '-32 -32 0', '32 32 128');
1403
1404         //setorigin(e, self.origin);
1405         self.enemy.colormap = self.colormap;
1406
1407         waypoint_spawnforitem(self);
1408         
1409         self.think = onslaught_controlpoint_think;
1410         self.nextthink = time;
1411
1412         WaypointSprite_SpawnFixed(string_null, self.origin + '0 0 128', self, sprite, RADARICON_NONE, '0 0 0');
1413         WaypointSprite_UpdateRule(self.sprite, NUM_TEAM_2, SPRITERULE_TEAMPLAY);
1414
1415         onslaught_updatelinks();
1416
1417         self.reset = onslaught_controlpoint_reset;
1418         
1419         CSQCMODEL_AUTOINIT();
1420 }
1421
1422 float onslaught_link_send(entity to, float sendflags)
1423 {
1424         WriteByte(MSG_ENTITY, ENT_CLIENT_RADARLINK);
1425         WriteByte(MSG_ENTITY, sendflags);
1426         if(sendflags & 1)
1427         {
1428                 WriteCoord(MSG_ENTITY, self.goalentity.origin_x);
1429                 WriteCoord(MSG_ENTITY, self.goalentity.origin_y);
1430                 WriteCoord(MSG_ENTITY, self.goalentity.origin_z);
1431         }
1432         if(sendflags & 2)
1433         {
1434                 WriteCoord(MSG_ENTITY, self.enemy.origin_x);
1435                 WriteCoord(MSG_ENTITY, self.enemy.origin_y);
1436                 WriteCoord(MSG_ENTITY, self.enemy.origin_z);
1437         }
1438         if(sendflags & 4)
1439         {
1440                 WriteByte(MSG_ENTITY, self.clientcolors); // which is goalentity's color + enemy's color * 16
1441         }
1442         return TRUE;
1443 }
1444
1445 void onslaught_link_checkupdate()
1446 {
1447         // TODO check if the two sides have moved (currently they won't move anyway)
1448         float redpower, bluepower;
1449
1450         redpower = bluepower = 0;
1451         if(self.goalentity.islinked)
1452         {
1453                 if(self.goalentity.team == NUM_TEAM_1)
1454                         redpower = 1;
1455                 else if(self.goalentity.team == NUM_TEAM_2)
1456                         bluepower = 1;
1457         }
1458         if(self.enemy.islinked)
1459         {
1460                 if(self.enemy.team == NUM_TEAM_1)
1461                         redpower = 2;
1462                 else if(self.enemy.team == NUM_TEAM_2)
1463                         bluepower = 2;
1464         }
1465
1466         float cc;
1467         if(redpower == 1 && bluepower == 2)
1468                 cc = (NUM_TEAM_1 - 1) * 0x01 + (NUM_TEAM_2 - 1) * 0x10;
1469         else if(redpower == 2 && bluepower == 1)
1470                 cc = (NUM_TEAM_1 - 1) * 0x10 + (NUM_TEAM_2 - 1) * 0x01;
1471         else if(redpower)
1472                 cc = (NUM_TEAM_1 - 1) * 0x11;
1473         else if(bluepower)
1474                 cc = (NUM_TEAM_2 - 1) * 0x11;
1475         else
1476                 cc = 0;
1477
1478         //print(etos(self), " rp=", ftos(redpower), " bp=", ftos(bluepower), " ");
1479         //print("cc=", ftos(cc), "\n");
1480
1481         if(cc != self.clientcolors)
1482         {
1483                 self.clientcolors = cc;
1484                 self.SendFlags |= 4;
1485         }
1486
1487         self.nextthink = time;
1488 }
1489
1490 void onslaught_link_delayed()
1491 {
1492         self.goalentity = find(world, targetname, self.target);
1493         self.enemy = find(world, targetname, self.target2);
1494         if (!self.goalentity)
1495                 objerror("can not find target\n");
1496         if (!self.enemy)
1497                 objerror("can not find target2\n");
1498         dprint(etos(self.goalentity), " linked with ", etos(self.enemy), "\n");
1499         self.SendFlags |= 3;
1500         self.think = onslaught_link_checkupdate;
1501         self.nextthink = time;
1502 }
1503
1504 /*QUAKED spawnfunc_onslaught_link (0 .5 .8) (-16 -16 -16) (16 16 16)
1505   Link between control points.
1506
1507   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.
1508
1509 keys:
1510 "target" - first control point.
1511 "target2" - second control point.
1512  */
1513 void spawnfunc_onslaught_link()
1514 {
1515         if (!g_onslaught)
1516         {
1517                 remove(self);
1518                 return;
1519         }
1520         if (self.target == "" || self.target2 == "")
1521                 objerror("target and target2 must be set\n");
1522         InitializeEntity(self, onslaught_link_delayed, INITPRIO_FINDTARGET);
1523         Net_LinkEntity(self, FALSE, 0, onslaught_link_send);
1524 }
1525
1526 MUTATOR_HOOKFUNCTION(ons_BuildMutatorsString)
1527 {
1528         ret_string = strcat(ret_string, ":ONS");
1529         return 0;
1530 }
1531
1532 MUTATOR_HOOKFUNCTION(ons_BuildMutatorsPrettyString)
1533 {
1534         ret_string = strcat(ret_string, ", Onslaught");
1535         return 0;
1536 }
1537
1538 MUTATOR_HOOKFUNCTION(ons_Spawn_Score)
1539 {
1540
1541     /*
1542     float _neer_home = (random() > 0.5 ? TRUE : FALSE);
1543
1544         RandomSelection_Init();
1545
1546         if(self.team == NUM_TEAM_1)
1547         RandomSelection_Add(ons_red_generator, 0, string_null, 1, 1);
1548
1549         if(self.team == NUM_TEAM_2)
1550         RandomSelection_Add(ons_blue_generator, 0, string_null, 1, 1);
1551
1552         entity _cp = findchain(classname, "onslaught_controlpoint"):
1553         while _cp;
1554         {
1555             if(_cp.team == self.team)
1556             RandomSelection_Add(_cp, 0, string_null, 1, 1);
1557
1558                 _cp = _cp.chain;
1559         }
1560
1561         if(RandomSelection_chosen_ent)
1562         {
1563                 self.tur_head = RandomSelection_chosen_ent;
1564                 spawn_score_x += SPAWN_PRIO_NEAR_TEAMMATE_FOUND;
1565         }
1566         else if(self.team == spawn_spot.team)
1567                 spawn_score_x += SPAWN_PRIO_NEAR_TEAMMATE_SAMETEAM; // prefer same team, if we can't find a spawn near teammate
1568
1569     */
1570
1571         return 0;
1572 }
1573
1574 MUTATOR_HOOKFUNCTION(ons_PlayerSpawn)
1575 {
1576     if(!autocvar_g_onslaught_spawn_at_controlpoints)
1577         return 0;
1578
1579     if(random() < 0.5)  // 50/50 chane to use default spawnsystem.
1580         return 0;
1581
1582     float _close_to_home = ((random() > 0.5) ? TRUE : FALSE);
1583     entity _best = world, _trg_gen = world;
1584     float _score, _best_score = MAX_SHOT_DISTANCE;
1585
1586         RandomSelection_Init();
1587
1588         if(self.team == NUM_TEAM_1)
1589         {
1590             if(!_close_to_home)
1591             _trg_gen = ons_blue_generator;
1592         else
1593             _trg_gen  = ons_red_generator;
1594         }
1595
1596         if(self.team == NUM_TEAM_2)
1597         {
1598             if(_close_to_home)
1599             _trg_gen = ons_blue_generator;
1600         else
1601             _trg_gen  = ons_red_generator;
1602         }
1603
1604         entity _cp = findchain(classname, "onslaught_controlpoint");
1605         while(_cp)
1606         {
1607             if(_cp.team == self.team)
1608         {
1609             _score = vlen(_trg_gen.origin - _cp.origin);
1610             if(_score < _best_score)
1611             {
1612                 _best = _cp;
1613                 _best_score = _score;
1614             }
1615         }
1616                 _cp = _cp.chain;
1617         }
1618
1619     vector _loc;
1620     float i;
1621     if(_best)
1622     {
1623         for(i = 0; i < 10; ++i)
1624         {
1625             _loc = _best.origin + '0 0 96';
1626             _loc += ('0 1 0' * random()) * 128;
1627             tracebox(_loc, PL_MIN, PL_MAX, _loc, MOVE_NORMAL, self);
1628             if(trace_fraction == 1.0 && !trace_startsolid)
1629             {
1630                 setorigin(self, _loc);
1631                 self.angles = normalize(_loc - _best.origin) * RAD2DEG;
1632                 return 0;
1633             }
1634         }
1635     }
1636     else
1637     {
1638         if(!autocvar_g_onslaught_spawn_at_generator)
1639             return 0;
1640
1641         _trg_gen = ((self.team == NUM_TEAM_1) ? ons_red_generator : ons_blue_generator);
1642
1643         for(i = 0; i < 10; ++i)
1644         {
1645             _loc = _trg_gen.origin + '0 0 96';
1646             _loc += ('0 1 0' * random()) * 128;
1647             tracebox(_loc, PL_MIN, PL_MAX, _loc, MOVE_NORMAL, self);
1648             if(trace_fraction == 1.0 && !trace_startsolid)
1649             {
1650                 setorigin(self, _loc);
1651                 self.angles = normalize(_loc - _trg_gen.origin) * RAD2DEG;
1652                 return 0;
1653             }
1654         }
1655     }
1656
1657     return 0;
1658 }
1659
1660 MUTATOR_HOOKFUNCTION(ons_MonsterThink)
1661 {
1662         entity e = find(world, targetname, self.target);
1663         if (e != world)
1664                 self.team = e.team;
1665
1666         return FALSE;
1667 }
1668
1669 MUTATOR_HOOKFUNCTION(ons_MonsterSpawn)
1670 {
1671         entity e, ee = world;
1672         
1673         if(self.targetname)
1674         {
1675                 e = find(world,target,self.targetname);
1676                 if(e != world)
1677                 {
1678                         self.team = e.team;
1679                         ee = e;
1680                 }
1681         }
1682         
1683         if(ee)
1684         {
1685         activator = ee;
1686         self.use();
1687     }
1688
1689         return FALSE;
1690 }
1691
1692 MUTATOR_DEFINITION(gamemode_onslaught)
1693 {
1694         MUTATOR_HOOK(BuildMutatorsPrettyString, ons_BuildMutatorsPrettyString, CBC_ORDER_ANY);
1695         MUTATOR_HOOK(BuildMutatorsString, ons_BuildMutatorsString, CBC_ORDER_ANY);
1696         MUTATOR_HOOK(PlayerSpawn, ons_PlayerSpawn, CBC_ORDER_ANY);
1697         MUTATOR_HOOK(MonsterMove, ons_MonsterThink, CBC_ORDER_ANY);
1698         MUTATOR_HOOK(MonsterSpawn, ons_MonsterSpawn, CBC_ORDER_ANY);
1699         //MUTATOR_HOOK(Spawn_Score, ons_Spawn_Score, CBC_ORDER_ANY);
1700
1701         MUTATOR_ONADD
1702         {
1703                 if(time > 1) // game loads at time 1
1704                         error("This is a game type and it cannot be added at runtime.");
1705         }
1706
1707         MUTATOR_ONREMOVE
1708         {
1709                 print("This is a game type and it cannot be removed at runtime.");
1710                 return -1;
1711         }
1712
1713         return 0;
1714 }