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