1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/lib/IGameCallback.cpp

296 lines
8.7 KiB
C++
Raw Normal View History

/*
* 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
*
*/
#include "StdInc.h"
#include "IGameCallback.h"
#include "CHeroHandler.h" // for CHeroHandler
#include "spells/CSpellHandler.h"// for CSpell
#include "CSkillHandler.h"// for CSkill
2011-05-03 06:14:18 +03:00
#include "NetPacks.h"
#include "CBonusTypeHandler.h"
#include "BattleFieldHandler.h"
#include "ObstacleHandler.h"
#include "bonuses/CBonusSystemNode.h"
2023-04-30 15:52:48 +02:00
#include "bonuses/Limiters.h"
#include "bonuses/Propagators.h"
2023-04-30 15:52:48 +02:00
#include "bonuses/Updaters.h"
#include "serializer/CSerializer.h" // for SAVEGAME_MAGIC
#include "serializer/BinaryDeserializer.h"
#include "serializer/BinarySerializer.h"
#include "serializer/CLoadIntegrityValidator.h"
#include "rmg/CMapGenOptions.h"
#include "mapObjectConstructors/AObjectTypeHandler.h"
#include "mapObjectConstructors/CObjectClassesHandler.h"
#include "mapObjects/CObjectHandler.h"
#include "mapObjects/ObjectTemplate.h"
2023-06-25 21:28:24 +02:00
#include "campaign/CampaignState.h"
#include "StartInfo.h"
#include "gameState/CGameState.h"
#include "gameState/CGameStateCampaign.h"
#include "gameState/TavernHeroesPool.h"
#include "mapping/CMap.h"
#include "modding/CModHandler.h"
#include "modding/CModInfo.h"
#include "modding/IdentifierStorage.h"
#include "modding/CModVersion.h"
#include "CPlayerState.h"
#include "GameSettings.h"
#include "ScriptHandler.h"
#include "RoadHandler.h"
#include "RiverHandler.h"
#include "TerrainHandler.h"
#include "serializer/Connection.h"
VCMI_LIB_NAMESPACE_BEGIN
void CPrivilegedInfoCallback::getFreeTiles(std::vector<int3> & tiles) const
2011-05-03 06:14:18 +03:00
{
std::vector<int> floors;
2023-03-13 23:26:44 +02:00
floors.reserve(gs->map->levels());
for(int b = 0; b < gs->map->levels(); ++b)
{
floors.push_back(b);
}
2023-03-13 23:26:44 +02:00
const TerrainTile * tinfo = nullptr;
for (auto zd : floors)
{
for (int xd = 0; xd < gs->map->width; xd++)
{
for (int yd = 0; yd < gs->map->height; yd++)
{
tinfo = getTile(int3 (xd,yd,zd));
2022-09-21 11:34:23 +02:00
if (tinfo->terType->isLand() && tinfo->terType->isPassable() && !tinfo->blocked) //land and free
2023-03-13 23:26:44 +02:00
tiles.emplace_back(xd, yd, zd);
}
}
}
2011-05-03 06:14:18 +03:00
}
2023-04-16 00:48:49 +02:00
void CPrivilegedInfoCallback::getTilesInRange(std::unordered_set<int3> & tiles,
2023-03-13 23:26:44 +02:00
const int3 & pos,
int radious,
2023-04-16 19:42:56 +02:00
std::optional<PlayerColor> player,
2023-03-13 23:26:44 +02:00
int mode,
int3::EDistanceFormula distanceFormula) const
{
if(!!player && !player->isValidPlayer())
{
2017-08-10 18:39:27 +02:00
logGlobal->error("Illegal call to getTilesInRange!");
return;
}
if(radious == CBuilding::HEIGHT_SKYSHIP) //reveal entire map
getAllTiles (tiles, player, -1, MapTerrainFilterMode::NONE);
else
{
const TeamState * team = !player ? nullptr : gs->getPlayerTeam(*player);
for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
{
for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
{
int3 tilePos(xd,yd,pos.z);
double distance = pos.dist(tilePos, distanceFormula);
if(distance <= radious)
{
2013-03-03 20:06:03 +03:00
if(!player
|| (mode == 1 && (*team->fogOfWarMap)[pos.z][xd][yd] == 0)
|| (mode == -1 && (*team->fogOfWarMap)[pos.z][xd][yd] == 1)
)
tiles.insert(int3(xd,yd,pos.z));
}
}
}
}
}
2023-04-16 00:48:49 +02:00
void CPrivilegedInfoCallback::getAllTiles(std::unordered_set<int3> & tiles, std::optional<PlayerColor> Player, int level, MapTerrainFilterMode tileFilterMode) const
{
if(!!Player && !Player->isValidPlayer())
{
2017-08-10 18:39:27 +02:00
logGlobal->error("Illegal call to getAllTiles !");
return;
}
std::vector<int> floors;
if(level == -1)
{
for(int b = 0; b < gs->map->levels(); ++b)
{
floors.push_back(b);
}
}
else
floors.push_back(level);
2022-12-30 16:51:13 +02:00
for(auto zd: floors)
{
2022-12-30 16:51:13 +02:00
for(int xd = 0; xd < gs->map->width; xd++)
{
2022-12-30 16:51:13 +02:00
for(int yd = 0; yd < gs->map->height; yd++)
{
2022-12-30 16:51:13 +02:00
bool isTileEligible = false;
switch(tileFilterMode)
{
case MapTerrainFilterMode::NONE:
isTileEligible = true;
break;
case MapTerrainFilterMode::WATER:
isTileEligible = getTile(int3(xd, yd, zd))->terType->isWater();
break;
case MapTerrainFilterMode::LAND:
isTileEligible = getTile(int3(xd, yd, zd))->terType->isLand();
break;
case MapTerrainFilterMode::LAND_CARTOGRAPHER:
isTileEligible = getTile(int3(xd, yd, zd))->terType->isSurfaceCartographerCompatible();
break;
case MapTerrainFilterMode::UNDERGROUND_CARTOGRAPHER:
isTileEligible = getTile(int3(xd, yd, zd))->terType->isUndergroundCartographerCompatible();
break;
}
if(isTileEligible)
tiles.insert(int3(xd, yd, zd));
}
}
}
}
2023-03-13 23:26:44 +02:00
void CPrivilegedInfoCallback::pickAllowedArtsSet(std::vector<const CArtifact *> & out, CRandomGenerator & rand) const
{
for (int j = 0; j < 3 ; j++)
out.push_back(VLC->arth->objects[VLC->arth->pickRandomArtifact(rand, CArtifact::ART_TREASURE)]);
for (int j = 0; j < 3 ; j++)
out.push_back(VLC->arth->objects[VLC->arth->pickRandomArtifact(rand, CArtifact::ART_MINOR)]);
out.push_back(VLC->arth->objects[VLC->arth->pickRandomArtifact(rand, CArtifact::ART_MAJOR)]);
2009-05-21 03:55:30 +03:00
}
void CPrivilegedInfoCallback::getAllowedSpells(std::vector<SpellID> & out, std::optional<ui16> level)
{
for (ui32 i = 0; i < gs->map->allowedSpells.size(); i++) //spellh size appears to be greater (?)
{
const spells::Spell * spell = SpellID(i).toSpell();
if (!isAllowed(0, spell->getIndex()))
continue;
if (level.has_value() && spell->getLevel() != level)
continue;
out.push_back(spell->getId());
}
}
CGameState * CPrivilegedInfoCallback::gameState()
{
return gs;
}
template<typename Loader>
void CPrivilegedInfoCallback::loadCommonState(Loader & in)
{
2017-08-10 18:39:27 +02:00
logGlobal->info("Loading lib part of game...");
in.checkMagicBytes(SAVEGAME_MAGIC);
CMapHeader dum;
2023-03-13 23:26:44 +02:00
StartInfo * si = nullptr;
2017-08-10 18:39:27 +02:00
logGlobal->info("\tReading header");
in.serializer & dum;
2017-08-10 18:39:27 +02:00
logGlobal->info("\tReading options");
in.serializer & si;
2017-08-10 18:39:27 +02:00
logGlobal->info("\tReading handlers");
in.serializer & *VLC;
2017-08-10 18:39:27 +02:00
logGlobal->info("\tReading gamestate");
in.serializer & gs;
}
template<typename Saver>
void CPrivilegedInfoCallback::saveCommonState(Saver & out) const
{
2017-08-10 18:39:27 +02:00
logGlobal->info("Saving lib part of game...");
out.putMagicBytes(SAVEGAME_MAGIC);
2017-08-10 18:39:27 +02:00
logGlobal->info("\tSaving header");
out.serializer & static_cast<CMapHeader&>(*gs->map);
2017-08-10 18:39:27 +02:00
logGlobal->info("\tSaving options");
out.serializer & gs->scenarioOps;
2017-08-10 18:39:27 +02:00
logGlobal->info("\tSaving handlers");
out.serializer & *VLC;
2017-08-10 18:39:27 +02:00
logGlobal->info("\tSaving gamestate");
out.serializer & gs;
}
2014-04-20 13:10:46 +03:00
// hardly memory usage for `-gdwarf-4` flag
template DLL_LINKAGE void CPrivilegedInfoCallback::loadCommonState<CLoadIntegrityValidator>(CLoadIntegrityValidator &);
template DLL_LINKAGE void CPrivilegedInfoCallback::loadCommonState<CLoadFile>(CLoadFile &);
template DLL_LINKAGE void CPrivilegedInfoCallback::saveCommonState<CSaveFile>(CSaveFile &) const;
2023-03-13 23:26:44 +02:00
TerrainTile * CNonConstInfoCallback::getTile(const int3 & pos)
{
if(!gs->map->isInTheMap(pos))
return nullptr;
return &gs->map->getTile(pos);
}
2023-03-13 23:26:44 +02:00
CGHeroInstance * CNonConstInfoCallback::getHero(const ObjectInstanceID & objid)
{
return const_cast<CGHeroInstance*>(CGameInfoCallback::getHero(objid));
}
2023-03-13 23:26:44 +02:00
CGTownInstance * CNonConstInfoCallback::getTown(const ObjectInstanceID & objid)
{
return const_cast<CGTownInstance*>(CGameInfoCallback::getTown(objid));
}
2023-03-13 23:26:44 +02:00
TeamState * CNonConstInfoCallback::getTeam(const TeamID & teamID)
{
return const_cast<TeamState*>(CGameInfoCallback::getTeam(teamID));
}
2023-03-13 23:26:44 +02:00
TeamState * CNonConstInfoCallback::getPlayerTeam(const PlayerColor & color)
{
return const_cast<TeamState*>(CGameInfoCallback::getPlayerTeam(color));
}
2023-03-13 23:26:44 +02:00
PlayerState * CNonConstInfoCallback::getPlayerState(const PlayerColor & color, bool verbose)
{
return const_cast<PlayerState*>(CGameInfoCallback::getPlayerState(color, verbose));
}
2023-03-13 23:26:44 +02:00
CArtifactInstance * CNonConstInfoCallback::getArtInstance(const ArtifactInstanceID & aid)
{
2018-03-10 23:19:36 +02:00
return gs->map->artInstances.at(aid.num);
}
2023-03-13 23:26:44 +02:00
CGObjectInstance * CNonConstInfoCallback::getObjInstance(const ObjectInstanceID & oid)
{
2018-03-10 23:19:36 +02:00
return gs->map->objects.at(oid.num);
}
2023-03-13 23:26:44 +02:00
CArmedInstance * CNonConstInfoCallback::getArmyInstance(const ObjectInstanceID & oid)
2018-03-10 23:19:36 +02:00
{
return dynamic_cast<CArmedInstance *>(getObjInstance(oid));
}
2013-05-28 00:46:04 +03:00
bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
{
//only server knows
logGlobal->error("isVisitCoveredByAnotherQuery call on client side");
2013-05-28 00:46:04 +03:00
return false;
}
VCMI_LIB_NAMESPACE_END