# This Xom vault features the "Monty Hall" problem.
# There are three doors. The idea is to have the player pick one of the
# doors, two have jellies, one has a friendly goblin.
# If the player approaches one door, Xom shows where one jelly is hidden.
# The player then can choose to open the door or pick the third one.
{{
function dk_xom_monty_hall(door_char, goblin_char)
  local str = "abc"

  str = str:gsub(door_char, "")
  str = str:gsub(goblin_char, "")

  if #str == 2 then
    local r = crawl.one_chance_in(2) and 1 or 2
    str = str:sub(r, r)
  end

  crawl.god_speaks("Xom", "You feel as though Xom is toying with you.")
  crawl.mpr("Suddenly, the walls become transparent!")
  for replica in iter.replica_iterator('char', str) do
    dgn.terrain_changed(replica.x, replica.y, "clear_rock_wall", false)
  end

end

function callback.dk_xom_monty_hall_move(data, triggerable, triggerer, marker, ev)
  if dgn.persist.dk_xom_monty_hall_triggered then
    return
  end

  dk_xom_monty_hall(data.door, data.goblin)

  dgn.persist.dk_xom_monty_hall_triggered = true
end
}}

NAME:    dk_xom_monty_hall
TAGS:    no_item_gen no_monster_gen temple_overflow_xom temple_overflow_1
TAGS:    transparent
WEIGHT:  3
KFEAT:   _ = altar_xom
MONS:    generate_awake att:good_neutral goblin
MONS:    generate_awake jelly
MARKER:  + = lua:props_marker({ door_restrict="veto" })
{{
dgn.persist.dk_xom_monty_hall_triggered = false

local str = "abc"
local r = crawl.random2(2) + 1
local goblin_char = str:sub(r, r)

local mon = {a ='Q',
             b ='W',
             c ='E'}
subst(mon[goblin_char] .. '=1')
str = str:gsub(goblin_char, "")
str:gsub(".", function(c)
  subst(mon[c] .. '=2')
end)

function get_marker(char)
  local marker = TriggerableFunction:new{
    func="callback.dk_xom_monty_hall_move",
    repeated=true,
    data={goblin=goblin_char,
          door=char}
  }

  marker:add_triggerer(DgnTriggerer:new {
  type="player_move"})

  return marker
end

local str = "abc"
str:gsub(".", function(c)
    lua_marker(c, get_marker(c))
    lua_marker(string.upper(c), props_marker {char=c})
end)

}}
SUBST:   abc = .
SUBST:   ABC = x
SUBST:   x = ccbbv
MAP
 xnxnxnx
xn-----nx
n---_---n
x-------x
x+xx+xx+x
AQABWBCEC
A+AB+BC+C
aaabbbccc
@@@@@@@@@
ENDMAP
