| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 | 
							- /*
 
-  * 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
 
-  *
 
-  */
 
- #include "StdInc.h"
 
- #include "CMapGenerator.h"
 
- #include "../mapping/CMap.h"
 
- #include "../mapping/MapFormat.h"
 
- #include "../VCMI_Lib.h"
 
- #include "../CGeneralTextHandler.h"
 
- #include "../mapObjectConstructors/AObjectTypeHandler.h"
 
- #include "../mapObjectConstructors/CObjectClassesHandler.h"
 
- #include "../mapping/CMapEditManager.h"
 
- #include "../CTownHandler.h"
 
- #include "../CHeroHandler.h"
 
- #include "../StringConstants.h"
 
- #include "../filesystem/Filesystem.h"
 
- #include "CZonePlacer.h"
 
- #include "TileInfo.h"
 
- #include "Zone.h"
 
- #include "Functions.h"
 
- #include "RmgMap.h"
 
- #include "threadpool/ThreadPool.h"
 
- #include "modificators/ObjectManager.h"
 
- #include "modificators/TreasurePlacer.h"
 
- #include "modificators/RoadPlacer.h"
 
- VCMI_LIB_NAMESPACE_BEGIN
 
- CMapGenerator::CMapGenerator(CMapGenOptions& mapGenOptions, int RandomSeed) :
 
- 	mapGenOptions(mapGenOptions), randomSeed(RandomSeed),
 
- 	allowedPrisons(0), monolithIndex(0)
 
- {
 
- 	loadConfig();
 
- 	rand.setSeed(this->randomSeed);
 
- 	mapGenOptions.finalize(rand);
 
- 	map = std::make_unique<RmgMap>(mapGenOptions);
 
- 	placer = std::make_shared<CZonePlacer>(*map);
 
- }
 
- int CMapGenerator::getRandomSeed() const
 
- {
 
- 	return randomSeed;
 
- }
 
- void CMapGenerator::loadConfig()
 
- {
 
- 	static const ResourceID path("config/randomMap.json");
 
- 	JsonNode randomMapJson(path);
 
- 	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();
 
- 	config.defaultRoadType = randomMapJson["defaultRoadType"].String();
 
- 	config.secondaryRoadType = randomMapJson["secondaryRoadType"].String();
 
- 	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();
 
- 	config.singleThread = randomMapJson["singleThread"].Bool();
 
- }
 
- const CMapGenerator::Config & CMapGenerator::getConfig() const
 
- {
 
- 	return config;
 
- }
 
- //must be instantiated in .cpp file for access to complete types of all member fields
 
- CMapGenerator::~CMapGenerator() = default;
 
- const CMapGenOptions& CMapGenerator::getMapGenOptions() const
 
- {
 
- 	return mapGenOptions;
 
- }
 
- void CMapGenerator::initPrisonsRemaining()
 
- {
 
- 	allowedPrisons = 0;
 
- 	for (auto isAllowed : map->getMap(this).allowedHeroes)
 
- 	{
 
- 		if (isAllowed)
 
- 			allowedPrisons++;
 
- 	}
 
- 	allowedPrisons = std::max<int> (0, allowedPrisons - 16 * mapGenOptions.getPlayerCount()); //so at least 16 heroes will be available for every player
 
- }
 
- void CMapGenerator::initQuestArtsRemaining()
 
- {
 
- 	//TODO: Move to QuestArtifactPlacer?
 
- 	for (auto art : VLC->arth->objects)
 
- 	{
 
- 		//Don't use parts of combined artifacts
 
- 		if (art->aClass == CArtifact::ART_TREASURE && VLC->arth->legalArtifact(art->getId()) && art->getPartOf().empty())
 
- 			questArtifacts.push_back(art->getId());
 
- 	}
 
- }
 
- std::unique_ptr<CMap> CMapGenerator::generate()
 
