################################################################################
# Trap: Xom altar
################################################################################

# Xom Altar Trap (by 7hm)
#
# In front of Xom's altar is a useful (preset) item.  He warns you that you shouldn't
# pick it up.  If you approach the altar followed by a monster, Xom disappears when
# you hit the pressure plate, as does the item. If you approach the altar alone, Xom's
# altar disappears and a he turns the empty spaces into a closed corridor.  He then
# spawns a monster at the other end of a corridor. The item you pick up is a wand
# geared towards escape / saving you options but with a small chance of random effects

# XXX: is this function really needed?
{{
-- This checks if there's a monster in the player's line of sight and returns true or false
function callback.see_monster(x, y)
  for _x = x-8,x+8,1 do
    for _y = y-8,y+8,1 do
      if you.see_cell(_x,_y) and dgn.mons_at(_x, _y) then
        return true
      end
    end
  end

  return false
end

function callback.xom_item_pickup(data, triggerable, triggerer, marker, ev)
  local x,y = marker:pos()
  local b = dgn.find_marker_positions_by_prop("monster", 1) [1]

  -- we check to ensure you didn't apport the item. if you did, Xom laughs at your impertinence
  local c = dgn.find_marker_positions_by_prop("item", 1) [1]
  local u_x,u_y = you.pos()
  if (u_x ~= c.x) or (u_y ~= c.y) then
    crawl.god_speaks("Xom", "Xom laughs at your impertinence.")
    return
  end

  -- we check to see if the player's LOS; if there are no monsters in sight we will continue
  -- otherwise we break the function and Xom gives some flavour
  if callback.see_monster(x,y) then
    crawl.god_speaks("Xom","You hear laughter all around you.")
    return
  end

  -- When you pick up the item, Xom reacts visibly
  crawl.god_speaks("Xom","The space around you changes and a monster appears!")
  crawl.god_speaks("Xom","You hear Xom's laughter.")

  -- This enacts the terrain changes; adding the walls and removing the altar
  for slave in iter.slave_iterator("wall_phase", 1) do
    dgn.terrain_changed(slave.x, slave.y, "rock_wall", false, false, false)
  end

  -- This creates the monsters
  if you.absdepth() <= 7 then
    dgn.create_monster(b.x, b.y, 'generate_awake ogre')
  elseif you.absdepth() <= 13 then
    dgn.create_monster(b.x, b.y, 'generate_awake troll')
  elseif you.absdepth() <= 17 then
    dgn.create_monster(b.x, b.y, 'generate_awake rock troll')
  elseif you.absdepth() <= 22 then
    dgn.create_monster(b.x, b.y, 'generate_awake iron troll')
  elseif you.absdepth() <= 27 then
    dgn.create_monster(b.x, b.y, 'generate_awake stone giant ; robe')
  end

end

function callback.xom_trap_step(data, triggerable, triggerer, marker, ev)
  local x,y = marker:pos()
  local c = dgn.find_marker_positions_by_prop("item", 1) [1]

  -- if you already apported (or otherwise got rid of the item) we need to get rid
  -- of the item
  local stack = dgn.items_at(c.x,c.y)
  local item_exist = #stack ~= 0

  if not item_exist then
    crawl.god_speaks("Xom","You sense Xom watching you.")
  else
    -- this checks LOS.  if you see a monster the item is destroyed; if not the item will remain
    -- and Xom warns the player (in his way)
    if callback.see_monster(x,y) then
      -- this eliminates the wand
      crawl.god_speaks("Xom", "Xom laughs as a nearby item is destroyed.")
      iter.stack_destroy(c.x,c.y)
    else
      crawl.god_speaks("Xom", "Xom guffaws as you eye the nearby treasure.")
    end
  end
end

}}


NAME:    trap_god_xom
TAGS:    no_monster_gen no_item_gen
DEPTH:   D:4-27
KITEM:   d = wand of teleportation / wand of digging / w:1 wand of random effects
KFEAT:   B = altar_xom
KPROP:   ^X.Bd|m = no_rtele_into
#MARKER: ^ = lua:props_marker { stop_explore = "an altar to Xom."}

{{
local cf = TriggerableFunction:new{func="callback.xom_item_pickup"}
local tm = TriggerableFunction:new{func="callback.xom_trap_step"}
tm:add_triggerer(DgnTriggerer:new{type="player_move"})
cf:add_triggerer(DgnTriggerer:new{type="item_pickup", target="auto"})
lua_marker('d', cf)
lua_marker('^', tm)
lua_marker('d', props_marker { item = 1 })
lua_marker("m", props_marker { monster = 1})
lua_marker("|", props_marker { wall_phase = 1})
lua_marker(".", props_marker { empty_space = 1})
lua_marker("m", props_marker { empty_space = 2})
lua_marker("d", props_marker { empty_space = 3})
lua_marker("X", props_marker { empty_space = 4})
}}

SUBST:    m|^ : .
SUBST:    X = xx.
MAP
   xxxxxxxxx
  xxxxxxxxxxx
 xxxxxX...XXxx
xxXXXx|||..Xxx
xB.d.^...m...@
xxXXXx|||..Xxx
 xxxxxX...XXxx
  xxxxxxxxxxx
   xxxxxxxxx
ENDMAP
