]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/mode_onslaught.qc
handle antilag for bots correctly for hitplot purposes
[divverent/nexuiz.git] / data / qcsrc / server / mode_onslaught.qc
1 void onslaught_generator_updatesprite(entity e);
2 void onslaught_controlpoint_updatesprite(entity e);
3 void onslaught_link_checkupdate();
4
5 .entity sprite;
6 .string target2;
7 .float iscaptured;
8 .float islinked;
9 .float isgenneighbor_red;
10 .float isgenneighbor_blue;
11 .float iscpneighbor_red;
12 .float iscpneighbor_blue;
13 .float isshielded;
14 .float lasthealth;
15 .float lastteam;
16 .float lastshielded;
17
18 .string model1, model2, model3;
19
20 void ons_gib_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
21 {
22     self.velocity += vforce;
23 }
24
25 void ons_trowgib(
26     vector v_from, vector v_to, vector v_colormod,
27     string smodel,
28     float f_lifetime, float f_fadetime, float b_burn)
29 {
30     local entity gib;
31     local entity burn;
32
33     gib = spawn();
34
35     gib.classname = "turret_gib";
36     setmodel(gib,smodel);
37     setorigin(gib,v_from);
38     SUB_SetFade(gib,time + f_lifetime,2);
39
40     gib.solid              = SOLID_BBOX;
41
42     gib.movetype           = MOVETYPE_BOUNCE;
43     gib.takedamage         = DAMAGE_YES;
44     gib.event_damage       = ons_gib_damage;
45     gib.health             = -1;
46     gib.effects            = EF_LOWPRECISION;
47     gib.flags              = FL_NOTARGET;
48     gib.colormod           = v_colormod;
49     gib.velocity           = v_to;
50
51     if (b_burn)
52     {
53         burn = spawn();
54         burn.effects        = EF_LOWPRECISION;//|EF_FLAME;
55         setattachment(burn,gib,"");
56         setorigin(burn,(gib.mins + gib.maxs) * 0.5);
57         SUB_SetFade(burn,time + (f_lifetime * 0.5) ,2);
58     }
59 }
60
61 void onslaught_updatelinks()
62 {
63         local entity l, links;
64         local float stop, t1, t2, t3, t4;
65         // first check if the game has ended
66         dprint("--- updatelinks ---\n");
67         links = findchain(classname, "onslaught_link");
68         // mark generators as being shielded and networked
69         l = findchain(classname, "onslaught_generator");
70         while (l)
71         {
72                 if (l.iscaptured)
73                         dprint(etos(l), " (generator) belongs to team ", ftos(l.team), "\n");
74                 else
75                         dprint(etos(l), " (generator) is destroyed\n");
76                 l.islinked = l.iscaptured;
77                 l.isshielded = l.iscaptured;
78                 l = l.chain;
79         }
80         // mark points as shielded and not networked
81         l = findchain(classname, "onslaught_controlpoint");
82         while (l)
83         {
84                 l.islinked = FALSE;
85                 l.isshielded = TRUE;
86                 l.isgenneighbor_red = FALSE;
87                 l.isgenneighbor_blue = FALSE;
88                 l.iscpneighbor_red = FALSE;
89                 l.iscpneighbor_blue = FALSE;
90                 dprint(etos(l), " (point) belongs to team ", ftos(l.team), "\n");
91                 l = l.chain;
92         }
93         // flow power outward from the generators through the network
94         l = links;
95         while (l)
96         {
97                 dprint(etos(l), " (link) connects ", etos(l.goalentity), " with ", etos(l.enemy), "\n");
98                 l = l.chain;
99         }
100         stop = FALSE;
101         while (!stop)
102         {
103                 stop = TRUE;
104                 l = links;
105                 while (l)
106                 {
107                         // if both points are captured by the same team, and only one of
108                         // them is powered, mark the other one as powered as well
109                         if (l.enemy.iscaptured && l.goalentity.iscaptured)
110                         if (l.enemy.islinked != l.goalentity.islinked)
111                         if (l.enemy.team == l.goalentity.team)
112                         {
113                                 if (!l.goalentity.islinked)
114                                 {
115                                         stop = FALSE;
116                                         l.goalentity.islinked = TRUE;
117                                         dprint(etos(l), " (link) is marking ", etos(l.goalentity), " (point) because its team matches ", etos(l.enemy), " (point)\n");
118                                 }
119                                 else if (!l.enemy.islinked)
120                                 {
121                                         stop = FALSE;
122                                         l.enemy.islinked = TRUE;
123                                         dprint(etos(l), " (link) is marking ", etos(l.enemy), " (point) because its team matches ", etos(l.goalentity), " (point)\n");
124                                 }
125                         }
126                         l = l.chain;
127                 }
128         }
129         // now that we know which points are powered we can mark their neighbors
130         // as unshielded if team differs
131         l = links;
132         while (l)
133         {
134                 if (l.goalentity.team != l.enemy.team)
135                 {
136                         if (l.goalentity.islinked)
137                         {
138                                 dprint(etos(l), " (link) is unshielding ", etos(l.enemy), " (point) because its team does not match ", etos(l.goalentity), " (point)\n");
139                                 l.enemy.isshielded = FALSE;
140                                 if(l.goalentity.classname == "onslaught_generator")
141                                 {
142                                         if(l.goalentity.team == COLOR_TEAM1)
143                                                 l.enemy.isgenneighbor_red = TRUE;
144                                         else if(l.goalentity.team == COLOR_TEAM2)
145                                                 l.enemy.isgenneighbor_blue = TRUE;
146                                 }
147                                 else
148                                 {
149                                         if(l.goalentity.team == COLOR_TEAM1)
150                                                 l.enemy.iscpneighbor_red = TRUE;
151                                         else if(l.goalentity.team == COLOR_TEAM2)
152                                                 l.enemy.iscpneighbor_blue = TRUE;
153                                 }
154                         }
155                         if (l.enemy.islinked)
156                         {
157                                 dprint(etos(l), " (link) is unshielding ", etos(l.goalentity), " (point) because its team does not match ", etos(l.enemy), " (point)\n");
158                                 l.goalentity.isshielded = FALSE;
159                                 if(l.enemy.classname == "onslaught_generator")
160                                 {
161                                         if(l.enemy.team == COLOR_TEAM1)
162                                                 l.goalentity.isgenneighbor_red = TRUE;
163                                         else if(l.enemy.team == COLOR_TEAM2)
164                                                 l.goalentity.isgenneighbor_blue = TRUE;
165                                 }
166                                 else
167                                 {
168                                         if(l.enemy.team == COLOR_TEAM1)
169                                                 l.goalentity.iscpneighbor_red = TRUE;
170                                         else if(l.enemy.team == COLOR_TEAM2)
171                                                 l.goalentity.iscpneighbor_blue = TRUE;
172                                 }
173                         }
174                 }
175                 l = l.chain;
176         }
177         // now update the takedamage and alpha variables on generator shields
178         l = findchain(classname, "onslaught_generator");
179         while (l)
180         {
181                 if (l.isshielded)
182                 {
183                         dprint(etos(l), " (generator) is shielded\n");
184                         l.enemy.alpha = 1;
185                         l.takedamage = DAMAGE_NO;
186                         l.bot_attack = FALSE;
187                 }
188                 else
189                 {
190                         dprint(etos(l), " (generator) is not shielded\n");
191                         l.enemy.alpha = -1;
192                         l.takedamage = DAMAGE_AIM;
193                         l.bot_attack = TRUE;
194                 }
195                 l = l.chain;
196         }
197         // now update the takedamage and alpha variables on control point icons
198         l = findchain(classname, "onslaught_controlpoint");
199         while (l)
200         {
201                 if (l.isshielded)
202                 {
203                         dprint(etos(l), " (point) is shielded\n");
204                         l.enemy.alpha = 1;
205                         if (l.goalentity)
206                         {
207                                 l.goalentity.takedamage = DAMAGE_NO;
208                                 l.goalentity.bot_attack = FALSE;
209                         }
210                 }
211                 else
212                 {
213                         dprint(etos(l), " (point) is not shielded\n");
214                         l.enemy.alpha = -1;
215                         if (l.goalentity)
216                         {
217                                 l.goalentity.takedamage = DAMAGE_AIM;
218                                 l.goalentity.bot_attack = TRUE;
219                         }
220                 }
221                 onslaught_controlpoint_updatesprite(l);
222                 l = l.chain;
223         }
224         // count generators owned by each team
225         t1 = t2 = t3 = t4 = 0;
226         l = findchain(classname, "onslaught_generator");
227         while (l)
228         {
229                 if (l.iscaptured)
230                 {
231                         if (l.team == COLOR_TEAM1) t1 = 1;
232                         if (l.team == COLOR_TEAM2) t2 = 1;
233                         if (l.team == COLOR_TEAM3) t3 = 1;
234                         if (l.team == COLOR_TEAM4) t4 = 1;
235                 }
236                 onslaught_generator_updatesprite(l);
237                 l = l.chain;
238         }
239         // see if multiple teams remain (if not, it's game over)
240         if (t1 + t2 + t3 + t4 < 2)
241                 dprint("--- game over ---\n");
242         else
243                 dprint("--- done updating links ---\n");
244 };
245
246 float onslaught_controlpoint_can_be_linked(entity cp, float t)
247 {
248         if(t == COLOR_TEAM1)
249         {
250                 if(cp.isgenneighbor_red)
251                         return 2;
252                 if(cp.iscpneighbor_red)
253                         return 1;
254         }
255         else if(t == COLOR_TEAM2)
256         {
257                 if(cp.isgenneighbor_blue)
258                         return 2;
259                 if(cp.iscpneighbor_blue)
260                         return 1;
261         }
262         return 0;
263 /*
264         entity e;
265         // check to see if this player has a legitimate claim to capture this
266         // control point - more specifically that there is a captured path of
267         // points leading back to the team generator
268         e = findchain(classname, "onslaught_link");
269         while (e)
270         {
271                 if (e.goalentity == cp)
272                 {
273                         dprint(etos(e), " (link) connects to ", etos(e.enemy), " (point)");
274                         if (e.enemy.islinked)
275                         {
276                                 dprint(" which is linked");
277                                 if (e.enemy.team == t)
278                                 {
279                                         dprint(" and has the correct team!\n");
280                                         return 1;
281                                 }
282                                 else
283                                         dprint(" but has the wrong team\n");
284                         }
285                         else
286                                 dprint("\n");
287                 }
288                 else if (e.enemy == cp)
289                 {
290                         dprint(etos(e), " (link) connects to ", etos(e.goalentity), " (point)");
291                         if (e.goalentity.islinked)
292                         {
293                                 dprint(" which is linked");
294                                 if (e.goalentity.team == t)
295                                 {
296                                         dprint(" and has a team!\n");
297                                         return 1;
298                                 }
299                                 else
300                                         dprint(" but has the wrong team\n");
301                         }
302                         else
303                                 dprint("\n");
304                 }
305                 e = e.chain;
306         }
307         return 0;
308 */
309 }
310
311 float onslaught_controlpoint_attackable(entity cp, float t)
312 // -2: SAME TEAM, attackable by enemy!
313 // -1: SAME TEAM!
314 // 0:  off limits
315 // 1:  attack it
316 // 2:  touch it
317 // 3:  attack it (HIGH PRIO)
318 // 4:  touch it (HIGH PRIO)
319 {
320         float a;
321
322         if(cp.isshielded)
323         {
324                 return 0;
325         }
326         else if(cp.goalentity)
327         {
328                 // if there's already an icon built, nothing happens
329                 if(cp.team == t)
330                 {
331                         a = onslaught_controlpoint_can_be_linked(cp, COLOR_TEAM1 + COLOR_TEAM2 - t);
332                         if(a) // attackable by enemy?
333                                 return -2; // EMERGENCY!
334                         return -1;
335                 }
336                 // we know it can be linked, so no need to check
337                 // but...
338                 a = onslaught_controlpoint_can_be_linked(cp, t);
339                 if(a == 2) // near our generator?
340                         return 3; // EMERGENCY!
341                 return 1;
342         }
343         else
344         {
345                 // free point
346                 if(onslaught_controlpoint_can_be_linked(cp, t))
347                 {
348                         a = onslaught_controlpoint_can_be_linked(cp, COLOR_TEAM1 + COLOR_TEAM2 - t);
349                         if(a == 2)
350                                 return 4; // GET THIS ONE NOW!
351                         else
352                                 return 2; // TOUCH ME
353                 }
354         }
355         return 0;
356 }
357
358 void onslaught_generator_think()
359 {
360         local float d;
361         local entity e;
362         self.nextthink = ceil(time + 1);
363         if (cvar("timelimit"))
364         if (time > cvar("timelimit") * 60 - 60)
365         {
366                 // self.max_health / 300 gives 5 minutes of overtime.
367                 // control points reduce the overtime duration.
368                 sound(self, CHAN_TRIGGER, "onslaught/generator_decay.wav", VOL_BASE, ATTN_NORM);
369                 d = 1;
370                 e = findchain(classname, "onslaught_controlpoint");
371                 while (e)
372                 {
373                         if (e.team != self.team)
374                         if (e.islinked)
375                                 d = d + 1;
376                         e = e.chain;
377                 }
378                 d = d * self.max_health / 300;
379                 Damage(self, self, self, d, DEATH_HURTTRIGGER, self.origin, '0 0 0');
380         }
381 };
382
383 void onslaught_generator_deaththink()
384 {
385         local vector org;
386         if (self.count > 0)
387         {
388                 self.nextthink = time + 0.1;
389                 self.count = self.count - 1;
390                 org = randompos(self.origin + self.mins + '8 8 8', self.origin + self.maxs + '-8 -8 -8');
391                 pointparticles(particleeffectnum("onslaught_generator_smallexplosion"), org, '0 0 0', 1);
392                 sound(self, CHAN_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
393         }
394         else
395         {
396                 org = self.origin;
397                 pointparticles(particleeffectnum("onslaught_generator_finalexplosion"), org, '0 0 0', 1);
398                 sound(self, CHAN_TRIGGER, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
399         }
400 };
401
402 void onslaught_generator_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
403 {
404         if (damage <= 0)
405                 return;
406         if (attacker != self)
407         {
408                 if (self.isshielded)
409                 {
410                         // this is protected by a shield, so ignore the damage
411                         if (time > self.pain_finished)
412                         if (attacker.classname == "player")
413                         {
414                                 play2(attacker, "onslaught/damageblockedbyshield.wav");
415                                 self.pain_finished = time + 1;
416                         }
417                         return;
418                 }
419                 if (time > self.pain_finished)
420                 {
421                         self.pain_finished = time + 5;
422                         bprint(ColoredTeamName(self.team), " generator under attack!\n");
423                         play2team(self.team, "onslaught/generator_underattack.wav");
424                 }
425         }
426         self.health = self.health - damage;
427         // choose an animation frame based on health
428         self.frame = 10 * bound(0, (1 - self.health / self.max_health), 1);
429         // see if the generator is still functional, or dying
430         if (self.health > 0)
431         {
432                 float h, lh;
433                 lh = ceil(self.lasthealth / 100) * 100;
434                 h = ceil(self.health / 100) * 100;
435                 if(lh != h)
436                         bprint(ColoredTeamName(self.team), " generator has less than ", ftos(h), " health remaining\n");
437                 self.lasthealth = self.health;
438         }
439         else
440         {
441                 if (attacker == self)
442                         bprint(ColoredTeamName(self.team), " generator spontaneously exploded due to overtime!\n");
443                 else
444                 {
445                         string t;
446                         t = ColoredTeamName(attacker.team);
447                         bprint(ColoredTeamName(self.team), " generator destroyed by ", t, "!\n");
448                 }
449                 self.iscaptured = FALSE;
450                 self.islinked = FALSE;
451                 self.isshielded = FALSE;
452                 self.takedamage = DAMAGE_NO; // can't be hurt anymore
453                 self.event_damage = SUB_Null; // won't do anything if hurt
454                 self.count = 30; // 30 explosions
455                 self.think = onslaught_generator_deaththink; // explosion sequence
456                 self.nextthink = time; // start exploding immediately
457                 self.think(); // do the first explosion now
458                 onslaught_updatelinks();
459         }
460         if(self.health < cvar("g_onslaught_gen_health") * 0.75)
461                 setmodel(self, "models/onslaught/generator_dmg1.md3");
462         if(self.health < cvar("g_onslaught_gen_health") * 0.35)
463                 setmodel(self, "models/onslaught/generator_dmg2.md3");
464         if(self.health <= 0)
465                 setmodel(self, "models/onslaught/generator_dead.md3");
466 };
467
468 // update links after a delay
469 void onslaught_generator_delayed()
470 {
471         onslaught_updatelinks();
472         // now begin normal thinking
473         self.think = onslaught_generator_think;
474         self.nextthink = time;
475 };
476
477 string onslaught_generator_waypointsprite_for_team(entity e, float t)
478 {
479         if(t == e.team)
480         {
481                 if(e.team == COLOR_TEAM1)
482                         return "ons-gen-red";
483                 else if(e.team == COLOR_TEAM2)
484                         return "ons-gen-blue";
485         }
486         if(e.isshielded)
487                 return "ons-gen-shielded";
488         if(e.team == COLOR_TEAM1)
489                 return "ons-gen-red";
490         else if(e.team == COLOR_TEAM2)
491                 return "ons-gen-blue";
492         return "";
493 }
494
495 void onslaught_generator_updatesprite(entity e)
496 {
497         string s1, s2, s3;
498         s1 = onslaught_generator_waypointsprite_for_team(e, COLOR_TEAM1);
499         s2 = onslaught_generator_waypointsprite_for_team(e, COLOR_TEAM2);
500         s3 = onslaught_generator_waypointsprite_for_team(e, -1);
501         WaypointSprite_UpdateSprites(e.sprite, s1, s2, s3);
502
503         if(e.lastteam != e.team + 2 || e.lastshielded != e.isshielded)
504         {
505                 e.lastteam = e.team + 2;
506                 e.lastshielded = e.isshielded;
507                 if(e.lastshielded)
508                 {
509                         if(e.team == COLOR_TEAM1)
510                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.5 0 0');
511                         else if(e.team == COLOR_TEAM2)
512                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0 0 0.5');
513                         else
514                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.5 0.5 0.5');
515                 }
516                 else
517                 {
518                         if(e.team == COLOR_TEAM1)
519                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '1 0 0');
520                         else if(e.team == COLOR_TEAM2)
521                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0 0 1');
522                         else
523                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.75 0.75 0.75');
524                 }
525                 WaypointSprite_Ping(e.sprite);
526         }
527 }
528
529 string onslaught_controlpoint_waypointsprite_for_team(entity e, float t)
530 {
531         float a;
532         if(t != -1)
533         {
534                 a = onslaught_controlpoint_attackable(e, t);
535                 if(a == 3 || a == 4) // ATTACK/TOUCH THIS ONE NOW
536                 {
537                         if(e.team == COLOR_TEAM1)
538                                 return "ons-cp-atck-red";
539                         else if(e.team == COLOR_TEAM2)
540                                 return "ons-cp-atck-blue";
541                         else
542                                 return "ons-cp-atck-neut";
543                 }
544                 else if(a == -2) // DEFEND THIS ONE NOW
545                 {
546                         if(e.team == COLOR_TEAM1)
547                                 return "ons-cp-dfnd-red";
548                         else if(e.team == COLOR_TEAM2)
549                                 return "ons-cp-dfnd-blue";
550                 }
551                 else if(e.team == t || a == -1 || a == 1) // own point, or fire at it
552                 {
553                         if(e.team == COLOR_TEAM1)
554                                 return "ons-cp-red";
555                         else if(e.team == COLOR_TEAM2)
556                                 return "ons-cp-blue";
557                 }
558                 else if(a == 2) // touch it
559                         return "ons-cp-neut";
560         }
561         else
562         {
563                 if(e.team == COLOR_TEAM1)
564                         return "ons-cp-red";
565                 else if(e.team == COLOR_TEAM2)
566                         return "ons-cp-blue";
567                 else
568                         return "ons-cp-neut";
569         }
570         return "";
571 }
572
573 void onslaught_controlpoint_updatesprite(entity e)
574 {
575         string s1, s2, s3;
576         s1 = onslaught_controlpoint_waypointsprite_for_team(e, COLOR_TEAM1);
577         s2 = onslaught_controlpoint_waypointsprite_for_team(e, COLOR_TEAM2);
578         s3 = onslaught_controlpoint_waypointsprite_for_team(e, -1);
579         WaypointSprite_UpdateSprites(e.sprite, s1, s2, s3);
580
581         float sh;
582         sh = !(onslaught_controlpoint_can_be_linked(e, COLOR_TEAM1) || onslaught_controlpoint_can_be_linked(e, COLOR_TEAM2));
583
584         if(e.lastteam != e.team + 2 || e.lastshielded != sh)
585         {
586                 e.lastteam = e.team + 2;
587                 e.lastshielded = sh;
588                 if(e.lastshielded)
589                 {
590                         if(e.team == COLOR_TEAM1)
591                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.5 0 0');
592                         else if(e.team == COLOR_TEAM2)
593                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0 0 0.5');
594                         else
595                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.5 0.5 0.5');
596                 }
597                 else
598                 {
599                         if(e.team == COLOR_TEAM1)
600                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '1 0 0');
601                         else if(e.team == COLOR_TEAM2)
602                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0 0 1');
603                         else
604                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.75 0.75 0.75');
605                 }
606                 WaypointSprite_Ping(e.sprite);
607         }
608 }
609
610 void onslaught_generator_reset()
611 {
612         self.team = self.team_saved;
613         self.lasthealth = self.max_health = self.health = cvar("g_onslaught_gen_health");
614         self.takedamage = DAMAGE_AIM;
615         self.bot_attack = TRUE;
616         self.iscaptured = TRUE;
617         self.islinked = TRUE;
618         self.isshielded = TRUE;
619         self.enemy.solid = SOLID_NOT;
620         self.think = onslaught_generator_delayed;
621         self.nextthink = time + 0.2;
622 }
623
624 /*QUAKED spawnfunc_onslaught_generator (0 .5 .8) (-32 -32 -24) (32 32 64)
625 Base generator.
626
627 spawnfunc_onslaught_link entities can target this.
628
629 keys:
630 "team" - team that owns this generator (5 = red, 14 = blue, etc), MUST BE SET.
631 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
632 */
633 void spawnfunc_onslaught_generator()
634 {
635         if (!g_onslaught)
636         {
637                 remove(self);
638                 return;
639         }
640
641         local entity e;
642         precache_model("models/onslaught/generator.md3");
643         precache_model("models/onslaught/generator_shield.md3");
644         precache_model("models/onslaught/generator_dmg1.md3");
645         precache_model("models/onslaught/generator_dmg2.md3");
646         precache_model("models/onslaught/generator_dead.md3");
647         precache_sound("onslaught/generator_decay.wav");
648         precache_sound("weapons/grenade_impact.wav");
649         precache_sound("weapons/rocket_impact.wav");
650         precache_sound("onslaught/generator_underattack.wav");
651         if (!self.team)
652                 objerror("team must be set");
653         self.team_saved = self.team;
654         self.colormap = 1024 + (self.team - 1) * 17;
655         self.solid = SOLID_BSP;
656         self.movetype = MOVETYPE_NONE;
657         self.lasthealth = self.max_health = self.health = cvar("g_onslaught_gen_health");
658         setmodel(self, "models/onslaught/generator.md3");
659         //setsize(self, '-32 -32 -24', '32 32 64');
660         setorigin(self, self.origin);
661         self.takedamage = DAMAGE_AIM;
662         self.bot_attack = TRUE;
663         self.event_damage = onslaught_generator_damage;
664         self.iscaptured = TRUE;
665         self.islinked = TRUE;
666         self.isshielded = TRUE;
667         // spawn shield model which indicates whether this can be damaged
668         self.enemy = e = spawn();
669         e.classname = "onslaught_generator_shield";
670         e.solid = SOLID_NOT;
671         e.movetype = MOVETYPE_NONE;
672         e.effects = EF_ADDITIVE;
673         setmodel(e, "models/onslaught/generator_shield.md3");
674         //setsize(e, '-32 -32 0', '32 32 128');
675         setorigin(e, self.origin);
676         e.colormap = self.colormap;
677         e.team = self.team;
678         self.think = onslaught_generator_delayed;
679         self.nextthink = time + 0.2;
680         InitializeEntity(self, onslaught_generator_delayed, INITPRIO_LAST);
681
682         WaypointSprite_SpawnFixed(string_null, e.origin + '0 0 1' * e.maxs_z, self, sprite);
683         WaypointSprite_UpdateRule(self.sprite, COLOR_TEAM2, SPRITERULE_TEAMPLAY);
684
685         onslaught_updatelinks();
686
687         self.reset = onslaught_generator_reset;
688 };
689
690 .float waslinked;
691 .float cp_bob_spd;
692 .vector cp_origin, cp_bob_origin, cp_bob_dmg;
693 void onslaught_controlpoint_icon_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
694 {
695         entity oself;
696         if (damage <= 0)
697                 return;
698         if (self.owner.isshielded)
699         {
700                 // this is protected by a shield, so ignore the damage
701                 if (time > self.pain_finished)
702                 if (attacker.classname == "player")
703                 {
704                         play2(attacker, "onslaught/damageblockedbyshield.wav");
705                         self.pain_finished = time + 1;
706                 }
707                 return;
708         }
709         if (time > self.pain_finished)
710         if (attacker.classname == "player")
711         {
712                 play2team(self.team, "onslaught/controlpoint_underattack.wav");
713                 self.pain_finished = time + 5;
714         }
715         self.health = self.health - damage;
716         self.pain_finished = time + 1;
717         self.punchangle = (2 * randomvec() - '1 1 1') * 45;
718         self.cp_bob_dmg_z = (2 * random() - 1) * 15;
719         // colormod flash when shot
720         self.colormod = '2 2 2';
721         if (self.health < 0)
722         {
723                 sound(self, CHAN_TRIGGER, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
724                 pointparticles(particleeffectnum("onslaught_controlpoint_explosion"), self.origin, '0 0 0', 1);
725                 {
726                         string t;
727                         t = ColoredTeamName(attacker.team);
728                         bprint(ColoredTeamName(self.team), " ", self.message, " control point destroyed by ", t, "\n");
729                         ons_trowgib(self.origin, (2 * randomvec() - '1 1 1') * 25,'1 1 1',"models/onslaught/controlpoint_icon_gib1.md3",5,1,1);
730                         ons_trowgib(self.origin, (2 * randomvec() - '1 1 1') * 45,'1 1 1',"models/onslaught/controlpoint_icon_gib2.md3",5,1,1);
731                         ons_trowgib(self.origin, (2 * randomvec() - '1 1 1') * 45,'1 1 1',"models/onslaught/controlpoint_icon_gib2.md3",5,1,1);
732                         ons_trowgib(self.origin, (2 * randomvec() - '1 1 1') * 75,'1 1 1',"models/onslaught/controlpoint_icon_gib4.md3",5,1,1);
733                         ons_trowgib(self.origin, (2 * randomvec() - '1 1 1') * 75,'1 1 1',"models/onslaught/controlpoint_icon_gib4.md3",5,1,1);
734                         ons_trowgib(self.origin, (2 * randomvec() - '1 1 1') * 75,'1 1 1',"models/onslaught/controlpoint_icon_gib4.md3",5,1,1);
735                         ons_trowgib(self.origin, (2 * randomvec() - '1 1 1') * 75,'1 1 1',"models/onslaught/controlpoint_icon_gib4.md3",5,1,1);
736                 }
737                 self.owner.goalentity = world;
738                 self.owner.islinked = FALSE;
739                 self.owner.iscaptured = FALSE;
740                 self.owner.team = 0;
741                 self.owner.colormap = 1024;
742                 onslaught_updatelinks();
743
744                 // Use targets now (somebody make sure this is in the right place..)
745                 oself = self;
746                 self = self.owner;
747                 activator = self;
748                 SUB_UseTargets ();
749                 self = oself;
750
751                 self.owner.waslinked = self.owner.islinked;
752                 if(self.owner.model != "models/onslaught/controlpoint_pad.md3")
753                         setmodel(self.owner, "models/onslaught/controlpoint_pad.md3");
754
755                 remove(self);
756         }
757 };
758
759 void onslaught_controlpoint_icon_think()
760 {
761         entity oself;
762         self.nextthink = time + 0.05;
763         if (time > self.pain_finished + 5)
764         {
765                 self.health = self.health + self.count;
766                 if (self.health >= self.max_health)
767                         self.health = self.max_health;
768         }
769         if (self.health < self.max_health * 0.90)
770                 setmodel(self, "models/onslaught/controlpoint_icon.md3");
771         if (self.health < self.max_health * 0.75)
772                 setmodel(self, "models/onslaught/controlpoint_icon_dmg1.md3");
773         if (self.health < self.max_health * 0.50)
774                 setmodel(self, "models/onslaught/controlpoint_icon_dmg2.md3");
775         if (self.health < self.max_health * 0.25)
776                 setmodel(self, "models/onslaught/controlpoint_icon_dmg3.md3");
777         // colormod flash when shot
778         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
779
780         if(self.owner.islinked != self.owner.waslinked)
781         {
782                 // unteam the spawnpoint if needed
783                 float t;
784                 t = self.owner.team;
785                 if(!self.owner.islinked)
786                         self.owner.team = 0;
787
788                 oself = self;
789                 self = self.owner;
790                 activator = self;
791                 SUB_UseTargets ();
792                 self = oself;
793
794                 self.owner.team = t;
795
796                 self.owner.waslinked = self.owner.islinked;
797         }
798         if (self.punchangle_x > 2)
799                 self.punchangle_x = self.punchangle_x - 2;
800         else if (self.punchangle_x < -2)
801                 self.punchangle_x = self.punchangle_x + 2;
802         else
803                 self.punchangle_x = 0;
804         if (self.punchangle_y > 2)
805                 self.punchangle_y = self.punchangle_y - 2;
806         else if (self.punchangle_y < -2)
807                 self.punchangle_y = self.punchangle_y + 2;
808         else
809                 self.punchangle_y = 0;
810         if (self.punchangle_z > 2)
811                 self.punchangle_z = self.punchangle_z - 2;
812         else if (self.punchangle_z < -2)
813                 self.punchangle_z = self.punchangle_z + 2;
814         else
815                 self.punchangle_z = 0;
816         self.angles_x = self.punchangle_x;
817         self.angles_y = self.angles_y + self.punchangle_y + 2;
818         self.angles_z = self.punchangle_z;
819
820         self.cp_bob_origin_z = 4 * PI * (1 - cos(self.cp_bob_spd / 8));
821         self.cp_bob_spd = self.cp_bob_spd + 0.5;
822         if(self.cp_bob_dmg_z > 0)
823                 self.cp_bob_dmg_z = self.cp_bob_dmg_z - 0.1;
824         else
825                 self.cp_bob_dmg_z = 0;
826         self.origin = self.cp_origin + self.cp_bob_origin + self.cp_bob_dmg;
827 };
828
829 void onslaught_controlpoint_icon_buildthink()
830 {
831         local entity oself;
832         float a;
833
834         self.nextthink = time + 0.05;
835
836         // only do this if there is power
837         a = onslaught_controlpoint_can_be_linked(self.owner, self.owner.team);
838         if(!a)
839                 return;
840
841         self.health = self.health + (self.count / 2);
842
843         if (self.health >= self.max_health)
844         {
845                 self.health = self.max_health;
846                 self.count = self.count * 0.1; // slow repair rate from now on
847                 self.think = onslaught_controlpoint_icon_think;
848                 sound(self, CHAN_TRIGGER, "onslaught/controlpoint_built.wav", VOL_BASE, ATTN_NORM);
849                 bprint(ColoredTeamName(self.team), " captured ", self.owner.message, " control point\n");
850                 self.owner.iscaptured = TRUE;
851                 onslaught_updatelinks();
852
853                 // Use targets now (somebody make sure this is in the right place..)
854                 oself = self;
855                 self = self.owner;
856                 activator = self;
857                 SUB_UseTargets ();
858                 self = oself;
859                 self.cp_origin = self.origin;
860                 self.cp_bob_origin = '0 0 0.1';
861                 self.cp_bob_spd = 0;
862         }
863         self.alpha = self.health / self.max_health;
864         // colormod flash when shot
865         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
866         if(self.owner.model != "models/onslaught/controlpoint_pad2.md3")
867                 setmodel(self.owner, "models/onslaught/controlpoint_pad2.md3");
868 };
869
870 void onslaught_controlpoint_touch()
871 {
872         local entity e;
873         float a;
874         if (other.classname != "player")
875                 return;
876         a = onslaught_controlpoint_attackable(self, other.team);
877         if(a != 2 && a != 4)
878                 return;
879         // we've verified that this player has a legitimate claim to this point,
880         // so start building the captured point icon (which only captures this
881         // point if it successfully builds without being destroyed first)
882         self.goalentity = e = spawn();
883         e.classname = "onslaught_controlpoint_icon";
884         e.owner = self;
885         e.max_health = cvar("g_onslaught_cp_health");
886         e.health = e.max_health * 0.1;
887         e.solid = SOLID_BBOX;
888         e.movetype = MOVETYPE_NONE;
889         setmodel(e, "models/onslaught/controlpoint_icon.md3");
890         setsize(e, '-32 -32 -32', '32 32 32');
891         setorigin(e, self.origin + '0 0 96');
892         e.takedamage = DAMAGE_AIM;
893         e.bot_attack = TRUE;
894         e.event_damage = onslaught_controlpoint_icon_damage;
895         e.team = other.team;
896         e.colormap = 1024 + (e.team - 1) * 17;
897         e.think = onslaught_controlpoint_icon_buildthink;
898         e.nextthink = time + 0.1;
899         e.count = e.max_health / 50; // how long it takes to build
900         sound(e, CHAN_TRIGGER, "onslaught/controlpoint_build.wav", VOL_BASE, ATTN_NORM);
901         self.team = e.team;
902         self.colormap = e.colormap;
903 };
904
905 void onslaught_controlpoint_reset()
906 {
907         if(self.goalentity && self.goalentity != world)
908                 remove(self.goalentity);
909         self.goalentity = world;
910         self.team = 0;
911         self.colormap = 1024;
912         self.iscaptured = FALSE;
913         self.islinked = FALSE;
914         self.isshielded = TRUE;
915         self.enemy.solid = SOLID_NOT;
916         self.enemy.colormap = self.colormap;
917         self.think = self.enemy.think = SUB_Null;
918         self.nextthink = 0; // don't like SUB_Null :P
919         
920         onslaught_updatelinks();
921
922         activator = self;
923         SUB_UseTargets(); // to reset the structures, playerspawns etc.
924 }
925
926 /*QUAKED spawnfunc_onslaught_controlpoint (0 .5 .8) (-32 -32 0) (32 32 128)
927 Control point.  Be sure to give this enough clearance so that the shootable part has room to exist
928
929 This should link to an spawnfunc_onslaught_controlpoint entity or spawnfunc_onslaught_generator entity.
930
931 keys:
932 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
933 "target" - target any entities that are tied to this control point, such as vehicles and buildable structure entities.
934 "message" - name of this control point (should reflect the location in the map, such as "center bridge", "north tower", etc)
935 */
936 void spawnfunc_onslaught_controlpoint()
937 {
938         local entity e;
939         if (!g_onslaught)
940         {
941                 remove(self);
942                 return;
943         }
944         precache_model("models/onslaught/controlpoint_pad.md3");
945         precache_model("models/onslaught/controlpoint_pad2.md3");
946         precache_model("models/onslaught/controlpoint_shield.md3");
947         precache_model("models/onslaught/controlpoint_icon.md3");
948         precache_model("models/onslaught/controlpoint_icon_dmg1.md3");
949         precache_model("models/onslaught/controlpoint_icon_dmg2.md3");
950         precache_model("models/onslaught/controlpoint_icon_dmg3.md3");
951         precache_model("models/onslaught/controlpoint_icon_gib1.md3");
952         precache_model("models/onslaught/controlpoint_icon_gib2.md3");
953         precache_model("models/onslaught/controlpoint_icon_gib4.md3");
954         precache_sound("onslaught/controlpoint_build.wav");
955         precache_sound("onslaught/controlpoint_built.wav");
956         precache_sound("weapons/grenade_impact.wav");
957         precache_sound("onslaught/damageblockedbyshield.wav");
958         precache_sound("onslaught/controlpoint_underattack.wav");
959         self.solid = SOLID_BSP;
960         self.movetype = MOVETYPE_NONE;
961         setmodel(self, "models/onslaught/controlpoint_pad.md3");
962         //setsize(self, '-32 -32 0', '32 32 8');
963         setorigin(self, self.origin);
964         self.touch = onslaught_controlpoint_touch;
965         self.team = 0;
966         self.colormap = 1024;
967         self.iscaptured = FALSE;
968         self.islinked = FALSE;
969         self.isshielded = TRUE;
970         // spawn shield model which indicates whether this can be damaged
971         self.enemy = e = spawn();
972         e.classname = "onslaught_controlpoint_shield";
973         e.solid = SOLID_NOT;
974         e.movetype = MOVETYPE_NONE;
975         e.effects = EF_ADDITIVE;
976         setmodel(e, "models/onslaught/controlpoint_shield.md3");
977         //setsize(e, '-32 -32 0', '32 32 128');
978         setorigin(e, self.origin);
979         e.colormap = self.colormap;
980
981         waypoint_spawnforitem(self);
982
983         WaypointSprite_SpawnFixed(string_null, e.origin + '0 0 1' * e.maxs_z, self, sprite);
984         WaypointSprite_UpdateRule(self.sprite, COLOR_TEAM2, SPRITERULE_TEAMPLAY);
985
986         onslaught_updatelinks();
987
988         self.reset = onslaught_controlpoint_reset;
989 };
990
991 float onslaught_link_send(entity to, float sendflags)
992 {
993         WriteByte(MSG_ENTITY, ENT_CLIENT_RADARLINK);
994         WriteByte(MSG_ENTITY, sendflags);
995         if(sendflags & 1)
996         {
997                 WriteCoord(MSG_ENTITY, self.goalentity.origin_x);
998                 WriteCoord(MSG_ENTITY, self.goalentity.origin_y);
999                 WriteCoord(MSG_ENTITY, self.goalentity.origin_z);
1000         }
1001         if(sendflags & 2)
1002         {
1003                 WriteCoord(MSG_ENTITY, self.enemy.origin_x);
1004                 WriteCoord(MSG_ENTITY, self.enemy.origin_y);
1005                 WriteCoord(MSG_ENTITY, self.enemy.origin_z);
1006         }
1007         if(sendflags & 4)
1008         {
1009                 WriteByte(MSG_ENTITY, self.clientcolors); // which is goalentity's color + enemy's color * 16
1010         }
1011         return TRUE;
1012 }
1013
1014 void onslaught_link_checkupdate()
1015 {
1016         // TODO check if the two sides have moved (currently they won't move anyway)
1017         float redpower, bluepower;
1018
1019         redpower = bluepower = 0;
1020         if(self.goalentity.islinked)
1021         {
1022                 if(self.goalentity.team == COLOR_TEAM1)
1023                         redpower = 1;
1024                 else if(self.goalentity.team == COLOR_TEAM2)
1025                         bluepower = 1;
1026         }
1027         if(self.enemy.islinked)
1028         {
1029                 if(self.enemy.team == COLOR_TEAM1)
1030                         redpower = 2;
1031                 else if(self.enemy.team == COLOR_TEAM2)
1032                         bluepower = 2;
1033         }
1034
1035         float cc;
1036         if(redpower == 1 && bluepower == 2)
1037                 cc = (COLOR_TEAM1 - 1) * 0x01 + (COLOR_TEAM2 - 1) * 0x10;
1038         else if(redpower == 2 && bluepower == 1)
1039                 cc = (COLOR_TEAM1 - 1) * 0x10 + (COLOR_TEAM2 - 1) * 0x01;
1040         else if(redpower)
1041                 cc = (COLOR_TEAM1 - 1) * 0x11;
1042         else if(bluepower)
1043                 cc = (COLOR_TEAM2 - 1) * 0x11;
1044         else
1045                 cc = 0;
1046
1047         if(cc != self.clientcolors)
1048         {
1049                 self.clientcolors = cc;
1050                 self.SendFlags |= 4;
1051         }
1052
1053         self.nextthink = time;
1054 }
1055
1056 void onslaught_link_delayed()
1057 {
1058         self.goalentity = find(world, targetname, self.target);
1059         self.enemy = find(world, targetname, self.target2);
1060         if (!self.goalentity)
1061                 objerror("can not find target\n");
1062         if (!self.enemy)
1063                 objerror("can not find target2\n");
1064         dprint(etos(self.goalentity), " linked with ", etos(self.enemy), "\n");
1065
1066         self.SendFlags |= 3;
1067         self.think = onslaught_link_checkupdate;
1068         self.nextthink = time;
1069 }
1070
1071 /*QUAKED spawnfunc_onslaught_link (0 .5 .8) (-16 -16 -16) (16 16 16)
1072 Link between control points.
1073
1074 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.
1075
1076 keys:
1077 "target" - first control point.
1078 "target2" - second control point.
1079 */
1080 void spawnfunc_onslaught_link()
1081 {
1082         if (!g_onslaught)
1083         {
1084                 remove(self);
1085                 return;
1086         }
1087         if (self.target == "" || self.target2 == "")
1088                 objerror("target and target2 must be set\n");
1089         InitializeEntity(self, onslaught_link_delayed, INITPRIO_FINDTARGET);
1090         Net_LinkEntity(self, FALSE, 0, onslaught_link_send);
1091 };