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

First compilable commit for obstacles

This commit is contained in:
nordsoft 2022-08-28 02:45:13 +04:00
parent b0526ca51b
commit 756793992f
15 changed files with 239 additions and 115 deletions

View File

@ -42,6 +42,7 @@
#include "../../lib/spells/Problem.h"
#include "../../lib/CTownHandler.h"
#include "../../lib/BattleFieldHandler.h"
#include "../../lib/ObstacleHandler.h"
#include "../../lib/CGameState.h"
#include "../../lib/mapping/CMap.h"
#include "../../lib/NetPacks.h"

View File

@ -20,6 +20,7 @@ class HeroTypeService;
class SkillService;
class JsonNode;
class BattleFieldService;
class ObstacleService;
namespace spells
{
@ -50,6 +51,7 @@ public:
virtual const spells::Service * spells() const = 0;
virtual const SkillService * skills() const = 0;
virtual const BattleFieldService * battlefields() const = 0;
virtual const ObstacleService * obstacles() const = 0;
virtual void updateEntity(Metatype metatype, int32_t index, const JsonNode & data) = 0;

View File

@ -1,5 +1,5 @@
/*
* BattleFieldHandler.h, part of VCMI engine
* BattleFieldHandler.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
@ -105,4 +105,4 @@ void BattleFieldInfo::registerIcons(const IconRegistar & cb) const
BattleField BattleFieldInfo::getId() const
{
return battlefield;
}
}

View File

@ -153,41 +153,6 @@ CHeroClass::CHeroClass()
{
}
std::vector<BattleHex> CObstacleInfo::getBlocked(BattleHex hex) const
{
std::vector<BattleHex> ret;
if(isAbsoluteObstacle)
{
assert(!hex.isValid());
range::copy(blockedTiles, std::back_inserter(ret));
return ret;
}
for(int offset : blockedTiles)
{
BattleHex toBlock = hex + offset;
if((hex.getY() & 1) && !(toBlock.getY() & 1))
toBlock += BattleHex::LEFT;
if(!toBlock.isValid())
logGlobal->error("Misplaced obstacle!");
else
ret.push_back(toBlock);
}
return ret;
}
bool CObstacleInfo::isAppropriate(const Terrain & terrainType, const BattleField & battlefield) const
{
auto bgInfo = battlefield.getInfo();
if(bgInfo->isSpecial)
return vstd::contains(allowedSpecialBfields, bgInfo->identifier);
return vstd::contains(allowedTerrains, terrainType);
}
void CHeroClassHandler::fillPrimarySkillData(const JsonNode & node, CHeroClass * heroClass, PrimarySkill::PrimarySkill pSkill)
{
const auto & skillName = PrimarySkill::names[pSkill];
@ -378,7 +343,6 @@ CHeroHandler::~CHeroHandler() = default;
CHeroHandler::CHeroHandler()
{
loadObstacles();
loadTerrains();
for(int i = 0; i < Terrain::Manager::terrains().size(); ++i)
{
@ -818,39 +782,6 @@ void CHeroHandler::loadExperience()
expPerLevel.pop_back();//last value is broken
}
void CHeroHandler::loadObstacles()
{
auto loadObstacles = [](const JsonNode & node, bool absolute, std::vector<CObstacleInfo> & out)
{
for(const JsonNode &obs : node.Vector())
{
out.emplace_back();
CObstacleInfo & obi = out.back();
obi.defName = obs["defname"].String();
obi.width = static_cast<si32>(obs["width"].Float());
obi.height = static_cast<si32>(obs["height"].Float());
for(auto & t : obs["allowedTerrain"].Vector())
obi.allowedTerrains.emplace_back(t.String());
for(auto & t : obs["specialBattlefields"].Vector())
obi.allowedSpecialBfields.emplace_back(t.String());
obi.blockedTiles = obs["blockedTiles"].convertTo<std::vector<si16> >();
obi.isAbsoluteObstacle = absolute;
}
};
auto allConfigs = VLC->modh->getActiveMods();
allConfigs.insert(allConfigs.begin(), "core");
for(auto & mod : allConfigs)
{
if(!CResourceHandler::get(mod)->existsResource(ResourceID("config/obstacles.json")))
continue;
const JsonNode config(mod, ResourceID("config/obstacles.json"));
loadObstacles(config["obstacles"], false, obstacles);
loadObstacles(config["absoluteObstacles"], true, absoluteObstacles);
}
}
/// convert h3-style ID (e.g. Gobin Wolf Rider) to vcmi (e.g. goblinWolfRider)
static std::string genRefName(std::string input)
{

View File

@ -223,32 +223,6 @@ public:
EAlignment::EAlignment getAlignment() const;
};
struct DLL_LINKAGE CObstacleInfo
{
std::string defName;
std::vector<Terrain> allowedTerrains;
std::vector<std::string> allowedSpecialBfields;
ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)
std::vector<BattleHex> getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex'
bool isAppropriate(const Terrain & terrainType, const BattleField & specialBattlefield) const;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & defName;
h & allowedTerrains;
h & allowedSpecialBfields;
h & isAbsoluteObstacle;
h & width;
h & height;
h & blockedTiles;
}
};
class DLL_LINKAGE CHeroClassHandler : public CHandlerBase<HeroClassID, HeroClass, CHeroClass, HeroClassService>
{
void fillPrimarySkillData(const JsonNode & node, CHeroClass * heroClass, PrimarySkill::PrimarySkill pSkill);
@ -286,7 +260,6 @@ class DLL_LINKAGE CHeroHandler : public CHandlerBase<HeroTypeID, HeroType, CHero
void loadExperience();
void loadBallistics();
void loadTerrains();
void loadObstacles();
public:
CHeroClassHandler classes;
@ -315,9 +288,6 @@ public:
};
std::vector<SBallisticsLevelInfo> ballistics; //info about ballistics ability per level; [0] - none; [1] - basic; [2] - adv; [3] - expert
std::vector<CObstacleInfo> obstacles; //info about obstacles that may be placed on battlefield
std::vector<CObstacleInfo> absoluteObstacles; //info about obstacles that may be placed on battlefield
ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount
ui64 reqExp(ui32 level) const; //calculates experience required for given level
@ -340,8 +310,6 @@ public:
h & expPerLevel;
h & ballistics;
h & terrCosts;
h & obstacles;
h & absoluteObstacles;
}
protected:

View File

@ -35,6 +35,7 @@
#include "CGeneralTextHandler.h"
#include "CModHandler.h"//todo: remove
#include "BattleFieldHandler.h"
#include "ObstacleHandler.h"
const SlotID SlotID::COMMANDER_SLOT_PLACEHOLDER = SlotID(-2);
const SlotID SlotID::SUMMONED_SLOT_PLACEHOLDER = SlotID(-3);
@ -292,4 +293,19 @@ BattleField BattleField::fromString(std::string identifier)
return BattleField(rawId.get());
else
return BattleField::NONE;
}
}
Obstacle::operator std::string() const
{
return VLC->obstacles()->getById(*this)->identifier;
}
Obstacle Obstacle::fromString(std::string identifier)
{
auto rawId = VLC->modh->identifiers.getIdentifier("core", "obstacle", identifier);
if(rawId)
return Obstacle(rawId.get());
else
return Obstacle(-1);
}

View File

@ -1125,6 +1125,16 @@ class BattleField : public BaseForID<BattleField, si32>
DLL_LINKAGE static BattleField fromString(std::string identifier);
};
class Obstacle : public BaseForID<Obstacle, si32>
{
INSTID_LIKE_CLASS_COMMON(Obstacle, si32)
///json serialization helpers
DLL_LINKAGE operator std::string() const;
DLL_LINKAGE static Obstacle fromString(std::string identifier);
};
enum class ESpellSchool: ui8
{

View File

@ -17,6 +17,7 @@
#include "CBonusTypeHandler.h"
#include "CModHandler.h"
#include "BattleFieldHandler.h"
#include "ObstacleHandler.h"
#include "serializer/CSerializer.h" // for SAVEGAME_MAGIC
#include "serializer/BinaryDeserializer.h"

110
lib/ObstacleHandler.cpp Normal file
View File

@ -0,0 +1,110 @@
/*
* ObstacleHandler.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 "ObstacleHandler.h"
#include "BattleFieldHandler.h"
int32_t ObstacleInfo::getIndex() const
{
return obstacle.getNum();
}
int32_t ObstacleInfo::getIconIndex() const
{
return iconIndex;
}
const std::string & ObstacleInfo::getName() const
{
return name;
}
const std::string & ObstacleInfo::getJsonKey() const
{
return identifier;
}
void ObstacleInfo::registerIcons(const IconRegistar & cb) const
{
}
Obstacle ObstacleInfo::getId() const
{
return obstacle;
}
std::vector<BattleHex> ObstacleInfo::getBlocked(BattleHex hex) const
{
std::vector<BattleHex> ret;
if(isAbsoluteObstacle)
{
assert(!hex.isValid());
range::copy(blockedTiles, std::back_inserter(ret));
return ret;
}
for(int offset : blockedTiles)
{
BattleHex toBlock = hex + offset;
if((hex.getY() & 1) && !(toBlock.getY() & 1))
toBlock += BattleHex::LEFT;
if(!toBlock.isValid())
logGlobal->error("Misplaced obstacle!");
else
ret.push_back(toBlock);
}
return ret;
}
bool ObstacleInfo::isAppropriate(const Terrain & terrainType, const BattleField & battlefield) const
{
auto bgInfo = battlefield.getInfo();
if(bgInfo->isSpecial)
return vstd::contains(allowedSpecialBfields, bgInfo->identifier);
return vstd::contains(allowedTerrains, terrainType);
}
void ObstacleHandler::loadObstacles()
{
auto loadObstacles = [](const JsonNode & node, bool absolute, std::vector<ObstacleInfo> & out)
{
for(const JsonNode &obs : node.Vector())
{
out.emplace_back();
ObstacleInfo & obi = out.back();
obi.defName = obs["defname"].String();
obi.width = static_cast<si32>(obs["width"].Float());
obi.height = static_cast<si32>(obs["height"].Float());
for(auto & t : obs["allowedTerrain"].Vector())
obi.allowedTerrains.emplace_back(t.String());
for(auto & t : obs["specialBattlefields"].Vector())
obi.allowedSpecialBfields.emplace_back(t.String());
obi.blockedTiles = obs["blockedTiles"].convertTo<std::vector<si16> >();
obi.isAbsoluteObstacle = absolute;
}
};
//auto allConfigs = VLC->modh->getActiveMods();
//allConfigs.insert(allConfigs.begin(), "core");
/*for(auto & mod : allConfigs)
{
if(!CResourceHandler::get(mod)->existsResource(ResourceID("config/obstacles.json")))
continue;
const JsonNode config(mod, ResourceID("config/obstacles.json"));
loadObstacles(config["obstacles"], false, obstacles);
loadObstacles(config["absoluteObstacles"], true, absoluteObstacles);
}*/
}

