3 #include <common/mutators/mutator/instagib/items.qh>
4 #include <server/campaign.qh>
5 #include <server/command/_mod.qh>
6 #include <server/world.qh>
7 #include <server/items/items.qh>
9 int autocvar_g_lms_extra_lives;
10 bool autocvar_g_lms_join_anytime;
11 int autocvar_g_lms_last_join;
12 bool autocvar_g_lms_regenerate;
13 int autocvar_g_lms_leader_wp_lives;
14 float autocvar_g_lms_leader_wp_max_relative;
15 float autocvar_g_lms_leader_wp_time;
16 float autocvar_g_lms_leader_wp_time_repeat;
17 float autocvar_g_lms_dynamic_respawn_delay;
18 float autocvar_g_lms_dynamic_respawn_delay_base;
19 float autocvar_g_lms_dynamic_respawn_delay_increase;
20 float autocvar_g_lms_dynamic_respawn_delay_max;
21 bool autocvar_g_lms_dynamic_vampire;
22 float autocvar_g_lms_dynamic_vampire_factor_base;
23 float autocvar_g_lms_dynamic_vampire_factor_increase;
24 float autocvar_g_lms_dynamic_vampire_factor_max;
25 int autocvar_g_lms_dynamic_vampire_min_lives_diff;
29 bool lms_visible_leaders;
30 bool lms_visible_leaders_prev;
33 int LMS_NewPlayerLives()
35 int fl = floor(autocvar_fraglimit);
36 if(fl == 0 || fl > 999)
39 // first player has left the game for dying too much? Nobody else can get in.
40 if(lms_lowest_lives < 1)
43 if(!autocvar_g_lms_join_anytime)
44 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
47 return bound(1, lms_lowest_lives, fl);
52 // LMS winning condition: game terminates if and only if there's at most one
53 // one player who's living lives. Top two scores being equal cancels the time
55 int WinningCondition_LMS()
57 if (warmup_stage || time <= game_starttime)
60 entity first_player = NULL;
62 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
72 // two or more active players - continue with the game
74 if (autocvar_g_campaign)
76 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
77 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
79 return WINNING_YES; // human player lost, game over
86 // exactly one player?
89 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
91 if (LMS_NewPlayerLives())
93 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
99 // and assign him his first place
100 GameRules_scoring_add(first_player, LMS_RANK, 1);
107 // nobody is playing at all...
108 if (LMS_NewPlayerLives())
110 // wait for players...
114 // SNAFU (maybe a draw game?)
116 LOG_TRACE("No players, ending game.");
121 // When we get here, we have at least two players who are actually LIVING,
122 // now check if the top two players have equal score.
123 WinningConditionHelper(NULL);
126 if(WinningConditionHelper_winner)
127 WinningConditionHelper_winner.winning = true;
128 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
129 return WINNING_NEVER;
131 // Top two have different scores? Way to go for our beloved TIMELIMIT!
135 // runs on waypoints which are attached to leaders, updates once per frame
136 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view)
140 return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
142 if (!lms_visible_leaders)
148 void lms_UpdateLeaders()
152 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
153 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
154 if (lives > max_lives)
159 int second_max_lives = 0;
160 int pl_cnt_with_max_lives = 0;
161 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
162 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
163 if (lives == max_lives)
164 pl_cnt_with_max_lives++;
165 else if (lives > second_max_lives)
166 second_max_lives = lives;
169 int lives_diff = autocvar_g_lms_leader_wp_lives;
170 if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
171 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
172 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
173 if (lives == max_lives)
176 it.lms_leader = true;
180 it.lms_leader = false;
184 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
185 if (it.waypointsprite_attachedforcarrier)
186 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
187 it.lms_leader = false;
192 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
194 lms_lowest_lives = 999;
197 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
199 FOREACH_CLIENT(true, {
200 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
202 // players who forfeited (rank >= 256) become spectators
203 if (it.lms_spectate_warning == 2)
204 it.frags = FRAGS_SPECTATOR;
206 it.frags = FRAGS_PLAYER;
209 CS(it).killcount = 0;
211 it.lms_spectate_warning = 0;
212 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
213 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
215 if (it.frags != FRAGS_PLAYER)
218 TRANSMUTE(Player, it);
219 PutClientInServer(it);
220 it.lms_leader = false;
221 if (it.waypointsprite_attachedforcarrier)
222 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
226 // FIXME add support for sv_ready_restart_after_countdown
227 // that is find a way to respawn/reset players IN GAME without setting lives to 0
228 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
231 sv_ready_restart_after_countdown = 0;
234 // returns true if player is added to the game
235 bool lms_AddPlayer(entity player)
237 if (!player.lmsplayer)
239 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
242 player.lmsplayer = 2; // temp value indicating player has just joined the game (but not spawned yet)
244 if (warmup_stage || time <= game_starttime)
246 if(player.lms_spectate_warning)
248 player.lms_spectate_warning = 0;
249 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
250 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
252 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
257 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
259 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
266 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
268 entity player = M_ARGV(0, entity);
269 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
271 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
272 TRANSMUTE(Observer, player);
276 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
278 entity player = M_ARGV(0, entity);
280 if (warmup_stage || time < game_starttime)
283 if (player.lmsplayer == 2) // just joined the game
285 // spawn player with the same amount of health / armor
286 // as the least healthy player with the least number of lives
287 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
288 float min_health = start_health;
289 float min_armorvalue = start_armorvalue;
290 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
291 if (GetResource(it, RES_HEALTH) < min_health)
292 min_health = GetResource(it, RES_HEALTH);
293 if (GetResource(it, RES_ARMOR) < min_armorvalue)
294 min_armorvalue = GetResource(it, RES_ARMOR);
296 if (min_health != start_health)
297 SetResource(player, RES_HEALTH, max(1, min_health));
298 if (min_armorvalue != start_armorvalue)
299 SetResource(player, RES_ARMOR, min_armorvalue);
300 player.lmsplayer = 1;
304 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
306 entity player = M_ARGV(0, entity);
308 if (warmup_stage || lms_AddPlayer(player))
314 void lms_RemovePlayer(entity player)
316 if (warmup_stage || time < game_starttime)
319 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
322 if (player.lms_spectate_warning < 2)
324 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
326 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
329 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
333 int min_forfeiter_rank = 665; // different from 666
334 FOREACH_CLIENT(true, {
335 // update rank of other players that were eliminated
336 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
338 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
339 if (it_rank > player_rank && it_rank <= 256)
340 GameRules_scoring_add(it, LMS_RANK, -1);
341 if (it_rank > 256 && it_rank <= min_forfeiter_rank)
342 min_forfeiter_rank = it_rank - 1;
344 else if (it.frags != FRAGS_SPECTATOR)
346 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
347 if(tl < lms_lowest_lives)
348 lms_lowest_lives = tl;
351 GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
353 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
354 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
355 TRANSMUTE(Observer, player);
357 if (autocvar_g_lms_leader_wp_lives > 0)
361 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
363 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
364 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
366 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
370 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
372 entity player = M_ARGV(0, entity);
374 // no further message other than the disconnect message
375 player.lms_spectate_warning = 3;
377 lms_RemovePlayer(player);
378 player.lmsplayer = 0;
381 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
383 entity player = M_ARGV(0, entity);
384 bool is_forced = M_ARGV(1, bool);
386 if (!IS_PLAYER(player))
389 if (warmup_stage || time <= game_starttime)
391 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
392 player.frags = FRAGS_SPECTATOR;
393 TRANSMUTE(Observer, player);
394 player.lmsplayer = 0;
399 player.lms_spectate_warning = 2;
400 if (!GameRules_scoring_add(player, LMS_RANK, 0))
401 lms_RemovePlayer(player);
403 return true; // prevent team reset
406 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
408 entity player = M_ARGV(0, entity);
409 player.frags = FRAGS_SPECTATOR;
412 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
414 entity player = M_ARGV(0, entity);
416 // recycled REDALIVE to avoid adding a dedicated stat
417 STAT(REDALIVE, player) = lms_leaders;
419 if(player.deadflag == DEAD_DYING)
420 player.deadflag = DEAD_RESPAWNING;
423 MUTATOR_HOOKFUNCTION(lms, SV_StartFrame)
425 float leader_time = autocvar_g_lms_leader_wp_time;
426 float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
427 lms_visible_leaders_prev = lms_visible_leaders;
428 lms_visible_leaders = (time % leader_repeat_time < leader_time);
431 FOREACH_CLIENT(true, {
432 STAT(OBJECTIVE_STATUS, it) = lms_visible_leaders;
433 if (IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME)
437 if (!it.waypointsprite_attachedforcarrier)
439 WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
440 it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
441 WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
442 vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
443 WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
444 WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
446 if (!lms_visible_leaders_prev && lms_visible_leaders && IS_REAL_CLIENT(it))
447 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_LMS_VISIBLE_LEADER);
453 if (IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME)
455 if (!lms_visible_leaders_prev && lms_visible_leaders && IS_REAL_CLIENT(it))
456 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_LMS_VISIBLE_OTHER);
458 if (it.waypointsprite_attachedforcarrier)
459 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
464 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
466 if(autocvar_g_lms_regenerate)
471 MUTATOR_HOOKFUNCTION(lms, PlayerPowerups)
473 entity player = M_ARGV(0, entity);
474 if (player.waypointsprite_attachedforcarrier)
475 player.effects |= (EF_ADDITIVE | EF_FULLBRIGHT);
477 player.effects &= ~(EF_ADDITIVE | EF_FULLBRIGHT);
480 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
486 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
488 if (!autocvar_g_lms_dynamic_vampire)
491 entity frag_attacker = M_ARGV(1, entity);
492 entity frag_target = M_ARGV(2, entity);
493 float frag_damage = M_ARGV(4, float);
495 if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_attacker)
496 && IS_PLAYER(frag_target) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
498 float vampire_factor = 0;
500 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
501 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
502 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
505 vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
506 if (vampire_factor > 0)
508 vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
509 SetResourceExplicit(frag_attacker, RES_HEALTH,
510 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
515 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
517 if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
521 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
523 entity player = M_ARGV(0, entity);
524 player.respawn_flags |= RESPAWN_FORCE;
526 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
529 player.respawn_flags = RESPAWN_SILENT;
530 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
531 player.respawn_time = time + 2;
535 if (autocvar_g_lms_dynamic_respawn_delay <= 0)
540 FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
541 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
542 if (lives > max_lives)
547 // min delay with only 2 players
548 if (pl_cnt == 1) // player wasn't counted
551 float dlay = autocvar_g_lms_dynamic_respawn_delay_base +
552 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
553 player.respawn_time = time + min(autocvar_g_lms_dynamic_respawn_delay_max, dlay);
557 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
559 entity frag_target = M_ARGV(1, entity);
561 if (!warmup_stage && time > game_starttime)
564 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
565 if(tl < lms_lowest_lives)
566 lms_lowest_lives = tl;
570 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
573 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
574 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
577 M_ARGV(2, float) = 0; // frag score
582 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
584 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
585 start_health = warmup_start_health = cvar("g_lms_start_health");
586 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
587 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
588 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
589 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
590 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
591 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
592 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
595 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
597 // don't clear player score
601 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
603 entity definition = M_ARGV(0, entity);
605 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
612 void lms_extralife(entity this)
614 StartItem(this, ITEM_ExtraLife);
617 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
619 if (MUTATOR_RETURNVALUE) return false;
620 if (!autocvar_g_powerups) return false;
621 if (!autocvar_g_lms_extra_lives) return false;
623 entity ent = M_ARGV(0, entity);
625 // Can't use .itemdef here
626 if (ent.classname != "item_health_mega") return false;
629 setthink(e, lms_extralife);
631 e.nextthink = time + 0.1;
632 e.spawnflags = ent.spawnflags;
633 e.noalign = ent.noalign;
634 setorigin(e, ent.origin);
639 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
641 if(MUTATOR_RETURNVALUE) return false;
643 entity item = M_ARGV(0, entity);
644 entity toucher = M_ARGV(1, entity);
646 if(item.itemdef == ITEM_ExtraLife)
648 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
649 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
650 return MUT_ITEMTOUCH_PICKUP;
653 return MUT_ITEMTOUCH_CONTINUE;
656 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
658 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
659 if (it.lmsplayer && it.lms_spectate_warning < 2)
660 ++M_ARGV(0, int); // activerealplayers
661 ++M_ARGV(1, int); // realplayers
667 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
669 entity player = M_ARGV(0, entity);
671 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
673 // for the forfeit message...
674 player.lms_spectate_warning = 2;
678 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
680 player.lms_spectate_warning = 1;
681 sprint(player, "WARNING: you won't be able to enter the game again after spectating in LMS. Use the same command again to spectate anyway.\n");
683 return MUT_SPECCMD_RETURN;
685 return MUT_SPECCMD_CONTINUE;
688 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
690 M_ARGV(0, float) = WinningCondition_LMS();
694 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
696 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
697 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
700 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
702 entity player = M_ARGV(0, entity);
704 return boolean(player.lmsplayer);
707 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
710 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
711 return true; // allow writing to this field in intermission as it is needed for newly joining players
714 void lms_Initialize()
716 lms_lowest_lives = 999;