mirror of
https://github.com/vcmi/vcmi.git
synced 2025-06-15 00:05:02 +02:00
Creature set serialization
This commit is contained in:
@ -188,6 +188,16 @@ CCreatureHandler::CCreatureHandler()
|
|||||||
loadCommanders();
|
loadCommanders();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CCreature * CCreatureHandler::getCreature(const std::string & scope, const std::string & identifier) const
|
||||||
|
{
|
||||||
|
boost::optional<si32> index = VLC->modh->identifiers.getIdentifier(scope, "creature", identifier);
|
||||||
|
|
||||||
|
if(!index)
|
||||||
|
throw std::runtime_error("Creature not found "+identifier);
|
||||||
|
|
||||||
|
return creatures[*index];
|
||||||
|
}
|
||||||
|
|
||||||
void CCreatureHandler::loadCommanders()
|
void CCreatureHandler::loadCommanders()
|
||||||
{
|
{
|
||||||
JsonNode data(ResourceID("config/commanders.json"));
|
JsonNode data(ResourceID("config/commanders.json"));
|
||||||
|
@ -141,7 +141,7 @@ public:
|
|||||||
if(version>=756)
|
if(version>=756)
|
||||||
{
|
{
|
||||||
h & identifier;
|
h & identifier;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CCreature();
|
CCreature();
|
||||||
@ -192,6 +192,8 @@ public:
|
|||||||
std::vector< std::vector <ui8> > skillLevels; //how much of a bonus will be given to commander with every level. SPELL_POWER also gives CASTS and RESISTANCE
|
std::vector< std::vector <ui8> > skillLevels; //how much of a bonus will be given to commander with every level. SPELL_POWER also gives CASTS and RESISTANCE
|
||||||
std::vector <std::pair <Bonus*, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
|
std::vector <std::pair <Bonus*, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
|
||||||
|
|
||||||
|
const CCreature * getCreature(const std::string & scope, const std::string & identifier) const;
|
||||||
|
|
||||||
void deserializationFix();
|
void deserializationFix();
|
||||||
CreatureID pickRandomMonster(CRandomGenerator & rand, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
|
CreatureID pickRandomMonster(CRandomGenerator & rand, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
|
||||||
void addBonusForTier(int tier, Bonus *b); //tier must be <1-7>
|
void addBonusForTier(int tier, Bonus *b); //tier must be <1-7>
|
||||||
|
@ -481,6 +481,28 @@ void CCreatureSet::armyChanged()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CCreatureSet::writeJson(JsonNode& json) const
|
||||||
|
{
|
||||||
|
for(const auto & p : stacks)
|
||||||
|
{
|
||||||
|
JsonNode stack_node;
|
||||||
|
p.second->writeJson(stack_node);
|
||||||
|
json.Vector()[p.first.getNum()] = stack_node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CCreatureSet::readJson(const JsonNode& json)
|
||||||
|
{
|
||||||
|
for(size_t idx = 0; idx < json.Vector().size(); idx++)
|
||||||
|
{
|
||||||
|
CStackInstance * new_stack = new CStackInstance();
|
||||||
|
|
||||||
|
new_stack->readJson(json.Vector()[idx]);
|
||||||
|
|
||||||
|
putStack(SlotID(idx), new_stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CStackInstance::CStackInstance()
|
CStackInstance::CStackInstance()
|
||||||
: armyObj(_armyObj)
|
: armyObj(_armyObj)
|
||||||
{
|
{
|
||||||
@ -605,7 +627,7 @@ std::string CStackInstance::bonusToString(const Bonus *bonus, bool description)
|
|||||||
{
|
{
|
||||||
return VLC->getBth()->bonusToString(bonus, this, description);
|
return VLC->getBth()->bonusToString(bonus, this, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string CStackInstance::bonusToGraphics(const Bonus *bonus) const
|
std::string CStackInstance::bonusToGraphics(const Bonus *bonus) const
|
||||||
@ -700,6 +722,25 @@ ArtBearer::ArtBearer CStackInstance::bearerType() const
|
|||||||
return ArtBearer::CREATURE;
|
return ArtBearer::CREATURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CStackInstance::writeJson(JsonNode& json) const
|
||||||
|
{
|
||||||
|
if(idRand > -1)
|
||||||
|
{
|
||||||
|
json["level"].Float() = (int)idRand / 2;
|
||||||
|
json["upgraded"].Bool() = (idRand % 2) > 0;
|
||||||
|
}
|
||||||
|
CStackBasicDescriptor::writeJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CStackInstance::readJson(const JsonNode& json)
|
||||||
|
{
|
||||||
|
if(json["type"].String() == "")
|
||||||
|
{
|
||||||
|
idRand = json["level"].Float() * 2 + (int)json["upgraded"].Bool();
|
||||||
|
}
|
||||||
|
CStackBasicDescriptor::readJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
CCommanderInstance::CCommanderInstance()
|
CCommanderInstance::CCommanderInstance()
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
@ -792,6 +833,22 @@ CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CStackBasicDescriptor::writeJson(JsonNode& json) const
|
||||||
|
{
|
||||||
|
json.setType(JsonNode::DATA_STRUCT);
|
||||||
|
if(type)
|
||||||
|
json["type"].String() = type->identifier;
|
||||||
|
json["amount"].Float() = count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CStackBasicDescriptor::readJson(const JsonNode& json)
|
||||||
|
{
|
||||||
|
auto typeName = json["type"].String();
|
||||||
|
if(typeName != "")
|
||||||
|
type = VLC->creh->getCreature("core", json["type"].String());
|
||||||
|
count = json["amount"].Float();
|
||||||
|
}
|
||||||
|
|
||||||
DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
|
DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
|
||||||
{
|
{
|
||||||
if(!sth.valid(true))
|
if(!sth.valid(true))
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
* Full text of license available in license.txt file, in main folder
|
* Full text of license available in license.txt file, in main folder
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
class JsonNode;
|
||||||
class CCreature;
|
class CCreature;
|
||||||
class CGHeroInstance;
|
class CGHeroInstance;
|
||||||
class CArmedInstance;
|
class CArmedInstance;
|
||||||
@ -34,6 +34,10 @@ public:
|
|||||||
{
|
{
|
||||||
h & type & count;
|
h & type & count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeJson(JsonNode & json) const;
|
||||||
|
|
||||||
|
void readJson(const JsonNode & json);
|
||||||
};
|
};
|
||||||
|
|
||||||
class DLL_LINKAGE CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor, public CArtifactSet
|
class DLL_LINKAGE CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor, public CArtifactSet
|
||||||
@ -59,6 +63,10 @@ public:
|
|||||||
BONUS_TREE_DESERIALIZATION_FIX
|
BONUS_TREE_DESERIALIZATION_FIX
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeJson(JsonNode & json) const;
|
||||||
|
|
||||||
|
void readJson(const JsonNode & json);
|
||||||
|
|
||||||
//overrides CBonusSystemNode
|
//overrides CBonusSystemNode
|
||||||
std::string bonusToString(const Bonus *bonus, bool description) const override; // how would bonus description look for this particular type of node
|
std::string bonusToString(const Bonus *bonus, bool description) const override; // how would bonus description look for this particular type of node
|
||||||
std::string bonusToGraphics(const Bonus *bonus) const; //file name of graphics from StackSkills , in future possibly others
|
std::string bonusToGraphics(const Bonus *bonus) const; //file name of graphics from StackSkills , in future possibly others
|
||||||
@ -110,7 +118,7 @@ public:
|
|||||||
bool gainsLevel() const; //true if commander has lower level than should upon his experience
|
bool gainsLevel() const; //true if commander has lower level than should upon his experience
|
||||||
ui64 getPower() const override {return 0;};
|
ui64 getPower() const override {return 0;};
|
||||||
int getExpRank() const override;
|
int getExpRank() const override;
|
||||||
int getLevel() const override;
|
int getLevel() const override;
|
||||||
ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
|
ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
|
||||||
|
|
||||||
template <typename Handler> void serialize(Handler &h, const int version)
|
template <typename Handler> void serialize(Handler &h, const int version)
|
||||||
@ -211,6 +219,12 @@ public:
|
|||||||
{
|
{
|
||||||
h & stacks & formation;
|
h & stacks & formation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void writeJson(JsonNode & json) const;
|
||||||
|
|
||||||
|
void readJson(const JsonNode & json);
|
||||||
|
|
||||||
operator bool() const
|
operator bool() const
|
||||||
{
|
{
|
||||||
return !stacks.empty();
|
return !stacks.empty();
|
||||||
|
@ -135,10 +135,12 @@ CBonusSystemNode * CArmedInstance::whatShouldBeAttached()
|
|||||||
|
|
||||||
void CArmedInstance::writeJsonOptions(JsonNode& json) const
|
void CArmedInstance::writeJsonOptions(JsonNode& json) const
|
||||||
{
|
{
|
||||||
|
CGObjectInstance::writeJsonOptions(json);
|
||||||
|
CCreatureSet::writeJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CArmedInstance::readJsonOptions(const JsonNode& json)
|
void CArmedInstance::readJsonOptions(const JsonNode& json)
|
||||||
{
|
{
|
||||||
|
CGObjectInstance::readJsonOptions(json);
|
||||||
|
CCreatureSet::readJson(json);
|
||||||
}
|
}
|
||||||
|
@ -325,7 +325,7 @@ bool CGObjectInstance::passableFor(PlayerColor color) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGObjectInstance::writeJson(JsonNode & json, bool withState) const
|
void CGObjectInstance::writeJson(JsonNode & json) const
|
||||||
{
|
{
|
||||||
logGlobal->debugStream() <<"Save: [" << pos << "] " << id << " " << ID << " " << subID << " " << typeName << " " << subTypeName;
|
logGlobal->debugStream() <<"Save: [" << pos << "] " << id << " " << ID << " " << subID << " " << typeName << " " << subTypeName;
|
||||||
|
|
||||||
@ -338,11 +338,9 @@ void CGObjectInstance::writeJson(JsonNode & json, bool withState) const
|
|||||||
|
|
||||||
appearance.writeJson(json["template"], false);
|
appearance.writeJson(json["template"], false);
|
||||||
writeJsonOptions(json["options"]);
|
writeJsonOptions(json["options"]);
|
||||||
if(withState)
|
|
||||||
writeJsonState(json["state"]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGObjectInstance::readJson(const JsonNode & json, bool withState)
|
void CGObjectInstance::readJson(const JsonNode & json)
|
||||||
{
|
{
|
||||||
if(json.getType() != JsonNode::DATA_STRUCT)
|
if(json.getType() != JsonNode::DATA_STRUCT)
|
||||||
{
|
{
|
||||||
@ -355,8 +353,6 @@ void CGObjectInstance::readJson(const JsonNode & json, bool withState)
|
|||||||
|
|
||||||
appearance.readJson(json["template"], false);
|
appearance.readJson(json["template"], false);
|
||||||
readJsonOptions(json["options"]);
|
readJsonOptions(json["options"]);
|
||||||
if(withState)
|
|
||||||
readJsonState(json["state"]);
|
|
||||||
|
|
||||||
logGlobal->debugStream() <<"Load: [" << pos << "] " << id << " " << ID << " " << subID << " " << typeName << " " << subTypeName;
|
logGlobal->debugStream() <<"Load: [" << pos << "] " << id << " " << ID << " " << subID << " " << typeName << " " << subTypeName;
|
||||||
}
|
}
|
||||||
@ -380,15 +376,6 @@ void CGObjectInstance::readJsonOptions(const JsonNode & json)
|
|||||||
tempOwner = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, json["owner"].String()));
|
tempOwner = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, json["owner"].String()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGObjectInstance::writeJsonState(JsonNode & json) const
|
|
||||||
{
|
|
||||||
json.setType(JsonNode::DATA_STRUCT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CGObjectInstance::readJsonState(const JsonNode & json)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
|
CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
|
||||||
{
|
{
|
||||||
|
@ -186,10 +186,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
///Entry point of Json serialization
|
///Entry point of Json serialization
|
||||||
void writeJson(JsonNode & json, bool withState = false) const;
|
void writeJson(JsonNode & json) const;
|
||||||
|
|
||||||
///Entry point of Json de-serialization
|
///Entry point of Json de-serialization
|
||||||
void readJson(const JsonNode & json, bool withState = false);
|
void readJson(const JsonNode & json);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// virtual method that allows synchronously update object state on server and all clients
|
/// virtual method that allows synchronously update object state on server and all clients
|
||||||
@ -206,14 +206,6 @@ protected:
|
|||||||
///(!) do not forget to call inherited method first when overriding
|
///(!) do not forget to call inherited method first when overriding
|
||||||
virtual void readJsonOptions(const JsonNode & json);
|
virtual void readJsonOptions(const JsonNode & json);
|
||||||
|
|
||||||
///Saves object-type specific state
|
|
||||||
///(!) do not forget to call inherited method first when overriding
|
|
||||||
virtual void writeJsonState(JsonNode & json) const;
|
|
||||||
|
|
||||||
///Loads object-type specific state
|
|
||||||
///(!) do not forget to call inherited method first when overriding
|
|
||||||
virtual void readJsonState(const JsonNode & json);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable std::string stringId;///<alternate id, dynamically generated, do not serialize
|
mutable std::string stringId;///<alternate id, dynamically generated, do not serialize
|
||||||
};
|
};
|
||||||
|
@ -600,7 +600,7 @@ void CMapLoaderJson::MapObjectLoader::construct()
|
|||||||
|
|
||||||
void CMapLoaderJson::MapObjectLoader::configure()
|
void CMapLoaderJson::MapObjectLoader::configure()
|
||||||
{
|
{
|
||||||
instance->readJson(configuration, false);
|
instance->readJson(configuration);
|
||||||
|
|
||||||
if(instance->ID == Obj::TOWN)
|
if(instance->ID == Obj::TOWN)
|
||||||
{
|
{
|
||||||
@ -856,7 +856,7 @@ void CMapSaverJson::writeObjects()
|
|||||||
JsonNode data(JsonNode::DATA_STRUCT);
|
JsonNode data(JsonNode::DATA_STRUCT);
|
||||||
|
|
||||||
for(const CGObjectInstance * obj : map->objects)
|
for(const CGObjectInstance * obj : map->objects)
|
||||||
obj->writeJson(data[obj->getStringId()], false);
|
obj->writeJson(data[obj->getStringId()]);
|
||||||
|
|
||||||
addToArchive(data, OBJECTS_FILE_NAME);
|
addToArchive(data, OBJECTS_FILE_NAME);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user