1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00
vcmi/AI/Nullkiller/Goals/AbstractGoal.h

210 lines
4.2 KiB
C
Raw Normal View History

2021-05-15 18:22:44 +02:00
/*
* 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/CBuildingHandler.h"
#include "../../../lib/CCreatureHandler.h"
#include "../../../lib/CTownHandler.h"
#include "../AIUtility.h"
struct HeroPtr;
2021-05-16 14:39:38 +02:00
class AIGateway;
2021-05-15 18:22:44 +02:00
class FuzzyHelper;
namespace Goals
{
class AbstractGoal;
class ITask;
2021-05-15 18:22:44 +02:00
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,
2021-05-15 18:22:44 +02:00
EXPLORE, GATHER_ARMY,
BOOST_HERO,
RECRUIT_HERO,
2021-05-16 13:38:53 +02:00
RECRUIT_HERO_BEHAVIOR,
2021-05-15 18:22:44 +02:00
BUILD_STRUCTURE, //if hero set, then in visited town
COLLECT_RES,
GATHER_TROOPS, // val of creatures with objid
2021-05-16 13:38:53 +02:00
CAPTURE_OBJECTS,
2021-05-15 18:22:44 +02:00
GET_ART_TYPE,
2021-05-16 13:38:53 +02:00
DEFENCE,
STARTUP,
2021-05-15 18:22:44 +02:00
DIG_AT_TILE,//elementar with hero on tile
BUY_ARMY, //at specific town
TRADE, //val resID at object objid
BUILD_BOAT,
COMPLETE_QUEST,
2021-05-15 20:56:31 +02:00
ADVENTURE_SPELL_CAST,
2021-05-15 21:02:52 +02:00
EXECUTE_HERO_CHAIN,
EXCHANGE_SWAP_TOWN_HEROES,
2021-05-16 13:38:53 +02:00
DISMISS_HERO,
2021-05-16 13:45:12 +02:00
COMPOSITION,
CLUSTER_BEHAVIOR,
UNLOCK_CLUSTER,
HERO_EXCHANGE,
2021-05-16 14:00:24 +02:00
ARMY_UPGRADE,
DEFEND_TOWN,
2021-05-16 14:39:38 +02:00
CAPTURE_OBJECT,
SAVE_RESOURCES
2021-05-15 18:22:44 +02:00
};
class DLL_EXPORT TSubgoal : public std::shared_ptr<AbstractGoal>
{
public:
bool operator==(const TSubgoal & rhs) const;
bool operator<(const TSubgoal & rhs) const;
};
typedef std::shared_ptr<ITask> TTask;
typedef std::vector<TTask> TTaskVec;
2021-05-15 18:22:44 +02:00
typedef std::vector<TSubgoal> TGoalVec;
//method chaining + clone pattern
2021-05-16 14:10:35 +02:00
#define SETTER(type, field) AbstractGoal & set ## field(const type &rhs) {field = rhs; return *this;};
2021-05-15 18:22:44 +02:00
enum { LOW_PR = -1 };
DLL_EXPORT TSubgoal sptr(const AbstractGoal & tmp);
DLL_EXPORT TTask taskptr(const AbstractGoal & tmp);
2021-05-16 13:38:53 +02:00
2021-05-15 18:22:44 +02:00
class DLL_EXPORT AbstractGoal
{
public:
2021-05-16 14:10:35 +02:00
bool isAbstract; SETTER(bool, isAbstract)
int value; SETTER(int, value)
ui64 goldCost; SETTER(ui64, goldCost)
int resID; SETTER(int, resID)
int objid; SETTER(int, objid)
int aid; SETTER(int, aid)
int3 tile; SETTER(int3, tile)
HeroPtr hero; SETTER(HeroPtr, hero)
const CGTownInstance *town; SETTER(CGTownInstance *, town)
int bid; SETTER(int, bid)
AbstractGoal(EGoals goal = EGoals::INVALID)
2021-05-16 13:38:53 +02:00
: goalType(goal), hero()
2021-05-15 18:22:44 +02:00
{
isAbstract = false;
value = 0;
aid = -1;
resID = -1;
objid = -1;
tile = int3(-1, -1, -1);
town = nullptr;
bid = -1;
2021-05-16 13:38:53 +02:00
goldCost = 0;
2021-05-15 18:22:44 +02:00
}
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
2021-05-15 18:22:44 +02:00
{
return TGoalVec();
}
EGoals goalType;
virtual std::string toString() const;
2021-05-15 18:22:44 +02:00
bool invalid() const;
2021-05-15 18:22:44 +02:00
virtual bool operator==(const AbstractGoal & g) const;
2021-05-16 13:38:53 +02:00
virtual bool isElementar() const { return false; }
virtual bool hasHash() const { return false; }
virtual uint64_t getHash() const { return 0; }
2021-05-15 18:22:44 +02:00
bool operator!=(const AbstractGoal & g) const
{
return !(*this == g);
}
};
class DLL_EXPORT ITask
{
public:
float priority;
2021-05-16 13:38:53 +02:00
ITask() : priority(0) {}
///Visitor pattern
//TODO: make accept work for std::shared_ptr... somehow
2021-05-16 14:39:38 +02:00
virtual void accept(AIGateway * ai) = 0; //unhandled goal will report standard error
virtual std::string toString() const = 0;
2021-05-16 14:01:34 +02:00
virtual HeroPtr getHero() const = 0;
2021-05-16 13:38:53 +02:00
virtual ~ITask() {}
};
2021-05-15 18:22:44 +02:00
}
2021-05-16 13:55:47 +02:00
class cannotFulfillGoalException : public std::exception
{
std::string msg;
public:
explicit cannotFulfillGoalException(crstring _Message)
: msg(_Message)
{
}
virtual ~cannotFulfillGoalException() throw ()
{
};
const char * what() const throw () 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();
}
virtual ~goalFulfilledException() throw ()
{
};
const char * what() const throw () override
{
return msg.c_str();
}
};