Updated room system and room API, more to come

There's a lot of verbosity in the scripts, so it might be beneficial to
redice that at some point.
This commit is contained in:
Kayne Ruse
2014-12-31 04:52:10 +11:00
parent f9c19a630d
commit 8e50be24d4
8 changed files with 93 additions and 46 deletions
+8 -4
View File
@@ -1,5 +1,6 @@
print("Lua script check") print("Lua script check")
mapSystem = require "map_system"
mapMaker = require "map_maker" mapMaker = require "map_maker"
mapSaver = require "map_saver" mapSaver = require "map_saver"
roomSystem = require "room_system" roomSystem = require "room_system"
@@ -12,10 +13,13 @@ local function dumpTable(t)
end end
end end
dumpTable(waypointSystem)
--NOTE: room 0 is the first that the client asks for, therefore it must exist --NOTE: room 0 is the first that the client asks for, therefore it must exist
local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld") local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp")
roomSystem.Room.Initialize(overworld, "overworld.bmp", mapSaver.Load, mapSaver.Save, mapMaker.debugIsland, mapSaver.Save)
--NOTE: This is horrible; room initialization is important
mapSystem.RegionPager.SetOnLoad(roomSystem.Room.GetPager(overworld), mapSaver.Load)
mapSystem.RegionPager.SetOnSave(roomSystem.Room.GetPager(overworld), mapSaver.Save)
mapSystem.RegionPager.SetOnCreate(roomSystem.Room.GetPager(overworld), mapMaker.debugIsland)
mapSystem.RegionPager.SetOnUnload(roomSystem.Room.GetPager(overworld), mapSaver.Save)
print("Finished the lua script") print("Finished the lua script")
+8 -7
View File
@@ -25,25 +25,25 @@
static int setRoomName(lua_State* L) { static int setRoomName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1)); RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
room->SetRoomName(lua_tostring(L, 2)); room->SetName(lua_tostring(L, 2));
return 0; return 0;
} }
static int getRoomName(lua_State* L) { static int getRoomName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1)); RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
lua_pushstring(L, room->GetRoomName().c_str()); lua_pushstring(L, room->GetName().c_str());
return 1; return 1;
} }
static int setTilesetName(lua_State* L) { static int setTilesetName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1)); RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
room->SetTilesetName(lua_tostring(L, 2)); room->SetTileset(lua_tostring(L, 2));
return 0; return 0;
} }
static int getTilesetName(lua_State* L) { static int getTilesetName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1)); RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
lua_pushstring(L, room->GetTilesetName().c_str()); lua_pushstring(L, room->GetTileset().c_str());
return 1; return 1;
} }
@@ -56,7 +56,8 @@ static int getPager(lua_State* L) {
static int initialize(lua_State* L) { static int initialize(lua_State* L) {
//set the members of the given room //set the members of the given room
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1)); RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
room->SetRoomName(lua_tostring(L, 2)); room->SetName(lua_tostring(L, 2));
room->SetTileset(lua_tostring(L, 3));
//set the refs of these parameters (backwards, since it pops from the top of the stack) //set the refs of these parameters (backwards, since it pops from the top of the stack)
room->GetPager()->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX)); room->GetPager()->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX));
@@ -70,8 +71,8 @@ static int initialize(lua_State* L) {
static const luaL_Reg roomLib[] = { static const luaL_Reg roomLib[] = {
{"GetPager",getPager}, {"GetPager",getPager},
{"SetRoomName", setRoomName}, {"SetName", setRoomName},
{"GetRoomName", getRoomName}, {"GetName", getRoomName},
{"SetTileset", setTilesetName}, {"SetTileset", setTilesetName},
{"GetTileset", getTilesetName}, {"GetTileset", getTilesetName},
{"Initialize", initialize}, {"Initialize", initialize},
+4 -4
View File
@@ -21,19 +21,19 @@
*/ */
#include "room_data.hpp" #include "room_data.hpp"
std::string RoomData::SetRoomName(std::string s) { std::string RoomData::SetName(std::string s) {
return roomName = s; return roomName = s;
} }
std::string RoomData::GetRoomName() { std::string RoomData::GetName() {
return roomName; return roomName;
} }
std::string RoomData::SetTilesetName(std::string s) { std::string RoomData::SetTileset(std::string s) {
return tilesetName = s; return tilesetName = s;
} }
std::string RoomData::GetTilesetName() { std::string RoomData::GetTileset() {
return tilesetName; return tilesetName;
} }
+6 -4
View File
@@ -40,15 +40,17 @@ public:
~RoomData() = default; ~RoomData() = default;
//accessors and mutators //accessors and mutators
std::string SetRoomName(std::string s); std::string SetName(std::string);
std::string GetRoomName(); std::string GetName();
std::string SetTilesetName(std::string s); std::string SetTileset(std::string);
std::string GetTilesetName(); std::string GetTileset();
RegionPagerLua* GetPager(); RegionPagerLua* GetPager();
std::list<Entity*>* GetEntityList(); std::list<Entity*>* GetEntityList();
//TODO: triggers for unload, save, per-second, player enter, player exit, etc.
private: private:
friend class RoomManager; friend class RoomManager;
+20 -16
View File
@@ -29,27 +29,18 @@
//public access methods //public access methods
//------------------------- //-------------------------
int RoomManager::Create(std::string roomName) { int RoomManager::Create(std::string roomName, std::string tileset) {
//create the room //create the room
RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element
newRoom->SetRoomName(roomName); newRoom->SetName(roomName);
newRoom->SetTileset(tileset);
newRoom->pager.SetLuaState(lua); newRoom->pager.SetLuaState(lua);
//finish the routine //finish the routine
return counter++; return counter++;
} }
void RoomManager::Unload(int uid) {
//find the room
std::map<int, RoomData>::iterator it = elementMap.find(uid);
if (it == elementMap.end()) {
return;
}
//free the memory
elementMap.erase(uid);
}
void RoomManager::UnloadAll() { void RoomManager::UnloadAll() {
elementMap.clear(); elementMap.clear();
} }
@@ -76,14 +67,27 @@ RoomData* RoomManager::Get(int uid) {
return &it->second; return &it->second;
} }
int RoomManager::GetLoadedCount() { RoomData* RoomManager::Get(std::string name) {
return elementMap.size(); for (std::map<int, RoomData>::iterator it = elementMap.begin(); it != elementMap.end(); ++it) {
if (it->second.GetName() == name) {
return &it->second;
}
}
return nullptr;
} }
int RoomManager::GetTotalCount() { int RoomManager::GetLoadedCount() {
return elementMap.size(); return elementMap.size();
} }
std::map<int, RoomData>* RoomManager::GetContainer() { std::map<int, RoomData>* RoomManager::GetContainer() {
return &elementMap; return &elementMap;
} }
lua_State* RoomManager::SetLuaState(lua_State* L) {
return lua = L;
}
lua_State* RoomManager::GetLuaState() {
return lua;
}
+4 -5
View File
@@ -37,21 +37,20 @@
class RoomManager: public Singleton<RoomManager> { class RoomManager: public Singleton<RoomManager> {
public: public:
//common public methods //common public methods
int Create(std::string); int Create(std::string name, std::string tileset);
void Unload(int uid);
void UnloadAll(); void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn); void UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn);
//accessors and mutators //accessors and mutators
RoomData* Get(int uid); RoomData* Get(int uid);
RoomData* Get(std::string name);
int GetLoadedCount(); int GetLoadedCount();
int GetTotalCount();
std::map<int, RoomData>* GetContainer(); std::map<int, RoomData>* GetContainer();
//hooks //hooks
lua_State* SetLuaState(lua_State* L) { return lua = L; } lua_State* SetLuaState(lua_State* L);
lua_State* GetLuaState() { return lua; } lua_State* GetLuaState();
private: private:
friend Singleton<RoomManager>; friend Singleton<RoomManager>;
+42 -6
View File
@@ -24,31 +24,67 @@
#include "room_manager.hpp" #include "room_manager.hpp"
int createRoom(lua_State* L) { int createRoom(lua_State* L) {
//create & get the room //create & get the room
RoomManager& roomMgr = RoomManager::GetSingleton(); RoomManager& roomMgr = RoomManager::GetSingleton();
int uid = roomMgr.Create(lua_tostring(L, 1)); int uid = roomMgr.Create(lua_tostring(L, 1), lua_tostring(L, 2));
RoomData* room = roomMgr.Get(uid); RoomData* room = roomMgr.Get(uid);
//TODO: initialization parameters here?
//return room, uid //return room, uid
lua_pushlightuserdata(L, static_cast<void*>(room)); lua_pushlightuserdata(L, static_cast<void*>(room));
lua_pushinteger(L, uid); lua_pushinteger(L, uid); //for debugging, mostly
return 2; return 2;
} }
int unloadRoom(lua_State* L) { int unloadRoom(lua_State* L) {
//TODO: check authorization for room deletion
RoomManager& roomMgr = RoomManager::GetSingleton(); RoomManager& roomMgr = RoomManager::GetSingleton();
roomMgr.Unload(lua_tointeger(L, 1));
switch(lua_type(L, 1)) {
case LUA_TNUMBER: {
//number
int uid = lua_tointeger(L, 1);
roomMgr.UnloadIf([uid](std::pair<int, RoomData> it){
return it.first == uid;
});
}
break;
case LUA_TSTRING: {
//name
std::string name = lua_tostring(L, 1);
roomMgr.UnloadIf([name](std::pair<int, RoomData> it){
return it.second.GetName() == name;
});
}
break;
case LUA_TLIGHTUSERDATA: {
//the room itself
std::string name = static_cast<RoomData*>(lua_touserdata(L, 1))->GetName();
roomMgr.UnloadIf([name](std::pair<int, RoomData> it){
return it.second.GetName() == name;
});
}
break;
}
return 0; return 0;
} }
int getRoom(lua_State* L) { int getRoom(lua_State* L) {
//TODO: integer vs name for getRoom() //TODO: integer vs name for getRoom()
RoomManager& roomMgr = RoomManager::GetSingleton(); RoomManager& roomMgr = RoomManager::GetSingleton();
RoomData* room = nullptr;
RoomData* room = roomMgr.Get(lua_tointeger(L, 1)); switch(lua_type(L, 1)) {
case LUA_TNUMBER:
//number
room = roomMgr.Get(lua_tointeger(L, 1));
break;
case LUA_TSTRING:
//name
room = roomMgr.Get(lua_tostring(L, 1));
break;
}
if (room) { if (room) {
lua_pushlightuserdata(L, static_cast<void*>(room)); lua_pushlightuserdata(L, static_cast<void*>(room));
+1
View File
@@ -10,6 +10,7 @@ TODO: Account passwords (list)
* ... * ...
* salts & hashes * salts & hashes
TODO: Pagers giving their region counts in the API
TODO: Waypoints, with positions and trigger zones (collision areas) for doors, monster spawns, etc. TODO: Waypoints, with positions and trigger zones (collision areas) for doors, monster spawns, etc.
TODO: Fix shoddy movement TODO: Fix shoddy movement
TODO: Periodic mass server saves TODO: Periodic mass server saves