diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index b711273..ebceded 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -7,9 +7,19 @@ roomAPI = require("room") mapMaker = require("map_maker") mapSaver = require("map_saver") +characterAPI = require("character") +entityAPI = require("entity") +networkAPI = require("network") + --test the room hooks roomManagerAPI.SetOnCreate(function(room, index) print("", "Creating room: ", roomAPI.GetName(room), index) + + roomAPI.SetOnTick(room, function(room) + roomAPI.ForEachCharacter(room, function(character) + characterAPI.SetRoomIndex(character, 0) + end) + end) end) roomManagerAPI.SetOnUnload(function(room, index) @@ -40,10 +50,6 @@ function createTrigger(handle, room, x, y, script) ) end -characterAPI = require("character") -entityAPI = require("entity") -networkAPI = require("network") - --simple teleporter createTrigger("trigger 1", overworld, 0, 0, function(entity) if entityAPI.GetType(entity) ~= "character" then diff --git a/server/characters/character_api.cpp b/server/characters/character_api.cpp index c6a0414..bb35147 100644 --- a/server/characters/character_api.cpp +++ b/server/characters/character_api.cpp @@ -25,6 +25,18 @@ #include "entity_api.hpp" +#include "room_manager.hpp" + +static int setRoomIndex(lua_State* L) { + //NOTE: type-dependant calls to various API functions, see bug #43 + CharacterData* character = static_cast(lua_touserdata(L, 1)); + RoomManager::GetSingleton().PopCharacter(character); + character->SetRoomIndex(lua_tointeger(L, 2)); + RoomManager::GetSingleton().PushCharacter(character); + //TODO: (0) send character room change messages + return 0; +} + static int getOwner(lua_State* L) { CharacterData* character = static_cast(lua_touserdata(L, 1)); lua_pushinteger(L, character->GetOwner()); @@ -45,33 +57,34 @@ static int getAvatar(lua_State* L) { static const luaL_Reg characterLib[] = { // {"GetOwner", getOwner}, //unusable without account API + {"SetRoomIndex", setRoomIndex}, {"GetHandle", getHandle}, {"GetAvatar", getAvatar}, {nullptr, nullptr} }; LUAMOD_API int openCharacterAPI(lua_State* L) { - //the local table - luaL_newlib(L, characterLib); - //get the parent table luaL_requiref(L, TORTUGA_ENTITY_API, openEntityAPI, false); - //clone the parent table into the local table + //the local table + luaL_newlib(L, characterLib); + + //merge the local table into the parent table lua_pushnil(L); //first key while(lua_next(L, -2)) { //copy the key-value pair lua_pushvalue(L, -2); lua_pushvalue(L, -2); - //push the copy to the local table + //push the copy to the parent table lua_settable(L, -6); //pop the original value before continuing lua_pop(L, 1); } - //remove the parent table, leaving the expanded child table + //remove the local table, leaving the expanded parent table lua_pop(L, 1); return 1; diff --git a/server/characters/makefile b/server/characters/makefile index 7e666d5..998ca93 100644 --- a/server/characters/makefile +++ b/server/characters/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../entities ../server_utilities ../../common/gameplay ../../common/utilities +INCLUDES+=. ../entities ../monsters ../rooms ../server_utilities ../triggers ../../common/gameplay ../../common/map ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/entities/entity_api.cpp b/server/entities/entity_api.cpp index 30daaea..13bda83 100644 --- a/server/entities/entity_api.cpp +++ b/server/entities/entity_api.cpp @@ -24,7 +24,6 @@ #include "entity.hpp" static int setRoomIndex(lua_State* L) { - //TODO: (1) if this is a character, push/pop from the room system Entity* entity = static_cast(lua_touserdata(L, 1)); entity->SetRoomIndex(lua_tointeger(L, 2)); return 0; diff --git a/server/monsters/monster_api.cpp b/server/monsters/monster_api.cpp index 06d6551..915b0c3 100644 --- a/server/monsters/monster_api.cpp +++ b/server/monsters/monster_api.cpp @@ -61,27 +61,27 @@ static const luaL_Reg monsterLib[] = { }; LUAMOD_API int openMonsterAPI(lua_State* L) { - //the local table - luaL_newlib(L, monsterLib); - //get the parent table luaL_requiref(L, TORTUGA_ENTITY_API, openEntityAPI, false); - //clone the parent table into the local table + //the local table + luaL_newlib(L, monsterLib); + + //merge the local table into the parent table lua_pushnil(L); //first key while(lua_next(L, -2)) { //copy the key-value pair lua_pushvalue(L, -2); lua_pushvalue(L, -2); - //push the copy to the local table + //push the copy to the parent table lua_settable(L, -6); //pop the original value before continuing lua_pop(L, 1); } - //remove the parent table, leaving the expanded child table + //remove the local table, leaving the expanded parent table lua_pop(L, 1); return 1;