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