oops... huge bugs in the spawnfunc patch... it also changed them in strings, probably...
[divverent/nexuiz.git] / data / qcsrc / server / mode_onslaught.qc
1 .entity sprite;
2 .string target2;
3 .float iscaptured;
4 .float islinked;
5 .float isgenneighbor_red;
6 .float isgenneighbor_blue;
7 .float iscpneighbor_red;
8 .float iscpneighbor_blue;
9 .float isshielded;
10 .float lasthealth;
11
12 float ons_sprite_cp_red, ons_sprite_cp_blue, ons_sprite_cp_neut;
13 float ons_sprite_cp_atck_red, ons_sprite_cp_atck_blue, ons_sprite_cp_atck_neut;
14 float ons_sprite_cp_dfnd_red, ons_sprite_cp_dfnd_blue;
15 float ons_sprite_gen_red, ons_sprite_gen_blue, ons_sprite_gen_shielded;
16
17 void() onslaught_updatelinks =
18 {
19         local entity l, links;
20         local float stop, t1, t2, t3, t4;
21         // first check if the game has ended
22         dprint("--- updatelinks ---\n");
23         links = findchain(classname, "onslaught_link");
24         // mark generators as being shielded and networked
25         l = findchain(classname, "onslaught_generator");
26         while (l)
27         {
28                 if (l.iscaptured)
29                         dprint(etos(l), " (generator) belongs to team ", ftos(l.team), "\n");
30                 else
31                         dprint(etos(l), " (generator) is destroyed\n");
32                 l.islinked = l.iscaptured;
33                 l.isshielded = l.iscaptured;
34                 l = l.chain;
35         }
36         // mark points as shielded and not networked
37         l = findchain(classname, "onslaught_controlpoint");
38         while (l)
39         {
40                 l.islinked = FALSE;
41                 l.isshielded = TRUE;
42                 l.isgenneighbor_red = FALSE;
43                 l.isgenneighbor_blue = FALSE;
44                 l.iscpneighbor_red = FALSE;
45                 l.iscpneighbor_blue = FALSE;
46                 dprint(etos(l), " (point) belongs to team ", ftos(l.team), "\n");
47                 l = l.chain;
48         }
49         // flow power outward from the generators through the network
50         l = links;
51         while (l)
52         {
53                 dprint(etos(l), " (link) connects ", etos(l.goalentity), " with ", etos(l.enemy), "\n");
54                 l = l.chain;
55         }
56         stop = FALSE;
57         while (!stop)
58         {
59                 stop = TRUE;
60                 l = links;
61                 while (l)
62                 {
63                         // if both points are captured by the same team, and only one of
64                         // them is powered, mark the other one as powered as well
65                         if (l.enemy.iscaptured && l.goalentity.iscaptured)
66                         if (l.enemy.islinked != l.goalentity.islinked)
67                         if (l.enemy.team == l.goalentity.team)
68                         {
69                                 if (!l.goalentity.islinked)
70                                 {
71                                         stop = FALSE;
72                                         l.goalentity.islinked = TRUE;
73                                         dprint(etos(l), " (link) is marking ", etos(l.goalentity), " (point) because its team matches ", etos(l.enemy), " (point)\n");
74                                 }
75                                 else if (!l.enemy.islinked)
76                                 {
77                                         stop = FALSE;
78                                         l.enemy.islinked = TRUE;
79                                         dprint(etos(l), " (link) is marking ", etos(l.enemy), " (point) because its team matches ", etos(l.goalentity), " (point)\n");
80                                 }
81                         }
82                         l = l.chain;
83                 }
84         }
85         // now that we know which points are powered we can mark their neighbors
86         // as unshielded if team differs
87         l = links;
88         while (l)
89         {
90                 if (l.goalentity.team != l.enemy.team)
91                 {
92                         if (l.goalentity.islinked)
93                         {
94                                 dprint(etos(l), " (link) is unshielding ", etos(l.enemy), " (point) because its team does not match ", etos(l.goalentity), " (point)\n");
95                                 l.enemy.isshielded = FALSE;
96                                 if(l.goalentity.classname == "onslaught_generator")
97                                 {
98                                         if(l.goalentity.team == COLOR_TEAM1)
99                                                 l.enemy.isgenneighbor_red = TRUE;
100                                         else if(l.goalentity.team == COLOR_TEAM2)
101                                                 l.enemy.isgenneighbor_blue = TRUE;
102                                 }
103                                 else
104                                 {
105                                         if(l.goalentity.team == COLOR_TEAM1)
106                                                 l.enemy.iscpneighbor_red = TRUE;
107                                         else if(l.goalentity.team == COLOR_TEAM2)
108                                                 l.enemy.iscpneighbor_blue = TRUE;
109                                 }
110                         }
111                         if (l.enemy.islinked)
112                         {
113                                 dprint(etos(l), " (link) is unshielding ", etos(l.goalentity), " (point) because its team does not match ", etos(l.enemy), " (point)\n");
114                                 l.goalentity.isshielded = FALSE;
115                                 if(l.enemy.classname == "onslaught_generator")
116                                 {
117                                         if(l.enemy.team == COLOR_TEAM1)
118                                                 l.goalentity.isgenneighbor_red = TRUE;
119                                         else if(l.enemy.team == COLOR_TEAM2)
120                                                 l.goalentity.isgenneighbor_blue = TRUE;
121                                 }
122                                 else
123                                 {
124                                         if(l.enemy.team == COLOR_TEAM1)
125                                                 l.goalentity.iscpneighbor_red = TRUE;
126                                         else if(l.enemy.team == COLOR_TEAM2)
127                                                 l.goalentity.iscpneighbor_blue = TRUE;
128                                 }
129                         }
130                 }
131                 l = l.chain;
132         }
133         // now update the takedamage and alpha variables on generator shields
134         l = findchain(classname, "onslaught_generator");
135         while (l)
136         {
137                 if (l.isshielded)
138                 {
139                         dprint(etos(l), " (generator) is shielded\n");
140                         l.enemy.alpha = 1;
141                         l.takedamage = DAMAGE_NO;
142                         l.bot_attack = FALSE;
143                 }
144                 else
145                 {
146                         dprint(etos(l), " (generator) is not shielded\n");
147                         l.enemy.alpha = -1;
148                         l.takedamage = DAMAGE_AIM;
149                         l.bot_attack = TRUE;
150                 }
151                 l = l.chain;
152         }
153         // now update the takedamage and alpha variables on control point icons
154         l = findchain(classname, "onslaught_controlpoint");
155         while (l)
156         {
157                 if (l.isshielded)
158                 {
159                         dprint(etos(l), " (point) is shielded\n");
160                         l.enemy.alpha = 1;
161                         if (l.goalentity)
162                         {
163                                 l.goalentity.takedamage = DAMAGE_NO;
164                                 l.goalentity.bot_attack = FALSE;
165                         }
166                 }
167                 else
168                 {
169                         dprint(etos(l), " (point) is not shielded\n");
170                         l.enemy.alpha = -1;
171                         if (l.goalentity)
172                         {
173                                 l.goalentity.takedamage = DAMAGE_AIM;
174                                 l.goalentity.bot_attack = TRUE;
175                         }
176                 }
177                 l = l.chain;
178         }
179         // count generators owned by each team
180         t1 = t2 = t3 = t4 = 0;
181         l = findchain(classname, "onslaught_generator");
182         while (l)
183         {
184                 if (l.iscaptured)
185                 {
186                         if (l.team == COLOR_TEAM1) t1 = 1;
187                         if (l.team == COLOR_TEAM2) t2 = 1;
188                         if (l.team == COLOR_TEAM3) t3 = 1;
189                         if (l.team == COLOR_TEAM4) t4 = 1;
190                 }
191                 l = l.chain;
192         }
193         // see if multiple teams remain (if not, it's game over)
194         if (t1 + t2 + t3 + t4 < 2)
195                 dprint("--- game over ---\n");
196         else
197                 dprint("--- done updating links ---\n");
198 };
199
200 float onslaught_controlpoint_can_be_linked(entity cp, float t)
201 {
202         if(t == COLOR_TEAM1)
203         {
204                 if(cp.isgenneighbor_red)
205                         return 2;
206                 if(cp.iscpneighbor_red)
207                         return 1;
208         }
209         else if(t == COLOR_TEAM2)
210         {
211                 if(cp.isgenneighbor_blue)
212                         return 2;
213                 if(cp.iscpneighbor_blue)
214                         return 1;
215         }
216         return 0;
217 /*
218         entity e;
219         // check to see if this player has a legitimate claim to capture this
220         // control point - more specifically that there is a captured path of
221         // points leading back to the team generator
222         e = findchain(classname, "onslaught_link");
223         while (e)
224         {
225                 if (e.goalentity == cp)
226                 {
227                         dprint(etos(e), " (link) connects to ", etos(e.enemy), " (point)");
228                         if (e.enemy.islinked)
229                         {
230                                 dprint(" which is linked");
231                                 if (e.enemy.team == t)
232                                 {
233                                         dprint(" and has the correct team!\n");
234                                         return 1;
235                                 }
236                                 else
237                                         dprint(" but has the wrong team\n");
238                         }
239                         else
240                                 dprint("\n");
241                 }
242                 else if (e.enemy == cp)
243                 {
244                         dprint(etos(e), " (link) connects to ", etos(e.goalentity), " (point)");
245                         if (e.goalentity.islinked)
246                         {
247                                 dprint(" which is linked");
248                                 if (e.goalentity.team == t)
249                                 {
250                                         dprint(" and has a team!\n");
251                                         return 1;
252                                 }
253                                 else
254                                         dprint(" but has the wrong team\n");
255                         }
256                         else
257                                 dprint("\n");
258                 }
259                 e = e.chain;
260         }
261         return 0;
262 */
263 }
264
265 float onslaught_controlpoint_attackable(entity cp, float t)
266 // -2: SAME TEAM, attackable by enemy!
267 // -1: SAME TEAM!
268 // 0:  off limits
269 // 1:  attack it
270 // 2:  touch it
271 // 3:  attack it (HIGH PRIO)
272 // 4:  touch it (HIGH PRIO)
273 {
274         float a;
275
276         if(cp.isshielded)
277         {
278                 return 0;
279         }
280         else if(cp.goalentity)
281         {
282                 // if there's already an icon built, nothing happens
283                 if(cp.team == t)
284                 {
285                         a = onslaught_controlpoint_can_be_linked(cp, COLOR_TEAM1 + COLOR_TEAM2 - t);
286                         if(a) // attackable by enemy?
287                                 return -2; // EMERGENCY!
288                         return -1;
289                 }
290                 // we know it can be linked, so no need to check
291                 // but...
292                 a = onslaught_controlpoint_can_be_linked(cp, t);
293                 if(a == 2) // near our generator?
294                         return 3; // EMERGENCY!
295                 return 1;
296         }
297         else
298         {
299                 // free point
300                 if(onslaught_controlpoint_can_be_linked(cp, t))
301                 {
302                         a = onslaught_controlpoint_can_be_linked(cp, COLOR_TEAM1 + COLOR_TEAM2 - t);
303                         if(a == 2)
304                                 return 4; // GET THIS ONE NOW!
305                         else
306                                 return 2; // TOUCH ME
307                 }
308         }
309         return 0;
310 }
311
312 void() onslaught_generator_think =
313 {
314         local float d;
315         local entity e;
316         self.nextthink = ceil(time + 1);
317         if (cvar("timelimit"))
318         if (time > cvar("timelimit") * 60 - 60)
319         {
320                 // self.max_health / 300 gives 5 minutes of overtime.
321                 // control points reduce the overtime duration.
322                 sound(self, CHAN_AUTO, "sound/onslaught/generator_decay.wav", 1, ATTN_NORM);
323                 d = 1;
324                 e = findchain(classname, "onslaught_controlpoint");
325                 while (e)
326                 {
327                         if (e.team != self.team)
328                         if (e.islinked)
329                                 d = d + 1;
330                         e = e.chain;
331                 }
332                 d = d * self.max_health / 300;
333                 Damage(self, self, self, d, DEATH_HURTTRIGGER, self.origin, '0 0 0');
334         }
335 };
336
337 void() onslaught_generator_deaththink =
338 {
339         local vector org;
340         if (self.count > 0)
341         {
342                 self.nextthink = time + 0.1;
343                 self.count = self.count - 1;
344                 org = randompos(self.origin + self.mins + '8 8 8', self.origin + self.maxs + '-8 -8 -8');
345                 pointparticles(particleeffectnum("onslaught_generator_smallexplosion"), org, '0 0 0', 1);
346                 sound(self, CHAN_AUTO, "sound/weapons/grenade_impact.wav", 1, ATTN_NORM);
347         }
348         else
349         {
350                 org = self.origin;
351                 pointparticles(particleeffectnum("onslaught_generator_finalexplosion"), org, '0 0 0', 1);
352                 sound(self, CHAN_AUTO, "sound/weapons/rocket_impact.wav", 1, ATTN_NORM);
353         }
354 };
355
356 void(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force) onslaught_generator_damage =
357 {
358         if (damage <= 0)
359                 return;
360         if (attacker != self)
361         {
362                 if (self.isshielded)
363                 {
364                         // this is protected by a shield, so ignore the damage
365                         if (time > self.pain_finished)
366                         if (attacker.classname == "player")
367                         {
368                                 play2(attacker, "sound/onslaught/damageblockedbyshield.wav");
369                                 self.pain_finished = time + 1;
370                         }
371                         return;
372                 }
373                 if (time > self.pain_finished)
374                 {
375                         self.pain_finished = time + 5;
376                         bprint(ColoredTeamName(self.team), " generator under attack!\n");
377                         play2team(self.team, "sound/onslaught/generator_underattack.wav");
378                 }
379         }
380         self.health = self.health - damage;
381         // choose an animation frame based on health
382         self.frame = 10 * bound(0, (1 - self.health / self.max_health), 1);
383         // see if the generator is still functional, or dying
384         if (self.health > 0)
385         {
386                 float h, lh;
387                 lh = ceil(self.lasthealth / 100) * 100;
388                 h = ceil(self.health / 100) * 100;
389                 if(lh != h)
390                         bprint(ColoredTeamName(self.team), " generator has less than ", ftos(h), " health remaining\n");
391                 self.lasthealth = self.health;
392         }
393         else
394         {
395                 if (attacker == self)
396                         bprint(ColoredTeamName(self.team), " generator spontaneously exploded due to overtime!\n");
397                 else
398                 {
399                         string t;
400                         t = ColoredTeamName(attacker.team);
401                         bprint(ColoredTeamName(self.team), " generator destroyed by ", t, "!\n");
402                 }
403                 self.iscaptured = FALSE;
404                 self.islinked = FALSE;
405                 self.isshielded = FALSE;
406                 self.takedamage = DAMAGE_NO; // can't be hurt anymore
407                 self.event_damage = SUB_Null; // won't do anything if hurt
408                 self.count = 30; // 30 explosions
409                 self.think = onslaught_generator_deaththink; // explosion sequence
410                 self.nextthink = time; // start exploding immediately
411                 self.think(); // do the first explosion now
412                 onslaught_updatelinks();
413         }
414 };
415
416 // update links after a delay
417 void() onslaught_generator_delayed =
418 {
419         onslaught_updatelinks();
420         // now begin normal thinking
421         self.think = onslaught_generator_think;
422         self.nextthink = time;
423 };
424
425 float onslaught_generator_waypointsprite_for_player(entity e)
426 {
427         if(e.classname == "player")
428                 if(e.team == self.owner.team)
429                 {
430                         if(self.owner.team == COLOR_TEAM1)
431                                 return ons_sprite_gen_red;
432                         else if(self.owner.team == COLOR_TEAM2)
433                                 return ons_sprite_gen_blue;
434                 }
435         if(self.owner.isshielded)
436                 return ons_sprite_gen_shielded;
437         if(self.owner.team == COLOR_TEAM1)
438                 return ons_sprite_gen_red;
439         else if(self.owner.team == COLOR_TEAM2)
440                 return ons_sprite_gen_blue;
441         return 0;
442 }
443
444 float onslaught_controlpoint_waypointsprite_for_player(entity e)
445 {
446         float a;
447         if(e.classname == "player")
448         {
449                 a = onslaught_controlpoint_attackable(self.owner, e.team);
450                 if(a == 3 || a == 4) // ATTACK/TOUCH THIS ONE NOW
451                 {
452                         if(self.owner.team == COLOR_TEAM1)
453                                 return ons_sprite_cp_atck_red;
454                         else if(self.owner.team == COLOR_TEAM2)
455                                 return ons_sprite_cp_atck_blue;
456                         else
457                                 return ons_sprite_cp_atck_neut;
458                 }
459                 else if(a == -2) // DEFEND THIS ONE NOW
460                 {
461                         if(self.owner.team == COLOR_TEAM1)
462                                 return ons_sprite_cp_dfnd_red;
463                         else if(self.owner.team == COLOR_TEAM2)
464                                 return ons_sprite_cp_dfnd_blue;
465                 }
466                 else if(self.owner.team == e.team || a == -1 || a == 1) // own point, or fire at it
467                 {
468                         if(self.owner.team == COLOR_TEAM1)
469                                 return ons_sprite_cp_red;
470                         else if(self.owner.team == COLOR_TEAM2)
471                                 return ons_sprite_cp_blue;
472                 }
473                 else if(a == 2) // touch it
474                         return ons_sprite_cp_neut;
475         }
476         else
477         {
478                 if(self.owner.team == COLOR_TEAM1)
479                         return ons_sprite_cp_red;
480                 else if(self.owner.team == COLOR_TEAM2)
481                         return ons_sprite_cp_blue;
482                 else
483                         return ons_sprite_cp_neut;
484         }
485         return 0;
486 }
487
488 /*QUAKED spawnfunc_onslaught_generator (0 .5 .8) (-32 -32 -24) (32 32 64)
489 Base generator.
490
491 spawnfunc_onslaught_link entities can target this.
492
493 keys:
494 "team" - team that owns this generator (5 = red, 14 = blue, etc), MUST BE SET.
495 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
496 */
497 void() spawnfunc_onslaught_generator =
498 {
499         if (!g_onslaught)
500         {
501                 remove(self);
502                 return;
503         }
504
505         if(!ons_sprite_cp_blue)
506         {
507                 precache_model("models/sprites/ons-cp-blue.sp2");
508                 setmodel(self, "models/sprites/ons-cp-blue.sp2");
509                 ons_sprite_cp_blue = self.modelindex;
510                 precache_model("models/sprites/ons-cp-red.sp2");
511                 setmodel(self, "models/sprites/ons-cp-red.sp2");
512                 ons_sprite_cp_red = self.modelindex;
513                 precache_model("models/sprites/ons-cp-neut.sp2");
514                 setmodel(self, "models/sprites/ons-cp-neut.sp2");
515                 ons_sprite_cp_neut = self.modelindex;
516                 precache_model("models/sprites/ons-cp-dfnd-blue.sp2");
517                 setmodel(self, "models/sprites/ons-cp-dfnd-blue.sp2");
518                 ons_sprite_cp_dfnd_blue = self.modelindex;
519                 precache_model("models/sprites/ons-cp-dfnd-red.sp2");
520                 setmodel(self, "models/sprites/ons-cp-dfnd-red.sp2");
521                 ons_sprite_cp_dfnd_red = self.modelindex;
522                 precache_model("models/sprites/ons-cp-atck-blue.sp2");
523                 setmodel(self, "models/sprites/ons-cp-atck-blue.sp2");
524                 ons_sprite_cp_atck_blue = self.modelindex;
525                 precache_model("models/sprites/ons-cp-atck-red.sp2");
526                 setmodel(self, "models/sprites/ons-cp-atck-red.sp2");
527                 ons_sprite_cp_atck_red = self.modelindex;
528                 precache_model("models/sprites/ons-cp-atck-neut.sp2");
529                 setmodel(self, "models/sprites/ons-cp-atck-neut.sp2");
530                 ons_sprite_cp_atck_neut = self.modelindex;
531                 precache_model("models/sprites/ons-gen-blue.sp2");
532                 setmodel(self, "models/sprites/ons-gen-blue.sp2");
533                 ons_sprite_gen_blue = self.modelindex;
534                 precache_model("models/sprites/ons-gen-red.sp2");
535                 setmodel(self, "models/sprites/ons-gen-red.sp2");
536                 ons_sprite_gen_red = self.modelindex;
537                 precache_model("models/sprites/ons-gen-shielded.sp2");
538                 setmodel(self, "models/sprites/ons-gen-shielded.sp2");
539                 ons_sprite_gen_shielded = self.modelindex;
540         }
541
542         local entity e;
543         precache_model("models/onslaught/generator.md3");
544         precache_model("models/onslaught/generator_shield.md3");
545         precache_sound("sound/onslaught/generator_decay.wav");
546         precache_sound("sound/weapons/grenade_impact.wav");
547         precache_sound("sound/weapons/rocket_impact.wav");
548         precache_sound("sound/onslaught/generator_underattack.wav");
549         if (!self.team)
550                 objerror("team must be set");
551         self.team_saved = self.team;
552         self.colormap = 1024 + (self.team - 1) * 17;
553         self.solid = SOLID_BSP;
554         self.movetype = MOVETYPE_NONE;
555         self.lasthealth = self.max_health = self.health = cvar("g_onslaught_gen_health");
556         setmodel(self, "models/onslaught/generator.md3");
557         //setsize(self, '-32 -32 -24', '32 32 64');
558         setorigin(self, self.origin);
559         self.takedamage = DAMAGE_AIM;
560         self.bot_attack = TRUE;
561         self.event_damage = onslaught_generator_damage;
562         self.iscaptured = TRUE;
563         self.islinked = TRUE;
564         self.isshielded = TRUE;
565         // spawn shield model which indicates whether this can be damaged
566         self.enemy = e = spawn();
567         e.solid = SOLID_NOT;
568         e.movetype = MOVETYPE_NONE;
569         e.effects = EF_ADDITIVE;
570         setmodel(e, "models/onslaught/generator_shield.md3");
571         //setsize(e, '-32 -32 0', '32 32 128');
572         setorigin(e, self.origin);
573         e.colormap = self.colormap;
574         e.team = self.team;
575         self.think = onslaught_generator_delayed;
576         self.nextthink = time + 0.2;
577
578         WaypointSprite_SpawnFixed(string_null, e.origin + '0 0 1' * e.maxs_z, self, sprite);
579         self.sprite.waypointsprite_for_player = onslaught_generator_waypointsprite_for_player;
580 };
581
582 void() onslaught_generator_reset =
583 {
584         self.team = self.team_saved;
585         self.lasthealth = self.max_health = self.health = cvar("g_onslaught_gen_health");
586         self.takedamage = DAMAGE_AIM;
587         self.bot_attack = TRUE;
588         self.iscaptured = TRUE;
589         self.islinked = TRUE;
590         self.isshielded = TRUE;
591         self.enemy.solid = SOLID_NOT;
592         self.think = onslaught_generator_delayed;
593         self.nextthink = time + 0.2;
594 }
595
596 void(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force) onslaught_controlpoint_icon_damage =
597 {
598         entity oself;
599         if (damage <= 0)
600                 return;
601         if (self.owner.isshielded)
602         {
603                 // this is protected by a shield, so ignore the damage
604                 if (time > self.pain_finished)
605                 if (attacker.classname == "player")
606                 {
607                         play2(attacker, "sound/onslaught/damageblockedbyshield.wav");
608                         self.pain_finished = time + 1;
609                 }
610                 return;
611         }
612         if (time > self.pain_finished)
613         if (attacker.classname == "player")
614         {
615                 play2team(self.team, "sound/onslaught/controlpoint_underattack.wav");
616                 self.pain_finished = time + 5;
617         }
618         self.health = self.health - damage;
619         self.alpha = self.health / self.max_health;
620         self.pain_finished = time + 1;
621         // colormod flash when shot
622         self.colormod = '2 2 2';
623         if (self.health < 0)
624         {
625                 sound(self, CHAN_AUTO, "sound/weapons/grenade_impact.wav", 1, ATTN_NORM);
626                 pointparticles(particleeffectnum("onslaught_controlpoint_explosion"), self.origin, '0 0 0', 1);
627                 {
628                         string t;
629                         t = ColoredTeamName(attacker.team);
630                         bprint(ColoredTeamName(self.team), " ", self.message, " control point destroyed by ", t, "\n");
631                 }
632                 self.owner.goalentity = world;
633                 self.owner.islinked = FALSE;
634                 self.owner.iscaptured = FALSE;
635                 self.owner.team = 0;
636                 self.owner.colormap = 1024;
637                 onslaught_updatelinks();
638
639                 // Use targets now (somebody make sure this is in the right place..)
640                 oself = self;
641                 self = self.owner;
642                 activator = self.owner;
643                 SUB_UseTargets ();
644                 self = oself;
645
646                 remove(self);
647         }
648 };
649
650 void() onslaught_controlpoint_icon_think =
651 {
652         self.nextthink = time + 0.1;
653         if (time > self.pain_finished + 1)
654         {
655                 self.health = self.health + self.count;
656                 if (self.health >= self.max_health)
657                         self.health = self.max_health;
658         }
659         self.alpha = self.health / self.max_health;
660         // colormod flash when shot
661         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
662 };
663
664 void() onslaught_controlpoint_icon_buildthink =
665 {
666         local entity oself;
667
668         self.nextthink = time + 0.1;
669         self.health = self.health + self.count;
670         if (self.health >= self.max_health)
671         {
672                 self.health = self.max_health;
673                 self.count = self.count * 0.2; // slow repair rate from now on
674                 self.think = onslaught_controlpoint_icon_think;
675                 sound(self, CHAN_BODY, "sound/onslaught/controlpoint_built.wav", 1, ATTN_NORM);
676                 bprint(ColoredTeamName(self.team), " captured ", self.owner.message, " control point\n");
677                 self.owner.iscaptured = TRUE;
678                 onslaught_updatelinks();
679
680                 // Use targets now (somebody make sure this is in the right place..)
681                 oself = self;
682                 self = self.owner;
683                 activator = self;
684                 SUB_UseTargets ();
685                 self = oself;
686         }
687         self.alpha = self.health / self.max_health;
688         // colormod flash when shot
689         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
690 };
691
692 void() onslaught_controlpoint_touch =
693 {
694         local entity e;
695         float a;
696         if (other.classname != "player")
697                 return;
698         a = onslaught_controlpoint_attackable(self, other.team);
699         if(a != 2 && a != 4)
700                 return;
701         // we've verified that this player has a legitimate claim to this point,
702         // so start building the captured point icon (which only captures this
703         // point if it successfully builds without being destroyed first)
704         self.goalentity = e = spawn();
705         e.owner = self;
706         e.max_health = cvar("g_onslaught_cp_health");
707         e.health = e.max_health * 0.1;
708         e.alpha = e.health / e.max_health;
709         e.solid = SOLID_BBOX;
710         e.movetype = MOVETYPE_NONE;
711         setmodel(e, "models/onslaught/controlpoint_icon.md3");
712         setsize(e, '-32 -32 -32', '32 32 32');
713         setorigin(e, self.origin + '0 0 96');
714         e.takedamage = DAMAGE_AIM;
715         e.bot_attack = TRUE;
716         e.event_damage = onslaught_controlpoint_icon_damage;
717         e.team = other.team;
718         e.colormap = 1024 + (e.team - 1) * 17;
719         e.think = onslaught_controlpoint_icon_buildthink;
720         e.nextthink = time + 0.1;
721         e.count = e.max_health / 50; // how long it takes to build
722         sound(e, CHAN_BODY, "sound/onslaught/controlpoint_build.wav", 1, ATTN_NORM);
723         self.team = e.team;
724         self.colormap = e.colormap;
725 };
726
727 /*QUAKED spawnfunc_onslaught_controlpoint (0 .5 .8) (-32 -32 0) (32 32 128)
728 Control point.  Be sure to give this enough clearance so that the shootable part has room to exist
729
730 This should link to an spawnfunc_onslaught_controlpoint entity or spawnfunc_onslaught_generator entity.
731
732 keys:
733 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
734 "target" - target any entities that are tied to this control point, such as vehicles and buildable structure entities.
735 "message" - name of this control point (should reflect the location in the map, such as "center bridge", "north tower", etc)
736 */
737 void() spawnfunc_onslaught_controlpoint =
738 {
739         local entity e;
740         if (!g_onslaught)
741         {
742                 remove(self);
743                 return;
744         }
745         precache_model("models/onslaught/controlpoint_pad.md3");
746         precache_model("models/onslaught/controlpoint_shield.md3");
747         precache_model("models/onslaught/controlpoint_icon.md3");
748         precache_sound("sound/onslaught/controlpoint_build.wav");
749         precache_sound("sound/onslaught/controlpoint_built.wav");
750         precache_sound("sound/weapons/grenade_impact.wav");
751         precache_sound("sound/onslaught/damageblockedbyshield.wav");
752         precache_sound("sound/onslaught/controlpoint_underattack.wav");
753         self.solid = SOLID_BSP;
754         self.movetype = MOVETYPE_NONE;
755         setmodel(self, "models/onslaught/controlpoint_pad.md3");
756         //setsize(self, '-32 -32 0', '32 32 8');
757         setorigin(self, self.origin);
758         self.touch = onslaught_controlpoint_touch;
759         self.team = 0;
760         self.colormap = 1024;
761         self.iscaptured = FALSE;
762         self.islinked = FALSE;
763         self.isshielded = TRUE;
764         // spawn shield model which indicates whether this can be damaged
765         self.enemy = e = spawn();
766         e.solid = SOLID_NOT;
767         e.movetype = MOVETYPE_NONE;
768         e.effects = EF_ADDITIVE;
769         setmodel(e, "models/onslaught/controlpoint_shield.md3");
770         //setsize(e, '-32 -32 0', '32 32 128');
771         setorigin(e, self.origin);
772         e.colormap = self.colormap;
773         onslaught_updatelinks();
774
775         waypoint_spawnforitem(self);
776
777         WaypointSprite_SpawnFixed(string_null, e.origin + '0 0 1' * e.maxs_z, self, sprite);
778         self.sprite.waypointsprite_for_player = onslaught_controlpoint_waypointsprite_for_player;
779 };
780
781 void() onslaught_controlpoint_reset =
782 {
783         if(self.goalentity && self.goalentity != world)
784                 remove(self.goalentity);
785         self.goalentity = world;
786         self.team = 0;
787         self.colormap = 1024;
788         self.iscaptured = FALSE;
789         self.islinked = FALSE;
790         self.isshielded = TRUE;
791         self.enemy.solid = SOLID_NOT;
792         self.enemy.colormap = self.colormap;
793         self.think = self.enemy.think = SUB_Null;
794         self.nextthink = 0; // don't like SUB_Null :P
795         
796         onslaught_updatelinks();
797
798         activator = self;
799         SUB_UseTargets(); // to reset the structures, playerspawns etc.
800 }
801
802 void() onslaught_link_delayed =
803 {
804         self.goalentity = find(world, targetname, self.target);
805         self.enemy = find(world, targetname, self.target2);
806         if (!self.goalentity)
807                 objerror("can not find target\n");
808         if (!self.enemy)
809                 objerror("can not find target2\n");
810         dprint(etos(self.goalentity), " linked with ", etos(self.enemy), "\n");
811 }
812
813 /*QUAKED spawnfunc_onslaught_link (0 .5 .8) (-16 -16 -16) (16 16 16)
814 Link between control points.
815
816 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.
817
818 keys:
819 "target" - first control point.
820 "target2" - second control point.
821 */
822 void() spawnfunc_onslaught_link =
823 {
824         if (!g_onslaught)
825         {
826                 remove(self);
827                 return;
828         }
829         if (self.target == "" || self.target2 == "")
830                 objerror("target and target2 must be set\n");
831         self.think = onslaught_link_delayed;
832         self.nextthink = time + 0.1;
833 };