Skip to content

Commit

Permalink
Implement a settings dialog for the sample cave map generator
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Oct 11, 2019
1 parent 80e2af2 commit a140266
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 0 deletions.
3 changes: 3 additions & 0 deletions data/lua/cave_map_generator.lua
Expand Up @@ -72,6 +72,7 @@ function callbacks.generate_map(params)
local passages = {}

for chamber in wml.child_range(params, "chamber") do
if chamber.ignore then goto continue end
local chance = tonumber(chamber.chance) or 100
local x, y = MG.random_location(chamber.x, chamber.y)
if chamber.relative_to == "top-right" then
Expand Down Expand Up @@ -124,6 +125,7 @@ function callbacks.generate_map(params)
})
chambers_by_id[id] = chambers[#chambers]
for passage in wml.child_range(chamber, "passage") do
if passage.ignore then goto continue end
local dst = chambers_by_id[passage.destination]
if dst ~= nil then
local road_costs, road_ops = {}, {}
Expand All @@ -141,6 +143,7 @@ function callbacks.generate_map(params)
roads = road_ops,
})
end
::continue::
end
::continue::
end
Expand Down
284 changes: 284 additions & 0 deletions data/multiplayer/scenarios/Random_Scenario_Cave.cfg
Expand Up @@ -117,6 +117,7 @@ Rb,Rb,Rb,Rb,Rb,Rb,Rb,Rb^Uf,Rb^Ii,Sm,Sm,Uue,Rb^Fetd,Rb^Fdw#enddef
place_villages=yes
[/passage]
[passage]
ignore=no
destination=central_chamber
windiness=2
jagged=2
Expand All @@ -126,6 +127,73 @@ Rb,Rb,Rb,Rb,Rb,Rb,Rb,Rb^Uf,Rb^Ii,Sm,Sm,Uue,Rb^Fetd,Rb^Fdw#enddef
[/chamber]
#enddef

#define MAP_OPTION_CONTROL ID LABEL TYPE WML
[row]
[column]
grow_factor=0
horizontal_grow=true
border=all
border_size=5
[label]
definition=default
label={LABEL}
text_alignment=right
[/label]
[/column]
[column]
grow_factor=1
horizontal_grow=true
border=all
border_size=10
[{TYPE}]
id={ID}
{WML}
[/{TYPE}]
[/column]
[column]
grow_factor=0
horizontal_grow=true
border=all
border_size=5
# This is basically the expansion of the GUI_FORCE_WIDGET_MINIMUM_SIZE macro
# That macro is not available here, as it's defined in the GUI2 scope
# whereas we're in the game config scope.
[stacked_widget]
definition=default
[layer]
[row]
grow_factor=1
[column]
grow_factor=1
horizontal_grow=true
vertical_grow=true
[spacer]
definition=default
width=100
height=0
[/spacer]
[/column]
[/row]
[/layer]
[layer]
[row]
grow_factor = 1
[column]
grow_factor = 1
horizontal_grow = true
vertical_grow = true
[label]
id={ID}_label
definition=default
[/label]
[/column]
[/row]
[/layer]
[/stacked_widget]
[/column]
[/row]
#enddef

[multiplayer]
# This id is currently hardcoded by the random map generator of the editor
id=multiplayer_Random_Map_Cave
Expand All @@ -138,6 +206,114 @@ Rb,Rb,Rb,Rb,Rb,Rb,Rb,Rb^Uf,Rb^Ii,Sm,Sm,Uue,Rb^Fetd,Rb^Fdw#enddef
create_scenario=<<
return wesnoth.require("lua/cave_map_generator.lua").generate_scenario(...)
>>
user_config=<<
local params = ...

local function players_changed()
params.nplayers = wesnoth.get_dialog_value("players")
wesnoth.set_dialog_value(params.nplayers, "players_label")
end

local function width_changed()
params.map_width = wesnoth.get_dialog_value("width")
wesnoth.set_dialog_value(params.map_width, "width_label")
end

local function height_changed()
params.map_height = wesnoth.get_dialog_value("height")
wesnoth.set_dialog_value(params.map_height, "height_label")
end

local function density_changed()
params.village_density = wesnoth.get_dialog_value("village_density")
-- Need wesnoth-lib for the village density label
local _ = wesnoth.textdomain "wesnoth-lib"
local label = wesnoth.format(_"$villages/1000 tiles", {villages = params.village_density})
wesnoth.set_dialog_value(label, "village_density_label")
end

local function jagged_changed()
-- We're setting the value, so wml.get_child and co won't work
-- However they do return the all-children index of the child
local __, i = wml.get_nth_child(params, "chamber", 1)
params[i][2].jagged = wesnoth.get_dialog_value("jagged")
wesnoth.set_dialog_value(params[i][2].jagged, "jagged_label")
end

local function lake_size_changed()
-- We're setting the value, so wml.get_child and co won't work
-- However they do return the all-children index of the child
local __, i = wml.get_nth_child(params, "chamber", 2)
params[i][2].size = wesnoth.get_dialog_value("lake_size")
wesnoth.set_dialog_value(params[i][2].size, "lake_size_label")
end

local function windiness_changed()
-- We're setting the value, so wml.get_child and co won't work
-- However they do return the all-children index of the child
local __, i = wml.get_nth_child(params, "chamber", 3)
local val = wesnoth.get_dialog_value("windiness")
for j = i, #params do
if params[i][1] == "chamber" then
local __, k = wml.get_nth_child(params[i][2], "passage", 1)
params[i][2][k][2].windiness = val
end
end
wesnoth.set_dialog_value(val, "windiness_label")
end

