]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
Set lms_spectate right when player is moved to spectator, not when the spectate comma...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / lms / sv_lms.qc
index 5742b28a5ce60095c78ef69cd7ccdf678ca43e55..2d7f724a1032cc3114c60ca862fe28ce6745abc9 100644 (file)
@@ -7,8 +7,10 @@
 #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;
 int autocvar_g_lms_leader_lives_diff = 2;
 float autocvar_g_lms_leader_minpercent = 0.5;
@@ -27,9 +29,10 @@ int autocvar_g_lms_dynamic_vampire_min_lives_diff = 2;
 
 .float lms_leader;
 int lms_leaders;
-bool lms_visible_leaders;
+float lms_visible_leaders_time;
+bool lms_visible_leaders = true; // triggers lms_visible_leaders_time update in the first frame
 bool lms_visible_leaders_prev;
-float lms_leader_interval_jitter;
+bool autocvar_g_lms_rot;
 
 // main functions
 int LMS_NewPlayerLives()
@@ -61,10 +64,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)
@@ -73,7 +82,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);
@@ -88,10 +97,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;
                        }
@@ -100,6 +114,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;
                        }
                }
@@ -109,6 +124,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
@@ -147,6 +167,7 @@ bool lms_waypointsprite_visible_for_player(entity this, entity player, entity vi
        return true;
 }
 
+int lms_leaders_lives_diff;
 void lms_UpdateLeaders()
 {
        int max_lives = 0;
@@ -168,8 +189,10 @@ void lms_UpdateLeaders()
                        second_max_lives = lives;
        });
 
+       lms_leaders_lives_diff = max_lives - second_max_lives;
+
        int lives_diff = autocvar_g_lms_leader_lives_diff;
-       if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_minpercent)
+       if (lms_leaders_lives_diff >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_minpercent)
                FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
                        int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
                        if (lives == max_lives)
@@ -200,17 +223,11 @@ MUTATOR_HOOKFUNCTION(lms, reset_map_players)
 {
        FOREACH_CLIENT(true, {
                if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
-               {
-                       // players who forfeited (rank >= 256) become spectators
-                       if (it.lms_spectate_warning == 2)
-                               it.frags = FRAGS_SPECTATOR;
-                       else
-                               it.frags = FRAGS_PLAYER;
-               }
+                       it.frags = FRAGS_PLAYER;
 
                CS(it).killcount = 0;
-               it.lmsplayer = 0;
-               it.lms_spectate_warning = 0;
+               INGAME_STATUS_CLEAR(it);
+               it.lms_spectate = false;
                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));
 
@@ -236,23 +253,23 @@ 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 = 2; // temp value indicating player has just joined the game (but not spawned yet)
+               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)
        {
-               if(player.lms_spectate_warning)
-               {
-                       player.lms_spectate_warning = 0;
-                       GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
-                       int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
-                       if(lives <= 0)
-                               GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
-               }
+               player.lms_spectate = false;
+               GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
+               int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
+               if(lives <= 0)
+                       GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
        }
        else
        {
@@ -282,7 +299,7 @@ MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
        if (warmup_stage || time < game_starttime)
                return true;
 
-       if (player.lmsplayer == 2) // just joined the game
+       if (INGAME_JOINING(player))
        {
                // spawn player with the same amount of health / armor
                // as the least healthy player with the least number of lives
@@ -299,7 +316,7 @@ MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
                        SetResource(player, RES_HEALTH, max(1, min_health));
                if (min_armorvalue != start_armorvalue)
                        SetResource(player, RES_ARMOR, min_armorvalue);
-               player.lmsplayer = 1;
+               INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
        }
 }
 
@@ -321,7 +338,7 @@ void lms_RemovePlayer(entity player)
        float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
        if (!player_rank)
        {
-               if (player.lms_spectate_warning < 2)
+               if (!player.lms_spectate)
                {
                        player.frags = FRAGS_PLAYER_OUT_OF_GAME;
                        int pl_cnt = 0;
@@ -330,42 +347,31 @@ void lms_RemovePlayer(entity player)
                        });
                        GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
                }
-               else
+               else if (INGAME(player))
                {
-                       int min_forfeiter_rank = 665; // different from 666
-                       FOREACH_CLIENT(true, {
-                               // update rank of other players that were eliminated
+                       FOREACH_CLIENT(it != player, {
+                               // update rank of other players
                                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)
-                               {
-                                       float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
-                                       if(tl < lms_lowest_lives)
-                                               lms_lowest_lives = tl;
-                               }
+                                       GameRules_scoring_add(it, LMS_RANK, -1);
                        });
-                       GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
+                       int rank = GameRules_scoring_add(player, LMS_RANK, 0);
+                       GameRules_scoring_add(player, LMS_RANK, -rank);
                        if(!warmup_stage)
                                GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
-                       player.frags = FRAGS_PLAYER_OUT_OF_GAME;
+                       player.frags = FRAGS_SPECTATOR;
                        TRANSMUTE(Observer, player);
+                       INGAME_STATUS_CLEAR(player);
+                       player.lms_spectate = false;
+                       CS(player).killcount = FRAGS_SPECTATOR;
                }
                if (autocvar_g_lms_leader_lives_diff > 0)
                        lms_UpdateLeaders();
        }
 
