# Crumbling vaults (by dk)

{{
function callback.dk_crumbling_pickup(data, triggerable, triggerer, marker, ev)
  dgn.persist.dk_crumbling_triggered = true
  crawl.mpr("Suddenly, the floor starts to tremble!", "warning")
end

function callback.dk_crumbling_turn(data, triggerable, triggerer, marker, ev)

  if not dgn.persist.dk_crumbling_triggered or
     triggerer.type ~= "turn" or
     triggerer.sub_type ~= "countdown" then
    return
  end

  -- don't spam too much messages
  if crawl.coinflip() then
    crawl.mpr("The floor trembles.")
  end

  -- create a dictionary to keep track of positions already changed to water
  if data.s == nil then
    data.s = {} -- key is the x position, value is a list of y positions
    data.c = 0  -- total number of tiles converted to water
  end

  -- don't create too much water
  if data.c > 20 and crawl.one_chance_in(3) then
    triggerable:remove(marker)
    crawl.mpr("The trembling seems to have ended.")
    dgn.persist.dk_crumbling_triggered = false
    return
  end

  local water = nil
  for slave in iter.slave_iterator('floor', 1) do
    if data.s[slave.x] == nil then
      data.s[slave.x] = {}
    else
      if data.s[slave.x][slave.y] == nil and crawl.one_chance_in(10) then
        dgn.terrain_changed(slave.x, slave.y, "shallow_water", false, false, false)
        data.s[slave.x][slave.y] = 1
        data.c = data.c + 1
        water = {slave.x, slave.y}
      end
    end
  end

  -- don't spam too much messages
  if water ~= nil and crawl.coinflip() then
    if you.see_cell(water[1], water[2]) then
      crawl.mpr("Water seeps in.")
    end
  end

  -- destroy the statues
  if data.no < 10 then
    for slave in iter.slave_iterator('no', data.no) do
      dgn.terrain_changed(slave.x, slave.y, "floor", false, false, false)
      dgn.create_item(slave.x, slave.y, "stone q:3 / stone q:2 / stone")

      -- don't spam too much messages
      if crawl.coinflip() and you.see_cell(slave.x, slave.y) then
        crawl.mpr("The statue crumbles to dust.")
      end
    end

    data.no = data.no + 1
  end

end

function init_crumbling(e)
  e.depth('D')
  e.depth('Elf')
  e.depth('Vaults')
  e.tags('no_monster_gen')
  e.tags('no_item_gen')
  e.tags('uniq_dk_crumbling')
  e.shuffle('0123456789')
  e.orient('float')
  e.weight('6')

  e.dgn.persist.dk_crumbling_triggered = false

  local tup = TriggerableFunction:new{
    func="callback.dk_crumbling_pickup",
    repeated=false
  }

  tup:add_triggerer(DgnTriggerer:new{type="item_pickup",
                                     target="auto"})

  e.lua_marker('*', tup)

  local tturn = TriggerableFunction:new{
    func="callback.dk_crumbling_turn",
    repeated=true,
    data={no=0,
          s=nil}
  }

  tturn:add_triggerer(DgnTriggerer:new{type="turn",
                                       delay_min=10,
                                       delay_max=40})

  e.lua_marker('M', tturn)

  local str = "0123456789"
  str:gsub(".", function(c)
      e.lua_marker(c, props_marker {no=c, floor=1})
      e.subst(c .. "=G")
  end)

  e.lua_marker('.', props_marker {floor=1})

  e.subst('M = x')
end
}}

NAME:   dk_crumbling
: init_crumbling(_G)
MAP
xxxxxxxxxxxxxxxxxxxxxxxx
xMx0x1x2x3x4x5x6x7x8x9xx
x*.....................+
xxx0x1x2x3x4x5x6x7x8x9xx
xxxxxxxxxxxxxxxxxxxxxxxx
ENDMAP

NAME:   dk_crumbling_2
: init_crumbling(_G)
MAP
       xx+xx
       xx.xx
       x0.0x
       xx.xx
       x1.1x
       xx.xx
       x2.2x
       xx.xx
       x3.3x
xxxxxxxxx.xxxxxxxxxxxx
xx6x5x4xx.xx7x8xx9x0xx
+........*...........+
xx6x5x4xx.Mx7x8xx9x0xx
xxxxxxxxx.xxxxxxxxxxxx
       x0.0x
       xx.xx
       x1.1x
       xx.xx
       x2.2x
       xx.xx
       x3.3x
       xx.xx
       xx+xx
ENDMAP

NAME:   dk_crumbling_3
: init_crumbling(_G)
MAP
    xx+xx
   xx...xx
  xx.....xx
 xx.0...4.xx
xx.7.....8.xx
x...1...2...x
+.....*.....+
x...3...4...x
xx.5.....6.xx
 xM.9...0.xx
  xx.....xx
   xx...xx
    xx+xx
ENDMAP

NAME:   dk_crumbling_4
: init_crumbling(_G)
MAP
    xx+xx
   xx0.1xx
  xx5...6xx
 xx9.....7xx
xx8.......8xx
x2.........7x
+.....*.....+
x3.........6x
xx6.......9xx
 xM3.....0xx
  xx2...1xx
   xx4.5xx
    xx+xx
ENDMAP
