| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769 | 
							
- /*
 
-  * CRmgTemplateZone.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 "CRmgTemplateZone.h"
 
- #include "../mapping/CMapEditManager.h"
 
- #include "../mapping/CMap.h"
 
- #include "../VCMI_Lib.h"
 
- #include "../CTownHandler.h"
 
- #include "../CCreatureHandler.h"
 
- #include "../CSpellHandler.h" //for choosing random spells
 
- #include "../mapObjects/CommonConstructors.h"
 
- #include "../mapObjects/MapObjects.h" //needed to resolve templates for CommonConstructors.h
 
- #include "../mapObjects/CGPandoraBox.h"
 
- #include "../mapObjects/CRewardableObject.h"
 
- class CMap;
 
- class CMapEditManager;
 
- //class CGObjectInstance;
 
- CRmgTemplateZone::CTownInfo::CTownInfo() : townCount(0), castleCount(0), townDensity(0), castleDensity(0)
 
- {
 
- }
 
- int CRmgTemplateZone::CTownInfo::getTownCount() const
 
- {
 
- 	return townCount;
 
- }
 
- void CRmgTemplateZone::CTownInfo::setTownCount(int value)
 
- {
 
- 	if(value < 0)
 
- 		throw rmgException("Negative value for town count not allowed.");
 
- 	townCount = value;
 
- }
 
- int CRmgTemplateZone::CTownInfo::getCastleCount() const
 
- {
 
- 	return castleCount;
 
- }
 
- void CRmgTemplateZone::CTownInfo::setCastleCount(int value)
 
- {
 
- 	if(value < 0)
 
- 		throw rmgException("Negative value for castle count not allowed.");
 
- 	castleCount = value;
 
- }
 
- int CRmgTemplateZone::CTownInfo::getTownDensity() const
 
- {
 
- 	return townDensity;
 
- }
 
- void CRmgTemplateZone::CTownInfo::setTownDensity(int value)
 
- {
 
- 	if(value < 0)
 
- 		throw rmgException("Negative value for town density not allowed.");
 
- 	townDensity = value;
 
- }
 
- int CRmgTemplateZone::CTownInfo::getCastleDensity() const
 
- {
 
- 	return castleDensity;
 
- }
 
- void CRmgTemplateZone::CTownInfo::setCastleDensity(int value)
 
- {
 
- 	if(value < 0)
 
- 		throw rmgException("Negative value for castle density not allowed.");
 
- 	castleDensity = value;
 
- }
 
- CTileInfo::CTileInfo():nearestObjectDistance(INT_MAX), terrain(ETerrainType::WRONG) 
 
- {
 
- 	occupied = ETileType::POSSIBLE; //all tiles are initially possible to place objects or passages
 
- }
 
- int CTileInfo::getNearestObjectDistance() const
 
- {
 
- 	return nearestObjectDistance;
 
- }
 
- void CTileInfo::setNearestObjectDistance(int value)
 
- {
 
- 	nearestObjectDistance = std::max(0, value); //never negative (or unitialized)
 
- }
 
- bool CTileInfo::shouldBeBlocked() const
 
- {
 
- 	return occupied == ETileType::BLOCKED;
 
- }
 
- bool CTileInfo::isBlocked() const
 
- {
 
- 	return occupied == ETileType::BLOCKED || occupied == ETileType::USED;
 
- }
 
- bool CTileInfo::isPossible() const
 
- {
 
- 	return occupied == ETileType::POSSIBLE;
 
- }
 
- bool CTileInfo::isFree() const
 
- {
 
- 	return occupied == ETileType::FREE;
 
- }
 
- bool CTileInfo::isUsed() const
 
- {
 
- 	return occupied == ETileType::USED;
 
- }
 
- void CTileInfo::setOccupied(ETileType::ETileType value)
 
- {
 
- 	occupied = value;
 
- }
 
- ETerrainType CTileInfo::getTerrainType() const
 
- {
 
- 	return terrain;
 
- }
 
- void CTileInfo::setTerrainType(ETerrainType value)
 
- {
 
- 	terrain = value;
 
- }
 
- CRmgTemplateZone::CRmgTemplateZone() :
 
- 	id(0),
 
- 	type(ETemplateZoneType::PLAYER_START),
 
- 	size(1),
 
- 	townsAreSameType(false),
 
- 	matchTerrainToTown(true),
 
- 	townType(0),
 
- 	terrainType (ETerrainType::GRASS),
 
- 	zoneMonsterStrength(EMonsterStrength::ZONE_NORMAL),
 
- 	totalDensity(0)
 
- {
 
- 	townTypes = getDefaultTownTypes();
 
- 	terrainTypes = getDefaultTerrainTypes();
 
- }
 
- TRmgTemplateZoneId CRmgTemplateZone::getId() const
 
- {
 
- 	return id;
 
- }
 
- void CRmgTemplateZone::setId(TRmgTemplateZoneId value)
 
- {
 
- 	if(value <= 0)
 
- 		throw rmgException(boost::to_string(boost::format("Zone %d id should be greater than 0.") %id));
 
- 	id = value;
 
- }
 
- ETemplateZoneType::ETemplateZoneType CRmgTemplateZone::getType() const
 
- {
 
- 	return type;
 
- }
 
- void CRmgTemplateZone::setType(ETemplateZoneType::ETemplateZoneType value)
 
- {
 
- 	type = value;
 
- }
 
- int CRmgTemplateZone::getSize() const
 
- {
 
- 	return size;
 
- }
 
- void CRmgTemplateZone::setSize(int value)
 
- {
 
- 	if(value <= 0)
 
- 		throw rmgException(boost::to_string(boost::format("Zone %d size needs to be greater than 0.") % id));
 
- 	size = value;
 
- }
 
- boost::optional<int> CRmgTemplateZone::getOwner() const
 
- {
 
- 	return owner;
 
- }
 
- void CRmgTemplateZone::setOwner(boost::optional<int> value)
 
- {
 
- 	if(!(*value >= 0 && *value <= PlayerColor::PLAYER_LIMIT_I))
 
- 		throw rmgException(boost::to_string(boost::format ("Owner of zone %d has to be in range 0 to max player count.") %id));
 
- 	owner = value;
 
- }
 
- const CRmgTemplateZone::CTownInfo & CRmgTemplateZone::getPlayerTowns() const
 
- {
 
- 	return playerTowns;
 
- }
 
- void CRmgTemplateZone::setPlayerTowns(const CTownInfo & value)
 
- {
 
- 	playerTowns = value;
 
- }
 
- const CRmgTemplateZone::CTownInfo & CRmgTemplateZone::getNeutralTowns() const
 
- {
 
- 	return neutralTowns;
 
- }
 
- void CRmgTemplateZone::setNeutralTowns(const CTownInfo & value)
 
- {
 
- 	neutralTowns = value;
 
- }
 
- bool CRmgTemplateZone::getTownsAreSameType() const
 
- {
 
- 	return townsAreSameType;
 
- }
 
- void CRmgTemplateZone::setTownsAreSameType(bool value)
 
- {
 
- 	townsAreSameType = value;
 
- }
 
- const std::set<TFaction> & CRmgTemplateZone::getTownTypes() const
 
- {
 
- 	return townTypes;
 
- }
 
- void CRmgTemplateZone::setTownTypes(const std::set<TFaction> & value)
 
- {
 
- 	townTypes = value;
 
- }
 
- std::set<TFaction> CRmgTemplateZone::getDefaultTownTypes() const
 
- {
 
- 	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;
 
- }
 
- bool CRmgTemplateZone::getMatchTerrainToTown() const
 
- {
 
- 	return matchTerrainToTown;
 
- }
 
- void CRmgTemplateZone::setMatchTerrainToTown(bool value)
 
- {
 
- 	matchTerrainToTown = value;
 
- }
 
- const std::set<ETerrainType> & CRmgTemplateZone::getTerrainTypes() const
 
- {
 
- 	return terrainTypes;
 
- }
 
- void CRmgTemplateZone::setTerrainTypes(const std::set<ETerrainType> & value)
 
- {
 
- 	assert(value.find(ETerrainType::WRONG) == value.end() && value.find(ETerrainType::BORDER) == value.end() &&
 
- 		   value.find(ETerrainType::WATER) == value.end() && value.find(ETerrainType::ROCK) == value.end());
 
- 	terrainTypes = value;
 
- }
 
- std::set<ETerrainType> CRmgTemplateZone::getDefaultTerrainTypes() const
 
- {
 
- 	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 (auto & allowedTerType : allowedTerTypes)
 
- 		terTypes.insert(allowedTerType);
 
- 	return terTypes;
 
- }
 
- void CRmgTemplateZone::setMinesAmount (TResource res, ui16 amount)
 
- {
 
- 	mines[res] = amount;
 
- }
 
- std::map<TResource, ui16> CRmgTemplateZone::getMinesInfo() const
 
- {
 
- 	return mines;
 
- }
 
- void CRmgTemplateZone::addConnection(TRmgTemplateZoneId otherZone)
 
- {
 
- 	connections.push_back (otherZone);
 
- }
 
- std::vector<TRmgTemplateZoneId> CRmgTemplateZone::getConnections() const
 
- {
 
- 	return connections;
 
- }
 
- void CRmgTemplateZone::setMonsterStrength (EMonsterStrength::EMonsterStrength val)
 
- {
 
- 	assert (vstd::iswithin(val, EMonsterStrength::ZONE_WEAK, EMonsterStrength::ZONE_STRONG));
 
- 	zoneMonsterStrength = val;
 
- }
 
- void CRmgTemplateZone::setTotalDensity (ui16 val)
 
- {
 
- 	totalDensity = val;
 
- }
 
- ui16 CRmgTemplateZone::getTotalDensity () const
 
- {
 
- 	return totalDensity;
 
- }
 
- void CRmgTemplateZone::addTreasureInfo(CTreasureInfo & info)
 
- {
 
- 	treasureInfo.push_back(info);
 
- }
 
- std::vector<CTreasureInfo> CRmgTemplateZone::getTreasureInfo()
 
- {
 
- 	return treasureInfo;
 
- }
 
- std::set<int3>* CRmgTemplateZone::getFreePaths()
 
- {
 
- 	return &freePaths;
 
- }
 
- float3 CRmgTemplateZone::getCenter() const
 
- {
 
- 	return center;
 
- }
 
- void CRmgTemplateZone::setCenter(const float3 &f)
 
- {
 
- 	//limit boundaries to (0,1) square
 
- 	center = float3 (std::min(std::max(f.x, 0.f), 1.f), std::min(std::max(f.y, 0.f), 1.f), f.z);
 
- }
 
- bool CRmgTemplateZone::pointIsIn(int x, int y)
 
- {
 
- 	return true;
 
- }
 
- int3 CRmgTemplateZone::getPos() const
 
- {
 
- 	return pos;
 
- }
 
- void CRmgTemplateZone::setPos(const int3 &Pos)
 
- {
 
- 	pos = Pos;
 
- }
 
- void CRmgTemplateZone::addTile (const int3 &pos)
 
- {
 
- 	tileinfo.insert(pos);
 
- }
 
- std::set<int3> CRmgTemplateZone::getTileInfo () const
 
- {
 
- 	return tileinfo;
 
- }
 
- void CRmgTemplateZone::discardDistantTiles (CMapGenerator* gen, float distance)
 
- {
 
- 	//TODO: mark tiles beyond zone as unavailable, but allow to connect with adjacent zones
 
- 	//for (auto tile : tileinfo)
 
- 	//{
 
- 	//	if (tile.dist2d(this->pos) > distance)
 
- 	//	{
 
- 	//		gen->setOccupied(tile, ETileType::USED);
 
- 	//		//gen->setOccupied(tile, ETileType::BLOCKED); //fixme: crash at rendering?
 
- 	//	}
 
- 	//}
 
- 	vstd::erase_if (tileinfo, [distance, this](const int3 &tile) -> bool
 
- 	{
 
- 		return tile.dist2d(this->pos) > distance;
 
- 	});
 
- }
 
- void CRmgTemplateZone::createBorder(CMapGenerator* gen)
 
- {
 
- 	for (auto tile : tileinfo)
 
- 	{
 
- 		gen->foreach_neighbour (tile, [this, gen](int3 &pos)
 
- 		{
 
- 			if (!vstd::contains(this->tileinfo, pos))
 
- 			{
 
- 				gen->foreach_neighbour (pos, [this, gen](int3 &pos)
 
- 				{
 
- 					if (gen->isPossible(pos))
 
- 						gen->setOccupied (pos, ETileType::BLOCKED);
 
- 				});
 
- 			}
 
- 		});
 
- 	}
 
- }
 
- void CRmgTemplateZone::fractalize(CMapGenerator* gen)
 
- {
 
- 	std::vector<int3> clearedTiles (freePaths.begin(), freePaths.end());
 
- 	std::set<int3> possibleTiles;
 
- 	std::set<int3> tilesToClear; //will be set clear
 
- 	std::set<int3> tilesToIgnore; //will be erased in this iteration
 
- 	//the more treasure density, the greater distance between paths. Scaling is experimental.
 
- 	const float minDistance = std::sqrt(totalDensity * 3);
 
- 	for (auto tile : tileinfo)
 
- 	{
 
- 		if (gen->isFree(tile))
 
- 			clearedTiles.push_back(tile);
 
- 		else if (gen->isPossible(tile))
 
- 			possibleTiles.insert(tile);
 
- 	}
 
- 	if (clearedTiles.empty()) //this should come from zone connections
 
- 	{
 
- 		clearedTiles.push_back(pos); //zone center should be always clear
 
- 	}
 
- 	while (possibleTiles.size())
 
- 	{
 
- 		for (auto tileToMakePath : possibleTiles)
 
- 		{
 
- 			//find closest free tile
 
- 			float currentDistance = 1e10;
 
- 			int3 closestTile (-1,-1,-1);
 
- 			for (auto clearTile : clearedTiles)
 
- 			{
 
- 				float distance = tileToMakePath.dist2d(clearTile);
 
- 				
 
- 				if (distance < currentDistance)
 
- 				{
 
- 					currentDistance = distance;
 
- 					closestTile = clearTile;
 
- 				}
 
- 				if (currentDistance <= minDistance)
 
- 				{
 
- 					//this tile is close enough. Forget about it and check next one
 
- 					tilesToIgnore.insert (tileToMakePath);
 
- 					break;
 
- 				}
 
- 			}
 
- 			//if tiles is not close enough, make path to it
 
- 			if (currentDistance > minDistance)
 
- 			{
 
- 				crunchPath (gen, tileToMakePath, closestTile, id, &tilesToClear);
 
- 				break; //next iteration - use already cleared tiles
 
- 			}
 
- 		}
 
- 		for (auto tileToClear : tilesToClear)
 
- 		{
 
- 			//move cleared tiles from one set to another
 
- 			clearedTiles.push_back(tileToClear);
 
- 			vstd::erase_if_present(possibleTiles, tileToClear);
 
- 		}
 
- 		for (auto tileToClear : tilesToIgnore)
 
- 		{
 
- 			//these tiles are already connected, ignore them
 
- 			vstd::erase_if_present(possibleTiles, tileToClear);
 
- 		}
 
- 		if (tilesToClear.empty()) //nothing else can be done (?)
 
- 			break;
 
- 		tilesToClear.clear(); //empty this container
 
- 		tilesToIgnore.clear();
 
- 	}
 
- 	for (auto tile : clearedTiles)
 
- 	{
 
- 		freePaths.insert(tile);
 
- 	}
 
- 	
 
- 	if (0) //enable to debug
 
- 	{
 
- 		std::ofstream out(boost::to_string(boost::format("zone %d") % id));
 
- 		int levels = gen->map->twoLevel ? 2 : 1;
 
- 		int width =  gen->map->width;
 
- 		int height = gen->map->height;
 
- 		for (int k = 0; k < levels; k++)
 
- 		{
 
- 			for(int j=0; j<height; j++)
 
- 			{
 
- 				for (int i=0; i<width; i++)
 
- 				{
 
- 					out << (int)vstd::contains(freePaths, int3(i,j,k));
 
- 				}
 
- 				out << std::endl;
 
- 			}
 
- 			out << std::endl;
 
- 		}
 
- 	}
 
- 	logGlobal->infoStream() << boost::format ("Zone %d subdivided fractally") %id;
 
- }
 
- bool CRmgTemplateZone::crunchPath (CMapGenerator* gen, const int3 &src, const int3 &dst, TRmgTemplateZoneId zone, std::set<int3>* clearedTiles)
 
- {
 
- /*
 
- make shortest path with free tiles, reachning dst or closest already free tile. Avoid blocks.
 
- do not leave zone border
 
- */
 
