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

- added default values to JsonNode

- small cleanup to heroes.json
This commit is contained in:
Ivan Savenko 2011-08-27 21:17:45 +00:00
parent f3b6ecf9a3
commit 33719696d8
7 changed files with 487 additions and 501 deletions

View File

@ -121,12 +121,10 @@ static void setButton(ButtonInfo &button, const JsonNode &g)
button.playerColoured = g["playerColoured"].Float(); button.playerColoured = g["playerColoured"].Float();
button.defName = g["graphic"].String(); button.defName = g["graphic"].String();
if (!g["additionalDefs"].isNull()) { const JsonVector &defs_vec = g["additionalDefs"].Vector();
const JsonVector &defs_vec = g["additionalDefs"].Vector();
BOOST_FOREACH(const JsonNode &def, defs_vec) { BOOST_FOREACH(const JsonNode &def, defs_vec) {
button.additionalDefs.push_back(def.String()); button.additionalDefs.push_back(def.String());
}
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -479,7 +479,6 @@ void CCreatureHandler::loadCreatures()
for (JsonVector::const_iterator it = creatures_vec.begin(); it!=creatures_vec.end(); ++it) { for (JsonVector::const_iterator it = creatures_vec.begin(); it!=creatures_vec.end(); ++it) {
const JsonNode &creature = *it; const JsonNode &creature = *it;
int creatureID = creature["id"].Float(); int creatureID = creature["id"].Float();
const JsonNode *value;
/* A creature can have several names. */ /* A creature can have several names. */
const JsonVector &names_vec = creature["name"].Vector(); const JsonVector &names_vec = creature["name"].Vector();
@ -495,20 +494,11 @@ void CCreatureHandler::loadCreatures()
c->faction = creature["faction"].Float(); c->faction = creature["faction"].Float();
c->animDefName = creature["defname"].String(); c->animDefName = creature["defname"].String();
value = &creature["upgrade"]; c->upgrades.insert(creature["upgrade"].Float());
if (!value->isNull()) idToProjectile[creatureID] = creature["projectile_defname"].String();
c->upgrades.insert(value->Float()); idToProjectileSpin[creatureID] = creature["projectile_spin"].Bool();
value = &creature["projectile_defname"]; if (creature["turret_shooter"].Bool(false))
if (!value->isNull()) {
idToProjectile[creatureID] = value->String();
value = &creature["projectile_spin"];
idToProjectileSpin[creatureID] = value->Bool();
}
value = &creature["turret_shooter"];
if (!value->isNull() && value->Bool())
factionToTurretCreature[c->faction] = creatureID; factionToTurretCreature[c->faction] = creatureID;
} }

View File

@ -290,29 +290,24 @@ void CHeroHandler::loadHeroes()
int hid = hero["id"].Float(); int hid = hero["id"].Float();
const JsonNode *value; const JsonNode *value;
heroes[hid]->sex = hero["sex"].Float(); //sex: 0=male, 1=female
heroes[hid]->sex = !hero["female"].Bool(false);
BOOST_FOREACH(const JsonNode &set, hero["skill_set"].Vector()) { BOOST_FOREACH(const JsonNode &set, hero["skill_set"].Vector()) {
heroes[hid]->secSkillsInit.push_back(std::make_pair(set["skill"].Float(), set["level"].Float())); heroes[hid]->secSkillsInit.push_back(std::make_pair(set["skill"].Float(), set["level"].Float(1)));
} }
value = &hero["spell"]; heroes[hid]->startingSpell = hero["spell"].Float(-1);
if (!value->isNull()) {
heroes[hid]->startingSpell = value->Float();
}
value = &hero["specialties"]; BOOST_FOREACH(const JsonNode &specialty, hero["specialties"].Vector()) {
if (!value->isNull()) { SSpecialtyInfo dummy;
BOOST_FOREACH(const JsonNode &specialty, value->Vector()) {
SSpecialtyInfo dummy;
dummy.type = specialty["type"].Float(); dummy.type = specialty["type"].Float();
dummy.val = specialty["val"].Float(); dummy.val = specialty["val"].Float(0);
dummy.subtype = specialty["subtype"].Float(); dummy.subtype = specialty["subtype"].Float(0);
dummy.additionalinfo = specialty["info"].Float(); dummy.additionalinfo = specialty["info"].Float(0);
heroes[hid]->spec.push_back(dummy); //put a copy of dummy heroes[hid]->spec.push_back(dummy); //put a copy of dummy
}
} }
} }

