Merge pull request 'Implements cellular automata, closes #13' (#16) from feature/rla-13_CellularAutomata into master
Reviewed-on: #16
This commit was merged in pull request #16.
This commit is contained in:
@@ -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)
|
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
|
end
|
||||||
|
|
||||||
function CellularAutomata:update()
|
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
|
end
|
||||||
|
|
||||||
visualizer.algorithm_manager:register_algorithm("Cellular Automata", function (w, h) CellularAutomata:initialize() end, function () return CellularAutomata:update() end, 1)
|
visualizer.algorithm_manager:register_algorithm("Cellular Automata", function (w, h) CellularAutomata:initialize(w, h) end, function () return CellularAutomata:update() end, 1)
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
require "math"
|
|
||||||
DrunkenWalk = {
|
DrunkenWalk = {
|
||||||
curr_x = 0,
|
curr_x = 0,
|
||||||
curr_y = 0,
|
curr_y = 0,
|
||||||
@@ -8,9 +7,10 @@ DrunkenWalk = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function DrunkenWalk:initialize(w, h)
|
function DrunkenWalk:initialize(w, h)
|
||||||
math.randomseed(os.time())
|
math.randomseed()
|
||||||
self.map_w = w
|
self.map_w = w
|
||||||
self.map_h = h
|
self.map_h = h
|
||||||
|
self.iterations = 30
|
||||||
visualizer.map:fill(true)
|
visualizer.map:fill(true)
|
||||||
self.curr_x = math.random(0, w-1)
|
self.curr_x = math.random(0, w-1)
|
||||||
self.curr_y = math.random(0, h-1);
|
self.curr_y = math.random(0, h-1);
|
||||||
|
|||||||
@@ -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
|
// TODO: Switch the arg to a struct and use that to init libraries
|
||||||
std::shared_ptr<sol::state> LuaContextManager::load_resource(std::string args) {
|
std::shared_ptr<sol::state> LuaContextManager::load_resource(std::string args) {
|
||||||
auto lua = std::make_shared<sol::state>();
|
auto lua = std::make_shared<sol::state>();
|
||||||
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;
|
return lua;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,3 +45,7 @@ int TileMap::get_width() {
|
|||||||
int TileMap::get_height() {
|
int TileMap::get_height() {
|
||||||
return grid.get_height();
|
return grid.get_height();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TileMap TileMap::clone() {
|
||||||
|
return {this->get_width(), this->get_height(), this->wall_tile, this->floor_tile};
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ public:
|
|||||||
bool is_wall(int x, int y);
|
bool is_wall(int x, int y);
|
||||||
int get_width();
|
int get_width();
|
||||||
int get_height();
|
int get_height();
|
||||||
|
TileMap clone();
|
||||||
|
|
||||||
TileMap(int width, int height, char wall_tile, char floor_tile) {
|
TileMap(int width, int height, char wall_tile, char floor_tile) {
|
||||||
this->grid = Grid2D<char>(width, height);
|
this->grid = Grid2D<char>(width, height);
|
||||||
|
|||||||
@@ -104,6 +104,9 @@ void Visualizer::initialize_context() {
|
|||||||
tilemap_lua["draw_wall"] = &TileMap::draw_wall;
|
tilemap_lua["draw_wall"] = &TileMap::draw_wall;
|
||||||
tilemap_lua["fill"] = &TileMap::fill;
|
tilemap_lua["fill"] = &TileMap::fill;
|
||||||
tilemap_lua["is_wall"] = &TileMap::is_wall;
|
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>("AlgorithmManager", sol::no_constructor);
|
auto algo_manager_lua = lua->new_usertype<AlgorithmManager>("AlgorithmManager", sol::no_constructor);
|
||||||
algo_manager_lua["register_algorithm"] = &AlgorithmManager::register_algorithm;
|
algo_manager_lua["register_algorithm"] = &AlgorithmManager::register_algorithm;
|
||||||
|
|||||||
Reference in New Issue
Block a user