- {
 
- 	Load::Progress::reset();
 
- 	Load::Progress::setupStepsTill(5, 30);
 
- 	try
 
- 	{
 
- 		addHeaderInfo();
 
- 		map->initTiles(*this, rand);
 
- 		Load::Progress::step();
 
- 		initPrisonsRemaining();
 
- 		initQuestArtsRemaining();
 
- 		genZones();
 
- 		Load::Progress::step();
 
- 		map->getMap(this).calculateGuardingGreaturePositions(); //clear map so that all tiles are unguarded
 
- 		map->addModificators();
 
- 		Load::Progress::step(3);
 
- 		fillZones();
 
- 		//updated guarded tiles will be calculated in CGameState::initMapObjects()
 
- 		map->getZones().clear();
 
- 	}
 
- 	catch (rmgException &e)
 
- 	{
 
- 		logGlobal->error("Random map generation received exception: %s", e.what());
 
- 	}
 
- 	Load::Progress::finish();
 
- 	return std::move(map->mapInstance);
 
- }
 
- std::string CMapGenerator::getMapDescription() const
 
- {
 
- 	assert(map);
 
- 	const std::string waterContentStr[3] = { "none", "normal", "islands" };
 
- 	const std::string monsterStrengthStr[3] = { "weak", "normal", "strong" };
 
- 	int monsterStrengthIndex = mapGenOptions.getMonsterStrength() - EMonsterStrength::GLOBAL_WEAK; //does not start from 0
 
- 	const auto * mapTemplate = mapGenOptions.getMapTemplate();
 
- 	if(!mapTemplate)
 
- 		throw rmgException("Map template for Random Map Generator is not found. Could not start the game.");
 
-     std::stringstream ss;
 
-     ss << boost::str(boost::format(std::string("Map created by the Random Map Generator.\nTemplate was %s, size %dx%d") +
 
-         ", levels %d, players %d, computers %d, water %s, monster %s, VCMI map") % mapTemplate->getName() %
 
- 		map->width() % map->height() % static_cast<int>(map->levels()) % static_cast<int>(mapGenOptions.getPlayerCount()) %
 
- 		static_cast<int>(mapGenOptions.getCompOnlyPlayerCount()) % waterContentStr[mapGenOptions.getWaterContent()] %
 
- 		monsterStrengthStr[monsterStrengthIndex]);
 
- 	for(const auto & pair : mapGenOptions.getPlayersSettings())
 
- 	{
 
- 		const auto & pSettings = pair.second;
 
- 		if(pSettings.getPlayerType() == EPlayerType::HUMAN)
 
- 		{
 
- 			ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()] << " is human";
 
- 		}
 
- 		if(pSettings.getStartingTown() != CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
 
- 		{
 
- 			ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()]
 
- 			   << " town choice is " << (*VLC->townh)[pSettings.getStartingTown()]->getNameTranslated();
 
- 		}
 
- 	}
 
- 	return ss.str();
 
- }
 
- void CMapGenerator::addPlayerInfo()
 
