# A shop (or perhaps really a series of shops) that sells items whose names all
# start with the same letter.
# Any initial class description (amulet, book, manual, potion, ring, scroll,
# staff, wand) or unit of measurement (slice, pair) is ignored when
# alphabetizing item names of the form "X of (the) Y". This way the shops have
# more variety between them.
# Various items, such as carrion, hides, skins, blessed weapons, and zigfigs
# aren't listed. Letters with less than five items don't get shops. Code exists
# for shopless letters anyway, so there's less code editing needed if they get
# new items at some point in the future.

{{
   crawl_require("dlua/util.lua")

   function ownername(s)
     if s == "N" and crawl.one_chance_in( 20 ) then
       return "Nicolae"
     else
       -- The string.lower and .gsub calls are to crunch names with spaces down
       -- into a single word. Otherwise it messes up the "KFEAT" shop line.
       local name = s .. string.sub(string.lower(crawl.make_name()), 2)
       return string.gsub(name, " ", "")
     end
   end

   function shopname(s)
      -- Shop letters that have too few item types to generate still get names
      -- on the off chance they do get five items someday.
      -- X and Z are really hard letters to make shop names with, so it's
      -- a good thing they'll probably be unused for a while.
      local n
      if     s == "A" then n = " type:Alliterative suffix:Accessories "
      elseif s == "B" then n = " type:Bounteous suffix:Boutique "
      elseif s == "C" then n = " type:Commerce suffix:Corner "
      elseif s == "D" then n = " type:Desiderata suffix:Depot "
      elseif s == "E" then n = " type:Eclectic suffix:Emporium "
      elseif s == "F" then n = " type:Fabulous suffix:Fripperies "
      elseif s == "G" then n = " type:Goods suffix:Gallery "
      elseif s == "H" then n = " type:Humungous suffix:Hoard "
      elseif s == "I" then n = " type:Incredible suffix:Inventory "
      elseif s == "J" then n = " type:Jolly suffix:Junkyard "
      elseif s == "K" then n = " type:Killer suffix:Kiosk "
      elseif s == "L" then n = " type:Luxury suffix:Loot "
      elseif s == "M" then n = " type:Merry suffix:Market "
      elseif s == "N" then n = " type:Needful suffix:Niceties "
      elseif s == "O" then n = " type:Offbeat suffix:Offerings "
      elseif s == "P" then n = " type:Practical suffix:Paraphernalia "
      elseif s == "Q" then n = " type:Quotidian suffix:Quintessences "
      elseif s == "R" then n = " type:Retail suffix:Resort "
      elseif s == "S" then n = " type:Superlative suffix:Supplies "
      elseif s == "T" then n = " type:Tempting suffix:Trifles "
      elseif s == "U" then n = " type:Unparalleled suffix:Utilities "
      elseif s == "V" then n = " type:Vending suffix:Venue "
      elseif s == "W" then n = " type:Whimsical suffix:Warehouse "
      elseif s == "X" then n = " type:Xenial suffix:Xenodochy "
      elseif s == "Y" then n = " type:Yearning suffix:Yield "
      elseif s == "Z" then n = " type:Zaibatsu suffix:Zone "
      end
      return n
   end

   function shopcontents(s)
      local i = {}
      if s == "A" then i = { "arbalest", "book of air",
       "manual of air magic", "manual of alchemy", "book of annihilations",
       "potion of ambrosia", "scroll of acquirement q:1",
       "scroll of amnesia", "staff of air", "staff of alchemy",
       "manual of axes", "manual of armour", "wand of acid",
       "acid dragon scales", "potion of attraction", "book of armaments",
       "handbook of applied construction" }

      elseif s == "B" then i = { "bardiche", "battleaxe",
       "book of battle", "book of burglary", "book of blasting",
       "book of blood", "buckler", "boomerang", "blade talisman",
       "broad axe", "pair of boots", "potion of berserk rage",
       "potion of brilliance", "scroll of butterflies",
       "scroll of blinking", "scroll of brand weapon" }

      elseif s == "C" then i = { "book of callings", "book of cantrips",
       "club", "book of conjurations",
       "chain mail", "cloak", "crystal plate armour", "potion of cancellation",
       "staff of cold", "staff of conjuration", "book of callings",
       "book of chaos", "manual of conjurations",
       "potion of curing", "wand of charming", "book of contraptions",
       "book of cryoformation", "codex of conductivity" }

      elseif s == "D" then i = { "book of death", "book of debilitation",
       "book of dreams", "book of the dragon", "dagger", "dragon-coil talisman",
       "demon blade", "demon trident", "demon whip", "dire flail",
       "double sword", "ring of dexterity", "manual of dodging",
       "wand of digging", "dart", "book of dangerous friends",
       "book of displacement", "book of decay", "book of duality" }

      elseif s == "E" then i = { "manual of evocations", "potion of enlightenment",
       "eveningstar", "ring of evasion", "executioner's axe", "eel talisman",
       "potion of experience q:1", "scroll of enchant armour", "staff of earth",
       "book of the earth", "scroll of enchant weapon", "manual of earth magic",
       "everburning encyclopedia" }

      elseif s == "F" then i = { "book of fire", "book of flames", "flail",
       "book of frost", "falchion", "fire dragon scales",
       "scroll of fear", "staff of fire", "fen folio",
       "ring of flight", "scroll of fog", "wand of flame",
       "manual of fighting", "amulet of faith", "manual of fire magic",
       "manual of forgecraft", "book of the fortress", "fortress talisman" }

      elseif s == "G" then i = { "amulet of guardian spirit", "giant club",
       "giant spiked club", "book of geomancy", "granite talisman",
       "glaive", "golden dragon scales", "grand grimoire", "great mace",
       "great sword", "pair of gloves", "book of the grave",
       "great wizards, vol. ii", "great wizards, vol. vii" }

      elseif s == "H" then i = { "halberd", "hand axe",
       "hand cannon", "potion of heal wounds",
       "potion of haste", "manual of hexes", "hat",
       "helmet", "book of hexes", "book of the hunter", "hive talisman" }

      elseif s == "I" then i = { "scroll of immolation",
       "ice dragon scales", "book of ice",
       "potion of invisibility", "ring of intelligence", "scroll of identify",
       "wand of iceblast", "manual of invocations", "manual of ice magic",
       "book of iron", "inkwell talisman" }

      elseif s == "J" then i = { "javelin" }

      elseif s == "K" then i = { "any" }

      elseif s == "L" then i = { "manual of long blades", "large rock",
       "leather armour", "long sword", "potion of lignification", "lajatang",
       "longbow",  "book of lightning", "wand of light", "lupine talisman" }

      elseif s == "M" then i = { "manual of maces & flails",
       "book of minor magic", "book of misfortune", "maw talisman",
       "medusa talisman", "morningstar", "mace", "potion of magic",
       "wand of mindburst", "ring of magical power",
       "amulet of magic regeneration", "potion of might",
       "potion of mutation", "book of the moon", "book of movement",
       "book of maladies", "book of metalworking",
       "maxwell's memoranda", "my sojourn through swampland" }

      elseif s == "N" then i = { "book of necromancy", "staff of necromancy",
       "necronomicon", "scroll of noise", "manual of necromancy" }

      elseif s == "O" then i = { "orcbow" }

      elseif s == "P" then i = { "book of party tricks", "book of power",
       "partisan", "pearl dragon scales", "plate armour",
       "ring of poison resistance", "manual of polearms",
       "wand of polymorph", "ring of positive energy",
       "wand of paralysis", "ring of protection",
       "ring of protection from fire", "scroll of poison",
       "ring of protection from cold" }

      elseif s == "Q" then i = { "quarterstaff", "quicksilver dragon scales",
       "quick blade", "quill talisman" }

      elseif s == "R" then i = { "amulet of reflection",
       "manual of ranged weapons", "wand of roots", "scroll of revelation",
       "amulet of regeneration", "rapier", "ring of resist corrosion",
       "potion of resistance", "robe", "ring mail",
       "rimehorn talisman", "riddle talisman" }

      elseif s == "S" then i = { "scarf", "scimitar", "short sword",
       "scale mail", "spear", "scarab talisman", "spider talisman",
       "serpent talisman", "sanguine talisman", "storm talisman",
       "spore talisman", "ring of see invisible", "ring of slaying",
       "ring of strength", "manual of stealth", "scroll of summoning",
       "book of spatial translocations", "scroll of silence", "shortbow",
       "manual of staves", "steam dragon scales", "manual of shapeshifting",
       "shadow dragon scales", "storm dragon scales", "swamp dragon scales",
       "manual of short blades", "manual of shields", "sling",
       "manual of spellcasting", "manual of summonings", "stone",
       "book of storms", "book of sloth", "book of the senses",
       "book of spectacle", "book of the spheres", "book of scorching",
       "book of spontaneous combustion", "compendium of siegecraft" }

      elseif s == "T" then i = { "scroll of torment",
       "scroll of teleportation", "triple sword", "talisman of death",
       "triple crossbow", "troll leather armour", "manual of throwing",
       "manual of translocations", "trident", "throwing net", "tower shield",
       "book of the tundra", "book of touch", "there-and-back book",
       "trismegistus codex", "treatise on traps"}

      elseif s == "U" then i = { "book of unlife", "manual of unarmed combat",
       "the unrestrained analects" }

      elseif s == "V" then i = { "scroll of vulnerability" }

      elseif s == "W" then i = { "ring of wizardry", "book of the warp", "whip",
       "war axe", "ring of willpower", "book of winter", "book of weapons",
       "book of the wilderness", "book of wicked creation", "wellspring talisman" }

      elseif s == "X" then i = { "any" }

      elseif s == "Y" then i = { "young poisoner's handbook" }

      elseif s == "Z" then i = { "any" }

      else i = { "any" }

      end

      return util.filter(items.pickable, i)
   end

   -- Automatically determine which letters have sufficient variety for a shop,
   -- in other words, that have five or more items, and then pick one.
   function pickletter()
      local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      for i=1, string.len(alphabet) do
         local checkletter = string.sub(alphabet, i, i)
         local itemlist = shopcontents(checkletter)
         local itemcount = #itemlist
         itemcount = itemcount + 1
         if itemcount < 5 then
            alphabet = string.gsub(alphabet, checkletter, "-")
         end
      end
      local options = string.gsub(alphabet, "-", "")
      local number = crawl.random2(string.len(options)) + 1
      local letter = string.sub(options, number, number)
      return letter
   end
}}

NAME: nicolae_alphabet_shops
TAGS: transparent allow_dup extra
DEPTH: D:4-, Depths, Orc, Elf, Snake, Shoals
WEIGHT: 5
# We allow shop lists with no fewer than 5 item types, and place 5-15
# items or place itemcount items, whichever is less, where itemcount is
# the number of item types.
{{
if is_validating() then return end
local letter = pickletter()
local itemlist = shopcontents(letter)
local itemcount = #itemlist
maxcount = math.min(15, itemcount)
-- random2 returns 0 for values <= 1
local shopcount = 5 + crawl.random2(maxcount - 4)
shopcount = math.min(itemcount, shopcount)
inventory = util.join(" | ", util.random_subset(itemlist, shopcount))
tags("uniq_alphashops_" .. letter)
kfeat("s = general shop name:" .. ownername(letter) .. shopname(letter) ..
      " count:" .. shopcount .. " use_all ; " .. inventory)
}}
MAP
 ...
.....
..s..
.....
 ...
ENDMAP
