1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Removed buggy and poorly designed fromString method

Use VLC->modh directly with proper parameters instead
This commit is contained in:
Ivan Savenko 2023-07-25 22:36:45 +03:00
parent 1335a834e2
commit e733b55c90
8 changed files with 16 additions and 46 deletions

View File

@ -305,44 +305,14 @@ bool operator<(const BattleField & l, const BattleField & r)
return l.num < r.num; return l.num < r.num;
} }
BattleField::operator std::string() const
{
return getInfo()->identifier;
}
const BattleFieldInfo * BattleField::getInfo() const const BattleFieldInfo * BattleField::getInfo() const
{ {
return VLC->battlefields()->getById(*this); return VLC->battlefields()->getById(*this);
} }
BattleField BattleField::fromString(const std::string & identifier)
{
auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeBuiltin(), "battlefield", identifier);
if(rawId)
return BattleField(rawId.value());
else
return BattleField::NONE;
}
const ObstacleInfo * Obstacle::getInfo() const const ObstacleInfo * Obstacle::getInfo() const
{ {
return VLC->obstacles()->getById(*this); return VLC->obstacles()->getById(*this);
} }
Obstacle::operator std::string() const
{
return getInfo()->identifier;
}
Obstacle Obstacle::fromString(const std::string & identifier)
{
auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeBuiltin(), "obstacle", identifier);
if(rawId)
return Obstacle(rawId.value());
else
return Obstacle(-1);
}
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -1292,10 +1292,7 @@ class BattleField : public BaseForID<BattleField, si32>
DLL_LINKAGE friend bool operator!=(const BattleField & l, const BattleField & r); DLL_LINKAGE friend bool operator!=(const BattleField & l, const BattleField & r);
DLL_LINKAGE friend bool operator<(const BattleField & l, const BattleField & r); DLL_LINKAGE friend bool operator<(const BattleField & l, const BattleField & r);
DLL_LINKAGE operator std::string() const;
DLL_LINKAGE const BattleFieldInfo * getInfo() const; DLL_LINKAGE const BattleFieldInfo * getInfo() const;
DLL_LINKAGE static BattleField fromString(const std::string & identifier);
}; };
enum class EBoatId : int32_t enum class EBoatId : int32_t
@ -1336,8 +1333,6 @@ class Obstacle : public BaseForID<Obstacle, si32>
INSTID_LIKE_CLASS_COMMON(Obstacle, si32) INSTID_LIKE_CLASS_COMMON(Obstacle, si32)
DLL_LINKAGE const ObstacleInfo * getInfo() const; DLL_LINKAGE const ObstacleInfo * getInfo() const;
DLL_LINKAGE operator std::string() const;
DLL_LINKAGE static Obstacle fromString(const std::string & identifier);
}; };
enum class ESpellSchool: int8_t enum class ESpellSchool: int8_t

View File

