2010-11-11 23:01:35 +02:00
|
|
|
#include "../vcmi/global.h"
|
|
|
|
#include "../vcmi/CCallback.h"
|
2010-11-12 17:20:44 +02:00
|
|
|
#include "../vcmi/lib/HeroBonus.h"
|
|
|
|
#include <boost/bind.hpp>
|
2010-11-11 23:01:35 +02:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ExpertSystem.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
|
|
|
|
*
|
|
|
|
*/
|
2010-11-12 17:20:44 +02:00
|
|
|
struct Bonus;
|
2010-11-13 21:25:21 +02:00
|
|
|
template <typename fact> class AIholder;
|
2010-11-12 17:20:44 +02:00
|
|
|
template <typename input, typename output> class Rule;
|
|
|
|
typedef Rule<Bonus, Bonus> BRule;
|
2010-11-13 21:25:21 +02:00
|
|
|
bool greaterThan (int prop, si32 val);
|
2010-11-11 23:01:35 +02:00
|
|
|
|
2010-11-13 21:25:21 +02:00
|
|
|
enum conditionType {LESS_THAN, EQUAL, GREATER_THAN, UNEQUAL, PRESENT};
|
|
|
|
|
|
|
|
template <typename ruleType, typename fact> class ExpertSystemShell
|
2010-11-11 23:01:35 +02:00
|
|
|
{
|
2010-11-12 17:20:44 +02:00
|
|
|
enum runType {ANY_GOAL, TRESHOLD, FULL};
|
2010-11-11 23:01:35 +02:00
|
|
|
private:
|
|
|
|
ICallback* m_cb;
|
|
|
|
protected:
|
2010-11-12 17:20:44 +02:00
|
|
|
std::set<ruleType> knowledge;
|
2010-11-13 21:25:21 +02:00
|
|
|
std::set<ruleType*> rulesToErase;
|
|
|
|
std::set<ruleType*> rulesToAdd;
|
|
|
|
std::set<fact*> factsToErase;
|
|
|
|
std::set<fact*> factsToAdd;
|
2010-11-12 17:20:44 +02:00
|
|
|
ui16 goalCounter; //count / evaluate achieved goals for runType
|
2010-11-11 23:01:35 +02:00
|
|
|
public:
|
2010-11-12 17:20:44 +02:00
|
|
|
ExpertSystemShell(){goalCounter = 0;};
|
2010-11-13 21:25:21 +02:00
|
|
|
std::list<fact> facts; //facts are AIholders with actual game objects, for eg. "my bonus is worthless"
|
2010-11-11 23:01:35 +02:00
|
|
|
|
|
|
|
template <typename t1> void getKnowledge(const t1 &a1){};
|
2010-11-13 21:25:21 +02:00
|
|
|
void getNextRule(){};
|
|
|
|
void addRule(ruleType &rule){rulesToAdd.insert(rule);};
|
|
|
|
void removeRule(ruleType &rule){rulesToErase.insert(rule);};
|
2010-11-11 23:01:35 +02:00
|
|
|
template <typename t2> void returnGoals(const t2 &a2){};
|
2010-11-13 21:25:21 +02:00
|
|
|
template <typename cond> void DataDrivenReasoning(runType type);
|
2010-11-12 17:20:44 +02:00
|
|
|
};
|
2010-11-13 21:25:21 +02:00
|
|
|
template <typename ruleType, typename fact> class Blackboard : public ExpertSystemShell <ruleType, fact>
|
2010-11-12 17:20:44 +02:00
|
|
|
//handle Bonus info coming from different sections of the game
|
|
|
|
{
|
|
|
|
public:
|
2010-11-13 21:25:21 +02:00
|
|
|
Blackboard(){this->goalCounter = 0;}; //template magic, do not touch!
|
2010-11-12 17:20:44 +02:00
|
|
|
std::vector<ExpertSystemShell*> experts; //modules responsible for different tasks
|
2010-11-11 23:01:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename input> class condition
|
2010-11-28 18:39:13 +02:00
|
|
|
{//compares selected object parameter with value using functor. universal logic handler
|
2010-11-11 23:01:35 +02:00
|
|
|
public:
|
2010-11-13 21:25:21 +02:00
|
|
|
input object; //what the fact is, or what it's like (CSelector)
|
|
|
|
si32 value;
|
|
|
|
ui8 parameter;
|
|
|
|
boost::function<bool(int,si32)> functor; //value of selected parameter, condition value
|
|
|
|
|
|
|
|
condition(){object = NULL; value = 0; parameter = 0; functor = greaterThan;};
|
|
|
|
|
|
|
|
bool matchesFact(input &fact){return false;};
|
2010-11-11 23:01:35 +02:00
|
|
|
};
|
|
|
|
|
2010-11-13 21:25:21 +02:00
|
|
|
template <typename input, typename conType> class Rule
|
2010-11-11 23:01:35 +02:00
|
|
|
{
|
2010-11-13 21:25:21 +02:00
|
|
|
friend class ExpertSystemShell <input, conType>;
|
2010-11-11 23:01:35 +02:00
|
|
|
public:
|
2010-11-12 17:20:44 +02:00
|
|
|
bool fired; //if conditions of rule were met and it produces some output
|
2010-11-13 21:25:21 +02:00
|
|
|
ui8 conditionCounter;
|
2010-11-11 23:01:35 +02:00
|
|
|
protected:
|
2010-11-13 21:25:21 +02:00
|
|
|
std::set<std::pair<conType, input*>> cons; //conditions and matching facts
|
|
|
|
input decision;
|
2010-11-28 18:39:13 +02:00
|
|
|
virtual void canBeFired(); //if this data makes any sense for rule - type check
|
|
|
|
virtual bool checkCondition(); //if condition is true or false
|
|
|
|
virtual bool checkCondition(std::set<input*> &feed);
|
2010-11-13 21:25:21 +02:00
|
|
|
virtual void fireRule(); //use paired conditions and facts by default
|
2010-11-28 18:39:13 +02:00
|
|
|
virtual void fireRule(ExpertSystemShell<input, conType> &system);
|
|
|
|
virtual void fireRule(std::set<input*> &feed);
|
|
|
|
virtual void refreshRule();
|
2010-11-13 21:25:21 +02:00
|
|
|
virtual void refreshRule(std::set<conType> &conditionSet); //in case conditions were erased
|
2010-11-11 23:01:35 +02:00
|
|
|
public:
|
2010-11-13 21:25:21 +02:00
|
|
|
Rule(){fired = false; conditionCounter = 0; decision = NULL;};
|
2010-11-12 17:20:44 +02:00
|
|
|
template <typename givenInput> bool matchesInput() //if condition and data match type
|
|
|
|
{return dynamic_cast<input*>(&givenInput);};
|
2010-11-11 23:01:35 +02:00
|
|
|
};
|
|
|
|
|
2010-11-12 17:20:44 +02:00
|
|
|
template <typename input, typename output> class Goal : public Rule <input, output>
|
2010-11-11 23:01:35 +02:00
|
|
|
{
|
2010-11-12 17:20:44 +02:00
|
|
|
protected:
|
|
|
|
boost::function<void(output)> decision(); //do something with AI eventually
|
2010-11-11 23:01:35 +02:00
|
|
|
public:
|
2010-11-12 17:20:44 +02:00
|
|
|
void fireRule(){};
|
2010-11-11 23:01:35 +02:00
|
|
|
};
|
|
|
|
|
2010-11-12 17:20:44 +02:00
|
|
|
template <typename input, typename output> class Weight : public Rule <input, output>
|
2010-11-11 23:01:35 +02:00
|
|
|
{
|
|
|
|
public:
|
2010-11-13 21:25:21 +02:00
|
|
|
float multiplier; //multiply input by value and return to output
|
2010-11-11 23:01:35 +02:00
|
|
|
void fireTule(){};
|
|
|
|
};
|
|
|
|
|
2010-11-13 21:25:21 +02:00
|
|
|
template <typename input> class AIholder
|
|
|
|
{ //stores certain condition and its temporal value
|
|
|
|
public:
|
|
|
|
si32 aiValue;
|
|
|
|
input *object;
|
|
|
|
|
|
|
|
AIholder(){object = NULL; aiValue = 0;}
|
|
|
|
AIholder(input &o){object = o; aiValue = 0;}
|
|
|
|
AIholder(input &o, si32 val){object = 0; aiValue = val;}
|
|
|
|
};
|
|
|
|
|
2010-11-12 17:20:44 +02:00
|
|
|
class BonusSystemExpert : public ExpertSystemShell <BRule, Bonus>
|
|
|
|
{ //TODO: less templates?
|
2010-11-13 21:25:21 +02:00
|
|
|
enum effectType {POSITIVE=1, NEGATIVE=2, EXCLUDING=4, ENEMY=8, ALLY=16}; //what is the influence of bonus and for who
|
|
|
|
};
|
|
|
|
|
|
|
|
class BonusCondition : public condition<CSelector> //used to test rule
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Parameter
|
|
|
|
{
|
|
|
|
type, subtype, val, duration, source, id, valType, additionalInfo, effectRange, limiter //?
|
|
|
|
};
|
|
|
|
bool matchesFact(Bonus &fact);
|
|
|
|
};
|
2010-11-28 18:39:13 +02:00
|
|
|
|
2010-11-13 21:25:21 +02:00
|
|
|
class BonusHolder : public AIholder<Bonus>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BonusHolder(Bonus &bonus){object = &bonus; aiValue = bonus.val;}
|
|
|
|
BonusHolder(Bonus &bonus, si32 val){object = &bonus; aiValue = val;}
|
|
|
|
};
|
2010-11-28 18:39:13 +02:00
|
|
|
|
2010-11-13 21:25:21 +02:00
|
|
|
class BonusRule : public Rule <BonusHolder, BonusCondition>
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
void fireRule();
|
|
|
|
};
|
|
|
|
|
2010-11-28 18:39:13 +02:00
|
|
|
inline bool greaterThan (int prop, si32 val)
|
2010-11-13 21:25:21 +02:00
|
|
|
{
|
|
|
|
if ((si32)prop > val)
|
|
|
|
return true;
|
|
|
|
return false;
|
2010-11-28 18:39:13 +02:00
|
|
|
}
|
|
|
|
inline bool lessThan (int prop, si32 val)
|
|
|
|
{
|
|
|
|
if ((si32)prop < val)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
inline bool eqal (int prop, si32 val)
|
|
|
|
{
|
|
|
|
if ((si32)prop == val)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
inline bool unequal (int prop, si32 val)
|
|
|
|
{
|
|
|
|
if ((si32)prop != val)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
inline bool present (int prop, si32 val=0)
|
|
|
|
//inline bool present (int prop) //TODO: can we use function with less arguments?
|
|
|
|
{
|
|
|
|
return(prop); //unfixable warning :(
|
|
|
|
}
|
|
|
|
|
|
|
|
class KnowledgeHandler///I'd opt for one omniscent knowledge manager, so no templates here
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::list<BonusRule> knowledge; //permanent storage of rules
|
|
|
|
|
|
|
|
void parseKnowledge(std::string &filename){};
|
|
|
|
void addKnowledge(ExpertSystemShell<BRule,Bonus> &expert);
|
|
|
|
void addFacts(ExpertSystemShell<BRule,Bonus> &expert);
|
|
|
|
};
|