# DES-related lua only needed for save compatibility.
# Please remove the code in this file when the next line is false.
# TAG_MAJOR_VERSION == 34
{{
function callback.grunt_victim_ignite(data, triggerable, triggerer, marker, ev)
  -- the victims don't spawn before; do nothing
end

function callback.ldierk_statue_collapse(data, triggerable, triggerer, marker, ev)
  -- wall_hit events removed; do nothing
end

function callback.wall_jericho_stepped(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function callback.wall_jericho_tick(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function callback.fedhas_growing_garden_grow(data, triggerable,
                                             triggerer, marker, ev)
  -- do nothing
end

function callback.monster_doors_stepped(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function callback.cloud_trap_stepped(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function callback.dk_crumbling_pickup(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function callback.dk_crumbling_turn(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function callback.cathedral_bats (data, triggerable, triggerer, marker, ev)
  -- do nothing
end

-- I'm keeping the code here in case someone is unfortunate enough to
-- have this vault present in the game; connectivity might be broken
-- otherwise.
-- -- Grunt
function callback.grunt_zot_stairs_roulette_spawn_doors(data, triggerable,
                                                        triggerer, marker, ev)
  if data.triggered then
    return
  end

  data.triggered = true

  dgn.persist.grunt_zot_stairs_roulette_doors_spawned = true

  for p in iter.replica_iterator("hidden_door", 1) do
    dgn.terrain_changed(p.x, p.y, "clear_stone_wall", false)
  end
end

function callback.grunt_zot_stairs_roulette_door_open(data, triggerable,
                                                      triggerer, marker, ev)
  if data.triggered then
    return
  end

  if dgn.persist.grunt_zot_stairs_roulette_doors_spawned == false then
    return
  end

  data.triggered = true

  crawl.mpr("Several panes of glass retract into the floor.")
  for p in iter.replica_iterator("hidden_door", 1) do
    dgn.terrain_changed(p.x, p.y, "floor", false)
  end
end

function callback.grunt_zot_stairs_roulette_teleport(data, triggerable,
                                                     triggerer, marker, ev)
  replica = dgn.find_marker_positions_by_prop("dest", data.dest)
  if you.teleport_to(replica[1].x, replica[1].y) then
      crawl.mpr("Your surroundings suddenly seem different.")
  else
      crawl.mpr("There is a strange hissing noise.")
  end
end

function teleporter_serial_action_fn
                      (data, triggerable, triggerer, marker, ev)
  local position = dgn.point(marker:pos())
  my_replicas = dgn.find_marker_positions_by_prop("teleport_spot",
                                                  data.teleport_spot)

  -- third parameter is whether to teleport whatever is on the receiving pad
  if you.teleport_to(my_replicas[1].x, my_replicas[1].y, false) then
    crawl.mpr("Your surroundings suddenly seem different!")
  else
    crawl.mpr("There is a strange hissing noise.")
  end
end

function callback.xom_trap_step(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function callback.xom_item_pickup(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function callback.amethyst_srp_altar(data, triggerable, triggerer, marker, ev)
  local replicas = dgn.find_marker_positions_by_prop("teleport_spot",
                                                     data.teleport_spot)
  you.teleport_to(replicas[1].x, replicas[1].y)
  crawl.mpr("Your surroundings suddenly seem different.")
end

-- Allow people with the ancient champion vault to still clear it.
function callback.ancient_champions_mu_skele_death(data, triggerable,
                                                    triggerer, marker, ev)
  data.skele_number = data.skele_number + 1

  -- Only 7 skeles!
  if data.skele_number > 7 then
    return
  end

  local function get_replica (replicanum)
    local myreplicas = dgn.find_marker_positions_by_prop("skele_replica", replicanum)
    return myreplicas[1]
  end

  local wall_pos = get_replica(data.skele_number)
  if wall_pos == nil then
    crawl.mpr("Couldn't find a replica!")
  end

  dgn.terrain_changed(wall_pos.x, wall_pos.y, "floor", false)

  if you.see_cell(wall_pos.x, wall_pos.y) then
    crawl.mpr("As the champion is destroyed, a metal wall slides away!")
  else
    crawl.mpr("As the champion is destroyed, you hear a distant grinding noise.")
  end
end

function tukima_machine (data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function wizlab_eringya_grow(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function callback.spiders_nest_cobweb_light(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

function callback.spiders_nest_web_lair(data, triggerable, triggerer, marker, ev)
  -- do nothing
end

-- let this vault still function if it already exists
function callback.beogh_warn_stepped(data, triggerable, triggerer, marker, ev)
  local x, y = marker:pos()

  if you.god() == "Beogh" or you.god() == "No God" then
     crawl.god_speaks("Beogh" , "Your presence is welcomed at the altar of Beogh.")
     dgn.grid(x, y, "floor")
  end
end

function callback.beogh_trap_stepped(data, triggerable, triggerer, marker, ev)
  local x, y = marker:pos()
-- This enacts the changes. First it checks to see if the player is a Beogh
-- worshipper or godless (a potential worshipper). If the player is one of
-- these things nothing happens. Otherwise the trap is sprung.
  if monster_step == true then
    return
  elseif you.god() == "Beogh" or you.god() == "No God" then
    dgn.grid(x, y, "floor")
  else
    crawl.god_speaks("Beogh", "Beogh's servants are called upon to defend the altar!")
    for replica in iter.replica_iterator("bridge", 1) do
      dgn.terrain_changed(replica.x, replica.y, "floor", false)
    end
    dgn.grid(x, y, "floor")
  end
end

function callback.boulder_grate_trap_stepped(data, triggerable, triggerer,
                                             marker, ev)
  -- do nothing
end

function callback.grate_trap_stepped(data, triggerable, triggerer,
                                             marker, ev)
  -- do nothing
end

callback.grunt_nemelex_gamble_announce = callback.nemelex_gamble_announce

function callback.grunt_nemelex_gamble_start(data,triggerable,triggerer,marker,ev)
    if dgn.persist.grunt_nemelex_gamble_triggered then
        return
    end
    dgn.persist.grunt_nemelex_gamble_triggered = true
    replica = dgn.find_marker_positions_by_prop("start_tele", 1)
    dgn.terrain_changed(replica[1].x, replica[1].y, "floor", false)
    if crawl.coinflip() then
        replica = dgn.find_marker_positions_by_prop("prize", 1)
        you.teleport_to(replica[1].x, replica[1].y)
        crawl.god_speaks("Nemelex Xobeh", "Nemelex Xobeh says, \"You win!\"")
    else
        replica = dgn.find_marker_positions_by_prop("peril", 1)
        you.teleport_to(replica[1].x, replica[1].y)
        crawl.god_speaks("Nemelex Xobeh", "Nemelex Xobeh says, \"You lose!\"")
    end
    crawl.mpr("Your surroundings suddenly seem different.")
end

function callback.grunt_nemelex_gamble_finish1(data,triggerable,triggerer,marker,ev)
    replica = dgn.find_marker_positions_by_prop("exit", 1)
    you.teleport_to(replica[1].x, replica[1].y)
    crawl.mpr("Your surroundings suddenly seem different.")
end

function callback.grunt_nemelex_gamble_finish2(data,triggerable,triggerer,marker,ev)
    replica = dgn.find_marker_positions_by_prop("exit", 1)
    you.teleport_to(replica[1].x, replica[1].y)
    crawl.mpr("Your surroundings suddenly seem different.")
end

function callback.grunt_nemelex_gamble_expire(data,triggerable,triggerer,marker,ev)
    if dgn.persist.grunt_nemelex_gamble_triggered then
        return
    end
    if not dgn.persist.grunt_nemelex_gamble_announced then
        return
    end
    data.dur = data.dur - ev:ticks()
    if data.dur > 0 then
        return
    end
    dgn.persist.grunt_nemelex_gamble_triggered = true
    replica = dgn.find_marker_positions_by_prop("start_tele", 1)
    dgn.terrain_changed(replica[1].x, replica[1].y, "floor", false)
    if (you.see_cell(replica[1].x, replica[1].y)) then
        crawl.mpr("<white>The teleporter glows with a rainbow of weird colours and disappears.</white>")
    end
end

-- Find the replica of the teleporter and move to it.
function teleporter_vault_action_fn(data, triggerable, triggerer, marker, ev)
    local position = dgn.point(marker:pos())
    my_replicas = dgn.find_marker_positions_by_prop("teleport_spot",
                                                    data.teleport_spot)

    if you.teleport_to(my_replicas[1].x, my_replicas[1].y, true) then
        crawl.mpr("Your surroundings suddenly seem different!")
    else
        crawl.mpr("There is a strange hissing noise.")
    end
end

-- Find the replica of the teleporter and move to it.
function callback.ice_cave_statue_garden_teleporter(data, triggerable,
                                                     triggerer, marker, ev)
  local position = dgn.point(marker:pos())
  my_replicas = dgn.find_marker_positions_by_prop("teleport_spot",
    data.teleport_spot)

  you.teleport_to(my_replicas[1].x, my_replicas[1].y)
  crawl.mpr("Your surroundings suddenly seem different!")
end

function teleporter_golubria_switch_fn(data, triggerable, triggerer, marker, ev)
  local position = dgn.point(marker:pos())
  my_replicas = dgn.find_marker_positions_by_prop("teleport_spot",
                                                data.teleport_spot)

  if you.teleport_to(my_replicas[1].x, my_replicas[1].y, true) then
    crawl.mpr("Your surroundings suddenly seem different!")
  else
    crawl.mpr("There is a strange hissing noise.")
  end
end

function callback.spider_trap_stepped(data, triggerable, triggerer, marker, ev)
  data.mons="generate_awake redback"
  data.msg="A basket of spiders falls from above!"
  data.max=8
  callback.drop_trap_stepped(data, triggerable, triggerer, marker, ev)
end

-- Save compat for gammafunk_pakellas_* vaults, formerly in
-- pakellas_experiments.des.
function make_dummy_trigger_func()
  return function (data, triggerable, triggerer, marker, ev)
    -- do nothing.
   end
end

callback.pakellas_cloud_experiment_begin = make_dummy_trigger_func()
callback.pakellas_cloud_experiment_end = make_dummy_trigger_func()
callback.pakellas_cloud_cloud_cycle = make_dummy_trigger_func()
cycle_cloud_type = callback.pakellas_cloud_cycle
callback.pakellas_statue_experiment_begin = make_dummy_trigger_func()
callback.pakellas_statue_experiment_end = make_dummy_trigger_func()
callback.pakellas_its_alive_clouds = make_dummy_trigger_func()
callback.pakellas_its_alive_begin = make_dummy_trigger_func()
callback.pakellas_its_alive_end = make_dummy_trigger_func()
callback.grunt_zot_stairs_overgrown_start = make_dummy_trigger_func()
callback.grunt_zot_stairs_overgrown_oklobify = make_dummy_trigger_func()

-- save compat for grunt_zot_stairs_overgrown
-- let this vault still work if it exists
function callback.grunt_zot_stairs_overgrown_start(data, triggerable,
                                                   triggerer, marker, ev)
  if data.triggered == true then
    return
  end

  crawl.mpr("You have a sense of imminent foreboding.", "warning")
  dgn.persist.grunt_zot_stairs_overgrown_started = true
end

function callback.grunt_zot_stairs_overgrown_oklobify(data, triggerable,
                                                      triggerer, marker, ev)
  if triggerer.type ~= "turn" or triggerer.sub_type ~= "countdown" then
    return
  end

  if dgn.persist.grunt_zot_stairs_overgrown_started == nil then
    return
  end

  if data.count >= 10 then
    dgn.persist.grunt_zot_stairs_overgrown_started = nil
    return
  end

  local possible = {}
  local targets = dgn.find_marker_positions_by_prop("oklobify", 1)
  for _, pos in ipairs(targets) do
    local mons = dgn.mons_at(pos.x, pos.y)
    if mons ~= nil and
       (mons.name == "plant" or mons.name == "fungus"
        or mons.name == "bush") then
      possible[#possible + 1] = pos
    end
  end

  if #possible == 0 then
    dgn.persist.grunt_zot_stairs_overgrown_started = nil
    return
  end

  data.count = data.count + 1

  local pos = possible[crawl.random2(#possible) + 1]
  local mons = dgn.mons_at(pos.x, pos.y)
  mons.dismiss()

  dgn.create_monster(pos.x, pos.y, "oklob plant")

  if not you.see_cell(pos.x, pos.y) then return end

  if not data.warned then
    data.warned = true
    crawl.mpr("One of the plants suddenly grows acid sacs!", "warning")
  else
    crawl.mpr("Another plant grows acid sacs!", "warning")
  end
end

}}
