###############################################################################
#
# Spider's Nest
#
# This is a portal vault adaptation of an upcoming branch for Lair rotation.
# Original branch concept by Lemuel Pitkin.
#
# Spiders, bugs, webs. There should be no bottlenecks for the player to camp
# at. The monsters should always be able to swarm the player. Water (or even
# lava) is good, so that the spiders can use their clinging ability.
#
###############################################################################

{{
function spiders_nest_portal(e)
  local desc_long = "An entrance to the Spider's Nest. It is crumbling away."

  local timeout_turns_long = crawl.random_range(2700, 3300)
  local timeout_turns_short = timeout_turns_long/10

  local messager =
    timed_msg {
      visible = true,
      -- $F{xxx} will be substituted with the 'entity' property of the timed
      -- marker, or with the desc property (if entity is not set).
      messages = time_messages(timeout_turns_short,
                               "$F{The} is slowly crumbling away.",
                               "$F{The} is crumbling away.",
                               "$F{The} is quickly crumbling away.",
                               "$F{The} is almost completely crumbled.")
    }

  e.lua_marker('O',
      timed_marker {
        disappear = "The entrance crumbles away.",
        desc = "A crumbling entrance",
        desc_long = desc_long,
        entity = 'archway',
        dst = "spiders_nest",
        dstname_abbrev = "Spider",
        dstorigin = "in a spider's nest",
        overview = "crumbling entrance",
        turns = timeout_turns_long,
        turns_short = timeout_turns_short,
        floor = "floor",
        msg = messager })
  e.kfeat("O = enter_portal_vault")
  e.colour("O = white")
  e.tile("O = dngn_portal")
end
}}

# For destination (not entry) vaults, use the following line after all
# substitutions have been performed:
# : spiders_nest_colours(_G)
{{
function spiders_nest_colours(e)
  e.kfeat("< = exit_portal_vault")
  e.colour("< = white")
  e.tile("< = dngn_portal")
  e.lrockcol("yellow")
  e.lfloorcol("brown")
  e.lrocktile("wall_yellow_rock")
  e.lfloortile("floor_hive")
end
}}

# Monster set function
# : spiders_nest_random_monster_set(_G)
{{
function spiders_nest_random_monster_set(e)
  e.set_random_mon_list("giant cockroach / giant mite / scorpion / redback / \
                         wolf spider / trapdoor spider / vampire mosquito / \
                         yellow wasp / red wasp / \
                         goliath beetle / boring beetle / boulder beetle / \
                         giant centipede / emperor scorpion / jumping spider / \
                         tarantella / demonic crawler w:2")
                         -- no more ghost moths, please restore for the branch
end
}}

# Add a milestone for entering the Spider's Nest.
{{
function spiders_nest_milestone(e)
  crawl.mark_milestone("br.enter", "entered a Spider's Nest", true)
end
}}

####################################################################
# Functions for the spider trap
{{
function callback.spider_nest_trap(data, triggerable, triggerer, marker, ev)
  local x, y = marker:pos()
  if not you.pos(x, y) then
    -- pretend the monster avoided it
    return
  end
  local spiders = 0
  for p in iter.rect_iterator(dgn.point(x-1, y-1), dgn.point(x+1, y+1)) do
    if (dgn.mons_at(p.x, p.y) == nil and p ~= you.pos() and dgn.is_passable(p.x, p.y)) then
      if (dgn.create_monster(p.x, p.y, "generate_awake spider")) then
        spiders = spiders + 1
      end
    end
  end
 if (spiders > 0) then
   crawl.mpr("A basket of spiders falls from above!")
 end
end

function callback.spider_trap_warn(data, triggerable, triggerer, marker, ev)
    crawl.mpr("You notice webbing strewn about the room and hear a chittering noise in the background." , 1)
    crawl.more()
end
}}

# Flavour text for Spiderweb map ##########################################

