2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* StartInfo.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "StartInfo.h"
|
|
|
|
|
|
|
|
#include "CGeneralTextHandler.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "VCMI_Lib.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "rmg/CMapGenOptions.h"
|
|
|
|
#include "mapping/CMapInfo.h"
|
2023-06-25 21:28:24 +02:00
|
|
|
#include "campaign/CampaignState.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "mapping/CMapHeader.h"
|
2023-04-16 13:38:13 +02:00
|
|
|
#include "mapping/CMapService.h"
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "modding/ModIncompatibility.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
PlayerSettings::PlayerSettings()
|
2023-06-29 18:09:47 +02:00
|
|
|
: bonus(RANDOM), castle(NONE), hero(RANDOM), heroPortrait(RANDOM), color(0), handicap(NO_HANDICAP), compOnly(false)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlayerSettings::isControlledByAI() const
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
return connectedPlayerIDs.empty();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PlayerSettings::isControlledByHuman() const
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
return !connectedPlayerIDs.empty();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
PlayerSettings & StartInfo::getIthPlayersSettings(const PlayerColor & no)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(playerInfos.find(no) != playerInfos.end())
|
|
|
|
return playerInfos[no];
|
2023-09-04 21:21:02 +02:00
|
|
|
logGlobal->error("Cannot find info about player %s. Throwing...", no.toString());
|
2018-01-05 19:21:07 +02:00
|
|
|
throw std::runtime_error("Cannot find info about player");
|
|
|
|
}
|
2023-03-13 23:26:44 +02:00
|
|
|
const PlayerSettings & StartInfo::getIthPlayersSettings(const PlayerColor & no) const
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
return const_cast<StartInfo &>(*this).getIthPlayersSettings(no);
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerSettings * StartInfo::getPlayersSettings(const ui8 connectedPlayerId)
|
|
|
|
{
|
|
|
|
for(auto & elem : playerInfos)
|
|
|
|
{
|
|
|
|
if(vstd::contains(elem.second.connectedPlayerIDs, connectedPlayerId))
|
|
|
|
return &elem.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string StartInfo::getCampaignName() const
|
|
|
|
{
|
2023-06-27 13:09:04 +02:00
|
|
|
if(!campState->getName().empty())
|
2023-06-26 00:07:55 +02:00
|
|
|
return campState->getName();
|
2018-01-05 19:21:07 +02:00
|
|
|
else
|
|
|
|
return VLC->generaltexth->allTexts[508];
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyInfo::verifyStateBeforeStart(bool ignoreNoHuman) const
|
|
|
|
{
|
2023-04-16 13:38:13 +02:00
|
|
|
if(!mi || !mi->mapHeader)
|
2023-09-12 20:34:58 +02:00
|
|
|
throw std::domain_error(VLC->generaltexth->translate("core.genrltxt.529"));
|
2023-04-16 13:38:13 +02:00
|
|
|
|
|
|
|
auto missingMods = CMapService::verifyMapHeaderMods(*mi->mapHeader);
|
2023-09-23 00:32:48 +02:00
|
|
|
ModIncompatibility::ModListWithVersion modList;
|
2023-04-16 13:38:13 +02:00
|
|
|
for(const auto & m : missingMods)
|
2023-09-21 04:31:08 +02:00
|
|
|
modList.push_back({m.second.name, m.second.version.toString()});
|
2023-04-16 13:38:13 +02:00
|
|
|
|
|
|
|
if(!modList.empty())
|
2023-09-23 00:32:48 +02:00
|
|
|
throw ModIncompatibility(modList);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
//there must be at least one human player before game can be started
|
|
|
|
std::map<PlayerColor, PlayerSettings>::const_iterator i;
|
|
|
|
for(i = si->playerInfos.cbegin(); i != si->playerInfos.cend(); i++)
|
|
|
|
if(i->second.isControlledByHuman())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(i == si->playerInfos.cend() && !ignoreNoHuman)
|
2023-09-12 20:34:58 +02:00
|
|
|
throw std::domain_error(VLC->generaltexth->translate("core.genrltxt.530"));
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
if(si->mapGenOptions && si->mode == StartInfo::NEW_GAME)
|
|
|
|
{
|
|
|
|
if(!si->mapGenOptions->checkOptions())
|
2023-09-12 20:34:58 +02:00
|
|
|
throw std::domain_error(VLC->generaltexth->translate("core.genrltxt.751"));
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LobbyInfo::isClientHost(int clientId) const
|
|
|
|
{
|
|
|
|
return clientId == hostClientId;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<PlayerColor> LobbyInfo::getAllClientPlayers(int clientId)
|
|
|
|
{
|
|
|
|
std::set<PlayerColor> players;
|
|
|
|
for(auto & elem : si->playerInfos)
|
|
|
|
{
|
|
|
|
if(isClientHost(clientId) && elem.second.isControlledByAI())
|
|
|
|
players.insert(elem.first);
|
|
|
|
|
|
|
|
for(ui8 id : elem.second.connectedPlayerIDs)
|
|
|
|
{
|
|
|
|
if(vstd::contains(getConnectedPlayerIdsForClient(clientId), id))
|
|
|
|
players.insert(elem.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(isClientHost(clientId))
|
|
|
|
players.insert(PlayerColor::NEUTRAL);
|
|
|
|
|
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ui8> LobbyInfo::getConnectedPlayerIdsForClient(int clientId) const
|
|
|
|
{
|
|
|
|
std::vector<ui8> ids;
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & pair : playerNames)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(pair.second.connection == clientId)
|
|
|
|
{
|
|
|
|
for(auto & elem : si->playerInfos)
|
|
|
|
{
|
|
|
|
if(vstd::contains(elem.second.connectedPlayerIDs, pair.first))
|
|
|
|
ids.push_back(pair.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<PlayerColor> LobbyInfo::clientHumanColors(int clientId)
|
|
|
|
{
|
|
|
|
std::set<PlayerColor> players;
|
|
|
|
for(auto & elem : si->playerInfos)
|
|
|
|
{
|
|
|
|
for(ui8 id : elem.second.connectedPlayerIDs)
|
|
|
|
{
|
|
|
|
if(vstd::contains(getConnectedPlayerIdsForClient(clientId), id))
|
|
|
|
{
|
|
|
|
players.insert(elem.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PlayerColor LobbyInfo::clientFirstColor(int clientId) const
|
|
|
|
{
|
|
|
|
for(auto & pair : si->playerInfos)
|
|
|
|
{
|
|
|
|
if(isClientColor(clientId, pair.first))
|
|
|
|
return pair.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PlayerColor::CANNOT_DETERMINE;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
bool LobbyInfo::isClientColor(int clientId, const PlayerColor & color) const
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(si->playerInfos.find(color) != si->playerInfos.end())
|
|
|
|
{
|
|
|
|
for(ui8 id : si->playerInfos.find(color)->second.connectedPlayerIDs)
|
|
|
|
{
|
|
|
|
if(playerNames.find(id) != playerNames.end())
|
|
|
|
{
|
|
|
|
if(playerNames.find(id)->second.connection == clientId)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui8 LobbyInfo::clientFirstId(int clientId) const
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & pair : playerNames)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(pair.second.connection == clientId)
|
|
|
|
return pair.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerInfo & LobbyInfo::getPlayerInfo(int color)
|
|
|
|
{
|
|
|
|
return mi->mapHeader->players[color];
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
TeamID LobbyInfo::getPlayerTeamId(const PlayerColor & color)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-08-27 00:35:38 +02:00
|
|
|
if(color.isValidPlayer())
|
2018-01-05 19:21:07 +02:00
|
|
|
return getPlayerInfo(color.getNum()).team;
|
|
|
|
else
|
|
|
|
return TeamID::NO_TEAM;
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|