{{
function callback.grunt_nemelex_gamble_announce(data,triggerable,triggerer,marker,ev)
    if not dgn.persist.grunt_nemelex_gamble_announced then
        dgn.persist.grunt_nemelex_gamble_announced = true
        local msg = {
            "\"Care to risk it all for some fabulous prizes, mortal?\"",
            "\"Through this portal lies a prize... or peril!\"",
            "\"The prize could be yours... at the risk of your life!\"",
            "\"Care for a little wager, mortal?\""
        }
        crawl.god_speaks("Nemelex Xobeh", "Nemelex Xobeh says, " ..
                         msg[crawl.random_range(1, 4)])
        crawl.god_speaks("Nemelex Xobeh", "\"Beware! This is a limited time offer!\"")
    end
end

function callback.grunt_nemelex_gamble_start(data,triggerable,triggerer,marker,ev)
    if dgn.persist.grunt_nemelex_gamble_triggered then
        return
    end
    dgn.persist.grunt_nemelex_gamble_triggered = true
    slave = dgn.find_marker_positions_by_prop("start_tele", 1)
    dgn.terrain_changed(slave[1].x, slave[1].y, "floor", false, false, false)
    if crawl.coinflip() then
        slave = dgn.find_marker_positions_by_prop("prize", 1)
        you.teleport_to(slave[1].x, slave[1].y)
        crawl.god_speaks("Nemelex Xobeh", "Nemelex Xobeh says, \"You win!\"")
    else
        slave = dgn.find_marker_positions_by_prop("peril", 1)
        you.teleport_to(slave[1].x, slave[1].y)
        crawl.god_speaks("Nemelex Xobeh", "Nemelex Xobeh says, \"You lose!\"")
    end
    crawl.mpr("Your surroundings suddenly seem different.")
end

function callback.grunt_nemelex_gamble_finish1(data,triggerable,triggerer,marker,ev)
    slave = dgn.find_marker_positions_by_prop("exit", 1)
    you.teleport_to(slave[1].x, slave[1].y)
    crawl.mpr("Your surroundings suddenly seem different.")
end

function callback.grunt_nemelex_gamble_finish2(data,triggerable,triggerer,marker,ev)
    slave = dgn.find_marker_positions_by_prop("exit", 1)
    you.teleport_to(slave[1].x, slave[1].y)
    crawl.mpr("Your surroundings suddenly seem different.")
end

function callback.grunt_nemelex_gamble_expire(data,triggerable,triggerer,marker,ev)
    if dgn.persist.grunt_nemelex_gamble_triggered then
        return
    end
    if not dgn.persist.grunt_nemelex_gamble_announced then
        return
    end
    data.dur = data.dur - ev:ticks()
    if data.dur > 0 then
        return
    end
    dgn.persist.grunt_nemelex_gamble_triggered = true
    slave = dgn.find_marker_positions_by_prop("start_tele", 1)
    dgn.terrain_changed(slave[1].x, slave[1].y, "floor", false, false, false)
    if (you.see_cell(slave[1].x, slave[1].y)) then
        crawl.mpr("<white>The teleporter glows with a rainbow of weird colours and disappears.</white>")
    end
end
}}

NAME:   grunt_nemelex_the_gamble
TAGS:   temple_overflow_nemelex_xobeh uniq_altar_nemelex_xobeh no_trap_gen no_monster_gen
WEIGHT: 2
DEPTH:  D:2-13
{{
    dgn.persist.grunt_nemelex_gamble_announced = false
    dgn.persist.grunt_nemelex_gamble_triggered = false

    local announce_marker = TriggerableFunction:new {
        func = "callback.grunt_nemelex_gamble_announce",
        repeated = true
        }
    announce_marker:add_triggerer(DgnTriggerer:new {
        type="player_los"})

    local start_marker = TriggerableFunction:new {
        func = "callback.grunt_nemelex_gamble_start",
        repeated = true
        }
    start_marker:add_triggerer(DgnTriggerer:new {
        type="player_move"})

    local finish_prize_marker = TriggerableFunction:new {
        func = "callback.grunt_nemelex_gamble_finish1",
        repeated = true
        }
    finish_prize_marker:add_triggerer(DgnTriggerer:new {
        type="player_move"})

    local finish_peril_marker = TriggerableFunction:new {
        func = "callback.grunt_nemelex_gamble_finish2",
        repeated = true }
    finish_peril_marker:add_triggerer(DgnTriggerer:new {
        type="player_move"})

    local expire_marker = TriggerableFunction:new {
        func = "callback.grunt_nemelex_gamble_expire",
        repeated = true,
        data={dur=500 + crawl.random2(500)}
        }
    expire_marker:add_triggerer(DgnTriggerer:new {
        type="turn"})

    lua_marker('d', props_marker{prize=1})
    lua_marker('e', props_marker{peril=1})
    lua_marker('f', props_marker{exit=1})
    lua_marker('g', finish_prize_marker)
    lua_marker('h', finish_peril_marker)
    lua_marker('H', props_marker{start_tele=1})
    lua_marker('H', expire_marker)
    lua_marker('H', start_marker)
    lua_marker('_', announce_marker)
}}
KFEAT:  _ = altar_nemelex_xobeh
KFEAT:  Hgh = teleporter
KFEAT:  K = fountain_sparkling
KPROP:  UKgh'P89de = no_tele_into
KITEM:  P = any, any, any, superb_item
MARKER: J = lua:fog_machine { cloud_type = "magical condensation", \
                              pow_min = 10, pow_max = 10, delay = 10, \
                              size = 1, walk_dist = 0 }
MAP
xxxxxxxxxxxxxxxxxxx
xXXXXXXXxxxXXXXXXXx
xXU'g'UoJHJoU'h'UXx
xX'''''o._.o'''''Xx
xX''P''o...o'8'9'Xx
xX'''''o...o'''''Xx
xXU'd'Uo...oU'e'UXx
xXXXXXXX.f.XXXXXXXx
xxxxxxxxx+xxxxxxxxx
ENDMAP
