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"
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "GameConstants.h"
|
2012-08-02 14:03:26 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
const std::string & CBuilding::Name() const
|
|
|
|
{
|
|
|
|
if(name.length())
|
|
|
|
return name;
|
|
|
|
else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
|
|
|
|
return VLC->generaltexth->buildings[tid][bid].first;
|
|
|
|
tlog2 << "Warning: Cannot find name text for building " << bid << "for " << tid << "town.\n";
|
2012-09-05 15:49:23 +03:00
|
|
|
return name;
|
2012-09-02 13:33:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & CBuilding::Description() const
|
|
|
|
{
|
|
|
|
if(description.length())
|
|
|
|
return description;
|
|
|
|
else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
|
|
|
|
return VLC->generaltexth->buildings[tid][bid].second;
|
|
|
|
tlog2 << "Warning: Cannot find description text for building " << bid << "for " << tid << "town.\n";
|
2012-09-05 15:49:23 +03:00
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBuilding::BuildingType CBuilding::getBase() const
|
|
|
|
{
|
|
|
|
const CBuilding * build = this;
|
|
|
|
while (build->upgrade >= 0)
|
|
|
|
build = VLC->townh->towns[build->tid].buildings[build->upgrade];
|
|
|
|
|
|
|
|
return build->bid;
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 CBuilding::getDistance(CBuilding::BuildingType buildID) const
|
|
|
|
{
|
|
|
|
const CBuilding * build = VLC->townh->towns[tid].buildings[buildID];
|
|
|
|
int distance = 0;
|
|
|
|
while (build->upgrade >= 0 && build != this)
|
|
|
|
{
|
|
|
|
build = VLC->townh->towns[build->tid].buildings[build->upgrade];
|
|
|
|
distance++;
|
|
|
|
}
|
|
|
|
if (build == this)
|
|
|
|
return distance;
|
|
|
|
return -1;
|
2012-09-02 13:33:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & CTown::Name() const
|
|
|
|
{
|
|
|
|
if(name.length())
|
|
|
|
return name;
|
|
|
|
else
|
|
|
|
return VLC->generaltexth->townTypes[typeID];
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string> & CTown::Names() const
|
|
|
|
{
|
|
|
|
if(names.size())
|
|
|
|
return names;
|
|
|
|
else
|
|
|
|
return VLC->generaltexth->townNames[typeID];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
parser.endLine();
|
|
|
|
return ret;
|
2008-12-22 19:48:41 +02:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
|
|
|
|
void CTownHandler::loadLegacyData(JsonNode & dest)
|
2007-07-26 15:00:18 +03:00
|
|
|
{
|
2012-09-02 13:33:41 +03:00
|
|
|
CLegacyConfigParser parser("DATA/BUILDING.TXT");
|
|
|
|
dest.Vector().resize(GameConstants::F_NUMBER);
|
|
|
|
|
|
|
|
parser.endLine(); // header
|
|
|
|
parser.endLine();
|
2011-08-26 06:10:56 +03:00
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
//Unique buildings
|
|
|
|
for (size_t town=0; town<GameConstants::F_NUMBER; town++)
|
2007-07-26 15:00:18 +03:00
|
|
|
{
|
2012-09-02 13:33:41 +03:00
|
|
|
JsonVector & buildList = dest.Vector()[town].Vector();
|
|
|
|
|
|
|
|
buildList.resize( 30 ); //prepare vector for first set of buildings
|
2008-12-22 19:48:41 +02:00
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
parser.endLine(); //header
|
|
|
|
parser.endLine();
|
|
|
|
|
|
|
|
int buildID = 17;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
buildList[buildID] = readBuilding(parser);
|
|
|
|
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
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
for (size_t town=0; town<GameConstants::F_NUMBER; town++)
|
|
|
|
dest.Vector()[town].Vector()[buildID] = building;
|
|
|
|
|
|
|
|
buildID++;
|
|
|
|
}
|
|
|
|
while (!parser.isNextEntryEmpty());
|
|
|
|
|
|
|
|
parser.endLine(); //header
|
|
|
|
parser.endLine();
|
|
|
|
|
|
|
|
//Dwellings
|
|
|
|
for (size_t town=0; town<GameConstants::F_NUMBER; town++)
|
|
|
|
{
|
|
|
|
parser.endLine(); //header
|
|
|
|
parser.endLine();
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
dest.Vector()[town].Vector().push_back(readBuilding(parser));
|
2008-01-19 13:55:04 +02:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
while (!parser.isNextEntryEmpty());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"};
|
|
|
|
|
|
|
|
ret->mode = boost::find(modes, source["mode"].String()) - modes;
|
2012-09-02 13:33:41 +03:00
|
|
|
|
|
|
|
ret->tid = town.typeID;
|
|
|
|
ret->bid = 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())
|
|
|
|
ret->requirements.insert(building.Float());
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
if (!source["upgrades"].isNull())
|
|
|
|
{
|
|
|
|
ret->requirements.insert(source["upgrades"].Float());
|
|
|
|
ret->upgrade = source["upgrades"].Float();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret->upgrade = -1;
|
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
town.buildings[ret->bid] = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownHandler::loadBuildings(CTown &town, const JsonNode & source)
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(const JsonNode &node, source.Vector())
|
|
|
|
{
|
|
|
|
loadBuilding(town, node);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
ret->building = town.buildings[source["id"].Float()];
|
|
|
|
|
|
|
|
if (source["builds"].isNull())
|
|
|
|
ret->buildable = ret->building;
|
|
|
|
else
|
|
|
|
ret->buildable = town.buildings[source["builds"].Float()];
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2012-09-05 15:49:23 +03:00
|
|
|
BOOST_FOREACH(const JsonNode &node, source.Vector())
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
|
|
|
loadStructure(town, node);
|
|
|
|
}
|
|
|
|
}
|
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())
|
|
|
|
{
|
|
|
|
std::vector< std::vector<int> > hallRow;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const JsonNode &box, row.Vector())
|
|
|
|
{
|
|
|
|
std::vector<int> hallBox;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const JsonNode &value, box.Vector())
|
|
|
|
{
|
|
|
|
hallBox.push_back(value.Float());
|
|
|
|
}
|
|
|
|
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-09-05 15:49:23 +03:00
|
|
|
void CTownHandler::loadClientData(CTown &town, const JsonNode & source)
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
2012-09-22 18:10:15 +03:00
|
|
|
town.clientInfo.icons[0][0] = source["icons"]["village"]["normal"].Float();
|
|
|
|
town.clientInfo.icons[0][1] = source["icons"]["village"]["built"].Float();
|
|
|
|
town.clientInfo.icons[1][0] = source["icons"]["fort"]["normal"].Float();
|
|
|
|
town.clientInfo.icons[1][1] = source["icons"]["fort"]["built"].Float();
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
town.clientInfo.hallBackground = source["hallBackground"].String();
|
|
|
|
town.clientInfo.musicTheme = source["musicTheme"].String();
|
|
|
|
town.clientInfo.townBackground = source["townBackground"].String();
|
|
|
|
town.clientInfo.guildWindow = source["guildWindow"].String();
|
|
|
|
town.clientInfo.buildingsIcons = source["buildingsIcons"].String();
|
2011-08-26 07:12:46 +03:00
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
loadTownHall(town, source["hallSlots"]);
|
|
|
|
loadStructures(town, source["structures"]);
|
|
|
|
}
|
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-09-02 13:33:41 +03:00
|
|
|
town.bonus = town.typeID;
|
|
|
|
if (town.bonus==8)
|
|
|
|
town.bonus=3;
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
town.mageLevel = source["mageGuild"].Float();
|
|
|
|
town.primaryRes = source["primaryResource"].Float();
|
|
|
|
town.warMachine = source["warMachine"].Float();
|
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-09-02 13:33:41 +03:00
|
|
|
BOOST_FOREACH(const JsonNode &list, source["creatures"].Vector())
|
|
|
|
{
|
2012-09-26 16:13:39 +03:00
|
|
|
std::vector<TCreature> level;
|
2012-09-02 13:33:41 +03:00
|
|
|
BOOST_FOREACH(const JsonNode &node, list.Vector())
|
|
|
|
{
|
|
|
|
level.push_back(node.Float());
|
2008-01-27 15:18:18 +02:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
town.creatures.push_back(level);
|
2008-01-27 15:18:18 +02:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
void CTownHandler::loadFactions(const JsonNode &source)
|
2008-12-22 19:48:41 +02:00
|
|
|
{
|
2012-09-05 15:49:23 +03:00
|
|
|
BOOST_FOREACH(auto & node, source.Struct())
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
2012-09-05 15:49:23 +03:00
|
|
|
int id = node.second["index"].Float();
|
|
|
|
CFaction & faction = factions[id];
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
faction.factionID = id;
|
|
|
|
faction.name = node.first;
|
|
|
|
|
|
|
|
faction.creatureBg120 = node.second["creatureBackground"]["120px"].String();
|
|
|
|
faction.creatureBg130 = node.second["creatureBackground"]["130px"].String();
|
|
|
|
|
|
|
|
if (!node.second["nativeTerrain"].isNull())
|
|
|
|
{
|
|
|
|
//get terrain as string and converto to numeric ID
|
|
|
|
faction.nativeTerrain = boost::find(GameConstants::TERRAIN_NAMES, node.second["nativeTerrain"].String()) - GameConstants::TERRAIN_NAMES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
faction.nativeTerrain = -1;
|
|
|
|
|
|
|
|
if (!node.second["town"].isNull())
|
|
|
|
{
|
|
|
|
towns[id].typeID = id;
|
|
|
|
loadTown(towns[id], node.second["town"]);
|
|
|
|
}
|
2012-09-21 00:28:18 +03:00
|
|
|
if (!node.second["puzzleMap"].isNull())
|
|
|
|
loadPuzzle(faction, node.second["puzzleMap"]);
|
2012-09-05 15:49:23 +03:00
|
|
|
}
|
2008-12-22 19:48:41 +02:00
|
|
|
}
|
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
void CTownHandler::load()
|
2008-12-22 19:48:41 +02:00
|
|
|
{
|
2012-09-02 13:33:41 +03:00
|
|
|
JsonNode buildingsConf(ResourceID("config/buildings.json"));
|
|
|
|
|
|
|
|
JsonNode legacyConfig;
|
|
|
|
loadLegacyData(legacyConfig);
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
//hardocoded list of H3 factions. Should be only used to convert H3 configs
|
|
|
|
static const std::string factionName [GameConstants::F_NUMBER] =
|
|
|
|
{
|
|
|
|
"castle", "rampart", "tower",
|
|
|
|
"inferno", "necropolis", "dungeon",
|
|
|
|
"stronghold", "fortress", "conflux"
|
|
|
|
};
|
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
// semi-manually merge legacy config with towns json
|
|
|
|
// legacy config have only one item: town buildings stored in 2d vector
|
2012-09-05 15:49:23 +03:00
|
|
|
|
|
|
|
for (size_t i=0; i< legacyConfig.Vector().size(); i++)
|
2012-09-02 13:33:41 +03:00
|
|
|
{
|
2012-09-05 15:49:23 +03:00
|
|
|
JsonNode & buildings = buildingsConf[factionName[i]]["town"]["buildings"];
|
2012-09-02 13:33:41 +03:00
|
|
|
BOOST_FOREACH(JsonNode & building, buildings.Vector())
|
|
|
|
{
|
2012-09-05 15:49:23 +03:00
|
|
|
JsonNode & legacyFaction = legacyConfig.Vector()[i];
|
2012-09-02 13:33:41 +03:00
|
|
|
if (vstd::contains(building.Struct(), "id"))
|
|
|
|
{
|
2012-09-05 15:49:23 +03:00
|
|
|
//find same buildings in legacy and json configs
|
|
|
|
JsonNode & legacyBuilding = legacyFaction.Vector()[building["id"].Float()];
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
if (!legacyBuilding.isNull()) //merge if h3 config was found for this building
|
2012-09-02 13:33:41 +03:00
|
|
|
JsonNode::merge(building, legacyBuilding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
loadFactions(buildingsConf);
|
2009-10-04 05:02:45 +03:00
|
|
|
}
|