1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-20 03:29:32 +02:00
vcmi/lib/mapping/CCampaignHandler.h

308 lines
8.4 KiB
C
Raw Normal View History

/*
* CCampaignHandler.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
#include "../../lib/GameConstants.h"
2010-02-08 14:39:19 +00:00
VCMI_LIB_NAMESPACE_BEGIN
struct StartInfo;
class CGHeroInstance;
class CBinaryReader;
class CInputStream;
class CMap;
class CMapHeader;
class CMapInfo;
class JsonNode;
namespace CampaignVersion
{
enum Version
{
RoE = 4,
AB = 5,
SoD = 6,
2023-04-09 03:24:40 +04:00
WoG = 6,
// Chr = 7, // Heroes Chronicles, likely identical to SoD, untested
2023-04-09 03:24:40 +04:00
VCMI = 1
};
2023-04-09 03:24:40 +04:00
const int VCMI_MIN = 1;
const int VCMI_MAX = 1;
}
2023-04-17 04:26:35 +04:00
struct DLL_LINKAGE CampaignRegions
{
std::string campPrefix;
int colorSuffixLength;
struct DLL_LINKAGE RegionDescription
{
std::string infix;
int xpos, ypos;
template <typename Handler> void serialize(Handler &h, const int formatVersion)
{
h & infix;
h & xpos;
h & ypos;
}
static CampaignRegions::RegionDescription fromJson(const JsonNode & node);
};
std::vector<RegionDescription> regions;
template <typename Handler> void serialize(Handler &h, const int formatVersion)
{
h & campPrefix;
h & colorSuffixLength;
h & regions;
}
static CampaignRegions fromJson(const JsonNode & node);
static CampaignRegions getLegacy(int campId);
};
class DLL_LINKAGE CCampaignHeader
2010-02-08 14:39:19 +00:00
{
public:
2023-06-01 11:34:26 +03:00
si32 version = 0; //4 - RoE, 5 - AB, 6 - SoD, WoG and HotA
2023-04-17 04:26:35 +04:00
CampaignRegions campaignRegions;
int numberOfScenarios = 0;
std::string name, description;
2023-04-17 23:19:56 +04:00
bool difficultyChoosenByPlayer = false;
2023-04-09 03:24:40 +04:00
bool valid = false;
2010-02-18 12:34:44 +00:00
std::string filename;
2023-02-25 17:44:15 +02:00
std::string modName;
std::string encoding;
2023-04-17 04:26:35 +04:00
void loadLegacyData(ui8 campId);
2016-11-25 17:21:27 +03:00
template <typename Handler> void serialize(Handler &h, const int formatVersion)
{
h & version;
2023-04-17 23:19:56 +04:00
h & campaignRegions;
h & numberOfScenarios;
h & name;
h & description;
h & difficultyChoosenByPlayer;
h & filename;
2023-02-25 17:44:15 +02:00
h & modName;
h & encoding;
2023-04-17 23:19:56 +04:00
h & valid;
}
2010-02-08 14:39:19 +00:00
};
class DLL_LINKAGE CScenarioTravel
2010-02-11 14:12:22 +00:00
{
public:
2023-04-17 23:19:56 +04:00
struct DLL_LINKAGE WhatHeroKeeps
{
bool experience = false;
bool primarySkills = false;
bool secondarySkills = false;
bool spells = false;
bool artifacts = false;
template <typename Handler> void serialize(Handler &h, const int formatVersion)
{
h & experience;
h & primarySkills;
h & secondarySkills;
h & spells;
h & artifacts;
}
};
WhatHeroKeeps whatHeroKeeps;
2023-04-19 03:11:13 +04:00
//TODO: use typed containers
std::set<int> monstersKeptByHero;
std::set<int> artifactsKeptByHero;
2010-02-11 14:12:22 +00:00
2023-02-11 19:30:06 +03:00
ui8 startOptions = 0; //1 - start bonus, 2 - traveling hero, 3 - hero options
2010-02-11 14:12:22 +00:00
2023-02-11 19:30:06 +03:00
ui8 playerColor = 0; //only for startOptions == 1
2010-02-11 14:12:22 +00:00
struct DLL_LINKAGE STravelBonus
2010-02-11 14:12:22 +00:00
{
2012-09-22 15:16:45 +00:00
enum EBonusType {SPELL, MONSTER, BUILDING, ARTIFACT, SPELL_SCROLL, PRIMARY_SKILL, SECONDARY_SKILL, RESOURCE,
HEROES_FROM_PREVIOUS_SCENARIO, HERO};
2023-02-11 19:30:06 +03:00
EBonusType type = EBonusType::SPELL; //uses EBonusType
si32 info1 = 0, info2 = 0, info3 = 0; //purpose depends on type
2010-02-11 14:12:22 +00:00
bool isBonusForHero() const;
2010-02-11 14:12:22 +00:00
template <typename Handler> void serialize(Handler &h, const int formatVersion)
{
h & type;
h & info1;
h & info2;
h & info3;
2010-02-11 14:12:22 +00:00
}
};
std::vector<STravelBonus> bonusesToChoose;
template <typename Handler> void serialize(Handler &h, const int formatVersion)
{
h & whatHeroKeeps;
h & monstersKeptByHero;
2023-04-17 23:19:56 +04:00
h & artifactsKeptByHero;
h & startOptions;
h & playerColor;
h & bonusesToChoose;
2010-02-11 14:12:22 +00:00
}
};
class DLL_LINKAGE CCampaignScenario
2010-02-11 14:12:22 +00:00
{
public:
struct DLL_LINKAGE SScenarioPrologEpilog
2010-02-11 14:12:22 +00:00
{
2023-02-11 19:30:06 +03:00
bool hasPrologEpilog = false;
std::string prologVideo; // from CmpMovie.txt
std::string prologMusic; // from CmpMusic.txt
2010-02-11 14:12:22 +00:00
std::string prologText;
template <typename Handler> void serialize(Handler &h, const int formatVersion)
{
h & hasPrologEpilog;
h & prologVideo;
h & prologMusic;
h & prologText;
2010-02-11 14:12:22 +00:00
}
};
2013-12-18 18:18:12 +00:00
std::string mapName; //*.h3m
std::string scenarioName; //from header. human-readble
std::set<ui8> preconditionRegions; //what we need to conquer to conquer this one (stored as bitfield in h3c)
2023-02-11 19:30:06 +03:00
ui8 regionColor = 0;
ui8 difficulty = 0;
bool conquered = false;
2013-12-18 18:18:12 +00:00
std::string regionText;
2010-02-11 14:12:22 +00:00
SScenarioPrologEpilog prolog, epilog;
CScenarioTravel travelOptions;
std::vector<HeroTypeID> keepHeroes; // contains list of heroes which should be kept for next scenario (doesn't matter if they lost)
std::vector<JsonNode> crossoverHeroes; // contains all heroes with the same state when the campaign scenario was finished
std::vector<JsonNode> placedCrossoverHeroes; // contains all placed crossover heroes defined by hero placeholders when the scenario was started
void loadPreconditionRegions(ui32 regions);
bool isNotVoid() const;
// FIXME: due to usage of JsonNode I can't make these methods const
2023-02-11 19:30:06 +03:00
const CGHeroInstance * strongestHero(const PlayerColor & owner);
std::vector<CGHeroInstance *> getLostCrossoverHeroes(); /// returns a list of crossover heroes which started the scenario, but didn't complete it
2010-02-11 14:12:22 +00:00
template <typename Handler> void serialize(Handler &h, const int formatVersion)
{
h & mapName;
h & scenarioName;
h & preconditionRegions;
h & regionColor;
h & difficulty;
h & conquered;
h & regionText;
h & prolog;
h & epilog;
h & travelOptions;
h & crossoverHeroes;
h & placedCrossoverHeroes;
h & keepHeroes;
2010-02-11 14:12:22 +00:00
}
};
class DLL_LINKAGE CCampaign
2010-02-11 14:12:22 +00:00
{
public:
CCampaignHeader header;
std::vector<CCampaignScenario> scenarios;
std::map<int, std::string > mapPieces; //binary h3ms, scenario number -> map data
2010-02-11 14:12:22 +00:00
template <typename Handler> void serialize(Handler &h, const int formatVersion)
{
h & header;
h & scenarios;
h & mapPieces;
2010-02-11 14:12:22 +00:00
}
bool conquerable(int whichScenario) const;
2010-02-11 14:12:22 +00:00
};
2010-02-08 14:39:19 +00:00
class DLL_LINKAGE CCampaignState
{
public:
std::unique_ptr<CCampaign> camp;
std::string fileEncoding;
std::vector<ui8> mapsConquered, mapsRemaining;
2023-04-16 20:42:56 +03:00
std::optional<si32> currentMap;
std::map<ui8, ui8> chosenCampaignBonuses;
void setCurrentMapAsConquered(const std::vector<CGHeroInstance*> & heroes);
2023-04-16 20:42:56 +03:00
std::optional<CScenarioTravel::STravelBonus> getBonusForCurrentMap() const;
const CCampaignScenario & getCurrentScenario() const;
CCampaignScenario & getCurrentScenario();
ui8 currentBonusID() const;
CMap * getMap(int scenarioId = -1) const;
std::unique_ptr<CMapHeader> getHeader(int scenarioId = -1) const;
std::shared_ptr<CMapInfo> getMapInfo(int scenarioId = -1) const;
static JsonNode crossoverSerialize(CGHeroInstance * hero);
static CGHeroInstance * crossoverDeserialize(JsonNode & node);
2023-02-11 19:30:06 +03:00
CCampaignState() = default;
CCampaignState(std::unique_ptr<CCampaign> _camp);
template <typename Handler> void serialize(Handler &h, const int version)
{
h & camp;
h & mapsRemaining;
h & mapsConquered;
h & currentMap;
h & chosenCampaignBonuses;
}
};
class DLL_LINKAGE CCampaignHandler
2010-02-08 14:39:19 +00:00
{
2023-03-14 19:46:15 +02:00
static std::string readLocalizedString(CBinaryReader & reader, std::string filename, std::string modName, std::string encoding, std::string identifier);
2023-04-09 03:24:40 +04:00
//parsers for VCMI campaigns (*.vcmp)
static CCampaignHeader readHeaderFromJson(JsonNode & reader, std::string filename, std::string modName, std::string encoding);
2023-04-17 04:26:35 +04:00
static CCampaignScenario readScenarioFromJson(JsonNode & reader);
static CScenarioTravel readScenarioTravelFromJson(JsonNode & reader);
2023-04-09 03:24:40 +04:00
//parsers for original H3C campaigns
2023-02-25 17:44:15 +02:00
static CCampaignHeader readHeaderFromMemory(CBinaryReader & reader, std::string filename, std::string modName, std::string encoding);
2023-04-17 04:26:35 +04:00
static CCampaignScenario readScenarioFromMemory(CBinaryReader & reader, const CCampaignHeader & header);
static CScenarioTravel readScenarioTravelFromMemory(CBinaryReader & reader, int version);
/// returns h3c split in parts. 0 = h3c header, 1-end - maps (binary h3m)
/// headerOnly - only header will be decompressed, returned vector wont have any maps
static std::vector<std::vector<ui8>> getFile(std::unique_ptr<CInputStream> file, bool headerOnly);
static std::string prologVideoName(ui8 index);
static std::string prologMusicName(ui8 index);
static std::string prologVoiceName(ui8 index);
public:
static CCampaignHeader getHeader( const std::string & name); //name - name of appropriate file
2010-02-11 14:12:22 +00:00
static std::unique_ptr<CCampaign> getCampaign(const std::string & name); //name - name of appropriate file
2010-02-08 14:39:19 +00:00
};
VCMI_LIB_NAMESPACE_END