From 35e7d0cf61f287f278c5f895203fba23ef6cb7e8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Mar 2015 04:09:07 +1100 Subject: [PATCH] Tweaked scripts, added ForEachMonster() (cherry picked from commit 0cf6a3dceb56a85969e8248e19f9143364b75c68) --- rsc/scripts/setup_server.lua | 5 +++++ rsc/scripts/setup_server.sql | 43 ++++++++++++++++++++++++------------ server/rooms/room_api.cpp | 21 ++++++++++++++++-- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 2f22c67..a815015 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -41,6 +41,10 @@ roomManagerAPI.SetOnCreate(function(room, index) roomAPI.ForEachCharacter(room, function(character) -- end) + + roomAPI.ForEachMonster(room, function(monster) + -- + end) end) end) @@ -51,6 +55,7 @@ end) --NOTE: room 0 is the first that the client asks for, therefore it must exist local overworld, uidOne = roomManagerAPI.CreateRoom("overworld", "overworld.bmp") roomAPI.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save) + local underworld, uidTwo = roomManagerAPI.CreateRoom("underworld", "overworld.bmp") roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrassland, mapSaver.Save) diff --git a/rsc/scripts/setup_server.sql b/rsc/scripts/setup_server.sql index 809f736..1ef5983 100644 --- a/rsc/scripts/setup_server.sql +++ b/rsc/scripts/setup_server.sql @@ -20,8 +20,6 @@ * distribution. */ ---TODO: (3) An archive table of all dead characters - CREATE TABLE IF NOT EXISTS UserAccounts ( uid INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(100) UNIQUE, --TODO: (3) Swap username for email address @@ -54,27 +52,44 @@ CREATE TABLE IF NOT EXISTS LiveCharacters ( boundsY INTEGER DEFAULT 0, boundsW INTEGER DEFAULT 0, boundsH INTEGER DEFAULT 0 - - --TODO: statistics --- baseStats INTEGER REFERENCES StatisticSets(uid) - - --TODO: equipment --- weapon INTEGER REFERENCES WornEquipment(uid) --- helmet INTEGER REFERENCES WornEquipment(uid) --- armour INTEGER REFERENCES WornEquipment(uid) - --etc. ); CREATE TABLE IF NOT EXISTS DeadCharacters ( - uid INTEGER PRIMARY KEY AUTOINCREMENT + uid INTEGER PRIMARY KEY, + + --metadata + owner INTEGER REFERENCES Accounts(uid), + handle varchar(100), + avatar varchar(100), + birth timestamp NOT NULL ); CREATE TABLE IF NOT EXISTS LiveMonsters ( - uid INTEGER PRIMARY KEY AUTOINCREMENT + uid INTEGER PRIMARY KEY AUTOINCREMENT, + + --metadata + handle varchar(100) UNIQUE, + avatar varchar(100), + + --actions +-- script + + --physically exists in the world + roomIndex INTEGER DEFAULT 0, + originX INTEGER DEFAULT 0, + originY INTEGER DEFAULT 0, + boundsX INTEGER DEFAULT 0, + boundsY INTEGER DEFAULT 0, + boundsW INTEGER DEFAULT 0, + boundsH INTEGER DEFAULT 0 ); CREATE TABLE IF NOT EXISTS DeadMonsters ( - uid INTEGER PRIMARY KEY AUTOINCREMENT + uid INTEGER PRIMARY KEY, + + --metadata + handle varchar(100) UNIQUE, + avatar varchar(100) ); ------------------------- diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index a04b558..438675f 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -68,8 +68,6 @@ static int getTriggerMgr(lua_State* L) { return 1; } -//TODO: character list - static int forEachCharacter(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); //pass each character to the given function @@ -87,6 +85,24 @@ static int forEachCharacter(lua_State* L) { return 0; } +static int forEachMonster(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + MonsterManager* monsterMgr = room->GetMonsterMgr(); + //pass each monster to the given function + for (auto& it : *monsterMgr->GetContainer()) { + lua_pushvalue(L, -1); + lua_pushlightuserdata(L, static_cast(&it.second)); + //call each iteration, throwing an exception if something happened + if (lua_pcall(L, 1, 0, 0) != LUA_OK) { + std::ostringstream os; + os << "Lua error: "; + os << lua_tostring(L, -1); + throw(std::runtime_error(os.str())); + } + } + return 0; +} + static int setOnTick(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); luaL_unref(L, LUA_REGISTRYINDEX, room->GetTickReference()); @@ -124,6 +140,7 @@ static const luaL_Reg roomLib[] = { {"GetTriggerMgr",getTriggerMgr}, {"ForEachCharacter", forEachCharacter}, + {"ForEachMonster", forEachMonster}, {"SetOnTick", setOnTick}, {"GetOnTick", getOnTick},