diff --git a/scripts/algorithms/cellular_auotmata.lua b/scripts/algorithms/cellular_auotmata.lua index 4c9807d..5eb8fdf 100644 --- a/scripts/algorithms/cellular_auotmata.lua +++ b/scripts/algorithms/cellular_auotmata.lua @@ -1,11 +1,83 @@ -CellularAutomata = {} +CellularAutomata = { + map_w = 0, + map_h = 0, + iterations = 0, +} + +function CellularAutomata.get_neighbors(map, x, y) + local min_x = math.max(0, x-1) + local min_y = math.max(0, y-1) + local max_x = math.min(x+1, map:get_width()-1) + local max_y = math.min(y+1, map:get_height()-1) + local result = {} + for mx = min_x, max_x do + for my = min_y, max_y do + table.insert(result, {x = mx, y = my}) + end + end + return result +end function CellularAutomata:initialize(w, h) - + self.map_w = w + self.map_h = h + self.iterations = 3 + math.randomseed() + for mx = 0, w-1 do + for my = 0, h-1 do + local is_wall = math.random(0, 1) == 1 + if is_wall then + visualizer.map:draw_wall(mx, my) + else + visualizer.map:draw_floor(mx, my) + end + end + end end function CellularAutomata:update() - return true + if self.iterations <= 0 then + return true + end + + local old_map = visualizer.map + local new_map = old_map:clone() + + for mx = 0, self.map_w - 1 do + for my = 0, self.map_h - 1 do + local neighbors = self.get_neighbors(old_map, mx, my) + local walls = 0 + local required_walls = 5 + --print(old_map:is_wall(mx, my)) + --print(new_map:is_wall(mx, my)) + for k,v in ipairs(neighbors) do + if old_map:is_wall(v.x, v.y) then + walls = walls + 1 + end + end + if walls >= required_walls then + new_map:draw_wall(mx, my) + else + new_map:draw_floor(mx, my) + end + --print(old_map:is_wall(mx, my)) + --print(new_map:is_wall(mx, my)) + end + end + + -- Now draw the new map onto the existing one + for mx = 0, self.map_w - 1 do + for my = 0, self.map_h - 1 do + if new_map:is_wall(mx, my) then + old_map:draw_wall(mx, my) + else + old_map:draw_floor(mx, my) + end + end + end + + self.iterations = self.iterations - 1 + return false end -visualizer.algorithm_manager:register_algorithm("Cellular Automata", function (w, h) CellularAutomata:initialize() end, function () return CellularAutomata:update() end, 1) \ No newline at end of file +visualizer.algorithm_manager:register_algorithm("Cellular Automata", function (w, h) CellularAutomata:initialize(w, h) end, function () return CellularAutomata:update() end, 1) \ No newline at end of file diff --git a/scripts/algorithms/drunk_walk.lua b/scripts/algorithms/drunk_walk.lua index 77f6a05..11ed5bd 100644 --- a/scripts/algorithms/drunk_walk.lua +++ b/scripts/algorithms/drunk_walk.lua @@ -1,4 +1,3 @@ -require "math" DrunkenWalk = { curr_x = 0, curr_y = 0, @@ -8,9 +7,10 @@ DrunkenWalk = { } function DrunkenWalk:initialize(w, h) - math.randomseed(os.time()) + math.randomseed() self.map_w = w self.map_h = h + self.iterations = 30 visualizer.map:fill(true) self.curr_x = math.random(0, w-1) self.curr_y = math.random(0, h-1); diff --git a/src/engine/scripting/luacontextmanager.cpp b/src/engine/scripting/luacontextmanager.cpp index 9545eef..a683bb0 100644 --- a/src/engine/scripting/luacontextmanager.cpp +++ b/src/engine/scripting/luacontextmanager.cpp @@ -11,6 +11,6 @@ std::string LuaContextManager::get_key(std::string args) { // TODO: Switch the arg to a struct and use that to init libraries std::shared_ptr LuaContextManager::load_resource(std::string args) { auto lua = std::make_shared(); - lua->open_libraries(sol::lib::base, sol::lib::package, sol::lib::math, sol::lib::os); + lua->open_libraries(sol::lib::base, sol::lib::package, sol::lib::math, sol::lib::table); return lua; } diff --git a/src/game/tilemap.cpp b/src/game/tilemap.cpp index 593b3e2..7518584 100644 --- a/src/game/tilemap.cpp +++ b/src/game/tilemap.cpp @@ -45,3 +45,7 @@ int TileMap::get_width() { int TileMap::get_height() { return grid.get_height(); } + +TileMap TileMap::clone() { + return {this->get_width(), this->get_height(), this->wall_tile, this->floor_tile}; +} diff --git a/src/game/tilemap.hpp b/src/game/tilemap.hpp index de01ed1..a3eeaa3 100644 --- a/src/game/tilemap.hpp +++ b/src/game/tilemap.hpp @@ -21,6 +21,7 @@ public: bool is_wall(int x, int y); int get_width(); int get_height(); + TileMap clone(); TileMap(int width, int height, char wall_tile, char floor_tile) { this->grid = Grid2D(width, height); diff --git a/src/game/visualizer.cpp b/src/game/visualizer.cpp index e2fd3fb..4994127 100644 --- a/src/game/visualizer.cpp +++ b/src/game/visualizer.cpp @@ -109,6 +109,9 @@ void Visualizer::initialize_context() { tilemap_lua["draw_wall"] = &TileMap::draw_wall; tilemap_lua["fill"] = &TileMap::fill; tilemap_lua["is_wall"] = &TileMap::is_wall; + tilemap_lua["clone"] = &TileMap::clone; + tilemap_lua["get_width"] = &TileMap::get_width; + tilemap_lua["get_height"] = &TileMap::get_height; auto algo_manager_lua = lua->new_usertype("AlgorithmManager", sol::no_constructor); algo_manager_lua["register_algorithm"] = &AlgorithmManager::register_algorithm;