##############################################################################
# Grate Traps
#
# See basic_grate_trap for a simple example of how to use this. Note that
# multiple grates and multiple pressure plates are supported. When one plate
# is triggered, all the grates are lowered, and all the plates are disabled.
#
# Currently only works once per level - ALL grate traps are activated when a
# plate is stepped on. Vaults MUST have luniq_grate_trap or things will be
# somewhat messed up.
#
# TODO:
# There is currently a similar trap vault with a lot of duplicated functionality
# to this one (monster_doors.des). It could probably be moved into here.
##############################################################################

{{
function place_grate_trap(e, plate_glyph, grate_glyph)
    local tm = TriggerableFunction:new{func="callback.grate_trap_stepped",
                                       repeated=true, data={triggered=false} }
    tm:add_triggerer(DgnTriggerer:new{type="pressure_plate"})
    e.lua_marker(plate_glyph, tm)
    e.lua_marker(plate_glyph, props_marker { plate = 1 })
    e.kfeat(plate_glyph .. ' = pressure plate trap')

    e.lua_marker(grate_glyph, props_marker { grate_trap = 1})
    e.kfeat(grate_glyph .. ' = floor')
end

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

    local x, y = marker:pos()
    local you_x, you_y = you.pos()

    -- Don't let monsters trigger this when you're not in view of it
    if not you.see_cell(x, y) then
        return
    end

    for slave in iter.slave_iterator("grate_trap", 1) do
        -- Skip squares that are occupied, either by a monster or by the player
        -- If there is an item on the square it will be destroyed.
        -- TODO: deal damage to the player / monsters that are caught under a grate!
        if (dgn.mons_at(slave.x, slave.y) ~= nil or (slave.x == you_x and slave.y == you_y)) then
            crawl.mpr("You hear a faint grinding sound.")
        else
            dgn.terrain_changed(slave.x, slave.y, "iron_grate", false, false, false)
            if (you.see_cell(slave.x, slave.y)) then
                crawl.mpr("An iron grate slams shut!")
            else
                crawl.mpr("You hear an iron grate slam shut!")
            end
        end
    end

    -- Remove pressure plate(s).
    for slave in iter.slave_iterator("plate", 1) do
        dgn.grid(slave.x, slave.y, "floor")
    end
    data.triggered = true
end
}}

###############################################################################
# Stair grate traps. Total weight: 30
NAME:   basic_stair_grate_trap
DEPTH:  D:9-27, Vaults:2-7, Crypt:2-5, Elf:2-5, Snake:2-5, Zot:2-5
ORIENT: float
TAGS:   allow_dup extra luniq_grate_trap layout_rooms layout_caves
: place_grate_trap(_G, '^', 'm')
MAP
xxxx
x(m^@
xxxx
ENDMAP

NAME:   stair_cage_open
DEPTH:  D:9-27, Vaults:2-7, Crypt:2-5, Elf:2-5, Snake:2-5, Zot:2-5
TAGS:   extra luniq_grate_trap no_monster_gen mini_float
WEIGHT: 2
: place_grate_trap(_G, '^', 'm')
MAP
.......
.^^^^^.
.^mmm^.
.^m(m^.
.^mmm^.
.^^^^^.
.......
ENDMAP

NAME:   stair_cage_cross
DEPTH:  D:9-27, Vaults:2-7, Crypt:2-5, Elf:2-5, Snake:2-5, Zot:2-5
TAGS:   extra luniq_grate_trap
WEIGHT: 5
: place_grate_trap(_G, '^', 'm')
MAP
xxxx@xxxx
xxxx^xxxx
xxxxmxxxx
xxxx.xxxx
@^m.(.m^@
xxxx.xxxx
xxxxmxxxx
xxxx^xxxx
xxxx@xxxx
ENDMAP

NAME:   stair_cage_line
DEPTH:  D:9-27, Vaults:2-7, Crypt:2-5, Elf:2-5, Snake:2-5, Zot:2-5
TAGS:   allow_dup extra luniq_grate_trap
: place_grate_trap(_G, '^', 'm')
MAP
xxxxxxx
xxxxxxx
@^m(m^@
xxxxxxx
xxxxxxx
ENDMAP

NAME:   stair_cage_wide
DEPTH:  D:9-27, Vaults:2-7, Crypt:2-5, Elf:2-5, Snake:2-5, Zot:2-5
TAGS:   allow_dup extra luniq_grate_trap
WEIGHT: 3
: place_grate_trap(_G, '^', 'm')
MAP
xxxxxxx
x(m^...
x.m^..@
x[m^...
xxxxxxx
ENDMAP

###############################################################################
# Other grate traps. Make sure there is an escape route.

NAME:   basic_loot_grate_trap
DEPTH:  D:8-26, Vaults:1-7, Crypt:1-4, Elf:1-4, Snake:1-4, Zot:1-4
TAGS:   extra luniq_grate_trap
: place_grate_trap(_G, '^', 'm')
MAP
xxxxxxx
xxxx>xx
xxxx=xx
@^.m|xx
xxxxxxx
xxxxxxx
xxxxxxx
ENDMAP
