summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/dat/clua/ziggurat.lua
blob: 65927e7b1f55265c16c901f63c55981101a9f388 (plain) (blame)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
------------------------------------------------------------------------------
-- ziggurat.lua:
--
-- Code for ziggurats.
--
-- Important notes:
-- ----------------
-- Functions that are attached to Lua markers' onclimb properties
-- cannot be closures, because Lua markers must be saved and closure
-- upvalues cannot (yet) be saved.
------------------------------------------------------------------------------

function zig()
  if not dgn.persist.ziggurat or not dgn.persist.ziggurat.depth then
    dgn.persist.ziggurat = { }
    -- Initialise here to handle ziggurats accessed directly by &P.
    initialise_ziggurat(dgn.persist.ziggurat)
  end
  return dgn.persist.ziggurat
end

function cleanup_ziggurat()
  return one_way_stair {
    onclimb = function(...)
                dgn.persist.ziggurat = { }
              end,
    dstplace = zig().origin_level
  }
end

local wall_colours = {
  "blue", "red", "lightblue", "magenta", "green", "white"
}

function ziggurat_wall_colour()
  return util.random_from(wall_colours)
end

function initialise_ziggurat(z)
  z.depth = 1

  -- Any given ziggurat will use the same builder for all its levels.
  z.builder = ziggurat_choose_builder()

  z.colour = ziggurat_wall_colour()
  z.level  = { }

  z.origin_level = dgn.level_name(dgn.level_id())
end

function ziggurat_initialiser()
  -- First ziggurat will be initialised twice.
  initialise_ziggurat(zig())
end

local function random_floor_colour()
  return ziggurat_wall_colour()
end

-- Returns a function that changes the depth in the ziggurat to the depth
-- specified.
function zig_depth_increment()
  zig().depth = zig().depth + 1
  zig().level = { }
  dgn.set_level_type_origin("on level " .. zig().depth .. " of a ziggurat")
end

-- Returns the current depth in the ziggurat.
local function zig_depth()
  return zig().depth or 0
end

-- Common setup for ziggurat entry vaults.
function ziggurat_portal(e)
  local function stair()
    return one_way_stair {
      desc = "gateway to a ziggurat",
      dst = "ziggurat",
      dstorigin = "on level 1 of a ziggurat",
      floor = "stone_arch",
      onclimb = ziggurat_initialiser
    }
  end

  e.lua_marker("O", stair)
  e.kfeat("O = enter_portal_vault")
end

-- Common setup for ziggurat levels.
function ziggurat_level(e)
  e.tags("ziggurat")
  e.tags("allow_dup")
  e.orient("encompass")
  ziggurat_build_level(e)
end

-----------------------------------------------------------------------------
-- Ziggurat level builders.

function ziggurat_build_level(e)
  local builder = zig().builder
  if builder then
    return ziggurat_builder_map[builder](e)
  end
end

local function zigstair(x, y, stair, marker)
  dgn.grid(x, y, stair)
  if marker then
    local t = type(marker)
    if t == "function" or t == "table" then
      dgn.register_lua_marker(x, y, marker)
    else
      dgn.register_feature_marker(x, y, marker)
    end
  end
end

-- Creates a Lua marker table that increments ziggurat depth.
local function zig_go_deeper()
  return one_way_stair {
    onclimb = zig_depth_increment
  }
end

local function map_area()
  local base_area = 20 + 8 * zig_depth()
  return 2 * base_area + crawl.random2(base_area)
end

local function clamp_in(val, low, high)
  if val < low then
    return low
  elseif val > high then
    return high
  else
    return val
  end
end

local function clamp_in_bounds(x, y)
  return clamp_in(x, 1, dgn.GXM - 2), clamp_in(y, 1, dgn.GYM - 2)
end

