瀏覽代碼

AI walks around randomly until movement is almost out. Battle causes crash.

Trevor Standley 16 年之前
父節點
當前提交
3cdff92e42
共有 2 個文件被更改,包括 120 次插入7 次删除
  1. 115 5
      AI/GeniusAI/CGeniusAI.cpp
  2. 5 2
      AI/GeniusAI/CGeniusAI.h

+ 115 - 5
AI/GeniusAI/CGeniusAI.cpp

@@ -22,7 +22,7 @@ void MsgBox(const char *msg, bool messageBox)
 }
 
 CGeniusAI::CGeniusAI()
-	: m_generalAI()
+	: m_generalAI(),turn(0)
 {
 }
 
@@ -43,21 +43,123 @@ void CGeniusAI::init(ICallback *CB)
 	MsgBox(info.c_str());
 }
 
+unsigned long randomFromInt(unsigned long in)
+{
+	return (in*214013+2531011);
+}
+
+void CGeniusAI::doHero(const CGHeroInstance * h)
+{
+
+	
+	if(!h==NULL)
+	{
+		int3 destination, pos;
+		pos=h->convertPosition(h->pos,false);
+		
+		vector<int3> buildingPath;
+
+		int movement = h->movement;
+		int usedMovement = 0;
+		int attempts = 0;
+		CPath path;
+		do{
+			do{
+				destination=pos;
+				attempts++;
+				destination.x+=randomFromInt((attempts*1243)+turn)%9-4;
+				destination.y+=randomFromInt((attempts*1243)+turn+1234)%9-4;
+			}while((!m_cb->getPath(pos,destination,h,path)||(path.nodes[0].dist>=(movement-usedMovement))) && attempts<100);
+			
+			if(attempts<100)
+			{
+				pos = destination;
+				usedMovement += path.nodes[0].dist;
+				path.convert(0);
+				for(int i = path.nodes.size()-2;i>=0;i--)
+					buildingPath.push_back(path.nodes[i].coord);
+			}
+			else break;
+		}while(movement-usedMovement>=50);
+
+		for(int i = 0; i < buildingPath.size();i++)
+		{
+			m_cb->moveHero(h,buildingPath[i]);
+			//std::cout << "(" << buildingPath[i].x << ", " << buildingPath[i].y << ")" << std::endl;
+		}
+
+	}
+
+}
+
+void CGeniusAI::doTown(const CGTownInstance * t)
+{
+	if(m_cb->howManyHeroes(true)<3)		//recrute up to 3 heroes
+	{
+		if(t->visitingHero==NULL)
+		{
+			std::vector<const CGHeroInstance *> toBuy = m_cb->getAvailableHeroes(t);
+			if(toBuy[0]->army.slots.size()>1)//only buy heros with units
+			{
+				m_cb->recruitHero(t,toBuy[0]);
+			}
+		}
+
+	}
+
+//	m_cb->recruitCreatures(t, ui32 ID, ui32 amount)
+
+	
+}
+
+
 void CGeniusAI::yourTurn()
 {
+	//static boost::mutex mutex;
+	//boost::mutex::scoped_lock scoped_lock(mutex);
+	turn++;
+
+	std::cout << "AI Player " <<m_cb->getMySerial()<< " with " <<  m_cb->howManyHeroes(true) << " heroes. " << std::endl;
+
+	
+	std::cout << m_cb->getResourceAmount(0) << " wood. ";
+	std::cout << m_cb->getResourceAmount(1) << " mercury. ";
+	std::cout << m_cb->getResourceAmount(2) << " ore. ";
+	std::cout << m_cb->getResourceAmount(3) << " sulfer. ";
+	std::cout << m_cb->getResourceAmount(4) << " cristal. ";
+	std::cout << m_cb->getResourceAmount(5) << " gems. ";
+	std::cout << m_cb->getResourceAmount(6) << " gold.";
+	std::cout << std::endl;
+
+
+	std::vector < const CGHeroInstance *> heroes = m_cb->getHeroesInfo();	
+	for(std::vector < const CGHeroInstance *>::iterator i = heroes.begin(); i < heroes.end(); i++)
+		doHero(*i);
+
+//	std::vector < const CGTownInstance *> towns = m_cb->getTownsInfo();	
+//	for(std::vector < const CGTownInstance *>::iterator i = towns.begin(); i < towns.end(); i++)
+//		doTown(*i);
+
+
 	m_cb->endTurn();
 }
 
-void CGeniusAI::heroKilled(const CGHeroInstance *)
+void CGeniusAI::heroKilled(const CGHeroInstance * hero)
 {
+
 }
 
-void CGeniusAI::heroCreated(const CGHeroInstance *)
+void CGeniusAI::heroCreated(const CGHeroInstance *hero)
 {
+
 }
 
-void CGeniusAI::heroMoved(const TryMoveHero &)
+void CGeniusAI::heroMoved(const TryMoveHero &TMH)
 {
+	MsgBox("** CGeniusAI::heroMoved **");
+
+	
+
 }
 
 void CGeniusAI::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
@@ -116,9 +218,10 @@ void CGeniusAI::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
 /**
  * called by engine when battle starts; side=0 - left, side=1 - right
  */
+
 void CGeniusAI::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side)
 {
-	assert(!m_battleLogic);
+	assert(!m_battleLogic); //************** assert fails when AI starts two battles at same time? ***************
 	m_battleLogic = new BattleAI::CBattleLogic(m_cb, army1, army2, tile, hero1, hero2, side);
 
 	MsgBox("** CGeniusAI::battleStart **");
@@ -128,6 +231,13 @@ void CGeniusAI::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile,
  */
 void CGeniusAI::battleEnd(BattleResult *br)
 {
+	/*switch(br->winner)
+	{
+		case 0:	std::cout << "The winner is the attacker." << std::endl;break;
+		case 1:	std::cout << "The winner is the defender." << std::endl;break;
+		case 2:	std::cout << "It's a draw." << std::endl;break;
+	};*/
+	
 	delete m_battleLogic;
 	m_battleLogic = NULL;
 

+ 5 - 2
AI/GeniusAI/CGeniusAI.h

@@ -4,7 +4,6 @@
 #include "Common.h"
 #include "BattleLogic.h"
 #include "GeneralAI.h"
-
 namespace GeniusAI {
 
 class CGeniusAI : public CGlobalAI
@@ -13,6 +12,11 @@ private:
 	ICallback*							m_cb;
 	GeniusAI::BattleAI::CBattleLogic*	m_battleLogic;
 	GeniusAI::GeneralAI::CGeneralAI		m_generalAI;
+
+	void doHero(const CGHeroInstance * h);
+	void doTown(const CGTownInstance * t);
+	int turn;
+
 public:
 	CGeniusAI();
 	virtual ~CGeniusAI();
@@ -46,7 +50,6 @@ public:
 	virtual void battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting);
 	virtual BattleAction activeStack(int stackID);
 };
-
 }
 
 #endif // __CGENIUSAI_H__