2008-12-27 03:01:59 +02:00
|
|
|
#define VCMI_DLL
|
|
|
|
#include "IGameCallback.h"
|
2009-05-21 03:55:30 +03:00
|
|
|
#include "../lib/CGameState.h"
|
|
|
|
#include "../lib/map.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CObjectHandler.h"
|
2009-02-08 08:42:15 +02:00
|
|
|
#include "../StartInfo.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CArtHandler.h"
|
|
|
|
#include "CSpellHandler.h"
|
2009-04-16 03:28:54 +03:00
|
|
|
#include "../lib/VCMI_Lib.h"
|
2009-10-25 12:41:58 +02:00
|
|
|
#include <boost/random/linear_congruential.hpp>
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CTownHandler.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-10-24 22:21:32 +03:00
|
|
|
extern boost::rand48 ran;
|
|
|
|
|
2009-08-19 09:56:53 +03:00
|
|
|
CGameState *const IGameCallback::gameState ()
|
|
|
|
{
|
|
|
|
return gs;
|
|
|
|
}
|
2010-07-27 00:14:08 +03:00
|
|
|
const CGObjectInstance* IGameCallback::getObj(int objid, bool verbose)
|
2008-12-27 03:01:59 +02:00
|
|
|
{
|
2009-03-09 12:37:49 +02:00
|
|
|
if(objid < 0 || objid >= gs->map->objects.size())
|
|
|
|
{
|
2010-07-27 00:14:08 +03:00
|
|
|
if(verbose)
|
|
|
|
tlog1 << "Cannot get object with id " << objid << std::endl;
|
2009-03-09 12:37:49 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (!gs->map->objects[objid])
|
|
|
|
{
|
2010-07-27 00:14:08 +03:00
|
|
|
if(verbose)
|
|
|
|
tlog1 << "Cannot get object with id " << objid << ". Object was removed.\n";
|
2009-03-09 12:37:49 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-27 03:01:59 +02:00
|
|
|
return gs->map->objects[objid];
|
|
|
|
}
|
|
|
|
const CGHeroInstance* IGameCallback::getHero(int objid)
|
|
|
|
{
|
2010-07-27 00:14:08 +03:00
|
|
|
const CGObjectInstance *obj = getObj(objid, false);
|
2009-03-09 12:37:49 +02:00
|
|
|
if(obj)
|
|
|
|
return dynamic_cast<const CGHeroInstance*>(obj);
|
|
|
|
else
|
|
|
|
return NULL;
|
2008-12-27 03:01:59 +02:00
|
|
|
}
|
|
|
|
const CGTownInstance* IGameCallback::getTown(int objid)
|
|
|
|
{
|
2010-07-27 00:14:08 +03:00
|
|
|
const CGObjectInstance *obj = getObj(objid, false);
|
2009-03-09 12:37:49 +02:00
|
|
|
if(obj)
|
2010-12-19 00:11:28 +02:00
|
|
|
return dynamic_cast<const CGTownInstance*>(gs->map->objects[objid].get());
|
2009-03-09 12:37:49 +02:00
|
|
|
else
|
|
|
|
return NULL;
|
2008-12-27 03:01:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int IGameCallback::getOwner(int heroID)
|
|
|
|
{
|
|
|
|
return gs->map->objects[heroID]->tempOwner;
|
|
|
|
}
|
|
|
|
int IGameCallback::getResource(int player, int which)
|
|
|
|
{
|
|
|
|
return gs->players.find(player)->second.resources[which];
|
|
|
|
}
|
|
|
|
int IGameCallback::getDate(int mode)
|
|
|
|
{
|
|
|
|
return gs->getDate(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance* IGameCallback::getSelectedHero( int player )
|
|
|
|
{
|
2009-08-07 11:41:40 +03:00
|
|
|
if(gs->players.find(player)->second.currentSelection==-1)
|
|
|
|
return NULL;
|
2008-12-27 03:01:59 +02:00
|
|
|
return getHero(gs->players.find(player)->second.currentSelection);
|
2009-02-08 08:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const PlayerSettings * IGameCallback::getPlayerSettings( int color )
|
|
|
|
{
|
|
|
|
return &gs->scenarioOps->getIthPlayersSettings(color);
|
2009-02-14 21:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int IGameCallback::getHeroCount( int player, bool includeGarrisoned )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
if(includeGarrisoned)
|
2009-03-09 12:37:49 +02:00
|
|
|
return gs->getPlayer(player)->heroes.size();
|
2009-02-14 21:12:40 +02:00
|
|
|
else
|
2009-03-09 12:37:49 +02:00
|
|
|
for(int i=0; i < gs->getPlayer(player)->heroes.size(); i++)
|
|
|
|
if(!gs->getPlayer(player)->heroes[i]->inTownGarrison)
|
2009-02-14 21:12:40 +02:00
|
|
|
ret++;
|
|
|
|
return ret;
|
2009-03-14 13:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IGameCallback::getTilesInRange( std::set<int3> &tiles, int3 pos, int radious, int player/*=-1*/, int mode/*=0*/ )
|
|
|
|
{
|
|
|
|
if(player >= PLAYER_LIMIT)
|
|
|
|
{
|
|
|
|
tlog1 << "Illegal call to getTilesInRange!\n";
|
|
|
|
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
|
|
|
{
|
2010-08-03 15:34:06 +03:00
|
|
|
const TeamState * team = 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)
|
|
|
|
{
|
|
|
|
if(player < 0
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-20 13:30:00 +03:00
|
|
|
void IGameCallback::getAllTiles (std::set<int3> &tiles, int player/*=-1*/, int level, int surface )
|
2009-08-19 09:56:53 +03:00
|
|
|
{
|
|
|
|
if(player >= PLAYER_LIMIT)
|
|
|
|
{
|
2009-11-11 19:45:03 +02:00
|
|
|
tlog1 << "Illegal call to getAllTiles !\n";
|
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)
|
|
|
|
{
|
2009-08-23 18:02:21 +03:00
|
|
|
|
|
|
|
for (int xd = 0; xd <= gs->map->width - 1; xd++)
|
2009-08-20 13:30:00 +03:00
|
|
|
for(int b=0; b<gs->map->twoLevel + 1; ++b) //if gs->map->twoLevel is false then false (0) + 1 is 1, if it's true (1) then we have 2
|
|
|
|
{
|
|
|
|
floors.push_back(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
floors.push_back(level);
|
|
|
|
|
|
|
|
for (std::vector<int>::const_iterator i = floors.begin(); i!= floors.end(); i++)
|
2009-08-19 09:56:53 +03:00
|
|
|
{
|
2009-08-23 18:02:21 +03:00
|
|
|
register int zd = *i;
|
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
|
|
|
{
|
2009-08-23 18:02:21 +03:00
|
|
|
if ((getTile (int3 (xd,yd,zd))->tertype == 8 && water)
|
|
|
|
|| (getTile (int3 (xd,yd,zd))->tertype != 8 && land))
|
|
|
|
tiles.insert(int3(xd,yd,zd));
|
2009-08-19 09:56:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-22 10:11:46 +03:00
|
|
|
void IGameCallback::getFreeTiles (std::vector<int3> &tiles)
|
|
|
|
{
|
|
|
|
std::vector<int> floors;
|
|
|
|
for (int b=0; b<gs->map->twoLevel + 1; ++b) //if gs->map->twoLevel is false then false (0) + 1 is 1, if it's true (1) then we have 2
|
|
|
|
{
|
|
|
|
floors.push_back(b);
|
|
|
|
}
|
|
|
|
TerrainTile *tinfo;
|
|
|
|
for (std::vector<int>::const_iterator i = floors.begin(); i!= floors.end(); i++)
|
|
|
|
{
|
|
|
|
register int zd = *i;
|
|
|
|
for (int xd = 0; xd < gs->map->width; xd++)
|
|
|
|
{
|
|
|
|
for (int yd = 0; yd < gs->map->height; yd++)
|
|
|
|
{
|
|
|
|
tinfo = getTile (int3 (xd,yd,zd));
|
|
|
|
if (tinfo->tertype != 8 && !tinfo->blocked) //land and free
|
|
|
|
tiles.push_back (int3 (xd,yd,zd));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-03-14 13:25:25 +02:00
|
|
|
bool IGameCallback::isAllowed( int type, int id )
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
tlog1 << "Wrong call to IGameCallback::isAllowed!\n";
|
2009-03-14 19:19:53 +02:00
|
|
|
return false;
|
2009-03-14 13:25:25 +02:00
|
|
|
}
|
2009-04-16 03:28:54 +03:00
|
|
|
}
|
|
|
|
|
2010-06-27 19:03:01 +03:00
|
|
|
void IGameCallback::pickAllowedArtsSet(std::vector<const CArtifact*> &out)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < 3 ; j++)
|
|
|
|
{
|
|
|
|
out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_TREASURE << i)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_MAJOR)]);
|
|
|
|
}
|
|
|
|
|
2010-06-28 08:07:21 +03:00
|
|
|
ui16 IGameCallback::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
|
|
|
}
|
|
|
|
|
2010-06-28 08:07:21 +03:00
|
|
|
ui16 IGameCallback::getArtSync (ui32 rand, int flags)
|
2009-10-24 22:21:32 +03:00
|
|
|
{
|
2010-06-28 08:07:21 +03:00
|
|
|
return VLC->arth->getArtSync (rand, flags);
|
2009-05-21 03:55:30 +03:00
|
|
|
}
|
2009-07-26 06:33:13 +03:00
|
|
|
|
2010-06-28 08:07:21 +03:00
|
|
|
void IGameCallback::erasePickedArt (si32 id)
|
|
|
|
{
|
|
|
|
VLC->arth->erasePickedArt(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-24 20:54:02 +03:00
|
|
|
void IGameCallback::getAllowedSpells(std::vector<ui16> &out, ui16 level)
|
|
|
|
{
|
2009-09-24 22:28:26 +03:00
|
|
|
|
|
|
|
CSpell *spell;
|
|
|
|
for (int i = 0; i < gs->map->allowedSpell.size(); i++) //spellh size appears to be greater (?)
|
2009-09-24 20:54:02 +03:00
|
|
|
{
|
2010-12-19 16:39:56 +02:00
|
|
|
spell = VLC->spellh->spells[i];
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-24 13:35:11 +03:00
|
|
|
int3 IGameCallback::getMapSize()
|
2009-08-23 18:02:21 +03:00
|
|
|
{
|
2009-08-24 13:35:11 +03:00
|
|
|
return int3(gs->map->width, gs->map->height, gs->map->twoLevel + 1);
|
2009-08-23 18:02:21 +03:00
|
|
|
}
|
2009-08-24 13:35:11 +03:00
|
|
|
|
2009-08-23 18:02:21 +03:00
|
|
|
inline TerrainTile * IGameCallback::getTile( int3 pos )
|
2009-07-26 06:33:13 +03:00
|
|
|
{
|
|
|
|
if(!gs->map->isInTheMap(pos))
|
|
|
|
return NULL;
|
|
|
|
return &gs->map->getTile(pos);
|
2010-02-10 04:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const PlayerState * IGameCallback::getPlayerState( int color )
|
|
|
|
{
|
|
|
|
return gs->getPlayer(color, false);
|
2010-06-27 19:03:01 +03:00
|
|
|
}
|
2010-07-09 02:03:27 +03:00
|
|
|
|
|
|
|
const CTown * IGameCallback::getNativeTown(int color)
|
|
|
|
{
|
|
|
|
return &VLC->townh->towns[gs->scenarioOps->getIthPlayersSettings(color).castle];
|
|
|
|
}
|