1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/lib/CTownHandler.cpp
Ivan Savenko 7e778045b8 - removed no longer used CSndHandler. Transition to new fs is finished
- moved TownHandler::requirements to CBuilding class
2012-08-09 10:32:50 +00:00

179 lines
4.8 KiB
C++

#include "StdInc.h"
#include "CTownHandler.h"
#include "VCMI_Lib.h"
#include "CGeneralTextHandler.h"
#include "JsonNode.h"
#include "GameConstants.h"
#include "Filesystem/CResourceLoader.h"
/*
* 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
*
*/
CTownHandler::CTownHandler()
{
VLC->townh = this;
}
CTownHandler::~CTownHandler()
{
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++)
delete j->second;
}
void CTownHandler::loadStructures()
{
int townID;
for (townID=0; townID<GameConstants::F_NUMBER; townID++)
{
CTown town;
town.typeID=townID;
town.bonus=towns.size();
if (town.bonus==8) town.bonus=3;
towns.push_back(town);
}
for(int x=0;x<towns.size();x++)
towns[x].creatures.resize(GameConstants::CREATURES_PER_TOWN);
structures.resize(GameConstants::F_NUMBER);
// read city properties
const JsonNode config(ResourceID("config/buildings.json"));
// Iterate for each city type
townID = 0;
BOOST_FOREACH(const JsonNode &town_node, config["town_type"].Vector()) {
int level;
std::map<int, Structure*> &town = structures[townID];
// Read buildings coordinates for that city
BOOST_FOREACH(const JsonNode &node, town_node["defnames"].Vector()) {
Structure *vinya = new Structure;
const JsonNode *value;
vinya->group = -1;
vinya->townID = townID;
vinya->ID = node["id"].Float();
vinya->defName = node["defname"].String();
vinya->name = vinya->defName; //TODO - use normal names
vinya->pos.x = node["x"].Float();
vinya->pos.y = node["y"].Float();
vinya->pos.z = 0;
value = &node["border"];
if (!value->isNull())
vinya->borderName = value->String();
value = &node["area"];
if (!value->isNull())
vinya->areaName = value->String();
town[vinya->ID] = vinya;
}
// Read buildings blit order for that city
int itr = 1;
BOOST_FOREACH(const JsonNode &node, town_node["blit_order"].Vector()) {
int buildingID = node.Float();
/* 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++;
else
tlog3 << "Warning1: No building " << buildingID << " in the castle " << townID << std::endl;
}
// Read creatures belonging to that city
level = 0;
BOOST_FOREACH(const JsonNode &list, town_node["creatures"].Vector())
{
BOOST_FOREACH(const JsonNode &node, list.Vector())
{
towns[townID].creatures[level].push_back(node.Float());
}
level ++;
}
// Horde building creature level
level = 0;
BOOST_FOREACH(const JsonNode &node, town_node["horde"].Vector()) {
towns[townID].hordeLvl[level] = node.Float();
level ++;
}
// Misc.
towns[townID].mageLevel = town_node["mage_guild"].Float();
towns[townID].primaryRes = town_node["primary_resource"].Float();
towns[townID].warMachine = town_node["war_machine"].Float();
townID ++;
}
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;
}
}
}
}
}
}
}
}
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];
}