1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-30 04:30:42 +02:00

Merge pull request #481 from vcmi/VCAI_fixes

Vcai fixes
This commit is contained in:
DjWarmonger 2018-08-22 10:41:12 +02:00 committed by GitHub
commit 2b6bb0f4c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 19 deletions

View File

@ -105,8 +105,13 @@ boost::optional<BuildingID> BuildingManager::canBuildAnyStructure(const CGTownIn
{ {
if (t->hasBuilt(building)) if (t->hasBuilt(building))
continue; continue;
if (cb->canBuildStructure(t, building)) switch (cb->canBuildStructure(t, building))
{
case EBuildingState::ALLOWED:
case EBuildingState::NO_RESOURCES: //TODO: allow this via optional parameter?
return boost::optional<BuildingID>(building); return boost::optional<BuildingID>(building);
break;
}
} }
return boost::optional<BuildingID>(); //Can't build anything return boost::optional<BuildingID>(); //Can't build anything
} }

View File

@ -1052,13 +1052,18 @@ TSubgoal BuildThis::whatToDoToAchieve()
} }
if (town) //we have specific town to build this if (town) //we have specific town to build this
{ {
if (cb->canBuildStructure(town, b) != EBuildingState::ALLOWED) //FIXME: decompose further? kind of mess if we're here switch (cb->canBuildStructure(town, b))
throw cannotFulfillGoalException("Not possible to build"); {
else case EBuildingState::ALLOWED:
case EBuildingState::NO_RESOURCES:
{ {
auto res = town->town->buildings.at(BuildingID(bid))->resources; auto res = town->town->buildings.at(BuildingID(bid))->resources;
return ah->whatToDo(res, iAmElementar()); //realize immediately or gather resources return ah->whatToDo(res, iAmElementar()); //realize immediately or gather resources
} }
break;
default:
throw cannotFulfillGoalException("Not possible to build");
}
} }
else else
throw cannotFulfillGoalException("Cannot find town to build this"); throw cannotFulfillGoalException("Cannot find town to build this");
@ -1137,13 +1142,11 @@ TGoalVec Goals::CollectRes::getAllPossibleSubgoals()
} }
for (auto obj : ourObjs) for (auto obj : ourObjs)
{ {
if (ai->isAccessibleForHero(obj->visitablePos(), h)) auto pos = obj->visitablePos();
{ if (ai->isTileNotReserved(h, pos)) //further decomposition and evaluation will be handled by VisitObj
//further decomposition and evaluation will be handled by VisitObj
ret.push_back(sptr(Goals::VisitObj(obj->id.getNum()).sethero(h).setisAbstract(true))); ret.push_back(sptr(Goals::VisitObj(obj->id.getNum()).sethero(h).setisAbstract(true)));
} }
} }
}
return ret; return ret;
} }
@ -1227,7 +1230,7 @@ TSubgoal Goals::CollectRes::whatToDoToTrade()
else //either it's our town, or we have hero there else //either it's our town, or we have hero there
{ {
Goals::Trade trade(resID, value, objid); Goals::Trade trade(resID, value, objid);
return sptr(trade.setisElementar(true)); //we can do this immediately - highest priority return sptr(trade.setisElementar(true)); //we can do this immediately
} }
} }
} }
@ -1454,7 +1457,7 @@ TGoalVec Goals::Build::getAllPossibleSubgoals()
} }
if (ret.empty()) if (ret.empty())
throw cannotFulfillGoalException("BUILD has been realized as much as possible."); //who catches it and what for? throw cannotFulfillGoalException("BUILD has been realized as much as possible.");
else else
return ret; return ret;
} }
@ -1522,7 +1525,9 @@ TGoalVec GatherArmy::getAllPossibleSubgoals()
} }
} }
//build dwelling //build dwelling
auto bid = ah->canBuildAnyStructure(t, std::vector<BuildingID>(unitsSource, unitsSource + ARRAY_COUNT(unitsSource)), 8 - cb->getDate(Date::DAY_OF_WEEK)); //TODO: plan building over multiple turns?
//auto bid = ah->canBuildAnyStructure(t, std::vector<BuildingID>(unitsSource, unitsSource + ARRAY_COUNT(unitsSource)), 8 - cb->getDate(Date::DAY_OF_WEEK));
auto bid = ah->canBuildAnyStructure(t, std::vector<BuildingID>(unitsSource, unitsSource + ARRAY_COUNT(unitsSource)), 1);
if (bid.is_initialized()) if (bid.is_initialized())
{ {
auto goal = sptr(BuildThis(bid.get(), t).setpriority(priority)); auto goal = sptr(BuildThis(bid.get(), t).setpriority(priority));

View File

@ -432,7 +432,7 @@ public:
resID = rid; resID = rid;
value = val; value = val;
objid = Objid; objid = Objid;
priority = 10; //do it immediately priority = 3; //trading is instant, but picking resources is free
} }
TSubgoal whatToDoToAchieve() override; TSubgoal whatToDoToAchieve() override;
bool operator==(AbstractGoal & g) override; bool operator==(AbstractGoal & g) override;

View File

@ -2123,15 +2123,14 @@ void VCAI::tryRealize(Goals::Trade & g) //trade
int toGive, toGet; int toGive, toGet;
m->getOffer(res, g.resID, toGive, toGet, EMarketMode::RESOURCE_RESOURCE); m->getOffer(res, g.resID, toGive, toGet, EMarketMode::RESOURCE_RESOURCE);
toGive = toGive * (it->resVal / toGive); toGive = toGive * (it->resVal / toGive); //round down
//TODO trade only as much as needed //TODO trade only as much as needed
if (toGive) //don't try to sell 0 resources if (toGive) //don't try to sell 0 resources
{ {
cb->trade(obj, EMarketMode::RESOURCE_RESOURCE, res, g.resID, toGive); cb->trade(obj, EMarketMode::RESOURCE_RESOURCE, res, g.resID, toGive);
logAi->debug("Traded %d of %s for %d of %s at %s", toGive, res, toGet, g.resID, obj->getObjectName()); accquiredResources = toGet * (it->resVal / toGive);
accquiredResources += toGet; //FIXME: this is incorrect, always equal to 1 logAi->debug("Traded %d of %s for %d of %s at %s", toGive, res, accquiredResources, g.resID, obj->getObjectName());
} }
//if (accquiredResources >= g.value)
if (ah->freeResources()[g.resID] >= g.value) if (ah->freeResources()[g.resID] >= g.value)
throw goalFulfilledException(sptr(g)); //we traded all we needed throw goalFulfilledException(sptr(g)); //we traded all we needed
} }