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"
|
|
|
|
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../CGeneralTextHandler.h"
|
|
|
|
#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"
|
2023-06-07 23:42:47 +02:00
|
|
|
#include "../JsonRandom.h"
|
2023-04-18 22:14:15 +02:00
|
|
|
#include "../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
|
2022-11-29 22:34:32 +02:00
|
|
|
filtersJson.setMeta(input["faction"].meta);
|
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-07-30 19:12:25 +02:00
|
|
|
return BuildingID(VLC->identifiers()->getIdentifier("building." + faction->getJsonKey(), node.Vector()[0]).value());
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-07 23:42:47 +02:00
|
|
|
void CTownInstanceConstructor::randomizeObject(CGTownInstance * object, CRandomGenerator & rng) const
|
2014-06-14 18:42:13 +03:00
|
|
|
{
|
2023-02-12 22:39:17 +02:00
|
|
|
auto templ = getOverride(CGObjectInstance::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"],
|
|
|
|
[&](si32 index) { heroClass = VLC->heroh->classes[index]; });
|
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-07-30 19:12:25 +02:00
|
|
|
return HeroTypeID(VLC->identifiers()->getIdentifier("hero", node.Vector()[0]).value());
|
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.
|
|
|
|
}
|
|
|
|
|
2023-06-07 23:42:47 +02:00
|
|
|
void CHeroInstanceConstructor::randomizeObject(CGHeroInstance * object, CRandomGenerator & 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-04-19 00:11:24 +02:00
|
|
|
onboardAssaultAllowed = input["onboardAssaultAllowed"].Bool();
|
|
|
|
onboardVisitAllowed = input["onboardVisitAllowed"].Bool();
|
2023-04-18 22:14:15 +02:00
|
|
|
actualAnimation = input["actualAnimation"].String();
|
|
|
|
overlayAnimation = input["overlayAnimation"].String();
|
|
|
|
for(int i = 0; i < flagAnimations.size() && i < input["flagAnimations"].Vector().size(); ++i)
|
|
|
|
flagAnimations[i] = input["flagAnimations"].Vector()[i].String();
|
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-06-07 18:51:44 +02:00
|
|
|
std::string BoatInstanceConstructor::getBoatAnimationName() const
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-06-07 23:42:47 +02:00
|
|
|
CGMarket * MarketInstanceConstructor::createObject() const
|
2023-04-28 03:16:10 +02:00
|
|
|
{
|
|
|
|
if(marketModes.size() == 1)
|
|
|
|
{
|
|
|
|
switch(*marketModes.begin())
|
|
|
|
{
|
|
|
|
case EMarketMode::ARTIFACT_RESOURCE:
|
|
|
|
case EMarketMode::RESOURCE_ARTIFACT:
|
2023-06-07 23:42:47 +02:00
|
|
|
return new CGBlackMarket;
|
|
|
|
|
2023-04-28 03:16:10 +02:00
|
|
|
case EMarketMode::RESOURCE_SKILL:
|
2023-06-07 23:42:47 +02:00
|
|
|
return new CGUniversity;
|
2023-04-28 03:16:10 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 23:42:47 +02:00
|
|
|
return new CGMarket;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-06-07 23:42:47 +02:00
|
|
|
void MarketInstanceConstructor::randomizeObject(CGMarket * object, CRandomGenerator & rng) const
|
2023-04-28 03:16:10 +02:00
|
|
|
{
|
|
|
|
if(auto * university = dynamic_cast<CGUniversity *>(object))
|
|
|
|
{
|
|
|
|
for(auto skill : JsonRandom::loadSecondary(predefinedOffer, rng))
|
|
|
|
university->skills.push_back(skill.first.getNum());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|