2013-01-06 22:30:12 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CMapGenerator.h"
|
|
|
|
|
2013-04-07 13:48:07 +03:00
|
|
|
#include "../mapping/CMap.h"
|
2013-01-06 22:30:12 +03:00
|
|
|
#include "../VCMI_Lib.h"
|
|
|
|
#include "../CGeneralTextHandler.h"
|
2013-04-07 13:48:07 +03:00
|
|
|
#include "../mapping/CMapEditManager.h"
|
2013-01-06 22:30:12 +03:00
|
|
|
#include "../CObjectHandler.h"
|
|
|
|
#include "../CDefObjInfoHandler.h"
|
|
|
|
#include "../CTownHandler.h"
|
|
|
|
#include "../StringConstants.h"
|
2013-05-11 20:36:11 +03:00
|
|
|
#include "../filesystem/CResourceLoader.h"
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CMapGenOptions::CMapGenOptions() : width(CMapHeader::MAP_SIZE_MIDDLE), height(CMapHeader::MAP_SIZE_MIDDLE), hasTwoLevels(false),
|
|
|
|
playerCount(RANDOM_SIZE), teamCount(RANDOM_SIZE), compOnlyPlayerCount(0), compOnlyTeamCount(RANDOM_SIZE),
|
|
|
|
waterContent(EWaterContent::RANDOM), monsterStrength(EMonsterStrength::RANDOM), mapTemplate(nullptr)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
|
|
|
resetPlayersMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 CMapGenOptions::getWidth() const
|
|
|
|
{
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setWidth(si32 value)
|
|
|
|
{
|
2013-04-22 19:04:17 +03:00
|
|
|
assert(value >= 1);
|
|
|
|
width = value;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si32 CMapGenOptions::getHeight() const
|
|
|
|
{
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setHeight(si32 value)
|
|
|
|
{
|
2013-04-22 19:04:17 +03:00
|
|
|
assert(value >= 1);
|
|
|
|
height = value;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CMapGenOptions::getHasTwoLevels() const
|
|
|
|
{
|
|
|
|
return hasTwoLevels;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setHasTwoLevels(bool value)
|
|
|
|
{
|
|
|
|
hasTwoLevels = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
si8 CMapGenOptions::getPlayerCount() const
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return playerCount;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CMapGenOptions::setPlayerCount(si8 value)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-04-22 19:04:17 +03:00
|
|
|
assert((value >= 1 && value <= PlayerColor::PLAYER_LIMIT_I) || value == RANDOM_SIZE);
|
2013-05-21 22:08:06 +03:00
|
|
|
playerCount = value;
|
2013-04-22 19:04:17 +03:00
|
|
|
resetPlayersMap();
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
si8 CMapGenOptions::getTeamCount() const
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return teamCount;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CMapGenOptions::setTeamCount(si8 value)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
assert(playerCount == RANDOM_SIZE || (value >= 0 && value < playerCount) || value == RANDOM_SIZE);
|
|
|
|
teamCount = value;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
si8 CMapGenOptions::getCompOnlyPlayerCount() const
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return compOnlyPlayerCount;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CMapGenOptions::setCompOnlyPlayerCount(si8 value)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
assert(value == RANDOM_SIZE || (value >= 0 && value <= PlayerColor::PLAYER_LIMIT_I - playerCount));
|
|
|
|
compOnlyPlayerCount = value;
|
2013-04-22 19:04:17 +03:00
|
|
|
resetPlayersMap();
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
si8 CMapGenOptions::getCompOnlyTeamCount() const
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return compOnlyTeamCount;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CMapGenOptions::setCompOnlyTeamCount(si8 value)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
assert(value == RANDOM_SIZE || compOnlyPlayerCount == RANDOM_SIZE || (value >= 0 && value <= std::max(compOnlyPlayerCount - 1, 0)));
|
|
|
|
compOnlyTeamCount = value;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
monsterStrength = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::resetPlayersMap()
|
|
|
|
{
|
|
|
|
players.clear();
|
2013-05-21 22:08:06 +03:00
|
|
|
int realPlayersCnt = playerCount == RANDOM_SIZE ? static_cast<int>(PlayerColor::PLAYER_LIMIT_I) : playerCount;
|
|
|
|
int realCompOnlyPlayersCnt = compOnlyPlayerCount == RANDOM_SIZE ? (PlayerColor::PLAYER_LIMIT_I - realPlayersCnt) : compOnlyPlayerCount;
|
2013-04-15 20:18:04 +03:00
|
|
|
for(int color = 0; color < (realPlayersCnt + realCompOnlyPlayersCnt); ++color)
|
|
|
|
{
|
|
|
|
CPlayerSettings player;
|
|
|
|
player.setColor(PlayerColor(color));
|
|
|
|
player.setPlayerType((color >= realPlayersCnt) ? EPlayerType::COMP_ONLY : EPlayerType::AI);
|
|
|
|
players[PlayerColor(color)] = player;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::map<PlayerColor, CMapGenOptions::CPlayerSettings> & CMapGenOptions::getPlayersSettings() const
|
|
|
|
{
|
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setStartingTownForPlayer(PlayerColor color, si32 town)
|
|
|
|
{
|
|
|
|
auto it = players.find(color);
|
2013-04-22 19:04:17 +03:00
|
|
|
if(it == players.end()) assert(0);
|
2013-04-15 20:18:04 +03:00
|
|
|
it->second.setStartingTown(town);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::setPlayerTypeForStandardPlayer(PlayerColor color, EPlayerType::EPlayerType playerType)
|
|
|
|
{
|
2013-04-22 19:04:17 +03:00
|
|
|
assert(playerType != EPlayerType::COMP_ONLY);
|
2013-04-15 20:18:04 +03:00
|
|
|
auto it = players.find(color);
|
2013-04-22 19:04:17 +03:00
|
|
|
if(it == players.end()) assert(0);
|
2013-04-15 20:18:04 +03:00
|
|
|
it->second.setPlayerType(playerType);
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
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 CRmgTemplateStorage::get().getTemplates();
|
|
|
|
}
|
|
|
|
|
2013-04-15 20:18:04 +03:00
|
|
|
void CMapGenOptions::finalize()
|
|
|
|
{
|
|
|
|
CRandomGenerator gen;
|
|
|
|
finalize(gen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::finalize(CRandomGenerator & gen)
|
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(!mapTemplate)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
mapTemplate = getPossibleTemplate(gen);
|
|
|
|
assert(mapTemplate);
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
|
|
|
|
if(playerCount == RANDOM_SIZE)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
auto possiblePlayers = mapTemplate->getPlayers().getNumbers();
|
|
|
|
possiblePlayers.erase(possiblePlayers.begin(), possiblePlayers.lower_bound(countHumanPlayers()));
|
|
|
|
assert(!possiblePlayers.empty());
|
|
|
|
playerCount = *std::next(possiblePlayers.begin(), gen.getInteger(0, possiblePlayers.size() - 1));
|
|
|
|
updatePlayers();
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
if(teamCount == RANDOM_SIZE)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
teamCount = gen.getInteger(0, playerCount - 1);
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
if(compOnlyPlayerCount == RANDOM_SIZE)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
auto possiblePlayers = mapTemplate->getCpuPlayers().getNumbers();
|
|
|
|
compOnlyPlayerCount = *std::next(possiblePlayers.begin(), gen.getInteger(0, possiblePlayers.size() - 1));
|
|
|
|
updateCompOnlyPlayers();
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
if(compOnlyTeamCount == RANDOM_SIZE)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
compOnlyTeamCount = gen.getInteger(0, std::max(compOnlyPlayerCount - 1, 0));
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1 team isn't allowed
|
2013-05-21 22:08:06 +03:00
|
|
|
if(teamCount == 1 && compOnlyPlayerCount == 0)
|
2013-04-15 20:18:04 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
teamCount = 0;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(waterContent == EWaterContent::RANDOM)
|
|
|
|
{
|
|
|
|
waterContent = static_cast<EWaterContent::EWaterContent>(gen.getInteger(0, 2));
|
|
|
|
}
|
|
|
|
if(monsterStrength == EMonsterStrength::RANDOM)
|
|
|
|
{
|
|
|
|
monsterStrength = static_cast<EMonsterStrength::EMonsterStrength>(gen.getInteger(0, 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +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;
|
|
|
|
if(players.size() == playerCount) break;
|
|
|
|
if(it->second.getPlayerType() == EPlayerType::AI)
|
|
|
|
{
|
|
|
|
players.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--itrev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::updateCompOnlyPlayers()
|
|
|
|
{
|
|
|
|
auto totalPlayersCnt = playerCount + compOnlyPlayerCount;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
if(players.size() <= totalPlayersCnt) break;
|
|
|
|
if(it->second.getPlayerType() == EPlayerType::COMP_ONLY)
|
|
|
|
{
|
|
|
|
players.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--itrev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add some comp only players if necessary
|
|
|
|
auto compOnlyPlayersToAdd = totalPlayersCnt - players.size();
|
|
|
|
for(int i = 0; i < compOnlyPlayersToAdd; ++i)
|
|
|
|
{
|
|
|
|
CPlayerSettings pSettings;
|
|
|
|
pSettings.setPlayerType(EPlayerType::COMP_ONLY);
|
|
|
|
pSettings.setColor(getNextPlayerColor());
|
|
|
|
players[pSettings.getColor()] = pSettings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 20:18:04 +03:00
|
|
|
int CMapGenOptions::countHumanPlayers() const
|
|
|
|
{
|
|
|
|
return static_cast<int>(boost::count_if(players, [](const std::pair<PlayerColor, CPlayerSettings> & pair)
|
|
|
|
{
|
|
|
|
return pair.second.getPlayerType() == EPlayerType::HUMAN;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor CMapGenOptions::getNextPlayerColor() const
|
|
|
|
{
|
|
|
|
for(PlayerColor i = PlayerColor(0); i < PlayerColor::PLAYER_LIMIT; i.advance(1))
|
|
|
|
{
|
|
|
|
if(!players.count(i))
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2013-04-22 19:04:17 +03:00
|
|
|
assert(0);
|
|
|
|
return PlayerColor(0);
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
bool CMapGenOptions::checkOptions() const
|
|
|
|
{
|
|
|
|
assert(countHumanPlayers() > 0);
|
|
|
|
if(mapTemplate)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CRandomGenerator gen;
|
|
|
|
return getPossibleTemplate(gen) != nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const CRmgTemplate * CMapGenOptions::getPossibleTemplate(CRandomGenerator & gen) const
|
|
|
|
{
|
|
|
|
// Find potential templates
|
|
|
|
const auto & tpls = getAvailableTemplates();
|
|
|
|
std::list<const CRmgTemplate *> potentialTpls;
|
|
|
|
BOOST_FOREACH(const auto & tplPair, tpls)
|
|
|
|
{
|
|
|
|
const auto & tpl = tplPair.second;
|
|
|
|
CRmgTemplate::CSize tplSize(width, height, hasTwoLevels);
|
|
|
|
if(tplSize >= tpl.getMinSize() && tplSize <= tpl.getMaxSize())
|
|
|
|
{
|
|
|
|
bool isPlayerCountValid = false;
|
|
|
|
if(playerCount != RANDOM_SIZE)
|
|
|
|
{
|
|
|
|
if(tpl.getPlayers().isInRange(playerCount)) isPlayerCountValid = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Human players shouldn't be banned when playing with random player count
|
|
|
|
auto playerNumbers = tpl.getPlayers().getNumbers();
|
|
|
|
if(playerNumbers.lower_bound(countHumanPlayers()) != playerNumbers.end())
|
|
|
|
{
|
|
|
|
isPlayerCountValid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isPlayerCountValid)
|
|
|
|
{
|
|
|
|
bool isCpuPlayerCountValid = false;
|
|
|
|
if(compOnlyPlayerCount != RANDOM_SIZE)
|
|
|
|
{
|
|
|
|
if(tpl.getCpuPlayers().isInRange(compOnlyPlayerCount)) isCpuPlayerCountValid = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isCpuPlayerCountValid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isCpuPlayerCountValid) potentialTpls.push_back(&tpl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select tpl
|
|
|
|
if(potentialTpls.empty())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return *std::next(potentialTpls.begin(), gen.getInteger(0, potentialTpls.size() - 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 20:18:04 +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)
|
|
|
|
{
|
2013-04-22 19:04:17 +03:00
|
|
|
assert(value >= PlayerColor(0) && value < PlayerColor::PLAYER_LIMIT);
|
|
|
|
color = value;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si32 CMapGenOptions::CPlayerSettings::getStartingTown() const
|
|
|
|
{
|
|
|
|
return startingTown;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::CPlayerSettings::setStartingTown(si32 value)
|
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
assert(value >= -1);
|
|
|
|
if(value >= 0)
|
|
|
|
{
|
|
|
|
assert(value < static_cast<int>(VLC->townh->factions.size()));
|
|
|
|
assert(VLC->townh->factions[value]->town != nullptr);
|
|
|
|
}
|
2013-04-22 19:04:17 +03:00
|
|
|
startingTown = value;
|
2013-04-15 20:18:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
EPlayerType::EPlayerType CMapGenOptions::CPlayerSettings::getPlayerType() const
|
|
|
|
{
|
|
|
|
return playerType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenOptions::CPlayerSettings::setPlayerType(EPlayerType::EPlayerType value)
|
|
|
|
{
|
|
|
|
playerType = value;
|
|
|
|
}
|
|
|
|
|
2013-04-22 17:49:28 +03:00
|
|
|
CMapGenerator::CMapGenerator(const CMapGenOptions & mapGenOptions, int randomSeed /*= std::time(nullptr)*/) :
|
2013-04-14 22:24:31 +03:00
|
|
|
mapGenOptions(mapGenOptions), randomSeed(randomSeed)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
gen.seed(randomSeed);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CMapGenerator::~CMapGenerator()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CMap> CMapGenerator::generate()
|
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
mapGenOptions.finalize(gen);
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-01-20 17:43:58 +03:00
|
|
|
map = make_unique<CMap>();
|
2013-04-19 14:43:11 +03:00
|
|
|
editManager = map->getEditManager();
|
2013-04-22 17:49:28 +03:00
|
|
|
editManager->getUndoManager().setUndoRedoLimit(0);
|
2013-01-06 22:30:12 +03:00
|
|
|
addHeaderInfo();
|
|
|
|
|
|
|
|
genTerrain();
|
|
|
|
genTowns();
|
|
|
|
|
|
|
|
return std::move(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CMapGenerator::getMapDescription() const
|
|
|
|
{
|
|
|
|
const std::string waterContentStr[3] = { "none", "normal", "islands" };
|
|
|
|
const std::string monsterStrengthStr[3] = { "weak", "normal", "strong" };
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << boost::str(boost::format(std::string("Map created by the Random Map Generator.\nTemplate was %s, Random seed was %d, size %dx%d") +
|
|
|
|
", levels %s, humans %d, computers %d, water %s, monster %s, second expansion map") % mapGenOptions.getMapTemplate()->getName() %
|
|
|
|
randomSeed % map->width % map->height % (map->twoLevel ? "2" : "1") % static_cast<int>(mapGenOptions.getPlayerCount()) %
|
|
|
|
static_cast<int>(mapGenOptions.getCompOnlyPlayerCount()) % waterContentStr[mapGenOptions.getWaterContent()] %
|
|
|
|
monsterStrengthStr[mapGenOptions.getMonsterStrength()]);
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
BOOST_FOREACH(const auto & pair, mapGenOptions.getPlayersSettings())
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
const auto & pSettings = pair.second;
|
|
|
|
if(pSettings.getPlayerType() == EPlayerType::HUMAN)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()] << " is human";
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-04-14 22:24:31 +03:00
|
|
|
if(pSettings.getStartingTown() != CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()]
|
2013-04-14 22:24:31 +03:00
|
|
|
<< " town choice is " << ETownType::names[pSettings.getStartingTown()];
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::addPlayerInfo()
|
|
|
|
{
|
|
|
|
// Calculate which team numbers exist
|
|
|
|
std::array<std::list<int>, 2> teamNumbers; // 0= cpu/human, 1= cpu only
|
|
|
|
int teamOffset = 0;
|
|
|
|
for(int i = 0; i < 2; ++i)
|
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
int playerCount = i == 0 ? mapGenOptions.getPlayerCount() : mapGenOptions.getCompOnlyPlayerCount();
|
|
|
|
int teamCount = i == 0 ? mapGenOptions.getTeamCount() : mapGenOptions.getCompOnlyTeamCount();
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
if(playerCount == 0)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
int playersPerTeam = playerCount /
|
|
|
|
(teamCount == 0 ? playerCount : teamCount);
|
|
|
|
int teamCountNorm = teamCount;
|
|
|
|
if(teamCountNorm == 0)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
teamCountNorm = playerCount;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
for(int j = 0; j < teamCountNorm; ++j)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
for(int k = 0; k < playersPerTeam; ++k)
|
|
|
|
{
|
|
|
|
teamNumbers[i].push_back(j + teamOffset);
|
|
|
|
}
|
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
for(int j = 0; j < playerCount - teamCountNorm * playersPerTeam; ++j)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
teamNumbers[i].push_back(j + teamOffset);
|
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
teamOffset += teamCountNorm;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Team numbers are assigned randomly to every player
|
2013-04-14 22:24:31 +03:00
|
|
|
BOOST_FOREACH(const auto & pair, mapGenOptions.getPlayersSettings())
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
const auto & pSettings = pair.second;
|
2013-01-06 22:30:12 +03:00
|
|
|
PlayerInfo player;
|
|
|
|
player.canComputerPlay = true;
|
2013-04-14 22:24:31 +03:00
|
|
|
int j = pSettings.getPlayerType() == EPlayerType::COMP_ONLY ? 1 : 0;
|
2013-01-06 22:30:12 +03:00
|
|
|
if(j == 0)
|
|
|
|
{
|
|
|
|
player.canHumanPlay = true;
|
|
|
|
}
|
|
|
|
auto itTeam = std::next(teamNumbers[j].begin(), gen.getInteger(0, teamNumbers[j].size() - 1));
|
2013-03-03 20:06:03 +03:00
|
|
|
player.team = TeamID(*itTeam);
|
2013-01-06 22:30:12 +03:00
|
|
|
teamNumbers[j].erase(itTeam);
|
2013-03-03 20:06:03 +03:00
|
|
|
map->players[pSettings.getColor().getNum()] = player;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
map->howManyTeams = (mapGenOptions.getTeamCount() == 0 ? mapGenOptions.getPlayerCount() : mapGenOptions.getTeamCount())
|
|
|
|
+ (mapGenOptions.getCompOnlyTeamCount() == 0 ? mapGenOptions.getCompOnlyPlayerCount() : mapGenOptions.getCompOnlyTeamCount());
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::genTerrain()
|
|
|
|
{
|
2013-04-22 17:49:28 +03:00
|
|
|
map->initTerrain();
|
2013-04-19 14:43:11 +03:00
|
|
|
editManager->clearTerrain(&gen);
|
2013-05-21 22:08:06 +03:00
|
|
|
editManager->getTerrainSelection().selectRange(MapRect(int3(4, 4, 0), 24, 30));
|
2013-04-29 18:51:39 +03:00
|
|
|
editManager->drawTerrain(ETerrainType::GRASS, &gen);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::genTowns()
|
|
|
|
{
|
|
|
|
//FIXME mock gen
|
2013-05-21 22:08:06 +03:00
|
|
|
const int3 townPos[2] = { int3(11, 7, 0), int3(19,7, 0) };
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
for(size_t i = 0; i < map->players.size(); ++i)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
auto & playerInfo = map->players[i];
|
|
|
|
if(!playerInfo.canAnyonePlay()) break;
|
2013-04-14 21:52:05 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
PlayerColor owner(i);
|
|
|
|
int side = i % 2;
|
2013-01-06 22:30:12 +03:00
|
|
|
CGTownInstance * town = new CGTownInstance();
|
|
|
|
town->ID = Obj::TOWN;
|
2013-05-21 22:08:06 +03:00
|
|
|
int townId = mapGenOptions.getPlayersSettings().find(PlayerColor(i))->second.getStartingTown();
|
|
|
|
if(townId == CMapGenOptions::CPlayerSettings::RANDOM_TOWN) townId = gen.getInteger(0, 8); // Default towns
|
|
|
|
town->subID = townId;
|
2013-01-06 22:30:12 +03:00
|
|
|
town->tempOwner = owner;
|
|
|
|
town->defInfo = VLC->dobjinfo->gobjs[town->ID][town->subID];
|
2013-02-11 02:24:57 +03:00
|
|
|
town->builtBuildings.insert(BuildingID::FORT);
|
2013-02-11 22:11:34 +03:00
|
|
|
town->builtBuildings.insert(BuildingID::DEFAULT);
|
2013-04-29 18:51:39 +03:00
|
|
|
editManager->insertObject(town, int3(townPos[side].x, townPos[side].y + (i / 2) * 5, 0));
|
2013-01-20 17:43:58 +03:00
|
|
|
|
|
|
|
// Update player info
|
2013-04-14 22:24:31 +03:00
|
|
|
playerInfo.allowedFactions.clear();
|
2013-05-21 22:08:06 +03:00
|
|
|
playerInfo.allowedFactions.insert(townId);
|
2013-04-14 22:24:31 +03:00
|
|
|
playerInfo.hasMainTown = true;
|
|
|
|
playerInfo.posOfMainTown = town->pos - int3(2, 0, 0);
|
|
|
|
playerInfo.generateHeroAtMainTown = true;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::addHeaderInfo()
|
|
|
|
{
|
|
|
|
map->version = EMapFormat::SOD;
|
|
|
|
map->width = mapGenOptions.getWidth();
|
|
|
|
map->height = mapGenOptions.getHeight();
|
|
|
|
map->twoLevel = mapGenOptions.getHasTwoLevels();
|
|
|
|
map->name = VLC->generaltexth->allTexts[740];
|
|
|
|
map->description = getMapDescription();
|
|
|
|
map->difficulty = 1;
|
|
|
|
addPlayerInfo();
|
|
|
|
}
|
2013-05-11 20:36:11 +03:00
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplateZone::CTownInfo::CTownInfo() : townCount(0), castleCount(0), townDensity(0), castleDensity(0)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
int CRmgTemplateZone::CTownInfo::getTownCount() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return townCount;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::CTownInfo::setTownCount(int value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(value < 0) throw std::runtime_error("Negative value for town count not allowed.");
|
|
|
|
townCount = value;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
int CRmgTemplateZone::CTownInfo::getCastleCount() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return castleCount;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::CTownInfo::setCastleCount(int value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(value < 0) throw std::runtime_error("Negative value for castle count not allowed.");
|
|
|
|
castleCount = value;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
int CRmgTemplateZone::CTownInfo::getTownDensity() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return townDensity;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::CTownInfo::setTownDensity(int value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(value < 0) throw std::runtime_error("Negative value for town density not allowed.");
|
2013-05-11 20:36:11 +03:00
|
|
|
townDensity = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
int CRmgTemplateZone::CTownInfo::getCastleDensity() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return castleDensity;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::CTownInfo::setCastleDensity(int value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(value < 0) throw std::runtime_error("Negative value for castle density not allowed.");
|
2013-05-11 20:36:11 +03:00
|
|
|
castleDensity = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplateZone::CRmgTemplateZone() : id(0), type(ETemplateZoneType::PLAYER_START), size(1),
|
|
|
|
townsAreSameType(false), matchTerrainToTown(true)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
townTypes = getDefaultTownTypes();
|
|
|
|
terrainTypes = getDefaultTerrainTypes();
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
TRmgTemplateZoneId CRmgTemplateZone::getId() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::setId(TRmgTemplateZoneId value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(value <= 0) throw std::runtime_error("Zone id should be greater than 0.");
|
2013-05-11 20:36:11 +03:00
|
|
|
id = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
ETemplateZoneType::ETemplateZoneType CRmgTemplateZone::getType() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::setType(ETemplateZoneType::ETemplateZoneType value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
type = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
int CRmgTemplateZone::getSize() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return size;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::setSize(int value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(value <= 0) throw std::runtime_error("Zone size needs to be greater than 0.");
|
|
|
|
size = value;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
boost::optional<int> CRmgTemplateZone::getOwner() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::setOwner(boost::optional<int> value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(!(*value >= 0 && *value <= PlayerColor::PLAYER_LIMIT_I)) throw std::runtime_error("Owner has to be in range 0 to max player count.");
|
2013-05-11 20:36:11 +03:00
|
|
|
owner = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const CRmgTemplateZone::CTownInfo & CRmgTemplateZone::getPlayerTowns() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return playerTowns;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::setPlayerTowns(const CTownInfo & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
playerTowns = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const CRmgTemplateZone::CTownInfo & CRmgTemplateZone::getNeutralTowns() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return neutralTowns;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::setNeutralTowns(const CTownInfo & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
neutralTowns = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
bool CRmgTemplateZone::getTownsAreSameType() const
|
|
|
|
{
|
|
|
|
return townsAreSameType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRmgTemplateZone::setTownsAreSameType(bool value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
townsAreSameType = value;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const std::set<TFaction> & CRmgTemplateZone::getTownTypes() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return townTypes;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::setTownTypes(const std::set<TFaction> & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
townTypes = value;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
std::set<TFaction> CRmgTemplateZone::getDefaultTownTypes() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
std::set<TFaction> defaultTowns;
|
|
|
|
auto towns = VLC->townh->getDefaultAllowed();
|
|
|
|
for(int i = 0; i < towns.size(); ++i)
|
|
|
|
{
|
|
|
|
if(towns[i]) defaultTowns.insert(i);
|
|
|
|
}
|
|
|
|
return defaultTowns;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
bool CRmgTemplateZone::getMatchTerrainToTown() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return matchTerrainToTown;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::setMatchTerrainToTown(bool value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
matchTerrainToTown = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const std::set<ETerrainType> & CRmgTemplateZone::getTerrainTypes() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return terrainTypes;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::setTerrainTypes(const std::set<ETerrainType> & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
assert(value.find(ETerrainType::WRONG) == value.end() && value.find(ETerrainType::BORDER) == value.end() &&
|
2013-05-21 22:08:06 +03:00
|
|
|
value.find(ETerrainType::WATER) == value.end() && value.find(ETerrainType::ROCK) == value.end());
|
2013-05-11 20:36:11 +03:00
|
|
|
terrainTypes = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
std::set<ETerrainType> CRmgTemplateZone::getDefaultTerrainTypes() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
std::set<ETerrainType> terTypes;
|
|
|
|
static const ETerrainType::EETerrainType allowedTerTypes[] = { ETerrainType::DIRT, ETerrainType::SAND, ETerrainType::GRASS, ETerrainType::SNOW,
|
|
|
|
ETerrainType::SWAMP, ETerrainType::ROUGH, ETerrainType::SUBTERRANEAN, ETerrainType::LAVA };
|
|
|
|
for(int i = 0; i < ARRAY_COUNT(allowedTerTypes); ++i) terTypes.insert(allowedTerTypes[i]);
|
|
|
|
return terTypes;
|
|
|
|
}
|
2013-05-11 20:36:11 +03:00
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
boost::optional<TRmgTemplateZoneId> CRmgTemplateZone::getTerrainTypeLikeZone() const
|
|
|
|
{
|
|
|
|
return terrainTypeLikeZone;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZone::setTerrainTypeLikeZone(boost::optional<TRmgTemplateZoneId> value)
|
|
|
|
{
|
|
|
|
terrainTypeLikeZone = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<TRmgTemplateZoneId> CRmgTemplateZone::getTownTypeLikeZone() const
|
|
|
|
{
|
|
|
|
return townTypeLikeZone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRmgTemplateZone::setTownTypeLikeZone(boost::optional<TRmgTemplateZoneId> value)
|
|
|
|
{
|
|
|
|
townTypeLikeZone = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
CRmgTemplateZoneConnection::CRmgTemplateZoneConnection() : zoneA(0), zoneB(0), guardStrength(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TRmgTemplateZoneId CRmgTemplateZoneConnection::getZoneA() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return zoneA;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZoneConnection::setZoneA(TRmgTemplateZoneId value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
zoneA = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
TRmgTemplateZoneId CRmgTemplateZoneConnection::getZoneB() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return zoneB;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZoneConnection::setZoneB(TRmgTemplateZoneId value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
zoneB = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
int CRmgTemplateZoneConnection::getGuardStrength() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return guardStrength;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplateZoneConnection::setGuardStrength(int value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(value < 0) throw std::runtime_error("Negative value for guard strenth not allowed.");
|
2013-05-11 20:36:11 +03:00
|
|
|
guardStrength = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplate::CSize::CSize() : width(CMapHeader::MAP_SIZE_MIDDLE), height(CMapHeader::MAP_SIZE_MIDDLE), under(true)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplate::CSize::CSize(int width, int height, bool under) : under(under)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
setWidth(width);
|
|
|
|
setHeight(height);
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
int CRmgTemplate::CSize::getWidth() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::CSize::setWidth(int value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(value <= 0) throw std::runtime_error("Width > 0 failed.");
|
2013-05-11 20:36:11 +03:00
|
|
|
width = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
int CRmgTemplate::CSize::getHeight() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::CSize::setHeight(int value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(value <= 0) throw std::runtime_error("Height > 0 failed.");
|
2013-05-11 20:36:11 +03:00
|
|
|
height = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
bool CRmgTemplate::CSize::getUnder() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return under;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::CSize::setUnder(bool value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
under = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
bool CRmgTemplate::CSize::operator<=(const CSize & value) const
|
|
|
|
{
|
|
|
|
if(width < value.width && height < value.height)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(width == value.width && height == value.height)
|
|
|
|
{
|
|
|
|
return under ? value.under : true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CRmgTemplate::CSize::operator>=(const CSize & value) const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(width > value.width && height > value.height)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(width == value.width && height == value.height)
|
|
|
|
{
|
|
|
|
return under ? true : !value.under;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplate::CRmgTemplate()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & CRmgTemplate::getName() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::setName(const std::string & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
name = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const CRmgTemplate::CSize & CRmgTemplate::getMinSize() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return minSize;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::setMinSize(const CSize & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
minSize = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const CRmgTemplate::CSize & CRmgTemplate::getMaxSize() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return maxSize;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::setMaxSize(const CSize & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
maxSize = value;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getPlayers() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return players;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::setPlayers(const CPlayerCountRange & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
players = value;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getCpuPlayers() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return cpuPlayers;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::setCpuPlayers(const CPlayerCountRange & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
cpuPlayers = value;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const std::map<TRmgTemplateZoneId, CRmgTemplateZone> & CRmgTemplate::getZones() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return zones;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::setZones(const std::map<TRmgTemplateZoneId, CRmgTemplateZone> & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
zones = value;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const std::list<CRmgTemplateZoneConnection> & CRmgTemplate::getConnections() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
return connections;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::setConnections(const std::list<CRmgTemplateZoneConnection> & value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
connections = value;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::validate() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
//TODO add some validation checks, throw on failure
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::CPlayerCountRange::addRange(int lower, int upper)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
range.push_back(std::make_pair(lower, upper));
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CRmgTemplate::CPlayerCountRange::addNumber(int value)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
range.push_back(std::make_pair(value, value));
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
bool CRmgTemplate::CPlayerCountRange::isInRange(int count) const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
BOOST_FOREACH(const auto & pair, range)
|
|
|
|
{
|
|
|
|
if(count >= pair.first && count <= pair.second) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
std::set<int> CRmgTemplate::CPlayerCountRange::getNumbers() const
|
|
|
|
{
|
|
|
|
std::set<int> numbers;
|
|
|
|
BOOST_FOREACH(const auto & pair, range)
|
|
|
|
{
|
|
|
|
for(int i = pair.first; i <= pair.second; ++i) numbers.insert(i);
|
|
|
|
}
|
|
|
|
return numbers;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::map<std::string, CRmgTemplate> & CRmgTemplateLoader::getTemplates() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return templates;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
void CJsonRmgTemplateLoader::loadTemplates()
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
const JsonNode rootNode(ResourceID("config/rmg.json"));
|
|
|
|
BOOST_FOREACH(const auto & templatePair, rootNode.Struct())
|
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplate tpl;
|
|
|
|
try
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
tpl.setName(templatePair.first);
|
|
|
|
const auto & templateNode = templatePair.second;
|
|
|
|
|
|
|
|
// Parse main template data
|
|
|
|
tpl.setMinSize(parseMapTemplateSize(templateNode["minSize"].String()));
|
|
|
|
tpl.setMaxSize(parseMapTemplateSize(templateNode["maxSize"].String()));
|
|
|
|
tpl.setPlayers(parsePlayers(templateNode["players"].String()));
|
|
|
|
tpl.setCpuPlayers(parsePlayers(templateNode["cpu"].String()));
|
|
|
|
|
|
|
|
// Parse zones
|
|
|
|
std::map<TRmgTemplateZoneId, CRmgTemplateZone> zones;
|
|
|
|
BOOST_FOREACH(const auto & zonePair, templateNode["zones"].Struct())
|
|
|
|
{
|
|
|
|
CRmgTemplateZone zone;
|
|
|
|
auto zoneId = boost::lexical_cast<TRmgTemplateZoneId>(zonePair.first);
|
|
|
|
zone.setId(zoneId);
|
|
|
|
const auto & zoneNode = zonePair.second;
|
|
|
|
zone.setType(parseZoneType(zoneNode["type"].String()));
|
|
|
|
zone.setSize(zoneNode["size"].Float());
|
|
|
|
if(!zoneNode["owner"].isNull()) zone.setOwner(zoneNode["owner"].Float());
|
|
|
|
zone.setPlayerTowns(parseTemplateZoneTowns(zoneNode["playerTowns"]));
|
|
|
|
zone.setNeutralTowns(parseTemplateZoneTowns(zoneNode["neutralTowns"]));
|
|
|
|
zone.setTownTypes(parseTownTypes(zoneNode["townTypes"].Vector(), zone.getDefaultTownTypes()));
|
|
|
|
zone.setMatchTerrainToTown(zoneNode["matchTerrainToTown"].Bool());
|
|
|
|
zone.setTerrainTypes(parseTerrainTypes(zoneNode["terrainTypes"].Vector(), zone.getDefaultTerrainTypes()));
|
|
|
|
zone.setTownsAreSameType((zoneNode["townsAreSameType"].Bool()));
|
|
|
|
if(!zoneNode["terrainTypeLikeZone"].isNull()) zone.setTerrainTypeLikeZone(boost::lexical_cast<int>(zoneNode["terrainTypeLikeZone"].String()));
|
|
|
|
if(!zoneNode["townTypeLikeZone"].isNull()) zone.setTownTypeLikeZone(boost::lexical_cast<int>(zoneNode["townTypeLikeZone"].String()));
|
|
|
|
zones[zone.getId()] = zone;
|
|
|
|
}
|
|
|
|
tpl.setZones(zones);
|
2013-05-11 20:36:11 +03:00
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
// Parse connections
|
|
|
|
std::list<CRmgTemplateZoneConnection> connections;
|
|
|
|
BOOST_FOREACH(const auto & connPair, templateNode["connections"].Vector())
|
|
|
|
{
|
|
|
|
CRmgTemplateZoneConnection conn;
|
|
|
|
conn.setZoneA(boost::lexical_cast<TRmgTemplateZoneId>(connPair["a"].String()));
|
|
|
|
conn.setZoneB(boost::lexical_cast<TRmgTemplateZoneId>(connPair["b"].String()));
|
|
|
|
conn.setGuardStrength(connPair["guard"].Float());
|
|
|
|
connections.push_back(conn);
|
|
|
|
}
|
|
|
|
tpl.setConnections(connections);
|
|
|
|
tpl.validate();
|
|
|
|
templates[tpl.getName()] = tpl;
|
|
|
|
}
|
|
|
|
catch(const std::exception & e)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
logGlobal->errorStream() << boost::format("Template %s has errors. Message: %s.") % tpl.getName() % std::string(e.what());
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplate::CSize CJsonRmgTemplateLoader::parseMapTemplateSize(const std::string & text) const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplate::CSize size;
|
2013-05-11 20:36:11 +03:00
|
|
|
if(text.empty()) return size;
|
|
|
|
|
|
|
|
std::vector<std::string> parts;
|
|
|
|
boost::split(parts, text, boost::is_any_of("+"));
|
|
|
|
static const std::map<std::string, int> mapSizeMapping = boost::assign::map_list_of("s", CMapHeader::MAP_SIZE_SMALL)
|
|
|
|
("m", CMapHeader::MAP_SIZE_MIDDLE)("l", CMapHeader::MAP_SIZE_LARGE)("xl", CMapHeader::MAP_SIZE_XLARGE);
|
|
|
|
auto it = mapSizeMapping.find(parts[0]);
|
|
|
|
if(it == mapSizeMapping.end())
|
|
|
|
{
|
|
|
|
// Map size is given as a number representation
|
|
|
|
const auto & numericalRep = parts[0];
|
|
|
|
parts.clear();
|
|
|
|
boost::split(parts, numericalRep, boost::is_any_of("x"));
|
|
|
|
assert(parts.size() == 3);
|
|
|
|
size.setWidth(boost::lexical_cast<int>(parts[0]));
|
|
|
|
size.setHeight(boost::lexical_cast<int>(parts[1]));
|
|
|
|
size.setUnder(boost::lexical_cast<int>(parts[2]) == 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size.setWidth(it->second);
|
|
|
|
size.setHeight(it->second);
|
|
|
|
size.setUnder(parts.size() > 1 ? parts[1] == std::string("u") : false);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
ETemplateZoneType::ETemplateZoneType CJsonRmgTemplateLoader::parseZoneType(const std::string & type) const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
static const std::map<std::string, ETemplateZoneType::ETemplateZoneType> zoneTypeMapping = boost::assign::map_list_of
|
2013-05-21 22:08:06 +03:00
|
|
|
("playerStart", ETemplateZoneType::PLAYER_START)("cpuStart", ETemplateZoneType::CPU_START)
|
2013-05-11 20:36:11 +03:00
|
|
|
("treasure", ETemplateZoneType::TREASURE)("junction", ETemplateZoneType::JUNCTION);
|
|
|
|
auto it = zoneTypeMapping.find(type);
|
2013-05-21 22:08:06 +03:00
|
|
|
if(it == zoneTypeMapping.end()) throw std::runtime_error("Zone type unknown.");
|
2013-05-11 20:36:11 +03:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplateZone::CTownInfo CJsonRmgTemplateLoader::parseTemplateZoneTowns(const JsonNode & node) const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplateZone::CTownInfo towns;
|
|
|
|
towns.setTownCount(node["towns"].Float());
|
|
|
|
towns.setCastleCount(node["castles"].Float());
|
2013-05-11 20:36:11 +03:00
|
|
|
towns.setTownDensity(node["townDensity"].Float());
|
|
|
|
towns.setCastleDensity(node["castleDensity"].Float());
|
|
|
|
return towns;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
std::set<TFaction> CJsonRmgTemplateLoader::parseTownTypes(const JsonVector & townTypesVector, const std::set<TFaction> & defaultTownTypes) const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
std::set<TFaction> townTypes;
|
|
|
|
BOOST_FOREACH(const auto & townTypeNode, townTypesVector)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
auto townTypeStr = townTypeNode.String();
|
|
|
|
if(townTypeStr == "all") return defaultTownTypes;
|
|
|
|
|
|
|
|
bool foundFaction = false;
|
2013-05-11 20:36:11 +03:00
|
|
|
BOOST_FOREACH(auto factionPtr, VLC->townh->factions)
|
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
if(factionPtr->town != nullptr && townTypeStr == factionPtr->name)
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
townTypes.insert(factionPtr->index);
|
|
|
|
foundFaction = true;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
if(!foundFaction) throw std::runtime_error("Given faction is invalid.");
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
2013-05-21 22:08:06 +03:00
|
|
|
return townTypes;
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
std::set<ETerrainType> CJsonRmgTemplateLoader::parseTerrainTypes(const JsonVector & terTypeStrings, const std::set<ETerrainType> & defaultTerrainTypes) const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
std::set<ETerrainType> terTypes;
|
|
|
|
BOOST_FOREACH(const auto & node, terTypeStrings)
|
|
|
|
{
|
|
|
|
const auto & terTypeStr = node.String();
|
2013-05-21 22:08:06 +03:00
|
|
|
if(terTypeStr == "all") return defaultTerrainTypes;
|
|
|
|
auto pos = vstd::find_pos(GameConstants::TERRAIN_NAMES, terTypeStr);
|
|
|
|
if (pos != -1)
|
|
|
|
{
|
|
|
|
terTypes.insert(ETerrainType(pos));
|
|
|
|
}
|
|
|
|
else
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
throw std::runtime_error("Terrain type is invalid.");
|
2013-05-11 20:36:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return terTypes;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplate::CPlayerCountRange CJsonRmgTemplateLoader::parsePlayers(const std::string & players) const
|
|
|
|
{
|
|
|
|
CRmgTemplate::CPlayerCountRange playerRange;
|
|
|
|
if(players.empty())
|
|
|
|
{
|
|
|
|
playerRange.addNumber(0);
|
|
|
|
return playerRange;
|
|
|
|
}
|
|
|
|
std::vector<std::string> commaParts;
|
|
|
|
boost::split(commaParts, players, boost::is_any_of(","));
|
|
|
|
BOOST_FOREACH(const auto & commaPart, commaParts)
|
|
|
|
{
|
|
|
|
std::vector<std::string> rangeParts;
|
|
|
|
boost::split(rangeParts, commaPart, boost::is_any_of("-"));
|
|
|
|
if(rangeParts.size() == 2)
|
|
|
|
{
|
|
|
|
auto lower = boost::lexical_cast<int>(rangeParts[0]);
|
|
|
|
auto upper = boost::lexical_cast<int>(rangeParts[1]);
|
|
|
|
playerRange.addRange(lower, upper);
|
|
|
|
}
|
|
|
|
else if(rangeParts.size() == 1)
|
|
|
|
{
|
|
|
|
auto val = boost::lexical_cast<int>(rangeParts.front());
|
|
|
|
playerRange.addNumber(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return playerRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::mutex CRmgTemplateStorage::smx;
|
2013-05-11 20:36:11 +03:00
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplateStorage & CRmgTemplateStorage::get()
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
TLockGuard _(smx);
|
2013-05-21 22:08:06 +03:00
|
|
|
static CRmgTemplateStorage storage;
|
2013-05-11 20:36:11 +03:00
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
const std::map<std::string, CRmgTemplate> & CRmgTemplateStorage::getTemplates() const
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
return templates;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplateStorage::CRmgTemplateStorage()
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
2013-05-21 22:08:06 +03:00
|
|
|
auto jsonLoader = make_unique<CJsonRmgTemplateLoader>();
|
2013-05-11 20:36:11 +03:00
|
|
|
jsonLoader->loadTemplates();
|
|
|
|
const auto & tpls = jsonLoader->getTemplates();
|
|
|
|
templates.insert(tpls.begin(), tpls.end());
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
CRmgTemplateStorage::~CRmgTemplateStorage()
|
2013-05-11 20:36:11 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|