Implemented trigger creation & deletion via lua
Triggers now have handles for simple understanding, but there's nothing preventing multiple triggers from sharing the same name.
This commit is contained in:
@@ -94,7 +94,6 @@ void World::SendAdminDisconnectForced() {
|
|||||||
//TODO: (9) World::SendAdminDisconnectForced()
|
//TODO: (9) World::SendAdminDisconnectForced()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void World::SendAdminShutdownRequest() {
|
void World::SendAdminShutdownRequest() {
|
||||||
ClientPacket newPacket;
|
ClientPacket newPacket;
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ mapSaver = require("map_saver")
|
|||||||
roomSystem = require("room_system")
|
roomSystem = require("room_system")
|
||||||
characterSystem = require("character_system")
|
characterSystem = require("character_system")
|
||||||
networkSystem = require("network")
|
networkSystem = require("network")
|
||||||
|
triggerSystem = require("trigger_system")
|
||||||
|
|
||||||
local function dumpTable(t)
|
local function dumpTable(t)
|
||||||
print(t)
|
print(t)
|
||||||
@@ -43,4 +44,17 @@ end)
|
|||||||
local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp")
|
local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp")
|
||||||
roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save)
|
roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save)
|
||||||
|
|
||||||
|
--debugging
|
||||||
|
triggerMgr = roomSystem.Room.GetTriggerMgr(overworld)
|
||||||
|
trigger1, uid1 = triggerSystem.TriggerManager.Create(triggerMgr, "handle1")
|
||||||
|
trigger2, uid2 = triggerSystem.TriggerManager.Create(triggerMgr, "handle1")
|
||||||
|
|
||||||
|
print("triggers:", triggerSystem.TriggerManager.GetCount(triggerMgr))
|
||||||
|
|
||||||
|
local deleted = triggerSystem.TriggerManager.Unload(triggerMgr, triggerSystem.Trigger.GetHandle(trigger1))
|
||||||
|
|
||||||
|
print("triggers:", triggerSystem.TriggerManager.GetCount(triggerMgr))
|
||||||
|
|
||||||
|
print("deleted: ", deleted)
|
||||||
|
|
||||||
print("Finished the lua script")
|
print("Finished the lua script")
|
||||||
|
|||||||
@@ -23,6 +23,19 @@
|
|||||||
|
|
||||||
#include "trigger_data.hpp"
|
#include "trigger_data.hpp"
|
||||||
|
|
||||||
|
//hamdle
|
||||||
|
static int setHandle(lua_State* L) {
|
||||||
|
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
|
||||||
|
trigger->SetHandle(lua_tostring(L, 2));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getHandle(lua_State* L) {
|
||||||
|
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
|
||||||
|
lua_pushstring(L, trigger->GetHandle().c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
//origin
|
//origin
|
||||||
static int setOrigin(lua_State* L) {
|
static int setOrigin(lua_State* L) {
|
||||||
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
|
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
|
||||||
@@ -74,6 +87,9 @@ static int getReference(lua_State* L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static const luaL_Reg triggerLib[] = {
|
static const luaL_Reg triggerLib[] = {
|
||||||
|
{"SetHandle", setHandle},
|
||||||
|
{"GetHandle", getHandle},
|
||||||
|
|
||||||
{"SetOrigin",setOrigin},
|
{"SetOrigin",setOrigin},
|
||||||
{"GetOrigin",getOrigin},
|
{"GetOrigin",getOrigin},
|
||||||
|
|
||||||
@@ -82,6 +98,7 @@ static const luaL_Reg triggerLib[] = {
|
|||||||
|
|
||||||
{"SetScript",setReference},
|
{"SetScript",setReference},
|
||||||
{"GetScript",getReference},
|
{"GetScript",getReference},
|
||||||
|
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,14 @@
|
|||||||
*/
|
*/
|
||||||
#include "trigger_data.hpp"
|
#include "trigger_data.hpp"
|
||||||
|
|
||||||
|
std::string TriggerData::SetHandle(std::string s) {
|
||||||
|
return handle = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string TriggerData::GetHandle() const {
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
int TriggerData::SetScriptReference(int i) {
|
int TriggerData::SetScriptReference(int i) {
|
||||||
return scriptRef = i;
|
return scriptRef = i;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ public:
|
|||||||
TriggerData() = default;
|
TriggerData() = default;
|
||||||
~TriggerData() = default;
|
~TriggerData() = default;
|
||||||
|
|
||||||
|
std::string SetHandle(std::string);
|
||||||
|
std::string GetHandle() const;
|
||||||
|
|
||||||
Vector2 SetOrigin(Vector2 v);
|
Vector2 SetOrigin(Vector2 v);
|
||||||
Vector2 GetOrigin();
|
Vector2 GetOrigin();
|
||||||
|
|
||||||
@@ -44,8 +47,7 @@ public:
|
|||||||
int GetScriptReference();
|
int GetScriptReference();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class TriggerManager;
|
std::string handle;
|
||||||
|
|
||||||
Vector2 origin;
|
Vector2 origin;
|
||||||
BoundingBox bounds;
|
BoundingBox bounds;
|
||||||
int scriptRef = LUA_NOREF;
|
int scriptRef = LUA_NOREF;
|
||||||
|
|||||||
@@ -29,23 +29,13 @@ TriggerManager::~TriggerManager() {
|
|||||||
UnloadAll();
|
UnloadAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
int TriggerManager::Create() {
|
int TriggerManager::Create(std::string handle, Vector2 origin, BoundingBox bounds) {
|
||||||
//implicitly creates the element
|
//implicitly creates the element
|
||||||
TriggerData& triggerData = elementMap[counter];
|
TriggerData& triggerData = elementMap[counter];
|
||||||
|
|
||||||
//no real values set
|
triggerData.SetHandle(handle);
|
||||||
triggerData.origin = {0, 0};
|
triggerData.SetOrigin(origin);
|
||||||
triggerData.bounds = {0, 0, 0, 0};
|
triggerData.SetBoundingBox(bounds);
|
||||||
|
|
||||||
return counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
int TriggerManager::Create(Vector2 origin, BoundingBox bounds) {
|
|
||||||
//implicitly creates the element
|
|
||||||
TriggerData& triggerData = elementMap[counter];
|
|
||||||
|
|
||||||
triggerData.origin = origin;
|
|
||||||
triggerData.bounds = bounds;
|
|
||||||
|
|
||||||
return counter++;
|
return counter++;
|
||||||
}
|
}
|
||||||
@@ -80,6 +70,15 @@ TriggerData* TriggerManager::Get(int uid) {
|
|||||||
return &it->second;
|
return &it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TriggerData* TriggerManager::Get(std::string handle) {
|
||||||
|
for (std::map<int, TriggerData>::iterator it = elementMap.begin(); it != elementMap.end(); ++it) {
|
||||||
|
if (it->second.GetHandle() == handle) {
|
||||||
|
return &it->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
int TriggerManager::GetLoadedCount() {
|
int TriggerManager::GetLoadedCount() {
|
||||||
return elementMap.size();
|
return elementMap.size();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,8 +38,7 @@ public:
|
|||||||
~TriggerManager();
|
~TriggerManager();
|
||||||
|
|
||||||
//common public methods
|
//common public methods
|
||||||
int Create();
|
int Create(std::string handle, Vector2 origin, BoundingBox bounds);
|
||||||
int Create(Vector2 origin, BoundingBox bounds);
|
|
||||||
void Unload(int uid);
|
void Unload(int uid);
|
||||||
|
|
||||||
void UnloadAll();
|
void UnloadAll();
|
||||||
@@ -47,6 +46,7 @@ public:
|
|||||||
|
|
||||||
//accessors & mutators
|
//accessors & mutators
|
||||||
TriggerData* Get(int uid);
|
TriggerData* Get(int uid);
|
||||||
|
TriggerData* Get(std::string handle);
|
||||||
int GetLoadedCount();
|
int GetLoadedCount();
|
||||||
std::map<int, TriggerData>* GetContainer();
|
std::map<int, TriggerData>* GetContainer();
|
||||||
|
|
||||||
|
|||||||
@@ -27,18 +27,105 @@
|
|||||||
|
|
||||||
static int create(lua_State* L) {
|
static int create(lua_State* L) {
|
||||||
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
||||||
|
|
||||||
|
//pad the stack with default parameters
|
||||||
|
if (lua_gettop(L) == 2) {
|
||||||
|
lua_pushnumber(L, 0.0); //vector.x
|
||||||
|
lua_pushnumber(L, 0.0); //vector.y
|
||||||
|
}
|
||||||
|
if (lua_gettop(L) == 4) {
|
||||||
|
lua_pushinteger(L, 0); //bounds.x
|
||||||
|
lua_pushinteger(L, 0); //bounds.y
|
||||||
|
lua_pushinteger(L, 0); //bounds.w
|
||||||
|
lua_pushinteger(L, 0); //bounds.h
|
||||||
|
}
|
||||||
|
|
||||||
|
//create the trigger
|
||||||
|
int index = mgr->Create(
|
||||||
|
lua_tostring(L, 2), //handle
|
||||||
|
{
|
||||||
|
lua_tonumber(L, 3), //vector.x
|
||||||
|
lua_tonumber(L, 4) //vector.y
|
||||||
|
},
|
||||||
|
{
|
||||||
|
lua_tointeger(L, 5), //bounds.x
|
||||||
|
lua_tointeger(L, 6), //bounds.y
|
||||||
|
lua_tointeger(L, 7), //bounds.w
|
||||||
|
lua_tointeger(L, 8) //bounds.h
|
||||||
|
});
|
||||||
|
|
||||||
|
//push to the scipts
|
||||||
|
lua_pushlightuserdata(L, static_cast<void*>(mgr->Get(index)) );
|
||||||
|
lua_pushinteger(L, index);
|
||||||
|
|
||||||
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int unload(lua_State* L) {
|
static int unload(lua_State* L) {
|
||||||
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
||||||
|
int count = 0; //the number removed
|
||||||
|
|
||||||
|
//based on the type
|
||||||
|
switch(lua_type(L, 2)) {
|
||||||
|
//unload this index
|
||||||
|
case LUA_TNUMBER:
|
||||||
|
mgr->UnloadIf([L, &count](std::pair<int, TriggerData const&> it) -> bool {
|
||||||
|
if (it.first == lua_tointeger(L, 2)) {
|
||||||
|
count++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
//unload this name
|
||||||
|
case LUA_TSTRING:
|
||||||
|
mgr->UnloadIf([L, &count](std::pair<int, TriggerData const&> it) -> bool {
|
||||||
|
if (it.second.GetHandle() == lua_tostring(L, 2)) {
|
||||||
|
count++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//return the number removed
|
||||||
|
lua_pushinteger(L, count);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getTrigger(lua_State* L) { //TODO: (1) named triggers
|
static int getTrigger(lua_State* L) {
|
||||||
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
||||||
lua_pushlightuserdata(L, mgr->Get(lua_tointeger(L, 2)));
|
TriggerData* triggerData = nullptr;
|
||||||
|
|
||||||
|
switch(lua_type(L, 2)) {
|
||||||
|
case LUA_TNUMBER:
|
||||||
|
triggerData = mgr->Get(lua_tointeger(L, 2));
|
||||||
|
break;
|
||||||
|
case LUA_TSTRING:
|
||||||
|
triggerData = mgr->Get(lua_tostring(L, 2));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (triggerData) {
|
||||||
|
lua_pushlightuserdata(L, static_cast<void*>(triggerData));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
lua_pushnil(L);
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int forEach(lua_State* L) {
|
||||||
|
//TODO: (9) forEach()
|
||||||
|
}
|
||||||
|
|
||||||
static int getLoadedCount(lua_State* L) {
|
static int getLoadedCount(lua_State* L) {
|
||||||
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
||||||
lua_pushinteger(L, mgr->GetLoadedCount());
|
lua_pushinteger(L, mgr->GetLoadedCount());
|
||||||
|
|||||||
Reference in New Issue
Block a user