2023-04-02 18:56:10 +02:00
|
|
|
/*
|
|
|
|
* MapReaderH3M.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 "MapReaderH3M.h"
|
2023-04-03 14:02:15 +02:00
|
|
|
|
2023-04-02 18:56:10 +02:00
|
|
|
#include "../filesystem/CBinaryReader.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "../int3.h"
|
2023-05-25 18:35:21 +02:00
|
|
|
#include "../mapObjects/ObjectTemplate.h"
|
2023-04-02 18:56:10 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-05-23 22:55:18 +02:00
|
|
|
template<>
|
|
|
|
BuildingID MapReaderH3M::remapIdentifier(const BuildingID & identifier)
|
|
|
|
{
|
|
|
|
return identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
GameResID MapReaderH3M::remapIdentifier(const GameResID & identifier)
|
|
|
|
{
|
|
|
|
return identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
SpellID MapReaderH3M::remapIdentifier(const SpellID & identifier)
|
|
|
|
{
|
|
|
|
return identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Identifier>
|
|
|
|
Identifier MapReaderH3M::remapIdentifier(const Identifier & identifier)
|
|
|
|
{
|
|
|
|
return remapper.remap(identifier);
|
|
|
|
}
|
|
|
|
|
2023-04-02 18:56:10 +02:00
|
|
|
MapReaderH3M::MapReaderH3M(CInputStream * stream)
|
|
|
|
: reader(std::make_unique<CBinaryReader>(stream))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-23 14:07:44 +02:00
|
|
|
void MapReaderH3M::setFormatLevel(const MapFormatFeaturesH3M & newFeatures)
|
2023-04-02 18:56:10 +02:00
|
|
|
{
|
2023-05-23 14:07:44 +02:00
|
|
|
features = newFeatures;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::setIdentifierRemapper(const MapIdentifiersH3M & newRemapper)
|
|
|
|
{
|
|
|
|
remapper = newRemapper;
|
2023-04-02 18:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ArtifactID MapReaderH3M::readArtifact()
|
|
|
|
{
|
|
|
|
ArtifactID result;
|
|
|
|
|
2023-04-03 14:02:15 +02:00
|
|
|
if(features.levelAB)
|
2023-04-02 18:56:10 +02:00
|
|
|
result = ArtifactID(reader->readUInt16());
|
|
|
|
else
|
|
|
|
result = ArtifactID(reader->readUInt8());
|
|
|
|
|
2023-08-19 19:48:28 +02:00
|
|
|
if(result.getNum() == features.artifactIdentifierInvalid)
|
2023-04-02 18:56:10 +02:00
|
|
|
return ArtifactID::NONE;
|
|
|
|
|
2023-08-19 19:48:28 +02:00
|
|
|
if (result.getNum() < features.artifactsCount)
|
2023-05-23 22:55:18 +02:00
|
|
|
return remapIdentifier(result);
|
2023-05-20 14:09:53 +02:00
|
|
|
|
|
|
|
logGlobal->warn("Map contains invalid artifact %d. Will be removed!", result.getNum());
|
|
|
|
return ArtifactID::NONE;
|
2023-04-05 12:40:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ArtifactID MapReaderH3M::readArtifact32()
|
|
|
|
{
|
|
|
|
ArtifactID result(reader->readInt32());
|
|
|
|
|
|
|
|
if(result == ArtifactID::NONE)
|
|
|
|
return ArtifactID::NONE;
|
|
|
|
|
2023-08-19 19:48:28 +02:00
|
|
|
if (result.getNum() < features.artifactsCount)
|
2023-05-23 22:55:18 +02:00
|
|
|
return remapIdentifier(result);
|
2023-05-20 14:09:53 +02:00
|
|
|
|
|
|
|
logGlobal->warn("Map contains invalid artifact %d. Will be removed!", result.getNum());
|
|
|
|
return ArtifactID::NONE;
|
2023-04-02 18:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HeroTypeID MapReaderH3M::readHero()
|
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
HeroTypeID result(reader->readUInt8());
|
2023-04-02 18:56:10 +02:00
|
|
|
|
2023-04-03 14:02:15 +02:00
|
|
|
if(result.getNum() == features.heroIdentifierInvalid)
|
2023-04-02 18:56:10 +02:00
|
|
|
return HeroTypeID(-1);
|
|
|
|
|
2023-06-20 14:09:42 +02:00
|
|
|
assert(result.getNum() < features.heroesCount);
|
|
|
|
return remapIdentifier(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t MapReaderH3M::readHeroPortrait()
|
|
|
|
{
|
|
|
|
HeroTypeID result(reader->readUInt8());
|
|
|
|
|
|
|
|
if(result.getNum() == features.heroIdentifierInvalid)
|
|
|
|
return int32_t(-1);
|
|
|
|
|
2023-04-02 18:56:10 +02:00
|
|
|
assert(result.getNum() < features.heroesPortraitsCount);
|
2023-06-20 14:09:42 +02:00
|
|
|
return remapper.remapPortrrait(result);
|
2023-04-02 18:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CreatureID MapReaderH3M::readCreature()
|
|
|
|
{
|
|
|
|
CreatureID result;
|
|
|
|
|
2023-04-03 14:02:15 +02:00
|
|
|
if(features.levelAB)
|
2023-04-02 18:56:10 +02:00
|
|
|
result = CreatureID(reader->readUInt16());
|
|
|
|
else
|
|
|
|
result = CreatureID(reader->readUInt8());
|
|
|
|
|
2023-08-19 19:48:28 +02:00
|
|
|
if(result.getNum() == features.creatureIdentifierInvalid)
|
2023-04-02 18:56:10 +02:00
|
|
|
return CreatureID::NONE;
|
|
|
|
|
2023-08-19 19:48:28 +02:00
|
|
|
if(result.getNum() < features.creaturesCount)
|
2023-05-23 22:55:18 +02:00
|
|
|
return remapIdentifier(result);;
|
2023-05-20 14:09:53 +02:00
|
|
|
|
|
|
|
// this may be random creature in army/town, to be randomized later
|
|
|
|
CreatureID randomIndex(result.getNum() - features.creatureIdentifierInvalid - 1);
|
|
|
|
assert(randomIndex < CreatureID::NONE);
|
|
|
|
|
2023-08-19 19:48:28 +02:00
|
|
|
if (randomIndex.getNum() > -16)
|
2023-04-02 18:56:10 +02:00
|
|
|
return randomIndex;
|
|
|
|
|
2023-05-20 14:09:53 +02:00
|
|
|
logGlobal->warn("Map contains invalid creature %d. Will be removed!", result.getNum());
|
|
|
|
return CreatureID::NONE;
|
2023-04-02 18:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TerrainId MapReaderH3M::readTerrain()
|
|
|
|
{
|
|
|
|
TerrainId result(readUInt8());
|
2023-04-03 14:02:15 +02:00
|
|
|
assert(result.getNum() < features.terrainsCount);
|
2023-05-23 22:55:18 +02:00
|
|
|
return remapIdentifier(result);;
|
2023-04-02 18:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RoadId MapReaderH3M::readRoad()
|
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
RoadId result(readInt8());
|
2023-08-20 17:16:00 +02:00
|
|
|
assert(result.getNum() < features.roadsCount);
|
2023-04-02 18:56:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
RiverId MapReaderH3M::readRiver()
|
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
RiverId result(readInt8());
|
2023-08-20 17:16:00 +02:00
|
|
|
assert(result.getNum() < features.riversCount);
|
2023-04-02 18:56:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SecondarySkill MapReaderH3M::readSkill()
|
|
|
|
{
|
|
|
|
SecondarySkill result(readUInt8());
|
2023-08-18 12:38:19 +02:00
|
|
|
assert(result.getNum() < features.skillsCount);
|
2023-05-23 22:55:18 +02:00
|
|
|
return remapIdentifier(result);;
|
2023-04-02 18:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SpellID MapReaderH3M::readSpell()
|
|
|
|
{
|
|
|
|
SpellID result(readUInt8());
|
2023-08-19 19:48:28 +02:00
|
|
|
if(result.getNum() == features.spellIdentifierInvalid)
|
2023-04-02 18:56:10 +02:00
|
|
|
return SpellID::NONE;
|
2023-08-19 19:48:28 +02:00
|
|
|
if(result.getNum() == features.spellIdentifierInvalid - 1)
|
2023-04-02 18:56:10 +02:00
|
|
|
return SpellID::PRESET;
|
|
|
|
|
2023-08-19 19:48:28 +02:00
|
|
|
assert(result.getNum() < features.spellsCount);
|
2023-05-23 22:55:18 +02:00
|
|
|
return remapIdentifier(result);;
|
2023-04-02 18:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SpellID MapReaderH3M::readSpell32()
|
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
SpellID result(readInt32());
|
2023-08-19 19:48:28 +02:00
|
|
|
if(result.getNum() == features.spellIdentifierInvalid)
|
2023-04-02 18:56:10 +02:00
|
|
|
return SpellID::NONE;
|
2023-08-19 19:48:28 +02:00
|
|
|
assert(result.getNum() < features.spellsCount);
|
2023-04-02 18:56:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor MapReaderH3M::readPlayer()
|
|
|
|
{
|
|
|
|
PlayerColor result(readUInt8());
|
2023-04-03 14:02:15 +02:00
|
|
|
assert(result < PlayerColor::PLAYER_LIMIT || result == PlayerColor::NEUTRAL);
|
2023-04-02 18:56:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor MapReaderH3M::readPlayer32()
|
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
PlayerColor result(readInt32());
|
2023-04-02 18:56:10 +02:00
|
|
|
|
2023-04-03 14:02:15 +02:00
|
|
|
assert(result < PlayerColor::PLAYER_LIMIT || result == PlayerColor::NEUTRAL);
|
2023-04-02 18:56:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-23 15:13:09 +02:00
|
|
|
void MapReaderH3M::readBitmaskBuildings(std::set<BuildingID> & dest, std::optional<FactionID> faction)
|
2023-05-23 14:07:44 +02:00
|
|
|
{
|
|
|
|
std::set<BuildingID> h3m;
|
|
|
|
readBitmask(h3m, features.buildingsBytes, features.buildingsCount, false);
|
|
|
|
|
|
|
|
for (auto const & h3mEntry : h3m)
|
2023-05-23 15:13:09 +02:00
|
|
|
{
|
|
|
|
BuildingID mapped = remapper.remapBuilding(faction, h3mEntry);
|
|
|
|
|
|
|
|
if (mapped != BuildingID::NONE) // artifact merchant may be set in random town, but not present in actual town
|
|
|
|
dest.insert(mapped);
|
|
|
|
}
|
2023-05-23 14:07:44 +02:00
|
|
|
}
|
|
|
|
|
2023-05-23 21:41:21 +02:00
|
|
|
void MapReaderH3M::readBitmaskFactions(std::set<FactionID> & dest, bool invert)
|
|
|
|
{
|
|
|
|
readBitmask(dest, features.factionsBytes, features.factionsCount, invert);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::readBitmaskResources(std::set<GameResID> & dest, bool invert)
|
|
|
|
{
|
|
|
|
readBitmask(dest, features.resourcesBytes, features.resourcesCount, invert);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::readBitmaskHeroClassesSized(std::set<HeroClassID> & dest, bool invert)
|
|
|
|
{
|
|
|
|
uint32_t classesCount = reader->readUInt32();
|
|
|
|
uint32_t classesBytes = (classesCount + 7) / 8;
|
|
|
|
|
|
|
|
readBitmask(dest, classesBytes, classesCount, invert);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::readBitmaskHeroes(std::vector<bool> & dest, bool invert)
|
|
|
|
{
|
2023-05-23 22:55:18 +02:00
|
|
|
readBitmask<HeroTypeID>(dest, features.heroesBytes, features.heroesCount, invert);
|
2023-05-23 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::readBitmaskHeroesSized(std::vector<bool> & dest, bool invert)
|
|
|
|
{
|
|
|
|
uint32_t heroesCount = readUInt32();
|
|
|
|
uint32_t heroesBytes = (heroesCount + 7) / 8;
|
|
|
|
assert(heroesCount <= features.heroesCount);
|
|
|
|
|
2023-05-23 22:55:18 +02:00
|
|
|
readBitmask<HeroTypeID>(dest, heroesBytes, heroesCount, invert);
|
2023-05-23 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::readBitmaskArtifacts(std::vector<bool> &dest, bool invert)
|
|
|
|
{
|
2023-05-23 22:55:18 +02:00
|
|
|
readBitmask<ArtifactID>(dest, features.artifactsBytes, features.artifactsCount, invert);
|
2023-05-23 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::readBitmaskArtifactsSized(std::vector<bool> &dest, bool invert)
|
|
|
|
{
|
|
|
|
uint32_t artifactsCount = reader->readUInt32();
|
|
|
|
uint32_t artifactsBytes = (artifactsCount + 7) / 8;
|
|
|
|
assert(artifactsCount <= features.artifactsCount);
|
|
|
|
|
2023-05-23 22:55:18 +02:00
|
|
|
readBitmask<ArtifactID>(dest, artifactsBytes, artifactsCount, invert);
|
2023-05-23 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::readBitmaskSpells(std::vector<bool> & dest, bool invert)
|
|
|
|
{
|
2023-05-23 22:55:18 +02:00
|
|
|
readBitmask<SpellID>(dest, features.spellsBytes, features.spellsCount, invert);
|
2023-05-23 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::readBitmaskSpells(std::set<SpellID> & dest, bool invert)
|
|
|
|
{
|
|
|
|
readBitmask(dest, features.spellsBytes, features.spellsCount, invert);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::readBitmaskSkills(std::vector<bool> & dest, bool invert)
|
|
|
|
{
|
2023-05-23 22:55:18 +02:00
|
|
|
readBitmask<SecondarySkill>(dest, features.skillsBytes, features.skillsCount, invert);
|
2023-05-23 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::readBitmaskSkills(std::set<SecondarySkill> & dest, bool invert)
|
|
|
|
{
|
|
|
|
readBitmask(dest, features.skillsBytes, features.skillsCount, invert);
|
|
|
|
}
|
|
|
|
|
2023-05-23 22:55:18 +02:00
|
|
|
template<class Identifier>
|
2023-04-03 14:02:15 +02:00
|
|
|
void MapReaderH3M::readBitmask(std::vector<bool> & dest, const int bytesToRead, const int objectsToRead, bool invert)
|
2023-04-02 18:56:10 +02:00
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
for(int byte = 0; byte < bytesToRead; ++byte)
|
2023-04-02 18:56:10 +02:00
|
|
|
{
|
|
|
|
const ui8 mask = reader->readUInt8();
|
|
|
|
for(int bit = 0; bit < 8; ++bit)
|
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
if(byte * 8 + bit < objectsToRead)
|
2023-04-02 18:56:10 +02:00
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
const size_t index = byte * 8 + bit;
|
2023-04-02 18:56:10 +02:00
|
|
|
const bool flag = mask & (1 << bit);
|
2023-04-03 14:02:15 +02:00
|
|
|
const bool result = (flag != invert);
|
2023-05-23 22:55:18 +02:00
|
|
|
|
|
|
|
Identifier h3mID(index);
|
|
|
|
Identifier vcmiID = remapIdentifier(h3mID);
|
|
|
|
|
|
|
|
if (vcmiID.getNum() >= dest.size())
|
|
|
|
dest.resize(vcmiID.getNum() + 1);
|
|
|
|
dest[vcmiID.getNum()] = result;
|
2023-04-02 18:56:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 21:41:21 +02:00
|
|
|
template<class Identifier>
|
|
|
|
void MapReaderH3M::readBitmask(std::set<Identifier> & dest, int bytesToRead, int objectsToRead, bool invert)
|
|
|
|
{
|
|
|
|
std::vector<bool> bitmap;
|
|
|
|
bitmap.resize(objectsToRead, false);
|
2023-05-23 22:55:18 +02:00
|
|
|
readBitmask<Identifier>(bitmap, bytesToRead, objectsToRead, invert);
|
2023-05-23 21:41:21 +02:00
|
|
|
|
|
|
|
for(int i = 0; i < bitmap.size(); i++)
|
|
|
|
if(bitmap[i])
|
|
|
|
dest.insert(static_cast<Identifier>(i));
|
|
|
|
}
|
|
|
|
|
2023-04-02 18:56:10 +02:00
|
|
|
int3 MapReaderH3M::readInt3()
|
|
|
|
{
|
|
|
|
int3 p;
|
|
|
|
p.x = reader->readUInt8();
|
|
|
|
p.y = reader->readUInt8();
|
|
|
|
p.z = reader->readUInt8();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:35:21 +02:00
|
|
|
std::shared_ptr<ObjectTemplate> MapReaderH3M::readObjectTemplate()
|
|
|
|
{
|
|
|
|
auto tmpl = std::make_shared<ObjectTemplate>();
|
|
|
|
tmpl->readMap(*reader);
|
|
|
|
remapper.remapTemplate(*tmpl);
|
|
|
|
return tmpl;
|
|
|
|
}
|
|
|
|
|
2023-04-02 18:56:10 +02:00
|
|
|
void MapReaderH3M::skipUnused(size_t amount)
|
|
|
|
{
|
|
|
|
reader->skip(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapReaderH3M::skipZero(size_t amount)
|
|
|
|
{
|
|
|
|
#ifdef NDEBUG
|
|
|
|
skipUnused(amount);
|
|
|
|
#else
|
2023-04-03 14:02:15 +02:00
|
|
|
for(size_t i = 0; i < amount; ++i)
|
2023-04-02 18:56:10 +02:00
|
|
|
{
|
|
|
|
uint8_t value = reader->readUInt8();
|
|
|
|
assert(value == 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-04-03 14:02:15 +02:00
|
|
|
void MapReaderH3M::readResourses(TResources & resources)
|
2023-04-02 18:56:10 +02:00
|
|
|
{
|
|
|
|
for(int x = 0; x < features.resourcesCount; ++x)
|
2023-04-03 14:02:15 +02:00
|
|
|
resources[x] = reader->readInt32();
|
2023-04-02 18:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MapReaderH3M::readBool()
|
|
|
|
{
|
|
|
|
uint8_t result = readUInt8();
|
|
|
|
assert(result == 0 || result == 1);
|
|
|
|
|
|
|
|
return result != 0;
|
|
|
|
}
|
|
|
|
|
2023-04-03 14:02:15 +02:00
|
|
|
ui8 MapReaderH3M::readUInt8()
|
2023-04-02 18:56:10 +02:00
|
|
|
{
|
|
|
|
return reader->readUInt8();
|
|
|
|
}
|
|
|
|
|
2023-04-03 14:02:15 +02:00
|
|
|
si8 MapReaderH3M::readInt8()
|
2023-04-02 18:56:10 +02:00
|
|
|
{
|
|
|
|
return reader->readInt8();
|
|
|
|
}
|
|
|
|
|
|
|
|
ui16 MapReaderH3M::readUInt16()
|
|
|
|
{
|
|
|
|
return reader->readUInt16();
|
|
|
|
}
|
|
|
|
|
|
|
|
si16 MapReaderH3M::readInt16()
|
|
|
|
{
|
|
|
|
return reader->readInt16();
|
|
|
|
}
|
|
|
|
|
|
|
|
ui32 MapReaderH3M::readUInt32()
|
|
|
|
{
|
|
|
|
return reader->readUInt32();
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 MapReaderH3M::readInt32()
|
|
|
|
{
|
|
|
|
return reader->readInt32();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MapReaderH3M::readBaseString()
|
|
|
|
{
|
|
|
|
return reader->readBaseString();
|
|
|
|
}
|
|
|
|
|
|
|
|
CBinaryReader & MapReaderH3M::getInternalReader()
|
|
|
|
{
|
|
|
|
return *reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|