Collapsed the pager into a single file, removing lua hooks
I need to re-add the lua hooks, but it'll be easy.
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* 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 "map_allocator.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
void BlankAllocator::Create(Region** const ptr, int x, int y) {
|
||||
(*ptr) = new Region(x, y);
|
||||
}
|
||||
|
||||
void BlankAllocator::Unload(Region* const ptr) {
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
void LuaAllocator::Create(Region** const ptr, int x, int y) {
|
||||
//something to work on
|
||||
(*ptr) = new Region(x, y);
|
||||
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "create");
|
||||
lua_pushlightuserdata(state, *ptr);
|
||||
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
}
|
||||
|
||||
void LuaAllocator::Unload(Region* const ptr) {
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "unload");
|
||||
lua_pushlightuserdata(state, ptr);
|
||||
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
|
||||
//clean up the memory
|
||||
delete ptr;
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* 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 MAPALLOCATOR_HPP_
|
||||
#define MAPALLOCATOR_HPP_
|
||||
|
||||
#include "region.hpp"
|
||||
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
class BlankAllocator {
|
||||
public:
|
||||
void Create(Region** const, int x, int y);
|
||||
void Unload(Region* const);
|
||||
private:
|
||||
//
|
||||
};
|
||||
|
||||
class LuaAllocator {
|
||||
public:
|
||||
void Create(Region** const, int x, int y);
|
||||
void Unload(Region* const);
|
||||
|
||||
lua_State* SetLuaState(lua_State* L) { return state = L; }
|
||||
lua_State* GetLuaState() { return state; }
|
||||
private:
|
||||
lua_State* state = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,66 +0,0 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* 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 "map_file_format.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
void DummyFormat::Load(Region** const ptr, int x, int y) {
|
||||
//EMPTY
|
||||
}
|
||||
|
||||
void DummyFormat::Save(Region* const ptr) {
|
||||
//EMPTY
|
||||
}
|
||||
|
||||
void LuaFormat::Load(Region** const ptr, int x, int y) {
|
||||
//something to load into
|
||||
|
||||
if (!(*ptr)) {
|
||||
(*ptr) = new Region(x, y);
|
||||
}
|
||||
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "load");
|
||||
lua_pushlightuserdata(state, *ptr);
|
||||
lua_pushstring(state, saveDir.c_str());
|
||||
if (lua_pcall(state, 2, 1, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
if (lua_toboolean(state, -1) == false) {
|
||||
delete (*ptr);
|
||||
(*ptr) = nullptr;
|
||||
}
|
||||
lua_pop(state, 2);
|
||||
}
|
||||
|
||||
void LuaFormat::Save(Region* const ptr) {
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "save");
|
||||
lua_pushlightuserdata(state, ptr);
|
||||
lua_pushstring(state, saveDir.c_str());
|
||||
if (lua_pcall(state, 2, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* 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 MAPFILEFORMAT_HPP_
|
||||
#define MAPFILEFORMAT_HPP_
|
||||
|
||||
#include "region.hpp"
|
||||
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
//TODO: I'm unhappy with using this system, there needs to be a way to handle saving/loading better
|
||||
|
||||
class DummyFormat {
|
||||
public:
|
||||
void Load(Region** const, int x, int y);
|
||||
void Save(Region* const);
|
||||
|
||||
std::string SetSaveDir(std::string s) { return saveDir = s; }
|
||||
std::string GetSaveDir() { return saveDir; }
|
||||
private:
|
||||
std::string saveDir;
|
||||
};
|
||||
|
||||
//TODO: verbose save file format
|
||||
//TODO: compact save file format
|
||||
|
||||
class LuaFormat {
|
||||
public:
|
||||
void Load(Region** const, int x, int y);
|
||||
void Save(Region* const);
|
||||
|
||||
std::string SetSaveDir(std::string s) { return saveDir = s; }
|
||||
std::string GetSaveDir() { return saveDir; }
|
||||
|
||||
lua_State* SetLuaState(lua_State* L) { return state = L; }
|
||||
lua_State* GetLuaState() { return state; }
|
||||
private:
|
||||
std::string saveDir;
|
||||
lua_State* state = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -23,17 +23,17 @@
|
||||
|
||||
#include "utility.hpp"
|
||||
|
||||
Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
|
||||
Region::type_t RegionPager::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);
|
||||
}
|
||||
|
||||
Region::type_t RegionPagerBase::GetTile(int x, int y, int z) {
|
||||
Region::type_t RegionPager::GetTile(int x, int y, int z) {
|
||||
Region* ptr = GetRegion(x, y);
|
||||
return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z);
|
||||
}
|
||||
|
||||
Region* RegionPagerBase::GetRegion(int x, int y) {
|
||||
Region* RegionPager::GetRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
@@ -47,7 +47,7 @@ Region* RegionPagerBase::GetRegion(int x, int y) {
|
||||
return CreateRegion(x, y);
|
||||
}
|
||||
|
||||
Region* RegionPagerBase::FindRegion(int x, int y) {
|
||||
Region* RegionPager::FindRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
@@ -61,7 +61,54 @@ Region* RegionPagerBase::FindRegion(int x, int y) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Region* RegionPagerBase::PushRegion(Region* ptr) {
|
||||
regionList.push_front(ptr);
|
||||
return regionList.front();
|
||||
Region* RegionPager::LoadRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//load the region if possible
|
||||
//TODO: Load the region (lua)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Region* RegionPager::SaveRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//find & save the region
|
||||
Region* ptr = FindRegion(x, y);
|
||||
if (ptr) {
|
||||
//TODO: save the region (lua)
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
Region* RegionPager::CreateRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//create and push the object
|
||||
Region* ptr = new Region(x, y);
|
||||
//TODO: create the region (lua)
|
||||
regionList.push_front(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void RegionPager::UnloadRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//custom loop, not FindRegion()
|
||||
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
|
||||
if ((*it)->GetX() == x && (*it)->GetY() == y) {
|
||||
//TODO: unload the region (lua)
|
||||
delete (*it);
|
||||
it = regionList.erase(it);
|
||||
continue;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
}
|
||||
@@ -23,14 +23,14 @@
|
||||
#define REGIONPAGER_HPP_
|
||||
|
||||
#include "region.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <list>
|
||||
|
||||
class RegionPagerBase {
|
||||
//TODO: add lua interface?
|
||||
class RegionPager {
|
||||
public:
|
||||
RegionPagerBase() {};
|
||||
virtual ~RegionPagerBase() {};
|
||||
RegionPager() = default;
|
||||
~RegionPager() = default;
|
||||
|
||||
//tile manipulation
|
||||
Region::type_t SetTile(int x, int y, int z, Region::type_t v);
|
||||
@@ -39,14 +39,12 @@ public:
|
||||
//region manipulation
|
||||
Region* GetRegion(int x, int y);
|
||||
Region* FindRegion(int x, int y);
|
||||
Region* PushRegion(Region*);
|
||||
|
||||
//interface
|
||||
virtual Region* LoadRegion(int x, int y) = 0;
|
||||
virtual Region* SaveRegion(int x, int y) = 0;
|
||||
virtual Region* CreateRegion(int x, int y) = 0;
|
||||
virtual void UnloadRegion(int x, int y) = 0;
|
||||
//TODO: delete existing regions
|
||||
Region* LoadRegion(int x, int y);
|
||||
Region* SaveRegion(int x, int y);
|
||||
Region* CreateRegion(int x, int y);
|
||||
void UnloadRegion(int x, int y);
|
||||
void DeleteRegion(int x, int y);
|
||||
|
||||
//accessors & mutators
|
||||
std::list<Region*>* GetContainer() { return ®ionList; }
|
||||
@@ -54,80 +52,4 @@ protected:
|
||||
std::list<Region*> regionList;
|
||||
};
|
||||
|
||||
template<typename Allocator, typename FileFormat>
|
||||
class RegionPager : public RegionPagerBase {
|
||||
public:
|
||||
RegionPager() {};
|
||||
~RegionPager() {
|
||||
UnloadAll();
|
||||
}
|
||||
|
||||
Region* LoadRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//load the region if possible
|
||||
Region* ptr = nullptr;
|
||||
format.Load(&ptr, x, y);
|
||||
if (ptr) {
|
||||
return PushRegion(ptr);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Region* SaveRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//find & save the region
|
||||
Region* ptr = FindRegion(x, y);
|
||||
if (ptr) {
|
||||
format.Save(ptr);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
Region* CreateRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//create and push the object
|
||||
Region* ptr = nullptr;
|
||||
allocator.Create(&ptr, x, y);
|
||||
return PushRegion(ptr);
|
||||
}
|
||||
|
||||
void UnloadRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//custom loop
|
||||
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
|
||||
if ((*it)->GetX() == x && (*it)->GetY() == y) {
|
||||
allocator.Unload(*it);
|
||||
it = regionList.erase(it);
|
||||
continue;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
}
|
||||
void UnloadAll() {
|
||||
for (auto& it : regionList) {
|
||||
allocator.Unload(it);
|
||||
}
|
||||
regionList.clear();
|
||||
}
|
||||
|
||||
//accessors
|
||||
Allocator* GetAllocator() { return &allocator; }
|
||||
FileFormat* GetFormat() { return &format; }
|
||||
protected:
|
||||
Allocator allocator;
|
||||
FileFormat format;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -23,8 +23,6 @@
|
||||
|
||||
#include "serial_util.hpp"
|
||||
|
||||
#include "map_allocator.hpp"
|
||||
|
||||
void serializeRegionFormat(RegionPacket* packet, void* buffer) {
|
||||
SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
||||
|
||||
@@ -71,11 +69,7 @@ void deserializeRegionContent(RegionPacket* packet, void* buffer) {
|
||||
DESERIALIZE(buffer, &packet->y, sizeof(int));
|
||||
|
||||
//an object to work on
|
||||
BlankAllocator().Create(
|
||||
&packet->region,
|
||||
packet->x,
|
||||
packet->y
|
||||
);
|
||||
packet->region = new Region(packet->x, packet->y);
|
||||
|
||||
//content
|
||||
for (register int i = 0; i < REGION_WIDTH; i++) {
|
||||
|
||||
@@ -22,8 +22,6 @@
|
||||
#include "map_api.hpp"
|
||||
|
||||
//map headers
|
||||
#include "map_allocator.hpp"
|
||||
#include "map_file_format.hpp"
|
||||
#include "region_pager.hpp"
|
||||
|
||||
//NOTE: When operating on a region, setTile() & getTile() *are not* zero indexed, but when operating on the entire map they *are* zero indexed.
|
||||
@@ -42,7 +40,7 @@ static int setTile(lua_State* L) {
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
|
||||
//assume the pager is using lua
|
||||
RegionPager<LuaAllocator, LuaFormat>* pager = reinterpret_cast<RegionPager<LuaAllocator, LuaFormat>*>(lua_touserdata(L, -1));
|
||||
RegionPager* pager = reinterpret_cast<RegionPager*>(lua_touserdata(L, -1));
|
||||
|
||||
//balance the stack
|
||||
lua_pop(L, 1);
|
||||
@@ -65,7 +63,7 @@ static int getTile(lua_State* L) {
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
|
||||
//assume the pager is using lua
|
||||
RegionPager<LuaAllocator, LuaFormat>* pager = reinterpret_cast<RegionPager<LuaAllocator, LuaFormat>*>(lua_touserdata(L, -1));
|
||||
RegionPager* pager = reinterpret_cast<RegionPager*>(lua_touserdata(L, -1));
|
||||
|
||||
//balance the stack
|
||||
lua_pop(L, 1);
|
||||
|
||||
@@ -23,8 +23,6 @@
|
||||
#define ROOMDATA_HPP_
|
||||
|
||||
//map system
|
||||
#include "map_allocator.hpp"
|
||||
#include "map_file_format.hpp"
|
||||
#include "region_pager.hpp"
|
||||
|
||||
struct RoomData {
|
||||
@@ -37,7 +35,7 @@ struct RoomData {
|
||||
};
|
||||
|
||||
//members
|
||||
RegionPager<LuaAllocator, LuaFormat> pager;
|
||||
RegionPager pager;
|
||||
RoomType type;
|
||||
|
||||
//TODO: collision map
|
||||
|
||||
@@ -35,7 +35,6 @@ public:
|
||||
|
||||
//public access methods
|
||||
//TODO
|
||||
//TODO: setup the pagers and functors of each room object
|
||||
|
||||
//accessors and mutators
|
||||
RoomData* GetRoom(int uid);
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
#include "sql_utility.hpp"
|
||||
#include "serial.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
Reference in New Issue
Block a user