File: AbstractGoal.h

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 (220 lines) | stat: -rw-r--r-- 4,716 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
/*
* AbstractGoal.h, 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
*
*/
#pragma once

#include "../../../lib/VCMI_Lib.h"
#include "../../../lib/mapObjects/CGTownInstance.h"
#include "../../../lib/mapObjects/CGHeroInstance.h"
#include "../AIUtility.h"

namespace NKAI
{

struct HeroPtr;
class AIGateway;
class FuzzyHelper;
class Nullkiller;

namespace Goals
{
	class AbstractGoal;
	class ITask;
	class RecruitHero;
	class BuildThis;
	class DigAtTile;
	class CollectRes;
	class BuyArmy;
	class BuildBoat;
	class Invalid;
	class Trade;
	class AdventureSpellCast;

	enum EGoals
	{
		INVALID = -1,
		WIN, CONQUER,
		BUILD,
		EXPLORE, GATHER_ARMY,
		BOOST_HERO,
		RECRUIT_HERO,
		RECRUIT_HERO_BEHAVIOR,
		BUILD_STRUCTURE, //if hero set, then in visited town
		COLLECT_RES,
		GATHER_TROOPS, // val of creatures with objid

		CAPTURE_OBJECTS,

		GET_ART_TYPE,

		DEFENCE,
		STARTUP,
		DIG_AT_TILE,//elementar with hero on tile
		BUY_ARMY, //at specific town
		TRADE, //val resID at object objid
		BUILD_BOAT,
		COMPLETE_QUEST,
		ADVENTURE_SPELL_CAST,
		EXECUTE_HERO_CHAIN,
		EXCHANGE_SWAP_TOWN_HEROES,
		DISMISS_HERO,
		COMPOSITION,
		CLUSTER_BEHAVIOR,
		UNLOCK_CLUSTER,
		HERO_EXCHANGE,
		ARMY_UPGRADE,
		DEFEND_TOWN,
		CAPTURE_OBJECT,
		SAVE_RESOURCES,
		STAY_AT_TOWN_BEHAVIOR,
		STAY_AT_TOWN,
		EXPLORATION_BEHAVIOR,
		EXPLORATION_POINT,
		EXPLORE_NEIGHBOUR_TILE
	};

	class DLL_EXPORT TSubgoal : public std::shared_ptr<AbstractGoal>
	{
	public:
		bool operator==(const TSubgoal & rhs) const;
		bool operator<(const TSubgoal & rhs) const;
	};

	using TTask = std::shared_ptr<ITask>;
	using TTaskVec = std::vector<TTask>;
	using TGoalVec = std::vector<TSubgoal>;

	//method chaining + clone pattern
#define SETTER(type, field) AbstractGoal & set ## field(const type &rhs) {field = rhs; return *this;};

	enum { LOW_PR = -1 };

	DLL_EXPORT TSubgoal sptr(const AbstractGoal & tmp);
	DLL_EXPORT TTask taskptr(const AbstractGoal & tmp);
	
	class DLL_EXPORT AbstractGoal
	{
	public:
		bool isAbstract; SETTER(bool, isAbstract)
		int value; SETTER(int, value)
		ui64 goldCost; SETTER(ui64, goldCost)
		TResources buildingCost; SETTER(TResources, buildingCost)
		int resID; SETTER(int, resID)
		int objid; SETTER(int, objid)
		int aid; SETTER(int, aid)
		int3 tile; SETTER(int3, tile)
		const CGHeroInstance * hero; SETTER(CGHeroInstance *, hero)
		const CGTownInstance *town; SETTER(CGTownInstance *, town)
		int bid; SETTER(int, bid)

		AbstractGoal(EGoals goal = EGoals::INVALID): goalType(goal)
		{
			isAbstract = false;
			value = 0;
			aid = -1;
			resID = -1;
			objid = -1;
			tile = int3(-1, -1, -1);
			town = nullptr;
			hero = nullptr;
			bid = -1;
			goldCost = 0;
		}
		virtual ~AbstractGoal() {}
		//FIXME: abstract goal should be abstract, but serializer fails to instantiate subgoals in such case
		virtual AbstractGoal * clone() const
		{
			return const_cast<AbstractGoal *>(this);
		}

		virtual TGoalVec decompose(const Nullkiller * ai) const
		{
			return TGoalVec();
		}

		EGoals goalType;

		virtual std::string toString() const;

		bool invalid() const;
		
		virtual bool operator==(const AbstractGoal & g) const;

		virtual bool isElementar() const { return false; }

		virtual bool hasHash() const { return false; }

		virtual uint64_t getHash() const { return 0; }

		virtual ITask * asTask()
		{
			throw std::runtime_error("Abstract goal is not a task");
		}
		
		bool operator!=(const AbstractGoal & g) const
		{
			return !(*this == g);
		}
	};

	class DLL_EXPORT ITask
	{
	public:
		float priority;

		ITask() : priority(0) {}

		///Visitor pattern
		//TODO: make accept work for std::shared_ptr... somehow
		virtual void accept(AIGateway * ai) = 0; //unhandled goal will report standard error
		virtual std::string toString() const = 0;
		virtual const CGHeroInstance * getHero() const = 0;
		virtual ~ITask() {}
		virtual int getHeroExchangeCount() const = 0;
		virtual bool isObjectAffected(ObjectInstanceID h) const = 0;
		virtual std::vector<ObjectInstanceID> getAffectedObjects() const = 0;
	};
}

class cannotFulfillGoalException : public std::exception
{
	std::string msg;

public:
	explicit cannotFulfillGoalException(const std::string  & message)
		: msg(message)
	{
	}

	const char * what() const noexcept override
	{
		return msg.c_str();
	}
};

class goalFulfilledException : public std::exception
{
	std::string msg;

public:
	Goals::TSubgoal goal;

	explicit goalFulfilledException(Goals::TSubgoal Goal)
		: goal(Goal)
	{
		msg = goal->toString();
	}

	const char * what() const noexcept override
	{
		return msg.c_str();
	}
};

}