View File

@ -62,7 +62,6 @@ void CTownHandler::loadStructures()
// Read buildings coordinates for that city // Read buildings coordinates for that city
BOOST_FOREACH(const JsonNode &node, town_node["defnames"].Vector()) { BOOST_FOREACH(const JsonNode &node, town_node["defnames"].Vector()) {
Structure *vinya = new Structure; Structure *vinya = new Structure;
const JsonNode *value;
vinya->group = -1; vinya->group = -1;
vinya->townID = townID; vinya->townID = townID;
@ -73,13 +72,8 @@ void CTownHandler::loadStructures()
vinya->pos.y = node["y"].Float(); vinya->pos.y = node["y"].Float();
vinya->pos.z = 0; vinya->pos.z = 0;
value = &node["border"]; vinya->borderName = node["border"].String();
if (!value->isNull()) vinya->areaName = node["area"].String();
vinya->borderName = value->String();
value = &node["area"];
if (!value->isNull())
vinya->areaName = value->String();
town[vinya->ID] = vinya; town[vinya->ID] = vinya;
} }

View File

@ -125,32 +125,42 @@ JsonMap & JsonNode::Struct()
} }
const bool & JsonNode::Bool() const const bool & JsonNode::Bool(const bool &value) const
{ {
if (isNull())
return value;
assert(type == DATA_BOOL); assert(type == DATA_BOOL);
return data.Bool; return data.Bool;
} }
const float & JsonNode::Float() const const float & JsonNode::Float(const float &value) const
{ {
if (isNull())
return value;
assert(type == DATA_FLOAT); assert(type == DATA_FLOAT);
return data.Float; return data.Float;
} }
const std::string & JsonNode::String() const const std::string & JsonNode::String(const std::string &value) const
{ {
if (isNull())
return value;
assert(type == DATA_STRING); assert(type == DATA_STRING);
return *data.String; return *data.String;
} }
const JsonVector & JsonNode::Vector() const const JsonVector & JsonNode::Vector(const JsonVector &value) const
{ {
if (isNull())
return value;
assert(type == DATA_VECTOR); assert(type == DATA_VECTOR);
return *data.Vector; return *data.Vector;
} }
const JsonMap & JsonNode::Struct() const const JsonMap & JsonNode::Struct(const JsonMap &value) const
{ {
if (isNull())
return value;
assert(type == DATA_STRUCT); assert(type == DATA_STRUCT);
return *data.Struct; return *data.Struct;
} }

View File

@ -60,19 +60,18 @@ public:
//non-const accessors, node will change type on type mismatch //non-const accessors, node will change type on type mismatch
bool & Bool(); bool & Bool();
int & Int();
float & Float(); float & Float();
std::string & String(); std::string & String();
JsonVector & Vector(); JsonVector & Vector();
JsonMap & Struct(); JsonMap & Struct();
//const accessors, will cause assertion failure on type mismatch //const accessors, will cause assertion failure on type mismatch
const bool & Bool() const; //if node is null then default value will be returned instead
const int & Int() const; const bool & Bool(const bool &value = false) const;
const float & Float() const; const float & Float(const float &value = 0) const;
const std::string & String() const; const std::string & String(const std::string &value = "") const;
const JsonVector & Vector() const; const JsonVector & Vector(const JsonVector &value = JsonVector()) const;
const JsonMap & Struct() const; const JsonMap & Struct(const JsonMap &value = JsonMap()) const;
//formatted output of this node in JSON format //formatted output of this node in JSON format
void write(std::ostream &out, std::string prefix="") const; void write(std::ostream &out, std::string prefix="") const;