#include "StdInc.h" #include "CTownHandler.h" #include "VCMI_Lib.h" #include "CGeneralTextHandler.h" #include "JsonNode.h" #include "StringConstants.h" #include "CCreatureHandler.h" #include "CModHandler.h" #include "CHeroHandler.h" #include "CArtHandler.h" #include "CSpellHandler.h" #include "filesystem/Filesystem.h" #include "CObjectClassesHandler.h" #include "CObjectHandler.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 * */ const int NAMES_PER_TOWN=16; // number of town names per faction in H3 files. Json can define any number const std::string & CBuilding::Name() const { return name; } const std::string & CBuilding::Description() const { return description; } BuildingID CBuilding::getBase() const { const CBuilding * build = this; while (build->upgrade >= 0) { build = build->town->buildings.at(build->upgrade); } return build->bid; } si32 CBuilding::getDistance(BuildingID buildID) const { const CBuilding * build = town->buildings.at(buildID); int distance = 0; while (build->upgrade >= 0 && build != this) { build = build->town->buildings.at(build->upgrade); distance++; } if (build == this) return distance; return -1; } CFaction::CFaction() { town = nullptr; } CFaction::~CFaction() { delete town; } CTown::CTown() { } CTown::~CTown() { for(auto & build : buildings) build.second.dellNull(); for(auto & str : clientInfo.structures) str.dellNull(); } CTownHandler::CTownHandler() { VLC->townh = this; } CTownHandler::~CTownHandler() { for(auto faction : factions) faction.dellNull(); } JsonNode readBuilding(CLegacyConfigParser & parser) { JsonNode ret; JsonNode & cost = ret["cost"]; //note: this code will try to parse mithril as well but wil always return 0 for it for(const std::string & resID : GameConstants::RESOURCE_NAMES) cost[resID].Float() = parser.readNumber(); cost.Struct().erase("mithril"); // erase mithril to avoid confusing validator parser.endLine(); return ret; } std::vector CTownHandler::loadLegacyData(size_t dataSize) { std::vector dest(dataSize); factions.resize(dataSize); auto getBuild = [&](size_t town, size_t building) -> JsonNode & { return dest[town]["town"]["buildings"][EBuildingType::names[building]]; }; CLegacyConfigParser parser("DATA/BUILDING.TXT"); parser.endLine(); // header parser.endLine(); //Unique buildings for (size_t town=0; townmode = static_cast(boost::find(modes, source["mode"].String()) - modes); ret->identifier = stringID; ret->town = &town; ret->bid = BuildingID(source["id"].Float()); ret->name = source["name"].String(); ret->description = source["description"].String(); ret->resources = TResources(source["cost"]); ret->produce = TResources(source["produce"]); //for compatibility with older town mods if(!ret->produce.nonZero()) { if (ret->bid == BuildingID::VILLAGE_HALL) ret->produce[Res::GOLD] = 500; if (ret->bid == BuildingID::TOWN_HALL) ret->produce[Res::GOLD] = 1000; if (ret->bid == BuildingID::CITY_HALL) ret->produce[Res::GOLD] = 2000; if (ret->bid == BuildingID::CAPITOL) ret->produce[Res::GOLD] = 4000; if (ret->bid == BuildingID::GRAIL) ret->produce[Res::GOLD] = 5000; // if (ret->bid == BuildingID::RESOURCE_SILO) { if ((ret->town->primaryRes != Res::WOOD) && (ret->town->primaryRes != Res::ORE) && (ret->town->primaryRes != Res::GOLD)) ret->produce[ret->town->primaryRes] = 1; else { if (ret->town->primaryRes == Res::GOLD) ret->produce[ret->town->primaryRes] = 500; else { ret->produce[Res::WOOD] = 1; ret->produce[Res::ORE] = 1; } } } } loadBuildingRequirements(town, *ret, source["requires"]); if (!source["upgrades"].isNull()) { //MODS COMPATIBILITY if (source["upgrades"].getType() == JsonNode::DATA_FLOAT) ret->upgrade = BuildingID(source["upgrades"].Float()); else { // building id and upgrades can't be the same if(stringID == source["upgrades"].String()) { throw std::runtime_error(boost::str(boost::format("Building with ID '%s' of town '%s' can't be an upgrade of the same building.") % stringID % town.faction->name)); } VLC->modh->identifiers.requestIdentifier("building." + town.faction->identifier, source["upgrades"], [=](si32 identifier) { ret->upgrade = BuildingID(identifier); }); } } else ret->upgrade = BuildingID::NONE; town.buildings[ret->bid] = ret; VLC->modh->identifiers.registerObject(source.meta, "building." + town.faction->identifier, ret->identifier, ret->bid); } void CTownHandler::loadBuildings(CTown &town, const JsonNode & source) { for(auto &node : source.Struct()) { if (!node.second.isNull()) { loadBuilding(town, node.first, node.second); } } } void CTownHandler::loadStructure(CTown &town, const std::string & stringID, const JsonNode & source) { auto ret = new CStructure; //Note: MODS COMPATIBILITY CODE ret->building = nullptr; ret->buildable = nullptr; VLC->modh->identifiers.tryRequestIdentifier( source.meta, "building." + town.faction->identifier, stringID, [=, &town](si32 identifier) mutable { ret->building = town.buildings[BuildingID(identifier)]; }); if (source["builds"].isNull()) { VLC->modh->identifiers.tryRequestIdentifier( source.meta, "building." + town.faction->identifier, stringID, [=, &town](si32 identifier) mutable { ret->building = town.buildings[BuildingID(identifier)]; }); } else { if (source["builds"].getType() == JsonNode::DATA_FLOAT) { ret->buildable = town.buildings[BuildingID(source["builds"].Float())]; } else { VLC->modh->identifiers.requestIdentifier("building." + town.faction->identifier, source["builds"], [=, &town](si32 identifier) mutable { ret->buildable = town.buildings[BuildingID(identifier)]; }); } } ret->identifier = stringID; ret->pos.x = source["x"].Float(); ret->pos.y = source["y"].Float(); ret->pos.z = source["z"].Float(); ret->hiddenUpgrade = source["hidden"].Bool(); ret->defName = source["animation"].String(); ret->borderName = source["border"].String(); ret->areaName = source["area"].String(); town.clientInfo.structures.push_back(ret); } void CTownHandler::loadStructures(CTown &town, const JsonNode & source) { for(auto &node : source.Struct()) { if (!node.second.isNull()) loadStructure(town, node.first, node.second); } } void CTownHandler::loadTownHall(CTown &town, const JsonNode & source) { auto & dstSlots = town.clientInfo.hallSlots; auto & srcSlots = source.Vector(); dstSlots.resize(srcSlots.size()); for(size_t i=0; imodh->identifiers.requestIdentifier("building." + town.faction->identifier, src, [&](si32 identifier) { dst = BuildingID(identifier); }); } } } } } 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(); VLC->modh->identifiers.requestIdentifier("creature", source["shooter"], [&town](si32 creature) { town.clientInfo.siegeShooter = CreatureID(creature); }); 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"]); } static void readIcon(JsonNode source, std::string & small, std::string & large) { if (source.getType() == JsonNode::DATA_STRUCT) // don't crash on old format { small = source["small"].String(); large = source["large"].String(); } } 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(); //left for back compatibility - will be removed later if (source["guildBackground"].String() != "") info.guildBackground = source["guildBackground"].String(); else info.guildBackground = "TPMAGE.bmp"; if (source["tavernVideo"].String() != "") info.tavernVideo = source["tavernVideo"].String(); else info.tavernVideo = "TAVERN.BIK"; //end of legacy assignment info.advMapVillage = source["adventureMap"]["village"].String(); info.advMapCastle = source["adventureMap"]["castle"].String(); info.advMapCapitol = source["adventureMap"]["capitol"].String(); loadTownHall(town, source["hallSlots"]); loadStructures(town, source["structures"]); loadSiegeScreen(town, source["siege"]); } void CTownHandler::loadTown(CTown &town, const JsonNode & source) { auto resIter = boost::find(GameConstants::RESOURCE_NAMES, source["primaryResource"].String()); if (resIter == std::end(GameConstants::RESOURCE_NAMES)) town.primaryRes = Res::WOOD_AND_ORE; //Wood + Ore else town.primaryRes = resIter - std::begin(GameConstants::RESOURCE_NAMES); VLC->modh->identifiers.requestIdentifier("creature", source["warMachine"], [&town](si32 creature) { town.warMachine = CArtHandler::creatureToMachineID(CreatureID(creature)); }); town.moatDamage = source["moatDamage"].Float(); town.mageLevel = source["mageGuild"].Float(); town.names = source["names"].convertTo >(); // Horde building creature level for(const JsonNode &node : source["horde"].Vector()) town.hordeLvl[town.hordeLvl.size()] = node.Float(); // town needs to have exactly 2 horde entries. Validation will take care of 2+ entries // but anything below 2 must be handled here for (size_t i=source["horde"].Vector().size(); i<2; i++) town.hordeLvl[i] = -1; const JsonVector & creatures = source["creatures"].Vector(); town.creatures.resize(creatures.size()); for (size_t i=0; i< creatures.size(); i++) { const JsonVector & level = creatures[i].Vector(); town.creatures[i].resize(level.size()); for (size_t j=0; jmodh->identifiers.requestIdentifier("creature", level[j], [=, &town](si32 creature) { town.creatures[i][j] = CreatureID(creature); }); } } town.defaultTavernChance = source["defaultTavern"].Float(); /// set chance of specific hero class to appear in this town for(auto &node : source["tavern"].Struct()) { int chance = node.second.Float(); VLC->modh->identifiers.requestIdentifier(node.second.meta, "heroClass",node.first, [=, &town](si32 classID) { VLC->heroh->classes.heroClasses[classID]->selectionProbability[town.faction->index] = chance; }); } for(auto &node : source["guildSpells"].Struct()) { int chance = node.second.Float(); VLC->modh->identifiers.requestIdentifier(node.second.meta, "spell", node.first, [=, &town](si32 spellID) { SpellID(spellID).toSpell()->probabilities[town.faction->index] = chance; }); } for (const JsonNode &d : source["adventureMap"]["dwellings"].Vector()) { town.dwellings.push_back (d["graphics"].String()); town.dwellingNames.push_back (d["name"].String()); } loadBuildings(town, source["buildings"]); loadClientData(town,source); } void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source) { faction.puzzleMap.reserve(GameConstants::PUZZLE_MAP_PIECES); std::string prefix = source["prefix"].String(); for(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); } CFaction * CTownHandler::loadFromJson(const JsonNode &source, std::string identifier) { auto faction = new CFaction(); faction->name = source["name"].String(); faction->identifier = identifier; //FIXME: MODS COMPATIBILITY if (!source["commander"].isNull()) { VLC->modh->identifiers.requestIdentifier ("creature", source["commander"], [=](si32 commanderID) { for (auto ptr : VLC->heroh->classes.heroClasses) { if (ptr->commander == nullptr && ptr->faction == faction->index) ptr->commander = VLC->creh->creatures[commanderID]; } }); } faction->creatureBg120 = source["creatureBackground"]["120px"].String(); faction->creatureBg130 = source["creatureBackground"]["130px"].String(); faction->nativeTerrain = ETerrainType(vstd::find_pos(GameConstants::TERRAIN_NAMES, source["nativeTerrain"].String())); int alignment = vstd::find_pos(EAlignment::names, source["alignment"].String()); if (alignment == -1) faction->alignment = EAlignment::NEUTRAL; else faction->alignment = static_cast(alignment); if (!source["town"].isNull()) { faction->town = new CTown; faction->town->faction = faction; loadTown(*faction->town, source["town"]); } else faction->town = nullptr; if (!source["puzzleMap"].isNull()) loadPuzzle(*faction, source["puzzleMap"]); return faction; } void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data) { auto object = loadFromJson(data, name); object->index = factions.size(); 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; } factions.push_back(object); VLC->modh->identifiers.registerObject(scope, "faction", name, object->index); } void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) { auto object = loadFromJson(data, name); object->index = index; 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; } assert(factions[index] == nullptr); // ensure that this id was not loaded before factions[index] = object; VLC->modh->identifiers.registerObject(scope, "faction", name, object->index); } void CTownHandler::afterLoadFinalization() { initializeRequirements(); for (CFaction * fact : factions) { if (fact->town) { VLC->objtypeh->createObject(fact->identifier, JsonNode(), Obj::TOWN, fact->index); if (!fact->town->clientInfo.advMapCastle.empty()) { JsonNode templ; templ["animation"].String() = fact->town->clientInfo.advMapCastle; VLC->objtypeh->getHandlerFor(Obj::TOWN, fact->index)->addTemplate(templ); } assert(fact->town->dwellings.size() == fact->town->dwellingNames.size()); for (size_t i=0; itown->dwellings.size(); i++) { //both unupgraded and upgraded get same dwelling for (auto cre : fact->town->creatures[i]) { if (VLC->objh->cregens.count(cre) == 0) { JsonNode templ; templ["animation"].String() = fact->town->dwellings[i]; VLC->objtypeh->createObject("", JsonNode(), Obj::CREATURE_GENERATOR1, 80 + cre); VLC->objtypeh->getHandlerFor(Obj::CREATURE_GENERATOR1, 80 + cre)->addTemplate(templ); VLC->objh->cregens[80 + cre] = cre; //map of dwelling -> creature id } } } } } } void CTownHandler::initializeRequirements() { // must be done separately after all ID's are known for (auto & requirement : requirementsToLoad) { requirement.building->requirements = CBuilding::TRequired(requirement.json, [&](const JsonNode & node) { return BuildingID(VLC->modh->identifiers.getIdentifier("building." + requirement.faction->identifier, node.Vector()[0]).get()); }); } requirementsToLoad.clear(); } std::vector CTownHandler::getDefaultAllowed() const { std::vector allowedFactions; for(auto town : factions) { allowedFactions.push_back(town->town != nullptr); } return allowedFactions; } std::set CTownHandler::getAllowedFactions() const { std::set allowedFactions; auto allowed = getDefaultAllowed(); for (size_t i=0; i