Merge branch 'develop'

This commit is contained in:
Kayne Ruse
2014-12-31 06:10:24 +11:00
37 changed files with 451 additions and 218 deletions
+6 -4
View File
@@ -501,7 +501,9 @@ void InWorld::SendRegionRequest(int roomIndex, int x, int y) {
void InWorld::HandleRegionContent(RegionPacket* const argPacket) {
//replace existing regions
regionPager.UnloadRegion(argPacket->x, argPacket->y);
regionPager.UnloadIf([&](Region const& region) -> bool {
return region.GetX() == argPacket->x && region.GetY() == argPacket->y;
});
regionPager.PushRegion(argPacket->region);
//clean up after the serial code
@@ -540,9 +542,9 @@ void InWorld::UpdateMap() {
//entity management
//-------------------------
//NOTE: preexisting characters will result in query responses
//NOTE: new characters will result in create messages
//NOTE: this client's character will exist in both (skipped)
//DOCS: preexisting characters will result in query responses
//DOCS: new characters will result in create messages
//DOCS: this client's character will exist in both (skipped)
void InWorld::HandleCharacterCreate(CharacterPacket* const argPacket) {
//prevent double message
+1 -21
View File
@@ -26,31 +26,11 @@
#include "region_pager_api.hpp"
#include "tile_sheet_api.hpp"
//macros
#include "region.hpp"
//useful "globals"
static int getRegionWidth(lua_State* L) {
lua_pushinteger(L, REGION_WIDTH);
return 1;
}
static int getRegionHeight(lua_State* L) {
lua_pushinteger(L, REGION_HEIGHT);
return 1;
}
static int getRegionDepth(lua_State* L) {
lua_pushinteger(L, REGION_DEPTH);
return 1;
}
//...
//This mimics linit.c to create a nested collection of all map modules.
static const luaL_Reg funcs[] = {
//synonyms
{"GetRegionWidth", getRegionWidth},
{"GetRegionHeight", getRegionHeight},
{"GetRegionDepth", getRegionDepth},
{nullptr, nullptr}
};
+13 -1
View File
@@ -21,9 +21,9 @@
*/
#include "region.hpp"
#include <stdexcept>
#include <cmath>
#include <cstring>
#include <stdexcept>
int snapToBase(int base, int x) {
return floor((double)x / base) * base;
@@ -55,4 +55,16 @@ bool Region::SetSolid(int x, int y, bool b) {
bool Region::GetSolid(int x, int y) {
return solid[x * REGION_WIDTH + y];
}
int Region::GetX() const {
return x;
}
int Region::GetY() const {
return y;
}
std::bitset<REGION_WIDTH*REGION_HEIGHT>* Region::GetSolidBitset() {
return &solid;
}
+3 -3
View File
@@ -48,10 +48,10 @@ public:
bool GetSolid(int x, int y);
//accessors
int GetX() const { return x; }
int GetY() const { return y; }
int GetX() const;
int GetY() const;
std::bitset<REGION_WIDTH*REGION_HEIGHT>* GetSolidBitset() { return &solid; }
std::bitset<REGION_WIDTH*REGION_HEIGHT>* GetSolidBitset();
private:
const int x;
const int y;
+2
View File
@@ -85,6 +85,8 @@ static const luaL_Reg regionLib[] = {
{"GetSolid",getSolid},
{"GetX",getX},
{"GetY",getY},
//the global macros
{"GetWidth",getWidth},
{"GetHeight",getHeight},
{"GetDepth",getDepth},
+26 -1
View File
@@ -84,7 +84,22 @@ static int createRegion(lua_State* L) {
static int unloadRegion(lua_State* L) {
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
pager->UnloadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
//two argument types: coords & the region itself
switch(lua_type(L, 2)) {
case LUA_TNUMBER:
pager->UnloadIf([&](Region const& region) -> bool {
int x = lua_tointeger(L, 2);
int y = lua_tointeger(L, 3);
return region.GetX() == x && region.GetY() == y;
});
break;
case LUA_TLIGHTUSERDATA:
pager->UnloadIf([&](Region const& region) -> bool {
return (&region) == lua_touserdata(L, 2);
});
break;
}
return 0;
}
@@ -116,6 +131,13 @@ static int setOnUnload(lua_State* L) {
return 0;
}
//debugging
static int containerSize(lua_State* L) {
RegionPagerLua* pager = static_cast<RegionPagerLua*>(lua_touserdata(L, 1));
lua_pushinteger(L, pager->GetContainer()->size());
return 1;
}
static const luaL_Reg regionPagerLib[] = {
//curry
{"SetTile", setTile},
@@ -136,6 +158,9 @@ static const luaL_Reg regionPagerLib[] = {
{"SetOnCreate",setOnCreate},
{"SetOnUnload",setOnUnload},
//debugging
{"ContainerSize", containerSize},
//sentinel
{nullptr, nullptr}
};
+10 -4
View File
@@ -24,6 +24,10 @@
#include <stdexcept>
#include <algorithm>
RegionPagerBase::~RegionPagerBase() {
UnloadAll();
};
Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
Region* ptr = GetRegion(x, y);
return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v);
@@ -88,12 +92,14 @@ Region* RegionPagerBase::CreateRegion(int x, int y) {
return &regionList.front();
}
void RegionPagerBase::UnloadRegion(int x, int y) {
regionList.remove_if([x, y](Region& region) -> bool {
return region.GetX() == x && region.GetY() == y;
});
void RegionPagerBase::UnloadIf(std::function<bool(Region const&)> fn) {
regionList.remove_if(fn);
}
void RegionPagerBase::UnloadAll() {
regionList.clear();
}
std::list<Region>* RegionPagerBase::GetContainer() {
return &regionList;
}
+4 -3
View File
@@ -24,12 +24,13 @@
#include "region.hpp"
#include <functional>
#include <list>
class RegionPagerBase {
public:
RegionPagerBase() = default;
virtual ~RegionPagerBase() { UnloadAll(); };
virtual ~RegionPagerBase();
//tile manipulation
virtual Region::type_t SetTile(int x, int y, int z, Region::type_t v);
@@ -47,12 +48,12 @@ public:
virtual Region* LoadRegion(int x, int y);
virtual Region* SaveRegion(int x, int y);
virtual Region* CreateRegion(int x, int y);
virtual void UnloadRegion(int x, int y);
virtual void UnloadIf(std::function<bool(Region const&)> fn);
virtual void UnloadAll();
//accessors & mutators
std::list<Region>* GetContainer() { return &regionList; }
std::list<Region>* GetContainer();
protected:
std::list<Region> regionList;
};
+10 -3
View File
@@ -23,6 +23,9 @@
#include <stdexcept>
//DOCS: Load, Save and Create fail unless the lua function has been set
//DOCS: UnloadIf and UnloadAll will still continue without the function set
RegionPagerLua::~RegionPagerLua() {
//unload all regions
UnloadAll();
@@ -130,23 +133,25 @@ Region* RegionPagerLua::CreateRegion(int x, int y) {
}
//no return
void RegionPagerLua::UnloadRegion(int x, int y) {
void RegionPagerLua::UnloadIf(std::function<bool(Region const&)> fn) {
//get the pager's function from the registry
lua_rawgeti(lua, LUA_REGISTRYINDEX, unloadRef);
//check if this function is available
if (lua_isnil(lua, -1)) {
lua_pop(lua, 1);
//remove the regions anyway
regionList.remove_if(fn);
return;
}
//run each region through this lambda
regionList.remove_if([&](Region& region) -> bool {
if (region.GetX() == x && region.GetY() == y) {
if (fn(region)) {
//push a copy of the function onto the stack with the region
lua_pushvalue(lua, -1);
lua_pushlightuserdata(lua, &region);
lua_pushlightuserdata(lua, static_cast<void*>(&region));
//call the function, 1 arg, 0 return
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
@@ -171,6 +176,8 @@ void RegionPagerLua::UnloadAll() {
//check if this function is available
if (lua_isnil(lua, -1)) {
lua_pop(lua, 1);
//remove the regions anyway
regionList.clear();
return;
}
+2 -1
View File
@@ -30,6 +30,7 @@
#include "lua.hpp"
#endif
#include <functional>
#include <string>
class RegionPagerLua : public RegionPagerBase {
@@ -41,8 +42,8 @@ public:
Region* LoadRegion(int x, int y) override;
Region* SaveRegion(int x, int y) override;
Region* CreateRegion(int x, int y) override;
void UnloadRegion(int x, int y) override;
void UnloadIf(std::function<bool(Region const&)> fn) override;
void UnloadAll() override;
//accessors & mutators
+1 -1
View File
@@ -26,7 +26,7 @@
#include <stdexcept>
//NOTE: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network
//DOCS: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network
//NOTE: don't confuse SerialPacketBase with UDPpacket
void UDPNetworkUtility::Open(int port) {
+58 -2
View File
@@ -1,11 +1,67 @@
print("Lua script check")
mapSystem = require "map_system"
mapMaker = require "map_maker"
mapSaver = require "map_saver"
roomSystem = require "room_system"
waypointSystem = require "waypoint_system"
local function dumpTable(t)
print(t)
for k, v in pairs(t) do
print("",k,v)
end
end
--NOTE: room 0 is the first that the client asks for, therefore it must exist
local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld")
roomSystem.Room.Initialize(overworld, "overworld.bmp", mapSaver.Load, mapSaver.Save, mapMaker.debugIsland, mapSaver.Save)
local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp")
--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")
--[[
debugging test
Ideal output:
-------------------------
pager: userdata: [memory location]
Size 0: 0
[debug output from load]
Size 1: 1
[debug output from save]
Size 2: 0
[debug output from load]
Size 3: 1
[debug output from save]
Size 4: 0
-------------------------
--]-]
print("-------------------------")
local pager = roomSystem.Room.GetPager(overworld)
print("pager:", pager)
print("Size 0:", mapSystem.RegionPager.ContainerSize(pager))
local regionFoo = mapSystem.RegionPager.GetRegion(pager, 0, 0)
print("Size 1:", mapSystem.RegionPager.ContainerSize(pager))
mapSystem.RegionPager.UnloadRegion(pager, regionFoo)
print("Size 2:", mapSystem.RegionPager.ContainerSize(pager))
local regionFoo = mapSystem.RegionPager.GetRegion(pager, 0, 0)
print("Size 3:", mapSystem.RegionPager.ContainerSize(pager))
mapSystem.RegionPager.UnloadRegion(pager, 0, 0)
print("Size 4:", mapSystem.RegionPager.ContainerSize(pager))
print("-------------------------")
--]]
-1
View File
@@ -219,7 +219,6 @@ void AccountManager::UnloadIf(std::function<bool(std::pair<const int, AccountDat
//-------------------------
AccountData* AccountManager::Get(int uid) {
//TODO: could this load an account first?
std::map<int, AccountData>::iterator it = elementMap.find(uid);
if (it == elementMap.end()) {
+14 -16
View File
@@ -24,7 +24,6 @@
#include "account_data.hpp"
#include "singleton.hpp"
#include "manager_interface.hpp"
#if defined(__MINGW32__)
#include "sqlite3/sqlite3.h"
@@ -35,26 +34,23 @@
#include <functional>
#include <map>
class AccountManager:
public Singleton<AccountManager>,
public ManagerInterface<AccountData, std::string, int>
{
class AccountManager: public Singleton<AccountManager> {
public:
//common public methods
int Create(std::string username, int clientIndex) override;
int Load(std::string username, int clientIndex) override;
int Save(int uid) override;
void Unload(int uid) override;
void Delete(int uid) override;
int Create(std::string username, int clientIndex);
int Load(std::string username, int clientIndex);
int Save(int uid);
void Unload(int uid);
void Delete(int uid);
void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, AccountData>)> fn) override;
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int, AccountData>)> fn);
//accessors and mutators
AccountData* Get(int uid) override;
int GetLoadedCount() override;
int GetTotalCount() override;
std::map<int, AccountData>* GetContainer() override;
AccountData* Get(int uid);
int GetLoadedCount();
int GetTotalCount();
std::map<int, AccountData>* GetContainer();
sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase();
@@ -65,6 +61,8 @@ private:
AccountManager() = default;
~AccountManager() = default;
//members
std::map<int, AccountData> elementMap;
sqlite3* database = nullptr;
};
+14 -16
View File
@@ -24,7 +24,6 @@
#include "character_data.hpp"
#include "singleton.hpp"
#include "manager_interface.hpp"
#if defined(__MINGW32__)
#include "sqlite3/sqlite3.h"
@@ -35,26 +34,23 @@
#include <functional>
#include <map>
class CharacterManager:
public Singleton<CharacterManager>,
public ManagerInterface<CharacterData, int, std::string, std::string>
{
class CharacterManager: public Singleton<CharacterManager> {
public:
//common public methods
int Create(int owner, std::string handle, std::string avatar) override;
int Load(int owner, std::string handle, std::string avatar) override;
int Save(int uid) override;
void Unload(int uid) override;
void Delete(int uid) override;
int Create(int owner, std::string handle, std::string avatar);
int Load(int owner, std::string handle, std::string avatar);
int Save(int uid);
void Unload(int uid);
void Delete(int uid);
void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, CharacterData>)> fn) override;
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int, CharacterData>)> fn);
//accessors and mutators
CharacterData* Get(int uid) override;
int GetLoadedCount() override;
int GetTotalCount() override;
std::map<int, CharacterData>* GetContainer() override;
CharacterData* Get(int uid);
int GetLoadedCount();
int GetTotalCount();
std::map<int, CharacterData>* GetContainer();
sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase();
@@ -65,6 +61,8 @@ private:
CharacterManager() = default;
~CharacterManager() = default;
//members
std::map<int, CharacterData> elementMap;
sqlite3* database = nullptr;
};
-1
View File
@@ -78,7 +78,6 @@ void ClientManager::UnloadIf(std::function<bool(std::pair<const int, ClientData>
while (it != elementMap.end()) {
if (fn(*it)) {
it = elementMap.erase(it);
//TODO: ? disconnect, unload characters, notify other clients
}
else {
++it;
+12 -18
View File
@@ -23,35 +23,32 @@
#define CLIENTMANAGER_HPP_
#include "client_data.hpp"
#include "manager_interface.hpp"
#include "server_packet.hpp"
#include "singleton.hpp"
#include "SDL/SDL_net.h"
#include <functional>
#include <map>
class ClientManager:
public Singleton<ClientManager>,
public ManagerInterface<ClientData, IPaddress>
{
class ClientManager: public Singleton<ClientManager> {
public:
//methods
int CheckConnections();
void HandlePong(ServerPacket* const argPacket);
//common public methods
int Create(IPaddress) override;
void Unload(int uid) override;
int Create(IPaddress);
void Unload(int uid);
void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, ClientData>)> fn) override;
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int, ClientData>)> fn);
//accessors & mutators
ClientData* Get(int uid) override;
int GetLoadedCount() override;
int GetTotalCount() override;
std::map<int, ClientData>* GetContainer() override;
ClientData* Get(int uid);
int GetLoadedCount();
int GetTotalCount();
std::map<int, ClientData>* GetContainer();
private:
friend Singleton<ClientManager>;
@@ -59,11 +56,8 @@ private:
ClientManager() = default;
~ClientManager() = default;
//EMPTY
int Load(IPaddress) override { return -1; }
int Save(int uid) override { return -1; }
void Delete(int uid) override { return; }
//members
std::map<int, ClientData> elementMap;
int counter = 0;
};
+1 -1
View File
@@ -38,7 +38,7 @@ public:
protected:
Entity() = default;
~Entity() = default;
virtual ~Entity() = default;
int roomIndex = -1;
Vector2 origin;
+44
View File
@@ -23,7 +23,51 @@
#include "entity.hpp"
static int setRoomIndex(lua_State* L) {
Entity* entity = static_cast<Entity*>(lua_touserdata(L, 1));
entity->SetRoomIndex(lua_tointeger(L, 2));
return 0;
}
static int setOrigin(lua_State* L) {
Entity* entity = static_cast<Entity*>(lua_touserdata(L, 1));
entity->SetOrigin({lua_tonumber(L, 2), lua_tonumber(L, 3)});
return 0;
}
static int setMotion(lua_State* L) {
Entity* entity = static_cast<Entity*>(lua_touserdata(L, 1));
entity->SetMotion({lua_tonumber(L, 2), lua_tonumber(L, 3)});
return 0;
}
static int getRoomIndex(lua_State* L) {
Entity* entity = static_cast<Entity*>(lua_touserdata(L, 1));
lua_pushinteger(L, entity->GetRoomIndex());
return 1;
}
static int getOrigin(lua_State* L) {
Entity* entity = static_cast<Entity*>(lua_touserdata(L, 1));
lua_pushnumber(L, entity->GetOrigin().x);
lua_pushnumber(L, entity->GetOrigin().y);
return 2;
}
static int getMotion(lua_State* L) {
Entity* entity = static_cast<Entity*>(lua_touserdata(L, 1));
lua_pushnumber(L, entity->GetOrigin().x);
lua_pushnumber(L, entity->GetOrigin().y);
return 2;
}
static const luaL_Reg entityLib[] = {
{"SetRoomIndex", setRoomIndex},
{"SetOrigin", setOrigin},
{"SetMotion", setMotion},
{"GetRoomIndex", getRoomIndex},
{"GetOrigin", getOrigin},
{"GetMotion", getMotion},
{nullptr, nullptr}
};
+2
View File
@@ -42,6 +42,7 @@
#include "map_system_api.hpp"
#include "room_system_api.hpp"
#include "waypoint_system_api.hpp"
//these libs are loaded by lua.c and are readily available to any Lua program
static const luaL_Reg loadedlibs[] = {
@@ -64,6 +65,7 @@ static const luaL_Reg loadedlibs[] = {
static const luaL_Reg preloadedlibs[] = {
{TORTUGA_MAP_SYSTEM_API, openMapSystemAPI},
{TORTUGA_ROOM_SYSTEM_API, openRoomSystemAPI},
{TORTUGA_WAYPOINT_SYSTEM_API, openWaypointSystemAPI},
{NULL, NULL}
};
+15 -16
View File
@@ -22,7 +22,6 @@
#ifndef MONSTERMANAGER_HPP_
#define MONSTERMANAGER_HPP_
#include "manager_interface.hpp"
#include "monster_data.hpp"
#include "singleton.hpp"
@@ -35,28 +34,26 @@
#endif
#include <functional>
#include <map>
#include <string>
class MonsterManager:
public Singleton<MonsterManager>,
public ManagerInterface<MonsterData, std::string>
{
class MonsterManager: public Singleton<MonsterManager> {
public:
//common public methods
int Create(std::string) override;
int Load(std::string) override;
int Save(int uid) override;
void Unload(int uid) override;
void Delete(int uid) override;
int Create(std::string);
int Load(std::string);
int Save(int uid);
void Unload(int uid);
void Delete(int uid);
void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, MonsterData>)> fn) override;
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int, MonsterData>)> fn);
//accessors & mutators
MonsterData* Get(int uid) override;
int GetLoadedCount() override;
int GetTotalCount() override;
std::map<int, MonsterData>* GetContainer() override;
MonsterData* Get(int uid);
int GetLoadedCount();
int GetTotalCount();
std::map<int, MonsterData>* GetContainer();
//hooks
sqlite3* SetDatabase(sqlite3* db);
@@ -70,6 +67,8 @@ private:
MonsterManager() = default;
~MonsterManager() = default;
//members
std::map<int, MonsterData> elementMap;
sqlite3* database = nullptr;
lua_State* lua = nullptr;
};
+8 -7
View File
@@ -25,25 +25,25 @@
static int setRoomName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
room->SetRoomName(lua_tostring(L, 2));
room->SetName(lua_tostring(L, 2));
return 0;
}
static int getRoomName(lua_State* L) {
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;
}
static int setTilesetName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
room->SetTilesetName(lua_tostring(L, 2));
room->SetTileset(lua_tostring(L, 2));
return 0;
}
static int getTilesetName(lua_State* L) {
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;
}
@@ -56,7 +56,8 @@ static int getPager(lua_State* L) {
static int initialize(lua_State* L) {
//set the members of the given room
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)
room->GetPager()->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX));
@@ -70,8 +71,8 @@ static int initialize(lua_State* L) {
static const luaL_Reg roomLib[] = {
{"GetPager",getPager},
{"SetRoomName", setRoomName},
{"GetRoomName", getRoomName},
{"SetName", setRoomName},
{"GetName", getRoomName},
{"SetTileset", setTilesetName},
{"GetTileset", getTilesetName},
{"Initialize", initialize},
+4 -4
View File
@@ -21,19 +21,19 @@
*/
#include "room_data.hpp"
std::string RoomData::SetRoomName(std::string s) {
std::string RoomData::SetName(std::string s) {
return roomName = s;
}
std::string RoomData::GetRoomName() {
std::string RoomData::GetName() {
return roomName;
}
std::string RoomData::SetTilesetName(std::string s) {
std::string RoomData::SetTileset(std::string s) {
return tilesetName = s;
}
std::string RoomData::GetTilesetName() {
std::string RoomData::GetTileset() {
return tilesetName;
}
+6 -4
View File
@@ -40,15 +40,17 @@ public:
~RoomData() = default;
//accessors and mutators
std::string SetRoomName(std::string s);
std::string GetRoomName();
std::string SetName(std::string);
std::string GetName();
std::string SetTilesetName(std::string s);
std::string GetTilesetName();
std::string SetTileset(std::string);
std::string GetTileset();
RegionPagerLua* GetPager();
std::list<Entity*>* GetEntityList();
//TODO: triggers for unload, save, per-second, player enter, player exit, etc.
private:
friend class RoomManager;
+20 -16
View File
@@ -29,27 +29,18 @@
//public access methods
//-------------------------
int RoomManager::Create(std::string roomName) {
int RoomManager::Create(std::string roomName, std::string tileset) {
//create the room
RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element
newRoom->SetRoomName(roomName);
newRoom->SetName(roomName);
newRoom->SetTileset(tileset);
newRoom->pager.SetLuaState(lua);
//finish the routine
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() {
elementMap.clear();
}
@@ -76,14 +67,27 @@ RoomData* RoomManager::Get(int uid) {
return &it->second;
}
int RoomManager::GetLoadedCount() {
return elementMap.size();
RoomData* RoomManager::Get(std::string name) {
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();
}
std::map<int, RoomData>* RoomManager::GetContainer() {
return &elementMap;
}
lua_State* RoomManager::SetLuaState(lua_State* L) {
return lua = L;
}
lua_State* RoomManager::GetLuaState() {
return lua;
}
+15 -19
View File
@@ -24,7 +24,6 @@
#include "room_data.hpp"
#include "singleton.hpp"
#include "manager_interface.hpp"
#if defined(__MINGW32__)
#include "lua/lua.hpp"
@@ -32,27 +31,26 @@
#include "lua.hpp"
#endif
class RoomManager:
public Singleton<RoomManager>,
public ManagerInterface<RoomData, std::string>
{
#include <functional>
#include <map>
class RoomManager: public Singleton<RoomManager> {
public:
//common public methods
int Create(std::string) override;
void Unload(int uid) override;
int Create(std::string name, std::string tileset);
void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn) override;
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn);
//accessors and mutators
RoomData* Get(int uid) override;
int GetLoadedCount() override;
int GetTotalCount() override;
std::map<int, RoomData>* GetContainer() override;
RoomData* Get(int uid);
RoomData* Get(std::string name);
int GetLoadedCount();
std::map<int, RoomData>* GetContainer();
//hooks
lua_State* SetLuaState(lua_State* L) { return lua = L; }
lua_State* GetLuaState() { return lua; }
lua_State* SetLuaState(lua_State* L);
lua_State* GetLuaState();
private:
friend Singleton<RoomManager>;
@@ -60,10 +58,8 @@ private:
RoomManager() = default;
~RoomManager() = default;
int Load(std::string) override { return -1; }
int Save(int uid) override { return -1; }
void Delete(int uid) override { }
//members
std::map<int, RoomData> elementMap;
lua_State* lua = nullptr;
int counter = 0;
};
+42 -6
View File
@@ -24,31 +24,67 @@
#include "room_manager.hpp"
int createRoom(lua_State* L) {
//create & get the room
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);
//TODO: initialization parameters here?
//return room, uid
lua_pushlightuserdata(L, static_cast<void*>(room));
lua_pushinteger(L, uid);
lua_pushinteger(L, uid); //for debugging, mostly
return 2;
}
int unloadRoom(lua_State* L) {
//TODO: check authorization for room deletion
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;
}
int getRoom(lua_State* L) {
//TODO: integer vs name for getRoom()
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) {
lua_pushlightuserdata(L, static_cast<void*>(room));
+1 -2
View File
@@ -105,7 +105,7 @@ void ServerApplication::HandleCharacterLoad(CharacterPacket* const argPacket) {
if (characterIndex == -1) {
msg << "Character already loaded: ";
}
if (characterIndex == -1) {
if (characterIndex == -2) {
msg << "Character name is taken: ";
}
msg << argPacket->handle;
@@ -191,7 +191,6 @@ void ServerApplication::HandleCharacterSetRoom(CharacterPacket* const argPacket)
}
//set the character's room, zero it's origin, zero it's motion
//TODO: Set the origin here
characterData->SetRoomIndex(argPacket->roomIndex);
characterData->SetOrigin({0, 0});
characterData->SetMotion({0, 0});
+1
View File
@@ -105,6 +105,7 @@ void ServerApplication::Init(int argc, char* argv[]) {
characterMgr.SetDatabase(database);
roomMgr.SetLuaState(luaState);
waypointMgr.SetLuaState(luaState);
std::cout << "Internal managers initialized" << std::endl;
+1 -1
View File
@@ -1,5 +1,5 @@
#config
INCLUDES+=. ../server_utilities ../../common/utilities
INCLUDES+=. ../entities ../server_utilities ../../common/utilities
LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+1
View File
@@ -23,6 +23,7 @@
#include "waypoint_data.hpp"
//TODO: Can I alias the entity API for this?
static const luaL_Reg waypointLib[] = {
{nullptr, nullptr}
};
+7
View File
@@ -21,3 +21,10 @@
*/
#include "waypoint_data.hpp"
int WaypointData::SetTriggerReference(int i) {
return triggerRef = i;
}
int WaypointData::GetTriggerReference() {
return triggerRef;
}
+14 -1
View File
@@ -22,15 +22,28 @@
#ifndef WAYPOINTDATA_HPP_
#define WAYPOINTDATA_HPP_
#include "entity.hpp"
#if defined(__MINGW32__)
#include "lua/lua.hpp"
#else
#include "lua.hpp"
#endif
#include <string>
class WaypointData {
class WaypointData: public Entity {
public:
WaypointData() = default;
~WaypointData() = default;
int SetTriggerReference(int i);
int GetTriggerReference();
private:
friend class WaypointManager;
int triggerRef = LUA_NOREF;
};
#endif
+29 -16
View File
@@ -23,39 +23,52 @@
#define WAYPOINTMANAGER_HPP_
#include "waypoint_data.hpp"
#include "manager_interface.hpp"
#include "singleton.hpp"
#include "vector2.hpp"
#if defined(__MINGW32__)
#include "lua/lua.hpp"
#else
#include "lua.hpp"
#endif
#include <functional>
#include <map>
#include <string>
class WaypointManager:
public Singleton<WaypointManager>,
public ManagerInterface<WaypointData>
{
//TODO: should waypoints be managed on a per-room basis?
class WaypointManager: public Singleton<WaypointManager> {
public:
//common public methods
int Create() override;
int Load() override;
int Save(int uid) override;
void Unload(int uid) override;
void Delete(int uid) override;
int Create();
int Load();
int Save(int uid);
void Unload(int uid);
void Delete(int uid);
void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, WaypointData>)> fn) override;
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int, WaypointData>)> fn);
//accessors & mutators
WaypointData* Get(int uid) override;
int GetLoadedCount() override;
int GetTotalCount() override;
std::map<int, WaypointData>* GetContainer() override;
WaypointData* Get(int uid);
int GetLoadedCount();
int GetTotalCount();
std::map<int, WaypointData>* GetContainer();
//hooks
lua_State* SetLuaState(lua_State* L) { return lua = L; }
lua_State* GetLuaState() { return lua; }
private:
friend Singleton<WaypointManager>;
WaypointManager() = default;
~WaypointManager() = default;
//members
std::map<int, WaypointData> elementMap;
lua_State* lua = nullptr;
int counter = 0;
};
#endif
@@ -19,37 +19,37 @@
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef MANAGERINTERFACE_HPP_
#define MANAGERINTERFACE_HPP_
#include "waypoint_system_api.hpp"
#include <functional>
#include <map>
//all waypoint API headers
#include "waypoint_api.hpp"
#include "waypoint_manager_api.hpp"
template<typename T, typename... Arguments>
class ManagerInterface {
public:
//common public methods
virtual int Create(Arguments... parameters) = 0;
virtual int Load(Arguments... parameters) = 0;
virtual int Save(int uid) = 0;
virtual void Unload(int uid) = 0;
virtual void Delete(int uid) = 0;
//useful "globals"
//...
virtual void UnloadAll() = 0;
virtual void UnloadIf(std::function<bool(std::pair<const int, T>)> fn) = 0;
//accessors & mutators
virtual T* Get(int uid) = 0;
virtual int GetLoadedCount() = 0;
virtual int GetTotalCount() = 0; //can be an alias of GetLoadedCount()
virtual std::map<int, T>* GetContainer() = 0;
protected:
ManagerInterface() = default;
~ManagerInterface() = default;
//members
std::map<int, T> elementMap;
//This mimics linit.c to create a nested collection of all waypoint modules.
static const luaL_Reg funcs[] = {
{nullptr, nullptr}
};
#endif
static const luaL_Reg libs[] = {
{"Waypoint", openWaypointAPI},
{"WaypointManager", openWaypointManagerAPI},
{nullptr, nullptr}
};
int openWaypointSystemAPI(lua_State* L) {
//create the table
luaL_newlibtable(L, libs);
//push the "global" functions
luaL_setfuncs(L, funcs, 0);
//push the substable
for (const luaL_Reg* lib = libs; lib->func; lib++) {
lib->func(L);
lua_setfield(L, -2, lib->name);
}
return 1;
}
+34
View File
@@ -0,0 +1,34 @@
/* 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.
*/
#ifndef WAYPOINTSYSTEMAPI_HPP_
#define WAYPOINTSYSTEMAPI_HPP_
#if defined(__MINGW32__)
#include "lua/lua.hpp"
#else
#include "lua.hpp"
#endif
#define TORTUGA_WAYPOINT_SYSTEM_API "waypoint_system"
LUAMOD_API int openWaypointSystemAPI(lua_State* L);
#endif
+1
View File
@@ -10,6 +10,7 @@ TODO: Account passwords (list)
* ...
* 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: Fix shoddy movement
TODO: Periodic mass server saves