mirror of
https://github.com/vcmi/vcmi.git
synced 2025-09-16 09:26:28 +02:00
CGMine, CGArtifact, CGGarrison serialization
This commit is contained in:
@@ -135,12 +135,10 @@ CBonusSystemNode * CArmedInstance::whatShouldBeAttached()
|
||||
|
||||
void CArmedInstance::writeJsonOptions(JsonNode& json) const
|
||||
{
|
||||
CGObjectInstance::writeJsonOptions(json);
|
||||
CCreatureSet::writeJson(json["army"]);
|
||||
}
|
||||
|
||||
void CArmedInstance::readJsonOptions(const JsonNode& json)
|
||||
{
|
||||
CGObjectInstance::readJsonOptions(json);
|
||||
CCreatureSet::readJson(json["army"]);
|
||||
}
|
||||
|
@@ -313,6 +313,18 @@ void CGDwelling::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer)
|
||||
}
|
||||
}
|
||||
|
||||
void CGDwelling::writeJsonOptions(JsonNode& json) const
|
||||
{
|
||||
//todo:CGDwelling::writeJsonOptions
|
||||
CGObjectInstance::writeOwner(json);
|
||||
}
|
||||
|
||||
void CGDwelling::readJsonOptions(const JsonNode& json)
|
||||
{
|
||||
//todo:CGDwelling::readJsonOptions
|
||||
CGObjectInstance::readOwner(json);
|
||||
}
|
||||
|
||||
int CGTownInstance::getSightRadious() const //returns sight distance
|
||||
{
|
||||
if (subID == ETownType::TOWER)
|
||||
@@ -360,7 +372,7 @@ CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - for
|
||||
|
||||
int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
|
||||
{
|
||||
|
||||
|
||||
if (hasBuilt(BuildingID::CAPITOL))
|
||||
return 3;
|
||||
if (hasBuilt(BuildingID::CITY_HALL))
|
||||
@@ -455,12 +467,12 @@ TResources CGTownInstance::dailyIncome() const
|
||||
{
|
||||
TResources ret;
|
||||
|
||||
for (auto & p : town->buildings)
|
||||
{
|
||||
for (auto & p : town->buildings)
|
||||
{
|
||||
BuildingID buildingUpgrade;
|
||||
|
||||
for (auto & p2 : town->buildings)
|
||||
{
|
||||
for (auto & p2 : town->buildings)
|
||||
{
|
||||
if (p2.second->upgrade == p.first)
|
||||
{
|
||||
buildingUpgrade = p2.first;
|
||||
@@ -471,7 +483,7 @@ TResources CGTownInstance::dailyIncome() const
|
||||
{
|
||||
ret += p.second->produce;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -964,7 +976,7 @@ void CGTownInstance::setVisitingHero(CGHeroInstance *h)
|
||||
//{
|
||||
// logGlobal->warnStream() << boost::format("Hero visiting town %s is %s ") % name % (visitingHero.get() ? visitingHero->name : "NULL");
|
||||
// logGlobal->warnStream() << boost::format("New hero will be %s ") % (h ? h->name : "NULL");
|
||||
//
|
||||
//
|
||||
//}
|
||||
assert(!!visitingHero == !h);
|
||||
|
||||
|
@@ -52,6 +52,10 @@ public:
|
||||
CSpecObjInfo * info; //h3m info about dewlling
|
||||
TCreaturesSet creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
|
||||
|
||||
protected:
|
||||
void writeJsonOptions(JsonNode & json) const override;
|
||||
void readJsonOptions(const JsonNode & json) override;
|
||||
|
||||
private:
|
||||
void initObj() override;
|
||||
void onHeroVisit(const CGHeroInstance * h) const override;
|
||||
|
@@ -362,7 +362,23 @@ void CGObjectInstance::writeJsonOptions(JsonNode & json) const
|
||||
{
|
||||
json.setType(JsonNode::DATA_STRUCT);
|
||||
|
||||
//todo: move up to descendants
|
||||
// //todo: move up to descendants
|
||||
// if(tempOwner != PlayerColor::UNFLAGGABLE)
|
||||
// {
|
||||
// PlayerColor p (tempOwner);
|
||||
// if(p.isValidPlayer())
|
||||
// json["owner"].String() = GameConstants::PLAYER_COLOR_NAMES[p.getNum()];
|
||||
// }
|
||||
}
|
||||
|
||||
void CGObjectInstance::readJsonOptions(const JsonNode & json)
|
||||
{
|
||||
// if(json["owner"].getType() == JsonNode::DATA_STRING)
|
||||
// tempOwner = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, json["owner"].String()));
|
||||
}
|
||||
|
||||
void CGObjectInstance::writeOwner(JsonNode & json) const
|
||||
{
|
||||
if(tempOwner != PlayerColor::UNFLAGGABLE)
|
||||
{
|
||||
PlayerColor p (tempOwner);
|
||||
@@ -371,13 +387,12 @@ void CGObjectInstance::writeJsonOptions(JsonNode & json) const
|
||||
}
|
||||
}
|
||||
|
||||
void CGObjectInstance::readJsonOptions(const JsonNode & json)
|
||||
void CGObjectInstance::readOwner(const JsonNode & json)
|
||||
{
|
||||
if(json["owner"].getType() == JsonNode::DATA_STRING)
|
||||
tempOwner = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, json["owner"].String()));
|
||||
}
|
||||
|
||||
|
||||
CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
|
||||
{
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#pragma once
|
||||
#pragma once
|
||||
|
||||
#include "ObjectTemplate.h"
|
||||
|
||||
@@ -206,6 +206,8 @@ protected:
|
||||
///(!) do not forget to call inherited method first when overriding
|
||||
virtual void readJsonOptions(const JsonNode & json);
|
||||
|
||||
void writeOwner(JsonNode & json) const;
|
||||
void readOwner(const JsonNode & json);
|
||||
private:
|
||||
mutable std::string stringId;///<alternate id, dynamically generated, do not serialize
|
||||
};
|
||||
|
@@ -11,6 +11,7 @@
|
||||
#include "StdInc.h"
|
||||
#include "MiscObjects.h"
|
||||
|
||||
#include "../StringConstants.h"
|
||||
#include "../NetPacks.h"
|
||||
#include "../CGeneralTextHandler.h"
|
||||
#include "../CSoundBase.h"
|
||||
@@ -606,7 +607,7 @@ void CGMine::newTurn() const
|
||||
|
||||
void CGMine::initObj()
|
||||
{
|
||||
if(subID >= 7) //Abandoned Mine
|
||||
if(isAbandoned())
|
||||
{
|
||||
//set guardians
|
||||
int howManyTroglodytes = cb->gameState()->getRandomGenerator().nextInt(100, 199);
|
||||
@@ -633,6 +634,11 @@ void CGMine::initObj()
|
||||
producedQuantity = defaultResProduction();
|
||||
}
|
||||
|
||||
bool CGMine::isAbandoned() const
|
||||
{
|
||||
return (subID >= 7);
|
||||
}
|
||||
|
||||
std::string CGMine::getObjectName() const
|
||||
{
|
||||
return VLC->generaltexth->mines.at(subID).first;
|
||||
@@ -690,7 +696,7 @@ void CGMine::battleFinished(const CGHeroInstance *hero, const BattleResult &resu
|
||||
{
|
||||
if(result.winner == 0) //attacker won
|
||||
{
|
||||
if(subID == 7)
|
||||
if(isAbandoned())
|
||||
{
|
||||
showInfoDialog(hero->tempOwner, 85, 0);
|
||||
}
|
||||
@@ -704,14 +710,68 @@ void CGMine::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) con
|
||||
cb->startBattleI(hero, this);
|
||||
}
|
||||
|
||||
void CGMine::writeJsonOptions(JsonNode& json) const
|
||||
void CGMine::writeJsonOptions(JsonNode & json) const
|
||||
{
|
||||
CArmedInstance::writeJsonOptions(json);
|
||||
|
||||
if(isAbandoned())
|
||||
{
|
||||
JsonNode & node = json["possibleResources"];
|
||||
|
||||
for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
|
||||
if(tempOwner.getNum() & 1<<i)
|
||||
{
|
||||
JsonNode one(JsonNode::DATA_STRING);
|
||||
one.String() = GameConstants::RESOURCE_NAMES[i];
|
||||
node.Vector().push_back(one);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CGObjectInstance::writeOwner(json);
|
||||
}
|
||||
}
|
||||
|
||||
void CGMine::readJsonOptions(const JsonNode& json)
|
||||
void CGMine::readJsonOptions(const JsonNode & json)
|
||||
{
|
||||
CArmedInstance::readJsonOptions(json);
|
||||
|
||||
if(isAbandoned())
|
||||
{
|
||||
const JsonNode & node = json["possibleResources"];
|
||||
|
||||
std::set<int> possibleResources;
|
||||
|
||||
if(node.Vector().size() == 0)
|
||||
{
|
||||
//assume all allowed
|
||||
for(int i = (int)Res::WOOD; i < (int) Res::GOLD; i++)
|
||||
possibleResources.insert(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto names = node.convertTo<std::vector<std::string>>();
|
||||
|
||||
for(const std::string & s : names)
|
||||
{
|
||||
int raw_res = vstd::find_pos(GameConstants::RESOURCE_NAMES, s);
|
||||
if(raw_res < 0)
|
||||
logGlobal->errorStream() << "Invalid resource name: "+s;
|
||||
else
|
||||
possibleResources.insert(raw_res);
|
||||
}
|
||||
|
||||
int tmp = 0;
|
||||
|
||||
for(int r : possibleResources)
|
||||
tmp |= (1<<r);
|
||||
tempOwner = PlayerColor(tmp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CGObjectInstance::readOwner(json);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1298,12 +1358,14 @@ void CGArtifact::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer)
|
||||
|
||||
void CGArtifact::writeJsonOptions(JsonNode& json) const
|
||||
{
|
||||
|
||||
CCreatureSet::writeJson(json["guards"]);
|
||||
json["guardMessage"].String() = message;
|
||||
}
|
||||
|
||||
void CGArtifact::readJsonOptions(const JsonNode& json)
|
||||
{
|
||||
|
||||
CCreatureSet::readJson(json["guards"]);
|
||||
message = json["guardMessage"].String();
|
||||
}
|
||||
|
||||
void CGWitchHut::initObj()
|
||||
@@ -1681,12 +1743,16 @@ void CGGarrison::battleFinished(const CGHeroInstance *hero, const BattleResult &
|
||||
|
||||
void CGGarrison::writeJsonOptions(JsonNode& json) const
|
||||
{
|
||||
|
||||
CArmedInstance::writeJsonOptions(json);
|
||||
CGObjectInstance::writeOwner(json);
|
||||
json["removableUnits"].Bool() = removableUnits;
|
||||
}
|
||||
|
||||
void CGGarrison::readJsonOptions(const JsonNode& json)
|
||||
{
|
||||
|
||||
CArmedInstance::readJsonOptions(json);
|
||||
CGObjectInstance::readOwner(json);
|
||||
removableUnits = json["removableUnits"].Bool();
|
||||
}
|
||||
|
||||
void CGMagi::initObj()
|
||||
|
@@ -263,6 +263,7 @@ private:
|
||||
std::string getObjectName() const override;
|
||||
std::string getHoverText(PlayerColor player) const override;
|
||||
|
||||
bool isAbandoned() const;
|
||||
public:
|
||||
template <typename Handler> void serialize(Handler &h, const int version)
|
||||
{
|
||||
|
Reference in New Issue
Block a user