From 75d4ac2512a87e587f795fa062e25204100cfbe6 Mon Sep 17 00:00:00 2001 From: Lukas Werling Date: Sat, 2 Feb 2019 00:20:50 +0100 Subject: [PATCH] Fix team count inconsistency with random teams (#2051) Switching to random teams would remove all teams but the first two on the host, but not on the clients. With this fix, the extra teams are removed on the clients as well. This fixes a desync when using GetTeamCount() in a sync-relevant way. --- src/control/C4Teams.cpp | 29 +++++++++++++++++++++-------- src/control/C4Teams.h | 3 +++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/control/C4Teams.cpp b/src/control/C4Teams.cpp index d43dc8085..05e7d73d2 100644 --- a/src/control/C4Teams.cpp +++ b/src/control/C4Teams.cpp @@ -671,7 +671,13 @@ void C4TeamList::RecheckTeams() // automatic team distributions only if (!IsRandomTeam()) return; // host decides random teams - if (!::Control.isCtrlHost()) return; + if (!::Control.isCtrlHost()) + { + // Still make sure that we have the right number of teams on the clients as well so that + // GetTeamCount() does not report inconsistent values. + EnsureTeamCount(); + return; + } // random teams in auto generate mode? Make sure there are exactly two teams if (IsAutoGenerateTeams() && GetTeamCount() != 2) { @@ -710,6 +716,19 @@ void C4TeamList::RecheckTeams() } } +void C4TeamList::EnsureTeamCount() +{ + // in random autogenerate mode, there must be exactly two teams + if (IsRandomTeam()) + { + if (IsAutoGenerateTeams() && GetTeamCount() != 2) + { + ClearTeams(); + GenerateDefaultTeams(2); + } + } +} + void C4TeamList::ReassignAllTeams() { assert(::Control.isCtrlHost()); @@ -731,13 +750,7 @@ void C4TeamList::ReassignAllTeams() } // clear players from team lists RecheckPlayers(); - // in random autogenerate mode, there must be exactly two teams - if (IsRandomTeam()) - if (IsAutoGenerateTeams() && GetTeamCount() != 2) - { - ClearTeams(); - GenerateDefaultTeams(2); - } + EnsureTeamCount(); // reassign them idStart = -1; while ((pNfo = Game.PlayerInfos.GetNextPlayerInfoByID(idStart))) diff --git a/src/control/C4Teams.h b/src/control/C4Teams.h index 1662455d3..91f6f6f85 100644 --- a/src/control/C4Teams.h +++ b/src/control/C4Teams.h @@ -195,6 +195,9 @@ public: // any changed players will be flagged "updated" void RecheckTeams(); + // Makes sure that there's the right amount of teams when switching to random teams. + void EnsureTeamCount(); + // marks all unjoined players as not-in-team and reassigns a team for them // also automatically flags all affected player infos as updated void ReassignAllTeams();