1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-27 22:49:25 +02:00

Cleanup and formatting of H3M loader code

This commit is contained in:
Ivan Savenko
2023-04-03 15:02:15 +03:00
parent 3738171b21
commit f93335d678
5 changed files with 110 additions and 110 deletions

View File

@@ -10,6 +10,7 @@
#include "StdInc.h"
#include "MapReaderH3M.h"
#include "CMap.h"
#include "../filesystem/CBinaryReader.h"
@@ -29,12 +30,12 @@ ArtifactID MapReaderH3M::readArtifact()
{
ArtifactID result;
if (features.levelAB)
if(features.levelAB)
result = ArtifactID(reader->readUInt16());
else
result = ArtifactID(reader->readUInt8());
if (result == features.artifactIdentifierInvalid)
if(result == features.artifactIdentifierInvalid)
return ArtifactID::NONE;
assert(result < features.artifactsCount);
@@ -43,9 +44,9 @@ ArtifactID MapReaderH3M::readArtifact()
HeroTypeID MapReaderH3M::readHero()
{
HeroTypeID result (reader->readUInt8());
HeroTypeID result(reader->readUInt8());
if (result.getNum() == features.heroIdentifierInvalid)
if(result.getNum() == features.heroIdentifierInvalid)
return HeroTypeID(-1);
assert(result.getNum() < features.heroesPortraitsCount);
@@ -56,105 +57,103 @@ CreatureID MapReaderH3M::readCreature()
{
CreatureID result;
if (features.levelAB)
if(features.levelAB)
result = CreatureID(reader->readUInt16());
else
result = CreatureID(reader->readUInt8());
if (result == features.creatureIdentifierInvalid)
if(result == features.creatureIdentifierInvalid)
return CreatureID::NONE;
if(result > features.creaturesCount)
{
// this may be random creature in army/town, to be randomized later
CreatureID randomIndex (result.getNum() - features.creatureIdentifierInvalid - 1);
CreatureID randomIndex(result.getNum() - features.creatureIdentifierInvalid - 1);
assert(randomIndex < CreatureID::NONE);
assert(randomIndex > -16);
return randomIndex;
}
return result;
}
TerrainId MapReaderH3M::readTerrain()
{
TerrainId result(readUInt8());
assert (result.getNum() < features.terrainsCount);
assert(result.getNum() < features.terrainsCount);
return result;
}
RoadId MapReaderH3M::readRoad()
{
RoadId result(readUInt8());
assert (result < Road::ORIGINAL_ROAD_COUNT);
RoadId result(readInt8());
assert(result < Road::ORIGINAL_ROAD_COUNT);
return result;
}
RiverId MapReaderH3M::readRiver()
{
RiverId result(readUInt8());
assert (result < River::ORIGINAL_RIVER_COUNT);
RiverId result(readInt8());
assert(result < River::ORIGINAL_RIVER_COUNT);
return result;
}
SecondarySkill MapReaderH3M::readSkill()
{
SecondarySkill result(readUInt8());
assert (result < features.skillsCount);
assert(result < features.skillsCount);
return result;
}
SpellID MapReaderH3M::readSpell()
{
SpellID result(readUInt8());
if (result == features.spellIdentifierInvalid)
if(result == features.spellIdentifierInvalid)
return SpellID::NONE;
if (result == features.spellIdentifierInvalid - 1)
if(result == features.spellIdentifierInvalid - 1)
return SpellID::PRESET;
assert (result < features.spellsCount);
assert(result < features.spellsCount);
return result;
}
SpellID MapReaderH3M::readSpell32()
{
SpellID result(readUInt32());
if (result == features.spellIdentifierInvalid)
SpellID result(readInt32());
if(result == features.spellIdentifierInvalid)
return SpellID::NONE;
assert (result < features.spellsCount);
assert(result < features.spellsCount);
return result;
}
PlayerColor MapReaderH3M::readPlayer()
{
PlayerColor result(readUInt8());
assert (result < PlayerColor::PLAYER_LIMIT || result == PlayerColor::NEUTRAL);
assert(result < PlayerColor::PLAYER_LIMIT || result == PlayerColor::NEUTRAL);
return result;
}
PlayerColor MapReaderH3M::readPlayer32()
{
PlayerColor result(readUInt32());
PlayerColor result(readInt32());
assert (result < PlayerColor::PLAYER_LIMIT || result == PlayerColor::NEUTRAL);
assert(result < PlayerColor::PLAYER_LIMIT || result == PlayerColor::NEUTRAL);
return result;
}
void MapReaderH3M::readBitmask(std::vector<bool> & dest, const int byteCount, const int limit, bool negate)
void MapReaderH3M::readBitmask(std::vector<bool> & dest, const int bytesToRead, const int objectsToRead, bool invert)
{
for(int byte = 0; byte < byteCount; ++byte)
for(int byte = 0; byte < bytesToRead; ++byte)
{
const ui8 mask = reader->readUInt8();
for(int bit = 0; bit < 8; ++bit)
{
if(byte * 8 + bit < limit)
if(byte * 8 + bit < objectsToRead)
{
const size_t index = byte * 8 + bit;
const bool flag = mask & (1 << bit);
if((negate && flag) || (!negate && !flag)) // FIXME: check PR388
dest[byte * 8 + bit] = false;
const bool result = (flag != invert);
dest[index] = result;
}
}
}
@@ -179,7 +178,7 @@ void MapReaderH3M::skipZero(size_t amount)
#ifdef NDEBUG
skipUnused(amount);
#else
for (size_t i = 0; i < amount; ++i)
for(size_t i = 0; i < amount; ++i)
{
uint8_t value = reader->readUInt8();
assert(value == 0);
@@ -187,10 +186,10 @@ void MapReaderH3M::skipZero(size_t amount)
#endif
}
void MapReaderH3M::readResourses(TResources& resources)
void MapReaderH3M::readResourses(TResources & resources)
{
for(int x = 0; x < features.resourcesCount; ++x)
resources[x] = reader->readUInt32();
resources[x] = reader->readInt32();
}
bool MapReaderH3M::readBool()
@@ -201,12 +200,12 @@ bool MapReaderH3M::readBool()
return result != 0;
}
ui8 MapReaderH3M::readUInt8()
ui8 MapReaderH3M::readUInt8()
{
return reader->readUInt8();
}
si8 MapReaderH3M::readInt8()
si8 MapReaderH3M::readInt8()
{
return reader->readInt8();
}