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