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