From e4599848974779a5b6cfbde5ae41c90156de75fc Mon Sep 17 00:00:00 2001 From: DjWarmonger Date: Sat, 15 Feb 2014 08:10:06 +0000 Subject: [PATCH] Cleanup in AI. --- AI/VCAI/AIUtility.cpp | 99 +++++++++++++++++++++++++++++++++++++++++ AI/VCAI/AIUtility.h | 6 +-- AI/VCAI/VCAI.cpp | 101 ------------------------------------------ 3 files changed, 102 insertions(+), 104 deletions(-) diff --git a/AI/VCAI/AIUtility.cpp b/AI/VCAI/AIUtility.cpp index eb947c7c6..08f2092c2 100644 --- a/AI/VCAI/AIUtility.cpp +++ b/AI/VCAI/AIUtility.cpp @@ -23,6 +23,105 @@ extern FuzzyHelper *fh; //extern static const int3 dirs[8]; +const CGObjectInstance * ObjectIdRef::operator->() const +{ + return cb->getObj(id, false); +} + +ObjectIdRef::operator const CGObjectInstance*() const +{ + return cb->getObj(id, false); +} + +ObjectIdRef::ObjectIdRef(ObjectInstanceID _id) : id(_id) +{ + +} + +ObjectIdRef::ObjectIdRef(const CGObjectInstance *obj) : id(obj->id) +{ + +} + +bool ObjectIdRef::operator<(const ObjectIdRef &rhs) const +{ + return id < rhs.id; +} + +HeroPtr::HeroPtr(const CGHeroInstance *H) +{ + if(!H) + { + //init from nullptr should equal to default init + *this = HeroPtr(); + return; + } + + h = H; + name = h->name; + + hid = H->id; +// infosCount[ai->playerID][hid]++; +} + +HeroPtr::HeroPtr() +{ + h = nullptr; + hid = ObjectInstanceID(); +} + +HeroPtr::~HeroPtr() +{ +// if(hid >= 0) +// infosCount[ai->playerID][hid]--; +} + +bool HeroPtr::operator<(const HeroPtr &rhs) const +{ + return hid < rhs.hid; +} + +const CGHeroInstance * HeroPtr::get(bool doWeExpectNull /*= false*/) const +{ + //TODO? check if these all assertions every time we get info about hero affect efficiency + // + //behave terribly when attempting unauthorized access to hero that is not ours (or was lost) + assert(doWeExpectNull || h); + + if(h) + { + auto obj = cb->getObj(hid); + const bool owned = obj && obj->tempOwner == ai->playerID; + + if(doWeExpectNull && !owned) + { + return nullptr; + } + else + { + assert(obj); + assert(owned); + } + } + + return h; +} + +const CGHeroInstance * HeroPtr::operator->() const +{ + return get(); +} + +bool HeroPtr::validAndSet() const +{ + return get(true); +} + +const CGHeroInstance * HeroPtr::operator*() const +{ + return get(); +} + void foreach_tile_pos(std::function foo) { for(int i = 0; i < cb->getMapSize().x; i++) diff --git a/AI/VCAI/AIUtility.h b/AI/VCAI/AIUtility.h index 8f186a203..a131b5f9d 100644 --- a/AI/VCAI/AIUtility.h +++ b/AI/VCAI/AIUtility.h @@ -22,9 +22,6 @@ * */ -//provisional class for AI to store a reference to an owned hero object -//checks if it's valid on access, should be used in place of const CGHeroInstance* - typedef const int3& crint3; typedef const std::string& crstring; @@ -37,6 +34,9 @@ const int ALLOWED_ROAMING_HEROES = 8; extern const double SAFE_ATTACK_CONSTANT; extern const int GOLD_RESERVE; +//provisional class for AI to store a reference to an owned hero object +//checks if it's valid on access, should be used in place of const CGHeroInstance* + struct HeroPtr { const CGHeroInstance *h; diff --git a/AI/VCAI/VCAI.cpp b/AI/VCAI/VCAI.cpp index a197dbf95..c66e6901f 100644 --- a/AI/VCAI/VCAI.cpp +++ b/AI/VCAI/VCAI.cpp @@ -24,8 +24,6 @@ const double SAFE_ATTACK_CONSTANT = 1.5; const int GOLD_RESERVE = 10000; //when buying creatures we want to keep at least this much gold (10000 so at least we'll be able to reach capitol) using namespace vstd; -//extern Goals::TSubgoal sptr(const Goals::AbstractGoal & tmp); -//#define sptr(x) Goals::sptr(x) //one thread may be turn of AI and another will be handling a side effect for AI2 boost::thread_specific_ptr cb; @@ -2973,102 +2971,3 @@ unsigned char & SectorMap::retreiveTile(crint3 pos) return retreiveTileN(sector, pos); } -const CGObjectInstance * ObjectIdRef::operator->() const -{ - return cb->getObj(id, false); -} - -ObjectIdRef::operator const CGObjectInstance*() const -{ - return cb->getObj(id, false); -} - -ObjectIdRef::ObjectIdRef(ObjectInstanceID _id) : id(_id) -{ - -} - -ObjectIdRef::ObjectIdRef(const CGObjectInstance *obj) : id(obj->id) -{ - -} - -bool ObjectIdRef::operator<(const ObjectIdRef &rhs) const -{ - return id < rhs.id; -} - -HeroPtr::HeroPtr(const CGHeroInstance *H) -{ - if(!H) - { - //init from nullptr should equal to default init - *this = HeroPtr(); - return; - } - - h = H; - name = h->name; - - hid = H->id; -// infosCount[ai->playerID][hid]++; -} - -HeroPtr::HeroPtr() -{ - h = nullptr; - hid = ObjectInstanceID(); -} - -HeroPtr::~HeroPtr() -{ -// if(hid >= 0) -// infosCount[ai->playerID][hid]--; -} - -bool HeroPtr::operator<(const HeroPtr &rhs) const -{ - return hid < rhs.hid; -} - -const CGHeroInstance * HeroPtr::get(bool doWeExpectNull /*= false*/) const -{ - //TODO? check if these all assertions every time we get info about hero affect efficiency - // - //behave terribly when attempting unauthorized access to hero that is not ours (or was lost) - assert(doWeExpectNull || h); - - if(h) - { - auto obj = cb->getObj(hid); - const bool owned = obj && obj->tempOwner == ai->playerID; - - if(doWeExpectNull && !owned) - { - return nullptr; - } - else - { - assert(obj); - assert(owned); - } - } - - return h; -} - -const CGHeroInstance * HeroPtr::operator->() const -{ - return get(); -} - -bool HeroPtr::validAndSet() const -{ - return get(true); -} - -const CGHeroInstance * HeroPtr::operator*() const -{ - return get(); -} -