2017-07-13 10:26:03 +02:00
|
|
|
/*
|
2021-05-16 19:53:11 +02:00
|
|
|
* CommonConstructors.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
|
|
|
|
*
|
|
|
|
*/
|
2014-06-14 18:42:13 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CommonConstructors.h"
|
|
|
|
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../texts/CGeneralTextHandler.h"
|
2014-06-25 17:11:07 +03:00
|
|
|
#include "../IGameCallback.h"
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "../json/JsonRandom.h"
|
2023-08-19 23:22:31 +02:00
|
|
|
#include "../constants/StringConstants.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../TerrainHandler.h"
|
2023-06-08 16:29:29 +02:00
|
|
|
#include "../VCMI_Lib.h"
|
2023-06-07 23:42:47 +02:00
|
|
|
|
2024-07-21 12:49:40 +02:00
|
|
|
#include "../entities/faction/CTownHandler.h"
|
2024-10-11 18:30:16 +02:00
|
|
|
#include "../entities/hero/CHeroClass.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
2023-06-07 23:42:47 +02:00
|
|
|
#include "../mapObjects/CGMarket.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjects/CGTownInstance.h"
|
2023-06-07 23:42:47 +02:00
|
|
|
#include "../mapObjects/MiscObjects.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjects/ObjectTemplate.h"
|
2023-06-07 23:42:47 +02:00
|
|
|
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "../modding/IdentifierStorage.h"
|
|
|
|
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapping/CMapDefines.h"
|
2014-06-14 18:42:13 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
bool CObstacleConstructor::isStaticObject()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-15 13:50:09 +02:00
|
|
|
bool CreatureInstanceConstructor::hasNameTextID() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CreatureInstanceConstructor::getNameTextID() const
|
|
|
|
{
|
|
|
|
return VLC->creatures()->getByIndex(getSubIndex())->getNamePluralTextID();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceInstanceConstructor::hasNameTextID() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ResourceInstanceConstructor::getNameTextID() const
|
|
|
|
{
|
|
|
|
return TextIdentifier("core", "restypes", getSubIndex()).get();
|
|
|
|
}
|
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
void CTownInstanceConstructor::initTypeData(const JsonNode & input)
|
|
|
|
{
|
2023-07-30 19:12:25 +02:00
|
|
|
VLC->identifiers()->requestIdentifier("faction", input["faction"], [&](si32 index)
|
2014-06-15 19:43:01 +03:00
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
faction = (*VLC->townh)[index];
|
2014-06-15 19:43:01 +03:00
|
|
|
});
|
2014-06-14 18:42:13 +03:00
|
|
|
|
|
|
|
filtersJson = input["filters"];
|
2022-11-30 17:36:55 +02:00
|
|
|
|
|
|
|
// change scope of "filters" to scope of object that is being loaded
|
|
|
|
// since this filters require to resolve building ID's
|
2024-02-13 14:34:16 +02:00
|
|
|
filtersJson.setModScope(input["faction"].getModScope());
|
2014-06-14 18:42:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownInstanceConstructor::afterLoadFinalization()
|
|
|
|
{
|
2014-06-15 19:43:01 +03:00
|
|
|
assert(faction);
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & entry : filtersJson.Struct())
|
2014-06-14 18:42:13 +03:00
|
|
|
{
|
2014-06-15 19:43:01 +03:00
|
|
|
filters[entry.first] = LogicalExpression<BuildingID>(entry.second, [this](const JsonNode & node)
|
2014-06-14 18:42:13 +03:00
|
|
|
{
|
2023-12-13 16:55:38 +02:00
|
|
|
return BuildingID(VLC->identifiers()->getIdentifier("building." + faction->getJsonKey(), node.Vector()[0]).value_or(-1));
|
2014-06-14 18:42:13 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
bool CTownInstanceConstructor::objectFilter(const CGObjectInstance * object, std::shared_ptr<const ObjectTemplate> templ) const
|
2014-06-14 18:42:13 +03:00
|
|
|
{
|
2023-02-12 22:39:17 +02:00
|
|
|
const auto * town = dynamic_cast<const CGTownInstance *>(object);
|
2014-06-14 18:42:13 +03:00
|
|
|
|
|
|
|
auto buildTest = [&](const BuildingID & id)
|
|
|
|
{
|
|
|
|
return town->hasBuilt(id);
|
|
|
|
};
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
return filters.count(templ->stringID) != 0 && filters.at(templ->stringID).test(buildTest);
|
2014-06-14 18:42:13 +03:00
|
|
|
}
|
|
|
|
|
2023-06-07 23:42:47 +02:00
|
|
|
void CTownInstanceConstructor::initializeObject(CGTownInstance * obj) const
|
2014-06-14 18:42:13 +03:00
|
|
|
{
|
|
|
|
obj->tempOwner = PlayerColor::NEUTRAL;
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void CTownInstanceConstructor::randomizeObject(CGTownInstance * object, vstd::RNG & rng) const
|
2014-06-14 18:42:13 +03:00
|
|
|
{
|
2024-07-13 20:37:13 +02:00
|
|
|
auto templ = getOverride(object->cb->getTile(object->pos)->getTerrainID(), object);
|
2021-05-16 19:53:11 +02:00
|
|
|
if(templ)
|
2022-09-11 15:12:35 +02:00
|
|
|
object->appearance = templ;
|
2014-06-14 18:42:13 +03:00
|
|
|
}
|
|
|
|
|
2023-07-15 13:50:09 +02:00
|
|
|
bool CTownInstanceConstructor::hasNameTextID() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CTownInstanceConstructor::getNameTextID() const
|
|
|
|
{
|
|
|
|
return faction->getNameTextID();
|
|
|
|
}
|
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
void CHeroInstanceConstructor::initTypeData(const JsonNode & input)
|
|
|
|
{
|
2023-07-30 19:12:25 +02:00
|
|
|
VLC->identifiers()->requestIdentifier(
|
2021-05-16 19:53:11 +02:00
|
|
|
"heroClass",
|
|
|
|
input["heroClass"],
|
2023-12-31 23:43:35 +02:00
|
|
|
[&](si32 index) { heroClass = HeroClassID(index).toHeroClass(); });
|
2014-06-16 19:27:26 +03:00
|
|
|
|
2024-11-03 20:50:47 +02:00
|
|
|
for (const auto & [name, config] : input["filters"].Struct())
|
2014-06-16 19:27:26 +03:00
|
|
|
{
|
2024-11-03 20:50:47 +02:00
|
|
|
HeroFilter filter;
|
|
|
|
filter.allowFemale = config["female"].Bool();
|
|
|
|
filter.allowMale = config["male"].Bool();
|
|
|
|
filters[name] = filter;
|
|
|
|
|
|
|
|
if (!config["hero"].isNull())
|
2014-06-16 19:27:26 +03:00
|
|
|
{
|
2024-11-03 20:50:47 +02:00
|
|
|
VLC->identifiers()->requestIdentifier( "hero", config["hero"], [this, templateName = name](si32 index) {
|
|
|
|
filters.at(templateName).fixedHero = HeroTypeID(index);
|
|
|
|
});
|
|
|
|
}
|
2014-06-16 19:27:26 +03:00
|
|
|
}
|
2014-06-14 18:42:13 +03:00
|
|
|
}
|
|
|
|
|
2024-11-03 20:50:47 +02:00
|
|
|
std::shared_ptr<const ObjectTemplate> CHeroInstanceConstructor::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
|
2014-06-14 18:42:13 +03:00
|
|
|
{
|
2023-02-12 22:39:17 +02:00
|
|
|
const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
|
2014-06-16 19:27:26 +03:00
|
|
|
|
2024-11-03 20:50:47 +02:00
|
|
|
std::vector<std::shared_ptr<const ObjectTemplate>> allTemplates = getTemplates();
|
|
|
|
std::shared_ptr<const ObjectTemplate> candidateFullMatch;
|
|
|
|
std::shared_ptr<const ObjectTemplate> candidateGenderMatch;
|
|
|
|
std::shared_ptr<const ObjectTemplate> candidateBase;
|
|
|
|
|
|
|
|
assert(hero->gender != EHeroGender::DEFAULT);
|
2014-06-16 19:27:26 +03:00
|
|
|
|
2024-11-03 20:50:47 +02:00
|
|
|
for (const auto & templ : allTemplates)
|
2014-06-16 19:27:26 +03:00
|
|
|
{
|
2024-11-03 20:50:47 +02:00
|
|
|
if (filters.count(templ->stringID))
|
|
|
|
{
|
|
|
|
const auto & filter = filters.at(templ->stringID);
|
|
|
|
if (filter.fixedHero.hasValue())
|
|
|
|
{
|
|
|
|
if (filter.fixedHero == hero->getHeroTypeID())
|
|
|
|
candidateFullMatch = templ;
|
|
|
|
}
|
|
|
|
else if (filter.allowMale)
|
|
|
|
{
|
|
|
|
if (hero->gender == EHeroGender::MALE)
|
|
|
|
candidateGenderMatch = templ;
|
|
|
|
}
|
|
|
|
else if (filter.allowFemale)
|
|
|
|
{
|
|
|
|
if (hero->gender == EHeroGender::FEMALE)
|
|
|
|
candidateGenderMatch = templ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
candidateBase = templ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
candidateBase = templ;
|
|
|
|
}
|
2014-06-16 19:27:26 +03:00
|
|
|
}
|
2024-11-03 20:50:47 +02:00
|
|
|
|
|
|
|
if (candidateFullMatch)
|
|
|
|
return candidateFullMatch;
|
|
|
|
|
|
|
|
if (candidateGenderMatch)
|
|
|
|
return candidateGenderMatch;
|
|
|
|
|
|
|
|
return candidateBase;
|
2014-06-14 18:42:13 +03:00
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void CHeroInstanceConstructor::randomizeObject(CGHeroInstance * object, vstd::RNG & rng) const
|
2014-06-14 18:42:13 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-15 13:50:09 +02:00
|
|
|
bool CHeroInstanceConstructor::hasNameTextID() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CHeroInstanceConstructor::getNameTextID() const
|
|
|
|
{
|
|
|
|
return heroClass->getNameTextID();
|
|
|
|
}
|
|
|
|
|
2023-04-18 15:27:39 +02:00
|
|
|
void BoatInstanceConstructor::initTypeData(const JsonNode & input)
|
|
|
|
{
|
2023-04-18 22:14:15 +02:00
|
|
|
layer = EPathfindingLayer::SAIL;
|
|
|
|
int pos = vstd::find_pos(NPathfindingLayer::names, input["layer"].String());
|
|
|
|
if(pos != -1)
|
|
|
|
layer = EPathfindingLayer(pos);
|
2023-09-26 12:22:36 +02:00
|
|
|
else
|
|
|
|
logMod->error("Unknown layer %s found in boat!", input["layer"].String());
|
|
|
|
|
2023-04-19 00:11:24 +02:00
|
|
|
onboardAssaultAllowed = input["onboardAssaultAllowed"].Bool();
|
|
|
|
onboardVisitAllowed = input["onboardVisitAllowed"].Bool();
|
2023-08-23 14:07:50 +02:00
|
|
|
actualAnimation = AnimationPath::fromJson(input["actualAnimation"]);
|
|
|
|
overlayAnimation = AnimationPath::fromJson(input["overlayAnimation"]);
|
2023-04-18 22:14:15 +02:00
|
|
|
for(int i = 0; i < flagAnimations.size() && i < input["flagAnimations"].Vector().size(); ++i)
|
2023-08-23 14:07:50 +02:00
|
|
|
flagAnimations[i] = AnimationPath::fromJson(input["flagAnimations"].Vector()[i]);
|
2023-04-18 23:11:51 +02:00
|
|
|
bonuses = JsonRandom::loadBonuses(input["bonuses"]);
|
2023-04-18 15:27:39 +02:00
|
|
|
}
|
|
|
|
|
2023-06-07 23:42:47 +02:00
|
|
|
void BoatInstanceConstructor::initializeObject(CGBoat * boat) const
|
2023-04-18 15:27:39 +02:00
|
|
|
{
|
|
|
|
boat->layer = layer;
|
2023-04-18 22:14:15 +02:00
|
|
|
boat->actualAnimation = actualAnimation;
|
|
|
|
boat->overlayAnimation = overlayAnimation;
|
|
|
|
boat->flagAnimations = flagAnimations;
|
2023-06-07 20:10:04 +02:00
|
|
|
boat->onboardAssaultAllowed = onboardAssaultAllowed;
|
|
|
|
boat->onboardVisitAllowed = onboardVisitAllowed;
|
2023-04-18 23:11:51 +02:00
|
|
|
for(auto & b : bonuses)
|
|
|
|
boat->addNewBonus(std::make_shared<Bonus>(b));
|
2023-04-18 15:27:39 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
AnimationPath BoatInstanceConstructor::getBoatAnimationName() const
|
2023-06-07 18:51:44 +02:00
|
|
|
{
|
|
|
|
return actualAnimation;
|
|
|
|
}
|
|
|
|
|
2023-04-28 03:16:10 +02:00
|
|
|
void MarketInstanceConstructor::initTypeData(const JsonNode & input)
|
|
|
|
{
|
2024-10-30 15:20:21 +02:00
|
|
|
if (!input["description"].isNull())
|
|
|
|
{
|
|
|
|
description = input["description"].String();
|
|
|
|
VLC->generaltexth->registerString(input.getModScope(), TextIdentifier(getBaseTextID(), "description"), description);
|
|
|
|
}
|
|
|
|
|
2023-04-28 03:16:10 +02:00
|
|
|
for(auto & element : input["modes"].Vector())
|
|
|
|
{
|
|
|
|
if(MappedKeys::MARKET_NAMES_TO_TYPES.count(element.String()))
|
|
|
|
marketModes.insert(MappedKeys::MARKET_NAMES_TO_TYPES.at(element.String()));
|
|
|
|
}
|
|
|
|
|
2023-05-01 02:07:31 +02:00
|
|
|
marketEfficiency = input["efficiency"].isNull() ? 5 : input["efficiency"].Integer();
|
2023-04-28 03:16:10 +02:00
|
|
|
predefinedOffer = input["offer"];
|
|
|
|
|
|
|
|
title = input["title"].String();
|
|
|
|
speech = input["speech"].String();
|
|
|
|
}
|
|
|
|
|
2024-10-30 15:20:21 +02:00
|
|
|
bool MarketInstanceConstructor::hasDescription() const
|
|
|
|
{
|
|
|
|
return !description.empty();
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
CGMarket * MarketInstanceConstructor::createObject(IGameCallback * cb) const
|
2023-04-28 03:16:10 +02:00
|
|
|
{
|
|
|
|
if(marketModes.size() == 1)
|
|
|
|
{
|
|
|
|
switch(*marketModes.begin())
|
|
|
|
{
|
|
|
|
case EMarketMode::ARTIFACT_RESOURCE:
|
|
|
|
case EMarketMode::RESOURCE_ARTIFACT:
|
2024-01-01 16:37:48 +02:00
|
|
|
return new CGBlackMarket(cb);
|
2023-06-07 23:42:47 +02:00
|
|
|
|
2023-04-28 03:16:10 +02:00
|
|
|
case EMarketMode::RESOURCE_SKILL:
|
2024-01-01 16:37:48 +02:00
|
|
|
return new CGUniversity(cb);
|
2023-04-28 03:16:10 +02:00
|
|
|
}
|
|
|
|
}
|
2024-01-01 16:37:48 +02:00
|
|
|
return new CGMarket(cb);
|
2023-06-07 23:42:47 +02:00
|
|
|
}
|
2023-04-28 03:16:10 +02:00
|
|
|
|
2023-06-07 23:42:47 +02:00
|
|
|
void MarketInstanceConstructor::initializeObject(CGMarket * market) const
|
|
|
|
{
|
2023-05-01 02:07:31 +02:00
|
|
|
market->marketEfficiency = marketEfficiency;
|
2023-04-28 03:16:10 +02:00
|
|
|
|
2024-08-17 21:06:48 +02:00
|
|
|
if(auto university = dynamic_cast<CGUniversity*>(market))
|
2024-08-12 16:38:30 +02:00
|
|
|
{
|
2024-08-17 21:06:48 +02:00
|
|
|
university->title = market->getObjectName();
|
2024-08-12 16:38:30 +02:00
|
|
|
if(!title.empty())
|
2024-08-17 21:06:48 +02:00
|
|
|
university->title = VLC->generaltexth->translate(title);
|
2024-08-12 16:38:30 +02:00
|
|
|
|
|
|
|
if(!speech.empty())
|
2024-08-17 21:06:48 +02:00
|
|
|
university->speech = VLC->generaltexth->translate(speech);
|
2024-08-12 16:38:30 +02:00
|
|
|
}
|
2023-04-28 03:16:10 +02:00
|
|
|
}
|
|
|
|
|
2024-08-27 15:44:30 +02:00
|
|
|
const std::set<EMarketMode> & MarketInstanceConstructor::availableModes() const
|
|
|
|
{
|
|
|
|
return marketModes;
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void MarketInstanceConstructor::randomizeObject(CGMarket * object, vstd::RNG & rng) const
|
2023-04-28 03:16:10 +02:00
|
|
|
{
|
2024-01-01 16:37:48 +02:00
|
|
|
JsonRandom randomizer(object->cb);
|
2023-09-30 17:47:47 +02:00
|
|
|
JsonRandom::Variables emptyVariables;
|
|
|
|
|
2023-04-28 03:16:10 +02:00
|
|
|
if(auto * university = dynamic_cast<CGUniversity *>(object))
|
|
|
|
{
|
2024-01-01 16:37:48 +02:00
|
|
|
for(auto skill : randomizer.loadSecondaries(predefinedOffer, rng, emptyVariables))
|
2023-11-08 17:49:08 +02:00
|
|
|
university->skills.push_back(skill.first);
|
2023-04-28 03:16:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|