- 	bool result = false;
 
- 	bool end = false;
 
- 	int3 currentPos = src;
 
- 	float distance = currentPos.dist2dSQ (dst);
 
- 	while (!end)
 
- 	{
 
- 		if (currentPos == dst)
 
- 		{
 
- 			result = true;
 
- 			break;
 
- 		}
 
- 		auto lastDistance = distance;
 
- 		gen->foreach_neighbour (currentPos, [this, gen, ¤tPos, dst, &distance, &result, &end, clearedTiles](int3 &pos)
 
- 		{
 
- 			if (!result) //not sure if lambda is worth it...
 
- 			{
 
- 				if (pos == dst)
 
- 				{
 
- 					result = true;
 
- 					end = true;
 
- 				}
 
- 				if (pos.dist2dSQ (dst) < distance)
 
- 				{
 
- 					if (!gen->isBlocked(pos))
 
- 					{
 
- 						if (vstd::contains (tileinfo, pos))
 
- 						{
 
- 							if (gen->isPossible(pos))
 
- 							{
 
- 								gen->setOccupied (pos, ETileType::FREE);
 
- 								if (clearedTiles)
 
- 									clearedTiles->insert(pos);
 
- 								currentPos = pos;
 
- 								distance = currentPos.dist2dSQ (dst);
 
- 							}
 
- 							else if (gen->isFree(pos))
 
- 							{
 
- 								end = true;
 
- 								result = true;
 
- 							}
 
- 							else
 
- 								throw rmgException(boost::to_string(boost::format("Tile %s of uknown type found on path") % pos()));
 
- 						}
 
- 					}
 
- 				}
 
- 			}
 
- 		});
 
- 		if (!(result || distance < lastDistance)) //we do not advance, use more avdnaced pathfinding algorithm?
 
- 		{
 
- 			logGlobal->warnStream() << boost::format ("No tile closer than %s found on path from %s to %s") %currentPos %src %dst;
 
- 			break;
 
- 		}
 
- 	}
 
