#########################################################
# Boulder traps
# Using the new rolling behaviour of boulder beetles,
# a number of traps in no way affiliated with Lucasfilm.
#########################################################

{{

-- Generates a boulder rolling at the target x and y
function boulder_generate(x, y, targetx, targety)
  local boulder = dgn.create_monster(x, y, 'generate_awake boulder beetle')

  if boulder == nil then
    return
  end

  -- Set x, y, velocity
  local tx = targetx - x
  local ty = targety - y
  boulder.set_prop("boulder_x",x)
  boulder.set_prop("boulder_y",y)
  boulder.set_prop("boulder_vx",tx)
  boulder.set_prop("boulder_vy",ty)
  -- Let it roll!
  boulder.add_ench("rolling",1,50)

end

-- TODO: Avoid enemies triggering it (altho allow summons)
function callback.boulder_grate_trap_stepped(data, triggerable, triggerer, marker, ev)
  if data.triggered == true then
    return
  end
  local x, y = marker:pos()
  if not you.see_cell(x, y) then
    -- pretend the monster avoided it
    return
  end
  local p = dgn.find_marker_positions_by_prop(data.gratename, 1)[1]
  if (p ~= nil) then
    -- Monster is in the way, don't do anything
    if (dgn.mons_at(p.x, p.y) ~= nil or p == you.pos()) then
      if (you.see_cell(x, y)) then
        crawl.mpr("A part of the floor depresses, but nothing seems to happen.")
        return
      end
    end
    dgn.grid(p.x, p.y, "iron_grate")
    if (you.see_cell(p.x, p.y)) then
      crawl.mpr("An iron grate slams shut!")
    else
      crawl.mpr("You hear a soft click.")
    end
  end
  -- Use trap position as default target
  local tx, ty = marker:pos()
  -- Check for an explicit target marker
  local target = dgn.find_marker_positions_by_prop(data.targetname, 1)[1]
  if target ~= nil then
    tx = target.x
    ty = target.y
  end

  for slave in iter.slave_iterator(data.bouldername, 1) do
    boulder_generate(slave.x, slave.y, tx,ty)
  end
  -- remove the trap
  dgn.grid(x, y, "floor")
  data.triggered = true

end
}}

######################################
# Simple boulder corridor
# by mumra
# A grate slams shut, meaning you have to run from the boulder
# (or destroy it)
#
NAME:   boulder_corridor_run
DEPTH:  D:8-13
TAGS:   luniq_boulder_trap no_monster_gen
ORIENT: float
KFEAT: ^ = pressure plate trap
KITEM: ^ = *
{{
local tm = TriggerableFunction:new{func="callback.boulder_grate_trap_stepped",
                                   repeated=true,
                                   data = {targetname="target_run", gratename="grate_run", bouldername="boulder_run"} }
tm:add_triggerer(DgnTriggerer:new{type="pressure_plate",})
lua_marker('^', tm)
lua_marker(',', props_marker { grate_run=1 })
lua_marker('A', props_marker { boulder_run=1 })
lua_marker('B', props_marker { target_run=1 })
}}
KFEAT: AB=floor
MAP
xxxxxxxxxxxxxxxxxx
xA..^............B@
xxx,xxxxxxxxxxxxxx
   @
ENDMAP

######################################
# Dual boulder corridor
# by mumra
# Two boulders at either end of the corridor
#
NAME:   boulder_dual_corridor_run
DEPTH:  Lair:3-8
TAGS:   luniq_boulder_trap no_monster_gen ruin_lair
ORIENT: float
KFEAT: ^ = pressure plate trap
KITEM: ^ = *
{{
local tm = TriggerableFunction:new{func="callback.boulder_grate_trap_stepped",
                                   repeated=true,
                                   data = {targetname="target_dual", gratename="grate_dual", bouldername="boulder_dual"} }
tm:add_triggerer(DgnTriggerer:new{type="pressure_plate"})
lua_marker('^', tm)
lua_marker(',', props_marker { grate_dual=1 })
lua_marker('A', props_marker { boulder_dual=1 })
lua_marker('B', props_marker { target_dual=1 })
}}
KFEAT: AB=floor
MAP
xxxxxxxxxxxxxxxxxxx
.A.......^.......A.
xxxxxxxxxxxxxxxxxxx
ENDMAP

########################################
# Quad boulder room
# by mumra
# Four boulders all go for you in the center of the room
# -- TODO: Make all the boulders slightly miss each other
NAME:   boulder_quad_collide
DEPTH:  Lair:4-8
TAGS:   luniq_boulder_trap no_monster_gen
ORIENT: float
KFEAT:  ^ = pressure plate trap
{{
local tm = TriggerableFunction:new{func="callback.boulder_grate_trap_stepped",
                                   repeated=true,
                                   data = {targetname="target_quad", gratename="grate_quad", bouldername="boulder_quad"} }
tm:add_triggerer(DgnTriggerer:new{type="pressure_plate"})
lua_marker('^', tm)
lua_marker('^', props_marker { target_quad=1 })
lua_marker(',', props_marker { grate_quad=1 })
lua_marker('A', props_marker { boulder_quad=1 })
}}
KFEAT: A=floor
KITEM: ^=*
MAP
   xxx@xxx
 xxx.....xxx
 xA.......Ax
xx.........xx
x...........x
x...........x
@.....^.....@
x...........x
x...........x
xx.........xx
 xA.......Ax
 xxx....xxxx
   xxx@xx
ENDMAP

###################################################
# "Indie" (mumra)
# ZorbaBeta's fault
# TODO: Could steer the boulder along the corridor better by placing target markers on the corners and using a monster_move event
NAME:   boulder_indie
DEPTH:  D:7-14, Snake:1-4
TAGS:   luniq_boulder_trap no_monster_gen
ORIENT: float
{{
local tm = TriggerableFunction:new{func="callback.boulder_grate_trap_stepped",
                                   repeated=true,
                                   data = {targetname="target_indie", gratename="grate_indie", bouldername="boulder_indie"} }
tm:add_triggerer(DgnTriggerer:new{type="pressure_plate"})
lua_marker('^', tm)
lua_marker(',', props_marker { grate_indie=1 })
lua_marker('A', props_marker { boulder_indie=1 })
lua_marker('B', props_marker { target_indie=1 })
}}
KFEAT:  ^ = pressure plate trap
SUBST: 2 = x 1 2
SUBST: 1 = 1 2 .:5
KMONS: 1 = ball python
KMONS: 2 = adder
 : dgn.delayed_decay(_G,'a', 'human skeleton')
KFEAT: abAB=floor
KITEM: b = whip w:50 / cursed whip w:10 / whip ego:reaching w:1
MAP
   xxxxx
  xx222xx
 xx21112xx
 x211ab12x
 xx2..12xx
  xx...xx
   xx.xx
    x.xxxxxx
    x......x
    xxxxxx.x
         x.x
xxxxxxx  x.x
xB....x  x.x
x.xxx.x  x.x
x.x x.x  x.x
x^x x.xxxx.x
x.x x......x
x.x xxxxxxxx
xAx
x.x
x@x
ENDMAP
