]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
LMS: fix all players declared winners in console
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / lms / sv_lms.qc
index e69e1b7d4490295e68cc371b3449a34272d56290..9a210262b68e80b8b1c8b323a81739f8f8111f94 100644 (file)
@@ -7,9 +7,12 @@
 #include <server/items/items.qh>
 
 int autocvar_g_lms_extra_lives;
+float autocvar_g_lms_forfeit_min_match_time;
 bool autocvar_g_lms_join_anytime;
 int autocvar_g_lms_last_join;
+bool autocvar_g_lms_items;
 bool autocvar_g_lms_regenerate;
+bool autocvar_g_lms_rot;
 
 // main functions
 int LMS_NewPlayerLives()
@@ -41,10 +44,16 @@ int WinningCondition_LMS()
 
        entity first_player = NULL;
        int totalplayers = 0;
-       FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
-               if (!totalplayers)
-                       first_player = it;
-               ++totalplayers;
+       int totalplayed = 0;
+       FOREACH_CLIENT(true, {
+               if (IS_PLAYER(it) && it.frags == FRAGS_PLAYER)
+               {
+                       if (!totalplayers)
+                               first_player = it;
+                       ++totalplayers;
+               }
+               else if (GameRules_scoring_add(it, LMS_RANK, 0))
+                       ++totalplayed;
        });
 
        if (totalplayers)
@@ -53,7 +62,7 @@ int WinningCondition_LMS()
                {
                        // two or more active players - continue with the game
 
-                       if (autocvar_g_campaign)
+                       if (autocvar_g_campaign && campaign_bots_may_start)
                        {
                                FOREACH_CLIENT(IS_REAL_CLIENT(it), {
                                        float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
@@ -68,10 +77,15 @@ int WinningCondition_LMS()
                        // exactly one player?
 
                        ClearWinners();
-                       SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
 
                        if (LMS_NewPlayerLives())
                        {
+                               if (totalplayed && game_starttime > 0 && time > game_starttime + autocvar_g_lms_forfeit_min_match_time) // give players time to join
+                               {
+                                       GameRules_scoring_add(first_player, LMS_RANK, 1);
+                                       first_player.winning = 1;
+                                       return WINNING_YES;
+                               }
                                // game still running (that is, nobody got removed from the game by a frag yet)? then continue
                                return WINNING_NO;
                        }
@@ -80,6 +94,7 @@ int WinningCondition_LMS()
                                // a winner!
                                // and assign him his first place
                                GameRules_scoring_add(first_player, LMS_RANK, 1);
+                               first_player.winning = 1;
                                return WINNING_YES;
                        }
                }
@@ -89,6 +104,11 @@ int WinningCondition_LMS()
                // nobody is playing at all...
                if (LMS_NewPlayerLives())
                {
+                       if (totalplayed && game_starttime > 0 && time > game_starttime + autocvar_g_lms_forfeit_min_match_time) // give players time to join
+                       {
+                               ClearWinners();
+                               return WINNING_YES;
+                       }
                        // wait for players...
                }
                else
@@ -118,7 +138,6 @@ int WinningCondition_LMS()
 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
 {
        lms_lowest_lives = 999;
-       lms_quitters = 0;
 }
 
 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
@@ -134,7 +153,7 @@ MUTATOR_HOOKFUNCTION(lms, reset_map_players)
                }
 
                CS(it).killcount = 0;
-               it.lmsplayer = 0;
+               INGAME_STATUS_CLEAR(it);
                it.lms_spectate_warning = 0;
                GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
                GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
@@ -158,12 +177,15 @@ MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
 // returns true if player is added to the game
 bool lms_AddPlayer(entity player)
 {
-       if (!player.lmsplayer)
+       if (!INGAME(player))
        {
                int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
                if(lives <= 0)
                        return false;
-               player.lmsplayer = 1;
+               if (time < game_starttime)
+                       INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
+               else
+                       INGAME_STATUS_SET(player, INGAME_STATUS_JOINING); // this is just to delay setting health and armor that can't be done here
        }
        if (warmup_stage || time <= game_starttime)
        {
@@ -197,6 +219,34 @@ MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
        }
 }
 
+MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
+{
+       entity player = M_ARGV(0, entity);
+
+       if (warmup_stage || time < game_starttime)
+               return true;
+
+       if (INGAME_JOINING(player))
+       {
+               // spawn player with the same amount of health / armor
+               // as the least healthy player with the least number of lives
+               int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
+               float min_health = start_health;
+               float min_armorvalue = start_armorvalue;
+               FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
+                       if (GetResource(it, RES_HEALTH) < min_health)
+                               min_health = GetResource(it, RES_HEALTH);
+                       if (GetResource(it, RES_ARMOR) < min_armorvalue)
+                               min_armorvalue = GetResource(it, RES_ARMOR);
+               });
+               if (min_health != start_health)
+                       SetResource(player, RES_HEALTH, max(1, min_health));
+               if (min_armorvalue != start_armorvalue)
+                       SetResource(player, RES_ARMOR, min_armorvalue);
+               INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
+       }
+}
+
 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
 {
        entity player = M_ARGV(0, entity);
@@ -238,15 +288,18 @@ void lms_RemovePlayer(entity player)
                        });
                        GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
                }