73
lib/ObstacleHandler.h Normal file
View File

@ -0,0 +1,73 @@
/*
* ObstacleHandler.h, 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
*
*/
#pragma once
#include <vcmi/EntityService.h>
#include <vcmi/Entity.h>
#include "GameConstants.h"
#include "IHandlerBase.h"
#include "Terrain.h"
#include "battle/BattleHex.h"
struct DLL_LINKAGE ObstacleInfo : public EntityT<Obstacle>
{
Obstacle obstacle;
si32 iconIndex;
std::string name;
std::string identifier;
std::string defName;
std::vector<Terrain> allowedTerrains;
std::vector<std::string> allowedSpecialBfields;
ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)
int32_t getIndex() const override;
int32_t getIconIndex() const override;
const std::string & getJsonKey() const override;
const std::string & getName() const override;
void registerIcons(const IconRegistar & cb) const override;
Obstacle getId() const override;
std::vector<BattleHex> getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex'
bool isAppropriate(const Terrain & terrainType, const BattleField & specialBattlefield) const;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & defName;
h & allowedTerrains;
h & allowedSpecialBfields;
h & isAbsoluteObstacle;
h & width;
h & height;
h & blockedTiles;
}
};
class DLL_LINKAGE ObstacleService : public EntityServiceT<Obstacle, ObstacleInfo>
{
public:
};
class ObstacleHandler: public CHandlerBase<Obstacle, ObstacleInfo, ObstacleInfo, ObstacleService>
{
public:
void loadObstacles();
std::vector<ObstacleInfo> obstacles; //info about obstacles that may be placed on battlefield
std::vector<ObstacleInfo> absoluteObstacles; //info about obstacles that may be placed on battlefield
template <typename Handler> void serialize(Handler & h, const int version)
{
h & objects;
}
};

