Browse Source

More fixes for trading. Seems to work now.

DJWarmonger 7 years ago
parent
commit
2aa0a6fe2f
4 changed files with 55 additions and 9 deletions
  1. 23 3
      AI/VCAI/Goals.cpp
  2. 22 2
      AI/VCAI/Goals.h
  3. 9 3
      AI/VCAI/VCAI.cpp
  4. 1 1
      AI/VCAI/VCAI.h

+ 23 - 3
AI/VCAI/Goals.cpp

@@ -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;

+ 22 - 2
AI/VCAI/Goals.h

@@ -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);
 };
 

+ 9 - 3
AI/VCAI/VCAI.cpp

@@ -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));
 			}
 

+ 1 - 1
AI/VCAI/VCAI.h

@@ -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);