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