2022-08-09 07:54:32 +02:00
|
|
|
/*
|
|
|
|
* RiverPlacer.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 "RiverPlacer.h"
|
2023-05-20 10:17:37 +02:00
|
|
|
#include "../Functions.h"
|
|
|
|
#include "../CMapGenerator.h"
|
|
|
|
#include "../RmgMap.h"
|
|
|
|
#include "../../RiverHandler.h"
|
|
|
|
#include "../../TerrainHandler.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../../mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
|
#include "../../mapObjectConstructors/CObjectClassesHandler.h"
|
|
|
|
#include "../../mapObjects/ObjectTemplate.h"
|
2023-05-20 10:17:37 +02:00
|
|
|
#include "../../mapping/CMap.h"
|
|
|
|
#include "../../mapping/CMapEditManager.h"
|
2024-05-17 00:05:51 +02:00
|
|
|
#include "../../VCMI_Lib.h"
|
2023-05-20 10:17:37 +02:00
|
|
|
#include "../RmgPath.h"
|
2022-08-09 07:54:32 +02:00
|
|
|
#include "ObjectManager.h"
|
|
|
|
#include "ObstaclePlacer.h"
|
|
|
|
#include "WaterProxy.h"
|
|
|
|
#include "RoadPlacer.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
const int RIVER_DELTA_ID = 143;
|
|
|
|
const int RIVER_DELTA_SUBTYPE = 0;
|
|
|
|
|
|
|
|
const std::array<std::array<int, 25>, 4> deltaTemplates
|
|
|
|
{
|
|
|
|
//0 - must be on ground
|
|
|
|
//1 - delta entry
|
|
|
|
//2 - must be on water
|
|
|
|
//3 - anything
|
|
|
|
//4 - prohibit river placement
|
|
|
|
//5 - must be on ground + position
|
|
|
|
//6 - must be on water + position
|
|
|
|
std::array<int, 25>{
|
|
|
|
3, 4, 3, 4, 3,
|
|
|
|
3, 4, 1, 4, 3,
|
|
|
|
3, 0, 0, 0, 3,
|
|
|
|
3, 0, 0, 0, 3,
|
|
|
|
3, 2, 2, 6, 3
|
|
|
|
},
|
|
|
|
std::array<int, 25>{
|
|
|
|
3, 2, 2, 2, 3,
|
|
|
|
3, 0, 0, 0, 3,
|
|
|
|
3, 0, 0, 5, 3,
|
|
|
|
3, 4, 1, 4, 3,
|
|
|
|
3, 4, 3, 4, 3
|
|
|
|
},
|
|
|
|
std::array<int, 25>{
|
|
|
|
3, 3, 3, 3, 3,
|
|
|
|
4, 4, 0, 0, 2,
|
|
|
|
3, 1, 0, 0, 2,
|
|
|
|
4, 4, 0, 0, 6,
|
|
|
|
3, 3, 3, 3, 3
|
|
|
|
},
|
|
|
|
std::array<int, 25> {
|
|
|
|
3, 3, 3, 3, 3,
|
|
|
|
2, 0, 0, 4, 4,
|
|
|
|
2, 0, 0, 1, 3,
|
|
|
|
2, 0, 5, 4, 4,
|
|
|
|
3, 3, 3, 3, 3
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void RiverPlacer::process()
|
|
|
|
{
|
|
|
|
preprocess();
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & t : riverNodes)
|
2022-08-09 07:54:32 +02:00
|
|
|
connectRiver(t);
|
|
|
|
|
|
|
|
if(!rivers.empty())
|
|
|
|
drawRivers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RiverPlacer::init()
|
|
|
|
{
|
2023-06-10 14:58:12 +02:00
|
|
|
if (!zone.isUnderground())
|
|
|
|
{
|
|
|
|
DEPENDENCY_ALL(WaterProxy);
|
|
|
|
}
|
2022-08-09 07:54:32 +02:00
|
|
|
DEPENDENCY(ObjectManager);
|
|
|
|
DEPENDENCY(ObstaclePlacer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RiverPlacer::drawRivers()
|
|
|
|
{
|
2023-05-19 20:30:15 +02:00
|
|
|
auto tiles = rivers.getTilesVector();
|
2023-05-20 11:46:32 +02:00
|
|
|
mapProxy->drawRivers(zone.getRand(), tiles, zone.getTerrainType());
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char RiverPlacer::dump(const int3 & t)
|
|
|
|
{
|
|
|
|
if(riverNodes.count(t))
|
|
|
|
return '@';
|
|
|
|
if(rivers.contains(t))
|
|
|
|
return '~';
|
|
|
|
if(sink.contains(t))
|
|
|
|
return '2';
|
|
|
|
if(source.contains(t))
|
|
|
|
return '1';
|
2024-03-27 07:16:48 +02:00
|
|
|
if(zone.area()->contains(t))
|
2022-08-09 07:54:32 +02:00
|
|
|
return ' ';
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
void RiverPlacer::addRiverNode(const int3 & node)
|
|
|
|
{
|
2024-03-27 07:16:48 +02:00
|
|
|
assert(zone.area()->contains(node));
|
2022-08-09 07:54:32 +02:00
|
|
|
riverNodes.insert(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
rmg::Area & RiverPlacer::riverSource()
|
|
|
|
{
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
rmg::Area & RiverPlacer::riverSink()
|
|
|
|
{
|
|
|
|
return sink;
|
|
|
|
}
|
|
|
|
|
|
|
|
rmg::Area & RiverPlacer::riverProhibit()
|
|
|
|
{
|
|
|
|
return prohibit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RiverPlacer::prepareHeightmap()
|
|
|
|
{
|
|
|
|
rmg::Area roads;
|
|
|
|
if(auto * m = zone.getModificator<RoadPlacer>())
|
|
|
|
{
|
|
|
|
roads.unite(m->getRoads());
|
|
|
|
}
|
2023-02-11 18:05:02 +02:00
|
|
|
|
2024-03-27 07:16:48 +02:00
|
|
|
auto area = zone.area();
|
|
|
|
auto areaUsed = zone.areaUsed();
|
|
|
|
|
|
|
|
for(const auto & t : area->getTilesVector())
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2023-05-20 11:46:32 +02:00
|
|
|
heightMap[t] = zone.getRand().nextInt(5);
|
2022-08-09 07:54:32 +02:00
|
|
|
|
|
|
|
if(roads.contains(t))
|
|
|
|
heightMap[t] += 30.f;
|
|
|
|
|
2024-03-27 07:16:48 +02:00
|
|
|
if(areaUsed->contains(t))
|
2022-08-09 07:54:32 +02:00
|
|
|
heightMap[t] += 1000.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
//make grid
|
2023-05-19 20:30:15 +02:00
|
|
|
for(int j = 0; j < map.height(); j += 2)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2023-05-19 20:30:15 +02:00
|
|
|
for(int i = 0; i < map.width(); i += 2)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
int3 t{i, j, zone.getPos().z};
|
2024-03-27 07:16:48 +02:00
|
|
|
if(area->contains(t))
|
2022-08-09 07:54:32 +02:00
|
|
|
heightMap[t] += 10.f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RiverPlacer::preprocess()
|
|
|
|
{
|
|
|
|
rmg::Area outOfMapTiles;
|
|
|
|
std::map<TRmgTemplateZoneId, rmg::Area> neighbourZonesTiles;
|
2024-03-27 07:16:48 +02:00
|
|
|
|
|
|
|
rmg::Area borderArea(zone.area()->getBorder());
|
2022-08-09 07:54:32 +02:00
|
|
|
TRmgTemplateZoneId connectedToWaterZoneId = -1;
|
2024-03-27 07:16:48 +02:00
|
|
|
for(const auto & t : zone.area()->getBorderOutside())
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
if(!map.isOnMap(t))
|
|
|
|
{
|
|
|
|
outOfMapTiles.add(t);
|
|
|
|
}
|
|
|
|
else if(map.getZoneID(t) != zone.getId())
|
|
|
|
{
|
|
|
|
if(map.getZones()[map.getZoneID(t)]->getType() == ETemplateZoneType::WATER)
|
|
|
|
connectedToWaterZoneId = map.getZoneID(t);
|
|
|
|
neighbourZonesTiles[map.getZoneID(t)].add(t);
|
|
|
|
}
|
|
|
|
}
|
2024-03-27 07:16:48 +02:00
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
rmg::Area outOfMapInternal(outOfMapTiles.getBorderOutside());
|
|
|
|
outOfMapInternal.intersect(borderArea);
|
|
|
|
|
|
|
|
//looking outside map
|
|
|
|
if(!outOfMapInternal.empty())
|
|
|
|
{
|
2023-05-20 11:46:32 +02:00
|
|
|
auto elem = *RandomGeneratorUtil::nextItem(outOfMapInternal.getTilesVector(), zone.getRand());
|
2022-08-09 07:54:32 +02:00
|
|
|
source.add(elem);
|
|
|
|
outOfMapInternal.erase(elem);
|
|
|
|
}
|
|
|
|
if(!outOfMapInternal.empty())
|
|
|
|
{
|
2023-05-20 11:46:32 +02:00
|
|
|
auto elem = *RandomGeneratorUtil::nextItem(outOfMapInternal.getTilesVector(), zone.getRand());
|
2022-08-09 07:54:32 +02:00
|
|
|
sink.add(elem);
|
|
|
|
outOfMapInternal.erase(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
//calculate delta positions
|
|
|
|
if(connectedToWaterZoneId > -1)
|
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
auto river = VLC->terrainTypeHandler->getById(zone.getTerrainType())->river;
|
2022-08-09 07:54:32 +02:00
|
|
|
auto & a = neighbourZonesTiles[connectedToWaterZoneId];
|
|
|
|
auto availableArea = zone.areaPossible() + zone.freePaths();
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & tileToProcess : availableArea.getTilesVector())
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
int templateId = -1;
|
|
|
|
for(int tId = 0; tId < 4; ++tId)
|
|
|
|
{
|
|
|
|
templateId = tId;
|
|
|
|
for(int i = 0; i < 25; ++i)
|
|
|
|
{
|
|
|
|
if((deltaTemplates[tId][i] == 2 || deltaTemplates[tId][i] == 6) && !a.contains(tileToProcess + int3(i % 5 - 2, i / 5 - 2, 0)))
|
|
|
|
{
|
|
|
|
templateId = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if((deltaTemplates[tId][i] < 2 || deltaTemplates[tId][i] == 5) && !availableArea.contains(tileToProcess + int3(i % 5 - 2, i / 5 - 2, 0)))
|
|
|
|
{
|
|
|
|
templateId = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(templateId > -1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(templateId > -1)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < 25; ++i)
|
|
|
|
{
|
|
|
|
auto p = tileToProcess + int3(i % 5 - 2, i / 5 - 2, 0);
|
|
|
|
if(deltaTemplates[templateId][i] == 1)
|
|
|
|
{
|
|
|
|
sink.add(p);
|
|
|
|
deltaSink.add(p);
|
|
|
|
deltaOrientations[p] = templateId + 1;
|
|
|
|
|
|
|
|
//specific case: deltas for ice rivers amd mud rivers are messed :(
|
2022-09-23 16:24:01 +02:00
|
|
|
if(river == River::ICY_RIVER)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
switch(deltaOrientations[p])
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
deltaOrientations[p] = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
deltaOrientations[p] = 3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
deltaOrientations[p] = 4;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
deltaOrientations[p] = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 16:24:01 +02:00
|
|
|
if(river == River::MUD_RIVER)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
switch(deltaOrientations[p])
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
deltaOrientations[p] = 4;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
deltaOrientations[p] = 3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
deltaOrientations[p] = 1;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
deltaOrientations[p] = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto j = 0; j < 25; ++j)
|
|
|
|
{
|
|
|
|
if(deltaTemplates[templateId][j] >= 5)
|
|
|
|
{
|
|
|
|
deltaPositions[p] = tileToProcess + int3(j % 5 - 2, j / 5 - 2, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(deltaTemplates[templateId][i] == 0 || deltaTemplates[templateId][i] == 4 || deltaTemplates[templateId][i] == 5)
|
|
|
|
{
|
|
|
|
prohibit.add(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareHeightmap();
|
|
|
|
|
|
|
|
//decorative river
|
2024-03-27 07:16:48 +02:00
|
|
|
if(!sink.empty() && !source.empty() && riverNodes.empty() && !zone.areaPossible()->empty())
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2023-05-20 11:46:32 +02:00
|
|
|
addRiverNode(*RandomGeneratorUtil::nextItem(source.getTilesVector(), zone.getRand()));
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(source.empty())
|
|
|
|
{
|
|
|
|
logGlobal->info("River source is empty!");
|
|
|
|
|
|
|
|
//looking outside map
|
|
|
|
|
|
|
|
for(auto & i : heightMap)
|
|
|
|
{
|
|
|
|
if(i.second > 0)
|
|
|
|
source.add(i.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sink.empty())
|
|
|
|
{
|
|
|
|
logGlobal->error("River sink is empty!");
|
|
|
|
for(auto & i : heightMap)
|
|
|
|
{
|
|
|
|
if(i.second <= 0)
|
|
|
|
sink.add(i.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RiverPlacer::connectRiver(const int3 & tile)
|
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
auto riverType = VLC->terrainTypeHandler->getById(zone.getTerrainType())->river;
|
2022-12-20 18:35:40 +02:00
|
|
|
const auto * river = VLC->riverTypeHandler->getById(riverType);
|
2023-01-01 17:10:47 +02:00
|
|
|
if(river->getId() == River::NO_RIVER)
|
2022-08-09 07:54:32 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
rmg::Area roads;
|
|
|
|
if(auto * m = zone.getModificator<RoadPlacer>())
|
|
|
|
{
|
|
|
|
roads.unite(m->getRoads());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto movementCost = [this, &roads](const int3 & s, const int3 & d)
|
|
|
|
{
|
|
|
|
float cost = heightMap[d];
|
|
|
|
if(roads.contains(s))
|
|
|
|
cost += 1000.f; //allow road intersection, but avoid long overlaps
|
|
|
|
return cost;
|
|
|
|
};
|
|
|
|
|
2024-03-27 07:16:48 +02:00
|
|
|
auto availableArea = zone.area().get() - prohibit;
|
2022-08-09 07:54:32 +02:00
|
|
|
|
|
|
|
rmg::Path pathToSource(availableArea);
|
|
|
|
pathToSource.connect(source);
|
|
|
|
pathToSource.connect(rivers);
|
|
|
|
pathToSource = pathToSource.search(tile, true, movementCost);
|
|
|
|
|
|
|
|
availableArea.subtract(pathToSource.getPathArea());
|
|
|
|
|
|
|
|
rmg::Path pathToSink(availableArea);
|
|
|
|
pathToSink.connect(sink);
|
|
|
|
pathToSource.connect(rivers);
|
|
|
|
pathToSink = pathToSink.search(tile, true, movementCost);
|
|
|
|
|
|
|
|
if(pathToSource.getPathArea().empty() || pathToSink.getPathArea().empty())
|
|
|
|
{
|
|
|
|
logGlobal->error("Cannot build river");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//delta placement
|
|
|
|
auto deltaPos = pathToSink.getPathArea() * deltaSink;
|
|
|
|
if(!deltaPos.empty())
|
|
|
|
{
|
|
|
|
assert(deltaPos.getTilesVector().size() == 1);
|
|
|
|
|
|
|
|
auto pos = deltaPos.getTilesVector().front();
|
|
|
|
auto handler = VLC->objtypeh->getHandlerFor(RIVER_DELTA_ID, RIVER_DELTA_SUBTYPE);
|
|
|
|
assert(handler->isStaticObject());
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
std::vector<std::shared_ptr<const ObjectTemplate>> tmplates;
|
2022-08-09 07:54:32 +02:00
|
|
|
for(auto & temp : handler->getTemplates())
|
|
|
|
{
|
2022-09-11 15:12:35 +02:00
|
|
|
if(temp->canBePlacedAt(zone.getTerrainType()))
|
2022-08-09 07:54:32 +02:00
|
|
|
tmplates.push_back(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tmplates.size() > 3)
|
|
|
|
{
|
|
|
|
if(tmplates.size() % 4 != 0)
|
2023-08-20 22:45:41 +02:00
|
|
|
throw rmgException(boost::str(boost::format("River templates for (%d,%d) at terrain %s, river %s are incorrect") %
|
2022-12-21 00:45:35 +02:00
|
|
|
RIVER_DELTA_ID % RIVER_DELTA_SUBTYPE % zone.getTerrainType() % river->shortIdentifier));
|
2022-08-09 07:54:32 +02:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
AnimationPath targetTemplateName = AnimationPath::builtinTODO(river->deltaName + std::to_string(deltaOrientations[pos]) + ".def");
|
2022-08-09 07:54:32 +02:00
|
|
|
for(auto & templ : tmplates)
|
|
|
|
{
|
2022-09-11 15:12:35 +02:00
|
|
|
if(templ->animationFile == targetTemplateName)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2024-01-01 16:37:48 +02:00
|
|
|
auto * obj = handler->create(map.mapInstance->cb, templ);
|
2022-08-09 07:54:32 +02:00
|
|
|
rmg::Object deltaObj(*obj, deltaPositions[pos]);
|
2023-09-30 01:19:18 +02:00
|
|
|
deltaObj.finalize(map, zone.getRand());
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rivers.unite(pathToSource.getPathArea());
|
|
|
|
rivers.unite(pathToSink.getPathArea());
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|