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