Browse Source

[programming challenge] An experiment.

Michał W. Urbańczyk 14 years ago
parent
commit
f5bb1f3ba3

+ 2 - 5
AI/GeniusAI/BattleLogic.cpp

@@ -77,10 +77,7 @@ void CBattleLogic::MakeStatistics(int currentCreatureId)
 	typedef std::vector<const CStack*> vector_stacks;
 	vector_stacks allStacks = m_cb->battleGetStacks();
 	const CStack *currentStack = m_cb->battleGetStackByID(currentCreatureId);
-	if(currentStack->position < 0) //turret
-	{
-		return;
-	}
+
 	/*
 	// find all creatures belong to the enemy
 	std::for_each(allStacks.begin(), allStacks.end(),
@@ -268,7 +265,7 @@ void CBattleLogic::MakeStatistics(int currentCreatureId)
 BattleAction CBattleLogic::MakeDecision(int stackID)
 {
 	const CStack *currentStack = m_cb->battleGetStackByID(stackID);
-	if(currentStack->position < 0 || currentStack->getCreature()->idNumber == 147) //turret or first aid kit
+	if(!currentStack->position.isValid() || currentStack->getCreature()->idNumber == 147) //turret or first aid kit
 	{
 		return BattleAction::makeDefend(currentStack);
 	}

+ 4 - 4
client/CBattleInterface.cpp

@@ -360,7 +360,7 @@ bool CBattleStackAnimation::isToReverseHlp(THex hexFrom, THex hexTo, bool curDir
 
 bool CBattleStackAnimation::isToReverse(THex hexFrom, THex hexTo, bool curDir, bool toDoubleWide, bool toDir)
 {
-	if(hexTo < 0) //turret
+	if(!hexTo.isValid()) //turret
 		return false;
 
 	if(toDoubleWide)
@@ -2533,7 +2533,7 @@ void CBattleInterface::newStack(const CStack * stack)
 {
 	Point coords = CBattleHex::getXYUnitAnim(stack->position, stack->owner == attackingHeroInstance->tempOwner, stack, this);;
 
-	if(stack->position < 0) //turret
+	if(!stack->position.isValid()) //turret
 	{
 		const CCreature & turretCreature = *CGI->creh->creatures[ CGI->creh->factionToTurretCreature[siegeH->town->town->typeID] ];
 		creAnims[stack->ID] = new CCreatureAnimation(turretCreature.animDefName);	
@@ -2714,7 +2714,7 @@ void CBattleInterface::giveCommand(ui8 action, THex tile, ui32 stack, si32 addit
 	case 6:
 		assert(curInt->cb->battleGetStackByPos(additional)); //stack to attack must exist
 	case 2: case 7: case 9:
-		assert(tile < BFIELD_SIZE);
+		assert(tile.isValid());
 		break;
 	}
 
@@ -4173,7 +4173,7 @@ CBattleHero::~CBattleHero()
 Point CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * stack, const CBattleInterface * cbi)
 {
 	Point ret(-500, -500); //returned value
-	if(stack && stack->position < 0) //creatures in turrets
+	if(stack && !stack->position.isValid()) //creatures in turrets
 	{
 		switch(stack->position)
 		{

+ 6 - 0
global.h

@@ -220,6 +220,12 @@ struct THex
 		return std::make_pair(getX(), getY());
 	}
 
+	bool operator<(const THex &rhs) const
+	{
+		return hex < rhs.hex;
+	}
+
+
 	//moving to direction
 	void operator+=(EDir dir)
 	{

+ 5 - 5
lib/BattleState.cpp

@@ -145,7 +145,7 @@ void BattleInfo::getAccessibilityMap(bool *accessibility, bool twoHex, bool atta
 
 	for(unsigned int g=0; g<stacks.size(); ++g)
 	{
-		if(!stacks[g]->alive() || (stackToOmmit && stacks[g]->ID==stackToOmmit->ID) || stacks[g]->position < 0) //we don't want to lock position of this stack (eg. if it's a turret)
+		if(!stacks[g]->alive() || (stackToOmmit && stacks[g]->ID==stackToOmmit->ID) || stacks[g]->position.isValid()) //we don't want to lock position of this stack (eg. if it's a turret)
 			continue;
 
 		accessibility[stacks[g]->position] = false;
@@ -163,7 +163,7 @@ void BattleInfo::getAccessibilityMap(bool *accessibility, bool twoHex, bool atta
 		std::vector<THex> blocked = VLC->heroh->obstacles[obstacles[b].ID].getBlocked(obstacles[b].pos);
 		for(unsigned int c=0; c<blocked.size(); ++c)
 		{
-			if(blocked[c] >=0 && blocked[c] < BFIELD_SIZE)
+			if(blocked[c].isValid())
 				accessibility[blocked[c]] = false;
 		}
 	}
@@ -277,7 +277,7 @@ std::vector<THex> BattleInfo::getAccessibility( const CStack * stack, bool addOc
 	std::vector<THex> ret;
 	bool ac[BFIELD_SIZE];
 
-	if(stack->position < 0) //turrets
+	if(!stack->position.isValid()) //turrets
 		return std::vector<THex>();
 
 	std::set<THex> occupyable;
@@ -1637,7 +1637,7 @@ BattleInfo * BattleInfo::setupBattle( int3 tile, int terrain, int terType, const
 				bool badObstacle = false;
 				for(int b=0; b<block.size(); ++b)
 				{
-					if(block[b] < 0 || block[b] >= BFIELD_SIZE || !obAv[block[b]])
+					if(!block[b].isValid()|| !obAv[block[b]])
 					{
 						badObstacle = true;
 						break;
@@ -1648,7 +1648,7 @@ BattleInfo * BattleInfo::setupBattle( int3 tile, int terrain, int terType, const
 				curB->obstacles.push_back(coi);
 				for(int b=0; b<block.size(); ++b)
 				{
-					if(block[b] >= 0 && block[b] < BFIELD_SIZE)
+					if(block[b].isValid())
 						obAv[block[b]] = false;
 				}
 				toBlock -= block.size();

+ 13 - 5
lib/IGameCallback.cpp

@@ -166,12 +166,20 @@ int CBattleInfoCallback::battleGetBattlefieldType()
 int CBattleInfoCallback::battleGetObstaclesAtTile(THex tile)
 {
 	std::vector<CObstacleInstance> obstacles = battleGetAllObstacles();
-	std::set<THex> coveredHexes;
-	for(int b = 0; b < obstacles.size(); ++b)
+	std::vector<THex> coveredHexes;
+	BOOST_FOREACH(const CObstacleInstance &coi, obstacles)
 	{
-		std::vector<THex> blocked = VLC->heroh->obstacles.find(obstacles[b].ID)->second.getBlocked(obstacles[b].pos);
-		for(int w = 0; w < blocked.size(); ++w)
-			coveredHexes.insert(blocked[w]);
+		std::map<int, CObstacleInfo>::iterator i = VLC->heroh->obstacles.find(coi.ID);
+		if(i == VLC->heroh->obstacles.end())
+		{
+			tlog1 << "Obstacle with a strange id " << coi.ID << std::endl;
+			continue;
+		}
+
+		std::vector<THex> blocked = i->second.getBlocked(coi.pos);
+		BOOST_FOREACH(THex hex, blocked)
+			if(hex.isValid())
+				coveredHexes.push_back(hex);
 	}
 	return vstd::contains(coveredHexes, tile);
 }

+ 2 - 2
server/CGameHandler.cpp

@@ -755,7 +755,7 @@ int CGameHandler::moveStack(int stack, THex dest)
 		*stackAtEnd = gs->curB->getStackT(dest);
 
 	assert(curStack);
-	assert(dest < BFIELD_SIZE);
+	assert(dest.isValid());
 
 	if (gs->curB->tacticDistance)
 	{
@@ -5070,7 +5070,7 @@ void CGameHandler::runBattle()
 
 			const CGHeroInstance * curOwner = gs->curB->battleGetOwner(next);
 
-			if( (next->position < 0 || next->getCreature()->idNumber == 146)	//arrow turret or ballista
+			if( (!next->position.isValid() || next->getCreature()->idNumber == 146)	//arrow turret or ballista
 				&& (!curOwner || curOwner->getSecSkillLevel(CGHeroInstance::ARTILLERY) == 0)) //hero has no artillery
 			{
 				BattleAction attack;

+ 3 - 0
server/CVCMIServer.cpp

@@ -554,6 +554,9 @@ std::string clearBaseName(const std::string &fname)
 {
 	std::string ret = fname;
 	int hlp = ret.find_last_of('/');
+	if(hlp == std::string::npos)
+		hlp = ret.find_first_of('\\');
+
 	if(hlp != std::string::npos)
 	{
 		ret =  ret.substr(hlp+1);