local function rectangle_dimensions()
  local area = map_area()

  local cx, cy = dgn.GXM / 2, dgn.GYM / 2

  local asqrt = math.sqrt(area)
  local b = crawl.random_range(1 + asqrt / 2, asqrt + 1)
  local a = math.floor((area + b - 1) / b)

  local a2 = math.floor(a / 2) + (a % 2);
  local b2 = math.floor(b / 2) + (b % 2);
  local x1, y1 = clamp_in_bounds(cx - a2, cy - b2)
  local x2, y2 = clamp_in_bounds(cx + a2, cy + b2)
  return x1, y1, x2, y2
end

local mons_populations = {
  "Elf:7", "Orc:4", "Vault:8", "Slime:6",
  "Snake:5", "Lair:10", "Tomb:3", "Crypt:5",
  "Abyss", "Shoal:5", "Pan"
}

local function set_floor_colour(colour)
  if not zig().level.floor_colour then
    zig().level.floor_colour = colour
    dgn.change_floor_colour(colour, false)
  end
end

local function set_random_floor_colour()
  set_floor_colour( random_floor_colour() )
end

local function mons_random_gen(x, y, nth)
  set_random_floor_colour()
  return dgn.create_monster(x, y, "place:" ..
                            util.random_from(mons_populations))
end

local function mons_drac_gen(x, y, nth)
  set_random_floor_colour()
  return dgn.create_monster(x, y, "random draconian")
end

local function mons_panlord_gen(x, y, nth)
  set_random_floor_colour()
  if nth == 1 then
    return dgn.create_monster(x, y, "pandemonium lord band")
  else
    return dgn.create_monster(x, y, "place:Pan")
  end
end

local mons_generators = {
  mons_random_gen,
  mons_drac_gen,
  mons_panlord_gen
}

local function monster_creator_fn(arg)
  if type(arg) == "string" then
    local _, _, branch = string.find(arg, "^(%w+):")
    return function (x, y, nth)
             if branch then
               set_floor_colour(dgn.br_floorcol(branch))
             end

             return dgn.create_monster(x, y, "place:" .. arg)
           end
  else
    return arg
  end
end

local monster_creators =
  util.map(monster_creator_fn, util.catlist(mons_populations, mons_generators))

local function ziggurat_vet_monster(fn)
  return function (x, y, nth)
           while true do
             local mons = fn(x, y, nth)
             if mons then
               if mons.experience == 0 then
                 mons.dismiss()
               else
                 -- Monster is ok!
                 return mons
               end
             end
           end
         end
end

local function choose_monster_set()
  return ziggurat_vet_monster(util.random_from(monster_creators))
end

local function ziggurat_create_monsters(p)
  local depth = zig_depth()
  local hd_pool = depth * (depth + 8)

  local function mons_place_p(point)
    return not dgn.mons_at(point.x, point.y)
  end

  local mfn = choose_monster_set()
  local nth = 1

  -- No monsters
  while hd_pool > 0 do
    local place = dgn.find_adjacent_point(p, mons_place_p)
    local mons = mfn(place.x, place.y, nth)

    if mons then
      nth = nth + 1
      hd_pool = hd_pool - mons.hd
    end
  end
end

local function flip_rectangle(x1, y1, x2, y2)
  local cx = math.floor((x1 + x2) / 2)
  local cy = math.floor((y1 + y2) / 2)
  local nx1 = cx + y1 - cy
  local nx2 = cx + y2 - cy
  local ny1 = cy + x1 - cx
  local ny2 = cy + x2 - cx
  return { nx1, ny1, nx2, ny2 }
end

