2014-03-17 22:51:07 +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-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
|
|
|
|
2014-04-20 10:13:37 +03:00
|
|
|
#include "CHeroHandler.h" // for CHeroHandler
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "spells/CSpellHandler.h"// for CSpell
|
2011-05-03 06:14:18 +03:00
|
|
|
#include "NetPacks.h"
|
2013-03-06 21:49:56 +03:00
|
|
|
#include "CBonusTypeHandler.h"
|
2014-06-25 17:11:07 +03:00
|
|
|
#include "CModHandler.h"
|
2013-02-19 01:37:22 +03:00
|
|
|
|
2014-04-20 10:13:37 +03:00
|
|
|
#include "Connection.h" // for SAVEGAME_MAGIC
|
2014-06-05 19:52:14 +03:00
|
|
|
#include "mapObjects/CObjectClassesHandler.h"
|
2014-06-25 17:11:07 +03:00
|
|
|
#include "StartInfo.h"
|
|
|
|
#include "CGameState.h"
|
2015-12-02 21:05:10 +02:00
|
|
|
#include "mapping/CMap.h"
|
2015-12-02 21:39:53 +02:00
|
|
|
#include "CPlayerState.h"
|
2008-12-27 03:01:59 +02:00
|
|
|
|
2014-04-20 10:13:37 +03:00
|
|
|
void CPrivilagedInfoCallback::getFreeTiles (std::vector<int3> &tiles) const
|
2011-05-03 06:14:18 +03:00
|
|
|
{
|
2014-04-20 10:13:37 +03:00
|
|
|
std::vector<int> floors;
|
|
|
|
for (int b = 0; b < (gs->map->twoLevel ? 2 : 1); ++b)
|
|
|
|
{
|
|
|
|
floors.push_back(b);
|
|
|
|
}
|
|
|
|
const TerrainTile *tinfo;
|
|
|
|
for (auto zd : floors)
|
|
|
|
{
|
2011-05-03 06:14:18 +03:00
|
|
|
|
2014-04-20 10:13:37 +03:00
|
|
|
for (int xd = 0; xd < gs->map->width; xd++)
|
|
|
|
{
|
|
|
|
for (int yd = 0; yd < gs->map->height; yd++)
|
|
|
|
{
|
|
|
|
tinfo = getTile(int3 (xd,yd,zd));
|
2015-11-25 08:12:09 +02:00
|
|
|
if (tinfo->terType != ETerrainType::WATER && tinfo->terType != ETerrainType::ROCK && !tinfo->blocked) //land and free
|
2014-04-20 10:13:37 +03:00
|
|
|
tiles.push_back (int3 (xd,yd,zd));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-03 06:14:18 +03:00
|
|
|
}
|
|
|
|
|
2015-12-04 20:08:09 +02:00
|
|
|
void CPrivilagedInfoCallback::getTilesInRange( std::unordered_set<int3, ShashInt3> &tiles, int3 pos, int radious, boost::optional<PlayerColor> player/*=uninit*/, int mode/*=0*/, bool patrolDistance/*=false*/) 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
|
|
|
{
|
2016-03-12 03:41:27 +02: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
|
|
|
{
|
2015-12-04 20:08:09 +02:00
|
|
|
int3 tilePos(xd,yd,pos.z);
|
|
|
|
double distance;
|
|
|
|
if(patrolDistance)
|
|
|
|
distance = pos.mandist2d(tilePos);
|
|
|
|
else
|
|
|
|
distance = pos.dist2d(tilePos) - 0.5;
|
|
|
|
|
2009-11-11 19:45:03 +02:00
|
|
|
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
|
|
|
{
|
2016-03-12 03:41:27 +02: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
|
|
|
{
|
2014-04-20 10:13:37 +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::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++)
|
2014-03-17 22:51:07 +03:00
|
|
|
out.push_back(VLC->arth->artifacts[VLC->arth->pickRandomArtifact(gameState()->getRandomGenerator(), CArtifact::ART_TREASURE)]);
|
2012-09-06 13:39:48 +03:00
|
|
|
for (int j = 0; j < 3 ; j++)
|
2014-03-17 22:51:07 +03:00
|
|
|
out.push_back(VLC->arth->artifacts[VLC->arth->pickRandomArtifact(gameState()->getRandomGenerator(), CArtifact::ART_MINOR)]);
|
2012-09-06 13:39:48 +03:00
|
|
|
|
2014-03-17 22:51:07 +03:00
|
|
|
out.push_back(VLC->arth->artifacts[VLC->arth->pickRandomArtifact(gameState()->getRandomGenerator(), CArtifact::ART_MAJOR)]);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 10:13:37 +03:00
|
|
|
CGameState * CPrivilagedInfoCallback::gameState ()
|
|
|
|
{
|
|
|
|
return gs;
|
|
|
|
}
|
2013-02-19 01:37:22 +03:00
|
|
|
|
|
|
|
template<typename Loader>
|
|
|
|
void CPrivilagedInfoCallback::loadCommonState(Loader &in)
|
|
|
|
{
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->infoStream() << "Loading lib part of game...";
|
2013-02-19 01:37:22 +03:00
|
|
|
in.checkMagicBytes(SAVEGAME_MAGIC);
|
|
|
|
|
|
|
|
CMapHeader dum;
|
|
|
|
StartInfo *si;
|
|
|
|
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->infoStream() <<"\tReading header";
|
2014-12-21 16:27:50 +02:00
|
|
|
in.serializer >> dum;
|
2013-02-19 01:37:22 +03:00
|
|
|
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->infoStream() << "\tReading options";
|
2014-12-21 16:27:50 +02:00
|
|
|
in.serializer >> si;
|
2013-02-19 01:37:22 +03:00
|
|
|
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->infoStream() <<"\tReading handlers";
|
2014-12-21 16:27:50 +02:00
|
|
|
in.serializer >> *VLC;
|
2013-02-19 01:37:22 +03:00
|
|
|
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->infoStream() <<"\tReading gamestate";
|
2014-12-21 16:27:50 +02:00
|
|
|
in.serializer >> gs;
|
2013-02-19 01:37:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Saver>
|
|
|
|
void CPrivilagedInfoCallback::saveCommonState(Saver &out) const
|
|
|
|
{
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->infoStream() << "Saving lib part of game...";
|
2013-02-19 01:37:22 +03:00
|
|
|
out.putMagicBytes(SAVEGAME_MAGIC);
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->infoStream() <<"\tSaving header";
|
2014-12-21 16:27:50 +02:00
|
|
|
out.serializer << static_cast<CMapHeader&>(*gs->map);
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->infoStream() << "\tSaving options";
|
2014-12-21 16:27:50 +02:00
|
|
|
out.serializer << gs->scenarioOps;
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->infoStream() << "\tSaving handlers";
|
2014-12-21 16:27:50 +02:00
|
|
|
out.serializer << *VLC;
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->infoStream() << "\tSaving gamestate";
|
2014-12-21 16:27:50 +02:00
|
|
|
out.serializer << gs;
|
2013-02-19 01:37:22 +03:00
|
|
|
}
|
|
|
|
|
2014-04-20 13:10:46 +03:00
|
|
|
// hardly memory usage for `-gdwarf-4` flag
|
2013-02-19 01:37:22 +03:00
|
|
|
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-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-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;
|
|
|
|
}
|