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