2023-10-19 16:19:09 +02: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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "IGameCallback.h"
|
|
|
|
|
|
|
|
#include "spells/CSpellHandler.h"// for CSpell
|
|
|
|
#include "CSkillHandler.h"// for CSkill
|
|
|
|
#include "CBonusTypeHandler.h"
|
|
|
|
#include "BattleFieldHandler.h"
|
|
|
|
#include "ObstacleHandler.h"
|
|
|
|
#include "bonuses/Limiters.h"
|
|
|
|
#include "bonuses/Propagators.h"
|
|
|
|
#include "bonuses/Updaters.h"
|
2024-07-21 12:49:40 +02:00
|
|
|
#include "entities/building/CBuilding.h"
|
2024-10-11 18:30:16 +02:00
|
|
|
#include "entities/hero/CHero.h"
|
2024-02-01 10:43:16 +02:00
|
|
|
#include "networkPacks/ArtifactLocation.h"
|
2023-11-11 00:39:08 +02:00
|
|
|
#include "serializer/CLoadFile.h"
|
|
|
|
#include "serializer/CSaveFile.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "rmg/CMapGenOptions.h"
|
|
|
|
#include "mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
|
#include "mapObjectConstructors/CObjectClassesHandler.h"
|
2024-02-01 10:43:16 +02:00
|
|
|
#include "mapObjects/CGMarket.h"
|
2024-08-16 16:49:42 +02:00
|
|
|
#include "mapObjects/TownBuildingInstance.h"
|
2024-01-09 16:43:36 +02:00
|
|
|
#include "mapObjects/CGTownInstance.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "mapObjects/CObjectHandler.h"
|
2024-01-09 16:43:36 +02:00
|
|
|
#include "mapObjects/CQuest.h"
|
|
|
|
#include "mapObjects/MiscObjects.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "mapObjects/ObjectTemplate.h"
|
|
|
|
#include "campaign/CampaignState.h"
|
|
|
|
#include "StartInfo.h"
|
|
|
|
#include "gameState/CGameState.h"
|
|
|
|
#include "gameState/CGameStateCampaign.h"
|
|
|
|
#include "gameState/TavernHeroesPool.h"
|
2023-10-23 12:59:15 +02:00
|
|
|
#include "gameState/QuestInfo.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "mapping/CMap.h"
|
|
|
|
#include "modding/CModHandler.h"
|
|
|
|
#include "modding/CModInfo.h"
|
|
|
|
#include "modding/IdentifierStorage.h"
|
|
|
|
#include "modding/CModVersion.h"
|
2023-11-07 18:32:40 +02:00
|
|
|
#include "modding/ActiveModsInSaveList.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "CPlayerState.h"
|
|
|
|
#include "GameSettings.h"
|
|
|
|
#include "ScriptHandler.h"
|
|
|
|
#include "RoadHandler.h"
|
|
|
|
#include "RiverHandler.h"
|
|
|
|
#include "TerrainHandler.h"
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
#include <vstd/RNG.h>
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
void CPrivilegedInfoCallback::getFreeTiles(std::vector<int3> & tiles) const
|
|
|
|
{
|
|
|
|
std::vector<int> floors;
|
|
|
|
floors.reserve(gs->map->levels());
|
|
|
|
for(int b = 0; b < gs->map->levels(); ++b)
|
|
|
|
{
|
|
|
|
floors.push_back(b);
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
if (tinfo->terType->isLand() && tinfo->terType->isPassable() && !tinfo->blocked) //land and free
|
|
|
|
tiles.emplace_back(xd, yd, zd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivilegedInfoCallback::getTilesInRange(std::unordered_set<int3> & tiles,
|
|
|
|
const int3 & pos,
|
|
|
|
int radious,
|
2023-10-22 17:36:41 +02:00
|
|
|
ETileVisibility mode,
|
2023-10-19 16:19:09 +02:00
|
|
|
std::optional<PlayerColor> player,
|
|
|
|
int3::EDistanceFormula distanceFormula) const
|
|
|
|
{
|
|
|
|
if(!!player && !player->isValidPlayer())
|
|
|
|
{
|
|
|
|
logGlobal->error("Illegal call to getTilesInRange!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(radious == CBuilding::HEIGHT_SKYSHIP) //reveal entire map
|
2023-10-22 17:36:41 +02:00
|
|
|
getAllTiles (tiles, player, -1, [](auto * tile){return true;});
|
2023-10-19 16:19:09 +02:00
|
|
|
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);
|
2023-10-22 17:36:41 +02:00
|
|
|
int distance = pos.dist(tilePos, distanceFormula);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
if(distance <= radious)
|
|
|
|
{
|
|
|
|
if(!player
|
2024-05-07 21:17:05 +02:00
|
|
|
|| (mode == ETileVisibility::HIDDEN && team->fogOfWarMap[pos.z][xd][yd] == 0)
|
|
|
|
|| (mode == ETileVisibility::REVEALED && team->fogOfWarMap[pos.z][xd][yd] == 1)
|
2023-10-19 16:19:09 +02:00
|
|
|
)
|
|
|
|
tiles.insert(int3(xd,yd,pos.z));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-22 17:36:41 +02:00
|
|
|
void CPrivilegedInfoCallback::getAllTiles(std::unordered_set<int3> & tiles, std::optional<PlayerColor> Player, int level, std::function<bool(const TerrainTile *)> filter) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
if(!!Player && !Player->isValidPlayer())
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
for(auto zd: floors)
|
|
|
|
{
|
|
|
|
for(int xd = 0; xd < gs->map->width; xd++)
|
|
|
|
{
|
|
|
|
for(int yd = 0; yd < gs->map->height; yd++)
|
|
|
|
{
|
2023-10-22 17:36:41 +02:00
|
|
|
int3 coordinates(xd, yd, zd);
|
|
|
|
if (filter(getTile(coordinates)))
|
|
|
|
tiles.insert(coordinates);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void CPrivilegedInfoCallback::pickAllowedArtsSet(std::vector<const CArtifact *> & out, vstd::RNG & rand)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
for (int j = 0; j < 3 ; j++)
|
2023-11-07 14:27:25 +02:00
|
|
|
out.push_back(gameState()->pickRandomArtifact(rand, CArtifact::ART_TREASURE).toArtifact());
|
2023-10-19 16:19:09 +02:00
|
|
|
for (int j = 0; j < 3 ; j++)
|
2023-11-07 14:27:25 +02:00
|
|
|
out.push_back(gameState()->pickRandomArtifact(rand, CArtifact::ART_MINOR).toArtifact());
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2023-11-07 14:27:25 +02:00
|
|
|
out.push_back(gameState()->pickRandomArtifact(rand, CArtifact::ART_MAJOR).toArtifact());
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivilegedInfoCallback::getAllowedSpells(std::vector<SpellID> & out, std::optional<ui16> level)
|
|
|
|
{
|
2024-06-17 22:58:04 +02:00
|
|
|
for (auto const & spellID : gs->map->allowedSpells)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-06-17 22:58:04 +02:00
|
|
|
const auto * spell = spellID.toEntity(VLC);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-06-17 22:58:04 +02:00
|
|
|
if (!isAllowed(spellID))
|
2023-10-19 16:19:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (level.has_value() && spell->getLevel() != level)
|
|
|
|
continue;
|
|
|
|
|
2024-06-17 22:58:04 +02:00
|
|
|
out.push_back(spellID);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CGameState * CPrivilegedInfoCallback::gameState()
|
|
|
|
{
|
|
|
|
return gs;
|
|
|
|
}
|
|
|
|
|
2024-08-24 00:32:02 +02:00
|
|
|
void CPrivilegedInfoCallback::loadCommonState(CLoadFile & in)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
logGlobal->info("Loading lib part of game...");
|
|
|
|
in.checkMagicBytes(SAVEGAME_MAGIC);
|
|
|
|
|
|
|
|
CMapHeader dum;
|
|
|
|
StartInfo * si = nullptr;
|
2023-11-07 18:32:40 +02:00
|
|
|
ActiveModsInSaveList activeMods;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
logGlobal->info("\tReading header");
|
|
|
|
in.serializer & dum;
|
|
|
|
|
|
|
|
logGlobal->info("\tReading options");
|
|
|
|
in.serializer & si;
|
|
|
|
|
2023-11-07 18:32:40 +02:00
|
|
|
logGlobal->info("\tReading mod list");
|
|
|
|
in.serializer & activeMods;
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
logGlobal->info("\tReading gamestate");
|
|
|
|
in.serializer & gs;
|
|
|
|
}
|
|
|
|
|
2024-08-24 00:32:02 +02:00
|
|
|
void CPrivilegedInfoCallback::saveCommonState(CSaveFile & out) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2023-11-07 18:32:40 +02:00
|
|
|
ActiveModsInSaveList activeMods;
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
logGlobal->info("Saving lib part of game...");
|
|
|
|
out.putMagicBytes(SAVEGAME_MAGIC);
|
|
|
|
logGlobal->info("\tSaving header");
|
|
|
|
out.serializer & static_cast<CMapHeader&>(*gs->map);
|
|
|
|
logGlobal->info("\tSaving options");
|
|
|
|
out.serializer & gs->scenarioOps;
|
2023-11-07 18:32:40 +02:00
|
|
|
logGlobal->info("\tSaving mod list");
|
|
|
|
out.serializer & activeMods;
|
2023-10-19 16:19:09 +02:00
|
|
|
logGlobal->info("\tSaving gamestate");
|
|
|
|
out.serializer & gs;
|
|
|
|
}
|
|
|
|
|
|
|
|
TerrainTile * CNonConstInfoCallback::getTile(const int3 & pos)
|
|
|
|
{
|
|
|
|
if(!gs->map->isInTheMap(pos))
|
|
|
|
return nullptr;
|
|
|
|
return &gs->map->getTile(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGHeroInstance * CNonConstInfoCallback::getHero(const ObjectInstanceID & objid)
|
|
|
|
{
|
|
|
|
return const_cast<CGHeroInstance*>(CGameInfoCallback::getHero(objid));
|
|
|
|
}
|
|
|
|
|
|
|
|
CGTownInstance * CNonConstInfoCallback::getTown(const ObjectInstanceID & objid)
|
|
|
|
{
|
|
|
|
return const_cast<CGTownInstance*>(CGameInfoCallback::getTown(objid));
|
|
|
|
}
|
|
|
|
|
|
|
|
TeamState * CNonConstInfoCallback::getTeam(const TeamID & teamID)
|
|
|
|
{
|
|
|
|
return const_cast<TeamState*>(CGameInfoCallback::getTeam(teamID));
|
|
|
|
}
|
|
|
|
|
|
|
|
TeamState * CNonConstInfoCallback::getPlayerTeam(const PlayerColor & color)
|
|
|
|
{
|
|
|
|
return const_cast<TeamState*>(CGameInfoCallback::getPlayerTeam(color));
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerState * CNonConstInfoCallback::getPlayerState(const PlayerColor & color, bool verbose)
|
|
|
|
{
|
|
|
|
return const_cast<PlayerState*>(CGameInfoCallback::getPlayerState(color, verbose));
|
|
|
|
}
|
|
|
|
|
|
|
|
CArtifactInstance * CNonConstInfoCallback::getArtInstance(const ArtifactInstanceID & aid)
|
|
|
|
{
|
|
|
|
return gs->map->artInstances.at(aid.num);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGObjectInstance * CNonConstInfoCallback::getObjInstance(const ObjectInstanceID & oid)
|
|
|
|
{
|
|
|
|
return gs->map->objects.at(oid.num);
|
|
|
|
}
|
|
|
|
|
|
|
|
CArmedInstance * CNonConstInfoCallback::getArmyInstance(const ObjectInstanceID & oid)
|
|
|
|
{
|
|
|
|
return dynamic_cast<CArmedInstance *>(getObjInstance(oid));
|
|
|
|
}
|
|
|
|
|
2024-02-01 10:43:16 +02:00
|
|
|
CArtifactSet * CNonConstInfoCallback::getArtSet(const ArtifactLocation & loc)
|
|
|
|
{
|
|
|
|
if(auto hero = getHero(loc.artHolder))
|
|
|
|
{
|
|
|
|
if(loc.creature.has_value())
|
|
|
|
{
|
|
|
|
if(loc.creature.value() == SlotID::COMMANDER_SLOT_PLACEHOLDER)
|
|
|
|
return hero->commander;
|
|
|
|
else
|
|
|
|
return hero->getStackPtr(loc.creature.value());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return hero;
|
|
|
|
}
|
|
|
|
}
|
2024-08-20 16:15:50 +02:00
|
|
|
else if(auto market = getMarket(loc.artHolder))
|
2024-02-01 10:43:16 +02:00
|
|
|
{
|
2024-08-17 19:19:48 +02:00
|
|
|
if(auto artSet = market->getArtifactsStorage())
|
2024-08-27 15:44:30 +02:00
|
|
|
return artSet;
|
2024-02-01 10:43:16 +02:00
|
|
|
}
|
2024-08-17 21:06:48 +02:00
|
|
|
else if(auto army = getArmyInstance(loc.artHolder))
|
|
|
|
{
|
|
|
|
return army->getStackPtr(loc.creature.value());
|
|
|
|
}
|
2024-08-17 19:19:48 +02:00
|
|
|
return nullptr;
|
2024-02-01 10:43:16 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
|
|
|
|
{
|
|
|
|
//only server knows
|
|
|
|
logGlobal->error("isVisitCoveredByAnotherQuery call on client side");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|