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