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