- {
 
- 	// Calculate which team numbers exist
 
- 	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
 
- 	std::array<std::list<int>, 2> teamNumbers;
 
- 	std::set<int> teamsTotal;
 
- 	int teamOffset = 0;
 
- 	int playerCount = 0;
 
- 	int teamCount = 0;
 
- 	for (int i = CPHUMAN; i < AFTER_LAST; ++i)
 
- 	{
 
- 		if (i == CPHUMAN)
 
- 		{
 
- 			playerCount = mapGenOptions.getPlayerCount();
 
- 			teamCount = mapGenOptions.getTeamCount();
 
- 		}
 
- 		else
 
- 		{
 
- 			playerCount = mapGenOptions.getCompOnlyPlayerCount();
 
- 			teamCount = mapGenOptions.getCompOnlyTeamCount();
 
- 		}
 
- 		if(playerCount == 0)
 
- 		{
 
- 			continue;
 
- 		}
 
- 		int playersPerTeam = playerCount / (teamCount == 0 ? playerCount : teamCount);
 
- 		int teamCountNorm = teamCount;
 
- 		if(teamCountNorm == 0)
 
- 		{
 
- 			teamCountNorm = playerCount;
 
- 		}
 
- 		for(int j = 0; j < teamCountNorm; ++j)
 
- 		{
 
- 			for(int k = 0; k < playersPerTeam; ++k)
 
- 			{
 
- 				teamNumbers[i].push_back(j + teamOffset);
 
- 			}
 
- 		}
 
- 		for(int j = 0; j < playerCount - teamCountNorm * playersPerTeam; ++j)
 
- 		{
 
- 			teamNumbers[i].push_back(j + teamOffset);
 
- 		}
 
- 		teamOffset += teamCountNorm;
 
- 	}
 
- 	// Team numbers are assigned randomly to every player
 
- 	//TODO: allow customize teams in rmg template
 
- 	for(const auto & pair : mapGenOptions.getPlayersSettings())
 
- 	{
 
- 		const auto & pSettings = pair.second;
 
- 		PlayerInfo player;
 
- 		player.canComputerPlay = true;
 
- 		int j = (pSettings.getPlayerType() == EPlayerType::COMP_ONLY) ? CPUONLY : CPHUMAN;
 
- 		if (j == CPHUMAN)
 
- 		{
 
- 			player.canHumanPlay = true;
 
- 		}
 
- 		if(pSettings.getTeam() != TeamID::NO_TEAM)
 
- 		{
 
- 			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);
 
- 		}
 
- 		teamsTotal.insert(player.team.getNum());
 
- 		map->getMap(this).players[pSettings.getColor().getNum()] = player;
 
- 	}
 
- 	map->getMap(this).howManyTeams = teamsTotal.size();
 
- }
 
- void CMapGenerator::genZones()
 
- {
 
- 	placer->placeZones(&rand);
 
- 	placer->assignZones(&rand);
 
- 	logGlobal->info("Zones generated successfully");
 
- }
 
- void CMapGenerator::addWaterTreasuresInfo()
 
- {
 
- 	if (!getZoneWater())
 
- 		return;
 
- 	//add treasures on water
 
- 	for (const auto& treasureInfo : getConfig().waterTreasure)
 
- 	{
 
- 		getZoneWater()->addTreasureInfo(treasureInfo);
 
- 	}
 
- }
 
- void CMapGenerator::fillZones()
 
- {
 
- 	addWaterTreasuresInfo();
 
- 	logGlobal->info("Started filling zones");
 
- 	size_t numZones = map->getZones().size();
 
- 	//we need info about all town types to evaluate dwellings and pandoras with creatures properly
 
- 	//place main town in the middle
 
- 	Load::Progress::setupStepsTill(numZones, 50);
 
- 	for (const auto& it : map->getZones())
 
- 	{
 
- 		it.second->initFreeTiles();
 
- 		it.second->initModificators();
 
- 		Progress::Progress::step();
 
- 	}
 
- 	std::vector<std::shared_ptr<Zone>> treasureZones;
 
- 	TModificators allJobs;
 
- 	for (auto& it : map->getZones())
 
- 	{
 
- 		allJobs.splice(allJobs.end(), it.second->getModificators());
 
- 	}
 
- 	Load::Progress::setupStepsTill(allJobs.size(), 240);
 
- 	if (config.singleThread) //No thread pool, just queue with deterministic order
 
- 	{
 
- 		while (!allJobs.empty())
 
- 		{
 
- 			for (auto it = allJobs.begin(); it != allJobs.end();)
 
- 			{
 
- 				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;
 
- 				}
 
- 			}
 
- 		}
 
- 	}
 
- 	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();)
 
- 			{
 
- 				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;
 
- 				}
 
- 			}
 
- 		}
 
- 		//Wait for all the tasks
 
- 		for (auto& fut : futures)
 
- 		{
 
- 			fut.get();
 
- 		}
 
- 	}
 
- 	for (const auto& it : map->getZones())
 
- 	{
 
- 		if (it.second->getType() == ETemplateZoneType::TREASURE)
 
- 			treasureZones.push_back(it.second);
 
- 	}
 
- 	//find place for Grail
 
