2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2007-07-26 15:00:18 +03:00
|
|
|
#include "CTownHandler.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2012-08-02 14:03:26 +03:00
|
|
|
#include "VCMI_Lib.h"
|
2008-12-22 19:48:41 +02:00
|
|
|
#include "CGeneralTextHandler.h"
|
2012-08-02 14:03:26 +03:00
|
|
|
#include "JsonNode.h"
|
2012-12-14 18:32:53 +03:00
|
|
|
#include "StringConstants.h"
|
2012-12-03 19:00:17 +03:00
|
|
|
#include "CModHandler.h"
|
2012-12-17 15:55:29 +03:00
|
|
|
#include "CHeroHandler.h"
|
2012-12-15 11:47:02 +03:00
|
|
|
#include "CArtHandler.h"
|
2012-12-18 13:32:11 +03:00
|
|
|
#include "CSpellHandler.h"
|
2013-04-07 13:48:07 +03:00
|
|
|
#include "filesystem/CResourceLoader.h"
|
2009-04-15 17:03:31 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CTownHandler.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-03-02 19:55:51 +03:00
|
|
|
const int NAMES_PER_TOWN=16; // number of town names per faction in H3 files. Json can define any number
|
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
const std::string & CBuilding::Name() const
|
|
|
|
{
|
2012-09-05 15:49:23 +03:00
|
|
|
return name;
|
2012-09-02 13:33:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & CBuilding::Description() const
|
|
|
|
{
|
2012-09-05 15:49:23 +03:00
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
2013-02-11 22:11:34 +03:00
|
|
|
BuildingID CBuilding::getBase() const
|
2012-09-05 15:49:23 +03:00
|
|
|
{
|
|
|
|
const CBuilding * build = this;
|
|
|
|
while (build->upgrade >= 0)
|
2013-04-21 15:49:26 +03:00
|
|
|
build = build->town->buildings[build->upgrade];
|
2012-09-05 15:49:23 +03:00
|
|
|
|
|
|
|
return build->bid;
|
|
|
|
}
|
|
|
|
|
2013-02-11 22:11:34 +03:00
|
|
|
si32 CBuilding::getDistance(BuildingID buildID) const
|
2012-09-05 15:49:23 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
const CBuilding * build = town->buildings[buildID];
|
2012-09-05 15:49:23 +03:00
|
|
|
int distance = 0;
|
|
|
|
while (build->upgrade >= 0 && build != this)
|
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
build = build->town->buildings[build->upgrade];
|
2012-09-05 15:49:23 +03:00
|
|
|
distance++;
|
|
|
|
}
|
|
|
|
if (build == this)
|
|
|
|
return distance;
|
|
|
|
return -1;
|
2012-09-02 13:33:41 +03:00
|
|
|
}
|
|
|
|
|
2013-04-21 17:08:46 +03:00
|
|
|
CFaction::CFaction()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CFaction::~CFaction()
|
|
|
|
{
|
|
|
|
delete town;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTown::CTown()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CTown::~CTown()
|
|
|
|
{
|
2013-04-21 19:38:31 +03:00
|
|
|
BOOST_FOREACH(auto & build, buildings)
|
2013-04-21 17:08:46 +03:00
|
|
|
build.second.dellNull();
|
|
|
|
|
2013-04-21 19:38:31 +03:00
|
|
|
BOOST_FOREACH(auto & str, clientInfo.structures)
|
|
|
|
str.dellNull();
|
2013-04-21 17:08:46 +03:00
|
|
|
}
|
|
|
|
|
2007-07-26 15:00:18 +03:00
|
|
|
CTownHandler::CTownHandler()
|
2008-06-30 03:06:41 +03:00
|
|
|
{
|
|
|
|
VLC->townh = this;
|
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2013-04-21 19:38:31 +03:00
|
|
|
CTownHandler::~CTownHandler()
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(auto faction, factions)
|
|
|
|
faction.dellNull();
|
|
|
|
}
|
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
JsonNode readBuilding(CLegacyConfigParser & parser)
|
2008-12-22 19:48:41 +02:00
|
|
|
{
|
2012-09-02 13:33:41 +03:00
|
|
|
JsonNode ret;
|
|
|
|
JsonNode & cost = ret["cost"];
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
//note: this code will try to parse mithril as well but wil always return 0 for it
|
|
|
|
BOOST_FOREACH(const std::string & resID, GameConstants::RESOURCE_NAMES)
|
2012-09-02 13:33:41 +03:00
|
|
|
cost[resID].Float() = parser.readNumber();
|
|
|
|
|
2013-04-11 22:24:14 +03:00
|
|
|
cost.Struct().erase("mithril"); // erase mithril to avoid confusing validator
|
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
parser.endLine();
|
|
|
|
return ret;
|
2008-12-22 19:48:41 +02:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
std::vector<JsonNode> CTownHandler::loadLegacyData(size_t dataSize)
|
2007-07-26 15:00:18 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
std::vector<JsonNode> dest(dataSize);
|
|
|
|
factions.resize(dataSize);
|
|
|
|
|
|
|
|
auto getBuild = [&](size_t town, size_t building) -> JsonNode &
|
|
|
|
{
|
|
|
|
return dest[town]["town"]["buildings"][EBuildingType::names[building]];
|
|
|
|
};
|
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
CLegacyConfigParser parser("DATA/BUILDING.TXT");
|
|
|
|
|
|
|
|
parser.endLine(); // header
|
|
|
|
parser.endLine();
|
2011-08-26 06:10:56 +03:00
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
//Unique buildings
|
2013-04-21 15:49:26 +03:00
|
|
|
for (size_t town=0; town<dataSize; town++)
|
2007-07-26 15:00:18 +03:00
|
|
|
{
|
2012-09-02 13:33:41 +03:00
|
|
|
parser.endLine(); //header
|
|
|
|
parser.endLine();
|
|
|
|
|
|
|
|
int buildID = 17;
|
|
|
|
do
|
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
getBuild(town, buildID) = readBuilding(parser);
|
2012-09-02 13:33:41 +03:00
|
|
|
buildID++;
|
2011-08-20 07:48:23 +03:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
while (!parser.isNextEntryEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common buildings
|
|
|
|
parser.endLine(); // header
|
|
|
|
parser.endLine();
|
|
|
|
parser.endLine();
|
|
|
|
|
|
|
|
int buildID = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
JsonNode building = readBuilding(parser);
|
2011-08-20 09:08:48 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
for (size_t town=0; town<dataSize; town++)
|
|
|
|
getBuild(town, buildID) = building;
|
2012-09-02 13:33:41 +03:00
|
|
|
|
|
|
|
buildID++;
|
|
|
|
}
|
|
|
|
while (!parser.isNextEntryEmpty());
|
|
|
|
|
|
|
|
parser.endLine(); //header
|
|
|
|
parser.endLine();
|
|
|
|
|
|
|
|
//Dwellings
|
2013-04-21 15:49:26 +03:00
|
|
|
for (size_t town=0; town<dataSize; town++)
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
|
|
|
parser.endLine(); //header
|
|
|
|
parser.endLine();
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
for (size_t i=0; i<14; i++)
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
getBuild(town, 30+i) = readBuilding(parser);
|
2008-01-19 13:55:04 +02:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
}
|
2012-10-05 16:11:26 +03:00
|
|
|
{
|
|
|
|
CLegacyConfigParser parser("DATA/BLDGNEUT.TXT");
|
|
|
|
|
|
|
|
for(int building=0; building<15; building++)
|
|
|
|
{
|
|
|
|
std::string name = parser.readString();
|
|
|
|
std::string descr = parser.readString();
|
|
|
|
parser.endLine();
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
for(int j=0; j<dataSize; j++)
|
2012-10-05 16:11:26 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
getBuild(j, building)["name"].String() = name;
|
|
|
|
getBuild(j, building)["description"].String() = descr;
|
2012-10-05 16:11:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
parser.endLine(); // silo
|
|
|
|
parser.endLine(); // blacksmith //unused entries
|
|
|
|
parser.endLine(); // moat
|
|
|
|
|
|
|
|
//shipyard with the ship
|
|
|
|
std::string name = parser.readString();
|
|
|
|
std::string descr = parser.readString();
|
|
|
|
parser.endLine();
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
for(int town=0; town<dataSize; town++)
|
2012-10-05 16:11:26 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
getBuild(town, 20)["name"].String() = name;
|
|
|
|
getBuild(town, 20)["description"].String() = descr;
|
2012-10-05 16:11:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//blacksmith
|
2013-04-21 15:49:26 +03:00
|
|
|
for(int town=0; town<dataSize; town++)
|
2012-10-05 16:11:26 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
getBuild(town, 16)["name"].String() = parser.readString();
|
|
|
|
getBuild(town, 16)["description"].String() = parser.readString();
|
2012-10-05 16:11:26 +03:00
|
|
|
parser.endLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
CLegacyConfigParser parser("DATA/BLDGSPEC.TXT");
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
for(int town=0; town<dataSize; town++)
|
2012-10-05 16:11:26 +03:00
|
|
|
{
|
|
|
|
for(int build=0; build<9; build++)
|
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
getBuild(town, 17 + build)["name"].String() = parser.readString();
|
|
|
|
getBuild(town, 17 + build)["description"].String() = parser.readString();
|
2012-10-05 16:11:26 +03:00
|
|
|
parser.endLine();
|
|
|
|
}
|
2013-04-21 15:49:26 +03:00
|
|
|
getBuild(town, 26)["name"].String() = parser.readString(); // Grail
|
|
|
|
getBuild(town, 26)["description"].String() = parser.readString();
|
2012-10-05 16:11:26 +03:00
|
|
|
parser.endLine();
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
getBuild(town, 15)["name"].String() = parser.readString(); // Resource silo
|
|
|
|
getBuild(town, 15)["description"].String() = parser.readString();
|
2012-10-05 16:11:26 +03:00
|
|
|
parser.endLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
CLegacyConfigParser parser("DATA/DWELLING.TXT");
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
for(int town=0; town<dataSize; town++)
|
2012-10-05 16:11:26 +03:00
|
|
|
{
|
|
|
|
for(int build=0; build<14; build++)
|
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
getBuild(town, 30 + build)["name"].String() = parser.readString();
|
|
|
|
getBuild(town, 30 + build)["description"].String() = parser.readString();
|
2012-10-05 16:11:26 +03:00
|
|
|
parser.endLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
CLegacyConfigParser typeParser("DATA/TOWNTYPE.TXT");
|
|
|
|
CLegacyConfigParser nameParser("DATA/TOWNNAME.TXT");
|
|
|
|
size_t townID=0;
|
|
|
|
do
|
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
dest[townID]["name"].String() = typeParser.readString();
|
2012-10-05 16:11:26 +03:00
|
|
|
|
2013-03-02 19:55:51 +03:00
|
|
|
for (int i=0; i<NAMES_PER_TOWN; i++)
|
2012-10-05 16:11:26 +03:00
|
|
|
{
|
|
|
|
JsonNode name;
|
|
|
|
name.String() = nameParser.readString();
|
2013-04-21 15:49:26 +03:00
|
|
|
dest[townID]["town"]["names"].Vector().push_back(name);
|
2012-10-05 16:11:26 +03:00
|
|
|
nameParser.endLine();
|
|
|
|
}
|
|
|
|
townID++;
|
|
|
|
}
|
|
|
|
while (typeParser.endLine());
|
|
|
|
}
|
2013-04-21 15:49:26 +03:00
|
|
|
return dest;
|
2012-09-02 13:33:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownHandler::loadBuilding(CTown &town, const JsonNode & source)
|
|
|
|
{
|
|
|
|
CBuilding * ret = new CBuilding;
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
static const std::string modes [] = {"normal", "auto", "special", "grail"};
|
|
|
|
|
2013-02-11 22:11:34 +03:00
|
|
|
ret->mode = static_cast<CBuilding::EBuildMode>(boost::find(modes, source["mode"].String()) - modes);
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
ret->town = &town;
|
2013-02-11 22:11:34 +03:00
|
|
|
ret->bid = BuildingID(source["id"].Float());
|
2012-09-05 15:49:23 +03:00
|
|
|
ret->name = source["name"].String();
|
|
|
|
ret->description = source["description"].String();
|
2012-09-02 13:33:41 +03:00
|
|
|
ret->resources = TResources(source["cost"]);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const JsonNode &building, source["requires"].Vector())
|
2013-02-11 22:11:34 +03:00
|
|
|
ret->requirements.insert(BuildingID(building.Float()));
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
if (!source["upgrades"].isNull())
|
|
|
|
{
|
2013-02-11 22:11:34 +03:00
|
|
|
ret->requirements.insert(BuildingID(source["upgrades"].Float()));
|
|
|
|
ret->upgrade = BuildingID(source["upgrades"].Float());
|
2012-09-05 15:49:23 +03:00
|
|
|
}
|
|
|
|
else
|
2013-02-11 22:11:34 +03:00
|
|
|
ret->upgrade = BuildingID::NONE;
|
2012-09-05 15:49:23 +03:00
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
town.buildings[ret->bid] = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownHandler::loadBuildings(CTown &town, const JsonNode & source)
|
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
if (source.getType() == JsonNode::DATA_VECTOR)
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
BOOST_FOREACH(auto &node, source.Vector())
|
|
|
|
{
|
|
|
|
if (!node.isNull())
|
|
|
|
loadBuilding(town, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(auto &node, source.Struct())
|
|
|
|
{
|
|
|
|
if (!node.second.isNull())
|
|
|
|
loadBuilding(town, node.second);
|
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
}
|
|
|
|
}
|
2011-08-26 06:10:56 +03:00
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
void CTownHandler::loadStructure(CTown &town, const JsonNode & source)
|
|
|
|
{
|
|
|
|
CStructure * ret = new CStructure;
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
if (source["id"].isNull())
|
|
|
|
{
|
|
|
|
ret->building = nullptr;
|
|
|
|
ret->buildable = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-11 22:11:34 +03:00
|
|
|
ret->building = town.buildings[BuildingID(source["id"].Float())];
|
2012-09-05 15:49:23 +03:00
|
|
|
|
|
|
|
if (source["builds"].isNull())
|
|
|
|
ret->buildable = ret->building;
|
|
|
|
else
|
2013-02-11 22:11:34 +03:00
|
|
|
ret->buildable = town.buildings[BuildingID(source["builds"].Float())];
|
2012-09-05 15:49:23 +03:00
|
|
|
}
|
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
ret->pos.x = source["x"].Float();
|
|
|
|
ret->pos.y = source["y"].Float();
|
2012-09-05 15:49:23 +03:00
|
|
|
ret->pos.z = source["z"].Float();
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
ret->hiddenUpgrade = source["hidden"].Bool();
|
|
|
|
ret->defName = source["animation"].String();
|
2012-09-02 13:33:41 +03:00
|
|
|
ret->borderName = source["border"].String();
|
|
|
|
ret->areaName = source["area"].String();
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
town.clientInfo.structures.push_back(ret);
|
2012-09-02 13:33:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownHandler::loadStructures(CTown &town, const JsonNode & source)
|
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
if (source.getType() == JsonNode::DATA_VECTOR)
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
BOOST_FOREACH(auto &node, source.Vector())
|
|
|
|
{
|
|
|
|
if (!node.isNull())
|
|
|
|
loadStructure(town, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(auto &node, source.Struct())
|
|
|
|
{
|
|
|
|
if (!node.second.isNull())
|
|
|
|
loadStructure(town, node.second);
|
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
}
|
|
|
|
}
|
2011-08-26 06:43:43 +03:00
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
void CTownHandler::loadTownHall(CTown &town, const JsonNode & source)
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(const JsonNode &row, source.Vector())
|
|
|
|
{
|
2013-02-11 22:11:34 +03:00
|
|
|
std::vector< std::vector<BuildingID> > hallRow;
|
2012-09-02 13:33:41 +03:00
|
|
|
|
|
|
|
BOOST_FOREACH(const JsonNode &box, row.Vector())
|
|
|
|
{
|
2013-02-11 22:11:34 +03:00
|
|
|
std::vector<BuildingID> hallBox;
|
2012-09-02 13:33:41 +03:00
|
|
|
|
|
|
|
BOOST_FOREACH(const JsonNode &value, box.Vector())
|
|
|
|
{
|
2013-02-11 22:11:34 +03:00
|
|
|
hallBox.push_back(BuildingID(value.Float()));
|
2012-09-02 13:33:41 +03:00
|
|
|
}
|
|
|
|
hallRow.push_back(hallBox);
|
2011-08-26 06:58:07 +03:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
town.clientInfo.hallSlots.push_back(hallRow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-05 21:03:49 +03:00
|
|
|
CTown::ClientInfo::Point JsonToPoint(const JsonNode & node)
|
|
|
|
{
|
|
|
|
CTown::ClientInfo::Point ret;
|
|
|
|
ret.x = node["x"].Float();
|
|
|
|
ret.y = node["y"].Float();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownHandler::loadSiegeScreen(CTown &town, const JsonNode & source)
|
|
|
|
{
|
|
|
|
town.clientInfo.siegePrefix = source["imagePrefix"].String();
|
2013-04-25 17:03:35 +03:00
|
|
|
VLC->modh->identifiers.requestIdentifier("creature", source["shooter"], [&town](si32 creature)
|
2012-12-10 23:08:00 +03:00
|
|
|
{
|
2013-02-11 02:24:57 +03:00
|
|
|
town.clientInfo.siegeShooter = CreatureID(creature);
|
2012-12-10 23:08:00 +03:00
|
|
|
});
|
|
|
|
|
2012-10-05 21:03:49 +03:00
|
|
|
town.clientInfo.siegeShooterCropHeight = source["shooterHeight"].Float();
|
|
|
|
|
|
|
|
auto & pos = town.clientInfo.siegePositions;
|
|
|
|
pos.resize(21);
|
|
|
|
|
|
|
|
pos[8] = JsonToPoint(source["towers"]["top"]["tower"]);
|
|
|
|
pos[17] = JsonToPoint(source["towers"]["top"]["battlement"]);
|
|
|
|
pos[20] = JsonToPoint(source["towers"]["top"]["creature"]);
|
|
|
|
|
|
|
|
pos[2] = JsonToPoint(source["towers"]["keep"]["tower"]);
|
|
|
|
pos[15] = JsonToPoint(source["towers"]["keep"]["battlement"]);
|
|
|
|
pos[18] = JsonToPoint(source["towers"]["keep"]["creature"]);
|
|
|
|
|
|
|
|
pos[3] = JsonToPoint(source["towers"]["bottom"]["tower"]);
|
|
|
|
pos[16] = JsonToPoint(source["towers"]["bottom"]["battlement"]);
|
|
|
|
pos[19] = JsonToPoint(source["towers"]["bottom"]["creature"]);
|
|
|
|
|
|
|
|
pos[9] = JsonToPoint(source["gate"]["gate"]);
|
|
|
|
pos[10] = JsonToPoint(source["gate"]["arch"]);
|
|
|
|
|
|
|
|
pos[7] = JsonToPoint(source["walls"]["upper"]);
|
|
|
|
pos[6] = JsonToPoint(source["walls"]["upperMid"]);
|
|
|
|
pos[5] = JsonToPoint(source["walls"]["bottomMid"]);
|
|
|
|
pos[4] = JsonToPoint(source["walls"]["bottom"]);
|
|
|
|
|
|
|
|
pos[13] = JsonToPoint(source["moat"]["moat"]);
|
|
|
|
pos[14] = JsonToPoint(source["moat"]["bank"]);
|
|
|
|
|
|
|
|
pos[11] = JsonToPoint(source["static"]["bottom"]);
|
|
|
|
pos[12] = JsonToPoint(source["static"]["top"]);
|
|
|
|
pos[1] = JsonToPoint(source["static"]["background"]);
|
|
|
|
}
|
|
|
|
|
2013-04-22 22:51:22 +03:00
|
|
|
static void readIcon(JsonNode source, std::string & small, std::string & large)
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
2013-04-22 22:51:22 +03:00
|
|
|
if (source.getType() == JsonNode::DATA_STRUCT) // don't crash on old format
|
2012-12-19 19:35:58 +03:00
|
|
|
{
|
2013-04-22 22:51:22 +03:00
|
|
|
small = source["small"].String();
|
|
|
|
large = source["large"].String();
|
2012-12-19 19:35:58 +03:00
|
|
|
}
|
2013-04-22 22:51:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownHandler::loadClientData(CTown &town, const JsonNode & source)
|
|
|
|
{
|
|
|
|
CTown::ClientInfo & info = town.clientInfo;
|
|
|
|
|
|
|
|
readIcon(source["icons"]["village"]["normal"], info.iconSmall[0][0], info.iconLarge[0][0]);
|
|
|
|
readIcon(source["icons"]["village"]["built"], info.iconSmall[0][1], info.iconLarge[0][1]);
|
|
|
|
readIcon(source["icons"]["fort"]["normal"], info.iconSmall[1][0], info.iconLarge[1][0]);
|
|
|
|
readIcon(source["icons"]["fort"]["built"], info.iconSmall[1][1], info.iconLarge[1][1]);
|
|
|
|
|
|
|
|
info.hallBackground = source["hallBackground"].String();
|
|
|
|
info.musicTheme = source["musicTheme"].String();
|
|
|
|
info.townBackground = source["townBackground"].String();
|
|
|
|
info.guildWindow = source["guildWindow"].String();
|
|
|
|
info.buildingsIcons = source["buildingsIcons"].String();
|
|
|
|
|
|
|
|
info.advMapVillage = source["adventureMap"]["village"].String();
|
|
|
|
info.advMapCastle = source["adventureMap"]["castle"].String();
|
|
|
|
info.advMapCapitol = source["adventureMap"]["capitol"].String();
|
2012-12-19 19:35:58 +03:00
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
loadTownHall(town, source["hallSlots"]);
|
|
|
|
loadStructures(town, source["structures"]);
|
2012-10-05 21:03:49 +03:00
|
|
|
loadSiegeScreen(town, source["siege"]);
|
2012-09-05 15:49:23 +03:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
void CTownHandler::loadTown(CTown &town, const JsonNode & source)
|
|
|
|
{
|
2012-12-11 15:12:46 +03:00
|
|
|
auto resIter = boost::find(GameConstants::RESOURCE_NAMES, source["primaryResource"].String());
|
|
|
|
if (resIter == boost::end(GameConstants::RESOURCE_NAMES))
|
2013-04-21 15:49:26 +03:00
|
|
|
town.primaryRes = Res::WOOD_AND_ORE; //Wood + Ore
|
2012-12-11 15:12:46 +03:00
|
|
|
else
|
|
|
|
town.primaryRes = resIter - boost::begin(GameConstants::RESOURCE_NAMES);
|
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
VLC->modh->identifiers.requestIdentifier("creature", source["warMachine"],
|
2012-12-11 15:12:46 +03:00
|
|
|
[&town](si32 creature)
|
|
|
|
{
|
2013-02-11 02:24:57 +03:00
|
|
|
town.warMachine = CArtHandler::creatureToMachineID(CreatureID(creature));
|
2012-12-11 15:12:46 +03:00
|
|
|
});
|
|
|
|
|
2013-02-09 00:17:39 +03:00
|
|
|
town.moatDamage = source["moatDamage"].Float();
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
town.mageLevel = source["mageGuild"].Float();
|
2012-12-02 15:21:44 +03:00
|
|
|
town.names = source["names"].convertTo<std::vector<std::string> >();
|
2012-09-02 13:33:41 +03:00
|
|
|
|
|
|
|
// Horde building creature level
|
|
|
|
BOOST_FOREACH(const JsonNode &node, source["horde"].Vector())
|
|
|
|
{
|
|
|
|
town.hordeLvl[town.hordeLvl.size()] = node.Float();
|
2008-01-19 13:55:04 +02:00
|
|
|
}
|
2008-01-20 18:24:03 +02:00
|
|
|
|
2012-12-03 19:00:17 +03:00
|
|
|
const JsonVector & creatures = source["creatures"].Vector();
|
|
|
|
|
|
|
|
town.creatures.resize(creatures.size());
|
|
|
|
|
|
|
|
for (size_t i=0; i< creatures.size(); i++)
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
2012-12-03 19:00:17 +03:00
|
|
|
const JsonVector & level = creatures[i].Vector();
|
|
|
|
|
|
|
|
town.creatures[i].resize(level.size());
|
|
|
|
|
|
|
|
for (size_t j=0; j<level.size(); j++)
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
2013-04-25 17:03:35 +03:00
|
|
|
VLC->modh->identifiers.requestIdentifier("creature", level[j], [=, &town](si32 creature)
|
2012-12-03 19:00:17 +03:00
|
|
|
{
|
2013-02-11 02:24:57 +03:00
|
|
|
town.creatures[i][j] = CreatureID(creature);
|
2012-12-03 19:00:17 +03:00
|
|
|
});
|
2008-01-27 15:18:18 +02:00
|
|
|
}
|
2012-12-17 15:55:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// set chance of specific hero class to appear in this town
|
|
|
|
BOOST_FOREACH(auto &node, source["tavern"].Struct())
|
|
|
|
{
|
|
|
|
int chance = node.second.Float();
|
2012-12-03 19:00:17 +03:00
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
VLC->modh->identifiers.requestIdentifier(node.second.meta, "heroClass",node.first, [=, &town](si32 classID)
|
2012-12-17 15:55:29 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
VLC->heroh->classes.heroClasses[classID]->selectionProbability[town.faction->index] = chance;
|
2012-12-17 15:55:29 +03:00
|
|
|
});
|
2008-01-27 15:18:18 +02:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2012-12-18 13:32:11 +03:00
|
|
|
BOOST_FOREACH(auto &node, source["guildSpells"].Struct())
|
|
|
|
{
|
|
|
|
int chance = node.second.Float();
|
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
VLC->modh->identifiers.requestIdentifier(node.second.meta, "spell", node.first, [=, &town](si32 spellID)
|
2012-12-18 13:32:11 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
SpellID(spellID).toSpell()->probabilities[town.faction->index] = chance;
|
2012-12-18 13:32:11 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-04-22 22:51:22 +03:00
|
|
|
BOOST_FOREACH (const JsonNode &d, source["adventureMap"]["dwellings"].Vector())
|
|
|
|
{
|
|
|
|
town.dwellings.push_back (d["graphics"].String());
|
|
|
|
town.dwellingNames.push_back (d["name"].String());
|
|
|
|
}
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
loadBuildings(town, source["buildings"]);
|
|
|
|
loadClientData(town,source);
|
2008-08-02 18:08:03 +03:00
|
|
|
}
|
2011-08-26 05:39:58 +03:00
|
|
|
|
2012-09-21 00:28:18 +03:00
|
|
|
void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source)
|
|
|
|
{
|
|
|
|
faction.puzzleMap.reserve(GameConstants::PUZZLE_MAP_PIECES);
|
|
|
|
|
|
|
|
std::string prefix = source["prefix"].String();
|
|
|
|
BOOST_FOREACH(const JsonNode &piece, source["pieces"].Vector())
|
|
|
|
{
|
|
|
|
size_t index = faction.puzzleMap.size();
|
|
|
|
SPuzzleInfo spi;
|
|
|
|
|
|
|
|
spi.x = piece["x"].Float();
|
|
|
|
spi.y = piece["y"].Float();
|
|
|
|
spi.whenUncovered = piece["index"].Float();
|
|
|
|
spi.number = index;
|
|
|
|
|
|
|
|
// filename calculation
|
|
|
|
std::ostringstream suffix;
|
|
|
|
suffix << std::setfill('0') << std::setw(2) << index;
|
|
|
|
|
|
|
|
spi.filename = prefix + suffix.str();
|
|
|
|
|
|
|
|
faction.puzzleMap.push_back(spi);
|
|
|
|
}
|
|
|
|
assert(faction.puzzleMap.size() == GameConstants::PUZZLE_MAP_PIECES);
|
|
|
|
}
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
CFaction * CTownHandler::loadFromJson(const JsonNode &source)
|
2008-12-22 19:48:41 +02:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
CFaction * faction = new CFaction();
|
2012-12-16 16:47:53 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
faction->name = source["name"].String();
|
2012-09-05 15:49:23 +03:00
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
VLC->modh->identifiers.requestIdentifier ("creature", source["commander"],
|
2013-03-02 19:55:51 +03:00
|
|
|
[=](si32 commanderID)
|
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
faction->commander = CreatureID(commanderID);
|
2013-03-02 19:55:51 +03:00
|
|
|
});
|
2012-12-19 19:35:58 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
faction->creatureBg120 = source["creatureBackground"]["120px"].String();
|
|
|
|
faction->creatureBg130 = source["creatureBackground"]["130px"].String();
|
2012-09-05 15:49:23 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
faction->nativeTerrain = ETerrainType(vstd::find_pos(GameConstants::TERRAIN_NAMES,
|
2013-03-02 19:55:51 +03:00
|
|
|
source["nativeTerrain"].String()));
|
|
|
|
int alignment = vstd::find_pos(EAlignment::names, source["alignment"].String());
|
|
|
|
if (alignment == -1)
|
2013-04-21 15:49:26 +03:00
|
|
|
faction->alignment = EAlignment::NEUTRAL;
|
2013-03-02 19:55:51 +03:00
|
|
|
else
|
2013-04-21 15:49:26 +03:00
|
|
|
faction->alignment = static_cast<EAlignment::EAlignment>(alignment);
|
2012-11-13 14:52:23 +03:00
|
|
|
|
2013-03-02 19:55:51 +03:00
|
|
|
if (!source["town"].isNull())
|
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
faction->town = new CTown;
|
|
|
|
faction->town->faction = faction;
|
|
|
|
loadTown(*faction->town, source["town"]);
|
2012-09-05 15:49:23 +03:00
|
|
|
}
|
2013-04-21 17:08:46 +03:00
|
|
|
else
|
|
|
|
faction->town = nullptr;
|
|
|
|
|
2013-03-02 19:55:51 +03:00
|
|
|
if (!source["puzzleMap"].isNull())
|
2013-04-21 15:49:26 +03:00
|
|
|
loadPuzzle(*faction, source["puzzleMap"]);
|
2013-03-02 19:55:51 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
return faction;
|
2008-12-22 19:48:41 +02:00
|
|
|
}
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
|
2008-12-22 19:48:41 +02:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
auto object = loadFromJson(data);
|
|
|
|
object->index = factions.size();
|
2013-04-22 22:51:22 +03:00
|
|
|
if (object->town)
|
|
|
|
{
|
|
|
|
auto & info = object->town->clientInfo;
|
|
|
|
info.icons[0][0] = 8 + object->index * 4 + 0;
|
|
|
|
info.icons[0][1] = 8 + object->index * 4 + 1;
|
|
|
|
info.icons[1][0] = 8 + object->index * 4 + 2;
|
|
|
|
info.icons[1][1] = 8 + object->index * 4 + 3;
|
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
factions.push_back(object);
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
VLC->modh->identifiers.registerObject(scope, "faction", name, object->index);
|
|
|
|
}
|
2012-10-05 16:11:26 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
|
|
|
|
{
|
|
|
|
auto object = loadFromJson(data);
|
|
|
|
object->index = index;
|
2013-04-22 22:51:22 +03:00
|
|
|
if (object->town)
|
|
|
|
{
|
|
|
|
auto & info = object->town->clientInfo;
|
|
|
|
info.icons[0][0] = (GameConstants::F_NUMBER + object->index) * 2 + 0;
|
|
|
|
info.icons[0][1] = (GameConstants::F_NUMBER + object->index) * 2 + 1;
|
|
|
|
info.icons[1][0] = object->index * 2 + 0;
|
|
|
|
info.icons[1][1] = object->index * 2 + 1;
|
|
|
|
}
|
2012-10-05 16:11:26 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
assert(factions[index] == nullptr); // ensure that this id was not loaded before
|
|
|
|
factions[index] = object;
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
VLC->modh->identifiers.registerObject(scope, "faction", name, object->index);
|
2009-10-04 05:02:45 +03:00
|
|
|
}
|
2012-11-20 20:53:45 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
std::vector<bool> CTownHandler::getDefaultAllowed() const
|
2012-11-20 20:53:45 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
std::vector<bool> allowedFactions;
|
|
|
|
BOOST_FOREACH(auto town, factions)
|
2012-11-20 20:53:45 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
allowedFactions.push_back(town->town != nullptr);
|
2012-11-20 20:53:45 +03:00
|
|
|
}
|
|
|
|
return allowedFactions;
|
|
|
|
}
|