1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
|
/*
* 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
*
*/
#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 "spells/CSpellHandler.h"
#include "filesystem/Filesystem.h"
#include "mapObjects/CObjectClassesHandler.h"
#include "mapObjects/CObjectHandler.h"
const int NAMES_PER_TOWN=16; // number of town names per faction in H3 files. Json can define any number
CBuilding::CBuilding():
town(nullptr),mode(BUILD_NORMAL)
{
}
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;
}
void CBuilding::deserializeFix()
{
//default value for mode was broken, have to fix it here for old saves (v777 and older)
switch(mode)
{
case BUILD_NORMAL:
case BUILD_AUTO:
case BUILD_SPECIAL:
case BUILD_GRAIL:
break;
default:
mode = BUILD_NORMAL;
break;
}
}
CFaction::CFaction()
{
town = nullptr;
index = 0;
alignment = EAlignment::NEUTRAL;
}
CFaction::~CFaction()
{
delete town;
}
CTown::CTown()
: faction(nullptr), mageLevel(0), primaryRes(0), moatDamage(0), defaultTavernChance(0)
{
}
CTown::~CTown()
{
for(auto & build : buildings)
build.second.dellNull();
for(auto & str : clientInfo.structures)
str.dellNull();
}
std::vector<BattleHex> CTown::defaultMoatHexes()
{
static const std::vector<BattleHex> moatHexes = {11, 28, 44, 61, 77, 111, 129, 146, 164, 181};
return moatHexes;
}
std::string CTown::getFactionName() const
{
if(faction == nullptr)
return "Random";
else
return faction->name;
}
std::string CTown::getBuildingScope() const
{
if(faction == nullptr)
//no faction == random faction
return "building";
else
return "building." + faction->identifier;
}
std::set<si32> CTown::getAllBuildings() const
{
std::set<si32> res;
for(const auto & b : buildings)
{
res.insert(b.first.num);
}
return res;
}
CTownHandler::CTownHandler()
{
VLC->townh = this;
randomTown = new CTown();
}
CTownHandler::~CTownHandler()
{
delete randomTown;
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<JsonNode> CTownHandler::loadLegacyData(size_t dataSize)
{
std::vector<JsonNode> 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; town<dataSize; town++)
{
parser.endLine(); //header
parser.endLine();
int buildID = 17;
do
{
getBuild(town, buildID) = readBuilding(parser);
buildID++;
}
while (!parser.isNextEntryEmpty());
}
// Common buildings
parser.endLine(); // header
parser.endLine();
parser.endLine();
int buildID = 0;
do
{
JsonNode building = readBuilding(parser);
for (size_t town=0; town<dataSize; town++)
getBuild(town, buildID) = building;
buildID++;
}
while (!parser.isNextEntryEmpty());
parser.endLine(); //header
parser.endLine();
//Dwellings
for (size_t town=0; town<dataSize; town++)
{
parser.endLine(); //header
parser.endLine();
for (size_t i=0; i<14; i++)
{
getBuild(town, 30+i) = readBuilding(parser);
}
}
{
CLegacyConfigParser parser("DATA/BLDGNEUT.TXT");
for(int building=0; building<15; building++)
{
std::string name = parser.readString();
std::string descr = parser.readString();
parser.endLine();
for(int j=0; j<dataSize; j++)
{
getBuild(j, building)["name"].String() = name;
getBuild(j, building)["description"].String() = descr;
}
}
parser.endLine(); // silo
parser.endLine(); // blacksmith //unused entries
parser.endLine(); // moat
//shipyard with the ship
std::string name = parser.readString();
std::string descr = parser.readString();
parser.endLine();
for(int town=0; town<dataSize; town++)
{
getBuild(town, 20)["name"].String() = name;
getBuild(town, 20)["description"].String() = descr;
}
//blacksmith
for(int town=0; town<dataSize; town++)
{
getBuild(town, 16)["name"].String() = parser.readString();
getBuild(town, 16)["description"].String() = parser.readString();
parser.endLine();
}
}
{
CLegacyConfigParser parser("DATA/BLDGSPEC.TXT");
for(int town=0; town<dataSize; town++)
{
for(int build=0; build<9; build++)
{
getBuild(town, 17 + build)["name"].String() = parser.readString();
getBuild(town, 17 + build)["description"].String() = parser.readString();
parser.endLine();
}
getBuild(town, 26)["name"].String() = parser.readString(); // Grail
getBuild(town, 26)["description"].String() = parser.readString();
parser.endLine();
getBuild(town, 15)["name"].String() = parser.readString(); // Resource silo
getBuild(town, 15)["description"].String() = parser.readString();
parser.endLine();
}
}
{
CLegacyConfigParser parser("DATA/DWELLING.TXT");
for(int town=0; town<dataSize; town++)
{
for(int build=0; build<14; build++)
{
getBuild(town, 30 + build)["name"].String() = parser.readString();
getBuild(town, 30 + build)["description"].String() = parser.readString();
parser.endLine();
}
}
}
{
CLegacyConfigParser typeParser("DATA/TOWNTYPE.TXT");
CLegacyConfigParser nameParser("DATA/TOWNNAME.TXT");
size_t townID=0;
do
{
dest[townID]["name"].String() = typeParser.readString();
for (int i=0; i<NAMES_PER_TOWN; i++)
{
JsonNode name;
name.String() = nameParser.readString();
dest[townID]["town"]["names"].Vector().push_back(name);
nameParser.endLine();
}
townID++;
}
while (typeParser.endLine());
}
return dest;
}
void CTownHandler::loadBuildingRequirements(CBuilding * building, const JsonNode & source)
{
if (source.isNull())
return;
BuildingRequirementsHelper hlp;
hlp.building = building;
hlp.town = building->town;
hlp.json = source;
requirementsToLoad.push_back(hlp);
}
void CTownHandler::loadBuilding(CTown * town, const std::string & stringID, const JsonNode & source)
{
auto ret = new CBuilding();
static const std::vector<std::string> MODES =
{
"normal", "auto", "special", "grail"
};
ret->mode = CBuilding::BUILD_NORMAL;
{
if(source["mode"].getType() == JsonNode::JsonType::DATA_STRING)
{
auto rawMode = vstd::find_pos(MODES, source["mode"].String());
if(rawMode > 0)
ret->mode = static_cast<CBuilding::EBuildMode>(rawMode);
}
}
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"]);
//MODS COMPATIBILITY FOR 0.96
if(!ret->produce.nonZero())
{
switch (ret->bid) {
break; case BuildingID::VILLAGE_HALL: ret->produce[Res::GOLD] = 500;
break; case BuildingID::TOWN_HALL : ret->produce[Res::GOLD] = 1000;
break; case BuildingID::CITY_HALL : ret->produce[Res::GOLD] = 2000;
break; case BuildingID::CAPITOL : ret->produce[Res::GOLD] = 4000;
break; case BuildingID::GRAIL : ret->produce[Res::GOLD] = 5000;
break; case BuildingID::RESOURCE_SILO :
{
switch (ret->town->primaryRes)
{
case Res::GOLD:
ret->produce[ret->town->primaryRes] = 500;
break;
case Res::WOOD_AND_ORE:
ret->produce[Res::WOOD] = 1;
ret->produce[Res::ORE] = 1;
break;
default:
ret->produce[ret->town->primaryRes] = 1;
break;
}
}
}
}
loadBuildingRequirements(ret, source["requires"]);
if (!source["upgrades"].isNull())
{
// 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 % ret->town->getFactionName()));
}
VLC->modh->identifiers.requestIdentifier(ret->town->getBuildingScope(), source["upgrades"], [=](si32 identifier)
{
ret->upgrade = BuildingID(identifier);
});
}
else
ret->upgrade = BuildingID::NONE;
ret->town->buildings[ret->bid] = ret;
VLC->modh->identifiers.registerObject(source.meta, ret->town->getBuildingScope(), 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();
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
{
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; i<dstSlots.size(); i++)
{
auto & dstRow = dstSlots[i];
auto & srcRow = srcSlots[i].Vector();
dstRow.resize(srcRow.size());
for(size_t j=0; j < dstRow.size(); j++)
{
auto & dstBox = dstRow[j];
auto & srcBox = srcRow[j].Vector();
dstBox.resize(srcBox.size());
for(size_t k=0; k<dstBox.size(); k++)
{
auto & dst = dstBox[k];
auto & src = srcBox[k];
VLC->modh->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::JsonType::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
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);
warMachinesToLoad[town] = source["warMachine"];
town->moatDamage = source["moatDamage"].Float();
// Compatability for <= 0.98f mods
if(source["moatHexes"].isNull())
town->moatHexes = CTown::defaultMoatHexes();
else
town->moatHexes = source["moatHexes"].convertTo<std::vector<BattleHex> >();
town->mageLevel = source["mageGuild"].Float();
town->names = source["names"].convertTo<std::vector<std::string> >();
// 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; j<level.size(); j++)
{
VLC->modh->identifiers.requestIdentifier("creature", level[j], [=](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, [=](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, [=](si32 spellID)
{
VLC->spellh->objects.at(spellID)->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, const std::string & identifier)
{
auto faction = new CFaction();
faction->name = source["name"].String();
faction->identifier = identifier;
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<EAlignment::EAlignment>(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, normalizeIdentifier(scope, "core", name));
object->index = factions.size();
factions.push_back(object);
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;
VLC->modh->identifiers.requestIdentifier(scope, "object", "town", [=](si32 index)
{
// register town once objects are loaded
JsonNode config = data["town"]["mapObject"];
config["faction"].String() = name;
config["faction"].meta = scope;
if (config.meta.empty())// MODS COMPATIBILITY FOR 0.96
config.meta = scope;
VLC->objtypeh->loadSubObject(object->identifier, config, index, object->index);
// MODS COMPATIBILITY FOR 0.96
auto & advMap = data["town"]["adventureMap"];
if (!advMap.isNull())
{
logMod->warn("Outdated town mod. Will try to generate valid templates out of fort");
JsonNode config;
config["animation"] = advMap["castle"];
VLC->objtypeh->getHandlerFor(index, object->index)->addTemplate(config);
}
});
}
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, normalizeIdentifier(scope, "core", name));
object->index = index;
if (factions.size() > index)
assert(factions[index] == nullptr); // ensure that this id was not loaded before
else
factions.resize(index + 1);
factions[index] = object;
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;
VLC->modh->identifiers.requestIdentifier(scope, "object", "town", [=](si32 index)
{
// register town once objects are loaded
JsonNode config = data["town"]["mapObject"];
config["faction"].String() = name;
config["faction"].meta = scope;
VLC->objtypeh->loadSubObject(object->identifier, config, index, object->index);
});
}
VLC->modh->identifiers.registerObject(scope, "faction", name, object->index);
}
void CTownHandler::loadRandomFaction()
{
static const ResourceID randomFactionPath("config/factions/random.json");
JsonNode randomFactionJson(randomFactionPath);
randomFactionJson.setMeta("core", true);
loadBuildings(randomTown, randomFactionJson["random"]["town"]["buildings"]);
}
void CTownHandler::loadCustom()
{
loadRandomFaction();
}
void CTownHandler::afterLoadFinalization()
{
initializeRequirements();
initializeWarMachines();
}
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) -> BuildingID
{
if (node.Vector().size() > 1)
{
logMod->warn("Unexpected length of town buildings requirements: %d", node.Vector().size());
logMod->warn("Entry contains: ");
logMod->warn(node.toJson());
}
return BuildingID(VLC->modh->identifiers.getIdentifier(requirement.town->getBuildingScope(), node.Vector()[0]).get());
});
}
requirementsToLoad.clear();
}
void CTownHandler::initializeWarMachines()
{
// must be done separately after all objects are loaded
for(auto & p : warMachinesToLoad)
{
CTown * t = p.first;
JsonNode creatureKey = p.second;
auto ret = VLC->modh->identifiers.getIdentifier("creature", creatureKey, false);
if(ret)
{
const CCreature * creature = CreatureID(*ret).toCreature();
t->warMachine = creature->warMachine;
}
}
warMachinesToLoad.clear();
}
std::vector<bool> CTownHandler::getDefaultAllowed() const
{
std::vector<bool> allowedFactions;
for(auto town : factions)
{
allowedFactions.push_back(town->town != nullptr);
}
return allowedFactions;
}
std::set<TFaction> CTownHandler::getAllowedFactions(bool withTown) const
{
std::set<TFaction> allowedFactions;
std::vector<bool> allowed;
if (withTown)
allowed = getDefaultAllowed();
else
allowed.resize( factions.size(), true);
for (size_t i=0; i<allowed.size(); i++)
if (allowed[i])
allowedFactions.insert(i);
return allowedFactions;
}
si32 CTownHandler::decodeFaction(const std::string & identifier)
{
auto rawId = VLC->modh->identifiers.getIdentifier("core", "faction", identifier);
if(rawId)
return rawId.get();
else
return -1;
}
std::string CTownHandler::encodeFaction(const si32 index)
{
return VLC->townh->factions[index]->identifier;
}
|