@ -1244,12 +1244,11 @@ BattleField CGameState::battleGetBattlefieldType(int3 tile, CRandomGenerator & r
} }
if(map->isCoastalTile(tile)) //coastal tile is always ground if(map->isCoastalTile(tile)) //coastal tile is always ground
return BattleField::fromString("sand_shore"); return BattleField(*VLC->modh->identifiers.getIdentifier("core", "battlefield", "sand_shore"));
return BattleField(*RandomGeneratorUtil::nextItem(t.terType->battleFields, rand)); return BattleField(*RandomGeneratorUtil::nextItem(t.terType->battleFields, rand));
} }
void CGameState::fillUpgradeInfo(const CArmedInstance *obj, SlotID stackPos, UpgradeInfo &out) const void CGameState::fillUpgradeInfo(const CArmedInstance *obj, SlotID stackPos, UpgradeInfo &out) const
{ {
assert(obj); assert(obj);

View File

@ -13,6 +13,7 @@
#include "IObjectInfo.h" #include "IObjectInfo.h"
#include "../CGeneralTextHandler.h" #include "../CGeneralTextHandler.h"
#include "../CModHandler.h"
#include "../VCMI_Lib.h" #include "../VCMI_Lib.h"
#include "../mapObjects/CGObjectInstance.h" #include "../mapObjects/CGObjectInstance.h"
#include "../mapObjects/ObjectTemplate.h" #include "../mapObjects/ObjectTemplate.h"
@ -95,10 +96,15 @@ void AObjectTypeHandler::init(const JsonNode & input)
else else
aiValue = static_cast<std::optional<si32>>(input["aiValue"].Integer()); aiValue = static_cast<std::optional<si32>>(input["aiValue"].Integer());
if(input["battleground"].getType() == JsonNode::JsonType::DATA_STRING) battlefield = BattleField::NONE;
battlefield = input["battleground"].String();
else if(!input["battleground"].isNull())
battlefield = std::nullopt; {
VLC->modh->identifiers.requestIdentifier("battlefield", input["battleground"], [this](int32_t identifier)
{
battlefield = BattleField(identifier);
});
}
initTypeData(input); initTypeData(input);
} }
@ -175,7 +181,7 @@ std::vector<std::shared_ptr<const ObjectTemplate>> AObjectTypeHandler::getTempla
BattleField AObjectTypeHandler::getBattlefield() const BattleField AObjectTypeHandler::getBattlefield() const
{ {
return battlefield ? BattleField::fromString(battlefield.value()) : BattleField::NONE; return battlefield;
} }
std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getTemplates(TerrainId terrainType) const std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getTemplates(TerrainId terrainType) const

View File

@ -34,7 +34,7 @@ class DLL_LINKAGE AObjectTypeHandler : public boost::noncopyable
SObjectSounds sounds; SObjectSounds sounds;
std::optional<si32> aiValue; std::optional<si32> aiValue;
std::optional<std::string> battlefield; BattleField battlefield;
std::string modScope; std::string modScope;
std::string typeName; std::string typeName;

View File

@ -2113,7 +2113,7 @@ void CGameHandler::setupBattle(int3 tile, const CArmedInstance *armies[2], const
BattleField terType = gs->battleGetBattlefieldType(tile, getRandomGenerator()); BattleField terType = gs->battleGetBattlefieldType(tile, getRandomGenerator());
if (heroes[0] && heroes[0]->boat && heroes[1] && heroes[1]->boat) if (heroes[0] && heroes[0]->boat && heroes[1] && heroes[1]->boat)
terType = BattleField::fromString("ship_to_ship"); terType = BattleField(*VLC->modh->identifiers.getIdentifier("core", "battlefield", "ship_to_ship"));
//send info about battles //send info about battles
BattleStart bs; BattleStart bs;

View File

@ -195,7 +195,7 @@ public:
const auto & t = *gameCallback->getTile(tile); const auto & t = *gameCallback->getTile(tile);
auto terrain = t.terType->getId(); auto terrain = t.terType->getId();
BattleField terType = BattleField::fromString("grass_hills"); BattleField terType(0);
//send info about battles //send info about battles

View File

@ -94,7 +94,7 @@ void BattleFake::setupEmptyBattlefield()
{ {
EXPECT_CALL(*this, getDefendedTown()).WillRepeatedly(Return(nullptr)); EXPECT_CALL(*this, getDefendedTown()).WillRepeatedly(Return(nullptr));
EXPECT_CALL(*this, getAllObstacles()).WillRepeatedly(Return(IBattleInfo::ObstacleCList())); EXPECT_CALL(*this, getAllObstacles()).WillRepeatedly(Return(IBattleInfo::ObstacleCList()));
EXPECT_CALL(*this, getBattlefieldType()).WillRepeatedly(Return(BattleField::fromString("grass_hills"))); EXPECT_CALL(*this, getBattlefieldType()).WillRepeatedly(Return(BattleField(0)));
} }
#if SCRIPTING_ENABLED #if SCRIPTING_ENABLED