Module:Species

From CrawlWiki
Jump to: navigation, search

Documentation for this module may be created at Module:Species/doc

local p = {}

local data = mw.loadData('Module:Table of species')

local function name_arg(frame)
  local name = frame.args[1]
  if not name or name == "" then
    name = mw.title.getCurrentTitle().text
  end
  return name
end

local function species_plural(species)
  if species == "Mummy" then
    return "Mummies"
  elseif species == "Deep Elf" or species == "High Elf" or species == "Deep Dwarf" then
    return species:sub(1, -2) .. "ves"
  elseif species == "Naga" or species == "Demonspawn" or species == "Tengu" or species == "Merfolk" then
    return species
  end
  return species .. "s"
end

function p.preferred_backgrounds(frame)
  local name = name_arg(frame)
  local species = data[name]

  local categories = {{}, {}, {}, {}, {}}
  local cat_names = {"Warriors", "Adventurers", "Zealots", "Warrior-mages", "Mages"}

  for _, bg in pairs(species.Backgrounds) do
    local cat = categories[5]
    if bg == "Fighter" or bg == "Gladiator" or bg == "Monk" or bg == "Hunter" or bg == "Assassin" then
      cat = categories[1]
    elseif bg == "Artificer" or bg == "Wanderer" then
      cat = categories[2]
    elseif bg == "Berserker" or bg == "Abyssal Knight" or bg == "Chaos Knight" then
      cat = categories[3]
    elseif bg == "Skald" or bg == "Transmuter" or bg == "Warper" or bg == "Arcane Marksman" or bg == "Enchanter" then
      cat = categories[4]
    end
    table.insert(cat, "[[" .. bg .. "]]")
  end

  local result = "==Preferred Backgrounds==\n"
  for i, cat in pairs(categories) do
    if #cat > 0 then
      result = result .. "*'''" .. cat_names[i] .. ":''' " .. table.concat(cat, ", ") .. "\n"
    end
  end
  return result
end

function p.base_stats(frame)
  local name = name_arg(frame)
  local species = data[name]
  local result = ("%s have a base [[Strength]] of %d, [[Intelligence]] of %d, and [[Dexterity]] of %d (before Background modifiers)."):format(species_plural(name), species.Strength, species.Intelligence, species.Dexterity)
  local mp = species.Aptitudes["Magic Points"]
  if mp ~= 0 then
    result = result .. (" They have %+d to base [[magic points]]."):format(mp)
  end
  return result
end

local function mr_gain(species)
  if name == "Draconian" then
    return ("+%d [[magic resistance]] per level (+%d for purple Draconians)."):format(species.MR, data["Purple Draconian"].MR)
  else
    return ("+%d [[magic resistance]] per level."):format(species.MR)
  end
end

local function hp_apt(species)
  local hp = species.Aptitudes["Hit Points"]
  if hp < 0 then
    return ("%d%% less HP than average."):format(-hp*10)
  elseif hp == 0 then
    return "Average [[hit points]]."
  else
    return ("%d%% more HP than average."):format(hp*10)
  end
end

function p.level_bonuses(frame)
  local name = name_arg(frame)
  local species = data[name]
  return ([[* %s
* %s
]]):format(mr_gain(species), hp_apt(species))
end

return p