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:
@@ -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},
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user