Skip to content

Commit

Permalink
I have no idea what I'm doing
Browse files Browse the repository at this point in the history
  • Loading branch information
DJWarmonger authored and DJWarmonger committed Jul 26, 2018
1 parent 75f8c8b commit 273802c
Show file tree
Hide file tree
Showing 28 changed files with 1,889 additions and 433 deletions.
49 changes: 49 additions & 0 deletions AI/VCAI/AIUtility.cpp
Expand Up @@ -126,6 +126,11 @@ const CGHeroInstance * HeroPtr::operator*() const
return get();
}

bool HeroPtr::operator==(const HeroPtr & rhs) const
{
return h == rhs.get(true);
}

void foreach_tile_pos(std::function<void(const int3 & pos)> foo)
{
// some micro-optimizations since this function gets called a LOT
Expand Down Expand Up @@ -475,6 +480,50 @@ void getVisibleNeighbours(const std::vector<int3> & tiles, std::vector<int3> & o
}
}

creInfo infoFromDC(const dwellingContent & dc)
{
creInfo ci;
ci.count = dc.first;
ci.creID = dc.second.size() ? dc.second.back() : CreatureID(-1); //should never be accessed
if (ci.creID != -1)
{
ci.cre = VLC->creh->creatures[ci.creID];
ci.level = ci.cre->level; //this is cretaure tier, while tryRealize expects dwelling level. Ignore.
}
else
{
ci.cre = nullptr;
ci.level = 0;
}
return ci;
}

ui64 howManyReinforcementsCanBuy(HeroPtr h, const CGTownInstance * t)
{
ui64 aivalue = 0;

int freeHeroSlots = GameConstants::ARMY_SIZE - h->stacksCount();
for (auto const dc : t->creatures)
{
creInfo ci = infoFromDC(dc);
if (ci.count && ci.creID != -1) //valid creature at this level
{
//can be merged with another stack?
SlotID dst = h->getSlotFor(ci.creID);
if (!h->hasStackAtSlot(dst)) //need another new slot for this stack
if (!freeHeroSlots) //no more place for stacks
continue;
else
freeHeroSlots--; //new slot will be occupied

//we found matching occupied or free slot
aivalue += ci.count * ci.cre->AIValue;
}
}

return aivalue;
}

ui64 howManyReinforcementsCanGet(HeroPtr h, const CGTownInstance * t)
{
ui64 ret = 0;
Expand Down
17 changes: 15 additions & 2 deletions AI/VCAI/AIUtility.h
Expand Up @@ -20,9 +20,11 @@
#include "../../lib/CPathfinder.h"

class CCallback;
struct creInfo;

typedef const int3 & crint3;
typedef const std::string & crstring;
typedef std::pair<ui32, std::vector<CreatureID>> dwellingContent;

const int GOLD_MINE_PRODUCTION = 1000, WOOD_ORE_MINE_PRODUCTION = 2, RESOURCE_MINE_PRODUCTION = 1;
const int ACTUAL_RESOURCE_COUNT = 7;
Expand All @@ -35,7 +37,7 @@ 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
struct DLL_EXPORT HeroPtr
{
const CGHeroInstance * h;
ObjectInstanceID hid;
Expand All @@ -56,6 +58,7 @@ struct HeroPtr
bool operator<(const HeroPtr & rhs) const;
const CGHeroInstance * operator->() const;
const CGHeroInstance * operator*() const; //not that consistent with -> but all interfaces use CGHeroInstance*, so it's convenient
bool operator==(const HeroPtr & rhs) const;

const CGHeroInstance * get(bool doWeExpectNull = false) const;
bool validAndSet() const;
Expand Down Expand Up @@ -137,6 +140,15 @@ bool objWithID(const CGObjectInstance * obj)
return obj->ID == id;
}

struct creInfo
{
int count;
CreatureID creID;
CCreature * cre;
int level;
};
creInfo infoFromDC(const dwellingContent & dc);

void foreach_tile_pos(std::function<void(const int3 & pos)> foo);
void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
void foreach_neighbour(const int3 & pos, std::function<void(const int3 & pos)> foo);
Expand All @@ -160,6 +172,7 @@ bool compareMovement(HeroPtr lhs, HeroPtr rhs);
bool compareHeroStrength(HeroPtr h1, HeroPtr h2);
bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2);
bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2);
ui64 howManyReinforcementsCanBuy(HeroPtr h, const CGTownInstance * t);
ui64 howManyReinforcementsCanGet(HeroPtr h, const CGTownInstance * t);
int3 whereToExplore(HeroPtr h);

Expand All @@ -173,4 +186,4 @@ class CDistanceSorter
{
}
bool operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs);
};
};
84 changes: 84 additions & 0 deletions AI/VCAI/AIhelper.cpp
@@ -0,0 +1,84 @@
/*
* AIhelper.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
*
*/
#include "StdInc.h"

#include "AIhelper.h"
#include "ResourceManager.h"

