1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-26 08:41:13 +02:00
vcmi/lib/mapping/CMapService.cpp

158 lines
4.9 KiB
C++
Raw Normal View History

/*
* CMapService.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
*
*/
2012-11-21 12:13:33 +03:00
#include "StdInc.h"
#include "CMapService.h"
#include "../filesystem/Filesystem.h"
2013-04-07 13:48:07 +03:00
#include "../filesystem/CBinaryReader.h"
#include "../filesystem/CCompressedStream.h"
#include "../filesystem/CMemoryStream.h"
#include "../filesystem/CMemoryBuffer.h"
#include "../CModHandler.h"
#include "../Languages.h"
2016-02-13 18:43:05 +02:00
#include "CMap.h"
2013-02-02 20:20:31 +03:00
#include "MapFormatH3M.h"
#include "MapFormatJson.h"
VCMI_LIB_NAMESPACE_BEGIN
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
std::unique_ptr<CMap> CMapService::loadMap(const ResourceID & name) const
{
std::string modName = VLC->modh->findResourceOrigin(name);
std::string language = VLC->modh->getModLanguage(modName);
std::string encoding = Languages::getLanguageOptions(language).encoding;
auto stream = getStreamFromFS(name);
2023-02-25 17:44:15 +02:00
return getMapLoader(stream, name.getName(), modName, encoding)->loadMap();
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ResourceID & name) const
{
std::string modName = VLC->modh->findResourceOrigin(name);
std::string language = VLC->modh->getModLanguage(modName);
std::string encoding = Languages::getLanguageOptions(language).encoding;
auto stream = getStreamFromFS(name);
2023-02-25 17:44:15 +02:00
return getMapLoader(stream, name.getName(), modName, encoding)->loadMapHeader();
}
2023-02-25 17:44:15 +02:00
std::unique_ptr<CMap> CMapService::loadMap(const ui8 * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding) const
{
auto stream = getStreamFromMem(buffer, size);
2023-02-25 17:44:15 +02:00
std::unique_ptr<CMap> map(getMapLoader(stream, name, modName, encoding)->loadMap());
std::unique_ptr<CMapHeader> header(map.get());
2017-06-04 21:42:48 +02:00
//might be original campaign and require patch
getMapPatcher(name)->patchMapHeader(header);
header.release();
2015-12-04 01:06:02 +02:00
return map;
}
2023-02-25 17:44:15 +02:00
std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ui8 * buffer, int size, const std::string & name, const std::string & modName, const std::string & encoding) const
{
auto stream = getStreamFromMem(buffer, size);
2023-02-25 17:44:15 +02:00
std::unique_ptr<CMapHeader> header = getMapLoader(stream, name, modName, encoding)->loadMapHeader();
2017-06-04 21:42:48 +02:00
//might be original campaign and require patch
getMapPatcher(name)->patchMapHeader(header);
2015-12-04 01:06:02 +02:00
return header;
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
void CMapService::saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::path fullPath) const
{
CMemoryBuffer serializeBuffer;
{
CMapSaverJson saver(&serializeBuffer);
saver.saveMap(map);
}
{
boost::filesystem::remove(fullPath);
boost::filesystem::ofstream tmp(fullPath, boost::filesystem::ofstream::binary);
2023-02-11 18:30:06 +02:00
tmp.write(reinterpret_cast<const char *>(serializeBuffer.getBuffer().data()), serializeBuffer.getSize());
tmp.flush();
tmp.close();
}
}
2017-06-04 21:42:48 +02:00
std::unique_ptr<CInputStream> CMapService::getStreamFromFS(const ResourceID & name)
{
2017-06-04 21:42:48 +02:00
return CResourceHandler::get()->load(name);
}
std::unique_ptr<CInputStream> CMapService::getStreamFromMem(const ui8 * buffer, int size)
{
return std::unique_ptr<CInputStream>(new CMemoryStream(buffer, size));
}
2023-02-25 17:44:15 +02:00
std::unique_ptr<IMapLoader> CMapService::getMapLoader(std::unique_ptr<CInputStream> & stream, std::string mapName, std::string modName, std::string encoding)
{
// Read map header
CBinaryReader reader(stream.get());
ui32 header = reader.readUInt32();
reader.getStream()->seek(0);
//check for ZIP magic. Zip files are VCMI maps
switch(header)
{
case 0x06054b50:
case 0x04034b50:
case 0x02014b50:
2016-02-10 06:28:00 +02:00
return std::unique_ptr<IMapLoader>(new CMapLoaderJson(stream.get()));
break;
default:
// Check which map format is used
// gzip header is 3 bytes only in size
switch(header & 0xffffff)
{
// gzip header magic number, reversed for LE
case 0x00088B1F:
stream = std::unique_ptr<CInputStream>(new CCompressedStream(std::move(stream), true));
2023-02-25 17:44:15 +02:00
return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(mapName, modName, encoding, stream.get()));
case static_cast<int>(EMapFormat::WOG) :
case static_cast<int>(EMapFormat::AB) :
case static_cast<int>(EMapFormat::ROE) :
case static_cast<int>(EMapFormat::SOD) :
case static_cast<int>(EMapFormat::HOTA) :
2023-02-25 17:44:15 +02:00
return std::unique_ptr<IMapLoader>(new CMapLoaderH3M(mapName, modName, encoding, stream.get()));
default :
throw std::runtime_error("Unknown map format");
}
}
}
static JsonNode loadPatches(std::string path)
{
2023-02-11 18:30:06 +02:00
JsonNode node = JsonUtils::assembleFromFiles(std::move(path));
for (auto & entry : node.Struct())
JsonUtils::validate(entry.second, "vcmi:mapHeader", "patch for " + entry.first);
node.setMeta(CModHandler::scopeMap());
return node;
}
std::unique_ptr<IMapPatcher> CMapService::getMapPatcher(std::string scenarioName)
{
static JsonNode node;
if (node.isNull())
node = loadPatches("config/mapOverrides.json");
boost::to_lower(scenarioName);
logGlobal->debug("Request to patch map %s", scenarioName);
return std::unique_ptr<IMapPatcher>(new CMapPatcher(node[scenarioName]));
}
VCMI_LIB_NAMESPACE_END