File: RoadPlacer.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 (259 lines) | stat: -rw-r--r-- 5,802 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * RoadPlacer.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 "RoadPlacer.h"
#include "ObjectManager.h"
#include "ObstaclePlacer.h"
#include "RockFiller.h"
#include "../Functions.h"
#include "../CMapGenerator.h"
#include "../threadpool/MapProxy.h"
#include "../../mapping/CMapEditManager.h"
#include "../../mapObjects/CGObjectInstance.h"
#include "../../modding/IdentifierStorage.h"
#include "../../modding/ModScope.h"
#include "../../TerrainHandler.h"
#include "../../VCMI_Lib.h"

VCMI_LIB_NAMESPACE_BEGIN

class TerrainType;

void RoadPlacer::process()
{
	if(generator.getConfig().defaultRoadType.empty() && generator.getConfig().secondaryRoadType.empty())
		return; //do not generate roads at all
	
	connectRoads();
}

void RoadPlacer::postProcess()
{
	//Draw dirt roads if there are only mines
	drawRoads(noRoadNodes);
}

void RoadPlacer::init()
{
}

rmg::Area & RoadPlacer::areaForRoads()
{
	return areaRoads;
}

rmg::Area & RoadPlacer::areaIsolated()
{
	return isolated;
}

rmg::Area & RoadPlacer::areaVisitable()
{
	return visitableTiles;
}

const rmg::Area & RoadPlacer::getRoads() const
{
	return roads;
}

bool RoadPlacer::createRoad(const int3 & destination)
{
	auto searchArea = zone.areaPossible() + zone.freePaths() + areaRoads + roads;

	rmg::Area border(zone.area()->getBorder());

	rmg::Path path(searchArea);
	path.connect(roads);

	auto simpleRoutig = [this, &border](const int3& src, const int3& dst)
	{
		if(std::abs((src - dst).y) == 1)
		{
			//Do not allow connections straight up through object not visitable from top
			if(areaIsolated().contains(dst) || areaIsolated().contains(src))
			{
				return 1e12f;
			}
		}
		else
		{
			if(areaIsolated().contains(dst))
			{
				//Simply do not route road behind objects that are not visitable from top, such as Monoliths
				return 1e6f;
			}
		}

		float ret = dst.dist2d(src);

		if (visitableTiles.contains(src) || visitableTiles.contains(dst))
		{
			ret *= VISITABLE_PENALTY;
		}
		float dist = border.distanceSqr(dst);
		if(dist > 1.0f)
		{
			ret /= dist;
		}
		return ret;
	};
	
	auto res = path.search(destination, true, simpleRoutig);
	if(!res.valid())
	{
		res = createRoadDesperate(path, destination);
		if (!res.valid())
		{
			logGlobal->warn("Failed to create road to node %s", destination.toString());
			return false;
		}
	}
	roads.unite(res.getPathArea());
	return true;
	
}

rmg::Path RoadPlacer::createRoadDesperate(rmg::Path & path, const int3 & destination)
{
	auto desperateRoutig = [this](const int3& src, const int3& dst) -> float
	{
		//Do not allow connections straight up through object not visitable from top
		if(std::abs((src - dst).y) == 1)
		{
			if(areaIsolated().contains(dst) || areaIsolated().contains(src))
			{
				return 1e12;
			}
		}
		else
		{
			if(areaIsolated().contains(dst))
			{
				return 1e6;
			}
		}

		float weight = dst.dist2dSQ(src);
		auto ret =  weight * weight; // Still prefer straight paths

		if (visitableTiles.contains(src) || visitableTiles.contains(dst))
		{
			ret *= VISITABLE_PENALTY;
		}
		return ret;
	};
	return path.search(destination, false, desperateRoutig);
}

void RoadPlacer::drawRoads(bool secondary)
{	
	//Do not draw roads on underground rock or water
	roads.erase_if([this](const int3& pos) -> bool
	{
		const auto* terrain = map.getTile(pos).getTerrain();
		return !terrain->isPassable() || !terrain->isLand();
	});

	if(!generator.getMapGenOptions().isRoadEnabled())
	{
		return;
	}

	if((secondary && generator.getConfig().secondaryRoadType.empty())
		|| (!secondary && generator.getConfig().defaultRoadType.empty()))
		return;

	//TODO: Allow custom road type for object
	//TODO: Remove these default types

	auto tiles = roads.getTilesVector();

	std::string roadName = (secondary ? generator.getConfig().secondaryRoadType : generator.getConfig().defaultRoadType);
	RoadId roadType(*VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "road", roadName));

	//If our road type is not enabled, choose highest below it
	for (int8_t bestRoad = roadType.getNum(); bestRoad > RoadId(Road::NO_ROAD).getNum(); bestRoad--)
	{
		if(generator.getMapGenOptions().isRoadEnabled(RoadId(bestRoad)))
		{
			mapProxy->drawRoads(zone.getRand(), tiles, RoadId(bestRoad));
			return;
		}
	}
}

void RoadPlacer::addRoadNode(const int3& node)
{
	RecursiveLock lock(externalAccessMutex);
	roadNodes.insert(node);
}

void RoadPlacer::connectRoads()
{
	//Assumes objects are already placed
	if(roadNodes.size() < 2)
	{
		//If there are no nodes, draw roads to mines
		noRoadNodes = true;
		if(auto* m = zone.getModificator<ObjectManager>())
		{
			for(auto * object : m->getMines())
			{
				addRoadNode(object->visitablePos());
			}
		}
	}

	if(roadNodes.size() < 2)
		return;
	
	//take any tile from road nodes as destination zone for all other road nodes
	RecursiveLock lock(externalAccessMutex);
	if(roads.empty())
		roads.add(*roadNodes.begin());

	for(const auto & node : roadNodes)
	{
		try
		{
			createRoad(node);
		}
		catch (const rmgException& e)
		{
			logGlobal->warn("Handled exception while drawing road to node %s: %s", node.toString(), e.what());
		}
		catch (const std::exception & e)
		{
			logGlobal->error("Unhandled exception while drawing road to node %s: %s", node.toString(), e.what());
			throw;
		}
	}
	
	if (!zone.isUnderground())
	{
		// Otherwise roads will be drawn only after rock is placed
		postProcess();
	}
}

char RoadPlacer::dump(const int3 & t)
{
	if(vstd::contains(roadNodes, t))
		return '@';
	if(roads.contains(t))
		return '+';
	if(isolated.contains(t))
		return 'i';
	return Modificator::dump(t);
}

VCMI_LIB_NAMESPACE_END