1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-31 22:05:10 +02:00

Setup town`s war machine in finalization step

* fixes https://bugs.vcmi.eu/view.php?id=2969
This commit is contained in:
AlexVinS 2018-07-16 15:16:55 +03:00
parent 51229e88b2
commit d6f889f08a
2 changed files with 52 additions and 34 deletions

View File

@ -601,70 +601,64 @@ void CTownHandler::loadClientData(CTown &town, const JsonNode & source)
loadSiegeScreen(town, source["siege"]); loadSiegeScreen(town, source["siege"]);
} }
void CTownHandler::loadTown(CTown &town, const JsonNode & source) void CTownHandler::loadTown(CTown * town, const JsonNode & source)
{ {
auto resIter = boost::find(GameConstants::RESOURCE_NAMES, source["primaryResource"].String()); auto resIter = boost::find(GameConstants::RESOURCE_NAMES, source["primaryResource"].String());
if (resIter == std::end(GameConstants::RESOURCE_NAMES)) if(resIter == std::end(GameConstants::RESOURCE_NAMES))
town.primaryRes = Res::WOOD_AND_ORE; //Wood + Ore town->primaryRes = Res::WOOD_AND_ORE; //Wood + Ore
else else
town.primaryRes = resIter - std::begin(GameConstants::RESOURCE_NAMES); town->primaryRes = resIter - std::begin(GameConstants::RESOURCE_NAMES);
VLC->modh->identifiers.requestIdentifier("creature", source["warMachine"], warMachinesToLoad[town] = source["warMachine"];
[&town](si32 creature)
{
town.warMachine = CreatureID(creature).toCreature()->warMachine;
});
town.moatDamage = source["moatDamage"].Float(); town->moatDamage = source["moatDamage"].Float();
// Compatability for <= 0.98f mods // Compatability for <= 0.98f mods
if(source["moatHexes"].isNull()) if(source["moatHexes"].isNull())
{ town->moatHexes = CTown::defaultMoatHexes();
town.moatHexes = CTown::defaultMoatHexes();
}
else else
town.moatHexes = source["moatHexes"].convertTo<std::vector<BattleHex> >(); town->moatHexes = source["moatHexes"].convertTo<std::vector<BattleHex> >();
town.mageLevel = source["mageGuild"].Float(); town->mageLevel = source["mageGuild"].Float();
town.names = source["names"].convertTo<std::vector<std::string> >(); town->names = source["names"].convertTo<std::vector<std::string> >();
// Horde building creature level // Horde building creature level
for(const JsonNode &node : source["horde"].Vector()) for(const JsonNode &node : source["horde"].Vector())
town.hordeLvl[town.hordeLvl.size()] = node.Float(); town->hordeLvl[town->hordeLvl.size()] = node.Float();
// town needs to have exactly 2 horde entries. Validation will take care of 2+ entries // town needs to have exactly 2 horde entries. Validation will take care of 2+ entries
// but anything below 2 must be handled here // but anything below 2 must be handled here
for (size_t i=source["horde"].Vector().size(); i<2; i++) for (size_t i=source["horde"].Vector().size(); i<2; i++)
town.hordeLvl[i] = -1; town->hordeLvl[i] = -1;
const JsonVector & creatures = source["creatures"].Vector(); const JsonVector & creatures = source["creatures"].Vector();
town.creatures.resize(creatures.size()); town->creatures.resize(creatures.size());
for (size_t i=0; i< creatures.size(); i++) for (size_t i=0; i< creatures.size(); i++)
{ {
const JsonVector & level = creatures[i].Vector(); const JsonVector & level = creatures[i].Vector();
town.creatures[i].resize(level.size()); town->creatures[i].resize(level.size());
for (size_t j=0; j<level.size(); j++) for (size_t j=0; j<level.size(); j++)
{ {
VLC->modh->identifiers.requestIdentifier("creature", level[j], [=, &town](si32 creature) VLC->modh->identifiers.requestIdentifier("creature", level[j], [=](si32 creature)
{ {
town.creatures[i][j] = CreatureID(creature); town->creatures[i][j] = CreatureID(creature);
}); });
} }
} }
town.defaultTavernChance = source["defaultTavern"].Float(); town->defaultTavernChance = source["defaultTavern"].Float();
/// set chance of specific hero class to appear in this town /// set chance of specific hero class to appear in this town
for(auto &node : source["tavern"].Struct()) for(auto &node : source["tavern"].Struct())
{ {
int chance = node.second.Float(); int chance = node.second.Float();
VLC->modh->identifiers.requestIdentifier(node.second.meta, "heroClass",node.first, [=, &town](si32 classID) VLC->modh->identifiers.requestIdentifier(node.second.meta, "heroClass",node.first, [=](si32 classID)
{ {
VLC->heroh->classes.heroClasses[classID]->selectionProbability[town.faction->index] = chance; VLC->heroh->classes.heroClasses[classID]->selectionProbability[town->faction->index] = chance;
}); });
} }
@ -672,20 +666,20 @@ void CTownHandler::loadTown(CTown &town, const JsonNode & source)
{ {
int chance = node.second.Float(); int chance = node.second.Float();
VLC->modh->identifiers.requestIdentifier(node.second.meta, "spell", node.first, [=, &town](si32 spellID) VLC->modh->identifiers.requestIdentifier(node.second.meta, "spell", node.first, [=](si32 spellID)
{ {
VLC->spellh->objects.at(spellID)->probabilities[town.faction->index] = chance; VLC->spellh->objects.at(spellID)->probabilities[town->faction->index] = chance;
}); });
} }
for (const JsonNode &d : source["adventureMap"]["dwellings"].Vector()) for(const JsonNode & d : source["adventureMap"]["dwellings"].Vector())
{ {
town.dwellings.push_back (d["graphics"].String()); town->dwellings.push_back(d["graphics"].String());
town.dwellingNames.push_back (d["name"].String()); town->dwellingNames.push_back(d["name"].String());
} }
loadBuildings(&town, source["buildings"]); loadBuildings(town, source["buildings"]);
loadClientData(town,source); loadClientData(*town, source);
} }
void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source) void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source)
@ -737,7 +731,7 @@ CFaction * CTownHandler::loadFromJson(const JsonNode &source, const std::string
{ {
faction->town = new CTown(); faction->town = new CTown();
faction->town->faction = faction; faction->town->faction = faction;
loadTown(*faction->town, source["town"]); loadTown(faction->town, source["town"]);
} }
else else
faction->town = nullptr; faction->town = nullptr;
@ -836,6 +830,7 @@ void CTownHandler::loadCustom()
void CTownHandler::afterLoadFinalization() void CTownHandler::afterLoadFinalization()
{ {
initializeRequirements(); initializeRequirements();
initializeWarMachines();
} }
void CTownHandler::initializeRequirements() void CTownHandler::initializeRequirements()
@ -857,6 +852,27 @@ void CTownHandler::initializeRequirements()
requirementsToLoad.clear(); 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> CTownHandler::getDefaultAllowed() const
{ {
std::vector<bool> allowedFactions; std::vector<bool> allowedFactions;

View File

@ -296,8 +296,10 @@ class DLL_LINKAGE CTownHandler : public IHandlerBase
CTown * town; CTown * town;
}; };
std::map<CTown *, JsonNode> warMachinesToLoad;
std::vector<BuildingRequirementsHelper> requirementsToLoad; std::vector<BuildingRequirementsHelper> requirementsToLoad;
void initializeRequirements(); void initializeRequirements();
void initializeWarMachines();
/// loads CBuilding's into town /// loads CBuilding's into town
void loadBuildingRequirements(CBuilding * building, const JsonNode & source); void loadBuildingRequirements(CBuilding * building, const JsonNode & source);
@ -314,7 +316,7 @@ class DLL_LINKAGE CTownHandler : public IHandlerBase
void loadClientData(CTown &town, const JsonNode & source); void loadClientData(CTown &town, const JsonNode & source);
void loadTown(CTown &town, const JsonNode & source); void loadTown(CTown * town, const JsonNode & source);
void loadPuzzle(CFaction & faction, const JsonNode & source); void loadPuzzle(CFaction & faction, const JsonNode & source);