1 #ifndef GAMEMODE_KEYHUNT_H
2 #define GAMEMODE_KEYHUNT_H
4 #define autocvar_g_keyhunt_point_limit cvar("g_keyhunt_point_limit")
5 int autocvar_g_keyhunt_point_leadlimit;
6 bool autocvar_g_keyhunt_team_spawns;
9 REGISTER_MUTATOR(kh, false)
13 if (time > 1) // game loads at time 1
14 error("This is a game type and it cannot be added at runtime.");
18 SetLimits(autocvar_g_keyhunt_point_limit, autocvar_g_keyhunt_point_leadlimit, -1, -1);
19 if (autocvar_g_keyhunt_team_spawns)
20 have_team_spawns = -1; // request team spawns
23 MUTATOR_ONROLLBACK_OR_REMOVE
25 // we actually cannot roll back kh_Initialize here
26 // BUT: we don't need to! If this gets called, adding always
32 LOG_INFO("This is a game type and it cannot be removed at runtime.");
39 #define FOR_EACH_KH_KEY(v) for(v = kh_worldkeylist; v; v = v.kh_worldkeynext )
41 // ALL OF THESE should be removed in the future, as other code should not have to care
44 float kh_tracking_enabled;
46 float kh_Key_AllOwnedByWhichTeam();
48 typedef void() kh_Think_t;
50 void kh_Controller_SetThink(float t, kh_Think_t func);
52 entity kh_worldkeylist;
53 .entity kh_worldkeynext;
58 float autocvar_g_balance_keyhunt_damageforcescale;
59 float autocvar_g_balance_keyhunt_delay_collect;
60 float autocvar_g_balance_keyhunt_delay_return;
61 float autocvar_g_balance_keyhunt_delay_round;
62 float autocvar_g_balance_keyhunt_delay_tracking;
63 float autocvar_g_balance_keyhunt_dropvelocity;
64 float autocvar_g_balance_keyhunt_maxdist;
65 float autocvar_g_balance_keyhunt_protecttime;
67 int autocvar_g_balance_keyhunt_score_capture;
68 int autocvar_g_balance_keyhunt_score_carrierfrag;
69 int autocvar_g_balance_keyhunt_score_collect;
70 int autocvar_g_balance_keyhunt_score_destroyed;
71 int autocvar_g_balance_keyhunt_score_destroyed_ownfactor;
72 int autocvar_g_balance_keyhunt_score_push;
73 float autocvar_g_balance_keyhunt_throwvelocity;
75 int autocvar_g_keyhunt_teams;
76 int autocvar_g_keyhunt_teams_override;
78 // #define KH_PLAYER_USE_ATTACHMENT
79 // #define KH_PLAYER_USE_CARRIEDMODEL
81 #ifdef KH_PLAYER_USE_ATTACHMENT
82 const vector KH_PLAYER_ATTACHMENT_DIST_ROTATED = '0 -4 0';
83 const vector KH_PLAYER_ATTACHMENT_DIST = '4 0 0';
84 const vector KH_PLAYER_ATTACHMENT = '0 0 0';
85 const vector KH_PLAYER_ATTACHMENT_ANGLES = '0 0 0';
86 const string KH_PLAYER_ATTACHMENT_BONE = "";
88 const float KH_KEY_ZSHIFT = 22;
89 const float KH_KEY_XYDIST = 24;
90 const float KH_KEY_XYSPEED = 45;
92 const float KH_KEY_WP_ZSHIFT = 20;
94 const vector KH_KEY_MIN = '-10 -10 -46';
95 const vector KH_KEY_MAX = '10 10 3';
96 const float KH_KEY_BRIGHTNESS = 2;
98 float kh_no_radar_circles;
101 // bits 0- 4: team of key 1, or 0 for no such key, or 30 for dropped, or 31 for self
102 // bits 5- 9: team of key 2, or 0 for no such key, or 30 for dropped, or 31 for self
103 // bits 10-14: team of key 3, or 0 for no such key, or 30 for dropped, or 31 for self
104 // bits 15-19: team of key 4, or 0 for no such key, or 30 for dropped, or 31 for self
105 .float kh_state = _STAT(KH_KEYS);
106 .float siren_time; // time delay the siren
107 //.float stuff_time; // time delay to stuffcmd a cvar
109 float kh_keystatus[17];
110 //kh_keystatus[0] = status of dropped keys, kh_keystatus[1 - 16] = player #
111 //replace 17 with cvar("maxplayers") or similar !!!!!!!!!
112 //for(i = 0; i < maxplayers; ++i)
113 // kh_keystatus[i] = "0";
115 float kh_Team_ByID(float t)
117 if(t == 0) return NUM_TEAM_1;
118 if(t == 1) return NUM_TEAM_2;
119 if(t == 2) return NUM_TEAM_3;
120 if(t == 3) return NUM_TEAM_4;
124 //entity kh_worldkeylist;
125 .entity kh_worldkeynext;
126 entity kh_controller;
127 //float kh_tracking_enabled;
129 float kh_interferemsg_time, kh_interferemsg_team;
130 .entity kh_next, kh_prev; // linked list
132 .float kh_dropperteam;
133 .entity kh_previous_owner;
134 .float kh_previous_owner_playerid;
135 .float kh_cp_duration;
137 float kh_key_dropped, kh_key_carried;
139 const float ST_KH_CAPS = 1;
140 const float SP_KH_CAPS = 4;
141 const float SP_KH_PUSHES = 5;
142 const float SP_KH_DESTROYS = 6;
143 const float SP_KH_PICKUPS = 7;
144 const float SP_KH_KCKILLS = 8;
145 const float SP_KH_LOSSES = 9;
146 void kh_ScoreRules(float teams)
148 ScoreRules_basics(teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, true);
149 ScoreInfo_SetLabel_TeamScore( ST_KH_CAPS, "caps", SFL_SORT_PRIO_SECONDARY);
150 ScoreInfo_SetLabel_PlayerScore(SP_KH_CAPS, "caps", SFL_SORT_PRIO_SECONDARY);
151 ScoreInfo_SetLabel_PlayerScore(SP_KH_PUSHES, "pushes", 0);
152 ScoreInfo_SetLabel_PlayerScore(SP_KH_DESTROYS, "destroyed", SFL_LOWER_IS_BETTER);
153 ScoreInfo_SetLabel_PlayerScore(SP_KH_PICKUPS, "pickups", 0);
154 ScoreInfo_SetLabel_PlayerScore(SP_KH_KCKILLS, "kckills", 0);
155 ScoreInfo_SetLabel_PlayerScore(SP_KH_LOSSES, "losses", SFL_LOWER_IS_BETTER);
156 ScoreRules_basics_end();
159 float kh_KeyCarrier_waypointsprite_visible_for_player(entity e) // runs all the time
161 if(!IS_PLAYER(e) || DIFF_TEAM(self, e))
162 if(!kh_tracking_enabled)
168 float kh_Key_waypointsprite_visible_for_player(entity e) // ??
170 if(!kh_tracking_enabled)
174 if(!self.owner.owner)
176 return false; // draw only when key is not owned
179 void kh_update_state()
192 s |= pow(32, key.count) * f;
195 FOREACH_CLIENT(true, LAMBDA(it.kh_state = s));
200 key.owner.kh_state |= pow(32, key.count) * 31;
202 //print(ftos((nextent(world)).kh_state), "\n");
208 var kh_Think_t kh_Controller_Thinkfunc;
209 void kh_Controller_SetThink(float t, kh_Think_t func) // runs occasionaly
211 kh_Controller_Thinkfunc = func;
212 kh_controller.cnt = ceil(t);
214 kh_controller.nextthink = time; // force
216 void kh_WaitForPlayers();
217 void kh_Controller_Think() // called a lot
219 if(intermission_running)
222 { if(self.think != kh_WaitForPlayers) { self.cnt -= 1; } }
223 else if(self.cnt == 0)
226 kh_Controller_Thinkfunc();
228 self.nextthink = time + 1;
231 // frags f: take from cvar * f
233 void kh_Scores_Event(entity player, entity key, string what, float frags_player, float frags_owner) // update the score when a key is captured
236 if(intermission_running)
240 UpdateFrags(player, frags_player);
242 if(key && key.owner && frags_owner)
243 UpdateFrags(key.owner, frags_owner);
245 if(!autocvar_sv_eventlog) //output extra info to the console or text file
248 s = strcat(":keyhunt:", what, ":", ftos(player.playerid), ":", ftos(frags_player));
251 s = strcat(s, ":", ftos(key.owner.playerid));
255 s = strcat(s, ":", ftos(frags_owner), ":");
258 s = strcat(s, key.netname);
263 vector kh_AttachedOrigin(entity e) // runs when a team captures the flag, it can run 2 or 3 times.
267 makevectors(e.tag_entity.angles);
268 return e.tag_entity.origin + e.origin.x * v_forward - e.origin.y * v_right + e.origin.z * v_up;
274 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
276 #ifdef KH_PLAYER_USE_ATTACHMENT
278 first = key.owner.kh_next;
281 setattachment(key, key.owner, KH_PLAYER_ATTACHMENT_BONE);
284 setattachment(key.kh_next, key, "");
285 setorigin(key, key.kh_next.origin - 0.5 * KH_PLAYER_ATTACHMENT_DIST);
286 setorigin(key.kh_next, KH_PLAYER_ATTACHMENT_DIST_ROTATED);
287 key.kh_next.angles = '0 0 0';
290 setorigin(key, KH_PLAYER_ATTACHMENT);
291 key.angles = KH_PLAYER_ATTACHMENT_ANGLES;
295 setattachment(key, key.kh_prev, "");
297 setattachment(key.kh_next, key, "");
298 setorigin(key, KH_PLAYER_ATTACHMENT_DIST_ROTATED);
299 setorigin(first, first.origin - 0.5 * KH_PLAYER_ATTACHMENT_DIST);
300 key.angles = '0 0 0';
303 setattachment(key, key.owner, "");
304 setorigin(key, '0 0 1' * KH_KEY_ZSHIFT); // fixing x, y in think
305 key.angles_y -= key.owner.angles.y;
308 key.solid = SOLID_NOT;
309 key.movetype = MOVETYPE_NONE;
310 key.team = key.owner.team;
311 key.nextthink = time;
312 key.damageforcescale = 0;
313 key.takedamage = DAMAGE_NO;
314 key.modelindex = kh_key_carried;
317 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
319 #ifdef KH_PLAYER_USE_ATTACHMENT
321 first = key.owner.kh_next;
326 setattachment(key.kh_next, key.owner, KH_PLAYER_ATTACHMENT_BONE);
327 setorigin(key.kh_next, key.origin + 0.5 * KH_PLAYER_ATTACHMENT_DIST);
328 key.kh_next.angles = KH_PLAYER_ATTACHMENT_ANGLES;
334 setattachment(key.kh_next, key.kh_prev, "");
335 setorigin(first, first.origin + 0.5 * KH_PLAYER_ATTACHMENT_DIST);
338 setattachment(key, world, "");
339 setorigin(key, key.owner.origin + '0 0 1' * (STAT(PL_MIN, NULL).z - KH_KEY_MIN_z));
340 key.angles = key.owner.angles;
342 setorigin(key, key.owner.origin + key.origin.z * '0 0 1');
343 setattachment(key, world, "");
344 key.angles_y += key.owner.angles.y;
347 key.solid = SOLID_TRIGGER;
348 key.movetype = MOVETYPE_TOSS;
349 key.pain_finished = time + autocvar_g_balance_keyhunt_delay_return;
350 key.damageforcescale = autocvar_g_balance_keyhunt_damageforcescale;
351 key.takedamage = DAMAGE_YES;
353 key.modelindex = kh_key_dropped;
354 key.kh_previous_owner = key.owner;
355 key.kh_previous_owner_playerid = key.owner.playerid;
358 void kh_Key_AssignTo(entity key, entity player) // runs every time a key is picked up or assigned. Runs prior to kh_key_attach
361 float ownerteam0, ownerteam;
362 if(key.owner == player)
365 ownerteam0 = kh_Key_AllOwnedByWhichTeam();
371 // remove from linked list
373 key.kh_next.kh_prev = key.kh_prev;
374 key.kh_prev.kh_next = key.kh_next;
378 if(key.owner.kh_next == world)
380 // No longer a key carrier
381 if(!kh_no_radar_circles)
382 WaypointSprite_Ping(key.owner.waypointsprite_attachedforcarrier);
383 WaypointSprite_DetachCarrier(key.owner);
391 // insert into linked list
392 key.kh_next = player.kh_next;
393 key.kh_prev = player;
394 player.kh_next = key;
396 key.kh_next.kh_prev = key;
399 i = kh_keystatus[key.owner.playerid];
400 if(key.netname == "^1red key")
402 if(key.netname == "^4blue key")
404 if(key.netname == "^3yellow key")
406 if(key.netname == "^6pink key")
408 kh_keystatus[key.owner.playerid] = i;
412 if(key.kh_next == world)
414 // player is now a key carrier
415 entity wp = WaypointSprite_AttachCarrier(WP_Null, player, RADARICON_FLAGCARRIER);
416 wp.colormod = colormapPaletteColor(player.team - 1, 0);
417 player.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = kh_KeyCarrier_waypointsprite_visible_for_player;
418 WaypointSprite_UpdateRule(player.waypointsprite_attachedforcarrier, player.team, SPRITERULE_TEAMPLAY);
419 if(player.team == NUM_TEAM_1)
420 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierRed, WP_KeyCarrierFriend, WP_KeyCarrierRed);
421 else if(player.team == NUM_TEAM_2)
422 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierBlue, WP_KeyCarrierFriend, WP_KeyCarrierBlue);
423 else if(player.team == NUM_TEAM_3)
424 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierYellow, WP_KeyCarrierFriend, WP_KeyCarrierYellow);
425 else if(player.team == NUM_TEAM_4)
426 WaypointSprite_UpdateSprites(player.waypointsprite_attachedforcarrier, WP_KeyCarrierPink, WP_KeyCarrierFriend, WP_KeyCarrierPink);
427 if(!kh_no_radar_circles)
428 WaypointSprite_Ping(player.waypointsprite_attachedforcarrier);
432 // moved that here, also update if there's no player
437 ownerteam = kh_Key_AllOwnedByWhichTeam();
438 if(ownerteam != ownerteam0)
442 kh_interferemsg_time = time + 0.2;
443 kh_interferemsg_team = player.team;
445 // audit all key carrier sprites, update them to RUN HERE
448 if (!k.owner) continue;
449 entity first = WP_Null;
450 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model1, LAMBDA(first = it; break));
451 entity third = WP_Null;
452 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model3, LAMBDA(third = it; break));
453 WaypointSprite_UpdateSprites(k.owner.waypointsprite_attachedforcarrier, first, WP_KeyCarrierFinish, third);
458 kh_interferemsg_time = 0;
460 // audit all key carrier sprites, update them to RUN HERE
463 if (!k.owner) continue;
464 entity first = WP_Null;
465 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model1, LAMBDA(first = it; break));
466 entity third = WP_Null;
467 FOREACH(Waypoints, it.netname == k.owner.waypointsprite_attachedforcarrier.model3, LAMBDA(third = it; break));
468 WaypointSprite_UpdateSprites(k.owner.waypointsprite_attachedforcarrier, first, WP_KeyCarrierFriend, third);
474 void kh_Key_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
478 if(ITEM_DAMAGE_NEEDKILL(deathtype))
480 // touching lava, or hurt trigger
482 // immediately return is bad
483 // maybe start a shorter countdown?
487 if(time > this.pushltime)
488 if(IS_PLAYER(attacker))
489 this.team = attacker.team;
492 void kh_Key_Collect(entity key, entity player) //a player picks up a dropped key
494 sound(player, CH_TRIGGER, SND_KH_COLLECT, VOL_BASE, ATTEN_NORM);
496 if(key.kh_dropperteam != player.team)
498 kh_Scores_Event(player, key, "collect", autocvar_g_balance_keyhunt_score_collect, 0);
499 PlayerScore_Add(player, SP_KH_PICKUPS, 1);
501 key.kh_dropperteam = 0;
502 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_ENT(key, INFO_KEYHUNT_PICKUP), player.netname);
504 kh_Key_AssignTo(key, player); // this also updates .kh_state
507 void kh_Key_Touch() // runs many, many times when a key has been dropped and can be picked up
509 if(intermission_running)
512 if(self.owner) // already carried
515 if(ITEM_TOUCH_NEEDKILL())
517 // touching sky, or nodrop
519 // immediately return is bad
520 // maybe start a shorter countdown?
523 if (!IS_PLAYER(other))
527 if(other == self.enemy)
528 if(time < self.kh_droptime + autocvar_g_balance_keyhunt_delay_collect)
529 return; // you just dropped it!
530 kh_Key_Collect(self, other);
533 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
537 kh_Key_AssignTo(key, world);
538 if(o) // it was attached
539 WaypointSprite_Kill(key.waypointsprite_attachedforcarrier);
540 else // it was dropped
541 WaypointSprite_DetachCarrier(key);
543 // remove key from key list
544 if (kh_worldkeylist == key)
545 kh_worldkeylist = kh_worldkeylist.kh_worldkeynext;
551 if (o.kh_worldkeynext == key)
553 o.kh_worldkeynext = o.kh_worldkeynext.kh_worldkeynext;
556 o = o.kh_worldkeynext;
565 void kh_FinishRound() // runs when a team captures the keys
567 // prepare next round
568 kh_interferemsg_time = 0;
571 kh_no_radar_circles = true;
574 kh_no_radar_circles = false;
576 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_KEYHUNT_ROUNDSTART, autocvar_g_balance_keyhunt_delay_round);
577 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round, kh_StartRound);
580 void kh_WinnerTeam(float teem) // runs when a team wins // Samual: Teem?.... TEEM?!?! what the fuck is wrong with you people
582 // all key carriers get some points
583 vector firstorigin, lastorigin, midpoint;
587 score = (kh_teams - 1) * autocvar_g_balance_keyhunt_score_capture;
588 DistributeEvenly_Init(score, kh_teams);
589 // twice the score for 3 team games, three times the score for 4 team games!
590 // note: for a win by destroying the key, this should NOT be applied
594 f = DistributeEvenly_Get(1);
595 kh_Scores_Event(key.owner, key, "capture", f, 0);
596 PlayerTeamScore_Add(key.owner, SP_KH_CAPS, ST_KH_CAPS, 1);
597 nades_GiveBonus(key.owner, autocvar_g_nades_bonus_score_high);
601 string keyowner = "";
603 if(key.owner.kh_next == key)
606 keyowner = strcat(keyowner, ", ");
607 keyowner = key.owner.netname;
611 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM(teem, INFO_KEYHUNT_CAPTURE), keyowner);
615 firstorigin = '0 0 0';
616 lastorigin = '0 0 0';
621 thisorigin = kh_AttachedOrigin(key);
622 //dprint("Key origin: ", vtos(thisorigin), "\n");
623 midpoint += thisorigin;
626 te_lightning2(world, lastorigin, thisorigin);
627 lastorigin = thisorigin;
629 firstorigin = thisorigin;
634 te_lightning2(world, lastorigin, firstorigin);
636 midpoint = midpoint * (1 / kh_teams);
637 te_customflash(midpoint, 1000, 1, Team_ColorRGB(teem) * 0.5 + '0.5 0.5 0.5'); // make the color >=0.5 in each component
639 play2all(SND(KH_CAPTURE));
643 void kh_LoserTeam(float teem, entity lostkey) // runs when a player pushes a flag carrier off the map
645 entity key, attacker;
652 if(lostkey.pusher.team != teem)
653 if(IS_PLAYER(lostkey.pusher))
654 attacker = lostkey.pusher;
660 if(lostkey.kh_previous_owner)
661 kh_Scores_Event(lostkey.kh_previous_owner, world, "pushed", 0, -autocvar_g_balance_keyhunt_score_push);
662 // don't actually GIVE him the -nn points, just log
663 kh_Scores_Event(attacker, world, "push", autocvar_g_balance_keyhunt_score_push, 0);
664 PlayerScore_Add(attacker, SP_KH_PUSHES, 1);
665 //centerprint(attacker, "Your push is the best!"); // does this really need to exist?
669 float of, fragsleft, i, j, thisteam;
670 of = autocvar_g_balance_keyhunt_score_destroyed_ownfactor;
672 FOREACH_CLIENT(IS_PLAYER(it) && it.team != teem, LAMBDA(++players));
675 if(key.owner && key.team != teem)
678 if(lostkey.kh_previous_owner)
679 kh_Scores_Event(lostkey.kh_previous_owner, world, "destroyed", 0, -autocvar_g_balance_keyhunt_score_destroyed);
680 // don't actually GIVE him the -nn points, just log
682 if(lostkey.kh_previous_owner.playerid == lostkey.kh_previous_owner_playerid)
683 PlayerScore_Add(lostkey.kh_previous_owner, SP_KH_DESTROYS, 1);
685 DistributeEvenly_Init(autocvar_g_balance_keyhunt_score_destroyed, keys * of + players);
688 if(key.owner && key.team != teem)
690 f = DistributeEvenly_Get(of);
691 kh_Scores_Event(key.owner, world, "destroyed_holdingkey", f, 0);
694 fragsleft = DistributeEvenly_Get(players);
696 // Now distribute these among all other teams...
698 for(i = 0; i < kh_teams; ++i)
700 thisteam = kh_Team_ByID(i);
701 if(thisteam == teem) // bad boy, no cookie - this WILL happen
705 FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, LAMBDA(++players));
707 DistributeEvenly_Init(fragsleft, j);
708 fragsleft = DistributeEvenly_Get(j - 1);
709 DistributeEvenly_Init(DistributeEvenly_Get(1), players);
711 FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, LAMBDA(
712 f = DistributeEvenly_Get(1);
713 kh_Scores_Event(it, world, "destroyed", f, 0);
720 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_ENT(lostkey, INFO_KEYHUNT_LOST), lostkey.kh_previous_owner.netname);
722 play2all(SND(KH_DESTROY));
723 te_tarexplosion(lostkey.origin);
728 void kh_Key_Think() // runs all the time
730 if(intermission_running)
735 #ifndef KH_PLAYER_USE_ATTACHMENT
736 makevectors('0 1 0' * (self.cnt + (time % 360) * KH_KEY_XYSPEED));
737 setorigin(self, v_forward * KH_KEY_XYDIST + '0 0 1' * self.origin.z);
741 // if in nodrop or time over, end the round
743 if(time > self.pain_finished)
744 kh_LoserTeam(self.team, self);
747 if(kh_Key_AllOwnedByWhichTeam() != -1)
749 if(self.siren_time < time)
751 sound(self.owner, CH_TRIGGER, SND_KH_ALARM, VOL_BASE, ATTEN_NORM); // play a simple alarm
752 self.siren_time = time + 2.5; // repeat every 2.5 seconds
757 p = self.owner.origin;
759 if(vlen(key.owner.origin - p) > autocvar_g_balance_keyhunt_maxdist)
761 kh_WinnerTeam(self.team);
765 if(kh_interferemsg_time && time > kh_interferemsg_time)
767 kh_interferemsg_time = 0;
768 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
769 if(it.team == kh_interferemsg_team)
771 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_MEET);
773 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_HELP);
775 Send_Notification(NOTIF_ONE, it, MSG_CENTER, APP_TEAM_NUM(kh_interferemsg_team, CENTER_KEYHUNT_INTERFERE));
779 self.nextthink = time + 0.05;
782 void key_reset(entity this)
784 kh_Key_AssignTo(this, world);
788 const string STR_ITEM_KH_KEY = "item_kh_key";
789 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
791 entity key = spawn();
793 key.classname = STR_ITEM_KH_KEY;
794 key.touch = kh_Key_Touch;
795 key.think = kh_Key_Think;
796 key.nextthink = time;
797 key.items = IT_KEY1 | IT_KEY2;
799 key.angles = '0 360 0' * random();
800 key.event_damage = kh_Key_Damage;
801 key.takedamage = DAMAGE_YES;
802 key.modelindex = kh_key_dropped;
804 key.kh_dropperteam = 0;
805 key.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
806 setsize(key, KH_KEY_MIN, KH_KEY_MAX);
807 key.colormod = Team_ColorRGB(initial_owner.team) * KH_KEY_BRIGHTNESS;
808 key.reset = key_reset;
810 switch(initial_owner.team)
813 key.netname = "^1red key";
816 key.netname = "^4blue key";
819 key.netname = "^3yellow key";
822 key.netname = "^6pink key";
825 key.netname = "NETGIER key";
829 // link into key list
830 key.kh_worldkeynext = kh_worldkeylist;
831 kh_worldkeylist = key;
833 Send_Notification(NOTIF_ONE, initial_owner, MSG_CENTER, APP_TEAM_NUM(initial_owner.team, CENTER_KEYHUNT_START));
835 WaypointSprite_Spawn(WP_KeyDropped, 0, 0, key, '0 0 1' * KH_KEY_WP_ZSHIFT, world, key.team, key, waypointsprite_attachedforcarrier, false, RADARICON_FLAG);
836 key.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = kh_Key_waypointsprite_visible_for_player;
838 kh_Key_AssignTo(key, initial_owner);
841 // -1 when no team completely owns all keys yet
842 float kh_Key_AllOwnedByWhichTeam() // constantly called. check to see if all the keys are owned by the same team
856 else if(teem != key.team)
865 void kh_Key_DropOne(entity key)
867 // prevent collecting this one for some time
871 key.kh_droptime = time;
874 kh_Scores_Event(player, key, "dropkey", 0, 0);
875 PlayerScore_Add(player, SP_KH_LOSSES, 1);
876 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_ENT(key, INFO_KEYHUNT_DROP), player.netname);
878 kh_Key_AssignTo(key, world);
879 makevectors(player.v_angle);
880 key.velocity = W_CalculateProjectileVelocity(player.velocity, autocvar_g_balance_keyhunt_throwvelocity * v_forward, false);
882 key.pushltime = time + autocvar_g_balance_keyhunt_protecttime;
883 key.kh_dropperteam = key.team;
885 sound(player, CH_TRIGGER, SND_KH_DROP, VOL_BASE, ATTEN_NORM);
888 void kh_Key_DropAll(entity player, float suicide) // runs whenever a player dies
896 if(time < player.pushltime)
897 mypusher = player.pusher;
898 while((key = player.kh_next))
900 kh_Scores_Event(player, key, "losekey", 0, 0);
901 PlayerScore_Add(player, SP_KH_LOSSES, 1);
902 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_ENT(key, INFO_KEYHUNT_LOST), player.netname);
903 kh_Key_AssignTo(key, world);
904 makevectors('-1 0 0' * (45 + 45 * random()) + '0 360 0' * random());
905 key.velocity = W_CalculateProjectileVelocity(player.velocity, autocvar_g_balance_keyhunt_dropvelocity * v_forward, false);
906 key.pusher = mypusher;
907 key.pushltime = time + autocvar_g_balance_keyhunt_protecttime;
909 key.kh_dropperteam = player.team;
911 sound(player, CH_TRIGGER, SND_KH_DROP, VOL_BASE, ATTEN_NORM);
915 float kh_CheckPlayers(float num)
919 float t_team = kh_Team_ByID(num);
921 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
922 if(!IS_DEAD(it) && !PHYS_INPUT_BUTTON_CHAT(it) && it.team == t_team)
926 if (!players) { return t_team; }
931 #define KH_READY_TEAMS() (!p1 + !p2 + ((kh_teams >= 3) ? !p3 : p3) + ((kh_teams >= 4) ? !p4 : p4))
932 #define KH_READY_TEAMS_OK() (KH_READY_TEAMS() == kh_teams)
933 void kh_WaitForPlayers() // delay start of the round until enough players are present
935 if(time < game_starttime)
937 kh_Controller_SetThink(game_starttime - time + 0.1, kh_WaitForPlayers);
941 static float prev_missing_teams_mask;
942 float p1 = kh_CheckPlayers(0), p2 = kh_CheckPlayers(1), p3 = kh_CheckPlayers(2), p4 = kh_CheckPlayers(3);
943 if(KH_READY_TEAMS_OK())
945 if(prev_missing_teams_mask > 0)
946 Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CPID_MISSING_TEAMS);
947 prev_missing_teams_mask = -1;
948 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_KEYHUNT_ROUNDSTART, autocvar_g_balance_keyhunt_delay_round);
949 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round, kh_StartRound);
953 if(player_count == 0)
955 if(prev_missing_teams_mask > 0)
956 Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CPID_MISSING_TEAMS);
957 prev_missing_teams_mask = -1;
961 float missing_teams_mask = boolean(p1) + boolean(p2) * 2;
962 if(kh_teams >= 3) missing_teams_mask += boolean(p3) * 4;
963 if(kh_teams >= 4) missing_teams_mask += boolean(p4) * 8;
964 if(prev_missing_teams_mask != missing_teams_mask)
966 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
967 prev_missing_teams_mask = missing_teams_mask;
970 kh_Controller_SetThink(1, kh_WaitForPlayers);
974 void kh_EnableTrackingDevice() // runs after each round
976 Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CPID_KEYHUNT);
977 Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CPID_KEYHUNT_OTHER);
979 kh_tracking_enabled = true;
982 void kh_StartRound() // runs at the start of each round
984 float i, players, teem;
986 if(time < game_starttime)
988 kh_Controller_SetThink(game_starttime - time + 0.1, kh_WaitForPlayers);
992 float p1 = kh_CheckPlayers(0), p2 = kh_CheckPlayers(1), p3 = kh_CheckPlayers(2), p4 = kh_CheckPlayers(3);
993 if(!KH_READY_TEAMS_OK())
995 kh_Controller_SetThink(1, kh_WaitForPlayers);
999 Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CPID_KEYHUNT);
1000 Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CPID_KEYHUNT_OTHER);
1002 for(i = 0; i < kh_teams; ++i)
1004 teem = kh_Team_ByID(i);
1006 entity my_player = world;
1007 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
1008 if(!IS_DEAD(it) && !PHYS_INPUT_BUTTON_CHAT(it) && it.team == teem)
1011 if(random() * players <= 1)
1015 kh_Key_Spawn(my_player, 360 * i / kh_teams, i);
1018 kh_tracking_enabled = false;
1019 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_KEYHUNT_SCAN, autocvar_g_balance_keyhunt_delay_tracking);
1020 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_tracking, kh_EnableTrackingDevice);
1023 float kh_HandleFrags(entity attacker, entity targ, float f) // adds to the player score
1025 if(attacker == targ)
1030 if(attacker.team == targ.team)
1035 for(k = targ.kh_next; k != world; k = k.kh_next)
1037 kh_Scores_Event(attacker, targ.kh_next, "carrierfrag", -nk * autocvar_g_balance_keyhunt_score_collect, 0);
1041 kh_Scores_Event(attacker, targ.kh_next, "carrierfrag", autocvar_g_balance_keyhunt_score_carrierfrag-1, 0);
1042 PlayerScore_Add(attacker, SP_KH_KCKILLS, 1);
1043 // the frag gets added later
1050 void kh_Initialize() // sets up th KH environment
1053 kh_teams = autocvar_g_keyhunt_teams_override;
1055 kh_teams = autocvar_g_keyhunt_teams;
1056 kh_teams = bound(2, kh_teams, 4);
1058 // make a KH entity for controlling the game
1059 kh_controller = spawn();
1060 kh_controller.think = kh_Controller_Think;
1061 kh_Controller_SetThink(0, kh_WaitForPlayers);
1063 setmodel(kh_controller, MDL_KH_KEY);
1064 kh_key_dropped = kh_controller.modelindex;
1066 dprint(vtos(kh_controller.mins));
1067 dprint(vtos(kh_controller.maxs));
1070 #ifdef KH_PLAYER_USE_CARRIEDMODEL
1071 setmodel(kh_controller, MDL_KH_KEY_CARRIED);
1072 kh_key_carried = kh_controller.modelindex;
1074 kh_key_carried = kh_key_dropped;
1077 kh_controller.model = "";
1078 kh_controller.modelindex = 0;
1080 kh_ScoreRules(kh_teams);
1085 // to be called before intermission
1087 remove(kh_controller);
1088 kh_controller = world;
1093 void() havocbot_role_kh_carrier;
1094 void() havocbot_role_kh_defense;
1095 void() havocbot_role_kh_offense;
1096 void() havocbot_role_kh_freelancer;
1099 void havocbot_goalrating_kh(float ratingscale_team, float ratingscale_dropped, float ratingscale_enemy)
1102 for (head = kh_worldkeylist; head; head = head.kh_worldkeynext)
1104 if(head.owner == self)
1106 if(!kh_tracking_enabled)
1108 // if it's carried by our team we know about it
1109 // otherwise we have to see it to know about it
1110 if(!head.owner || head.team != self.team)
1112 traceline(self.origin + self.view_ofs, head.origin, MOVE_NOMONSTERS, self);
1113 if (trace_fraction < 1 && trace_ent != head)
1114 continue; // skip what I can't see
1118 navigation_routerating(head, ratingscale_dropped * BOT_PICKUP_RATING_HIGH, 100000);
1119 else if(head.team == self.team)
1120 navigation_routerating(head.owner, ratingscale_team * BOT_PICKUP_RATING_HIGH, 100000);
1122 navigation_routerating(head.owner, ratingscale_enemy * BOT_PICKUP_RATING_HIGH, 100000);
1125 havocbot_goalrating_items(1, self.origin, 10000);
1128 void havocbot_role_kh_carrier()
1133 if (!(self.kh_next))
1135 LOG_TRACE("changing role to freelancer\n");
1136 self.havocbot_role = havocbot_role_kh_freelancer;
1137 self.havocbot_role_timeout = 0;
1141 if (self.bot_strategytime < time)
1143 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1144 navigation_goalrating_start();
1146 if(kh_Key_AllOwnedByWhichTeam() == self.team)
1147 havocbot_goalrating_kh(10, 0.1, 0.1); // bring home
1149 havocbot_goalrating_kh(4, 4, 1); // play defensively
1151 navigation_goalrating_end();
1155 void havocbot_role_kh_defense()
1162 LOG_TRACE("changing role to carrier\n");
1163 self.havocbot_role = havocbot_role_kh_carrier;
1164 self.havocbot_role_timeout = 0;
1168 if (!self.havocbot_role_timeout)
1169 self.havocbot_role_timeout = time + random() * 10 + 20;
1170 if (time > self.havocbot_role_timeout)
1172 LOG_TRACE("changing role to freelancer\n");
1173 self.havocbot_role = havocbot_role_kh_freelancer;
1174 self.havocbot_role_timeout = 0;
1178 if (self.bot_strategytime < time)
1180 float key_owner_team;
1181 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1182 navigation_goalrating_start();
1184 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1185 if(key_owner_team == self.team)
1186 havocbot_goalrating_kh(10, 0.1, 0.1); // defend key carriers
1187 else if(key_owner_team == -1)
1188 havocbot_goalrating_kh(4, 1, 0.1); // play defensively
1190 havocbot_goalrating_kh(0.1, 0.1, 10); // ATTACK ANYWAY
1192 navigation_goalrating_end();
1196 void havocbot_role_kh_offense()
1203 LOG_TRACE("changing role to carrier\n");
1204 self.havocbot_role = havocbot_role_kh_carrier;
1205 self.havocbot_role_timeout = 0;
1209 if (!self.havocbot_role_timeout)
1210 self.havocbot_role_timeout = time + random() * 10 + 20;
1211 if (time > self.havocbot_role_timeout)
1213 LOG_TRACE("changing role to freelancer\n");
1214 self.havocbot_role = havocbot_role_kh_freelancer;
1215 self.havocbot_role_timeout = 0;
1219 if (self.bot_strategytime < time)
1221 float key_owner_team;
1223 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1224 navigation_goalrating_start();
1226 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1227 if(key_owner_team == self.team)
1228 havocbot_goalrating_kh(10, 0.1, 0.1); // defend anyway
1229 else if(key_owner_team == -1)
1230 havocbot_goalrating_kh(0.1, 1, 4); // play offensively
1232 havocbot_goalrating_kh(0.1, 0.1, 10); // ATTACK! EMERGENCY!
1234 navigation_goalrating_end();
1238 void havocbot_role_kh_freelancer()
1245 LOG_TRACE("changing role to carrier\n");
1246 self.havocbot_role = havocbot_role_kh_carrier;
1247 self.havocbot_role_timeout = 0;
1251 if (!self.havocbot_role_timeout)
1252 self.havocbot_role_timeout = time + random() * 10 + 10;
1253 if (time > self.havocbot_role_timeout)
1257 LOG_TRACE("changing role to offense\n");
1258 self.havocbot_role = havocbot_role_kh_offense;
1262 LOG_TRACE("changing role to defense\n");
1263 self.havocbot_role = havocbot_role_kh_defense;
1265 self.havocbot_role_timeout = 0;
1269 if (self.bot_strategytime < time)
1271 float key_owner_team;
1273 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1274 navigation_goalrating_start();
1276 key_owner_team = kh_Key_AllOwnedByWhichTeam();
1277 if(key_owner_team == self.team)
1278 havocbot_goalrating_kh(10, 0.1, 0.1); // defend anyway
1279 else if(key_owner_team == -1)
1280 havocbot_goalrating_kh(1, 10, 4); // prefer dropped keys
1282 havocbot_goalrating_kh(0.1, 0.1, 10); // ATTACK ANYWAY
1284 navigation_goalrating_end();
1289 // register this as a mutator
1291 MUTATOR_HOOKFUNCTION(kh, ClientDisconnect)
1293 kh_Key_DropAll(self, true);
1297 MUTATOR_HOOKFUNCTION(kh, MakePlayerObserver)
1299 kh_Key_DropAll(self, true);
1303 MUTATOR_HOOKFUNCTION(kh, PlayerDies)
1305 if(frag_target == other)
1306 kh_Key_DropAll(frag_target, true);
1307 else if(IS_PLAYER(other))
1308 kh_Key_DropAll(frag_target, false);
1310 kh_Key_DropAll(frag_target, true);
1314 MUTATOR_HOOKFUNCTION(kh, GiveFragsForKill, CBC_ORDER_FIRST)
1316 frag_score = kh_HandleFrags(frag_attacker, frag_target, frag_score);
1320 MUTATOR_HOOKFUNCTION(kh, MatchEnd)
1326 MUTATOR_HOOKFUNCTION(kh, GetTeamCount, CBC_ORDER_EXCLUSIVE)
1328 ret_float = kh_teams;
1332 MUTATOR_HOOKFUNCTION(kh, SpectateCopy)
1334 self.kh_state = other.kh_state;
1338 MUTATOR_HOOKFUNCTION(kh, PlayerUseKey)
1340 if(MUTATOR_RETURNVALUE == 0)
1353 MUTATOR_HOOKFUNCTION(kh, HavocBot_ChooseRole)
1358 float r = random() * 3;
1360 self.havocbot_role = havocbot_role_kh_offense;
1362 self.havocbot_role = havocbot_role_kh_defense;
1364 self.havocbot_role = havocbot_role_kh_freelancer;
1369 MUTATOR_HOOKFUNCTION(kh, DropSpecialItems)
1371 kh_Key_DropAll(frag_target, false);
1375 MUTATOR_HOOKFUNCTION(kh, reset_map_global)
1377 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round + (game_starttime - time), kh_StartRound);