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