- 	return result;
 
- }
 
- void CRmgTemplateZone::addRequiredObject(CGObjectInstance * obj, si32 strength)
 
- {
 
- 	requiredObjects.push_back(std::make_pair(obj, strength));
 
- }
 
- bool CRmgTemplateZone::addMonster(CMapGenerator* gen, int3 &pos, si32 strength)
 
- {
 
- 	//precalculate actual (randomized) monster strength based on this post
 
- 	//http://forum.vcmi.eu/viewtopic.php?p=12426#12426
 
- 	int mapMonsterStrength = gen->mapGenOptions->getMonsterStrength();
 
- 	int monsterStrength = zoneMonsterStrength + mapMonsterStrength - 1; //array index from 0 to 4
 
- 	static const int value1[] = {2500, 1500, 1000, 500, 0};
 
- 	static const int value2[] = {7500, 7500, 7500, 5000, 5000};
 
- 	static const float multiplier1[] = {0.5, 0.75, 1.0, 1.5, 1.5};
 
- 	static const float multiplier2[] = {0.5, 0.75, 1.0, 1.0, 1.5};
 
- 	int strength1 = std::max(0.f, (strength - value1[monsterStrength]) * multiplier1[monsterStrength]); 
 
- 	int strength2 = std::max(0.f, (strength - value2[monsterStrength]) * multiplier2[monsterStrength]); 
 
- 	strength = strength1 + strength2;
 
- 	if (strength < 2000)
 
- 		return false; //no guard at all
 
- 	CreatureID creId = CreatureID::NONE;
 
- 	int amount = 0;
 
- 	std::vector<CreatureID> possibleCreatures;
 
- 	for (auto cre : VLC->creh->creatures)
 
- 	{
 
- 		if (cre->special)
 
- 			continue;
 
- 		if ((cre->AIValue * (cre->ammMin + cre->ammMax) / 2 < strength) && (strength < cre->AIValue * 100)) //at least one full monster. size between minimum size of given stack and 100
 
- 		{
 
- 			possibleCreatures.push_back(cre->idNumber);
 
- 		}
 
- 	}
 
- 	if (possibleCreatures.size())
 
- 	{
 
- 		creId = *RandomGeneratorUtil::nextItem(possibleCreatures, gen->rand);
 
- 		amount = strength / VLC->creh->creatures[creId]->AIValue;
 
- 		if (amount >= 4)
 
- 			amount *= gen->rand.nextDouble(0.75, 1.25);
 
- 	}
 
- 	else //just pick any available creature
 
- 	{
 
- 		creId = CreatureID(132); //Azure Dragon
 
- 		amount = strength / VLC->creh->creatures[creId]->AIValue;
 
- 	}
 
- 	auto guard = new CGCreature();
 
- 	guard->ID = Obj::MONSTER;
 
- 	guard->subID = creId;
 
- 	auto  hlp = new CStackInstance(creId, amount);
 
- 	//will be set during initialization
 
- 	guard->putStack(SlotID(0), hlp);
 
- 	placeObject(gen, guard, pos);
 
- 	return true;
 
- }
 
- bool CRmgTemplateZone::createTreasurePile (CMapGenerator* gen, int3 &pos)
 
- {
 
- 	CTreasurePileInfo info;
 
- 	std::map<int3, CGObjectInstance *> treasures;
 
- 	std::set<int3> boundary;
 
- 	int3 guardPos (-1,-1,-1);
 
- 	info.nextTreasurePos = pos;
 
- 	//default values
 
- 	int maxValue = 5000;
 
- 	int minValue = 1500;
 
- 	if (treasureInfo.size())
 
- 	{
 
- 		//roulette wheel
 
- 		int r = gen->rand.nextInt (1, totalDensity);
 
- 		for (auto t : treasureInfo)
 
- 		{
 
- 			if (r <= t.threshold)
 
- 			{
 
- 				maxValue = t.max;
 
- 				minValue = t.min;
 
- 				break;
 
- 			}
 
- 		}
 
- 	}
 
- 	int currentValue = 0;
 
- 	CGObjectInstance * object = nullptr;
 
- 	while (currentValue < minValue)
 
- 	{
 
- 		//make sure our shape is consistent
 
- 		treasures[info.nextTreasurePos] = nullptr;
 
- 		for (auto treasurePos : treasures)
 
- 		{
 
- 			gen->foreach_neighbour (treasurePos.first, [gen, &boundary](int3 pos)
 
- 			{
 
- 				boundary.insert(pos);
 
- 			});
 
- 		}
 
- 		for (auto treasurePos : treasures)
 
- 		{
 
- 			//leaving only boundary around objects
 
- 			vstd::erase_if_present (boundary, treasurePos.first);
 
- 		}
 
- 		for (auto tile : boundary)
 
- 		{
 
- 			//we can't extend boundary anymore
 
- 			if (!(gen->isBlocked(tile) || gen->isPossible(tile)))
 
- 				break;
 
- 		}
 
- 		int remaining = maxValue - currentValue;
 
- 		ObjectInfo oi = getRandomObject(gen, info, remaining);
 
- 		object = oi.generateObject();
 
- 		if (!object)
 
- 		{
 
- 			vstd::erase_if_present(treasures, info.nextTreasurePos);
 
- 			break;
 
- 		}
 
- 		else
 
- 		{
 
- 			//update treasure pile area
 
- 			int3 visitablePos = oi.templ.getVisitableOffset() + info.nextTreasurePos;
 
- 			info.visitablePositions.insert(visitablePos); //can be accessed only from bottom or side
 
- 			if (oi.templ.isVisitableFromTop())
 
- 				info.visitableFromTopPositions.insert(visitablePos); //can be accessed from any direction
 
- 			for (auto blockedOffset : oi.templ.getBlockedOffsets())
 
- 				info.occupiedPositions.insert(info.nextTreasurePos + blockedOffset);
 
- 			info.occupiedPositions.insert(visitablePos);
 
- 		}
 
- 		currentValue += oi.value;
 
- 		
 
- 		treasures[info.nextTreasurePos] = object;
 
- 		//now find place for next object
 
- 		int3 placeFound(-1,-1,-1);
 
- 		//FIXME: find out why teh following code causes crashes
 
- 		//std::vector<int3> boundaryVec(boundary.begin(), boundary.end());
 
- 		//RandomGeneratorUtil::randomShuffle(boundaryVec, gen->rand);
 
- 		//for (auto tile : boundaryVec)
 
- 		for (auto tile : boundary)
 
- 		{
 
- 			if (gen->isPossible(tile)) //we can place new treasure only on possible tile
 
- 			{
 
- 				bool here = true;
 
- 				gen->foreach_neighbour (tile, [gen, &here](int3 pos)
 
- 				{
 
- 					if (!(gen->isBlocked(pos) || gen->isPossible(pos)))
 
- 						here = false;
 
- 				});
 
- 				if (here)
 
- 				{
 
- 					placeFound = tile;
 
- 					break;
 
- 				}
 
- 			}
 
- 		}
 
- 		if (placeFound.valid())
 
- 			info.nextTreasurePos = placeFound;
 
- 	}
 
- 	if (treasures.size())
 
- 	{
 
- 		//find object closest to zone center, then con nect it to the middle of the zone
 
- 		int3 zoneCenter = getPos();
 
- 		int3 closestTile = int3(-1,-1,-1);
 
- 		float minDistance = 1e10;
 
- 		for (auto treasure : treasures)
 
- 		{
 
- 			if (zoneCenter.dist2d(treasure.first) < minDistance)
 
- 			{
 
- 				closestTile = treasure.first;
 
- 				minDistance = zoneCenter.dist2d(treasure.first);
 
- 			}
 
- 		}
 
- 		assert (closestTile.valid());
 
- 		if (!crunchPath (gen, closestTile, findClosestTile(freePaths, closestTile), id)) //make sure pile is connected to the middle of zone
 
- 		{
 
- 			for (auto treasure : treasures)
 
- 			{
 
- 				if (gen->isPossible(treasure.first))
 
- 					gen->setOccupied (treasure.first, ETileType::BLOCKED);
 
- 			}
 
- 			return true;
 
- 		}
 
- 		for (auto tile : boundary) //guard must be standing there
 
- 		{
 
- 			if (gen->isFree(tile)) //this tile could be already blocked, don't place a monster here
 
- 			{
 
- 				guardPos = tile;
 
- 				break;
 
- 			}
 
- 		}
 
- 		if (guardPos.valid())
 
- 		{
 
- 			for (auto treasure : treasures)
 
- 			{
 
- 				int3 visitableOffset = treasure.second->getVisitableOffset();
 
- 				placeObject(gen, treasure.second, treasure.first + visitableOffset);
 
- 			}
 
- 			if (addMonster(gen, guardPos, currentValue))
 
- 			{//block only if the object is guarded
 
- 				for (auto tile : boundary)
 
- 				{
 
- 					if (gen->isPossible(tile))
 
- 						gen->setOccupied (tile, ETileType::BLOCKED);
 
- 				}
 
- 			}
 
- 		}
 
- 		else //we couldn't make a connection to this location, block it
 
- 		{
 
- 			for (auto treasure : treasures)
 
- 			{
 
- 				if (gen->isPossible(treasure.first))
 
- 					gen->setOccupied (treasure.first, ETileType::BLOCKED);
 
- 			}
 
- 		}
 
- 		return true;
 
- 	}
 
- 	else //we did not place eveyrthing successfully
 
- 		return false;
 
- }
 
