mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-23 22:37:55 +02: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.
This commit is contained in:
47
scripting/lua/api/events/AdventureEvents.cpp
Normal file
47
scripting/lua/api/events/AdventureEvents.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* AdventureEvents.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"
|
||||
|
||||
#include "AdventureEvents.h"
|
||||
|
||||
#include "../../LuaStack.h"
|
||||
#include "../../LuaCallWrapper.h"
|
||||
#include "../Registry.h"
|
||||
#include "SubscriptionRegistryProxy.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
|
||||
VCMI_REGISTER_SCRIPT_API(ObjectVisitStartedProxy, "events.ObjectVisitStarted");
|
||||
|
||||
const std::vector<ObjectVisitStartedProxy::RegType> ObjectVisitStartedProxy::REGISTER = {};
|
||||
|
||||
const std::vector<ObjectVisitStartedProxy::CustomRegType> ObjectVisitStartedProxy::REGISTER_CUSTOM =
|
||||
{
|
||||
{
|
||||
"subscribeBefore",
|
||||
&SubscriptionRegistryProxy<ObjectVisitStartedProxy>::subscribeBefore,
|
||||
true
|
||||
},
|
||||
{
|
||||
"subscribeAfter",
|
||||
&SubscriptionRegistryProxy<ObjectVisitStartedProxy>::subscribeAfter,
|
||||
true
|
||||
},
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
41
scripting/lua/api/events/AdventureEvents.h
Normal file
41
scripting/lua/api/events/AdventureEvents.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* AdventureEvents.h, 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
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vcmi/events/AdventureEvents.h>
|
||||
|
||||
#include "../../LuaWrapper.h"
|
||||
|
||||
#include "EventBusProxy.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
|
||||
using ::events::ObjectVisitStarted;
|
||||
|
||||
class ObjectVisitStartedProxy : public OpaqueWrapper<ObjectVisitStarted, ObjectVisitStartedProxy>
|
||||
{
|
||||
public:
|
||||
using Wrapper = OpaqueWrapper<ObjectVisitStarted, ObjectVisitStartedProxy>;
|
||||
static const std::vector<typename Wrapper::RegType> REGISTER;
|
||||
static const std::vector<typename Wrapper::CustomRegType> REGISTER_CUSTOM;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
67
scripting/lua/api/events/BattleEvents.cpp
Normal file
67
scripting/lua/api/events/BattleEvents.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* BattleEvents.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"
|
||||
|
||||
#include "BattleEvents.h"
|
||||
|
||||
#include "../../LuaStack.h"
|
||||
#include "../../LuaCallWrapper.h"
|
||||
#include "../Registry.h"
|
||||
#include "../../../../lib/battle/Unit.h"
|
||||
#include "SubscriptionRegistryProxy.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
using ::events::ApplyDamage;
|
||||
|
||||
VCMI_REGISTER_SCRIPT_API(ApplyDamageProxy, "events.ApplyDamage");
|
||||
|
||||
const std::vector<ApplyDamageProxy::RegType> ApplyDamageProxy::REGISTER =
|
||||
{
|
||||
{
|
||||
"getInitalDamage",
|
||||
LuaCallWrapper<ApplyDamage>::createFunctor(&ApplyDamage::getInitalDamage)
|
||||
},
|
||||
{
|
||||
"getDamage",
|
||||
LuaCallWrapper<ApplyDamage>::createFunctor(&ApplyDamage::getDamage)
|
||||
},
|
||||
{
|
||||
"setDamage",
|
||||
LuaCallWrapper<ApplyDamage>::createFunctor(&ApplyDamage::setDamage)
|
||||
},
|
||||
{
|
||||
"getTarget",
|
||||
LuaCallWrapper<ApplyDamage>::createFunctor(&ApplyDamage::getTarget)
|
||||
}
|
||||
};
|
||||
|
||||
const std::vector<ApplyDamageProxy::CustomRegType> ApplyDamageProxy::REGISTER_CUSTOM =
|
||||
{
|
||||
{
|
||||
"subscribeBefore",
|
||||
&SubscriptionRegistryProxy<ApplyDamageProxy>::subscribeBefore,
|
||||
true
|
||||
},
|
||||
{
|
||||
"subscribeAfter",
|
||||
&SubscriptionRegistryProxy<ApplyDamageProxy>::subscribeAfter,
|
||||
true
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
38
scripting/lua/api/events/BattleEvents.h
Normal file
38
scripting/lua/api/events/BattleEvents.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* BattleEvents.h, 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
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vcmi/events/BattleEvents.h>
|
||||
|
||||
#include "../../LuaWrapper.h"
|
||||
|
||||
#include "EventBusProxy.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
|
||||
class ApplyDamageProxy : public OpaqueWrapper<::events::ApplyDamage, ApplyDamageProxy>
|
||||
{
|
||||
public:
|
||||
using Wrapper = OpaqueWrapper<::events::ApplyDamage, ApplyDamageProxy>;
|
||||
static const std::vector<typename Wrapper::RegType> REGISTER;
|
||||
static const std::vector<typename Wrapper::CustomRegType> REGISTER_CUSTOM;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
33
scripting/lua/api/events/EventBusProxy.cpp
Normal file
33
scripting/lua/api/events/EventBusProxy.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* EventBusProxy.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"
|
||||
|
||||
#include "EventBusProxy.h"
|
||||
|
||||
#include "../../LuaStack.h"
|
||||
#include "../../LuaCallWrapper.h"
|
||||
|
||||
#include "../Registry.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
//No methods here, just an empty metatable for type safety.
|
||||
VCMI_REGISTER_CORE_SCRIPT_API(EventBusProxy, "EventBus");
|
||||
const std::vector<EventBusProxy::RegType> EventBusProxy::REGISTER = {};
|
||||
const std::vector<EventBusProxy::CustomRegType> EventBusProxy::REGISTER_CUSTOM = {};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
34
scripting/lua/api/events/EventBusProxy.h
Normal file
34
scripting/lua/api/events/EventBusProxy.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* EventBusProxy.h, 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
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vcmi/events/EventBus.h>
|
||||
|
||||
#include "../../LuaWrapper.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
|
||||
class EventBusProxy : public OpaqueWrapper<::events::EventBus, EventBusProxy>
|
||||
{
|
||||
public:
|
||||
using Wrapper = OpaqueWrapper<::events::EventBus, EventBusProxy>;
|
||||
static const std::vector<typename Wrapper::RegType> REGISTER;
|
||||
static const std::vector<typename Wrapper::CustomRegType> REGISTER_CUSTOM;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
94
scripting/lua/api/events/GenericEvents.cpp
Normal file
94
scripting/lua/api/events/GenericEvents.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* GenericEvents.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"
|
||||
|
||||
#include "GenericEvents.h"
|
||||
|
||||
#include "../../LuaStack.h"
|
||||
#include "../../LuaCallWrapper.h"
|
||||
#include "../Registry.h"
|
||||
#include "SubscriptionRegistryProxy.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
using ::events::GameResumed;
|
||||
using ::events::PlayerGotTurn;
|
||||
using ::events::TurnStarted;
|
||||
|
||||
VCMI_REGISTER_SCRIPT_API(GameResumedProxy, "events.GameResumed");
|
||||
VCMI_REGISTER_SCRIPT_API(PlayerGotTurnProxy, "events.PlayerGotTurn");
|
||||
VCMI_REGISTER_SCRIPT_API(TurnStartedProxy, "events.TurnStarted");
|
||||
|
||||
const std::vector<GameResumedProxy::RegType> GameResumedProxy::REGISTER = {};
|
||||
|
||||
const std::vector<GameResumedProxy::CustomRegType> GameResumedProxy::REGISTER_CUSTOM =
|
||||
{
|
||||
{
|
||||
"subscribeBefore",
|
||||
&SubscriptionRegistryProxy<GameResumedProxy>::subscribeBefore,
|
||||
true
|
||||
},
|
||||
{
|
||||
"subscribeAfter",
|
||||
&SubscriptionRegistryProxy<GameResumedProxy>::subscribeAfter,
|
||||
true
|
||||
}
|
||||
};
|
||||
|
||||
const std::vector<PlayerGotTurnProxy::RegType> PlayerGotTurnProxy::REGISTER =
|
||||
{
|
||||
{
|
||||
"getPlayer",
|
||||
LuaCallWrapper<PlayerGotTurn>::createFunctor(&PlayerGotTurn::getPlayerIndex)
|
||||
},
|
||||
{
|
||||
"setPlayer",
|
||||
LuaCallWrapper<PlayerGotTurn>::createFunctor(&PlayerGotTurn::setPlayerIndex)
|
||||
},
|
||||
};
|
||||
|
||||
const std::vector<PlayerGotTurnProxy::CustomRegType> PlayerGotTurnProxy::REGISTER_CUSTOM =
|
||||
{
|
||||
{
|
||||
"subscribeBefore",
|
||||
&SubscriptionRegistryProxy<PlayerGotTurnProxy>::subscribeBefore,
|
||||
true
|
||||
},
|
||||
{
|
||||
"subscribeAfter",
|
||||
&SubscriptionRegistryProxy<PlayerGotTurnProxy>::subscribeAfter,
|
||||
true
|
||||
}
|
||||
};
|
||||
|
||||
const std::vector<TurnStartedProxy::RegType> TurnStartedProxy::REGISTER = {};
|
||||
|
||||
const std::vector<TurnStartedProxy::CustomRegType> TurnStartedProxy::REGISTER_CUSTOM =
|
||||
{
|
||||
{
|
||||
"subscribeBefore",
|
||||
&SubscriptionRegistryProxy<TurnStartedProxy>::subscribeBefore,
|
||||
true
|
||||
},
|
||||
{
|
||||
"subscribeAfter",
|
||||
&SubscriptionRegistryProxy<TurnStartedProxy>::subscribeAfter,
|
||||
true
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
54
scripting/lua/api/events/GenericEvents.h
Normal file
54
scripting/lua/api/events/GenericEvents.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* GenericEvents.h, 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
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vcmi/events/GenericEvents.h>
|
||||
|
||||
#include "../../LuaWrapper.h"
|
||||
|
||||
#include "EventBusProxy.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
|
||||
class GameResumedProxy : public OpaqueWrapper<::events::GameResumed, GameResumedProxy>
|
||||
{
|
||||
public:
|
||||
using Wrapper = OpaqueWrapper<::events::GameResumed, GameResumedProxy>;
|
||||
static const std::vector<typename Wrapper::RegType> REGISTER;
|
||||
static const std::vector<typename Wrapper::CustomRegType> REGISTER_CUSTOM;
|
||||
};
|
||||
|
||||
class PlayerGotTurnProxy : public OpaqueWrapper<::events::PlayerGotTurn, PlayerGotTurnProxy>
|
||||
{
|
||||
public:
|
||||
using Wrapper = OpaqueWrapper<::events::PlayerGotTurn, PlayerGotTurnProxy>;
|
||||
static const std::vector<typename Wrapper::RegType> REGISTER;
|
||||
static const std::vector<typename Wrapper::CustomRegType> REGISTER_CUSTOM;
|
||||
};
|
||||
|
||||
class TurnStartedProxy : public OpaqueWrapper<::events::TurnStarted, TurnStartedProxy>
|
||||
{
|
||||
public:
|
||||
using Wrapper = OpaqueWrapper<::events::TurnStarted, TurnStartedProxy>;
|
||||
static const std::vector<typename Wrapper::RegType> REGISTER;
|
||||
static const std::vector<typename Wrapper::CustomRegType> REGISTER_CUSTOM;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
28
scripting/lua/api/events/SubscriptionRegistryProxy.cpp
Normal file
28
scripting/lua/api/events/SubscriptionRegistryProxy.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* SubscriptionRegistryProxy.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"
|
||||
|
||||
#include "SubscriptionRegistryProxy.h"
|
||||
|
||||
#include "../Registry.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
//No methods here, just an empty metatable for type safety.
|
||||
VCMI_REGISTER_CORE_SCRIPT_API(EventSubscriptionProxy, "EventSubscription");
|
||||
const std::vector<EventSubscriptionProxy::RegType> EventSubscriptionProxy::REGISTER = {};
|
||||
const std::vector<EventSubscriptionProxy::CustomRegType> EventSubscriptionProxy::REGISTER_CUSTOM = {};
|
||||
}
|
||||
}
|
||||
}
|
||||
131
scripting/lua/api/events/SubscriptionRegistryProxy.h
Normal file
131
scripting/lua/api/events/SubscriptionRegistryProxy.h
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* SubscriptionRegistryProxy.h, 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
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vcmi/events/Event.h>
|
||||
#include <vcmi/events/EventBus.h>
|
||||
#include <vcmi/events/SubscriptionRegistry.h>
|
||||
|
||||
#include "../../LuaWrapper.h"
|
||||
#include "../../LuaStack.h"
|
||||
#include "../../LuaReference.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
|
||||
|
||||
class EventSubscriptionProxy : public UniqueOpaqueWrapper<::events::EventSubscription, EventSubscriptionProxy>
|
||||
{
|
||||
public:
|
||||
using Wrapper = UniqueOpaqueWrapper<::events::EventSubscription, EventSubscriptionProxy>;
|
||||
static const std::vector<typename Wrapper::RegType> REGISTER;
|
||||
static const std::vector<typename Wrapper::CustomRegType> REGISTER_CUSTOM;
|
||||
};
|
||||
|
||||
template <typename EventProxy>
|
||||
class SubscriptionRegistryProxy
|
||||
{
|
||||
public:
|
||||
using EventType = typename EventProxy::ObjectType;
|
||||
using RegistryType = ::events::SubscriptionRegistry<EventType>;
|
||||
|
||||
static_assert(std::is_base_of<::events::Event, EventType>::value, "Invalid template parameter");
|
||||
|
||||
static int subscribeBefore(lua_State * L)
|
||||
{
|
||||
LuaStack S(L);
|
||||
// subscription = subscribeBefore(eventBus, callback)
|
||||
|
||||
//TODO: use capture by move from c++14
|
||||
auto callbackRef = std::make_shared<LuaReference>(L);
|
||||
|
||||
::events::EventBus * eventBus = nullptr;
|
||||
|
||||
if(!S.tryGet(1, eventBus))
|
||||
{
|
||||
S.push("No event bus");
|
||||
return 1;
|
||||
}
|
||||
|
||||
S.clear();
|
||||
|
||||
RegistryType * registry = EventType::getRegistry();
|
||||
|
||||
typename EventType::PreHandler callback = [=](EventType & event)
|
||||
{
|
||||
LuaStack S(L);
|
||||
callbackRef->push();
|
||||
S.push(&event);
|
||||
|
||||
if(lua_pcall(L, 1, 0, 0) != 0)
|
||||
{
|
||||
std::string msg;
|
||||
S.tryGet(1, msg);
|
||||
logMod->error("Script callback error: %s", msg);
|
||||
}
|
||||
|
||||
S.clear();
|
||||
};
|
||||
|
||||
std::unique_ptr<::events::EventSubscription> subscription = registry->subscribeBefore(eventBus, std::move(callback));
|
||||
S.push(std::move(subscription));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int subscribeAfter(lua_State * L)
|
||||
{
|
||||
LuaStack S(L);
|
||||
|
||||
//TODO: use capture by move from c++14
|
||||
auto callbackRef = std::make_shared<LuaReference>(L);
|
||||
|
||||
::events::EventBus * eventBus = nullptr;
|
||||
|
||||
if(!S.tryGet(1, eventBus))
|
||||
{
|
||||
S.push("No event bus");
|
||||
return 1;
|
||||
}
|
||||
|
||||
S.clear();
|
||||
|
||||
RegistryType * registry = EventType::getRegistry();
|
||||
|
||||
typename EventType::PostHandler callback = [=](const EventType & event)
|
||||
{
|
||||
LuaStack S(L);
|
||||
callbackRef->push();
|
||||
S.push(const_cast<EventType *>(&event)); //FIXME:
|
||||
|
||||
if(lua_pcall(L, 1, 0, 0) != 0)
|
||||
{
|
||||
std::string msg;
|
||||
S.tryGet(1, msg);
|
||||
logMod->error("Script callback error: %s", msg);
|
||||
}
|
||||
|
||||
S.clear();
|
||||
};
|
||||
|
||||
std::unique_ptr<::events::EventSubscription> subscription = registry->subscribeAfter(eventBus, std::move(callback));
|
||||
S.push(std::move(subscription));
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user