Skip to content

Commit

Permalink
Support [road_cost] in [passage]
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Oct 24, 2018
1 parent e5fa9d6 commit faf9a9d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 6 deletions.
61 changes: 56 additions & 5 deletions data/lua/cave_map_generator.lua
Expand Up @@ -20,7 +20,7 @@ function callbacks.generate_map(params)
end
end

local function clear_tile(x, y)
local function clear_tile(x, y, terrain_clear)
if not map:on_board(x,y) then
return
end
Expand All @@ -31,7 +31,39 @@ function callbacks.generate_map(params)
if r <= params.village_density then
map:set_tile(x, y, helper.rand(params.terrain_village))
else
map:set_tile(x, y, helper.rand(params.terrain_clear))
map:set_tile(x, y, helper.rand(terrain_clear or params.terrain_clear))
end
end

local function place_road(to_x, to_y, from_x, from_y, road_ops, terrain_clear)
if not map:on_board(to_x, to_y) then
return
end
if map:get_tile(to_x, to_y) == params.terrain_castle or map:get_tile(to_x, to_y) == params.terrain_keep then
return
end
local tile_op = road_ops[map:get_tile(to_x, to_y)]
if tile_op then
if tile_op.convert_to_bridge and from_x and from_y then
local bridges = {}
for elem in tile_op.convert_to_bridge:gmatch("[^%s,][^,]*") do
table.insert(bridges, elem)
end
local dir = wesnoth.map_location.get_relative_dir(from_x, from_y, to_x, to_y)
if dir == 'n' or dir == 's' then
map:set_tile(to_x, to_y, bridges[1])
elseif dir == 'sw' or dir == 'ne' then
map:set_tile(to_x, to_y, bridges[2])
elseif dir == 'se' or dir == 'nw' then
map:set_tile(to_x, to_y, bridges[3])
end
elseif tile_op.convert_to then
local tile = helper.rand(tile_op.convert_to)
map:set_tile(to_x, to_y, tile)
end
else
local tile = helper.rand(terrain_clear or params.terrain_clear)
map:set_tile(to_x, to_y, tile)
end
end

Expand Down Expand Up @@ -67,17 +99,25 @@ function callbacks.generate_map(params)
locs_set = locs_set,
id = id,
items = items,
data = chamber,
})
chambers_by_id[id] = chambers[#chambers]
for passage in wml.child_range(chamber, "passage") do
local dst = chambers_by_id[passage.destination]
if dst ~= nil then
local road_costs, road_ops = {}, {}
for road in helper.child_range(passage, "road_cost") do
road_costs[road.terrain] = road.cost
road_ops[road.terrain] = road
end
table.insert(passages, {
start_x = x,
start_y = y,
dest_x = dst.center_x,
dest_y = dst.center_y,
data = passage,
costs = road_costs,
roads = road_ops,
})
end
end
Expand All @@ -87,7 +127,7 @@ function callbacks.generate_map(params)
for i,v in ipairs(chambers) do
local locs_list = {}
for x, y in v.locs_set:stable_iter() do
clear_tile(x, y)
clear_tile(x, y, v.data.terrain_clear)
if map:on_inner_board(x, y) then
table.insert(locs_list, {x,y})
end
Expand Down Expand Up @@ -119,8 +159,11 @@ function callbacks.generate_map(params)
local jagged = v.data.jagged or 0
local calc = function(x, y)
local res = 1.0
if map:get_tile(x, y) == params.terrain_wall then
local tile = map:get_tile(x, y)
if tile == params.terrain_wall then
res = laziness
else
res = v.costs[tile] or 1.0
end
if windiness > 1 then
res = res * random(windiness)
Expand All @@ -132,8 +175,16 @@ function callbacks.generate_map(params)
for i, loc in ipairs(path) do
local locs_set = LS.create()
build_chamber(loc[1], loc[2], locs_set, width, jagged)
local prev_x, prev_y
for x,y in locs_set:stable_iter() do
clear_tile(x, y)
local r = 1000
if v.data.place_villages then r = random(1000) end
if r <= params.village_density then
place_road(x, y, prev_x, prev_y, v.roads, params.terrain_village)
else
place_road(x, y, prev_x, prev_y, v.roads, v.data.terrain_clear)
end
prev_x, prev_y = x, y
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion data/lua/mapgen_helper.lua
Expand Up @@ -106,7 +106,7 @@ function map_mt.__tostring(map)
for y = 0, map.h - 1 do
local string_builder = {}
for x = 0, map.w - 1 do
local tile_string = map:get_tile(x, y)
local tile_string = map:get_tile(x, y) or 'Xv'
if map.locations and map.locations:get(x,y) then
for i,v in ipairs(map.locations:get(x,y)) do
tile_string = v .. ' ' .. tile_string
Expand Down
51 changes: 51 additions & 0 deletions data/multiplayer/scenarios/Random_Scenario_Cave.cfg
Expand Up @@ -2,6 +2,40 @@

#ifdef DEBUG

#define ROAD_COSTS
[road_cost]
terrain=Rb
cost=1
convert_to=Ur
[/road_cost]
[road_cost]
terrain=Rb^Uf
cost=5
convert_to=Ur
[/road_cost]
[road_cost]
terrain=Uue
cost=3
convert_to=Ur
[/road_cost]
[road_cost]
terrain=Rb^Ii
cost=1
convert_to=Ur^Ii
[/road_cost]
[road_cost]
terrain=Uue
cost=10
convert_to=Ur
[/road_cost]
[road_cost]
terrain=Wwg
cost=50
convert_to_bridge=Wwg^Bw|,Wwg^Bw/,Wwg^Bw\
convert_to=Ur
[/road_cost]
#enddef

#define PLAYER_CHAMBER NUMBER X Y
[chamber]
id=player_{NUMBER}
Expand All @@ -24,7 +58,16 @@
windiness=3
laziness=2
jagged=3
width=3
terrain_clear=Rb
place_villages=yes
[/passage]
[passage]
destination=central_chamber
windiness=2
jagged=2
width=2
{ROAD_COSTS}
[/passage]
[/chamber]
#enddef
Expand Down Expand Up @@ -65,6 +108,14 @@
size=20
jagged=30
[/chamber]
[chamber]
id=lake
x=14..26
y=14..26
size=5
jagged=12
terrain_clear=Wwg
[/chamber]
{PLAYER_CHAMBER 1 3..10 3..10}
{PLAYER_CHAMBER 2 30..37 30..37}
{PLAYER_CHAMBER 3 3..10 30..37}
Expand Down

0 comments on commit faf9a9d

Please sign in to comment.