1 #include "gamemode_keyhunt.qh"
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;
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;
22 //int autocvar_g_keyhunt_teams;
23 int autocvar_g_keyhunt_teams_override;
25 // #define KH_PLAYER_USE_ATTACHMENT
26 // #define KH_PLAYER_USE_CARRIEDMODEL
28 #ifdef KH_PLAYER_USE_ATTACHMENT
29 const vector KH_PLAYER_ATTACHMENT_DIST_ROTATED = '0 -4 0';
30 const vector KH_PLAYER_ATTACHMENT_DIST = '4 0 0';
31 const vector KH_PLAYER_ATTACHMENT = '0 0 0';
32 const vector KH_PLAYER_ATTACHMENT_ANGLES = '0 0 0';
33 const string KH_PLAYER_ATTACHMENT_BONE = "";
35 const float KH_KEY_ZSHIFT = 22;
36 const float KH_KEY_XYDIST = 24;
37 const float KH_KEY_XYSPEED = 45;
39 const float KH_KEY_WP_ZSHIFT = 20;
41 const vector KH_KEY_MIN = '-10 -10 -46';
42 const vector KH_KEY_MAX = '10 10 3';
43 const float KH_KEY_BRIGHTNESS = 2;
45 bool kh_no_radar_circles;
48 // bits 0- 4: team of key 1, or 0 for no such key, or 30 for dropped, or 31 for self
49 // bits 5- 9: team of key 2, or 0 for no such key, or 30 for dropped, or 31 for self
50 // bits 10-14: team of key 3, or 0 for no such key, or 30 for dropped, or 31 for self
51 // bits 15-19: team of key 4, or 0 for no such key, or 30 for dropped, or 31 for self
52 .int kh_state = _STAT(KH_KEYS);
53 .float siren_time; // time delay the siren
54 //.float stuff_time; // time delay to stuffcmd a cvar
57 //kh_keystatus[0] = status of dropped keys, kh_keystatus[1 - 16] = player #
58 //replace 17 with cvar("maxplayers") or similar !!!!!!!!!
59 //for(i = 0; i < maxplayers; ++i)
60 // kh_keystatus[i] = "0";
62 int kh_Team_ByID(int t)
64 if(t == 0) return NUM_TEAM_1;
65 if(t == 1) return NUM_TEAM_2;
66 if(t == 2) return NUM_TEAM_3;
67 if(t == 3) return NUM_TEAM_4;
71 //entity kh_worldkeylist;
72 .entity kh_worldkeynext;
74 //bool kh_tracking_enabled;
76 int kh_interferemsg_team;
77 float kh_interferemsg_time;
78 .entity kh_next, kh_prev; // linked list
81 .entity kh_previous_owner;
82 .int kh_previous_owner_playerid;
84 int kh_key_dropped, kh_key_carried;
86 int kh_Key_AllOwnedByWhichTeam();
88 const float ST_KH_CAPS = 1;
89 void kh_ScoreRules(int teams)
91 GameRules_scoring(teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, {
92 field_team(ST_KH_CAPS, "caps", SFL_SORT_PRIO_SECONDARY);
93 field(SP_KH_CAPS, "caps", SFL_SORT_PRIO_SECONDARY);
94 field(SP_KH_PUSHES, "pushes", 0);
95 field(SP_KH_DESTROYS, "destroyed", SFL_LOWER_IS_BETTER);
96 field(SP_KH_PICKUPS, "pickups", 0);
97 field(SP_KH_KCKILLS, "kckills", 0);
98 field(SP_KH_LOSSES, "losses", SFL_LOWER_IS_BETTER);
102 bool kh_KeyCarrier_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs all the time
104 if(!IS_PLAYER(view) || DIFF_TEAM(this, view))
105 if(!kh_tracking_enabled)
111 bool kh_Key_waypointsprite_visible_for_player(entity this, entity player, entity view)
113 if(!kh_tracking_enabled)
117 if(!this.owner.owner)
119 return false; // draw only when key is not owned
122 void kh_update_state()
133 s |= (32 ** key.count) * f;
136 FOREACH_CLIENT(true, { it.kh_state = s; });
141 key.owner.kh_state |= (32 ** key.count) * 31;
143 //print(ftos((nextent(NULL)).kh_state), "\n");
149 var kh_Think_t kh_Controller_Thinkfunc;
150 void kh_Controller_SetThink(float t, kh_Think_t func) // runs occasionaly
152 kh_Controller_Thinkfunc = func;
153 kh_controller.cnt = ceil(t);
155 kh_controller.nextthink = time; // force
157 void kh_WaitForPlayers();
158 void kh_Controller_Think(entity this) // called a lot
164 if(getthink(this) != kh_WaitForPlayers)
167 else if(this.cnt == 0)
170 kh_Controller_Thinkfunc();
172 this.nextthink = time + 1;
175 // frags f: take from cvar * f
177 void kh_Scores_Event(entity player, entity key, string what, float frags_player, float frags_owner) // update the score when a key is captured
184 UpdateFrags(player, frags_player);
186 if(key && key.owner && frags_owner)
187 UpdateFrags(key.owner, frags_owner);
189 if(!autocvar_sv_eventlog) //output extra info to the console or text file
192 s = strcat(":keyhunt:", what, ":", ftos(player.playerid), ":", ftos(frags_player));
195 s = strcat(s, ":", ftos(key.owner.playerid));
199 s = strcat(s, ":", ftos(frags_owner), ":");
202 s = strcat(s, key.netname);
207 vector kh_AttachedOrigin(entity e) // runs when a team captures the flag, it can run 2 or 3 times.
211 makevectors(e.tag_entity.angles);
212 return e.tag_entity.origin + e.origin.x * v_forward - e.origin.y * v_right + e.origin.z * v_up;
218 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
220 #ifdef KH_PLAYER_USE_ATTACHMENT
221 entity first = key.owner.kh_next;
224 setattachment(key, key.owner, KH_PLAYER_ATTACHMENT_BONE);
227 setattachment(key.kh_next, key, "");
228 setorigin(key, key.kh_next.origin - 0.5 * KH_PLAYER_ATTACHMENT_DIST);
229 setorigin(key.kh_next, KH_PLAYER_ATTACHMENT_DIST_ROTATED);
230 key.kh_next.angles = '0 0 0';
233 setorigin(key, KH_PLAYER_ATTACHMENT);
234 key.angles = KH_PLAYER_ATTACHMENT_ANGLES;
238 setattachment(key, key.kh_prev, "");
240 setattachment(key.kh_next, key, "");
241 setorigin(key, KH_PLAYER_ATTACHMENT_DIST_ROTATED);
242 setorigin(first, first.origin - 0.5 * KH_PLAYER_ATTACHMENT_DIST);
243 key.angles = '0 0 0';
246 setattachment(key, key.owner, "");
247 setorigin(key, '0 0 1' * KH_KEY_ZSHIFT); // fixing x, y in think
248 key.angles_y -= key.owner.angles.y;
251 if(IL_CONTAINS(g_items, key))
252 IL_REMOVE(g_items, key);
253 key.solid = SOLID_NOT;
254 set_movetype(key, MOVETYPE_NONE);
255 key.team = key.owner.team;
256 key.nextthink = time;
257 key.damageforcescale = 0;
258 key.takedamage = DAMAGE_NO;
259 key.modelindex = kh_key_carried;
260 navigation_dynamicgoal_unset(key);
263 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
265 #ifdef KH_PLAYER_USE_ATTACHMENT
266 entity first = key.owner.kh_next;
271 setattachment(key.kh_next, key.owner, KH_PLAYER_ATTACHMENT_BONE);
272 setorigin(key.kh_next, key.origin + 0.5 * KH_PLAYER_ATTACHMENT_DIST);
273 key.kh_next.angles = KH_PLAYER_ATTACHMENT_ANGLES;
279 setattachment(key.kh_next, key.kh_prev, "");
280 setorigin(first, first.origin + 0.5 * KH_PLAYER_ATTACHMENT_DIST);
283 setattachment(key, NULL, "");
284 setorigin(key, key.owner.origin + '0 0 1' * (STAT(PL_MIN, key.owner).z - KH_KEY_MIN_z));
285 key.angles = key.owner.angles;
287 setorigin(key, key.owner.origin + key.origin.z * '0 0 1');
288 setattachment(key, NULL, "");
289 key.angles_y += key.owner.angles.y;
292 if(!IL_CONTAINS(g_items, key))
293 IL_PUSH(g_items, key);
294 key.solid = SOLID_TRIGGER;
295 set_movetype(key, MOVETYPE_TOSS);
296 key.pain_finished = time + autocvar_g_balance_keyhunt_delay_return;
297 key.damageforcescale = autocvar_g_balance_keyhunt_damageforcescale;
298 key.takedamage = DAMAGE_YES;
300 key.modelindex = kh_key_dropped;
301 navigation_dynamicgoal_set(key);
302 key.kh_previous_owner = key.owner;
303 key.kh_previous_owner_playerid = key.owner.playerid;
306 void kh_Key_AssignTo(entity key, entity player) // runs every time a key is picked up or assigned. Runs prior to kh_key_attach
308 if(key.owner == player)
311 int ownerteam0 = kh_Key_AllOwnedByWhichTeam();
317 // remove from linked list
319 key.kh_next.kh_prev = key.kh_prev;
320 key.kh_prev.kh_next = key.kh_next;
324 if(key.owner.kh_next == NULL)
326 // No longer a key carrier
327 if(!kh_no_radar_circles)
328 WaypointSprite_Ping(key.owner.waypointsprite_attachedforcarrier);
329 WaypointSprite_DetachCarrier(key.owner);
337 // insert into linked list
338 key.kh_next = player.kh_next;
339 key.kh_prev = player;
340 player.kh_next = key;
342 key.kh_next.kh_prev = key;
345 i = kh_keystatus[key.owner.playerid];
346 if(key.netname == "^1red key")
348 if(key.netname == "^4blue key")
350 if(key.netname == "^3yellow key")
352 if(key.netname == "^6pink key")
354 kh_keystatus[key.owner.playerid] = i;
358 if(key.kh_next == NULL)
360 // player is now a key carrier
361 entity wp = WaypointSprite_AttachCarrier(WP_Null, player, RADARICON_FLAGCARRIER);
362 wp.colormod = colormapPaletteColor(player.team - 1, 0);
363 player.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = kh_KeyCarrier_waypointsprite_visible_for_player;
364 WaypointSprite_UpdateRule(player.waypointsprite_attachedforcarrier, player.team, SPRITERULE_TEAMPLAY);
365 if(player.team == NUM_TEAM_1)
366 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierRed, WP_KeyCarrierFriend, WP_KeyCarrierRed);
367 else if(player.team == NUM_TEAM_2)
368 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierBlue, WP_KeyCarrierFriend, WP_KeyCarrierBlue);
369 else if(player.team == NUM_TEAM_3)
370 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierYellow, WP_KeyCarrierFriend, WP_KeyCarrierYellow);
371 else if(player.team == NUM_TEAM_4)
372 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierPink, WP_KeyCarrierFriend, WP_KeyCarrierPink);
373 if(!kh_no_radar_circles)
374 WaypointSprite_Ping(player.waypointsprite_attachedforcarrier);
378 // moved that here, also update if there's no player
383 int ownerteam = kh_Key_AllOwnedByWhichTeam();
384 if(ownerteam != ownerteam0)
389 kh_interferemsg_time = time + 0.2;
390 kh_interferemsg_team = player.team;
392 // audit all key carrier sprites, update them to "Run here"
395 if (!k.owner) continue;
396 entity first = WP_Null;
397 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model1, { first = it; break; });
398 entity third = WP_Null;
399 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model3, { third = it; break; });
400 WaypointSprite_UpdateSprites(k.owner.waypointsprite_attachedforcarrier, first, WP_KeyCarrierFinish, third);
405 kh_interferemsg_time = 0;
407 // audit all key carrier sprites, update them to "Key Carrier"
410 if (!k.owner) continue;
411 entity first = WP_Null;
412 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model1, { first = it; break; });
413 entity third = WP_Null;
414 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model3, { third = it; break; });
415 WaypointSprite_UpdateSprites(k.owner.waypointsprite_attachedforcarrier, first, WP_KeyCarrierFriend, third);
421 void kh_Key_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
425 if(ITEM_DAMAGE_NEEDKILL(deathtype))
427 this.pain_finished = bound(time, time + autocvar_g_balance_keyhunt_delay_damage_return, this.pain_finished);
432 if(time > this.pushltime)
433 if(IS_PLAYER(attacker))
434 this.team = attacker.team;
437 void kh_Key_Collect(entity key, entity player) //a player picks up a dropped key
439 sound(player, CH_TRIGGER, SND_KH_COLLECT, VOL_BASE, ATTEN_NORM);
441 if(key.kh_dropperteam != player.team)
443 kh_Scores_Event(player, key, "collect", autocvar_g_balance_keyhunt_score_collect, 0);
444 PlayerScore_Add(player, SP_KH_PICKUPS, 1);
446 key.kh_dropperteam = 0;
447 int realteam = kh_Team_ByID(key.count);
448 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_PICKUP), player.netname);
450 kh_Key_AssignTo(key, player); // this also updates .kh_state
453 void kh_Key_Touch(entity this, entity toucher) // runs many, many times when a key has been dropped and can be picked up
458 if(this.owner) // already carried
461 if(ITEM_TOUCH_NEEDKILL())
463 this.pain_finished = bound(time, time + autocvar_g_balance_keyhunt_delay_damage_return, this.pain_finished);
467 if (!IS_PLAYER(toucher))
471 if(toucher == this.enemy)
472 if(time < this.kh_droptime + autocvar_g_balance_keyhunt_delay_collect)
473 return; // you just dropped it!
474 kh_Key_Collect(this, toucher);
477 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
479 entity o = key.owner;
480 kh_Key_AssignTo(key, NULL);
481 if(o) // it was attached
482 WaypointSprite_Kill(key.waypointsprite_attachedforcarrier);
483 else // it was dropped
484 WaypointSprite_DetachCarrier(key);
486 // remove key from key list
487 if (kh_worldkeylist == key)
488 kh_worldkeylist = kh_worldkeylist.kh_worldkeynext;
494 if (o.kh_worldkeynext == key)
496 o.kh_worldkeynext = o.kh_worldkeynext.kh_worldkeynext;
499 o = o.kh_worldkeynext;
508 void kh_FinishRound() // runs when a team captures the keys
510 // prepare next round
511 kh_interferemsg_time = 0;
514 kh_no_radar_circles = true;
517 kh_no_radar_circles = false;
519 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_ROUNDSTART, autocvar_g_balance_keyhunt_delay_round);
520 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round, kh_StartRound);
523 void nades_GiveBonus(entity player, float score);
525 void kh_WinnerTeam(int winner_team) // runs when a team wins
527 // all key carriers get some points
529 float score = (NumTeams(kh_teams) - 1) * autocvar_g_balance_keyhunt_score_capture;
530 DistributeEvenly_Init(score, NumTeams(kh_teams));
531 // twice the score for 3 team games, three times the score for 4 team games!
532 // note: for a win by destroying the key, this should NOT be applied
535 float f = DistributeEvenly_Get(1);
536 kh_Scores_Event(key.owner, key, "capture", f, 0);
537 PlayerTeamScore_Add(key.owner, SP_KH_CAPS, ST_KH_CAPS, 1);
538 nades_GiveBonus(key.owner, autocvar_g_nades_bonus_score_high);
542 string keyowner = "";
544 if(key.owner.kh_next == key)
547 keyowner = strcat(keyowner, ", ");
548 keyowner = key.owner.netname;
552 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
553 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_KEYHUNT_CAPTURE), keyowner);
556 vector firstorigin = '0 0 0', lastorigin = '0 0 0', midpoint = '0 0 0';
559 vector thisorigin = kh_AttachedOrigin(key);
560 //dprint("Key origin: ", vtos(thisorigin), "\n");
561 midpoint += thisorigin;
564 te_lightning2(NULL, lastorigin, thisorigin);
565 lastorigin = thisorigin;
567 firstorigin = thisorigin;
570 if(NumTeams(kh_teams) > 2)
572 te_lightning2(NULL, lastorigin, firstorigin);
574 midpoint = midpoint * (1 / NumTeams(kh_teams));
575 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
577 play2all(SND(KH_CAPTURE));
581 void kh_LoserTeam(int loser_team, entity lostkey) // runs when a player pushes a flag carrier off the map
584 entity attacker = NULL;
586 if(lostkey.pusher.team != loser_team)
587 if(IS_PLAYER(lostkey.pusher))
588 attacker = lostkey.pusher;
592 if(lostkey.kh_previous_owner)
593 kh_Scores_Event(lostkey.kh_previous_owner, NULL, "pushed", 0, -autocvar_g_balance_keyhunt_score_push);
594 // don't actually GIVE him the -nn points, just log
595 kh_Scores_Event(attacker, NULL, "push", autocvar_g_balance_keyhunt_score_push, 0);
596 PlayerScore_Add(attacker, SP_KH_PUSHES, 1);
597 //centerprint(attacker, "Your push is the best!"); // does this really need to exist?
602 float of = autocvar_g_balance_keyhunt_score_destroyed_ownfactor;
604 FOREACH_CLIENT(IS_PLAYER(it) && it.team != loser_team, { ++players; });
609 if(key.owner && key.team != loser_team)
612 if(lostkey.kh_previous_owner)
613 kh_Scores_Event(lostkey.kh_previous_owner, NULL, "destroyed", 0, -autocvar_g_balance_keyhunt_score_destroyed);
614 // don't actually GIVE him the -nn points, just log
616 if(lostkey.kh_previous_owner.playerid == lostkey.kh_previous_owner_playerid)
617 PlayerScore_Add(lostkey.kh_previous_owner, SP_KH_DESTROYS, 1);
619 DistributeEvenly_Init(autocvar_g_balance_keyhunt_score_destroyed, keys * of + players);
622 if(key.owner && key.team != loser_team)
624 f = DistributeEvenly_Get(of);
625 kh_Scores_Event(key.owner, NULL, "destroyed_holdingkey", f, 0);
628 int fragsleft = DistributeEvenly_Get(players);
630 // Now distribute these among all other teams...
631 int j = NumTeams(kh_teams) - 1;
632 for(int i = 0; i < NumTeams(kh_teams); ++i)
634 int thisteam = kh_Team_ByID(i);
635 if(thisteam == loser_team) // bad boy, no cookie - this WILL happen
639 FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, { ++players; });
641 DistributeEvenly_Init(fragsleft, j);
642 fragsleft = DistributeEvenly_Get(j - 1);
643 DistributeEvenly_Init(DistributeEvenly_Get(1), players);
645 FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, {
646 f = DistributeEvenly_Get(1);
647 kh_Scores_Event(it, NULL, "destroyed", f, 0);
654 int realteam = kh_Team_ByID(lostkey.count);
655 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(loser_team, CENTER_ROUND_TEAM_LOSS));
657 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_PUSHED), attacker.netname, lostkey.kh_previous_owner.netname);
659 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_DESTROYED), lostkey.kh_previous_owner.netname);
661 play2all(SND(KH_DESTROY));
662 te_tarexplosion(lostkey.origin);
667 void kh_Key_Think(entity this) // runs all the time
674 #ifndef KH_PLAYER_USE_ATTACHMENT
675 makevectors('0 1 0' * (this.cnt + (time % 360) * KH_KEY_XYSPEED));
676 setorigin(this, v_forward * KH_KEY_XYDIST + '0 0 1' * this.origin.z);
680 // if in nodrop or time over, end the round
682 if(time > this.pain_finished)
683 kh_LoserTeam(this.team, this);
686 if(kh_Key_AllOwnedByWhichTeam() != -1)
688 if(this.siren_time < time)
690 sound(this.owner, CH_TRIGGER, SND_KH_ALARM, VOL_BASE, ATTEN_NORM); // play a simple alarm
691 this.siren_time = time + 2.5; // repeat every 2.5 seconds
695 vector p = this.owner.origin;
697 if(vdist(key.owner.origin - p, >, autocvar_g_balance_keyhunt_maxdist))
699 kh_WinnerTeam(this.team);
703 if(kh_interferemsg_time && time > kh_interferemsg_time)
705 kh_interferemsg_time = 0;
706 FOREACH_CLIENT(IS_PLAYER(it), {
707 if(it.team == kh_interferemsg_team)
709 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_MEET);
711 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_HELP);
713 Send_Notification(NOTIF_ONE, it, MSG_CENTER, APP_TEAM_NUM(kh_interferemsg_team, CENTER_KEYHUNT_INTERFERE));
717 this.nextthink = time + 0.05;
720 void key_reset(entity this)
722 kh_Key_AssignTo(this, NULL);
726 const string STR_ITEM_KH_KEY = "item_kh_key";
727 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
729 entity key = spawn();
731 key.classname = STR_ITEM_KH_KEY;
732 settouch(key, kh_Key_Touch);
733 setthink(key, kh_Key_Think);
734 key.nextthink = time;
735 key.items = IT_KEY1 | IT_KEY2;
737 key.angles = '0 360 0' * random();
738 key.event_damage = kh_Key_Damage;
739 key.takedamage = DAMAGE_YES;
740 key.damagedbytriggers = autocvar_g_balance_keyhunt_return_when_unreachable;
741 key.damagedbycontents = autocvar_g_balance_keyhunt_return_when_unreachable;
742 key.modelindex = kh_key_dropped;
744 key.kh_dropperteam = 0;
745 key.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
746 setsize(key, KH_KEY_MIN, KH_KEY_MAX);
747 key.colormod = Team_ColorRGB(initial_owner.team) * KH_KEY_BRIGHTNESS;
748 key.reset = key_reset;
749 navigation_dynamicgoal_init(key, false);
751 switch(initial_owner.team)
754 key.netname = "^1red key";
757 key.netname = "^4blue key";
760 key.netname = "^3yellow key";
763 key.netname = "^6pink key";
766 key.netname = "NETGIER key";
770 // link into key list
771 key.kh_worldkeynext = kh_worldkeylist;
772 kh_worldkeylist = key;
774 Send_Notification(NOTIF_ONE, initial_owner, MSG_CENTER, APP_TEAM_NUM(initial_owner.team, CENTER_KEYHUNT_START));
776 WaypointSprite_Spawn(WP_KeyDropped, 0, 0, key, '0 0 1' * KH_KEY_WP_ZSHIFT, NULL, key.team, key, waypointsprite_attachedforcarrier, false, RADARICON_FLAG);
777 key.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = kh_Key_waypointsprite_visible_for_player;
779 kh_Key_AssignTo(key, initial_owner);
782 // -1 when no team completely owns all keys yet
783 int kh_Key_AllOwnedByWhichTeam() // constantly called. check to see if all the keys are owned by the same team
787 int keys = NumTeams(kh_teams);
794 else if(teem != key.team)
803 void kh_Key_DropOne(entity key)
805 // prevent collecting this one for some time
806 entity player = key.owner;
808 key.kh_droptime = time;
811 kh_Scores_Event(player, key, "dropkey", 0, 0);
812 PlayerScore_Add(player, SP_KH_LOSSES, 1);
813 int realteam = kh_Team_ByID(key.count);
814 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_DROP), player.netname);
816 kh_Key_AssignTo(key, NULL);
817 makevectors(player.v_angle);
818 key.velocity = W_CalculateProjectileVelocity(player, player.velocity, autocvar_g_balance_keyhunt_throwvelocity * v_forward, false);
820 key.pushltime = time + autocvar_g_balance_keyhunt_protecttime;
821 key.kh_dropperteam = key.team;
823 sound(player, CH_TRIGGER, SND_KH_DROP, VOL_BASE, ATTEN_NORM);
826 void kh_Key_DropAll(entity player, float suicide) // runs whenever a player dies
830 entity mypusher = NULL;
832 if(time < player.pushltime)
833 mypusher = player.pusher;
836 while((key = player.kh_next))
838 kh_Scores_Event(player, key, "losekey", 0, 0);
839 PlayerScore_Add(player, SP_KH_LOSSES, 1);
840 int realteam = kh_Team_ByID(key.count);
841 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_LOST), player.netname);
842 kh_Key_AssignTo(key, NULL);
843 makevectors('-1 0 0' * (45 + 45 * random()) + '0 360 0' * random());
844 key.velocity = W_CalculateProjectileVelocity(player, player.velocity, autocvar_g_balance_keyhunt_dropvelocity * v_forward, false);
845 key.pusher = mypusher;
846 key.pushltime = time + autocvar_g_balance_keyhunt_protecttime;
848 key.kh_dropperteam = player.team;
850 sound(player, CH_TRIGGER, SND_KH_DROP, VOL_BASE, ATTEN_NORM);
854 int kh_GetMissingTeams()
856 int missing_teams = 0;
857 for(int i = 0; i < NumTeams(kh_teams); ++i)
859 int teem = kh_Team_ByID(i);
861 FOREACH_CLIENT(IS_PLAYER(it), {
862 if(!IS_DEAD(it) && !PHYS_INPUT_BUTTON_CHAT(it) && it.team == teem)
866 missing_teams |= (2 ** i);
868 return missing_teams;
871 void kh_WaitForPlayers() // delay start of the round until enough players are present
873 static int prev_missing_teams_mask;
874 if(time < game_starttime)
876 if (prev_missing_teams_mask > 0)
877 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
878 prev_missing_teams_mask = -1;
879 kh_Controller_SetThink(game_starttime - time + 0.1, kh_WaitForPlayers);
883 int missing_teams_mask = kh_GetMissingTeams();
884 if(!missing_teams_mask)
886 if(prev_missing_teams_mask > 0)
887 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
888 prev_missing_teams_mask = -1;
889 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_ROUNDSTART, autocvar_g_balance_keyhunt_delay_round);
890 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round, kh_StartRound);
894 if(player_count == 0)
896 if(prev_missing_teams_mask > 0)
897 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
898 prev_missing_teams_mask = -1;
902 if(prev_missing_teams_mask != missing_teams_mask)
904 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
905 prev_missing_teams_mask = missing_teams_mask;
908 kh_Controller_SetThink(1, kh_WaitForPlayers);
912 void kh_EnableTrackingDevice() // runs after each round
914 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT);
915 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT_OTHER);
917 kh_tracking_enabled = true;
920 void kh_StartRound() // runs at the start of each round
922 if(time < game_starttime)
924 kh_Controller_SetThink(game_starttime - time + 0.1, kh_WaitForPlayers);
928 if(kh_GetMissingTeams())
930 kh_Controller_SetThink(1, kh_WaitForPlayers);
934 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT);
935 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT_OTHER);
937 for(int i = 0; i < NumTeams(kh_teams); ++i)
939 int teem = kh_Team_ByID(i);
941 entity my_player = NULL;
942 FOREACH_CLIENT(IS_PLAYER(it), {
943 if(!IS_DEAD(it) && !PHYS_INPUT_BUTTON_CHAT(it) && it.team == teem)
946 if(random() * players <= 1)
950 kh_Key_Spawn(my_player, 360 * i / NumTeams(kh_teams), i);
953 kh_tracking_enabled = false;
954 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_SCAN, autocvar_g_balance_keyhunt_delay_tracking);
955 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_tracking, kh_EnableTrackingDevice);
958 float kh_HandleFrags(entity attacker, entity targ, float f) // adds to the player score
965 if(attacker.team == targ.team)
968 for(entity k = targ.kh_next; k != NULL; k = k.kh_next)
970 kh_Scores_Event(attacker, targ.kh_next, "carrierfrag", -nk * autocvar_g_balance_keyhunt_score_collect, 0);
974 kh_Scores_Event(attacker, targ.kh_next, "carrierfrag", autocvar_g_balance_keyhunt_score_carrierfrag-1, 0);
975 PlayerScore_Add(attacker, SP_KH_KCKILLS, 1);
976 // the frag gets added later
983 void kh_Initialize() // sets up th KH environment
986 kh_teams = autocvar_g_keyhunt_teams_override;
988 kh_teams = cvar("g_keyhunt_teams"); // read the cvar directly as it gets written earlier in the same frame
989 kh_teams = bound(2, kh_teams, 4);
992 if(kh_teams >= 1) teams |= BIT(0);
993 if(kh_teams >= 2) teams |= BIT(1);
994 if(kh_teams >= 3) teams |= BIT(2);
995 if(kh_teams >= 4) teams |= BIT(3);
997 kh_teams = teams; // now set it?
999 // make a KH entity for controlling the game
1000 kh_controller = spawn();
1001 setthink(kh_controller, kh_Controller_Think);
1002 kh_Controller_SetThink(0, kh_WaitForPlayers);
1004 setmodel(kh_controller, MDL_KH_KEY);
1005 kh_key_dropped = kh_controller.modelindex;
1007 dprint(vtos(kh_controller.mins));
1008 dprint(vtos(kh_controller.maxs));
1011 #ifdef KH_PLAYER_USE_CARRIEDMODEL
1012 setmodel(kh_controller, MDL_KH_KEY_CARRIED);
1013 kh_key_carried = kh_controller.modelindex;
1015 kh_key_carried = kh_key_dropped;
1018 kh_controller.model = "";
1019 kh_controller.modelindex = 0;
1021 kh_ScoreRules(kh_teams);
1026 // to be called before intermission
1028 delete(kh_controller);
1029 kh_controller = NULL;
1034 void(entity this) havocbot_role_kh_carrier;
1035 void(entity this) havocbot_role_kh_defense;
1036 void(entity this) havocbot_role_kh_offense;
1037 void(entity this) havocbot_role_kh_freelancer;
1040 void havocbot_goalrating_kh(entity this, float ratingscale_team, float ratingscale_dropped, float ratingscale_enemy)
1043 for (head = kh_worldkeylist; head; head = head.kh_worldkeynext)
1045 if(head.owner == this)
1047 if(!kh_tracking_enabled)
1049 // if it's carried by our team we know about it
1050 // otherwise we have to see it to know about it
1051 if(!head.owner || head.team != this.team)
1053 traceline(this.origin + this.view_ofs, head.origin, MOVE_NOMONSTERS, this);
1054 if (trace_fraction < 1 && trace_ent != head)
1055 continue; // skip what I can't see
1059 navigation_routerating(this, head, ratingscale_dropped * 10000, 100000);
1060 else if(head.team == this.team)
1061 navigation_routerating(this, head.owner, ratingscale_team * 10000, 100000);
1063 navigation_routerating(this, head.owner, ratingscale_enemy * 10000, 100000);
1066 havocbot_goalrating_items(this, 1, this.origin, 10000);
1069 void havocbot_role_kh_carrier(entity this)
1074 if (!(this.kh_next))
1076 LOG_TRACE("changing role to freelancer");
1077 this.havocbot_role = havocbot_role_kh_freelancer;
1078 this.havocbot_role_timeout = 0;
1082 if (this.bot_strategytime < time)
1084 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1085 navigation_goalrating_start(this);
1087 if(kh_Key_AllOwnedByWhichTeam() == this.team)
1088 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // bring home
1090 havocbot_goalrating_kh(this, 4, 4, 1); // play defensively
1092 navigation_goalrating_end(this);
1096 void havocbot_role_kh_defense(entity this)
1103 LOG_TRACE("changing role to carrier");
1104 this.havocbot_role = havocbot_role_kh_carrier;
1105 this.havocbot_role_timeout = 0;
1109 if (!this.havocbot_role_timeout)
1110 this.havocbot_role_timeout = time + random() * 10 + 20;
1111 if (time > this.havocbot_role_timeout)
1113 LOG_TRACE("changing role to freelancer");
1114 this.havocbot_role = havocbot_role_kh_freelancer;
1115 this.havocbot_role_timeout = 0;
1119 if (this.bot_strategytime < time)
1121 float key_owner_team;
1122 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1123 navigation_goalrating_start(this);
1125 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1126 if(key_owner_team == this.team)
1127 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend key carriers
1128 else if(key_owner_team == -1)
1129 havocbot_goalrating_kh(this, 4, 1, 0.1); // play defensively
1131 havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK ANYWAY
1133 navigation_goalrating_end(this);
1137 void havocbot_role_kh_offense(entity this)
1144 LOG_TRACE("changing role to carrier");
1145 this.havocbot_role = havocbot_role_kh_carrier;
1146 this.havocbot_role_timeout = 0;
1150 if (!this.havocbot_role_timeout)
1151 this.havocbot_role_timeout = time + random() * 10 + 20;
1152 if (time > this.havocbot_role_timeout)
1154 LOG_TRACE("changing role to freelancer");
1155 this.havocbot_role = havocbot_role_kh_freelancer;
1156 this.havocbot_role_timeout = 0;
1160 if (this.bot_strategytime < time)
1162 float key_owner_team;
1164 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1165 navigation_goalrating_start(this);
1167 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1168 if(key_owner_team == this.team)
1169 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend anyway
1170 else if(key_owner_team == -1)
1171 havocbot_goalrating_kh(this, 0.1, 1, 4); // play offensively
1173 havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK! EMERGENCY!
1175 navigation_goalrating_end(this);
1179 void havocbot_role_kh_freelancer(entity this)
1186 LOG_TRACE("changing role to carrier");
1187 this.havocbot_role = havocbot_role_kh_carrier;
1188 this.havocbot_role_timeout = 0;
1192 if (!this.havocbot_role_timeout)
1193 this.havocbot_role_timeout = time + random() * 10 + 10;
1194 if (time > this.havocbot_role_timeout)
1198 LOG_TRACE("changing role to offense");
1199 this.havocbot_role = havocbot_role_kh_offense;
1203 LOG_TRACE("changing role to defense");
1204 this.havocbot_role = havocbot_role_kh_defense;
1206 this.havocbot_role_timeout = 0;
1210 if (this.bot_strategytime < time)
1212 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1213 navigation_goalrating_start(this);
1215 int key_owner_team = kh_Key_AllOwnedByWhichTeam();
1216 if(key_owner_team == this.team)
1217 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend anyway
1218 else if(key_owner_team == -1)
1219 havocbot_goalrating_kh(this, 1, 10, 4); // prefer dropped keys
1221 havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK ANYWAY
1223 navigation_goalrating_end(this);
1228 // register this as a mutator
1230 MUTATOR_HOOKFUNCTION(kh, ClientDisconnect)
1232 entity player = M_ARGV(0, entity);
1234 kh_Key_DropAll(player, true);
1237 MUTATOR_HOOKFUNCTION(kh, MakePlayerObserver)
1239 entity player = M_ARGV(0, entity);
1241 kh_Key_DropAll(player, true);
1244 MUTATOR_HOOKFUNCTION(kh, PlayerDies)
1246 entity frag_attacker = M_ARGV(1, entity);
1247 entity frag_target = M_ARGV(2, entity);
1249 if(frag_target == frag_attacker)
1250 kh_Key_DropAll(frag_target, true);
1251 else if(IS_PLAYER(frag_attacker))
1252 kh_Key_DropAll(frag_target, false);
1254 kh_Key_DropAll(frag_target, true);
1257 MUTATOR_HOOKFUNCTION(kh, GiveFragsForKill, CBC_ORDER_FIRST)
1259 entity frag_attacker = M_ARGV(0, entity);
1260 entity frag_target = M_ARGV(1, entity);
1261 float frag_score = M_ARGV(2, float);
1262 M_ARGV(2, float) = kh_HandleFrags(frag_attacker, frag_target, frag_score);
1265 MUTATOR_HOOKFUNCTION(kh, MatchEnd)
1270 MUTATOR_HOOKFUNCTION(kh, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
1272 M_ARGV(0, float) = kh_teams;
1275 MUTATOR_HOOKFUNCTION(kh, SpectateCopy)
1277 entity spectatee = M_ARGV(0, entity);
1278 entity client = M_ARGV(1, entity);
1280 client.kh_state = spectatee.kh_state;
1283 MUTATOR_HOOKFUNCTION(kh, PlayerUseKey)
1285 entity player = M_ARGV(0, entity);
1287 if(MUTATOR_RETURNVALUE == 0)
1289 entity k = player.kh_next;
1298 MUTATOR_HOOKFUNCTION(kh, HavocBot_ChooseRole)
1300 entity bot = M_ARGV(0, entity);
1305 float r = random() * 3;
1307 bot.havocbot_role = havocbot_role_kh_offense;
1309 bot.havocbot_role = havocbot_role_kh_defense;
1311 bot.havocbot_role = havocbot_role_kh_freelancer;
1316 MUTATOR_HOOKFUNCTION(kh, DropSpecialItems)
1318 entity frag_target = M_ARGV(0, entity);
1320 kh_Key_DropAll(frag_target, false);
1323 MUTATOR_HOOKFUNCTION(kh, reset_map_global)
1325 kh_WaitForPlayers(); // takes care of killing the "missing teams" message