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