{{
function callback.spiders_nest_cobweb_light(data, triggerable, triggerer, marker, ev)
    crawl.mpr("Something lightly brushes against you...",1)
    crawl.more()
    crawl.mpr("You realise it was just a hanging strand of cobweb; you push it aside and calm your nerves." , "talk_visual")
end
}}

{{
function callback.spiders_nest_web_lair(data, triggerable, triggerer, marker, ev)
    crawl.mpr("The chamber up ahead is thickly strewn with cobwebs." , "talk_visual")
end
}}

# Entries ##########################################

NAME:   portal_spiders_nest_entry_a
TAGS:   uniq_spiders_nest patrolling no_monster_gen chance_spiders_nest
CHANCE: 5%
DEPTH:  Lair
: spiders_nest_portal(_G)
MAP
.WWW.WWW.
W.WW.WW.W
WW.W.W.WW
WWW...WWW
....O....
WWW...WWW
WW.W.W.WW
W.WW.WW.W
.WWW.WWW.
ENDMAP

# The destination vaults ############

###############################################################################
# Layout: A quick mockup of diagonal corridors that are always at least two
#         squares wide, so that the player cannot camp at a bottleneck. Also
#         a bit of water as a pointer towards the future wall-clinging ability.
# Foes:   A wave of centipedes, a wave of scorpions, a few scattered trapdoor
#         spiders, and a good bunch of the more threatening spiders, plus a
#         moth of wrath as the boss.
# Loot:   Just some goodish items.
NAME:    spiders_nest_a
TAGS:    spiders_nest no_item_gen no_monster_gen no_pool_fixup
ORIENT:  encompass
: spiders_nest_colours(_G)
: spiders_nest_random_monster_set(_G)
epilogue{{
spiders_nest_milestone(_G)
}}
MONS:    giant centipede
MONS:    scorpion / emperor scorpion w:1
MONS:    trapdoor spider generate_awake / nothing w:20
MONS:    redback / wolf spider
MONS:    jumping spider / tarantella w:20
MONS:    emperor scorpion / demonic crawler
MAP
xxxxx
x...x
x.<.xx        xxxxxx
x....xx       xWWWWx
xxx...xx      xWW.Wx
  xx...xxx   xxW.3.xx
   xx....x  xx......xx
    xx.A.xxxx...xx...xx   xxxxxx
     x....xx...xxxx...xxxxxW4||x
     xxx.1111.xxxxxx..WWWxxWW4|x
       xx1111xxxxxxxx..WWxxWWW4x
       xx1111xxxxxxxx...WxxWWWWx
      xx.1111.xxxxxx..3.WxxxWWxx
     xx...xx...xxxx.....xxxxWWx
  xxxx...xxxx...xx...x..xxxxWWxx
  xW....xxxxxx.2222.xx..xxxWWWWx
  x....xxxxxxxx2222xxx..xxxWWWWx    xxxxx
  x..3.xxxxxxxx2222xxx..xxxW..Wxx   x.**x
  xW....xxxxxx.2222.xx..xx.....Wxxxxx.<*x
  xxxx...xxxx...xx...x..x...xx..Wxxx....x
     xx...xx...xxxx........xxxx..Wx...xxx
      xx......xxxxxx..44..xxxxxx.55..xx
       xx............4554.......5665xx
        xW..3........4554.......5665x
        xWW..xxxxxxxW.44.Wxxxxxx.55.x
        xxxxxx     xWW..WWx    xxxxxx
                   xxxxxxxx
ENDMAP

