File: ObstaclePlacer.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (175 lines) | stat: -rw-r--r-- 4,209 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * ObstaclePlacer.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 "ObstaclePlacer.h"
#include "ObjectManager.h"
#include "TreasurePlacer.h"
#include "RockFiller.h"
#include "WaterRoutes.h"
#include "WaterProxy.h"
#include "RoadPlacer.h"
#include "RiverPlacer.h"
#include "../RmgMap.h"
#include "../CMapGenerator.h"
#include "../Functions.h"
#include "../../entities/faction/CFaction.h"
#include "../../mapping/CMapEditManager.h"
#include "../../mapping/CMap.h"
#include "../../mapping/ObstacleProxy.h"
#include "../../mapObjects/CGObjectInstance.h"
#include "../../mapObjects/ObstacleSetHandler.h"

VCMI_LIB_NAMESPACE_BEGIN

void ObstaclePlacer::process()
{
	manager = zone.getModificator<ObjectManager>();
	if(!manager)
		return;

	auto faction = zone.getTownType().toFaction();

	ObstacleSetFilter filter(ObstacleSet::EObstacleType::INVALID,
							zone.getTerrainType(),
							static_cast<EMapLevel>(zone.isUnderground()),
							faction->getId(),
							faction->alignment);

	if (!prepareBiome(filter, zone.getRand()))
	{
		logGlobal->warn("Failed to prepare biome, using all possible obstacles");
		// Use all if we fail to create proper biome
		collectPossibleObstacles(zone.getTerrainType());
	}
	else
	{
		logGlobal->info("Biome prepared successfully for zone %d", zone.getId());
	}
	
	{
		auto area = zone.area();
		auto areaPossible = zone.areaPossible();
		auto areaUsed = zone.areaUsed();

		blockedArea = area->getSubarea([this](const int3& t)
		{
			return map.shouldBeBlocked(t);
		});
		blockedArea.subtract(*areaUsed);
		areaPossible->subtract(blockedArea);

		prohibitedArea = zone.freePaths() + areaUsed + manager->getVisitableArea();
		if (auto * rp = zone.getModificator<RoadPlacer>())
		{
			prohibitedArea.unite(rp->getRoads());
		}

		//Progressively block tiles, but make sure they don't seal any gap between blocks
		rmg::Area toBlock;
		do
		{
			toBlock.clear();
			for (const auto& tile : areaPossible->getTilesVector())
			{
				rmg::Area neighbors;
				rmg::Area t;
				t.add(tile);

				for (const auto& n : t.getBorderOutside())
				{
					//Area outside the map is also impassable
					if (!map.isOnMap(n) || map.shouldBeBlocked(n))
					{
						neighbors.add(n);
					}
				}
				if (neighbors.empty())
				{
					continue;
				}
				//Will only be added if it doesn't connect two disjointed blocks
				if (neighbors.connected(true)) //Do not block diagonal pass
				{
					toBlock.add(tile);
				}
			}
			areaPossible->subtract(toBlock);
			for (const auto& tile : toBlock.getTilesVector())
			{
				map.setOccupied(tile, ETileType::BLOCKED);
			}

		} while (!toBlock.empty());

		prohibitedArea.unite(areaPossible.get());
	}

	auto objs = createObstacles(zone.getRand(), map.mapInstance->cb);
	mapProxy->insertObjects(objs);
}

void ObstaclePlacer::init()
{
	DEPENDENCY(ObjectManager);
	DEPENDENCY(TreasurePlacer);
	DEPENDENCY(RoadPlacer);
	if (zone.isUnderground())
	{
		DEPENDENCY_ALL(RockFiller);
	}
	else
	{
		DEPENDENCY(WaterRoutes);
		DEPENDENCY(WaterProxy);
	}
}

bool ObstaclePlacer::isInTheMap(const int3& tile)
{
	return map.isOnMap(tile);
}

void ObstaclePlacer::placeObject(rmg::Object & object, std::set<CGObjectInstance*> &)
{
	manager->placeObject(object, false, false);
}

std::pair<bool, bool> ObstaclePlacer::verifyCoverage(const int3 & t) const
{
	return {map.shouldBeBlocked(t), zone.areaPossible()->contains(t)};
}

void ObstaclePlacer::postProcess(const rmg::Object & object)
{
	//river processing
	riverManager = zone.getModificator<RiverPlacer>();
	if(riverManager)
	{
		const auto objTypeName = object.instances().front()->object().getTypeName();
		if(objTypeName == "mountain")
			riverManager->riverSource().unite(object.getArea());
		else if(objTypeName == "lake")
			riverManager->riverSink().unite(object.getArea());
	}
}

bool ObstaclePlacer::isProhibited(const rmg::Area & objArea) const
{
	if(prohibitedArea.overlap(objArea))
		return true;
	 
	if(!zone.area()->contains(objArea))
		return true;
	
	return false;
}

VCMI_LIB_NAMESPACE_END