# A bot to exercise player-vs-monsters combat and Abyss generation.
#
# Usage: test/stress/abyss
# (or disable the autosave then ./crawl --no-save --rc test/stress/abyss.rc)

name = Abyss_walker
species = mu
background = ck
weapon = mace
restart_after_game = false
show_more = false
autofight_stop = 0

Lua{
bot_start = true
last_turn = -1
time1000 = math.floor(you.time() / 10000)
local cmds = {string.char(9), 'u', 'k', 'l', 'n', '.'}
function ready()
  local esc = string.char(27)
  local eol = string.char(13)
  if you.turns() == 0 and bot_start then
    bot_start = false
    crawl.enable_more(false)
    crawl.sendkeys("&Y" .. esc)
    crawl.sendkeys("&" .. string.char(20) ..
                   "debug.disable('save_checkpoints')" .. eol ..
                   "debug.disable('confirmations')" .. eol ..
                   "debug.disable('death')" .. eol ..
                   "debug.disable('hunger')" .. eol ..
                   "debug.disable('afflictions')" .. eol ..
                   --# don't let them stop us gearing up
                   "debug.disable('mon_act')" .. eol .. esc)
    crawl.sendkeys("&Y" .. esc)
    crawl.sendkeys("&" .. string.char(12) .. "27" .. eol .. "N")
    crawl.sendkeys("&A27" .. eol)
    crawl.sendkeys("&@72 72 72" .. eol)
    crawl.sendkeys("&o)demon whip" .. eol .. "chaos" .. eol)
    crawl.sendkeys("g&o[boots" .. eol .. "running" .. eol)
    crawl.sendkeys("g&o[gold dragon armour" .. eol .. "" .. eol)
    crawl.sendkeys("g&o[large shield" .. eol .. "reflection" .. eol)
    crawl.sendkeys("g&o[helmet" .. eol .. "see invis" .. eol)
    crawl.sendkeys("g&o[gloves" .. eol .. "strength" .. eol)
    crawl.sendkeys("g&o[cloak" .. eol .. "" .. eol)
    crawl.sendkeys("g&o=sustain" .. eol)
    --# somehow ga doesn't work... why?
    crawl.sendkeys("gwcWdWeWfWgWhWiPj")
    crawl.sendkeys("&tca9" .. eol .. "b9" .. eol .. esc)
    crawl.sendkeys("&tda2" .. eol .. esc)
    crawl.sendkeys("&tea12" .. eol .. esc)
    crawl.sendkeys("&tfa3" .. eol .. esc)
    crawl.sendkeys("&tga2" .. eol .. esc)
    crawl.sendkeys("&tha2" .. eol .. esc)
    crawl.sendkeys("&tia2" .. eol .. esc)
    --# re-enable monsters
    crawl.sendkeys("&" .. string.char(20) ..
                   "debug.disable('mon_act', false)" .. eol .. esc)
    --# exit the Abyss then go back, so it's not the special starting Abyss
    crawl.sendkeys("&B")
  elseif bot_start then
    bot_start = false
    crawl.enable_more(false)
    crawl.sendkeys("&Y" .. esc)
    crawl.sendkeys("&" .. string.char(20) ..
                   "debug.disable('save_checkpoints')" .. eol ..
                   "debug.disable('confirmations')" .. eol ..
                   "debug.disable('death')" .. eol ..
                   "debug.disable('hunger')" .. eol ..
                   "debug.disable('afflictions')" .. eol .. esc)
  end
  if you.turns() ~= last_turn then
    command = 1
    last_turn = you.turns()

    -- every 50 turns, cast a spell assigned to an uppercase slot if there is one
    if you.turns() % 50 == 0 then
      local sp = you.spell_letters()
      local to_cast = 0
      local count = 0
      for i = 1, 52 do
        if sp[i] ~= nil and sp[i] >= 'A' and sp[i] <= 'Z' then
          count = count + 1
          if crawl.one_chance_in(count) then
            to_cast = sp[i]
          end
        end
      end
      if to_cast ~= 0 then
        command = 0
        crawl.sendkeys("Z" .. to_cast .. eol .. esc)
        return
      end
    end
  else
    command = command + 1
  end
  --# don't spam
  if (command == 1) and you.feel_safe() then
      command = 2
  end
  crawl.sendkeys(cmds[command])

  --# do only 1000 turns per invocation
  if math.floor(you.time() / 10000) > time1000 then
      crawl.sendkeys(string.char(19))
  end
end
}
