##############################################################################
# Trap: Monsters behind doors
##############################################################################

# By sevenhm.
# When the player steps on the pressure plate they are shut into the corridor
# Doors appear where none were before.  Behind the doors are monsters.

{{

function callback.monster_doors_stepped(data, triggerable, triggerer, marker, ev)
  local x, y = marker:pos()

  -- if the trap has already been triggered, cancel it
  if data.triggered == true then
     return
  end
  -- if a monster triggers the trap, we want to either cancel it (if you see the monster)
  -- or pretend like it didn't happen
  if dgn.mons_at(x,y) then
    if not you.see_cell(x, y) then
      return
    else
      data.triggered = true
      dgn.grid(x,y,"floor")
      crawl.mpr("You hear a faint click.")
      return
    end
  end

  -- Once the plate is stepped on, we need to get rid of it regardless
  data.triggered = true
  dgn.grid(x, y, "floor")

  -- This checks to see if there are monsters on the spots where the grate will be, and
  -- if there are, it cancels the entire trap
  for slave in iter.slave_iterator("grate", 1) do
     if dgn.mons_at(slave.x, slave.y) ~= nil then
       crawl.mpr("You feel the floor shift and hear a click.")
       return
     end
  end
  -- This actually enacts the changes
  for slave in iter.slave_iterator("door", 1) do
  -- We want to make sure that someone hasn't already altered the terrain with dig
  -- if they have removed one of the doors, we cancel the entire thing
      if not dgn.is_passable(slave.x,slave.y) then
        dgn.terrain_changed(slave.x, slave.y, "closed_door", false, false, false)
      else
        crawl.mpr("You feel the floor shift and hear a click.")
        return
      end
  end
  for slave in iter.slave_iterator("grate", 1) do
        dgn.terrain_changed(slave.x, slave.y, "iron_grate", false, false, false)
  end
  for slave in iter.slave_iterator("exit", 1) do
      dgn.terrain_changed(slave.x, slave.y, "floor", false, false, false)
  end
  -- and then announce the changes
  crawl.mpr("You step on to a pressure plate and iron grates slam shut around you!")
  crawl.mpr("You now see doors in the wall!")

end

}}

NAME:   trap_monster_doors_1
DEPTH:  D:5-27
TAGS:   allow_dup extra luniq_monster_door
ORIENT: float
NSUBST: ' = q / x
KFEAT:  ^ = pressure plate trap
{{
local tm = TriggerableFunction:new{func="callback.monster_doors_stepped",
                                   repeated=true, data={triggered=false} }
tm:add_triggerer(DgnTriggerer:new{type="pressure_plate"})
lua_marker('^', tm)
lua_marker(',', props_marker { grate=1 })
lua_marker('z', props_marker { door=1 })
lua_marker('q', props_marker { exit=1 })
}}
KPROP:  1q = no_rtele_into
KMASK:  , = no_item_gen
SUBST:  zq : x
NSUBST: 1 = 8 / 111998
MAP
.............
.xx'x'x'x'xx.
.xx1x1x1x1xx.
.xxzxzxzxzxx.
@.,...^...,.@
 xxzxzxzxzxx
 xx1x1x1x1xx
 xxxxxxxxxxx
ENDMAP