View File

@ -33,6 +33,7 @@
#include "mapping/CMapEditManager.h"
#include "ScriptHandler.h"
#include "BattleFieldHandler.h"
#include "ObstacleHandler.h"
LibClasses * VLC = nullptr;
@ -116,6 +117,11 @@ const BattleFieldService * LibClasses::battlefields() const
return battlefieldsHandler;
}
const ObstacleService * LibClasses::obstacles() const
{
return obstacleHandler;
}
void LibClasses::updateEntity(Metatype metatype, int32_t index, const JsonNode & data)
{
switch(metatype)

View File

@ -27,6 +27,7 @@ class CContentHandler;
class BattleFieldHandler;
class IBonusTypeHandler;
class CBonusTypeHandler;
class ObstacleHandler;
class CTerrainViewPatternConfig;
class CRmgTemplateStorage;
class IHandlerBase;
@ -58,6 +59,7 @@ public:
const spells::Service * spells() const override;
const SkillService * skills() const override;
const BattleFieldService * battlefields() const override;
const ObstacleService * obstacles() const override;
void updateEntity(Metatype metatype, int32_t index, const JsonNode & data) override;
@ -79,6 +81,7 @@ public:
CTerrainViewPatternConfig * terviewh;
CRmgTemplateStorage * tplh;
BattleFieldHandler * battlefieldsHandler;
ObstacleHandler * obstacleHandler;
scripting::ScriptHandler * scriptHandler;
LibClasses(); //c-tor, loads .lods and NULLs handlers
@ -108,6 +111,7 @@ public:
h & spellh;
h & skillh;
h & battlefieldsHandler;
h & obstacleHandler;
if(!h.saving)
{

View File

@ -17,6 +17,7 @@
#include "../CGeneralTextHandler.h"
#include "../Terrain.h"
#include "../BattleFieldHandler.h"
#include "../ObstacleHandler.h"
//TODO: remove
#include "../IGameCallback.h"
@ -240,8 +241,8 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, const Terrain & terrain,
//randomize obstacles
if (town == nullptr && !creatureBank) //do it only when it's not siege and not creature bank
{
const int ABSOLUTE_OBSTACLES_COUNT = VLC->heroh->absoluteObstacles.size();
const int USUAL_OBSTACLES_COUNT = VLC->heroh->obstacles.size(); //shouldn't be changes if we want H3-like obstacle placement
const int ABSOLUTE_OBSTACLES_COUNT = VLC->obstacleHandler->absoluteObstacles.size();
const int USUAL_OBSTACLES_COUNT = VLC->obstacleHandler->obstacles.size(); //shouldn't be changes if we want H3-like obstacle placement
RandGen r;
auto ourRand = [&](){ return r.rand(); };
@ -253,11 +254,11 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, const Terrain & terrain,
auto appropriateAbsoluteObstacle = [&](int id)
{
return VLC->heroh->absoluteObstacles[id].isAppropriate(curB->terrainType, battlefieldType);
return VLC->obstacleHandler->absoluteObstacles[id].isAppropriate(curB->terrainType, battlefieldType);
};
auto appropriateUsualObstacle = [&](int id) -> bool
{
return VLC->heroh->obstacles[id].isAppropriate(curB->terrainType, battlefieldType);
return VLC->obstacleHandler->obstacles[id].isAppropriate(curB->terrainType, battlefieldType);
};
if(r.rand(1,100) <= 40) //put cliff-like obstacle
@ -274,7 +275,7 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, const Terrain & terrain,
for(BattleHex blocked : obstPtr->getBlockedTiles())
blockedTiles.push_back(blocked);
tilesToBlock -= (int)VLC->heroh->absoluteObstacles[obstPtr->ID].blockedTiles.size() / 2;
tilesToBlock -= (int)VLC->obstacleHandler->absoluteObstacles[obstPtr->ID].blockedTiles.size() / 2;
}
catch(RangeGenerator::ExhaustedPossibilities &)
{
@ -290,7 +291,7 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, const Terrain & terrain,
{
auto tileAccessibility = curB->getAccesibility();
const int obid = obidgen.getSuchNumber(appropriateUsualObstacle);
const CObstacleInfo &obi = VLC->heroh->obstacles[obid];
const ObstacleInfo &obi = VLC->obstacleHandler->obstacles[obid];
auto validPosition = [&](BattleHex pos) -> bool
{

View File

@ -11,6 +11,7 @@
#include "CObstacleInstance.h"
#include "../CHeroHandler.h"
#include "../CTownHandler.h"
#include "../ObstacleHandler.h"
#include "../VCMI_Lib.h"
#include "../NetPacksBase.h"
@ -29,14 +30,14 @@ CObstacleInstance::~CObstacleInstance()
}
const CObstacleInfo & CObstacleInstance::getInfo() const
const ObstacleInfo & CObstacleInstance::getInfo() const
{
switch(obstacleType)
{
case ABSOLUTE_OBSTACLE:
return VLC->heroh->absoluteObstacles[ID];
return VLC->obstacleHandler->absoluteObstacles[ID];
case USUAL:
return VLC->heroh->obstacles[ID];
return VLC->obstacleHandler->obstacles[ID];
default:
throw std::runtime_error("Unknown obstacle type in CObstacleInstance::getInfo()");
}

View File

@ -10,7 +10,7 @@
#pragma once
#include "BattleHex.h"
struct CObstacleInfo;
struct ObstacleInfo;
class ObstacleChanges;
class JsonSerializeFormat;
@ -30,7 +30,7 @@ struct DLL_LINKAGE CObstacleInstance
CObstacleInstance();
virtual ~CObstacleInstance();
const CObstacleInfo &getInfo() const; //allowed only when not generated by spell (usual or absolute)
const ObstacleInfo &getInfo() const; //allowed only when not generated by spell (usual or absolute)
std::vector<BattleHex> getBlockedTiles() const;
std::vector<BattleHex> getStoppingTile() const; //hexes that will stop stack move