File: ObjectGraphCalculator.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 (387 lines) | stat: -rw-r--r-- 9,683 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* ObjectGraphCalculator.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 "ObjectGraphCalculator.h"
#include "AIPathfinderConfig.h"
#include "../../../lib/CRandomGenerator.h"
#include "../../../CCallback.h"
#include "../../../lib/mapping/CMap.h"
#include "../Engine/Nullkiller.h"
#include "../../../lib/logging/VisualLogger.h"
#include "Actions/QuestAction.h"
#include "../pforeach.h"

namespace NKAI
{

ObjectGraphCalculator::ObjectGraphCalculator(ObjectGraph * target, const Nullkiller * ai)
	:ai(ai), target(target), syncLock()
{
}

void ObjectGraphCalculator::setGraphObjects()
{
	for(auto obj : ai->memory->visitableObjs)
	{
		if(obj && obj->isVisitable() && obj->ID != Obj::HERO && obj->ID != Obj::EVENT)
		{
			addObjectActor(obj);
		}
	}

	for(auto town : ai->cb->getTownsInfo())
	{
		addObjectActor(town);
	}
}

void ObjectGraphCalculator::calculateConnections()
{
	updatePaths();

	std::vector<AIPath> pathCache;

	foreach_tile_pos(ai->cb.get(), [this, &pathCache](const CPlayerSpecificInfoCallback * cb, const int3 & pos)
		{
			calculateConnections(pos, pathCache);
		});

	removeExtraConnections();
}

float ObjectGraphCalculator::getNeighborConnectionsCost(const int3 & pos, std::vector<AIPath> & pathCache)
{
	float neighborCost = std::numeric_limits<float>::max();

	if(NKAI_GRAPH_TRACE_LEVEL >= 2)
	{
		logAi->trace("Checking junction %s", pos.toString());
	}

	foreach_neighbour(
		ai->cb.get(),
		pos,
		[this, &neighborCost, &pathCache](const CPlayerSpecificInfoCallback * cb, const int3 & neighbor)
		{
			ai->pathfinder->calculatePathInfo(pathCache, neighbor);

			auto costTotal = this->getConnectionsCost(pathCache);

			if(costTotal.connectionsCount > 2 && costTotal.avg < neighborCost)
			{
				neighborCost = costTotal.avg;

				if(NKAI_GRAPH_TRACE_LEVEL >= 2)
				{
					logAi->trace("Better node found at %s", neighbor.toString());
				}
			}
		});

	return neighborCost;
}

void ObjectGraphCalculator::addMinimalDistanceJunctions()
{
	tbb::concurrent_unordered_set<int3, std::hash<int3>> junctions;

	pforeachTilePaths(ai->cb->getMapSize(), ai, [this, &junctions](const int3 & pos, std::vector<AIPath> & paths)
		{
			if(target->hasNodeAt(pos))
				return;

			if(ai->cb->getGuardingCreaturePosition(pos).valid())
				return;

			ConnectionCostInfo currentCost = getConnectionsCost(paths);

			if(currentCost.connectionsCount <= 2)
				return;

			float neighborCost = getNeighborConnectionsCost(pos, paths);

			if(currentCost.avg < neighborCost)
			{
				junctions.insert(pos);
			}
		});

	for(auto pos : junctions)
	{
		addJunctionActor(pos);
	}
}

void ObjectGraphCalculator::updatePaths()
{
	PathfinderSettings ps;

	ps.mainTurnDistanceLimit = 5;
	ps.scoutTurnDistanceLimit = 1;
	ps.allowBypassObjects = false;

	ai->pathfinder->updatePaths(actors, ps);
}

void ObjectGraphCalculator::calculateConnections(const int3 & pos, std::vector<AIPath> & pathCache)
{
	if(target->hasNodeAt(pos))
	{
		foreach_neighbour(
			ai->cb.get(),
			pos,
			[this, &pos, &pathCache](const CPlayerSpecificInfoCallback * cb, const int3 & neighbor)
			{
				if(target->hasNodeAt(neighbor))
				{
					ai->pathfinder->calculatePathInfo(pathCache, neighbor);

					for(auto & path : pathCache)
					{
						if(pos == path.targetHero->visitablePos())
						{
							target->tryAddConnection(pos, neighbor, path.movementCost(), path.getTotalDanger());
						}
					}
				}
			});

		auto obj = ai->cb->getTopObj(pos);

		if((obj && obj->ID == Obj::BOAT) || target->isVirtualBoat(pos))
		{
			ai->pathfinder->calculatePathInfo(pathCache, pos);

			for(AIPath & path : pathCache)
			{
				auto from = path.targetHero->visitablePos();
				auto fromObj = actorObjectMap[path.targetHero];

				auto danger = ai->dangerEvaluator->evaluateDanger(pos, path.targetHero, true);
				auto updated = target->tryAddConnection(
					from,
					pos,
					path.movementCost(),
					danger);

				if(NKAI_GRAPH_TRACE_LEVEL >= 2 && updated)
				{
					logAi->trace(
						"Connected %s[%s] -> %s[%s] through [%s], cost %2f",
						fromObj ? fromObj->getObjectName() : "J", from.toString(),
						"Boat", pos.toString(),
						pos.toString(),
						path.movementCost());
				}
			}
		}

		return;
	}

	auto guardPos = ai->cb->getGuardingCreaturePosition(pos);
		
	ai->pathfinder->calculatePathInfo(pathCache, pos);

	for(AIPath & path1 : pathCache)
	{
		for(AIPath & path2 : pathCache)
		{
			if(path1.targetHero == path2.targetHero)
				continue;

			auto pos1 = path1.targetHero->visitablePos();
			auto pos2 = path2.targetHero->visitablePos();

			if(guardPos.valid() && guardPos != pos1 && guardPos != pos2)
				continue;

			auto obj1 = actorObjectMap[path1.targetHero];
			auto obj2 = actorObjectMap[path2.targetHero];

			auto tile1 = cb->getTile(pos1);
			auto tile2 = cb->getTile(pos2);

			if(tile2->isWater() && !tile1->isWater())
			{
				if(!cb->getTile(pos)->isWater())
					continue;

				auto startingObjIsBoat = (obj1 && obj1->ID == Obj::BOAT) || target->isVirtualBoat(pos1);

				if(!startingObjIsBoat)
					continue;
			}

			auto danger = ai->dangerEvaluator->evaluateDanger(pos2, path1.targetHero, true);

			auto updated = target->tryAddConnection(
				pos1,
				pos2,
				path1.movementCost() + path2.movementCost(),
				danger);

			if(NKAI_GRAPH_TRACE_LEVEL >= 2 && updated)
			{
				logAi->trace(
					"Connected %s[%s] -> %s[%s] through [%s], cost %2f",
					obj1 ? obj1->getObjectName() : "J", pos1.toString(),
					obj2 ? obj2->getObjectName() : "J", pos2.toString(),
					pos.toString(),
					path1.movementCost() + path2.movementCost());
			}
		}
	}
}

bool ObjectGraphCalculator::isExtraConnection(float direct, float side1, float side2) const
{
	float sideRatio = (side1 + side2) / direct;

	return sideRatio < 1.25f && direct > side1 && direct > side2;
}

void ObjectGraphCalculator::removeExtraConnections()
{
	std::vector<std::pair<int3, int3>> connectionsToRemove;

	for(auto & actor : temporaryActorHeroes)
	{
		auto pos = actor->visitablePos();
		auto & currentNode = target->getNode(pos);

		target->iterateConnections(pos, [this, &pos, &connectionsToRemove, &currentNode](int3 n1, ObjectLink o1)
			{
				target->iterateConnections(n1, [&pos, &o1, &currentNode, &connectionsToRemove, this](int3 n2, ObjectLink o2)
					{
						auto direct = currentNode.connections.find(n2);

						if(direct != currentNode.connections.end() && isExtraConnection(direct->second.cost, o1.cost, o2.cost))
						{
							connectionsToRemove.push_back({pos, n2});
						}
					});
			});
	}

	vstd::removeDuplicates(connectionsToRemove);

	for(auto & c : connectionsToRemove)
	{
		target->removeConnection(c.first, c.second);

		if(NKAI_GRAPH_TRACE_LEVEL >= 2)
		{
			logAi->trace("Remove ineffective connection %s->%s", c.first.toString(), c.second.toString());
		}
	}
}

void ObjectGraphCalculator::addObjectActor(const CGObjectInstance * obj)
{
	auto objectActor = temporaryActorHeroes.emplace_back(std::make_unique<CGHeroInstance>(obj->cb)).get();

	CRandomGenerator rng;
	auto visitablePos = obj->visitablePos();

	objectActor->setOwner(ai->playerID); // lets avoid having multiple colors
	objectActor->initHero(rng, static_cast<HeroTypeID>(0));
	objectActor->pos = objectActor->convertFromVisitablePos(visitablePos);
	objectActor->initObj(rng);

	if(cb->getTile(visitablePos)->isWater())
	{
		objectActor->boat = temporaryBoats.emplace_back(std::make_unique<CGBoat>(objectActor->cb)).get();
	}

	assert(objectActor->visitablePos() == visitablePos);

	actorObjectMap[objectActor] = obj;
	actors[objectActor] = obj->ID == Obj::TOWN || obj->ID == Obj::BOAT ? HeroRole::MAIN : HeroRole::SCOUT;

	target->addObject(obj);

	auto shipyard = dynamic_cast<const IShipyard *>(obj);
		
	if(shipyard && shipyard->bestLocation().valid())
	{
		int3 virtualBoat = shipyard->bestLocation();
			
		addJunctionActor(virtualBoat, true);
		target->addVirtualBoat(virtualBoat, obj);
	}
}

void ObjectGraphCalculator::addJunctionActor(const int3 & visitablePos, bool isVirtualBoat)
{
	std::lock_guard lock(syncLock);

	auto internalCb = temporaryActorHeroes.front()->cb;
	auto objectActor = temporaryActorHeroes.emplace_back(std::make_unique<CGHeroInstance>(internalCb)).get();

	CRandomGenerator rng;

	objectActor->setOwner(ai->playerID); // lets avoid having multiple colors
	objectActor->initHero(rng, static_cast<HeroTypeID>(0));
	objectActor->pos = objectActor->convertFromVisitablePos(visitablePos);
	objectActor->initObj(rng);

	if(isVirtualBoat || ai->cb->getTile(visitablePos)->isWater())
	{
		objectActor->boat = temporaryBoats.emplace_back(std::make_unique<CGBoat>(objectActor->cb)).get();
	}

	assert(objectActor->visitablePos() == visitablePos);

	actorObjectMap[objectActor] = nullptr;
	actors[objectActor] = isVirtualBoat ? HeroRole::MAIN : HeroRole::SCOUT;

	target->registerJunction(visitablePos);
}

ConnectionCostInfo ObjectGraphCalculator::getConnectionsCost(std::vector<AIPath> & paths) const
{
	std::map<int3, float> costs;

	for(auto & path : paths)
	{
		auto fromPos = path.targetHero->visitablePos();
		auto cost = costs.find(fromPos);
			
		if(cost == costs.end())
		{
			costs.emplace(fromPos, path.movementCost());
		}
		else
		{
			if(path.movementCost() < cost->second)
			{
				costs[fromPos] = path.movementCost();
			}
		}
	}

	ConnectionCostInfo result;

	for(auto & cost : costs)
	{
		result.totalCost += cost.second;
		result.connectionsCount++;
	}

	if(result.connectionsCount)
	{
		result.avg = result.totalCost / result.connectionsCount;
	}

	return result;
}

}