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