2013-08-17 15:46:48 +03:00
/*
* 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"
2014-05-22 20:25:17 +03:00
# include "../mapping/CMapEditManager.h"
# include "../mapping/CMap.h"
2013-08-17 15:46:48 +03:00
# include "../VCMI_Lib.h"
# include "../CTownHandler.h"
2014-06-01 15:10:44 +03:00
# include "../CCreatureHandler.h"
2013-08-17 15:46:48 +03:00
2014-05-22 20:25:17 +03:00
class CMap ;
class CMapEditManager ;
2013-08-17 15:46:48 +03:00
CRmgTemplateZone : : CTownInfo : : CTownInfo ( ) : townCount ( 0 ) , castleCount ( 0 ) , townDensity ( 0 ) , castleDensity ( 0 )
{
}
int CRmgTemplateZone : : CTownInfo : : getTownCount ( ) const
{
return townCount ;
}
void CRmgTemplateZone : : CTownInfo : : setTownCount ( int value )
{
2014-05-23 18:12:31 +03:00
if ( value < 0 )
throw rmgException ( " Negative value for town count not allowed. " ) ;
2013-08-17 15:46:48 +03:00
townCount = value ;
}
int CRmgTemplateZone : : CTownInfo : : getCastleCount ( ) const
{
return castleCount ;
}
void CRmgTemplateZone : : CTownInfo : : setCastleCount ( int value )
{
2014-05-23 18:12:31 +03:00
if ( value < 0 )
throw rmgException ( " Negative value for castle count not allowed. " ) ;
2013-08-17 15:46:48 +03:00
castleCount = value ;
}
int CRmgTemplateZone : : CTownInfo : : getTownDensity ( ) const
{
return townDensity ;
}
void CRmgTemplateZone : : CTownInfo : : setTownDensity ( int value )
{
2014-05-23 18:12:31 +03:00
if ( value < 0 )
throw rmgException ( " Negative value for town density not allowed. " ) ;
2013-08-17 15:46:48 +03:00
townDensity = value ;
}
int CRmgTemplateZone : : CTownInfo : : getCastleDensity ( ) const
{
return castleDensity ;
}
void CRmgTemplateZone : : CTownInfo : : setCastleDensity ( int value )
{
2014-05-23 18:12:31 +03:00
if ( value < 0 )
throw rmgException ( " Negative value for castle density not allowed. " ) ;
2013-08-17 15:46:48 +03:00
castleDensity = value ;
}
2014-05-30 17:50:06 +03:00
CTileInfo : : CTileInfo ( ) : nearestObjectDistance ( INT_MAX ) , terrain ( ETerrainType : : WRONG )
2014-05-22 20:25:17 +03:00
{
2014-05-30 17:50:06 +03:00
occupied = ETileType : : POSSIBLE ; //all tiles are initially possible to place objects or passages
2014-05-22 20:25:17 +03:00
}
2014-05-30 17:50:06 +03:00
int CTileInfo : : getNearestObjectDistance ( ) const
2014-05-22 20:25:17 +03:00
{
return nearestObjectDistance ;
}
2014-05-30 17:50:06 +03:00
void CTileInfo : : setNearestObjectDistance ( int value )
2014-05-22 20:25:17 +03:00
{
2014-05-23 20:14:20 +03:00
nearestObjectDistance = std : : max ( 0 , value ) ; //never negative (or unitialized)
2014-05-22 20:25:17 +03:00
}
2014-05-30 17:50:06 +03:00
bool CTileInfo : : shouldBeBlocked ( ) const
2014-05-22 20:25:17 +03:00
{
2014-05-30 17:50:06 +03:00
return occupied = = ETileType : : BLOCKED ;
2014-05-22 20:25:17 +03:00
}
2014-05-30 17:50:06 +03:00
bool CTileInfo : : isBlocked ( ) const
2014-05-22 20:25:17 +03:00
{
2014-05-30 17:50:06 +03:00
return occupied = = ETileType : : BLOCKED | | occupied = = ETileType : : USED ;
2014-05-22 20:25:17 +03:00
}
2014-05-30 17:50:06 +03:00
bool CTileInfo : : isPossible ( ) const
2014-05-22 20:25:17 +03:00
{
2014-05-30 17:50:06 +03:00
return occupied = = ETileType : : POSSIBLE ;
2014-05-22 20:25:17 +03:00
}
2014-05-30 17:50:06 +03:00
bool CTileInfo : : isFree ( ) const
{
return occupied = = ETileType : : FREE ;
}
void CTileInfo : : setOccupied ( ETileType : : ETileType value )
2014-05-22 20:25:17 +03:00
{
occupied = value ;
}
2014-05-30 17:50:06 +03:00
ETerrainType CTileInfo : : getTerrainType ( ) const
2014-05-22 20:25:17 +03:00
{
return terrain ;
}
2014-05-30 17:50:06 +03:00
void CTileInfo : : setTerrainType ( ETerrainType value )
2014-05-22 20:25:17 +03:00
{
terrain = value ;
}
2013-08-17 15:46:48 +03:00
CRmgTemplateZone : : CRmgTemplateZone ( ) : id ( 0 ) , type ( ETemplateZoneType : : PLAYER_START ) , size ( 1 ) ,
townsAreSameType ( false ) , matchTerrainToTown ( true )
{
townTypes = getDefaultTownTypes ( ) ;
terrainTypes = getDefaultTerrainTypes ( ) ;
}
TRmgTemplateZoneId CRmgTemplateZone : : getId ( ) const
{
return id ;
}
void CRmgTemplateZone : : setId ( TRmgTemplateZoneId value )
{
2014-05-23 18:12:31 +03:00
if ( value < = 0 )
throw rmgException ( boost : : to_string ( boost : : format ( " Zone %d id should be greater than 0. " ) % id ) ) ;
2013-08-17 15:46:48 +03:00
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 )
{
2014-05-23 18:12:31 +03:00
if ( value < = 0 )
throw rmgException ( boost : : to_string ( boost : : format ( " Zone %d size needs to be greater than 0. " ) % id ) ) ;
2013-08-17 15:46:48 +03:00
size = value ;
}
boost : : optional < int > CRmgTemplateZone : : getOwner ( ) const
{
return owner ;
}
void CRmgTemplateZone : : setOwner ( boost : : optional < int > value )
{
2014-05-23 18:12:31 +03:00
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 ) ) ;
2013-08-17 15:46:48 +03:00
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 ;
}
boost : : optional < TRmgTemplateZoneId > CRmgTemplateZone : : getTerrainTypeLikeZone ( ) const
{
return terrainTypeLikeZone ;
}
void CRmgTemplateZone : : setTerrainTypeLikeZone ( boost : : optional < TRmgTemplateZoneId > value )
{
terrainTypeLikeZone = value ;
}
boost : : optional < TRmgTemplateZoneId > CRmgTemplateZone : : getTownTypeLikeZone ( ) const
{
return townTypeLikeZone ;
}
void CRmgTemplateZone : : setTownTypeLikeZone ( boost : : optional < TRmgTemplateZoneId > value )
{
townTypeLikeZone = value ;
}
2014-05-22 20:25:17 +03:00
2014-05-24 13:42:06 +03:00
void CRmgTemplateZone : : addConnection ( TRmgTemplateZoneId otherZone )
{
connections . push_back ( otherZone ) ;
}
std : : vector < TRmgTemplateZoneId > CRmgTemplateZone : : getConnections ( ) const
{
return connections ;
}
float3 CRmgTemplateZone : : getCenter ( ) const
{
return center ;
}
2014-05-30 17:50:06 +03:00
void CRmgTemplateZone : : setCenter ( const float3 & f )
2014-05-24 13:42:06 +03:00
{
//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 ) ;
}
2014-05-22 20:25:17 +03:00
bool CRmgTemplateZone : : pointIsIn ( int x , int y )
{
2014-05-24 15:06:08 +03:00
return true ;
2014-05-22 20:25:17 +03:00
}
2014-05-30 17:50:06 +03:00
int3 CRmgTemplateZone : : getPos ( ) const
2014-05-22 20:25:17 +03:00
{
2014-05-24 13:42:06 +03:00
return pos ;
}
2014-05-25 12:02:15 +03:00
void CRmgTemplateZone : : setPos ( const int3 & Pos )
2014-05-24 13:42:06 +03:00
{
pos = Pos ;
2014-05-22 20:25:17 +03:00
}
2014-05-25 12:02:15 +03:00
void CRmgTemplateZone : : addTile ( const int3 & pos )
2014-05-24 15:06:08 +03:00
{
2014-05-30 22:23:41 +03:00
tileinfo . insert ( pos ) ;
}
2014-05-31 11:56:14 +03:00
std : : set < int3 > CRmgTemplateZone : : getTileInfo ( ) const
{
return tileinfo ;
}
2014-05-30 22:23:41 +03:00
void CRmgTemplateZone : : createBorder ( CMapGenerator * gen )
{
for ( auto tile : tileinfo )
{
gen - > foreach_neighbour ( tile , [ this , gen ] ( int3 & pos )
{
if ( ! vstd : : contains ( this - > tileinfo , pos ) )
2014-05-31 11:56:14 +03:00
{
gen - > foreach_neighbour ( pos , [ this , gen ] ( int3 & pos )
{
if ( gen - > isPossible ( pos ) )
gen - > setOccupied ( pos , ETileType : : BLOCKED ) ;
} ) ;
}
} ) ;
}
}
2014-05-31 15:11:20 +03:00
bool CRmgTemplateZone : : crunchPath ( CMapGenerator * gen , const int3 & src , const int3 & dst , TRmgTemplateZoneId zone )
2014-05-31 11:56:14 +03:00
{
/*
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 )
break ;
auto lastDistance = distance ;
gen - > foreach_neighbour ( currentPos , [ this , gen , & currentPos , dst , & distance , & result , & end ] ( 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 ) ;
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 ( ) ) ) ;
}
}
}
}
2014-05-30 22:23:41 +03:00
} ) ;
2014-05-31 11:56:14 +03:00
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 ;
}
2014-05-30 22:23:41 +03:00
}
2014-05-31 11:56:14 +03:00
return result ;
2014-05-24 15:06:08 +03:00
}
2014-06-01 17:31:15 +03:00
void CRmgTemplateZone : : addRequiredObject ( CGObjectInstance * obj , si32 strength )
2014-05-22 20:25:17 +03:00
{
2014-06-01 17:31:15 +03:00
requiredObjects . push_back ( std : : make_pair ( obj , strength ) ) ;
2014-06-01 13:02:43 +03:00
}
2014-05-24 15:33:22 +03:00
2014-06-01 15:10:44 +03:00
void CRmgTemplateZone : : addMonster ( CMapGenerator * gen , int3 & pos , si32 strength )
{
CreatureID creId = CreatureID : : NONE ;
int amount = 0 ;
while ( true )
{
creId = VLC - > creh - > pickRandomMonster ( gen - > rand ) ;
auto cre = VLC - > creh - > creatures [ creId ] ;
amount = strength / cre - > AIValue ;
if ( amount > = cre - > ammMin & & amount < = 100 )
break ;
}
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 ) ;
}
2014-06-01 13:02:43 +03:00
bool CRmgTemplateZone : : fill ( CMapGenerator * gen )
{
2014-05-24 15:33:22 +03:00
int townId = 0 ;
2014-05-22 20:25:17 +03:00
if ( ( type = = ETemplateZoneType : : CPU_START ) | | ( type = = ETemplateZoneType : : PLAYER_START ) )
{
logGlobal - > infoStream ( ) < < " Preparing playing zone " ;
int player_id = * owner - 1 ;
auto & playerInfo = gen - > map - > players [ player_id ] ;
if ( playerInfo . canAnyonePlay ( ) )
{
PlayerColor player ( player_id ) ;
auto town = new CGTownInstance ( ) ;
town - > ID = Obj : : TOWN ;
2014-05-24 15:33:22 +03:00
townId = gen - > mapGenOptions - > getPlayersSettings ( ) . find ( player ) - > second . getStartingTown ( ) ;
2014-05-22 20:25:17 +03:00
if ( townId = = CMapGenOptions : : CPlayerSettings : : RANDOM_TOWN )
2014-05-23 18:12:31 +03:00
townId = * RandomGeneratorUtil : : nextItem ( VLC - > townh - > getAllowedFactions ( ) , gen - > rand ) ; // all possible towns, skip neutral
2014-05-22 20:25:17 +03:00
town - > subID = townId ;
town - > tempOwner = player ;
town - > builtBuildings . insert ( BuildingID : : FORT ) ;
town - > builtBuildings . insert ( BuildingID : : DEFAULT ) ;
2014-05-24 13:42:06 +03:00
placeObject ( gen , town , getPos ( ) ) ;
2014-05-24 19:39:58 +03:00
logGlobal - > traceStream ( ) < < " Placed object " ;
2014-05-22 20:25:17 +03:00
2014-05-24 19:39:58 +03:00
logGlobal - > traceStream ( ) < < " Fill player info " < < player_id ;
2014-05-22 20:25:17 +03:00
auto & playerInfo = gen - > map - > players [ player_id ] ;
// Update player info
playerInfo . allowedFactions . clear ( ) ;
playerInfo . allowedFactions . insert ( town - > subID ) ;
playerInfo . hasMainTown = true ;
playerInfo . posOfMainTown = town - > pos - int3 ( 2 , 0 , 0 ) ;
playerInfo . generateHeroAtMainTown = true ;
2014-06-01 13:02:43 +03:00
//requiredObjects.push_back(town);
2014-05-22 20:25:17 +03:00
std : : vector < Res : : ERes > required_mines ;
required_mines . push_back ( Res : : ERes : : WOOD ) ;
required_mines . push_back ( Res : : ERes : : ORE ) ;
for ( const auto res : required_mines )
{
auto mine = new CGMine ( ) ;
mine - > ID = Obj : : MINE ;
mine - > subID = static_cast < si32 > ( res ) ;
mine - > producedResource = res ;
mine - > producedQuantity = mine - > defaultResProduction ( ) ;
2014-06-01 17:31:15 +03:00
addRequiredObject ( mine ) ;
2014-05-22 20:25:17 +03:00
}
}
else
{
type = ETemplateZoneType : : TREASURE ;
2014-05-24 15:33:22 +03:00
townId = * RandomGeneratorUtil : : nextItem ( VLC - > townh - > getAllowedFactions ( ) , gen - > rand ) ;
2014-05-22 20:25:17 +03:00
logGlobal - > infoStream ( ) < < " Skipping this zone cause no player " ;
}
}
2014-05-24 15:33:22 +03:00
else //no player
{
townId = * RandomGeneratorUtil : : nextItem ( VLC - > townh - > getAllowedFactions ( ) , gen - > rand ) ;
}
//paint zone with matching terrain
std : : vector < int3 > tiles ;
for ( auto tile : tileinfo )
{
2014-05-30 22:23:41 +03:00
tiles . push_back ( tile ) ;
2014-05-24 15:33:22 +03:00
}
gen - > editManager - > getTerrainSelection ( ) . setSelection ( tiles ) ;
gen - > editManager - > drawTerrain ( VLC - > townh - > factions [ townId ] - > nativeTerrain , & gen - > rand ) ;
2014-05-22 20:25:17 +03:00
logGlobal - > infoStream ( ) < < " Creating required objects " ;
2014-06-01 13:02:43 +03:00
for ( const auto & obj : requiredObjects )
2014-05-22 20:25:17 +03:00
{
int3 pos ;
2014-05-24 19:39:58 +03:00
logGlobal - > traceStream ( ) < < " Looking for place " ;
2014-06-01 17:31:15 +03:00
if ( ! findPlaceForObject ( gen , obj . first , 3 , pos ) )
2014-05-22 20:25:17 +03:00
{
2014-05-23 18:12:31 +03:00
logGlobal - > errorStream ( ) < < boost : : format ( " Failed to fill zone %d due to lack of space " ) % id ;
2014-05-22 20:25:17 +03:00
//TODO CLEANUP!
return false ;
}
2014-05-24 19:39:58 +03:00
logGlobal - > traceStream ( ) < < " Place found " ;
2014-05-22 20:25:17 +03:00
2014-06-01 17:31:15 +03:00
placeObject ( gen , obj . first , pos ) ;
if ( obj . second )
{
2014-06-01 22:01:18 +03:00
guardObject ( gen , obj . first , obj . second ) ;
2014-06-01 17:31:15 +03:00
}
2014-05-22 20:25:17 +03:00
}
std : : vector < CGObjectInstance * > guarded_objects ;
static auto res_gen = gen - > rand . getIntRange ( Res : : ERes : : WOOD , Res : : ERes : : GOLD ) ;
const double res_mindist = 5 ;
do {
auto obj = new CGResource ( ) ;
auto restype = static_cast < Res : : ERes > ( res_gen ( ) ) ;
obj - > ID = Obj : : RESOURCE ;
obj - > subID = static_cast < si32 > ( restype ) ;
obj - > amount = 0 ;
int3 pos ;
if ( ! findPlaceForObject ( gen , obj , res_mindist , pos ) )
{
delete obj ;
break ;
}
2014-05-23 20:14:20 +03:00
placeObject ( gen , obj , pos ) ;
2014-05-22 20:25:17 +03:00
if ( ( restype ! = Res : : ERes : : WOOD ) & & ( restype ! = Res : : ERes : : ORE ) )
{
guarded_objects . push_back ( obj ) ;
}
} while ( true ) ;
for ( const auto & obj : guarded_objects )
{
2014-06-01 15:10:44 +03:00
if ( ! guardObject ( gen , obj , 1000 ) )
2014-05-22 20:25:17 +03:00
{
//TODO, DEL obj from map
}
}
auto sel = gen - > editManager - > getTerrainSelection ( ) ;
sel . clearSelection ( ) ;
2014-05-30 22:23:41 +03:00
for ( auto tile : tileinfo )
2014-05-22 20:25:17 +03:00
{
2014-05-31 13:26:37 +03:00
//test code - block all the map to show paths clearly
//if (gen->isPossible(tile))
// gen->setOccupied(tile, ETileType::BLOCKED);
2014-05-30 22:23:41 +03:00
if ( gen - > shouldBeBlocked ( tile ) ) //fill tiles that should be blocked with obstacles
2014-05-22 20:25:17 +03:00
{
auto obj = new CGObjectInstance ( ) ;
obj - > ID = static_cast < Obj > ( 130 ) ;
obj - > subID = 0 ;
2014-05-30 22:23:41 +03:00
placeObject ( gen , obj , tile ) ;
2014-05-22 20:25:17 +03:00
}
}
2014-05-23 20:14:20 +03:00
//logGlobal->infoStream() << boost::format("Filling %d with ROCK") % sel.getSelectedItems().size();
2014-05-22 20:25:17 +03:00
//gen->editManager->drawTerrain(ETerrainType::ROCK, &gen->gen);
2014-05-23 18:12:31 +03:00
logGlobal - > infoStream ( ) < < boost : : format ( " Zone %d filled successfully " ) % id ;
2014-05-22 20:25:17 +03:00
return true ;
}
bool CRmgTemplateZone : : findPlaceForObject ( CMapGenerator * gen , CGObjectInstance * obj , si32 min_dist , int3 & pos )
{
2014-05-31 13:26:37 +03:00
//we need object apperance to deduce free tiles
if ( obj - > appearance . id = = Obj : : NO_OBJ )
{
2014-06-03 22:45:18 +03:00
auto templates = VLC - > objtypeh - > getHandlerFor ( obj - > ID , obj - > subID ) - > getTemplates ( gen - > map - > getTile ( getPos ( ) ) . terType ) ;
2014-05-31 13:26:37 +03:00
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 ( ) ;
}
2014-05-22 20:25:17 +03:00
//si32 min_dist = sqrt(tileinfo.size()/density);
int best_distance = 0 ;
bool result = false ;
si32 w = gen - > map - > width ;
si32 h = gen - > map - > height ;
2014-05-31 15:11:20 +03:00
2014-05-22 20:25:17 +03:00
//logGlobal->infoStream() << boost::format("Min dist for density %f is %d") % density % min_dist;
2014-05-30 22:23:41 +03:00
for ( auto tile : tileinfo )
2014-05-22 20:25:17 +03:00
{
2014-05-31 15:11:20 +03:00
auto ti = gen - > getTile ( tile ) ;
2014-05-22 20:25:17 +03:00
auto dist = ti . getNearestObjectDistance ( ) ;
//avoid borders
2014-05-30 22:23:41 +03:00
if ( ( tile . x < 3 ) | | ( w - tile . x < 3 ) | | ( tile . y < 3 ) | | ( h - tile . y < 3 ) )
2014-05-22 20:25:17 +03:00
continue ;
2014-05-30 22:23:41 +03:00
if ( gen - > isPossible ( tile ) & & ( dist > = min_dist ) & & ( dist > best_distance ) )
2014-05-22 20:25:17 +03:00
{
2014-05-31 11:56:14 +03:00
bool allTilesAvailable = true ;
for ( auto blockingTile : obj - > getBlockedOffsets ( ) )
{
2014-05-31 13:26:37 +03:00
int3 t = tile + blockingTile ;
if ( ! gen - > map - > isInTheMap ( t ) | | ! gen - > isPossible ( t ) )
2014-05-31 11:56:14 +03:00
{
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 ;
}
2014-05-22 20:25:17 +03:00
}
}
2014-05-31 11:56:14 +03:00
if ( result )
{
gen - > setOccupied ( pos , ETileType : : BLOCKED ) ; //block that tile
}
2014-05-22 20:25:17 +03:00
return result ;
}
2014-05-23 18:12:31 +03:00
void CRmgTemplateZone : : checkAndPlaceObject ( CMapGenerator * gen , CGObjectInstance * object , const int3 & pos )
2014-05-22 20:25:17 +03:00
{
2014-05-23 20:14:20 +03:00
if ( ! gen - > map - > isInTheMap ( pos ) )
throw rmgException ( boost : : to_string ( boost : : format ( " Position of object %d at %s is outside the map " ) % object - > id % object - > pos ( ) ) ) ;
2014-05-22 20:25:17 +03:00
object - > pos = pos ;
2014-05-23 18:12:31 +03:00
2014-05-23 20:14:20 +03:00
if ( object - > isVisitable ( ) & & ! gen - > map - > isInTheMap ( object - > visitablePos ( ) ) )
2014-05-23 18:12:31 +03:00
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 ( ) ) ) ;
}
2014-05-31 13:26:37 +03:00
if ( object - > appearance . id = = Obj : : NO_OBJ )
{
2014-06-03 22:45:18 +03:00
auto templates = VLC - > objtypeh - > getHandlerFor ( object - > ID , object - > subID ) - > getTemplates ( gen - > map - > getTile ( pos ) . terType ) ;
2014-05-31 13:26:37 +03:00
if ( templates . empty ( ) )
throw rmgException ( boost : : to_string ( boost : : format ( " Did not find graphics for object (%d,%d) at % s " ) %object->ID %object->subID %pos)) ;
2014-05-23 20:14:20 +03:00
2014-05-31 13:26:37 +03:00
object - > appearance = templates . front ( ) ;
}
2014-05-23 20:14:20 +03:00
gen - > map - > addBlockVisTiles ( object ) ;
2014-05-22 20:25:17 +03:00
gen - > editManager - > insertObject ( object , pos ) ;
2014-05-23 20:14:20 +03:00
logGlobal - > traceStream ( ) < < boost : : format ( " Successfully inserted object (%d,%d) at pos %s " ) % object - > ID % object - > subID % pos ( ) ;
2014-05-23 18:12:31 +03:00
}
void CRmgTemplateZone : : placeObject ( CMapGenerator * gen , CGObjectInstance * object , const int3 & pos )
{
2014-05-24 13:42:06 +03:00
logGlobal - > traceStream ( ) < < boost : : format ( " Inserting object at %d %d " ) % pos . x % pos . y ;
2014-05-23 18:12:31 +03:00
checkAndPlaceObject ( gen , object , pos ) ;
2014-05-22 20:25:17 +03:00
auto points = object - > getBlockedPos ( ) ;
if ( object - > isVisitable ( ) )
2014-05-29 13:42:05 +03:00
points . insert ( pos + object - > getVisitableOffset ( ) ) ;
points . insert ( pos ) ;
2014-06-01 15:10:44 +03:00
for ( auto p : points )
2014-05-22 20:25:17 +03:00
{
2014-06-01 15:10:44 +03:00
if ( gen - > map - > isInTheMap ( p ) )
2014-05-22 20:25:17 +03:00
{
2014-06-01 15:10:44 +03:00
gen - > setOccupied ( p , ETileType : : USED ) ;
2014-05-22 20:25:17 +03:00
}
}
2014-05-30 22:23:41 +03:00
for ( auto tile : tileinfo )
2014-05-22 20:25:17 +03:00
{
2014-05-30 22:23:41 +03:00
si32 d = pos . dist2d ( tile ) ;
gen - > setNearestObjectDistance ( tile , std : : min ( d , gen - > getNearestObjectDistance ( tile ) ) ) ;
2014-05-22 20:25:17 +03:00
}
}
bool CRmgTemplateZone : : guardObject ( CMapGenerator * gen , CGObjectInstance * object , si32 str )
{
2014-05-24 13:42:06 +03:00
logGlobal - > traceStream ( ) < < boost : : format ( " Guard object at %d %d " ) % object - > pos . x % object - > pos . y ;
2014-05-23 20:14:20 +03:00
int3 visitable = object - > visitablePos ( ) ;
2014-05-22 20:25:17 +03:00
std : : vector < int3 > tiles ;
2014-05-30 22:23:41 +03:00
gen - > foreach_neighbour ( visitable , [ & ] ( int3 & pos )
2014-05-22 20:25:17 +03:00
{
2014-05-30 22:23:41 +03:00
logGlobal - > traceStream ( ) < < boost : : format ( " Block at %d %d " ) % pos . x % pos . y ;
if ( gen - > isPossible ( pos ) )
2014-05-22 20:25:17 +03:00
{
2014-05-30 22:23:41 +03:00
tiles . push_back ( pos ) ;
gen - > setOccupied ( pos , ETileType : : BLOCKED ) ;
} ;
} ) ;
2014-05-22 20:25:17 +03:00
if ( ! tiles . size ( ) )
{
logGlobal - > infoStream ( ) < < " Failed " ;
return false ;
}
2014-05-23 20:14:20 +03:00
auto guard_tile = * RandomGeneratorUtil : : nextItem ( tiles , gen - > rand ) ;
2014-05-30 22:23:41 +03:00
gen - > setOccupied ( guard_tile , ETileType : : USED ) ;
2014-05-22 20:25:17 +03:00
2014-06-01 15:10:44 +03:00
addMonster ( gen , guard_tile , str ) ;
2014-05-22 20:25:17 +03:00
return true ;
}