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