Browse Source

* more support for attacking stacks for back by two hex stacks
* minor typo fixed

mateuszb 16 years ago
parent
commit
0070c93888
7 changed files with 75 additions and 23 deletions
  1. 1 1
      AI/GeniusAI/CGeniusAI.cpp
  2. 37 4
      CBattleInterface.cpp
  3. 2 2
      CCallback.cpp
  4. 2 2
      CCallback.h
  5. 15 11
      CGameState.cpp
  6. 2 2
      CGameState.h
  7. 16 1
      server/CGameHandler.cpp

+ 1 - 1
AI/GeniusAI/CGeniusAI.cpp

@@ -638,7 +638,7 @@ BattleAction CBattleLogic::MakeDecision(int stackID)
 			}
 		}
 
-		std::vector<int> fields = m_cb->battleGetAvailableHexes(stackID);
+		std::vector<int> fields = m_cb->battleGetAvailableHexes(stackID, false);
 		BattleAction ba; 
 		ba.side = 1;
 		//ba.actionType = 6; // go and attack

+ 37 - 4
CBattleInterface.cpp

@@ -1392,7 +1392,18 @@ void CBattleInterface::hexLclicked(int whichOne)
 				if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
 				{
 					CGI->curh->changeGraphic(1, 6); //cursor should be changed
-					giveCommand(2,whichOne,activeStack);
+					if(LOCPLINT->cb->battleGetStackByID(activeStack)->creature->isDoubleWide())
+					{
+						std::vector<int> acc = LOCPLINT->cb->battleGetAvailableHexes(activeStack, false);
+						if(vstd::contains(acc, whichOne))
+							giveCommand(2,whichOne,activeStack);
+						else
+							giveCommand(2,whichOne + (LOCPLINT->cb->battleGetStackByID(activeStack)->attackerOwned ? 1 : -1),activeStack);
+					}
+					else
+					{
+						giveCommand(2,whichOne,activeStack);
+					}
 				}
 			}
 			else if(dest->owner != attackingHeroInstance->tempOwner
@@ -1412,7 +1423,18 @@ void CBattleInterface::hexLclicked(int whichOne)
 					giveCommand(6,whichOne + ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH-1 : BFIELD_WIDTH ),activeStack,whichOne);
 					break;
 				case 8:
-					giveCommand(6,whichOne - 1,activeStack,whichOne);
+					if(LOCPLINT->cb->battleGetStackByID(activeStack)->creature->isDoubleWide() && !LOCPLINT->cb->battleGetStackByID(activeStack)->attackerOwned)
+					{
+						std::vector<int> acc = LOCPLINT->cb->battleGetAvailableHexes(activeStack, false);
+						if(vstd::contains(acc, whichOne))
+							giveCommand(6,whichOne - 1,activeStack,whichOne);
+						else
+							giveCommand(6,whichOne - 2,activeStack,whichOne);
+					}
+					else
+					{
+						giveCommand(6,whichOne - 1,activeStack,whichOne);
+					}
 					break;
 				case 9:
 					giveCommand(6,whichOne - ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH+1 : BFIELD_WIDTH ),activeStack,whichOne);
@@ -1421,7 +1443,18 @@ void CBattleInterface::hexLclicked(int whichOne)
 					giveCommand(6,whichOne - ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH : BFIELD_WIDTH-1 ),activeStack,whichOne);
 					break;
 				case 11:
-					giveCommand(6,whichOne + 1,activeStack,whichOne);
+					if(LOCPLINT->cb->battleGetStackByID(activeStack)->creature->isDoubleWide() && LOCPLINT->cb->battleGetStackByID(activeStack)->attackerOwned)
+					{
+						std::vector<int> acc = LOCPLINT->cb->battleGetAvailableHexes(activeStack, false);
+						if(vstd::contains(acc, whichOne))
+							giveCommand(6,whichOne + 1,activeStack,whichOne);
+						else
+							giveCommand(6,whichOne + 2,activeStack,whichOne);
+					}
+					else
+					{
+						giveCommand(6,whichOne + 1,activeStack,whichOne);
+					}
 					break;
 				}
 				CGI->curh->changeGraphic(1, 6); //cursor should be changed
