2007-08-27 17:15:03 +03:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "CCallback.h"
|
2007-08-29 13:05:50 +03:00
|
|
|
#include "CPathfinder.h"
|
2007-08-30 13:11:53 +03:00
|
|
|
#include "hch\CHeroHandler.h"
|
2007-09-18 16:30:26 +03:00
|
|
|
#include "hch\CTownHandler.h"
|
2007-08-29 15:18:31 +03:00
|
|
|
#include "CGameInfo.h"
|
2007-08-30 13:11:53 +03:00
|
|
|
#include "hch\CAmbarCendamo.h"
|
2007-08-29 15:18:31 +03:00
|
|
|
#include "mapHandler.h"
|
2007-08-30 13:11:53 +03:00
|
|
|
#include "CGameState.h"
|
|
|
|
#include "CGameInterface.h"
|
2007-10-03 21:53:49 +03:00
|
|
|
int CCallback::lowestSpeed(CHeroInstance * chi)
|
|
|
|
{
|
|
|
|
int min = 150;
|
|
|
|
for ( std::map<int,std::pair<CCreature*,int> >::iterator i = chi->army.slots.begin();
|
|
|
|
i!=chi->army.slots.end(); i++ )
|
|
|
|
{
|
|
|
|
if (min>(*i).second.first->speed)
|
|
|
|
min = (*i).second.first->speed;
|
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
int CCallback::valMovePoints(CHeroInstance * chi)
|
|
|
|
{
|
|
|
|
int ret = 1270+70*lowestSpeed(chi);
|
|
|
|
if (ret>2000)
|
|
|
|
ret=2000;
|
|
|
|
|
|
|
|
//TODO: additional bonuses (but they aren't currently stored in chi)
|
2007-08-27 17:15:03 +03:00
|
|
|
|
2007-10-03 21:53:49 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
void CCallback::newTurn()
|
|
|
|
{
|
|
|
|
//std::map<int, PlayerState>::iterator i = gs->players.begin() ;
|
|
|
|
for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
|
|
|
|
{
|
|
|
|
for (int j=0;j<(*i).second.heroes.size();j++)
|
|
|
|
{
|
|
|
|
(*i).second.heroes[j]->movement = valMovePoints((*i).second.heroes[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-05 21:16:22 +03:00
|
|
|
bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
|
2007-08-27 17:15:03 +03:00
|
|
|
{
|
2007-10-05 21:16:22 +03:00
|
|
|
CHeroInstance * hero = NULL;
|
|
|
|
|
|
|
|
if (idtype==0)
|
|
|
|
{
|
|
|
|
if (player==-1)
|
|
|
|
hero=gs->players[player+1].heroes[ID];
|
|
|
|
else
|
|
|
|
hero=gs->players[player].heroes[ID];
|
|
|
|
}
|
2007-10-11 23:13:13 +03:00
|
|
|
else if (idtype==1 && player>=0) //looking for it in local area
|
2007-10-05 21:16:22 +03:00
|
|
|
{
|
|
|
|
for (int i=0; i<gs->players[player].heroes.size();i++)
|
|
|
|
{
|
|
|
|
if (gs->players[player].heroes[i]->type->ID == ID)
|
|
|
|
hero = gs->players[player].heroes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else //idtype==1; player<0
|
2007-10-04 19:40:40 +03:00
|
|
|
{
|
2007-10-05 21:16:22 +03:00
|
|
|
|
|
|
|
for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)
|
|
|
|
{
|
|
|
|
for (int i=0; i<(*j).second.heroes.size();i++)
|
|
|
|
{
|
|
|
|
if ((*j).second.heroes[i]->type->ID == ID)
|
|
|
|
{
|
|
|
|
hero = (*j).second.heroes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-04 19:40:40 +03:00
|
|
|
}
|
2007-10-05 21:16:22 +03:00
|
|
|
|
|
|
|
if (!hero)
|
|
|
|
return false; //can't find hero
|
|
|
|
if(!verifyPath(path,!hero->canWalkOnSea()))//TODO: not check sea, if hero has flying or walking on water
|
|
|
|
return false; //invalid path
|
|
|
|
|
|
|
|
//check path format
|
|
|
|
if (pathType==0)
|
|
|
|
CPathfinder::convertPath(path,pathType);
|
|
|
|
if (pathType>1)
|
|
|
|
throw std::exception("Unknown path format");
|
|
|
|
|
|
|
|
CPath * ourPath = path;
|
2007-08-29 15:18:31 +03:00
|
|
|
if(!ourPath)
|
|
|
|
return false;
|
2007-09-13 16:34:59 +03:00
|
|
|
for(int i=ourPath->nodes.size()-1; i>0; i--)
|
2007-08-30 13:11:53 +03:00
|
|
|
{
|
|
|
|
int3 stpos, endpos;
|
2007-10-05 21:16:22 +03:00
|
|
|
stpos = int3(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z);
|
|
|
|
endpos = int3(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
|
2007-08-30 13:11:53 +03:00
|
|
|
HeroMoveDetails curd;
|
|
|
|
curd.src = stpos;
|
|
|
|
curd.dst = endpos;
|
2007-10-05 21:16:22 +03:00
|
|
|
curd.ho = hero->ourObject;
|
|
|
|
curd.owner = hero->owner;
|
2007-10-04 19:40:40 +03:00
|
|
|
if(player!=-1)
|
|
|
|
{
|
2007-10-05 21:16:22 +03:00
|
|
|
hero->pos = endpos;
|
2007-10-04 19:40:40 +03:00
|
|
|
}
|
2007-09-13 16:34:59 +03:00
|
|
|
//if(CGI->heroh->heroInstances[ID]->movement>=CGI->mh->getCost(stpos, endpos, CGI->heroh->heroInstances[ID]))
|
2007-08-30 13:11:53 +03:00
|
|
|
{ //performing move
|
2007-09-15 18:44:08 +03:00
|
|
|
int nn=0; //number of interfece of currently browsed player
|
|
|
|
for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
|
2007-08-30 13:11:53 +03:00
|
|
|
{
|
2007-10-09 18:15:35 +03:00
|
|
|
if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z])
|
2007-08-30 13:11:53 +03:00
|
|
|
{ //player should be notified
|
2007-09-15 18:44:08 +03:00
|
|
|
CGI->playerint[nn]->heroMoved(curd);
|
2007-08-30 13:11:53 +03:00
|
|
|
}
|
2007-09-15 18:44:08 +03:00
|
|
|
++nn;
|
|
|
|
break; //for testing only
|
2007-08-30 13:11:53 +03:00
|
|
|
}
|
|
|
|
}
|
2007-09-13 16:34:59 +03:00
|
|
|
//else
|
2007-09-15 18:44:08 +03:00
|
|
|
//return true; //move ended - no more movement points
|
2007-10-05 21:16:22 +03:00
|
|
|
hero->pos = curd.dst;
|
2007-08-30 13:11:53 +03:00
|
|
|
}
|
|
|
|
return true;
|
2007-08-29 15:18:31 +03:00
|
|
|
}
|
2007-09-14 16:11:10 +03:00
|
|
|
|
|
|
|
|
2007-09-18 16:30:26 +03:00
|
|
|
int CCallback::howManyTowns()
|
|
|
|
{
|
|
|
|
return gs->players[gs->currentPlayer].towns.size();
|
|
|
|
}
|
|
|
|
const CTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
|
|
|
|
{
|
|
|
|
if (!mode)
|
|
|
|
return gs->players[gs->currentPlayer].towns[val];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//TODO: add some smart ID to the CTownInstance
|
|
|
|
|
|
|
|
|
|
|
|
//for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
|
|
|
|
//{
|
|
|
|
// if (gs->players[gs->currentPlayer].towns[i]->someID==val)
|
|
|
|
// return gs->players[gs->currentPlayer].towns[i];
|
|
|
|
//}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-09-14 16:11:10 +03:00
|
|
|
int CCallback::howManyHeroes(int player)
|
|
|
|
{
|
|
|
|
if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
|
|
|
|
return -1;
|
|
|
|
return gs->players[player].heroes.size();
|
|
|
|
}
|
|
|
|
const CHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
|
|
|
|
{
|
|
|
|
if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
|
|
|
|
return NULL;
|
|
|
|
if (!mode)
|
|
|
|
return gs->players[player].heroes[val];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i=0; i<gs->players[player].heroes.size();i++)
|
|
|
|
{
|
|
|
|
if (gs->players[player].heroes[i]->type->ID==val)
|
|
|
|
return gs->players[player].heroes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2007-09-16 20:21:23 +03:00
|
|
|
}
|
2007-09-18 16:30:26 +03:00
|
|
|
|
2007-09-16 20:21:23 +03:00
|
|
|
int CCallback::getResourceAmount(int type)
|
|
|
|
{
|
|
|
|
return gs->players[gs->currentPlayer].resources[type];
|
2007-09-18 16:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CCallback::getDate(int mode)
|
|
|
|
{
|
|
|
|
int temp;
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return gs->day;
|
|
|
|
case 1:
|
|
|
|
temp = (gs->day)%7;
|
|
|
|
if (temp)
|
|
|
|
return temp;
|
|
|
|
else return 7;
|
|
|
|
case 2:
|
|
|
|
temp = ((gs->day-1)/7)+1;
|
|
|
|
if (temp%4)
|
|
|
|
return temp;
|
|
|
|
else return 4;
|
|
|
|
case 3:
|
|
|
|
return ((gs->day-1)/28)+1;
|
|
|
|
}
|
2007-10-05 21:10:33 +03:00
|
|
|
}
|
2007-10-05 21:16:22 +03:00
|
|
|
bool CCallback::verifyPath(CPath * path, bool blockSea)
|
|
|
|
{
|
|
|
|
for (int i=0;i<path->nodes.size();i++)
|
|
|
|
{
|
|
|
|
if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
|
|
|
|
&& (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].visitable)))
|
|
|
|
return false; //path is wrong - one of the tiles is blocked
|
|
|
|
|
|
|
|
if (blockSea)
|
|
|
|
{
|
|
|
|
if (i==0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (
|
|
|
|
((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType==EterrainType::water)
|
|
|
|
&&
|
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType!=EterrainType::water))
|
|
|
|
||
|
|
|
|
((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType!=EterrainType::water)
|
|
|
|
&&
|
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::water))
|
2007-10-05 23:38:14 +03:00
|
|
|
||
|
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::rock)
|
2007-10-05 21:16:22 +03:00
|
|
|
|
|
|
|
)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-10-05 21:10:33 +03:00
|
|
|
|
|
|
|
std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
|
|
|
|
{
|
|
|
|
if(gs->players[player].fogOfWarMap[pos.x][pos.y][pos.z])
|
|
|
|
return CGI->mh->getObjDescriptions(pos);
|
|
|
|
else return std::vector< std::string > ();
|
|
|
|
}
|