1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00
* showing ranges
* improvements in moving
* workaround for neutrals
This commit is contained in:
Michał W. Urbańczyk 2008-08-05 23:33:08 +00:00
parent c7bb04235c
commit d8eb861117
6 changed files with 61 additions and 363 deletions

View File

@ -571,7 +571,8 @@ CCreature CCallback::battleGetCreature(int number)
std::vector<int> CCallback::battleGetAvailableHexes(int ID)
{
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
return gs->battleGetRange(ID);
return gs->curB->getAccessibility(ID);
//return gs->battleGetRange(ID);
}
bool CCallback::battleIsStackMine(int ID)

View File

@ -129,6 +129,7 @@ void BattleInfo::getAccessibilityMap(bool *accessibility)
accessibility[stacks[g]->position+1] = false;
}
}
//TODO: obstacles
}
void BattleInfo::getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide) //send pointer to at least 187 allocated bytes
{
@ -143,23 +144,22 @@ void BattleInfo::getAccessibilityMapForTwoHex(bool *accessibility, bool atackerS
accessibility[b] = false;
}
}
//removing accessibility for side hexes
for(int v=0; v<187; ++v)
if(atackerSide ? (v%17)==1 : (v%17)==15)
accessibility[v] = false;
}
std::vector<int> BattleInfo::getPath(int start, int dest, bool*accessibility)
{
int predecessor[187]; //for getting the Path
void BattleInfo::makeBFS(int start, bool*accessibility, int *predecessor, int *dists) //both pointers must point to the at least 187-elements int arrays
{
//inits
for(int b=0; b<187; ++b)
predecessor[b] = -1;
//bfsing
int dists[187]; //calculated distances
for(int g=0; g<187; ++g)
dists[g] = 100000000;
std::queue<int> hexq; //bfs queue
hexq.push(start);
for(int g=0; g<187; ++g)
dists[g] = 100000000;
dists[hexq.front()] = 0;
int curNext = -1; //for bfs loop only (helper var)
while(!hexq.empty()) //bfs loop
@ -167,50 +167,77 @@ std::vector<int> BattleInfo::getPath(int start, int dest, bool*accessibility)
int curHex = hexq.front();
hexq.pop();
curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
if((curNext > 0) && (accessibility[curNext]) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
if((curNext > 0) && (accessibility[curNext]) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
curNext = curHex - 1;
if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
if((curNext > 0) && (accessibility[curNext]) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
curNext = curHex + 1;
if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
if((curNext < 187) && (accessibility[curNext]) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
if((curNext < 187) && (accessibility[curNext]) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
if((curNext < 187) && (accessibility[curNext]) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
}
};
//following the Path
std::vector<int> BattleInfo::getAccessibility(int stackID)
{
std::vector<int> ret;
bool ac[187];
CStack *s = getStack(stackID);
if(s->creature->isDoubleWide())
getAccessibilityMapForTwoHex(ac,s->attackerOwned);
else
getAccessibilityMap(ac);
int pr[187], dist[187];
makeBFS(s->position,ac,pr,dist);
for(int i=0;i<187;i++)
if(dist[i] <= s->creature->speed)
ret.push_back(i);
return ret;
}
std::vector<int> BattleInfo::getPath(int start, int dest, bool*accessibility)
{
int predecessor[187]; //for getting the Path
int dist[187]; //calculated distances
makeBFS(start,accessibility,predecessor,dist);
//making the Path
std::vector<int> path;
int curElem = dest;
while(curElem != start)
@ -895,225 +922,6 @@ void CGameState::init(StartInfo * si, Mapa * map, int Seed)
}
}
}
bool CGameState::battleMoveCreatureStack(int ID, int dest)
{/*
//first checks
if(curB->stackActionPerformed) //because unit cannot be moved more than once
return false;
unsigned char owner = -1; //owner moved of unit
for(int g=0; g<curB->stacks.size(); ++g)
{
if(curB->stacks[g]->ID == ID)
{
owner = curB->stacks[g]->owner;
break;
}
}
bool stackAtEnd = false; //true if there is a stack at the end of the path (we should attack it)
int numberOfStackAtEnd = -1;
for(int g=0; g<curB->stacks.size(); ++g)
{
if(curB->stacks[g]->position == dest
|| (curB->stacks[g]->creature->isDoubleWide() && curB->stacks[g]->attackerOwned && curB->stacks[g]->position-1 == dest)
|| (curB->stacks[g]->creature->isDoubleWide() && !curB->stacks[g]->attackerOwned && curB->stacks[g]->position+1 == dest))
{
if(curB->stacks[g]->alive)
{
stackAtEnd = true;
numberOfStackAtEnd = g;
break;
}
}
}
//selecting moved stack
CStack * curStack = NULL;
for(int y=0; y<curB->stacks.size(); ++y)
{
if(curB->stacks[y]->ID == ID)
{
curStack = curB->stacks[y];
break;
}
}
if(!curStack)
return false;
//initing necessary tables
bool accessibility[187]; //accesibility of hexes
for(int k=0; k<187; k++)
accessibility[k] = true;
for(int g=0; g<curB->stacks.size(); ++g)
{
if(curB->stacks[g]->ID != ID && curB->stacks[g]->alive) //we don't want to lock enemy's positions and this units' position
{
accessibility[curB->stacks[g]->position] = false;
if(curB->stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
{
if(curB->stacks[g]->attackerOwned)
accessibility[curB->stacks[g]->position-1] = false;
else
accessibility[curB->stacks[g]->position+1] = false;
}
}
}
accessibility[dest] = true;
if(curStack->creature->isDoubleWide()) //locking positions unreachable by two-hex creatures
{
bool mac[187];
for(int b=0; b<187; ++b)
{
//
// && ( ? (curStack->attackerOwned ? accessibility[curNext-1] : accessibility[curNext+1]) : true )
mac[b] = accessibility[b];
if( accessibility[b] && !(curStack->attackerOwned ? accessibility[b-1] : accessibility[b+1]))
{
mac[b] = false;
}
}
mac[curStack->attackerOwned ? curStack->position+1 : curStack->position-1]=true;
for(int v=0; v<187; ++v)
accessibility[v] = mac[v];
//removing accessibility for side hexes
for(int v=0; v<187; ++v)
if(curStack->attackerOwned ? (v%17)==1 : (v%17)==15)
accessibility[v] = false;
}
if(!stackAtEnd && !accessibility[dest])
return false;
int predecessor[187]; //for getting the Path
for(int b=0; b<187; ++b)
predecessor[b] = -1;
//bfsing
int dists[187]; //calculated distances
std::queue<int> hexq; //bfs queue
hexq.push(curStack->position);
for(int g=0; g<187; ++g)
dists[g] = 100000000;
dists[hexq.front()] = 0;
int curNext = -1; //for bfs loop only (helper var)
while(!hexq.empty()) //bfs loop
{
int curHex = hexq.front();
hexq.pop();
curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
curNext = curHex - 1;
if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
curNext = curHex + 1;
if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex;
}
}
//following the Path
if(dists[dest] > curStack->creature->speed && !(stackAtEnd && dists[dest] == curStack->creature->speed+1)) //we can attack a stack if we can go to adjacent hex
return false;
std::vector<int> path;
int curElem = dest;
while(curElem!=curStack->position)
{
path.push_back(curElem);
curElem = predecessor[curElem];
}
for(int v=path.size()-1; v>=0; --v)
{
if(v!=0 || !stackAtEnd) //it's not the last step
{
LOCPLINT->battleStackMoved(ID, path[v], v==path.size()-1, v==0 || (stackAtEnd && v==1) );
curStack->position = path[v];
}
else //if it's last step and we should attack unit at the end
{
LOCPLINT->battleStackAttacking(ID, path[v]);
//counting dealt damage
int finalDmg = calculateDmg(curStack, curB->stacks[numberOfStackAtEnd]);
//applying damages
int cresKilled = finalDmg / curB->stacks[numberOfStackAtEnd]->creature->hitPoints;
int damageFirst = finalDmg % curB->stacks[numberOfStackAtEnd]->creature->hitPoints;
if( curB->stacks[numberOfStackAtEnd]->firstHPleft <= damageFirst )
{
curB->stacks[numberOfStackAtEnd]->amount -= 1;
curB->stacks[numberOfStackAtEnd]->firstHPleft += curB->stacks[numberOfStackAtEnd]->creature->hitPoints - damageFirst;
}
else
{
curB->stacks[numberOfStackAtEnd]->firstHPleft -= damageFirst;
}
int cresInstackBefore = curB->stacks[numberOfStackAtEnd]->amount;
curB->stacks[numberOfStackAtEnd]->amount -= cresKilled;
if(curB->stacks[numberOfStackAtEnd]->amount<=0) //stack killed
{
curB->stacks[numberOfStackAtEnd]->amount = 0;
LOCPLINT->battleStackKilled(curB->stacks[numberOfStackAtEnd]->ID, finalDmg, std::min(cresKilled, cresInstackBefore) , ID, false);
curB->stacks[numberOfStackAtEnd]->alive = false;
}
else
{
LOCPLINT->battleStackIsAttacked(curB->stacks[numberOfStackAtEnd]->ID, finalDmg, std::min(cresKilled, cresInstackBefore), ID, false);
}
//damage applied
}
}
curB->stackActionPerformed = true;
LOCPLINT->actionFinished(BattleAction());*/
return true;
}
bool CGameState::battleAttackCreatureStack(int ID, int dest)
{
int attackedCreaure = -1; //-1 - there is no attacked creature
for(int b=0; b<curB->stacks.size(); ++b) //TODO: make upgrades for two-hex cres.
{
if(curB->stacks[b]->position == dest)
{
attackedCreaure = curB->stacks[b]->ID;
break;
}
}
if(attackedCreaure == -1)
return false;
//LOCPLINT->cb->
return true;
}
bool CGameState::battleShootCreatureStack(int ID, int dest)
{/*
@ -1236,130 +1044,6 @@ int CGameState::calculateDmg(const CStack* attacker, const CStack* defender)
std::vector<int> CGameState::battleGetRange(int ID)
{/*
int initialPlace=-1; //position of unit
int radius=-1; //range of unit
unsigned char owner = -1; //owner of unit
//selecting stack
CStack * curStack = NULL;
for(int y=0; y<curB->stacks.size(); ++y)
{
if(curB->stacks[y]->ID == ID)
{
curStack = curB->stacks[y];
break;
}
}
for(int g=0; g<curB->stacks.size(); ++g)
{
if(curB->stacks[g]->ID == ID)
{
initialPlace = curB->stacks[g]->position;
radius = curB->stacks[g]->creature->speed;
owner = curB->stacks[g]->owner;
break;
}
}
bool accessibility[187]; //accesibility of hexes
for(int k=0; k<187; k++)
accessibility[k] = true;
for(int g=0; g<curB->stacks.size(); ++g)
{
if(curB->stacks[g]->ID != ID && curB->stacks[g]->alive) //we don't want to lock current unit's position
{
accessibility[curB->stacks[g]->position] = false;
if(curB->stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
{
if(curB->stacks[g]->attackerOwned)
accessibility[curB->stacks[g]->position-1] = false;
else
accessibility[curB->stacks[g]->position+1] = false;
}
}
}
if(curStack->creature->isDoubleWide()) //locking positions unreachable by two-hex creatures
{
bool mac[187];
for(int b=0; b<187; ++b)
{
//
// && ( ? (curStack->attackerOwned ? accessibility[curNext-1] : accessibility[curNext+1]) : true )
mac[b] = accessibility[b];
if( accessibility[b] && !(curStack->attackerOwned ? accessibility[b-1] : accessibility[b+1]))
{
mac[b] = false;
}
}
mac[curStack->attackerOwned ? curStack->position+1 : curStack->position-1]=true;
for(int v=0; v<187; ++v)
accessibility[v] = mac[v];
//removing accessibility for side hexes
for(int v=0; v<187; ++v)
if(curStack->attackerOwned ? (v%17)==1 : (v%17)==15)
accessibility[v] = false;
}
int dists[187]; //calculated distances
std::queue<int> hexq; //bfs queue
hexq.push(initialPlace);
for(int g=0; g<187; ++g)
dists[g] = 100000000;
dists[initialPlace] = 0;
int curNext = -1; //for bfs loop only (helper var)
while(!hexq.empty()) //bfs loop
{
int curHex = hexq.front();
hexq.pop();
curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
}
curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
}
curNext = curHex - 1;
if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
}
curNext = curHex + 1;
if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
}
curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
}
curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
{
hexq.push(curNext);
dists[curNext] = dists[curHex] + 1;
}
}
std::vector<int> ret;
for(int i=0; i<187; ++i)
{
if(dists[i]<=radius)
{
ret.push_back(i);
}
}
std::vector<int> additionals;
//adding enemies' positions

View File

@ -67,7 +67,9 @@ struct DLL_EXPORT BattleInfo
CStack * getStackT(int tileID);
void getAccessibilityMap(bool *accessibility); //send pointer to at least 187 allocated bytes
void getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide); //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)
};
class DLL_EXPORT CStack

View File

@ -354,7 +354,18 @@ void CClient::process(int what)
*serv >> sas;
std::cout << "Active stack: " << sas.stack <<std::endl;
gs->apply(&sas);
boost::thread(boost::bind(&CClient::waitForMoveAndSend,this,gs->curB->getStack(sas.stack)->owner));
int owner = gs->curB->getStack(sas.stack)->owner;
if(owner >= PLAYER_LIMIT) //ugly workaround to skip neutral creatures - should be replaced with AI
{
BattleAction ba;
ba.stackNumber = sas.stack;
ba.actionType = 3;
*serv << ui16(3002) << ba;
}
else
{
boost::thread(boost::bind(&CClient::waitForMoveAndSend,this,owner));
}
break;
}
case 3003:

View File

@ -5,7 +5,7 @@ struct BattleAction
ui32 stackNumber;//stack ID, -1 left hero, -2 right hero,
ui8 actionType; // 0 = Cancel BattleAction 1 = Hero cast a spell 2 = Walk 3 = Defend 4 = Retreat from the battle 5 = Surrender 6 = Walk and Attack 7 = Shoot 8 = Wait 9 = Catapult 10 = Monster casts a spell (i.e. Faerie Dragons)
ui16 destinationTile;
ui16 additionalInfo; // e.g. spell number if type is 1 || 10
si32 additionalInfo; // e.g. spell number if type is 1 || 10
template <typename Handler> void serialize(Handler &h, const int version)
{
h & side & stackNumber & actionType & destinationTile & additionalInfo;

View File

@ -606,8 +606,8 @@ void CGameHandler::handleConnection(std::set<int> players, CConnection &c)
// return false;
std::vector<int> path = gs->curB->getPath(curStack->position,ba.destinationTile,accessibility);
for(int v=path.size()-1; v>=0; --v)
int tilesToMove = std::max((int)path.size()-curStack->creature->speed, 0);
for(int v=path.size()-1; v>=tilesToMove; --v)
{
if(v!=0 || !stackAtEnd) //it's not the last step or the last tile is free
{