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