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

Implemented rule-based evaluation of tactical advantage between armies.

AI will creep more agressively and smarter.
This commit is contained in:
DjWarmonger 2012-03-04 10:43:46 +00:00
parent c5007f8bef
commit cd0ed39fbf
6 changed files with 620 additions and 388 deletions

View File

@ -41,12 +41,24 @@
*
*/
#define MIN_AI_STRENGHT (0.5f) //lower when combat AI gets smarter
class BankConfig;
class FuzzyEngine;
class InputLVar;
class CGTownInstance;
using namespace boost::assign;
using namespace vstd;
FuzzyHelper fh;
struct armyStructure
{
float walkers, shooters, flyers;
ui32 maxSpeed;
};
ui64 evaluateBankConfig (BankConfig * bc)
{
ui64 danger = 0;
@ -57,63 +69,233 @@ ui64 evaluateBankConfig (BankConfig * bc)
return danger;
}
armyStructure evaluateArmyStructure (const CArmedInstance * army)
{
ui64 totalStrenght = army->getArmyStrength();
double walkersStrenght = 0;
double flyersStrenght = 0;
double shootersStrenght = 0;
ui32 maxSpeed = 0;
BOOST_FOREACH(auto s, army->Slots())
{
bool walker = true;
if (s.second->type->hasBonusOfType(Bonus::SHOOTER))
{
shootersStrenght += s.second->getPower();
walker = false;
}
if (s.second->type->hasBonusOfType(Bonus::FLYING))
{
flyersStrenght += s.second->getPower();
walker = false;
}
if (walker)
walkersStrenght += s.second->getPower();
amax (maxSpeed, s.second->type->speed);
}
armyStructure as;
as.walkers = walkersStrenght / totalStrenght;
as.shooters = shootersStrenght / totalStrenght;
as.flyers = flyersStrenght / totalStrenght;
as.maxSpeed = maxSpeed;
return as;
}
FuzzyHelper::FuzzyHelper()
{
initBank();
initTacticalAdvantage();
}
void FuzzyHelper::initBank()
{
try
{
//Trivial bank estimation
bankInput = new fl::InputLVar("BankInput");
bankDanger = new fl::OutputLVar("BankDanger");
bankInput->addTerm(new fl::SingletonTerm ("SET"));
engine.addRuleBlock (&ruleBlock); //have to be added before the rules are parsed
engine.addRuleBlock (&bankBlock); //have to be added before the rules are parsed
engine.addInputLVar (bankInput);
engine.addOutputLVar (bankDanger);
for (int i = 0; i < 4; ++i)
{
bankDanger->addTerm(new fl::TriangularTerm ("Bank" + boost::lexical_cast<std::string>(i), 0, 1));
ruleBlock.addRule(new fl::MamdaniRule("if BankInput is SET then BankDanger is Bank" + boost::lexical_cast<std::string>(i), engine));
bankBlock.addRule(new fl::MamdaniRule("if BankInput is SET then BankDanger is Bank" + boost::lexical_cast<std::string>(i), engine));
}
}
catch (fl::FuzzyException fe)
{
tlog1 << fe.name() << ": " << fe.message() << '\n';
tlog1 << "initBank " << fe.name() << ": " << fe.message() << '\n';
}
}
void FuzzyHelper::initTacticalAdvantage()
{
try
{
//Tactical advantage calculation
std::vector<fl::InputLVar*> helper;
ourShooters = new fl::InputLVar("OurShooters");
ourWalkers = new fl::InputLVar("OurWalkers");
ourFlyers = new fl::InputLVar("OurFlyers");
enemyShooters = new fl::InputLVar("EnemyShooters");
enemyWalkers = new fl::InputLVar("EnemyWalkers");
enemyFlyers = new fl::InputLVar("EnemyFlyers");
helper += ourShooters, ourWalkers, ourFlyers, enemyShooters, enemyWalkers, enemyFlyers;
BOOST_FOREACH (auto val, helper)
{
val->addTerm (new fl::ShoulderTerm("FEW", 0, 0.75, true));
val->addTerm (new fl::ShoulderTerm("MANY", 0.25, 1, false));
engine.addInputLVar(val);
}
helper.clear();
ourSpeed = new fl::InputLVar("OurSpeed");
enemySpeed = new fl::InputLVar("EnemySpeed");
helper += ourSpeed, enemySpeed;
BOOST_FOREACH (auto val, helper)
{
val->addTerm (new fl::ShoulderTerm("LOW", 3, 9, true));
val->addTerm (new fl::TriangularTerm("MEDIUM", 7, 13));
val->addTerm (new fl::ShoulderTerm("HIGH", 11, 16, false));
engine.addInputLVar(val);
}
castleWalls = new fl::InputLVar("CastleWalls");
castleWalls->addTerm(new fl::SingletonTerm("NONE", CGTownInstance::NONE));
castleWalls->addTerm(new fl::TrapezoidalTerm("MEDIUM", CGTownInstance::FORT, 2.5));
castleWalls->addTerm(new fl::ShoulderTerm("HIGH", CGTownInstance::CITADEL, CGTownInstance::CASTLE));
engine.addInputLVar(castleWalls);
bankPresent = new fl::InputLVar("Bank");
bankPresent->addTerm(new fl::SingletonTerm("FALSE", 0));
bankPresent->addTerm(new fl::SingletonTerm("TRUE", 1));
engine.addInputLVar(bankPresent);
threat = new fl::OutputLVar("Threat");
threat->addTerm (new fl::TriangularTerm("LOW", MIN_AI_STRENGHT, 1));
threat->addTerm (new fl::TriangularTerm("MEDIUM", 0.8, 1.2));
threat->addTerm (new fl::ShoulderTerm("HIGH", 1, 1.5, false));
engine.addOutputLVar(threat);
engine.hedgeSet().add(new fl::HedgeSomewhat());
engine.hedgeSet().add(new fl::HedgeVery());
tacticalAdvantage.addRule(new fl::MamdaniRule("if OurShooters is MANY and EnemySpeed is LOW then Threat is very LOW", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if OurSpeed is LOW and OurShooters is FEW and EnemyShooters is MANY then Threat is very HIGH", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if (OurShooters is MANY or OurFlyers is MANY) and EnemyShooters is MANY then Threat is LOW", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if OurShooters is MANY and EnemySpeed is HIGH then Threat is somewhat HIGH", engine));
//tacticalAdvantage.addRule(new fl::MamdaniRule("if OurShooters is MANY and EnemyShooters is MANY then Threat is MEDIUM", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if Bank is TRUE and OurShooters is MANY then Threat is somewhat HIGH", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if Bank is TRUE and EnemyShooters is MANY then Threat is LOW", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if CastleWalls is HIGH and OurWalkers is MANY then Threat is very HIGH", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if CastleWalls is HIGH and OurFlyers is MANY and OurShooters is MANY then Threat is MEDIUM", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if CastleWalls is MEDIUM and OurShooters is MANY and EnemyWalkers is MANY then Threat is LOW", engine));
engine.addRuleBlock (&tacticalAdvantage);
}
catch(fl::ParsingException pe)
{
tlog1 << "initTacticalAdvantage " << pe.name() << ": " << pe.message() << '\n';
}
catch (fl::FuzzyException fe)
{
tlog1 << "initTacticalAdvantage " << fe.name() << ": " << fe.message() << '\n';
}
}
ui64 FuzzyHelper::estimateBankDanger (int ID)
{
std::vector <ConstTransitivePtr<BankConfig>> & configs = VLC->objh->banksInfo[ID];
ui64 val = INFINITY;;
switch (configs.size())
ui64 val = INFINITY;
try
{
case 4:
try
{
int bankVal;
for (int i = 0; i < 4; ++i)
switch (configs.size())
{
case 4:
try
{
bankVal = evaluateBankConfig (VLC->objh->banksInfo[ID][i]);
bankDanger->term("Bank" + boost::lexical_cast<std::string>(i))->setMinimum(bankVal * 0.5f);
bankDanger->term("Bank" + boost::lexical_cast<std::string>(i))->setMaximum(bankVal * 1.5f);
int bankVal;
for (int i = 0; i < 4; ++i)
{
bankVal = evaluateBankConfig (VLC->objh->banksInfo[ID][i]);
bankDanger->term("Bank" + boost::lexical_cast<std::string>(i))->setMinimum(bankVal * 0.5f);
bankDanger->term("Bank" + boost::lexical_cast<std::string>(i))->setMaximum(bankVal * 1.5f);
}
//comparison purposes
//int averageValue = (evaluateBankConfig (VLC->objh->banksInfo[ID][0]) + evaluateBankConfig (VLC->objh->banksInfo[ID][3])) * 0.5;
dynamic_cast<fl::SingletonTerm*>(bankInput->term("SET"))->setValue(0.5);
bankInput->setInput (0.5);
engine.process (BANK_DANGER);
val = bankDanger->output().defuzzify(); //some expected value of this bank
}
//comparison purposes
//int averageValue = (evaluateBankConfig (VLC->objh->banksInfo[ID][0]) + evaluateBankConfig (VLC->objh->banksInfo[ID][3])) * 0.5;
dynamic_cast<fl::SingletonTerm*>(bankInput->term("SET"))->setValue(0.5);
bankInput->setInput (0.5);
engine.process();
val = bankDanger->output().defuzzify(); //some expected value of this bank
}
catch (fl::FuzzyException fe)
{
tlog1 << fe.name() << ": " << fe.message() << '\n';
}
break;
case 1: //rare case - Pyramid
val = evaluateBankConfig (VLC->objh->banksInfo[ID][0]);
break;
default:
tlog3 << ("Uhnandled bank config!\n");
catch (fl::FuzzyException fe)
{
tlog1 << fe.name() << ": " << fe.message() << '\n';
}
break;
case 1: //rare case - Pyramid
val = evaluateBankConfig (VLC->objh->banksInfo[ID][0]);
break;
default:
tlog3 << ("Uhnandled bank config!\n");
}
}
catch (fl::FuzzyException fe)
{
tlog1 << "estimateBankDanger " << fe.name() << ": " << fe.message() << '\n';
}
return val;
}
float FuzzyHelper::getTacticalAdvantage (const CArmedInstance *we, const CArmedInstance *enemy)
{
float output = 1;
try
{
armyStructure ourStructure = evaluateArmyStructure(we);
armyStructure enemyStructure = evaluateArmyStructure(enemy);
ourWalkers->setInput(ourStructure.walkers);
ourShooters->setInput(ourStructure.shooters);
ourFlyers->setInput(ourStructure.flyers);
ourSpeed->setInput(ourStructure.maxSpeed);
enemyWalkers->setInput(enemyStructure.walkers);
enemyShooters->setInput(enemyStructure.shooters);
enemyFlyers->setInput(enemyStructure.flyers);
enemySpeed->setInput(enemyStructure.maxSpeed);
bool bank = dynamic_cast<const CBank*>(enemy);
if (bank)
bankPresent->setInput(1);
else
bankPresent->setInput(0);
const CGTownInstance * fort = dynamic_cast<const CGTownInstance*>(enemy);
if (fort)
{
castleWalls->setInput (fort->fortLevel());
}
else
castleWalls->setInput(0);
engine.process (TACTICAL_ADVANTAGE);
output = threat->output().defuzzify();
}
catch (fl::FuzzyException fe)
{
tlog1 << "getTacticalAdvantage " << fe.name() << ": " << fe.message() << '\n';
}
return output;
}

View File

@ -13,6 +13,7 @@
*/
class VCAI;
class CArmedInstance;
template <typename T> bool isinf (T val)
{
@ -24,11 +25,25 @@ class FuzzyHelper
friend class VCAI;
fl::FuzzyEngine engine;
fl::InputLVar* bankInput;
fl::OutputLVar* bankDanger;
fl::RuleBlock ruleBlock;
fl::RuleBlock bankBlock;
fl::InputLVar * ourWalkers, * ourShooters, * ourFlyers, * enemyWalkers, * enemyShooters, * enemyFlyers;
fl::InputLVar * ourSpeed, * enemySpeed;
fl::InputLVar * bankPresent;
fl::InputLVar * castleWalls;
fl::OutputLVar * threat;
fl::RuleBlock tacticalAdvantage;
public:
enum RuleBlocks {BANK_DANGER, TACTICAL_ADVANTAGE};
FuzzyHelper();
void initBank();
void initTacticalAdvantage();
ui64 estimateBankDanger (int ID);
float getTacticalAdvantage (const CArmedInstance *we, const CArmedInstance *enemy); //returns factor how many times enemy is stronger than us
};

View File

@ -11,7 +11,7 @@ extern FuzzyHelper fh;
const int ACTUAL_RESOURCE_COUNT = 7;
const double SAFE_ATTACK_CONSTANT = 2.5;
const double SAFE_ATTACK_CONSTANT = 1.5;
using namespace vstd;
//one thread may be turn of AI and another will be handling a side effect for AI2
@ -297,6 +297,39 @@ ui64 evaluateDanger(crint3 tile)
return 0;
}
ui64 evaluateDanger(crint3 tile, const CGHeroInstance *visitor)
{
const TerrainTile *t = cb->getTile(tile, false);
if(!t) //we can know about guard but can't check its tile (the edge of fow)
return 190000000; //MUCH
ui64 objectDanger = 0, guardDanger = 0;
CArmedInstance * dangerousObject;
if(t->visitable)
{
dangerousObject = dynamic_cast<CArmedInstance*>(t->visitableObjects.back());
if (dangerousObject)
{
objectDanger = evaluateDanger(dangerousObject);
objectDanger *= fh.getTacticalAdvantage (visitor, dangerousObject);
}
else
{
objectDanger = 0;
}
}
int3 guardPos = cb->guardingCreaturePosition(tile);
if(guardPos.x >= 0 && guardPos != tile)
guardDanger = evaluateDanger(guardPos, visitor);
//TODO mozna odwiedzic blockvis nie ruszajac straznika
return std::max(objectDanger, guardDanger);
return 0;
}
ui64 evaluateDanger(const CGObjectInstance *obj)
{
if(obj->tempOwner < GameConstants::PLAYER_LIMIT && cb->getPlayerRelations(obj->tempOwner, ai->playerID)) //owned or allied objects don't pose any threat
@ -945,7 +978,7 @@ void VCAI::buildStructure(const CGTownInstance * t)
bool isSafeToVisit(const CGHeroInstance *h, crint3 tile)
{
const ui64 heroStrength = h->getTotalStrength(),
dangerStrength = evaluateDanger(tile);
dangerStrength = evaluateDanger(tile, h);
if(dangerStrength)
{
if(heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength)

698
Global.h
View File

@ -1,357 +1,357 @@
#pragma once
// Standard include file
// Contents:
// Includes C/C++ libraries, STL libraries, IOStream and String libraries
// Includes the most important boost headers
// Defines the import + export, override and exception handling macros
// Defines the vstd library
// Includes the logger
// This file shouldn't be changed, except if there is a important header file missing which is shared among several projects.
/*
* Global.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
*
*/
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <cstdio>
#include <stdio.h>
#ifdef _WIN32
#include <tchar.h>
#else
#include "tchar_amigaos4.h"
#endif
#include <cmath>
#include <cassert>
#include <assert.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <numeric>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <memory>
#include <cstdlib>
//filesystem version 3 causes problems (and it's default as of boost 1.46)
#define BOOST_FILESYSTEM_VERSION 2
#include <boost/algorithm/string.hpp>
#include <boost/assert.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/cstdint.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/function.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/logic/tribool.hpp>
#include <boost/program_options.hpp>
#include <boost/thread.hpp>
#include <boost/unordered_set.hpp>
#ifdef ANDROID
#include <android/log.h>
#endif
// Integral data types
typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
typedef boost::int8_t si8; //signed int 8 bits (1 byte)
#ifdef __GNUC__
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ )
#endif
// Import + Export macro declarations
#ifdef _WIN32
#define DLL_EXPORT __declspec(dllexport)
#else
#if defined(__GNUC__) && GCC_VERSION >= 400
#define DLL_EXPORT __attribute__ ((visibility("default")))
#else
#define DLL_EXPORT
#endif
#endif
#ifdef _WIN32
#define DLL_IMPORT __declspec(dllimport)
#else
#if defined(__GNUC__) && GCC_VERSION >= 400
#define DLL_IMPORT __attribute__ ((visibility("default")))
#else
#define DLL_IMPORT
#endif
#endif
#ifdef VCMI_DLL
#define DLL_LINKAGE DLL_EXPORT
#else
#define DLL_LINKAGE DLL_IMPORT
#endif
//defining available c++11 features
//initialization lists - only gcc-4.4 or later
#if defined(__GNUC__) && (GCC_VERSION >= 404)
#define CPP11_USE_INITIALIZERS_LIST
#endif
//nullptr - only msvc and gcc-4.6 or later, othervice define it as NULL
#if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 406))
#define nullptr NULL
#endif
//override keyword - only msvc and gcc-4.7 or later.
#if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 407))
#define override
#endif
//workaround to support existing code
#define OVERRIDE override
//a normal std::map with a const operator[] for sanity
template<typename KeyT, typename ValT>
class bmap : public std::map<KeyT, ValT>
{
public:
const ValT & operator[](KeyT key) const
{
return find(key)->second;
}
ValT & operator[](KeyT key)
{
return static_cast<std::map<KeyT, ValT> &>(*this)[key];
}
template <typename Handler> void serialize(Handler &h, const int version)
{
h & static_cast<std::map<KeyT, ValT> &>(*this);
}
};
namespace vstd
{
//returns true if container c contains item i
template <typename Container, typename Item>
bool contains(const Container & c, const Item &i)
{
return std::find(c.begin(),c.end(),i) != c.end();
}
//returns true if map c contains item i
template <typename V, typename Item, typename Item2>
bool contains(const std::map<Item,V> & c, const Item2 &i)
{
return c.find(i)!=c.end();
}
//returns true if bmap c contains item i
template <typename V, typename Item, typename Item2>
bool contains(const bmap<Item,V> & c, const Item2 &i)
{
return c.find(i)!=c.end();
}
//returns true if unordered set c contains item i
template <typename Item>
bool contains(const boost::unordered_set<Item> & c, const Item &i)
{
return c.find(i)!=c.end();
}
//returns position of first element in vector c equal to s, if there is no such element, -1 is returned
template <typename T1, typename T2>
int find_pos(const std::vector<T1> & c, const T2 &s)
{
for(size_t i=0; i < c.size(); ++i)
if(c[i] == s)
return i;
return -1;
}
//Func(T1,T2) must say if these elements matches
template <typename T1, typename T2, typename Func>
int find_pos(const std::vector<T1> & c, const T2 &s, const Func &f)
{
for(size_t i=0; i < c.size(); ++i)
if(f(c[i],s))
return i;
return -1;
}
//returns iterator to the given element if present in container, end() if not
template <typename Container, typename Item>
typename Container::iterator find(Container & c, const Item &i)
{
return std::find(c.begin(),c.end(),i);
}
//returns const iterator to the given element if present in container, end() if not
template <typename Container, typename Item>
typename Container::const_iterator find(const Container & c, const Item &i)
{
return std::find(c.begin(),c.end(),i);
}
//removes element i from container c, returns false if c does not contain i
template <typename Container, typename Item>
typename Container::size_type operator-=(Container &c, const Item &i)
{
typename Container::iterator itr = find(c,i);
if(itr == c.end())
return false;
c.erase(itr);
return true;
}
//assigns greater of (a, b) to a and returns maximum of (a, b)
template <typename t1, typename t2>
t1 &amax(t1 &a, const t2 &b)
{
if(a >= b)
return a;
else
{
a = b;
return a;
}
}
//assigns smaller of (a, b) to a and returns minimum of (a, b)
template <typename t1, typename t2>
t1 &amin(t1 &a, const t2 &b)
{
if(a <= b)
return a;
else
{
a = b;
return a;
}
}
//makes a to fit the range <b, c>
template <typename t1, typename t2, typename t3>
t1 &abetween(t1 &a, const t2 &b, const t3 &c)
{
amax(a,b);
amin(a,c);
return a;
}
//checks if a is between b and c
template <typename t1, typename t2, typename t3>
bool isbetween(const t1 &a, const t2 &b, const t3 &c)
{
return a > b && a < c;
}
//checks if a is within b and c
template <typename t1, typename t2, typename t3>
bool iswithin(const t1 &a, const t2 &b, const t3 &c)
{
return a >= b && a <= c;
}
template <typename t1, typename t2>
struct assigner
{
public:
t1 &op1;
t2 op2;
assigner(t1 &a1, const t2 & a2)
:op1(a1), op2(a2)
{}
void operator()()
{
op1 = op2;
}
};
// Assigns value a2 to a1. The point of time of the real operation can be controlled
// with the () operator.
template <typename t1, typename t2>
assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
{
return assigner<t1,t2>(a1,a2);
}
//deleted pointer and sets it to NULL
template <typename T>
void clear_pointer(T* &ptr)
{
delete ptr;
ptr = NULL;
}
#pragma once
// Standard include file
// Contents:
// Includes C/C++ libraries, STL libraries, IOStream and String libraries
// Includes the most important boost headers
// Defines the import + export, override and exception handling macros
// Defines the vstd library
// Includes the logger
// This file shouldn't be changed, except if there is a important header file missing which is shared among several projects.
/*
* Global.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
*
*/
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <cstdio>
#include <stdio.h>
#ifdef _WIN32
#include <tchar.h>
#else
#include "tchar_amigaos4.h"
#endif
#include <cmath>
#include <cassert>
#include <assert.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <numeric>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <memory>
#include <cstdlib>
//filesystem version 3 causes problems (and it's default as of boost 1.46)
#define BOOST_FILESYSTEM_VERSION 2
#include <boost/algorithm/string.hpp>
#include <boost/assert.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/cstdint.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/function.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/logic/tribool.hpp>
#include <boost/program_options.hpp>
#include <boost/thread.hpp>
#include <boost/unordered_set.hpp>
#ifdef ANDROID
#include <android/log.h>
#endif
// Integral data types
typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
typedef boost::int8_t si8; //signed int 8 bits (1 byte)
#ifdef __GNUC__
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ )
#endif
// Import + Export macro declarations
#ifdef _WIN32
#define DLL_EXPORT __declspec(dllexport)
#else
#if defined(__GNUC__) && GCC_VERSION >= 400
#define DLL_EXPORT __attribute__ ((visibility("default")))
#else
#define DLL_EXPORT
#endif
#endif
#ifdef _WIN32
#define DLL_IMPORT __declspec(dllimport)
#else
#if defined(__GNUC__) && GCC_VERSION >= 400
#define DLL_IMPORT __attribute__ ((visibility("default")))
#else
#define DLL_IMPORT
#endif
#endif
#ifdef VCMI_DLL
#define DLL_LINKAGE DLL_EXPORT
#else
#define DLL_LINKAGE DLL_IMPORT
#endif
//defining available c++11 features
//initialization lists - only gcc-4.4 or later
#if defined(__GNUC__) && (GCC_VERSION >= 404)
#define CPP11_USE_INITIALIZERS_LIST
#endif
//nullptr - only msvc and gcc-4.6 or later, othervice define it as NULL
#if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 406))
#define nullptr NULL
#endif
//override keyword - only msvc and gcc-4.7 or later.
#if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 407))
#define override
#endif
//workaround to support existing code
#define OVERRIDE override
//a normal std::map with a const operator[] for sanity
template<typename KeyT, typename ValT>
class bmap : public std::map<KeyT, ValT>
{
public:
const ValT & operator[](KeyT key) const
{
return find(key)->second;
}
ValT & operator[](KeyT key)
{
return static_cast<std::map<KeyT, ValT> &>(*this)[key];
}
template <typename Handler> void serialize(Handler &h, const int version)
{
h & static_cast<std::map<KeyT, ValT> &>(*this);
}
};
namespace vstd
{
//returns true if container c contains item i
template <typename Container, typename Item>
bool contains(const Container & c, const Item &i)
{
return std::find(c.begin(),c.end(),i) != c.end();
}
//returns true if map c contains item i
template <typename V, typename Item, typename Item2>
bool contains(const std::map<Item,V> & c, const Item2 &i)
{
return c.find(i)!=c.end();
}
//returns true if bmap c contains item i
template <typename V, typename Item, typename Item2>
bool contains(const bmap<Item,V> & c, const Item2 &i)
{
return c.find(i)!=c.end();
}
//returns true if unordered set c contains item i
template <typename Item>
bool contains(const boost::unordered_set<Item> & c, const Item &i)
{
return c.find(i)!=c.end();
}
//returns position of first element in vector c equal to s, if there is no such element, -1 is returned
template <typename T1, typename T2>
int find_pos(const std::vector<T1> & c, const T2 &s)
{
for(size_t i=0; i < c.size(); ++i)
if(c[i] == s)
return i;
return -1;
}
//Func(T1,T2) must say if these elements matches
template <typename T1, typename T2, typename Func>
int find_pos(const std::vector<T1> & c, const T2 &s, const Func &f)
{
for(size_t i=0; i < c.size(); ++i)
if(f(c[i],s))
return i;
return -1;
}
//returns iterator to the given element if present in container, end() if not
template <typename Container, typename Item>
typename Container::iterator find(Container & c, const Item &i)
{
return std::find(c.begin(),c.end(),i);
}
//returns const iterator to the given element if present in container, end() if not
template <typename Container, typename Item>
typename Container::const_iterator find(const Container & c, const Item &i)
{
return std::find(c.begin(),c.end(),i);
}
//removes element i from container c, returns false if c does not contain i
template <typename Container, typename Item>
typename Container::size_type operator-=(Container &c, const Item &i)
{
typename Container::iterator itr = find(c,i);
if(itr == c.end())
return false;
c.erase(itr);
return true;
}
//assigns greater of (a, b) to a and returns maximum of (a, b)
template <typename t1, typename t2>
t1 &amax(t1 &a, const t2 &b)
{
if(a >= b)
return a;
else
{
a = b;
return a;
}
}
//assigns smaller of (a, b) to a and returns minimum of (a, b)
template <typename t1, typename t2>
t1 &amin(t1 &a, const t2 &b)
{
if(a <= b)
return a;
else
{
a = b;
return a;
}
}
//makes a to fit the range <b, c>
template <typename t1, typename t2, typename t3>
t1 &abetween(t1 &a, const t2 &b, const t3 &c)
{
amax(a,b);
amin(a,c);
return a;
}
//checks if a is between b and c
template <typename t1, typename t2, typename t3>
bool isbetween(const t1 &a, const t2 &b, const t3 &c)
{
return a > b && a < c;
}
//checks if a is within b and c
template <typename t1, typename t2, typename t3>
bool iswithin(const t1 &a, const t2 &b, const t3 &c)
{
return a >= b && a <= c;
}
template <typename t1, typename t2>
struct assigner
{
public:
t1 &op1;
t2 op2;
assigner(t1 &a1, const t2 & a2)
:op1(a1), op2(a2)
{}
void operator()()
{
op1 = op2;
}
};
// Assigns value a2 to a1. The point of time of the real operation can be controlled
// with the () operator.
template <typename t1, typename t2>
assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
{
return assigner<t1,t2>(a1,a2);
}
//deleted pointer and sets it to NULL
template <typename T>
void clear_pointer(T* &ptr)
{
delete ptr;
ptr = NULL;
}
template<typename T>
std::unique_ptr<T> make_unique()
{
return std::unique_ptr<T>(new T());
}
}
template<typename T, typename Arg1>
std::unique_ptr<T> make_unique(Arg1&& arg1)
{
return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1)));
}
}
using vstd::operator-=;
// can be used for counting arrays
template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
#define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
//XXX pls dont - 'debug macros' are usually more trouble than it's worth
#define HANDLE_EXCEPTION \
catch (const std::exception& e) { \
tlog1 << e.what() << std::endl; \
throw; \
} \
catch (const std::exception * e) \
{ \
tlog1 << e->what()<< std::endl; \
throw; \
} \
catch (const std::string& e) { \
tlog1 << e << std::endl; \
throw; \
}
#define HANDLE_EXCEPTIONC(COMMAND) \
catch (const std::exception& e) { \
COMMAND; \
tlog1 << e.what() << std::endl; \
throw; \
} \
catch (const std::string &e) \
{ \
COMMAND; \
tlog1 << e << std::endl; \
throw; \
}
#include "lib/CLogger.h"
}
}
using vstd::operator-=;
// can be used for counting arrays
template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
#define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
//XXX pls dont - 'debug macros' are usually more trouble than it's worth
#define HANDLE_EXCEPTION \
catch (const std::exception& e) { \
tlog1 << e.what() << std::endl; \
throw; \
} \
catch (const std::exception * e) \
{ \
tlog1 << e->what()<< std::endl; \
throw; \
} \
catch (const std::string& e) { \
tlog1 << e << std::endl; \
throw; \
}
#define HANDLE_EXCEPTIONC(COMMAND) \
catch (const std::exception& e) { \
COMMAND; \
tlog1 << e.what() << std::endl; \
throw; \
} \
catch (const std::string &e) \
{ \
COMMAND; \
tlog1 << e << std::endl; \
throw; \
}
#include "lib/CLogger.h"

View File

@ -1820,15 +1820,15 @@ void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
break;
}
}
int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
{
if((builtBuildings.find(9))!=builtBuildings.end())
return 3;
return CASTLE;
if((builtBuildings.find(8))!=builtBuildings.end())
return 2;
return CITADEL;
if((builtBuildings.find(7))!=builtBuildings.end())
return 1;
return 0;
return FORT;
return NONE;
}
int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol

View File

@ -544,6 +544,8 @@ struct DLL_LINKAGE GrowthInfo
class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket
{
public:
enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
CTownAndVisitingHero townAndVis;
CTown * town;
std::string name; // name of town
@ -605,7 +607,7 @@ public:
//////////////////////////////////////////////////////////////////////////
bool needsLastStack() const;
int fortLevel() const; //0 - none, 1 - fort, 2 - citadel, 3 - castle
CGTownInstance::EFortLevel fortLevel() const;
int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
bool creatureDwelling(const int & level, bool upgraded=false) const;