@@ -1974,7 +2007,7 @@ void CBattleInterface::attackingShowHelper()
 
 void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
 {
-	shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack);
+	shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack, true);
 
 	//preparating background graphic with hexes and shaded hexes
 	blitAt(background, 0, 0, backgroundWithHexes);

+ 2 - 2
CCallback.cpp

@@ -520,10 +520,10 @@ CCreature CCallback::battleGetCreature(int number)
 #endif
 }
 
-std::vector<int> CCallback::battleGetAvailableHexes(int ID)
+std::vector<int> CCallback::battleGetAvailableHexes(int ID, bool addOccupiable)
 {
 	boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
-	return gs->curB->getAccessibility(ID);
+	return gs->curB->getAccessibility(ID, addOccupiable);
 	//return gs->battleGetRange(ID);
 }
 

+ 2 - 2
CCallback.h

@@ -85,7 +85,7 @@ public:
 	virtual std::vector<CStack> battleGetStackQueue()=0; //returns vector of stack in order of their move sequence
 	virtual CCreature battleGetCreature(int number)=0; //returns type of creature by given number of stack
 	//virtual bool battleMoveCreature(int ID, int dest)=0; //moves creature with id ID to dest if possible
-	virtual std::vector<int> battleGetAvailableHexes(int ID)=0; //reutrns numbers of hexes reachable by creature with id ID
+	virtual std::vector<int> battleGetAvailableHexes(int ID, bool addOccupiable)=0; //reutrns numbers of hexes reachable by creature with id ID
 	virtual bool battleIsStackMine(int ID)=0; //returns true if stack with id ID belongs to caller
 	virtual bool battleCanShoot(int ID, int dest)=0; //returns true if unit with id ID can shoot to dest
 };
@@ -172,7 +172,7 @@ public:
 	std::map<int, CStack> battleGetStacks(); //returns stacks on battlefield
 	std::vector<CStack> battleGetStackQueue(); //returns vector of stack in order of their move sequence
 	CCreature battleGetCreature(int number); //returns type of creature by given number of stack
-	std::vector<int> battleGetAvailableHexes(int ID); //reutrns numbers of hexes reachable by creature with id ID
+	std::vector<int> battleGetAvailableHexes(int ID, bool addOccupiable); //reutrns numbers of hexes reachable by creature with id ID
 	bool battleIsStackMine(int ID); //returns true if stack with id ID belongs to caller
 	bool battleCanShoot(int ID, int dest); //returns true if unit with id ID can shoot to dest
 

+ 15 - 11
CGameState.cpp

@@ -186,24 +186,28 @@ void BattleInfo::getAccessibilityMap(bool *accessibility, int stackToOmmit)
 	}
 	//TODO: obstacles
 }
-void BattleInfo::getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit) //send pointer to at least 187 allocated bytes
+void BattleInfo::getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit, bool addOccupiable) //send pointer to at least 187 allocated bytes
 {	
 	bool mac[BFIELD_SIZE];
 	getAccessibilityMap(mac,stackToOmmit);
 	memcpy(accessibility,mac,BFIELD_SIZE);
 
-	for(int b=0; b<BFIELD_SIZE; ++b)
+
+	if(!addOccupiable)
 	{
-		if( mac[b] && !(atackerSide ? mac[b-1] : mac[b+1]))
+		for(int b=0; b<BFIELD_SIZE; ++b)
 		{
-			accessibility[b] = false;
+			if( mac[b] && !(atackerSide ? mac[b-1] : mac[b+1]))
+			{
+				accessibility[b] = false;
+			}
 		}
-	}
 
-	//removing accessibility for side hexes
-	for(int v=0; v<BFIELD_SIZE; ++v)
-		if(atackerSide ? (v%17)==1 : (v%17)==15)
-			accessibility[v] = false;
+		//removing accessibility for side hexes
+		for(int v=0; v<BFIELD_SIZE; ++v)
+			if(atackerSide ? (v%BFIELD_WIDTH)==1 : (v%BFIELD_WIDTH)==(BFIELD_WIDTH - 2))
+				accessibility[v] = false;
+	}
 }
 void BattleInfo::makeBFS(int start, bool*accessibility, int *predecessor, int *dists) //both pointers must point to the at least 187-elements int arrays
 {
@@ -234,13 +238,13 @@ void BattleInfo::makeBFS(int start, bool*accessibility, int *predecessor, int *d
 	}
 };
 