- 	if (treasureZones.empty())
 
- 	{
 
- 		for (const auto& it : map->getZones())
 
- 			if (it.second->getType() != ETemplateZoneType::WATER)
 
- 				treasureZones.push_back(it.second);
 
- 	}
 
- 	auto grailZone = *RandomGeneratorUtil::nextItem(treasureZones, rand);
 
- 	map->getMap(this).grailPos = *RandomGeneratorUtil::nextItem(grailZone->freePaths().getTiles(), rand);
 
- 	logGlobal->info("Zones filled successfully");
 
- 	Load::Progress::set(250);
 
- }
 
- void CMapGenerator::addHeaderInfo()
 
- {
 
- 	auto& m = map->getMap(this);
 
- 	m.version = EMapFormat::VCMI;
 
- 	m.width = mapGenOptions.getWidth();
 
- 	m.height = mapGenOptions.getHeight();
 
- 	m.twoLevel = mapGenOptions.getHasTwoLevels();
 
- 	m.name = VLC->generaltexth->allTexts[740];
 
- 	m.description = getMapDescription();
 
- 	m.difficulty = 1;
 
- 	addPlayerInfo();
 
- 	m.waterMap = (mapGenOptions.getWaterContent() != EWaterContent::EWaterContent::NONE);
 
- 	m.banWaterContent();
 
- }
 
- int CMapGenerator::getNextMonlithIndex()
 
- {
 
- 	while (true)
 
- 	{
 
- 		if (monolithIndex >= VLC->objtypeh->knownSubObjects(Obj::MONOLITH_TWO_WAY).size())
 
- 			throw rmgException(boost::to_string(boost::format("There is no Monolith Two Way with index %d available!") % monolithIndex));
 
- 		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++;
 
- 			}
 
- 		}
 
- 	}
 
- }
 
- int CMapGenerator::getPrisonsRemaning() const
 
- {
 
- 	return allowedPrisons;
 
- }
 
- std::shared_ptr<CZonePlacer> CMapGenerator::getZonePlacer() const
 
- {
 
- 	return placer;
 
- }
 
- const std::vector<ArtifactID> & CMapGenerator::getAllPossibleQuestArtifacts() const
 
- {
 
- 	return questArtifacts;
 
- }
 
- const std::vector<HeroTypeID> CMapGenerator::getAllPossibleHeroes() const
 
- {
 
- 	auto isWaterMap = map->getMap(this).isWaterMap();
 
- 	//Skip heroes that were banned, including the ones placed in prisons
 
- 	std::vector<HeroTypeID> ret;
 
- 	for (int j = 0; j < map->getMap(this).allowedHeroes.size(); j++)
 
- 	{
 
- 		if (map->getMap(this).allowedHeroes[j])
 
- 		{
 
- 			auto * h = dynamic_cast<const CHero*>(VLC->heroTypes()->getByIndex(j));
 
- 			if ((h->onlyOnWaterMap && !isWaterMap) || (h->onlyOnMapWithoutWater && isWaterMap))
 
- 			{
 
- 				continue;
 
- 			}
 
- 			else
 
- 			{
 
- 				ret.push_back(HeroTypeID(j));
 
- 			}
 
- 		}
 
- 	}
 
- 	return ret;
 
- }
 
- void CMapGenerator::banQuestArt(const ArtifactID & id)
 
- {
 
- 	//TODO: Protect with mutex
 
- 	map->getMap(this).allowedArtifact[id] = false;
 
- }
 
- void CMapGenerator::banHero(const HeroTypeID & id)
 
- {
 
- 	//TODO: Protect with mutex
 
- 	map->getMap(this).banHero(id);
 
- }
 
- Zone * CMapGenerator::getZoneWater() const
 
- {
 
- 	for(auto & z : map->getZones())
 
- 		if(z.second->getType() == ETemplateZoneType::WATER)
 
- 			return z.second.get();
 
- 	return nullptr;
 
- }
 
- VCMI_LIB_NAMESPACE_END
 
 
  |