-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
cave_map_generator.lua
254 lines (238 loc) · 7.4 KB
/
cave_map_generator.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
local helper = wesnoth.require "helper"
local MG = wesnoth.require "mapgen_helper"
local LS = wesnoth.require "location_set"
local T = helper.set_wml_tag_metatable {}
local random = wesnoth.random
local callbacks = {}
function callbacks.generate_map(params)
local map = MG.create_map(params.map_width, params.map_height, params.terrain_wall)
local function build_chamber(x, y, locs_set, size, jagged)
if locs_set:get(x,y) or not map:on_board(x, y) or size == 0 then
return
end
locs_set:insert(x,y)
for xn, yn in MG.adjacent_tiles(x, y) do
if random(100) <= 100 - jagged then
build_chamber(xn, yn, locs_set, size - 1, jagged)
end
end
end
local function clear_tile(x, y, terrain_clear)
if not map:on_board(x,y) then
return
end
if map:get_tile(x,y) == params.terrain_castle or map:get_tile(x,y) == params.terrain_keep then
return
end
local r = random(1000)
if r <= params.village_density then
map:set_tile(x, y, helper.rand(params.terrain_village))
else
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.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
local chambers = {}
local chambers_by_id = {}
local passages = {}
for chamber in helper.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
x = map.w - x
elseif chamber.relative_to == "bottom-right" then
x = map.w - x
y = map.h - y
elseif chamber.relative_to == "bottom-left" then
y = map.h - y
elseif chamber.relative_to == "top-middle" then
x = (map.w / 2) + x
elseif chamber.relative_to == "bottom-middle" then
x = (map.w / 2) + x
y = map.h - y
elseif chamber.relative_to == "middle-left" then
y = (map.h / 2) + y
elseif chamber.relative_to == "middle-right" then
y = (map.h / 2) + y
x = map.w - x
elseif chamber.relative_to == "center" then
x = (map.w / 2) + x
y = (map.h / 2) + y
end -- Default is "top-left" which means no adjustments needed
local id = chamber.id
if chance == 0 or random(100) > chance then
-- Set chance to 0 so that the scenario generator can tell which chambers were used
params.chance = 0
goto continue
end
if type(chamber.require_player) == "number" and chamber.require_player > params.nplayers then
params.chance = 0
goto continue
end
-- Ditto, set it to 100
params.chance = 100
local locs_set = LS.create()
build_chamber(x, y, locs_set, chamber.size or 3, chamber.jagged or 0)
local items = {}
for item in helper.child_range(chamber, "item_location") do
table.insert(items, item)
end
table.insert(chambers, {
center_x = x,
center_y = y,
side_num = chamber.side,
locs_set = locs_set,
id = id,
items = items,
data = chamber,
})
chambers_by_id[id] = chambers[#chambers]
for passage in helper.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 = {}, {}
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
::continue::
end
::continue::
end
for i,v in ipairs(chambers) do
local locs_list = {}
for x, y in v.locs_set:stable_iter() do
clear_tile(x, y, v.data.terrain_clear)
if map:on_inner_board(x, y) then
table.insert(locs_list, {x,y})
end
end
for i1, item in ipairs(v.items or {}) do
local index = random(#locs_list)
local loc = locs_list[index]
table.remove(locs_list, index)
local x, y = table.unpack(loc)
if item.id then
map:add_location(x, y, item.id)
end
if item.place_castle then
map:set_tile(x, y, params.terrain_keep)
for x2, y2 in MG.adjacent_tiles(x, y) do
map:set_tile(x2, y2, params.terrain_castle)
end
end
end
end
for i,v in ipairs(passages) do
if random(100) <= (v.data.chance or 100) then
local windiness = v.data.windiness or 0
local laziness = math.max(v.data.laziness or 1, 1)
local width = math.max(v.data.width or 1, 1)
local jagged = v.data.jagged or 0
local calc = function(x, y, current_cost)
local res = 1.0
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)
end
return res
end
local path, cost = wesnoth.find_path(v.start_x, v.start_y, v.dest_x, v.dest_y, calc, params.map_width, params.map_height)
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
local r = 1000
local ter_to_place
if v.data.place_villages then r = random(1000) end
if r <= params.village_density then
ter_to_place = v.data.terrain_village or params.terrain_village
else
ter_to_place = v.data.terrain_clear or params.terrain_clear
end
place_road(x, y, prev_x, prev_y, v.roads, ter_to_place)
prev_x, prev_y = x, y
end
end
end
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
if MG.is_valid_transform(t) then
table.insert(transforms, t)
else
helper.wml_error("Unknown transformation '" .. t .. "'")
end
end
map[transforms[random(#transforms)]](map)
end
end
return tostring(map)
end
function callbacks.generate_scenario(params)
-- This is more or less backwards compatible with the cave generator syntax
local scenario = helper.get_child(params, "scenario")
scenario.map_data = callbacks.generate_map(params)
for chamber in helper.child_range(params, "chamber") do
local items = helper.get_child(chamber, "items")
if chamber.chance == 100 and chamber_items then
-- TODO: Should we support [event]same_location_as_previous=yes?
for i,tag in ipairs(chamber_items) do
table.insert(scenario, tag)
end
end
end
return scenario
end
return callbacks