mirror of
https://github.com/vcmi/vcmi.git
synced 2024-11-28 08:48:48 +02:00
Creature set serialization
This commit is contained in:
parent
37d0dad70a
commit
a638d0cd51
@ -188,6 +188,16 @@ CCreatureHandler::CCreatureHandler()
|
||||
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()
|
||||
{
|
||||
JsonNode data(ResourceID("config/commanders.json"));
|
||||
|
@ -141,7 +141,7 @@ public:
|
||||
if(version>=756)
|
||||
{
|
||||
h & identifier;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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::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();
|
||||
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>
|
||||
|
@ -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()
|
||||
: armyObj(_armyObj)
|
||||
{
|
||||
@ -605,7 +627,7 @@ std::string CStackInstance::bonusToString(const Bonus *bonus, bool description)
|
||||
{
|
||||
return VLC->getBth()->bonusToString(bonus, this, description);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
std::string CStackInstance::bonusToGraphics(const Bonus *bonus) const
|
||||
@ -700,6 +722,25 @@ ArtBearer::ArtBearer CStackInstance::bearerType() const
|
||||
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()
|
||||
{
|
||||
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)
|
||||
{
|
||||
if(!sth.valid(true))
|
||||
|
@ -14,7 +14,7 @@
|
||||
* Full text of license available in license.txt file, in main folder
|
||||
*
|
||||
*/
|
||||
|
||||
class JsonNode;
|
||||
class CCreature;
|
||||
class CGHeroInstance;
|
||||
class CArmedInstance;
|
||||
@ -34,6 +34,10 @@ public:
|
||||
{
|
||||
h & type & count;
|
||||
}
|
||||
|
||||
void writeJson(JsonNode & json) const;
|
||||
|
||||
void readJson(const JsonNode & json);
|
||||
};
|
||||
|
||||
class DLL_LINKAGE CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor, public CArtifactSet
|
||||
@ -59,6 +63,10 @@ public:
|
||||
BONUS_TREE_DESERIALIZATION_FIX
|
||||
}
|
||||
|
||||
void writeJson(JsonNode & json) const;
|
||||
|
||||
void readJson(const JsonNode & json);
|
||||
|
||||
//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 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
|
||||
ui64 getPower() const override {return 0;};
|
||||
int getExpRank() const override;
|
||||
int getLevel() const override;
|
||||
int getLevel() const override;
|
||||
ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
|
||||
|
||||
template <typename Handler> void serialize(Handler &h, const int version)
|
||||
@ -211,6 +219,12 @@ public:
|
||||
{
|
||||
h & stacks & formation;
|
||||
}
|
||||
|
||||
|
||||
void writeJson(JsonNode & json) const;
|
||||
|
||||
void readJson(const JsonNode & json);
|
||||
|
||||
operator bool() const
|
||||
{
|
||||
return !stacks.empty();
|
||||
|
@ -135,10 +135,12 @@ CBonusSystemNode * CArmedInstance::whatShouldBeAttached()
|
||||
|
||||
void CArmedInstance::writeJsonOptions(JsonNode& json) const
|
||||
{
|
||||
|
||||
CGObjectInstance::writeJsonOptions(json);
|
||||
CCreatureSet::writeJson(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;
|
||||
}
|
||||
|
||||
void CGObjectInstance::writeJson(JsonNode & json, bool withState) const
|
||||
void CGObjectInstance::writeJson(JsonNode & json) const
|
||||
{
|
||||
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);
|
||||
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)
|
||||
{
|
||||
@ -355,8 +353,6 @@ void CGObjectInstance::readJson(const JsonNode & json, bool withState)
|
||||
|
||||
appearance.readJson(json["template"], false);
|
||||
readJsonOptions(json["options"]);
|
||||
if(withState)
|
||||
readJsonState(json["state"]);
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
void CGObjectInstance::writeJsonState(JsonNode & json) const
|
||||
{
|
||||
json.setType(JsonNode::DATA_STRUCT);
|
||||
}
|
||||
|
||||
void CGObjectInstance::readJsonState(const JsonNode & json)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
|
||||
{
|
||||
|
@ -186,10 +186,10 @@ public:
|
||||
}
|
||||
|
||||
///Entry point of Json serialization
|
||||
void writeJson(JsonNode & json, bool withState = false) const;
|
||||
void writeJson(JsonNode & json) const;
|
||||
|
||||
///Entry point of Json de-serialization
|
||||
void readJson(const JsonNode & json, bool withState = false);
|
||||
void readJson(const JsonNode & json);
|
||||
|
||||
protected:
|
||||
/// 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
|
||||
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:
|
||||
mutable std::string stringId;///<alternate id, dynamically generated, do not serialize
|
||||
};
|
||||
|
@ -600,7 +600,7 @@ void CMapLoaderJson::MapObjectLoader::construct()
|
||||
|
||||
void CMapLoaderJson::MapObjectLoader::configure()
|
||||
{
|
||||
instance->readJson(configuration, false);
|
||||
instance->readJson(configuration);
|
||||
|
||||
if(instance->ID == Obj::TOWN)
|
||||
{
|
||||
@ -856,7 +856,7 @@ void CMapSaverJson::writeObjects()
|
||||
JsonNode data(JsonNode::DATA_STRUCT);
|
||||
|
||||
for(const CGObjectInstance * obj : map->objects)
|
||||
obj->writeJson(data[obj->getStringId()], false);
|
||||
obj->writeJson(data[obj->getStringId()]);
|
||||
|
||||
addToArchive(data, OBJECTS_FILE_NAME);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user