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