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