]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Merge branch 'terencehill/lms_spectatable' into 'master'
authorterencehill <piuntn@gmail.com>
Sun, 12 Sep 2021 15:56:01 +0000 (15:56 +0000)
committerterencehill <piuntn@gmail.com>
Sun, 12 Sep 2021 15:56:01 +0000 (15:56 +0000)
LMS: refactor LMS code to avoid forcing players to join on connection

Closes #1833

See merge request xonotic/xonotic-data.pk3dir!923

gamemodes-server.cfg
qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc
qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
qcsrc/common/gamemodes/gamemode/lms/sv_lms.qh
qcsrc/common/minigames/sv_minigames.qc
qcsrc/server/client.qc
qcsrc/server/client.qh
qcsrc/server/clientkill.qc
qcsrc/server/command/sv_cmd.qc
qcsrc/server/mutators/events.qh

index be48bcd7bc293d1448ae420898741f32d5b794f0..17b90c624fde8f8af9a50534ad5e69fae6e27ead 100644 (file)
@@ -446,8 +446,8 @@ set g_lms 0 "Last Man Standing: everyone starts with a certain amount of lives,
 set g_lms_lives_override -1
 set g_lms_extra_lives 0
 set g_lms_regenerate 0
-set g_lms_last_join 3  "if g_lms_join_anytime is false, new players can only join if the worst active player has more than (fraglimit - g_lms_last_join) lives"
-set g_lms_join_anytime 1       "if true, new players can join, but get same amount of lives as the worst player"
+set g_lms_last_join 3  "if g_lms_join_anytime is 0, new players can only join if the worst active player has (fraglimit - g_lms_last_join) or more lives; in other words, new players can no longer join once the worst player loses more than g_lms_last_join lives"
+set g_lms_join_anytime 1       "1: new players can join, but get same amount of lives as the worst player; 0: new players can only join if the worst active player has (fraglimit - g_lms_last_join) or more lives"
 set g_lms_weaponarena "most_available" "starting weapons - takes the same options as g_weaponarena"
 
 
index ba2402f2768229c8576c3399a1084bb99649b02e..abdfe030eebd088fba76fc219b1acc73e04a0501 100644 (file)
@@ -227,14 +227,6 @@ MUTATOR_HOOKFUNCTION(ca, reset_map_players)
        return true;
 }
 
-MUTATOR_HOOKFUNCTION(ca, ClientConnect)
-{
-       entity player = M_ARGV(0, entity);
-
-       TRANSMUTE(Observer, player);
-       return true;
-}
-
 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
 {
        allowed_to_spawn = true;
@@ -302,6 +294,10 @@ MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
 {
        entity player = M_ARGV(0, entity);
 
+       bool is_forced = M_ARGV(1, bool);
+       if (is_forced && player.caplayer)
+               player.caplayer = 0;
+
        if (IS_PLAYER(player) && !IS_DEAD(player))
                ca_LastPlayerForTeam_Notify(player);
        if (player.killindicator_teamchange == -2) // player wants to spectate
index dc18b7c9072413a51d0d93d2aff0993682cb0750..e9ddb4e815ef2a10344f2e0cd11c4aeb17d86d6c 100644 (file)
@@ -36,9 +36,12 @@ void ClearWinners();
 // limit.
 int WinningCondition_LMS()
 {
+       if (warmup_stage || time <= game_starttime)
+               return WINNING_NO;
+
        entity first_player = NULL;
        int totalplayers = 0;
-       FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
+       FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
                if (!totalplayers)
                        first_player = it;
                ++totalplayers;
@@ -77,10 +80,7 @@ int WinningCondition_LMS()
                                // a winner!
                                // and assign him his first place
                                GameRules_scoring_add(first_player, LMS_RANK, 1);
-                               if(warmup_stage)
-                                       return WINNING_NO;
-                               else
-                                       return WINNING_YES;
+                               return WINNING_YES;
                        }
                }
        }
@@ -123,9 +123,25 @@ MUTATOR_HOOKFUNCTION(lms, reset_map_global)
 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;
+               }
+
+               CS(it).killcount = 0;
+               it.lmsplayer = 0;
+               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));
+
+               if (it.frags != FRAGS_PLAYER)
+                       continue;
+
                TRANSMUTE(Player, it);
