2013-08-17 15:46:48 +03:00
|
|
|
/*
|
|
|
|
* CMapGenOptions.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 "CMapGenOptions.h"
|
|
|
|
|
|
|
|
#include "../mapping/CMap.h"
|
|
|
|
#include "CRmgTemplateStorage.h"
|
|
|
|
#include "CRmgTemplate.h"
|
|
|
|
#include "../VCMI_Lib.h"
|
|
|
|
#include "../CTownHandler.h"
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
CMapGenOptions::CMapGenOptions()
|
|
|
|
: width(CMapHeader::MAP_SIZE_MIDDLE), height(CMapHeader::MAP_SIZE_MIDDLE), hasTwoLevels(true),
|
2015-05-27 11:30:46 +02:00
|
|
|
playerCount(RANDOM_SIZE), teamCount(RANDOM_SIZE), compOnlyPlayerCount(RANDOM_SIZE), compOnlyTeamCount(RANDOM_SIZE), humanPlayersCount(0),
|
2013-08-17 15:46:48 +03:00
|
|
|
waterContent(EWaterContent::RANDOM), monsterStrength(EMonsterStrength::RANDOM), mapTemplate(nullptr)
|
|
|
|
{
|
|
|
|
resetPlayersMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 CMapGenOptions::getWidth() const
|
|
|
|
{
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setWidth(si32 value)
|
|
|
|
{
|
|
|
|
assert(value >= 1);
|
|
|
|
width = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 CMapGenOptions::getHeight() const
|
|
|
|
{
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setHeight(si32 value)
|
|
|
|
{
|
|
|
|
assert(value >= 1);
|
|
|
|
height = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CMapGenOptions::getHasTwoLevels() const
|
|
|
|
{
|
|
|
|
return hasTwoLevels;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setHasTwoLevels(bool value)
|
|
|
|
{
|
|
|
|
hasTwoLevels = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
si8 CMapGenOptions::getPlayerCount() const
|
|
|
|
{
|
|
|
|
return playerCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setPlayerCount(si8 value)
|
|
|
|
{
|
2014-07-26 15:20:33 +03:00
|
|
|
assert((value >= 1 && value <= PlayerColor::PLAYER_LIMIT_I) || value == RANDOM_SIZE);
|
2013-08-17 15:46:48 +03:00
|
|
|
playerCount = value;
|
2015-05-27 18:58:14 +02:00
|
|
|
|
2015-05-27 11:30:46 +02:00
|
|
|
auto possibleCompPlayersCount = value;
|
|
|
|
if (compOnlyPlayerCount > possibleCompPlayersCount)
|
2015-03-14 21:19:24 +02:00
|
|
|
setCompOnlyPlayerCount(possibleCompPlayersCount);
|
2015-05-27 11:30:46 +02:00
|
|
|
|
2018-10-27 18:18:08 +02:00
|
|
|
if (getPlayerCount() != RANDOM_SIZE)
|
|
|
|
{
|
|
|
|
if (getCompOnlyPlayerCount() != RANDOM_SIZE)
|
|
|
|
humanPlayersCount = getPlayerCount() - getCompOnlyPlayerCount();
|
|
|
|
else
|
|
|
|
humanPlayersCount = getPlayerCount();
|
|
|
|
}
|
2015-05-27 18:58:14 +02:00
|
|
|
|
2013-08-17 15:46:48 +03:00
|
|
|
resetPlayersMap();
|
|
|
|
}
|
|
|
|
|
2015-11-25 06:25:44 +02:00
|
|
|
si8 CMapGenOptions::getHumanOnlyPlayerCount() const
|
|
|
|
{
|
|
|
|
return humanPlayersCount;
|
|
|
|
}
|
|
|
|
|
2013-08-17 15:46:48 +03:00
|
|
|
si8 CMapGenOptions::getTeamCount() const
|
|
|
|
{
|
|
|
|
return teamCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setTeamCount(si8 value)
|
|
|
|
{
|
2015-05-27 18:58:14 +02:00
|
|
|
assert(getPlayerCount() == RANDOM_SIZE || (value >= 0 && value < getPlayerCount()) || value == RANDOM_SIZE);
|
2013-08-17 15:46:48 +03:00
|
|
|
teamCount = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
si8 CMapGenOptions::getCompOnlyPlayerCount() const
|
|
|
|
{
|
|
|
|
return compOnlyPlayerCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setCompOnlyPlayerCount(si8 value)
|
|
|
|
{
|
2015-11-25 06:25:44 +02:00
|
|
|
assert(value == RANDOM_SIZE || (getPlayerCount() == RANDOM_SIZE || (value >= 0 && value <= getPlayerCount())));
|
2013-08-17 15:46:48 +03:00
|
|
|
compOnlyPlayerCount = value;
|
2015-05-27 18:58:14 +02:00
|
|
|
|
|
|
|
if (getPlayerCount() != RANDOM_SIZE && getCompOnlyPlayerCount() != RANDOM_SIZE)
|
|
|
|
humanPlayersCount = getPlayerCount() - getCompOnlyPlayerCount();
|
|
|
|
|
2013-08-17 15:46:48 +03:00
|
|
|
resetPlayersMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
si8 CMapGenOptions::getCompOnlyTeamCount() const
|
|
|
|
{
|
|
|
|
return compOnlyTeamCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setCompOnlyTeamCount(si8 value)
|
|
|
|
{
|
|
|
|
assert(value == RANDOM_SIZE || compOnlyPlayerCount == RANDOM_SIZE || (value >= 0 && value <= std::max(compOnlyPlayerCount - 1, 0)));
|
|
|
|
compOnlyTeamCount = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
EWaterContent::EWaterContent CMapGenOptions::getWaterContent() const
|
|
|
|
{
|
|
|
|
return waterContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setWaterContent(EWaterContent::EWaterContent value)
|
|
|
|
{
|
|
|
|
waterContent = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
EMonsterStrength::EMonsterStrength CMapGenOptions::getMonsterStrength() const
|
|
|
|
{
|
|
|
|
return monsterStrength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setMonsterStrength(EMonsterStrength::EMonsterStrength value)
|
|
|
|
{
|
2018-07-26 12:06:55 +02:00
|
|
|
monsterStrength = value;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::resetPlayersMap()
|
|
|
|
{
|
2015-07-18 20:14:45 +02:00
|
|
|
|
|
|
|
std::map<PlayerColor, TFaction> rememberTownTypes;
|
|
|
|
|
|
|
|
for (auto p : players)
|
|
|
|
{
|
|
|
|
auto town = p.second.getStartingTown();
|
|
|
|
if (town != RANDOM_SIZE)
|
|
|
|
rememberTownTypes[p.first] = town;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-17 15:46:48 +03:00
|
|
|
players.clear();
|
2015-05-27 11:30:46 +02:00
|
|
|
int realPlayersCnt = humanPlayersCount;
|
|
|
|
int realCompOnlyPlayersCnt = (compOnlyPlayerCount == RANDOM_SIZE) ? (PlayerColor::PLAYER_LIMIT_I - realPlayersCnt) : compOnlyPlayerCount;
|
2015-03-14 21:19:24 +02:00
|
|
|
int totalPlayersLimit = realPlayersCnt + realCompOnlyPlayersCnt;
|
2015-05-27 18:58:14 +02:00
|
|
|
if (getPlayerCount() == RANDOM_SIZE || compOnlyPlayerCount == RANDOM_SIZE)
|
2015-03-14 21:19:24 +02:00
|
|
|
totalPlayersLimit = static_cast<int>(PlayerColor::PLAYER_LIMIT_I);
|
|
|
|
|
2015-05-27 11:30:46 +02:00
|
|
|
//FIXME: what happens with human players here?
|
2015-03-14 21:19:24 +02:00
|
|
|
for(int color = 0; color < totalPlayersLimit; ++color)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
CPlayerSettings player;
|
2015-07-18 20:14:45 +02:00
|
|
|
auto pc = PlayerColor(color);
|
|
|
|
player.setColor(pc);
|
2015-03-14 21:19:24 +02:00
|
|
|
auto playerType = EPlayerType::AI;
|
2018-10-27 18:18:08 +02:00
|
|
|
if (getPlayerCount() != RANDOM_SIZE && color < realPlayersCnt)
|
|
|
|
{
|
|
|
|
playerType = EPlayerType::HUMAN;
|
2018-10-28 17:36:11 +02:00
|
|
|
}
|
|
|
|
else if((getPlayerCount() != RANDOM_SIZE && color >= realPlayersCnt)
|
2015-03-14 21:19:24 +02:00
|
|
|
|| (compOnlyPlayerCount != RANDOM_SIZE && color >= (PlayerColor::PLAYER_LIMIT_I-compOnlyPlayerCount)))
|
|
|
|
{
|
|
|
|
playerType = EPlayerType::COMP_ONLY;
|
|
|
|
}
|
|
|
|
player.setPlayerType(playerType);
|
2015-07-18 20:14:45 +02:00
|
|
|
players[pc] = player;
|
|
|
|
|
|
|
|
if (vstd::contains(rememberTownTypes, pc))
|
|
|
|
players[pc].setStartingTown(rememberTownTypes[pc]);
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::map<PlayerColor, CMapGenOptions::CPlayerSettings> & CMapGenOptions::getPlayersSettings() const
|
|
|
|
{
|
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setStartingTownForPlayer(PlayerColor color, si32 town)
|
|
|
|
{
|
|
|
|
auto it = players.find(color);
|
|
|
|
if(it == players.end()) assert(0);
|
|
|
|
it->second.setStartingTown(town);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setPlayerTypeForStandardPlayer(PlayerColor color, EPlayerType::EPlayerType playerType)
|
|
|
|
{
|
|
|
|
assert(playerType != EPlayerType::COMP_ONLY);
|
|
|
|
auto it = players.find(color);
|
|
|
|
if(it == players.end()) assert(0);
|
|
|
|
it->second.setPlayerType(playerType);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CRmgTemplate * CMapGenOptions::getMapTemplate() const
|
|
|
|
{
|
|
|
|
return mapTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setMapTemplate(const CRmgTemplate * value)
|
|
|
|
{
|
|
|
|
mapTemplate = value;
|
|
|
|
//TODO validate & adapt options according to template
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::map<std::string, CRmgTemplate *> & CMapGenOptions::getAvailableTemplates() const
|
|
|
|
{
|
|
|
|
return VLC->tplh->getTemplates();
|
|
|
|
}
|
|
|
|
|
2014-03-17 22:51:07 +03:00
|
|
|
void CMapGenOptions::finalize(CRandomGenerator & rand)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->info("RMG settings: players %d, teams %d, computer players %d, computer teams %d, water %d, monsters %d",
|
2017-08-12 14:43:41 +02:00
|
|
|
static_cast<int>(getPlayerCount()), static_cast<int>(getTeamCount()), static_cast<int>(getCompOnlyPlayerCount()),
|
|
|
|
static_cast<int>(getCompOnlyTeamCount()), static_cast<int>(getWaterContent()), static_cast<int>(getMonsterStrength()));
|
2014-07-15 17:44:07 +03:00
|
|
|
|
2013-08-17 15:46:48 +03:00
|
|
|
if(!mapTemplate)
|
|
|
|
{
|
2014-03-17 22:51:07 +03:00
|
|
|
mapTemplate = getPossibleTemplate(rand);
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
2014-09-18 15:29:57 +03:00
|
|
|
assert(mapTemplate);
|
2013-08-17 15:46:48 +03:00
|
|
|
|
2015-05-27 18:58:14 +02:00
|
|
|
if (getPlayerCount() == RANDOM_SIZE)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
auto possiblePlayers = mapTemplate->getPlayers().getNumbers();
|
2015-06-01 21:57:43 +02:00
|
|
|
//ignore all non-randomized players, make sure these players will not be missing after roll
|
|
|
|
possiblePlayers.erase(possiblePlayers.begin(), possiblePlayers.lower_bound(countHumanPlayers() + countCompOnlyPlayers()));
|
2013-08-17 15:46:48 +03:00
|
|
|
assert(!possiblePlayers.empty());
|
2015-05-27 18:58:14 +02:00
|
|
|
setPlayerCount (*RandomGeneratorUtil::nextItem(possiblePlayers, rand));
|
2013-08-17 15:46:48 +03:00
|
|
|
updatePlayers();
|
|
|
|
}
|
|
|
|
if(teamCount == RANDOM_SIZE)
|
|
|
|
{
|
2015-05-27 18:58:14 +02:00
|
|
|
teamCount = rand.nextInt(getPlayerCount() - 1);
|
2014-05-24 23:10:46 +03:00
|
|
|
if (teamCount == 1)
|
|
|
|
teamCount = 0;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
if(compOnlyPlayerCount == RANDOM_SIZE)
|
|
|
|
{
|
|
|
|
auto possiblePlayers = mapTemplate->getCpuPlayers().getNumbers();
|
2014-05-23 10:22:22 +03:00
|
|
|
compOnlyPlayerCount = *RandomGeneratorUtil::nextItem(possiblePlayers, rand);
|
2013-08-17 15:46:48 +03:00
|
|
|
updateCompOnlyPlayers();
|
|
|
|
}
|
|
|
|
if(compOnlyTeamCount == RANDOM_SIZE)
|
|
|
|
{
|
2014-03-17 22:51:07 +03:00
|
|
|
compOnlyTeamCount = rand.nextInt(std::max(compOnlyPlayerCount - 1, 0));
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(waterContent == EWaterContent::RANDOM)
|
|
|
|
{
|
2014-06-04 11:16:08 +03:00
|
|
|
waterContent = static_cast<EWaterContent::EWaterContent>(rand.nextInt(EWaterContent::NONE, EWaterContent::ISLANDS));
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
if(monsterStrength == EMonsterStrength::RANDOM)
|
|
|
|
{
|
2014-06-04 11:16:08 +03:00
|
|
|
monsterStrength = static_cast<EMonsterStrength::EMonsterStrength>(rand.nextInt(EMonsterStrength::GLOBAL_WEAK, EMonsterStrength::GLOBAL_STRONG));
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
2014-06-03 09:57:20 +03:00
|
|
|
|
2014-09-18 15:29:57 +03:00
|
|
|
assert (vstd::iswithin(waterContent, EWaterContent::NONE, EWaterContent::ISLANDS));
|
|
|
|
assert (vstd::iswithin(monsterStrength, EMonsterStrength::GLOBAL_WEAK, EMonsterStrength::GLOBAL_STRONG));
|
|
|
|
|
2015-06-01 21:57:43 +02:00
|
|
|
|
2014-06-03 09:57:20 +03:00
|
|
|
//rectangular maps are the future of gaming
|
|
|
|
//setHeight(20);
|
|
|
|
//setWidth(50);
|
2015-06-01 21:57:43 +02:00
|
|
|
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->trace("Player config:");
|
2015-06-01 21:57:43 +02:00
|
|
|
int humanPlayers = 0, cpuOnlyPlayers = 0, AIplayers = 0;
|
|
|
|
for (auto player : players)
|
|
|
|
{
|
|
|
|
std::string playerType;
|
|
|
|
switch (player.second.getPlayerType())
|
|
|
|
{
|
|
|
|
case EPlayerType::AI:
|
|
|
|
playerType = "AI";
|
|
|
|
AIplayers++;
|
|
|
|
break;
|
|
|
|
case EPlayerType::COMP_ONLY:
|
|
|
|
playerType = "computer only";
|
|
|
|
cpuOnlyPlayers++;
|
|
|
|
break;
|
|
|
|
case EPlayerType::HUMAN:
|
|
|
|
playerType = "human only";
|
|
|
|
humanPlayers++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
2017-08-10 19:17:10 +02:00
|
|
|
logGlobal->trace("Player %d: %s", player.second.getColor(), playerType);
|
2015-06-01 21:57:43 +02:00
|
|
|
}
|
|
|
|
setCompOnlyPlayerCount(cpuOnlyPlayers); //human players are set automaticlaly (?)
|
2017-08-10 19:17:10 +02:00
|
|
|
logGlobal->info("Final player config: %d total, %d cpu-only", players.size(), (int)getCompOnlyPlayerCount());
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::updatePlayers()
|
|
|
|
{
|
|
|
|
// Remove AI players only from the end of the players map if necessary
|
|
|
|
for(auto itrev = players.end(); itrev != players.begin();)
|
|
|
|
{
|
|
|
|
auto it = itrev;
|
|
|
|
--it;
|
2015-05-27 18:58:14 +02:00
|
|
|
if (players.size() == getPlayerCount()) break;
|
2013-08-17 15:46:48 +03:00
|
|
|
if(it->second.getPlayerType() == EPlayerType::AI)
|
|
|
|
{
|
|
|
|
players.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--itrev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::updateCompOnlyPlayers()
|
|
|
|
{
|
|
|
|
// Remove comp only players only from the end of the players map if necessary
|
|
|
|
for(auto itrev = players.end(); itrev != players.begin();)
|
|
|
|
{
|
|
|
|
auto it = itrev;
|
|
|
|
--it;
|
2015-05-27 18:58:14 +02:00
|
|
|
if (players.size() <= getPlayerCount()) break;
|
2013-08-17 15:46:48 +03:00
|
|
|
if(it->second.getPlayerType() == EPlayerType::COMP_ONLY)
|
|
|
|
{
|
|
|
|
players.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--itrev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add some comp only players if necessary
|
2015-05-27 18:58:14 +02:00
|
|
|
int compOnlyPlayersToAdd = getPlayerCount() - players.size();
|
2016-02-15 12:34:37 +02:00
|
|
|
|
2015-05-27 18:58:14 +02:00
|
|
|
if (compOnlyPlayersToAdd < 0)
|
|
|
|
{
|
2017-08-10 19:17:10 +02:00
|
|
|
logGlobal->error("Incorrect number of players to add. Requested players %d, current players %d", playerCount, players.size());
|
2015-05-27 18:58:14 +02:00
|
|
|
assert (compOnlyPlayersToAdd < 0);
|
|
|
|
}
|
2013-08-17 15:46:48 +03:00
|
|
|
for(int i = 0; i < compOnlyPlayersToAdd; ++i)
|
|
|
|
{
|
|
|
|
CPlayerSettings pSettings;
|
|
|
|
pSettings.setPlayerType(EPlayerType::COMP_ONLY);
|
|
|
|
pSettings.setColor(getNextPlayerColor());
|
|
|
|
players[pSettings.getColor()] = pSettings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CMapGenOptions::countHumanPlayers() const
|
|
|
|
{
|
|
|
|
return static_cast<int>(boost::count_if(players, [](const std::pair<PlayerColor, CPlayerSettings> & pair)
|
|
|
|
{
|
|
|
|
return pair.second.getPlayerType() == EPlayerType::HUMAN;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2015-06-01 21:57:43 +02:00
|
|
|
int CMapGenOptions::countCompOnlyPlayers() const
|
|
|
|
{
|
|
|
|
return static_cast<int>(boost::count_if(players, [](const std::pair<PlayerColor, CPlayerSettings> & pair)
|
|
|
|
{
|
|
|
|
return pair.second.getPlayerType() == EPlayerType::COMP_ONLY;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-17 15:46:48 +03:00
|
|
|
PlayerColor CMapGenOptions::getNextPlayerColor() const
|
|
|
|
{
|
|
|
|
for(PlayerColor i = PlayerColor(0); i < PlayerColor::PLAYER_LIMIT; i.advance(1))
|
|
|
|
{
|
|
|
|
if(!players.count(i))
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->error("Failed to get next player color");
|
2015-05-27 18:58:14 +02:00
|
|
|
assert(false);
|
2013-08-17 15:46:48 +03:00
|
|
|
return PlayerColor(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CMapGenOptions::checkOptions() const
|
|
|
|
{
|
|
|
|
assert(countHumanPlayers() > 0);
|
|
|
|
if(mapTemplate)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CRandomGenerator gen;
|
|
|
|
return getPossibleTemplate(gen) != nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 22:51:07 +03:00
|
|
|
const CRmgTemplate * CMapGenOptions::getPossibleTemplate(CRandomGenerator & rand) const
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
// Find potential templates
|
|
|
|
const auto & tpls = getAvailableTemplates();
|
|
|
|
std::list<const CRmgTemplate *> potentialTpls;
|
|
|
|
for(const auto & tplPair : tpls)
|
|
|
|
{
|
|
|
|
const auto & tpl = tplPair.second;
|
2018-03-09 20:11:20 +02:00
|
|
|
int3 tplSize(width, height, (hasTwoLevels ? 2 : 1));
|
|
|
|
if(tpl->matchesSize(tplSize))
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
bool isPlayerCountValid = false;
|
2015-05-27 18:58:14 +02:00
|
|
|
if (getPlayerCount() != RANDOM_SIZE)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
2015-05-27 18:58:14 +02:00
|
|
|
if (tpl->getPlayers().isInRange(getPlayerCount()))
|
2015-05-27 11:30:46 +02:00
|
|
|
isPlayerCountValid = true;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Human players shouldn't be banned when playing with random player count
|
|
|
|
auto playerNumbers = tpl->getPlayers().getNumbers();
|
2015-05-27 11:30:46 +02:00
|
|
|
if(countHumanPlayers() <= *boost::min_element(playerNumbers))
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
isPlayerCountValid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 11:30:46 +02:00
|
|
|
if (isPlayerCountValid)
|
2013-08-17 15:46:48 +03:00
|
|
|
{
|
|
|
|
bool isCpuPlayerCountValid = false;
|
|
|
|
if(compOnlyPlayerCount != RANDOM_SIZE)
|
|
|
|
{
|
2015-05-27 11:30:46 +02:00
|
|
|
if (tpl->getCpuPlayers().isInRange(compOnlyPlayerCount))
|
|
|
|
isCpuPlayerCountValid = true;
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isCpuPlayerCountValid = true;
|
|
|
|
}
|
|
|
|
|
2015-05-27 11:30:46 +02:00
|
|
|
if(isCpuPlayerCountValid)
|
|
|
|
potentialTpls.push_back(tpl);
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select tpl
|
|
|
|
if(potentialTpls.empty())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-17 22:51:07 +03:00
|
|
|
return *RandomGeneratorUtil::nextItem(potentialTpls, rand);
|
2013-08-17 15:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CMapGenOptions::CPlayerSettings::CPlayerSettings() : color(0), startingTown(RANDOM_TOWN), playerType(EPlayerType::AI)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor CMapGenOptions::CPlayerSettings::getColor() const
|
|
|
|
{
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::CPlayerSettings::setColor(PlayerColor value)
|
|
|
|
{
|
|
|
|
assert(value >= PlayerColor(0) && value < PlayerColor::PLAYER_LIMIT);
|
|
|
|
color = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 CMapGenOptions::CPlayerSettings::getStartingTown() const
|
|
|
|
{
|
|
|
|
return startingTown;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::CPlayerSettings::setStartingTown(si32 value)
|
|
|
|
{
|
|
|
|
assert(value >= -1);
|
|
|
|
if(value >= 0)
|
|
|
|
{
|
|
|
|
assert(value < static_cast<int>(VLC->townh->factions.size()));
|
|
|
|
assert(VLC->townh->factions[value]->town != nullptr);
|
|
|
|
}
|
|
|
|
startingTown = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
EPlayerType::EPlayerType CMapGenOptions::CPlayerSettings::getPlayerType() const
|
|
|
|
{
|
|
|
|
return playerType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::CPlayerSettings::setPlayerType(EPlayerType::EPlayerType value)
|
|
|
|
{
|
|
|
|
playerType = value;
|
|
|
|
}
|