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