File: StartupBehavior.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 (235 lines) | stat: -rw-r--r-- 6,432 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
/*
* StartupBehavior.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 "StartupBehavior.h"
#include "../AIGateway.h"
#include "../AIUtility.h"
#include "../Goals/BuildThis.h"
#include "../Goals/RecruitHero.h"
#include "../Goals/ExecuteHeroChain.h"
#include "../Goals/ExchangeSwapTownHeroes.h"
#include "../../../lib/mapObjects/CGResource.h"
#include "../Engine/Nullkiller.h"

namespace NKAI
{

using namespace Goals;

std::string StartupBehavior::toString() const
{
	return "Startup";
}

const AIPath getShortestPath(const CGTownInstance * town, const std::vector<AIPath> & paths)
{
	auto shortestPath = *vstd::minElementByFun(paths, [town](const AIPath & path) -> float
	{
		if(town->garrisonHero && path.targetHero == town->garrisonHero.get())
			return 1;

		return path.movementCost();
	});

	return shortestPath;
}

const CGHeroInstance * getNearestHero(const Nullkiller * ai, const CGTownInstance * town)
{
	auto paths = ai->pathfinder->getPathInfo(town->visitablePos());

	if(paths.empty())
		return nullptr;

	auto shortestPath = getShortestPath(town, paths);

	if(shortestPath.nodes.size() > 1
		|| shortestPath.turn() != 0
		|| shortestPath.targetHero->visitablePos().dist2dSQ(town->visitablePos()) > 4
		|| (town->garrisonHero && shortestPath.targetHero == town->garrisonHero.get()))
		return nullptr;

	return shortestPath.targetHero;
}

bool needToRecruitHero(const Nullkiller * ai, const CGTownInstance * startupTown)
{
	if(!ai->heroManager->canRecruitHero(startupTown))
		return false;

	if(!startupTown->garrisonHero && !startupTown->visitingHero)
		return true;

	int treasureSourcesCount = 0;

	for(auto obj : ai->objectClusterizer->getNearbyObjects())
	{
		auto armed = dynamic_cast<const CArmedInstance *>(obj);

		if(armed && armed->getArmyStrength() > 0)
			continue;

		bool isGoldPile = dynamic_cast<const CGResource *>(obj)
			&& dynamic_cast<const CGResource *>(obj)->resourceID() == EGameResID::GOLD;

		auto rewardable = dynamic_cast<const Rewardable::Interface *>(obj);

		if(rewardable)
		{
			for(auto & info : rewardable->configuration.info)
				if(info.reward.resources[EGameResID::GOLD] > 0)
					isGoldPile = true;
		}

		if(isGoldPile
			|| obj->ID == Obj::TREASURE_CHEST
			|| obj->ID == Obj::CAMPFIRE
			|| obj->ID == Obj::WATER_WHEEL)
		{
			treasureSourcesCount++;
		}
	}

	auto basicCount = cb->getTownsInfo().size() + 2;
	auto boost = std::min(
		(int)std::floor(std::pow(1 + (cb->getMapSize().x / 50), 2)),
		treasureSourcesCount / 2);

	logAi->trace("Treasure sources found %d", treasureSourcesCount);
	logAi->trace("Startup allows %d+%d heroes", basicCount, boost);

	return cb->getHeroCount(ai->playerID, true) < basicCount + boost;
}

Goals::TGoalVec StartupBehavior::decompose(const Nullkiller * ai) const
{
	Goals::TGoalVec tasks;
	auto towns = ai->cb->getTownsInfo();

	if(!towns.size())
		return tasks;

	const CGTownInstance * startupTown = towns.front();

	if(towns.size() > 1)
	{
		startupTown = *vstd::maxElementByFun(towns, [ai](const CGTownInstance * town) -> float
		{
			if(town->garrisonHero)
				return ai->heroManager->evaluateHero(town->garrisonHero.get());

			auto closestHero = getNearestHero(ai, town);

			if(closestHero)
				return ai->heroManager->evaluateHero(closestHero);

			return 0;
		});
	}

	if(!startupTown->hasBuilt(BuildingID::TAVERN)
		&& ai->cb->canBuildStructure(startupTown, BuildingID::TAVERN) == EBuildingState::ALLOWED)
	{
		tasks.push_back(Goals::sptr(Goals::BuildThis(BuildingID::TAVERN, startupTown).setpriority(100)));

		return tasks;
	}

	bool canRecruitHero = needToRecruitHero(ai, startupTown);
	auto closestHero = getNearestHero(ai, startupTown);

	if(closestHero)
	{
		if(!startupTown->visitingHero)
		{
			if(ai->armyManager->howManyReinforcementsCanGet(startupTown->getUpperArmy(), startupTown->getUpperArmy(), closestHero) > 200)
			{
				auto paths = ai->pathfinder->getPathInfo(startupTown->visitablePos());

				if(paths.size())
				{
					auto path = getShortestPath(startupTown, paths);

					tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path, startupTown).setpriority(100)));
				}
			}
		}
		else
		{
			auto visitingHero = startupTown->visitingHero.get();
			auto visitingHeroScore = ai->heroManager->evaluateHero(visitingHero);
				
			if(startupTown->garrisonHero)
			{
				auto garrisonHero = startupTown->garrisonHero.get();
				auto garrisonHeroScore = ai->heroManager->evaluateHero(garrisonHero);

				if(visitingHeroScore > garrisonHeroScore
					|| (ai->heroManager->getHeroRole(garrisonHero) == HeroRole::SCOUT && ai->heroManager->getHeroRole(visitingHero) == HeroRole::MAIN))
				{
					if(canRecruitHero || ai->armyManager->howManyReinforcementsCanGet(visitingHero, garrisonHero) > 200)
					{
						tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero, HeroLockedReason::STARTUP).setpriority(100)));
					}
				}
				else if(ai->armyManager->howManyReinforcementsCanGet(garrisonHero, visitingHero) > 200)
				{
					tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, garrisonHero, HeroLockedReason::STARTUP).setpriority(100)));
				}
			}
			else if(canRecruitHero)
			{
				auto canPickTownArmy = startupTown->stacksCount() == 0
					|| ai->armyManager->howManyReinforcementsCanGet(startupTown->visitingHero, startupTown) > 0;

				if(canPickTownArmy)
				{
					tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero, HeroLockedReason::STARTUP).setpriority(100)));
				}
			}
		}
	}

	if(tasks.empty() && canRecruitHero && !startupTown->visitingHero)
	{
		tasks.push_back(Goals::sptr(Goals::RecruitHero(startupTown)));
	}

	if(tasks.empty() && !startupTown->visitingHero)
	{
		for(auto town : towns)
		{
			if(!town->visitingHero && needToRecruitHero(ai, town))
			{
				tasks.push_back(Goals::sptr(Goals::RecruitHero(town)));

				break;
			}
		}
	}

	if(tasks.empty() && towns.size())
	{
		for(const CGTownInstance * town : towns)
		{
			if(town->garrisonHero
				&& town->garrisonHero->movementPointsRemaining()
				&& !town->visitingHero
				&& ai->getHeroLockedReason(town->garrisonHero) != HeroLockedReason::DEFENCE)
			{
				tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(town, nullptr).setpriority(MIN_PRIORITY)));
			}
		}
	}

	return tasks;
}

}