world.callScriptContext

This commit is contained in:
Kae 2023-07-23 13:49:34 +10:00
parent d93b026829
commit 121d27446b
4 changed files with 21 additions and 2 deletions

View File

@ -1578,6 +1578,13 @@ bool WorldServer::regionActive(RectI const& region) {
return true;
}
WorldServer::ScriptComponentPtr WorldServer::scriptContext(String const& contextName) {
if (auto context = m_scriptContexts.ptr(contextName))
return *context;
else
return nullptr;
}
RpcPromise<Vec2I> WorldServer::enqueuePlacement(List<BiomeItemDistribution> distributions, Maybe<DungeonId> id) {
return m_worldStorage->enqueuePlacement(move(distributions), id);
}

View File

@ -49,6 +49,9 @@ extern EnumMap<WorldServerFidelity> const WorldServerFidelityNames;
class WorldServer : public World {
public:
typedef LuaMessageHandlingComponent<LuaUpdatableComponent<LuaWorldComponent<LuaBaseComponent>>> ScriptComponent;
typedef shared_ptr<ScriptComponent> ScriptComponentPtr;
// Create a new world with the given template, writing new storage file.
WorldServer(WorldTemplatePtr const& worldTemplate, IODevicePtr storage);
// Synonym for WorldServer(make_shared<WorldTemplate>(size), storage);
@ -193,6 +196,8 @@ public:
// Returns true if a region is fully active without signaling it.
bool regionActive(RectI const& region);
ScriptComponentPtr scriptContext(String const& contextName);
// Queues a microdungeon for placement
RpcPromise<Vec2I> enqueuePlacement(List<BiomeItemDistribution> distributions, Maybe<DungeonId> id);
@ -350,8 +355,6 @@ private:
WireProcessorPtr m_wireProcessor;
LuaRootPtr m_luaRoot;
typedef LuaMessageHandlingComponent<LuaUpdatableComponent<LuaWorldComponent<LuaBaseComponent>>> ScriptComponent;
typedef shared_ptr<ScriptComponent> ScriptComponentPtr;
StringMap<ScriptComponentPtr> m_scriptContexts;
WorldGeometry m_geometry;

View File

@ -383,6 +383,7 @@ namespace LuaBindings {
callbacks.registerCallbackWithSignature<void, Vec2F, Maybe<bool>>("setPlayerStart", bind(ServerWorldCallbacks::setPlayerStart, world, _1, _2));
callbacks.registerCallbackWithSignature<List<EntityId>>("players", bind(ServerWorldCallbacks::players, world));
callbacks.registerCallbackWithSignature<LuaString, LuaEngine&>("fidelity", bind(ServerWorldCallbacks::fidelity, world, _1));
callbacks.registerCallbackWithSignature<Maybe<LuaValue>, String, String, LuaVariadic<LuaValue>>("callScriptContext", bind(ServerWorldCallbacks::callScriptContext, world, _1, _2, _3));
callbacks.registerCallbackWithSignature<double>("skyTime", [serverWorld]() {
return serverWorld->sky()->epochTime();
@ -1168,6 +1169,13 @@ namespace LuaBindings {
return engine.createString(WorldServerFidelityNames.getRight(as<WorldServer>(world)->fidelity()));
}
Maybe<LuaValue> ServerWorldCallbacks::callScriptContext(World* world, String const& contextName, String const& function, LuaVariadic<LuaValue> const& args) {
auto context = as<WorldServer>(world)->scriptContext(contextName);
if (!context)
throw StarException::format("Context {} does not exist", contextName);
return context->invoke(function, args);
}
void WorldDebugCallbacks::debugPoint(Vec2F const& arg1, Color const& arg2) {
SpatialLogger::logPoint("world", arg1, arg2.toRgba());
}

View File

@ -86,6 +86,7 @@ namespace LuaBindings {
void setPlayerStart(World* world, Vec2F const& playerStart, Maybe<bool> respawnInWorld);
List<EntityId> players(World* world);
LuaString fidelity(World* world, LuaEngine& engine);
Maybe<LuaValue> callScriptContext(World* world, String const& contextName, String const& function, LuaVariadic<LuaValue> const& args);
}
namespace WorldDebugCallbacks {