File: GatherTroops.cpp

package info (click to toggle)
vcmi 1.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: cpp: 181,738; sh: 220; python: 178; ansic: 69; objc: 66; xml: 59; makefile: 34
file content (157 lines) | stat: -rw-r--r-- 3,940 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
/*
* GatherTroops.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 "Goals.h"
#include "../VCAI.h"
#include "../AIUtility.h"
#include "../AIhelper.h"
#include "../FuzzyHelper.h"
#include "../ResourceManager.h"
#include "../BuildingManager.h"
#include "../../../lib/mapping/CMap.h" //for victory conditions
#include "../../../lib/CPathfinder.h"
#include "../../../lib/StringConstants.h"


extern boost::thread_specific_ptr<CCallback> cb;
extern boost::thread_specific_ptr<VCAI> ai;
extern FuzzyHelper * fh;

using namespace Goals;

bool GatherTroops::operator==(const GatherTroops & other) const
{
	return objid == other.objid;
}

int GatherTroops::getCreaturesCount(const CArmedInstance * army)
{
	int count = 0;

	for(auto stack : army->Slots())
	{
		if(objid == stack.second->getCreatureID().num)
		{
			count += stack.second->count;
		}
	}

	return count;
}

TSubgoal GatherTroops::whatToDoToAchieve()
{
	logAi->trace("Entering GatherTroops::whatToDoToAchieve");

	auto heroes = cb->getHeroesInfo(true);

	for(auto hero : heroes)
	{
		if(getCreaturesCount(hero) >= this->value)
		{
			logAi->trace("Completing GATHER_TROOPS by hero %s", hero->name);

			throw goalFulfilledException(sptr(*this));
		}
	}

	TGoalVec solutions = getAllPossibleSubgoals();

	if(solutions.empty())
		return sptr(Explore());

	return fh->chooseSolution(solutions);
}


TGoalVec GatherTroops::getAllPossibleSubgoals()
{
	TGoalVec solutions;

	for(const CGTownInstance * t : cb->getTownsInfo())
	{
		int count = getCreaturesCount(t->getUpperArmy());

		if(count >= this->value)
		{
			if(t->visitingHero)
			{
				solutions.push_back(sptr(VisitObj(t->id.getNum()).sethero(t->visitingHero.get())));
			}
			else
			{
				vstd::concatenate(solutions, ai->ah->howToVisitObj(t));
			}

			continue;
		}

		auto creature = VLC->creh->objects[objid];
		if(t->subID == creature->faction) //TODO: how to force AI to build unupgraded creatures? :O
		{
			auto creatures = vstd::tryAt(t->town->creatures, creature->level - 1);
			if(!creatures)
				continue;

			int upgradeNumber = vstd::find_pos(*creatures, creature->idNumber);
			if(upgradeNumber < 0)
				continue;

			BuildingID bid(BuildingID::DWELL_FIRST + creature->level - 1 + upgradeNumber * GameConstants::CREATURES_PER_TOWN);
			if(t->hasBuilt(bid) && ai->ah->freeResources().canAfford(creature->cost)) //this assumes only creatures with dwellings are assigned to faction
			{
				solutions.push_back(sptr(BuyArmy(t, creature->AIValue * this->value).setobjid(objid)));
			}
			/*else //disable random building requests for now - this code needs to know a lot of town/resource context to do more good than harm
			{
				return sptr(BuildThis(bid, t).setpriority(priority));
			}*/
		}
	}

	for(auto obj : ai->visitableObjs)
	{
		auto d = dynamic_cast<const CGDwelling *>(obj);

		if(!d || obj->ID == Obj::TOWN)
			continue;

		for(auto creature : d->creatures)
		{
			if(creature.first) //there are more than 0 creatures avaliabe
			{
				for(auto type : creature.second)
				{
					if(type == objid && ai->ah->freeResources().canAfford(VLC->creh->objects[type]->cost))
						vstd::concatenate(solutions, ai->ah->howToVisitObj(obj));
				}
			}
		}
	}

	CreatureID creID = CreatureID(objid);

	vstd::erase_if(solutions, [&](TSubgoal goal)->bool
	{
		return goal->hero && !goal->hero->getSlotFor(creID).validSlot() && !goal->hero->getFreeSlot().validSlot();
	});

	return solutions;
	//TODO: exchange troops between heroes
}

bool GatherTroops::fulfillsMe(TSubgoal goal)
{
	if (!hero || hero == goal->hero) //we got army for desired hero or any hero
		if (goal->objid == objid) //same creature type //TODO: consider upgrades?
			if (goal->value >= value) //notify every time we get resources?
				return true;
	return false;
}