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.
This commit is contained in:
Kayne Ruse
2014-06-23 10:29:39 +10:00
parent f5c58bf5ad
commit 95362286f8
5 changed files with 53 additions and 9 deletions
+1 -1
View File
@@ -87,7 +87,7 @@ static int onUnload(lua_State* L) {
static const luaL_Reg regionLib[] = { static const luaL_Reg regionLib[] = {
{"SetTile",setTile}, {"SetTile",setTile},
{"SetTile",getTile}, {"GetTile",getTile},
{"GetX",getX}, {"GetX",getX},
{"GetY",getY}, {"GetY",getY},
{"GetWidth",getWidth}, {"GetWidth",getWidth},
+7 -6
View File
@@ -29,12 +29,12 @@ Region* RegionPagerLua::LoadRegion(int x, int y) {
//load the region if possible //load the region if possible
//something to work on //something to work on
regionList.emplace_front(x, y); Region tmpRegion(x, y);
//API hook //API hook
lua_getglobal(luaState, "Region"); lua_getglobal(luaState, "Region");
lua_getfield(luaState, -1, "OnLoad"); lua_getfield(luaState, -1, "OnLoad");
lua_pushlightuserdata(luaState, &regionList.front()); lua_pushlightuserdata(luaState, &tmpRegion);
lua_pushstring(luaState, directory.c_str()); lua_pushstring(luaState, directory.c_str());
if (lua_pcall(luaState, 2, 1, 0) != LUA_OK) { if (lua_pcall(luaState, 2, 1, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); 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 //success or failure
if (!lua_toboolean(luaState, -1)) { if (!lua_toboolean(luaState, -1)) {
lua_pop(luaState, 2); lua_pop(luaState, 2);
regionList.pop_front();
return nullptr; return nullptr;
} }
lua_pop(luaState, 2); lua_pop(luaState, 2);
regionList.push_front(tmpRegion);
return &regionList.front(); return &regionList.front();
} }
@@ -72,18 +72,19 @@ Region* RegionPagerLua::CreateRegion(int x, int y) {
} }
//something to work on //something to work on
regionList.emplace_front(x, y); Region tmpRegion(x, y);
//API hook //API hook
lua_getglobal(luaState, "Region"); lua_getglobal(luaState, "Region");
lua_getfield(luaState, -1, "OnCreate"); lua_getfield(luaState, -1, "OnCreate");
lua_pushlightuserdata(luaState, &regionList.front()); lua_pushlightuserdata(luaState, &tmpRegion);
//TODO: parameters //TODO: parameters
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
lua_pop(luaState, 1); lua_pop(luaState, 1);
return &regionList.front();; regionList.push_front(tmpRegion);
return &regionList.front();
} }
void RegionPagerLua::UnloadRegion(int x, int y) { void RegionPagerLua::UnloadRegion(int x, int y) {
+1
View File
@@ -3,6 +3,7 @@
#MKDIR=mkdir #MKDIR=mkdir
#RM=del /y #RM=del /y
#CXXFLAGS+=-static-libgcc -static-libstdc++ -g -fno-inline-functions -Wall
CXXFLAGS+=-static-libgcc -static-libstdc++ CXXFLAGS+=-static-libgcc -static-libstdc++
export export
+41
View File
@@ -1 +1,42 @@
print("Lua script check (./rsc)") 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")
+3 -2
View File
@@ -41,6 +41,8 @@ RoomData* RoomManager::CreateRoom(MapType mapType) {
//create the generator, use a lambda because I'm lazy //create the generator, use a lambda because I'm lazy
newRoom->generator = [mapType]() -> BaseGenerator* { newRoom->generator = [mapType]() -> BaseGenerator* {
switch(mapType) { 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::OVERWORLD: return new OverworldGenerator();
case MapType::RUINS: return new RuinsGenerator(); case MapType::RUINS: return new RuinsGenerator();
case MapType::TOWERS: return new TowersGenerator(); case MapType::TOWERS: return new TowersGenerator();
@@ -97,8 +99,7 @@ void RoomManager::UnloadRoom(int uid) {
RoomData* RoomManager::GetRoom(int uid) { RoomData* RoomManager::GetRoom(int uid) {
RoomData* ptr = FindRoom(uid); RoomData* ptr = FindRoom(uid);
if (ptr) return ptr; if (ptr) return ptr;
//TODO: proper Get() method return CreateRoom(MapType::NONE);
return nullptr;
} }
RoomData* RoomManager::FindRoom(int uid) { RoomData* RoomManager::FindRoom(int uid) {