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