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-04-05 13:08:41 +02:00
|
|
|
#include "CMap.h"
|
2023-04-02 18:56:10 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
MapReaderH3M::MapReaderH3M(CInputStream * stream)
|
|
|
|
: reader(std::make_unique<CBinaryReader>(stream))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-03 19:51:02 +02:00
|
|
|
void MapReaderH3M::setFormatLevel(EMapFormat newFormat, uint8_t hotaVersion)
|
2023-04-02 18:56:10 +02:00
|
|
|
{
|
2023-04-03 19:51:02 +02:00
|
|
|
features = MapFormatFeaturesH3M::find(newFormat, hotaVersion);
|
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-04-03 14:02:15 +02:00
|
|
|
if(result == features.artifactIdentifierInvalid)
|
2023-04-02 18:56:10 +02:00
|
|
|
return ArtifactID::NONE;
|
|
|
|
|
2023-05-20 14:09:53 +02:00
|
|
|
if (result < features.artifactsCount)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
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-05-20 14:09:53 +02:00
|
|
|
if (result < features.artifactsCount)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
assert(result.getNum() < features.heroesPortraitsCount);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-03 14:02:15 +02:00
|
|
|
if(result == features.creatureIdentifierInvalid)
|
2023-04-02 18:56:10 +02:00
|
|
|
return CreatureID::NONE;
|
|
|
|
|
2023-05-20 14:09:53 +02:00
|
|
|
if(result < features.creaturesCount)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// this may be random creature in army/town, to be randomized later
|
|
|
|
CreatureID randomIndex(result.getNum() - features.creatureIdentifierInvalid - 1);
|
|
|
|
assert(randomIndex < CreatureID::NONE);
|
|
|
|
|
|
|
|
if (randomIndex > -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-04-02 18:56:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
RoadId MapReaderH3M::readRoad()
|
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
RoadId result(readInt8());
|
|
|
|
assert(result < Road::ORIGINAL_ROAD_COUNT);
|
2023-04-02 18:56:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
RiverId MapReaderH3M::readRiver()
|
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
RiverId result(readInt8());
|
|
|
|
assert(result < River::ORIGINAL_RIVER_COUNT);
|
2023-04-02 18:56:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SecondarySkill MapReaderH3M::readSkill()
|
|
|
|
{
|
|
|
|
SecondarySkill result(readUInt8());
|
2023-04-03 14:02:15 +02:00
|
|
|
assert(result < features.skillsCount);
|
2023-04-02 18:56:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpellID MapReaderH3M::readSpell()
|
|
|
|
{
|
|
|
|
SpellID result(readUInt8());
|
2023-04-03 14:02:15 +02:00
|
|
|
if(result == features.spellIdentifierInvalid)
|
2023-04-02 18:56:10 +02:00
|
|
|
return SpellID::NONE;
|
2023-04-03 14:02:15 +02:00
|
|
|
if(result == features.spellIdentifierInvalid - 1)
|
2023-04-02 18:56:10 +02:00
|
|
|
return SpellID::PRESET;
|
|
|
|
|
2023-04-03 14:02:15 +02:00
|
|
|
assert(result < features.spellsCount);
|
2023-04-02 18:56:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpellID MapReaderH3M::readSpell32()
|
|
|
|
{
|
2023-04-03 14:02:15 +02:00
|
|
|
SpellID result(readInt32());
|
|
|
|
if(result == features.spellIdentifierInvalid)
|
2023-04-02 18:56:10 +02:00
|
|
|
return SpellID::NONE;
|
2023-04-03 14:02:15 +02:00
|
|
|
assert(result < 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-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);
|
|
|
|
dest[index] = result;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|