- void CRmgTemplateZone::initTownType (CMapGenerator* gen)
 
- {
 
- 	//FIXME: handle case that this player is not present -> towns should be set to neutral
 
- 	int totalTowns = 0;
 
- 	auto addNewTowns = [&totalTowns, gen, this](int count, bool hasFort, PlayerColor player)
 
- 	{
 
- 		for (int i = 0; i < count; i++)
 
- 		{
 
- 			auto  town = new CGTownInstance();
 
- 			town->ID = Obj::TOWN;
 
- 			if (this->townsAreSameType)
 
- 				town->subID = townType;
 
- 			else
 
- 				town->subID = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand); //TODO: check allowed town types for this zone
 
- 			town->tempOwner = player;
 
- 			if (hasFort)
 
- 				town->builtBuildings.insert(BuildingID::FORT);
 
- 			town->builtBuildings.insert(BuildingID::DEFAULT);
 
- 			if (!totalTowns) 
 
- 			{
 
- 				//first town in zone sets the facton of entire zone
 
- 				town->subID = townType;
 
- 				//register MAIN town of zone
 
- 				gen->registerZone(town->subID);
 
- 				//first town in zone goes in the middle
 
- 				placeAndGuardObject(gen, town, getPos() + town->getVisitableOffset(), 0);
 
- 			}
 
- 			else
 
- 				addRequiredObject (town);
 
- 			totalTowns++;
 
- 		}
 
- 	};
 
- 	if ((type == ETemplateZoneType::CPU_START) || (type == ETemplateZoneType::PLAYER_START))
 
- 	{
 
- 		//set zone types to player faction, generate main town
 
- 		logGlobal->infoStream() << "Preparing playing zone";
 
- 		int player_id = *owner - 1;
 
- 		auto & playerInfo = gen->map->players[player_id];
 
- 		if (playerInfo.canAnyonePlay())
 
- 		{
 
- 			PlayerColor player(player_id);
 
- 			townType = gen->mapGenOptions->getPlayersSettings().find(player)->second.getStartingTown();
 
- 			if(townType == CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
 
- 				townType = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand); // all possible towns, skip neutral
 
- 			
 
- 			auto  town = new CGTownInstance();
 
- 			town->ID = Obj::TOWN;
 
- 			town->subID = townType;
 
- 			town->tempOwner = player;
 
- 			town->builtBuildings.insert(BuildingID::FORT);
 
- 			town->builtBuildings.insert(BuildingID::DEFAULT);
 
- 			//towns are big objects and should be centered around visitable position
 
- 			placeAndGuardObject(gen, town, getPos() + town->getVisitableOffset(), 0); //generate no guards, but free path to entrance
 
- 			totalTowns++;
 
- 			//register MAIN town of zone only
 
- 			gen->registerZone (town->subID);
 
- 			logGlobal->traceStream() << "Fill player info " << player_id;
 
- 			// Update player info
 
- 			playerInfo.allowedFactions.clear();
 
- 			playerInfo.allowedFactions.insert (townType);
 
- 			playerInfo.hasMainTown = true;
 
- 			playerInfo.posOfMainTown = town->pos - town->getVisitableOffset();
 
- 			playerInfo.generateHeroAtMainTown = true;
 
- 			//now create actual towns
 
- 			addNewTowns (playerTowns.getCastleCount() - 1, true, player);
 
- 			addNewTowns (playerTowns.getTownCount(), false, player);
 
- 			//requiredObjects.push_back(town);
 
- 		}
 
- 		else
 
- 		{			
 
- 			type = ETemplateZoneType::TREASURE;
 
- 			townType = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand);
 
- 			logGlobal->infoStream() << "Skipping this zone cause no player";
 
- 		}
 
- 	}
 
- 	else //no player
 
- 	{
 
- 		townType = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand);
 
- 	}
 
- 	addNewTowns (neutralTowns.getCastleCount(), true, PlayerColor::NEUTRAL);
 
- 	addNewTowns (neutralTowns.getTownCount(), false, PlayerColor::NEUTRAL);
 
- }
 
- void CRmgTemplateZone::initTerrainType (CMapGenerator* gen)
 
- {
 
- 	if (matchTerrainToTown)
 
- 		terrainType = VLC->townh->factions[townType]->nativeTerrain;
 
- 	else
 
- 		terrainType = *RandomGeneratorUtil::nextItem(terrainTypes, gen->rand);
 
- 	//TODO: allow new types of terrain?
 
- 	if (pos.z)
 
- 	{
 
- 		if (terrainType != ETerrainType::LAVA)
 
- 			terrainType = ETerrainType::SUBTERRANEAN;
 
- 	}
 
- 	else
 
- 	{
 
- 		if (terrainType == ETerrainType::SUBTERRANEAN)
 
- 			terrainType = ETerrainType::DIRT;
 
- 	}
 
- 	paintZoneTerrain (gen, terrainType);
 
- }
 
- void CRmgTemplateZone::paintZoneTerrain (CMapGenerator* gen, ETerrainType terrainType)
 
- {
 
- 	std::vector<int3> tiles;
 
- 	for (auto tile : tileinfo)
 
- 	{
 
- 		tiles.push_back (tile);
 
- 	}
 
- 	gen->editManager->getTerrainSelection().setSelection(tiles);
 
- 	gen->editManager->drawTerrain(terrainType, &gen->rand);
 
- }
 
- bool CRmgTemplateZone::placeMines (CMapGenerator* gen)
 
