Bläddra i källkod

[experiments2] More.

Michał W. Urbańczyk 12 år sedan
förälder
incheckning
0b494a7bb0

+ 0 - 15
AI/BattleAI/BattleAI.cpp

@@ -25,22 +25,7 @@ auto maxElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(boos
 #define LOGL(text) print(text)
 #define LOGL(text) print(text)
 #define LOGFL(text, formattingEl) print(boost::str(boost::format(text) % formattingEl))
 #define LOGFL(text, formattingEl) print(boost::str(boost::format(text) % formattingEl))
 
 
-struct Priorities
-{
-	double manaValue;
-	double generalResourceValueModifier;
-	std::vector<double> resourceTypeBaseValues;
-	std::function<double(const CStack *)> stackEvaluator;
-
 
 
-	Priorities()
-	{
-		manaValue = 0.;
-		generalResourceValueModifier = 1.;
-		range::copy(VLC->objh->resVals, std::back_inserter(resourceTypeBaseValues));
-		stackEvaluator = [](const CStack*){ return 1.0; };
-	}
-} priorities;
 
 
 
 
 int distToNearestNeighbour(BattleHex hex, const ReachabilityInfo::TDistances& dists, BattleHex *chosenHex = NULL)
 int distToNearestNeighbour(BattleHex hex, const ReachabilityInfo::TDistances& dists, BattleHex *chosenHex = NULL)

+ 17 - 10
AI/VisitObjects/ObjectVisitingModule.cpp

@@ -29,11 +29,17 @@ void ObjectVisitingModule::receivedMessage(const boost::any &msg)
 		{
 		{
 			destinations = getDestinations();
 			destinations = getDestinations();
 			logGlobal->debugStream() << "Added as destination all possible objects:";
 			logGlobal->debugStream() << "Added as destination all possible objects:";
+			printObjects(destinations);
 		}
 		}
 		if(w2 == "list")
 		if(w2 == "list")
 		{
 		{
 			auto dsts = getDestinations();
 			auto dsts = getDestinations();
 			logGlobal->debugStream() << "Possible visit destinations:";
 			logGlobal->debugStream() << "Possible visit destinations:";
+			printObjects(dsts);
+		}
+		if(w2 == "clear")
+		{
+			destinations.clear();
 		}
 		}
 		if(w2 == "add")
 		if(w2 == "add")
 		{
 		{
@@ -55,6 +61,11 @@ void ObjectVisitingModule::receivedMessage(const boost::any &msg)
 			}
 			}
 		}
 		}
 	}
 	}
+	
+	if(auto objs = boost::any_cast<std::vector<const CGObjectInstance*>>(&msg))
+	{
+		destinations = *objs;
+	}
 }
 }
 
 
 bool compareNodes(const CGPathNode *lhs, const CGPathNode *rhs)
 bool compareNodes(const CGPathNode *lhs, const CGPathNode *rhs)
