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