2007-08-27 17:15:03 +03:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "CCallback.h"
|
2009-05-20 13:08:56 +03:00
|
|
|
#include "client/CGameInfo.h"
|
|
|
|
#include "lib/CGameState.h"
|
|
|
|
#include "client/CPlayerInterface.h"
|
2008-08-30 00:41:32 +03:00
|
|
|
#include "client/Client.h"
|
2009-05-20 13:08:56 +03:00
|
|
|
#include "lib/map.h"
|
2008-03-21 02:03:31 +02:00
|
|
|
#include "hch/CBuildingHandler.h"
|
2008-08-30 00:41:32 +03:00
|
|
|
#include "hch/CDefObjInfoHandler.h"
|
2010-03-21 00:17:19 +02:00
|
|
|
#include "hch/CGeneralTextHandler.h"
|
2008-08-30 00:41:32 +03:00
|
|
|
#include "hch/CHeroHandler.h"
|
2008-07-02 11:39:56 +03:00
|
|
|
#include "hch/CObjectHandler.h"
|
2008-07-27 20:07:37 +03:00
|
|
|
#include "lib/Connection.h"
|
2008-07-28 15:44:08 +03:00
|
|
|
#include "lib/NetPacks.h"
|
2010-04-06 11:59:24 +03:00
|
|
|
#include "client/mapHandler.h"
|
2008-08-30 00:41:32 +03:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/thread.hpp>
|
2008-08-05 02:04:15 +03:00
|
|
|
#include <boost/thread/shared_mutex.hpp>
|
2009-08-23 16:41:57 +03:00
|
|
|
#include "hch/CSpellHandler.h"
|
2008-08-04 18:56:36 +03:00
|
|
|
#ifdef min
|
|
|
|
#undef min
|
|
|
|
#endif
|
|
|
|
#ifdef max
|
|
|
|
#undef max
|
|
|
|
#endif
|
2008-03-14 20:24:37 +02:00
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CCallback.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-03-21 00:17:19 +02:00
|
|
|
|
2008-07-28 15:44:08 +03:00
|
|
|
HeroMoveDetails::HeroMoveDetails(int3 Src, int3 Dst, CGHeroInstance*Ho)
|
|
|
|
:src(Src),dst(Dst),ho(Ho)
|
|
|
|
{
|
|
|
|
owner = ho->getOwner();
|
|
|
|
};
|
2009-03-07 00:11:17 +02:00
|
|
|
|
|
|
|
template <ui16 N> bool isType(CPack *pack)
|
|
|
|
{
|
|
|
|
return pack->getType() == N;
|
|
|
|
}
|
|
|
|
|
2010-06-30 22:27:35 +03:00
|
|
|
bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
|
|
|
|
{
|
|
|
|
CastleTeleportHero pack(who->id, where->id, 1);
|
|
|
|
sendRequest(&pack);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-04 02:53:18 +03:00
|
|
|
bool CCallback::moveHero(const CGHeroInstance *h, int3 dst)
|
2009-03-12 20:50:36 +02:00
|
|
|
{
|
2009-03-20 20:51:48 +02:00
|
|
|
MoveHero pack(dst,h->id);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2007-08-30 13:11:53 +03:00
|
|
|
return true;
|
2007-08-29 15:18:31 +03:00
|
|
|
}
|
2007-12-25 18:25:53 +02:00
|
|
|
void CCallback::selectionMade(int selection, int asker)
|
|
|
|
{
|
2009-03-20 20:51:48 +02:00
|
|
|
QueryReply pack(asker,selection);
|
|
|
|
*cl->serv << &pack;
|
2007-12-25 18:25:53 +02:00
|
|
|
}
|
2010-07-10 19:50:23 +03:00
|
|
|
void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level/*=-1*/)
|
2008-04-11 20:41:02 +03:00
|
|
|
{
|
2010-05-15 11:33:32 +03:00
|
|
|
if(player!=obj->tempOwner && obj->ID != 106)
|
|
|
|
return;
|
2009-03-20 20:51:48 +02:00
|
|
|
|
2010-07-10 19:50:23 +03:00
|
|
|
RecruitCreatures pack(obj->id,ID,amount,level);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-04-11 20:41:02 +03:00
|
|
|
}
|
2007-09-14 16:11:10 +03:00
|
|
|
|
2008-05-23 22:50:11 +03:00
|
|
|
|
|
|
|
bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
|
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
if(((player>=0) && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
|
2008-05-31 23:37:54 +03:00
|
|
|
return false;
|
2009-03-20 20:51:48 +02:00
|
|
|
|
|
|
|
DisbandCreature pack(stackPos,obj->id);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-05-31 23:37:54 +03:00
|
|
|
return true;
|
2008-05-23 22:50:11 +03:00
|
|
|
}
|
2008-05-31 23:37:54 +03:00
|
|
|
bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
|
2008-05-23 22:50:11 +03:00
|
|
|
{
|
2009-03-20 20:51:48 +02:00
|
|
|
UpgradeCreature pack(stackPos,obj->id,newID);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-05-23 22:50:11 +03:00
|
|
|
return false;
|
|
|
|
}
|
2008-07-27 20:07:37 +03:00
|
|
|
void CCallback::endTurn()
|
|
|
|
{
|
2009-10-31 18:33:11 +02:00
|
|
|
tlog5 << "Player " << (unsigned)player << " ended his turn." << std::endl;
|
2009-03-20 20:51:48 +02:00
|
|
|
EndTurn pack;
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack); //report that we ended turn
|
2008-07-27 20:07:37 +03:00
|
|
|
}
|
2008-09-26 17:16:01 +03:00
|
|
|
UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos) const
|
2008-05-23 22:50:11 +03:00
|
|
|
{
|
2008-08-20 22:02:48 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2010-07-15 06:04:57 +03:00
|
|
|
return gs->getUpgradeInfo(obj->getStack(stackPos));
|
2008-05-23 22:50:11 +03:00
|
|
|
}
|
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
const StartInfo * CCallback::getStartInfo() const
|
2008-06-30 03:06:41 +03:00
|
|
|
{
|
2008-08-20 22:02:48 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-06-30 03:06:41 +03:00
|
|
|
return gs->scenarioOps;
|
|
|
|
}
|
|
|
|
|
2009-08-23 16:41:57 +03:00
|
|
|
int CCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
|
|
|
|
{
|
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
|
|
|
|
//if there is a battle
|
|
|
|
if(gs->curB)
|
|
|
|
return gs->curB->getSpellCost(sp, caster);
|
|
|
|
|
|
|
|
//if there is no battle
|
2010-03-11 01:16:30 +02:00
|
|
|
return caster->getSpellCost(sp);
|
2009-08-23 16:41:57 +03:00
|
|
|
}
|
|
|
|
|
2009-11-08 16:44:58 +02:00
|
|
|
int CCallback::estimateSpellDamage(const CSpell * sp) const
|
|
|
|
{
|
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
|
|
|
|
if(!gs->curB)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const CGHeroInstance * ourHero = gs->curB->heroes[0]->tempOwner == player ? gs->curB->heroes[0] : gs->curB->heroes[1];
|
2010-02-24 20:11:08 +02:00
|
|
|
return gs->curB->calculateSpellDmg(sp, ourHero, NULL, ourHero->getSpellSchoolLevel(sp), ourHero->getPrimSkillLevel(2));
|
2009-11-08 16:44:58 +02:00
|
|
|
}
|
|
|
|
|
2010-02-01 19:51:33 +02:00
|
|
|
void CCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
|
|
|
|
if(obj == NULL)
|
|
|
|
return;
|
|
|
|
|
2010-07-09 02:03:27 +03:00
|
|
|
if(obj->ID == TOWNI_TYPE || obj->ID == 95) //it is a town or adv map tavern
|
2010-02-01 19:51:33 +02:00
|
|
|
{
|
|
|
|
gs->obtainPlayersStats(thi, gs->players[player].towns.size());
|
|
|
|
}
|
|
|
|
else if(obj->ID == 97) //Den of Thieves
|
|
|
|
{
|
|
|
|
gs->obtainPlayersStats(thi, 20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
int CCallback::howManyTowns() const
|
2007-09-18 16:30:26 +03:00
|
|
|
{
|
2008-08-20 22:02:48 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
return gs->players[player].towns.size();
|
2007-09-18 16:30:26 +03:00
|
|
|
}
|
2009-08-23 16:41:57 +03:00
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
const CGTownInstance * CCallback::getTownInfo(int val, bool mode) const //mode = 0 -> val = serial; mode = 1 -> val = ID
|
2007-09-18 16:30:26 +03:00
|
|
|
{
|
2008-08-20 22:02:48 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-09-18 16:30:26 +03:00
|
|
|
if (!mode)
|
2009-08-01 13:08:16 +03:00
|
|
|
{
|
|
|
|
const std::vector<CGTownInstance *> &towns = gs->players[gs->currentPlayer].towns;
|
|
|
|
if(val < towns.size())
|
|
|
|
return towns[val];
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-07-06 05:10:26 +03:00
|
|
|
else if(mode == 1)
|
2007-09-18 16:30:26 +03:00
|
|
|
{
|
2010-07-06 05:10:26 +03:00
|
|
|
const CGObjectInstance *obj = getObjectInfo(val);
|
|
|
|
if(!obj)
|
|
|
|
return NULL;
|
|
|
|
if(obj->ID != TOWNI_TYPE)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return static_cast<const CGTownInstance *>(obj);
|
2007-09-18 16:30:26 +03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-07-20 05:56:35 +03:00
|
|
|
|
|
|
|
bool CCallback::getTownInfo( const CGObjectInstance *town, InfoAboutTown &dest ) const
|
|
|
|
{
|
2010-03-21 00:17:19 +02:00
|
|
|
if(!isVisible(town, player)) //it's not a town or it's not visible for layer
|
2009-07-20 05:56:35 +03:00
|
|
|
return false;
|
|
|
|
|
2010-03-21 00:17:19 +02:00
|
|
|
bool detailed = hasAccess(town->tempOwner);
|
|
|
|
|
2009-07-20 05:56:35 +03:00
|
|
|
//TODO vision support, info about allies
|
2010-03-21 00:17:19 +02:00
|
|
|
if(town->ID == TOWNI_TYPE)
|
|
|
|
dest.initFromTown(static_cast<const CGTownInstance *>(town), detailed);
|
|
|
|
else if(town->ID == 33 || town->ID == 219)
|
|
|
|
dest.initFromGarrison(static_cast<const CGGarrison *>(town), detailed);
|
|
|
|
else
|
|
|
|
return false;
|
2009-07-20 05:56:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-06 15:13:31 +03:00
|
|
|
int3 CCallback::guardingCreaturePosition (int3 pos) const
|
|
|
|
{
|
|
|
|
return gs->guardingCreaturePosition(pos);
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:05:20 +03:00
|
|
|
int CCallback::howManyHeroes(bool includeGarrisoned) const
|
2007-09-14 16:11:10 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-04-09 18:05:20 +03:00
|
|
|
return cl->getHeroCount(player,includeGarrisoned);
|
2007-09-14 16:11:10 +03:00
|
|
|
}
|
2008-09-26 17:16:01 +03:00
|
|
|
const CGHeroInstance * CCallback::getHeroInfo(int val, int mode) const //mode = 0 -> val = serial; mode = 1 -> val = ID
|
2007-09-14 16:11:10 +03:00
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx); //TODO use me?
|
2008-08-27 13:19:18 +03:00
|
|
|
//if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
|
|
|
|
// return NULL;
|
|
|
|
if (!mode) //esrial id
|
2008-12-23 15:59:03 +02:00
|
|
|
{
|
|
|
|
if(val<gs->players[player].heroes.size())
|
|
|
|
{
|
2008-01-20 18:24:03 +02:00
|
|
|
return gs->players[player].heroes[val];
|
2008-12-23 15:59:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-08-27 13:19:18 +03:00
|
|
|
else if(mode==1) //it's hero type id
|
2007-09-14 16:11:10 +03:00
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0; i < gs->players[player].heroes.size(); ++i)
|
2007-09-14 16:11:10 +03:00
|
|
|
{
|
2008-12-23 15:59:03 +02:00
|
|
|
if (gs->players[player].heroes[i]->type->ID==val)
|
|
|
|
{
|
2007-09-14 16:11:10 +03:00
|
|
|
return gs->players[player].heroes[i];
|
2008-12-23 15:59:03 +02:00
|
|
|
}
|
2007-09-14 16:11:10 +03:00
|
|
|
}
|
|
|
|
}
|
2008-08-27 13:19:18 +03:00
|
|
|
else //object id
|
|
|
|
{
|
2009-06-16 14:18:14 +03:00
|
|
|
return static_cast<const CGHeroInstance*>(gs->map->objects[val]);
|
2008-08-27 13:19:18 +03:00
|
|
|
}
|
2007-09-14 16:11:10 +03:00
|
|
|
return NULL;
|
2007-09-16 20:21:23 +03:00
|
|
|
}
|
2007-09-18 16:30:26 +03:00
|
|
|
|
2009-07-30 15:49:45 +03:00
|
|
|
const CGObjectInstance * CCallback::getObjectInfo(int ID) const
|
|
|
|
{
|
2010-07-06 05:10:26 +03:00
|
|
|
//TODO: check for visibility
|
2009-07-30 15:49:45 +03:00
|
|
|
return gs->map->objects[ID];
|
|
|
|
}
|
|
|
|
|
2009-07-20 04:47:49 +03:00
|
|
|
bool CCallback::getHeroInfo( const CGObjectInstance *hero, InfoAboutHero &dest ) const
|
|
|
|
{
|
|
|
|
const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(hero);
|
|
|
|
if(!h || !isVisible(h->getPosition(false))) //it's not a hero or it's not visible for layer
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//TODO vision support, info about allies
|
2010-03-21 00:17:19 +02:00
|
|
|
dest.initFromHero(h, hasAccess(h->tempOwner));
|
2009-07-20 04:47:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
int CCallback::getResourceAmount(int type) const
|
2007-09-16 20:21:23 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-12-01 14:50:33 +02:00
|
|
|
return gs->players[player].resources[type];
|
2007-09-18 16:30:26 +03:00
|
|
|
}
|
2008-09-26 17:16:01 +03:00
|
|
|
std::vector<si32> CCallback::getResourceAmount() const
|
2008-05-12 08:46:04 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-05-12 08:46:04 +03:00
|
|
|
return gs->players[player].resources;
|
|
|
|
}
|
2008-09-26 17:16:01 +03:00
|
|
|
int CCallback::getDate(int mode) const
|
2007-09-18 16:30:26 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-07-25 20:28:28 +03:00
|
|
|
return gs->getDate(mode);
|
2007-10-05 21:10:33 +03:00
|
|
|
}
|
2008-09-26 17:16:01 +03:00
|
|
|
std::vector < std::string > CCallback::getObjDescriptions(int3 pos) const
|
2008-07-29 12:53:27 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-07-29 12:53:27 +03:00
|
|
|
std::vector<std::string> ret;
|
2008-09-12 11:51:46 +03:00
|
|
|
if(!isVisible(pos,player))
|
|
|
|
return ret;
|
2008-07-30 20:51:19 +03:00
|
|
|
BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
|
2008-12-22 19:48:41 +02:00
|
|
|
ret.push_back(obj->getHoverText());
|
2008-07-29 12:53:27 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2008-09-26 17:16:01 +03:00
|
|
|
bool CCallback::verifyPath(CPath * path, bool blockSea) const
|
2007-10-05 21:16:22 +03:00
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0; i < path->nodes.size(); ++i)
|
2007-10-05 21:16:22 +03:00
|
|
|
{
|
2008-07-29 12:53:27 +03:00
|
|
|
if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->blocked
|
|
|
|
&& (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->visitable)))
|
2007-10-05 21:16:22 +03:00
|
|
|
return false; //path is wrong - one of the tiles is blocked
|
|
|
|
|
|
|
|
if (blockSea)
|
|
|
|
{
|
|
|
|
if (i==0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (
|
2009-05-07 20:20:41 +03:00
|
|
|
((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype==TerrainTile::water)
|
2007-10-05 21:16:22 +03:00
|
|
|
&&
|
2009-05-07 20:20:41 +03:00
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype!=TerrainTile::water))
|
2007-10-05 21:16:22 +03:00
|
|
|
||
|
2009-05-07 20:20:41 +03:00
|
|
|
((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype!=TerrainTile::water)
|
2007-10-05 21:16:22 +03:00
|
|
|
&&
|
2009-05-07 20:20:41 +03:00
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==TerrainTile::water))
|
2007-10-05 23:38:14 +03:00
|
|
|
||
|
2009-05-07 20:20:41 +03:00
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==TerrainTile::rock)
|
2007-10-05 21:16:22 +03:00
|
|
|
|
|
|
|
)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-10-05 21:10:33 +03:00
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
std::vector< std::vector< std::vector<unsigned char> > > & CCallback::getVisibilityMap() const
|
2007-10-16 20:46:01 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-10-16 20:46:01 +03:00
|
|
|
return gs->players[player].fogOfWarMap;
|
2007-10-20 00:12:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
bool CCallback::isVisible(int3 pos, int Player) const
|
2007-10-20 00:12:37 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2010-02-10 04:56:00 +02:00
|
|
|
return gs->map->isInTheMap(pos) && gs->isVisible(pos, Player);
|
2007-10-20 00:12:37 +03:00
|
|
|
}
|
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur) const
|
2008-02-18 23:14:28 +02:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-02-18 23:14:28 +02:00
|
|
|
std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
|
2008-07-25 20:28:28 +03:00
|
|
|
for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
|
2008-02-18 23:14:28 +02:00
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t j=0; j < (*i).second.towns.size(); ++j)
|
2008-02-18 23:14:28 +02:00
|
|
|
{
|
2010-02-26 13:18:09 +02:00
|
|
|
if ((*i).first==player
|
|
|
|
|| (isVisible((*i).second.towns[j],player) && !onlyOur))
|
2008-02-18 23:14:28 +02:00
|
|
|
{
|
|
|
|
ret.push_back((*i).second.towns[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
|
|
|
|
return ret;
|
|
|
|
}
|
2008-09-26 17:16:01 +03:00
|
|
|
std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur) const
|
2007-10-20 00:12:37 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-08-02 00:41:38 +03:00
|
|
|
std::vector < const CGHeroInstance *> ret;
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t i=0;i<gs->map->heroes.size();i++)
|
2007-10-20 00:12:37 +03:00
|
|
|
{
|
2008-08-02 00:41:38 +03:00
|
|
|
if( (gs->map->heroes[i]->tempOwner==player) ||
|
|
|
|
(isVisible(gs->map->heroes[i]->getPosition(false),player) && !onlyOur) )
|
2007-10-20 00:12:37 +03:00
|
|
|
{
|
2008-08-02 00:41:38 +03:00
|
|
|
ret.push_back(gs->map->heroes[i]);
|
2007-10-20 00:12:37 +03:00
|
|
|
}
|
2008-08-02 00:41:38 +03:00
|
|
|
}
|
2007-10-20 00:12:37 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
bool CCallback::isVisible(int3 pos) const
|
2007-10-20 00:12:37 +03:00
|
|
|
{
|
2008-08-20 22:02:48 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-10-20 00:12:37 +03:00
|
|
|
return isVisible(pos,player);
|
2007-10-24 22:54:35 +03:00
|
|
|
}
|
2007-11-19 00:58:28 +02:00
|
|
|
|
2009-07-20 05:56:35 +03:00
|
|
|
bool CCallback::isVisible( const CGObjectInstance *obj, int Player ) const
|
2008-08-30 00:41:32 +03:00
|
|
|
{
|
2009-07-30 15:49:45 +03:00
|
|
|
return gs->isVisible(obj, Player);
|
2008-08-30 00:41:32 +03:00
|
|
|
}
|
2009-07-30 15:49:45 +03:00
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
int CCallback::getMyColor() const
|
2007-12-01 14:50:33 +02:00
|
|
|
{
|
|
|
|
return player;
|
|
|
|
}
|
2009-07-30 15:49:45 +03:00
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
int CCallback::getHeroSerial(const CGHeroInstance * hero) const
|
2007-12-06 21:06:07 +02:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0; i<gs->players[player].heroes.size();i++)
|
2007-12-06 21:06:07 +02:00
|
|
|
{
|
|
|
|
if (gs->players[player].heroes[i]==hero)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2009-07-30 15:49:45 +03:00
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj) const
|
2008-01-28 16:01:09 +02:00
|
|
|
{
|
2008-08-20 22:02:48 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-04-12 03:58:41 +03:00
|
|
|
const CArmedInstance *armi = dynamic_cast<const CArmedInstance*>(obj);
|
|
|
|
if(!armi)
|
2008-01-28 22:58:19 +02:00
|
|
|
return NULL;
|
2009-04-12 03:58:41 +03:00
|
|
|
else
|
2010-05-02 21:20:26 +03:00
|
|
|
return armi;
|
2008-01-28 16:01:09 +02:00
|
|
|
}
|
2008-01-26 21:36:31 +02:00
|
|
|
|
2009-04-12 03:58:41 +03:00
|
|
|
int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
|
2008-01-26 21:36:31 +02:00
|
|
|
{
|
2009-03-20 20:51:48 +02:00
|
|
|
ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-04-13 14:05:39 +03:00
|
|
|
return 0;
|
2008-01-26 21:36:31 +02:00
|
|
|
}
|
2008-01-27 18:07:27 +02:00
|
|
|
|
2009-04-12 03:58:41 +03:00
|
|
|
int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
|
|
|
|
{
|
2009-03-20 20:51:48 +02:00
|
|
|
ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-04-13 14:05:39 +03:00
|
|
|
return 0;
|
2008-01-30 00:47:43 +02:00
|
|
|
}
|
2009-04-12 03:58:41 +03:00
|
|
|
int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)
|
2008-01-30 00:47:43 +02:00
|
|
|
{
|
2009-03-20 20:51:48 +02:00
|
|
|
ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-04-19 19:15:04 +03:00
|
|
|
return 0;
|
2008-01-30 00:47:43 +02:00
|
|
|
}
|
|
|
|
|
2008-01-27 18:07:27 +02:00
|
|
|
bool CCallback::dismissHero(const CGHeroInstance *hero)
|
|
|
|
{
|
2008-08-02 00:41:38 +03:00
|
|
|
if(player!=hero->tempOwner) return false;
|
2009-03-20 20:51:48 +02:00
|
|
|
|
|
|
|
DismissHero pack(hero->id);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-08-02 00:41:38 +03:00
|
|
|
return true;
|
2008-01-27 18:07:27 +02:00
|
|
|
}
|
|
|
|
|
2010-08-03 14:36:52 +03:00
|
|
|
// int CCallback::getMySerial() const
|
|
|
|
// {
|
|
|
|
// boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
// return gs->players[player].serial;
|
|
|
|
// }
|
2007-11-19 00:58:28 +02:00
|
|
|
|
2009-03-29 15:02:37 +03:00
|
|
|
bool CCallback::swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)
|
2008-01-30 12:55:43 +02:00
|
|
|
{
|
2008-08-20 22:02:48 +03:00
|
|
|
if(player!=hero1->tempOwner || player!=hero2->tempOwner)
|
2008-01-30 12:55:43 +02:00
|
|
|
return false;
|
2009-03-29 15:02:37 +03:00
|
|
|
|
2009-04-04 01:34:31 +03:00
|
|
|
ExchangeArtifacts ea(hero1->id, hero2->id, pos1, pos2);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&ea);
|
2008-01-30 12:55:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
2007-11-19 00:58:28 +02:00
|
|
|
|
2010-02-16 16:39:56 +02:00
|
|
|
/**
|
|
|
|
* Assembles or disassembles a combination artifact.
|
|
|
|
* @param hero Hero holding the artifact(s).
|
|
|
|
* @param artifactSlot The worn slot ID of the combination- or constituent artifact.
|
|
|
|
* @param assemble True for assembly operation, false for disassembly.
|
|
|
|
* @param assembleTo If assemble is true, this represents the artifact ID of the combination
|
|
|
|
* artifact to assemble to. Otherwise it's not used.
|
|
|
|
*/
|
|
|
|
bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo)
|
|
|
|
{
|
|
|
|
if (player != hero->tempOwner)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
|
|
|
|
sendRequest(&aa);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-01 14:21:15 +03:00
|
|
|
bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
|
2008-03-21 02:03:31 +02:00
|
|
|
{
|
|
|
|
CGTownInstance * t = const_cast<CGTownInstance *>(town);
|
2008-05-03 18:30:11 +03:00
|
|
|
|
2008-08-01 14:21:15 +03:00
|
|
|
if(town->tempOwner!=player)
|
2008-03-23 03:01:17 +02:00
|
|
|
return false;
|
2008-08-01 14:21:15 +03:00
|
|
|
CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
|
2009-08-05 12:46:55 +03:00
|
|
|
for(int i=0;i<b->resources.size();i++)
|
2008-08-01 14:21:15 +03:00
|
|
|
if(b->resources[i] > gs->players[player].resources[i])
|
|
|
|
return false; //lack of resources
|
|
|
|
|
2009-03-20 20:51:48 +02:00
|
|
|
BuildStructure pack(town->id,buildingID);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-03-21 02:03:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-03-10 22:19:41 +02:00
|
|
|
int CCallback::battleGetBattlefieldType()
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-02-09 16:50:32 +02:00
|
|
|
return gs->battleGetBattlefieldType();
|
2008-03-10 22:19:41 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 14:05:39 +03:00
|
|
|
int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield
|
|
|
|
{
|
|
|
|
//TODO - write
|
|
|
|
return -1;
|
|
|
|
}
|
2009-02-09 16:50:32 +02:00
|
|
|
|
|
|
|
std::vector<CObstacleInstance> CCallback::battleGetAllObstacles()
|
|
|
|
{
|
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
if(gs->curB)
|
|
|
|
return gs->curB->obstacles;
|
|
|
|
else
|
|
|
|
return std::vector<CObstacleInstance>();
|
|
|
|
}
|
|
|
|
|
2009-08-05 15:46:08 +03:00
|
|
|
int CCallback::battleGetStack(int pos, bool onlyAlive)
|
2008-04-07 20:51:46 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-08-05 15:46:08 +03:00
|
|
|
return gs->battleGetStack(pos, onlyAlive);
|
2008-04-07 20:51:46 +03:00
|
|
|
}
|
|
|
|
|
2009-08-05 15:46:08 +03:00
|
|
|
const CStack* CCallback::battleGetStackByID(int ID, bool onlyAlive)
|
2008-04-07 20:51:46 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
if(!gs->curB) return NULL;
|
2009-08-05 15:46:08 +03:00
|
|
|
return gs->curB->getStack(ID, onlyAlive);
|
2008-04-07 20:51:46 +03:00
|
|
|
}
|
|
|
|
|
2008-09-29 00:01:49 +03:00
|
|
|
int CCallback::battleMakeAction(BattleAction* action)
|
|
|
|
{
|
2009-03-20 20:51:48 +02:00
|
|
|
MakeCustomAction mca(*action);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&mca);
|
2008-09-29 00:01:49 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-05 15:46:08 +03:00
|
|
|
const CStack* CCallback::battleGetStackByPos(int pos, bool onlyAlive)
|
2008-06-11 04:58:18 +03:00
|
|
|
{
|
2008-08-20 22:02:48 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-08-05 15:46:08 +03:00
|
|
|
return battleGetStackByID(battleGetStack(pos, onlyAlive), onlyAlive);
|
2008-06-11 04:58:18 +03:00
|
|
|
}
|
|
|
|
|
2008-03-29 12:59:18 +02:00
|
|
|
int CCallback::battleGetPos(int stack)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-02-14 15:49:30 +02:00
|
|
|
if(!gs->curB)
|
|
|
|
{
|
|
|
|
tlog2<<"battleGetPos called when there is no battle!"<<std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t g=0; g<gs->curB->stacks.size(); ++g)
|
2008-03-29 12:59:18 +02:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
if(gs->curB->stacks[g]->ID == stack)
|
|
|
|
return gs->curB->stacks[g]->position;
|
2008-03-29 12:59:18 +02:00
|
|
|
}
|
2008-04-07 20:51:46 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-03-14 20:24:37 +02:00
|
|
|
std::map<int, CStack> CCallback::battleGetStacks()
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-03-14 20:24:37 +02:00
|
|
|
std::map<int, CStack> ret;
|
2008-09-05 19:08:25 +03:00
|
|
|
if(!gs->curB) //there is no battle
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t g=0; g<gs->curB->stacks.size(); ++g)
|
2008-03-14 20:24:37 +02:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
ret[gs->curB->stacks[g]->ID] = *(gs->curB->stacks[g]);
|
2008-03-14 20:24:37 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-07 05:29:44 +03:00
|
|
|
void CCallback::getStackQueue( std::vector<const CStack *> &out, int howMany )
|
2008-12-20 19:08:51 +02:00
|
|
|
{
|
2009-02-14 15:49:30 +02:00
|
|
|
if(!gs->curB)
|
|
|
|
{
|
2009-09-07 05:29:44 +03:00
|
|
|
tlog2 << "battleGetStackQueue called when there is not battle!" << std::endl;
|
|
|
|
return;
|
2009-02-14 15:49:30 +02:00
|
|
|
}
|
2009-09-07 05:29:44 +03:00
|
|
|
gs->curB->getStackQueue(out, howMany);
|
2008-12-20 19:08:51 +02:00
|
|
|
}
|
|
|
|
|
2008-03-23 19:25:38 +02:00
|
|
|
CCreature CCallback::battleGetCreature(int number)
|
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx); //TODO use me?
|
2009-02-14 15:49:30 +02:00
|
|
|
if(!gs->curB)
|
|
|
|
{
|
|
|
|
tlog2<<"battleGetCreature called when there is no battle!"<<std::endl;
|
|
|
|
}
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t h=0; h<gs->curB->stacks.size(); ++h)
|
2008-03-24 15:25:11 +02:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
if(gs->curB->stacks[h]->ID == number) //creature found
|
2010-05-02 21:20:26 +03:00
|
|
|
return *(gs->curB->stacks[h]->type);
|
2008-03-24 15:25:11 +02:00
|
|
|
}
|
2008-08-04 12:05:52 +03:00
|
|
|
#ifndef __GNUC__
|
2008-04-11 20:41:02 +03:00
|
|
|
throw new std::exception("Cannot find the creature");
|
2008-08-04 12:05:52 +03:00
|
|
|
#else
|
|
|
|
throw new std::exception();
|
|
|
|
#endif
|
2008-03-23 19:25:38 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 16:44:27 +02:00
|
|
|
std::vector<int> CCallback::battleGetAvailableHexes(int ID, bool addOccupiable)
|
2008-03-29 12:59:18 +02:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-02-14 15:49:30 +02:00
|
|
|
if(!gs->curB)
|
|
|
|
{
|
|
|
|
tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
|
|
|
|
return std::vector<int>();
|
|
|
|
}
|
2009-02-05 16:44:27 +02:00
|
|
|
return gs->curB->getAccessibility(ID, addOccupiable);
|
2008-08-06 02:33:08 +03:00
|
|
|
//return gs->battleGetRange(ID);
|
2008-03-29 12:59:18 +02:00
|
|
|
}
|
|
|
|
|
2008-04-07 20:51:46 +03:00
|
|
|
bool CCallback::battleIsStackMine(int ID)
|
|
|
|
{
|
2009-02-14 15:49:30 +02:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
if(!gs->curB)
|
|
|
|
{
|
|
|
|
tlog2<<"battleIsStackMine called when there is no battle!"<<std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t h=0; h<gs->curB->stacks.size(); ++h)
|
2008-04-07 20:51:46 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
if(gs->curB->stacks[h]->ID == ID) //creature found
|
|
|
|
return gs->curB->stacks[h]->owner == player;
|
2008-04-07 20:51:46 +03:00
|
|
|
}
|
|
|
|
return false;
|
2009-09-04 17:11:42 +03:00
|
|
|
}
|
|
|
|
|
2008-11-01 00:41:22 +02:00
|
|
|
bool CCallback::battleCanShoot(int ID, int dest)
|
2008-08-02 18:08:03 +03:00
|
|
|
{
|
2009-02-14 15:49:30 +02:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-11-17 20:47:43 +02:00
|
|
|
|
2009-09-04 17:11:42 +03:00
|
|
|
if(!gs->curB) return false;
|
2009-08-21 22:31:41 +03:00
|
|
|
|
2009-09-04 17:11:42 +03:00
|
|
|
return gs->battleCanShoot(ID, dest);
|
2008-08-02 18:08:03 +03:00
|
|
|
}
|
2008-08-16 11:47:41 +03:00
|
|
|
|
2009-04-16 17:01:27 +03:00
|
|
|
bool CCallback::battleCanCastSpell()
|
|
|
|
{
|
|
|
|
if(!gs->curB) //there is no battle
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(gs->curB->side1 == player)
|
2009-10-06 03:32:33 +03:00
|
|
|
return gs->curB->castSpells[0] == 0 && gs->curB->heroes[0]->getArt(17);
|
2009-04-16 17:01:27 +03:00
|
|
|
else
|
2009-10-06 03:32:33 +03:00
|
|
|
return gs->curB->castSpells[1] == 0 && gs->curB->heroes[1]->getArt(17);
|
2009-04-16 17:01:27 +03:00
|
|
|
}
|
|
|
|
|
2009-08-24 15:55:05 +03:00
|
|
|
bool CCallback::battleCanFlee()
|
2009-08-16 16:44:17 +03:00
|
|
|
{
|
|
|
|
return gs->battleCanFlee(player);
|
|
|
|
}
|
|
|
|
|
2009-08-24 15:55:05 +03:00
|
|
|
const CGTownInstance *CCallback::battleGetDefendedTown()
|
|
|
|
{
|
|
|
|
if(!gs->curB || gs->curB->tid == -1)
|
|
|
|
return NULL;
|
|
|
|
|
2009-08-28 12:03:58 +03:00
|
|
|
return static_cast<const CGTownInstance *>(gs->map->objects[gs->curB->tid]);
|
2009-08-24 15:55:05 +03:00
|
|
|
}
|
|
|
|
|
2009-08-26 17:09:55 +03:00
|
|
|
ui8 CCallback::battleGetWallState(int partOfWall)
|
|
|
|
{
|
|
|
|
if(!gs->curB || gs->curB->siege == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return gs->curB->si.wallState[partOfWall];
|
|
|
|
}
|
|
|
|
|
2009-09-01 16:54:13 +03:00
|
|
|
int CCallback::battleGetWallUnderHex(int hex)
|
|
|
|
{
|
|
|
|
if(!gs->curB || gs->curB->siege == 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return gs->curB->hexToWallPart(hex);
|
|
|
|
}
|
|
|
|
|
2009-08-29 20:09:07 +03:00
|
|
|
std::pair<ui32, ui32> CCallback::battleEstimateDamage(int attackerID, int defenderID)
|
|
|
|
{
|
|
|
|
if(!gs->curB)
|
|
|
|
return std::make_pair(0, 0);
|
|
|
|
|
|
|
|
const CGHeroInstance * attackerHero, * defenderHero;
|
|
|
|
|
|
|
|
if(gs->curB->side1 == player)
|
|
|
|
{
|
2009-10-06 03:32:33 +03:00
|
|
|
attackerHero = gs->curB->heroes[0];
|
|
|
|
defenderHero = gs->curB->heroes[1];
|
2009-08-29 20:09:07 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-06 03:32:33 +03:00
|
|
|
attackerHero = gs->curB->heroes[1];
|
|
|
|
defenderHero = gs->curB->heroes[0];
|
2009-08-29 20:09:07 +03:00
|
|
|
}
|
|
|
|
|
2009-09-21 15:48:28 +03:00
|
|
|
const CStack * attacker = gs->curB->getStack(attackerID, false),
|
|
|
|
* defender = gs->curB->getStack(defenderID);
|
2009-08-29 20:09:07 +03:00
|
|
|
|
2010-04-06 16:19:54 +03:00
|
|
|
return gs->curB->calculateDmgRange(attacker, defender, attackerHero, defenderHero, battleCanShoot(attacker->ID, defender->position), 0, false);
|
2009-08-29 20:09:07 +03:00
|
|
|
}
|
|
|
|
|
2009-09-02 17:10:19 +03:00
|
|
|
ui8 CCallback::battleGetSiegeLevel()
|
|
|
|
{
|
|
|
|
if(!gs->curB)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return gs->curB->siege;
|
|
|
|
}
|
|
|
|
|
2009-11-08 16:44:58 +02:00
|
|
|
const CGHeroInstance * CCallback::battleGetFightingHero(ui8 side) const
|
|
|
|
{
|
|
|
|
if(!gs->curB)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return gs->curB->heroes[side];
|
|
|
|
}
|
|
|
|
|
2008-08-16 11:47:41 +03:00
|
|
|
void CCallback::swapGarrisonHero( const CGTownInstance *town )
|
|
|
|
{
|
|
|
|
if(town->tempOwner != player) return;
|
2009-03-20 20:51:48 +02:00
|
|
|
|
|
|
|
GarrisonHeroSwap pack(town->id);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-08-25 13:25:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
|
|
|
|
{
|
|
|
|
if(hero->tempOwner != player) return;
|
2009-03-20 20:51:48 +02:00
|
|
|
|
|
|
|
BuyArtifact pack(hero->id,aid);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-08-28 20:36:34 +03:00
|
|
|
}
|
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
std::vector < const CGObjectInstance * > CCallback::getBlockingObjs( int3 pos ) const
|
2008-08-28 20:36:34 +03:00
|
|
|
{
|
|
|
|
std::vector<const CGObjectInstance *> ret;
|
2008-08-30 00:41:32 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-02-01 16:11:41 +02:00
|
|
|
if(!gs->map->isInTheMap(pos) || !isVisible(pos))
|
|
|
|
return ret;
|
2008-08-28 20:36:34 +03:00
|
|
|
BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
|
|
|
|
ret.push_back(obj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
std::vector < const CGObjectInstance * > CCallback::getVisitableObjs( int3 pos ) const
|
2008-08-28 20:36:34 +03:00
|
|
|
{
|
|
|
|
std::vector<const CGObjectInstance *> ret;
|
2008-08-30 00:41:32 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-02-01 16:11:41 +02:00
|
|
|
if(!gs->map->isInTheMap(pos) || !isVisible(pos))
|
|
|
|
return ret;
|
2008-08-28 20:36:34 +03:00
|
|
|
BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].visitableObjects)
|
|
|
|
ret.push_back(obj);
|
|
|
|
return ret;
|
2008-09-07 06:38:37 +03:00
|
|
|
}
|
|
|
|
|
2008-09-26 17:16:01 +03:00
|
|
|
std::vector < const CGObjectInstance * > CCallback::getFlaggableObjects(int3 pos) const
|
|
|
|
{
|
2009-02-01 16:11:41 +02:00
|
|
|
if(!isVisible(pos))
|
2008-09-26 17:16:01 +03:00
|
|
|
return std::vector < const CGObjectInstance * >();
|
|
|
|
|
|
|
|
std::vector < const CGObjectInstance * > ret;
|
|
|
|
|
|
|
|
std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t b=0; b<objs.size(); ++b)
|
2008-09-26 17:16:01 +03:00
|
|
|
{
|
|
|
|
if(objs[b].first->tempOwner!=254 && !((objs[b].first->defInfo->blockMap[pos.y - objs[b].first->pos.y + 5] >> (objs[b].first->pos.x - pos.x)) & 1))
|
|
|
|
ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 CCallback::getMapSize() const
|
|
|
|
{
|
|
|
|
return CGI->mh->sizes;
|
|
|
|
}
|
|
|
|
|
2010-05-18 10:01:54 +03:00
|
|
|
void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
|
2008-09-07 06:38:37 +03:00
|
|
|
{
|
2010-05-18 10:01:54 +03:00
|
|
|
TradeOnMarketplace pack;
|
|
|
|
pack.market = market;
|
|
|
|
pack.hero = hero;
|
|
|
|
pack.mode = mode;
|
|
|
|
pack.r1 = id1;
|
|
|
|
pack.r2 = id2;
|
|
|
|
pack.val = val1;
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-09-18 16:54:54 +03:00
|
|
|
}
|
|
|
|
|
2008-09-19 11:16:19 +03:00
|
|
|
void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
|
2008-09-18 16:54:54 +03:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
const_cast<CGHeroInstance*>(hero)-> formation = tight;
|
2009-03-20 20:51:48 +02:00
|
|
|
SetFormation pack(hero->id,tight);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2008-10-19 02:20:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCallback::setSelection(const CArmedInstance * obj)
|
|
|
|
{
|
2008-12-27 03:01:59 +02:00
|
|
|
SetSelection ss;
|
|
|
|
ss.player = player;
|
|
|
|
ss.id = obj->id;
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&ss);
|
2009-09-13 01:17:23 +03:00
|
|
|
|
|
|
|
if(obj->ID == HEROI_TYPE)
|
|
|
|
{
|
|
|
|
cl->gs->calculatePaths(static_cast<const CGHeroInstance *>(obj), *cl->pathInfo);
|
|
|
|
//nasty workaround. TODO: nice workaround
|
|
|
|
cl->gs->getPlayer(player)->currentSelection = obj->id;
|
|
|
|
}
|
2008-10-26 22:58:34 +02:00
|
|
|
}
|
|
|
|
|
2010-07-09 02:03:27 +03:00
|
|
|
void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
|
2008-10-26 22:58:34 +02:00
|
|
|
{
|
|
|
|
ui8 i=0;
|
2009-03-09 12:37:49 +02:00
|
|
|
for(; i<gs->players[player].availableHeroes.size(); i++)
|
|
|
|
{
|
2008-10-26 22:58:34 +02:00
|
|
|
if(gs->players[player].availableHeroes[i] == hero)
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2010-07-09 02:03:27 +03:00
|
|
|
HireHero pack(i,townOrTavern->id);
|
|
|
|
pack.player = player;
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pack);
|
2009-03-09 12:37:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-10-26 22:58:34 +02:00
|
|
|
}
|
|
|
|
|
2010-07-09 02:03:27 +03:00
|
|
|
std::vector<const CGHeroInstance *> CCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
|
2008-10-26 22:58:34 +02:00
|
|
|
{
|
|
|
|
std::vector<const CGHeroInstance *> ret(gs->players[player].availableHeroes.size());
|
|
|
|
std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
|
|
|
|
return ret;
|
2009-02-20 17:44:49 +02:00
|
|
|
}
|
|
|
|
|
2009-03-13 16:16:53 +02:00
|
|
|
const TerrainTile * CCallback::getTileInfo( int3 tile ) const
|
2009-02-20 17:44:49 +02:00
|
|
|
{
|
2009-05-22 22:20:30 +03:00
|
|
|
if(!gs->map->isInTheMap(tile))
|
|
|
|
{
|
|
|
|
tlog1 << tile << "is outside the map! (call to getTileInfo)\n";
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-03-13 16:16:53 +02:00
|
|
|
if(!isVisible(tile, player)) return NULL;
|
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-02-20 17:44:49 +02:00
|
|
|
return &gs->map->getTile(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCallback::canBuildStructure( const CGTownInstance *t, int ID )
|
|
|
|
{
|
|
|
|
return gs->canBuildStructure(t,ID);
|
2009-12-30 09:49:25 +02:00
|
|
|
}
|
|
|
|
|
2009-12-29 15:40:16 +02:00
|
|
|
std::set<int> CCallback::getBuildingRequiments( const CGTownInstance *t, int ID )
|
|
|
|
{
|
|
|
|
return gs->getBuildingRequiments(t,ID);
|
2009-03-19 16:17:19 +02:00
|
|
|
}
|
|
|
|
|
2009-06-11 20:21:06 +03:00
|
|
|
bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
|
2009-03-19 16:17:19 +02:00
|
|
|
{
|
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2009-06-11 20:21:06 +03:00
|
|
|
return gs->getPath(src,dest,hero, ret);
|
2009-03-28 02:38:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCallback::save( const std::string &fname )
|
|
|
|
{
|
|
|
|
cl->save(fname);
|
2009-04-04 22:26:41 +03:00
|
|
|
}
|
|
|
|
|
2009-04-05 17:37:14 +03:00
|
|
|
|
|
|
|
void CCallback::sendMessage(const std::string &mess)
|
|
|
|
{
|
|
|
|
PlayerMessage pm(player, mess);
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&pm);
|
2009-05-12 06:35:51 +03:00
|
|
|
}
|
2009-07-20 04:47:49 +03:00
|
|
|
|
2009-07-26 06:33:13 +03:00
|
|
|
void CCallback::buildBoat( const IShipyard *obj )
|
|
|
|
{
|
|
|
|
BuildBoat bb;
|
|
|
|
bb.objid = obj->o->id;
|
2009-08-04 02:53:18 +03:00
|
|
|
sendRequest(&bb);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void CCallback::sendRequest(const T* request)
|
|
|
|
{
|
|
|
|
//TODO? should be part of CClient but it would have to be very tricky cause template/serialization issues
|
|
|
|
if(waitTillRealize)
|
|
|
|
cl->waitingRequest.set(true);
|
|
|
|
|
|
|
|
*cl->serv << request;
|
|
|
|
|
|
|
|
if(waitTillRealize)
|
|
|
|
cl->waitingRequest.waitWhileTrue();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCallback::CCallback( CGameState * GS, int Player, CClient *C )
|
|
|
|
:gs(GS), cl(C), player(Player)
|
|
|
|
{
|
|
|
|
waitTillRealize = false;
|
2009-07-26 06:33:13 +03:00
|
|
|
}
|
|
|
|
|
2009-08-27 11:04:32 +03:00
|
|
|
const CMapHeader * CCallback::getMapHeader() const
|
|
|
|
{
|
|
|
|
return gs->map;
|
|
|
|
}
|
|
|
|
|
2009-08-30 15:47:40 +03:00
|
|
|
const CGPathNode * CCallback::getPathInfo( int3 tile )
|
|
|
|
{
|
|
|
|
return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCallback::getPath2( int3 dest, CGPath &ret )
|
|
|
|
{
|
2009-10-23 05:11:45 +03:00
|
|
|
if (!gs->map->isInTheMap(dest))
|
|
|
|
return false;
|
|
|
|
|
2009-09-07 05:29:44 +03:00
|
|
|
const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
|
|
|
|
assert(cl->pathInfo->hero == h);
|
|
|
|
if(cl->pathInfo->hpos != h->getPosition(false)) //hero position changed, must update paths
|
|
|
|
{
|
|
|
|
recalculatePaths();
|
|
|
|
}
|
2009-08-30 15:47:40 +03:00
|
|
|
return cl->pathInfo->getPath(dest, ret);
|
|
|
|
}
|
|
|
|
|
2009-09-07 05:29:44 +03:00
|
|
|
void CCallback::recalculatePaths()
|
|
|
|
{
|
|
|
|
gs->calculatePaths(cl->IGameCallback::getSelectedHero(player), *cl->pathInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
|
|
|
|
{
|
|
|
|
gs->calculatePaths(hero, out, src, movement);
|
|
|
|
}
|
|
|
|
|
2010-02-10 04:56:00 +02:00
|
|
|
int3 CCallback::getGrailPos( float &outKnownRatio )
|
|
|
|
{
|
2010-02-18 16:09:16 +02:00
|
|
|
if (CGObelisk::obeliskCount == 0)
|
|
|
|
{
|
|
|
|
outKnownRatio = 0.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outKnownRatio = (float)CGObelisk::visited[player] / CGObelisk::obeliskCount;
|
|
|
|
}
|
2010-02-10 04:56:00 +02:00
|
|
|
return gs->map->grailPos;
|
|
|
|
}
|
|
|
|
|
2010-02-21 17:03:30 +02:00
|
|
|
void CCallback::dig( const CGObjectInstance *hero )
|
|
|
|
{
|
|
|
|
DigWithHero dwh;
|
|
|
|
dwh.id = hero->id;
|
|
|
|
sendRequest(&dwh);
|
|
|
|
}
|
|
|
|
|
2010-02-24 20:11:08 +02:00
|
|
|
si8 CCallback::battleGetStackMorale( int stackID )
|
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
return gs->curB->getStack(stackID)->MoraleVal();
|
2010-02-24 20:11:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
si8 CCallback::battleGetStackLuck( int stackID )
|
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
return gs->curB->getStack(stackID)->LuckVal();
|
2010-02-24 20:11:08 +02:00
|
|
|
}
|
|
|
|
|
2010-03-11 01:16:30 +02:00
|
|
|
void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
|
|
|
|
{
|
|
|
|
CastAdvSpell cas;
|
|
|
|
cas.hid = hero->id;
|
|
|
|
cas.sid = spellID;
|
|
|
|
cas.pos = pos;
|
|
|
|
sendRequest(&cas);
|
|
|
|
}
|
|
|
|
|
2010-03-21 00:17:19 +02:00
|
|
|
bool CCallback::hasAccess(int playerId) const
|
|
|
|
{
|
|
|
|
return playerId == player || player < 0;
|
|
|
|
}
|
|
|
|
|
2010-04-06 16:19:54 +03:00
|
|
|
si8 CCallback::battleHasDistancePenalty( int stackID, int destHex )
|
|
|
|
{
|
|
|
|
return gs->curB->hasDistancePenalty(stackID, destHex);
|
|
|
|
}
|
|
|
|
|
|
|
|
si8 CCallback::battleHasWallPenalty( int stackID, int destHex )
|
|
|
|
{
|
|
|
|
return gs->curB->hasWallPenalty(stackID, destHex);
|
|
|
|
}
|
|
|
|
|
2010-05-07 17:05:48 +03:00
|
|
|
si8 CCallback::battleCanTeleportTo(int stackID, int destHex, int telportLevel)
|
|
|
|
{
|
|
|
|
return gs->curB->canTeleportTo(stackID, destHex, telportLevel);
|
|
|
|
}
|
|
|
|
|
2010-05-08 01:10:32 +03:00
|
|
|
int CCallback::getPlayerStatus(int player) const
|
|
|
|
{
|
|
|
|
const PlayerState *ps = gs->getPlayer(player, false);
|
|
|
|
if(!ps)
|
|
|
|
return -1;
|
|
|
|
return ps->status;
|
|
|
|
}
|
2010-07-09 02:03:27 +03:00
|
|
|
|
|
|
|
std::string CCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
|
|
|
|
{
|
|
|
|
return "GOSSIP TEST";
|
|
|
|
}
|
|
|
|
|
2009-07-20 05:56:35 +03:00
|
|
|
InfoAboutTown::InfoAboutTown()
|
|
|
|
{
|
|
|
|
tType = NULL;
|
|
|
|
details = NULL;
|
|
|
|
fortLevel = 0;
|
|
|
|
owner = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
InfoAboutTown::~InfoAboutTown()
|
|
|
|
{
|
|
|
|
delete details;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoAboutTown::initFromTown( const CGTownInstance *t, bool detailed )
|
|
|
|
{
|
2009-10-25 21:32:02 +02:00
|
|
|
obj = t;
|
2010-05-02 21:20:26 +03:00
|
|
|
army = t->getArmy();
|
2009-07-20 05:56:35 +03:00
|
|
|
built = t->builded;
|
|
|
|
fortLevel = t->fortLevel();
|
|
|
|
name = t->name;
|
|
|
|
tType = t->town;
|
|
|
|
owner = t->tempOwner;
|
|
|
|
|
|
|
|
if(detailed)
|
|
|
|
{
|
|
|
|
//include details about hero
|
|
|
|
details = new Details;
|
|
|
|
details->goldIncome = t->dailyIncome();
|
|
|
|
details->customRes = vstd::contains(t->builtBuildings, 15);
|
|
|
|
details->hallLevel = t->hallLevel();
|
|
|
|
details->garrisonedHero = t->garrisonHero;
|
|
|
|
}
|
2009-10-25 21:32:02 +02:00
|
|
|
/*else
|
2009-07-20 05:56:35 +03:00
|
|
|
{
|
|
|
|
//hide info about hero stacks counts
|
2010-05-02 21:20:26 +03:00
|
|
|
for(std::map<si32,std::pair<ui32,si32> >::iterator i = slots.begin(); i != slots.end(); ++i)
|
2009-07-20 05:56:35 +03:00
|
|
|
{
|
|
|
|
i->second.second = 0;
|
|
|
|
}
|
2009-10-25 21:32:02 +02:00
|
|
|
}*/
|
2009-10-23 05:11:45 +03:00
|
|
|
}
|
2010-03-21 00:17:19 +02:00
|
|
|
|
|
|
|
void InfoAboutTown::initFromGarrison(const CGGarrison *garr, bool detailed)
|
|
|
|
{
|
|
|
|
obj = garr;
|
|
|
|
fortLevel = 0;
|
2010-05-02 21:20:26 +03:00
|
|
|
army = garr->getArmy();
|
2010-03-21 00:17:19 +02:00
|
|
|
name = CGI->generaltexth->names[33]; // "Garrison"
|
|
|
|
owner = garr->tempOwner;
|
|
|
|
built = false;
|
|
|
|
tType = NULL;
|
|
|
|
|
|
|
|
// Show detailed info only to owning player.
|
|
|
|
if(detailed)
|
|
|
|
{
|
|
|
|
details = new InfoAboutTown::Details;
|
|
|
|
details->customRes = false;
|
|
|
|
details->garrisonedHero = false;
|
|
|
|
details->goldIncome = -1;
|
|
|
|
details->hallLevel = -1;
|
|
|
|
}
|
|
|
|
}
|