1 #include "gamemode_keyhunt.qh"
2 #ifndef GAMEMODE_KEYHUNT_H
3 #define GAMEMODE_KEYHUNT_H
5 #define autocvar_g_keyhunt_point_limit cvar("g_keyhunt_point_limit")
6 int autocvar_g_keyhunt_point_leadlimit;
7 bool autocvar_g_keyhunt_team_spawns;
10 REGISTER_MUTATOR(kh, false)
14 if (time > 1) // game loads at time 1
15 error("This is a game type and it cannot be added at runtime.");
19 SetLimits(autocvar_g_keyhunt_point_limit, autocvar_g_keyhunt_point_leadlimit, autocvar_timelimit_override, -1);
20 if (autocvar_g_keyhunt_team_spawns)
21 have_team_spawns = -1; // request team spawns
24 MUTATOR_ONROLLBACK_OR_REMOVE
26 // we actually cannot roll back kh_Initialize here
27 // BUT: we don't need to! If this gets called, adding always
33 LOG_INFO("This is a game type and it cannot be removed at runtime.");
40 #define FOR_EACH_KH_KEY(v) for(v = kh_worldkeylist; v; v = v.kh_worldkeynext )
42 // ALL OF THESE should be removed in the future, as other code should not have to care
45 float kh_tracking_enabled;
47 float kh_Key_AllOwnedByWhichTeam();
49 USING(kh_Think_t, void());
51 void kh_Controller_SetThink(float t, kh_Think_t func);
53 entity kh_worldkeylist;
54 .entity kh_worldkeynext;
59 float autocvar_g_balance_keyhunt_damageforcescale;
60 float autocvar_g_balance_keyhunt_delay_collect;
61 float autocvar_g_balance_keyhunt_delay_return;
62 float autocvar_g_balance_keyhunt_delay_round;
63 float autocvar_g_balance_keyhunt_delay_tracking;
64 float autocvar_g_balance_keyhunt_dropvelocity;
65 float autocvar_g_balance_keyhunt_maxdist;
66 float autocvar_g_balance_keyhunt_protecttime;
68 int autocvar_g_balance_keyhunt_score_capture;
69 int autocvar_g_balance_keyhunt_score_carrierfrag;
70 int autocvar_g_balance_keyhunt_score_collect;
71 int autocvar_g_balance_keyhunt_score_destroyed;
72 int autocvar_g_balance_keyhunt_score_destroyed_ownfactor;
73 int autocvar_g_balance_keyhunt_score_push;
74 float autocvar_g_balance_keyhunt_throwvelocity;
76 int autocvar_g_keyhunt_teams;
77 int autocvar_g_keyhunt_teams_override;
79 // #define KH_PLAYER_USE_ATTACHMENT
80 // #define KH_PLAYER_USE_CARRIEDMODEL
82 #ifdef KH_PLAYER_USE_ATTACHMENT
83 const vector KH_PLAYER_ATTACHMENT_DIST_ROTATED = '0 -4 0';
84 const vector KH_PLAYER_ATTACHMENT_DIST = '4 0 0';
85 const vector KH_PLAYER_ATTACHMENT = '0 0 0';
86 const vector KH_PLAYER_ATTACHMENT_ANGLES = '0 0 0';
87 const string KH_PLAYER_ATTACHMENT_BONE = "";
89 const float KH_KEY_ZSHIFT = 22;
90 const float KH_KEY_XYDIST = 24;
91 const float KH_KEY_XYSPEED = 45;
93 const float KH_KEY_WP_ZSHIFT = 20;
95 const vector KH_KEY_MIN = '-10 -10 -46';
96 const vector KH_KEY_MAX = '10 10 3';
97 const float KH_KEY_BRIGHTNESS = 2;
99 float kh_no_radar_circles;
102 // bits 0- 4: team of key 1, or 0 for no such key, or 30 for dropped, or 31 for self
103 // bits 5- 9: team of key 2, or 0 for no such key, or 30 for dropped, or 31 for self
104 // bits 10-14: team of key 3, or 0 for no such key, or 30 for dropped, or 31 for self
105 // bits 15-19: team of key 4, or 0 for no such key, or 30 for dropped, or 31 for self
106 .float kh_state = _STAT(KH_KEYS);
107 .float siren_time; // time delay the siren
108 //.float stuff_time; // time delay to stuffcmd a cvar
110 float kh_keystatus[17];
111 //kh_keystatus[0] = status of dropped keys, kh_keystatus[1 - 16] = player #
112 //replace 17 with cvar("maxplayers") or similar !!!!!!!!!
113 //for(i = 0; i < maxplayers; ++i)
114 // kh_keystatus[i] = "0";
116 float kh_Team_ByID(float t)
118 if(t == 0) return NUM_TEAM_1;
119 if(t == 1) return NUM_TEAM_2;
120 if(t == 2) return NUM_TEAM_3;
121 if(t == 3) return NUM_TEAM_4;
125 //entity kh_worldkeylist;
126 .entity kh_worldkeynext;
127 entity kh_controller;
128 //float kh_tracking_enabled;
130 float kh_interferemsg_time, kh_interferemsg_team;
131 .entity kh_next, kh_prev; // linked list
133 .float kh_dropperteam;
134 .entity kh_previous_owner;
135 .float kh_previous_owner_playerid;
136 .float kh_cp_duration;
138 float kh_key_dropped, kh_key_carried;
140 const float ST_KH_CAPS = 1;
141 void kh_ScoreRules(float teams)
143 ScoreRules_basics(teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, true);
144 ScoreInfo_SetLabel_TeamScore( ST_KH_CAPS, "caps", SFL_SORT_PRIO_SECONDARY);
145 ScoreInfo_SetLabel_PlayerScore(SP_KH_CAPS, "caps", SFL_SORT_PRIO_SECONDARY);
146 ScoreInfo_SetLabel_PlayerScore(SP_KH_PUSHES, "pushes", 0);
147 ScoreInfo_SetLabel_PlayerScore(SP_KH_DESTROYS, "destroyed", SFL_LOWER_IS_BETTER);
148 ScoreInfo_SetLabel_PlayerScore(SP_KH_PICKUPS, "pickups", 0);
149 ScoreInfo_SetLabel_PlayerScore(SP_KH_KCKILLS, "kckills", 0);
150 ScoreInfo_SetLabel_PlayerScore(SP_KH_LOSSES, "losses", SFL_LOWER_IS_BETTER);
151 ScoreRules_basics_end();
154 bool kh_KeyCarrier_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs all the time
156 if(!IS_PLAYER(view) || DIFF_TEAM(this, view))
157 if(!kh_tracking_enabled)
163 bool kh_Key_waypointsprite_visible_for_player(entity this, entity player, entity view)
165 if(!kh_tracking_enabled)
169 if(!this.owner.owner)
171 return false; // draw only when key is not owned
174 void kh_update_state()
187 s |= pow(32, key.count) * f;
190 FOREACH_CLIENT(true, LAMBDA(it.kh_state = s));
195 key.owner.kh_state |= pow(32, key.count) * 31;
197 //print(ftos((nextent(NULL)).kh_state), "\n");
203 var kh_Think_t kh_Controller_Thinkfunc;
204 void kh_Controller_SetThink(float t, kh_Think_t func) // runs occasionaly
206 kh_Controller_Thinkfunc = func;
207 kh_controller.cnt = ceil(t);
209 kh_controller.nextthink = time; // force
211 void kh_WaitForPlayers();
212 void kh_Controller_Think(entity this) // called a lot
214 if(intermission_running)
217 { if(getthink(this) != kh_WaitForPlayers) { this.cnt -= 1; } }
218 else if(this.cnt == 0)
221 kh_Controller_Thinkfunc();
223 this.nextthink = time + 1;
226 // frags f: take from cvar * f
228 void kh_Scores_Event(entity player, entity key, string what, float frags_player, float frags_owner) // update the score when a key is captured
231 if(intermission_running)
235 UpdateFrags(player, frags_player);
237 if(key && key.owner && frags_owner)
238 UpdateFrags(key.owner, frags_owner);
240 if(!autocvar_sv_eventlog) //output extra info to the console or text file
243 s = strcat(":keyhunt:", what, ":", ftos(player.playerid), ":", ftos(frags_player));
246 s = strcat(s, ":", ftos(key.owner.playerid));
250 s = strcat(s, ":", ftos(frags_owner), ":");
253 s = strcat(s, key.netname);
258 vector kh_AttachedOrigin(entity e) // runs when a team captures the flag, it can run 2 or 3 times.
262 makevectors(e.tag_entity.angles);
263 return e.tag_entity.origin + e.origin.x * v_forward - e.origin.y * v_right + e.origin.z * v_up;
269 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
271 #ifdef KH_PLAYER_USE_ATTACHMENT
273 first = key.owner.kh_next;
276 setattachment(key, key.owner, KH_PLAYER_ATTACHMENT_BONE);
279 setattachment(key.kh_next, key, "");
280 setorigin(key, key.kh_next.origin - 0.5 * KH_PLAYER_ATTACHMENT_DIST);
281 setorigin(key.kh_next, KH_PLAYER_ATTACHMENT_DIST_ROTATED);
282 key.kh_next.angles = '0 0 0';
285 setorigin(key, KH_PLAYER_ATTACHMENT);
286 key.angles = KH_PLAYER_ATTACHMENT_ANGLES;
290 setattachment(key, key.kh_prev, "");
292 setattachment(key.kh_next, key, "");
293 setorigin(key, KH_PLAYER_ATTACHMENT_DIST_ROTATED);
294 setorigin(first, first.origin - 0.5 * KH_PLAYER_ATTACHMENT_DIST);
295 key.angles = '0 0 0';
298 setattachment(key, key.owner, "");
299 setorigin(key, '0 0 1' * KH_KEY_ZSHIFT); // fixing x, y in think
300 key.angles_y -= key.owner.angles.y;
303 key.solid = SOLID_NOT;
304 key.movetype = MOVETYPE_NONE;
305 key.team = key.owner.team;
306 key.nextthink = time;
307 key.damageforcescale = 0;
308 key.takedamage = DAMAGE_NO;
309 key.modelindex = kh_key_carried;
312 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
314 #ifdef KH_PLAYER_USE_ATTACHMENT
316 first = key.owner.kh_next;
321 setattachment(key.kh_next, key.owner, KH_PLAYER_ATTACHMENT_BONE);
322 setorigin(key.kh_next, key.origin + 0.5 * KH_PLAYER_ATTACHMENT_DIST);
323 key.kh_next.angles = KH_PLAYER_ATTACHMENT_ANGLES;
329 setattachment(key.kh_next, key.kh_prev, "");
330 setorigin(first, first.origin + 0.5 * KH_PLAYER_ATTACHMENT_DIST);
333 setattachment(key, NULL, "");
334 setorigin(key, key.owner.origin + '0 0 1' * (STAT(PL_MIN, NULL).z - KH_KEY_MIN_z));
335 key.angles = key.owner.angles;
337 setorigin(key, key.owner.origin + key.origin.z * '0 0 1');
338 setattachment(key, NULL, "");
339 key.angles_y += key.owner.angles.y;
342 key.solid = SOLID_TRIGGER;
343 key.movetype = MOVETYPE_TOSS;
344 key.pain_finished = time + autocvar_g_balance_keyhunt_delay_return;
345 key.damageforcescale = autocvar_g_balance_keyhunt_damageforcescale;
346 key.takedamage = DAMAGE_YES;
348 key.modelindex = kh_key_dropped;
349 key.kh_previous_owner = key.owner;
350 key.kh_previous_owner_playerid = key.owner.playerid;
353 void kh_Key_AssignTo(entity key, entity player) // runs every time a key is picked up or assigned. Runs prior to kh_key_attach
356 float ownerteam0, ownerteam;
357 if(key.owner == player)
360 ownerteam0 = kh_Key_AllOwnedByWhichTeam();
366 // remove from linked list
368 key.kh_next.kh_prev = key.kh_prev;
369 key.kh_prev.kh_next = key.kh_next;
373 if(key.owner.kh_next == NULL)
375 // No longer a key carrier
376 if(!kh_no_radar_circles)
377 WaypointSprite_Ping(key.owner.waypointsprite_attachedforcarrier);
378 WaypointSprite_DetachCarrier(key.owner);
386 // insert into linked list
387 key.kh_next = player.kh_next;
388 key.kh_prev = player;
389 player.kh_next = key;
391 key.kh_next.kh_prev = key;
394 i = kh_keystatus[key.owner.playerid];
395 if(key.netname == "^1red key")
397 if(key.netname == "^4blue key")
399 if(key.netname == "^3yellow key")
401 if(key.netname == "^6pink key")
403 kh_keystatus[key.owner.playerid] = i;
407 if(key.kh_next == NULL)
409 // player is now a key carrier
410 entity wp = WaypointSprite_AttachCarrier(WP_Null, player, RADARICON_FLAGCARRIER);
411 wp.colormod = colormapPaletteColor(player.team - 1, 0);
412 player.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = kh_KeyCarrier_waypointsprite_visible_for_player;
413 WaypointSprite_UpdateRule(player.waypointsprite_attachedforcarrier, player.team, SPRITERULE_TEAMPLAY);
414 if(player.team == NUM_TEAM_1)
415 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierRed, WP_KeyCarrierFriend, WP_KeyCarrierRed);
416 else if(player.team == NUM_TEAM_2)
417 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierBlue, WP_KeyCarrierFriend, WP_KeyCarrierBlue);
418 else if(player.team == NUM_TEAM_3)
419 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierYellow, WP_KeyCarrierFriend, WP_KeyCarrierYellow);
420 else if(player.team == NUM_TEAM_4)
421 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierPink, WP_KeyCarrierFriend, WP_KeyCarrierPink);
422 if(!kh_no_radar_circles)
423 WaypointSprite_Ping(player.waypointsprite_attachedforcarrier);
427 // moved that here, also update if there's no player
432 ownerteam = kh_Key_AllOwnedByWhichTeam();
433 if(ownerteam != ownerteam0)
437 kh_interferemsg_time = time + 0.2;
438 kh_interferemsg_team = player.team;
440 // audit all key carrier sprites, update them to RUN HERE
443 if (!k.owner) continue;
444 entity first = WP_Null;
445 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model1, LAMBDA(first = it; break));
446 entity third = WP_Null;
447 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model3, LAMBDA(third = it; break));
448 WaypointSprite_UpdateSprites(k.owner.waypointsprite_attachedforcarrier, first, WP_KeyCarrierFinish, third);
453 kh_interferemsg_time = 0;
455 // audit all key carrier sprites, update them to RUN HERE
458 if (!k.owner) continue;
459 entity first = WP_Null;
460 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model1, LAMBDA(first = it; break));
461 entity third = WP_Null;
462 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model3, LAMBDA(third = it; break));
463 WaypointSprite_UpdateSprites(k.owner.waypointsprite_attachedforcarrier, first, WP_KeyCarrierFriend, third);
469 void kh_Key_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
473 if(ITEM_DAMAGE_NEEDKILL(deathtype))
475 // touching lava, or hurt trigger
477 // immediately return is bad
478 // maybe start a shorter countdown?
482 if(time > this.pushltime)
483 if(IS_PLAYER(attacker))
484 this.team = attacker.team;
487 void kh_Key_Collect(entity key, entity player) //a player picks up a dropped key
489 sound(player, CH_TRIGGER, SND_KH_COLLECT, VOL_BASE, ATTEN_NORM);
491 if(key.kh_dropperteam != player.team)
493 kh_Scores_Event(player, key, "collect", autocvar_g_balance_keyhunt_score_collect, 0);
494 PlayerScore_Add(player, SP_KH_PICKUPS, 1);
496 key.kh_dropperteam = 0;
497 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_ENT(key, INFO_KEYHUNT_PICKUP), player.netname);
499 kh_Key_AssignTo(key, player); // this also updates .kh_state
502 void kh_Key_Touch(entity this) // runs many, many times when a key has been dropped and can be picked up
504 if(intermission_running)
507 if(this.owner) // already carried
510 if(ITEM_TOUCH_NEEDKILL())
512 // touching sky, or nodrop
514 // immediately return is bad
515 // maybe start a shorter countdown?
518 if (!IS_PLAYER(other))
522 if(other == this.enemy)
523 if(time < this.kh_droptime + autocvar_g_balance_keyhunt_delay_collect)
524 return; // you just dropped it!
525 kh_Key_Collect(this, other);
528 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
532 kh_Key_AssignTo(key, NULL);
533 if(o) // it was attached
534 WaypointSprite_Kill(key.waypointsprite_attachedforcarrier);
535 else // it was dropped
536 WaypointSprite_DetachCarrier(key);
538 // remove key from key list
539 if (kh_worldkeylist == key)
540 kh_worldkeylist = kh_worldkeylist.kh_worldkeynext;
546 if (o.kh_worldkeynext == key)
548 o.kh_worldkeynext = o.kh_worldkeynext.kh_worldkeynext;
551 o = o.kh_worldkeynext;
560 void kh_FinishRound() // runs when a team captures the keys
562 // prepare next round
563 kh_interferemsg_time = 0;
566 kh_no_radar_circles = true;
569 kh_no_radar_circles = false;
571 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_ROUNDSTART, autocvar_g_balance_keyhunt_delay_round);
572 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round, kh_StartRound);
575 void kh_WinnerTeam(float teem) // runs when a team wins // Samual: Teem?.... TEEM?!?! what the fuck is wrong with you people
577 // all key carriers get some points
578 vector firstorigin, lastorigin, midpoint;
582 score = (kh_teams - 1) * autocvar_g_balance_keyhunt_score_capture;
583 DistributeEvenly_Init(score, kh_teams);
584 // twice the score for 3 team games, three times the score for 4 team games!
585 // note: for a win by destroying the key, this should NOT be applied
589 f = DistributeEvenly_Get(1);
590 kh_Scores_Event(key.owner, key, "capture", f, 0);
591 PlayerTeamScore_Add(key.owner, SP_KH_CAPS, ST_KH_CAPS, 1);
592 nades_GiveBonus(key.owner, autocvar_g_nades_bonus_score_high);
596 string keyowner = "";
598 if(key.owner.kh_next == key)
601 keyowner = strcat(keyowner, ", ");
602 keyowner = key.owner.netname;
606 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(teem, INFO_KEYHUNT_CAPTURE), keyowner);
610 firstorigin = '0 0 0';
611 lastorigin = '0 0 0';
616 thisorigin = kh_AttachedOrigin(key);
617 //dprint("Key origin: ", vtos(thisorigin), "\n");
618 midpoint += thisorigin;
621 te_lightning2(NULL, lastorigin, thisorigin);
622 lastorigin = thisorigin;
624 firstorigin = thisorigin;
629 te_lightning2(NULL, lastorigin, firstorigin);
631 midpoint = midpoint * (1 / kh_teams);
632 te_customflash(midpoint, 1000, 1, Team_ColorRGB(teem) * 0.5 + '0.5 0.5 0.5'); // make the color >=0.5 in each component
634 play2all(SND(KH_CAPTURE));
638 void kh_LoserTeam(float teem, entity lostkey) // runs when a player pushes a flag carrier off the map
640 entity key, attacker;
647 if(lostkey.pusher.team != teem)
648 if(IS_PLAYER(lostkey.pusher))
649 attacker = lostkey.pusher;
655 if(lostkey.kh_previous_owner)
656 kh_Scores_Event(lostkey.kh_previous_owner, NULL, "pushed", 0, -autocvar_g_balance_keyhunt_score_push);
657 // don't actually GIVE him the -nn points, just log
658 kh_Scores_Event(attacker, NULL, "push", autocvar_g_balance_keyhunt_score_push, 0);
659 PlayerScore_Add(attacker, SP_KH_PUSHES, 1);
660 //centerprint(attacker, "Your push is the best!"); // does this really need to exist?
664 float of, fragsleft, i, j, thisteam;
665 of = autocvar_g_balance_keyhunt_score_destroyed_ownfactor;
667 FOREACH_CLIENT(IS_PLAYER(it) && it.team != teem, LAMBDA(++players));
670 if(key.owner && key.team != teem)
673 if(lostkey.kh_previous_owner)
674 kh_Scores_Event(lostkey.kh_previous_owner, NULL, "destroyed", 0, -autocvar_g_balance_keyhunt_score_destroyed);
675 // don't actually GIVE him the -nn points, just log
677 if(lostkey.kh_previous_owner.playerid == lostkey.kh_previous_owner_playerid)
678 PlayerScore_Add(lostkey.kh_previous_owner, SP_KH_DESTROYS, 1);
680 DistributeEvenly_Init(autocvar_g_balance_keyhunt_score_destroyed, keys * of + players);
683 if(key.owner && key.team != teem)
685 f = DistributeEvenly_Get(of);
686 kh_Scores_Event(key.owner, NULL, "destroyed_holdingkey", f, 0);
689 fragsleft = DistributeEvenly_Get(players);
691 // Now distribute these among all other teams...
693 for(i = 0; i < kh_teams; ++i)
695 thisteam = kh_Team_ByID(i);
696 if(thisteam == teem) // bad boy, no cookie - this WILL happen
700 FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, LAMBDA(++players));
702 DistributeEvenly_Init(fragsleft, j);
703 fragsleft = DistributeEvenly_Get(j - 1);
704 DistributeEvenly_Init(DistributeEvenly_Get(1), players);
706 FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, LAMBDA(
707 f = DistributeEvenly_Get(1);
708 kh_Scores_Event(it, NULL, "destroyed", f, 0);
715 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_ENT(lostkey, INFO_KEYHUNT_LOST), lostkey.kh_previous_owner.netname);
717 play2all(SND(KH_DESTROY));
718 te_tarexplosion(lostkey.origin);
723 void kh_Key_Think(entity this) // runs all the time
725 if(intermission_running)
730 #ifndef KH_PLAYER_USE_ATTACHMENT
731 makevectors('0 1 0' * (this.cnt + (time % 360) * KH_KEY_XYSPEED));
732 setorigin(this, v_forward * KH_KEY_XYDIST + '0 0 1' * this.origin.z);
736 // if in nodrop or time over, end the round
738 if(time > this.pain_finished)
739 kh_LoserTeam(this.team, this);
742 if(kh_Key_AllOwnedByWhichTeam() != -1)
744 if(this.siren_time < time)
746 sound(this.owner, CH_TRIGGER, SND_KH_ALARM, VOL_BASE, ATTEN_NORM); // play a simple alarm
747 this.siren_time = time + 2.5; // repeat every 2.5 seconds
752 p = this.owner.origin;
754 if(vdist(key.owner.origin - p, >, autocvar_g_balance_keyhunt_maxdist))
756 kh_WinnerTeam(this.team);
760 if(kh_interferemsg_time && time > kh_interferemsg_time)
762 kh_interferemsg_time = 0;
763 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
764 if(it.team == kh_interferemsg_team)
766 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_MEET);
768 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_HELP);
770 Send_Notification(NOTIF_ONE, it, MSG_CENTER, APP_TEAM_NUM(kh_interferemsg_team, CENTER_KEYHUNT_INTERFERE));
774 this.nextthink = time + 0.05;
777 void key_reset(entity this)
779 kh_Key_AssignTo(this, NULL);
783 const string STR_ITEM_KH_KEY = "item_kh_key";
784 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
786 entity key = spawn();
788 key.classname = STR_ITEM_KH_KEY;
789 settouch(key, kh_Key_Touch);
790 setthink(key, kh_Key_Think);
791 key.nextthink = time;
792 key.items = IT_KEY1 | IT_KEY2;
794 key.angles = '0 360 0' * random();
795 key.event_damage = kh_Key_Damage;
796 key.takedamage = DAMAGE_YES;
797 key.modelindex = kh_key_dropped;
799 key.kh_dropperteam = 0;
800 key.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
801 setsize(key, KH_KEY_MIN, KH_KEY_MAX);
802 key.colormod = Team_ColorRGB(initial_owner.team) * KH_KEY_BRIGHTNESS;
803 key.reset = key_reset;
805 switch(initial_owner.team)
808 key.netname = "^1red key";
811 key.netname = "^4blue key";
814 key.netname = "^3yellow key";
817 key.netname = "^6pink key";
820 key.netname = "NETGIER key";
824 // link into key list
825 key.kh_worldkeynext = kh_worldkeylist;
826 kh_worldkeylist = key;
828 Send_Notification(NOTIF_ONE, initial_owner, MSG_CENTER, APP_TEAM_NUM(initial_owner.team, CENTER_KEYHUNT_START));
830 WaypointSprite_Spawn(WP_KeyDropped, 0, 0, key, '0 0 1' * KH_KEY_WP_ZSHIFT, NULL, key.team, key, waypointsprite_attachedforcarrier, false, RADARICON_FLAG);
831 key.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = kh_Key_waypointsprite_visible_for_player;
833 kh_Key_AssignTo(key, initial_owner);
836 // -1 when no team completely owns all keys yet
837 float kh_Key_AllOwnedByWhichTeam() // constantly called. check to see if all the keys are owned by the same team
851 else if(teem != key.team)
860 void kh_Key_DropOne(entity key)
862 // prevent collecting this one for some time
866 key.kh_droptime = time;
869 kh_Scores_Event(player, key, "dropkey", 0, 0);
870 PlayerScore_Add(player, SP_KH_LOSSES, 1);
871 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_ENT(key, INFO_KEYHUNT_DROP), player.netname);
873 kh_Key_AssignTo(key, NULL);
874 makevectors(player.v_angle);
875 key.velocity = W_CalculateProjectileVelocity(player, player.velocity, autocvar_g_balance_keyhunt_throwvelocity * v_forward, false);
877 key.pushltime = time + autocvar_g_balance_keyhunt_protecttime;
878 key.kh_dropperteam = key.team;
880 sound(player, CH_TRIGGER, SND_KH_DROP, VOL_BASE, ATTEN_NORM);
883 void kh_Key_DropAll(entity player, float suicide) // runs whenever a player dies
891 if(time < player.pushltime)
892 mypusher = player.pusher;
893 while((key = player.kh_next))
895 kh_Scores_Event(player, key, "losekey", 0, 0);
896 PlayerScore_Add(player, SP_KH_LOSSES, 1);
897 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_ENT(key, INFO_KEYHUNT_LOST), player.netname);
898 kh_Key_AssignTo(key, NULL);
899 makevectors('-1 0 0' * (45 + 45 * random()) + '0 360 0' * random());
900 key.velocity = W_CalculateProjectileVelocity(player, player.velocity, autocvar_g_balance_keyhunt_dropvelocity * v_forward, false);
901 key.pusher = mypusher;
902 key.pushltime = time + autocvar_g_balance_keyhunt_protecttime;
904 key.kh_dropperteam = player.team;
906 sound(player, CH_TRIGGER, SND_KH_DROP, VOL_BASE, ATTEN_NORM);
910 float kh_CheckPlayers(float num)
914 float t_team = kh_Team_ByID(num);
916 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
917 if(!IS_DEAD(it) && !PHYS_INPUT_BUTTON_CHAT(it) && it.team == t_team)
921 if (!players) { return t_team; }
926 #define KH_READY_TEAMS() (!p1 + !p2 + ((kh_teams >= 3) ? !p3 : p3) + ((kh_teams >= 4) ? !p4 : p4))
927 #define KH_READY_TEAMS_OK() (KH_READY_TEAMS() == kh_teams)
928 void kh_WaitForPlayers() // delay start of the round until enough players are present
930 if(time < game_starttime)
932 kh_Controller_SetThink(game_starttime - time + 0.1, kh_WaitForPlayers);
936 static float prev_missing_teams_mask;
937 float p1 = kh_CheckPlayers(0), p2 = kh_CheckPlayers(1), p3 = kh_CheckPlayers(2), p4 = kh_CheckPlayers(3);
938 if(KH_READY_TEAMS_OK())
940 if(prev_missing_teams_mask > 0)
941 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
942 prev_missing_teams_mask = -1;
943 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_ROUNDSTART, autocvar_g_balance_keyhunt_delay_round);
944 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round, kh_StartRound);
948 if(player_count == 0)
950 if(prev_missing_teams_mask > 0)
951 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
952 prev_missing_teams_mask = -1;
956 float missing_teams_mask = boolean(p1) + boolean(p2) * 2;
957 if(kh_teams >= 3) missing_teams_mask += boolean(p3) * 4;
958 if(kh_teams >= 4) missing_teams_mask += boolean(p4) * 8;
959 if(prev_missing_teams_mask != missing_teams_mask)
961 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
962 prev_missing_teams_mask = missing_teams_mask;
965 kh_Controller_SetThink(1, kh_WaitForPlayers);
969 void kh_EnableTrackingDevice() // runs after each round
971 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT);
972 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT_OTHER);
974 kh_tracking_enabled = true;
977 void kh_StartRound() // runs at the start of each round
979 float i, players, teem;
981 if(time < game_starttime)
983 kh_Controller_SetThink(game_starttime - time + 0.1, kh_WaitForPlayers);
987 float p1 = kh_CheckPlayers(0), p2 = kh_CheckPlayers(1), p3 = kh_CheckPlayers(2), p4 = kh_CheckPlayers(3);
988 if(!KH_READY_TEAMS_OK())
990 kh_Controller_SetThink(1, kh_WaitForPlayers);
994 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT);
995 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_KEYHUNT_OTHER);
997 for(i = 0; i < kh_teams; ++i)
999 teem = kh_Team_ByID(i);
1001 entity my_player = NULL;
1002 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
1003 if(!IS_DEAD(it) && !PHYS_INPUT_BUTTON_CHAT(it) && it.team == teem)
1006 if(random() * players <= 1)
1010 kh_Key_Spawn(my_player, 360 * i / kh_teams, i);
1013 kh_tracking_enabled = false;
1014 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEYHUNT_SCAN, autocvar_g_balance_keyhunt_delay_tracking);
1015 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_tracking, kh_EnableTrackingDevice);
1018 float kh_HandleFrags(entity attacker, entity targ, float f) // adds to the player score
1020 if(attacker == targ)
1025 if(attacker.team == targ.team)
1030 for(k = targ.kh_next; k != NULL; k = k.kh_next)
1032 kh_Scores_Event(attacker, targ.kh_next, "carrierfrag", -nk * autocvar_g_balance_keyhunt_score_collect, 0);
1036 kh_Scores_Event(attacker, targ.kh_next, "carrierfrag", autocvar_g_balance_keyhunt_score_carrierfrag-1, 0);
1037 PlayerScore_Add(attacker, SP_KH_KCKILLS, 1);
1038 // the frag gets added later
1045 void kh_Initialize() // sets up th KH environment
1048 kh_teams = autocvar_g_keyhunt_teams_override;
1050 kh_teams = autocvar_g_keyhunt_teams;
1051 kh_teams = bound(2, kh_teams, 4);
1053 // make a KH entity for controlling the game
1054 kh_controller = spawn();
1055 setthink(kh_controller, kh_Controller_Think);
1056 kh_Controller_SetThink(0, kh_WaitForPlayers);
1058 setmodel(kh_controller, MDL_KH_KEY);
1059 kh_key_dropped = kh_controller.modelindex;
1061 dprint(vtos(kh_controller.mins));
1062 dprint(vtos(kh_controller.maxs));
1065 #ifdef KH_PLAYER_USE_CARRIEDMODEL
1066 setmodel(kh_controller, MDL_KH_KEY_CARRIED);
1067 kh_key_carried = kh_controller.modelindex;
1069 kh_key_carried = kh_key_dropped;
1072 kh_controller.model = "";
1073 kh_controller.modelindex = 0;
1075 kh_ScoreRules(kh_teams);
1080 // to be called before intermission
1082 remove(kh_controller);
1083 kh_controller = NULL;
1088 void(entity this) havocbot_role_kh_carrier;
1089 void(entity this) havocbot_role_kh_defense;
1090 void(entity this) havocbot_role_kh_offense;
1091 void(entity this) havocbot_role_kh_freelancer;
1094 void havocbot_goalrating_kh(entity this, float ratingscale_team, float ratingscale_dropped, float ratingscale_enemy)
1097 for (head = kh_worldkeylist; head; head = head.kh_worldkeynext)
1099 if(head.owner == this)
1101 if(!kh_tracking_enabled)
1103 // if it's carried by our team we know about it
1104 // otherwise we have to see it to know about it
1105 if(!head.owner || head.team != this.team)
1107 traceline(this.origin + this.view_ofs, head.origin, MOVE_NOMONSTERS, this);
1108 if (trace_fraction < 1 && trace_ent != head)
1109 continue; // skip what I can't see
1113 navigation_routerating(this, head, ratingscale_dropped * BOT_PICKUP_RATING_HIGH, 100000);
1114 else if(head.team == this.team)
1115 navigation_routerating(this, head.owner, ratingscale_team * BOT_PICKUP_RATING_HIGH, 100000);
1117 navigation_routerating(this, head.owner, ratingscale_enemy * BOT_PICKUP_RATING_HIGH, 100000);
1120 havocbot_goalrating_items(this, 1, this.origin, 10000);
1123 void havocbot_role_kh_carrier(entity this)
1128 if (!(this.kh_next))
1130 LOG_TRACE("changing role to freelancer\n");
1131 this.havocbot_role = havocbot_role_kh_freelancer;
1132 this.havocbot_role_timeout = 0;
1136 if (this.bot_strategytime < time)
1138 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1139 navigation_goalrating_start(this);
1141 if(kh_Key_AllOwnedByWhichTeam() == this.team)
1142 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // bring home
1144 havocbot_goalrating_kh(this, 4, 4, 1); // play defensively
1146 navigation_goalrating_end(this);
1150 void havocbot_role_kh_defense(entity this)
1157 LOG_TRACE("changing role to carrier\n");
1158 this.havocbot_role = havocbot_role_kh_carrier;
1159 this.havocbot_role_timeout = 0;
1163 if (!this.havocbot_role_timeout)
1164 this.havocbot_role_timeout = time + random() * 10 + 20;
1165 if (time > this.havocbot_role_timeout)
1167 LOG_TRACE("changing role to freelancer\n");
1168 this.havocbot_role = havocbot_role_kh_freelancer;
1169 this.havocbot_role_timeout = 0;
1173 if (this.bot_strategytime < time)
1175 float key_owner_team;
1176 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1177 navigation_goalrating_start(this);
1179 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1180 if(key_owner_team == this.team)
1181 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend key carriers
1182 else if(key_owner_team == -1)
1183 havocbot_goalrating_kh(this, 4, 1, 0.1); // play defensively
1185 havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK ANYWAY
1187 navigation_goalrating_end(this);
1191 void havocbot_role_kh_offense(entity this)
1198 LOG_TRACE("changing role to carrier\n");
1199 this.havocbot_role = havocbot_role_kh_carrier;
1200 this.havocbot_role_timeout = 0;
1204 if (!this.havocbot_role_timeout)
1205 this.havocbot_role_timeout = time + random() * 10 + 20;
1206 if (time > this.havocbot_role_timeout)
1208 LOG_TRACE("changing role to freelancer\n");
1209 this.havocbot_role = havocbot_role_kh_freelancer;
1210 this.havocbot_role_timeout = 0;
1214 if (this.bot_strategytime < time)
1216 float key_owner_team;
1218 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1219 navigation_goalrating_start(this);
1221 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1222 if(key_owner_team == this.team)
1223 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend anyway
1224 else if(key_owner_team == -1)
1225 havocbot_goalrating_kh(this, 0.1, 1, 4); // play offensively
1227 havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK! EMERGENCY!
1229 navigation_goalrating_end(this);
1233 void havocbot_role_kh_freelancer(entity this)
1240 LOG_TRACE("changing role to carrier\n");
1241 this.havocbot_role = havocbot_role_kh_carrier;
1242 this.havocbot_role_timeout = 0;
1246 if (!this.havocbot_role_timeout)
1247 this.havocbot_role_timeout = time + random() * 10 + 10;
1248 if (time > this.havocbot_role_timeout)
1252 LOG_TRACE("changing role to offense\n");
1253 this.havocbot_role = havocbot_role_kh_offense;
1257 LOG_TRACE("changing role to defense\n");
1258 this.havocbot_role = havocbot_role_kh_defense;
1260 this.havocbot_role_timeout = 0;
1264 if (this.bot_strategytime < time)
1266 float key_owner_team;
1268 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1269 navigation_goalrating_start(this);
1271 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1272 if(key_owner_team == this.team)
1273 havocbot_goalrating_kh(this, 10, 0.1, 0.1); // defend anyway
1274 else if(key_owner_team == -1)
1275 havocbot_goalrating_kh(this, 1, 10, 4); // prefer dropped keys
1277 havocbot_goalrating_kh(this, 0.1, 0.1, 10); // ATTACK ANYWAY
1279 navigation_goalrating_end(this);
1284 // register this as a mutator
1286 MUTATOR_HOOKFUNCTION(kh, ClientDisconnect)
1288 entity player = M_ARGV(0, entity);
1290 kh_Key_DropAll(player, true);
1293 MUTATOR_HOOKFUNCTION(kh, MakePlayerObserver)
1295 entity player = M_ARGV(0, entity);
1297 kh_Key_DropAll(player, true);
1300 MUTATOR_HOOKFUNCTION(kh, PlayerDies)
1302 entity frag_attacker = M_ARGV(1, entity);
1303 entity frag_target = M_ARGV(2, entity);
1305 if(frag_target == frag_attacker)
1306 kh_Key_DropAll(frag_target, true);
1307 else if(IS_PLAYER(frag_attacker))
1308 kh_Key_DropAll(frag_target, false);
1310 kh_Key_DropAll(frag_target, true);
1313 MUTATOR_HOOKFUNCTION(kh, GiveFragsForKill, CBC_ORDER_FIRST)
1315 entity frag_attacker = M_ARGV(0, entity);
1316 entity frag_target = M_ARGV(1, entity);
1317 float frag_score = M_ARGV(2, float);
1318 M_ARGV(2, float) = kh_HandleFrags(frag_attacker, frag_target, frag_score);
1321 MUTATOR_HOOKFUNCTION(kh, MatchEnd)
1326 MUTATOR_HOOKFUNCTION(kh, GetTeamCount, CBC_ORDER_EXCLUSIVE)
1328 M_ARGV(0, float) = kh_teams;
1331 MUTATOR_HOOKFUNCTION(kh, SpectateCopy)
1333 entity spectatee = M_ARGV(0, entity);
1334 entity client = M_ARGV(1, entity);
1336 client.kh_state = spectatee.kh_state;
1339 MUTATOR_HOOKFUNCTION(kh, PlayerUseKey)
1341 entity player = M_ARGV(0, entity);
1343 if(MUTATOR_RETURNVALUE == 0)
1345 entity k = player.kh_next;
1354 MUTATOR_HOOKFUNCTION(kh, HavocBot_ChooseRole)
1356 entity bot = M_ARGV(0, entity);
1361 float r = random() * 3;
1363 bot.havocbot_role = havocbot_role_kh_offense;
1365 bot.havocbot_role = havocbot_role_kh_defense;
1367 bot.havocbot_role = havocbot_role_kh_freelancer;
1372 MUTATOR_HOOKFUNCTION(kh, DropSpecialItems)
1374 entity frag_target = M_ARGV(0, entity);
1376 kh_Key_DropAll(frag_target, false);
1379 MUTATOR_HOOKFUNCTION(kh, reset_map_global)
1381 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round + (game_starttime - time), kh_StartRound);