From 95362286f8d8e70f936eeb5fd28fc5f864f69afa Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 23 Jun 2014 10:29:39 +1000 Subject: [PATCH] Committing the island generator script, and a bugfix region_api.cpp had a bug, where a glue function's name was used twice. It was an easy catch, but there was an issue in the new script, where I was counting from 0 instead of 1. As a result, I was chasing a segfault for 5 hours. --- common/map/region_api.cpp | 2 +- common/map/region_pager_lua.cpp | 13 ++++++----- makefile | 1 + rsc/scripts/setup_server.lua | 41 +++++++++++++++++++++++++++++++++ server/rooms/room_manager.cpp | 5 ++-- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp index 98f5ee1..fdff4f7 100644 --- a/common/map/region_api.cpp +++ b/common/map/region_api.cpp @@ -87,7 +87,7 @@ static int onUnload(lua_State* L) { static const luaL_Reg regionLib[] = { {"SetTile",setTile}, - {"SetTile",getTile}, + {"GetTile",getTile}, {"GetX",getX}, {"GetY",getY}, {"GetWidth",getWidth}, diff --git a/common/map/region_pager_lua.cpp b/common/map/region_pager_lua.cpp index a402f27..85ea167 100644 --- a/common/map/region_pager_lua.cpp +++ b/common/map/region_pager_lua.cpp @@ -29,12 +29,12 @@ Region* RegionPagerLua::LoadRegion(int x, int y) { //load the region if possible //something to work on - regionList.emplace_front(x, y); + Region tmpRegion(x, y); //API hook lua_getglobal(luaState, "Region"); lua_getfield(luaState, -1, "OnLoad"); - lua_pushlightuserdata(luaState, ®ionList.front()); + lua_pushlightuserdata(luaState, &tmpRegion); lua_pushstring(luaState, directory.c_str()); if (lua_pcall(luaState, 2, 1, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); @@ -42,10 +42,10 @@ Region* RegionPagerLua::LoadRegion(int x, int y) { //success or failure if (!lua_toboolean(luaState, -1)) { lua_pop(luaState, 2); - regionList.pop_front(); return nullptr; } lua_pop(luaState, 2); + regionList.push_front(tmpRegion); return ®ionList.front(); } @@ -72,18 +72,19 @@ Region* RegionPagerLua::CreateRegion(int x, int y) { } //something to work on - regionList.emplace_front(x, y); + Region tmpRegion(x, y); //API hook lua_getglobal(luaState, "Region"); lua_getfield(luaState, -1, "OnCreate"); - lua_pushlightuserdata(luaState, ®ionList.front()); + lua_pushlightuserdata(luaState, &tmpRegion); //TODO: parameters if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); } lua_pop(luaState, 1); - return ®ionList.front();; + regionList.push_front(tmpRegion); + return ®ionList.front(); } void RegionPagerLua::UnloadRegion(int x, int y) { diff --git a/makefile b/makefile index 080a22d..56e39a3 100644 --- a/makefile +++ b/makefile @@ -3,6 +3,7 @@ #MKDIR=mkdir #RM=del /y +#CXXFLAGS+=-static-libgcc -static-libstdc++ -g -fno-inline-functions -Wall CXXFLAGS+=-static-libgcc -static-libstdc++ export diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 3511fa9..6bc65f0 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1 +1,42 @@ print("Lua script check (./rsc)") + +--uber lazy declarations +function square(x) return x*x end +function distance(x, y, i, j) return math.sqrt(square(x - i) + square(y - j)) end + +--tile macros, mapped to the tilesheet +local base = 14 +local shift = 36 +plains = base + shift * 0 +grass = base + shift * 1 +dirt = base + shift * 2 +sand = base + shift * 3 +water = base + shift * 4 + +--Overwrite the original OnCreate with my own version +Region.hcOnCreate = Region.OnCreate +Region.OnCreate = function(region) + local ret = Region.hcOnCreate(region) --best practices + for i = 1, Region.GetWidth() do + for j = 1, Region.GetHeight() do + if distance(0, 0, i + Region.GetX(region) -1, j + Region.GetY(region) -1) > 10 then + Region.SetTile(region, i, j, 1, water) + else + Region.SetTile(region, i, j, 1, plains) + end + end + end + return ret +end + +--Get some regions +newRoom = RoomMgr.CreateRoom("overworld") +pager = Room.GetPager(newRoom) +regionTable = { + RegionPager.GetRegion(pager, 0, 0), + RegionPager.GetRegion(pager, 0, -20), + RegionPager.GetRegion(pager, -20, 0), + RegionPager.GetRegion(pager, -20, -20) +} + +print("Finished the lua script") \ No newline at end of file diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 637513e..3e4ec87 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -41,6 +41,8 @@ RoomData* RoomManager::CreateRoom(MapType mapType) { //create the generator, use a lambda because I'm lazy newRoom->generator = [mapType]() -> BaseGenerator* { switch(mapType) { + //BUG: Not having a map type results in an overworld generator by default + case MapType::NONE: case MapType::OVERWORLD: return new OverworldGenerator(); case MapType::RUINS: return new RuinsGenerator(); case MapType::TOWERS: return new TowersGenerator(); @@ -97,8 +99,7 @@ void RoomManager::UnloadRoom(int uid) { RoomData* RoomManager::GetRoom(int uid) { RoomData* ptr = FindRoom(uid); if (ptr) return ptr; - //TODO: proper Get() method - return nullptr; + return CreateRoom(MapType::NONE); } RoomData* RoomManager::FindRoom(int uid) {