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