From 8903b1e28d134c0b2425249b535a028620610b67 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 24 Mar 2015 02:01:14 +1100 Subject: [PATCH 1/6] Began marking changes for monsters These changes include storing monsters on shutdown, storing dead characters and monsters, etc. Also fleshed out the MonsterManager's internals a bit. (cherry picked from commit af982710f6de050c09ef503cf2f8e1c9fdd34979) --- rsc/scripts/setup_server.sql | 30 +++++++++++++------ server/accounts/account_manager.cpp | 10 +++---- server/characters/character_manager.cpp | 10 +++---- server/monsters/monster_api.cpp | 2 +- server/monsters/monster_manager.cpp | 38 +++++++++++++++++-------- server/network_api.cpp | 5 ++++ 6 files changed, 63 insertions(+), 32 deletions(-) diff --git a/rsc/scripts/setup_server.sql b/rsc/scripts/setup_server.sql index e8f1ec3..d672072 100644 --- a/rsc/scripts/setup_server.sql +++ b/rsc/scripts/setup_server.sql @@ -1,6 +1,6 @@ --TODO: (3) An archive table of all dead characters -CREATE TABLE IF NOT EXISTS Accounts ( +CREATE TABLE IF NOT EXISTS UserAccounts ( uid INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(100) UNIQUE, --TODO: (3) Swap username for email address @@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS Accounts ( admin BIT DEFAULT 0 ); -CREATE TABLE IF NOT EXISTS Characters ( +CREATE TABLE IF NOT EXISTS LiveCharacters ( uid INTEGER PRIMARY KEY AUTOINCREMENT, --metadata @@ -31,18 +31,30 @@ CREATE TABLE IF NOT EXISTS Characters ( boundsX INTEGER DEFAULT 0, boundsY INTEGER DEFAULT 0, boundsW INTEGER DEFAULT 0, - boundsH INTEGER DEFAULT 0, + boundsH INTEGER DEFAULT 0 - --statistics - baseStats INTEGER REFERENCES StatisticSets(uid), + --TODO: statistics +-- baseStats INTEGER REFERENCES StatisticSets(uid) - --equipment - weapon INTEGER REFERENCES WornEquipment(uid), - helmet INTEGER REFERENCES WornEquipment(uid), - armour INTEGER REFERENCES WornEquipment(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 +); + +CREATE TABLE IF NOT EXISTS LiveMonsters ( + uid INTEGER PRIMARY KEY AUTOINCREMENT +); + +CREATE TABLE IF NOT EXISTS DeadMonsters ( + uid INTEGER PRIMARY KEY AUTOINCREMENT +); + ------------------------- --Utility tables ------------------------- diff --git a/server/accounts/account_manager.cpp b/server/accounts/account_manager.cpp index 328ca48..bf67d41 100644 --- a/server/accounts/account_manager.cpp +++ b/server/accounts/account_manager.cpp @@ -27,7 +27,7 @@ //Define the queries //------------------------- -static const char* CREATE_USER_ACCOUNT = "INSERT INTO Accounts (username) VALUES (?);"; +static const char* CREATE_USER_ACCOUNT = "INSERT INTO UserAccounts (username) VALUES (?);"; static const char* LOAD_USER_ACCOUNT = "SELECT " "uid, " @@ -35,18 +35,18 @@ static const char* LOAD_USER_ACCOUNT = "SELECT " "whitelisted, " "mod, " "admin " - " FROM Accounts WHERE username = ?;"; + " FROM UserAccounts WHERE username = ?;"; -static const char* SAVE_USER_ACCOUNT = "UPDATE OR FAIL Accounts SET " +static const char* SAVE_USER_ACCOUNT = "UPDATE OR FAIL UserAccounts SET " "blacklisted = ?2, " "whitelisted = ?3, " "mod = ?4, " "admin = ?5 " "WHERE uid = ?1;"; -static const char* DELETE_USER_ACCOUNT = "DELETE FROM Accounts WHERE uid = ?;"; +static const char* DELETE_USER_ACCOUNT = "DELETE FROM UserAccounts WHERE uid = ?;"; -static const char* COUNT_USER_ACCOUNT_RECORDS = "SELECT COUNT(*) FROM Accounts;"; +static const char* COUNT_USER_ACCOUNT_RECORDS = "SELECT COUNT(*) FROM UserAccounts;"; //------------------------- //Define the public methods diff --git a/server/characters/character_manager.cpp b/server/characters/character_manager.cpp index a094f51..2f9d150 100644 --- a/server/characters/character_manager.cpp +++ b/server/characters/character_manager.cpp @@ -35,7 +35,7 @@ //NOTE: Programmer set variables are NOT zero-indexed //NOTE: SQLite3 returned variables (i.e. loading) ARE zero-indexed -static const char* CREATE_CHARACTER = "INSERT INTO Characters (" +static const char* CREATE_CHARACTER = "INSERT INTO LiveCharacters (" "owner, " "handle, " "avatar, " @@ -57,9 +57,9 @@ static const char* LOAD_CHARACTER = "SELECT " "boundsY, " "boundsW, " "boundsH " - "FROM Characters WHERE handle = ?;"; + "FROM LiveCharacters WHERE handle = ?;"; -static const char* SAVE_CHARACTER = "UPDATE OR FAIL Characters SET " +static const char* SAVE_CHARACTER = "UPDATE OR FAIL LiveCharacters SET " "roomIndex = ?2, " "originX = ?3, " "originY = ?4, " @@ -69,9 +69,9 @@ static const char* SAVE_CHARACTER = "UPDATE OR FAIL Characters SET " "boundsH = ?8 " "WHERE uid = ?1;"; -static const char* DELETE_CHARACTER = "DELETE FROM Characters WHERE uid = ?;"; +static const char* DELETE_CHARACTER = "DELETE FROM LiveCharacters WHERE uid = ?;"; -static const char* COUNT_CHARACTER_RECORDS = "SELECT COUNT(*) FROM Characters;"; +static const char* COUNT_CHARACTER_RECORDS = "SELECT COUNT(*) FROM LiveCharacters;"; //------------------------- //Define the methods diff --git a/server/monsters/monster_api.cpp b/server/monsters/monster_api.cpp index 915b0c3..c5c4d93 100644 --- a/server/monsters/monster_api.cpp +++ b/server/monsters/monster_api.cpp @@ -28,7 +28,7 @@ static int setAvatar(lua_State* L) { MonsterData* monster = static_cast(lua_touserdata(L, 1)); monster->SetAvatar(lua_tostring(L, 2)); - //TODO: send an update to the clients? + //TODO: (1) send an update to the clients? return 0; } diff --git a/server/monsters/monster_manager.cpp b/server/monsters/monster_manager.cpp index cd54bc1..a8cf51c 100644 --- a/server/monsters/monster_manager.cpp +++ b/server/monsters/monster_manager.cpp @@ -29,46 +29,60 @@ MonsterManager::~MonsterManager() { UnloadAll(); } -int MonsterManager::Create(std::string) { - //TODO: (9) MonsterManager::Create() +int MonsterManager::Create(std::string s) { + //TODO: (1) MonsterManager::Create() } void MonsterManager::Unload(int uid) { - //TODO: (9) MonsterManager::Unload() + elementMap.erase(uid); } void MonsterManager::UnloadAll() { - //TODO: (9) MonsterManager::UnloadAll() + elementMap.clear(); } void MonsterManager::UnloadIf(std::function)> fn) { - //TODO: (9) MonsterManager::UnloadIf() + std::map::iterator it = elementMap.begin(); + while (it != elementMap.end()) { + if (fn(*it)) { + it = elementMap.erase(it); + } + else { + ++it; + } + } } MonsterData* MonsterManager::Get(int uid) { - //TODO: (9) MonsterManager::Get() + std::map::iterator it = elementMap.find(uid); + + if (it == elementMap.end()) { + return nullptr; + } + + return &it->second; } int MonsterManager::GetLoadedCount() { - //TODO: (9) MonsterManager::GetLoadedCount() + return elementMap.size(); } std::map* MonsterManager::GetContainer() { - //TODO: (9) MonsterManager::GetContainer() + return &elementMap; } lua_State* MonsterManager::SetLuaState(lua_State* L) { - //TODO: (9) MonsterManager::SetLuaState() + return lua = L; } lua_State* MonsterManager::GetLuaState() { - //TODO: (9) MonsterManager::GetLuaState() + return lua; } sqlite3* MonsterManager::SetDatabase(sqlite3* db) { - //TODO: (9) MonsterManager::SetDatabase() + return database = db; } sqlite3* MonsterManager::GetDatabase() { - //TODO: (9) MonsterManager::GetDatabase() + return database; } diff --git a/server/network_api.cpp b/server/network_api.cpp index 9975cd0..83d2d80 100644 --- a/server/network_api.cpp +++ b/server/network_api.cpp @@ -62,8 +62,13 @@ static int pumpCharacterUpdate(lua_State* L) { return 1; } +static int pumpMonsterUpdate(lua_State* L) { + //TODO: (0) send the info about a specific monster instance +} + static const luaL_Reg networkLib[] = { {"PumpCharacterUpdate", pumpCharacterUpdate}, + {"PumpMonsterUpdate", pumpMonsterUpdate}, {nullptr, nullptr} }; From 27fd810cfd61d00b583e78414a112b563f997994 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 24 Mar 2015 02:28:26 +1100 Subject: [PATCH 2/6] Added a simple database update script, and comment headers I don't know why it took me this long to add those headers in the first place. (cherry picked from commit 3c28b6c3bf75c4bdb42e5ea9d9864efaa855620c) --- rsc/scripts/door_utility.lua | 24 ++++++++++++++++ rsc/scripts/map_maker.lua | 24 ++++++++++++++++ rsc/scripts/map_saver.lua | 24 ++++++++++++++++ rsc/scripts/setup_server.lua | 24 ++++++++++++++++ rsc/scripts/setup_server.sql | 22 +++++++++++++++ rsc/scripts/updates.sql | 54 ++++++++++++++++++++++++++++++++++++ 6 files changed, 172 insertions(+) create mode 100644 rsc/scripts/updates.sql diff --git a/rsc/scripts/door_utility.lua b/rsc/scripts/door_utility.lua index d1fc648..8c9447e 100644 --- a/rsc/scripts/door_utility.lua +++ b/rsc/scripts/door_utility.lua @@ -1,3 +1,27 @@ +--[[ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +--]] + local doorUtility = {} roomAPI = require("room") diff --git a/rsc/scripts/map_maker.lua b/rsc/scripts/map_maker.lua index d2a7f4e..69ed519 100644 --- a/rsc/scripts/map_maker.lua +++ b/rsc/scripts/map_maker.lua @@ -1,3 +1,27 @@ +--[[ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +--]] + local regionAPI = require("region") local mapMaker = {} diff --git a/rsc/scripts/map_saver.lua b/rsc/scripts/map_saver.lua index 2ec8832..f2f4ea0 100644 --- a/rsc/scripts/map_saver.lua +++ b/rsc/scripts/map_saver.lua @@ -1,3 +1,27 @@ +--[[ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +--]] + local region = require("region") local mapSaver = {} diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 5c55900..2f22c67 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1,3 +1,27 @@ +--[[ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +--]] + print("Lua script check") --requirements diff --git a/rsc/scripts/setup_server.sql b/rsc/scripts/setup_server.sql index d672072..809f736 100644 --- a/rsc/scripts/setup_server.sql +++ b/rsc/scripts/setup_server.sql @@ -1,3 +1,25 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ + --TODO: (3) An archive table of all dead characters CREATE TABLE IF NOT EXISTS UserAccounts ( diff --git a/rsc/scripts/updates.sql b/rsc/scripts/updates.sql new file mode 100644 index 0000000..2f0f817 --- /dev/null +++ b/rsc/scripts/updates.sql @@ -0,0 +1,54 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ + +--TODO: might need a version number for database structures + +--An example of a database update script. +--This is only rough right now, until I get some more eyes onto it + +--moving old to new +CREATE TABLE tempCharacters ( + uid INTEGER PRIMARY KEY AUTOINCREMENT, + + --metadata + owner INTEGER REFERENCES Accounts(uid), + handle varchar(100) UNIQUE, + avatar varchar(100), + birth timestamp NOT NULL DEFAULT (datetime()), + + --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 +); + +INSERT INTO tempCharacters (uid, owner, handle, avatar, birth, roomIndex, originX, originY, boundsX, boundsY, boundsW, boundsH) + SELECT uid, owner, handle, avatar, birth, roomIndex, originX, originY, boundsX, boundsY, boundsW, boundsH + FROM LiveCharacters; + +DROP TABLE LiveCharacters; + +ALTER TABLE tempCharacters RENAME TO LiveCharacters; From 35e7d0cf61f287f278c5f895203fba23ef6cb7e8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Mar 2015 04:09:07 +1100 Subject: [PATCH 3/6] 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}, From 07faf1b96bb5436913d27fc8364919e7dccb481e Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 8 May 2015 22:33:46 +1000 Subject: [PATCH 4/6] Moved a file --- server/{ => server_utilities}/network_api.cpp | 0 server/{ => server_utilities}/network_api.hpp | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename server/{ => server_utilities}/network_api.cpp (100%) rename server/{ => server_utilities}/network_api.hpp (100%) diff --git a/server/network_api.cpp b/server/server_utilities/network_api.cpp similarity index 100% rename from server/network_api.cpp rename to server/server_utilities/network_api.cpp diff --git a/server/network_api.hpp b/server/server_utilities/network_api.hpp similarity index 100% rename from server/network_api.hpp rename to server/server_utilities/network_api.hpp From 102ba18b7b11c349a6d4c466102cc9c146e9ff54 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 8 May 2015 23:17:35 +1000 Subject: [PATCH 5/6] Finally wrote the monster manager API --- rsc/scripts/setup_server.lua | 15 +++++++- server/entities/entity.hpp | 2 +- server/monsters/monster_data.cpp | 15 +++++++- server/monsters/monster_data.hpp | 13 ++++++- server/monsters/monster_manager.cpp | 10 ++++-- server/monsters/monster_manager.hpp | 3 +- server/monsters/monster_manager_api.cpp | 47 +++++++++++++++++++++++++ 7 files changed, 98 insertions(+), 7 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index a815015..afe6690 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -27,6 +27,8 @@ print("Lua script check") --requirements roomManagerAPI = require("room_manager") roomAPI = require("room") +monsterManagerAPI = require("monster_manager") +monsterAPI = require("monster") mapMaker = require("map_maker") mapSaver = require("map_saver") @@ -43,7 +45,7 @@ roomManagerAPI.SetOnCreate(function(room, index) end) roomAPI.ForEachMonster(room, function(monster) - -- + --TODO: move ForEachMonster to the monster manager API end) end) end) @@ -62,4 +64,15 @@ roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrass --call the monstrosity doorUtility.createDoorPair("pair 1", overworld, -64, -64, underworld, 64, 64) +--testing the monster creation +print("testing monsters") +local monsterMgr = roomAPI.GetMonsterMgr(overworld) + +local monster, mIndex = monsterManagerAPI.Create(monsterMgr, "skume.bmp", 0) + +print("Monster count: ", monsterManagerAPI.GetLoadedCount(monsterMgr)) +print("Monster avatar: ", monsterAPI.GetAvatar(monster), mIndex) + +monsterManagerAPI.UnloadAll(monsterMgr) + print("Finished the lua script") diff --git a/server/entities/entity.hpp b/server/entities/entity.hpp index 623a912..e7e9250 100644 --- a/server/entities/entity.hpp +++ b/server/entities/entity.hpp @@ -46,7 +46,7 @@ public: const char* GetType() const; protected: - Entity(const char*); + Entity(const char* type); virtual ~Entity() = default; int roomIndex = -1; diff --git a/server/monsters/monster_data.cpp b/server/monsters/monster_data.cpp index 8a29f12..d2d6abf 100644 --- a/server/monsters/monster_data.cpp +++ b/server/monsters/monster_data.cpp @@ -21,10 +21,23 @@ */ #include "monster_data.hpp" -MonsterData::MonsterData(): Entity("monster") { +MonsterData::MonsterData(std::string _avatar, int _scriptRef): + Entity("monster"), + avatar(_avatar), + scriptRef(_scriptRef) +{ //EMPTY } +void MonsterData::Update() { + Entity::Update(); + //TODO: (0) call the script reference +} + +//------------------------- +//accessors & mutators +//------------------------- + std::string MonsterData::SetAvatar(std::string s) { return avatar = s; } diff --git a/server/monsters/monster_data.hpp b/server/monsters/monster_data.hpp index 44a5300..45fb3dd 100644 --- a/server/monsters/monster_data.hpp +++ b/server/monsters/monster_data.hpp @@ -28,11 +28,22 @@ #include +/* DOCS: Monster attributes, read more + * species (avatar, script) + * level + * health/mana + * permadeath/respawn +*/ + class MonsterData: public Entity { public: - MonsterData(); + MonsterData(std::string avatar, int scriptRef); ~MonsterData() = default; + virtual void Update(); + + //accessors & mutators + std::string SetAvatar(std::string); std::string GetAvatar(); diff --git a/server/monsters/monster_manager.cpp b/server/monsters/monster_manager.cpp index a8cf51c..7f9a11a 100644 --- a/server/monsters/monster_manager.cpp +++ b/server/monsters/monster_manager.cpp @@ -29,10 +29,16 @@ MonsterManager::~MonsterManager() { UnloadAll(); } -int MonsterManager::Create(std::string s) { - //TODO: (1) MonsterManager::Create() +int MonsterManager::Create(std::string avatar, int scriptRef) { + //implicitly create the new + elementMap.emplace(counter, MonsterData(avatar, scriptRef)); + + //TODO: do various things like saving to the database + return counter++; } +//TODO: (1) monster load, save + void MonsterManager::Unload(int uid) { elementMap.erase(uid); } diff --git a/server/monsters/monster_manager.hpp b/server/monsters/monster_manager.hpp index 5d74e4c..7ab1dfd 100644 --- a/server/monsters/monster_manager.hpp +++ b/server/monsters/monster_manager.hpp @@ -37,7 +37,7 @@ public: ~MonsterManager(); //common public methods - int Create(std::string); + int Create(std::string avatar, int scriptRef); void Unload(int uid); void UnloadAll(); @@ -57,6 +57,7 @@ public: private: //members std::map elementMap; + int counter = 0; lua_State* lua = nullptr; sqlite3* database = nullptr; }; diff --git a/server/monsters/monster_manager_api.cpp b/server/monsters/monster_manager_api.cpp index 6fc6a81..08be549 100644 --- a/server/monsters/monster_manager_api.cpp +++ b/server/monsters/monster_manager_api.cpp @@ -23,7 +23,54 @@ #include "monster_manager.hpp" +static int create(lua_State* L) { + MonsterManager* mgr = static_cast(lua_touserdata(L, 1)); + int index = mgr->Create(lua_tostring(L, 2), lua_tointeger(L, 3)); + MonsterData* monster = mgr->Get(index); + lua_pushlightuserdata(L, static_cast(monster)); + lua_pushinteger(L, index); + return 2; +} + +//TOOD: this needs to take the userdata as a parameter too +static int unload(lua_State* L) { + MonsterManager* mgr = static_cast(lua_touserdata(L, 1)); + mgr->Unload(lua_tointeger(L, 2)); + return 0; +} + +static int unloadAll(lua_State* L) { + MonsterManager* mgr = static_cast(lua_touserdata(L, 1)); + mgr->UnloadAll(); + return 0; +} + +static int unloadIf(lua_State* L) { + MonsterManager* mgr = static_cast(lua_touserdata(L, 1)); + //TODO: unloadIf + return 0; +} + +static int get(lua_State* L) { + MonsterManager* mgr = static_cast(lua_touserdata(L, 1)); + MonsterData* monster = mgr->Get(lua_tointeger(L, 2)); + lua_pushlightuserdata(L, static_cast(monster)); + return 1; +} + +static int getLoadedCount(lua_State* L) { + MonsterManager* mgr = static_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, mgr->GetLoadedCount()); + return 1; +} + static const luaL_Reg monsterManagerLib[] = { + {"Create", create}, + {"Unload", unload}, + {"UnloadAll", unloadAll}, +// {"UnloadIf", unloadIf}, + {"Get", get}, + {"GetLoadedCount", getLoadedCount}, {nullptr, nullptr} }; From 210bccbe0d9b4deea8c3ea8a035e8d44a5e98b2a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 7 Jul 2015 12:41:57 +1000 Subject: [PATCH 6/6] Pruned the usage of the incomplete monster system --- rsc/scripts/door_utility.lua | 2 ++ rsc/scripts/map_maker.lua | 2 ++ rsc/scripts/setup_server.lua | 21 ++---------- rsc/scripts/updates.sql | 54 ------------------------------- server/linit.cpp | 8 ++--- server/makefile | 4 +-- server/rooms/room_api.cpp | 8 +++-- server/rooms/room_data.cpp | 6 ---- server/rooms/room_data.hpp | 4 +-- server/server_application.hpp | 4 --- server/server_logic.cpp | 5 --- server/server_monster_methods.cpp | 26 --------------- 12 files changed, 19 insertions(+), 125 deletions(-) delete mode 100644 rsc/scripts/updates.sql delete mode 100644 server/server_monster_methods.cpp diff --git a/rsc/scripts/door_utility.lua b/rsc/scripts/door_utility.lua index 8c9447e..49f272d 100644 --- a/rsc/scripts/door_utility.lua +++ b/rsc/scripts/door_utility.lua @@ -22,6 +22,8 @@ */ --]] +--DOCS: a placeholder door utility + local doorUtility = {} roomAPI = require("room") diff --git a/rsc/scripts/map_maker.lua b/rsc/scripts/map_maker.lua index 69ed519..596570f 100644 --- a/rsc/scripts/map_maker.lua +++ b/rsc/scripts/map_maker.lua @@ -22,6 +22,8 @@ */ --]] +--DOCS: a placeholder map generator + local regionAPI = require("region") local mapMaker = {} diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index afe6690..4cb4972 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -22,13 +22,13 @@ */ --]] +--DOCS: This script is run by the server on startup + print("Lua script check") --requirements roomManagerAPI = require("room_manager") roomAPI = require("room") -monsterManagerAPI = require("monster_manager") -monsterAPI = require("monster") mapMaker = require("map_maker") mapSaver = require("map_saver") @@ -43,10 +43,6 @@ roomManagerAPI.SetOnCreate(function(room, index) roomAPI.ForEachCharacter(room, function(character) -- end) - - roomAPI.ForEachMonster(room, function(monster) - --TODO: move ForEachMonster to the monster manager API - end) end) end) @@ -63,16 +59,3 @@ roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrass --call the monstrosity doorUtility.createDoorPair("pair 1", overworld, -64, -64, underworld, 64, 64) - ---testing the monster creation -print("testing monsters") -local monsterMgr = roomAPI.GetMonsterMgr(overworld) - -local monster, mIndex = monsterManagerAPI.Create(monsterMgr, "skume.bmp", 0) - -print("Monster count: ", monsterManagerAPI.GetLoadedCount(monsterMgr)) -print("Monster avatar: ", monsterAPI.GetAvatar(monster), mIndex) - -monsterManagerAPI.UnloadAll(monsterMgr) - -print("Finished the lua script") diff --git a/rsc/scripts/updates.sql b/rsc/scripts/updates.sql deleted file mode 100644 index 2f0f817..0000000 --- a/rsc/scripts/updates.sql +++ /dev/null @@ -1,54 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2015 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ - ---TODO: might need a version number for database structures - ---An example of a database update script. ---This is only rough right now, until I get some more eyes onto it - ---moving old to new -CREATE TABLE tempCharacters ( - uid INTEGER PRIMARY KEY AUTOINCREMENT, - - --metadata - owner INTEGER REFERENCES Accounts(uid), - handle varchar(100) UNIQUE, - avatar varchar(100), - birth timestamp NOT NULL DEFAULT (datetime()), - - --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 -); - -INSERT INTO tempCharacters (uid, owner, handle, avatar, birth, roomIndex, originX, originY, boundsX, boundsY, boundsW, boundsH) - SELECT uid, owner, handle, avatar, birth, roomIndex, originX, originY, boundsX, boundsY, boundsW, boundsH - FROM LiveCharacters; - -DROP TABLE LiveCharacters; - -ALTER TABLE tempCharacters RENAME TO LiveCharacters; diff --git a/server/linit.cpp b/server/linit.cpp index c0552ea..f1c05bb 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -41,8 +41,8 @@ #include "character_manager_api.hpp" #include "region_api.hpp" #include "region_pager_api.hpp" -#include "monster_api.hpp" -#include "monster_manager_api.hpp" +//#include "monster_api.hpp" +//#include "monster_manager_api.hpp" #include "network_api.hpp" #include "room_api.hpp" #include "room_manager_api.hpp" @@ -70,8 +70,8 @@ static const luaL_Reg preloadedlibs[] = { {TORTUGA_ENTITY_API, openEntityAPI}, //required by derived classes {TORTUGA_CHARACTER_API, openCharacterAPI}, {TORTUGA_CHARACTER_MANAGER_API, openCharacterManagerAPI}, - {TORTUGA_MONSTER_API, openMonsterAPI}, - {TORTUGA_MONSTER_MANAGER_API, openMonsterManagerAPI}, +// {TORTUGA_MONSTER_API, openMonsterAPI}, +// {TORTUGA_MONSTER_MANAGER_API, openMonsterManagerAPI}, {TORTUGA_NETWORK_API, openNetworkAPI}, {TORTUGA_REGION_API, openRegionAPI}, {TORTUGA_REGION_PAGER_API, openRegionPagerAPI}, diff --git a/server/makefile b/server/makefile index cd4b993..2f8de96 100644 --- a/server/makefile +++ b/server/makefile @@ -1,5 +1,5 @@ #include directories -INCLUDES+=SDL . accounts characters clients entities monsters rooms server_utilities triggers ../common/debugging ../common/gameplay ../common/map ../common/network ../common/network/packet_types ../common/utilities +INCLUDES+=SDL . accounts characters clients entities rooms server_utilities triggers ../common/debugging ../common/gameplay ../common/map ../common/network ../common/network/packet_types ../common/utilities #libraries #the order of the $(LIBS) is important, at least for MinGW @@ -33,7 +33,7 @@ all: $(OBJ) $(OUT) $(MAKE) -C characters $(MAKE) -C clients $(MAKE) -C entities - $(MAKE) -C monsters +# $(MAKE) -C monsters $(MAKE) -C rooms $(MAKE) -C server_utilities $(MAKE) -C triggers diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 438675f..9aad030 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -56,11 +56,13 @@ static int getPager(lua_State* L) { return 1; } +/* static int getMonsterMgr(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); lua_pushlightuserdata(L, reinterpret_cast(room->GetMonsterMgr()) ); return 1; } +*/ static int getTriggerMgr(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); @@ -85,6 +87,7 @@ 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(); @@ -102,6 +105,7 @@ static int forEachMonster(lua_State* L) { } return 0; } +*/ static int setOnTick(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); @@ -136,11 +140,11 @@ static const luaL_Reg roomLib[] = { {"GetTileset", getTilesetName}, {"GetPager",getPager}, - {"GetMonsterMgr",getMonsterMgr}, +// {"GetMonsterMgr",getMonsterMgr}, {"GetTriggerMgr",getTriggerMgr}, {"ForEachCharacter", forEachCharacter}, - {"ForEachMonster", forEachMonster}, +// {"ForEachMonster", forEachMonster}, {"SetOnTick", setOnTick}, {"GetOnTick", getOnTick}, diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index dcb544c..cb6e8e0 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -122,10 +122,6 @@ RegionPagerLua* RoomData::GetPager() { return &pager; } -MonsterManager* RoomData::GetMonsterMgr() { - return &monsterMgr; -} - TriggerManager* RoomData::GetTriggerMgr() { return &triggerMgr; } @@ -137,7 +133,6 @@ std::list* RoomData::GetCharacterList() { lua_State* RoomData::SetLuaState(lua_State* L) { lua = L; pager.SetLuaState(lua); - monsterMgr.SetLuaState(lua); triggerMgr.SetLuaState(lua); return lua; } @@ -148,7 +143,6 @@ lua_State* RoomData::GetLuaState() { sqlite3* RoomData::SetDatabase(sqlite3* db) { database = db; - monsterMgr.SetDatabase(database); return database; } diff --git a/server/rooms/room_data.hpp b/server/rooms/room_data.hpp index dabe81d..ac4331f 100644 --- a/server/rooms/room_data.hpp +++ b/server/rooms/room_data.hpp @@ -23,11 +23,11 @@ #define ROOMDATA_HPP_ #include "character_data.hpp" -#include "monster_manager.hpp" #include "region_pager_lua.hpp" #include "trigger_manager.hpp" #include "lua.hpp" +#include "sqlite3.h" #include #include @@ -47,7 +47,6 @@ public: std::string GetTileset(); RegionPagerLua* GetPager(); - MonsterManager* GetMonsterMgr(); TriggerManager* GetTriggerMgr(); std::list* GetCharacterList(); @@ -69,7 +68,6 @@ private: //members RegionPagerLua pager; - MonsterManager monsterMgr; TriggerManager triggerMgr; std::list characterList; diff --git a/server/server_application.hpp b/server/server_application.hpp index 3ba2b39..b3effbf 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -26,7 +26,6 @@ #include "account_manager.hpp" #include "character_manager.hpp" #include "client_manager.hpp" -#include "monster_manager.hpp" #include "room_manager.hpp" //utilities @@ -105,9 +104,6 @@ private: void hCharacterAttack(CharacterPacket* const); void hCharacterDamage(CharacterPacket* const); - //character management - void hMonsterDamage(MonsterPacket* const); - //chat void hTextBroadcast(TextPacket* const); void hTextSpeech(TextPacket* const); diff --git a/server/server_logic.cpp b/server/server_logic.cpp index d0a4283..ed31f6f 100644 --- a/server/server_logic.cpp +++ b/server/server_logic.cpp @@ -327,11 +327,6 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) { hCharacterDamage(static_cast(argPacket)); break; - //monster management - case SerialPacketType::MONSTER_DAMAGE: - hMonsterDamage(static_cast(argPacket)); - break; - //chat case SerialPacketType::TEXT_BROADCAST: hTextBroadcast(static_cast(argPacket)); diff --git a/server/server_monster_methods.cpp b/server/server_monster_methods.cpp deleted file mode 100644 index 662c255..0000000 --- a/server/server_monster_methods.cpp +++ /dev/null @@ -1,26 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2015 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ -#include "server_application.hpp" - -void ServerApplication::hMonsterDamage(MonsterPacket* const argPacket) { - //TODO: (9) ServerApplication::hMonsterDamage() -} \ No newline at end of file