2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2008-12-27 03:01:59 +02:00
|
|
|
#include "IGameCallback.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
#include "CGameState.h"
|
2013-04-07 13:48:07 +03:00
|
|
|
#include "mapping/CMap.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CObjectHandler.h"
|
2011-05-03 06:14:18 +03:00
|
|
|
#include "CHeroHandler.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StartInfo.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CArtHandler.h"
|
|
|
|
#include "CSpellHandler.h"
|
2012-09-05 15:49:23 +03:00
|
|
|
#include "VCMI_Lib.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CTownHandler.h"
|
2011-05-03 06:14:18 +03:00
|
|
|
#include "BattleState.h"
|
|
|
|
#include "NetPacks.h"
|
2011-05-10 01:20:47 +03:00
|
|
|
#include "CBuildingHandler.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "GameConstants.h"
|
2012-09-05 15:49:23 +03:00
|
|
|
#include "CModHandler.h"
|
2013-02-19 01:37:22 +03:00
|
|
|
#include "CDefObjInfoHandler.h"
|
2013-03-06 21:49:56 +03:00
|
|
|
#include "CBonusTypeHandler.h"
|
2013-02-19 01:37:22 +03:00
|
|
|
|
|
|
|
#include "Connection.h"
|
2008-12-27 03:01:59 +02:00
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* IGameCallback.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-05-10 01:20:47 +03:00
|
|
|
//TODO make clean
|
2011-05-03 06:14:18 +03:00
|
|
|
#define ERROR_SILENT_RET_VAL_IF(cond, txt, retVal) do {if(cond){return retVal;}} while(0)
|
2013-04-09 17:31:36 +03:00
|
|
|
#define ERROR_VERBOSE_OR_NOT_RET_VAL_IF(cond, verbose, txt, retVal) do {if(cond){if(verbose)logGlobal->errorStream() << BOOST_CURRENT_FUNCTION << ": " << txt; return retVal;}} while(0)
|
|
|
|
#define ERROR_RET_IF(cond, txt) do {if(cond){logGlobal->errorStream() << BOOST_CURRENT_FUNCTION << ": " << txt; return;}} while(0)
|
|
|
|
#define ERROR_RET_VAL_IF(cond, txt, retVal) do {if(cond){logGlobal->errorStream() << BOOST_CURRENT_FUNCTION << ": " << txt; return retVal;}} while(0)
|
2011-05-03 06:14:18 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
extern std::minstd_rand ran;
|
2009-10-24 22:21:32 +03:00
|
|
|
|
2012-05-28 22:29:32 +03:00
|
|
|
CGameState * CPrivilagedInfoCallback::gameState ()
|
2013-02-13 22:35:43 +03:00
|
|
|
{
|
2011-05-03 06:14:18 +03:00
|
|
|
return gs;
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerColor CGameInfoCallback::getOwner(ObjectInstanceID heroID) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
const CGObjectInstance *obj = getObj(heroID);
|
2013-03-03 20:06:03 +03:00
|
|
|
ERROR_RET_VAL_IF(!obj, "No such object!", PlayerColor::CANNOT_DETERMINE);
|
2013-02-14 02:55:42 +03:00
|
|
|
return obj->tempOwner;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
int CGameInfoCallback::getResource(PlayerColor Player, Res::ERes which) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2011-05-10 01:20:47 +03:00
|
|
|
const PlayerState *p = getPlayer(Player);
|
2011-05-03 06:14:18 +03:00
|
|
|
ERROR_RET_VAL_IF(!p, "No player info!", -1);
|
|
|
|
ERROR_RET_VAL_IF(p->resources.size() <= which || which < 0, "No such resource!", -1);
|
|
|
|
return p->resources[which];
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
const CGHeroInstance* CGameInfoCallback::getSelectedHero( PlayerColor Player ) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2011-05-10 01:20:47 +03:00
|
|
|
const PlayerState *p = getPlayer(Player);
|
2013-06-26 14:18:27 +03:00
|
|
|
ERROR_RET_VAL_IF(!p, "No player info!", nullptr);
|
2011-05-03 06:14:18 +03:00
|
|
|
return getHero(p->currentSelection);
|
|
|
|
}
|
|
|
|
|
2011-05-22 21:46:52 +03:00
|
|
|
const CGHeroInstance* CGameInfoCallback::getSelectedHero() const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2011-05-22 21:46:52 +03:00
|
|
|
return getSelectedHero(gs->currentPlayer);
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
const PlayerSettings * CGameInfoCallback::getPlayerSettings(PlayerColor color) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
return &gs->scenarioOps->getIthPlayersSettings(color);
|
|
|
|
}
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
void CPrivilagedInfoCallback::getTilesInRange( std::unordered_set<int3, ShashInt3> &tiles, int3 pos, int radious, boost::optional<PlayerColor> player/*=uninit*/, int mode/*=0*/ ) const
|
2009-03-14 13:25:25 +02:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
if(!!player && *player >= PlayerColor::PLAYER_LIMIT)
|
2009-03-14 13:25:25 +02:00
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Illegal call to getTilesInRange!";
|
2009-03-14 13:25:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-11-11 19:45:03 +02:00
|
|
|
if (radious == -1) //reveal entire map
|
|
|
|
getAllTiles (tiles, player, -1, 0);
|
|
|
|
else
|
2009-03-14 13:25:25 +02:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
const TeamState * team = !player ? nullptr : gs->getPlayerTeam(*player);
|
2009-11-11 19:45:03 +02:00
|
|
|
for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
|
2009-03-14 13:25:25 +02:00
|
|
|
{
|
2009-11-11 19:45:03 +02:00
|
|
|
for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
|
2009-03-14 13:25:25 +02:00
|
|
|
{
|
2009-11-11 19:45:03 +02:00
|
|
|
double distance = pos.dist2d(int3(xd,yd,pos.z)) - 0.5;
|
|
|
|
if(distance <= radious)
|
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
if(!player
|
2010-08-03 15:34:06 +03:00
|
|
|
|| (mode == 1 && team->fogOfWarMap[xd][yd][pos.z]==0)
|
|
|
|
|| (mode == -1 && team->fogOfWarMap[xd][yd][pos.z]==1)
|
2009-11-11 19:45:03 +02:00
|
|
|
)
|
|
|
|
tiles.insert(int3(xd,yd,pos.z));
|
|
|
|
}
|
2009-03-14 13:25:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
void CPrivilagedInfoCallback::getAllTiles (std::unordered_set<int3, ShashInt3> &tiles, boost::optional<PlayerColor> Player/*=uninit*/, int level, int surface ) const
|
2009-08-19 09:56:53 +03:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
if(!!Player && *Player >= PlayerColor::PLAYER_LIMIT)
|
2009-08-19 09:56:53 +03:00
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Illegal call to getAllTiles !";
|
2009-08-19 09:56:53 +03:00
|
|
|
return;
|
|
|
|
}
|
2009-08-20 13:30:00 +03:00
|
|
|
bool water = surface == 0 || surface == 2,
|
|
|
|
land = surface == 0 || surface == 1;
|
|
|
|
|
|
|
|
std::vector<int> floors;
|
|
|
|
if(level == -1)
|
|
|
|
{
|
2012-11-20 20:53:45 +03:00
|
|
|
for(int b = 0; b < (gs->map->twoLevel ? 2 : 1); ++b)
|
2009-08-20 13:30:00 +03:00
|
|
|
{
|
|
|
|
floors.push_back(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
floors.push_back(level);
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto zd : floors)
|
2009-08-19 09:56:53 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
|
2009-08-20 13:30:00 +03:00
|
|
|
for (int xd = 0; xd < gs->map->width; xd++)
|
2009-08-19 09:56:53 +03:00
|
|
|
{
|
2009-08-20 13:30:00 +03:00
|
|
|
for (int yd = 0; yd < gs->map->height; yd++)
|
2009-08-19 09:56:53 +03:00
|
|
|
{
|
2013-11-24 14:36:51 +03:00
|
|
|
if ((getTile (int3 (xd,yd,zd))->terType == ETerrainType::WATER && water)
|
|
|
|
|| (getTile (int3 (xd,yd,zd))->terType != ETerrainType::WATER && land))
|
2009-08-23 18:02:21 +03:00
|
|
|
tiles.insert(int3(xd,yd,zd));
|
2009-08-19 09:56:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
void CPrivilagedInfoCallback::getFreeTiles (std::vector<int3> &tiles) const
|
2010-08-22 10:11:46 +03:00
|
|
|
{
|
|
|
|
std::vector<int> floors;
|
2012-11-20 20:53:45 +03:00
|
|
|
for (int b = 0; b < (gs->map->twoLevel ? 2 : 1); ++b)
|
2010-08-22 10:11:46 +03:00
|
|
|
{
|
|
|
|
floors.push_back(b);
|
|
|
|
}
|
2011-05-10 01:20:47 +03:00
|
|
|
const TerrainTile *tinfo;
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto zd : floors)
|
2010-08-22 10:11:46 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
|
2010-08-22 10:11:46 +03:00
|
|
|
for (int xd = 0; xd < gs->map->width; xd++)
|
|
|
|
{
|
|
|
|
for (int yd = 0; yd < gs->map->height; yd++)
|
|
|
|
{
|
2011-05-03 06:14:18 +03:00
|
|
|
tinfo = getTile(int3 (xd,yd,zd));
|
2013-11-24 14:36:51 +03:00
|
|
|
if (tinfo->terType != ETerrainType::WATER && !tinfo->blocked) //land and free
|
2010-08-22 10:11:46 +03:00
|
|
|
tiles.push_back (int3 (xd,yd,zd));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
bool CGameInfoCallback::isAllowed( int type, int id )
|
2009-03-14 13:25:25 +02:00
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return gs->map->allowedSpell[id];
|
2009-04-16 03:28:54 +03:00
|
|
|
case 1:
|
|
|
|
return gs->map->allowedArtifact[id];
|
2010-07-20 17:08:13 +03:00
|
|
|
case 2:
|
|
|
|
return gs->map->allowedAbilities[id];
|
2009-03-14 13:25:25 +02:00
|
|
|
default:
|
2011-05-03 06:14:18 +03:00
|
|
|
ERROR_RET_VAL_IF(1, "Wrong type!", false);
|
2009-03-14 13:25:25 +02:00
|
|
|
}
|
2009-04-16 03:28:54 +03:00
|
|
|
}
|
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
void CPrivilagedInfoCallback::pickAllowedArtsSet(std::vector<const CArtifact*> &out)
|
2010-06-27 19:03:01 +03:00
|
|
|
{
|
2012-09-06 13:39:48 +03:00
|
|
|
for (int j = 0; j < 3 ; j++)
|
|
|
|
out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_TREASURE)]);
|
|
|
|
for (int j = 0; j < 3 ; j++)
|
|
|
|
out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_MINOR)]);
|
|
|
|
|
2010-06-27 19:03:01 +03:00
|
|
|
out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_MAJOR)]);
|
|
|
|
}
|
|
|
|
|
2013-02-13 01:24:48 +03:00
|
|
|
ArtifactID CPrivilagedInfoCallback::getRandomArt (int flags)
|
2009-04-16 03:28:54 +03:00
|
|
|
{
|
2010-06-28 08:07:21 +03:00
|
|
|
return VLC->arth->getRandomArt(flags);
|
2009-10-24 22:21:32 +03:00
|
|
|
}
|
|
|
|
|
2013-02-13 01:24:48 +03:00
|
|
|
ArtifactID CPrivilagedInfoCallback::getArtSync (ui32 rand, int flags, bool erasePicked)
|
2009-10-24 22:21:32 +03:00
|
|
|
{
|
2013-02-13 01:24:48 +03:00
|
|
|
return VLC->arth->getArtSync (rand, flags, erasePicked);
|
2009-05-21 03:55:30 +03:00
|
|
|
}
|
2009-07-26 06:33:13 +03:00
|
|
|
|
2013-02-11 02:24:57 +03:00
|
|
|
void CPrivilagedInfoCallback::getAllowedSpells(std::vector<SpellID> &out, ui16 level)
|
2009-09-24 20:54:02 +03:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
for (ui32 i = 0; i < gs->map->allowedSpell.size(); i++) //spellh size appears to be greater (?)
|
2009-09-24 20:54:02 +03:00
|
|
|
{
|
2013-02-13 22:35:43 +03:00
|
|
|
|
|
|
|
const CSpell *spell = SpellID(i).toSpell();
|
2009-09-24 22:28:26 +03:00
|
|
|
if (isAllowed (0, spell->id) && spell->level == level)
|
2009-09-24 20:54:02 +03:00
|
|
|
{
|
2009-09-25 12:33:59 +03:00
|
|
|
out.push_back(spell->id);
|
2009-09-24 20:54:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-19 01:37:22 +03:00
|
|
|
|
|
|
|
template<typename Loader>
|
|
|
|
void CPrivilagedInfoCallback::loadCommonState(Loader &in)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() << "Loading lib part of game...";
|
2013-02-19 01:37:22 +03:00
|
|
|
in.checkMagicBytes(SAVEGAME_MAGIC);
|
|
|
|
|
|
|
|
CMapHeader dum;
|
|
|
|
StartInfo *si;
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() <<"\tReading header";
|
2013-02-19 01:37:22 +03:00
|
|
|
in >> dum;
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() << "\tReading options";
|
2013-02-19 01:37:22 +03:00
|
|
|
in >> si;
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() <<"\tReading handlers";
|
2013-02-19 01:37:22 +03:00
|
|
|
in >> *VLC;
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() <<"\tReading gamestate";
|
2013-02-19 01:37:22 +03:00
|
|
|
in >> gs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Saver>
|
|
|
|
void CPrivilagedInfoCallback::saveCommonState(Saver &out) const
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() << "Saving lib part of game...";
|
2013-02-19 01:37:22 +03:00
|
|
|
out.putMagicBytes(SAVEGAME_MAGIC);
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() <<"\tSaving header";
|
2013-02-19 01:37:22 +03:00
|
|
|
out << static_cast<CMapHeader&>(*gs->map);
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() << "\tSaving options";
|
2013-02-19 01:37:22 +03:00
|
|
|
out << gs->scenarioOps;
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() << "\tSaving handlers";
|
2013-02-19 01:37:22 +03:00
|
|
|
out << *VLC;
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() << "\tSaving gamestate";
|
2013-02-19 01:37:22 +03:00
|
|
|
out << gs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template DLL_LINKAGE void CPrivilagedInfoCallback::loadCommonState<CLoadIntegrityValidator>(CLoadIntegrityValidator&);
|
|
|
|
template DLL_LINKAGE void CPrivilagedInfoCallback::loadCommonState<CLoadFile>(CLoadFile&);
|
|
|
|
template DLL_LINKAGE void CPrivilagedInfoCallback::saveCommonState<CSaveFile>(CSaveFile&) const;
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
TerrainTile * CNonConstInfoCallback::getTile( int3 pos )
|
2009-07-26 06:33:13 +03:00
|
|
|
{
|
|
|
|
if(!gs->map->isInTheMap(pos))
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2009-07-26 06:33:13 +03:00
|
|
|
return &gs->map->getTile(pos);
|
2010-02-10 04:56:00 +02:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
const PlayerState * CGameInfoCallback::getPlayer(PlayerColor color, bool verbose) const
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!hasAccess(color), verbose, "Cannot access player " << color << "info!", nullptr);
|
|
|
|
ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!vstd::contains(gs->players,color), verbose, "Cannot find player " << color << "info!", nullptr);
|
2011-05-10 01:20:47 +03:00
|
|
|
return &gs->players[color];
|
2010-06-27 19:03:01 +03:00
|
|
|
}
|
2010-07-09 02:03:27 +03:00
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
const CTown * CGameInfoCallback::getNativeTown(PlayerColor color) const
|
2010-07-09 02:03:27 +03:00
|
|
|
{
|
2011-05-03 06:14:18 +03:00
|
|
|
const PlayerSettings *ps = getPlayerSettings(color);
|
2013-06-26 14:18:27 +03:00
|
|
|
ERROR_RET_VAL_IF(!ps, "There is no such player!", nullptr);
|
2013-04-21 15:49:26 +03:00
|
|
|
return VLC->townh->factions[ps->castle]->town;
|
2011-02-11 14:27:38 +02:00
|
|
|
}
|
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
const CGObjectInstance * CGameInfoCallback::getObjByQuestIdentifier(int identifier) const
|
2011-02-11 14:27:38 +02:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
ERROR_RET_VAL_IF(!vstd::contains(gs->map->questIdentifierToId, identifier), "There is no object with such quest identifier!", nullptr);
|
2011-02-11 14:27:38 +02:00
|
|
|
return getObj(gs->map->questIdentifierToId[identifier]);
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
|
|
|
|
2013-02-14 02:55:42 +03:00
|
|
|
const CGObjectInstance* CGameInfoCallback::getObj(ObjectInstanceID objid, bool verbose) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-02-14 02:55:42 +03:00
|
|
|
si32 oid = objid.num;
|
|
|
|
if(oid < 0 || oid >= gs->map->objects.size())
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
if(verbose)
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Cannot get object with id " << oid;
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
2013-02-14 02:55:42 +03:00
|
|
|
const CGObjectInstance *ret = gs->map->objects[oid];
|
2011-05-03 06:14:18 +03:00
|
|
|
if(!ret)
|
|
|
|
{
|
|
|
|
if(verbose)
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Cannot get object with id " << oid << ". Object was removed.";
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!isVisible(ret, player))
|
|
|
|
{
|
|
|
|
if(verbose)
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Cannot get object with id " << oid << ". Object is not visible.";
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-14 02:55:42 +03:00
|
|
|
const CGHeroInstance* CGameInfoCallback::getHero(ObjectInstanceID objid) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
const CGObjectInstance *obj = getObj(objid, false);
|
|
|
|
if(obj)
|
|
|
|
return dynamic_cast<const CGHeroInstance*>(obj);
|
|
|
|
else
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
2013-02-14 02:55:42 +03:00
|
|
|
const CGTownInstance* CGameInfoCallback::getTown(ObjectInstanceID objid) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
const CGObjectInstance *obj = getObj(objid, false);
|
|
|
|
if(obj)
|
2013-02-14 02:55:42 +03:00
|
|
|
return dynamic_cast<const CGTownInstance*>(obj);
|
2011-05-03 06:14:18 +03:00
|
|
|
else
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
void CGameInfoCallback::getUpgradeInfo(const CArmedInstance *obj, SlotID stackPos, UpgradeInfo &out) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
ERROR_RET_IF(!canGetFullInfo(obj), "Cannot get info about not owned object!");
|
|
|
|
ERROR_RET_IF(!obj->hasStackAtSlot(stackPos), "There is no such stack!");
|
|
|
|
out = gs->getUpgradeInfo(obj->getStack(stackPos));
|
|
|
|
//return gs->getUpgradeInfo(obj->getStack(stackPos));
|
|
|
|
}
|
|
|
|
|
2012-05-23 00:08:16 +03:00
|
|
|
const StartInfo * CGameInfoCallback::getStartInfo(bool beforeRandomization /*= false*/) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2012-05-23 00:08:16 +03:00
|
|
|
if(beforeRandomization)
|
|
|
|
return gs->initialOpts;
|
|
|
|
else
|
|
|
|
return gs->scenarioOps;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CGameInfoCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
|
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
ERROR_RET_VAL_IF(!canGetFullInfo(caster), "Cannot get info about caster!", -1);
|
|
|
|
//if there is a battle
|
|
|
|
if(gs->curB)
|
2012-08-26 12:07:48 +03:00
|
|
|
return gs->curB->battleGetSpellCost(sp, caster);
|
2011-05-03 06:14:18 +03:00
|
|
|
|
|
|
|
//if there is no battle
|
|
|
|
return caster->getSpellCost(sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CGameInfoCallback::estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
|
|
|
|
ERROR_RET_VAL_IF(hero && !canGetFullInfo(hero), "Cannot get info about caster!", -1);
|
|
|
|
if(!gs->curB) //no battle
|
|
|
|
{
|
|
|
|
if (hero) //but we see hero's spellbook
|
2013-02-06 13:16:44 +03:00
|
|
|
return gs->curB->calculateSpellDmg(
|
2013-06-26 14:18:27 +03:00
|
|
|
sp, hero, nullptr, hero->getSpellSchoolLevel(sp), hero->getPrimSkillLevel(PrimarySkill::SPELL_POWER));
|
2011-05-03 06:14:18 +03:00
|
|
|
else
|
|
|
|
return 0; //mage guild
|
|
|
|
}
|
|
|
|
//gs->getHero(gs->currentPlayer)
|
|
|
|
//const CGHeroInstance * ourHero = gs->curB->heroes[0]->tempOwner == player ? gs->curB->heroes[0] : gs->curB->heroes[1];
|
|
|
|
const CGHeroInstance * ourHero = hero;
|
2013-02-06 13:16:44 +03:00
|
|
|
return gs->curB->calculateSpellDmg(
|
2013-06-26 14:18:27 +03:00
|
|
|
sp, ourHero, nullptr, ourHero->getSpellSchoolLevel(sp), ourHero->getPrimSkillLevel(PrimarySkill::SPELL_POWER));
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGameInfoCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
ERROR_RET_IF(!obj, "No guild object!");
|
2012-09-23 21:01:04 +03:00
|
|
|
ERROR_RET_IF(obj->ID == Obj::TOWN && !canGetFullInfo(obj), "Cannot get info about town guild object!");
|
2011-05-03 06:14:18 +03:00
|
|
|
//TODO: advmap object -> check if they're visited by our hero
|
|
|
|
|
2012-09-23 21:01:04 +03:00
|
|
|
if(obj->ID == Obj::TOWN || obj->ID == Obj::TAVERN)
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
gs->obtainPlayersStats(thi, gs->players[obj->tempOwner].towns.size());
|
|
|
|
}
|
2012-09-23 21:01:04 +03:00
|
|
|
else if(obj->ID == Obj::DEN_OF_THIEVES)
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
gs->obtainPlayersStats(thi, 20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
int CGameInfoCallback::howManyTowns(PlayerColor Player) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
ERROR_RET_VAL_IF(!hasAccess(Player), "Access forbidden!", -1);
|
|
|
|
return gs->players[Player].towns.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameInfoCallback::getTownInfo( const CGObjectInstance *town, InfoAboutTown &dest ) const
|
|
|
|
{
|
|
|
|
ERROR_RET_VAL_IF(!isVisible(town, player), "Town is not visible!", false); //it's not a town or it's not visible for layer
|
|
|
|
bool detailed = hasAccess(town->tempOwner);
|
|
|
|
|
|
|
|
//TODO vision support
|
2012-09-23 21:01:04 +03:00
|
|
|
if(town->ID == Obj::TOWN)
|
2011-05-03 06:14:18 +03:00
|
|
|
dest.initFromTown(static_cast<const CGTownInstance *>(town), detailed);
|
2012-08-26 12:07:48 +03:00
|
|
|
else if(town->ID == Obj::GARRISON || town->ID == Obj::GARRISON2)
|
2012-06-13 16:04:06 +03:00
|
|
|
dest.initFromArmy(static_cast<const CArmedInstance *>(town), detailed);
|
2011-05-03 06:14:18 +03:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 CGameInfoCallback::guardingCreaturePosition (int3 pos) const
|
|
|
|
{
|
|
|
|
ERROR_RET_VAL_IF(!isVisible(pos), "Tile is not visible!", int3(-1,-1,-1));
|
|
|
|
return gs->guardingCreaturePosition(pos);
|
|
|
|
}
|
|
|
|
|
2012-11-15 00:19:32 +03:00
|
|
|
std::vector<const CGObjectInstance*> CGameInfoCallback::getGuardingCreatures (int3 pos) const
|
2012-11-14 17:27:18 +03:00
|
|
|
{
|
2012-11-15 00:19:32 +03:00
|
|
|
ERROR_RET_VAL_IF(!isVisible(pos), "Tile is not visible!", std::vector<const CGObjectInstance*>());
|
|
|
|
std::vector<const CGObjectInstance*> ret;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto cr : gs->guardingCreatures(pos))
|
2012-11-15 00:19:32 +03:00
|
|
|
{
|
|
|
|
ret.push_back(cr);
|
|
|
|
}
|
|
|
|
return ret;
|
2012-11-14 17:27:18 +03:00
|
|
|
}
|
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
bool CGameInfoCallback::getHeroInfo( const CGObjectInstance *hero, InfoAboutHero &dest ) const
|
|
|
|
{
|
|
|
|
const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(hero);
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
ERROR_RET_VAL_IF(!h, "That's not a hero!", false);
|
|
|
|
ERROR_RET_VAL_IF(!isVisible(h->getPosition(false)), "That hero is not visible!", false);
|
|
|
|
|
|
|
|
//TODO vision support
|
|
|
|
dest.initFromHero(h, hasAccess(h->tempOwner));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-02 11:29:57 +03:00
|
|
|
int CGameInfoCallback::getDate(Date::EDateType mode) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
return gs->getDate(mode);
|
|
|
|
}
|
|
|
|
std::vector < std::string > CGameInfoCallback::getObjDescriptions(int3 pos) const
|
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
std::vector<std::string> ret;
|
2011-05-10 01:20:47 +03:00
|
|
|
const TerrainTile *t = getTile(pos);
|
2011-05-03 06:14:18 +03:00
|
|
|
ERROR_RET_VAL_IF(!t, "Not a valid tile given!", ret);
|
|
|
|
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const CGObjectInstance * obj : t->blockingObjects)
|
2011-05-03 06:14:18 +03:00
|
|
|
ret.push_back(obj->getHoverText());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
bool CGameInfoCallback::isVisible(int3 pos, boost::optional<PlayerColor> Player) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2013-02-11 17:42:09 +03:00
|
|
|
return gs->map->isInTheMap(pos) && (!Player || gs->isVisible(pos, *Player));
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameInfoCallback::isVisible(int3 pos) const
|
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
return isVisible(pos, player);
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
bool CGameInfoCallback::isVisible( const CGObjectInstance *obj, boost::optional<PlayerColor> Player ) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
return gs->isVisible(obj, Player);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameInfoCallback::isVisible(const CGObjectInstance *obj) const
|
|
|
|
{
|
|
|
|
return isVisible(obj, player);
|
|
|
|
}
|
|
|
|
// const CCreatureSet* CInfoCallback::getGarrison(const CGObjectInstance *obj) const
|
|
|
|
// {
|
|
|
|
// //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
// if()
|
|
|
|
// const CArmedInstance *armi = dynamic_cast<const CArmedInstance*>(obj);
|
|
|
|
// if(!armi)
|
2013-06-26 14:18:27 +03:00
|
|
|
// return nullptr;
|
2013-02-13 22:35:43 +03:00
|
|
|
// else
|
2011-05-03 06:14:18 +03:00
|
|
|
// return armi;
|
|
|
|
// }
|
|
|
|
|
|
|
|
std::vector < const CGObjectInstance * > CGameInfoCallback::getBlockingObjs( int3 pos ) const
|
|
|
|
{
|
|
|
|
std::vector<const CGObjectInstance *> ret;
|
2011-05-10 01:20:47 +03:00
|
|
|
const TerrainTile *t = getTile(pos);
|
2011-05-03 06:14:18 +03:00
|
|
|
ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const CGObjectInstance * obj : t->blockingObjects)
|
2011-05-03 06:14:18 +03:00
|
|
|
ret.push_back(obj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-14 10:22:08 +03:00
|
|
|
std::vector <const CGObjectInstance * > CGameInfoCallback::getVisitableObjs(int3 pos, bool verbose /*= true*/) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
std::vector<const CGObjectInstance *> ret;
|
2012-04-14 10:22:08 +03:00
|
|
|
const TerrainTile *t = getTile(pos, verbose);
|
|
|
|
ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!t, verbose, pos << " is not visible!", ret);
|
2011-05-03 06:14:18 +03:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const CGObjectInstance * obj : t->visitableObjects)
|
2012-04-14 10:22:08 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
if(player < nullptr || obj->ID != Obj::EVENT) //hide events from players
|
2012-04-14 10:22:08 +03:00
|
|
|
ret.push_back(obj);
|
|
|
|
}
|
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector < const CGObjectInstance * > CGameInfoCallback::getFlaggableObjects(int3 pos) const
|
|
|
|
{
|
|
|
|
std::vector<const CGObjectInstance *> ret;
|
2011-05-10 01:20:47 +03:00
|
|
|
const TerrainTile *t = getTile(pos);
|
2011-05-03 06:14:18 +03:00
|
|
|
ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const CGObjectInstance *obj : t->blockingObjects)
|
2013-03-03 20:06:03 +03:00
|
|
|
if(obj->tempOwner != PlayerColor::UNFLAGGABLE)
|
2011-05-03 06:14:18 +03:00
|
|
|
ret.push_back(obj);
|
|
|
|
// const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
|
|
|
|
// for(size_t b=0; b<objs.size(); ++b)
|
|
|
|
// {
|
|
|
|
// 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 CGameInfoCallback::getMapSize() const
|
|
|
|
{
|
2012-11-20 20:53:45 +03:00
|
|
|
return int3(gs->map->width, gs->map->height, gs->map->twoLevel ? 2 : 1);
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const CGHeroInstance *> CGameInfoCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
|
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
ASSERT_IF_CALLED_WITH_PLAYER
|
2011-05-03 06:14:18 +03:00
|
|
|
std::vector<const CGHeroInstance *> ret;
|
2011-06-01 21:26:44 +03:00
|
|
|
//ERROR_RET_VAL_IF(!isOwnedOrVisited(townOrTavern), "Town or tavern must be owned or visited!", ret);
|
|
|
|
//TODO: town needs to be owned, advmap tavern needs to be visited; to be reimplemented when visit tracking is done
|
2013-02-11 17:42:09 +03:00
|
|
|
ret.resize(gs->players[*player].availableHeroes.size());
|
|
|
|
std::copy(gs->players[*player].availableHeroes.begin(),gs->players[*player].availableHeroes.end(),ret.begin());
|
2011-05-03 06:14:18 +03:00
|
|
|
return ret;
|
2013-02-13 22:35:43 +03:00
|
|
|
}
|
2011-05-03 06:14:18 +03:00
|
|
|
|
2011-05-10 01:20:47 +03:00
|
|
|
const TerrainTile * CGameInfoCallback::getTile( int3 tile, bool verbose) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!isVisible(tile), verbose, tile << " is not visible!", nullptr);
|
2011-05-03 06:14:18 +03:00
|
|
|
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
return &gs->map->getTile(tile);
|
|
|
|
}
|
|
|
|
|
2013-02-11 22:11:34 +03:00
|
|
|
EBuildingState::EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID )
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-02-09 15:56:35 +03:00
|
|
|
ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED);
|
2011-05-10 01:20:47 +03:00
|
|
|
|
2013-11-24 14:36:51 +03:00
|
|
|
if(!t->town->buildings.count(ID))
|
|
|
|
return EBuildingState::BUILDING_ERROR;
|
|
|
|
|
2013-12-02 14:58:02 +03:00
|
|
|
const CBuilding * building = t->town->buildings.at(ID);
|
2013-11-24 14:36:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
if(t->hasBuilt(ID)) //already built
|
2012-09-23 17:32:49 +03:00
|
|
|
return EBuildingState::ALREADY_PRESENT;
|
|
|
|
|
|
|
|
//can we build it?
|
2013-02-11 22:11:34 +03:00
|
|
|
if(vstd::contains(t->forbiddenBuildings, ID))
|
2012-09-23 17:32:49 +03:00
|
|
|
return EBuildingState::FORBIDDEN; //forbidden
|
2011-05-10 01:20:47 +03:00
|
|
|
|
2013-02-11 02:24:57 +03:00
|
|
|
if(ID == BuildingID::CAPITOL)
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
|
|
|
const PlayerState *ps = getPlayer(t->tempOwner);
|
|
|
|
if(ps)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const CGTownInstance *t : ps->towns)
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
2013-02-11 02:24:57 +03:00
|
|
|
if(t->hasBuilt(BuildingID::CAPITOL))
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
2012-09-23 17:32:49 +03:00
|
|
|
return EBuildingState::HAVE_CAPITAL; //no more than one capitol
|
2011-05-10 01:20:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-11 02:24:57 +03:00
|
|
|
else if(ID == BuildingID::SHIPYARD)
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
2012-06-22 14:40:16 +03:00
|
|
|
const TerrainTile *tile = getTile(t->bestLocation(), false);
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2013-12-03 12:03:37 +03:00
|
|
|
if(!tile || tile->terType != ETerrainType::WATER)
|
2012-09-23 17:32:49 +03:00
|
|
|
return EBuildingState::NO_WATER; //lack of water
|
2011-05-10 01:20:47 +03:00
|
|
|
}
|
|
|
|
|
2013-12-03 12:03:37 +03:00
|
|
|
auto buildTest = [&](const BuildingID & id)
|
|
|
|
{
|
|
|
|
return t->hasBuilt(id);
|
|
|
|
};
|
|
|
|
|
|
|
|
if(t->builded >= VLC->modh->settings.MAX_BUILDING_PER_TURN)
|
|
|
|
return EBuildingState::CANT_BUILD_TODAY; //building limit
|
|
|
|
|
|
|
|
if (!building->requirements.test(buildTest))
|
|
|
|
return EBuildingState::PREREQUIRES;
|
|
|
|
|
|
|
|
if (building->upgrade != BuildingID::NONE && !t->hasBuilt(building->upgrade))
|
|
|
|
return EBuildingState::MISSING_BASE;
|
|
|
|
|
2012-09-23 17:32:49 +03:00
|
|
|
//checking resources
|
2013-12-02 14:58:02 +03:00
|
|
|
if(!building->resources.canBeAfforded(getPlayer(t->tempOwner)->resources))
|
2012-09-23 17:32:49 +03:00
|
|
|
return EBuildingState::NO_RESOURCES; //lack of res
|
|
|
|
|
|
|
|
return EBuildingState::ALLOWED;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const CMapHeader * CGameInfoCallback::getMapHeader() const
|
|
|
|
{
|
|
|
|
return gs->map;
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
bool CGameInfoCallback::hasAccess(boost::optional<PlayerColor> playerId) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
return !player || gs->getPlayerRelations( *playerId, *player ) != PlayerRelations::ENEMIES;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
2013-12-28 21:57:08 +03:00
|
|
|
EPlayerStatus::EStatus CGameInfoCallback::getPlayerStatus(PlayerColor player, bool verbose) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-12-28 21:57:08 +03:00
|
|
|
const PlayerState *ps = gs->getPlayer(player, verbose);
|
|
|
|
ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!ps, verbose, "No such player!", EPlayerStatus::WRONG);
|
2013-02-04 00:05:44 +03:00
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
return ps->status;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CGameInfoCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
|
|
|
|
{
|
|
|
|
return "GOSSIP TEST";
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerRelations::PlayerRelations CGameInfoCallback::getPlayerRelations( PlayerColor color1, PlayerColor color2 ) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
return gs->getPlayerRelations(color1, color2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameInfoCallback::canGetFullInfo(const CGObjectInstance *obj) const
|
|
|
|
{
|
|
|
|
return !obj || hasAccess(obj->tempOwner);
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
int CGameInfoCallback::getHeroCount( PlayerColor player, bool includeGarrisoned ) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
const PlayerState *p = gs->getPlayer(player);
|
|
|
|
ERROR_RET_VAL_IF(!p, "No such player!", -1);
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
if(includeGarrisoned)
|
|
|
|
return p->heroes.size();
|
|
|
|
else
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & elem : p->heroes)
|
|
|
|
if(!elem->inTownGarrison)
|
2011-05-03 06:14:18 +03:00
|
|
|
ret++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameInfoCallback::isOwnedOrVisited(const CGObjectInstance *obj) const
|
|
|
|
{
|
|
|
|
if(canGetFullInfo(obj))
|
|
|
|
return true;
|
|
|
|
|
2011-05-10 01:20:47 +03:00
|
|
|
const TerrainTile *t = getTile(obj->visitablePos()); //get entrance tile
|
2011-05-03 06:14:18 +03:00
|
|
|
const CGObjectInstance *visitor = t->visitableObjects.back(); //visitong hero if present or the obejct itself at last
|
2012-09-23 21:01:04 +03:00
|
|
|
return visitor->ID == Obj::HERO && canGetFullInfo(visitor); //owned or allied hero is a visitor
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerColor CGameInfoCallback::getCurrentPlayer() const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
return gs->currentPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGameInfoCallback::CGameInfoCallback()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
CGameInfoCallback::CGameInfoCallback(CGameState *GS, boost::optional<PlayerColor> Player)
|
2011-09-19 23:50:25 +03:00
|
|
|
{
|
|
|
|
gs = GS;
|
|
|
|
player = Player;
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
const std::vector< std::vector< std::vector<ui8> > > & CPlayerSpecificInfoCallback::getVisibilityMap() const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2013-02-11 17:42:09 +03:00
|
|
|
return gs->getPlayerTeam(*player)->fogOfWarMap;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CPlayerSpecificInfoCallback::howManyTowns() const
|
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2013-02-11 17:42:09 +03:00
|
|
|
ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
|
|
|
|
return CGameInfoCallback::howManyTowns(*player);
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector < const CGTownInstance *> CPlayerSpecificInfoCallback::getTownsInfo(bool onlyOur) const
|
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const auto & i : gs->players)
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const auto & town : i.second.towns)
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
if (i.first==player || (isVisible(town, player) && !onlyOur))
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
ret.push_back(town);
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
std::vector < const CGHeroInstance *> CPlayerSpecificInfoCallback::getHeroesInfo(bool onlyOur) const
|
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
std::vector < const CGHeroInstance *> ret;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto hero : gs->map->heroesOnMap)
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
if( !player || (hero->tempOwner == *player) ||
|
|
|
|
(isVisible(hero->getPosition(false), player) && !onlyOur) )
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
ret.push_back(hero);
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
boost::optional<PlayerColor> CPlayerSpecificInfoCallback::getMyColor() const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
2012-05-13 18:04:21 +03:00
|
|
|
int CPlayerSpecificInfoCallback::getHeroSerial(const CGHeroInstance * hero, bool includeGarrisoned) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2012-05-13 18:04:21 +03:00
|
|
|
if (hero->inTownGarrison && !includeGarrisoned)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
size_t index = 0;
|
2013-02-11 17:42:09 +03:00
|
|
|
auto & heroes = gs->players[*player].heroes;
|
2012-05-13 18:04:21 +03:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & heroe : heroes)
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
if (includeGarrisoned || !(heroe)->inTownGarrison)
|
2012-05-13 18:04:21 +03:00
|
|
|
index++;
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
if (heroe == hero)
|
2012-05-13 18:04:21 +03:00
|
|
|
return index;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
int3 CPlayerSpecificInfoCallback::getGrailPos( double &outKnownRatio )
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
if (!player || CGObelisk::obeliskCount == 0)
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
outKnownRatio = 0.0;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
outKnownRatio = static_cast<double>(CGObelisk::visited[gs->getPlayerTeam(*player)->id]) / CGObelisk::obeliskCount;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
return gs->map->grailPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector < const CGObjectInstance * > CPlayerSpecificInfoCallback::getMyObjects() const
|
|
|
|
{
|
|
|
|
std::vector < const CGObjectInstance * > ret;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const CGObjectInstance * obj : gs->map->objects)
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
if(obj && obj->tempOwner == player)
|
|
|
|
ret.push_back(obj);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector < const CGDwelling * > CPlayerSpecificInfoCallback::getMyDwellings() const
|
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
ASSERT_IF_CALLED_WITH_PLAYER
|
2011-05-03 06:14:18 +03:00
|
|
|
std::vector < const CGDwelling * > ret;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(CGDwelling * dw : gs->getPlayer(*player)->dwellings)
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
ret.push_back(dw);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-06 23:19:54 +03:00
|
|
|
std::vector <QuestInfo> CPlayerSpecificInfoCallback::getMyQuests() const
|
2012-07-06 22:12:04 +03:00
|
|
|
{
|
2012-07-06 23:19:54 +03:00
|
|
|
std::vector <QuestInfo> ret;
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto quest : gs->getPlayer(*player)->quests)
|
2012-07-06 22:12:04 +03:00
|
|
|
{
|
|
|
|
ret.push_back (quest);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
int CPlayerSpecificInfoCallback::howManyHeroes(bool includeGarrisoned) const
|
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2013-02-11 17:42:09 +03:00
|
|
|
ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
|
|
|
|
return getHeroCount(*player,includeGarrisoned);
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
2011-07-18 18:21:16 +03:00
|
|
|
const CGHeroInstance* CPlayerSpecificInfoCallback::getHeroBySerial(int serialId, bool includeGarrisoned) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
ASSERT_IF_CALLED_WITH_PLAYER
|
|
|
|
const PlayerState *p = getPlayer(*player);
|
2013-06-26 14:18:27 +03:00
|
|
|
ERROR_RET_VAL_IF(!p, "No player info", nullptr);
|
2011-07-18 18:21:16 +03:00
|
|
|
|
|
|
|
if (!includeGarrisoned)
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
for(ui32 i = 0; i < p->heroes.size() && i<=serialId; i++)
|
2011-07-18 18:21:16 +03:00
|
|
|
if(p->heroes[i]->inTownGarrison)
|
|
|
|
serialId++;
|
|
|
|
}
|
2013-06-26 14:18:27 +03:00
|
|
|
ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->heroes.size(), "No player info", nullptr);
|
2011-05-03 06:14:18 +03:00
|
|
|
return p->heroes[serialId];
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGTownInstance* CPlayerSpecificInfoCallback::getTownBySerial(int serialId) const
|
|
|
|
{
|
2013-02-11 17:42:09 +03:00
|
|
|
ASSERT_IF_CALLED_WITH_PLAYER
|
|
|
|
const PlayerState *p = getPlayer(*player);
|
2013-06-26 14:18:27 +03:00
|
|
|
ERROR_RET_VAL_IF(!p, "No player info", nullptr);
|
|
|
|
ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->towns.size(), "No player info", nullptr);
|
2011-05-03 06:14:18 +03:00
|
|
|
return p->towns[serialId];
|
|
|
|
}
|
|
|
|
|
2013-02-09 15:56:35 +03:00
|
|
|
int CPlayerSpecificInfoCallback::getResourceAmount(Res::ERes type) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2013-02-11 17:42:09 +03:00
|
|
|
ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
|
|
|
|
return getResource(*player, type);
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
TResources CPlayerSpecificInfoCallback::getResourceAmount() const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
|
|
|
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2013-02-11 17:42:09 +03:00
|
|
|
ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", TResources());
|
|
|
|
return gs->players[*player].resources;
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
2011-05-10 01:20:47 +03:00
|
|
|
|
2013-02-14 02:55:42 +03:00
|
|
|
CGHeroInstance *CNonConstInfoCallback::getHero(ObjectInstanceID objid)
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
|
|
|
return const_cast<CGHeroInstance*>(CGameInfoCallback::getHero(objid));
|
|
|
|
}
|
|
|
|
|
2013-02-14 02:55:42 +03:00
|
|
|
CGTownInstance *CNonConstInfoCallback::getTown(ObjectInstanceID objid)
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
|
|
|
return const_cast<CGTownInstance*>(CGameInfoCallback::getTown(objid));
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
TeamState *CNonConstInfoCallback::getTeam(TeamID teamID)
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
|
|
|
return const_cast<TeamState*>(CGameInfoCallback::getTeam(teamID));
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
TeamState *CNonConstInfoCallback::getPlayerTeam(PlayerColor color)
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
|
|
|
return const_cast<TeamState*>(CGameInfoCallback::getPlayerTeam(color));
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerState * CNonConstInfoCallback::getPlayer( PlayerColor color, bool verbose )
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
|
|
|
return const_cast<PlayerState*>(CGameInfoCallback::getPlayer(color, verbose));
|
|
|
|
}
|
|
|
|
|
2013-02-14 02:55:42 +03:00
|
|
|
CArtifactInstance * CNonConstInfoCallback::getArtInstance( ArtifactInstanceID aid )
|
|
|
|
{
|
|
|
|
return gs->map->artInstances[aid.num];
|
|
|
|
}
|
|
|
|
|
|
|
|
CGObjectInstance * CNonConstInfoCallback::getObjInstance( ObjectInstanceID oid )
|
|
|
|
{
|
|
|
|
return gs->map->objects[oid.num];
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
const TeamState * CGameInfoCallback::getTeam( TeamID teamID ) const
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
ERROR_RET_VAL_IF(!vstd::contains(gs->teams, teamID), "Cannot find info for team " << teamID, nullptr);
|
2011-05-10 01:20:47 +03:00
|
|
|
const TeamState *ret = &gs->teams[teamID];
|
2013-06-26 14:18:27 +03:00
|
|
|
ERROR_RET_VAL_IF(!!player && !vstd::contains(ret->players, *player), "Illegal attempt to access team data!", nullptr);
|
2011-05-10 01:20:47 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
const TeamState * CGameInfoCallback::getPlayerTeam( PlayerColor color ) const
|
2011-05-10 01:20:47 +03:00
|
|
|
{
|
2013-02-11 02:24:57 +03:00
|
|
|
const PlayerState * ps = getPlayer(color);
|
2011-05-10 01:20:47 +03:00
|
|
|
if (ps)
|
|
|
|
return getTeam(ps->team);
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-05-10 01:20:47 +03:00
|
|
|
}
|
|
|
|
|
2011-05-22 21:46:52 +03:00
|
|
|
const CGHeroInstance* CGameInfoCallback::getHeroWithSubid( int subid ) const
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const CGHeroInstance *h : gs->map->heroesOnMap)
|
2011-05-22 21:46:52 +03:00
|
|
|
if(h->subID == subid)
|
|
|
|
return h;
|
2011-05-25 02:17:57 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-05-22 21:46:52 +03:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerColor CGameInfoCallback::getLocalPlayer() const
|
2011-06-11 02:50:32 +03:00
|
|
|
{
|
|
|
|
return getCurrentPlayer();
|
|
|
|
}
|
2011-05-22 21:46:52 +03:00
|
|
|
|
2012-01-03 04:55:26 +03:00
|
|
|
bool CGameInfoCallback::isInTheMap(const int3 &pos) const
|
|
|
|
{
|
|
|
|
return gs->map->isInTheMap(pos);
|
|
|
|
}
|
|
|
|
|
2013-02-14 02:55:42 +03:00
|
|
|
const CArtifactInstance * CGameInfoCallback::getArtInstance( ArtifactInstanceID aid ) const
|
|
|
|
{
|
|
|
|
return gs->map->artInstances[aid.num];
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGObjectInstance * CGameInfoCallback::getObjInstance( ObjectInstanceID oid ) const
|
|
|
|
{
|
|
|
|
return gs->map->objects[oid.num];
|
|
|
|
}
|
|
|
|
|
2011-05-22 21:46:52 +03:00
|
|
|
void IGameEventRealizer::showInfoDialog( InfoWindow *iw )
|
|
|
|
{
|
|
|
|
commitPackage(iw);
|
|
|
|
}
|
2011-05-28 04:02:28 +03:00
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
void IGameEventRealizer::showInfoDialog(const std::string &msg, PlayerColor player)
|
2011-06-11 02:50:32 +03:00
|
|
|
{
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = player;
|
|
|
|
iw.text << msg;
|
|
|
|
showInfoDialog(&iw);
|
|
|
|
}
|
|
|
|
|
2013-02-14 02:55:42 +03:00
|
|
|
void IGameEventRealizer::setObjProperty(ObjectInstanceID objid, int prop, si64 val)
|
2011-05-28 04:02:28 +03:00
|
|
|
{
|
|
|
|
SetObjectProperty sob;
|
|
|
|
sob.id = objid;
|
|
|
|
sob.what = prop;
|
2011-07-13 21:39:02 +03:00
|
|
|
sob.val = static_cast<ui32>(val);
|
2011-05-28 04:02:28 +03:00
|
|
|
commitPackage(&sob);
|
|
|
|
}
|
|
|
|
|
2013-02-11 02:24:57 +03:00
|
|
|
const CGObjectInstance * IGameCallback::putNewObject(Obj ID, int subID, int3 pos)
|
2011-05-28 04:02:28 +03:00
|
|
|
{
|
|
|
|
NewObject no;
|
|
|
|
no.ID = ID; //creature
|
|
|
|
no.subID= subID;
|
|
|
|
no.pos = pos;
|
|
|
|
commitPackage(&no);
|
2011-07-05 09:14:07 +03:00
|
|
|
return getObj(no.id); //id field will be filled during applying on gs
|
2011-05-28 04:02:28 +03:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
const CGCreature * IGameCallback::putNewMonster(CreatureID creID, int count, int3 pos)
|
2011-05-28 04:02:28 +03:00
|
|
|
{
|
2013-02-07 20:34:50 +03:00
|
|
|
const CGObjectInstance *m = putNewObject(Obj::MONSTER, creID, pos);
|
2011-05-28 04:02:28 +03:00
|
|
|
setObjProperty(m->id, ObjProperty::MONSTER_COUNT, count);
|
|
|
|
setObjProperty(m->id, ObjProperty::MONSTER_POWER, (si64)1000*count);
|
|
|
|
return dynamic_cast<const CGCreature*>(m);
|
|
|
|
}
|
2013-05-28 00:46:04 +03:00
|
|
|
|
|
|
|
bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
|
|
|
|
{
|
|
|
|
//only server knows
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|