- {
 
- 	std::vector<Res::ERes> required_mines;
 
- 	required_mines.push_back(Res::ERes::WOOD);
 
- 	required_mines.push_back(Res::ERes::ORE);
 
- 	static const Res::ERes woodOre[] = {Res::ERes::WOOD, Res::ERes::ORE};
 
- 	static const Res::ERes preciousResources[] = {Res::ERes::GEMS, Res::ERes::CRYSTAL, Res::ERes::MERCURY, Res::ERes::SULFUR};
 
- 	//TODO: factory / copy constructor?
 
- 	for (const auto & res : woodOre)
 
- 	{
 
- 		//TODO: these 2 should be close to town (within 12 tiles radius)
 
- 		for (int i = 0; i < mines[res]; i++)
 
- 		{
 
- 			auto mine = new CGMine();
 
- 			mine->ID = Obj::MINE;
 
- 			mine->subID = static_cast<si32>(res);
 
- 			mine->producedResource = res;
 
- 			mine->producedQuantity = mine->defaultResProduction();
 
- 			addRequiredObject(mine, 1500);
 
- 		}
 
- 	}
 
- 	for (const auto & res : preciousResources)
 
- 	{
 
- 		for (int i = 0; i < mines[res]; i++)
 
- 		{
 
- 			auto mine = new CGMine();
 
- 			mine->ID = Obj::MINE;
 
- 			mine->subID = static_cast<si32>(res);
 
- 			mine->producedResource = res;
 
- 			mine->producedQuantity = mine->defaultResProduction();
 
- 			addRequiredObject(mine, 3500);
 
- 		}
 
- 	}
 
- 	for (int i = 0; i < mines[Res::GOLD]; i++)
 
- 	{
 
- 		auto mine = new CGMine();
 
- 		mine->ID = Obj::MINE;
 
- 		mine->subID = static_cast<si32>(Res::GOLD);
 
- 		mine->producedResource = Res::GOLD;
 
- 		mine->producedQuantity = mine->defaultResProduction();
 
- 		addRequiredObject(mine, 7000);
 
- 	}
 
- 	return true;
 
- }
 
- bool CRmgTemplateZone::createRequiredObjects(CMapGenerator* gen)
 
- {
 
- 	logGlobal->infoStream() << "Creating required objects";
 
- 	for(const auto &obj : requiredObjects)
 
- 	{
 
- 		int3 pos;
 
- 		logGlobal->traceStream() << "Looking for place";
 
- 		if ( ! findPlaceForObject(gen, obj.first, 3, pos))		
 
- 		{
 
- 			logGlobal->errorStream() << boost::format("Failed to fill zone %d due to lack of space") %id;
 
- 			//TODO CLEANUP!
 
- 			return false;
 
- 		}
 
- 		logGlobal->traceStream() << "Place found";
 
- 		placeObject(gen, obj.first, pos);
 
- 		guardObject (gen, obj.first, obj.second);
 
- 	}
 
- 	return true;
 
- }
 
- void CRmgTemplateZone::createTreasures(CMapGenerator* gen)
 
- {
 
- 	const double minDistance = 3;
 
- 	do {
 
- 		
 
- 		int3 pos;
 
- 		if ( ! findPlaceForTreasurePile(gen,  minDistance, pos))		
 
- 		{
 
- 			break;
 
- 		}
 
- 		createTreasurePile (gen, pos);
 
- 	} while(true);
 
- }
 
- void CRmgTemplateZone::createObstacles(CMapGenerator* gen)
 
- { 
 
- 	if (pos.z) //underground
 
- 	{
 
- 		std::vector<int3> rockTiles;
 
- 		for (auto tile : tileinfo)
 
- 		{	
 
- 			if (gen->shouldBeBlocked(tile))
 
- 			{
 
- 				bool placeRock = true;
 
- 				gen->foreach_neighbour (tile, [gen, &placeRock](int3 &pos)
 
- 				{
 
- 					if (!(gen->shouldBeBlocked(pos) || gen->isPossible(pos)))
 
- 						placeRock = false;
 
- 				});
 
- 				if (placeRock)
 
- 				{
 
- 					rockTiles.push_back(tile);
 
- 				}
 
- 			}
 
- 		}
 
- 		gen->editManager->getTerrainSelection().setSelection(rockTiles);
 
- 		gen->editManager->drawTerrain(ETerrainType::ROCK, &gen->rand);
 
- 		//for (auto tile : rockTiles)
 
- 		//{
 
- 		//	gen->setOccupied (tile, ETileType::USED);
 
- 		//	gen->foreach_neighbour (tile, [gen](int3 &pos)
 
- 		//	{
 
- 		//		if (!gen->isUsed(pos))
 
- 		//			gen->setOccupied (pos, ETileType::BLOCKED);
 
- 		//	});
 
- 		//}
 
- 	}
 
- 	//get all possible obstacles for this terrain
 
- 	for (auto primaryID : VLC->objtypeh->knownObjects()) 
 
- 	{ 
 
- 		for (auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID)) 
 
- 		{ 
 
- 			auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID); 
 
- 			if (handler->isStaticObject())
 
- 			{
 
- 				for (auto temp : handler->getTemplates())
 
- 				{
 
- 					if (temp.canBePlacedAt(terrainType) && temp.getBlockMapOffset().valid())
 
- 						possibleObstacles.push_back(temp);
 
- 				}
 
- 			}
 
- 		} 
 
- 	}
 
- 	auto sel = gen->editManager->getTerrainSelection();
 
- 	sel.clearSelection();
 
- 	auto tryToPlaceObstacleHere = [this, gen](int3& tile)-> bool
 
- 	{
 
- 		auto temp = *RandomGeneratorUtil::nextItem(possibleObstacles, gen->rand);
 
- 		int3 obstaclePos = tile - temp.getBlockMapOffset();
 
- 		if (canObstacleBePlacedHere(gen, temp, obstaclePos)) //can be placed here
 
- 		{
 
- 			auto obj = VLC->objtypeh->getHandlerFor(temp.id, temp.subid)->create(temp);
 
- 			placeObject(gen, obj, obstaclePos);
 
- 			return true;
 
- 		}
 
- 		return false;
 
- 	};
 
- 	for (auto tile : tileinfo)
 
- 	{
 
- 		if (gen->shouldBeBlocked(tile)) //fill tiles that should be blocked with obstacles
 
- 		{
 
- 			while (!tryToPlaceObstacleHere(tile));
 
- 		}
 
- 		else if (gen->isPossible(tile))
 
- 		{
 
- 			//try to place random obstacle once - if not possible, leave it clear
 
- 			tryToPlaceObstacleHere(tile);
 
- 		}
 
- 	}
 
- }
 
- bool CRmgTemplateZone::fill(CMapGenerator* gen)
 
- {
 
- 	initTownType(gen);
 
- 	initTerrainType(gen);
 
- 	addAllPossibleObjects (gen);
 
- 	placeMines(gen);
 
- 	createRequiredObjects(gen);
 
- 	fractalize(gen); //after required objects are created and linked with their own paths
 
- 	createTreasures(gen);
 
- 	createObstacles(gen);
 
- 	logGlobal->infoStream() << boost::format ("Zone %d filled successfully") %id;
 
- 	return true;
 
- }
 
- bool CRmgTemplateZone::findPlaceForTreasurePile(CMapGenerator* gen, si32 min_dist, int3 &pos)
 
- {
 
- 	//si32 min_dist = sqrt(tileinfo.size()/density);
 
- 	int best_distance = 0;
 
- 	bool result = false;
 
- 	//logGlobal->infoStream() << boost::format("Min dist for density %f is %d") % density % min_dist;
 
- 	for(auto tile : tileinfo)
 
- 	{
 
- 		auto dist = gen->getTile(tile).getNearestObjectDistance();
 
- 		if (gen->isPossible(tile) && (dist >= min_dist) && (dist > best_distance))
 
- 		{
 
- 			bool allTilesAvailable = true;
 
- 			gen->foreach_neighbour (tile, [&gen, &allTilesAvailable](int3 neighbour)
 
- 			{
 
- 				if (!(gen->isPossible(neighbour) || gen->isBlocked(neighbour)))
 
- 				{
 
- 					allTilesAvailable = false; //all present tiles must be already blocked or ready for new objects
 
- 				}
 
- 			});
 
- 			if (allTilesAvailable)
 
- 			{
 
- 				best_distance = dist;
 
- 				pos = tile;
 
- 				result = true;
 
- 			}
 
- 		}
 
- 	}
 
- 	if (result)
 
- 	{
 
- 		gen->setOccupied(pos, ETileType::BLOCKED); //block that tile
 
- 	}
 
- 	return result;
 
- }
 
- bool CRmgTemplateZone::canObstacleBePlacedHere(CMapGenerator* gen, ObjectTemplate &temp, int3 &pos)
 
