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