]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qc
Replace uses of the .weapons field with STAT(WEAPONS, player), doesn't replace the...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / onslaught / sv_onslaught.qc
1 #include "sv_onslaught.qh"
2 #include "sv_controlpoint.qh"
3 #include "sv_generator.qh"
4
5 bool g_onslaught;
6
7 float autocvar_g_onslaught_teleport_wait;
8 bool autocvar_g_onslaught_spawn_at_controlpoints;
9 bool autocvar_g_onslaught_spawn_at_generator;
10 float autocvar_g_onslaught_cp_proxydecap;
11 float autocvar_g_onslaught_cp_proxydecap_distance = 512;
12 float autocvar_g_onslaught_cp_proxydecap_dps = 100;
13 float autocvar_g_onslaught_spawn_at_controlpoints_chance = 0.5;
14 float autocvar_g_onslaught_spawn_at_controlpoints_random;
15 float autocvar_g_onslaught_spawn_at_generator_chance;
16 float autocvar_g_onslaught_spawn_at_generator_random;
17 float autocvar_g_onslaught_cp_buildhealth;
18 float autocvar_g_onslaught_cp_buildtime;
19 float autocvar_g_onslaught_cp_health;
20 float autocvar_g_onslaught_cp_regen;
21 float autocvar_g_onslaught_gen_health;
22 float autocvar_g_onslaught_shield_force = 100;
23 float autocvar_g_onslaught_allow_vehicle_touch;
24 float autocvar_g_onslaught_round_timelimit;
25 float autocvar_g_onslaught_warmup;
26 float autocvar_g_onslaught_teleport_radius;
27 float autocvar_g_onslaught_spawn_choose;
28 float autocvar_g_onslaught_click_radius;
29
30 void FixSize(entity e);
31 entity cam;
32
33 // =======================
34 // CaptureShield Functions
35 // =======================
36
37 bool clientcamera_send(entity this, entity to, int sf)
38 {
39         WriteHeader(MSG_ENTITY, ENT_ONSCAMERA);
40
41         WriteVector(MSG_ENTITY, this.origin);
42
43         WriteAngle(MSG_ENTITY, this.angles_x);
44         WriteAngle(MSG_ENTITY, this.angles_y);
45         WriteAngle(MSG_ENTITY, this.angles_z);
46
47         return true;
48 }
49
50 bool ons_CaptureShield_Customize(entity this, entity client)
51 {
52         entity e = WaypointSprite_getviewentity(client);
53
54         if(!this.enemy.isshielded && (ons_ControlPoint_Attackable(this.enemy, e.team) > 0 || this.enemy.classname != "onslaught_controlpoint")) { return false; }
55         if(SAME_TEAM(this, e)) { return false; }
56
57         return true;
58 }
59
60 void ons_CaptureShield_Touch(entity this, entity toucher)
61 {
62         if(!this.enemy.isshielded && (ons_ControlPoint_Attackable(this.enemy, toucher.team) > 0 || this.enemy.classname != "onslaught_controlpoint")) { return; }
63         if(!IS_PLAYER(toucher)) { return; }
64         if(SAME_TEAM(toucher, this)) { return; }
65
66         vector mymid = (this.absmin + this.absmax) * 0.5;
67         vector theirmid = (toucher.absmin + toucher.absmax) * 0.5;
68
69         Damage(toucher, this, this, 0, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, mymid, normalize(theirmid - mymid) * ons_captureshield_force);
70
71         if(IS_REAL_CLIENT(toucher))
72         {
73                 play2(toucher, SND(ONS_DAMAGEBLOCKEDBYSHIELD));
74
75                 if(this.enemy.classname == "onslaught_generator")
76                         Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_ONS_GENERATOR_SHIELDED);
77                 else
78                         Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_ONS_CONTROLPOINT_SHIELDED);
79         }
80 }
81
82 void ons_CaptureShield_Reset(entity this)
83 {
84         this.colormap = this.enemy.colormap;
85         this.team = this.enemy.team;
86 }
87
88 void ons_CaptureShield_Spawn(entity generator, bool is_generator)
89 {
90         entity shield = new(ons_captureshield);
91         IL_PUSH(g_onsshields, shield);
92
93         shield.enemy = generator;
94         shield.team = generator.team;
95         shield.colormap = generator.colormap;
96         shield.reset = ons_CaptureShield_Reset;
97         settouch(shield, ons_CaptureShield_Touch);
98         setcefc(shield, ons_CaptureShield_Customize);
99         shield.effects = EF_ADDITIVE;
100         set_movetype(shield, MOVETYPE_NOCLIP);
101         shield.solid = SOLID_TRIGGER;
102         shield.avelocity = '7 0 11';
103         shield.scale = 1;
104         shield.model = ((is_generator) ? "models/onslaught/generator_shield.md3" : "models/onslaught/controlpoint_shield.md3");
105
106         precache_model(shield.model);
107         setorigin(shield, generator.origin);
108         _setmodel(shield, shield.model);
109         setsize(shield, shield.scale * shield.mins, shield.scale * shield.maxs);
110 }
111
112
113 // ==========
114 // Junk Pile
115 // ==========
116
117 void setmodel_fixsize(entity e, Model m)
118 {
119         setmodel(e, m);
120         FixSize(e);
121 }
122
123 void onslaught_updatelinks()
124 {
125         entity l;
126         // first check if the game has ended
127         LOG_DEBUG("--- updatelinks ---");
128         // mark generators as being shielded and networked
129         for(l = ons_worldgeneratorlist; l; l = l.ons_worldgeneratornext)
130         {
131                 if (l.iscaptured)
132                         LOG_DEBUG(etos(l), " (generator) belongs to team ", ftos(l.team));
133                 else
134                         LOG_DEBUG(etos(l), " (generator) is destroyed");
135                 l.islinked = l.iscaptured;
136                 l.isshielded = l.iscaptured;
137                 l.sprite.SendFlags |= 16;
138         }
139         // mark points as shielded and not networked
140         for(l = ons_worldcplist; l; l = l.ons_worldcpnext)
141         {
142                 l.islinked = false;
143                 l.isshielded = true;
144                 l.aregensneighbor = 0;
145                 l.arecpsneighbor = 0;
146                 LOG_DEBUG(etos(l), " (point) belongs to team ", ftos(l.team));
147                 l.sprite.SendFlags |= 16;
148         }
149         // flow power outward from the generators through the network
150         bool stop = false;
151         while (!stop)
152         {
153                 stop = true;
154                 for(l = ons_worldlinklist; l; l = l.ons_worldlinknext)
155                 {
156                         // if both points are captured by the same team, and only one of
157                         // them is powered, mark the other one as powered as well
158                         if (l.enemy.iscaptured && l.goalentity.iscaptured)
159                                 if (l.enemy.islinked != l.goalentity.islinked)
160                                         if(SAME_TEAM(l.enemy, l.goalentity))
161                                         {
162                                                 if (!l.goalentity.islinked)
163                                                 {
164                                                         stop = false;
165                                                         l.goalentity.islinked = true;
166                                                         LOG_DEBUG(etos(l), " (link) is marking ", etos(l.goalentity), " (point) because its team matches ", etos(l.enemy), " (point)");
167                                                 }
168                                                 else if (!l.enemy.islinked)
169                                                 {
170                                                         stop = false;
171                                                         l.enemy.islinked = true;
172                                                         LOG_DEBUG(etos(l), " (link) is marking ", etos(l.enemy), " (point) because its team matches ", etos(l.goalentity), " (point)");
173                                                 }
174                                         }
175                 }
176         }
177         // now that we know which points are powered we can mark their neighbors
178         // as unshielded if team differs
179         for(l = ons_worldlinklist; l; l = l.ons_worldlinknext)
180         {
181                 if (l.goalentity.islinked)
182                 {
183                         if(DIFF_TEAM(l.goalentity, l.enemy))
184                         {
185                                 LOG_DEBUG(etos(l), " (link) is unshielding ", etos(l.enemy), " (point) because its team does not match ", etos(l.goalentity), " (point)");
186                                 l.enemy.isshielded = false;
187                         }
188                         if(l.goalentity.classname == "onslaught_generator")
189                                 l.enemy.aregensneighbor |= BIT(l.goalentity.team);
190                         else
191                                 l.enemy.arecpsneighbor |= BIT(l.goalentity.team);
192                 }
193                 if (l.enemy.islinked)
194                 {
195                         if(DIFF_TEAM(l.goalentity, l.enemy))
196                         {
197                                 LOG_DEBUG(etos(l), " (link) is unshielding ", etos(l.goalentity), " (point) because its team does not match ", etos(l.enemy), " (point)");
198                                 l.goalentity.isshielded = false;
199                         }
200                         if(l.enemy.classname == "onslaught_generator")
201                                 l.goalentity.aregensneighbor |= BIT(l.enemy.team);
202                         else
203                                 l.goalentity.arecpsneighbor |= BIT(l.enemy.team);
204                 }
205         }
206         // now update the generators
207         for(l = ons_worldgeneratorlist; l; l = l.ons_worldgeneratornext)
208         {
209                 if (l.isshielded)
210                 {
211                         LOG_DEBUG(etos(l), " (generator) is shielded");
212                         l.takedamage = DAMAGE_NO;
213                         if(l.bot_attack)
214                                 IL_REMOVE(g_bot_targets, l);
215                         l.bot_attack = false;
216                 }
217                 else
218                 {
219                         LOG_DEBUG(etos(l), " (generator) is not shielded");
220                         l.takedamage = DAMAGE_AIM;
221                         if(!l.bot_attack)
222                                 IL_PUSH(g_bot_targets, l);
223                         l.bot_attack = true;
224                 }
225
226                 ons_Generator_UpdateSprite(l);
227         }
228         // now update the takedamage and alpha variables on control point icons
229         for(l = ons_worldcplist; l; l = l.ons_worldcpnext)
230         {
231                 if (l.isshielded)
232                 {
233                         LOG_DEBUG(etos(l), " (point) is shielded");
234                         if (l.goalentity)
235                         {
236                                 l.goalentity.takedamage = DAMAGE_NO;
237                                 if(l.goalentity.bot_attack)
238                                         IL_REMOVE(g_bot_targets, l.goalentity);
239                                 l.goalentity.bot_attack = false;
240                         }
241                 }
242                 else
243                 {
244                         LOG_DEBUG(etos(l), " (point) is not shielded");
245                         if (l.goalentity)
246                         {
247                                 l.goalentity.takedamage = DAMAGE_AIM;
248                                 if(!l.goalentity.bot_attack)
249                                         IL_PUSH(g_bot_targets, l.goalentity);
250                                 l.goalentity.bot_attack = true;
251                         }
252                 }
253                 ons_ControlPoint_UpdateSprite(l);
254         }
255         IL_EACH(g_onsshields, true,
256         {
257                 it.team = it.enemy.team;
258                 it.colormap = it.enemy.colormap;
259         });
260 }
261
262
263 // ===================
264 // Main Link Functions
265 // ===================
266
267 bool ons_Link_Send(entity this, entity to, int sendflags)
268 {
269         WriteHeader(MSG_ENTITY, ENT_CLIENT_RADARLINK);
270         WriteByte(MSG_ENTITY, sendflags);
271         if(sendflags & 1)
272         {
273                 WriteVector(MSG_ENTITY, this.goalentity.origin);
274         }
275         if(sendflags & 2)
276         {
277                 WriteVector(MSG_ENTITY, this.enemy.origin);
278         }
279         if(sendflags & 4)
280         {
281                 WriteByte(MSG_ENTITY, this.clientcolors); // which is goalentity's color + enemy's color * 16
282         }
283         return true;
284 }
285
286 void ons_Link_CheckUpdate(entity this)
287 {
288         // TODO check if the two sides have moved (currently they won't move anyway)
289         float cc = 0, cc1 = 0, cc2 = 0;
290
291         if(this.goalentity.islinked || this.goalentity.iscaptured) { cc1 = (this.goalentity.team - 1) * 0x01; }
292         if(this.enemy.islinked || this.enemy.iscaptured) { cc2 = (this.enemy.team - 1) * 0x10; }
293
294         cc = cc1 + cc2;
295
296         if(cc != this.clientcolors)
297         {
298                 this.clientcolors = cc;
299                 this.SendFlags |= 4;
300         }
301
302         this.nextthink = time;
303 }
304
305 void ons_DelayedLinkSetup(entity this)
306 {
307         this.goalentity = find(NULL, targetname, this.target);
308         this.enemy = find(NULL, targetname, this.target2);
309         if(!this.goalentity) { objerror(this, "can not find target\n"); }
310         if(!this.enemy) { objerror(this, "can not find target2\n"); }
311
312         LOG_DEBUG(etos(this.goalentity), " linked with ", etos(this.enemy));
313         this.SendFlags |= 3;
314         setthink(this, ons_Link_CheckUpdate);
315         this.nextthink = time;
316 }
317
318
319 // =============================
320 // Main Control Point Functions
321 // =============================
322
323 int ons_ControlPoint_CanBeLinked(entity cp, int teamnumber)
324 {
325         if(cp.aregensneighbor & BIT(teamnumber)) return 2;
326         if(cp.arecpsneighbor & BIT(teamnumber)) return 1;
327
328         return 0;
329 }
330
331 int ons_ControlPoint_Attackable(entity cp, int teamnumber)
332         // -2: SAME TEAM, attackable by enemy!
333         // -1: SAME TEAM!
334         // 0: off limits
335         // 1: attack it
336         // 2: touch it
337         // 3: attack it (HIGH PRIO)
338         // 4: touch it (HIGH PRIO)
339 {
340         int a;
341
342         if(cp.isshielded)
343         {
344                 return 0;
345         }
346         else if(cp.goalentity)
347         {
348                 // if there's already an icon built, nothing happens
349                 if(cp.team == teamnumber)
350                 {
351                         a = ons_ControlPoint_CanBeLinked(cp, teamnumber);
352                         if(a) // attackable by enemy?
353                                 return -2; // EMERGENCY!
354                         return -1;
355                 }
356                 // we know it can be linked, so no need to check
357                 // but...
358                 a = ons_ControlPoint_CanBeLinked(cp, teamnumber);
359                 if(a == 2) // near our generator?
360                         return 3; // EMERGENCY!
361                 return 1;
362         }
363         else
364         {
365                 // free point
366                 if(ons_ControlPoint_CanBeLinked(cp, teamnumber))
367                 {
368                         a = ons_ControlPoint_CanBeLinked(cp, teamnumber); // why was this here NUM_TEAM_1 + NUM_TEAM_2 - t
369                         if(a == 2)
370                                 return 4; // GET THIS ONE NOW!
371                         else
372                                 return 2; // TOUCH ME
373                 }
374         }
375         return 0;
376 }
377
378 void ons_ControlPoint_Icon_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
379 {
380         if(damage <= 0) { return; }
381
382         if (this.owner.isshielded)
383         {
384                 // this is protected by a shield, so ignore the damage
385                 if (time > this.pain_finished)
386                         if (IS_PLAYER(attacker))
387                         {
388                                 play2(attacker, SND(ONS_DAMAGEBLOCKEDBYSHIELD));
389                                 this.pain_finished = time + 1;
390                                 attacker.typehitsound += 1; // play both sounds (shield is way too quiet)
391                         }
392
393                 return;
394         }
395
396         if(IS_PLAYER(attacker))
397         if(time - ons_notification_time[this.team] > 10)
398         {
399                 play2team(this.team, SND(ONS_CONTROLPOINT_UNDERATTACK));
400                 ons_notification_time[this.team] = time;
401         }
402
403         this.health = this.health - damage;
404         if(this.owner.iscaptured)
405                 WaypointSprite_UpdateHealth(this.owner.sprite, this.health);
406         else
407                 WaypointSprite_UpdateBuildFinished(this.owner.sprite, time + (this.max_health - this.health) / (this.count / ONS_CP_THINKRATE));
408         this.pain_finished = time + 1;
409         // particles on every hit
410         pointparticles(EFFECT_SPARKS, hitloc, force*-1, 1);
411         //sound on every hit
412         if (random() < 0.5)
413                 sound(this, CH_TRIGGER, SND_ONS_HIT1, VOL_BASE+0.3, ATTEN_NORM);
414         else
415                 sound(this, CH_TRIGGER, SND_ONS_HIT2, VOL_BASE+0.3, ATTEN_NORM);
416
417         if (this.health < 0)
418         {
419                 sound(this, CH_TRIGGER, SND_GRENADE_IMPACT, VOL_BASE, ATTEN_NORM);
420                 pointparticles(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
421                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(this.team, INFO_ONSLAUGHT_CPDESTROYED), this.owner.message, attacker.netname);
422
423                 GameRules_scoring_add(attacker, ONS_TAKES, 1);
424                 GameRules_scoring_add(attacker, SCORE, 10);
425
426                 this.owner.goalentity = NULL;
427                 this.owner.islinked = false;
428                 this.owner.iscaptured = false;
429                 this.owner.team = 0;
430                 this.owner.colormap = 1024;
431
432                 WaypointSprite_UpdateMaxHealth(this.owner.sprite, 0);
433
434                 onslaught_updatelinks();
435
436                 // Use targets now (somebody make sure this is in the right place..)
437                 SUB_UseTargets(this.owner, this, NULL);
438
439                 this.owner.waslinked = this.owner.islinked;
440                 if(this.owner.model != "models/onslaught/controlpoint_pad.md3")
441                         setmodel_fixsize(this.owner, MDL_ONS_CP_PAD1);
442                 //setsize(this, '-32 -32 0', '32 32 8');
443
444                 delete(this);
445         }
446
447         this.SendFlags |= CPSF_STATUS;
448 }
449
450 void ons_ControlPoint_Icon_Think(entity this)
451 {
452         this.nextthink = time + ONS_CP_THINKRATE;
453
454         if(autocvar_g_onslaught_cp_proxydecap)
455         {
456                 int _enemy_count = 0;
457                 int _friendly_count = 0;
458
459                 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it), {
460                         if(vdist(it.origin - this.origin, <, autocvar_g_onslaught_cp_proxydecap_distance))
461                         {
462                                 if(SAME_TEAM(it, this))
463                                         ++_friendly_count;
464                                 else
465                                         ++_enemy_count;
466                         }
467                 });
468
469                 _friendly_count = _friendly_count * (autocvar_g_onslaught_cp_proxydecap_dps * ONS_CP_THINKRATE);
470                 _enemy_count = _enemy_count * (autocvar_g_onslaught_cp_proxydecap_dps * ONS_CP_THINKRATE);
471
472                 this.health = bound(0, this.health + (_friendly_count - _enemy_count), this.max_health);
473                 this.SendFlags |= CPSF_STATUS;
474                 if(this.health <= 0)
475                 {
476                         ons_ControlPoint_Icon_Damage(this, this, this, 1, 0, DMG_NOWEP, this.origin, '0 0 0');
477                         return;
478                 }
479         }
480
481         if (time > this.pain_finished + 5)
482         {
483                 if(this.health < this.max_health)
484                 {
485                         this.health = this.health + this.count;
486                         if (this.health >= this.max_health)
487                                 this.health = this.max_health;
488                         WaypointSprite_UpdateHealth(this.owner.sprite, this.health);
489                 }
490         }
491
492         if(this.owner.islinked != this.owner.waslinked)
493         {
494                 // unteam the spawnpoint if needed
495                 int t = this.owner.team;
496                 if(!this.owner.islinked)
497                         this.owner.team = 0;
498
499                 SUB_UseTargets(this.owner, this, NULL);
500
501                 this.owner.team = t;
502
503                 this.owner.waslinked = this.owner.islinked;
504         }
505
506         // damaged fx
507         if(random() < 0.6 - this.health / this.max_health)
508         {
509                 Send_Effect(EFFECT_ELECTRIC_SPARKS, this.origin + randompos('-10 -10 -20', '10 10 20'), '0 0 0', 1);
510
511                 if(random() > 0.8)
512                         sound(this, CH_PAIN, SND_ONS_SPARK1, VOL_BASE, ATTEN_NORM);
513                 else if (random() > 0.5)
514                         sound(this, CH_PAIN, SND_ONS_SPARK2, VOL_BASE, ATTEN_NORM);
515         }
516 }
517
518 void ons_ControlPoint_Icon_BuildThink(entity this)
519 {
520         int a;
521
522         this.nextthink = time + ONS_CP_THINKRATE;
523
524         // only do this if there is power
525         a = ons_ControlPoint_CanBeLinked(this.owner, this.owner.team);
526         if(!a)
527                 return;
528
529         this.health = this.health + this.count;
530
531         this.SendFlags |= CPSF_STATUS;
532
533         if (this.health >= this.max_health)
534         {
535                 this.health = this.max_health;
536                 this.count = autocvar_g_onslaught_cp_regen * ONS_CP_THINKRATE; // slow repair rate from now on
537                 setthink(this, ons_ControlPoint_Icon_Think);
538                 sound(this, CH_TRIGGER, SND_ONS_CONTROLPOINT_BUILT, VOL_BASE, ATTEN_NORM);
539                 this.owner.iscaptured = true;
540                 this.solid = SOLID_BBOX;
541
542                 Send_Effect(EFFECT_CAP(this.owner.team), this.owner.origin, '0 0 0', 1);
543
544                 WaypointSprite_UpdateMaxHealth(this.owner.sprite, this.max_health);
545                 WaypointSprite_UpdateHealth(this.owner.sprite, this.health);
546
547                 if(IS_PLAYER(this.owner.ons_toucher))
548                 {
549                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ONSLAUGHT_CAPTURE, this.owner.ons_toucher.netname, this.owner.message);
550                         Send_Notification(NOTIF_ALL_EXCEPT, this.owner.ons_toucher, MSG_CENTER, APP_TEAM_NUM(this.owner.ons_toucher.team, CENTER_ONS_CAPTURE_TEAM), this.owner.message);
551                         Send_Notification(NOTIF_ONE, this.owner.ons_toucher, MSG_CENTER, CENTER_ONS_CAPTURE, this.owner.message);
552                         GameRules_scoring_add(this.owner.ons_toucher, ONS_CAPS, 1);
553                         GameRules_scoring_add_team(this.owner.ons_toucher, SCORE, 10);
554                 }
555
556                 this.owner.ons_toucher = NULL;
557
558                 onslaught_updatelinks();
559
560                 // Use targets now (somebody make sure this is in the right place..)
561                 SUB_UseTargets(this.owner, this, NULL);
562
563                 this.SendFlags |= CPSF_SETUP;
564         }
565         if(this.owner.model != MDL_ONS_CP_PAD2.model_str())
566                 setmodel_fixsize(this.owner, MDL_ONS_CP_PAD2);
567
568         if(random() < 0.9 - this.health / this.max_health)
569                 Send_Effect(EFFECT_RAGE, this.origin + 10 * randomvec(), '0 0 -1', 1);
570 }
571
572 void onslaught_controlpoint_icon_link(entity e, void(entity this) spawnproc);
573
574 void ons_ControlPoint_Icon_Spawn(entity cp, entity player)
575 {
576         entity e = new(onslaught_controlpoint_icon);
577
578         setsize(e, CPICON_MIN, CPICON_MAX);
579         setorigin(e, cp.origin + CPICON_OFFSET);
580
581         e.owner = cp;
582         e.max_health = autocvar_g_onslaught_cp_health;
583         e.health = autocvar_g_onslaught_cp_buildhealth;
584         e.solid = SOLID_NOT;
585         e.takedamage = DAMAGE_AIM;
586         e.bot_attack = true;
587         IL_PUSH(g_bot_targets, e);
588         e.event_damage = ons_ControlPoint_Icon_Damage;
589         e.team = player.team;
590         e.colormap = 1024 + (e.team - 1) * 17;
591         e.count = (e.max_health - e.health) * ONS_CP_THINKRATE / autocvar_g_onslaught_cp_buildtime; // how long it takes to build
592
593         sound(e, CH_TRIGGER, SND_ONS_CONTROLPOINT_BUILD, VOL_BASE, ATTEN_NORM);
594
595         cp.goalentity = e;
596         cp.team = e.team;
597         cp.colormap = e.colormap;
598
599         Send_Effect(EFFECT_FLAG_TOUCH(player.team), e.origin, '0 0 0', 1);
600
601         WaypointSprite_UpdateBuildFinished(cp.sprite, time + (e.max_health - e.health) / (e.count / ONS_CP_THINKRATE));
602         WaypointSprite_UpdateRule(cp.sprite,cp.team,SPRITERULE_TEAMPLAY);
603         cp.sprite.SendFlags |= 16;
604
605         onslaught_controlpoint_icon_link(e, ons_ControlPoint_Icon_BuildThink);
606 }
607
608 entity ons_ControlPoint_Waypoint(entity e)
609 {
610         if(e.team)
611         {
612                 int a = ons_ControlPoint_Attackable(e, e.team);
613
614                 if(a == -2) { return WP_OnsCPDefend; } // defend now
615                 if(a == -1 || a == 1 || a == 2) { return WP_OnsCP; } // touch
616                 if(a == 3 || a == 4) { return WP_OnsCPAttack; } // attack
617         }
618         else
619                 return WP_OnsCP;
620
621         return WP_Null;
622 }
623
624 void ons_ControlPoint_UpdateSprite(entity e)
625 {
626         entity s1 = ons_ControlPoint_Waypoint(e);
627         WaypointSprite_UpdateSprites(e.sprite, s1, s1, s1);
628
629         bool sh;
630         sh = !(ons_ControlPoint_CanBeLinked(e, NUM_TEAM_1) || ons_ControlPoint_CanBeLinked(e, NUM_TEAM_2) || ons_ControlPoint_CanBeLinked(e, NUM_TEAM_3) || ons_ControlPoint_CanBeLinked(e, NUM_TEAM_4));
631
632         if(e.lastteam != e.team + 2 || e.lastshielded != sh || e.iscaptured != e.lastcaptured)
633         {
634                 if(e.iscaptured) // don't mess up build bars!
635                 {
636                         if(sh)
637                         {
638                                 WaypointSprite_UpdateMaxHealth(e.sprite, 0);
639                         }
640                         else
641                         {
642                                 WaypointSprite_UpdateMaxHealth(e.sprite, e.goalentity.max_health);
643                                 WaypointSprite_UpdateHealth(e.sprite, e.goalentity.health);
644                         }
645                 }
646                 if(e.lastshielded)
647                 {
648                         if(e.team)
649                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, 0.5 * colormapPaletteColor(e.team - 1, false));
650                         else
651                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.5 0.5 0.5');
652                 }
653                 else
654                 {
655                         if(e.team)
656                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, colormapPaletteColor(e.team - 1, false));
657                         else
658                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_CONTROLPOINT, '0.75 0.75 0.75');
659                 }
660                 WaypointSprite_Ping(e.sprite);
661
662                 e.lastteam = e.team + 2;
663                 e.lastshielded = sh;
664                 e.lastcaptured = e.iscaptured;
665         }
666 }
667
668 void ons_ControlPoint_Touch(entity this, entity toucher)
669 {
670         int attackable;
671
672         if(IS_VEHICLE(toucher) && toucher.owner)
673         if(autocvar_g_onslaught_allow_vehicle_touch)
674                 toucher = toucher.owner;
675         else
676                 return;
677
678         if(!IS_PLAYER(toucher)) { return; }
679         if(STAT(FROZEN, toucher)) { return; }
680         if(IS_DEAD(toucher)) { return; }
681
682         if ( SAME_TEAM(this,toucher) )
683         if ( this.iscaptured )
684         {
685                 if(time <= toucher.teleport_antispam)
686                         Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_ONS_TELEPORT_ANTISPAM, rint(toucher.teleport_antispam - time));
687                 else
688                         Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_ONS_TELEPORT);
689         }
690
691         attackable = ons_ControlPoint_Attackable(this, toucher.team);
692         if(attackable != 2 && attackable != 4)
693                 return;
694         // we've verified that this player has a legitimate claim to this point,
695         // so start building the captured point icon (which only captures this
696         // point if it successfully builds without being destroyed first)
697         ons_ControlPoint_Icon_Spawn(this, toucher);
698
699         this.ons_toucher = toucher;
700
701         onslaught_updatelinks();
702 }
703
704 void ons_ControlPoint_Think(entity this)
705 {
706         this.nextthink = time + ONS_CP_THINKRATE;
707         CSQCMODEL_AUTOUPDATE(this);
708 }
709
710 void ons_ControlPoint_Reset(entity this)
711 {
712         if(this.goalentity)
713                 delete(this.goalentity);
714
715         this.goalentity = NULL;
716         this.team = 0;
717         this.colormap = 1024;
718         this.iscaptured = false;
719         this.islinked = false;
720         this.isshielded = true;
721         setthink(this, ons_ControlPoint_Think);
722         this.ons_toucher = NULL;
723         this.nextthink = time + ONS_CP_THINKRATE;
724         setmodel_fixsize(this, MDL_ONS_CP_PAD1);
725
726         WaypointSprite_UpdateMaxHealth(this.sprite, 0);
727         WaypointSprite_UpdateRule(this.sprite,this.team,SPRITERULE_TEAMPLAY);
728
729         onslaught_updatelinks();
730
731         SUB_UseTargets(this, this, NULL); // to reset the structures, playerspawns etc.
732
733         CSQCMODEL_AUTOUPDATE(this);
734 }
735
736 void ons_DelayedControlPoint_Setup(entity this)
737 {
738         onslaught_updatelinks();
739
740         // captureshield setup
741         ons_CaptureShield_Spawn(this, false);
742
743         CSQCMODEL_AUTOINIT(this);
744 }
745
746 void ons_ControlPoint_Setup(entity cp)
747 {
748         // main setup
749         cp.ons_worldcpnext = ons_worldcplist; // link control point into ons_worldcplist
750         ons_worldcplist = cp;
751
752         cp.netname = "Control point";
753         cp.team = 0;
754         cp.solid = SOLID_BBOX;
755         set_movetype(cp, MOVETYPE_NONE);
756         settouch(cp, ons_ControlPoint_Touch);
757         setthink(cp, ons_ControlPoint_Think);
758         cp.nextthink = time + ONS_CP_THINKRATE;
759         cp.reset = ons_ControlPoint_Reset;
760         cp.colormap = 1024;
761         cp.iscaptured = false;
762         cp.islinked = false;
763         cp.isshielded = true;
764
765         if(cp.message == "") { cp.message = "a"; }
766
767         // appearence
768         setmodel_fixsize(cp, MDL_ONS_CP_PAD1);
769
770         // control point placement
771         if((cp.spawnflags & 1) || cp.noalign) // don't drop to floor, just stay at fixed location
772         {
773                 cp.noalign = true;
774                 set_movetype(cp, MOVETYPE_NONE);
775         }
776         else // drop to floor, automatically find a platform and set that as spawn origin
777         {
778                 setorigin(cp, cp.origin + '0 0 20');
779                 cp.noalign = false;
780                 droptofloor(cp);
781                 set_movetype(cp, MOVETYPE_TOSS);
782         }
783
784         // waypointsprites
785         WaypointSprite_SpawnFixed(WP_Null, cp.origin + CPGEN_WAYPOINT_OFFSET, cp, sprite, RADARICON_NONE);
786         WaypointSprite_UpdateRule(cp.sprite, cp.team, SPRITERULE_TEAMPLAY);
787
788         InitializeEntity(cp, ons_DelayedControlPoint_Setup, INITPRIO_SETLOCATION);
789 }
790
791
792 // =========================
793 // Main Generator Functions
794 // =========================
795
796 entity ons_Generator_Waypoint(entity e)
797 {
798         if (e.isshielded)
799                 return WP_OnsGenShielded;
800         return WP_OnsGen;
801 }
802
803 void ons_Generator_UpdateSprite(entity e)
804 {
805         entity s1 = ons_Generator_Waypoint(e);
806         WaypointSprite_UpdateSprites(e.sprite, s1, s1, s1);
807
808         if(e.lastteam != e.team + 2 || e.lastshielded != e.isshielded)
809         {
810                 e.lastteam = e.team + 2;
811                 e.lastshielded = e.isshielded;
812                 if(e.lastshielded)
813                 {
814                         if(e.team)
815                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, 0.5 * colormapPaletteColor(e.team - 1, false));
816                         else
817                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.5 0.5 0.5');
818                 }
819                 else
820                 {
821                         if(e.team)
822                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, colormapPaletteColor(e.team - 1, false));
823                         else
824                                 WaypointSprite_UpdateTeamRadar(e.sprite, RADARICON_GENERATOR, '0.75 0.75 0.75');
825                 }
826                 WaypointSprite_Ping(e.sprite);
827         }
828 }
829
830 void ons_camSetup(entity this)
831 {
832         vector dir;
833         vector ang = '0 0 0';
834         vector best_ang = '0 0 0';
835         float best_trace_fraction = 0;
836         while(ang.y < 360)
837         {
838                 dir = vec2(cos(ang.y * DEG2RAD), sin(ang.y * DEG2RAD));
839                 dir *= 500;
840                 traceline(this.origin, this.origin - dir, MOVE_WORLDONLY, this);
841                 if(trace_fraction > best_trace_fraction)
842                 {
843                         best_trace_fraction = trace_fraction;
844                         best_ang = ang;
845                         if(trace_fraction == 1)
846                                 break;
847                 }
848                 ang.y += 90;
849                 if(ang.y == 360)
850                         ang.y = 45;
851         }
852         cam.origin = this.origin;
853         setorigin(cam, cam.origin);
854         cam.angles = best_ang;
855         Net_LinkEntity(cam, false, 0, clientcamera_send);
856
857         FOREACH_CLIENT(true, it.clientcamera = cam;);
858
859         WriteByte(MSG_ALL, SVC_SETVIEWANGLES);
860         WriteAngle(MSG_ALL, cam.angles_x);
861         WriteAngle(MSG_ALL, cam.angles_y);
862         WriteAngle(MSG_ALL, cam.angles_z);
863 }
864
865 void ons_GeneratorDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
866 {
867         if(damage <= 0) return;
868         if(warmup_stage || game_stopped) return;
869         if(!round_handler_IsRoundStarted()) return;
870
871         if (attacker != this)
872         {
873                 if (this.isshielded)
874                 {
875                         // generator is protected by a shield, so ignore the damage
876                         if (time > this.pain_finished)
877                                 if (IS_PLAYER(attacker))
878                                 {
879                                         play2(attacker, SND(ONS_DAMAGEBLOCKEDBYSHIELD));
880                                         attacker.typehitsound += 1;
881                                         this.pain_finished = time + 1;
882                                 }
883                         return;
884                 }
885                 if (time > this.pain_finished)
886                 {
887                         this.pain_finished = time + 10;
888                         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && SAME_TEAM(it, this), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_GENERATOR_UNDERATTACK));
889                         play2team(this.team, SND(ONS_GENERATOR_UNDERATTACK));
890                 }
891         }
892         this.health = this.health - damage;
893         WaypointSprite_UpdateHealth(this.sprite, this.health);
894         // choose an animation frame based on health
895         this.frame = 10 * bound(0, (1 - this.health / this.max_health), 1);
896         // see if the generator is still functional, or dying
897         if (this.health > 0)
898         {
899                 this.lasthealth = this.health;
900         }
901         else
902         {
903                 if (attacker == this)
904                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(this.team, INFO_ONSLAUGHT_GENDESTROYED_OVERTIME));
905                 else
906                 {
907                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(this.team, INFO_ONSLAUGHT_GENDESTROYED));
908                         GameRules_scoring_add(attacker, SCORE, 100);
909                 }
910                 this.iscaptured = false;
911                 this.islinked = false;
912                 this.isshielded = false;
913                 this.takedamage = DAMAGE_NO; // can't be hurt anymore
914                 this.event_damage = func_null; // won't do anything if hurt
915                 this.count = 0; // reset counter
916                 setthink(this, func_null);
917                 this.nextthink = 0;
918                 //this.think(); // do the first explosion now
919
920                 WaypointSprite_UpdateMaxHealth(this.sprite, 0);
921                 WaypointSprite_Ping(this.sprite);
922                 //WaypointSprite_Kill(this.sprite); // can't do this yet, code too poor
923
924                 onslaught_updatelinks();
925
926                 ons_camSetup(this);
927         }
928
929         // Throw some flaming gibs on damage, more damage = more chance for gib
930         if(random() < damage/220)
931         {
932                 sound(this, CH_TRIGGER, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
933         }
934         else
935         {
936                 // particles on every hit
937                 Send_Effect(EFFECT_SPARKS, hitloc, force * -1, 1);
938
939                 //sound on every hit
940                 if (random() < 0.5)
941                         sound(this, CH_TRIGGER, SND_ONS_HIT1, VOL_BASE, ATTEN_NORM);
942                 else
943                         sound(this, CH_TRIGGER, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM);
944         }
945
946         this.SendFlags |= GSF_STATUS;
947 }
948
949 void ons_GeneratorThink(entity this)
950 {
951         this.nextthink = time + GEN_THINKRATE;
952         if (!game_stopped)
953         {
954                 if(!this.isshielded && this.wait < time)
955                 {
956                         this.wait = time + 5;
957                         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
958                                 if(SAME_TEAM(it, this))
959                                 {
960                                         Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_ONS_NOTSHIELDED_TEAM);
961                                         soundto(MSG_ONE, it, CHAN_AUTO, SND(KH_ALARM), VOL_BASE, ATTEN_NONE);    // FIXME: unique sound?
962                                 }
963                                 else
964                                         Send_Notification(NOTIF_ONE, it, MSG_CENTER, APP_TEAM_NUM(this.team, CENTER_ONS_NOTSHIELDED));
965                         });
966                 }
967         }
968 }
969
970 void ons_GeneratorReset(entity this)
971 {
972         this.team = this.team_saved;
973         this.lasthealth = this.max_health = this.health = autocvar_g_onslaught_gen_health;
974         this.takedamage = DAMAGE_AIM;
975         this.bot_attack = true;
976         if(!IL_CONTAINS(g_bot_targets, this))
977                 IL_PUSH(g_bot_targets, this);
978         this.iscaptured = true;
979         this.islinked = true;
980         this.isshielded = true;
981         this.event_damage = ons_GeneratorDamage;
982         setthink(this, ons_GeneratorThink);
983         this.nextthink = time + GEN_THINKRATE;
984
985         Net_LinkEntity(this, false, 0, generator_send);
986
987         this.SendFlags = GSF_SETUP; // just incase
988         this.SendFlags |= GSF_STATUS;
989
990         WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
991         WaypointSprite_UpdateHealth(this.sprite, this.health);
992         WaypointSprite_UpdateRule(this.sprite,this.team,SPRITERULE_TEAMPLAY);
993
994         onslaught_updatelinks();
995 }
996
997 void ons_DelayedGeneratorSetup(entity this)
998 {
999         // bot waypoints
1000         waypoint_spawnforitem_force(this, this.origin);
1001         this.nearestwaypointtimeout = 0; // activate waypointing again
1002         this.bot_basewaypoint = this.nearestwaypoint;
1003
1004         // captureshield setup
1005         ons_CaptureShield_Spawn(this, true);
1006
1007         onslaught_updatelinks();
1008
1009         Net_LinkEntity(this, false, 0, generator_send);
1010 }
1011
1012
1013 void onslaught_generator_touch(entity this, entity toucher)
1014 {
1015         if ( IS_PLAYER(toucher) )
1016         if ( SAME_TEAM(this,toucher) )
1017         if ( this.iscaptured )
1018         {
1019                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_ONS_TELEPORT);
1020         }
1021 }
1022
1023 void ons_GeneratorSetup(entity gen) // called when spawning a generator entity on the map as a spawnfunc
1024 {
1025         // declarations
1026         int teamnumber = gen.team;
1027
1028         // main setup
1029         gen.ons_worldgeneratornext = ons_worldgeneratorlist; // link generator into ons_worldgeneratorlist
1030         ons_worldgeneratorlist = gen;
1031
1032         gen.netname = sprintf("%s generator", Team_ColoredFullName(teamnumber));
1033         gen.classname = "onslaught_generator";
1034         gen.solid = SOLID_BBOX;
1035         gen.team_saved = teamnumber;
1036         IL_PUSH(g_saved_team, gen);
1037         set_movetype(gen, MOVETYPE_NONE);
1038         gen.lasthealth = gen.max_health = gen.health = autocvar_g_onslaught_gen_health;
1039         gen.takedamage = DAMAGE_AIM;
1040         gen.bot_attack = true;
1041         IL_PUSH(g_bot_targets, gen);
1042         gen.event_damage = ons_GeneratorDamage;
1043         gen.reset = ons_GeneratorReset;
1044         setthink(gen, ons_GeneratorThink);
1045         gen.nextthink = time + GEN_THINKRATE;
1046         gen.iscaptured = true;
1047         gen.islinked = true;
1048         gen.isshielded = true;
1049         settouch(gen, onslaught_generator_touch);
1050
1051         // appearence
1052         // model handled by CSQC
1053         setsize(gen, GENERATOR_MIN, GENERATOR_MAX);
1054         setorigin(gen, (gen.origin + CPGEN_SPAWN_OFFSET));
1055         gen.colormap = 1024 + (teamnumber - 1) * 17;
1056
1057         // generator placement
1058         droptofloor(gen);
1059
1060         // waypointsprites
1061         WaypointSprite_SpawnFixed(WP_Null, gen.origin + CPGEN_WAYPOINT_OFFSET, gen, sprite, RADARICON_NONE);
1062         WaypointSprite_UpdateRule(gen.sprite, gen.team, SPRITERULE_TEAMPLAY);
1063         WaypointSprite_UpdateMaxHealth(gen.sprite, gen.max_health);
1064         WaypointSprite_UpdateHealth(gen.sprite, gen.health);
1065
1066         InitializeEntity(gen, ons_DelayedGeneratorSetup, INITPRIO_SETLOCATION);
1067 }
1068
1069
1070 // ===============
1071 //  Round Handler
1072 // ===============
1073
1074 int total_generators;
1075 void Onslaught_count_generators()
1076 {
1077         entity e;
1078         total_generators = redowned = blueowned = yellowowned = pinkowned = 0;
1079         for(e = ons_worldgeneratorlist; e; e = e.ons_worldgeneratornext)
1080         {
1081                 ++total_generators;
1082                 redowned += (e.team == NUM_TEAM_1 && e.health > 0);
1083                 blueowned += (e.team == NUM_TEAM_2 && e.health > 0);
1084                 yellowowned += (e.team == NUM_TEAM_3 && e.health > 0);
1085                 pinkowned += (e.team == NUM_TEAM_4 && e.health > 0);
1086         }
1087 }
1088
1089 int Onslaught_GetWinnerTeam()
1090 {
1091         int winner_team = 0;
1092         if(redowned > 0)
1093                 winner_team = NUM_TEAM_1;
1094         if(blueowned > 0)
1095         {
1096                 if(winner_team) return 0;
1097                 winner_team = NUM_TEAM_2;
1098         }
1099         if(yellowowned > 0)
1100         {
1101                 if(winner_team) return 0;
1102                 winner_team = NUM_TEAM_3;
1103         }
1104         if(pinkowned > 0)
1105         {
1106                 if(winner_team) return 0;
1107                 winner_team = NUM_TEAM_4;
1108         }
1109         if(winner_team)
1110                 return winner_team;
1111         return -1; // no generators left?
1112 }
1113
1114 void nades_Clear(entity e);
1115
1116 #define ONS_OWNED_GENERATORS() ((redowned > 0) + (blueowned > 0) + (yellowowned > 0) + (pinkowned > 0))
1117 #define ONS_OWNED_GENERATORS_OK() (ONS_OWNED_GENERATORS() > 1)
1118 bool Onslaught_CheckWinner()
1119 {
1120         if ((autocvar_timelimit && time > game_starttime + autocvar_timelimit * 60) || (round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0))
1121         {
1122                 ons_stalemate = true;
1123
1124                 if (!wpforenemy_announced)
1125                 {
1126                         Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_OVERTIME_CONTROLPOINT);
1127                         sound(NULL, CH_INFO, SND_ONS_GENERATOR_DECAY, VOL_BASE, ATTEN_NONE);
1128
1129                         wpforenemy_announced = true;
1130                 }
1131
1132                 entity tmp_entity; // temporary entity
1133                 float d;
1134                 for(tmp_entity = ons_worldgeneratorlist; tmp_entity; tmp_entity = tmp_entity.ons_worldgeneratornext) if(time >= tmp_entity.ons_overtime_damagedelay)
1135                 {
1136                         // tmp_entity.max_health / 300 gives 5 minutes of overtime.
1137                         // control points reduce the overtime duration.
1138                         d = 1;
1139                         entity e;
1140                         for(e = ons_worldcplist; e; e = e.ons_worldcpnext)
1141                         {
1142                                 if(DIFF_TEAM(e, tmp_entity))
1143                                 if(e.islinked)
1144                                         d = d + 1;
1145                         }
1146
1147                         if(autocvar_g_campaign && autocvar__campaign_testrun)
1148                                 d = d * tmp_entity.max_health;
1149                         else
1150                                 d = d * tmp_entity.max_health / max(30, 60 * autocvar_timelimit_suddendeath);
1151
1152                         Damage(tmp_entity, tmp_entity, tmp_entity, d, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, tmp_entity.origin, '0 0 0');
1153
1154                         tmp_entity.sprite.SendFlags |= 16;
1155
1156                         tmp_entity.ons_overtime_damagedelay = time + 1;
1157                 }
1158         }
1159         else { wpforenemy_announced = false; ons_stalemate = false; }
1160
1161         Onslaught_count_generators();
1162
1163         if(ONS_OWNED_GENERATORS_OK())
1164                 return 0;
1165
1166         int winner_team = Onslaught_GetWinnerTeam();
1167
1168         if(winner_team > 0)
1169         {
1170                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
1171                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
1172                 TeamScore_AddToTeam(winner_team, ST_ONS_CAPS, +1);
1173         }
1174         else if(winner_team == -1)
1175         {
1176                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
1177                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
1178         }
1179
1180         ons_stalemate = false;
1181
1182         play2all(SND(CTF_CAPTURE(winner_team)));
1183
1184         round_handler_Init(7, autocvar_g_onslaught_warmup, autocvar_g_onslaught_round_timelimit);
1185
1186         FOREACH_CLIENT(IS_PLAYER(it), {
1187                 STAT(ROUNDLOST, it) = true;
1188                 it.player_blocked = true;
1189
1190                 nades_Clear(it);
1191         });
1192
1193         game_stopped = true;
1194         return 1;
1195 }
1196
1197 bool Onslaught_CheckPlayers()
1198 {
1199         return 1;
1200 }
1201
1202 void Onslaught_RoundStart()
1203 {
1204         entity tmp_entity;
1205         FOREACH_CLIENT(IS_PLAYER(it), it.player_blocked = false);
1206
1207         for(tmp_entity = ons_worldcplist; tmp_entity; tmp_entity = tmp_entity.ons_worldcpnext)
1208                 tmp_entity.sprite.SendFlags |= 16;
1209
1210         for(tmp_entity = ons_worldgeneratorlist; tmp_entity; tmp_entity = tmp_entity.ons_worldgeneratornext)
1211                 tmp_entity.sprite.SendFlags |= 16;
1212 }
1213
1214
1215 // ================
1216 // Bot player logic
1217 // ================
1218
1219 // NOTE: LEGACY CODE, needs to be re-written!
1220
1221 void havocbot_goalrating_ons_offenseitems(entity this, float ratingscale, vector org, float sradius)
1222 {
1223         bool needarmor = false, needweapons = false;
1224
1225         // Needs armor/health?
1226         if(this.health<100)
1227                 needarmor = true;
1228
1229         // Needs weapons?
1230         int c = 0;
1231         FOREACH(Weapons, it != WEP_Null, {
1232                 if(STAT(WEAPONS, this) & (it.m_wepset))
1233                 if(++c >= 4)
1234                         break;
1235         });
1236
1237         if(c<4)
1238                 needweapons = true;
1239
1240         if(!needweapons && !needarmor)
1241                 return;
1242
1243         LOG_DEBUG(this.netname, " needs weapons ", ftos(needweapons));
1244         LOG_DEBUG(this.netname, " needs armor ", ftos(needarmor));
1245
1246         // See what is around
1247         IL_EACH(g_items, it.bot_pickup,
1248         {
1249                 // gather health and armor only
1250                 if (it.solid)
1251                 if ( ((it.health || it.armorvalue) && needarmor) || (it.weapons && needweapons ) )
1252                 if (vdist(it.origin - org, <, sradius))
1253                 {
1254                         int t = it.bot_pickupevalfunc(this, it);
1255                         if (t > 0)
1256                                 navigation_routerating(this, it, t * ratingscale, 500);
1257                 }
1258         });
1259 }
1260
1261 void havocbot_role_ons_setrole(entity this, int role)
1262 {
1263         LOG_DEBUG(this.netname," switched to ");
1264         switch(role)
1265         {
1266                 case HAVOCBOT_ONS_ROLE_DEFENSE:
1267                         LOG_DEBUG("defense");
1268                         this.havocbot_role = havocbot_role_ons_defense;
1269                         this.havocbot_role_flags = HAVOCBOT_ONS_ROLE_DEFENSE;
1270                         this.havocbot_role_timeout = 0;
1271                         break;
1272                 case HAVOCBOT_ONS_ROLE_ASSISTANT:
1273                         LOG_DEBUG("assistant");
1274                         this.havocbot_role = havocbot_role_ons_assistant;
1275                         this.havocbot_role_flags = HAVOCBOT_ONS_ROLE_ASSISTANT;
1276                         this.havocbot_role_timeout = 0;
1277                         break;
1278                 case HAVOCBOT_ONS_ROLE_OFFENSE:
1279                         LOG_DEBUG("offense");
1280                         this.havocbot_role = havocbot_role_ons_offense;
1281                         this.havocbot_role_flags = HAVOCBOT_ONS_ROLE_OFFENSE;
1282                         this.havocbot_role_timeout = 0;
1283                         break;
1284         }
1285         LOG_DEBUG("");
1286 }
1287
1288 void havocbot_goalrating_ons_controlpoints_attack(entity this, float ratingscale)
1289 {
1290         entity cp, cp1, cp2, best, wp;
1291         float radius, bestvalue;
1292         int c;
1293         bool found;
1294
1295         // Filter control points
1296         for(cp2 = ons_worldcplist; cp2; cp2 = cp2.ons_worldcpnext)
1297         {
1298                 cp2.wpcost = c = 0;
1299                 cp2.wpconsidered = false;
1300
1301                 if(cp2.isshielded)
1302                         continue;
1303
1304                 // Ignore owned controlpoints
1305                 if(!((cp2.aregensneighbor & BIT(this.team)) || (cp2.arecpsneighbor & BIT(this.team))))
1306                         continue;
1307
1308                 // Count team mates interested in this control point
1309                 // (easier and cleaner than keeping counters per cp and teams)
1310                 FOREACH_CLIENT(IS_PLAYER(it), {
1311                         if(SAME_TEAM(it, this))
1312                         if(it.havocbot_role_flags & HAVOCBOT_ONS_ROLE_OFFENSE)
1313                         if(it.havocbot_ons_target == cp2)
1314                                 ++c;
1315                 });
1316
1317                 // NOTE: probably decrease the cost of attackable control points
1318                 cp2.wpcost = c;
1319                 cp2.wpconsidered = true;
1320         }
1321
1322         // We'll consider only the best case
1323         bestvalue = 99999999999;
1324         cp = NULL;
1325         for(cp1 = ons_worldcplist; cp1; cp1 = cp1.ons_worldcpnext)
1326         {
1327                 if (!cp1.wpconsidered)
1328                         continue;
1329
1330                 if(cp1.wpcost<bestvalue)
1331                 {
1332                         bestvalue = cp1.wpcost;
1333                         cp = cp1;
1334                         this.havocbot_ons_target = cp1;
1335                 }
1336         }
1337
1338         if (!cp)
1339                 return;
1340
1341         LOG_DEBUG(this.netname, " chose cp ranked ", ftos(bestvalue));
1342
1343         if(cp.goalentity)
1344         {
1345                 // Should be attacked
1346                 // Rate waypoints near it
1347                 found = false;
1348                 best = NULL;
1349                 bestvalue = 99999999999;
1350                 for(radius=0; radius<1000 && !found; radius+=500)
1351                 {
1352                         for(wp=findradius(cp.origin,radius); wp; wp=wp.chain)
1353                         {
1354                                 if(!(wp.wpflags & WAYPOINTFLAG_GENERATED))
1355                                 if(wp.classname=="waypoint")
1356                                 if(checkpvs(wp.origin,cp))
1357                                 {
1358                                         found = true;
1359                                         if(wp.cnt<bestvalue)
1360                                         {
1361                                                 best = wp;
1362                                                 bestvalue = wp.cnt;
1363                                         }
1364                                 }
1365                         }
1366                 }
1367
1368                 if(best)
1369                 {
1370                         navigation_routerating(this, best, ratingscale, 10000);
1371                         best.cnt += 1;
1372
1373                         this.havocbot_attack_time = 0;
1374                         if(checkpvs(this.origin + this.view_ofs, cp))
1375                         if(checkpvs(this.origin + this.view_ofs, best))
1376                                 this.havocbot_attack_time = time + 2;
1377                 }
1378                 else
1379                 {
1380                         navigation_routerating(this, cp, ratingscale, 10000);
1381                 }
1382                 LOG_DEBUG(this.netname, " found an attackable controlpoint at ", vtos(cp.origin));
1383         }
1384         else
1385         {
1386                 // Should be touched
1387                 LOG_DEBUG(this.netname, " found a touchable controlpoint at ", vtos(cp.origin));
1388                 found = false;
1389
1390                 // Look for auto generated waypoint
1391                 if (!bot_waypoints_for_items)
1392                 for (wp = findradius(cp.origin,100); wp; wp = wp.chain)
1393                 {
1394                         if(wp.classname=="waypoint")
1395                         {
1396                                 navigation_routerating(this, wp, ratingscale, 10000);
1397                                 found = true;
1398                         }
1399                 }
1400
1401                 // Nothing found, rate the controlpoint itself
1402                 if (!found)
1403                         navigation_routerating(this, cp, ratingscale, 10000);
1404         }
1405 }
1406
1407 bool havocbot_goalrating_ons_generator_attack(entity this, float ratingscale)
1408 {
1409         entity g, wp, bestwp;
1410         bool found;
1411         int best;
1412
1413         for(g = ons_worldgeneratorlist; g; g = g.ons_worldgeneratornext)
1414         {
1415                 if(SAME_TEAM(g, this) || g.isshielded)
1416                         continue;
1417
1418                 // Should be attacked
1419                 // Rate waypoints near it
1420                 found = false;
1421                 bestwp = NULL;
1422                 best = 99999999999;
1423
1424                 for(wp=findradius(g.origin,400); wp; wp=wp.chain)
1425                 {
1426                         if(wp.classname=="waypoint")
1427                         if(checkpvs(wp.origin,g))
1428                         {
1429                                 found = true;
1430                                 if(wp.cnt<best)
1431                                 {
1432                                         bestwp = wp;
1433                                         best = wp.cnt;
1434                                 }
1435                         }
1436                 }
1437
1438                 if(bestwp)
1439                 {
1440                         LOG_DEBUG("waypoints found around generator");
1441                         navigation_routerating(this, bestwp, ratingscale, 10000);
1442                         bestwp.cnt += 1;
1443
1444                         this.havocbot_attack_time = 0;
1445                         if(checkpvs(this.origin + this.view_ofs, g))
1446                         if(checkpvs(this.origin + this.view_ofs, bestwp))
1447                                 this.havocbot_attack_time = time + 5;
1448
1449                         return true;
1450                 }
1451                 else
1452                 {
1453                         LOG_DEBUG("generator found without waypoints around");
1454                         // if there aren't waypoints near the generator go straight to it
1455                         navigation_routerating(this, g, ratingscale, 10000);
1456                         this.havocbot_attack_time = 0;
1457                         return true;
1458                 }
1459         }
1460         return false;
1461 }
1462
1463 void havocbot_role_ons_offense(entity this)
1464 {
1465         if(IS_DEAD(this))
1466         {
1467                 this.havocbot_attack_time = 0;
1468                 havocbot_ons_reset_role(this);
1469                 return;
1470         }
1471
1472         // Set the role timeout if necessary
1473         if (!this.havocbot_role_timeout)
1474                 this.havocbot_role_timeout = time + 120;
1475
1476         if (time > this.havocbot_role_timeout)
1477         {
1478                 havocbot_ons_reset_role(this);
1479                 return;
1480         }
1481
1482         if(this.havocbot_attack_time>time)
1483                 return;
1484
1485         if (navigation_goalrating_timeout(this))
1486         {
1487                 navigation_goalrating_start(this);
1488                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 650);
1489                 if(!havocbot_goalrating_ons_generator_attack(this, 20000))
1490                         havocbot_goalrating_ons_controlpoints_attack(this, 20000);
1491                 havocbot_goalrating_ons_offenseitems(this, 10000, this.origin, 10000);
1492                 navigation_goalrating_end(this);
1493
1494                 navigation_goalrating_timeout_set(this);
1495         }
1496 }
1497
1498 void havocbot_role_ons_assistant(entity this)
1499 {
1500         havocbot_ons_reset_role(this);
1501 }
1502
1503 void havocbot_role_ons_defense(entity this)
1504 {
1505         havocbot_ons_reset_role(this);
1506 }
1507
1508 void havocbot_ons_reset_role(entity this)
1509 {
1510         if(IS_DEAD(this))
1511                 return;
1512
1513         this.havocbot_ons_target = NULL;
1514
1515         // TODO: Defend control points or generator if necessary
1516
1517         havocbot_role_ons_setrole(this, HAVOCBOT_ONS_ROLE_OFFENSE);
1518 }
1519
1520
1521 /*
1522  * Find control point or generator owned by the same team self which is nearest to pos
1523  * if max_dist is positive, only control points within this range will be considered
1524  */
1525 entity ons_Nearest_ControlPoint(entity this, vector pos, float max_dist)
1526 {
1527         entity closest_target = NULL;
1528         for(entity cp = ons_worldcplist; cp; cp = cp.ons_worldcpnext)
1529         {
1530                 if(SAME_TEAM(cp, this))
1531                 if(cp.iscaptured)
1532                 if(max_dist <= 0 || vdist(cp.origin - pos, <=, max_dist))
1533                 if(vlen2(cp.origin - pos) <= vlen2(closest_target.origin - pos) || closest_target == NULL)
1534                         closest_target = cp;
1535         }
1536         for(entity gen = ons_worldgeneratorlist; gen; gen = gen.ons_worldgeneratornext)
1537         {
1538                 if(SAME_TEAM(gen, this))
1539                 if(max_dist <= 0 || vdist(gen.origin - pos, <, max_dist))
1540                 if(vlen2(gen.origin - pos) <= vlen2(closest_target.origin - pos) || closest_target == NULL)
1541                         closest_target = gen;
1542         }
1543
1544         return closest_target;
1545 }
1546
1547 /*
1548  * Find control point or generator owned by the same team self which is nearest to pos
1549  * if max_dist is positive, only control points within this range will be considered
1550  * This function only check distances on the XY plane, disregarding Z
1551  */
1552 entity ons_Nearest_ControlPoint_2D(entity this, vector pos, float max_dist)
1553 {
1554         entity closest_target = NULL;
1555         vector delta;
1556         float smallest_distance = 0, distance;
1557
1558         for(entity cp = ons_worldcplist; cp; cp = cp.ons_worldcpnext)
1559         {
1560                 delta = cp.origin - pos;
1561                 delta_z = 0;
1562                 distance = vlen(delta);
1563
1564                 if(SAME_TEAM(cp, this))
1565                 if(cp.iscaptured)
1566                 if(max_dist <= 0 || distance <= max_dist)
1567                 if(closest_target == NULL || distance <= smallest_distance )
1568                 {
1569                         closest_target = cp;
1570                         smallest_distance = distance;
1571                 }
1572         }
1573         for(entity gen = ons_worldgeneratorlist; gen; gen = gen.ons_worldgeneratornext)
1574         {
1575                 delta = gen.origin - pos;
1576                 delta_z = 0;
1577                 distance = vlen(delta);
1578
1579                 if(SAME_TEAM(gen, this))
1580                 if(max_dist <= 0 || distance <= max_dist)
1581                 if(closest_target == NULL || distance <= smallest_distance )
1582                 {
1583                         closest_target = gen;
1584                         smallest_distance = distance;
1585                 }
1586         }
1587
1588         return closest_target;
1589 }
1590 /**
1591  * find the number of control points and generators in the same team as this
1592  */
1593 int ons_Count_SelfControlPoints(entity this)
1594 {
1595         int n = 0;
1596         for(entity cp = ons_worldcplist; cp; cp = cp.ons_worldcpnext)
1597         {
1598                 if(SAME_TEAM(cp, this))
1599                 if(cp.iscaptured)
1600                         n++;
1601         }
1602         for(entity gen = ons_worldgeneratorlist; gen; gen = gen.ons_worldgeneratornext)
1603         {
1604                 if(SAME_TEAM(gen, this))
1605                         n++;
1606         }
1607         return n;
1608 }
1609
1610 /**
1611  * Teleport player to a random position near tele_target
1612  * if tele_effects is true, teleport sound+particles are created
1613  * return false on failure
1614  */
1615 bool ons_Teleport(entity player, entity tele_target, float range, bool tele_effects)
1616 {
1617         if ( !tele_target )
1618                 return false;
1619
1620         int i;
1621         vector loc;
1622         float theta;
1623         // narrow the range for each iteration to increase chances that a spawnpoint
1624         // can be found even if there's little room around the control point
1625         float iteration_scale = 1;
1626         for(i = 0; i < 16; ++i)
1627         {
1628                 iteration_scale -= i / 16;
1629                 theta = random() * 2 * M_PI;
1630                 loc_y = sin(theta);
1631                 loc_x = cos(theta);
1632                 loc_z = 0;
1633                 loc *= random() * range * iteration_scale;
1634
1635                 loc += tele_target.origin + '0 0 128' * iteration_scale;
1636
1637                 tracebox(loc, STAT(PL_MIN, player), STAT(PL_MAX, player), loc, MOVE_NORMAL, player);
1638                 if(trace_fraction == 1.0 && !trace_startsolid)
1639                 {
1640                         traceline(tele_target.origin, loc, MOVE_NOMONSTERS, tele_target); // double check to make sure we're not spawning outside the NULL
1641                         if(trace_fraction == 1.0 && !trace_startsolid)
1642                         {
1643                                 if ( tele_effects )
1644                                 {
1645                                         Send_Effect(EFFECT_TELEPORT, player.origin, '0 0 0', 1);
1646                                         sound (player, CH_TRIGGER, SND_TELEPORT, VOL_BASE, ATTEN_NORM);
1647                                 }
1648                                 setorigin(player, loc);
1649                                 player.angles = '0 1 0' * ( theta * RAD2DEG + 180 );
1650                                 makevectors(player.angles);
1651                                 player.fixangle = true;
1652                                 player.teleport_antispam = time + autocvar_g_onslaught_teleport_wait;
1653
1654                                 if ( tele_effects )
1655                                         Send_Effect(EFFECT_TELEPORT, player.origin + v_forward * 32, '0 0 0', 1);
1656                                 return true;
1657                         }
1658                 }
1659         }
1660
1661         return false;
1662 }
1663
1664 // ==============
1665 // Hook Functions
1666 // ==============
1667
1668 MUTATOR_HOOKFUNCTION(ons, reset_map_global)
1669 {
1670         FOREACH_CLIENT(IS_PLAYER(it), {
1671                 STAT(ROUNDLOST, it) = false;
1672                 it.ons_deathloc = '0 0 0';
1673                 PutClientInServer(it);
1674                 it.clientcamera = it;
1675         });
1676         return false;
1677 }
1678
1679 MUTATOR_HOOKFUNCTION(ons, ClientDisconnect)
1680 {
1681         entity player = M_ARGV(0, entity);
1682
1683         player.ons_deathloc = '0 0 0';
1684 }
1685
1686 MUTATOR_HOOKFUNCTION(ons, MakePlayerObserver)
1687 {
1688         entity player = M_ARGV(0, entity);
1689
1690         player.ons_deathloc = '0 0 0';
1691 }
1692
1693 MUTATOR_HOOKFUNCTION(ons, PlayerSpawn)
1694 {
1695         entity player = M_ARGV(0, entity);
1696
1697         if(!round_handler_IsRoundStarted())
1698         {
1699                 player.player_blocked = true;
1700                 return false;
1701         }
1702
1703         entity l;
1704         for(l = ons_worldgeneratorlist; l; l = l.ons_worldgeneratornext)
1705         {
1706                 l.sprite.SendFlags |= 16;
1707         }
1708         for(l = ons_worldcplist; l; l = l.ons_worldcpnext)
1709         {
1710                 l.sprite.SendFlags |= 16;
1711         }
1712
1713         if(ons_stalemate) { Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_OVERTIME_CONTROLPOINT); }
1714
1715         if ( autocvar_g_onslaught_spawn_choose )
1716         if ( player.ons_spawn_by )
1717         if ( ons_Teleport(player,player.ons_spawn_by,autocvar_g_onslaught_teleport_radius,false) )
1718         {
1719                 player.ons_spawn_by = NULL;
1720                 return false;
1721         }
1722
1723         if(autocvar_g_onslaught_spawn_at_controlpoints)
1724         if(random() <= autocvar_g_onslaught_spawn_at_controlpoints_chance)
1725         {
1726                 float random_target = autocvar_g_onslaught_spawn_at_controlpoints_random;
1727                 entity tmp_entity, closest_target = NULL;
1728                 vector spawn_loc = player.ons_deathloc;
1729
1730                 // new joining player or round reset, don't bother checking
1731                 if(spawn_loc == '0 0 0') { return false; }
1732
1733                 if(random_target) { RandomSelection_Init(); }
1734
1735                 for(tmp_entity = ons_worldcplist; tmp_entity; tmp_entity = tmp_entity.ons_worldcpnext)
1736                 {
1737                         if(SAME_TEAM(tmp_entity, player))
1738                         if(random_target)
1739                                 RandomSelection_AddEnt(tmp_entity, 1, 1);
1740                         else if(vlen2(tmp_entity.origin - spawn_loc) <= vlen2(closest_target.origin - spawn_loc) || closest_target == NULL)
1741                                 closest_target = tmp_entity;
1742                 }
1743
1744                 if(random_target) { closest_target = RandomSelection_chosen_ent; }
1745
1746                 if(closest_target)
1747                 {
1748                         float i;
1749                         vector loc;
1750                         float iteration_scale = 1;
1751                         for(i = 0; i < 10; ++i)
1752                         {
1753                                 iteration_scale -= i / 10;
1754                                 loc = closest_target.origin + '0 0 96' * iteration_scale;
1755                                 loc += ('0 1 0' * random()) * 128 * iteration_scale;
1756                                 tracebox(loc, STAT(PL_MIN, player), STAT(PL_MAX, player), loc, MOVE_NORMAL, player);
1757                                 if(trace_fraction == 1.0 && !trace_startsolid)
1758                                 {
1759                                         traceline(closest_target.origin, loc, MOVE_NOMONSTERS, closest_target); // double check to make sure we're not spawning outside the NULL
1760                                         if(trace_fraction == 1.0 && !trace_startsolid)
1761                                         {
1762                                                 setorigin(player, loc);
1763                                                 player.angles = normalize(loc - closest_target.origin) * RAD2DEG;
1764                                                 return false;
1765                                         }
1766                                 }
1767                         }
1768                 }
1769         }
1770
1771         if(autocvar_g_onslaught_spawn_at_generator)
1772         if(random() <= autocvar_g_onslaught_spawn_at_generator_chance)
1773         {
1774                 float random_target = autocvar_g_onslaught_spawn_at_generator_random;
1775                 entity tmp_entity, closest_target = NULL;
1776                 vector spawn_loc = player.ons_deathloc;
1777
1778                 // new joining player or round reset, don't bother checking
1779                 if(spawn_loc == '0 0 0') { return false; }
1780
1781                 if(random_target) { RandomSelection_Init(); }
1782
1783                 for(tmp_entity = ons_worldgeneratorlist; tmp_entity; tmp_entity = tmp_entity.ons_worldgeneratornext)
1784                 {
1785                         if(random_target)
1786                                 RandomSelection_AddEnt(tmp_entity, 1, 1);
1787                         else
1788                         {
1789                                 if(SAME_TEAM(tmp_entity, player))
1790                                 if(vlen2(tmp_entity.origin - spawn_loc) <= vlen2(closest_target.origin - spawn_loc) || closest_target == NULL)
1791                                         closest_target = tmp_entity;
1792                         }
1793                 }
1794
1795                 if(random_target) { closest_target = RandomSelection_chosen_ent; }
1796
1797                 if(closest_target)
1798                 {
1799                         float i;
1800                         vector loc;
1801                         float iteration_scale = 1;
1802                         for(i = 0; i < 10; ++i)
1803                         {
1804                                 iteration_scale -= i / 10;
1805                                 loc = closest_target.origin + '0 0 128' * iteration_scale;
1806                                 loc += ('0 1 0' * random()) * 256 * iteration_scale;
1807                                 tracebox(loc, STAT(PL_MIN, player), STAT(PL_MAX, player), loc, MOVE_NORMAL, player);
1808                                 if(trace_fraction == 1.0 && !trace_startsolid)
1809                                 {
1810                                         traceline(closest_target.origin, loc, MOVE_NOMONSTERS, closest_target); // double check to make sure we're not spawning outside the NULL
1811                                         if(trace_fraction == 1.0 && !trace_startsolid)
1812                                         {
1813                                                 setorigin(player, loc);
1814                                                 player.angles = normalize(loc - closest_target.origin) * RAD2DEG;
1815                                                 return false;
1816                                         }
1817                                 }
1818                         }
1819                 }
1820         }
1821
1822         return false;
1823 }
1824
1825 MUTATOR_HOOKFUNCTION(ons, PlayerDies)
1826 {
1827         entity frag_target = M_ARGV(2, entity);
1828
1829         frag_target.ons_deathloc = frag_target.origin;
1830         entity l;
1831         for(l = ons_worldgeneratorlist; l; l = l.ons_worldgeneratornext)
1832         {
1833                 l.sprite.SendFlags |= 16;
1834         }
1835         for(l = ons_worldcplist; l; l = l.ons_worldcpnext)
1836         {
1837                 l.sprite.SendFlags |= 16;
1838         }
1839
1840         if ( autocvar_g_onslaught_spawn_choose )
1841         if ( ons_Count_SelfControlPoints(frag_target) > 1 )
1842                 stuffcmd(frag_target, "qc_cmd_cl hud clickradar\n");
1843
1844         return false;
1845 }
1846
1847 MUTATOR_HOOKFUNCTION(ons, MonsterMove)
1848 {
1849         entity mon = M_ARGV(0, entity);
1850
1851         entity e = find(NULL, targetname, mon.target);
1852         if (e != NULL)
1853                 mon.team = e.team;
1854 }
1855
1856 void ons_MonsterSpawn_Delayed(entity this)
1857 {
1858         entity own = this.owner;
1859
1860         if(!own) { delete(this); return; }
1861
1862         if(own.targetname)
1863         {
1864                 entity e = find(NULL, target, own.targetname);
1865                 if(e != NULL)
1866                 {
1867                         own.team = e.team;
1868
1869                         own.use(own, e, NULL);
1870                 }
1871         }
1872
1873         delete(this);
1874 }
1875
1876 MUTATOR_HOOKFUNCTION(ons, MonsterSpawn)
1877 {
1878         entity mon = M_ARGV(0, entity);
1879
1880         entity e = spawn();
1881         e.owner = mon;
1882         InitializeEntity(e, ons_MonsterSpawn_Delayed, INITPRIO_FINDTARGET);
1883 }
1884
1885 void ons_TurretSpawn_Delayed(entity this)
1886 {
1887         entity own = this.owner;
1888
1889         if(!own) { delete(this); return; }
1890
1891         if(own.targetname)
1892         {
1893                 entity e = find(NULL, target, own.targetname);
1894                 if(e != NULL)
1895                 {
1896                         own.team = e.team;
1897                         own.active = ACTIVE_NOT;
1898
1899                         own.use(own, e, NULL);
1900                 }
1901         }
1902
1903         delete(this);
1904 }
1905
1906 MUTATOR_HOOKFUNCTION(ons, TurretSpawn)
1907 {
1908         entity turret = M_ARGV(0, entity);
1909
1910         entity e = spawn();
1911         e.owner = turret;
1912         InitializeEntity(e, ons_TurretSpawn_Delayed, INITPRIO_FINDTARGET);
1913
1914         return false;
1915 }
1916
1917 MUTATOR_HOOKFUNCTION(ons, HavocBot_ChooseRole)
1918 {
1919         entity bot = M_ARGV(0, entity);
1920
1921         havocbot_ons_reset_role(bot);
1922         return true;
1923 }
1924
1925 MUTATOR_HOOKFUNCTION(ons, CheckAllowedTeams)
1926 {
1927         // onslaught is special
1928         for(entity tmp_entity = ons_worldgeneratorlist; tmp_entity; tmp_entity = tmp_entity.ons_worldgeneratornext)
1929         {
1930                 switch(tmp_entity.team)
1931                 {
1932                         case NUM_TEAM_1: c1 = 0; break;
1933                         case NUM_TEAM_2: c2 = 0; break;
1934                         case NUM_TEAM_3: c3 = 0; break;
1935                         case NUM_TEAM_4: c4 = 0; break;
1936                 }
1937         }
1938
1939         return true;
1940 }
1941
1942 MUTATOR_HOOKFUNCTION(ons, SpectateCopy)
1943 {
1944         entity spectatee = M_ARGV(0, entity);
1945         entity client = M_ARGV(1, entity);
1946
1947         STAT(ROUNDLOST, client) = STAT(ROUNDLOST, spectatee); // make spectators see it too
1948 }
1949
1950 MUTATOR_HOOKFUNCTION(ons, SV_ParseClientCommand)
1951 {
1952         if(MUTATOR_RETURNVALUE) // command was already handled?
1953                 return false;
1954
1955         entity player = M_ARGV(0, entity);
1956         string cmd_name = M_ARGV(1, string);
1957         int cmd_argc = M_ARGV(2, int);
1958
1959         if ( cmd_name == "ons_spawn" )
1960         {
1961                 vector pos = player.origin;
1962                 if(cmd_argc > 1)
1963                         pos_x = stof(argv(1));
1964                 if(cmd_argc > 2)
1965                         pos_y = stof(argv(2));
1966                 if(cmd_argc > 3)
1967                         pos_z = stof(argv(3));
1968
1969                 if ( IS_PLAYER(player) )
1970                 {
1971                         if ( !STAT(FROZEN, player) )
1972                         {
1973                                 entity source_point = ons_Nearest_ControlPoint(player, player.origin, autocvar_g_onslaught_teleport_radius);
1974
1975                                 if ( !source_point && player.health > 0 )
1976                                 {
1977                                         sprint(player, "\nYou need to be next to a control point\n");
1978                                         return true;
1979                                 }
1980
1981
1982                                 entity closest_target = ons_Nearest_ControlPoint_2D(player, pos, autocvar_g_onslaught_click_radius);
1983
1984                                 if ( closest_target == NULL )
1985                                 {
1986                                         sprint(player, "\nNo control point found\n");
1987                                         return true;
1988                                 }
1989
1990                                 if ( player.health <= 0 )
1991                                 {
1992                                         player.ons_spawn_by = closest_target;
1993                                         player.respawn_flags = player.respawn_flags | RESPAWN_FORCE;
1994                                 }
1995                                 else
1996                                 {
1997                                         if ( source_point == closest_target )
1998                                         {
1999                                                 sprint(player, "\nTeleporting to the same point\n");
2000                                                 return true;
2001                                         }
2002
2003                                         if ( !ons_Teleport(player,closest_target,autocvar_g_onslaught_teleport_radius,true) )
2004                                                 sprint(player, "\nUnable to teleport there\n");
2005                                 }
2006
2007                                 return true;
2008                         }
2009
2010                         sprint(player, "\nNo teleportation for you\n");
2011                 }
2012
2013                 return true;
2014         }
2015         return false;
2016 }
2017
2018 MUTATOR_HOOKFUNCTION(ons, PlayerUseKey)
2019 {
2020         if(MUTATOR_RETURNVALUE || game_stopped) return false;
2021
2022         entity player = M_ARGV(0, entity);
2023
2024         if((time > player.teleport_antispam) && (!IS_DEAD(player)) && !player.vehicle)
2025         {
2026                 entity source_point = ons_Nearest_ControlPoint(player, player.origin, autocvar_g_onslaught_teleport_radius);
2027                 if ( source_point )
2028                 {
2029                         stuffcmd(player, "qc_cmd_cl hud clickradar\n");
2030                         return true;
2031                 }
2032         }
2033 }
2034
2035 MUTATOR_HOOKFUNCTION(ons, PlayHitsound)
2036 {
2037         entity frag_victim = M_ARGV(0, entity);
2038
2039         return (frag_victim.classname == "onslaught_generator" && !frag_victim.isshielded)
2040                 || (frag_victim.classname == "onslaught_controlpoint_icon" && !frag_victim.owner.isshielded);
2041 }
2042
2043 MUTATOR_HOOKFUNCTION(ons, SendWaypoint)
2044 {
2045     entity wp = M_ARGV(0, entity);
2046     entity to = M_ARGV(1, entity);
2047     int sf = M_ARGV(2, int);
2048     int wp_flag = M_ARGV(3, int);
2049
2050         if(sf & 16)
2051         {
2052                 if(wp.owner.classname == "onslaught_controlpoint")
2053                 {
2054                         entity wp_owner = wp.owner;
2055                         entity e = WaypointSprite_getviewentity(to);
2056                         if(SAME_TEAM(e, wp_owner) && wp_owner.goalentity.health >= wp_owner.goalentity.max_health) { wp_flag |= 2; }
2057                         if(!ons_ControlPoint_Attackable(wp_owner, e.team)) { wp_flag |= 2; }
2058                 }
2059                 if(wp.owner.classname == "onslaught_generator")
2060                 {
2061                         entity wp_owner = wp.owner;
2062                         if(wp_owner.isshielded && wp_owner.health >= wp_owner.max_health) { wp_flag |= 2; }
2063                         if(wp_owner.health <= 0) { wp_flag |= 2; }
2064                 }
2065         }
2066
2067         M_ARGV(3, int) = wp_flag;
2068 }
2069
2070 MUTATOR_HOOKFUNCTION(ons, TurretValidateTarget)
2071 {
2072         entity turret_target = M_ARGV(1, entity);
2073
2074         if(substring(turret_target.classname, 0, 10) == "onslaught_") // don't attack onslaught targets, that's the player's job!
2075         {
2076                 M_ARGV(3, float) = -3;
2077                 return true;
2078         }
2079
2080         return false;
2081 }
2082
2083 MUTATOR_HOOKFUNCTION(ons, TurretThink)
2084 {
2085     entity turret = M_ARGV(0, entity);
2086
2087         // ONS uses somewhat backwards linking.
2088         if(turret.target)
2089         {
2090                 entity e = find(NULL, targetname, turret.target);
2091                 if (e != NULL)
2092                         turret.team = e.team;
2093         }
2094
2095         if(turret.team != turret.tur_head.team)
2096                 turret_respawn(turret);
2097 }
2098
2099
2100 // ==========
2101 // Spawnfuncs
2102 // ==========
2103
2104 /*QUAKED spawnfunc_onslaught_link (0 .5 .8) (-16 -16 -16) (16 16 16)
2105   Link between control points.
2106
2107   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.
2108
2109 keys:
2110 "target" - first control point.
2111 "target2" - second control point.
2112  */
2113 spawnfunc(onslaught_link)
2114 {
2115         if(!g_onslaught) { delete(this); return; }
2116
2117         if (this.target == "" || this.target2 == "")
2118                 objerror(this, "target and target2 must be set\n");
2119
2120         this.ons_worldlinknext = ons_worldlinklist; // link into ons_worldlinklist
2121         ons_worldlinklist = this;
2122
2123         InitializeEntity(this, ons_DelayedLinkSetup, INITPRIO_FINDTARGET);
2124         Net_LinkEntity(this, false, 0, ons_Link_Send);
2125 }
2126
2127 /*QUAKED spawnfunc_onslaught_controlpoint (0 .5 .8) (-32 -32 0) (32 32 128)
2128   Control point. Be sure to give this enough clearance so that the shootable part has room to exist
2129
2130   This should link to an spawnfunc_onslaught_controlpoint entity or spawnfunc_onslaught_generator entity.
2131
2132 keys:
2133 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
2134 "target" - target any entities that are tied to this control point, such as vehicles and buildable structure entities.
2135 "message" - name of this control point (should reflect the location in the map, such as "center bridge", "north tower", etc)
2136  */
2137
2138 spawnfunc(onslaught_controlpoint)
2139 {
2140         if(!g_onslaught) { delete(this); return; }
2141
2142         ons_ControlPoint_Setup(this);
2143 }
2144
2145 /*QUAKED spawnfunc_onslaught_generator (0 .5 .8) (-32 -32 -24) (32 32 64)
2146   Base generator.
2147
2148   spawnfunc_onslaught_link entities can target this.
2149
2150 keys:
2151 "team" - team that owns this generator (5 = red, 14 = blue, etc), MUST BE SET.
2152 "targetname" - name that spawnfunc_onslaught_link entities will use to target this.
2153  */
2154 spawnfunc(onslaught_generator)
2155 {
2156         if(!g_onslaught) { delete(this); return; }
2157         if(!this.team) { objerror(this, "team must be set"); }
2158
2159         ons_GeneratorSetup(this);
2160 }
2161
2162 // scoreboard setup
2163 void ons_ScoreRules()
2164 {
2165         CheckAllowedTeams(NULL);
2166         int teams = 0;
2167         if(c1 >= 0) teams |= BIT(0);
2168         if(c2 >= 0) teams |= BIT(1);
2169         if(c3 >= 0) teams |= BIT(2);
2170         if(c4 >= 0) teams |= BIT(3);
2171         GameRules_scoring(teams, SFL_SORT_PRIO_PRIMARY, 0, {
2172             field_team(ST_ONS_CAPS, "destroyed", SFL_SORT_PRIO_PRIMARY);
2173             field(SP_ONS_CAPS, "caps", SFL_SORT_PRIO_SECONDARY);
2174             field(SP_ONS_TAKES, "takes", 0);
2175         });
2176 }
2177
2178 void ons_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up
2179 {
2180         ons_ScoreRules();
2181
2182         round_handler_Spawn(Onslaught_CheckPlayers, Onslaught_CheckWinner, Onslaught_RoundStart);
2183         round_handler_Init(5, autocvar_g_onslaught_warmup, autocvar_g_onslaught_round_timelimit);
2184 }
2185
2186 void ons_Initialize()
2187 {
2188         g_onslaught = true;
2189         ons_captureshield_force = autocvar_g_onslaught_shield_force;
2190
2191         cam = new(objective_camera);
2192
2193         InitializeEntity(NULL, ons_DelayedInit, INITPRIO_GAMETYPE);
2194 }