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