1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/*
* CollectRes.cpp, 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 "Goals.h"
#include "../VCAI.h"
#include "../AIUtility.h"
#include "../AIhelper.h"
#include "../FuzzyHelper.h"
#include "../ResourceManager.h"
#include "../BuildingManager.h"
#include "../../../lib/mapObjects/CGMarket.h"
#include "../../../lib/mapObjects/CGResource.h"
#include "../../../lib/constants/StringConstants.h"
using namespace Goals;
bool CollectRes::operator==(const CollectRes & other) const
{
return resID == other.resID;
}
TGoalVec CollectRes::getAllPossibleSubgoals()
{
TGoalVec ret;
auto givesResource = [this](const CGObjectInstance * obj) -> bool
{
//TODO: move this logic to object side
//TODO: remember mithril exists
//TODO: water objects
//TODO: Creature banks
//return false first from once-visitable, before checking if they were even visited
switch (obj->ID.num)
{
case Obj::TREASURE_CHEST:
return resID == GameResID(EGameResID::GOLD);
break;
case Obj::RESOURCE:
return dynamic_cast<const CGResource*>(obj)->resourceID() == GameResID(resID);
break;
case Obj::MINE:
return (dynamic_cast<const CGMine*>(obj)->producedResource == GameResID(resID) &&
(cb->getPlayerRelations(obj->tempOwner, ai->playerID) == PlayerRelations::ENEMIES)); //don't capture our mines
break;
case Obj::CAMPFIRE:
return true; //contains all resources
break;
case Obj::WINDMILL:
switch (GameResID(resID).toEnum())
{
case EGameResID::GOLD:
case EGameResID::WOOD:
return false;
}
break;
case Obj::MYSTICAL_GARDEN:
if ((resID != GameResID(EGameResID::GOLD)) && (resID != GameResID(EGameResID::GEMS)))
return false;
break;
case Obj::WATER_WHEEL:
case Obj::LEAN_TO:
case Obj::WAGON:
if (resID != GameResID(EGameResID::GOLD))
return false;
break;
default:
return false;
break;
}
return !vstd::contains(ai->alreadyVisited, obj); //for weekly / once visitable
};
std::vector<const CGObjectInstance *> objs;
for (auto obj : ai->visitableObjs)
{
if (givesResource(obj))
objs.push_back(obj);
}
for (auto h : cb->getHeroesInfo())
{
std::vector<const CGObjectInstance *> ourObjs(objs); //copy common objects
for (auto obj : ai->reservedHeroesMap[h]) //add objects reserved by this hero
{
if (givesResource(obj))
ourObjs.push_back(obj);
}
for (auto obj : ourObjs)
{
auto waysToGo = ai->ah->howToVisitObj(h, ObjectIdRef(obj));
vstd::concatenate(ret, waysToGo);
}
}
return ret;
}
TSubgoal CollectRes::whatToDoToAchieve()
{
auto goals = getAllPossibleSubgoals();
auto trade = whatToDoToTrade();
if (!trade->invalid())
goals.push_back(trade);
if (goals.empty())
return sptr(Explore()); //we can always do that
else
return fh->chooseSolution(goals); //TODO: evaluate trading
}
TSubgoal CollectRes::whatToDoToTrade()
{
std::vector<const IMarket *> markets;
std::vector<const CGObjectInstance *> visObjs;
ai->retrieveVisitableObjs(visObjs, true);
for(const CGObjectInstance * obj : visObjs)
{
const auto * m = dynamic_cast<const IMarket*>(obj);
if(m && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE))
{
if(obj->ID == Obj::TOWN)
{
if(obj->tempOwner == ai->playerID)
markets.push_back(m);
}
else
markets.push_back(m);
}
}
boost::sort(markets, [](const IMarket * m1, const IMarket * m2) -> bool
{
return m1->getMarketEfficiency() < m2->getMarketEfficiency();
});
markets.erase(boost::remove_if(markets, [](const IMarket * market) -> bool
{
auto * o = dynamic_cast<const CGObjectInstance *>(market);
// FIXME: disabled broken visitation of external markets
//if(o && !(o->ID == Obj::TOWN && o->tempOwner == ai->playerID))
if(o && o->ID == Obj::TOWN)
{
if(!ai->isAccessible(o->visitablePos()))
return true;
}
return false;
}), markets.end());
if (!markets.size())
{
for (const CGTownInstance * t : cb->getTownsInfo())
{
if (cb->canBuildStructure(t, BuildingID::MARKETPLACE) == EBuildingState::ALLOWED)
return sptr(BuildThis(BuildingID::MARKETPLACE, t).setpriority(2));
}
}
else
{
const IMarket * m = markets.back();
//attempt trade at back (best prices)
int howManyCanWeBuy = 0;
for (GameResID i = EGameResID::WOOD; i <= EGameResID::GOLD; ++i)
{
if (i.getNum() == resID)
continue;
int toGive = -1;
int toReceive = -1;
m->getOffer(i, resID, toGive, toReceive, EMarketMode::RESOURCE_RESOURCE);
assert(toGive > 0 && toReceive > 0);
howManyCanWeBuy += toReceive * (ai->ah->freeResources()[i] / toGive);
}
if (howManyCanWeBuy >= value)
{
auto * o = dynamic_cast<const CGObjectInstance *>(m);
auto backObj = cb->getTopObj(o->visitablePos()); //it'll be a hero if we have one there; otherwise marketplace
assert(backObj);
auto objid = o->id.getNum();
if (backObj->tempOwner != ai->playerID) //top object not owned
{
return sptr(VisitObj(objid)); //just go there
}
else //either it's our town, or we have hero there
{
return sptr(Trade(static_cast<EGameResID>(resID), value, objid).setisElementar(true)); //we can do this immediately
}
}
}
return sptr(Invalid()); //cannot trade
}
bool CollectRes::fulfillsMe(TSubgoal goal)
{
if (goal->resID == resID)
if (goal->value >= value)
return true;
return false;
}
|