2023-10-19 16:19:09 +02:00
|
|
|
/*
|
|
|
|
* CMap.h, 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-01-09 16:43:36 +02:00
|
|
|
#include "CMapDefines.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "CMapHeader.h"
|
2024-01-09 16:43:36 +02:00
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
#include "../GameCallbackHolder.h"
|
2024-01-09 16:43:36 +02:00
|
|
|
#include "../networkPacks/TradeItem.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
class CArtifactInstance;
|
2024-09-04 14:32:47 +03:00
|
|
|
class CArtifactSet;
|
2023-10-19 16:19:09 +02:00
|
|
|
class CGObjectInstance;
|
|
|
|
class CGHeroInstance;
|
|
|
|
class CCommanderInstance;
|
2025-04-08 16:37:45 +03:00
|
|
|
class CGameState;
|
2023-10-19 16:19:09 +02:00
|
|
|
class CGCreature;
|
|
|
|
class CQuest;
|
|
|
|
class CGTownInstance;
|
|
|
|
class IModableArt;
|
|
|
|
class IQuestObject;
|
|
|
|
class CInputStream;
|
|
|
|
class CMapEditManager;
|
|
|
|
class JsonSerializeFormat;
|
2024-08-31 11:00:36 +00:00
|
|
|
class IGameSettings;
|
|
|
|
class GameSettings;
|
2023-10-19 16:19:09 +02:00
|
|
|
struct TeleportChannel;
|
2024-08-31 11:00:36 +00:00
|
|
|
enum class EGameSettings;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
/// The rumor struct consists of a rumor name and text.
|
|
|
|
struct DLL_LINKAGE Rumor
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
MetaString text;
|
|
|
|
|
|
|
|
Rumor() = default;
|
|
|
|
~Rumor() = default;
|
|
|
|
|
|
|
|
template <typename Handler>
|
2024-01-20 20:34:51 +02:00
|
|
|
void serialize(Handler & h)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
h & name;
|
|
|
|
h & text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void serializeJson(JsonSerializeFormat & handler);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
|
2024-01-01 16:37:48 +02:00
|
|
|
class DLL_LINKAGE CMap : public CMapHeader, public GameCallbackHolder
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2025-03-09 22:44:36 +00:00
|
|
|
friend class CSerializer;
|
|
|
|
|
2024-08-31 11:00:36 +00:00
|
|
|
std::unique_ptr<GameSettings> gameSettings;
|
2025-03-09 22:44:36 +00:00
|
|
|
|
2025-03-14 17:07:30 +00:00
|
|
|
/// All artifacts that exists on map, whether on map, in hero inventory, or stored in some object
|
2025-03-13 21:49:57 +00:00
|
|
|
std::vector<std::shared_ptr<CArtifactInstance>> artInstances;
|
2025-03-14 17:07:30 +00:00
|
|
|
/// All heroes that are currently free for recruitment in taverns and are not present on map
|
|
|
|
std::vector<std::shared_ptr<CGHeroInstance> > heroesPool;
|
2025-03-13 21:49:57 +00:00
|
|
|
|
2025-03-14 17:07:30 +00:00
|
|
|
/// Precomputed indices of all towns on map
|
2025-03-13 21:49:57 +00:00
|
|
|
std::vector<ObjectInstanceID> towns;
|
|
|
|
|
2025-03-14 17:07:30 +00:00
|
|
|
/// Precomputed indices of all towns on map. Does not includes heroes in prisons
|
2025-03-13 21:49:57 +00:00
|
|
|
std::vector<ObjectInstanceID> heroesOnMap;
|
2025-03-10 16:20:40 +00:00
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
public:
|
2025-03-17 21:32:05 +00:00
|
|
|
/// Central lists of items in game. Position of item in the vectors below is their (instance) id.
|
|
|
|
/// TODO: make private
|
2025-03-18 19:54:09 +00:00
|
|
|
std::vector<std::shared_ptr<CGObjectInstance>> objects;
|
2025-03-17 21:32:05 +00:00
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
explicit CMap(IGameCallback *cb);
|
2023-10-19 16:19:09 +02:00
|
|
|
~CMap();
|
|
|
|
void initTerrain();
|
|
|
|
|
|
|
|
CMapEditManager * getEditManager();
|
2024-12-28 21:11:50 +00:00
|
|
|
inline TerrainTile & getTile(const int3 & tile);
|
|
|
|
inline const TerrainTile & getTile(const int3 & tile) const;
|
2023-10-19 16:19:09 +02:00
|
|
|
bool isCoastalTile(const int3 & pos) const;
|
2024-12-28 21:11:50 +00:00
|
|
|
inline bool isInTheMap(const int3 & pos) const;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
bool canMoveBetween(const int3 &src, const int3 &dst) const;
|
|
|
|
bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
|
|
|
|
int3 guardingCreaturePosition (int3 pos) const;
|
|
|
|
|
|
|
|
void addBlockVisTiles(CGObjectInstance * obj);
|
|
|
|
void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
|
|
|
|
void calculateGuardingGreaturePositions();
|
|
|
|
|
2025-03-10 16:20:40 +00:00
|
|
|
CArtifactInstance * createScroll(const SpellID & spellId);
|
|
|
|
CArtifactInstance * createArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
|
|
|
|
CArtifactInstance * createSingleArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
|
|
|
|
|
|
|
|
CArtifactInstance * getArtifactInstance(const ArtifactInstanceID & artifactID);
|
|
|
|
const CArtifactInstance * getArtifactInstance(const ArtifactInstanceID & artifactID) const;
|
|
|
|
|
|
|
|
void eraseArtifactInstance(const ArtifactInstanceID art);
|
2024-09-06 17:59:40 +03:00
|
|
|
void moveArtifactInstance(CArtifactSet & srcSet, const ArtifactPosition & srcSlot, CArtifactSet & dstSet, const ArtifactPosition & dstSlot);
|
2025-03-10 16:20:40 +00:00
|
|
|
void putArtifactInstance(CArtifactSet & set, const ArtifactInstanceID art, const ArtifactPosition & slot);
|
2024-09-06 17:59:40 +03:00
|
|
|
void removeArtifactInstance(CArtifactSet & set, const ArtifactPosition & slot);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-03-13 21:49:57 +00:00
|
|
|
void generateUniqueInstanceName(CGObjectInstance * target);
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
///Use only this method when creating new map object instances
|
2025-03-13 21:49:57 +00:00
|
|
|
void addNewObject(std::shared_ptr<CGObjectInstance> obj);
|
|
|
|
void moveObject(ObjectInstanceID target, const int3 & dst);
|
2025-03-13 19:42:18 +00:00
|
|
|
|
|
|
|
/// Remove objects and shifts object indicies.
|
|
|
|
/// Only for use in map editor / RMG
|
2025-03-13 21:49:57 +00:00
|
|
|
std::shared_ptr<CGObjectInstance> removeObject(ObjectInstanceID oldObject);
|
2025-03-13 19:42:18 +00:00
|
|
|
|
|
|
|
/// Replaced map object with specified ID with new object
|
|
|
|
/// Old object must exist and will be removed from map
|
|
|
|
/// Returns pointer to old object, which can be manipulated or dropped
|
2025-03-13 21:49:57 +00:00
|
|
|
std::shared_ptr<CGObjectInstance> replaceObject(ObjectInstanceID oldObject, const std::shared_ptr<CGObjectInstance> & newObject);
|
2025-03-13 19:42:18 +00:00
|
|
|
|
|
|
|
/// Erases object from map without shifting indices
|
|
|
|
/// Returns pointer to old object, which can be manipulated or dropped
|
2025-03-13 21:49:57 +00:00
|
|
|
std::shared_ptr<CGObjectInstance> eraseObject(ObjectInstanceID oldObject);
|
2025-03-13 19:42:18 +00:00
|
|
|
|
2025-03-13 21:49:57 +00:00
|
|
|
void heroAddedToMap(const CGHeroInstance * hero);
|
|
|
|
void heroRemovedFromMap(const CGHeroInstance * hero);
|
|
|
|
void townAddedToMap(const CGTownInstance * town);
|
|
|
|
void townRemovedFromMap(const CGTownInstance * town);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-03-14 17:07:30 +00:00
|
|
|
void addToHeroPool(std::shared_ptr<CGHeroInstance> hero);
|
|
|
|
std::shared_ptr<CGHeroInstance> tryTakeFromHeroPool(HeroTypeID hero);
|
|
|
|
CGHeroInstance * tryGetFromHeroPool(HeroTypeID hero);
|
|
|
|
std::vector<HeroTypeID> getHeroesInPool() const;
|
|
|
|
|
2025-03-15 17:40:10 +00:00
|
|
|
CGObjectInstance * getObject(ObjectInstanceID obj);
|
|
|
|
const CGObjectInstance * getObject(ObjectInstanceID obj) const;
|
|
|
|
|
2025-04-08 16:37:45 +03:00
|
|
|
void attachToBonusSystem(CGameState * gs);
|
|
|
|
|
2025-03-15 17:40:10 +00:00
|
|
|
template<typename ObjectType = CGObjectInstance>
|
|
|
|
std::vector<const ObjectType *> getObjects() const
|
|
|
|
{
|
|
|
|
std::vector<const ObjectType *> result;
|
|
|
|
for (const auto & object : objects)
|
|
|
|
{
|
|
|
|
auto casted = dynamic_cast<const ObjectType*>(object.get());
|
|
|
|
if (casted)
|
|
|
|
result.push_back(casted);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ObjectType = CGObjectInstance>
|
|
|
|
std::vector<ObjectType *> getObjects()
|
|
|
|
{
|
|
|
|
std::vector<ObjectType *> result;
|
|
|
|
for (const auto & object : objects)
|
|
|
|
{
|
|
|
|
auto casted = dynamic_cast<ObjectType*>(object.get());
|
|
|
|
if (casted)
|
|
|
|
result.push_back(casted);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2025-04-08 16:37:45 +03:00
|
|
|
std::vector<CArtifactInstance *> getArtifacts()
|
|
|
|
{
|
|
|
|
std::vector<CArtifactInstance *> result;
|
|
|
|
for (const auto & art : artInstances)
|
|
|
|
if (art)
|
|
|
|
result.push_back(art.get());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
bool isWaterMap() const;
|
|
|
|
bool calculateWaterContent();
|
|
|
|
void banWaterArtifacts();
|
|
|
|
void banWaterHeroes();
|
|
|
|
void banHero(const HeroTypeID& id);
|
2023-12-24 09:36:26 +01:00
|
|
|
void unbanHero(const HeroTypeID & id);
|
2023-10-19 16:19:09 +02:00
|
|
|
void banWaterSpells();
|
|
|
|
void banWaterSkills();
|
|
|
|
void banWaterContent();
|
|
|
|
|
|
|
|
/// Gets object of specified type on requested position
|
|
|
|
const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
|
2025-03-14 17:07:30 +00:00
|
|
|
|
|
|
|
/// Returns pointer to hero of specified type if hero is present on map
|
2023-10-24 17:11:25 +03:00
|
|
|
CGHeroInstance * getHero(HeroTypeID heroId);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-03-13 21:49:57 +00:00
|
|
|
/// Returns ID's of all heroes that are currently present on map
|
2025-04-08 16:37:45 +03:00
|
|
|
/// Includes all garrisoned and imprisoned heroes
|
2025-03-13 21:49:57 +00:00
|
|
|
const std::vector<ObjectInstanceID> & getHeroesOnMap();
|
|
|
|
|
|
|
|
/// Returns ID's of all towns present on map
|
|
|
|
const std::vector<ObjectInstanceID> & getAllTowns();
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
/// Sets the victory/loss condition objectives ??
|
|
|
|
void checkForObjectives();
|
|
|
|
|
2024-01-31 01:37:33 +02:00
|
|
|
void resolveQuestIdentifiers();
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-04-03 14:48:04 +02:00
|
|
|
void reindexObjects();
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
std::vector<Rumor> rumors;
|
2023-11-05 15:24:26 +02:00
|
|
|
std::set<SpellID> allowedSpells;
|
|
|
|
std::set<ArtifactID> allowedArtifact;
|
|
|
|
std::set<SecondarySkill> allowedAbilities;
|
2024-08-12 10:38:18 +00:00
|
|
|
std::vector<CMapEvent> events;
|
2023-10-19 16:19:09 +02:00
|
|
|
int3 grailPos;
|
|
|
|
int grailRadius;
|
|
|
|
|
|
|
|
//Helper lists
|
|
|
|
std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CMapEditManager> editManager;
|
|
|
|
boost::multi_array<int3, 3> guardingCreaturePositions;
|
|
|
|
|
2025-03-13 19:42:18 +00:00
|
|
|
std::map<std::string, std::shared_ptr<CGObjectInstance> > instanceNames;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
bool waterMap;
|
|
|
|
|
2024-01-09 16:43:36 +02:00
|
|
|
ui8 obeliskCount = 0; //how many obelisks are on map
|
|
|
|
std::map<TeamID, ui8> obelisksVisited; //map: team_id => how many obelisks has been visited
|
|
|
|
|
2024-10-12 18:19:58 +00:00
|
|
|
std::vector<ArtifactID> townMerchantArtifacts;
|
2024-01-09 16:43:36 +02:00
|
|
|
std::vector<TradeItemBuy> townUniversitySkills;
|
|
|
|
|
2024-09-04 18:18:10 +00:00
|
|
|
void overrideGameSettings(const JsonNode & input);
|
2024-08-31 11:00:36 +00:00
|
|
|
void overrideGameSetting(EGameSettings option, const JsonNode & input);
|
|
|
|
const IGameSettings & getSettings() const;
|
|
|
|
|
2025-03-13 21:49:57 +00:00
|
|
|
void postInitialize();
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
private:
|
|
|
|
/// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
|
|
|
|
boost::multi_array<TerrainTile, 3> terrain;
|
|
|
|
|
|
|
|
si32 uidCounter; //TODO: initialize when loading an old map
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <typename Handler>
|
2024-01-20 20:34:51 +02:00
|
|
|
void serialize(Handler &h)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
h & static_cast<CMapHeader&>(*this);
|
|
|
|
h & triggeredEvents; //from CMapHeader
|
|
|
|
h & rumors;
|
|
|
|
h & allowedSpells;
|
|
|
|
h & allowedAbilities;
|
|
|
|
h & allowedArtifact;
|
|
|
|
h & events;
|
|
|
|
h & grailPos;
|
|
|
|
h & artInstances;
|
2024-01-31 01:37:33 +02:00
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
//TODO: viccondetails
|
|
|
|
h & terrain;
|
|
|
|
h & guardingCreaturePositions;
|
|
|
|
|
|
|
|
h & objects;
|
|
|
|
h & heroesOnMap;
|
2025-04-01 15:59:08 +03:00
|
|
|
h & heroesPool;
|
2023-10-19 16:19:09 +02:00
|
|
|
h & teleportChannels;
|
|
|
|
h & towns;
|
|
|
|
h & artInstances;
|
|
|
|
|
|
|
|
// static members
|
2024-01-09 16:43:36 +02:00
|
|
|
h & obeliskCount;
|
|
|
|
h & obelisksVisited;
|
2025-03-18 11:35:15 +00:00
|
|
|
h & townMerchantArtifacts;
|
2024-01-09 16:43:36 +02:00
|
|
|
h & townUniversitySkills;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
h & instanceNames;
|
2025-03-18 11:35:15 +00:00
|
|
|
h & *gameSettings;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-12-28 12:28:14 +00:00
|
|
|
inline bool CMap::isInTheMap(const int3 & pos) const
|
|
|
|
{
|
|
|
|
// Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
|
|
|
|
return
|
|
|
|
static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
|
|
|
|
static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
|
|
|
|
static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline TerrainTile & CMap::getTile(const int3 & tile)
|
|
|
|
{
|
|
|
|
assert(isInTheMap(tile));
|
|
|
|
return terrain[tile.z][tile.x][tile.y];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const TerrainTile & CMap::getTile(const int3 & tile) const
|
|
|
|
{
|
|
|
|
assert(isInTheMap(tile));
|
|
|
|
return terrain[tile.z][tile.x][tile.y];
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|