1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/lib/VCMI_Lib.cpp

327 lines
7.1 KiB
C++
Raw Normal View History

/*
* VCMI_Lib.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"
2008-06-14 13:20:18 +03:00
#include "VCMI_Lib.h"
#include "CArtHandler.h"
#include "CBonusTypeHandler.h"
#include "CCreatureHandler.h"
#include "mapObjects/CObjectClassesHandler.h"
#include "CHeroHandler.h"
#include "mapObjects/CObjectHandler.h"
#include "CTownHandler.h"
#include "CBuildingHandler.h"
#include "spells/CSpellHandler.h"
#include "spells/effects/Registry.h"
#include "CSkillHandler.h"
#include "CGeneralTextHandler.h"
#include "CModHandler.h"
#include "IGameEventsReceiver.h"
#include "CStopWatch.h"
#include "VCMIDirs.h"
#include "filesystem/Filesystem.h"
#include "CConsoleHandler.h"
#include "rmg/CRmgTemplateStorage.h"
#include "mapping/CMapEditManager.h"
#include "ScriptHandler.h"
#include "BattleFieldHandler.h"
#include "ObstacleHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
LibClasses * VLC = nullptr;
DLL_LINKAGE void preinitDLL(CConsoleHandler * Console, bool onlyEssential, bool extractArchives)
2008-06-14 13:20:18 +03:00
{
console = Console;
VLC = new LibClasses();
try
{
VLC->loadFilesystem(onlyEssential, extractArchives);
}
2015-02-14 21:42:47 +02:00
catch(...)
{
handleException();
throw;
}
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
DLL_LINKAGE void loadDLLClasses(bool onlyEssential)
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
VLC->init(onlyEssential);
2008-06-14 13:20:18 +03:00
}
const ArtifactService * LibClasses::artifacts() const
{
return arth;
}
const CreatureService * LibClasses::creatures() const
{
return creh;
}
const FactionService * LibClasses::factions() const
{
return townh;
}
const HeroClassService * LibClasses::heroClasses() const
{
return &heroh->classes;
}
const HeroTypeService * LibClasses::heroTypes() const
{
return heroh;
}
#if SCRIPTING_ENABLED
const scripting::Service * LibClasses::scripts() const
{
return scriptHandler;
}
#endif
const spells::Service * LibClasses::spells() const
{
return spellh;
}
const SkillService * LibClasses::skills() const
{
return skillh;
}
const IBonusTypeHandler * LibClasses::getBth() const
{
return bth;
}
const spells::effects::Registry * LibClasses::spellEffects() const
{
return spells::effects::GlobalRegistry::get();
}
spells::effects::Registry * LibClasses::spellEffects()
{
return spells::effects::GlobalRegistry::get();
}
const BattleFieldService * LibClasses::battlefields() const
{
return battlefieldsHandler;
}
const ObstacleService * LibClasses::obstacles() const
{
return obstacleHandler;
}
void LibClasses::updateEntity(Metatype metatype, int32_t index, const JsonNode & data)
{
switch(metatype)
{
case Metatype::ARTIFACT:
arth->updateEntity(index, data);
break;
case Metatype::CREATURE:
creh->updateEntity(index, data);
break;
case Metatype::FACTION:
townh->updateEntity(index, data);
break;
case Metatype::HERO_CLASS:
heroh->classes.updateEntity(index, data);
break;
case Metatype::HERO_TYPE:
heroh->updateEntity(index, data);
break;
case Metatype::SKILL:
skillh->updateEntity(index, data);
break;
case Metatype::SPELL:
spellh->updateEntity(index, data);
break;
default:
logGlobal->error("Invalid Metatype id %d", static_cast<int32_t>(metatype));
break;
}
}
void LibClasses::loadFilesystem(bool onlyEssential, bool extractArchives)
{
CStopWatch totalTime;
CStopWatch loadTime;
CResourceHandler::initialize();
2017-08-10 20:59:55 +02:00
logGlobal->info("\tInitialization: %d ms", loadTime.getDiff());
CResourceHandler::load("config/filesystem.json", extractArchives);
2017-08-10 20:59:55 +02:00
logGlobal->info("\tData loading: %d ms", loadTime.getDiff());
2016-08-30 10:45:05 +02:00
modh = new CModHandler();
2017-08-10 20:59:55 +02:00
logGlobal->info("\tMod handler: %d ms", loadTime.getDiff());
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
modh->loadMods(onlyEssential);
modh->loadModFilesystems();
2017-08-10 20:59:55 +02:00
logGlobal->info("\tMod filesystems: %d ms", loadTime.getDiff());
2017-08-10 20:59:55 +02:00
logGlobal->info("Basic initialization: %d ms", totalTime.getDiff());
}
2017-08-10 20:59:55 +02:00
static void logHandlerLoaded(const std::string & name, CStopWatch & timer)
{
logGlobal->info("\t\t %s handler: %d ms", name, timer.getDiff());
}
template <class Handler> void createHandler(Handler *&handler, const std::string &name, CStopWatch &timer)
{
handler = new Handler();
logHandlerLoaded(name, timer);
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
void LibClasses::init(bool onlyEssential)
{
CStopWatch pomtime, totalTime;
modh->initializeConfig();
createHandler(bth, "Bonus type", pomtime);
createHandler(roadTypeHandler, "Road", pomtime);
createHandler(riverTypeHandler, "River", pomtime);
2022-09-21 11:34:23 +02:00
createHandler(terrainTypeHandler, "Terrain", pomtime);
createHandler(generaltexth, "General text", pomtime);
createHandler(heroh, "Hero", pomtime);
createHandler(arth, "Artifact", pomtime);
createHandler(creh, "Creature", pomtime);
createHandler(townh, "Town", pomtime);
createHandler(objh, "Object", pomtime);
createHandler(objtypeh, "Object types information", pomtime);
createHandler(spellh, "Spell", pomtime);
createHandler(skillh, "Skill", pomtime);
createHandler(terviewh, "Terrain view pattern", pomtime);
createHandler(tplh, "Template", pomtime); //templates need already resolved identifiers (refactor?)
#if SCRIPTING_ENABLED
createHandler(scriptHandler, "Script", pomtime);
#endif
createHandler(battlefieldsHandler, "Battlefields", pomtime);
createHandler(obstacleHandler, "Obstacles", pomtime);
2017-08-10 20:59:55 +02:00
logGlobal->info("\tInitializing handlers: %d ms", totalTime.getDiff());
modh->load();
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
modh->afterLoad(onlyEssential);
//FIXME: make sure that everything is ok after game restart
//TODO: This should be done every time mod config changes
}
void LibClasses::clear()
{
delete generaltexth;
delete heroh;
delete arth;
delete creh;
delete townh;
delete objh;
delete objtypeh;
delete spellh;
delete skillh;
delete modh;
delete bth;
delete tplh;
delete terviewh;
#if SCRIPTING_ENABLED
delete scriptHandler;
#endif
delete battlefieldsHandler;
makeNull();
}
void LibClasses::makeNull()
{
generaltexth = nullptr;
heroh = nullptr;
arth = nullptr;
creh = nullptr;
townh = nullptr;
objh = nullptr;
objtypeh = nullptr;
spellh = nullptr;
skillh = nullptr;
modh = nullptr;
bth = nullptr;
tplh = nullptr;
terviewh = nullptr;
#if SCRIPTING_ENABLED
scriptHandler = nullptr;
#endif
battlefieldsHandler = nullptr;
}
LibClasses::LibClasses()
{
2016-11-27 21:07:01 +02:00
IS_AI_ENABLED = false;
//init pointers to handlers
makeNull();
}
void LibClasses::callWhenDeserializing()
{
//FIXME: check if any of these are needed
//generaltexth = new CGeneralTextHandler();
//generaltexth->load();
//arth->load(true);
//modh->recreateHandlers();
2012-08-24 12:37:52 +03:00
//modh->loadConfigFromFile ("defaultMods"); //TODO: remember last saved config
}
#if SCRIPTING_ENABLED
void LibClasses::scriptsLoaded()
{
scriptHandler->performRegistration(this);
}
#endif
LibClasses::~LibClasses()
{
clear();
}
std::shared_ptr<CContentHandler> LibClasses::getContent() const
{
return modh->content;
}
void LibClasses::setContent(std::shared_ptr<CContentHandler> content)
{
modh->content = content;
}
VCMI_LIB_NAMESPACE_END