- Removed usetargets code from generator spawn func (should be called elsewhere...
[divverent/nexuiz.git] / data / qcsrc / server / mode_onslaught.qc
1
2 .string target2;
3 .float iscaptured;
4 .float islinked;
5 .float isshielded;
6 void() onslaught_updatelinks =
7 {
8         local entity l, links;
9         local float stop, t1, t2, t3, t4;
10         // first check if the game has ended
11         dprint("--- updatelinks ---\n");
12         links = findchain(classname, "onslaught_link");
13         // mark generators as being shielded and networked
14         l = findchain(classname, "onslaught_generator");
15         while (l)
16         {
17                 if (l.iscaptured)
18                         dprint(etos(l), " (generator) belongs to team ", ftos(l.team), "\n");
19                 else
20                         dprint(etos(l), " (generator) is destroyed\n");
21                 l.islinked = l.iscaptured;
22                 l.isshielded = l.iscaptured;
23                 l = l.chain;
24         }
25         // mark points as shielded and not networked
26         l = findchain(classname, "onslaught_controlpoint");
27         while (l)
28         {
29                 l.islinked = FALSE;
30                 l.isshielded = TRUE;
31                 dprint(etos(l), " (point) belongs to team ", ftos(l.team), "\n");
32                 l = l.chain;
33         }
34         // flow power outward from the generators through the network
35         l = links;
36         while (l)
37         {
38                 dprint(etos(l), " (link) connects ", etos(l.goalentity), " with ", etos(l.enemy), "\n");
39                 l = l.chain;
40         }
41         stop = FALSE;
42         while (!stop)
43         {
44                 stop = TRUE;
45                 l = links;
46                 while (l)
47                 {
48                         // if both points are captured by the same team, and only one of
49                         // them is powered, mark the other one as powered as well
50                         if (l.enemy.iscaptured && l.goalentity.iscaptured)
51                         if (l.enemy.islinked != l.goalentity.islinked)
52                         if (l.enemy.team == l.goalentity.team)
53                         {
54                                 if (!l.goalentity.islinked)
55                                 {
56                                         stop = FALSE;
57                                         l.goalentity.islinked = TRUE;
58                                         dprint(etos(l), " (link) is marking ", etos(l.goalentity), " (point) because its team matches ", etos(l.enemy), " (point)\n");
59                                 }
60                                 else if (!l.enemy.islinked)
61                                 {
62                                         stop = FALSE;
63                                         l.enemy.islinked = TRUE;
64                                         dprint(etos(l), " (link) is marking ", etos(l.enemy), " (point) because its team matches ", etos(l.goalentity), " (point)\n");
65                                 }
66                         }
67                         l = l.chain;
68                 }
69         }
70         // now that we know which points are powered we can mark their neighbors
71         // as unshielded if team differs
72         l = links;
73         while (l)
74         {
75                 if (l.goalentity.team != l.enemy.team)
76                 {
77                         if (l.goalentity.islinked)
78                         {
79                                 dprint(etos(l), " (link) is unshielding ", etos(l.enemy), " (point) because its team does not match ", etos(l.goalentity), " (point)\n");
80                                 l.enemy.isshielded = FALSE;
81                         }
82                         if (l.enemy.islinked)
83                         {
84                                 dprint(etos(l), " (link) is unshielding ", etos(l.goalentity), " (point) because its team does not match ", etos(l.enemy), " (point)\n");
85                                 l.goalentity.isshielded = FALSE;
86                         }
87                 }
88                 l = l.chain;
89         }
90         // now update the takedamage and alpha variables on generator shields
91         l = findchain(classname, "onslaught_generator");
92         while (l)
93         {
94                 if (l.isshielded)
95                 {
96                         dprint(etos(l), " (generator) is shielded\n");
97                         l.enemy.alpha = 1;
98                         l.takedamage = DAMAGE_NO;
99                         l.bot_attack = FALSE;
100                 }
101                 else
102                 {
103                         dprint(etos(l), " (generator) is not shielded\n");
104                         l.enemy.alpha = -1;
105                         l.takedamage = DAMAGE_AIM;
106                         l.bot_attack = TRUE;
107                 }
108                 l = l.chain;
109         }
110         // now update the takedamage and alpha variables on control point icons
111         l = findchain(classname, "onslaught_controlpoint");
112         while (l)
113         {
114                 if (l.isshielded)
115                 {
116                         dprint(etos(l), " (point) is shielded\n");
117                         l.enemy.alpha = 1;
118                         if (l.goalentity)
119                         {
120                                 l.goalentity.takedamage = DAMAGE_NO;
121                                 l.goalentity.bot_attack = FALSE;
122                         }
123                 }
124                 else
125                 {
126                         dprint(etos(l), " (point) is not shielded\n");
127                         l.enemy.alpha = -1;
128                         if (l.goalentity)
129                         {
130                                 l.goalentity.takedamage = DAMAGE_AIM;
131                                 l.goalentity.bot_attack = TRUE;
132                         }
133                 }
134                 l = l.chain;
135         }
136         // count generators owned by each team
137         t1 = t2 = t3 = t4 = 0;
138         l = findchain(classname, "onslaught_generator");
139         while (l)
140         {
141                 if (l.iscaptured)
142                 {
143                         if (l.team == COLOR_TEAM1) t1 = 1;
144                         if (l.team == COLOR_TEAM2) t2 = 1;
145                         if (l.team == COLOR_TEAM3) t3 = 1;
146                         if (l.team == COLOR_TEAM4) t4 = 1;
147                 }
148                 l = l.chain;
149         }
150         // see if multiple teams remain (if not, it's game over)
151         if (t1 + t2 + t3 + t4 < 2)
152                 dprint("--- game over ---\n");
153         else
154                 dprint("--- done updating links ---\n");
155 };
156
157 void() onslaught_generator_think =
158 {
159         local float d;
160         local entity e;
161         self.nextthink = ceil(time + 1);
162         if (cvar("timelimit"))
163         if (time > cvar("timelimit") * 60 - 60)
164         {
165                 // self.max_health / 300 gives 5 minutes of overtime.
166                 // control points reduce the overtime duration.
167                 sound(self, CHAN_AUTO, "sound/onslaught/generator_decay.wav", 1, ATTN_NORM);
168                 d = 1;
169                 e = findchain(classname, "onslaught_controlpoint");
170                 while (e)
171                 {
172                         if (e.team != self.team)
173                         if (e.islinked)
174                                 d = d + 1;
175                         e = e.chain;
176                 }
177                 d = d * self.max_health / 300;
178                 Damage(self, self, self, d, DEATH_HURTTRIGGER, self.origin, '0 0 0');
179         }
180 };
181
182 void() onslaught_generator_deaththink =
183 {
184         local vector org;
185         if (self.count > 0)
186         {
187                 self.nextthink = time + 0.1;
188                 self.count = self.count - 1;
189                 org = randompos(self.origin + self.mins + '8 8 8', self.origin + self.maxs + '-8 -8 -8');
190                 pointparticles(particleeffectnum("onslaught_generator_smallexplosion"), org, '0 0 0', 1);
191                 sound(self, CHAN_AUTO, "sound/weapons/grenade_impact.wav", 1, ATTN_NORM);
192         }
193         else
194         {
195                 org = self.origin;
196                 pointparticles(particleeffectnum("onslaught_generator_finalexplosion"), org, '0 0 0', 1);
197                 sound(self, CHAN_AUTO, "sound/weapons/rocket_impact.wav", 1, ATTN_NORM);
198         }
199 };
200
201 void(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force) onslaught_generator_damage =
202 {
203         if (damage <= 0)
204                 return;
205         if (attacker != self)
206         {
207                 if (self.isshielded)
208                 {
209                         // this is protected by a shield, so ignore the damage
210                         if (time > self.pain_finished)
211                         if (attacker.classname == "player")
212                         {
213                                 play2(attacker, "sound/onslaught/damageblockedbyshield.wav");
214                                 self.pain_finished = time + 1;
215                         }
216                         return;
217                 }
218                 if (time > self.pain_finished)
219                 {
220                         self.pain_finished = time + 5;
221                         bprint(ColoredTeamName(self.team), " generator under attack!\n");
222                         play2team(self.team, "sound/onslaught/generator_underattack.wav");
223                 }
224         }
225         self.health = self.health - damage;
226         // choose an animation frame based on health
227         self.frame = 10 * bound(0, (1 - self.health / self.max_health), 1);
228         // see if the generator is still functional, or dying
229         if (self.health > 0)
230                 bprint(ColoredTeamName(self.team), " generator has ", ftos(floor(self.health)), " health remaining\n");
231         else
232         {
233                 if (attacker == self)
234                         bprint(ColoredTeamName(self.team), " generator spontaneously exploded due to overtime!\n");
235                 else
236                         bprint(ColoredTeamName(self.team), " generator destroyed by ", ColoredTeamName(attacker.team), "!\n");
237                 self.iscaptured = FALSE;
238                 self.islinked = FALSE;
239                 self.isshielded = FALSE;
240                 self.takedamage = DAMAGE_NO; // can't be hurt anymore
241                 self.event_damage = SUB_Null; // won't do anything if hurt
242                 self.count = 30; // 30 explosions
243                 self.think = onslaught_generator_deaththink; // explosion sequence
244                 self.nextthink = time; // start exploding immediately
245                 self.think(); // do the first explosion now
246                 onslaught_updatelinks();
247         }
248 };
249
250 // update links after a delay
251 void() onslaught_generator_delayed =
252 {
253         onslaught_updatelinks();
254         // now begin normal thinking
255         self.think = onslaught_generator_think;
256         self.nextthink = time;
257 };
258
259 /*QUAKED onslaught_generator (0 .5 .8) (-32 -32 -24) (32 32 64)
260 Base generator.
261
262 onslaught_link entities can target this.
263
264 keys:
265 "team" - team that owns this generator (5 = red, 14 = blue, etc), MUST BE SET.
266 "targetname" - name that onslaught_link entities will use to target this.
267 */
268 void() onslaught_generator =
269 {
270         if (!g_onslaught)
271         {
272                 remove(self);
273                 return;
274         }
275         local entity e;
276         precache_model("models/onslaught/generator.md3");
277         precache_model("models/onslaught/generator_shield.md3");
278         precache_sound("sound/onslaught/generator_decay.wav");
279         precache_sound("sound/weapons/grenade_impact.wav");
280         precache_sound("sound/weapons/rocket_impact.wav");
281         precache_sound("sound/onslaught/generator_underattack.wav");
282         if (!self.team)
283                 objerror("team must be set");
284         self.colormap = 1024 + (self.team - 1) * 17;
285         self.solid = SOLID_BSP;
286         self.movetype = MOVETYPE_NONE;
287         self.max_health = self.health = 3000;
288         setmodel(self, "models/onslaught/generator.md3");
289         //setsize(self, '-32 -32 -24', '32 32 64');
290         setorigin(self, self.origin);
291         self.takedamage = DAMAGE_AIM;
292         self.bot_attack = TRUE;
293         self.event_damage = onslaught_generator_damage;
294         self.iscaptured = TRUE;
295         self.islinked = TRUE;
296         self.isshielded = TRUE;
297         // spawn shield model which indicates whether this can be damaged
298         self.enemy = e = spawn();
299         e.solid = SOLID_NOT;
300         e.movetype = MOVETYPE_NONE;
301         e.effects = EF_ADDITIVE;
302         setmodel(e, "models/onslaught/generator_shield.md3");
303         //setsize(e, '-32 -32 0', '32 32 128');
304         setorigin(e, self.origin);
305         e.colormap = self.colormap;
306         e.team = self.team;
307         self.think = onslaught_generator_delayed;
308         self.nextthink = time + 0.2;
309 };
310
311 void(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force) onslaught_controlpoint_icon_damage =
312 {
313         entity oself;
314         if (damage <= 0)
315                 return;
316         if (self.owner.isshielded)
317         {
318                 // this is protected by a shield, so ignore the damage
319                 if (time > self.pain_finished)
320                 if (attacker.classname == "player")
321                 {
322                         play2(attacker, "sound/onslaught/damageblockedbyshield.wav");
323                         self.pain_finished = time + 1;
324                 }
325                 return;
326         }
327         if (time > self.pain_finished)
328         if (attacker.classname == "player")
329         {
330                 play2team(self.team, "sound/onslaught/controlpoint_underattack.wav");
331                 self.pain_finished = time + 5;
332         }
333         self.health = self.health - damage;
334         self.alpha = self.health / self.max_health;
335         self.pain_finished = time + 1;
336         // colormod flash when shot
337         self.colormod = '2 2 2';
338         if (self.health < 0)
339         {
340                 sound(self, CHAN_AUTO, "sound/weapons/grenade_impact.wav", 1, ATTN_NORM);
341                 pointparticles(particleeffectnum("onslaught_controlpoint_explosion"), self.origin, '0 0 0', 1);
342                 bprint(ColoredTeamName(self.team), " ", self.message, " control point destroyed by ", ColoredTeamName(attacker.team), "\n");
343                 self.owner.goalentity = world;
344                 self.owner.islinked = FALSE;
345                 self.owner.iscaptured = FALSE;
346                 self.owner.team = 0;
347                 self.owner.colormap = 1024;
348                 onslaught_updatelinks();
349
350                 // Use targets now (somebody make sure this is in the right place..)
351                 oself = self;
352                 self = self.owner;
353                 activator = self.owner;
354                 SUB_UseTargets ();
355                 self = oself;
356
357                 remove(self);
358         }
359 };
360
361 void() onslaught_controlpoint_icon_think =
362 {
363         self.nextthink = time + 0.1;
364         if (time > self.pain_finished + 1)
365         {
366                 self.health = self.health + self.count;
367                 if (self.health >= self.max_health)
368                         self.health = self.max_health;
369         }
370         self.alpha = self.health / self.max_health;
371         // colormod flash when shot
372         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
373 };
374
375 void() onslaught_controlpoint_icon_buildthink =
376 {
377         local entity oself;
378
379         self.nextthink = time + 0.1;
380         self.health = self.health + self.count;
381         if (self.health >= self.max_health)
382         {
383                 self.health = self.max_health;
384                 self.count = self.count * 0.2; // slow repair rate from now on
385                 self.think = onslaught_controlpoint_icon_think;
386                 sound(self, CHAN_BODY, "sound/onslaught/controlpoint_built.wav", 1, ATTN_NORM);
387                 bprint(ColoredTeamName(self.team), " captured ", self.owner.message, " control point\n");
388                 self.owner.iscaptured = TRUE;
389                 onslaught_updatelinks();
390
391                 // Use targets now (somebody make sure this is in the right place..)
392                 oself = self;
393                 self = self.owner;
394                 activator = self;
395                 SUB_UseTargets ();
396                 self = oself;
397         }
398         self.alpha = self.health / self.max_health;
399         // colormod flash when shot
400         self.colormod = '1 1 1' * (2 - bound(0, (self.pain_finished - time) / 10, 1));
401 };
402
403 void() onslaught_controlpoint_touch =
404 {
405         local entity e;
406         if (other.classname != "player")
407                 return;
408         // if there's already an icon built, nothing happens
409         if (self.goalentity)
410         {
411                 dprint("a\n");
412                 return;
413         }
414         // shielded points are definitely off-limits
415         if (self.isshielded)
416         {
417                 dprint("b\n");
418                 return;
419         }
420         // check to see if this player has a legitimate claim to capture this
421         // control point - more specifically that there is a captured path of
422         // points leading back to the team generator
423         e = findchain(classname, "onslaught_link");
424         while (e)
425         {
426                 if (e.goalentity == self)
427                 {
428                         dprint(etos(e), " (link) connects to ", etos(e.enemy), " (point)");
429                         if (e.enemy.islinked)
430                         {
431                                 dprint(" which is linked");
432                                 if (e.enemy.team == other.team)
433                                 {
434                                         dprint(" and has the correct team!\n");
435                                         break;
436                                 }
437                                 else
438                                         dprint(" but has the wrong team\n");
439                         }
440                         else
441                                 dprint("\n");
442                 }
443                 else if (e.enemy == self)
444                 {
445                         dprint(etos(e), " (link) connects to ", etos(e.goalentity), " (point)");
446                         if (e.goalentity.islinked)
447                         {
448                                 dprint(" which is linked");
449                                 if (e.goalentity.team == other.team)
450                                 {
451                                         dprint(" and has a team!\n");
452                                         break;
453                                 }
454                                 else
455                                         dprint(" but has the wrong team\n");
456                         }
457                         else
458                                 dprint("\n");
459                 }
460                 e = e.chain;
461         }
462         if (!e)
463         {
464                 dprint("c\n");
465                 return;
466         }
467         // we've verified that this player has a legitimate claim to this point,
468         // so start building the captured point icon (which only captures this
469         // point if it successfully builds without being destroyed first)
470         self.goalentity = e = spawn();
471         e.owner = self;
472         e.max_health = 1000;
473         e.health = e.max_health * 0.1;
474         e.alpha = e.health / e.max_health;
475         e.solid = SOLID_BBOX;
476         e.movetype = MOVETYPE_NONE;
477         setmodel(e, "models/onslaught/controlpoint_icon.md3");
478         setsize(e, '-32 -32 -32', '32 32 32');
479         setorigin(e, self.origin + '0 0 96');
480         e.takedamage = DAMAGE_AIM;
481         e.bot_attack = TRUE;
482         e.event_damage = onslaught_controlpoint_icon_damage;
483         e.team = other.team;
484         e.colormap = 1024 + (e.team - 1) * 17;
485         e.think = onslaught_controlpoint_icon_buildthink;
486         e.nextthink = time + 0.1;
487         e.count = e.max_health / 50; // how long it takes to build
488         sound(e, CHAN_BODY, "sound/onslaught/controlpoint_build.wav", 1, ATTN_NORM);
489         self.team = e.team;
490         self.colormap = e.colormap;
491 };
492
493 /*QUAKED onslaught_controlpoint (0 .5 .8) (-32 -32 0) (32 32 128)
494 Control point.  Be sure to give this enough clearance so that the shootable part has room to exist
495
496 This should link to an onslaught_controlpoint entity or onslaught_generator entity.
497
498 keys:
499 "targetname" - name that onslaught_link entities will use to target this.
500 "target" - target any entities that are tied to this control point, such as vehicles and buildable structure entities.
501 "message" - name of this control point (should reflect the location in the map, such as "center bridge", "north tower", etc)
502 */
503 void() onslaught_controlpoint =
504 {
505         local entity e;
506         if (!g_onslaught)
507         {
508                 remove(self);
509                 return;
510         }
511         precache_model("models/onslaught/controlpoint_pad.md3");
512         precache_model("models/onslaught/controlpoint_shield.md3");
513         precache_model("models/onslaught/controlpoint_icon.md3");
514         precache_sound("sound/onslaught/controlpoint_build.wav");
515         precache_sound("sound/onslaught/controlpoint_built.wav");
516         precache_sound("sound/weapons/grenade_impact.wav");
517         precache_sound("sound/onslaught/damageblockedbyshield.wav");
518         precache_sound("sound/onslaught/controlpoint_underattack.wav");
519         self.solid = SOLID_BSP;
520         self.movetype = MOVETYPE_NONE;
521         setmodel(self, "models/onslaught/controlpoint_pad.md3");
522         //setsize(self, '-32 -32 0', '32 32 8');
523         setorigin(self, self.origin);
524         self.touch = onslaught_controlpoint_touch;
525         self.team = 0;
526         self.colormap = 1024;
527         self.iscaptured = FALSE;
528         self.islinked = FALSE;
529         self.isshielded = TRUE;
530         // spawn shield model which indicates whether this can be damaged
531         self.enemy = e = spawn();
532         e.solid = SOLID_NOT;
533         e.movetype = MOVETYPE_NONE;
534         e.effects = EF_ADDITIVE;
535         setmodel(e, "models/onslaught/controlpoint_shield.md3");
536         //setsize(e, '-32 -32 0', '32 32 128');
537         setorigin(e, self.origin);
538         e.colormap = self.colormap;
539         onslaught_updatelinks();
540 };
541
542 void() onslaught_link_delayed =
543 {
544         self.goalentity = find(world, targetname, self.target);
545         self.enemy = find(world, targetname, self.target2);
546         if (!self.goalentity)
547                 objerror("can not find target\n");
548         if (!self.enemy)
549                 objerror("can not find target2\n");
550         dprint(etos(self.goalentity), " linked with ", etos(self.enemy), "\n");
551 }
552
553 /*QUAKED onslaught_link (0 .5 .8) (-16 -16 -16) (16 16 16)
554 Link between control points.
555
556 This entity targets two different onslaught_controlpoint or onslaught_generator entities, and suppresses shielding on both if they are owned by different teams.
557
558 keys:
559 "target" - first control point.
560 "target2" - second control point.
561 */
562 void() onslaught_link =
563 {
564         if (!g_onslaught)
565         {
566                 remove(self);
567                 return;
568         }
569         if (self.target == "" || self.target2 == "")
570                 objerror("target and target2 must be set\n");
571         self.think = onslaught_link_delayed;
572         self.nextthink = time + 0.1;
573 };