-               it.frags = FRAGS_PLAYER;
-               GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
                PutClientInServer(it);
        });
 }
@@ -138,36 +154,84 @@ MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
        sv_ready_restart_after_countdown = 0;
 }
 
+// returns true if player is added to the game
+bool lms_AddPlayer(entity player)
+{
+       if (!player.lmsplayer)
+       {
+               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 (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());
+               }
+       }
+       else
+       {
+               if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
+               {
+                       Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
+                       return false;
+               }
+       }
+       return true;
+}
+
 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
 {
        entity player = M_ARGV(0, entity);
-
-       if(player.frags == FRAGS_SPECTATOR)
-               TRANSMUTE(Observer, player);
-       else
+       if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
        {
-               float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
-               if(tl < lms_lowest_lives)
-                       lms_lowest_lives = tl;
-               if(tl <= 0)
+               if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
                        TRANSMUTE(Observer, player);
-               if(warmup_stage)
-                       GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
        }
 }
 
-MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
+MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
 {
        entity player = M_ARGV(0, entity);
 
-       if(warmup_stage)
-               return false;
-       if(player.frags == FRAGS_SPECTATOR || GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
-       {
-               Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
+       if (warmup_stage || time < game_starttime)
                return true;
+
+       if (player.lmsplayer == 2) // just joined the game
+       {
+               // 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);
+               player.lmsplayer = 1;
        }
-       return false;
+}
+
+MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
+{
+       entity player = M_ARGV(0, entity);
+
+       if (warmup_stage || lms_AddPlayer(player))
+               return false;
+
+       return true;
 }
 
 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
@@ -186,7 +250,9 @@ MUTATOR_HOOKFUNCTION(lms, PlayerDies)
 
 void lms_RemovePlayer(entity player)
 {
-       static int quitters = 0;
+       if (warmup_stage || time < game_starttime)
+               return;
+
        float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
        if (!player_rank)
        {
@@ -194,19 +260,23 @@ void lms_RemovePlayer(entity player)
                {
                        player.frags = FRAGS_PLAYER_OUT_OF_GAME;
                        int pl_cnt = 0;
-                       FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
+                       FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
                                pl_cnt++;
                        });
                        GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
                }
                else
                {
+                       int min_forfeiter_rank = 665; // different from 666
                        FOREACH_CLIENT(true, {
+                               // 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)
                                {
@@ -215,12 +285,9 @@ void lms_RemovePlayer(entity player)
                                                lms_lowest_lives = tl;
                                }
                        });
-                       GameRules_scoring_add(player, LMS_RANK, 665 - 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));
-                               ++quitters;
-                       }
                        player.frags = FRAGS_PLAYER_OUT_OF_GAME;
                        TRANSMUTE(Observer, player);
                }
@@ -243,36 +310,38 @@ MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
        player.lms_spectate_warning = 3;
 
        lms_RemovePlayer(player);
+       player.lmsplayer = 0;
 }
 
 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
 {
        entity player = M_ARGV(0, entity);
+       bool is_forced = M_ARGV(1, bool);
 
        if (!IS_PLAYER(player))
                return true;
 
-       lms_RemovePlayer(player);
-       return true;  // prevent team reset
-}
-
-MUTATOR_HOOKFUNCTION(lms, ClientConnect)
-{
-       entity player = M_ARGV(0, entity);
-
-       if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
+       if (warmup_stage || time <= game_starttime)
        {
-               GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
+               GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
                player.frags = FRAGS_SPECTATOR;
+               TRANSMUTE(Observer, player);
+               player.lmsplayer = 0;
+       }
+       else
+       {
+               if (is_forced)
+                       player.lms_spectate_warning = 2;
+               if (!GameRules_scoring_add(player, LMS_RANK, 0))
+                       lms_RemovePlayer(player);
        }
+       return true;  // prevent team reset
 }
 
-// FIXME LMS doesn't allow clients to spectate due to its particular implementation
-MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
+MUTATOR_HOOKFUNCTION(lms, ClientConnect)
 {
-       if(autocvar_g_campaign)
-               return false;
-       return true;
+       entity player = M_ARGV(0, entity);
+       player.frags = FRAGS_SPECTATOR;
 }
 
 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