- {
 
- 	if (!gen->map->isInTheMap(pos)) //blockmap may fit in the map, but botom-right corner does not
 
- 		return false;
 
- 	auto tilesBlockedByObject = temp.getBlockedOffsets();
 
- 	for (auto blockingTile : tilesBlockedByObject)
 
- 	{
 
- 		int3 t = pos + blockingTile;
 
- 		if (!gen->map->isInTheMap(t) || !(gen->isPossible(t) || gen->shouldBeBlocked(t)))
 
- 		{
 
- 			return false; //if at least one tile is not possible, object can't be placed here
 
- 		}
 
- 	}
 
- 	return true;
 
- }
 
- bool CRmgTemplateZone::findPlaceForObject(CMapGenerator* gen, CGObjectInstance* obj, si32 min_dist, int3 &pos)
 
- {
 
- 	//we need object apperance to deduce free tiles
 
- 	if (obj->appearance.id == Obj::NO_OBJ)
 
- 	{
 
- 		auto templates = VLC->objtypeh->getHandlerFor(obj->ID, obj->subID)->getTemplates(gen->map->getTile(getPos()).terType);
 
- 		if (templates.empty())
 
- 			throw rmgException(boost::to_string(boost::format("Did not find graphics for object (%d,%d) at %s") %obj->ID %obj->subID %pos));
 
- 	
 
- 		obj->appearance = templates.front();
 
- 	}
 
- 	//si32 min_dist = sqrt(tileinfo.size()/density);
 
- 	int best_distance = 0;
 
- 	bool result = false;
 
- 	//si32 w = gen->map->width;
 
- 	//si32 h = gen->map->height;
 
- 	//logGlobal->infoStream() << boost::format("Min dist for density %f is %d") % density % min_dist;
 
- 	auto tilesBlockedByObject = obj->getBlockedOffsets();
 
- 	for (auto tile : tileinfo)
 
- 	{
 
- 		//object must be accessible from at least one surounding tile
 
- 		bool accessible = false;
 
- 		for (int x = -1; x < 2; x++)
 
- 			for (int y = -1; y <2; y++)
 
- 		{
 
- 			if (x && y) //check only if object is visitable from another tile
 
- 			{
 
- 				int3 offset = obj->getVisitableOffset() + int3(x, y, 0);
 
- 				if (!vstd::contains(tilesBlockedByObject, offset))
 
- 				{
 
- 					int3 nearbyPos = tile + offset;
 
- 					if (gen->map->isInTheMap(nearbyPos))
 
- 					{
 
- 						if (obj->appearance.isVisitableFrom(x, y) && !gen->isBlocked(nearbyPos))
 
- 							accessible = true;
 
- 					}
 
- 				}
 
- 			}
 
- 		};
 
- 		if (!accessible)
 
- 			continue;
 
- 		auto ti = gen->getTile(tile);
 
- 		auto dist = ti.getNearestObjectDistance();
 
- 		//avoid borders
 
- 		if (gen->isPossible(tile) && (dist >= min_dist) && (dist > best_distance))
 
- 		{
 
- 			bool allTilesAvailable = true;
 
- 			for (auto blockingTile : tilesBlockedByObject)
 
- 			{
 
- 				int3 t = tile + blockingTile;
 
- 				if (!gen->map->isInTheMap(t) || !gen->isPossible(t))
 
- 				{
 
- 					allTilesAvailable = false; //if at least one tile is not possible, object can't be placed here
 
- 					break;
 
- 				}
 
- 			}
 
- 			if (allTilesAvailable)
 
- 			{
 
- 				best_distance = dist;
 
- 				pos = tile;
 
- 				result = true;
 
- 			}
 
- 		}
 
- 	}
 
- 	if (result)
 
- 	{
 
- 		gen->setOccupied(pos, ETileType::BLOCKED); //block that tile
 
- 	}
 
- 	return result;
 
- }
 
- void CRmgTemplateZone::checkAndPlaceObject(CMapGenerator* gen, CGObjectInstance* object, const int3 &pos)
 
- {
 
- 	if (!gen->map->isInTheMap(pos))
 
- 		throw rmgException(boost::to_string(boost::format("Position of object %d at %s is outside the map") % object->id % pos));
 
- 	object->pos = pos;
 
- 	if (object->isVisitable() && !gen->map->isInTheMap(object->visitablePos()))
 
- 		throw rmgException(boost::to_string(boost::format("Visitable tile %s of object %d at %s is outside the map") % object->visitablePos() % object->id % object->pos()));
 
- 	for (auto tile : object->getBlockedPos())
 
- 	{
 
- 		if (!gen->map->isInTheMap(tile))
 
- 			throw rmgException(boost::to_string(boost::format("Tile %s of object %d at %s is outside the map") % tile() % object->id % object->pos()));
 
- 	}
 
- 	if (object->appearance.id == Obj::NO_OBJ)
 
- 	{
 
- 		auto terrainType = gen->map->getTile(pos).terType;
 
- 		auto templates = VLC->objtypeh->getHandlerFor(object->ID, object->subID)->getTemplates(terrainType);
 
- 		if (templates.empty())
 
- 			throw rmgException(boost::to_string(boost::format("Did not find graphics for object (%d,%d) at %s (terrain %d)") %object->ID %object->subID %pos %terrainType));
 
- 	
 
- 		object->appearance = templates.front();
 
- 	}
 
- 	gen->map->addBlockVisTiles(object);
 
- 	gen->editManager->insertObject(object, pos);
 
- 	//logGlobal->traceStream() << boost::format ("Successfully inserted object (%d,%d) at pos %s") %object->ID %object->subID %pos();
 
- }
 
- void CRmgTemplateZone::placeObject(CMapGenerator* gen, CGObjectInstance* object, const int3 &pos)
 
- {
 
- 	//logGlobal->traceStream() << boost::format("Inserting object at %d %d") % pos.x % pos.y;
 
- 	checkAndPlaceObject (gen, object, pos);
 
- 	auto points = object->getBlockedPos();
 
- 	if (object->isVisitable())
 
- 		points.insert(pos + object->getVisitableOffset());
 
- 	points.insert(pos);
 
- 	for(auto p : points)
 
- 	{		
 
- 		if (gen->map->isInTheMap(p))
 
- 		{
 
- 			gen->setOccupied(p, ETileType::USED);
 
- 		}
 
- 	}
 
- 	for(auto tile : tileinfo)
 
- 	{		
 
- 		si32 d = pos.dist2dSQ(tile); //optimization, only relative distance is interesting
 
- 		gen->setNearestObjectDistance(tile, std::min(d, gen->getNearestObjectDistance(tile)));
 
- 	}
 
- }
 
- void CRmgTemplateZone::placeAndGuardObject(CMapGenerator* gen, CGObjectInstance* object, const int3 &pos, si32 str)
 
- {
 
- 	placeObject(gen, object, pos);
 
- 	guardObject(gen, object, str);
 
- }
 
- std::vector<int3> CRmgTemplateZone::getAccessibleOffsets (CMapGenerator* gen, CGObjectInstance* object)
 
- {
 
- 	//get all tiles from which this object can be accessed
 
- 	int3 visitable = object->visitablePos();
 
- 	std::vector<int3> tiles;
 
- 	auto tilesBlockedByObject = object->getBlockedPos(); //absolue value, as object is already placed
 
- 	gen->foreach_neighbour(visitable, [&](int3& pos) 
 
- 	{
 
- 		if (gen->isPossible(pos))
 
- 		{
 
- 			if (!vstd::contains(tilesBlockedByObject, pos))
 
- 			{
 
- 				if (object->appearance.isVisitableFrom(pos.x - visitable.x, pos.y - visitable.y) && !gen->isBlocked(pos)) //TODO: refactor - info about visitability from absolute coordinates
 
- 				{
 
- 					tiles.push_back(pos);
 
- 				}
 
- 			}
 
- 		};
 
- 	});
 
- 	return tiles;
 
- }
 
- bool CRmgTemplateZone::guardObject(CMapGenerator* gen, CGObjectInstance* object, si32 str)
 
