Use the constants for player hitbox size when applicable (should fix observer hitbox)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_keyhunt.qc
1 #include "gamemode_keyhunt.qh"
2
3 float autocvar_g_balance_keyhunt_damageforcescale;
4 float autocvar_g_balance_keyhunt_delay_collect;
5 float autocvar_g_balance_keyhunt_delay_return;
6 float autocvar_g_balance_keyhunt_delay_round;
7 float autocvar_g_balance_keyhunt_delay_tracking;
8 float autocvar_g_balance_keyhunt_dropvelocity;
9 float autocvar_g_balance_keyhunt_maxdist;
10 float autocvar_g_balance_keyhunt_protecttime;
11
12 int autocvar_g_balance_keyhunt_score_capture;
13 int autocvar_g_balance_keyhunt_score_carrierfrag;
14 int autocvar_g_balance_keyhunt_score_collect;
15 int autocvar_g_balance_keyhunt_score_destroyed;
16 int autocvar_g_balance_keyhunt_score_destroyed_ownfactor;
17 int autocvar_g_balance_keyhunt_score_push;
18 float autocvar_g_balance_keyhunt_throwvelocity;
19
20 //int autocvar_g_keyhunt_teams;
21 int autocvar_g_keyhunt_teams_override;
22
23 // #define KH_PLAYER_USE_ATTACHMENT
24 // #define KH_PLAYER_USE_CARRIEDMODEL
25
26 #ifdef KH_PLAYER_USE_ATTACHMENT
27 const vector KH_PLAYER_ATTACHMENT_DIST_ROTATED = '0 -4 0';
28 const vector KH_PLAYER_ATTACHMENT_DIST = '4 0 0';
29 const vector KH_PLAYER_ATTACHMENT = '0 0 0';
30 const vector KH_PLAYER_ATTACHMENT_ANGLES = '0 0 0';
31 const string KH_PLAYER_ATTACHMENT_BONE = "";
32 #else
33 const float KH_KEY_ZSHIFT = 22;
34 const float KH_KEY_XYDIST = 24;
35 const float KH_KEY_XYSPEED = 45;
36 #endif
37 const float KH_KEY_WP_ZSHIFT = 20;
38
39 const vector KH_KEY_MIN = '-10 -10 -46';
40 const vector KH_KEY_MAX = '10 10 3';
41 const float KH_KEY_BRIGHTNESS = 2;
42
43 bool kh_no_radar_circles;
44
45 // kh_state
46 //     bits  0- 4: team of key 1, or 0 for no such key, or 30 for dropped, or 31 for self
47 //     bits  5- 9: team of key 2, or 0 for no such key, or 30 for dropped, or 31 for self
48 //     bits 10-14: team of key 3, or 0 for no such key, or 30 for dropped, or 31 for self
49 //     bits 15-19: team of key 4, or 0 for no such key, or 30 for dropped, or 31 for self
50 .int kh_state = _STAT(KH_KEYS);
51 .float siren_time;  //  time delay the siren
52 //.float stuff_time;  //  time delay to stuffcmd a cvar
53
54 int kh_keystatus[17];
55 //kh_keystatus[0] = status of dropped keys, kh_keystatus[1 - 16] = player #
56 //replace 17 with cvar("maxplayers") or similar !!!!!!!!!
57 //for(i = 0; i < maxplayers; ++i)
58 //      kh_keystatus[i] = "0";
59
60 int kh_Team_ByID(int t)
61 {
62         if(t == 0) return NUM_TEAM_1;
63         if(t == 1) return NUM_TEAM_2;
64         if(t == 2) return NUM_TEAM_3;
65         if(t == 3) return NUM_TEAM_4;
66         return 0;
67 }
68
69 //entity kh_worldkeylist;
70 .entity kh_worldkeynext;
71 entity kh_controller;
72 //bool kh_tracking_enabled;
73 int kh_teams;
74 int kh_interferemsg_team;
75 float kh_interferemsg_time;
76 .entity kh_next, kh_prev; // linked list
77 .float kh_droptime;
78 .int kh_dropperteam;
79 .entity kh_previous_owner;
80 .int kh_previous_owner_playerid;
81
82 int kh_key_dropped, kh_key_carried;
83
84 int kh_Key_AllOwnedByWhichTeam();
85
86 const float ST_KH_CAPS = 1;
87 void kh_ScoreRules(int teams)
88 {
89         ScoreRules_basics(teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, true);
90         ScoreInfo_SetLabel_TeamScore(  ST_KH_CAPS,      "caps",      SFL_SORT_PRIO_SECONDARY);
91         ScoreInfo_SetLabel_PlayerScore(SP_KH_CAPS,      "caps",      SFL_SORT_PRIO_SECONDARY);
92         ScoreInfo_SetLabel_PlayerScore(SP_KH_PUSHES,    "pushes",    0);
93         ScoreInfo_SetLabel_PlayerScore(SP_KH_DESTROYS,  "destroyed", SFL_LOWER_IS_BETTER);
94         ScoreInfo_SetLabel_PlayerScore(SP_KH_PICKUPS,   "pickups",   0);
95         ScoreInfo_SetLabel_PlayerScore(SP_KH_KCKILLS,   "kckills",   0);
96         ScoreInfo_SetLabel_PlayerScore(SP_KH_LOSSES,    "losses",    SFL_LOWER_IS_BETTER);
97         ScoreRules_basics_end();
98 }
99
100 bool kh_KeyCarrier_waypointsprite_visible_for_player(entity this, entity player, entity view)  // runs all the time
101 {
102         if(!IS_PLAYER(view) || DIFF_TEAM(this, view))
103                 if(!kh_tracking_enabled)
104                         return false;
105
106         return true;
107 }
108
109 bool kh_Key_waypointsprite_visible_for_player(entity this, entity player, entity view)
110 {
111         if(!kh_tracking_enabled)
112                 return false;
113         if(!this.owner)
114                 return true;
115         if(!this.owner.owner)
116                 return true;
117         return false;  // draw only when key is not owned
118 }
119
120 void kh_update_state()
121 {
122         entity key;
123         int f;
124         int s = 0;
125         FOR_EACH_KH_KEY(key)
126         {
127                 if(key.owner)
128                         f = key.team;
129                 else
130                         f = 30;
131                 s |= pow(32, key.count) * f;
132         }
133
134         FOREACH_CLIENT(true, LAMBDA(it.kh_state = s));
135
136         FOR_EACH_KH_KEY(key)
137         {
138                 if(key.owner)
139                         key.owner.kh_state |= pow(32, key.count) * 31;
140         }
141         //print(ftos((nextent(NULL)).kh_state), "\n");
142 }
143
144
145
146
147 var kh_Think_t kh_Controller_Thinkfunc;
148 void kh_Controller_SetThink(float t, kh_Think_t func)  // runs occasionaly
149 {
150         kh_Controller_Thinkfunc = func;
151         kh_controller.cnt = ceil(t);
152         if(t == 0)
153                 kh_controller.nextthink = time; // force
154 }
155 void kh_WaitForPlayers();
156 void kh_Controller_Think(entity this)  // called a lot
157 {
158         if(gameover)
159                 return;
160         if(this.cnt > 0)
161         {
162                 if(getthink(this) != kh_WaitForPlayers)
163                         this.cnt -= 1;
164         }
165         else if(this.cnt == 0)
166         {
167                 this.cnt -= 1;
168                 kh_Controller_Thinkfunc();
169         }
170         this.nextthink = time + 1;
171 }
172
173 // frags f: take from cvar * f
174 // frags 0: no frags
175 void kh_Scores_Event(entity player, entity key, string what, float frags_player, float frags_owner)  // update the score when a key is captured
176 {
177         string s;
178         if(gameover)
179                 return;
180
181         if(frags_player)
182                 UpdateFrags(player, frags_player);
183
184         if(key && key.owner && frags_owner)
185                 UpdateFrags(key.owner, frags_owner);
186
187         if(!autocvar_sv_eventlog)  //output extra info to the console or text file
188                 return;
189
190         s = strcat(":keyhunt:", what, ":", ftos(player.playerid), ":", ftos(frags_player));
191
192         if(key && key.owner)
193                 s = strcat(s, ":", ftos(key.owner.playerid));
194         else
195                 s = strcat(s, ":0");
196
197         s = strcat(s, ":", ftos(frags_owner), ":");
198
199         if(key)
200                 s = strcat(s, key.netname);
201
202         GameLogEcho(s);
203 }
204
205 vector kh_AttachedOrigin(entity e)  // runs when a team captures the flag, it can run 2 or 3 times.
206 {
207         if(e.tag_entity)
208         {
209                 makevectors(e.tag_entity.angles);
210                 return e.tag_entity.origin + e.origin.x * v_forward - e.origin.y * v_right + e.origin.z * v_up;
211         }
212         else
213                 return e.origin;
214 }
215
216 void kh_Key_Attach(entity key)  // runs when a player picks up a key and several times when a key is assigned to a player at the start of a round
217 {
218 #ifdef KH_PLAYER_USE_ATTACHMENT
219         entity first = key.owner.kh_next;
220         if(key == first)
221         {
222                 setattachment(key, key.owner, KH_PLAYER_ATTACHMENT_BONE);
223                 if(key.kh_next)
224                 {
225                         setattachment(key.kh_next, key, "");
226                         setorigin(key, key.kh_next.origin - 0.5 * KH_PLAYER_ATTACHMENT_DIST);
227                         setorigin(key.kh_next, KH_PLAYER_ATTACHMENT_DIST_ROTATED);
228                         key.kh_next.angles = '0 0 0';
229                 }
230                 else
231                         setorigin(key, KH_PLAYER_ATTACHMENT);
232                 key.angles = KH_PLAYER_ATTACHMENT_ANGLES;
233         }
234         else
235         {
236                 setattachment(key, key.kh_prev, "");
237                 if(key.kh_next)
238                         setattachment(key.kh_next, key, "");
239                 setorigin(key, KH_PLAYER_ATTACHMENT_DIST_ROTATED);
240                 setorigin(first, first.origin - 0.5 * KH_PLAYER_ATTACHMENT_DIST);
241                 key.angles = '0 0 0';
242         }
243 #else
244         setattachment(key, key.owner, "");
245         setorigin(key, '0 0 1' * KH_KEY_ZSHIFT);  // fixing x, y in think
246         key.angles_y -= key.owner.angles.y;
247 #endif
248         key.flags = 0;
249         IL_REMOVE(g_items, key);
250         key.solid = SOLID_NOT;
251         set_movetype(key, MOVETYPE_NONE);
252         key.team = key.owner.team;
253         key.nextthink = time;
254         key.damageforcescale = 0;
255         key.takedamage = DAMAGE_NO;
256         key.modelindex = kh_key_carried;
257 }
258
259 void kh_Key_Detach(entity key) // runs every time a key is dropped or lost. Runs several times times when all the keys are captured
260 {
261 #ifdef KH_PLAYER_USE_ATTACHMENT
262         entity first = key.owner.kh_next;
263         if(key == first)
264         {
265                 if(key.kh_next)
266                 {
267                         setattachment(key.kh_next, key.owner, KH_PLAYER_ATTACHMENT_BONE);
268                         setorigin(key.kh_next, key.origin + 0.5 * KH_PLAYER_ATTACHMENT_DIST);
269                         key.kh_next.angles = KH_PLAYER_ATTACHMENT_ANGLES;
270                 }
271         }
272         else
273         {
274                 if(key.kh_next)
275                         setattachment(key.kh_next, key.kh_prev, "");
276                 setorigin(first, first.origin + 0.5 * KH_PLAYER_ATTACHMENT_DIST);
277         }
278         // in any case:
279         setattachment(key, NULL, "");
280         setorigin(key, key.owner.origin + '0 0 1' * (STAT(PL_MIN, key.owner).z - KH_KEY_MIN_z));
281         key.angles = key.owner.angles;
282 #else
283         setorigin(key, key.owner.origin + key.origin.z * '0 0 1');
284         setattachment(key, NULL, "");
285         key.angles_y += key.owner.angles.y;
286 #endif
287         key.flags = FL_ITEM;
288         IL_PUSH(g_items, key);
289         key.solid = SOLID_TRIGGER;
290         set_movetype(key, MOVETYPE_TOSS);
291         key.pain_finished = time + autocvar_g_balance_keyhunt_delay_return;
292         key.damageforcescale = autocvar_g_balance_keyhunt_damageforcescale;
293         key.takedamage = DAMAGE_YES;
294         // let key.team stay
295         key.modelindex = kh_key_dropped;
296         key.kh_previous_owner = key.owner;
297         key.kh_previous_owner_playerid = key.owner.playerid;
298 }
299
300 void kh_Key_AssignTo(entity key, entity player)  // runs every time a key is picked up or assigned. Runs prior to kh_key_attach
301 {
302         if(key.owner == player)
303                 return;
304
305         int ownerteam0 = kh_Key_AllOwnedByWhichTeam();
306
307         if(key.owner)
308         {
309                 kh_Key_Detach(key);
310
311                 // remove from linked list
312                 if(key.kh_next)
313                         key.kh_next.kh_prev = key.kh_prev;
314                 key.kh_prev.kh_next = key.kh_next;
315                 key.kh_next = NULL;
316                 key.kh_prev = NULL;
317
318                 if(key.owner.kh_next == NULL)
319                 {
320                         // No longer a key carrier
321                         if(!kh_no_radar_circles)
322                                 WaypointSprite_Ping(key.owner.waypointsprite_attachedforcarrier);
323                         WaypointSprite_DetachCarrier(key.owner);
324                 }
325         }
326
327         key.owner = player;
328
329         if(player)
330         {
331                 // insert into linked list
332                 key.kh_next = player.kh_next;
333                 key.kh_prev = player;
334                 player.kh_next = key;
335                 if(key.kh_next)
336                         key.kh_next.kh_prev = key;
337
338                 float i;
339                 i = kh_keystatus[key.owner.playerid];
340                         if(key.netname == "^1red key")
341                                 i += 1;
342                         if(key.netname == "^4blue key")
343                                 i += 2;
344                         if(key.netname == "^3yellow key")
345                                 i += 4;
346                         if(key.netname == "^6pink key")
347                                 i += 8;
348                 kh_keystatus[key.owner.playerid] = i;
349
350                 kh_Key_Attach(key);
351
352                 if(key.kh_next == NULL)
353                 {
354                         // player is now a key carrier
355                         entity wp = WaypointSprite_AttachCarrier(WP_Null, player, RADARICON_FLAGCARRIER);
356                         wp.colormod = colormapPaletteColor(player.team - 1, 0);
357                         player.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = kh_KeyCarrier_waypointsprite_visible_for_player;
358                         WaypointSprite_UpdateRule(player.waypointsprite_attachedforcarrier, player.team, SPRITERULE_TEAMPLAY);
359                         if(player.team == NUM_TEAM_1)
360                                 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierRed, WP_KeyCarrierFriend, WP_KeyCarrierRed);
361                         else if(player.team == NUM_TEAM_2)
362                                 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierBlue, WP_KeyCarrierFriend, WP_KeyCarrierBlue);
363                         else if(player.team == NUM_TEAM_3)
364                                 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierYellow, WP_KeyCarrierFriend, WP_KeyCarrierYellow);
365                         else if(player.team == NUM_TEAM_4)
366                                 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierPink, WP_KeyCarrierFriend, WP_KeyCarrierPink);
367                         if(!kh_no_radar_circles)
368                                 WaypointSprite_Ping(player.waypointsprite_attachedforcarrier);
369                 }
370         }
371
372         // moved that here, also update if there's no player
373         kh_update_state();
374
375         key.pusher = NULL;
376
377         int ownerteam = kh_Key_AllOwnedByWhichTeam();
378         if(ownerteam != ownerteam0)
379         {
380                 entity k;
381                 if(ownerteam != -1)
382                 {
383                         kh_interferemsg_time = time + 0.2;
384                         kh_interferemsg_team = player.team;
385
386                         // audit all key carrier sprites, update them to "Run here"
387                         FOR_EACH_KH_KEY(k)
388                         {
389                                 if (!k.owner) continue;
390                                 entity first = WP_Null;
391                                 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model1, LAMBDA(first = it; break));
392                                 entity third = WP_Null;
393                                 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model3, LAMBDA(third = it; break));
394                                 WaypointSprite_UpdateSprites(k.owner.waypointsprite_attachedforcarrier, first, WP_KeyCarrierFinish, third);
395                         }
396                 }
397                 else
398                 {
399                         kh_interferemsg_time = 0;
400
401                         // audit all key carrier sprites, update them to "Key Carrier"
402                         FOR_EACH_KH_KEY(k)
403                         {
404                                 if (!k.owner) continue;
405                                 entity first = WP_Null;
406                                 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model1, LAMBDA(first = it; break));
407                                 entity third = WP_Null;
408                                 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model3, LAMBDA(third = it; break));
409                                 WaypointSprite_UpdateSprites(k.owner.waypointsprite_attachedforcarrier, first, WP_KeyCarrierFriend, third);
410                         }
411                 }
412         }
413 }
414
415 void kh_Key_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
416 {
417         if(this.owner)
418                 return;
419         if(ITEM_DAMAGE_NEEDKILL(deathtype))
420         {
421                 // touching lava, or hurt trigger
422                 // what shall we do?
423                 // immediately return is bad
424                 // maybe start a shorter countdown?
425         }
426         if(force == '0 0 0')
427                 return;
428         if(time > this.pushltime)
429                 if(IS_PLAYER(attacker))
430                         this.team = attacker.team;
431 }
432
433 void kh_Key_Collect(entity key, entity player)  //a player picks up a dropped key
434 {
435         sound(player, CH_TRIGGER, SND_KH_COLLECT, VOL_BASE, ATTEN_NORM);
436
437         if(key.kh_dropperteam != player.team)
438         {
439                 kh_Scores_Event(player, key, "collect", autocvar_g_balance_keyhunt_score_collect, 0);
440                 PlayerScore_Add(player, SP_KH_PICKUPS, 1);
441         }
442         key.kh_dropperteam = 0;
443         int realteam = kh_Team_ByID(key.count);
444         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_PICKUP), player.netname);
445
446         kh_Key_AssignTo(key, player); // this also updates .kh_state
447 }
448
449 void kh_Key_Touch(entity this, entity toucher)  // runs many, many times when a key has been dropped and can be picked up
450 {
451         if(gameover)
452                 return;
453
454         if(this.owner) // already carried
455                 return;
456
457         if(ITEM_TOUCH_NEEDKILL())
458         {
459                 // touching sky, or nodrop
460                 // what shall we do?
461                 // immediately return is bad
462                 // maybe start a shorter countdown?
463         }
464
465         if (!IS_PLAYER(toucher))
466                 return;
467         if(IS_DEAD(toucher))
468                 return;
469         if(toucher == this.enemy)
470                 if(time < this.kh_droptime + autocvar_g_balance_keyhunt_delay_collect)
471                         return;  // you just dropped it!
472         kh_Key_Collect(this, toucher);
473 }
474
475 void kh_Key_Remove(entity key)  // runs after when all the keys have been collected or when a key has been dropped for more than X seconds
476 {
477         entity o = key.owner;
478         kh_Key_AssignTo(key, NULL);
479         if(o) // it was attached
480                 WaypointSprite_Kill(key.waypointsprite_attachedforcarrier);
481         else // it was dropped
482                 WaypointSprite_DetachCarrier(key);
483
484         // remove key from key list
485         if (kh_worldkeylist == key)
486                 kh_worldkeylist = kh_worldkeylist.kh_worldkeynext;
487         else
488         {
489                 o = kh_worldkeylist;
490                 while (o)
491                 {
492                         if (o.kh_worldkeynext == key)
493                         {
494                                 o.kh_worldkeynext = o.kh_worldkeynext.kh_worldkeynext;
495                                 break;
496                         }
497                         o = o.kh_worldkeynext;
498                 }
499         }
500
501         delete(key);
502
503         kh_update_state();
504 }
505
506 void kh_FinishRound()  // runs when a team captures the keys
507 {
508         // prepare next round
509         kh_interferemsg_time = 0;
510         entity key;
511
512         kh_no_radar_circles = true;
513         FOR_EACH_KH_KEY(key)
514                 kh_Key_Remove(key);
515         kh_no_radar_circles = false;
516
517         Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_ROUNDSTART, autocvar_g_balance_keyhunt_delay_round);
518         kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round, kh_StartRound);
519 }
520
521 void nades_GiveBonus(entity player, float score);
522
523 void kh_WinnerTeam(int winner_team)  // runs when a team wins
524 {
525         // all key carriers get some points
526         entity key;
527         float score = (NumTeams(kh_teams) - 1) * autocvar_g_balance_keyhunt_score_capture;
528         DistributeEvenly_Init(score, NumTeams(kh_teams));
529         // twice the score for 3 team games, three times the score for 4 team games!
530         // note: for a win by destroying the key, this should NOT be applied
531         FOR_EACH_KH_KEY(key)
532         {
533                 float f = DistributeEvenly_Get(1);
534                 kh_Scores_Event(key.owner, key, "capture", f, 0);
535                 PlayerTeamScore_Add(key.owner, SP_KH_CAPS, ST_KH_CAPS, 1);
536                 nades_GiveBonus(key.owner, autocvar_g_nades_bonus_score_high);
537         }
538
539         bool first = true;
540         string keyowner = "";
541         FOR_EACH_KH_KEY(key)
542                 if(key.owner.kh_next == key)
543                 {
544                         if(!first)
545                                 keyowner = strcat(keyowner, ", ");
546                         keyowner = key.owner.netname;
547                         first = false;
548                 }
549
550         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_KEYHUNT_CAPTURE), keyowner);
551
552         first = true;
553         vector firstorigin = '0 0 0', lastorigin = '0 0 0', midpoint = '0 0 0';
554         FOR_EACH_KH_KEY(key)
555         {
556                 vector thisorigin = kh_AttachedOrigin(key);
557                 //dprint("Key origin: ", vtos(thisorigin), "\n");
558                 midpoint += thisorigin;
559
560                 if(!first)
561                         te_lightning2(NULL, lastorigin, thisorigin);
562                 lastorigin = thisorigin;
563                 if(first)
564                         firstorigin = thisorigin;
565                 first = false;
566         }
567         if(NumTeams(kh_teams) > 2)
568         {
569                 te_lightning2(NULL, lastorigin, firstorigin);
570         }
571         midpoint = midpoint * (1 / NumTeams(kh_teams));
572         te_customflash(midpoint, 1000, 1, Team_ColorRGB(winner_team) * 0.5 + '0.5 0.5 0.5');  // make the color >=0.5 in each component
573
574         play2all(SND(KH_CAPTURE));
575         kh_FinishRound();
576 }
577
578 void kh_LoserTeam(int loser_team, entity lostkey)  // runs when a player pushes a flag carrier off the map
579 {
580         float f;
581         entity attacker = NULL;
582         if(lostkey.pusher)
583                 if(lostkey.pusher.team != loser_team)
584                         if(IS_PLAYER(lostkey.pusher))
585                                 attacker = lostkey.pusher;
586
587         if(attacker)
588         {
589                 if(lostkey.kh_previous_owner)
590                         kh_Scores_Event(lostkey.kh_previous_owner, NULL, "pushed", 0, -autocvar_g_balance_keyhunt_score_push);
591                         // don't actually GIVE him the -nn points, just log
592                 kh_Scores_Event(attacker, NULL, "push", autocvar_g_balance_keyhunt_score_push, 0);
593                 PlayerScore_Add(attacker, SP_KH_PUSHES, 1);
594                 //centerprint(attacker, "Your push is the best!"); // does this really need to exist?
595         }
596         else
597         {
598                 int players = 0;
599                 float of = autocvar_g_balance_keyhunt_score_destroyed_ownfactor;
600
601                 FOREACH_CLIENT(IS_PLAYER(it) && it.team != loser_team, LAMBDA(++players));
602
603                 entity key;
604                 int keys = 0;
605                 FOR_EACH_KH_KEY(key)
606                         if(key.owner && key.team != loser_team)
607                                 ++keys;
608
609                 if(lostkey.kh_previous_owner)
610                         kh_Scores_Event(lostkey.kh_previous_owner, NULL, "destroyed", 0, -autocvar_g_balance_keyhunt_score_destroyed);
611                         // don't actually GIVE him the -nn points, just log
612
613                 if(lostkey.kh_previous_owner.playerid == lostkey.kh_previous_owner_playerid)
614                         PlayerScore_Add(lostkey.kh_previous_owner, SP_KH_DESTROYS, 1);
615
616                 DistributeEvenly_Init(autocvar_g_balance_keyhunt_score_destroyed, keys * of + players);
617
618                 FOR_EACH_KH_KEY(key)
619                         if(key.owner && key.team != loser_team)
620                         {
621                                 f = DistributeEvenly_Get(of);
622                                 kh_Scores_Event(key.owner, NULL, "destroyed_holdingkey", f, 0);
623                         }
624
625                 int fragsleft = DistributeEvenly_Get(players);
626
627                 // Now distribute these among all other teams...
628                 int j = NumTeams(kh_teams) - 1;
629                 for(int i = 0; i < NumTeams(kh_teams); ++i)
630                 {
631                         int thisteam = kh_Team_ByID(i);
632                         if(thisteam == loser_team) // bad boy, no cookie - this WILL happen
633                                 continue;
634
635                         players = 0;
636                         FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, LAMBDA(++players));
637
638                         DistributeEvenly_Init(fragsleft, j);
639                         fragsleft = DistributeEvenly_Get(j - 1);
640                         DistributeEvenly_Init(DistributeEvenly_Get(1), players);
641
642                         FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, LAMBDA(
643                                 f = DistributeEvenly_Get(1);
644                                 kh_Scores_Event(it, NULL, "destroyed", f, 0);
645                         ));
646
647                         --j;
648                 }
649         }
650
651         int realteam = kh_Team_ByID(lostkey.count);
652         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_LOST), lostkey.kh_previous_owner.netname);
653
654         play2all(SND(KH_DESTROY));
655         te_tarexplosion(lostkey.origin);
656
657         kh_FinishRound();
658 }
659
660 void kh_Key_Think(entity this)  // runs all the time
661 {
662         if(gameover)
663                 return;
664
665         if(this.owner)
666         {
667 #ifndef KH_PLAYER_USE_ATTACHMENT
668                 makevectors('0 1 0' * (this.cnt + (time % 360) * KH_KEY_XYSPEED));
669                 setorigin(this, v_forward * KH_KEY_XYDIST + '0 0 1' * this.origin.z);
670 #endif
671         }
672
673         // if in nodrop or time over, end the round
674         if(!this.owner)
675                 if(time > this.pain_finished)
676                         kh_LoserTeam(this.team, this);
677
678         if(this.owner)
679         if(kh_Key_AllOwnedByWhichTeam() != -1)
680         {
681                 if(this.siren_time < time)
682                 {
683                         sound(this.owner, CH_TRIGGER, SND_KH_ALARM, VOL_BASE, ATTEN_NORM);  // play a simple alarm
684                         this.siren_time = time + 2.5;  // repeat every 2.5 seconds
685                 }
686
687                 entity key;
688                 vector p = this.owner.origin;
689                 FOR_EACH_KH_KEY(key)
690                         if(vdist(key.owner.origin - p, >, autocvar_g_balance_keyhunt_maxdist))
691                                 goto not_winning;
692                 kh_WinnerTeam(this.team);
693 LABEL(not_winning)
694         }
695
696         if(kh_interferemsg_time && time > kh_interferemsg_time)
697         {
698                 kh_interferemsg_time = 0;
699                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
700                         if(it.team == kh_interferemsg_team)
701                                 if(it.kh_next)
702                                         Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_MEET);
703                                 else
704                                         Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_HELP);
705                         else
706                                 Send_Notification(NOTIF_ONE, it, MSG_CENTER, APP_TEAM_NUM(kh_interferemsg_team, CENTER_KEYHUNT_INTERFERE));
707                 ));
708         }
709
710         this.nextthink = time + 0.05;
711 }
712
713 void key_reset(entity this)
714 {
715         kh_Key_AssignTo(this, NULL);
716         kh_Key_Remove(this);
717 }
718
719 const string STR_ITEM_KH_KEY = "item_kh_key";
720 void kh_Key_Spawn(entity initial_owner, float _angle, float i)  // runs every time a new flag is created, ie after all the keys have been collected
721 {
722         entity key = spawn();
723         key.count = i;
724         key.classname = STR_ITEM_KH_KEY;
725         settouch(key, kh_Key_Touch);
726         setthink(key, kh_Key_Think);
727         key.nextthink = time;
728         key.items = IT_KEY1 | IT_KEY2;
729         key.cnt = _angle;
730         key.angles = '0 360 0' * random();
731         key.event_damage = kh_Key_Damage;
732         key.takedamage = DAMAGE_YES;
733         key.modelindex = kh_key_dropped;
734         key.model = "key";
735         key.kh_dropperteam = 0;
736         key.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
737         setsize(key, KH_KEY_MIN, KH_KEY_MAX);
738         key.colormod = Team_ColorRGB(initial_owner.team) * KH_KEY_BRIGHTNESS;
739         key.reset = key_reset;
740
741         switch(initial_owner.team)
742         {
743                 case NUM_TEAM_1:
744                         key.netname = "^1red key";
745                         break;
746                 case NUM_TEAM_2:
747                         key.netname = "^4blue key";
748                         break;
749                 case NUM_TEAM_3:
750                         key.netname = "^3yellow key";
751                         break;
752                 case NUM_TEAM_4:
753                         key.netname = "^6pink key";
754                         break;
755                 default:
756                         key.netname = "NETGIER key";
757                         break;
758         }
759
760         // link into key list
761         key.kh_worldkeynext = kh_worldkeylist;
762         kh_worldkeylist = key;
763
764         Send_Notification(NOTIF_ONE, initial_owner, MSG_CENTER, APP_TEAM_NUM(initial_owner.team, CENTER_KEYHUNT_START));
765
766         WaypointSprite_Spawn(WP_KeyDropped, 0, 0, key, '0 0 1' * KH_KEY_WP_ZSHIFT, NULL, key.team, key, waypointsprite_attachedforcarrier, false, RADARICON_FLAG);
767         key.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = kh_Key_waypointsprite_visible_for_player;
768
769         kh_Key_AssignTo(key, initial_owner);
770 }
771
772 // -1 when no team completely owns all keys yet
773 int kh_Key_AllOwnedByWhichTeam()  // constantly called. check to see if all the keys are owned by the same team
774 {
775         entity key;
776         int teem = -1;
777         int keys = NumTeams(kh_teams);
778         FOR_EACH_KH_KEY(key)
779         {
780                 if(!key.owner)
781                         return -1;
782                 if(teem == -1)
783                         teem = key.team;
784                 else if(teem != key.team)
785                         return -1;
786                 --keys;
787         }
788         if(keys != 0)
789                 return -1;
790         return teem;
791 }
792
793 void kh_Key_DropOne(entity key)
794 {
795         // prevent collecting this one for some time
796         entity player = key.owner;
797
798         key.kh_droptime = time;
799         key.enemy = player;
800
801         kh_Scores_Event(player, key, "dropkey", 0, 0);
802         PlayerScore_Add(player, SP_KH_LOSSES, 1);
803         int realteam = kh_Team_ByID(key.count);
804         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_DROP), player.netname);
805
806         kh_Key_AssignTo(key, NULL);
807         makevectors(player.v_angle);
808         key.velocity = W_CalculateProjectileVelocity(player, player.velocity, autocvar_g_balance_keyhunt_throwvelocity * v_forward, false);
809         key.pusher = NULL;
810         key.pushltime = time + autocvar_g_balance_keyhunt_protecttime;
811         key.kh_dropperteam = key.team;
812
813         sound(player, CH_TRIGGER, SND_KH_DROP, VOL_BASE, ATTEN_NORM);
814 }
815
816 void kh_Key_DropAll(entity player, float suicide) // runs whenever a player dies
817 {
818         if(player.kh_next)
819         {
820                 entity mypusher = NULL;
821                 if(player.pusher)
822                         if(time < player.pushltime)
823                                 mypusher = player.pusher;
824
825                 entity key;
826                 while((key = player.kh_next))
827                 {
828                         kh_Scores_Event(player, key, "losekey", 0, 0);
829                         PlayerScore_Add(player, SP_KH_LOSSES, 1);
830                         int realteam = kh_Team_ByID(key.count);
831                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_LOST), player.netname);
832                         kh_Key_AssignTo(key, NULL);
833                         makevectors('-1 0 0' * (45 + 45 * random()) + '0 360 0' * random());
834                         key.velocity = W_CalculateProjectileVelocity(player, player.velocity, autocvar_g_balance_keyhunt_dropvelocity * v_forward, false);
835                         key.pusher = mypusher;
836                         key.pushltime = time + autocvar_g_balance_keyhunt_protecttime;
837                         if(suicide)
838                                 key.kh_dropperteam = player.team;
839                 }
840                 sound(player, CH_TRIGGER, SND_KH_DROP, VOL_BASE, ATTEN_NORM);
841         }
842 }
843
844 int kh_GetMissingTeams()
845 {
846         int missing_teams = 0;
847         for(int i = 0; i < NumTeams(kh_teams); ++i)
848         {
849                 int teem = kh_Team_ByID(i);
850                 int players = 0;
851                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
852                         if(!IS_DEAD(it) && !PHYS_INPUT_BUTTON_CHAT(it) && it.team == teem)
853                                 ++players;
854                 ));
855                 if (!players)
856                         missing_teams |= pow(2, i);
857         }
858         return missing_teams;
859 }
860
861 void kh_WaitForPlayers()  // delay start of the round until enough players are present
862 {
863         if(time < game_starttime)
864         {
865                 kh_Controller_SetThink(game_starttime - time + 0.1, kh_WaitForPlayers);
866                 return;
867         }
868
869         static int prev_missing_teams_mask;
870         int missing_teams_mask = kh_GetMissingTeams();
871         if(!missing_teams_mask)
872         {
873                 if(prev_missing_teams_mask > 0)
874                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
875                 prev_missing_teams_mask = -1;
876                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_ROUNDSTART, autocvar_g_balance_keyhunt_delay_round);
877                 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round, kh_StartRound);
878         }
879         else
880         {
881                 if(player_count == 0)
882                 {
883                         if(prev_missing_teams_mask > 0)
884                                 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
885                         prev_missing_teams_mask = -1;
886                 }
887                 else
888                 {
889                         if(prev_missing_teams_mask != missing_teams_mask)
890                         {
891                                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
892                                 prev_missing_teams_mask = missing_teams_mask;
893                         }
894                 }
895                 kh_Controller_SetThink(1, kh_WaitForPlayers);
896         }
897 }
898
899 void kh_EnableTrackingDevice()  // runs after each round
900 {
901         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT);
902         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT_OTHER);
903
904         kh_tracking_enabled = true;
905 }
906
907 void kh_StartRound()  // runs at the start of each round
908 {
909         if(time < game_starttime)
910         {
911                 kh_Controller_SetThink(game_starttime - time + 0.1, kh_WaitForPlayers);
912                 return;
913         }
914
915         if(kh_GetMissingTeams())
916         {
917                 kh_Controller_SetThink(1, kh_WaitForPlayers);
918                 return;
919         }
920
921         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT);
922         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT_OTHER);
923
924         for(int i = 0; i < NumTeams(kh_teams); ++i)
925         {
926                 int teem = kh_Team_ByID(i);
927                 int players = 0;
928                 entity my_player = NULL;
929                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
930                         if(!IS_DEAD(it) && !PHYS_INPUT_BUTTON_CHAT(it) && it.team == teem)
931                         {
932                                 ++players;
933                                 if(random() * players <= 1)
934                                         my_player = it;
935                         }
936                 ));
937                 kh_Key_Spawn(my_player, 360 * i / NumTeams(kh_teams), i);
938         }
939
940         kh_tracking_enabled = false;
941         Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_SCAN, autocvar_g_balance_keyhunt_delay_tracking);
942         kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_tracking, kh_EnableTrackingDevice);
943 }
944
945 float kh_HandleFrags(entity attacker, entity targ, float f)  // adds to the player score
946 {
947         if(attacker == targ)
948                 return f;
949
950         if(targ.kh_next)
951         {
952                 if(attacker.team == targ.team)
953                 {
954                         int nk = 0;
955                         for(entity k = targ.kh_next; k != NULL; k = k.kh_next)
956                                 ++nk;
957                         kh_Scores_Event(attacker, targ.kh_next, "carrierfrag", -nk * autocvar_g_balance_keyhunt_score_collect, 0);
958                 }
959                 else
960                 {
961                         kh_Scores_Event(attacker, targ.kh_next, "carrierfrag", autocvar_g_balance_keyhunt_score_carrierfrag-1, 0);
962                         PlayerScore_Add(attacker, SP_KH_KCKILLS, 1);
963                         // the frag gets added later
964                 }
965         }
966
967         return f;
968 }
969
970 void kh_Initialize()  // sets up th KH environment
971 {
972         // setup variables
973         kh_teams = autocvar_g_keyhunt_teams_override;
974         if(kh_teams < 2)
975                 kh_teams = cvar("g_keyhunt_teams"); // read the cvar directly as it gets written earlier in the same frame
976         kh_teams = bound(2, kh_teams, 4);
977
978         int teams = 0;
979         if(kh_teams >= 1) teams |= BIT(0);
980         if(kh_teams >= 2) teams |= BIT(1);
981         if(kh_teams >= 3) teams |= BIT(2);
982         if(kh_teams >= 4) teams |= BIT(3);
983
984         kh_teams = teams; // now set it?
985
986         // make a KH entity for controlling the game
987         kh_controller = spawn();
988         setthink(kh_controller, kh_Controller_Think);
989         kh_Controller_SetThink(0, kh_WaitForPlayers);
990
991         setmodel(kh_controller, MDL_KH_KEY);
992         kh_key_dropped = kh_controller.modelindex;
993         /*
994         dprint(vtos(kh_controller.mins));
995         dprint(vtos(kh_controller.maxs));
996         dprint("\n");
997         */
998 #ifdef KH_PLAYER_USE_CARRIEDMODEL
999         setmodel(kh_controller, MDL_KH_KEY_CARRIED);
1000         kh_key_carried = kh_controller.modelindex;
1001 #else
1002         kh_key_carried = kh_key_dropped;
1003 #endif
1004
1005         kh_controller.model = "";
1006         kh_controller.modelindex = 0;
1007
1008         kh_ScoreRules(kh_teams);
1009 }
1010
1011 void kh_finalize()
1012 {
1013         // to be called before intermission
1014         kh_FinishRound();
1015         delete(kh_controller);
1016         kh_controller = NULL;
1017 }
1018
1019 // legacy bot role
1020
1021 void(entity this) havocbot_role_kh_carrier;
1022 void(entity this) havocbot_role_kh_defense;
1023 void(entity this) havocbot_role_kh_offense;
1024 void(entity this) havocbot_role_kh_freelancer;
1025
1026
1027 void havocbot_goalrating_kh(entity this, float ratingscale_team, float ratingscale_dropped, float ratingscale_enemy)
1028 {
1029         entity head;
1030         for (head = kh_worldkeylist; head; head = head.kh_worldkeynext)
1031         {
1032                 if(head.owner == this)
1033                         continue;
1034                 if(!kh_tracking_enabled)
1035                 {
1036                         // if it's carried by our team we know about it
1037                         // otherwise we have to see it to know about it
1038                         if(!head.owner || head.team != this.team)
1039                         {
1040                                 traceline(this.origin + this.view_ofs, head.origin, MOVE_NOMONSTERS, this);
1041                                 if (trace_fraction < 1 && trace_ent != head)
1042                                         continue; // skip what I can't see
1043                         }
1044                 }
1045                 if(!head.owner)
1046                         navigation_routerating(this, head, ratingscale_dropped * BOT_PICKUP_RATING_HIGH, 100000);
1047                 else if(head.team == this.team)
1048                         navigation_routerating(this, head.owner, ratingscale_team * BOT_PICKUP_RATING_HIGH, 100000);
1049                 else
1050                         navigation_routerating(this, head.owner, ratingscale_enemy * BOT_PICKUP_RATING_HIGH, 100000);
1051         }
1052
1053         havocbot_goalrating_items(this, 1, this.origin, 10000);
1054 }
1055
1056 void havocbot_role_kh_carrier(entity this)
1057 {
1058         if(IS_DEAD(this))
1059                 return;
1060
1061         if (!(this.kh_next))
1062         {
1063                 LOG_TRACE("changing role to freelancer");
1064                 this.havocbot_role = havocbot_role_kh_freelancer;
1065                 this.havocbot_role_timeout = 0;
1066                 return;
1067         }
1068
1069         if (this.bot_strategytime < time)
1070         {
1071                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1072                 navigation_goalrating_start(this);
1073
1074                 if(kh_Key_AllOwnedByWhichTeam() == this.team)
1075                         havocbot_goalrating_kh(this, 10, 0.1, 0.1); // bring home
1076                 else
1077                         havocbot_goalrating_kh(this, 4, 4, 1); // play defensively
1078
1079                 navigation_goalrating_end(this);
1080         }
1081 }
1082
1083 void havocbot_role_kh_defense(entity this)
1084 {
1085         if(IS_DEAD(this))
1086                 return;
1087
1088         if (this.kh_next)
1089         {
1090                 LOG_TRACE("changing role to carrier");
1091                 this.havocbot_role = havocbot_role_kh_carrier;
1092                 this.havocbot_role_timeout = 0;
1093                 return;
1094         }
1095
1096         if (!this.havocbot_role_timeout)
1097                 this.havocbot_role_timeout = time + random() * 10 + 20;
1098         if (time > this.havocbot_role_timeout)
1099         {
1100                 LOG_TRACE("changing role to freelancer");
1101                 this.havocbot_role = havocbot_role_kh_freelancer;
1102                 this.havocbot_role_timeout = 0;
1103                 return;
1104         }
1105
1106         if (this.bot_strategytime < time)
1107         {
1108                 float key_owner_team;
1109                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1110                 navigation_goalrating_start(this);
1111
1112                 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1113                 if(key_owner_team == this.team)
1114                         havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend key carriers
1115                 else if(key_owner_team == -1)
1116                         havocbot_goalrating_kh(this, 4, 1, 0.1); // play defensively
1117                 else
1118                         havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK ANYWAY
1119
1120                 navigation_goalrating_end(this);
1121         }
1122 }
1123
1124 void havocbot_role_kh_offense(entity this)
1125 {
1126         if(IS_DEAD(this))
1127                 return;
1128
1129         if (this.kh_next)
1130         {
1131                 LOG_TRACE("changing role to carrier");
1132                 this.havocbot_role = havocbot_role_kh_carrier;
1133                 this.havocbot_role_timeout = 0;
1134                 return;
1135         }
1136
1137         if (!this.havocbot_role_timeout)
1138                 this.havocbot_role_timeout = time + random() * 10 + 20;
1139         if (time > this.havocbot_role_timeout)
1140         {
1141                 LOG_TRACE("changing role to freelancer");
1142                 this.havocbot_role = havocbot_role_kh_freelancer;
1143                 this.havocbot_role_timeout = 0;
1144                 return;
1145         }
1146
1147         if (this.bot_strategytime < time)
1148         {
1149                 float key_owner_team;
1150
1151                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1152                 navigation_goalrating_start(this);
1153
1154                 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1155                 if(key_owner_team == this.team)
1156                         havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend anyway
1157                 else if(key_owner_team == -1)
1158                         havocbot_goalrating_kh(this, 0.1, 1, 4); // play offensively
1159                 else
1160                         havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK! EMERGENCY!
1161
1162                 navigation_goalrating_end(this);
1163         }
1164 }
1165
1166 void havocbot_role_kh_freelancer(entity this)
1167 {
1168         if(IS_DEAD(this))
1169                 return;
1170
1171         if (this.kh_next)
1172         {
1173                 LOG_TRACE("changing role to carrier");
1174                 this.havocbot_role = havocbot_role_kh_carrier;
1175                 this.havocbot_role_timeout = 0;
1176                 return;
1177         }
1178
1179         if (!this.havocbot_role_timeout)
1180                 this.havocbot_role_timeout = time + random() * 10 + 10;
1181         if (time > this.havocbot_role_timeout)
1182         {
1183                 if (random() < 0.5)
1184                 {
1185                         LOG_TRACE("changing role to offense");
1186                         this.havocbot_role = havocbot_role_kh_offense;
1187                 }
1188                 else
1189                 {
1190                         LOG_TRACE("changing role to defense");
1191                         this.havocbot_role = havocbot_role_kh_defense;
1192                 }
1193                 this.havocbot_role_timeout = 0;
1194                 return;
1195         }
1196
1197         if (this.bot_strategytime < time)
1198         {
1199                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1200                 navigation_goalrating_start(this);
1201
1202                 int key_owner_team = kh_Key_AllOwnedByWhichTeam();
1203                 if(key_owner_team == this.team)
1204                         havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend anyway
1205                 else if(key_owner_team == -1)
1206                         havocbot_goalrating_kh(this, 1, 10, 4); // prefer dropped keys
1207                 else
1208                         havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK ANYWAY
1209
1210                 navigation_goalrating_end(this);
1211         }
1212 }
1213
1214
1215 // register this as a mutator
1216
1217 MUTATOR_HOOKFUNCTION(kh, ClientDisconnect)
1218 {
1219         entity player = M_ARGV(0, entity);
1220
1221         kh_Key_DropAll(player, true);
1222 }
1223
1224 MUTATOR_HOOKFUNCTION(kh, MakePlayerObserver)
1225 {
1226         entity player = M_ARGV(0, entity);
1227
1228         kh_Key_DropAll(player, true);
1229 }
1230
1231 MUTATOR_HOOKFUNCTION(kh, PlayerDies)
1232 {
1233         entity frag_attacker = M_ARGV(1, entity);
1234         entity frag_target = M_ARGV(2, entity);
1235
1236         if(frag_target == frag_attacker)
1237                 kh_Key_DropAll(frag_target, true);
1238         else if(IS_PLAYER(frag_attacker))
1239                 kh_Key_DropAll(frag_target, false);
1240         else
1241                 kh_Key_DropAll(frag_target, true);
1242 }
1243
1244 MUTATOR_HOOKFUNCTION(kh, GiveFragsForKill, CBC_ORDER_FIRST)
1245 {
1246         entity frag_attacker = M_ARGV(0, entity);
1247         entity frag_target = M_ARGV(1, entity);
1248         float frag_score = M_ARGV(2, float);
1249         M_ARGV(2, float) = kh_HandleFrags(frag_attacker, frag_target, frag_score);
1250 }
1251
1252 MUTATOR_HOOKFUNCTION(kh, MatchEnd)
1253 {
1254         kh_finalize();
1255 }
1256
1257 MUTATOR_HOOKFUNCTION(kh, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
1258 {
1259         M_ARGV(0, float) = kh_teams;
1260 }
1261
1262 MUTATOR_HOOKFUNCTION(kh, SpectateCopy)
1263 {
1264         entity spectatee = M_ARGV(0, entity);
1265         entity client = M_ARGV(1, entity);
1266
1267         client.kh_state = spectatee.kh_state;
1268 }
1269
1270 MUTATOR_HOOKFUNCTION(kh, PlayerUseKey)
1271 {
1272         entity player = M_ARGV(0, entity);
1273
1274         if(MUTATOR_RETURNVALUE == 0)
1275         {
1276                 entity k = player.kh_next;
1277                 if(k)
1278                 {
1279                         kh_Key_DropOne(k);
1280                         return true;
1281                 }
1282         }
1283 }
1284
1285 MUTATOR_HOOKFUNCTION(kh, HavocBot_ChooseRole)
1286 {
1287     entity bot = M_ARGV(0, entity);
1288
1289         if(IS_DEAD(bot))
1290                 return true;
1291
1292         float r = random() * 3;
1293         if (r < 1)
1294                 bot.havocbot_role = havocbot_role_kh_offense;
1295         else if (r < 2)
1296                 bot.havocbot_role = havocbot_role_kh_defense;
1297         else
1298                 bot.havocbot_role = havocbot_role_kh_freelancer;
1299
1300         return true;
1301 }
1302
1303 MUTATOR_HOOKFUNCTION(kh, DropSpecialItems)
1304 {
1305         entity frag_target = M_ARGV(0, entity);
1306
1307         kh_Key_DropAll(frag_target, false);
1308 }
1309
1310 MUTATOR_HOOKFUNCTION(kh, reset_map_global)
1311 {
1312         kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round + (game_starttime - time), kh_StartRound);
1313 }