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