Adjusted the lua's map API, requiring a large rewrite
One the whole nothing major has changed, but I think this makes things more logical. I just hope its stable, since I'm releasing a new tag.
This commit is contained in:
@@ -36,8 +36,8 @@ void LuaAllocator::Create(Region** const ptr, int x, int y) {
|
|||||||
(*ptr) = new Region(x, y);
|
(*ptr) = new Region(x, y);
|
||||||
|
|
||||||
//API hook
|
//API hook
|
||||||
lua_getglobal(state, "Region");
|
lua_getglobal(state, "map");
|
||||||
lua_getfield(state, -1, "Create");
|
lua_getfield(state, -1, "create");
|
||||||
lua_pushlightuserdata(state, *ptr);
|
lua_pushlightuserdata(state, *ptr);
|
||||||
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||||
@@ -47,8 +47,8 @@ void LuaAllocator::Create(Region** const ptr, int x, int y) {
|
|||||||
|
|
||||||
void LuaAllocator::Unload(Region* const ptr) {
|
void LuaAllocator::Unload(Region* const ptr) {
|
||||||
//API hook
|
//API hook
|
||||||
lua_getglobal(state, "Region");
|
lua_getglobal(state, "map");
|
||||||
lua_getfield(state, -1, "Unload");
|
lua_getfield(state, -1, "unload");
|
||||||
lua_pushlightuserdata(state, ptr);
|
lua_pushlightuserdata(state, ptr);
|
||||||
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ void LuaFormat::Load(Region** const ptr, int x, int y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//API hook
|
//API hook
|
||||||
lua_getglobal(state, "Region");
|
lua_getglobal(state, "map");
|
||||||
lua_getfield(state, -1, "Load");
|
lua_getfield(state, -1, "load");
|
||||||
lua_pushlightuserdata(state, *ptr);
|
lua_pushlightuserdata(state, *ptr);
|
||||||
lua_pushstring(state, saveDir.c_str());
|
lua_pushstring(state, saveDir.c_str());
|
||||||
if (lua_pcall(state, 2, 1, 0) != LUA_OK) {
|
if (lua_pcall(state, 2, 1, 0) != LUA_OK) {
|
||||||
@@ -55,8 +55,8 @@ void LuaFormat::Load(Region** const ptr, int x, int y) {
|
|||||||
|
|
||||||
void LuaFormat::Save(Region* const ptr) {
|
void LuaFormat::Save(Region* const ptr) {
|
||||||
//API hook
|
//API hook
|
||||||
lua_getglobal(state, "Region");
|
lua_getglobal(state, "map");
|
||||||
lua_getfield(state, -1, "Save");
|
lua_getfield(state, -1, "save");
|
||||||
lua_pushlightuserdata(state, ptr);
|
lua_pushlightuserdata(state, ptr);
|
||||||
lua_pushstring(state, saveDir.c_str());
|
lua_pushstring(state, saveDir.c_str());
|
||||||
if (lua_pcall(state, 2, 0, 0) != LUA_OK) {
|
if (lua_pcall(state, 2, 0, 0) != LUA_OK) {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
#define LUA_LIB
|
#define LUA_LIB
|
||||||
|
|
||||||
#include "lua/lua.hpp"
|
#include "lua/lua.hpp"
|
||||||
#include "region_api.hpp"
|
#include "map_api.hpp"
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -41,7 +41,7 @@ static const luaL_Reg loadedlibs[] = {
|
|||||||
{LUA_DBLIBNAME, luaopen_debug},
|
{LUA_DBLIBNAME, luaopen_debug},
|
||||||
|
|
||||||
/* custom libs */
|
/* custom libs */
|
||||||
{LUA_REGIONLIBNAME, luaopen_regionapi},
|
{LUA_MAPLIBNAME, luaopen_mapapi},
|
||||||
|
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,126 @@
|
|||||||
|
/* 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_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.
|
||||||
|
|
||||||
|
static int setTile(lua_State* L) {
|
||||||
|
if (lua_gettop(L) == 5) {
|
||||||
|
//operating on a region
|
||||||
|
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||||
|
ptr->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//operating on the whole map
|
||||||
|
lua_pushstring(L, "pager");
|
||||||
|
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||||
|
|
||||||
|
//assume the pager is using lua
|
||||||
|
RegionPager<LuaAllocator, LuaFormat>* pager = reinterpret_cast<RegionPager<LuaAllocator, LuaFormat>*>(lua_touserdata(L, -1));
|
||||||
|
|
||||||
|
//balance the stack
|
||||||
|
lua_pop(L, 1);
|
||||||
|
|
||||||
|
pager->SetTile(lua_tointeger(L, 1), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4));
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getTile(lua_State* L) {
|
||||||
|
if (lua_gettop(L) == 4) {
|
||||||
|
//operating on a region
|
||||||
|
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||||
|
int ret = ptr->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1);
|
||||||
|
lua_pushnumber(L, ret);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//operating on the whole map
|
||||||
|
lua_pushstring(L, "pager");
|
||||||
|
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||||
|
|
||||||
|
//assume the pager is using lua
|
||||||
|
RegionPager<LuaAllocator, LuaFormat>* pager = reinterpret_cast<RegionPager<LuaAllocator, LuaFormat>*>(lua_touserdata(L, -1));
|
||||||
|
|
||||||
|
//balance the stack
|
||||||
|
lua_pop(L, 1);
|
||||||
|
|
||||||
|
int ret = pager->GetTile(lua_tointeger(L, 1), lua_tointeger(L, 2), lua_tointeger(L, 3));
|
||||||
|
lua_pushnumber(L, ret);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getX(lua_State* L) {
|
||||||
|
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||||
|
lua_pushinteger(L, ptr->GetX());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getY(lua_State* L) {
|
||||||
|
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||||
|
lua_pushinteger(L, ptr->GetY());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dummy(lua_State* L) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const luaL_Reg regionlib[] = {
|
||||||
|
{"create", dummy},
|
||||||
|
{"unload", dummy},
|
||||||
|
{"load", dummy},
|
||||||
|
{"save", dummy},
|
||||||
|
{"settile",setTile},
|
||||||
|
{"gettile",getTile},
|
||||||
|
{"getx",getX},
|
||||||
|
{"gety",getY},
|
||||||
|
{"getregionwidth",getRegionWidth},
|
||||||
|
{"getregionheight",getRegionHeight},
|
||||||
|
{"getregiondepth",getRegionDepth},
|
||||||
|
{nullptr, nullptr}
|
||||||
|
};
|
||||||
|
|
||||||
|
LUAMOD_API int luaopen_mapapi(lua_State* L) {
|
||||||
|
luaL_newlib(L, regionlib);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -19,12 +19,12 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#ifndef REGIONAPI_HPP_
|
#ifndef MAPAPI_HPP_
|
||||||
#define REGIONAPI_HPP_
|
#define MAPAPI_HPP_
|
||||||
|
|
||||||
#include "lua/lua.hpp"
|
#include "lua/lua.hpp"
|
||||||
|
|
||||||
#define LUA_REGIONLIBNAME "Region"
|
#define LUA_MAPLIBNAME "map"
|
||||||
LUAMOD_API int luaopen_regionapi(lua_State* L);
|
LUAMOD_API int luaopen_mapapi(lua_State* L);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,88 +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 "region_api.hpp"
|
|
||||||
|
|
||||||
#include "region.hpp"
|
|
||||||
|
|
||||||
static int setTile(lua_State* L) {
|
|
||||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
|
||||||
ptr->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getTile(lua_State* L) {
|
|
||||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
|
||||||
int ret = ptr->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1);
|
|
||||||
lua_pushnumber(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getWidth(lua_State* L) {
|
|
||||||
lua_pushinteger(L, REGION_WIDTH);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getHeight(lua_State* L) {
|
|
||||||
lua_pushinteger(L, REGION_HEIGHT);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getDepth(lua_State* L) {
|
|
||||||
lua_pushinteger(L, REGION_DEPTH);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getX(lua_State* L) {
|
|
||||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
|
||||||
lua_pushinteger(L, ptr->GetX());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getY(lua_State* L) {
|
|
||||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
|
||||||
lua_pushinteger(L, ptr->GetY());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int dummy(lua_State* L) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const luaL_Reg regionlib[] = {
|
|
||||||
{"SetTile",setTile},
|
|
||||||
{"GetTile",getTile},
|
|
||||||
{"GetWidth",getWidth},
|
|
||||||
{"GetHeight",getHeight},
|
|
||||||
{"GetDepth",getDepth},
|
|
||||||
{"GetX",getX},
|
|
||||||
{"GetY",getY},
|
|
||||||
{"Create", dummy},
|
|
||||||
{"Unload", dummy},
|
|
||||||
{"Load", dummy},
|
|
||||||
{"Save", dummy},
|
|
||||||
{nullptr, nullptr}
|
|
||||||
};
|
|
||||||
|
|
||||||
LUAMOD_API int luaopen_regionapi(lua_State* L) {
|
|
||||||
luaL_newlib(L, regionlib);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,29 +1,32 @@
|
|||||||
print("Lua script check OK (./rsc)")
|
print("Lua script check OK (./rsc)")
|
||||||
|
|
||||||
function Region.Create(r)
|
function map.create(region)
|
||||||
-- print("Region:Create(r", Region.GetX(r), Region.GetY(r), ")")
|
for i = 1, map.getregionwidth() do
|
||||||
for i = 1, Region.GetWidth(r) do
|
for j = 1, map.getregionheight() do
|
||||||
for j = 1, Region.GetHeight(r) do
|
if math.abs(map.getx(region) + i -1) == math.abs(map.gety(region) + j -1) then
|
||||||
if math.abs(i) == math.abs(j) then
|
map.settile(region, i, j, 1, 50)
|
||||||
Region.SetTile(r, i, j, 1, 50)
|
|
||||||
else
|
else
|
||||||
Region.SetTile(r, i, j, 1, 14)
|
map.settile(region, i, j, 1, 14)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- print("done")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Region.Unload(r)
|
function map.unload(region)
|
||||||
-- print("Region:Unload(r", Region.GetX(r), Region.GetY(r), ")")
|
--
|
||||||
end
|
end
|
||||||
|
|
||||||
--return true if file loaded, otherwise return false
|
--return true if file loaded, otherwise return false
|
||||||
function Region.Load(r, saveDir)
|
function map.load(region, dir)
|
||||||
-- print("Region:Load(r,", saveDir, Region.GetX(r), Region.GetY(r), ")")
|
--
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function Region.Save(r, saveDir)
|
function map.save(region, dir)
|
||||||
-- print("Region:Save(r,", saveDir, Region.GetX(r), Region.GetY(r), ")")
|
--
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--debugging
|
||||||
|
print("DEBUG: Initial tile value: ", map.gettile(0, 0, 0))
|
||||||
|
map.settile(0, 0, 0, 86)
|
||||||
|
map.settile(10, 10, 1, 156)
|
||||||
|
|||||||
+33
-13
@@ -38,6 +38,10 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
//initial setup
|
//initial setup
|
||||||
config.Load("rsc\\config.cfg");
|
config.Load("rsc\\config.cfg");
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Initialize the APIs
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
//Init SDL
|
//Init SDL
|
||||||
if (SDL_Init(0)) {
|
if (SDL_Init(0)) {
|
||||||
throw(std::runtime_error("Failed to initialize SDL"));
|
throw(std::runtime_error("Failed to initialize SDL"));
|
||||||
@@ -58,13 +62,7 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
std::cout << "Initialized SQL" << std::endl;
|
std::cout << "Initialized SQL" << std::endl;
|
||||||
|
|
||||||
//setup the database
|
//Init lua
|
||||||
if (runSQLScript(database, config["dir.scripts"] + "setup_server.sql")) {
|
|
||||||
throw(std::runtime_error("Failed to initialize SQL's setup script"));
|
|
||||||
}
|
|
||||||
std::cout << "Initialized SQL's setup script" << std::endl;
|
|
||||||
|
|
||||||
//lua
|
|
||||||
luaState = luaL_newstate();
|
luaState = luaL_newstate();
|
||||||
if (!luaState) {
|
if (!luaState) {
|
||||||
throw(std::runtime_error("Failed to initialize lua"));
|
throw(std::runtime_error("Failed to initialize lua"));
|
||||||
@@ -72,19 +70,41 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
luaL_openlibs(luaState);
|
luaL_openlibs(luaState);
|
||||||
std::cout << "Initialized lua" << std::endl;
|
std::cout << "Initialized lua" << std::endl;
|
||||||
|
|
||||||
//run the startup script
|
//-------------------------
|
||||||
if (luaL_dofile(luaState, (config["dir.scripts"] + "setup_server.lua").c_str())) {
|
//Setup the objects
|
||||||
throw(std::runtime_error(std::string() + "Failed to initialize lua's setup script: " + lua_tostring(luaState, -1) ));
|
//-------------------------
|
||||||
}
|
|
||||||
std::cout << "Initialized lua's setup script" << std::endl;
|
|
||||||
|
|
||||||
//setup the map object
|
//setup the map object
|
||||||
regionPager.GetAllocator()->SetLuaState(luaState);
|
regionPager.GetAllocator()->SetLuaState(luaState);
|
||||||
regionPager.GetFormat()->SetLuaState(luaState);
|
regionPager.GetFormat()->SetLuaState(luaState);
|
||||||
//TODO: config parameter
|
//TODO: config parameter
|
||||||
regionPager.GetFormat()->SetSaveDir("save/mapname/");
|
regionPager.GetFormat()->SetSaveDir("save/mapname/");
|
||||||
|
std::cout << "Prepared the map system" << std::endl;
|
||||||
|
|
||||||
std::cout << "Initialized the map system" << std::endl;
|
//push the pager onto the lua registry
|
||||||
|
lua_pushstring(luaState, "pager");
|
||||||
|
lua_pushlightuserdata(luaState, reinterpret_cast<void*>(®ionPager));
|
||||||
|
lua_settable(luaState, LUA_REGISTRYINDEX);
|
||||||
|
std::cout << "Registered the map system in lua" << std::endl;
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Run the startup scripts
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
//setup the database
|
||||||
|
if (runSQLScript(database, config["dir.scripts"] + "setup_server.sql")) {
|
||||||
|
throw(std::runtime_error("Failed to initialize SQL's setup script"));
|
||||||
|
}
|
||||||
|
std::cout << "Completed SQL's setup script" << std::endl;
|
||||||
|
|
||||||
|
//run lua's startup script
|
||||||
|
if (luaL_dofile(luaState, (config["dir.scripts"] + "setup_server.lua").c_str())) {
|
||||||
|
throw(std::runtime_error(std::string() + "Failed to initialize lua's setup script: " + lua_tostring(luaState, -1) ));
|
||||||
|
}
|
||||||
|
std::cout << "Completed lua's setup script" << std::endl;
|
||||||
|
|
||||||
|
//debug output
|
||||||
|
std::cout << "Internal sizes:" << std::endl;
|
||||||
std::cout << "\tsizeof(SerialPacket): " << sizeof(SerialPacket) << std::endl;
|
std::cout << "\tsizeof(SerialPacket): " << sizeof(SerialPacket) << std::endl;
|
||||||
std::cout << "\tPACKET_BUFFER_SIZE: " << PACKET_BUFFER_SIZE << std::endl;
|
std::cout << "\tPACKET_BUFFER_SIZE: " << PACKET_BUFFER_SIZE << std::endl;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user