-       if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
+       if (CS(player).killcount != FRAGS_SPECTATOR)
        {
-               if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
+               if (GameRules_scoring_add(player, LMS_RANK, 0) > 0)
                        Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
-               else
-                       Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
        }
 }
 
@@ -373,11 +379,10 @@ MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
 {
        entity player = M_ARGV(0, entity);
 
-       // no further message other than the disconnect message
-       player.lms_spectate_warning = 3;
+       player.lms_spectate = true;
 
        lms_RemovePlayer(player);
-       player.lmsplayer = 0;
+       INGAME_STATUS_CLEAR(player);
 }
 
 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
@@ -393,12 +398,12 @@ 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 (is_forced || player.killindicator_teamchange == -2) // player is forced or wants to spectate
+                       player.lms_spectate = true;
                if (!GameRules_scoring_add(player, LMS_RANK, 0))
                        lms_RemovePlayer(player);
        }
@@ -415,8 +420,9 @@ MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
 {
        entity player = M_ARGV(0, entity);
 
-       // recycled REDALIVE to avoid adding a dedicated stat
+       // recycled REDALIVE and BLUEALIVE to avoid adding a dedicated stat
        STAT(REDALIVE, player) = lms_leaders;
+       STAT(BLUEALIVE, player) = lms_leaders_lives_diff;
 
        if(player.deadflag == DEAD_DYING)
                player.deadflag = DEAD_RESPAWNING;
@@ -427,10 +433,9 @@ MUTATOR_HOOKFUNCTION(lms, SV_StartFrame)
        float leader_time = autocvar_g_lms_leader_wp_time;
        float leader_interval = leader_time + autocvar_g_lms_leader_wp_interval;
        lms_visible_leaders_prev = lms_visible_leaders;
-       lms_visible_leaders = (time % leader_interval < leader_time);
+       lms_visible_leaders = (time > lms_visible_leaders_time && time < lms_visible_leaders_time + leader_time);
        if (lms_visible_leaders_prev && !lms_visible_leaders)
-               lms_leader_interval_jitter = random() * autocvar_g_lms_leader_wp_interval_jitter;
-       leader_interval += lms_leader_interval_jitter;
+               lms_visible_leaders_time = time + leader_interval + random() * autocvar_g_lms_leader_wp_interval_jitter;
 
        lms_leaders = 0;
        FOREACH_CLIENT(true, {
@@ -452,25 +457,27 @@ MUTATOR_HOOKFUNCTION(lms, SV_StartFrame)
                                        Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_LMS_VISIBLE_LEADER);
                                lms_leaders++;
                        }
-               }
-               else
-               {
-                       if (IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME)
+                       else // if (!it.lms_leader)
                        {
-                               if (!lms_visible_leaders_prev && lms_visible_leaders && IS_REAL_CLIENT(it))
-                                       Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_LMS_VISIBLE_OTHER);
+                               if (IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME)
+                               {
+                                       if (!lms_visible_leaders_prev && lms_visible_leaders && IS_REAL_CLIENT(it))
+                                               Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_LMS_VISIBLE_OTHER);
+                               }
+                               if (it.waypointsprite_attachedforcarrier)
+                                       WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
                        }
-                       if (it.waypointsprite_attachedforcarrier)
-                               WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
                }
        });
 }
 
 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, PlayerPowerups)
@@ -587,6 +594,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");
@@ -605,13 +615,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)
@@ -661,7 +674,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))
                        ++M_ARGV(0, int); // activerealplayers
                ++M_ARGV(1, int); // realplayers
        });
@@ -672,22 +685,10 @@ MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
 {
        entity player = M_ARGV(0, entity);
-
-       if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
-       {
-               // for the forfeit message...
-               player.lms_spectate_warning = 2;
-       }
-       else
-       {
-               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");
-               }
-               return MUT_SPECCMD_RETURN;
-       }
-       return MUT_SPECCMD_CONTINUE;
+       if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
+               return MUT_SPECCMD_CONTINUE;
+       // ranked players (out of game) can no longer become real spectators
+       return MUT_SPECCMD_RETURN;
 }
 
 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
@@ -702,13 +703,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)