From be1b23ddfb4ed6798bb79dd7de37d0377ae06f76 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 20 Jul 2015 17:34:19 +1000 Subject: [PATCH] Wrote a simple dungeon generator --- rsc/roguegenerator.lua | 112 ++++++++++++++++++++++++++++++++++++++ rsc/startup.lua | 8 +++ src/example_scene.cpp | 8 +++ src/libmap/tile_sheet.cpp | 2 +- src/linit.cpp | 1 - 5 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 rsc/roguegenerator.lua diff --git a/rsc/roguegenerator.lua b/rsc/roguegenerator.lua new file mode 100644 index 0000000..2526289 --- /dev/null +++ b/rsc/roguegenerator.lua @@ -0,0 +1,112 @@ +--generate a roguelike map + +--[[ DOCS: Game design theory, read more + +As a basic starting point, I can create a list of "seed" tiles, which are the +center of rooms. The rooms can be any size, as long as they don't overlap. From +There, a tunnel can be "carved" from each seed tile to the next. Finally, the +map is populated, but that is beyond this exercise. + +This specification isn't ironclad. + +--]] + +print("Beginning generator...") + +math.randomseed(os.time()) + +roomcount = math.random(5, 10) +roomlist = {} +tunnellist = {} + +function newroom() + --place the room within proximity of the center, so that they're not too far apart. + local x = math.random(-30, 30) + local y = math.random(-30, 30) + local w = math.random(2, 5) + local h = math.random(2, 5) + + --return this new room + return { + seedX = x, + seedY = y, + --give it a decent size + north = y - h, + south = y + h, + east = x - w, + west = x + w, + } +end + +function newpath(x1, y1, x2, y2) + --NOTE: a path is an ordered list of {x, y} pairs + local path = {} + local step = 0 + + --vertical + if y1 > y2 then + step = -1 + else + step = 1 + end + for i = y1, y2, step do + table.insert(path, {x = x1, y = i}) + end + + --horizontal + if x1 > x2 then + step = -1 + else + step = 1 + end + for i = x1, x2, step do + table.insert(path, {x = i, y = y2}) + end + + --NOTE: {x, y} pairs are duplicated at the corners + --TODO: improve the pathing system + return path +end + +print("populating lists") + +--populate the roomlist +for i = 1, roomcount do + table.insert(roomlist, newroom()) +end + +--tunnel the shortest paths +for i = 1, roomcount-1 do + table.insert(tunnellist, newpath( + roomlist[i].seedX, + roomlist[i].seedY, + roomlist[i+1].seedX, + roomlist[i+1].seedY)) +end + +--pass the data onto the pager +pager = ... --called as a chunk + +--create the rooms +for k, iter in next, roomlist do + --for each tile in the room + for i = iter.east, iter.west do + for j = iter.north, iter.south do + --set + local ret = region_pager.SetTile(pager, i, j, 0, 14) +-- print("ret: ", ret) + end + end +end + +--create the paths +iter = nil +for k, path in next, tunnellist do --multiple paths in the tunnellsit + for k, iter in next, path do + --for each tile in the path, set + local ret = region_pager.SetTile(pager, iter.x, iter.y, 1, 50) --DEBUG: set to layer 1 +-- print("ret: ", ret) + end +end + +print("generator finished") \ No newline at end of file diff --git a/rsc/startup.lua b/rsc/startup.lua index 3ec0d74..713ef7f 100644 --- a/rsc/startup.lua +++ b/rsc/startup.lua @@ -24,4 +24,12 @@ end) --]] +generator, msg = loadfile("rsc/roguegenerator.lua") + +if generator == nil then + print("error: ", msg) +else + generator(pager) +end + print("Finished startup script") \ No newline at end of file diff --git a/src/example_scene.cpp b/src/example_scene.cpp index 3a52aee..3b45064 100644 --- a/src/example_scene.cpp +++ b/src/example_scene.cpp @@ -139,6 +139,14 @@ void ExampleScene::KeyDown(SDL_KeyboardEvent const& event) { case SDLK_ESCAPE: QuitEvent(); break; + + case SDLK_r: + SetSceneSignal(SceneSignal::EXAMPLE_SCENE); + break; + + case SDLK_SPACE: + camera.scale = 1.0; + break; } } diff --git a/src/libmap/tile_sheet.cpp b/src/libmap/tile_sheet.cpp index 2b458ca..bd18d0a 100644 --- a/src/libmap/tile_sheet.cpp +++ b/src/libmap/tile_sheet.cpp @@ -110,7 +110,7 @@ void TileSheet::DrawRegionTo(SDL_Renderer* const renderer, Region* const region, //set the sclip sclip.x = (tile-1) % countX * clip.h; - sclip.x = (tile-1) / countX * clip.w; + sclip.y = (tile-1) / countX * clip.w; //set the dclip dclip.x = ((region->x + i) * clip.w - camX) * scaleX; diff --git a/src/linit.cpp b/src/linit.cpp index bf54db3..1c78578 100644 --- a/src/linit.cpp +++ b/src/linit.cpp @@ -48,7 +48,6 @@ static const luaL_Reg preloadedlibs[] = { {NULL, NULL} }; - LUALIB_API void luaL_openlibs (lua_State *L) { const luaL_Reg *lib; /* call open functions from 'loadedlibs' and set results to global table */