local function roads_toggled()
-- We're setting the value, so wml.get_child and co won't work
-- However they do return the all-children index of the child
local __, i = wml.get_nth_child(params, "chamber", 3)
local val = not wesnoth.get_dialog_value("roads")
for j = i, #params do
if params[i][1] == "chamber" then
local __, k = wml.get_nth_child(params[i][2], "passage", 2)
params[i][2][k][2].ignore = val
end
end
end

local function pre_show()
wesnoth.set_dialog_value(params.nplayers, "players")
wesnoth.set_dialog_value(params.map_width, "width")
wesnoth.set_dialog_value(params.map_height, "height")
wesnoth.set_dialog_value(params.village_density, "village_density")
wesnoth.set_dialog_value(wml.get_nth_child(params, "chamber", 1).jagged, "jagged")
wesnoth.set_dialog_value(wml.get_nth_child(params, "chamber", 2).size, "lake_size")

local first_player = wml.get_nth_child(params, "chamber", 3)
local windiness = wml.get_nth_child(first_player, "passage", 1).windiness
wesnoth.set_dialog_value(windiness, "windiness")

local roads = not wml.get_nth_child(first_player, "passage", 2).ignore
wesnoth.set_dialog_value(roads, "roads")

-- Callbacks...
wesnoth.set_dialog_callback(players_changed, "players")
wesnoth.set_dialog_callback(width_changed, "width")
wesnoth.set_dialog_callback(height_changed, "height")
wesnoth.set_dialog_callback(density_changed, "village_density")
wesnoth.set_dialog_callback(jagged_changed, "jagged")
wesnoth.set_dialog_callback(lake_size_changed, "lake_size")
wesnoth.set_dialog_callback(windiness_changed, "windiness")
wesnoth.set_dialog_callback(roads_toggled, "roads")

-- Init labels
players_changed()
width_changed()
height_changed()
density_changed()
jagged_changed()
lake_size_changed()
windiness_changed()
roads_toggled()
end

wesnoth.show_dialog(wml.get_child(params, 'dialog'), pre_show)
return params
>>
[scenario]
name= _ "Random map (Cave)"
id=multiplayer_Random_Map_Cave
Expand Down Expand Up @@ -174,9 +350,117 @@ Rb,Rb,Rb,Rb,Rb,Rb,Rb,Rb^Uf,Rb^Ii,Sm,Sm,Uue,Rb^Fetd,Rb^Fdw#enddef
{PLAYER_CHAMBER 2 3..10 3..10 bottom-right}
{PLAYER_CHAMBER 3 3..10 3..10 bottom-left}
{PLAYER_CHAMBER 4 3..10 3..10 top-right}
{PLAYER_CHAMBER 5 3..10 3..10 top-middle}
{PLAYER_CHAMBER 6 3..10 3..10 bottom-middle}
{PLAYER_CHAMBER 7 3..10 3..10 middle-left}
{PLAYER_CHAMBER 8 3..10 3..10 middle-right}

# This section is not used by the generator
# Instead, it's used by the user config script as GUI2 [resolution] WML.
[dialog]
definition = "default"
automatic_placement = true
vertical_placement = "center"
horizontal_placement = "center"
maximum_height = 600
[tooltip]
id=tooltip
[/tooltip]
[helptip]
id=tooltip
[/helptip]
#textdomain wesnoth-lib
[grid]
[row]
grow_factor=0
[column]
grow_factor=1
border=all
border_size=5
horizontal_alignment=left
[label]
definition=title
label=_"Map Generator Settings"
[/label]
[/column]
[/row]
[row]
grow_factor=1
[column]
horizontal_grow=true
vertical_grow=true
[grid]
{MAP_OPTION_CONTROL players _"Players:" slider (
definition=minimal
minimum_value=2
maximum_value=8
step_size=1
)}
{MAP_OPTION_CONTROL width _"Width:" slider (
definition=minimal
minimum_value=20
maximum_value=100
step_size=1
)}
{MAP_OPTION_CONTROL height _"Height:" slider (
definition=minimal
minimum_value=20
maximum_value=100
step_size=1
)}
{MAP_OPTION_CONTROL village_density _"Villages:" slider (
definition=minimal
minimum_value=0
maximum_value=50
step_size=1
)}
#textdomain wesnoth-multiplayer
{MAP_OPTION_CONTROL jagged _"Chamber Jaggedness:" slider (
definition=minimal
minimum_value=10
maximum_value=50
step_size=5
)}
{MAP_OPTION_CONTROL lake_size _"Lake Size:" slider (
definition=minimal
minimum_value=5
maximum_value=50
step_size=5
)}
{MAP_OPTION_CONTROL windiness _"Passage Windiness:" slider (
definition=minimal
minimum_value=1
maximum_value=20
step_size=1
)}
#textdomain wesnoth-lib
{MAP_OPTION_CONTROL roads "" toggle_button (
definition=checkbox
label=_"Roads Between Castles"
)}
[/grid]
[/column]
[/row]
[row]
grow_factor=0
[column]
border=all
border_size=5
horizontal_alignment=right
#textdomain wesnoth-lib
[button]
definition=default
id=ok
label=_"Close"
[/button]
[/column]
[/row]
[/grid]
[/dialog]
[/generator]
[/multiplayer]

#undef CLEAR_TERRAINS
#undef ROAD_COSTS
#undef PLAYER_CHAMBER
#undef MAP_OPTION_CONTROL

0 comments on commit a140266

Please sign in to comment.