- {
 
- 	logGlobal->traceStream() << boost::format("Guard object at %s") % object->pos();
 
- 	std::vector<int3> tiles = getAccessibleOffsets (gen, object);
 
- 	int3 guardTile(-1,-1,-1);
 
- 	for (auto tile : tiles)
 
- 	{
 
- 		//crunching path may fail if center of teh zone is dirrectly over wide object
 
- 		if (crunchPath (gen, tile, findClosestTile(freePaths, tile), id)) //make sure object is accessible before surrounding it with blocked tiles
 
- 		{
 
- 			guardTile = tile;
 
- 			break;
 
- 		}
 
- 	}
 
- 	if (!guardTile.valid())
 
- 	{
 
- 		logGlobal->errorStream() << boost::format("Failed to crunch path to object at %s") % object->pos();
 
- 		return false;
 
- 	}
 
- 	if (addMonster (gen, guardTile, str)) //do not place obstacles around unguarded object
 
- 	{
 
- 		for (auto pos : tiles)
 
- 		{
 
- 			if (!gen->isFree(pos))
 
- 				gen->setOccupied(pos, ETileType::BLOCKED);
 
- 		}
 
- 		gen->setOccupied (guardTile, ETileType::USED);
 
- 	}
 
- 	else //allow no guard or other object in front of this object
 
- 	{
 
- 		for (auto tile : tiles)
 
- 			gen->setOccupied (tile, ETileType::FREE);
 
- 	}
 
- 	return true;
 
- }
 
- ObjectInfo CRmgTemplateZone::getRandomObject (CMapGenerator* gen, CTreasurePileInfo &info, ui32 value)
 
- {
 
- 	std::vector<std::pair<ui32, ObjectInfo>> tresholds;
 
- 	ui32 total = 0;
 
- 	ui32 minValue = 0.25f * value;
 
- 	//roulette wheel
 
- 	for (auto oi : possibleObjects)
 
- 	{
 
- 		if (oi.value >= minValue && oi.value <= value)
 
- 		{
 
- 			int3 visitableOffset = oi.templ.getVisitableOffset(); //visitablePos assumes object will be shifter by visitableOffset
 
- 			if (info.visitablePositions.size()) //do not try to match first object in zone
 
- 			{
 
- 				bool fitsHere = false;
 
- 				int3 visitablePos = info.nextTreasurePos;
 
- 				if (oi.templ.isVisitableFromTop())
 
- 				{
 
- 					for (auto tile : info.visitablePositions)
 
- 					{
 
- 						int3 actualTile = tile + visitableOffset;
 
- 						if (visitablePos.areNeighbours(actualTile)) //we access object from any position
 
- 						{
 
- 							fitsHere = true;
 
- 							break;
 
- 						}
 
- 					}
 
- 				}
 
- 				else
 
- 				{
 
- 					//if object is not visitable from top, it must be accessible from below or side
 
- 					for (auto tile : info.visitableFromTopPositions)
 
- 					{
 
- 						int3 actualTile = tile + visitableOffset;
 
- 						if (visitablePos.areNeighbours(actualTile) && visitablePos.y >= actualTile.y) //we access object from below or side
 
- 						{
 
- 							fitsHere = true;
 
- 							break;
 
- 						}
 
- 					}
 
- 				}
 
- 				if (!fitsHere)
 
- 					continue;
 
- 			}
 
- 				//now check blockmap, including our already reserved pile area
 
- 			bool fitsBlockmap = true;
 
- 			std::set<int3> blockedOffsets = oi.templ.getBlockedOffsets();
 
- 			blockedOffsets.insert (visitableOffset);
 
- 			for (auto blockingTile : blockedOffsets)
 
- 			{
 
- 				int3 t = info.nextTreasurePos + visitableOffset + blockingTile;
 
- 				if (!gen->map->isInTheMap(t) || vstd::contains(info.occupiedPositions, t))
 
- 				{
 
- 					fitsBlockmap = false; //if at least one tile is not possible, object can't be placed here
 
- 					break;
 
- 				}
 
- 				if (!(gen->isPossible(t) || gen->isBlocked(t))) //blocked tiles of object may cover blocked tiles, but not used or free tiles
 
- 				{
 
- 					fitsBlockmap = false;
 
- 					break;
 
- 				}
 
- 			}
 
- 			if (!fitsBlockmap)
 
- 				continue;
 
- 			total += oi.probability;
 
- 			tresholds.push_back (std::make_pair (total, oi));
 
- 		}
 
- 	}
 
- 	//Generate pandora Box with gold if the value is extremely high
 
- 	ObjectInfo oi;
 
- 	if (tresholds.empty())
 
- 	{
 
- 		if (minValue > 20000) //we don't have object valuable enough
 
- 		{
 
- 			oi.generateObject = [minValue]() -> CGObjectInstance *
 
- 			{
 
- 				auto obj = new CGPandoraBox();
 
- 				obj->ID = Obj::PANDORAS_BOX;
 
- 				obj->subID = 0;
 
- 				obj->resources[Res::GOLD] = minValue;
 
- 				return obj;
 
- 			};
 
- 			oi.setTemplate(Obj::PANDORAS_BOX, 0, terrainType);
 
- 			oi.value = minValue;
 
- 			oi.probability = 0;
 
- 		}
 
- 		else
 
- 		{
 
- 			oi.generateObject = [gen]() -> CGObjectInstance *
 
- 			{
 
- 				return nullptr;
 
- 			};
 
- 			oi.setTemplate(Obj::PANDORAS_BOX, 0, terrainType); //TODO: null template or something? should be never used, but hell knows
 
- 			oi.value = 0;
 
- 			oi.probability = 0;
 
- 		}
 
- 		return oi;
 
- 	}
 
- 	int r = gen->rand.nextInt (1, total);
 
- 	for (auto t : tresholds)
 
- 	{
 
- 		if (r <= t.first)
 
- 			return t.second;
 
- 	}
 
- 	//FIXME: control reaches end of non-void function. Missing return?
 
- }
 
- void CRmgTemplateZone::addAllPossibleObjects (CMapGenerator* gen)
 
- {
 
- 	ObjectInfo oi;
 
- 	for (auto primaryID : VLC->objtypeh->knownObjects()) 
 
- 	{ 
 
- 		for (auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID)) 
 
- 		{ 
 
- 			auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID); 
 
- 			if (!handler->isStaticObject() && handler->getRMGInfo().value)
 
- 			{
 
- 				for (auto temp : handler->getTemplates())
 
- 				{
 
- 					if (temp.canBePlacedAt(terrainType))
 
- 					{
 
- 						oi.generateObject = [gen, temp]() -> CGObjectInstance *
 
- 						{
 
- 							return VLC->objtypeh->getHandlerFor(temp.id, temp.subid)->create(temp);
 
- 						};
 
- 						oi.value = handler->getRMGInfo().value;
 
- 						oi.probability = handler->getRMGInfo().rarity;
 
- 						oi.templ = temp;
 
- 						possibleObjects.push_back (oi);
 
- 					}
 
- 				}
 
- 			}
 
- 		} 
 
- 	}
 
- 	//dwellings
 
- 	auto subObjects = VLC->objtypeh->knownSubObjects(Obj::CREATURE_GENERATOR1);
 
- 	//don't spawn original "neutral" dwellings that got replaced by Conflux dwellings in AB
 
- 	static int elementalConfluxROE[] = {7, 13, 16, 47};
 
- 	for (int i = 0; i < 4; i++)
 
- 		vstd::erase_if_present(subObjects, elementalConfluxROE[i]);
 
- 	for (auto secondaryID : subObjects)
 
- 	{
 
- 		auto dwellingHandler = dynamic_cast<const CDwellingInstanceConstructor*>(VLC->objtypeh->getHandlerFor(Obj::CREATURE_GENERATOR1, secondaryID).get());
 
- 		auto creatures = dwellingHandler->getProducedCreatures();
 
- 		if (creatures.empty())
 
- 			continue;
 
- 		auto cre = creatures.front();
 
- 		if (cre->faction == townType)
 
- 		{
 
- 			oi.value = cre->AIValue * cre->growth * (1 + (float)(gen->getZoneCount(cre->faction)) / gen->getTotalZoneCount() + (float)(gen->getZoneCount(cre->faction) / 2)); //TODO: include town count in formula
 
- 			oi.probability = 40;
 
- 			for (auto temp : dwellingHandler->getTemplates())
 
- 			{
 
- 				if (temp.canBePlacedAt(terrainType))
 
- 				{
 
- 					oi.generateObject = [gen, temp, secondaryID, dwellingHandler]() -> CGObjectInstance *
 
- 					{
 
- 						auto obj = VLC->objtypeh->getHandlerFor(Obj::CREATURE_GENERATOR1, secondaryID)->create(temp);
 
- 						//dwellingHandler->configureObject(obj, gen->rand);
 
- 						obj->tempOwner = PlayerColor::NEUTRAL;
 
- 						return obj;
 
- 					};
 
- 					oi.templ = temp;
 
- 					possibleObjects.push_back (oi);
 
- 				}
 
- 			}
 
- 		}
 
- 	}
 
- 	static const int scrollValues[] = {500, 2000, 3000, 4000, 5000};
 
