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 ScoreRules_basics(teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, true);
92 ScoreInfo_SetLabel_TeamScore( ST_KH_CAPS, "caps", SFL_SORT_PRIO_SECONDARY);
93 ScoreInfo_SetLabel_PlayerScore(SP_KH_CAPS, "caps", SFL_SORT_PRIO_SECONDARY);
94 ScoreInfo_SetLabel_PlayerScore(SP_KH_PUSHES, "pushes", 0);
95 ScoreInfo_SetLabel_PlayerScore(SP_KH_DESTROYS, "destroyed", SFL_LOWER_IS_BETTER);
96 ScoreInfo_SetLabel_PlayerScore(SP_KH_PICKUPS, "pickups", 0);
97 ScoreInfo_SetLabel_PlayerScore(SP_KH_KCKILLS, "kckills", 0);
98 ScoreInfo_SetLabel_PlayerScore(SP_KH_LOSSES, "losses", SFL_LOWER_IS_BETTER);
99 ScoreRules_basics_end();
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 |= pow(32, key.count) * f;
136 FOREACH_CLIENT(true, LAMBDA(it.kh_state = s));
141 key.owner.kh_state |= pow(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
160 if(intermission_running)
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
180 if(intermission_running)
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 IL_REMOVE(g_items, key);
252 key.solid = SOLID_NOT;
253 set_movetype(key, MOVETYPE_NONE);
254 key.team = key.owner.team;
255 key.nextthink = time;
256 key.damageforcescale = 0;
257 key.takedamage = DAMAGE_NO;
258 key.modelindex = kh_key_carried;
261 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
263 #ifdef KH_PLAYER_USE_ATTACHMENT
264 entity first = key.owner.kh_next;
269 setattachment(key.kh_next, key.owner, KH_PLAYER_ATTACHMENT_BONE);
270 setorigin(key.kh_next, key.origin + 0.5 * KH_PLAYER_ATTACHMENT_DIST);
271 key.kh_next.angles = KH_PLAYER_ATTACHMENT_ANGLES;
277 setattachment(key.kh_next, key.kh_prev, "");
278 setorigin(first, first.origin + 0.5 * KH_PLAYER_ATTACHMENT_DIST);
281 setattachment(key, NULL, "");
282 setorigin(key, key.owner.origin + '0 0 1' * (STAT(PL_MIN, NULL).z - KH_KEY_MIN_z));
283 key.angles = key.owner.angles;
285 setorigin(key, key.owner.origin + key.origin.z * '0 0 1');
286 setattachment(key, NULL, "");
287 key.angles_y += key.owner.angles.y;
290 IL_PUSH(g_items, key);
291 key.solid = SOLID_TRIGGER;
292 set_movetype(key, MOVETYPE_TOSS);
293 key.pain_finished = time + autocvar_g_balance_keyhunt_delay_return;
294 key.damageforcescale = autocvar_g_balance_keyhunt_damageforcescale;
295 key.takedamage = DAMAGE_YES;
297 key.modelindex = kh_key_dropped;
298 key.kh_previous_owner = key.owner;
299 key.kh_previous_owner_playerid = key.owner.playerid;
302 void kh_Key_AssignTo(entity key, entity player) // runs every time a key is picked up or assigned. Runs prior to kh_key_attach
304 if(key.owner == player)
307 int ownerteam0 = kh_Key_AllOwnedByWhichTeam();
313 // remove from linked list
315 key.kh_next.kh_prev = key.kh_prev;
316 key.kh_prev.kh_next = key.kh_next;
320 if(key.owner.kh_next == NULL)
322 // No longer a key carrier
323 if(!kh_no_radar_circles)
324 WaypointSprite_Ping(key.owner.waypointsprite_attachedforcarrier);
325 WaypointSprite_DetachCarrier(key.owner);
333 // insert into linked list
334 key.kh_next = player.kh_next;
335 key.kh_prev = player;
336 player.kh_next = key;
338 key.kh_next.kh_prev = key;
341 i = kh_keystatus[key.owner.playerid];
342 if(key.netname == "^1red key")
344 if(key.netname == "^4blue key")
346 if(key.netname == "^3yellow key")
348 if(key.netname == "^6pink key")
350 kh_keystatus[key.owner.playerid] = i;
354 if(key.kh_next == NULL)
356 // player is now a key carrier
357 entity wp = WaypointSprite_AttachCarrier(WP_Null, player, RADARICON_FLAGCARRIER);
358 wp.colormod = colormapPaletteColor(player.team - 1, 0);
359 player.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = kh_KeyCarrier_waypointsprite_visible_for_player;
360 WaypointSprite_UpdateRule(player.waypointsprite_attachedforcarrier, player.team, SPRITERULE_TEAMPLAY);
361 if(player.team == NUM_TEAM_1)
362 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierRed, WP_KeyCarrierFriend, WP_KeyCarrierRed);
363 else if(player.team == NUM_TEAM_2)
364 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierBlue, WP_KeyCarrierFriend, WP_KeyCarrierBlue);
365 else if(player.team == NUM_TEAM_3)
366 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierYellow, WP_KeyCarrierFriend, WP_KeyCarrierYellow);
367 else if(player.team == NUM_TEAM_4)
368 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierPink, WP_KeyCarrierFriend, WP_KeyCarrierPink);
369 if(!kh_no_radar_circles)
370 WaypointSprite_Ping(player.waypointsprite_attachedforcarrier);
374 // moved that here, also update if there's no player
379 int ownerteam = kh_Key_AllOwnedByWhichTeam();
380 if(ownerteam != ownerteam0)
385 kh_interferemsg_time = time + 0.2;
386 kh_interferemsg_team = player.team;
388 // audit all key carrier sprites, update them to "Run here"
391 if (!k.owner) continue;
392 entity first = WP_Null;
393 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model1, LAMBDA(first = it; break));
394 entity third = WP_Null;
395 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model3, LAMBDA(third = it; break));
396 WaypointSprite_UpdateSprites(k.owner.waypointsprite_attachedforcarrier, first, WP_KeyCarrierFinish, third);
401 kh_interferemsg_time = 0;
403 // audit all key carrier sprites, update them to "Key Carrier"
406 if (!k.owner) continue;
407 entity first = WP_Null;
408 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model1, LAMBDA(first = it; break));
409 entity third = WP_Null;
410 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model3, LAMBDA(third = it; break));
411 WaypointSprite_UpdateSprites(k.owner.waypointsprite_attachedforcarrier, first, WP_KeyCarrierFriend, third);
417 void kh_Key_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
421 if(ITEM_DAMAGE_NEEDKILL(deathtype))
423 this.pain_finished = bound(time, time + autocvar_g_balance_keyhunt_delay_damage_return, this.pain_finished);
428 if(time > this.pushltime)
429 if(IS_PLAYER(attacker))
430 this.team = attacker.team;
433 void kh_Key_Collect(entity key, entity player) //a player picks up a dropped key
435 sound(player, CH_TRIGGER, SND_KH_COLLECT, VOL_BASE, ATTEN_NORM);
437 if(key.kh_dropperteam != player.team)
439 kh_Scores_Event(player, key, "collect", autocvar_g_balance_keyhunt_score_collect, 0);
440 PlayerScore_Add(player, SP_KH_PICKUPS, 1);
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);
446 kh_Key_AssignTo(key, player); // this also updates .kh_state
449 void kh_Key_Touch(entity this, entity toucher) // runs many, many times when a key has been dropped and can be picked up
451 if(intermission_running)
454 if(this.owner) // already carried
457 if(ITEM_TOUCH_NEEDKILL())
459 this.pain_finished = bound(time, time + autocvar_g_balance_keyhunt_delay_damage_return, this.pain_finished);
463 if (!IS_PLAYER(toucher))
467 if(toucher == this.enemy)
468 if(time < this.kh_droptime + autocvar_g_balance_keyhunt_delay_collect)
469 return; // you just dropped it!
470 kh_Key_Collect(this, toucher);
473 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
475 entity o = key.owner;
476 kh_Key_AssignTo(key, NULL);
477 if(o) // it was attached
478 WaypointSprite_Kill(key.waypointsprite_attachedforcarrier);
479 else // it was dropped
480 WaypointSprite_DetachCarrier(key);
482 // remove key from key list
483 if (kh_worldkeylist == key)
484 kh_worldkeylist = kh_worldkeylist.kh_worldkeynext;
490 if (o.kh_worldkeynext == key)
492 o.kh_worldkeynext = o.kh_worldkeynext.kh_worldkeynext;
495 o = o.kh_worldkeynext;
504 void kh_FinishRound() // runs when a team captures the keys
506 // prepare next round
507 kh_interferemsg_time = 0;
510 kh_no_radar_circles = true;
513 kh_no_radar_circles = false;
515 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_ROUNDSTART, autocvar_g_balance_keyhunt_delay_round);
516 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round, kh_StartRound);
519 void nades_GiveBonus(entity player, float score);
521 void kh_WinnerTeam(int winner_team) // runs when a team wins
523 // all key carriers get some points
525 float score = (NumTeams(kh_teams) - 1) * autocvar_g_balance_keyhunt_score_capture;
526 DistributeEvenly_Init(score, NumTeams(kh_teams));
527 // twice the score for 3 team games, three times the score for 4 team games!
528 // note: for a win by destroying the key, this should NOT be applied
531 float f = DistributeEvenly_Get(1);
532 kh_Scores_Event(key.owner, key, "capture", f, 0);
533 PlayerTeamScore_Add(key.owner, SP_KH_CAPS, ST_KH_CAPS, 1);
534 nades_GiveBonus(key.owner, autocvar_g_nades_bonus_score_high);
538 string keyowner = "";
540 if(key.owner.kh_next == key)
543 keyowner = strcat(keyowner, ", ");
544 keyowner = key.owner.netname;
548 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_KEYHUNT_CAPTURE), keyowner);
551 vector firstorigin = '0 0 0', lastorigin = '0 0 0', midpoint = '0 0 0';
554 vector thisorigin = kh_AttachedOrigin(key);
555 //dprint("Key origin: ", vtos(thisorigin), "\n");
556 midpoint += thisorigin;
559 te_lightning2(NULL, lastorigin, thisorigin);
560 lastorigin = thisorigin;
562 firstorigin = thisorigin;
565 if(NumTeams(kh_teams) > 2)
567 te_lightning2(NULL, lastorigin, firstorigin);
569 midpoint = midpoint * (1 / NumTeams(kh_teams));
570 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
572 play2all(SND(KH_CAPTURE));
576 void kh_LoserTeam(int loser_team, entity lostkey) // runs when a player pushes a flag carrier off the map
579 entity attacker = NULL;
581 if(lostkey.pusher.team != loser_team)
582 if(IS_PLAYER(lostkey.pusher))
583 attacker = lostkey.pusher;
587 if(lostkey.kh_previous_owner)
588 kh_Scores_Event(lostkey.kh_previous_owner, NULL, "pushed", 0, -autocvar_g_balance_keyhunt_score_push);
589 // don't actually GIVE him the -nn points, just log
590 kh_Scores_Event(attacker, NULL, "push", autocvar_g_balance_keyhunt_score_push, 0);
591 PlayerScore_Add(attacker, SP_KH_PUSHES, 1);
592 //centerprint(attacker, "Your push is the best!"); // does this really need to exist?
597 float of = autocvar_g_balance_keyhunt_score_destroyed_ownfactor;
599 FOREACH_CLIENT(IS_PLAYER(it) && it.team != loser_team, LAMBDA(++players));
604 if(key.owner && key.team != loser_team)
607 if(lostkey.kh_previous_owner)
608 kh_Scores_Event(lostkey.kh_previous_owner, NULL, "destroyed", 0, -autocvar_g_balance_keyhunt_score_destroyed);
609 // don't actually GIVE him the -nn points, just log
611 if(lostkey.kh_previous_owner.playerid == lostkey.kh_previous_owner_playerid)
612 PlayerScore_Add(lostkey.kh_previous_owner, SP_KH_DESTROYS, 1);
614 DistributeEvenly_Init(autocvar_g_balance_keyhunt_score_destroyed, keys * of + players);
617 if(key.owner && key.team != loser_team)
619 f = DistributeEvenly_Get(of);
620 kh_Scores_Event(key.owner, NULL, "destroyed_holdingkey", f, 0);
623 int fragsleft = DistributeEvenly_Get(players);
625 // Now distribute these among all other teams...
626 int j = NumTeams(kh_teams) - 1;
627 for(int i = 0; i < NumTeams(kh_teams); ++i)
629 int thisteam = kh_Team_ByID(i);
630 if(thisteam == loser_team) // bad boy, no cookie - this WILL happen
634 FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, LAMBDA(++players));
636 DistributeEvenly_Init(fragsleft, j);
637 fragsleft = DistributeEvenly_Get(j - 1);
638 DistributeEvenly_Init(DistributeEvenly_Get(1), players);
640 FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, LAMBDA(
641 f = DistributeEvenly_Get(1);
642 kh_Scores_Event(it, NULL, "destroyed", f, 0);
649 int realteam = kh_Team_ByID(lostkey.count);
651 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_PUSHED), attacker.netname, lostkey.kh_previous_owner.netname);
653 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_DESTROYED), lostkey.kh_previous_owner.netname);
655 play2all(SND(KH_DESTROY));
656 te_tarexplosion(lostkey.origin);
661 void kh_Key_Think(entity this) // runs all the time
663 if(intermission_running)
668 #ifndef KH_PLAYER_USE_ATTACHMENT
669 makevectors('0 1 0' * (this.cnt + (time % 360) * KH_KEY_XYSPEED));
670 setorigin(this, v_forward * KH_KEY_XYDIST + '0 0 1' * this.origin.z);
674 // if in nodrop or time over, end the round
676 if(time > this.pain_finished)
677 kh_LoserTeam(this.team, this);
680 if(kh_Key_AllOwnedByWhichTeam() != -1)
682 if(this.siren_time < time)
684 sound(this.owner, CH_TRIGGER, SND_KH_ALARM, VOL_BASE, ATTEN_NORM); // play a simple alarm
685 this.siren_time = time + 2.5; // repeat every 2.5 seconds
689 vector p = this.owner.origin;
691 if(vdist(key.owner.origin - p, >, autocvar_g_balance_keyhunt_maxdist))
693 kh_WinnerTeam(this.team);
697 if(kh_interferemsg_time && time > kh_interferemsg_time)
699 kh_interferemsg_time = 0;
700 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
701 if(it.team == kh_interferemsg_team)
703 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_MEET);
705 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_HELP);
707 Send_Notification(NOTIF_ONE, it, MSG_CENTER, APP_TEAM_NUM(kh_interferemsg_team, CENTER_KEYHUNT_INTERFERE));
711 this.nextthink = time + 0.05;
714 void key_reset(entity this)
716 kh_Key_AssignTo(this, NULL);
720 const string STR_ITEM_KH_KEY = "item_kh_key";
721 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
723 entity key = spawn();
725 key.classname = STR_ITEM_KH_KEY;
726 settouch(key, kh_Key_Touch);
727 setthink(key, kh_Key_Think);
728 key.nextthink = time;
729 key.items = IT_KEY1 | IT_KEY2;
731 key.angles = '0 360 0' * random();
732 key.event_damage = kh_Key_Damage;
733 key.takedamage = DAMAGE_YES;
734 key.damagedbytriggers = autocvar_g_balance_keyhunt_return_when_unreachable;
735 key.damagedbycontents = autocvar_g_balance_keyhunt_return_when_unreachable;
736 key.modelindex = kh_key_dropped;
738 key.kh_dropperteam = 0;
739 key.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
740 setsize(key, KH_KEY_MIN, KH_KEY_MAX);
741 key.colormod = Team_ColorRGB(initial_owner.team) * KH_KEY_BRIGHTNESS;
742 key.reset = key_reset;
744 switch(initial_owner.team)
747 key.netname = "^1red key";
750 key.netname = "^4blue key";
753 key.netname = "^3yellow key";
756 key.netname = "^6pink key";
759 key.netname = "NETGIER key";
763 // link into key list
764 key.kh_worldkeynext = kh_worldkeylist;
765 kh_worldkeylist = key;
767 Send_Notification(NOTIF_ONE, initial_owner, MSG_CENTER, APP_TEAM_NUM(initial_owner.team, CENTER_KEYHUNT_START));
769 WaypointSprite_Spawn(WP_KeyDropped, 0, 0, key, '0 0 1' * KH_KEY_WP_ZSHIFT, NULL, key.team, key, waypointsprite_attachedforcarrier, false, RADARICON_FLAG);
770 key.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = kh_Key_waypointsprite_visible_for_player;
772 kh_Key_AssignTo(key, initial_owner);
775 // -1 when no team completely owns all keys yet
776 int kh_Key_AllOwnedByWhichTeam() // constantly called. check to see if all the keys are owned by the same team
780 int keys = NumTeams(kh_teams);
787 else if(teem != key.team)
796 void kh_Key_DropOne(entity key)
798 // prevent collecting this one for some time
799 entity player = key.owner;
801 key.kh_droptime = time;
804 kh_Scores_Event(player, key, "dropkey", 0, 0);
805 PlayerScore_Add(player, SP_KH_LOSSES, 1);
806 int realteam = kh_Team_ByID(key.count);
807 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_DROP), player.netname);
809 kh_Key_AssignTo(key, NULL);
810 makevectors(player.v_angle);
811 key.velocity = W_CalculateProjectileVelocity(player, player.velocity, autocvar_g_balance_keyhunt_throwvelocity * v_forward, false);
813 key.pushltime = time + autocvar_g_balance_keyhunt_protecttime;
814 key.kh_dropperteam = key.team;
816 sound(player, CH_TRIGGER, SND_KH_DROP, VOL_BASE, ATTEN_NORM);
819 void kh_Key_DropAll(entity player, float suicide) // runs whenever a player dies
823 entity mypusher = NULL;
825 if(time < player.pushltime)
826 mypusher = player.pusher;
829 while((key = player.kh_next))
831 kh_Scores_Event(player, key, "losekey", 0, 0);
832 PlayerScore_Add(player, SP_KH_LOSSES, 1);
833 int realteam = kh_Team_ByID(key.count);
834 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(realteam, INFO_KEYHUNT_LOST), player.netname);
835 kh_Key_AssignTo(key, NULL);
836 makevectors('-1 0 0' * (45 + 45 * random()) + '0 360 0' * random());
837 key.velocity = W_CalculateProjectileVelocity(player, player.velocity, autocvar_g_balance_keyhunt_dropvelocity * v_forward, false);
838 key.pusher = mypusher;
839 key.pushltime = time + autocvar_g_balance_keyhunt_protecttime;
841 key.kh_dropperteam = player.team;
843 sound(player, CH_TRIGGER, SND_KH_DROP, VOL_BASE, ATTEN_NORM);
847 int kh_GetMissingTeams()
849 int missing_teams = 0;
850 for(int i = 0; i < NumTeams(kh_teams); ++i)
852 int teem = kh_Team_ByID(i);
854 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
855 if(!IS_DEAD(it) && !PHYS_INPUT_BUTTON_CHAT(it) && it.team == teem)
859 missing_teams |= pow(2, i);
861 return missing_teams;
864 void kh_WaitForPlayers() // delay start of the round until enough players are present
866 if(time < game_starttime)
868 kh_Controller_SetThink(game_starttime - time + 0.1, kh_WaitForPlayers);
872 static int prev_missing_teams_mask;
873 int missing_teams_mask = kh_GetMissingTeams();
874 if(!missing_teams_mask)
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 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_ROUNDSTART, autocvar_g_balance_keyhunt_delay_round);
880 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round, kh_StartRound);
884 if(player_count == 0)
886 if(prev_missing_teams_mask > 0)
887 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
888 prev_missing_teams_mask = -1;
892 if(prev_missing_teams_mask != missing_teams_mask)
894 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
895 prev_missing_teams_mask = missing_teams_mask;
898 kh_Controller_SetThink(1, kh_WaitForPlayers);
902 void kh_EnableTrackingDevice() // runs after each round
904 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT);
905 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT_OTHER);
907 kh_tracking_enabled = true;
910 void kh_StartRound() // runs at the start of each round
912 if(time < game_starttime)
914 kh_Controller_SetThink(game_starttime - time + 0.1, kh_WaitForPlayers);
918 if(kh_GetMissingTeams())
920 kh_Controller_SetThink(1, kh_WaitForPlayers);
924 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT);
925 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT_OTHER);
927 for(int i = 0; i < NumTeams(kh_teams); ++i)
929 int teem = kh_Team_ByID(i);
931 entity my_player = NULL;
932 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
933 if(!IS_DEAD(it) && !PHYS_INPUT_BUTTON_CHAT(it) && it.team == teem)
936 if(random() * players <= 1)
940 kh_Key_Spawn(my_player, 360 * i / NumTeams(kh_teams), i);
943 kh_tracking_enabled = false;
944 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_SCAN, autocvar_g_balance_keyhunt_delay_tracking);
945 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_tracking, kh_EnableTrackingDevice);
948 float kh_HandleFrags(entity attacker, entity targ, float f) // adds to the player score
955 if(attacker.team == targ.team)
958 for(entity k = targ.kh_next; k != NULL; k = k.kh_next)
960 kh_Scores_Event(attacker, targ.kh_next, "carrierfrag", -nk * autocvar_g_balance_keyhunt_score_collect, 0);
964 kh_Scores_Event(attacker, targ.kh_next, "carrierfrag", autocvar_g_balance_keyhunt_score_carrierfrag-1, 0);
965 PlayerScore_Add(attacker, SP_KH_KCKILLS, 1);
966 // the frag gets added later
973 void kh_Initialize() // sets up th KH environment
976 kh_teams = autocvar_g_keyhunt_teams_override;
978 kh_teams = cvar("g_keyhunt_teams"); // read the cvar directly as it gets written earlier in the same frame
979 kh_teams = bound(2, kh_teams, 4);
982 if(kh_teams >= 1) teams |= BIT(0);
983 if(kh_teams >= 2) teams |= BIT(1);
984 if(kh_teams >= 3) teams |= BIT(2);
985 if(kh_teams >= 4) teams |= BIT(3);
987 kh_teams = teams; // now set it?
989 // make a KH entity for controlling the game
990 kh_controller = spawn();
991 setthink(kh_controller, kh_Controller_Think);
992 kh_Controller_SetThink(0, kh_WaitForPlayers);
994 setmodel(kh_controller, MDL_KH_KEY);
995 kh_key_dropped = kh_controller.modelindex;
997 dprint(vtos(kh_controller.mins));
998 dprint(vtos(kh_controller.maxs));
1001 #ifdef KH_PLAYER_USE_CARRIEDMODEL
1002 setmodel(kh_controller, MDL_KH_KEY_CARRIED);
1003 kh_key_carried = kh_controller.modelindex;
1005 kh_key_carried = kh_key_dropped;
1008 kh_controller.model = "";
1009 kh_controller.modelindex = 0;
1011 kh_ScoreRules(kh_teams);
1016 // to be called before intermission
1018 delete(kh_controller);
1019 kh_controller = NULL;
1024 void(entity this) havocbot_role_kh_carrier;
1025 void(entity this) havocbot_role_kh_defense;
1026 void(entity this) havocbot_role_kh_offense;
1027 void(entity this) havocbot_role_kh_freelancer;
1030 void havocbot_goalrating_kh(entity this, float ratingscale_team, float ratingscale_dropped, float ratingscale_enemy)
1033 for (head = kh_worldkeylist; head; head = head.kh_worldkeynext)
1035 if(head.owner == this)
1037 if(!kh_tracking_enabled)
1039 // if it's carried by our team we know about it
1040 // otherwise we have to see it to know about it
1041 if(!head.owner || head.team != this.team)
1043 traceline(this.origin + this.view_ofs, head.origin, MOVE_NOMONSTERS, this);
1044 if (trace_fraction < 1 && trace_ent != head)
1045 continue; // skip what I can't see
1049 navigation_routerating(this, head, ratingscale_dropped * BOT_PICKUP_RATING_HIGH, 100000);
1050 else if(head.team == this.team)
1051 navigation_routerating(this, head.owner, ratingscale_team * BOT_PICKUP_RATING_HIGH, 100000);
1053 navigation_routerating(this, head.owner, ratingscale_enemy * BOT_PICKUP_RATING_HIGH, 100000);
1056 havocbot_goalrating_items(this, 1, this.origin, 10000);
1059 void havocbot_role_kh_carrier(entity this)
1064 if (!(this.kh_next))
1066 LOG_TRACE("changing role to freelancer");
1067 this.havocbot_role = havocbot_role_kh_freelancer;
1068 this.havocbot_role_timeout = 0;
1072 if (this.bot_strategytime < time)
1074 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1075 navigation_goalrating_start(this);
1077 if(kh_Key_AllOwnedByWhichTeam() == this.team)
1078 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // bring home
1080 havocbot_goalrating_kh(this, 4, 4, 1); // play defensively
1082 navigation_goalrating_end(this);
1086 void havocbot_role_kh_defense(entity this)
1093 LOG_TRACE("changing role to carrier");
1094 this.havocbot_role = havocbot_role_kh_carrier;
1095 this.havocbot_role_timeout = 0;
1099 if (!this.havocbot_role_timeout)
1100 this.havocbot_role_timeout = time + random() * 10 + 20;
1101 if (time > this.havocbot_role_timeout)
1103 LOG_TRACE("changing role to freelancer");
1104 this.havocbot_role = havocbot_role_kh_freelancer;
1105 this.havocbot_role_timeout = 0;
1109 if (this.bot_strategytime < time)
1111 float key_owner_team;
1112 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1113 navigation_goalrating_start(this);
1115 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1116 if(key_owner_team == this.team)
1117 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend key carriers
1118 else if(key_owner_team == -1)
1119 havocbot_goalrating_kh(this, 4, 1, 0.1); // play defensively
1121 havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK ANYWAY
1123 navigation_goalrating_end(this);
1127 void havocbot_role_kh_offense(entity this)
1134 LOG_TRACE("changing role to carrier");
1135 this.havocbot_role = havocbot_role_kh_carrier;
1136 this.havocbot_role_timeout = 0;
1140 if (!this.havocbot_role_timeout)
1141 this.havocbot_role_timeout = time + random() * 10 + 20;
1142 if (time > this.havocbot_role_timeout)
1144 LOG_TRACE("changing role to freelancer");
1145 this.havocbot_role = havocbot_role_kh_freelancer;
1146 this.havocbot_role_timeout = 0;
1150 if (this.bot_strategytime < time)
1152 float key_owner_team;
1154 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1155 navigation_goalrating_start(this);
1157 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1158 if(key_owner_team == this.team)
1159 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend anyway
1160 else if(key_owner_team == -1)
1161 havocbot_goalrating_kh(this, 0.1, 1, 4); // play offensively
1163 havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK! EMERGENCY!
1165 navigation_goalrating_end(this);
1169 void havocbot_role_kh_freelancer(entity this)
1176 LOG_TRACE("changing role to carrier");
1177 this.havocbot_role = havocbot_role_kh_carrier;
1178 this.havocbot_role_timeout = 0;
1182 if (!this.havocbot_role_timeout)
1183 this.havocbot_role_timeout = time + random() * 10 + 10;
1184 if (time > this.havocbot_role_timeout)
1188 LOG_TRACE("changing role to offense");
1189 this.havocbot_role = havocbot_role_kh_offense;
1193 LOG_TRACE("changing role to defense");
1194 this.havocbot_role = havocbot_role_kh_defense;
1196 this.havocbot_role_timeout = 0;
1200 if (this.bot_strategytime < time)
1202 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1203 navigation_goalrating_start(this);
1205 int key_owner_team = kh_Key_AllOwnedByWhichTeam();
1206 if(key_owner_team == this.team)
1207 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend anyway
1208 else if(key_owner_team == -1)
1209 havocbot_goalrating_kh(this, 1, 10, 4); // prefer dropped keys
1211 havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK ANYWAY
1213 navigation_goalrating_end(this);
1218 // register this as a mutator
1220 MUTATOR_HOOKFUNCTION(kh, ClientDisconnect)
1222 entity player = M_ARGV(0, entity);
1224 kh_Key_DropAll(player, true);
1227 MUTATOR_HOOKFUNCTION(kh, MakePlayerObserver)
1229 entity player = M_ARGV(0, entity);
1231 kh_Key_DropAll(player, true);
1234 MUTATOR_HOOKFUNCTION(kh, PlayerDies)
1236 entity frag_attacker = M_ARGV(1, entity);
1237 entity frag_target = M_ARGV(2, entity);
1239 if(frag_target == frag_attacker)
1240 kh_Key_DropAll(frag_target, true);
1241 else if(IS_PLAYER(frag_attacker))
1242 kh_Key_DropAll(frag_target, false);
1244 kh_Key_DropAll(frag_target, true);
1247 MUTATOR_HOOKFUNCTION(kh, GiveFragsForKill, CBC_ORDER_FIRST)
1249 entity frag_attacker = M_ARGV(0, entity);
1250 entity frag_target = M_ARGV(1, entity);
1251 float frag_score = M_ARGV(2, float);
1252 M_ARGV(2, float) = kh_HandleFrags(frag_attacker, frag_target, frag_score);
1255 MUTATOR_HOOKFUNCTION(kh, MatchEnd)
1260 MUTATOR_HOOKFUNCTION(kh, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
1262 M_ARGV(0, float) = kh_teams;
1265 MUTATOR_HOOKFUNCTION(kh, SpectateCopy)
1267 entity spectatee = M_ARGV(0, entity);
1268 entity client = M_ARGV(1, entity);
1270 client.kh_state = spectatee.kh_state;
1273 MUTATOR_HOOKFUNCTION(kh, PlayerUseKey)
1275 entity player = M_ARGV(0, entity);
1277 if(MUTATOR_RETURNVALUE == 0)
1279 entity k = player.kh_next;
1288 MUTATOR_HOOKFUNCTION(kh, HavocBot_ChooseRole)
1290 entity bot = M_ARGV(0, entity);
1295 float r = random() * 3;
1297 bot.havocbot_role = havocbot_role_kh_offense;
1299 bot.havocbot_role = havocbot_role_kh_defense;
1301 bot.havocbot_role = havocbot_role_kh_freelancer;
1306 MUTATOR_HOOKFUNCTION(kh, DropSpecialItems)
1308 entity frag_target = M_ARGV(0, entity);
1310 kh_Key_DropAll(frag_target, false);
1313 MUTATOR_HOOKFUNCTION(kh, reset_map_global)
1315 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round + (game_starttime - time), kh_StartRound);