local function ziggurat_create_loot(c)
  local nloot = zig_depth()
  local depth = zig_depth()

  local function is_free_space(p)
    return dgn.grid(p.x, p.y) == dgn.fnum("floor") and
      #dgn.items_at(p.x, p.y) == 0
  end

  local function free_space_do(fn)
    local p = dgn.find_adjacent_point(c, is_free_space)
    if p then
      fn(p)
    end
  end

  local loot_depth = 20
  if you.absdepth() > loot_depth then
    loot_depth = you.absdepth() - 1
  end

  local function place_loot(what)
    free_space_do(function (p)
                    dgn.create_item(p.x, p.y, what, loot_depth)
                  end)
  end

  for i = 1, nloot do
    if crawl.one_chance_in(depth) then
      for j = 1, 4 do
        place_loot("*")
      end
    else
      place_loot("|")
    end
  end
end

local function ziggurat_place_pillars(c)
  local range = crawl.random_range
  local floor = dgn.fnum("floor")

  local map, vplace = dgn.resolve_map(dgn.map_by_tag("ziggurat_pillar"))

  if not map then
    return
  end

  local name = dgn.name(map)

  local size = dgn.point(dgn.mapsize(map))

  -- Does the pillar want to be centered?
  local centered = string.find(dgn.tags(map), " centered ")

  local function good_place(p)
    local function good_square(where)
      return dgn.grid(where.x, where.y) == floor
    end
    return dgn.rectangle_forall(p, p + size - 1, good_square)
  end

  local function place_pillar()
    if centered then
      if good_place(c) then
        return dgn.place_map(map, false, true, c.x, c.y)
      end
    else
      for i = 1, 100 do
        local offset = range(-15, -size.x)
        local offsets = {
          dgn.point(offset, offset) - size + 1,
          dgn.point(offset - size.x + 1, -offset),
          dgn.point(-offset, -offset),
          dgn.point(-offset, offset - size.y + 1)
        }

        offsets = util.map(function (o)
                             return o + c
                           end, offsets)

        if util.forall(offsets, good_place) then
          local function replace(at, hflip, vflip)
            dgn.reuse_map(vplace, at.x, at.y, hflip, vflip)
          end

          replace(offsets[1], false, false)
          replace(offsets[2], false, true)
          replace(offsets[3], true, false)
          replace(offsets[4], false, true)
          return true
        end
      end
    end
  end

  for i = 1, 5 do
    if place_pillar() then
      break
    end
  end
end

local function ziggurat_rectangle_builder(e)
  local grid = dgn.grid

  dgn.fill_area(0, 0, dgn.GXM - 1, dgn.GYM - 1, "stone_wall")

  local x1, y1, x2, y2 = rectangle_dimensions()
  dgn.fill_area(x1, y1, x2, y2, "floor")

  dgn.fill_area(unpack( util.catlist(flip_rectangle(x1, y1, x2, y2),
                                     { "floor" }) ) )

  local c = dgn.point(x1 + x2, y1 + y2) / 2

  local entry = { x = x1, y = c.y }
  local exit = { x = x2, y = c.y }

  if zig_depth() % 2 == 0 then
    entry, exit = exit, entry
  end

  zigstair(entry.x, entry.y, "stone_arch", "stone_stairs_up_i")
  zigstair(exit.x, exit.y, "stone_stairs_down_i", zig_go_deeper)
  zigstair(exit.x, exit.y + 1, "exit_portal_vault", cleanup_ziggurat())
  zigstair(exit.x, exit.y - 1, "exit_portal_vault", cleanup_ziggurat())

  ziggurat_place_pillars(c)

  ziggurat_create_loot(exit)

  ziggurat_create_monsters(exit)

  local function needs_colour(p)
    return not dgn.in_vault(p.x, p.y)
      and dgn.grid(p.x, p.y) == dgn.fnum("stone_wall")
  end

  dgn.colour_map(needs_colour, zig().colour)
end

----------------------------------------------------------------------

ziggurat_builder_map = {
  rectangle = ziggurat_rectangle_builder
}

local ziggurat_builders = { }
for key, val in pairs(ziggurat_builder_map) do
  table.insert(ziggurat_builders, key)
end

function ziggurat_choose_builder()
  return util.random_from(ziggurat_builders)
end