2008-02-25 01:06:27 +02:00
|
|
|
#include "CGameState.h"
|
|
|
|
#include "CGameInterface.h"
|
2008-02-26 20:01:26 +02:00
|
|
|
#include "CPlayerInterface.h"
|
2008-02-25 01:06:27 +02:00
|
|
|
#include <algorithm>
|
2008-02-28 19:00:16 +02:00
|
|
|
#include "SDL_Thread.h"
|
|
|
|
#include "SDL_Extensions.h"
|
2008-03-23 19:25:38 +02:00
|
|
|
#include <queue>
|
2008-03-10 01:06:35 +02:00
|
|
|
|
2008-02-28 19:00:16 +02:00
|
|
|
|
2008-02-25 01:06:27 +02:00
|
|
|
class CMP_stack
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator ()(const CStack* a, const CStack* b)
|
|
|
|
{
|
2008-04-14 21:24:46 +03:00
|
|
|
return (a->creature->speed)>(b->creature->speed);
|
2008-02-25 01:06:27 +02:00
|
|
|
}
|
|
|
|
} cmpst ;
|
|
|
|
|
|
|
|
void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CArmedInstance *hero1, CArmedInstance *hero2)
|
|
|
|
{
|
|
|
|
curB = new BattleInfo();
|
|
|
|
std::vector<CStack*> & stacks = (curB->stacks);
|
|
|
|
|
|
|
|
curB->army1=army1;
|
|
|
|
curB->army2=army2;
|
|
|
|
curB->hero1=dynamic_cast<CGHeroInstance*>(hero1);
|
|
|
|
curB->hero2=dynamic_cast<CGHeroInstance*>(hero2);
|
|
|
|
curB->side1=(hero1)?(hero1->tempOwner):(-1);
|
|
|
|
curB->side2=(hero2)?(hero2->tempOwner):(-1);
|
|
|
|
curB->round = -2;
|
2008-03-29 12:59:18 +02:00
|
|
|
curB->stackActionPerformed = false;
|
2008-02-25 01:06:27 +02:00
|
|
|
for(std::map<int,std::pair<CCreature*,int> >::iterator i = army1->slots.begin(); i!=army1->slots.end(); i++)
|
2008-03-14 20:24:37 +02:00
|
|
|
{
|
2008-04-14 21:24:46 +03:00
|
|
|
stacks.push_back(new CStack(i->second.first,i->second.second,0, stacks.size(), true));
|
2008-03-14 20:24:37 +02:00
|
|
|
stacks[stacks.size()-1]->ID = stacks.size()-1;
|
|
|
|
}
|
2008-03-10 22:19:41 +02:00
|
|
|
//initialization of positions
|
|
|
|
switch(army1->slots.size()) //for attacker
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
stacks[0]->position = 86; //6
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
stacks[0]->position = 35; //3
|
|
|
|
stacks[1]->position = 137; //9
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
stacks[0]->position = 35; //3
|
|
|
|
stacks[1]->position = 86; //6
|
|
|
|
stacks[2]->position = 137; //9
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
stacks[0]->position = 1; //1
|
|
|
|
stacks[1]->position = 69; //5
|
|
|
|
stacks[2]->position = 103; //7
|
|
|
|
stacks[3]->position = 171; //11
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
stacks[0]->position = 1; //1
|
|
|
|
stacks[1]->position = 35; //3
|
|
|
|
stacks[2]->position = 86; //6
|
|
|
|
stacks[3]->position = 137; //9
|
|
|
|
stacks[4]->position = 171; //11
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
stacks[0]->position = 1; //1
|
|
|
|
stacks[1]->position = 35; //3
|
|
|
|
stacks[2]->position = 69; //5
|
|
|
|
stacks[3]->position = 103; //7
|
|
|
|
stacks[4]->position = 137; //9
|
|
|
|
stacks[5]->position = 171; //11
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
stacks[0]->position = 1; //1
|
|
|
|
stacks[1]->position = 35; //3
|
|
|
|
stacks[2]->position = 69; //5
|
|
|
|
stacks[3]->position = 86; //6
|
|
|
|
stacks[4]->position = 103; //7
|
|
|
|
stacks[5]->position = 137; //9
|
|
|
|
stacks[6]->position = 171; //11
|
|
|
|
break;
|
|
|
|
default: //fault
|
|
|
|
break;
|
|
|
|
}
|
2008-02-25 01:06:27 +02:00
|
|
|
for(std::map<int,std::pair<CCreature*,int> >::iterator i = army2->slots.begin(); i!=army2->slots.end(); i++)
|
2008-04-14 21:24:46 +03:00
|
|
|
stacks.push_back(new CStack(i->second.first,i->second.second,1, stacks.size(), false));
|
|
|
|
switch(army2->slots.size()) //for defender
|
2008-03-10 22:19:41 +02:00
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
stacks[0+army1->slots.size()]->position = 100; //6
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
stacks[0+army1->slots.size()]->position = 49; //3
|
|
|
|
stacks[1+army1->slots.size()]->position = 151; //9
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
stacks[0+army1->slots.size()]->position = 49; //3
|
|
|
|
stacks[1+army1->slots.size()]->position = 100; //6
|
|
|
|
stacks[2+army1->slots.size()]->position = 151; //9
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
stacks[0+army1->slots.size()]->position = 15; //1
|
|
|
|
stacks[1+army1->slots.size()]->position = 83; //5
|
|
|
|
stacks[2+army1->slots.size()]->position = 117; //7
|
|
|
|
stacks[3+army1->slots.size()]->position = 185; //11
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
stacks[0+army1->slots.size()]->position = 15; //1
|
|
|
|
stacks[1+army1->slots.size()]->position = 49; //3
|
|
|
|
stacks[2+army1->slots.size()]->position = 100; //6
|
|
|
|
stacks[3+army1->slots.size()]->position = 151; //9
|
|
|
|
stacks[4+army1->slots.size()]->position = 185; //11
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
stacks[0+army1->slots.size()]->position = 15; //1
|
|
|
|
stacks[1+army1->slots.size()]->position = 49; //3
|
|
|
|
stacks[2+army1->slots.size()]->position = 83; //5
|
|
|
|
stacks[3+army1->slots.size()]->position = 117; //7
|
|
|
|
stacks[4+army1->slots.size()]->position = 151; //9
|
|
|
|
stacks[5+army1->slots.size()]->position = 185; //11
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
stacks[0+army1->slots.size()]->position = 15; //1
|
|
|
|
stacks[1+army1->slots.size()]->position = 49; //3
|
|
|
|
stacks[2+army1->slots.size()]->position = 83; //5
|
|
|
|
stacks[3+army1->slots.size()]->position = 100; //6
|
|
|
|
stacks[4+army1->slots.size()]->position = 117; //7
|
|
|
|
stacks[5+army1->slots.size()]->position = 151; //9
|
|
|
|
stacks[6+army1->slots.size()]->position = 185; //11
|
|
|
|
break;
|
|
|
|
default: //fault
|
|
|
|
break;
|
|
|
|
}
|
2008-04-14 21:24:46 +03:00
|
|
|
for(int g=0; g<stacks.size(); ++g) //shifting positions of two-hex creatures
|
|
|
|
{
|
|
|
|
if((stacks[g]->position%17)==1 && stacks[g]->creature->isDoubleWide())
|
|
|
|
{
|
|
|
|
stacks[g]->position += 1;
|
|
|
|
}
|
|
|
|
else if((stacks[g]->position%17)==15 && stacks[g]->creature->isDoubleWide())
|
|
|
|
{
|
|
|
|
stacks[g]->position -= 1;
|
|
|
|
}
|
|
|
|
}
|
2008-02-25 01:06:27 +02:00
|
|
|
std::stable_sort(stacks.begin(),stacks.end(),cmpst);
|
|
|
|
|
|
|
|
//for start inform players about battle
|
|
|
|
for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
|
|
|
|
{
|
|
|
|
if (j->first > PLAYER_LIMIT)
|
|
|
|
break;
|
|
|
|
if(j->second.fogOfWarMap[tile.x][tile.y][tile.z])
|
|
|
|
{ //player should be notified
|
|
|
|
tribool side = tribool::indeterminate_value;
|
|
|
|
if(j->first == curB->side1) //player is attacker
|
|
|
|
side = false;
|
|
|
|
else if(j->first == curB->side2) //player is defender
|
|
|
|
side = true;
|
|
|
|
else
|
|
|
|
return; //no witnesses
|
2008-02-26 20:01:26 +02:00
|
|
|
if(CGI->playerint[j->second.serial]->human)
|
|
|
|
{
|
2008-03-14 20:24:37 +02:00
|
|
|
((CPlayerInterface*)( CGI->playerint[j->second.serial] ))->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
|
2008-02-26 20:01:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//CGI->playerint[j->second.serial]->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
|
|
|
|
}
|
2008-02-25 01:06:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curB->round++;
|
2008-02-26 20:01:26 +02:00
|
|
|
if( (curB->hero1 && curB->hero1->getSecSkillLevel(19)>=0) || ( curB->hero2 && curB->hero2->getSecSkillLevel(19)>=0) )//someone has tactics
|
2008-02-25 01:06:27 +02:00
|
|
|
{
|
|
|
|
//TODO: wywolania dla rundy -1, ograniczenie pola ruchu, etc
|
|
|
|
}
|
|
|
|
|
|
|
|
curB->round++;
|
2008-02-28 19:00:16 +02:00
|
|
|
|
2008-03-01 00:14:53 +02:00
|
|
|
//SDL_Thread * eventh = SDL_CreateThread(battleEventThread, NULL);
|
2008-02-28 19:00:16 +02:00
|
|
|
|
2008-04-19 20:10:57 +03:00
|
|
|
while(true) //till the end of the battle ;]
|
2008-02-28 19:00:16 +02:00
|
|
|
{
|
2008-04-16 20:21:02 +03:00
|
|
|
bool battleEnd = false;
|
2008-04-19 20:10:57 +03:00
|
|
|
//tell players about next round
|
|
|
|
for(int v=0; v<CGI->playerint.size(); ++v)
|
|
|
|
CGI->playerint[v]->battleNewRound(curB->round);
|
|
|
|
|
|
|
|
//stack loop
|
2008-02-28 19:00:16 +02:00
|
|
|
for(int i=0;i<stacks.size();i++)
|
|
|
|
{
|
|
|
|
curB->activeStack = i;
|
2008-03-29 12:59:18 +02:00
|
|
|
curB->stackActionPerformed = false;
|
2008-04-15 19:52:31 +03:00
|
|
|
if(stacks[i]->alive) //indicate posiibility of making action for this unit
|
2008-02-28 19:00:16 +02:00
|
|
|
{
|
2008-04-14 21:24:46 +03:00
|
|
|
unsigned char owner = (stacks[i]->owner)?(hero2 ? hero2->tempOwner : 255):(hero1->tempOwner);
|
2008-03-06 18:42:34 +02:00
|
|
|
unsigned char serialOwner = -1;
|
|
|
|
for(int g=0; g<CGI->playerint.size(); ++g)
|
2008-02-28 19:00:16 +02:00
|
|
|
{
|
2008-03-06 18:42:34 +02:00
|
|
|
if(CGI->playerint[g]->playerID == owner)
|
|
|
|
{
|
|
|
|
serialOwner = g;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-04-14 21:24:46 +03:00
|
|
|
if(serialOwner==255) //neutral unit
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if(CGI->playerint[serialOwner]->human)
|
2008-03-06 18:42:34 +02:00
|
|
|
{
|
2008-04-15 19:52:31 +03:00
|
|
|
BattleAction ba = ((CPlayerInterface*)CGI->playerint[serialOwner])->activeStack(stacks[i]->ID);
|
|
|
|
switch(ba.actionType)
|
|
|
|
{
|
2008-05-27 16:16:35 +03:00
|
|
|
case 2: //walk
|
|
|
|
{
|
|
|
|
battleMoveCreatureStack(ba.stackNumber, ba.destinationTile);
|
|
|
|
}
|
2008-04-15 19:52:31 +03:00
|
|
|
case 3: //defend
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 4: //retreat/flee
|
|
|
|
{
|
|
|
|
for(int v=0; v<CGI->playerint.size(); ++v) //tell about the end of this battle to interfaces
|
|
|
|
CGI->playerint[v]->battleEnd(army1, army2, hero1, hero2, std::vector<int>(), 0, false);
|
2008-04-16 20:21:02 +03:00
|
|
|
battleEnd = true;
|
2008-04-15 19:52:31 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 6: //walk or attack
|
|
|
|
{
|
|
|
|
battleMoveCreatureStack(ba.stackNumber, ba.destinationTile);
|
2008-05-27 16:16:35 +03:00
|
|
|
battleAttackCreatureStack(ba.stackNumber, ba.destinationTile);
|
2008-04-15 19:52:31 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-02-28 19:00:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-06 18:42:34 +02:00
|
|
|
//CGI->playerint[serialOwner]->activeStack(stacks[i]->ID);
|
2008-02-28 19:00:16 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-16 20:21:02 +03:00
|
|
|
if(battleEnd)
|
|
|
|
break;
|
2008-02-28 19:00:16 +02:00
|
|
|
//sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
|
|
|
|
}
|
2008-04-16 20:21:02 +03:00
|
|
|
if(battleEnd)
|
|
|
|
break;
|
2008-04-19 20:10:57 +03:00
|
|
|
curB->round++;
|
2008-02-28 19:00:16 +02:00
|
|
|
SDL_Delay(50);
|
|
|
|
}
|
2008-02-25 01:06:27 +02:00
|
|
|
|
|
|
|
for(int i=0;i<stacks.size();i++)
|
|
|
|
delete stacks[i];
|
|
|
|
delete curB;
|
|
|
|
curB = NULL;
|
2008-03-10 22:19:41 +02:00
|
|
|
}
|
2008-03-23 19:25:38 +02:00
|
|
|
|
|
|
|
bool CGameState::battleMoveCreatureStack(int ID, int dest)
|
|
|
|
{
|
2008-03-29 12:59:18 +02:00
|
|
|
//first checks
|
|
|
|
if(curB->stackActionPerformed) //because unit cannot be moved more than once
|
|
|
|
return false;
|
2008-05-30 14:53:04 +03:00
|
|
|
|
2008-04-07 20:51:46 +03:00
|
|
|
unsigned char owner = -1; //owner moved of unit
|
|
|
|
for(int g=0; g<curB->stacks.size(); ++g)
|
|
|
|
{
|
2008-05-30 14:53:04 +03:00
|
|
|
if(curB->stacks[g]->ID == ID)
|
2008-04-07 20:51:46 +03:00
|
|
|
{
|
2008-05-30 14:53:04 +03:00
|
|
|
owner = curB->stacks[g]->owner;
|
2008-04-07 20:51:46 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 14:53:04 +03:00
|
|
|
|
|
|
|
bool stackAtEnd = false; //true if there is a stack at the end of the path (we should attack it)
|
|
|
|
int numberOfStackAtEnd = -1;
|
2008-04-07 20:51:46 +03:00
|
|
|
for(int g=0; g<curB->stacks.size(); ++g)
|
|
|
|
{
|
2008-05-30 14:53:04 +03:00
|
|
|
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))
|
2008-04-07 20:51:46 +03:00
|
|
|
{
|
2008-05-30 14:53:04 +03:00
|
|
|
stackAtEnd = true;
|
|
|
|
numberOfStackAtEnd = g;
|
2008-04-07 20:51:46 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 14:53:04 +03:00
|
|
|
|
2008-03-24 21:04:30 +02:00
|
|
|
//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;
|
2008-03-23 19:25:38 +02:00
|
|
|
//initing necessary tables
|
|
|
|
bool accessibility[187]; //accesibility of hexes
|
|
|
|
for(int k=0; k<187; k++)
|
|
|
|
accessibility[k] = true;
|
2008-03-24 21:04:30 +02:00
|
|
|
for(int g=0; g<curB->stacks.size(); ++g)
|
2008-03-23 19:25:38 +02:00
|
|
|
{
|
2008-05-27 16:16:35 +03:00
|
|
|
if(curB->stacks[g]->ID != ID) //we don't want to lock enemy's positions and this units' position
|
2008-04-14 21:24:46 +03:00
|
|
|
{
|
2008-04-07 20:51:46 +03:00
|
|
|
accessibility[curB->stacks[g]->position] = false;
|
2008-04-14 21:24:46 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2008-03-23 19:25:38 +02:00
|
|
|
}
|
2008-05-27 16:16:35 +03:00
|
|
|
accessibility[dest] = true;
|
2008-04-19 20:10:57 +03:00
|
|
|
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];
|
2008-04-19 21:08:29 +03:00
|
|
|
//removing accessibility for side hexes
|
|
|
|
for(int v=0; v<187; ++v)
|
|
|
|
if(curStack->attackerOwned ? (v%17)==1 : (v%17)==15)
|
|
|
|
accessibility[v] = false;
|
2008-04-19 20:10:57 +03:00
|
|
|
}
|
2008-04-19 21:08:29 +03:00
|
|
|
if(!accessibility[dest])
|
|
|
|
return false;
|
2008-03-23 19:25:38 +02:00
|
|
|
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
|
2008-03-24 21:04:30 +02:00
|
|
|
hexq.push(curStack->position);
|
2008-03-23 19:25:38 +02:00
|
|
|
for(int g=0; g<187; ++g)
|
|
|
|
dists[g] = 100000000;
|
|
|
|
dists[hexq.front()] = 0;
|
2008-03-24 15:25:11 +02:00
|
|
|
int curNext = -1; //for bfs loop only (helper var)
|
2008-03-23 19:25:38 +02:00
|
|
|
while(!hexq.empty()) //bfs loop
|
|
|
|
{
|
|
|
|
int curHex = hexq.front();
|
|
|
|
hexq.pop();
|
2008-03-29 12:59:18 +02:00
|
|
|
curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
|
2008-04-15 19:52:31 +03:00
|
|
|
if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
|
2008-03-23 19:25:38 +02:00
|
|
|
{
|
2008-03-24 15:25:11 +02:00
|
|
|
hexq.push(curNext);
|
|
|
|
dists[curNext] = dists[curHex] + 1;
|
|
|
|
predecessor[curNext] = curHex;
|
2008-03-23 19:25:38 +02:00
|
|
|
}
|
2008-03-29 12:59:18 +02:00
|
|
|
curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
|
2008-04-15 19:52:31 +03:00
|
|
|
if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
|
2008-03-23 19:25:38 +02:00
|
|
|
{
|
2008-03-24 15:25:11 +02:00
|
|
|
hexq.push(curNext);
|
|
|
|
dists[curNext] = dists[curHex] + 1;
|
|
|
|
predecessor[curNext] = curHex;
|
2008-03-23 19:25:38 +02:00
|
|
|
}
|
2008-03-24 15:25:11 +02:00
|
|
|
curNext = curHex - 1;
|
2008-04-15 19:52:31 +03:00
|
|
|
if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
|
2008-03-23 19:25:38 +02:00
|
|
|
{
|
2008-03-24 15:25:11 +02:00
|
|
|
hexq.push(curNext);
|
|
|
|
dists[curNext] = dists[curHex] + 1;
|
|
|
|
predecessor[curNext] = curHex;
|
2008-03-23 19:25:38 +02:00
|
|
|
}
|
2008-03-24 15:25:11 +02:00
|
|
|
curNext = curHex + 1;
|
2008-04-15 19:52:31 +03:00
|
|
|
if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
|
2008-03-23 19:25:38 +02:00
|
|
|
{
|
2008-03-24 15:25:11 +02:00
|
|
|
hexq.push(curNext);
|
|
|
|
dists[curNext] = dists[curHex] + 1;
|
|
|
|
predecessor[curNext] = curHex;
|
2008-03-23 19:25:38 +02:00
|
|
|
}
|
2008-03-24 15:25:11 +02:00
|
|
|
curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
|
2008-04-15 19:52:31 +03:00
|
|
|
if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
|
2008-03-23 19:25:38 +02:00
|
|
|
{
|
2008-03-24 15:25:11 +02:00
|
|
|
hexq.push(curNext);
|
|
|
|
dists[curNext] = dists[curHex] + 1;
|
|
|
|
predecessor[curNext] = curHex;
|
2008-03-23 19:25:38 +02:00
|
|
|
}
|
2008-03-24 15:25:11 +02:00
|
|
|
curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
|
2008-04-15 19:52:31 +03:00
|
|
|
if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
|
2008-03-23 19:25:38 +02:00
|
|
|
{
|
2008-03-24 15:25:11 +02:00
|
|
|
hexq.push(curNext);
|
|
|
|
dists[curNext] = dists[curHex] + 1;
|
|
|
|
predecessor[curNext] = curHex;
|
2008-03-23 19:25:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//following the Path
|
2008-03-24 21:04:30 +02:00
|
|
|
if(dists[dest] > curStack->creature->speed)
|
|
|
|
return false;
|
2008-03-23 19:25:38 +02:00
|
|
|
std::vector<int> path;
|
|
|
|
int curElem = dest;
|
2008-03-24 21:04:30 +02:00
|
|
|
while(curElem!=curStack->position)
|
2008-03-23 19:25:38 +02:00
|
|
|
{
|
|
|
|
path.push_back(curElem);
|
|
|
|
curElem = predecessor[curElem];
|
|
|
|
}
|
|
|
|
for(int v=path.size()-1; v>=0; --v)
|
|
|
|
{
|
2008-04-07 20:51:46 +03:00
|
|
|
if(v!=0 || !stackAtEnd) //it's not the last step
|
|
|
|
{
|
2008-05-30 14:53:04 +03:00
|
|
|
LOCPLINT->battleStackMoved(ID, path[v], v==path.size()-1, v==0 || (stackAtEnd && v==1) );
|
2008-04-07 20:51:46 +03:00
|
|
|
curStack->position = path[v];
|
|
|
|
}
|
|
|
|
else //if it's last step and we should attack unit at the end
|
|
|
|
{
|
|
|
|
LOCPLINT->battleStackAttacking(ID, path[v]);
|
2008-05-30 14:53:04 +03:00
|
|
|
//counting dealt damage
|
|
|
|
int numberOfCres = curStack->amount; //number of attacking creatures
|
|
|
|
int attackDefenseBonus = curStack->creature->attack - curB->stacks[numberOfStackAtEnd]->creature->defence;
|
|
|
|
int damageBase = 0;
|
|
|
|
if(curStack->creature->damageMax == curStack->creature->damageMin) //constant damage
|
|
|
|
{
|
|
|
|
damageBase = curStack->creature->damageMin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
damageBase = rand()%(curStack->creature->damageMax - curStack->creature->damageMin) + curStack->creature->damageMin + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
float dmgBonusMultiplier = 1.0;
|
|
|
|
if(attackDefenseBonus < 0) //decreasing dmg
|
|
|
|
{
|
|
|
|
if(0.02f * (-attackDefenseBonus) > 0.3f)
|
|
|
|
{
|
|
|
|
dmgBonusMultiplier += -0.3f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dmgBonusMultiplier += 0.02f * attackDefenseBonus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else //increasing dmg
|
|
|
|
{
|
|
|
|
if(0.05f * attackDefenseBonus > 4.0f)
|
|
|
|
{
|
|
|
|
dmgBonusMultiplier += 4.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dmgBonusMultiplier += 0.05f * attackDefenseBonus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int finalDmg = (float)damageBase * (float)curStack->amount * dmgBonusMultiplier;
|
|
|
|
|
|
|
|
//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;
|
|
|
|
}
|
|
|
|
|
|
|
|
curB->stacks[numberOfStackAtEnd]->amount -= cresKilled;
|
|
|
|
if(curB->stacks[numberOfStackAtEnd]->amount<=0) //stack killed
|
|
|
|
{
|
|
|
|
curB->stacks[numberOfStackAtEnd]->amount = 0;
|
|
|
|
curB->stacks[numberOfStackAtEnd]->alive = false;
|
|
|
|
LOCPLINT->battleStackKilled(curB->stacks[numberOfStackAtEnd]->ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
//damage applied
|
2008-04-07 20:51:46 +03:00
|
|
|
}
|
2008-03-23 19:25:38 +02:00
|
|
|
}
|
2008-03-29 12:59:18 +02:00
|
|
|
curB->stackActionPerformed = true;
|
|
|
|
LOCPLINT->actionFinished(BattleAction());
|
2008-03-23 19:25:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
2008-03-29 12:59:18 +02:00
|
|
|
|
2008-05-27 16:16:35 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-03-29 12:59:18 +02:00
|
|
|
std::vector<int> CGameState::battleGetRange(int ID)
|
|
|
|
{
|
|
|
|
int initialPlace=-1; //position of unit
|
|
|
|
int radius=-1; //range of unit
|
2008-04-07 20:51:46 +03:00
|
|
|
unsigned char owner = -1; //owner of unit
|
2008-04-19 20:10:57 +03:00
|
|
|
//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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-29 12:59:18 +02:00
|
|
|
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;
|
2008-04-07 20:51:46 +03:00
|
|
|
owner = curB->stacks[g]->owner;
|
2008-03-29 12:59:18 +02:00
|
|
|
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)
|
|
|
|
{
|
2008-05-30 14:53:04 +03:00
|
|
|
if(curB->stacks[g]->ID != ID) //we don't want to lock current unit's position
|
2008-04-14 21:24:46 +03:00
|
|
|
{
|
2008-04-07 20:51:46 +03:00
|
|
|
accessibility[curB->stacks[g]->position] = false;
|
2008-04-14 21:24:46 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2008-03-29 12:59:18 +02:00
|
|
|
}
|
|
|
|
|
2008-04-19 20:10:57 +03:00
|
|
|
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];
|
2008-04-19 21:08:29 +03:00
|
|
|
//removing accessibility for side hexes
|
|
|
|
for(int v=0; v<187; ++v)
|
|
|
|
if(curStack->attackerOwned ? (v%17)==1 : (v%17)==15)
|
|
|
|
accessibility[v] = false;
|
2008-04-19 20:10:57 +03:00
|
|
|
}
|
2008-03-29 12:59:18 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|