@@ -300,7 +369,7 @@ MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
 {
        entity frag_target = M_ARGV(1, entity);
 
-       if (!warmup_stage)
+       if (!warmup_stage && time > game_starttime)
        {
                // remove a life
                int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
@@ -309,7 +378,7 @@ MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
                if(tl <= 0)
                {
                        int pl_cnt = 0;
-                       FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
+                       FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
                                pl_cnt++;
                        });
                        frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
@@ -398,7 +467,8 @@ MUTATOR_HOOKFUNCTION(lms, ItemTouch)
 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
 {
        FOREACH_CLIENT(IS_REAL_CLIENT(it), {
-               ++M_ARGV(0, int); // activerealplayers
+               if (it.lmsplayer && it.lms_spectate_warning < 2)
+                       ++M_ARGV(0, int); // activerealplayers
                ++M_ARGV(1, int); // realplayers
        });
 
@@ -409,7 +479,7 @@ MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
 {
        entity player = M_ARGV(0, entity);
 
-       if(warmup_stage || player.lms_spectate_warning)
+       if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
        {
                // for the forfeit message...
                player.lms_spectate_warning = 2;
@@ -440,7 +510,9 @@ MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
 
 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
 {
-       return true;
+       entity player = M_ARGV(0, entity);
+
+       return boolean(player.lmsplayer);
 }
 
 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
index bf02920d2c1616207ac54545d3b331a617de90e6..1915530122d347fff45a0045bf8e93173ec53578 100644 (file)
@@ -7,6 +7,7 @@
 // 2 when player goes spectator (presses F3 to spectate for the second time)
 // 3 when player disconnects
 .int lms_spectate_warning;
+.int lmsplayer;
 
 #define autocvar_g_lms_lives_override cvar("g_lms_lives_override")
 string autocvar_g_lms_weaponarena = "most_available";
index 21f81055a463f59aa7650992bb59b14c6b5d36dd..a1fb3631dd8b4c634787b8a93c855b5934ab8de3 100644 (file)
@@ -148,9 +148,7 @@ int minigame_addplayer(entity minigame_session, entity player)
                Net_LinkEntity(player_pointer, false, 0, minigame_SendEntity);
 
                if ( !IS_OBSERVER(player) && autocvar_sv_minigames_observer )
-               {
-                       PutObserverInServer(player);
-               }
+                       PutObserverInServer(player, true);
                if ( autocvar_sv_minigames_observer == 2 )
                        Player_SetForcedTeamIndex(player, TEAM_FORCE_SPECTATOR);
 
index 804025ace1e5efab8a88d140ff2db5808a72b2eb..0477a2bc2c757375fa18f63a403233661a2a9295 100644 (file)
@@ -6,7 +6,6 @@
 #include <common/effects/qc/globalsound.qh>
 #include <common/ent_cs.qh>
 #include <common/gamemodes/_mod.qh>
-#include <common/gamemodes/gamemode/lms/sv_lms.qh>
 #include <common/gamemodes/gamemode/nexball/sv_nexball.qh>
 #include <common/items/_mod.qh>
 #include <common/items/inventory.qh>
@@ -236,9 +235,9 @@ void setplayermodel(entity e, string modelname)
 }
 
 /** putting a client as observer in the server */
-void PutObserverInServer(entity this)
+void PutObserverInServer(entity this, bool is_forced)
 {
-       bool mutator_returnvalue = MUTATOR_CALLHOOK(MakePlayerObserver, this);
+       bool mutator_returnvalue = MUTATOR_CALLHOOK(MakePlayerObserver, this, is_forced);
        PlayerState_detach(this);
 
        if (IS_PLAYER(this))
@@ -819,7 +818,7 @@ void PutClientInServer(entity this)
        MUTATOR_CALLHOOK(PutClientInServer, this);
 
        if (IS_OBSERVER(this)) {
-               PutObserverInServer(this);
+               PutObserverInServer(this, false);
        } else if (IS_PLAYER(this)) {
                PutPlayerInServer(this);
        }
@@ -1788,7 +1787,7 @@ bool SpectateSet(entity this)
        accuracy_resend(this);
 
        if(!SpectateUpdate(this))
-               PutObserverInServer(this);
+               PutObserverInServer(this, false);
 
        return true;
 }
@@ -2292,7 +2291,7 @@ void ObserverOrSpectatorThink(entity this)
                                TRANSMUTE(Observer, this);
                                PutClientInServer(this);
                        } else if(!SpectateUpdate(this) && !SpectateNext(this)) {
-                               PutObserverInServer(this);
+                               PutObserverInServer(this, false);
                                this.would_spectate = true;
                        }
                }
@@ -2318,7 +2317,7 @@ void ObserverOrSpectatorThink(entity this)
                        }
                }
                if(is_spec && !SpectateUpdate(this))