boost::thread_specific_ptr<AIhelper> ah;

AIhelper::AIhelper()
{
resourceManager.reset(new ResourceManager);
}

AIhelper::~AIhelper()
{
}

bool AIhelper::notifyGoalCompleted(Goals::TSubgoal goal)
{
return resourceManager->notifyGoalCompleted(goal);
}

void AIhelper::setCB(CPlayerSpecificInfoCallback * CB)
{
resourceManager->setCB(CB);
}

void AIhelper::setAI(VCAI * AI)
{
resourceManager->setAI(AI);
}

Goals::TSubgoal AIhelper::whatToDo(TResources & res, Goals::TSubgoal goal)
{
return resourceManager->whatToDo(res, goal);
}

Goals::TSubgoal AIhelper::whatToDo() const
{
return resourceManager->whatToDo();
}

bool AIhelper::hasTasksLeft() const
{
return resourceManager->hasTasksLeft();
}

bool AIhelper::canAfford(const TResources & cost) const
{
return resourceManager->canAfford(cost);
}

TResources AIhelper::reservedResources() const
{
return resourceManager->reservedResources();
}

TResources AIhelper::freeResources() const
{
return resourceManager->freeResources();
}

TResource AIhelper::freeGold() const
{
return resourceManager->freeGold();
}

TResources AIhelper::allResources() const
{
return resourceManager->allResources();
}

TResource AIhelper::allGold() const
{
return resourceManager->allGold();
}
52 changes: 52 additions & 0 deletions AI/VCAI/AIhelper.h
@@ -0,0 +1,52 @@
/*
* AIhelper.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

/*
!!! Note: Include THIS file at the end of include list to avoid "undefined base class" error
*/

#include "ResourceManager.h"

class ResourceManager;

//indirection interface for various modules
class DLL_EXPORT AIhelper : public IResourceManager
{
friend class VCAI;
friend struct SetGlobalState; //mess?

//members are thread_specific. AIhelper is global
std::shared_ptr<ResourceManager> resourceManager;
std::shared_ptr<VCAI> ai;
public:
AIhelper();
~AIhelper();

//TODO: consider common interface with Resource Manager?
bool canAfford(const TResources & cost) const;
TResources reservedResources() const override;
TResources freeResources() const override;
TResource freeGold() const override;
TResources allResources() const override;
TResource allGold() const override;

Goals::TSubgoal whatToDo(TResources &res, Goals::TSubgoal goal) override;
Goals::TSubgoal whatToDo() const override; //peek highest-priority goal
bool hasTasksLeft() const override;

private:
bool notifyGoalCompleted(Goals::TSubgoal goal);

void setCB(CPlayerSpecificInfoCallback * CB) override;
void setAI(VCAI * AI) override;
};

12 changes: 8 additions & 4 deletions AI/VCAI/Fuzzy.cpp
Expand Up @@ -322,7 +322,7 @@ float FuzzyHelper::evaluate(Goals::Explore & g)
}
float FuzzyHelper::evaluate(Goals::RecruitHero & g)
{
return 1; //just try to recruit hero as one of options
return 1;
}
FuzzyHelper::EvalVisitTile::~EvalVisitTile()
{
Expand Down Expand Up @@ -518,20 +518,24 @@ float FuzzyHelper::evaluate(Goals::ClearWayTo & g)

float FuzzyHelper::evaluate(Goals::BuildThis & g)
{
return 1;
return g.priority; //TODO
}
float FuzzyHelper::evaluate(Goals::DigAtTile & g)
{
return 0;
}
float FuzzyHelper::evaluate(Goals::CollectRes & g)
{
return 0;
return g.priority; //handled by ResourceManager
}
float FuzzyHelper::evaluate(Goals::Build & g)
{
return 0;
}
float FuzzyHelper::evaluate(Goals::BuyArmy & g)
{
return g.priority;
}
float FuzzyHelper::evaluate(Goals::Invalid & g)
{
return -1e10;
Expand All @@ -541,7 +545,7 @@ float FuzzyHelper::evaluate(Goals::AbstractGoal & g)
logAi->warn("Cannot evaluate goal %s", g.name());
return g.priority;
}
void FuzzyHelper::setPriority(Goals::TSubgoal & g)
void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pattern
{
g->setpriority(g->accept(this)); //this enforces returned value is set
}
1 change: 1 addition & 0 deletions AI/VCAI/Fuzzy.h
Expand Up @@ -73,6 +73,7 @@ class FuzzyHelper
float evaluate(Goals::DigAtTile & g);
float evaluate(Goals::CollectRes & g);
float evaluate(Goals::Build & g);
float evaluate(Goals::BuyArmy & g);
float evaluate(Goals::GatherArmy & g);
float evaluate(Goals::ClearWayTo & g);
float evaluate(Goals::Invalid & g);
Expand Down

0 comments on commit 273802c

Please sign in to comment.