-std::vector<int> BattleInfo::getAccessibility(int stackID)
+std::vector<int> BattleInfo::getAccessibility(int stackID, bool addOccupiable)
 {
 	std::vector<int> ret;
 	bool ac[BFIELD_SIZE];
 	CStack *s = getStack(stackID);
 	if(s->creature->isDoubleWide())
-		getAccessibilityMapForTwoHex(ac,s->attackerOwned,stackID);
+		getAccessibilityMapForTwoHex(ac,s->attackerOwned,stackID,addOccupiable);
 	else
 		getAccessibilityMap(ac,stackID);
 

+ 2 - 2
CGameState.h

@@ -103,10 +103,10 @@ struct DLL_EXPORT BattleInfo
 	CStack * getStack(int stackID);
 	CStack * getStackT(int tileID);
 	void getAccessibilityMap(bool *accessibility, int stackToOmmit=-1); //send pointer to at least 187 allocated bytes
-	void getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit=-1); //send pointer to at least 187 allocated bytes
+	void getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit=-1, bool addOccupiable = false); //send pointer to at least 187 allocated bytes
 	void makeBFS(int start, bool*accessibility, int *predecessor, int *dists); //*accessibility must be prepared bool[187] array; last two pointers must point to the at least 187-elements int arrays - there is written result
 	std::vector<int> getPath(int start, int dest, bool*accessibility);
-	std::vector<int> getAccessibility(int stackID); //returns vector of accessible tiles (taking into account the creature range)
+	std::vector<int> getAccessibility(int stackID, bool addOccupiable); //returns vector of accessible tiles (taking into account the creature range)
 
 	bool isStackBlocked(int ID); //returns true if there is neighbouring enemy stack
 	static signed char mutualPosition(int hex1, int hex2); //returns info about mutual position of given hexes (-1 - they're distant, 0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left)

+ 16 - 1
server/CGameHandler.cpp

@@ -1786,6 +1786,21 @@ void CGameHandler::moveStack(int stack, int dest)
 	else 
 		gs->curB->getAccessibilityMap(accessibility,curStack->ID);
 
+	//shifting destination (if we have double wide stack and we can occupy dest but not be exactly there)
+	if(!stackAtEnd && curStack->creature->isDoubleWide() && !accessibility[dest])
+	{
+		if(curStack->attackerOwned)
+		{
+			if(accessibility[dest+1])
+				dest+=1;
+		}
+		else
+		{
+			if(accessibility[dest-1])
+				dest-=1;
+		}
+	}
+
 	if((stackAtEnd && stackAtEnd!=curStack && stackAtEnd->alive()) || !accessibility[dest])
 		return;
 
@@ -2181,7 +2196,7 @@ void CGameHandler::setupBattle( BattleInfo * curB, int3 tile, CCreatureSet &army
 	}
 	if(hero2)
 	{
-		if(hero1->getArt(13)) //ballista
+		if(hero2->getArt(13)) //ballista
 		{
 			stacks.push_back(new CStack(&VLC->creh->creatures[146], 1, hero2->tempOwner, stacks.size(), false, 255));
 			stacks[stacks.size()-1]->position = 66;