###############################################################################
# Layout: Octagonal rooms.
# Foes:   A ghost moth and trapdoor spiders.
# Loot:   Random good items, possibility of ambrosia.
NAME:   spiders_nest_the_unseen
TAGS:   spiders_nest no_item_gen no_monster_gen
ORIENT: encompass
WEIGHT: 0
: spiders_nest_colours(_G)
: spiders_nest_random_monster_set(_G)
epilogue{{
spiders_nest_milestone(_G)
}}
MONS:   trapdoor spider, ghost moth
SUBST:  * = **d
ITEM:   piece of ambrosia
MAP
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxx$$$$xxxxxxxxx$$$$xxxxxxxxx$$$$xxxxx
xxxx......xxxxxxx......xxxxxxx......xxxx
xxx........xxxxx........xxxxx........xxx
xx..........xxx..........xxx..........xx
x*..1........x............x...........*x
x|..1..............<...............2..|x
x|..1..............A...............1..|x
x*..1........x............x...........*x
xx..........xxx..........xxx..........xx
xxx........xxxxx........xxxxx........xxx
xxxx......xxxxxxx......xxxxxxx......xxxx
xxxxx$$$$xxxxxxxxx$$$$xxxxxxxxx$$$$xxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ENDMAP

###############################################################################
# Layout: Octagonal room with a pillar and water.
# Foes:   A ghost moth with a few tarantellas.
# Loot:   Ambrosia.
NAME:   spiders_nest_ghost_moth_duel
TAGS:   spiders_nest no_item_gen no_monster_gen generate_awake
ORIENT: encompass
WEIGHT: 0
: spiders_nest_colours(_G)
: spiders_nest_random_monster_set(_G)
epilogue{{
spiders_nest_milestone(_G)
}}
MONS:   ghost moth, tarantella
ITEM:   piece of ambrosia
MAP
xxxxxxxxxxxxxx
xxxxx.<A.xxxxx
xxxx......xxxx
xxx........xxx
xx....WW....xx
x....WxxW....x
x...WxxxxW...x
x.2.WxxxxW.2.x
x.22.WxxW.22.x
xx....WW....xx
xxx...1....xxx
xxxx......xxxx
xxxxxddddxxxxx
xxxxxxxxxxxxxx
ENDMAP