- 	for (int i = 0; i < 5; i++)
 
- 	{
 
- 		oi.generateObject = [i, gen]() -> CGObjectInstance *
 
- 		{
 
- 			auto obj = new CGArtifact();
 
- 			obj->ID = Obj::SPELL_SCROLL;
 
- 			obj->subID = 0;
 
- 			std::vector<SpellID> out;
 
- 			//TODO: unify with cb->getAllowedSpells?
 
- 			for (ui32 i = 0; i < gen->map->allowedSpell.size(); i++) //spellh size appears to be greater (?)
 
- 			{
 
- 				const CSpell *spell = SpellID(i).toSpell();
 
- 				if (gen->map->allowedSpell[spell->id] && spell->level == i+1)
 
- 				{
 
- 					out.push_back(spell->id);
 
- 				}
 
- 			}
 
- 			auto a = CArtifactInstance::createScroll(RandomGeneratorUtil::nextItem(out, gen->rand)->toSpell());
 
- 			gen->map->addNewArtifactInstance(a);
 
- 			obj->storedArtifact = a;
 
- 			return obj;
 
- 		};
 
- 		oi.setTemplate (Obj::SPELL_SCROLL, 0, terrainType);
 
- 		oi.value = scrollValues[i];
 
- 		oi.probability = 30;
 
- 		possibleObjects.push_back (oi);
 
- 	}
 
- 	//pandora box with gold
 
- 	for (int i = 1; i < 5; i++)
 
- 	{
 
- 		oi.generateObject = [i]() -> CGObjectInstance *
 
- 		{
 
- 			auto obj = new CGPandoraBox();
 
- 			obj->ID = Obj::PANDORAS_BOX;
 
- 			obj->subID = 0;
 
- 			obj->resources[Res::GOLD] = i * 5000;
 
- 			return obj;
 
- 		};
 
- 		oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
 
- 		oi.value = i * 5000;;
 
- 		oi.probability = 5;
 
- 		possibleObjects.push_back (oi);
 
- 	}
 
- 	//pandora box with experience
 
- 	for (int i = 1; i < 5; i++)
 
- 	{
 
- 		oi.generateObject = [i]() -> CGObjectInstance *
 
- 		{
 
- 			auto obj = new CGPandoraBox();
 
- 			obj->ID = Obj::PANDORAS_BOX;
 
- 			obj->subID = 0;
 
- 			obj->gainedExp = i * 5000;
 
- 			return obj;
 
- 		};
 
- 		oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
 
- 		oi.value = i * 6000;;
 
- 		oi.probability = 20;
 
- 		possibleObjects.push_back (oi);
 
- 	}
 
- 	//pandora box with creatures
 
- 	static const int tierValues[] = {5000, 7000, 9000, 12000, 16000, 21000, 27000};
 
- 	for (auto creature : VLC->creh->creatures)
 
- 	{
 
- 		if (!creature->special && VLC->townh->factions[creature->faction]->nativeTerrain == terrainType)
 
- 		{
 
- 			int actualTier = creature->level > 7 ? 6 : creature->level-1;
 
- 			int creaturesAmount = tierValues[actualTier] / creature->AIValue;
 
- 			if (creaturesAmount <= 5)
 
- 			{
 
- 			}
 
- 			else if (creaturesAmount <= 12)
 
- 			{
 
- 				(creaturesAmount /= 2) *= 2;
 
- 			}
 
- 			else if (creaturesAmount <= 50)
 
- 			{
 
- 				creaturesAmount = boost::math::round((float)creaturesAmount / 5) * 5;
 
- 			}
 
- 			else if (creaturesAmount <= 12)
 
- 			{
 
- 				creaturesAmount = boost::math::round((float)creaturesAmount / 10) * 10;
 
- 			}
 
- 			oi.generateObject = [creature, creaturesAmount]() -> CGObjectInstance *
 
- 			{
 
- 				auto obj = new CGPandoraBox();
 
- 				obj->ID = Obj::PANDORAS_BOX;
 
- 				obj->subID = 0;
 
- 				auto stack = new CStackInstance(creature, creaturesAmount);
 
- 				obj->creatures.putStack(SlotID(0), stack);
 
- 				return obj;
 
- 			};
 
- 			oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
 
- 			oi.value = (2 * (creature->AIValue) * creaturesAmount * (1 + (float)(gen->getZoneCount(creature->faction)) / gen->getTotalZoneCount()))/3; //TODO: count number of towns on the map
 
- 			oi.probability = 3;
 
- 			possibleObjects.push_back (oi);
 
- 		}
 
- 	}
 
- 	//Pandora with 12 spells of certain level
 
- 	for (int i = 1; i <= GameConstants::SPELL_LEVELS; i++)
 
- 	{
 
- 		oi.generateObject = [i, gen]() -> CGObjectInstance *
 
- 		{
 
- 			auto obj = new CGPandoraBox();
 
- 			obj->ID = Obj::PANDORAS_BOX;
 
- 			obj->subID = 0;
 
- 			std::vector <CSpell *> spells;
 
- 			for (auto spell : VLC->spellh->objects)
 
- 			{
 
- 				if (!spell->isSpecialSpell() && spell->level == i)
 
- 					spells.push_back(spell);
 
- 			}
 
- 			RandomGeneratorUtil::randomShuffle(spells, gen->rand);
 
- 			for (int j = 0; j < std::min<int>(12, spells.size()); j++)
 
- 			{
 
- 				obj->spells.push_back (spells[j]->id);
 
- 			}
 
- 			return obj;
 
- 		};
 
- 		oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
 
- 		oi.value = (i + 1) * 2500; //5000 - 15000
 
- 		oi.probability = 2;
 
- 		possibleObjects.push_back (oi);
 
- 	}
 
- 	//Pandora with 15 spells of certain school
 
- 	for (int i = 1; i <= 4; i++)
 
- 	{
 
- 		oi.generateObject = [i, gen]() -> CGObjectInstance *
 
- 		{
 
- 			auto obj = new CGPandoraBox();
 
- 			obj->ID = Obj::PANDORAS_BOX;
 
- 			obj->subID = 0;
 
- 			std::vector <CSpell *> spells;
 
- 			for (auto spell : VLC->spellh->objects)
 
- 			{
 
- 				if (!spell->isSpecialSpell())
 
- 				{
 
- 					bool school = false; //TODO: we could have better interface for iterating schools
 
- 					switch (i)
 
- 					{
 
- 						case 1:
 
- 							school = spell->air;
 
- 						case 2:
 
- 							school = spell->earth;
 
- 						case 3:
 
- 							school = spell->fire;
 
- 						case 4:
 
- 							school = spell->water;
 
- 					}
 
- 					if (school)
 
- 						spells.push_back(spell);
 
- 				}
 
- 			}
 
- 			RandomGeneratorUtil::randomShuffle(spells, gen->rand);
 
- 			for (int j = 0; j < std::min<int>(15, spells.size()); j++)
 
- 			{
 
- 				obj->spells.push_back (spells[j]->id);
 
- 			}
 
- 			return obj;
 
- 		};
 
- 		oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
 
- 		oi.value = 15000;
 
- 		oi.probability = 2;
 
- 		possibleObjects.push_back (oi);
 
- 	}
 
- 	// Pandora box with 60 random spells
 
- 	oi.generateObject = [gen]() -> CGObjectInstance *
 
- 	{
 
- 		auto obj = new CGPandoraBox();
 
- 		obj->ID = Obj::PANDORAS_BOX;
 
- 		obj->subID = 0;
 
- 		std::vector <CSpell *> spells;
 
- 		for (auto spell : VLC->spellh->objects)
 
- 		{
 
- 			if (!spell->isSpecialSpell())
 
- 				spells.push_back(spell);
 
- 		}
 
- 		RandomGeneratorUtil::randomShuffle(spells, gen->rand);
 
- 		for (int j = 0; j < std::min<int>(60, spells.size()); j++)
 
- 		{
 
- 			obj->spells.push_back (spells[j]->id);
 
- 		}
 
- 		return obj;
 
- 	};
 
- 	oi.setTemplate (Obj::PANDORAS_BOX, 0, terrainType);
 
- 	oi.value = 3000;
 
- 	oi.probability = 2;
 
- 	possibleObjects.push_back (oi);
 
- }
 
- void ObjectInfo::setTemplate (si32 type, si32 subtype, ETerrainType terrainType)
 
- {
 
- 	templ = VLC->objtypeh->getHandlerFor(type, subtype)->getTemplates(terrainType).front();
 
- }
 
 
  |