# 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)
     -- 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

   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 alchemy", "book of air",
       "manual of air magic", "book of annihilations", "arrow",
       "potion of ambrosia", "scroll of acquirement q:1",
       "scroll of amnesia", "staff of air",
       "manual of axes", "manual of armour", "wand of acid",
       "acid dragon scales", "potion of attraction", "book of armaments" }

      elseif s == "B" then i = { "bardiche", "battleaxe", "bolt",
       "book of burglary", "box of beasts", "book of blasting",
       "book of blood",
       "broad axe", "pair of boots", "potion of berserk rage",
       "potion of brilliance",
       "scroll of blinking", "scroll of brand weapon",
       "manual of bows", "buckler", "boomerang" }

      elseif s == "C" then i = { "book of callings", "book of cantrips",
       "club", "book of changes", "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 crossbows", "manual of conjurations",
       "potion of curing", "condenser vane", "wand of charming" }

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

      elseif s == "E" then i = { "manual of evocations",
       "eveningstar", "ring of evasion", "executioner's axe",
       "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", "potion of flight",
       "ring of fire", "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",
       "fustibalus" }

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

      elseif s == "H" then i = { "halberd", "hand axe",
       "hand crossbow", "hunting sling", "potion of heal wounds",
       "potion of haste", "scroll of holy word", "manual of hexes", "hat",
       "helmet", "book of hexes", "book of the hunter" }

      elseif s == "I" then i = { "ring of ice",
       "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",
       "inescapable atlas", "book of iron" }

      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", "lightning rod", "book of lightning" }

      elseif s == "M" then i = { "manual of maces & flails",
       "book of minor magic", "book of misfortune", "book of maledictions",
       "morningstar", "mace", "potion of magic", "wand of mindburst",
       "ring of magical power", "amulet of magic regeneration", "potion of might",
       "potion of mutation", "scroll of magic mapping", "book of the moon" }

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

      elseif s == "O" then i = { "ozocubu's autobiography" }

      elseif s == "P" then i = { "book of party tricks", "book of power",
       "pearl dragon scales", "phantom mirror", "phial of floods",
       "plate armour", "ring of poison resistance", "manual of polearms",
       "wand of polymorph", "ring of positive energy", "ring of protection",
       "manual of poison magic", "wand of paralysis",
       "ring of protection from fire", "staff of poison",
       "ring of protection from cold", "book of pain" }

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

      elseif s == "R" then i = { "amulet of reflection",
       "amulet of regeneration", "rapier", "ring of resist corrosion",
       "potion of resistance", "robe", "ring mail", "book of rime" }

      elseif s == "S" then i = { "scarf", "scimitar", "short sword",
       "sling bullet", "scale mail", "book of storms", "book of the sky",
       "ring of see invisible", "ring of slaying", "spear",
       "ring of strength", "manual of stealth", "scroll of summoning",
       "scythe", "book of spatial translocations", "scroll of silence",
       "shortbow", "manual of staves", "steam dragon scales",
       "shadow dragon scales", "storm dragon scales", "swamp dragon scales",
       "manual of short blades", "manual of slings", "manual of shields",
       "manual of spellcasting", "manual of summonings", "stone",
       "book of storms", "book of sloth", "book of stone", "book of the senses",
       "book of spectacle", "book of the spheres" }

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

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

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

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

      elseif s == "X" then i = { "piece from xom's chessboard" }

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

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

      else i = { "any" }

      end

      return 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