####################################################################
# Scorpion Pit Map by 7hm
# Two corridors leading into a scorpion pit.  Deep water pools showcase spider
# clinging. Secret door at end of pool.
#
NAME:    spiders_nest_scorpion_pit
TAGS:    spiders_nest no_item_gen no_monster_gen no_pool_fixup
ORIENT:  encompass
: spiders_nest_colours(_G)
: spiders_nest_random_monster_set(_G)
epilogue{{
spiders_nest_milestone(_G)
}}
KMONS:  1 = spider
KMONS:  2 = scorpion / wolf spider w:3
KMONS:  3 = trapdoor spider generate_awake / nothing
KMONS: 74 = redback / wolf spider / nothing w:15
KMONS:  5 = jumping spider / tarantella w:20
KMONS:  6 = emperor scorpion
KMONS:  M = fungus
KFEAT: 14 = W
SUBST:  W = WW. , * = ||**WM , X = xxW
KITEM:  t = piece of ambrosia
SUBST:  _ : __x     , - : --x     , ' : ''x     , " : ""x
NSUBST: _ = 2:x/*:. , - = 1:x/*:. , ' = 1:x/*:. , " = 2:x/*:.
MAP
                              xxxx
          xxxxxxxxxxxxxxxxxxxxx''x
          xxxxXXX...WWWWWWxwww4x'x
          xXWWWW...2WW....wwwww''x
         xxW...WWXXXxxxxWW.wxxxxxx
   xxxxxxxX.WW2XXxxxx**xxWWxx
   x1wxxxXWWXXxxxxxx7**xx++xx    xxxx
   xwwwxxWWxxxxwwxxx+xxxx..Wxxxxxx""x
xxxxxwwxx++xwwwwww4x6xxx5..*Wwwww4""x
xA.xxx.WW.WWWwwwww1x.3=...65*Wwwww4xx
x<.WWWWW.WWWWwwwww1x6xxx6..*Wwwww4""x
xA.xxx.WWWWWWwwwwxxx+xxxx..Wxxxxxx""x
xxxxxxxxx++xwwww1xx**xxxx++xx    xxxx
       xx..xxwwwxx7**xxxxxWWx
       xXWWxxx1xxxxxxxxxxWWWx
       xXWWxxxxxxX3WWWXXXWWxx
       xXWWxxxxxxXWWWWW.WW.Wxx
  xxxx xXWWWXxxxxXWWWWWW..WWwxx
  x__xxxwW.WWWxxXXX..XXxxWWWwwxxxx
  x__xwwwwxW.WW.WW.3.Xxxxxxwwww--x
  x__4wwwxxxxx.WWWWxxxxxxxxxxw4x-x
  xxxxxxxxxxxxxxxxxxxxxxxxxxxxx--x
                              xxxx
ENDMAP

##############################################################
# Spider Trap Map by 7hm
# Small winding map leading to a treasure room.  In the room is a
# basket of spiders trap.
#
NAME:   spiders_nest_spider_trap
TAGS:   spiders_nest no_item_gen no_monster_gen no_pool_fixup
ORIENT: encompass
: spiders_nest_colours(_G)
: spiders_nest_random_monster_set(_G)
epilogue{{
spiders_nest_milestone(_G)
}}
NSUBST: * = 1:z / *:*
KITEM:  t = piece of ambrosia
KMONS:  M = fungus
{{
 local tm = TriggerableFunction:new{func="callback.spider_nest_trap"}
 local wm = TriggerableFunction:new{func="callback.spider_trap_warn"}
 tm:add_triggerer(DgnTriggerer:new{type="player_move"})
 wm:add_triggerer(DgnTriggerer:new{type="player_move"})
 lua_marker('z', tm)
 lua_marker('Z', wm)
}}
SUBST:  W = Ww , z : * , * = ||**..M , . = WW.
MONS:   spider w:20 / wolf spider / trapdoor spider generate_awake / \
        redback / tarantella w:2
MAP
xxxxxxxxxxxxxxxx
x1....xxxWWWWxxx
xW.1xW.x......1x
xW.xxxW..xxxxW.x
xxW.xx1xxxWWxx.x
xxxW.xxxxW....Wx
xxx1..xxxW.xx11x
xxxxxx.Wx1.Wxxxx
xxxxxx.Wxxx.Wxxx
xxxxxW.xxxxx.W1x
xxxx1..x***xx.Wx
xxxxW.xx****xx.x
xA<.xxx******Z.x
xxxxxxxxxxxxxxxx
ENDMAP

####################################################################
# Spider Web by Mumra
#
# A main spiral path to follow with randomised walls and pools / deep water.
# Narrow deep water channels run laterally into the center; hopefully spiders
# can pathfind these and keep jumping out at the player. The player could use
# them to head straight for the center with levitate or cling, but it could be
# dangerous getting blocked in.
#
# dpeg: The map can theoretically get a player stranded from a random teleport,
#       but chances are very slim and I'd like a more conceptual solution (like
#       disabling long-range teleports in spider vaults) before doing anything
#       ad-hoc here.
#
NAME:    mumra_spider_spiderweb
TAGS:    spiders_nest no_item_gen no_monster_gen no_pool_fixup
ORIENT:  encompass
NSUBST:  y = 1:y / *:.
{{
local ym = TriggerableFunction:new{func="callback.spiders_nest_cobweb_light"}
ym:add_triggerer(DgnTriggerer:new{type="player_move"})
lua_marker('y', ym)
local vm = TriggerableFunction:new{func="callback.spiders_nest_web_lair"}
vm:add_triggerer(DgnTriggerer:new{type="player_move"})
lua_marker('v', vm)
}}
SUBST:   y = .
SUBST:   v = .
# Water etc.
SUBST:   w = z
SUBST:   W = W w:15 x:4
SUBST:   . = .:6 W
SUBST:   z = W:5 w:50 .:2
SUBST:   X = w x
# Monster sets, and some items
NSUBST:  1 = 2:t / *:w
SUBST:   2 = 2 *:4
KMONS:   2 = tarantella / redback w:12 / jumping spider w:8 / wolf spider w:5
KMONS:   3 = redback
SUBST:   4 = 4 *:3
KMONS:   4 = jumping spider / wolf spider w:5 / spider w:20 / redback w:2
SUBST:   5 = 5 *:1
KMONS:   5 = wolf spider / jumping spider w:3 / spider w:5
SUBST:   6 = 6 *:2
KMONS:   6 = spider / wolf spider w:4 / jumping spider w:1
SUBST:   7 = 7 .:50
KMONS:   7 = trapdoor spider
KITEM:   t = piece of ambrosia
: spiders_nest_colours(_G)
: spiders_nest_random_monster_set(_G)
epilogue{{
spiders_nest_milestone(_G)
}}
MAP
          xxxx                           xxxxx
          xw6x                           x6wwx
          xwxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxwx6x
        xxxxw..WWWW...7..7.WWWWWWWW.7.WWWWwxxx
       xxwwyyx....7.WWWWWWW...7..7.WWW.7.WWxxx
       xwwyww.WWWxxxxxxxxxxxxxxxxxxxWWWwW.7Wxx
      xxwAwxxxwxxxx66xxxxxxxxxxxxxxxxxwxxxW.Wx
      xw<wxxxxxwxx66xxxxxxxWWxxxxxx66wxxxxW.Wxx
      xw.wx  x6xwwwxxxxxxWW.7.WWxxxxxw66xxxW7Wxx
     xxwWxx  xxxxxxwxWW.7..WWW.7.WWWxxwxxxxxW.Wx
     xWWWx       xwW7..WWWXxxxWWW...7wxxxxxxW7Wxxx
     xWwxx      xxW.WwxxxxxxxxxxxXXWw.WxxxxxxW.Xxxx
    xxwxx      xxW.WxxwwxxxxxxxxxxxxwW.WxxxxxxW.W5xx
   xxwxx      xxW.2xxxxxwwxxxxxxxxxwxxW.Wxxx5xxW.Wxx
  xxwxx   xxxxx7.WxxxxxxxxwwxxxxxxwxxxxW7WxxxwwW7Wwxx
  xwxx    x4xx.WWxxxxxxx2...w.22.wxxxxxxW.wwwx5xW.Wwx
  xxx     xxw.WXxxxxxx2..2|x2w..w.xxxxwwwW.WxxxxW.Wxx
          xx7WXxxxxx2..2.x2www.w..2wwwxxxW7WxxxW7Wxx
         xxW.Wxxxxx2.x...ww11wwxwww2xxxxxXW.xxxW.Wx
        xxW.Wxxxxxxx..wwwww11www...2xxxxxXW.xxx.Wxx
        xW.Wxxxxxxwwww.x..www.x.www.2xxxxXW7xxW.3x
  xxxxxxxW.WWxxxwwxxxx....x........wxxxxXW.xxxW7xx
  x4xx44wwW.<WWwxxxxxxxxx......xxxxxwxxxW.WxxW.Wx
 xx34wwwxxWWW.7.WWWxxxxxxxxxxW.WWWWxwxxW7Wxxx.Wxx
 x3ww44xxxxxxWWW.7.WWXxxxxxxxxW7.v.WWwWW.WxxW7Wx
xxwxxx2x    xxxxWWW.WWWXXxxxxxxxxWW..<..Wxxx.WWx
xwxx xxx       xxxxW.7WWW.7.xxxxxxxWWWWWxxx4.Wxx
xxx               xxxx...WWW.7.xxxxxxxxwxxxW74x
                    xxwxxXXWWWW.Wxxxx55w5xW.Wxx
                    x42xxxxxXXWW..Wxxxx5wxW7Wx
                    xxxx   xxxxXWW.7WxxxwxW.Wx
                              xxxxWW..WxwW.Wxx
                                 xxxxW...7WXx
                                    xxxxWWWxx
                                       xxxwx
                                         xxx
ENDMAP
