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

More fixes for trading. Seems to work now.

This commit is contained in:
DJWarmonger 2018-08-04 10:20:40 +02:00
parent a5ca32ad38
commit 2aa0a6fe2f
4 changed files with 55 additions and 9 deletions

View File

@ -66,6 +66,13 @@ std::string Goals::AbstractGoal::name() const //TODO: virtualize
case COLLECT_RES:
desc = "COLLECT RESOURCE " + GameConstants::RESOURCE_NAMES[resID] + " (" + boost::lexical_cast<std::string>(value) + ")";
break;
case TRADE:
{
auto obj = cb->getObjInstance(ObjectInstanceID(objid));
if (obj)
desc = (boost::format("TRADE %d of %s at %s") % value % GameConstants::RESOURCE_NAMES[resID] % obj->getObjectName()).str();
}
break;
case GATHER_TROOPS:
desc = "GATHER TROOPS";
break;
@ -158,6 +165,7 @@ bool Goals::AbstractGoal::operator==(AbstractGoal & g)
//no check atm
case COLLECT_RES:
case TRADE: //TODO
return (resID == g.resID); //every hero may collect resources
break;
case GATHER_TROOPS:
@ -224,6 +232,19 @@ namespace Goals
}
}
TSubgoal Trade::whatToDoToAchieve()
{
return iAmElementar();
}
bool Trade::operator==(CollectRes & g)
{
if (g.resID == resID)
if (g.value == value) //TODO: not sure if that logic is consitent
return true;
return false;
}
//TSubgoal AbstractGoal::whatToDoToAchieve()
//{
// logAi->debug("Decomposing goal of type %s",name());
@ -1079,7 +1100,8 @@ TSubgoal Goals::CollectRes::whatToDoToTrade()
}
else //either it's our town, or we have hero there
{
return sptr(setobjid(objid).setisElementar(true).setpriority(10)); //we can do this immediately - highest priority
Goals::Trade trade(resID, value, objid);
return sptr(trade.setisElementar(true)); //we can do this immediately - highest priority
}
}
}
@ -1099,8 +1121,6 @@ bool CollectRes::fulfillsMe(TSubgoal goal)
bool Goals::CollectRes::operator==(CollectRes & g)
{
if (isElementar != g.isElementar) //elementar means ready to trade on Marketplace / Trading Post
return false;
if (g.resID == resID)
if (g.value == value) //TODO: not sure if that logic is consitent
return true;

View File

@ -56,7 +56,8 @@ enum EGoals
VISIT_TILE, //tile, in conjunction with hero elementar; assumes tile is reachable
CLEAR_WAY_TO,
DIG_AT_TILE,//elementar with hero on tile
BUY_ARMY //at specific town
BUY_ARMY, //at specific town
TRADE //val resID at object objid
};
//method chaining + clone pattern
@ -414,7 +415,26 @@ public:
TGoalVec getAllPossibleSubgoals() override;
TSubgoal whatToDoToAchieve() override;
TSubgoal whatToDoToTrade();
bool fulfillsMe(TSubgoal goal) override;
bool fulfillsMe(TSubgoal goal) override; //TODO: Trade
bool operator==(CollectRes & g);
};
class DLL_EXPORT Trade : public CGoal<Trade>
{
public:
Trade()
: CGoal(Goals::TRADE)
{
}
Trade(int rid, int val, int Objid)
: CGoal(Goals::TRADE)
{
resID = rid;
value = val;
objid = Objid;
priority = 10; //do it immediately
}
TSubgoal whatToDoToAchieve() override;
bool operator==(CollectRes & g);
};

View File

@ -2161,11 +2161,12 @@ void VCAI::tryRealize(Goals::DigAtTile & g)
}
}
void VCAI::tryRealize(Goals::CollectRes & g) //trade
void VCAI::tryRealize(Goals::Trade & g) //trade
{
if(ah->freeResources()[g.resID] >= g.value) //goal is already fulfilled. Why we need this check, anyway?
throw goalFulfilledException(sptr(g));
int accquiredResources = 0;
if(const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(g.objid), false))
{
if(const IMarket * m = IMarket::castFrom(obj, false))
@ -2179,8 +2180,13 @@ void VCAI::tryRealize(Goals::CollectRes & g) //trade
m->getOffer(i, g.resID, toGive, toGet, EMarketMode::RESOURCE_RESOURCE);
toGive = toGive * (cb->getResourceAmount(i) / toGive);
//TODO trade only as much as needed
cb->trade(obj, EMarketMode::RESOURCE_RESOURCE, i, g.resID, toGive);
if (ah->freeResources()[g.resID] >= g.value)
if (toGive) //don't try to sell 0 resources
{
cb->trade(obj, EMarketMode::RESOURCE_RESOURCE, i, g.resID, toGive);
logAi->debug("Traded %d of %s for %d of %s at %s", toGive, i, toGet, g.resID, obj->getObjectName());
accquiredResources += toGet;
}
if (accquiredResources >= g.value) //we traded all we needed
throw goalFulfilledException(sptr(g));
}

View File

@ -190,7 +190,7 @@ public:
void tryRealize(Goals::VisitHero & g);
void tryRealize(Goals::BuildThis & g);
void tryRealize(Goals::DigAtTile & g);
void tryRealize(Goals::CollectRes & g);
void tryRealize(Goals::Trade & g);
void tryRealize(Goals::Build & g);
void tryRealize(Goals::BuyArmy & g);
void tryRealize(Goals::Invalid & g);