diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 6cd1c1e..3edc755 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -19,10 +19,10 @@ roomSystem.RoomManager.SetOnCreate(function(room, index) print("", "Creating room: ", roomSystem.Room.GetName(room), index) --called ~60 times per second - roomSystem.Room.SetOnTick(room, function(room) - roomSystem.Room.ForEachCharacter(room, function(character) - print(characterSystem.Character.GetHandle(character)) - end) +-- roomSystem.Room.SetOnTick(room, function(room) +-- roomSystem.Room.ForEachCharacter(room, function(character) +-- print(characterSystem.Character.GetHandle(character)) +-- end) --[[ local character = characterSystem.CharacterManager.GetCharacter("handle") if character ~= nil then @@ -36,7 +36,7 @@ roomSystem.RoomManager.SetOnCreate(function(room, index) end end --]] - end) +-- end) end) roomSystem.RoomManager.SetOnUnload(function(room, index) @@ -47,20 +47,21 @@ end) local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp") roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save) ---[[ --debugging +function dumpTrigger(t) + local originX, originY = triggerSystem.Trigger.GetOrigin(t) + local bx, by, bw, bh = triggerSystem.Trigger.GetBounds(t) + local s = triggerSystem.Trigger.GetScript(t) + print(triggerSystem.Trigger.GetHandle(t), originX, originY, bx, by, bw, bh, s) +end + 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) ---]] +trigger2, uid2 = triggerSystem.TriggerManager.Create(triggerMgr, "handle2", 30.2, 40.2) +trigger3, uid3 = triggerSystem.TriggerManager.Create(triggerMgr, "handle3", 30.2, 40.2, 0, 16, 32, 32, function() end) +dumpTrigger(trigger1) +dumpTrigger(trigger2) +dumpTrigger(trigger3) print("Finished the lua script") diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 8328f99..a04b558 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -96,7 +96,7 @@ static int setOnTick(lua_State* L) { static int getOnTick(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); - lua_rawgeti(lua, LUA_REGISTRYINDEX, room->GetTickReference()); + lua_rawgeti(L, LUA_REGISTRYINDEX, room->GetTickReference()); return 1; } diff --git a/server/triggers/trigger_api.cpp b/server/triggers/trigger_api.cpp index 8b063c6..7ea007c 100644 --- a/server/triggers/trigger_api.cpp +++ b/server/triggers/trigger_api.cpp @@ -64,10 +64,10 @@ static int setBoundingBox(lua_State* L) { static int getBoundingBox(lua_State* L) { TriggerData* trigger = static_cast(lua_touserdata(L, 1)); - lua_pushnumber(L, trigger->GetBoundingBox().x); - lua_pushnumber(L, trigger->GetBoundingBox().y); - lua_pushnumber(L, trigger->GetBoundingBox().w); - lua_pushnumber(L, trigger->GetBoundingBox().h); + lua_pushinteger(L, trigger->GetBoundingBox().x); + lua_pushinteger(L, trigger->GetBoundingBox().y); + lua_pushinteger(L, trigger->GetBoundingBox().w); + lua_pushinteger(L, trigger->GetBoundingBox().h); return 4; } diff --git a/server/triggers/trigger_data.cpp b/server/triggers/trigger_data.cpp index a6521af..f4edc80 100644 --- a/server/triggers/trigger_data.cpp +++ b/server/triggers/trigger_data.cpp @@ -29,12 +29,12 @@ std::string TriggerData::GetHandle() const { return handle; } -int TriggerData::SetScriptReference(int i) { - return scriptRef = i; +Vector2 TriggerData::SetOrigin(Vector2 v) { + return origin = v; } -int TriggerData::GetScriptReference() { - return scriptRef; +Vector2 TriggerData::GetOrigin() { + return origin; } BoundingBox TriggerData::SetBoundingBox(BoundingBox b) { @@ -45,10 +45,10 @@ BoundingBox TriggerData::GetBoundingBox() { return bounds; } -Vector2 TriggerData::SetOrigin(Vector2 v) { - return origin = v; +int TriggerData::SetScriptReference(int i) { + return scriptRef = i; } -Vector2 TriggerData::GetOrigin() { - return origin; -} +int TriggerData::GetScriptReference() { + return scriptRef; +} \ No newline at end of file diff --git a/server/triggers/trigger_manager.cpp b/server/triggers/trigger_manager.cpp index 238af08..c6de249 100644 --- a/server/triggers/trigger_manager.cpp +++ b/server/triggers/trigger_manager.cpp @@ -29,13 +29,11 @@ TriggerManager::~TriggerManager() { UnloadAll(); } -int TriggerManager::Create(std::string handle, Vector2 origin, BoundingBox bounds) { +int TriggerManager::Create(std::string handle) { //implicitly creates the element TriggerData& triggerData = elementMap[counter]; triggerData.SetHandle(handle); - triggerData.SetOrigin(origin); - triggerData.SetBoundingBox(bounds); return counter++; } @@ -45,6 +43,7 @@ void TriggerManager::Unload(int uid) { } void TriggerManager::UnloadAll() { + //TODO: save? elementMap.clear(); } diff --git a/server/triggers/trigger_manager.hpp b/server/triggers/trigger_manager.hpp index cd64fb3..9fa541c 100644 --- a/server/triggers/trigger_manager.hpp +++ b/server/triggers/trigger_manager.hpp @@ -38,7 +38,7 @@ public: ~TriggerManager(); //common public methods - int Create(std::string handle, Vector2 origin, BoundingBox bounds); + int Create(std::string handle); void Unload(int uid); void UnloadAll(); diff --git a/server/triggers/trigger_manager_api.cpp b/server/triggers/trigger_manager_api.cpp index f44e3ac..572b7dd 100644 --- a/server/triggers/trigger_manager_api.cpp +++ b/server/triggers/trigger_manager_api.cpp @@ -26,36 +26,40 @@ //TODO: (1) figure out a way to iterate through elements of managers from lua static int create(lua_State* L) { + //DOCS: params: create(triggerMgr, name[, originX, originY[, boundsX, boundsY, boundsW, boundsH]][, script]) + + //get the trigger manager TriggerManager* mgr = static_cast(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)); + TriggerData* triggerData = mgr->Get(index); + + //origin + if (lua_gettop(L) >= 4) { + triggerData->SetOrigin({lua_tonumber(L, 3), lua_tonumber(L, 4)}); //vectorX, vectorY } - //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 + //bounds + if (lua_gettop(L) >= 8) { + triggerData->SetBoundingBox({ + lua_tointeger(L, 5), //boundsX + lua_tointeger(L, 6), //boundsY + lua_tointeger(L, 7), //boundsW + lua_tointeger(L, 8) //boundsH }); + } + + //if the parameter list isn't capped with a script, append a nil instead + if (lua_type(L, -1) != LUA_TFUNCTION) { + lua_pushnil(L); + } + + //set the script reference (may be nil) + triggerData->SetScriptReference(luaL_ref(L, LUA_REGISTRYINDEX)); //push to the scipts - lua_pushlightuserdata(L, static_cast(mgr->Get(index)) ); + lua_pushlightuserdata(L, static_cast(triggerData)); lua_pushinteger(L, index); return 2;