2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CMapGenerator.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
|
|
|
|
*
|
|
|
|
*/
|
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"
|
2023-05-24 00:14:06 +02:00
|
|
|
#include "../mapping/MapFormat.h"
|
2013-01-06 22:30:12 +03:00
|
|
|
#include "../VCMI_Lib.h"
|
|
|
|
#include "../CGeneralTextHandler.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
|
#include "../mapObjectConstructors/CObjectClassesHandler.h"
|
2013-04-07 13:48:07 +03:00
|
|
|
#include "../mapping/CMapEditManager.h"
|
2013-01-06 22:30:12 +03:00
|
|
|
#include "../CTownHandler.h"
|
2023-07-09 11:16:36 +02:00
|
|
|
#include "../CHeroHandler.h"
|
2023-08-19 23:22:31 +02:00
|
|
|
#include "../constants/StringConstants.h"
|
2014-05-22 17:27:13 +03:00
|
|
|
#include "../filesystem/Filesystem.h"
|
2014-05-24 13:42:06 +03:00
|
|
|
#include "CZonePlacer.h"
|
2022-08-09 07:54:32 +02:00
|
|
|
#include "TileInfo.h"
|
|
|
|
#include "Zone.h"
|
|
|
|
#include "Functions.h"
|
|
|
|
#include "RmgMap.h"
|
2023-05-06 18:54:19 +02:00
|
|
|
#include "threadpool/ThreadPool.h"
|
2023-05-20 11:45:08 +02:00
|
|
|
#include "modificators/ObjectManager.h"
|
|
|
|
#include "modificators/TreasurePlacer.h"
|
|
|
|
#include "modificators/RoadPlacer.h"
|
2014-05-24 19:39:58 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2022-05-28 15:03:50 +02:00
|
|
|
CMapGenerator::CMapGenerator(CMapGenOptions& mapGenOptions, int RandomSeed) :
|
|
|
|
mapGenOptions(mapGenOptions), randomSeed(RandomSeed),
|
2023-05-05 10:30:36 +02:00
|
|
|
allowedPrisons(0), monolithIndex(0)
|
2014-05-22 17:27:13 +03:00
|
|
|
{
|
2022-06-05 08:02:58 +02:00
|
|
|
loadConfig();
|
2022-05-28 15:03:50 +02:00
|
|
|
rand.setSeed(this->randomSeed);
|
|
|
|
mapGenOptions.finalize(rand);
|
2022-08-09 07:54:32 +02:00
|
|
|
map = std::make_unique<RmgMap>(mapGenOptions);
|
2023-04-23 10:08:16 +02:00
|
|
|
placer = std::make_shared<CZonePlacer>(*map);
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CMapGenerator::getRandomSeed() const
|
|
|
|
{
|
|
|
|
return randomSeed;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2022-06-05 08:02:58 +02:00
|
|
|
void CMapGenerator::loadConfig()
|
|
|
|
{
|
2023-09-01 23:26:14 +02:00
|
|
|
JsonNode randomMapJson(JsonPath::builtin("config/randomMap.json"));
|
2022-09-22 18:23:31 +02:00
|
|
|
|
2022-06-05 08:02:58 +02:00
|
|
|
config.shipyardGuard = randomMapJson["waterZone"]["shipyard"]["value"].Integer();
|
|
|
|
for(auto & treasure : randomMapJson["waterZone"]["treasure"].Vector())
|
|
|
|
{
|
|
|
|
config.waterTreasure.emplace_back(treasure["min"].Integer(), treasure["max"].Integer(), treasure["density"].Integer());
|
|
|
|
}
|
|
|
|
config.mineExtraResources = randomMapJson["mines"]["extraResourcesLimit"].Integer();
|
|
|
|
config.minGuardStrength = randomMapJson["minGuardStrength"].Integer();
|
2022-06-20 16:39:50 +02:00
|
|
|
config.defaultRoadType = randomMapJson["defaultRoadType"].String();
|
2022-09-04 08:54:06 +02:00
|
|
|
config.secondaryRoadType = randomMapJson["secondaryRoadType"].String();
|
2022-06-05 08:02:58 +02:00
|
|
|
config.treasureValueLimit = randomMapJson["treasureValueLimit"].Integer();
|
|
|
|
for(auto & i : randomMapJson["prisons"]["experience"].Vector())
|
|
|
|
config.prisonExperience.push_back(i.Integer());
|
|
|
|
for(auto & i : randomMapJson["prisons"]["value"].Vector())
|
|
|
|
config.prisonValues.push_back(i.Integer());
|
|
|
|
for(auto & i : randomMapJson["scrolls"]["value"].Vector())
|
|
|
|
config.scrollValues.push_back(i.Integer());
|
|
|
|
for(auto & i : randomMapJson["pandoras"]["creaturesValue"].Vector())
|
|
|
|
config.pandoraCreatureValues.push_back(i.Integer());
|
|
|
|
for(auto & i : randomMapJson["quests"]["value"].Vector())
|
|
|
|
config.questValues.push_back(i.Integer());
|
|
|
|
for(auto & i : randomMapJson["quests"]["rewardValue"].Vector())
|
|
|
|
config.questRewardValues.push_back(i.Integer());
|
|
|
|
config.pandoraMultiplierGold = randomMapJson["pandoras"]["valueMultiplierGold"].Integer();
|
|
|
|
config.pandoraMultiplierExperience = randomMapJson["pandoras"]["valueMultiplierExperience"].Integer();
|
|
|
|
config.pandoraMultiplierSpells = randomMapJson["pandoras"]["valueMultiplierSpells"].Integer();
|
|
|
|
config.pandoraSpellSchool = randomMapJson["pandoras"]["valueSpellSchool"].Integer();
|
|
|
|
config.pandoraSpell60 = randomMapJson["pandoras"]["valueSpell60"].Integer();
|
2023-05-24 18:02:11 +02:00
|
|
|
config.singleThread = randomMapJson["singleThread"].Bool();
|
2022-06-05 08:02:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const CMapGenerator::Config & CMapGenerator::getConfig() const
|
|
|
|
{
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:05:02 +02:00
|
|
|
//must be instantiated in .cpp file for access to complete types of all member fields
|
|
|
|
CMapGenerator::~CMapGenerator() = default;
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
const CMapGenOptions& CMapGenerator::getMapGenOptions() const
|
2014-05-30 17:50:06 +03:00
|
|
|
{
|
2022-08-09 07:54:32 +02:00
|
|
|
return mapGenOptions;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2014-07-25 11:44:17 +03:00
|
|
|
void CMapGenerator::initPrisonsRemaining()
|
|
|
|
{
|
2023-05-05 10:30:36 +02:00
|
|
|
allowedPrisons = 0;
|
2023-05-19 20:30:15 +02:00
|
|
|
for (auto isAllowed : map->getMap(this).allowedHeroes)
|
2014-07-25 11:44:17 +03:00
|
|
|
{
|
|
|
|
if (isAllowed)
|
2023-05-05 10:30:36 +02:00
|
|
|
allowedPrisons++;
|
2014-07-25 11:44:17 +03:00
|
|
|
}
|
2023-05-05 10:30:36 +02:00
|
|
|
allowedPrisons = std::max<int> (0, allowedPrisons - 16 * mapGenOptions.getPlayerCount()); //so at least 16 heroes will be available for every player
|
2014-07-25 11:44:17 +03:00
|
|
|
}
|
|
|
|
|
2015-02-28 22:14:45 +02:00
|
|
|
void CMapGenerator::initQuestArtsRemaining()
|
|
|
|
{
|
2023-04-23 10:08:16 +02:00
|
|
|
//TODO: Move to QuestArtifactPlacer?
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
for (auto art : VLC->arth->objects)
|
2015-02-28 22:14:45 +02:00
|
|
|
{
|
2023-04-29 11:46:03 +02:00
|
|
|
//Don't use parts of combined artifacts
|
2023-07-03 18:15:40 +02:00
|
|
|
if (art->aClass == CArtifact::ART_TREASURE && VLC->arth->legalArtifact(art->getId()) && art->getPartOf().empty())
|
2023-01-02 15:58:56 +02:00
|
|
|
questArtifacts.push_back(art->getId());
|
2015-02-28 22:14:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 15:03:50 +02:00
|
|
|
std::unique_ptr<CMap> CMapGenerator::generate()
|
|
|
|
{
|
2022-09-17 13:04:01 +02:00
|
|
|
Load::Progress::reset();
|
|
|
|
Load::Progress::setupStepsTill(5, 30);
|
2014-05-23 18:12:31 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
addHeaderInfo();
|
2023-05-20 11:46:32 +02:00
|
|
|
map->initTiles(*this, rand);
|
2022-09-17 13:04:01 +02:00
|
|
|
Load::Progress::step();
|
2014-07-25 11:44:17 +03:00
|
|
|
initPrisonsRemaining();
|
2015-02-28 22:14:45 +02:00
|
|
|
initQuestArtsRemaining();
|
2014-05-23 18:12:31 +03:00
|
|
|
genZones();
|
2022-09-17 13:04:01 +02:00
|
|
|
Load::Progress::step();
|
2023-05-19 20:30:15 +02:00
|
|
|
map->getMap(this).calculateGuardingGreaturePositions(); //clear map so that all tiles are unguarded
|
2022-08-09 07:54:32 +02:00
|
|
|
map->addModificators();
|
2022-09-17 13:04:01 +02:00
|
|
|
Load::Progress::step(3);
|
2014-05-23 18:12:31 +03:00
|
|
|
fillZones();
|
2014-07-27 14:59:53 +03:00
|
|
|
//updated guarded tiles will be calculated in CGameState::initMapObjects()
|
2022-08-09 07:54:32 +02:00
|
|
|
map->getZones().clear();
|
2014-05-23 18:12:31 +03:00
|
|
|
}
|
|
|
|
catch (rmgException &e)
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Random map generation received exception: %s", e.what());
|
2014-05-23 18:12:31 +03:00
|
|
|
}
|
2022-09-17 13:04:01 +02:00
|
|
|
Load::Progress::finish();
|
2022-08-09 07:54:32 +02:00
|
|
|
return std::move(map->mapInstance);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CMapGenerator::getMapDescription() const
|
|
|
|
{
|
2014-09-18 15:29:57 +03:00
|
|
|
assert(map);
|
|
|
|
|
2013-01-06 22:30:12 +03:00
|
|
|
const std::string waterContentStr[3] = { "none", "normal", "islands" };
|
|
|
|
const std::string monsterStrengthStr[3] = { "weak", "normal", "strong" };
|
|
|
|
|
2022-05-28 15:03:50 +02:00
|
|
|
int monsterStrengthIndex = mapGenOptions.getMonsterStrength() - EMonsterStrength::GLOBAL_WEAK; //does not start from 0
|
|
|
|
const auto * mapTemplate = mapGenOptions.getMapTemplate();
|
2021-02-12 23:24:14 +02:00
|
|
|
|
|
|
|
if(!mapTemplate)
|
|
|
|
throw rmgException("Map template for Random Map Generator is not found. Could not start the game.");
|
2014-09-18 15:29:57 +03:00
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
std::stringstream ss;
|
2023-06-03 08:53:00 +02:00
|
|
|
ss << boost::str(boost::format(std::string("Map created by the Random Map Generator.\nTemplate was %s, size %dx%d") +
|
2022-09-18 16:39:10 +02:00
|
|
|
", levels %d, players %d, computers %d, water %s, monster %s, VCMI map") % mapTemplate->getName() %
|
2023-06-03 08:53:00 +02:00
|
|
|
map->width() % map->height() % static_cast<int>(map->levels()) % static_cast<int>(mapGenOptions.getPlayerCount()) %
|
2022-05-28 15:03:50 +02:00
|
|
|
static_cast<int>(mapGenOptions.getCompOnlyPlayerCount()) % waterContentStr[mapGenOptions.getWaterContent()] %
|
2014-09-18 15:29:57 +03:00
|
|
|
monsterStrengthStr[monsterStrengthIndex]);
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2022-05-28 15:03:50 +02:00
|
|
|
for(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
|
|
|
}
|
2023-08-18 12:38:19 +02:00
|
|
|
if(pSettings.getStartingTown() != FactionID::RANDOM)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()]
|
2023-01-04 15:17:50 +02:00
|
|
|
<< " town choice is " << (*VLC->townh)[pSettings.getStartingTown()]->getNameTranslated();
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::addPlayerInfo()
|
|
|
|
{
|
|
|
|
// Calculate which team numbers exist
|
2015-06-01 21:57:43 +02:00
|
|
|
|
2023-05-20 13:29:55 +02:00
|
|
|
enum ETeams {CPHUMAN = 0, CPUONLY = 1, AFTER_LAST = 2}; // Used as a kind of a local named array index, so left as enum, not enum class
|
2015-06-01 21:57:43 +02:00
|
|
|
std::array<std::list<int>, 2> teamNumbers;
|
2022-12-17 06:19:16 +02:00
|
|
|
std::set<int> teamsTotal;
|
2015-06-01 21:57:43 +02:00
|
|
|
|
2013-01-06 22:30:12 +03:00
|
|
|
int teamOffset = 0;
|
2015-06-01 21:57:43 +02:00
|
|
|
int playerCount = 0;
|
|
|
|
int teamCount = 0;
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2015-06-01 21:57:43 +02:00
|
|
|
for (int i = CPHUMAN; i < AFTER_LAST; ++i)
|
|
|
|
{
|
|
|
|
if (i == CPHUMAN)
|
|
|
|
{
|
2022-05-28 15:03:50 +02:00
|
|
|
playerCount = mapGenOptions.getPlayerCount();
|
|
|
|
teamCount = mapGenOptions.getTeamCount();
|
2015-06-01 21:57:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-28 15:03:50 +02:00
|
|
|
playerCount = mapGenOptions.getCompOnlyPlayerCount();
|
|
|
|
teamCount = mapGenOptions.getCompOnlyTeamCount();
|
2015-06-01 21:57:43 +02:00
|
|
|
}
|
2015-11-14 15:50:29 +02:00
|
|
|
|
2013-05-21 22:08:06 +03:00
|
|
|
if(playerCount == 0)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-01 21:57:43 +02:00
|
|
|
int playersPerTeam = playerCount / (teamCount == 0 ? playerCount : teamCount);
|
2013-05-21 22:08:06 +03:00
|
|
|
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
|
2015-06-01 21:57:43 +02:00
|
|
|
//TODO: allow customize teams in rmg template
|
2022-05-28 15:03:50 +02:00
|
|
|
for(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;
|
2015-06-01 21:57:43 +02:00
|
|
|
int j = (pSettings.getPlayerType() == EPlayerType::COMP_ONLY) ? CPUONLY : CPHUMAN;
|
|
|
|
if (j == CPHUMAN)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
player.canHumanPlay = true;
|
|
|
|
}
|
2014-05-29 18:34:46 +03:00
|
|
|
|
2022-12-17 01:52:40 +02:00
|
|
|
if(pSettings.getTeam() != TeamID::NO_TEAM)
|
2015-06-01 21:57:43 +02:00
|
|
|
{
|
2022-12-17 01:52:40 +02:00
|
|
|
player.team = pSettings.getTeam();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (teamNumbers[j].empty())
|
|
|
|
{
|
|
|
|
logGlobal->error("Not enough places in team for %s player", ((j == CPUONLY) ? "CPU" : "CPU or human"));
|
|
|
|
assert (teamNumbers[j].size());
|
|
|
|
}
|
|
|
|
auto itTeam = RandomGeneratorUtil::nextItem(teamNumbers[j], rand);
|
|
|
|
player.team = TeamID(*itTeam);
|
|
|
|
teamNumbers[j].erase(itTeam);
|
2015-06-01 21:57:43 +02:00
|
|
|
}
|
2022-12-17 06:19:16 +02:00
|
|
|
teamsTotal.insert(player.team.getNum());
|
2023-05-19 20:30:15 +02:00
|
|
|
map->getMap(this).players[pSettings.getColor().getNum()] = player;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2023-05-19 20:30:15 +02:00
|
|
|
map->getMap(this).howManyTeams = teamsTotal.size();
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2014-05-22 17:27:13 +03:00
|
|
|
void CMapGenerator::genZones()
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2023-04-23 10:08:16 +02:00
|
|
|
placer->placeZones(&rand);
|
|
|
|
placer->assignZones(&rand);
|
2014-05-24 13:42:06 +03:00
|
|
|
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->info("Zones generated successfully");
|
2014-05-22 17:27:13 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 10:08:16 +02:00
|
|
|
void CMapGenerator::addWaterTreasuresInfo()
|
2022-05-31 11:25:39 +02:00
|
|
|
{
|
2023-03-27 09:09:58 +02:00
|
|
|
if (!getZoneWater())
|
2022-08-09 07:54:32 +02:00
|
|
|
return;
|
2023-03-27 09:09:58 +02:00
|
|
|
|
2022-05-31 11:25:39 +02:00
|
|
|
//add treasures on water
|
2023-03-27 09:09:58 +02:00
|
|
|
for (const auto& treasureInfo : getConfig().waterTreasure)
|
2022-06-05 08:02:58 +02:00
|
|
|
{
|
2022-08-09 07:54:32 +02:00
|
|
|
getZoneWater()->addTreasureInfo(treasureInfo);
|
2022-06-05 08:02:58 +02:00
|
|
|
}
|
2022-05-31 11:25:39 +02:00
|
|
|
}
|
|
|
|
|
2014-05-22 17:27:13 +03:00
|
|
|
void CMapGenerator::fillZones()
|
2015-11-14 15:50:29 +02:00
|
|
|
{
|
2023-04-23 10:08:16 +02:00
|
|
|
addWaterTreasuresInfo();
|
2016-08-15 21:37:38 +02:00
|
|
|
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->info("Started filling zones");
|
2014-05-31 11:56:14 +03:00
|
|
|
|
2023-05-06 18:54:19 +02:00
|
|
|
size_t numZones = map->getZones().size();
|
|
|
|
|
2016-12-20 16:23:47 +02:00
|
|
|
//we need info about all town types to evaluate dwellings and pandoras with creatures properly
|
|
|
|
//place main town in the middle
|
2023-05-24 18:02:11 +02:00
|
|
|
|
2023-05-06 18:54:19 +02:00
|
|
|
Load::Progress::setupStepsTill(numZones, 50);
|
2023-03-27 09:09:58 +02:00
|
|
|
for (const auto& it : map->getZones())
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2017-11-03 22:03:51 +02:00
|
|
|
it.second->initFreeTiles();
|
2022-08-20 12:17:27 +02:00
|
|
|
it.second->initModificators();
|
2022-09-17 13:04:01 +02:00
|
|
|
Progress::Progress::step();
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
2022-05-31 11:25:39 +02:00
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
std::vector<std::shared_ptr<Zone>> treasureZones;
|
2023-05-06 18:54:19 +02:00
|
|
|
|
2023-05-07 07:48:12 +02:00
|
|
|
TModificators allJobs;
|
2023-05-24 18:02:11 +02:00
|
|
|
for (auto& it : map->getZones())
|
2014-07-23 20:17:07 +03:00
|
|
|
{
|
2023-05-07 07:48:12 +02:00
|
|
|
allJobs.splice(allJobs.end(), it.second->getModificators());
|
|
|
|
}
|
|
|
|
|
|
|
|
Load::Progress::setupStepsTill(allJobs.size(), 240);
|
|
|
|
|
2023-05-24 18:02:11 +02:00
|
|
|
if (config.singleThread) //No thread pool, just queue with deterministic order
|
2023-05-07 07:48:12 +02:00
|
|
|
{
|
2023-05-24 18:02:11 +02:00
|
|
|
while (!allJobs.empty())
|
2023-05-06 18:54:19 +02:00
|
|
|
{
|
2023-05-24 18:02:11 +02:00
|
|
|
for (auto it = allJobs.begin(); it != allJobs.end();)
|
2023-05-07 07:48:12 +02:00
|
|
|
{
|
2023-05-24 18:02:11 +02:00
|
|
|
if ((*it)->isReady())
|
|
|
|
{
|
|
|
|
auto jobCopy = *it;
|
|
|
|
jobCopy->run();
|
|
|
|
Progress::Progress::step(); //Update progress bar
|
|
|
|
allJobs.erase(it);
|
|
|
|
break; //Restart from the first job
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
}
|
2023-05-07 07:48:12 +02:00
|
|
|
}
|
2023-05-24 18:02:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ThreadPool pool;
|
|
|
|
std::vector<boost::future<void>> futures;
|
|
|
|
//At most one Modificator can run for every zone
|
|
|
|
pool.init(std::min<int>(boost::thread::hardware_concurrency(), numZones));
|
|
|
|
|
|
|
|
while (!allJobs.empty())
|
|
|
|
{
|
|
|
|
for (auto it = allJobs.begin(); it != allJobs.end();)
|
2023-05-07 07:48:12 +02:00
|
|
|
{
|
2023-05-24 18:02:11 +02:00
|
|
|
if ((*it)->isFinished())
|
|
|
|
{
|
|
|
|
it = allJobs.erase(it);
|
|
|
|
Progress::Progress::step();
|
|
|
|
}
|
|
|
|
else if ((*it)->isReady())
|
|
|
|
{
|
|
|
|
auto jobCopy = *it;
|
|
|
|
futures.emplace_back(pool.async([this, jobCopy]() -> void
|
|
|
|
{
|
|
|
|
jobCopy->run();
|
|
|
|
Progress::Progress::step(); //Update progress bar
|
|
|
|
}
|
|
|
|
));
|
|
|
|
it = allJobs.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
}
|
2023-05-07 07:48:12 +02:00
|
|
|
}
|
2023-05-06 18:54:19 +02:00
|
|
|
}
|
2023-03-27 09:09:58 +02:00
|
|
|
|
2023-05-24 18:02:11 +02:00
|
|
|
//Wait for all the tasks
|
|
|
|
for (auto& fut : futures)
|
|
|
|
{
|
|
|
|
fut.get();
|
|
|
|
}
|
2023-05-06 18:54:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& it : map->getZones())
|
|
|
|
{
|
2014-09-21 22:51:51 +03:00
|
|
|
if (it.second->getType() == ETemplateZoneType::TREASURE)
|
|
|
|
treasureZones.push_back(it.second);
|
|
|
|
}
|
2015-03-28 19:56:28 +02:00
|
|
|
|
2014-09-21 22:51:51 +03:00
|
|
|
//find place for Grail
|
2023-03-27 09:09:58 +02:00
|
|
|
if (treasureZones.empty())
|
2014-09-21 22:51:51 +03:00
|
|
|
{
|
2023-03-27 09:09:58 +02:00
|
|
|
for (const auto& it : map->getZones())
|
|
|
|
if (it.second->getType() != ETemplateZoneType::WATER)
|
2022-08-09 07:54:32 +02:00
|
|
|
treasureZones.push_back(it.second);
|
2014-09-21 22:51:51 +03:00
|
|
|
}
|
|
|
|
auto grailZone = *RandomGeneratorUtil::nextItem(treasureZones, rand);
|
|
|
|
|
2023-05-19 20:30:15 +02:00
|
|
|
map->getMap(this).grailPos = *RandomGeneratorUtil::nextItem(grailZone->freePaths().getTiles(), rand);
|
2014-09-21 22:51:51 +03:00
|
|
|
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->info("Zones filled successfully");
|
2022-09-17 13:04:01 +02:00
|
|
|
|
|
|
|
Load::Progress::set(250);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::addHeaderInfo()
|
|
|
|
{
|
2023-05-19 20:30:15 +02:00
|
|
|
auto& m = map->getMap(this);
|
|
|
|
m.version = EMapFormat::VCMI;
|
|
|
|
m.width = mapGenOptions.getWidth();
|
|
|
|
m.height = mapGenOptions.getHeight();
|
|
|
|
m.twoLevel = mapGenOptions.getHasTwoLevels();
|
2023-09-27 22:53:13 +02:00
|
|
|
m.name.appendLocalString(EMetaText::GENERAL_TXT, 740);
|
|
|
|
m.description.appendRawString(getMapDescription());
|
2023-05-19 20:30:15 +02:00
|
|
|
m.difficulty = 1;
|
2013-01-06 22:30:12 +03:00
|
|
|
addPlayerInfo();
|
2023-07-09 11:16:36 +02:00
|
|
|
m.waterMap = (mapGenOptions.getWaterContent() != EWaterContent::EWaterContent::NONE);
|
2023-07-17 17:21:28 +02:00
|
|
|
m.banWaterContent();
|
2014-05-24 13:42:06 +03:00
|
|
|
}
|
|
|
|
|
2014-06-01 13:02:43 +03:00
|
|
|
int CMapGenerator::getNextMonlithIndex()
|
|
|
|
{
|
2023-03-16 07:58:25 +02:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (monolithIndex >= VLC->objtypeh->knownSubObjects(Obj::MONOLITH_TWO_WAY).size())
|
2023-08-20 22:45:41 +02:00
|
|
|
throw rmgException(boost::str(boost::format("There is no Monolith Two Way with index %d available!") % monolithIndex));
|
2023-03-16 07:58:25 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//Skip modded Monoliths which can't beplaced on every terrain
|
|
|
|
auto templates = VLC->objtypeh->getHandlerFor(Obj::MONOLITH_TWO_WAY, monolithIndex)->getTemplates();
|
|
|
|
if (templates.empty() || !templates[0]->canBePlacedAtAnyTerrain())
|
|
|
|
{
|
|
|
|
monolithIndex++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return monolithIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-01 22:53:00 +03:00
|
|
|
}
|
2014-07-07 17:20:48 +03:00
|
|
|
|
2014-07-25 11:44:17 +03:00
|
|
|
int CMapGenerator::getPrisonsRemaning() const
|
|
|
|
{
|
2023-05-05 10:30:36 +02:00
|
|
|
return allowedPrisons;
|
2014-07-25 11:44:17 +03:00
|
|
|
}
|
2022-08-09 07:54:32 +02:00
|
|
|
|
2023-04-23 10:08:16 +02:00
|
|
|
std::shared_ptr<CZonePlacer> CMapGenerator::getZonePlacer() const
|
|
|
|
{
|
|
|
|
return placer;
|
|
|
|
}
|
|
|
|
|
2023-05-05 10:30:36 +02:00
|
|
|
const std::vector<ArtifactID> & CMapGenerator::getAllPossibleQuestArtifacts() const
|
2014-07-25 11:44:17 +03:00
|
|
|
{
|
2023-05-05 10:30:36 +02:00
|
|
|
return questArtifacts;
|
2014-07-25 11:44:17 +03:00
|
|
|
}
|
|
|
|
|
2023-05-06 18:54:19 +02:00
|
|
|
const std::vector<HeroTypeID> CMapGenerator::getAllPossibleHeroes() const
|
2015-02-28 22:14:45 +02:00
|
|
|
{
|
2023-07-09 11:16:36 +02:00
|
|
|
auto isWaterMap = map->getMap(this).isWaterMap();
|
2023-05-05 10:30:36 +02:00
|
|
|
//Skip heroes that were banned, including the ones placed in prisons
|
|
|
|
std::vector<HeroTypeID> ret;
|
2023-05-19 20:30:15 +02:00
|
|
|
for (int j = 0; j < map->getMap(this).allowedHeroes.size(); j++)
|
2023-05-05 10:30:36 +02:00
|
|
|
{
|
2023-05-19 20:30:15 +02:00
|
|
|
if (map->getMap(this).allowedHeroes[j])
|
2023-07-09 11:16:36 +02:00
|
|
|
{
|
|
|
|
auto * h = dynamic_cast<const CHero*>(VLC->heroTypes()->getByIndex(j));
|
|
|
|
if ((h->onlyOnWaterMap && !isWaterMap) || (h->onlyOnMapWithoutWater && isWaterMap))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.push_back(HeroTypeID(j));
|
|
|
|
}
|
|
|
|
}
|
2023-05-05 10:30:36 +02:00
|
|
|
}
|
|
|
|
return ret;
|
2015-02-28 22:14:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-11 18:05:02 +02:00
|
|
|
void CMapGenerator::banQuestArt(const ArtifactID & id)
|
2022-05-31 11:25:39 +02:00
|
|
|
{
|
2023-05-06 18:54:19 +02:00
|
|
|
//TODO: Protect with mutex
|
2023-05-19 20:30:15 +02:00
|
|
|
map->getMap(this).allowedArtifact[id] = false;
|
2022-05-31 11:25:39 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 10:30:36 +02:00
|
|
|
void CMapGenerator::banHero(const HeroTypeID & id)
|
|
|
|
{
|
2023-05-06 18:54:19 +02:00
|
|
|
//TODO: Protect with mutex
|
2023-07-17 17:21:28 +02:00
|
|
|
map->getMap(this).banHero(id);
|
2023-05-05 10:30:36 +02:00
|
|
|
}
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
Zone * CMapGenerator::getZoneWater() const
|
2022-05-31 11:25:39 +02:00
|
|
|
{
|
2022-08-09 07:54:32 +02:00
|
|
|
for(auto & z : map->getZones())
|
|
|
|
if(z.second->getType() == ETemplateZoneType::WATER)
|
|
|
|
return z.second.get();
|
|
|
|
return nullptr;
|
2022-05-31 11:25:39 +02:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|