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"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../CHeroHandler.h"
|
2023-06-08 16:29:29 +02:00
|
|
|
#include "../CTownHandler.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
|
|
|
|
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->town = faction->town;
|
|
|
|
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-01-01 16:37:48 +02:00
|
|
|
auto templ = getOverride(object->cb->getTile(object->pos)->terType->getId(), 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
|
|
|
|
|
|
|
filtersJson = input["filters"];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHeroInstanceConstructor::afterLoadFinalization()
|
|
|
|
{
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & entry : filtersJson.Struct())
|
2014-06-16 19:27:26 +03:00
|
|
|
{
|
2019-01-19 12:52:02 +02:00
|
|
|
filters[entry.first] = LogicalExpression<HeroTypeID>(entry.second, [](const JsonNode & node)
|
2014-06-16 19:27:26 +03:00
|
|
|
{
|
2023-12-09 18:08:22 +02:00
|
|
|
return HeroTypeID(VLC->identifiers()->getIdentifier("hero", node.Vector()[0]).value_or(-1));
|
2014-06-16 19:27:26 +03:00
|
|
|
});
|
|
|
|
}
|
2014-06-14 18:42:13 +03:00
|
|
|
}
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
bool CHeroInstanceConstructor::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 * hero = dynamic_cast<const CGHeroInstance *>(object);
|
2014-06-16 19:27:26 +03:00
|
|
|
|
|
|
|
auto heroTest = [&](const HeroTypeID & id)
|
|
|
|
{
|
2023-01-02 13:27:03 +02:00
|
|
|
return hero->type->getId() == id;
|
2014-06-16 19:27:26 +03:00
|
|
|
};
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
if(filters.count(templ->stringID))
|
2014-06-16 19:27:26 +03:00
|
|
|
{
|
2022-09-11 15:12:35 +02:00
|
|
|
return filters.at(templ->stringID).test(heroTest);
|
2014-06-16 19:27:26 +03:00
|
|
|
}
|
2014-06-14 18:42:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-07 23:42:47 +02:00
|
|
|
void CHeroInstanceConstructor::initializeObject(CGHeroInstance * obj) const
|
2014-06-14 18:42:13 +03:00
|
|
|
{
|
|
|
|
obj->type = nullptr; //FIXME: set to valid value. somehow.
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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-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-12 23:57:19 +02:00
|
|
|
else if(marketModes.size() == 2)
|
|
|
|
{
|
|
|
|
if(vstd::contains(marketModes, EMarketMode::ARTIFACT_EXP))
|
|
|
|
return new CGArtifactsAltar(cb);
|
|
|
|
}
|
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-04-28 03:16:10 +02:00
|
|
|
market->marketModes = marketModes;
|
2023-05-01 02:07:31 +02:00
|
|
|
market->marketEfficiency = marketEfficiency;
|
2023-04-28 03:16:10 +02:00
|
|
|
|
2023-04-29 17:08:42 +02:00
|
|
|
market->title = market->getObjectName();
|
|
|
|
if(!title.empty())
|
|
|
|
market->title = VLC->generaltexth->translate(title);
|
|
|
|
|
2023-05-25 18:35:21 +02:00
|
|
|
if (!speech.empty())
|
|
|
|
market->speech = VLC->generaltexth->translate(speech);
|
2023-04-28 03:16:10 +02:00
|
|
|
}
|
|
|
|
|
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
|