Lua Cave Mapgen: Support random chance of flipping the map

This has a similar effect to the flipx_chance and flipy_chance in the old
cave generator, except only one flip will be applied.
This commit is contained in:
Celtic Minstrel 2016-10-21 01:43:14 -04:00
parent 7c715130d4
commit 510dfd65df
4 changed files with 39 additions and 2 deletions

View file

@ -12,7 +12,6 @@
map_width=50
map_height=70
flipx_chance=50
village_density=20
terrain_wall = "Xu"
terrain_clear = "Uu"
@ -20,6 +19,9 @@
terrain_castle = "Cud"
terrain_village = "Uu^Vud"
transforms=flip_x
transform_chance=50
#
# The chamber with the player. Somewhere in the far south
#

View file

@ -23,7 +23,6 @@
map_width=45
map_height=45
flipx_chance=0
village_density=60
terrain_wall = "Xu"

View file

@ -142,6 +142,22 @@ function callbacks.generate_map(params)
end
if type(params.transform) == "string" then
local chance = params.transform_chance or 100
if random(100) <= chance then
local transforms = {}
for t in params.transform:gmatch("[^%s,][^,]*") do
-- TODO: Possibly support other transformations, such as transpose, rotate_cw, rotate_ccw, etc
if t == 'flip_x' or t == 'flip_y' then
table.insert(transforms, t)
else
error("Unknown transformation '" .. t .. "'")
end
end
map[transforms[random(#transforms)]](map)
end
end
return tostring(map)
end

View file

@ -45,6 +45,26 @@ function map_mt.__index.add_location(map, x, y, name)
end
end
function map_mt.__index.flip_x(map)
for y = 0, map.h - 1 do
for x = 0, map.w - 1 do
local i = loc_to_index(map, x, y)
local j = loc_to_index(map, map.w - x, y)
map[i], map[j] = map[j], map[i]
end
end
end
function map_mt.__index.flip_y(map)
for x = 0, map.w - 1 do
for y = 0, map.h - 1 do
local i = loc_to_index(map, x, y)
local j = loc_to_index(map, x, map.h - y)
map[i], map[j] = map[j], map[i]
end
end
end
function map_mt.__tostring(map)
local map_builder = {}
-- The coordinates are 0-based to match the in-game coordinates