2008-06-13 11:16:51 +03:00
|
|
|
#define VCMI_DLL
|
2008-01-09 19:21:31 +02:00
|
|
|
#include "../stdafx.h"
|
2007-07-26 15:00:18 +03:00
|
|
|
#include "CTownHandler.h"
|
2007-08-29 15:18:31 +03:00
|
|
|
#include "CLodHandler.h"
|
2008-08-02 18:08:03 +03:00
|
|
|
#include <sstream>
|
2008-06-30 03:06:41 +03:00
|
|
|
#include "../lib/VCMI_Lib.h"
|
2008-12-22 19:48:41 +02:00
|
|
|
#include "CGeneralTextHandler.h"
|
2011-08-20 07:48:23 +03:00
|
|
|
#include "../lib/JsonNode.h"
|
2011-08-26 05:39:58 +03:00
|
|
|
#include <boost/foreach.hpp>
|
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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-07-26 15:00:18 +03:00
|
|
|
CTownHandler::CTownHandler()
|
2008-06-30 03:06:41 +03:00
|
|
|
{
|
|
|
|
VLC->townh = this;
|
|
|
|
}
|
2007-07-26 15:00:18 +03:00
|
|
|
CTownHandler::~CTownHandler()
|
2008-12-22 19:48:41 +02:00
|
|
|
{
|
2010-08-16 12:54:09 +03:00
|
|
|
for( std::vector<std::map<int, Structure*> >::iterator i= structures.begin(); i!=structures.end(); i++)
|
|
|
|
for( std::map<int, Structure*>::iterator j = i->begin(); j!=i->end(); j++)
|
2008-12-22 19:48:41 +02:00
|
|
|
delete j->second;
|
|
|
|
}
|
2011-08-26 06:10:56 +03:00
|
|
|
void CTownHandler::loadStructures()
|
2007-07-26 15:00:18 +03:00
|
|
|
{
|
2011-08-26 06:10:56 +03:00
|
|
|
int townID;
|
|
|
|
|
|
|
|
for (townID=0; townID<F_NUMBER; townID++)
|
2007-07-26 15:00:18 +03:00
|
|
|
{
|
|
|
|
CTown town;
|
2011-08-26 06:10:56 +03:00
|
|
|
town.typeID=townID;
|
2007-07-26 15:00:18 +03:00
|
|
|
town.bonus=towns.size();
|
2008-08-02 18:08:03 +03:00
|
|
|
if (town.bonus==8) town.bonus=3;
|
2008-12-22 19:48:41 +02:00
|
|
|
towns.push_back(town);
|
|
|
|
}
|
|
|
|
|
2011-08-26 06:43:43 +03:00
|
|
|
for(int x=0;x<towns.size();x++) {
|
|
|
|
/* There is actually 8 basic creatures, but we ignore the 8th
|
|
|
|
* entry for now */
|
|
|
|
towns[x].basicCreatures.resize(7);
|
|
|
|
towns[x].upgradedCreatures.resize(7);
|
|
|
|
}
|
|
|
|
|
2010-08-16 12:54:09 +03:00
|
|
|
structures.resize(F_NUMBER);
|
2011-08-20 07:48:23 +03:00
|
|
|
|
2011-08-20 09:08:48 +03:00
|
|
|
// read city properties
|
2011-08-20 07:48:23 +03:00
|
|
|
const JsonNode config(DATA_DIR "/config/buildings.json");
|
|
|
|
|
2011-08-20 09:08:48 +03:00
|
|
|
// Iterate for each city type
|
2011-08-26 06:10:56 +03:00
|
|
|
townID = 0;
|
|
|
|
BOOST_FOREACH(const JsonNode &town_node, config["town_type"].Vector()) {
|
2011-08-26 06:43:43 +03:00
|
|
|
int level;
|
2011-08-20 09:08:48 +03:00
|
|
|
std::map<int, Structure*> &town = structures[townID];
|
2008-01-19 13:55:04 +02:00
|
|
|
|
2011-08-20 09:08:48 +03:00
|
|
|
// Read buildings coordinates for that city
|
2011-08-26 06:10:56 +03:00
|
|
|
BOOST_FOREACH(const JsonNode &node, town_node["defnames"].Vector()) {
|
2011-08-20 07:48:23 +03:00
|
|
|
Structure *vinya = new Structure;
|
2011-08-20 19:03:27 +03:00
|
|
|
const JsonNode *value;
|
2011-08-20 07:48:23 +03:00
|
|
|
|
|
|
|
vinya->group = -1;
|
2011-08-20 09:08:48 +03:00
|
|
|
vinya->townID = townID;
|
2011-08-20 19:03:27 +03:00
|
|
|
vinya->ID = node["id"].Float();
|
|
|
|
vinya->defName = node["defname"].String();
|
2011-08-20 07:48:23 +03:00
|
|
|
vinya->name = vinya->defName; //TODO - use normal names
|
2011-08-20 19:03:27 +03:00
|
|
|
vinya->pos.x = node["x"].Float();
|
|
|
|
vinya->pos.y = node["y"].Float();
|
2011-08-20 07:48:23 +03:00
|
|
|
vinya->pos.z = 0;
|
2011-08-20 19:03:27 +03:00
|
|
|
|
|
|
|
value = &node["border"];
|
|
|
|
if (!value->isNull())
|
|
|
|
vinya->borderName = value->String();
|
|
|
|
|
|
|
|
value = &node["area"];
|
|
|
|
if (!value->isNull())
|
|
|
|
vinya->areaName = value->String();
|
|
|
|
|
2011-08-20 09:08:48 +03:00
|
|
|
town[vinya->ID] = vinya;
|
2011-08-20 07:48:23 +03:00
|
|
|
}
|
2011-08-20 09:08:48 +03:00
|
|
|
|
|
|
|
// Read buildings blit order for that city
|
|
|
|
int itr = 1;
|
2011-08-26 06:43:43 +03:00
|
|
|
BOOST_FOREACH(const JsonNode &node, town_node["blit_order"].Vector()) {
|
2011-08-20 19:03:27 +03:00
|
|
|
int buildingID = node.Float();
|
2011-08-20 09:08:48 +03:00
|
|
|
|
|
|
|
/* Find the building and set its order. */
|
|
|
|
std::map<int, Structure*>::iterator i2 = town.find(buildingID);
|
|
|
|
if (i2 != (town.end()))
|
|
|
|
i2->second->pos.z = itr++;
|
2008-01-19 13:55:04 +02:00
|
|
|
else
|
2011-08-20 09:08:48 +03:00
|
|
|
tlog3 << "Warning1: No building " << buildingID << " in the castle " << townID << std::endl;
|
2008-01-19 13:55:04 +02:00
|
|
|
}
|
2011-08-26 06:10:56 +03:00
|
|
|
|
2011-08-26 06:43:43 +03:00
|
|
|
// Read basic creatures belonging to that city
|
|
|
|
level = 0;
|
|
|
|
BOOST_FOREACH(const JsonNode &node, town_node["creatures_basic"].Vector()) {
|
|
|
|
// This if ignores the 8th field (WoG creature)
|
|
|
|
if (level < towns[townID].basicCreatures.size())
|
|
|
|
towns[townID].basicCreatures[level] = node.Float();
|
|
|
|
level ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read upgraded creatures belonging to that city
|
|
|
|
level = 0;
|
|
|
|
BOOST_FOREACH(const JsonNode &node, town_node["creatures_upgraded"].Vector()) {
|
|
|
|
towns[townID].upgradedCreatures[level] = node.Float();
|
|
|
|
level ++;
|
|
|
|
}
|
|
|
|
|
2011-08-26 06:58:07 +03:00
|
|
|
// Horde building creature level
|
|
|
|
level = 0;
|
|
|
|
BOOST_FOREACH(const JsonNode &node, town_node["horde"].Vector()) {
|
|
|
|
towns[townID].hordeLvl[level] = node.Float();
|
|
|
|
level ++;
|
|
|
|
}
|
|
|
|
|
2011-08-27 04:17:23 +03:00
|
|
|
// Buildings dependencies. Which building depend on which other building.
|
|
|
|
requirements.resize(F_NUMBER);
|
|
|
|
BOOST_FOREACH(const JsonNode &node, town_node["building_requirements"].Vector()) {
|
|
|
|
std::set<int> &requires = requirements[townID][node["id"].Float()];
|
|
|
|
|
|
|
|
BOOST_FOREACH(const JsonNode &building, node["requires"].Vector()) {
|
|
|
|
requires.insert(building.Float());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 07:12:46 +03:00
|
|
|
// Misc.
|
|
|
|
towns[townID].mageLevel = town_node["mage_guild"].Float();
|
|
|
|
towns[townID].primaryRes = town_node["primary_ressource"].Float();
|
|
|
|
towns[townID].warMachine = town_node["war_machine"].Float();
|
|
|
|
|
2011-08-26 06:10:56 +03:00
|
|
|
townID ++;
|
2008-01-19 13:55:04 +02:00
|
|
|
}
|
2008-01-20 18:24:03 +02:00
|
|
|
|
2011-08-26 05:39:58 +03:00
|
|
|
int group_num=0;
|
|
|
|
|
|
|
|
// Iterate for each city
|
|
|
|
BOOST_FOREACH(const JsonNode &town_node, config["town_groups"].Vector()) {
|
|
|
|
townID = town_node["id"].Float();
|
|
|
|
|
|
|
|
// Iterate for each group for that city
|
|
|
|
BOOST_FOREACH(const JsonNode &group, town_node["groups"].Vector()) {
|
|
|
|
|
|
|
|
group_num ++;
|
|
|
|
|
|
|
|
// Iterate for each bulding value in the group
|
|
|
|
BOOST_FOREACH(const JsonNode &value, group.Vector()) {
|
|
|
|
int buildingID = value.Float();
|
|
|
|
|
|
|
|
std::vector<std::map<int, Structure*> >::iterator i;
|
|
|
|
std::map<int, Structure*>::iterator i2;
|
|
|
|
|
|
|
|
if (townID >= 0) {
|
|
|
|
if ((i = structures.begin() + townID) != structures.end()) {
|
|
|
|
if ((i2=(i->find(buildingID)))!=(i->end()))
|
|
|
|
i2->second->group = group_num;
|
|
|
|
else
|
|
|
|
tlog3 << "Warning3: No building "<<buildingID<<" in the castle "<<townID<<std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tlog3 << "Warning3: Castle "<<townID<<" not defined."<<std::endl;
|
|
|
|
} else {
|
|
|
|
// Set group for selected building in ALL castles
|
|
|
|
for (i=structures.begin();i!=structures.end();i++) {
|
|
|
|
for(i2=i->begin(); i2!=i->end(); i2++) {
|
|
|
|
if(i2->first == buildingID) {
|
|
|
|
i2->second->group = group_num;
|
|
|
|
break;
|
2008-01-27 15:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 05:39:58 +03:00
|
|
|
}
|
2008-01-27 15:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-02 18:08:03 +03:00
|
|
|
}
|
2011-08-26 05:39:58 +03:00
|
|
|
|
2008-12-22 19:48:41 +02: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];
|
2009-10-04 05:02:45 +03:00
|
|
|
}
|