-                       PutObserverInServer(this);
+                       PutObserverInServer(this, false);
        }
        if (is_spec)
                this.flags |= FL_CLIENT | FL_NOTARGET;
@@ -2692,10 +2691,7 @@ void PlayerPostThink (entity this)
                                if (IS_PLAYER(this) && autocvar_sv_maxidle_playertospectator > 0)
                                {
                                        Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_MOVETOSPEC_IDLING, this.netname, maxidle_time);
-                                       if (this.caplayer)
-                                               this.caplayer = 0;
-                                       this.lms_spectate_warning = 2; // TODO: mutator hook for players forcibly moved to spectator?
-                                       PutObserverInServer(this);
+                                       PutObserverInServer(this, true);
                                }
                                else
                                {
index b7166609bd2f79f4c74010962c7aeb7c8dfbc7fc..e7fd3406895ce2e7a078c18862ec27ca755ea8db 100644 (file)
@@ -382,7 +382,7 @@ bool Spectate(entity this, entity pl);
 
 void ClientInit_Spawn();
 
-void PutObserverInServer(entity this);
+void PutObserverInServer(entity this, bool is_forced);
 
 void SetSpectatee(entity this, entity spectatee);
 void SetSpectatee_status(entity this, int spectatee_num);
index e87a2b11831c350714e352ddd436e08ade9ba6b7..9b29273a65b2f65edd5c2c3830e1230a64d20c29 100644 (file)
@@ -24,7 +24,7 @@ void ClientKill_Now_TeamChange(entity this)
        {
                if (blockSpectators)
                        Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_SPECTATE_WARNING, autocvar_g_maxplayers_spectator_blocktime);
-               PutObserverInServer(this);
+               PutObserverInServer(this, false);
        }
        else
        {
index 5a9fae567589e695843c8702ff91ef11e0dea5ea..47e9f922abe4daf27b5f8e696b05ff87b908d654 100644 (file)
@@ -184,8 +184,7 @@ void GameCommand_allspec(int request, int argc)
                        string reason = argv(1);
                        int n = 0;
                        FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
-                               if (it.caplayer) it.caplayer = 0;
-                               PutObserverInServer(it);
+                               PutObserverInServer(it, true);
                                ++n;
                        });
                        if (n)   bprint(strcat("Successfully forced all (", ftos(n), ") players to spectate", (reason ? strcat(" for reason: '", reason, "'") : ""), ".\n"));
@@ -1009,8 +1008,7 @@ void GameCommand_moveplayer(int request, int argc)
                                                string pl_name = playername(client.netname, client.team, false);
                                                if (!IS_SPEC(client) && !IS_OBSERVER(client))
                                                {
-                                                       if (client.caplayer) client.caplayer = 0;
-                                                       PutObserverInServer(client);
+                                                       PutObserverInServer(client, true);
 
                                                        successful = strcat(successful, (successful ? ", " : ""), pl_name);
                                                }
index f33fc4598e0f785f04a5138b27ebcab5b835125b..ee5d18a69157a9d6210bc211ed0f813748578442 100644 (file)
@@ -10,6 +10,7 @@
 /** called when a player becomes observer, after shared setup */
 #define EV_MakePlayerObserver(i, o) \
     /** player */ i(entity, MUTATOR_ARGV_0_entity) \
+    /** is_forced */ i(bool, MUTATOR_ARGV_1_bool) \
     /**/
 MUTATOR_HOOKABLE(MakePlayerObserver, EV_MakePlayerObserver)