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