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