Changed map API to region API

This commit is contained in:
Kayne Ruse
2014-06-08 19:30:03 +10:00
parent b269ce5fb9
commit ba83fac29f
3 changed files with 16 additions and 60 deletions
+2 -2
View File
@@ -20,7 +20,7 @@
#define LUA_LIB
#include "lua/lua.hpp"
#include "map_api.hpp"
#include "region_api.hpp"
/*
@@ -41,7 +41,7 @@ static const luaL_Reg loadedlibs[] = {
{LUA_DBLIBNAME, luaopen_debug},
/* custom libs */
{LUA_MAPLIBNAME, luaopen_mapapi},
{LUA_REGIONLIBNAME, luaopen_regionapi},
{NULL, NULL}
};
@@ -19,58 +19,22 @@
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "map_api.hpp"
#include "region_api.hpp"
//map headers
#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.
//TODO: enforce all possible parameter counts
//TODO: update the map API to handle multiple rooms
#include "region.hpp"
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* pager = reinterpret_cast<RegionPager*>(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));
}
//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));
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* pager = reinterpret_cast<RegionPager*>(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);
}
//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);
return 1;
}
@@ -101,15 +65,7 @@ static int getRegionDepth(lua_State* L) {
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},
@@ -120,7 +76,7 @@ static const luaL_Reg regionlib[] = {
{nullptr, nullptr}
};
LUAMOD_API int luaopen_mapapi(lua_State* L) {
LUAMOD_API int luaopen_regionapi(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
* distribution.
*/
#ifndef MAPAPI_HPP_
#define MAPAPI_HPP_
#ifndef REGIONAPI_HPP_
#define REGIONAPI_HPP_
#include "lua/lua.hpp"
#define LUA_MAPLIBNAME "map"
LUAMOD_API int luaopen_mapapi(lua_State* L);
#define LUA_REGIONLIBNAME "region"
LUAMOD_API int luaopen_regionapi(lua_State* L);
#endif