-               else
+               else if (INGAME(player))
                {
-                       FOREACH_CLIENT(true, {
+                       int min_forfeiter_rank = 665; // different from 666
+                       FOREACH_CLIENT(it != player, {
                                // update rank of other players that were eliminated
                                if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
                                {
                                        float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
                                        if (it_rank > player_rank && it_rank <= 256)
                                                GameRules_scoring_add(it, LMS_RANK, -1);
+                                       if (it_rank > 256 && it_rank <= min_forfeiter_rank)
+                                               min_forfeiter_rank = it_rank - 1;
                                }
                                else if (it.frags != FRAGS_SPECTATOR)
                                {
@@ -255,12 +308,9 @@ void lms_RemovePlayer(entity player)
                                                lms_lowest_lives = tl;
                                }
                        });
-                       GameRules_scoring_add(player, LMS_RANK, 665 - lms_quitters); // different from 666
+                       GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
                        if(!warmup_stage)
-                       {
                                GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
-                               ++lms_quitters;
-                       }
                        player.frags = FRAGS_PLAYER_OUT_OF_GAME;
                        TRANSMUTE(Observer, player);
                }
@@ -283,12 +333,13 @@ MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
        player.lms_spectate_warning = 3;
 
        lms_RemovePlayer(player);
-       player.lmsplayer = 0;
+       INGAME_STATUS_CLEAR(player);
 }
 
 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
 {
        entity player = M_ARGV(0, entity);
+       bool is_forced = M_ARGV(1, bool);
 
        if (!IS_PLAYER(player))
                return true;
@@ -298,19 +349,22 @@ MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
                GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
                player.frags = FRAGS_SPECTATOR;
                TRANSMUTE(Observer, player);
-               player.lmsplayer = 0;
+               INGAME_STATUS_CLEAR(player);
+       }
+       else
+       {
+               if (is_forced)
+                       player.lms_spectate_warning = 2;
+               if (!GameRules_scoring_add(player, LMS_RANK, 0))
+                       lms_RemovePlayer(player);
        }
-       else if (!GameRules_scoring_add(player, LMS_RANK, 0))
-               lms_RemovePlayer(player);
        return true;  // prevent team reset
 }
 
 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
 {
        entity player = M_ARGV(0, entity);
-       TRANSMUTE(Observer, player);
        player.frags = FRAGS_SPECTATOR;
-       player.lms_spectate_warning = 0;
 }
 
 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
@@ -323,9 +377,11 @@ MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
 
 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
 {
-       if(autocvar_g_lms_regenerate)
-               return false;
-       return true;
+       if(!autocvar_g_lms_regenerate)
+               M_ARGV(2, float) = 0;
+       if(!autocvar_g_lms_rot)
+               M_ARGV(3, float) = 0;
+       return (!autocvar_g_lms_regenerate && !autocvar_g_lms_rot);
 }
 
 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
@@ -362,6 +418,9 @@ MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
 {
        start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
+       if(!cvar("g_use_ammunition"))
+               start_items |= IT_UNLIMITED_AMMO;
+
        start_health       = warmup_start_health       = cvar("g_lms_start_health");
        start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
        start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
@@ -380,13 +439,16 @@ MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
 
 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
 {
+       if (autocvar_g_lms_items)
+               return false;
+
        entity definition = M_ARGV(0, entity);
 
        if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
        {
                return false;
        }
-       return true;
+       return (autocvar_g_pickup_items <= 0); // only allow items if explicitly enabled
 }
 
 void lms_extralife(entity this)
@@ -436,7 +498,7 @@ MUTATOR_HOOKFUNCTION(lms, ItemTouch)
 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
 {
        FOREACH_CLIENT(IS_REAL_CLIENT(it), {
-               if (it.lmsplayer && it.lms_spectate_warning < 2)
+               if (INGAME(it) && it.lms_spectate_warning < 2)
                        ++M_ARGV(0, int); // activerealplayers
                ++M_ARGV(1, int); // realplayers
        });
@@ -458,7 +520,8 @@ MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
                if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
                {
                        player.lms_spectate_warning = 1;
-                       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");
+                       sprint(player, "^1WARNING:^7 you can't rejoin this match after spectating. Use the same command again to spectate anyway.\n");
+                       Send_Notification(NOTIF_ONE_ONLY, player, MSG_CENTER, CENTER_LMS_SPECWARN);
                }
                return MUT_SPECCMD_RETURN;
        }
@@ -477,13 +540,6 @@ MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
                M_ARGV(0, string) = autocvar_g_lms_weaponarena;
 }
 
-MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
-{
-       entity player = M_ARGV(0, entity);
-
-       return boolean(player.lmsplayer);
-}
-
 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
 {
        if(game_stopped)