#include "StdInc.h" #include "CTownHandler.h" #include "CLodHandler.h" #include "../lib/VCMI_Lib.h" #include "CGeneralTextHandler.h" #include "../lib/JsonNode.h" #include "GameConstants.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 >::iterator i= structures.begin(); i!=structures.end(); i++) for( std::map::iterator j = i->begin(); j!=i->end(); j++) delete j->second; } void CTownHandler::loadStructures() { int townID; for (townID=0; townID &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::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 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 ++; } // Horde building creature level level = 0; BOOST_FOREACH(const JsonNode &node, town_node["horde"].Vector()) { towns[townID].hordeLvl[level] = node.Float(); level ++; } // Buildings dependencies. Which building depend on which other building. requirements.resize(GameConstants::F_NUMBER); BOOST_FOREACH(const JsonNode &node, town_node["building_requirements"].Vector()) { std::set &requires = requirements[townID][node["id"].Float()]; BOOST_FOREACH(const JsonNode &building, node["requires"].Vector()) { requires.insert(building.Float()); } } // 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 >::iterator i; std::map::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 "<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 & CTown::Names() const { if(names.size()) return names; else return VLC->generaltexth->townNames[typeID]; }