@@ -78,14 +89,13 @@ void ObjectVisitingModule::executeInternal()
 	{
 	{
 		cb->setSelection(h);
 		cb->setSelection(h);
 
 
-		auto leftToVisit = destinations;
-
 		while(true)
 		while(true)
 		{
 		{
-			vstd::erase_if(destinations, [this](const CGObjectInstance *obj) { 
-				return vstd::contains(visitedThisWeek, obj); });
+			auto leftToVisit = destinations;
+			vstd::erase_if(leftToVisit, [this](const CGObjectInstance *obj) 
+				{ return vstd::contains(visitedThisWeek, obj); });
 
 
-			if(destinations.empty())
+			if(leftToVisit.empty())
 				return;
 				return;
 
 
 			auto isCloser = [this] (const CGObjectInstance *lhs, const CGObjectInstance *rhs) -> bool
 			auto isCloser = [this] (const CGObjectInstance *lhs, const CGObjectInstance *rhs) -> bool
@@ -94,14 +104,11 @@ void ObjectVisitingModule::executeInternal()
 			};
 			};
 
 
 			const auto toVisit = *range::min_element(leftToVisit, isCloser);
 			const auto toVisit = *range::min_element(leftToVisit, isCloser);
-			const int3 moveDest = (toVisit)->visitablePos();
-
-			CGPath path;
-			cb->getPath2(moveDest, path);
 
 
 			try
 			try
 			{
 			{
-				bool ret = moveHero(h, moveDest);
+				if(!moveHero(h, toVisit->visitablePos()))
+					break;
 			}
 			}
 			catch(...)
 			catch(...)
 			{
 			{

+ 3 - 3
AI/VisitObjects/ObjectVisitingModule.h

@@ -12,9 +12,9 @@ public:
 	ObjectVisitingModule(void);
 	ObjectVisitingModule(void);
 	~ObjectVisitingModule(void);
 	~ObjectVisitingModule(void);
 
 
-	virtual void receivedMessage(const boost::any &msg);
-	virtual void executeInternal();
-	virtual void heroVisit(const CGHeroInstance *visitor, const CGObjectInstance *visitedObj, bool start);
+	virtual void receivedMessage(const boost::any &msg)  override;
+	virtual void executeInternal() override;
+	virtual void heroVisit(const CGHeroInstance *visitor, const CGObjectInstance *visitedObj, bool start) override;
 
 
 	bool isInterestingObject(const CGObjectInstance *obj) const;
 	bool isInterestingObject(const CGObjectInstance *obj) const;
 	std::vector<const CGObjectInstance *> getDestinations() const;
 	std::vector<const CGObjectInstance *> getDestinations() const;

+ 1 - 8
AI/VisitObjects/main.cpp

@@ -7,16 +7,9 @@
 #define strcpy_s(a, b, c) strncpy(a, c, b)
 #define strcpy_s(a, b, c) strncpy(a, c, b)
 #endif
 #endif
 
 
-const char *g_cszAiName = "ObjectVisitingModule";
-
-extern "C" DLL_EXPORT int GetGlobalAiVersion()
-{
-	return AI_INTERFACE_VER;
-}
-
 extern "C" DLL_EXPORT void GetAiName(char* name)
 extern "C" DLL_EXPORT void GetAiName(char* name)
 {
 {
-	strcpy_s(name, strlen(g_cszAiName) + 1, g_cszAiName);
+	strcpy_s(name, 100, "ObjectVisitingModule");
 }
 }
 
 
 extern "C" DLL_EXPORT void GetNewAutomationModule(shared_ptr<ObjectVisitingModule> &out)
 extern "C" DLL_EXPORT void GetNewAutomationModule(shared_ptr<ObjectVisitingModule> &out)

+ 11 - 1
CCallback.cpp

@@ -598,4 +598,14 @@ CAutomationCallback::CAutomationCallback(CGameState * GS, boost::optional<Player
 	: CCallback(GS, Player, C)
 	: CCallback(GS, Player, C)
 {
 {
 	automationModule = module;
 	automationModule = module;
-}
+}
+
+void CAutomationCallback::sendMessage(const boost::any &msg)
+{
+	cl->playerint[*player]->receiveMessageFromAutomation(msg);
+}
+
+shared_ptr<CAutomationModule> CAutomationCallback::getModule()
+{
+	return automationModule.lock();
+}

+ 1 - 1
CCallback.h

@@ -207,5 +207,5 @@ public:
 
 
 
 
 	//new, automation specific
 	//new, automation specific
-	void sendMessage(const boost::any &msg);
+	virtual void sendMessage(const boost::any &msg);
 };
 };

+ 6 - 0
client/CAdvmapInterface.cpp

@@ -522,6 +522,12 @@ void CAdvMapInt::fendTurn()
 	if(!LOCPLINT->makingTurn)
 	if(!LOCPLINT->makingTurn)
 		return;
 		return;
 
 
+	if(LOCPLINT->duringAutomationExecution)
+	{
+		logGlobal->warnStream() <<"Cannot end turn during the automation run!";
+		return;
+	}
+
 	if ( settings["adventure"]["heroReminder"].Bool())
 	if ( settings["adventure"]["heroReminder"].Bool())
 	{
 	{
 		for (int i = 0; i < LOCPLINT->wanderingHeroes.size(); i++)
 		for (int i = 0; i < LOCPLINT->wanderingHeroes.size(); i++)

+ 6 - 2
client/CPlayerInterface.cpp

@@ -107,6 +107,7 @@ struct OCM_HLP_CGIN
 
 
 CPlayerInterface::CPlayerInterface(PlayerColor Player)
 CPlayerInterface::CPlayerInterface(PlayerColor Player)
 {
 {
+	duringAutomationExecution = false;
     logGlobal->traceStream() << "\tHuman player interface for player " << Player << " being constructed";
     logGlobal->traceStream() << "\tHuman player interface for player " << Player << " being constructed";
 	observerInDuelMode = false;
 	observerInDuelMode = false;
 	howManyPeople++;
 	howManyPeople++;
@@ -996,6 +997,8 @@ void CPlayerInterface::showInfoDialog(const std::string &text, CComponent * comp
 void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<CComponent*> & components, int soundID, bool delComps)
 void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<CComponent*> & components, int soundID, bool delComps)
 {
 {
 	waitWhileDialog();
 	waitWhileDialog();
+	if(duringAutomationExecution)
+		return;
 
 
 	if (settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed())
 	if (settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed())
 	{
 	{
@@ -2180,8 +2183,6 @@ void CPlayerInterface::acceptTurn()
 	if(howManyPeople > 1)
 	if(howManyPeople > 1)
 		adventureInt->startTurn();
 		adventureInt->startTurn();
 
 
-	cb->executeAutomation();
-
 	//select first hero if available.
 	//select first hero if available.
 	//TODO: check if hero is slept
 	//TODO: check if hero is slept
 	adventureInt->heroList.update();
 	adventureInt->heroList.update();
@@ -2503,7 +2504,10 @@ void CPlayerInterface::receivedConsoleMessage(const std::string &msg)
 	}
 	}
 	else if(s1 == "exec")
 	else if(s1 == "exec")
 	{
 	{
+		duringAutomationExecution = true;
+		auto pimUnlock = vstd::makeUnlockGuard(*pim);
 		cb->executeAutomation();
 		cb->executeAutomation();
+		duringAutomationExecution = false;
 	}
 	}
 	else if(s1 == "give")
 	else if(s1 == "give")
 	{
 	{

+ 1 - 0
client/CPlayerInterface.h

@@ -85,6 +85,7 @@ enum
 class CPlayerInterface : public CGameInterface, public IUpdateable
 class CPlayerInterface : public CGameInterface, public IUpdateable
 {
 {
 public:
 public:
+	bool duringAutomationExecution;
 	bool observerInDuelMode;
 	bool observerInDuelMode;
 
 
 	//minor interfaces
 	//minor interfaces

+ 22 - 2
client/NetPacksClient.cpp

@@ -43,6 +43,12 @@
 		{													\
 		{													\
 		if(vstd::contains(cl->playerint,player))			\
 		if(vstd::contains(cl->playerint,player))			\
 			cl->playerint[player]->function(__VA_ARGS__);	\
 			cl->playerint[player]->function(__VA_ARGS__);	\
+															\
+		if(cl->additionalBattleInts.count(player))				\
+		{														\
+			BOOST_FOREACH(auto bInt, cl->additionalPlayerInts[player])\
+				bInt->function(__VA_ARGS__);					\
+		}														\
 		}while(0)
 		}while(0)
 
 
 #define INTERFACE_CALL_IF_PRESENT(player,function,...) 				\
 #define INTERFACE_CALL_IF_PRESENT(player,function,...) 				\
@@ -247,7 +253,19 @@ void DisassembledArtifact::applyCl( CClient *cl )
 void HeroVisit::applyCl( CClient *cl )
 void HeroVisit::applyCl( CClient *cl )
 {
 {
 	assert(hero);
 	assert(hero);
-	INTERFACE_CALL_IF_PRESENT(hero->tempOwner, heroVisit, hero, obj, starting);
+	do													
+	{													
+		if(vstd::contains(cl->playerint,hero->tempOwner))			
+			cl->playerint[hero->tempOwner]->heroVisit(hero, obj, starting);	
+
+		if(cl->additionalPlayerInts.count(hero->tempOwner))				
+		{														
+			BOOST_FOREACH(auto bInt, cl->additionalPlayerInts[hero->tempOwner])
+				bInt->heroVisit(hero, obj, starting);					
+		}														
+	}while(0);
+
+//	INTERFACE_CALL_IF_PRESENT(hero->tempOwner, heroVisit, hero, obj, starting);
 }
 }
 
 
 void NewTurn::applyCl( CClient *cl )
 void NewTurn::applyCl( CClient *cl )
@@ -785,7 +803,9 @@ void PlayerBlocked::applyCl( CClient *cl )
 void YourTurn::applyCl( CClient *cl )
 void YourTurn::applyCl( CClient *cl )
 {
 {
 	CALL_IN_ALL_INTERFACES(playerStartsTurn, player);
 	CALL_IN_ALL_INTERFACES(playerStartsTurn, player);
-	CALL_ONLY_THAT_INTERFACE(player,yourTurn);
+
+	if(vstd::contains(cl->playerint,player))
+		cl->playerint[player]->yourTurn();
 }
 }
 
 
 void SaveGame::applyCl(CClient *cl)
 void SaveGame::applyCl(CClient *cl)

+ 8 - 0
lib/CGameInterface.cpp

@@ -296,3 +296,11 @@ std::vector<const CGHeroInstance*> CAutomationModule::getMyHeroes() const
 
 
 	return ret;
 	return ret;
 }
 }
+
+Priorities::Priorities()
+{
+	manaValue = 0.;
+	generalResourceValueModifier = 1.;
+	range::copy(VLC->objh->resVals, std::back_inserter(resourceTypeBaseValues));
+	stackEvaluator = [](const CStack*){ return 1.0; };
+}

+ 12 - 2
lib/CGameInterface.h

@@ -76,6 +76,16 @@ struct ObjectsCeded
 	}
 	}
 };
 };
 
 
+struct DLL_LINKAGE Priorities
+{
+	double manaValue;
+	double generalResourceValueModifier;
+	std::vector<double> resourceTypeBaseValues;
+	std::function<double(const CStack *)> stackEvaluator;
+
+
+	Priorities();
+};
 
 
 class DLL_LINKAGE CBattleGameInterface : public IBattleEventsReceiver
 class DLL_LINKAGE CBattleGameInterface : public IBattleEventsReceiver
 {
 {
@@ -90,6 +100,7 @@ public:
 	//battle call-ins
 	//battle call-ins
 	virtual BattleAction activeStack(const CStack * stack)=0; //called when it's turn of that stack
 	virtual BattleAction activeStack(const CStack * stack)=0; //called when it's turn of that stack
 	virtual void yourTacticPhase(int distance){}; //called when interface has opportunity to use Tactics skill -> use cb->battleMakeTacticAction from this function
 	virtual void yourTacticPhase(int distance){}; //called when interface has opportunity to use Tactics skill -> use cb->battleMakeTacticAction from this function
+	virtual void setPriorities(const Priorities &priorities){}
 
 
 	virtual void saveGame(COSer<CSaveFile> &h, const int version);
 	virtual void saveGame(COSer<CSaveFile> &h, const int version);
 	virtual void loadGame(CISer<CLoadFile> &h, const int version);
 	virtual void loadGame(CISer<CLoadFile> &h, const int version);
@@ -102,6 +113,7 @@ class CGameInterface : public CBattleGameInterface, public IGameEventsReceiver
 public:
 public:
 	virtual void init(shared_ptr<CCallback> CB){};
 	virtual void init(shared_ptr<CCallback> CB){};
 	virtual void yourTurn(){}; //called AFTER playerStartsTurn(player)
 	virtual void yourTurn(){}; //called AFTER playerStartsTurn(player)
+	virtual void receiveMessageFromAutomation(const boost::any &msg){}
 
 
 	//pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
 	//pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
 	virtual void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID)=0;
 	virtual void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID)=0;
@@ -194,7 +206,5 @@ public:
 
 
 	void execute();
 	void execute();
 
 
-	//friend class CAutomationCallback;
 	friend class CCallback;
 	friend class CCallback;
-	//friend class CClient;
 };
 };