Module:Sandbox/AuronTest: Difference between revisions

From Melvor Idle
(_getItemCategories: Include additional double loot modifiers)
Line 1: Line 1:
-- Used for testing Lua related bits without the possibility of impacting modules that are actively generating page content
--This module contains all sorts of functions for getting data on items
--Several functions related to use tables can be found at Module:Items/UseTables
--Functions related to source tables can be found at Module:Items/SourceTables
--Other functions moved to Module:Items/ComparisonTables
local p = {}
local p = {}


local ItemData = mw.loadData('Module:AuronTest/data')
local MonsterData = mw.loadData('Module:Monsters/data')
local ItemData = mw.loadData('Module:Items/data')
local SkillData = mw.loadData('Module:Skills/data')


local Constants = require('Module:Constants')
local Constants = require('Module:Constants')
local Shared = require('Module:Shared')
local Shared = require('Module:Shared')
local Magic = require('Module:Magic')
local Areas = require('Module:CombatAreas')
local Items = require('Module:Items')
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Agility = require('Module:Skills/Agility')
local Shop = require('Module:Shop')


p.EasterEggs = {'Amulet of Calculated Promotion', 'Clue Chasers Insignia', '8', 'Lemon', 'Easter Egg', 'Abnormal Log', 'Red Herring', 'Cool Glasses'}
p.OtherShopItems = {'Cooking Gloves', 'Mining Gloves', 'Gem Gloves', 'Smithing Gloves', 'Thieving Gloves'}
--This is hardcoded, so there's no easy way to scrape it. Hopefully it doesn't change
p.GemTable = {["Topaz"] = {name = 'Topaz', id = 128, chance = 50},
                  ["Sapphire"] = {name = "Sapphire", id = 129, chance = 17.5},
                  ["Ruby"] = {name = "Ruby", id = 130, chance = 17.5},
                  ["Emerald"] = {name = "Emerald", id = 131, chance = 10},
                  ["Diamond"] = {name = "Diamond", id = 132, chance = 5}}
--The base chance to receive a gem while mining
p.GemChance = .01
--The number of different fishing junk items
p.junkCount = 8
--Items (aside from bars & gems) which can be created via Alt Magic
local AltMagicProducts = {'Rune Essence', 'Bones', 'Holy Dust'}
--The kinds of gloves with cost & charges
p.GloveTable = {['Cooking Gloves'] = {cost=50000, charges=500},
                    ['Mining Gloves'] = {cost=75000, charges=500},
                    ['Smithing Gloves'] = {cost=100000, charges=500},
                    ['Thieving Gloves'] = {cost=100000, charges=500},
                    ['Gem Gloves'] = {cost=500000, charges=2000}}


--Brute forcing some item uses to make things easier
local itemUseArray = {
  Agility = {},
  Attack = {},
  Combat = {'Gold Emerald Ring'},
  Cooking = {'Cooking Gloves', 'Crown of Rhaelyx'},
  Crafting = {'Crown of Rhaelyx'},
  Defence = {},
  Farming = {'Compost', 'Weird Gloop', 'Bob's Rake'},
  Firemaking = {'Crown of Rhaelyx'},
  Fishing = {'Amulet of Fishing', 'Message in a Bottle'},
  Fletching = {'Crown of Rhaelyx'},
  Herblore = {'Crown of Rhaelyx'},
  Hitpoints = {},
  Magic = {},
  Mining = {'Mining Gloves', 'Gem Gloves'},
  Prayer = {},
  Ranged = {},
  Runecrafting = {'Crown of Rhaelyx'},
  Slayer = {},
  Smithing = {'Smithing Gloves', 'Crown of Rhaelyx'},
  Strength = {},
  Summoning = {'Crown of Rhaelyx'},
  Thieving = {'Chapeau Noir', 'Thieving Gloves', 'Gloves of Silence'},
  Woodcutting = {},
  }
local potionUseArray = {
  [0] = 'Combat',
  [1] = 'Combat',
  [2] = 'Combat',
  [3] = 'Combat',
  [4] = 'Combat',
  [5] = 'Combat',
  [6] = 'Combat',
  [7] = 'Woodcutting',
  [8] = 'Fishing',
  [9] = 'Firemaking',
  [10] = 'Cooking',
  [11] = 'Mining',
  [12] = 'Smithing',
  [13] = 'Thieving',
  [14] = 'Farming',
  [15] = 'Fletching',
  [16] = 'Crafting',
  [17] = 'Runecrafting',
  [18] = 'Herblore',
  [19] = 'Combat',
  [20] = 'Combat',
  [21] = 'Combat',
  [22] = 'Combat',
  [23] = 'Combat',
  [24] = 'Agility',
  [25] = 'Summoning'
}


p.specialFishWt = 6722
function p._getItemUses(item, asList, addCategories)
p.specialFishLoot = {{128, 2000}, {129, 1600}, {130, 1400}, {131, 1000}, {132, 400}, {667, 10}, {668, 10}, {902, 1}, {670, 1}, {669, 50}, {120, 250}}
  local useArray = {}
  local categoryArray = {}
  local chr = asList and '* ' or ''
  --Another fun one. This time getting all the different possible ways an item can be used


function p.buildSpecialFishingTable()
   --Before anything else, if this is a potion add it to the appropriate override section
   --This shouldn't ever be included in a page
   if item.masteryID ~= nil and item.masteryID[1] == 19 then
   --This is for generating the above 'specialFishLoot' variable if it ever needs to change
    table.insert(itemUseArray[potionUseArray[item.masteryID[2]]], item.name)
  --To re-run, edit the module, type in "console.log(p.buildSpecialFishingTable())" and copy+paste the result as the new value of the variable
   end
   --Also gives you the total fishing weight for saving time later
  local lootArray = {}
  local totalWt = 0


   for i, item in pairs(ItemData.Items) do
   --If the item has any modifiers that affect a given skill, add it to those lists
    if item.fishingCatchWeight ~= nil then
  if item.modifiers ~= nil then
      totalWt = totalWt + item.fishingCatchWeight
    local skillArray = Constants.getModifierSkills(item.modifiers)
       table.insert(lootArray, '{'..(i - 1)..', '..item.fishingCatchWeight..'}')
    for i, skillName in Shared.skpairs(skillArray) do
       table.insert(itemUseArray[skillName], item.name)
     end
     end
   end
   end


   local result = 'p.specialFishWt = '..totalWt..'\r\n'
   --First things added to the list are non-skill things that are easy to check
   result = result..'p.specialFishLoot = {'..table.concat(lootArray, ', ')..'}'
   if Items.hasCombatStats(item) or Shared.contains(itemUseArray.Combat, item.name) then
   return result
    table.insert(useArray, chr..Icons.Icon({'Combat'}))
end
   end


function p.getSpecialAttackByID(ID)
  if item.healsFor ~= nil then
  local result = Shared.clone(ItemData.SpecialAttacks[ID + 1])
    table.insert(categoryArray, '[[Category:Food Items]]')
  if result ~= nil then
    table.insert(useArray, chr..'[[Food]]')
    result.id = ID
   end
   end
  return result
end


function p.getItemByID(ID)
  if item.dropTable ~= nil then
  local result = Shared.clone(ItemData.Items[ID + 1])
    table.insert(categoryArray, '[[Category:Openable Items]]')
  if result ~= nil then
    table.insert(useArray, chr..'[[Chest Drop Tables|Can Be Opened]]')
    result.id = ID
   end
   end
  return result
end


function p.getItem(name)
  -- Check if the item is an entry requirement for any Slayer area
   local result = nil
   local isSlayerAreaReq = false
   name = string.gsub(name, "%%27", "'")
   if item.isEquipment then
  name = string.gsub(name, "'", "'")
    local slayerAreas = Areas.getAreas(function(area) return area.type == 'slayer' end)
  name = string.gsub(name, "'", "'")
    for i, area in pairs(slayerAreas) do
  for i, item in pairs(ItemData.Items) do
      if area.entryRequirements ~= nil and type(area.entryRequirements) == 'table' then
    local itemName = string.gsub(item.name, '#', '')
        for j, req in pairs(area.entryRequirements) do
    if(name == itemName) then
          if req.type == "SlayerItem" and req.itemID == item.id then
      result = Shared.clone(item)
            isSlayerAreaReq = true
      --Make sure every item has an id, and account for Lua being 1-index
            break
      result.id = i - 1
          end
       break
        end
        if isSlayerAreaReq then break end
       end
     end
     end
   end
   end
  return result
end


function p.getItems(checkFunc)
  --Next, upgrading, crafting, herblore, fletching, and runecrafting since we have to sift through other items for these
   local result = {}
  local canUpgrade = false
   for i, item in pairs(ItemData.Items) do
  local canCraft = false
    if checkFunc(item) then
   local canFletch = false
      local newItem = Shared.clone(item)
   local canRunecraft = false
      newItem.id = i - 1
  local canHerblore = false
      table.insert(result, newItem)
  local canAgile = false
     end
  local canSummon = false
 
  if item.trimmedItemID ~= nil then
     canUpgrade = true
   end
   end
  return result
end


-- Input: equipmentStats property of an item in the format { { ["key"] = "stabAttackBonus", ["value"] = 2 }, ... }
  for i, item2 in pairs(ItemData.Items) do
-- Output: Table in the format { ["stabAttackBonus"] = 2, ... }
    if item2.itemsRequired ~= nil then
function p._processEquipmentStats(equipStats)
      for j, req in pairs(item2.itemsRequired) do
  local out = {}
        if req[1] == item.id then
  if type(equipStats) == 'table' then
          canUpgrade = true
    for i, stat in pairs(equipStats) do
          break
      local k, v = stat["key"], stat["value"]
         end
      if out[k] == nil then
        out[k] = v
      else
         out[k] = out[k] + v
       end
       end
     end
     end
  end
  return out
end


-- Input: equipRequirements property of an item
    if item2.craftReq ~= nil then
-- Output: Table in the format { ["Attack"] = 80, ... }
      for j, req in pairs(item2.craftReq) do
function p._processEquipmentLevelReqs(equipReqs)
        if req.id == item.id then
  local out = {}
          canCraft = true
  if type(equipReqs) == 'table' then
          break
    for i, req in pairs(equipReqs) do
        end
      if req.type == 'Level' then
      end
         for j, levelReq in pairs(req.levels) do
    end
          local skillName = Constants.getSkillName(levelReq.skill)
    if item2.fletchReq ~= nil then
           if skillName ~= nil then out[skillName] = levelReq.level end
      for j, req in pairs(item2.fletchReq) do
        if req.id == item.id then
          canFletch = true
          break
         end
      end
    end
    if item2.runecraftReq ~= nil then
      for j, req in pairs(item2.runecraftReq) do
        if req.id == item.id then
           canRunecraft = true
          break
         end
         end
       end
       end
     end
     end
  end
  return out
end


function p._getItemStat(item, StatName, ZeroIfNil)
    if item2.herbloreReq ~= nil then
  local result = item[StatName]
      for j, req in pairs(item2.herbloreReq) do
  --Special Overrides:
        if req.id == item.id then
  -- Equipment stats first
          canHerblore = true
  if Shared.contains(ItemData.EquipmentStatKeys, StatName) then
          break
    local equipStats = p._processEquipmentStats(item.equipmentStats)
        end
    result = equipStats[StatName]
       end
  elseif StatName == 'isTwoHanded' then
    if item.validSlots ~= nil and item.occupiesSlots ~= nil then
      return Shared.contains(item.validSlots, 'Weapon') and Shared.contains(item.occupiesSlots, 'Shield')
    else
       return false
     end
     end
  elseif string.find(StatName, '^(.+)LevelRequired$') ~= nil then
 
     local skillName = Shared.titleCase(string.match(StatName, '^(.+)LevelRequired$'))
     if item2.summoningReq ~= nil then
    if skillName ~= nil then
       for j, reqSet in pairs(item2.summoningReq) do
       local levelReqs = p._processEquipmentLevelReqs(item.equipRequirements)
        for k, req in pairs(reqSet) do
      result = levelReqs[skillName]
          if req.id == item.id then
    end
            canSummon = true
  elseif StatName == 'attackType' then
            break
    result = p._getWeaponAttackType(item)
          end
  elseif StatName == 'description' then
        end
    result = item.description
       end
    if result == nil or result == '' then result = 'No Description' end
  elseif StatName == 'completionReq' then
    if item.ignoreCompletion == nil or not item.ignoreCompletion then
      return 'Yes'
    else
       return 'No'
     end
     end
  elseif StatName == 'slayerBonusXP' then
    return p._getItemModifier(item, 'increasedSkillXP', 'Slayer', false)
   end
   end
  if result == nil and ZeroIfNil then result = 0 end
  return result
end


function p.getItemStat(frame)
  --Check if Agility applies here
   local args = frame.args ~= nil and frame.args or frame
  canAgile = Shared.tableCount(Agility.getObstaclesForItem(item.id)) > 0
   local ItemName = args[1]
 
   local StatName = args[2]
  --Agility
   local ZeroIfNil = args.ForceZero ~= nil and args.ForceZero ~= '' and args.ForceZero ~= 'false'
   if canAgile or Shared.contains(itemUseArray.Agility, item.name) then
   local formatNum = args.formatNum ~= nil and args.formatNum ~= '' and args.formatNum ~= 'false'
    table.insert(useArray, chr..Icons.Icon({'Agility', type='skill'}))
   local item = p.getItem(ItemName)
  end
   if item == nil then
  --Cooking
     return "ERROR: No item named "..ItemName.." exists in the data module[[Category:Pages with script errors]]"
  if item.cookedItemID ~= nil or Shared.contains(itemUseArray.Cooking, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Cooking', type='skill'}))
  end
  --Crafting
  if canCraft or Shared.contains(itemUseArray.Crafting, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Crafting', type='skill'}))
  end
  --Farming
   if item.grownItemID ~= nil or Shared.contains(itemUseArray.Farming, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Farming', type='skill'}))
  end
   --Firemaking
   if item.firemakingID ~= nil or Shared.contains(itemUseArray.Firemaking, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Firemaking', type='skill'}))
  end
  --Fishing
  if Shared.contains(itemUseArray.Fishing, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Fishing', type='skill'}))
  end
  --Fletching
  if canFletch or Shared.contains(itemUseArray.Fletching, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Fletching', type='skill'}))
  end
  --Herblore
  if canHerblore or Shared.contains(itemUseArray.Herblore, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Herblore', type='skill'}))
  end
  --Mining
   if Shared.contains(itemUseArray.Mining, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Mining', type='skill'}))
  end
  --Prayer
  if item.prayerPoints ~= nil or (Shared.contains(itemUseArray.Prayer, item.name) and not Shared.contains(itemUseArray.Combat, item.name)) then
    if item.prayerPoints ~= nil then table.insert(categoryArray, '[[Category:Buriable Items]]') end
    table.insert(useArray, chr..Icons.Icon({'Prayer', type='skill'}))
  end
  --Runecrafting
  if canRunecraft or Shared.contains(itemUseArray.Runecrafting, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Runecrafting', type='skill'}))
  end
  --Slayer
  if isSlayerAreaReq or Shared.contains(itemUseArray.Slayer, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Slayer', type='skill'}))
  end
  --Smithing
   if item.type == 'Bar' or item.type == 'Ore' or Shared.contains(itemUseArray.Smithing, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Smithing', type='skill'}))
  end
  --Summoning
   if canSummon or (item.type == 'Shard' and item.category == 'Summoning') or item.type == 'Familiar' or Shared.contains(itemUseArray.Summoning, item.name) then
     table.insert(useArray, chr..Icons.Icon({'Summoning', type='skill'}))
  end
  --Thieving
  if Shared.contains(itemUseArray.Thieving, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Thieving', type='skill'}))
   end
   end
   local result = p._getItemStat(item, StatName, ZeroIfNil)
   --Woodcutting
   if formatNum then result = Shared.formatnum(result) end
   if Shared.contains(itemUseArray.Woodcutting, item.name) then
  return result
    table.insert(useArray, chr..Icons.Icon({'Woodcutting', type='skill'}))
end
 
--Gets the value of a given modifier for a given item
--asString is false by default, when true it writes the full bonus text
function p._getItemModifier(item, modifier, skill, asString)
  if asString == nil then asString = false end
  if skill == '' then
    skill = nil
  elseif type(skill) == 'string' then
    skill = Constants.getSkillID(skill)
   end
   end


   local result = 0
   --Other odds and ends:


   if item.modifiers ~= nil and item.modifiers[modifier] ~= nil then
  --Mastery Tokens are tied to 'Mastery'
     if type(item.modifiers[modifier]) == 'table' then
   if item.isToken and item.skill ~= nil then
      for i, subVal in Shared.skpairs(item.modifiers[modifier]) do
     table.insert(useArray, chr..Icons.Icon({'Mastery'}))
        if subVal[1] == skill then
          result = subVal[2]
          break
        end
      end
    else
      result = item.modifiers[modifier]
    end
   end
   end


   if asString then
  --Skillcapes are tied to the appropriate skill
     if skill ~= nil then
  --Except Max Skillcape, which is tied to all skills. (And so is the Signet Ring)
      return Constants._getModifierText(modifier, {skill, result})
  --And combat skillcapes, since combat skills don't get special treatment
     else
 
       return Constants._getModifierText(modifier, result)
  local ignoreCapes = {'Ranged Skillcape', 'Attack Skillcape', 'Strength Skillcape', 'Hitpoints Skillcape', 'Defence Skillcape'}
   if Shared.contains({'Max Skillcape', 'Aorpheat's Signet Ring', 'Cape of Completion'}, item.name) then
     table.insert(useArray, chr..'All skills')
  elseif item.name == 'Magic Skillcape' then
    table.insert(useArray, chr..Icons.Icon({'Magic', type='skill'}))
    table.insert(useArray, chr..Icons.Icon({'Alt. Magic', type='skill'}))
  elseif Shared.contains(item.name, 'Skillcape') and not Shared.contains(ignoreCapes, item.name) then
     local skillName = Shared.splitString(item.name, ' ')[1]
    --Avoiding double-listing the same skill twice
    if not Shared.contains(itemUseArray[skillName], item.name) then
       table.insert(useArray, chr..Icons.Icon({skillName, type='skill'}))
     end
     end
  else
    return result
   end
   end
end


function p.hasCombatStats(item)
  if Shared.contains(item.name, 'Skillcape') or item.name == 'Cape of Completion' then table.insert(categoryArray, '[[Category:Skillcapes]]') end
  if not item.isEquipment or item.validSlots == nil and item.equipmentStats ~= nil then
    -- Ensure at least one stat has a non-zero value
    local equipStats = p._processEquipmentStats(item.equipmentStats)
    for statName, statVal in pairs(equipStats) do
      if statVal ~= 0 then return true end
    end
  end
  return false
end


function p.getItemModifier(frame)
  --Special note for Charge Stone of Rhaelyx
   local itemName = frame.args ~= nil and frame.args[1] or frame[1]
   if item.name == 'Charge Stone of Rhaelyx' then
  local modName = frame.args ~= nil and frame.args[2] or frame[2]
    table.insert(useArray, chr..'Powering '..Icons.Icon({'Crown of Rhaelyx', type='item'}))
  local skillName = frame.args ~= nil and frame.args[3] or frame[3]
  local asString = frame.args ~= nil and frame.args[4] or frame[4]
  if asString ~= nil then
    if string.upper(asString) == 'FALSE' then
    asString = false
    else
    asString = true
    end
   end
   end


   local item = p.getItem(itemName)
  --Some items are needed to make shop purchases
   if item == nil then
   local shopArray = Shop.getItemCostArray(item.id)
     return "ERROR: No item named "..itemName.." exists in the data module[[Category:Pages with script errors]]"
   if Shared.tableCount(shopArray) > 0 then
     table.insert(useArray, chr..Icons.Icon({'Shop'}))
   end
   end


  return p._getItemModifier(item, modName, skillName, asString)
   if canUpgrade then
end
     if item.canUpgrade or (item.type == 'Armour' and item.canUpgrade == nil) then
 
       table.insert(categoryArray, '[[Category:Upgradeable Items]]')
function p._getWeaponAttackType(item)
   if item.isEquipment == true and item.validSlots ~= nil and Shared.contains(item.validSlots, 'Weapon') then
     if Shared.contains({'melee', 'ranged', 'magic'}, item.attackType) then
      local iconType = item.attackType ~= 'melee' and 'skill' or nil
       return Icons.Icon({Shared.titleCase(item.attackType), type=iconType, nolink='true'})
     end
     end
    table.insert(useArray, chr..'[[Upgrading Items]]')
   end
   end
   return 'Invalid'
 
   local result = asList and table.concat(useArray,'\r\n') or table.concat(useArray, '<br/>')
  if addCategories then result = result..table.concat(categoryArray, '') end
  return result
end
end


function p.getWeaponAttackType(frame)
function p.getItemUses(frame)
   local itemName = frame.args ~= nil and frame.args[1] or frame
   local itemName = frame.args ~= nil and frame.args[1] or frame
   local item = p.getItem(itemName)
   local item = Items.getItem(itemName)
  local addCategories = false
  local asList = true
  if frame.args ~= nil then
    addCategories = frame.args.addCategories ~= nil and frame.args.addCategories ~= '' and frame.args.addCategories ~= 'false'
    asList = frame.args.addCategories == nil or frame.args.addCategories == '' or frame.args.addCategories == 'true'
  end
   if item == nil then
   if item == nil then
     return "ERROR: No item named "..ItemName.." exists in the data module[[Category:Pages with script errors]]"
     return "ERROR: No item named "..itemName.." exists in the data module"
   end
   end
   return p._getWeaponAttackType(item)
 
   return p._getItemUses(item, asList, addCategories)
end
end


function p.getPotionTable(frame)
function p._getItemUseTable(item)
   local potionName = frame.args ~= nil and frame.args[1] or frame
   local useArray = {}
   local tiers = {'I', 'II', 'III', 'IV'}
   local potTierMastery = {[0] = 0, [1] = 20, [2] = 50, [3] = 90}


   local result = '{| class="wikitable"'
   --First, loop through all items to find anything that can be made or upgraded into using our source
  result = result..'\r\n!Potion!!Tier!!Charges!!Effect'
  for i, item2 in pairs(ItemData.Items) do
    if item2.itemsRequired ~= nil then
      for j, req in pairs(item2.itemsRequired) do
        if req[1] == item.id then
          local mat = item2.itemsRequired
          local xp = 'N/A'
          local rowReq = 'None'
          --Potions do have upgrade requirements though
          if item2.potionTier ~= nil then
            rowReq = Icons._MasteryReq(item2.name, potTierMastery[item2.potionTier])
          end
          table.insert(useArray, {item = item2, qty = 1, mats = mat, skill = 'Upgrade', req = rowReq, xp = xp, gp = item2.trimmedGPCost})
          break
        end
      end
    end
    if item2.craftReq ~= nil then
      for j, req in pairs(item2.craftReq) do
        if req.id == item.id then
          local mat = item2.craftReq
          local xp = item2.craftingXP
          local rowReq = item2.craftingLevel
          local qty = item2.craftQty
          table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Crafting', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.fletchReq ~= nil then
      for j, req in pairs(item2.fletchReq) do
        if req.id == item.id then
          local xp = item2.fletchingXP
          local rowReq = item2.fletchingLevel
          --Arrow Shafts are special and have to be treated specially
          local qty = item2.fletchQty
          local mat = item2.fletchReq
          if item2.name == 'Arrow Shafts' then
            mat = {{id = item.id, qty = 1}}
            qty =  qty + (qty * item.id)
          end
          table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Fletching', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.smithReq ~= nil then
      for j, req in pairs(item2.smithReq) do
        if req.id == item.id then
          local mat = item2.smithReq
          local xp = item2.smithingXP
          local rowReq = item2.smithingLevel
          local qty = item2.smithingQty
          table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Smithing', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.runecraftReq ~= nil then
      for j, req in pairs(item2.runecraftReq) do
        if req.id == item.id then
          local mat = item2.runecraftReq
          local xp = item2.runecraftingXP
          local rowReq = item2.runecraftingLevel
          local qty = item2.runecraftQty
          table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Runecrafting', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.herbloreReq ~= nil then
      for j, req in pairs(item2.herbloreReq) do
        if req.id == item.id then
          local potionData = SkillData.Herblore.ItemData[item2.masteryID[2] + 1]
          local mat = item2.herbloreReq
          local xp = potionData.herbloreXP
          --Potions do have upgrade requirements though
          local rowReq = Icons._SkillReq('Herblore', potionData.herbloreLevel)
          local masteryLvl = potTierMastery[item2.potionTier]
          if masteryLvl > 0 then
            rowReq = rowReq..'<br/>'..Icons._MasteryReq(item2.name, masteryLvl)
          end
          local reqVal = potionData.herbloreLevel + (masteryLvl * 0.01)
          table.insert(useArray, {item = item2, qty = 1, mats = mat, skill = 'Herblore', reqVal = reqVal, req = rowReq, xp = xp})
          break
        end
      end
    end


  local tier1potion = p.getItem(potionName..' I')
    if item2.summoningReq ~= nil then
  for i, tier in pairs(tiers) do
      for j, reqSet in pairs(item2.summoningReq) do
    local tierName = potionName..' '..tier
        for k, req in pairs(reqSet) do
    local potion = p.getItemByID(tier1potion.id + i - 1)
          if req.id == item.id then
    if potion ~= nil then
            local mat = Shared.clone(reqSet)
      result = result..'\r\n|-'
            mat[k].qty = math.max(math.floor(1000 / math.max(item.sellsFor, 20)), 1)
      result = result..'\r\n|'..Icons.Icon({tierName, type='item', notext='true', size='60'})
            local xp = 5 + 2 * math.floor(item2.summoningLevel * 0.2)
      result = result..'||'..'[['..tierName..'|'..tier..']]'
            local rowReq = item2.summoningLevel
       result = result..'||'..potion.potionCharges..'||'..potion.description
            table.insert(useArray, {item = item2, qty = 25, mats = mat, skill = 'Summoning', req = rowReq, xp = xp})
          end
        end
       end
     end
     end
  end


  result = result..'\r\n|}'
  return result
end
function p._getOtherItemBoxText(item)
  result = ''
  --For equipment, show the slot they go in
  if item.validSlots ~= nil then
    result = result.."\r\n|-\r\n|'''Equipment Slot:''' "..table.concat(item.validSlots, ', ')
   end
   end
  --For weapons with a special attack, show the details
   if item.grownItemID ~= nil then
   if item.hasSpecialAttack then
     local item2 = Items.getItemByID(item.grownItemID)
     local spAttPart = {}
     local mat = {{id = item.id, qty = item.seedsRequired}}
    table.insert(spAttPart, "\r\n|-\r\n|'''Special Attack:'''")
    local xp = item.farmingXP
     for i, spID in ipairs(item.specialAttacks) do
    local rowReq = item.farmingLevel
      local spAtt = p.getSpecialAttackByID(spID)
    local qty = 5
      table.insert(spAttPart, '\r\n* ' .. spAtt.defaultChance .. '% chance for ' .. spAtt.name .. ':')
    table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Farming', req = rowReq, xp = xp})
      table.insert(spAttPart, '\r\n** ' .. spAtt.description)
    end
    result = result .. table.concat(spAttPart)
   end
   end
  --For potions, show the number of charges
   if item.cookedItemID ~= nil then
   if item.potionCharges ~= nil then
     local item2 = Items.getItemByID(item.cookedItemID)
     result = result.."\r\n|-\r\n|'''Charges:''' "..item.potionCharges
    local mat = {{id = item.id, qty = 1}}
    local xp = item.cookingXP
    local rowReq = item.cookingLevel
    local qty = 1
    table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Cooking', req = rowReq, xp = xp})
   end
   end
  --For food, show how much it heals for
   if item.burntItemID ~= nil then
   if item.healsFor ~= nil then
     local item2 = Items.getItemByID(item.burntItemID)
     result = result.."\r\n|-\r\n|'''Heals for:''' "..Icons.Icon({"Hitpoints", type="skill", notext="true"})..' '..(item.healsFor * 10)
    local mat = {{id = item.id, qty = 1}}
    local xp = 1
    local rowReq = item.cookingLevel
    local qty = 1
    table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Cooking', req = rowReq, xp = xp})
   end
   end
   --For Prayer Points, show how many you get
 
   if item.prayerPoints ~= nil then
   --Handle shop purchases using Module:Shop
     result = result.."\r\n|-\r\n|'''"..Icons.Icon({'Prayer', type='skill'}).." Points:''' "..item.prayerPoints
   local shopUses = Shop.getItemCostArray(item.id)
  for i, purchase in Shared.skpairs(shopUses) do
    local rowReq = Shop.getRequirementString(purchase.unlockRequirements)
     local iconType = (purchase.contains.items ~= nil and Shared.tableCount(purchase.contains.items) > 0) and 'item' or 'upgrade'
    table.insert(useArray, {item = {name = purchase.name}, qty = 1, mats = purchase.cost.items, skill = 'Shop', req = rowReq, xp = 'N/A', gp = purchase.cost.gp, type = iconType})
   end
   end
   --For items with modifiers, show what those are
 
   if item.modifiers ~= nil and Shared.tableCount(item.modifiers) > 0 then
   --Finally build the table using what we've learned
    result = result.."\r\n|-\r\n|'''Modifiers:'''\r\n"..Constants.getModifiersText(item.modifiers, true)
   table.sort(useArray, function(a, b)
                        local aReqVal = a.reqVal ~= nil and a.reqVal or a.req
                        local bReqVal = b.reqVal ~= nil and b.reqVal or b.req
                        if a.skill ~= b.skill then
                          return a.skill < b.skill
                        elseif type(aReqVal) ~= type(bReqVal) then
                          return tostring(aReqVal) < tostring(bReqVal)
                        elseif aReqVal ~= bReqVal then
                          return aReqVal < bReqVal
                        else
                          return a.item.name < b.item.name
                        end end)
 
  local obstacles = Agility.getObstaclesForItem(item.id)
 
  local spellUseTable = p._getSpellUseTable(item)
  local result = ''
  if Shared.tableCount(useArray) == 0 and Shared.tableCount(obstacles) == 0 then
    if string.len(spellUseTable) > 0 then
      return '==Uses==\r\n==='..Icons.Icon({'Magic', type='skill', size='30'})..'===\r\n'..spellUseTable
    else
      return ''
    end
   end
   end
   return result
   result = result..'{| class="wikitable sortable"'
end
  result = result..'\r\n!colspan=2|Item Created!!Type!!Requirements!!XP!!Ingredients'
  for i, row in pairs(useArray) do
    local qty = row.qty ~= nil and row.qty or 1
    local iconType = row.type ~= nil and row.type or 'item'
    result = result..'\r\n|-\r\n|data-sort-value="'..row.item.name..'"|'
    result = result..Icons.Icon({row.item.name, type=iconType, notext=true, size=50})..'||'
    if qty > 1 then result = result.."'''"..qty.."x''' " end
    result = result..'[['..row.item.name..']]'
    if row.skill == 'Upgrade' then
      result = result..'||data-sort-value="Upgrade"|[[Upgrading Items|Upgrade]]'
    elseif row.skill == 'Shop' then
      result = result..'||data-sort-value="Shop"|'..Icons.Icon({'Shop'})
    else
      result = result..'||data-sort-value="'..row.skill..'"|'..Icons.Icon({row.skill, type='skill'})
    end
    if type(row.req) == 'number' then
      result = result..'|| data-sort-value="'..row.req..'"|'..Icons._SkillReq(row.skill, row.req)
    elseif row.reqVal ~= nil then
      result = result..'|| data-sort-value="'..row.reqVal..'"|'..row.req
    else
      result = result..'||'..row.req
    end
    if type(row.xp) == 'string' then
      result = result..'||data-sort-value="0"|'..row.xp
    else
      result = result..'||data-sort-value="'..row.xp..'"|'..row.xp..' '..Icons.Icon({row.skill, type='skill', notext=true})..' XP'
    end
    result = result..'||'
    for i, mat in Shared.skpairs(row.mats) do
      local matID = mat.id ~= nil and mat.id or mat[1]
      local matQty = mat.qty ~= nil and mat.qty or mat[2]
      local matText = ''


function p.getOtherItemBoxText(frame)
      if i > 1 then result = result..'<br/>' end
  local itemName = frame.args ~= nil and frame.args[1] or frame
      if matID >= 0 then
  local item = p.getItem(itemName)
        -- Item
  local asList = false
        local matItem = Items.getItemByID(matID)
  if frame.args ~= nil then
        if matItem == nil then
    asList = frame.args.asList ~= nil and frame.args.asList ~= '' and frame.args.asList ~= 'false'
          matText = 'ERROR: Failed to find item with ID ' .. matID .. '[[Category:Pages with Script Errors]]'
  end
        else
  if item == nil then
          matText = Icons.Icon({matItem.name, type='item', qty=matQty})
    return "ERROR: No item named "..itemName.." exists in the data module[[Category:Pages with script errors]]"
        end
      elseif matID == -4 then
        -- GP
        matText = Icons.GP(SkillData.Summoning.Settings.recipeGPCost)
      elseif matID == -5 then
        -- Slayer Coins
        matText = Icons.SC(SkillData.Summoning.Settings.recipeGPCost)
      else
        matText = 'ERROR: Unknown item ID: ' .. matID .. ' [[Category:Pages with Script Errors]]'
      end
      result = result .. matText
    end
    if row.gp ~= nil then result = result..'<br/>'..Icons.GP(row.gp) end
   end
   end


   return p._getOtherItemBoxText(item, asList)
   --Agility obstacles are weird and get their own section
end
  for i, obst in Shared.skpairs(obstacles) do
    result = result..'\r\n|-\r\n|'
    result = result..Icons.Icon({"Agility", type="skill", size="50", notext=true})
    result = result..'||[[Agility#Obstacles|'..obst.name..']]'
    result = result..'||'..Icons.Icon({"Agility", type="skill"})


function p._getItemCategories(item)
    --Adding the requirements for the Agility Obstacle
  local result = ''
    local reqArray = {}
  if item.category ~= nil then result = result..'[[Category:'..item.category..']]' end
    if obst.category == nil then --nil category means this is a passive pillar
  if item.type ~= nil then result = result..'[[Category:'..item.type..']]' end
      table.insert(reqArray, Icons._SkillReq('Agility', 99))
  if item.tier ~= nil then result = result..'[[Category:'..Shared.titleCase(item.tier)..' '..item.type..']]' end
    elseif obst.category > 0 then --Otherwise it's category * 10
  if item.hasSpecialAttack then result = result..'[[Category:Items With Special Attacks]]' end
       table.insert(reqArray, Icons._SkillReq('Agility', obst.category * 10))
  if item.validSlots ~= nil then
    local slotRemap = {
      ['Passive'] = 'Passive Items',
       ['Summon1'] = 'Summoning Familiars',
      ['Summon2'] = ''
    }
    for i, slotName in ipairs(item.validSlots) do
      local slotRemapName = slotName
      if slotRemap[slotName] ~= nil then slotRemapName = slotRemap[slotName] end
      if slotRemapName ~= '' then result = result .. '[[Category:' .. slotRemapName .. ']]' end
     end
     end
  end
    --Then the other skill levels if any are added
  if item.modifiers ~= nil then
    if obst.requirements ~= nil and obst.requirements.skillLevel ~= nil then
    local modsDL = {
       for j, req in Shared.skpairs(obst.requirements.skillLevel) do
      'increasedChanceToDoubleLootCombat',
        table.insert(reqArray, Icons._SkillReq(Constants.getSkillName(req[1]), req[2]))
      'decreasedChanceToDoubleLootCombat',
      'increasedChanceToDoubleLootThieving',
      'decreasedChanceToDoubleLootThieving',
      'increasedChanceToDoubleItemsGlobal',
       'decreasedChanceToDoubleItemsGlobal'
    }
    for modName, val in pairs(item.modifiers) do
      if Shared.contains(modsDL, modName) then
        result = result..'[[Category:Double Loot Chance Items]]'
        break
       end
       end
     end
     end
    result = result..'||'..table.concat(reqArray, '<br/>')
    --Just including 'N/A' for XP since it doesn't really apply for Agility Obstacles
    result = result..'||N/A'
    --Finally the cost
    local cost = obst.cost
    local costArray = {}
    if cost.gp ~= nil and cost.gp > 0 then
      table.insert(costArray, Icons.GP(cost.gp))
    end
    if cost.slayerCoins ~= nil and cost.slayerCoins > 0 then
      table.insert(costArray, Icons.SC(cost.slayerCoins))
    end
    for j, mat in Shared.skpairs(cost.items) do
      local item = Items.getItemByID(mat[1])
      table.insert(costArray, Icons.Icon({item.name, type="item", qty=mat[2]}))
    end
    result = result..'||'..table.concat(costArray, '<br/>')
  end
  result = result..'\r\n|}'
  if string.len(spellUseTable) > 0 then
    result = result..'\r\n==='..Icons.Icon({'Magic', type='skill', size='30'})..'===\r\n'..spellUseTable
   end
   end
   return result
   return '==Uses==\r\n'..result
end
end


function p.getItemCategories(frame)
function p.getItemUseTable(frame)
   local itemName = frame.args ~= nil and frame.args[1] or frame
   local itemName = frame.args ~= nil and frame.args[1] or frame
   local item = p.getItem(itemName)
   local item = Items.getItem(itemName)
   if item == nil then
   if item == nil then
     return "ERROR: No item named "..itemName.." exists in the data module[[Category:Pages with script errors]]"
     return "ERROR: No item named "..itemName.." exists in the data module"
   end
   end


   return p._getItemCategories(item)
   return p._getItemUseTable(item)
end
end


function p.getSkillcapeTable(frame)
function p._getSpellUseTable(item)
   local skillName = frame.args ~= nil and frame.args[1] or frame
   local spellList = Magic.getSpellsForRune(item.id)
  local cape = p.getItem(skillName..' Skillcape')
   --Bail immediately if no spells are found
   local result = '{| class="wikitable"\r\n'
   if Shared.tableCount(spellList) == 0 then
   result = result..'!Skillcape!!Name!!Effect'
    return ''
  result = result..'\r\n|-\r\n|'..Icons.Icon({cape.name, type='item', size='60', notext=true})
   end
  result = result..'||[['..cape.name..']]||'..cape.description
  result = result..'\r\n|}'
   return result
end


function p.getItemGrid(frame)
  local result = '{|class="wikitable sortable"\r\n!colspan="2"|Spell'
   result = '{|'
  result = result..'!!Requirements'
   for i, item in Shared.skpairs(ItemData.Items) do
   result = result..'!!Type!!style="width:275px"|Description'
     if i % 17 == 1 then
  result = result..'!!Runes'
      result = result..'\r\n|-\r\n|'
   for i, spell in pairs(spellList) do
     local rowTxt = '\r\n|-\r\n|data-sort-value="'..spell.name..'"|'
    if spell.type == 'Auroras' then
      rowTxt = rowTxt..Icons.Icon({spell.name, type='aurora', notext=true, size=50})
    elseif spell.type == 'Curses' then
      rowTxt = rowTxt..Icons.Icon({spell.name, type='curse', notext=true, size=50})
     else
     else
       result = result..'||'
       rowTxt = rowTxt..Icons.Icon({spell.name, type='spell', notext=true, size=50})
     end
     end
     result = result..'style="padding:3px"|'..Icons.Icon({item.name, type='item', notext=true, size='40'})
     rowTxt = rowTxt..'||[['..spell.name..']]'
    rowTxt = rowTxt..'||data-sort-value="'..spell.magicLevelRequired..'"|'..Icons._SkillReq('Magic', spell.magicLevelRequired)
    --Handle required items/dungeon clears
    if spell.requiredItem ~= nil and spell.requiredItem >= 0 then
      local reqItem = Items.getItemByID(spell.requiredItem)
      rowTxt = rowTxt..'<br/>'..Icons.Icon({reqItem.name, type='item', notext=true})..' equipped'
    end
    if spell.requiredDungeonCompletion ~= nil then
      local dung = Areas.getAreaByID('dungeon', spell.requiredDungeonCompletion[1])
      rowTxt = rowTxt..'<br/>'..Icons.Icon({dung.name, type='dungeon', notext=true, qty=spell.requiredDungeonCompletion[2]})..' Clears'
    end
    rowTxt = rowTxt..'||data-sort-value="'..Magic.getSpellTypeIndex(spell.type)..'"|'
    rowTxt = rowTxt..Magic.getSpellTypeLink(spell.type)
    rowTxt = rowTxt..'||'..Magic._getSpellStat(spell, 'description')
    rowTxt = rowTxt..'||style="text-align:center"|'
    rowTxt = rowTxt..Magic._getSpellRunes(spell)
    result = result..rowTxt
   end
   end
  --Add the table end and add the table to the result string
   result = result..'\r\n|}'
   result = result..'\r\n|}'
   return result
   return result
end
end


function p.getSpecialAttackTable(frame)
function p.getSpellUseTable(frame)
   local spAttTable = {}
   local itemName = frame.args ~= nil and frame.args[1] or frame
 
  local item = Items.getItem(itemName)
  for i, item in Shared.skpairs(ItemData.Items) do
  if item == nil then
    if item.hasSpecialAttack then
    return "ERROR: No item named "..itemName.." exists in the data module"
      for i, spID in ipairs(item.specialAttacks) do
        local spAtt = p.getSpecialAttackByID(spID)
        if spAttTable[spID] == nil then spAttTable[spID] = {sortName=item.name, Icons = {}} end
        table.insert(spAttTable[spID].Icons, Icons.Icon({item.name, type='item'}))
      end
    end
   end
   end


   local result = '{|class="wikitable sortable stickyHeader"'
   return p._getSpellUseTable(item)
  result = result..'\r\n|-class="headerRow-0"'
  result = result..'\r\n!style="min-width:180px"|Weapon(s)!!Name!!Chance!!Effect'
  for i, spAttData in Shared.skpairs(spAttTable) do
    local spAtt = p.getSpecialAttackByID(i)
    table.sort(spAttData.Icons, function(a, b) return a < b end)
    result = result..'\r\n|-'
    result = result..'\r\n|data-sort-value="'..spAttData.sortName..'"|'..table.concat(spAttData.Icons, '<br/>')
    result = result..'||'..spAtt.name..'||data-sort-value="'..spAtt.defaultChance..'"|'..spAtt.defaultChance..'%'
    result = result..'||'..spAtt.description
  end
  result = result..'\r\n|}'
 
  return result
end
end


return p
return p

Revision as of 21:18, 21 August 2021

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

local p = {}

local MonsterData = mw.loadData('Module:Monsters/data')
local ItemData = mw.loadData('Module:Items/data')
local SkillData = mw.loadData('Module:Skills/data')

local Constants = require('Module:Constants')
local Shared = require('Module:Shared')
local Magic = require('Module:Magic')
local Areas = require('Module:CombatAreas')
local Items = require('Module:Items')
local Icons = require('Module:Icons')
local Agility = require('Module:Skills/Agility')
local Shop = require('Module:Shop')


--Brute forcing some item uses to make things easier
local itemUseArray = {
  Agility = {},
  Attack = {},
  Combat = {'Gold Emerald Ring'},
  Cooking = {'Cooking Gloves', 'Crown of Rhaelyx'},
  Crafting = {'Crown of Rhaelyx'},
  Defence = {},
  Farming = {'Compost', 'Weird Gloop', 'Bob&apos;s Rake'},
  Firemaking = {'Crown of Rhaelyx'},
  Fishing = {'Amulet of Fishing', 'Message in a Bottle'},
  Fletching = {'Crown of Rhaelyx'},
  Herblore = {'Crown of Rhaelyx'},
  Hitpoints = {},
  Magic = {},
  Mining = {'Mining Gloves', 'Gem Gloves'},
  Prayer = {},
  Ranged = {},
  Runecrafting = {'Crown of Rhaelyx'},
  Slayer = {},
  Smithing = {'Smithing Gloves', 'Crown of Rhaelyx'},
  Strength = {},
  Summoning = {'Crown of Rhaelyx'},
  Thieving = {'Chapeau Noir', 'Thieving Gloves', 'Gloves of Silence'},
  Woodcutting = {},
  }
local potionUseArray = {
  [0] = 'Combat',
  [1] = 'Combat',
  [2] = 'Combat',
  [3] = 'Combat',
  [4] = 'Combat',
  [5] = 'Combat',
  [6] = 'Combat',
  [7] = 'Woodcutting',
  [8] = 'Fishing',
  [9] = 'Firemaking',
  [10] = 'Cooking',
  [11] = 'Mining',
  [12] = 'Smithing',
  [13] = 'Thieving',
  [14] = 'Farming',
  [15] = 'Fletching',
  [16] = 'Crafting',
  [17] = 'Runecrafting',
  [18] = 'Herblore',
  [19] = 'Combat',
  [20] = 'Combat',
  [21] = 'Combat',
  [22] = 'Combat',
  [23] = 'Combat',
  [24] = 'Agility',
  [25] = 'Summoning'
}

function p._getItemUses(item, asList, addCategories)
  local useArray = {}
  local categoryArray = {}
  local chr = asList and '* ' or ''
  --Another fun one. This time getting all the different possible ways an item can be used

  --Before anything else, if this is a potion add it to the appropriate override section
  if item.masteryID ~= nil and item.masteryID[1] == 19 then
    table.insert(itemUseArray[potionUseArray[item.masteryID[2]]], item.name)
  end

  --If the item has any modifiers that affect a given skill, add it to those lists
  if item.modifiers ~= nil then
    local skillArray = Constants.getModifierSkills(item.modifiers)
    for i, skillName in Shared.skpairs(skillArray) do
      table.insert(itemUseArray[skillName], item.name)
    end
  end

  --First things added to the list are non-skill things that are easy to check
  if Items.hasCombatStats(item) or Shared.contains(itemUseArray.Combat, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Combat'}))
  end

  if item.healsFor ~= nil then
    table.insert(categoryArray, '[[Category:Food Items]]')
    table.insert(useArray, chr..'[[Food]]')
  end

  if item.dropTable ~= nil then
    table.insert(categoryArray, '[[Category:Openable Items]]')
    table.insert(useArray, chr..'[[Chest Drop Tables|Can Be Opened]]')
  end

  -- Check if the item is an entry requirement for any Slayer area
  local isSlayerAreaReq = false
  if item.isEquipment then
    local slayerAreas = Areas.getAreas(function(area) return area.type == 'slayer' end)
    for i, area in pairs(slayerAreas) do
      if area.entryRequirements ~= nil and type(area.entryRequirements) == 'table' then
        for j, req in pairs(area.entryRequirements) do
          if req.type == "SlayerItem" and req.itemID == item.id then
            isSlayerAreaReq = true
            break
          end
        end
        if isSlayerAreaReq then break end
      end
    end
  end

  --Next, upgrading, crafting, herblore, fletching, and runecrafting since we have to sift through other items for these
  local canUpgrade = false
  local canCraft = false
  local canFletch = false
  local canRunecraft = false
  local canHerblore = false
  local canAgile = false
  local canSummon = false

  if item.trimmedItemID ~= nil then
    canUpgrade = true
  end

  for i, item2 in pairs(ItemData.Items) do
    if item2.itemsRequired ~= nil then
      for j, req in pairs(item2.itemsRequired) do
        if req[1] == item.id then
          canUpgrade = true
          break
        end
      end
    end

    if item2.craftReq ~= nil then
      for j, req in pairs(item2.craftReq) do
        if req.id == item.id then
          canCraft = true
          break
        end
      end
    end
    if item2.fletchReq ~= nil then
      for j, req in pairs(item2.fletchReq) do
        if req.id == item.id then
          canFletch = true
          break
        end
      end
    end
    if item2.runecraftReq ~= nil then
      for j, req in pairs(item2.runecraftReq) do
        if req.id == item.id then
          canRunecraft = true
          break
        end
      end
    end

    if item2.herbloreReq ~= nil then
      for j, req in pairs(item2.herbloreReq) do
        if req.id == item.id then
          canHerblore = true
          break
        end
      end
    end

    if item2.summoningReq ~= nil then
      for j, reqSet in pairs(item2.summoningReq) do
        for k, req in pairs(reqSet) do
          if req.id == item.id then
            canSummon = true
            break
          end
        end
      end
    end
  end

  --Check if Agility applies here
  canAgile = Shared.tableCount(Agility.getObstaclesForItem(item.id)) > 0

  --Agility
  if canAgile or Shared.contains(itemUseArray.Agility, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Agility', type='skill'}))
  end
  --Cooking
  if item.cookedItemID ~= nil or Shared.contains(itemUseArray.Cooking, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Cooking', type='skill'}))
  end
  --Crafting
  if canCraft or Shared.contains(itemUseArray.Crafting, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Crafting', type='skill'}))
  end
  --Farming
  if item.grownItemID ~= nil or Shared.contains(itemUseArray.Farming, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Farming', type='skill'}))
  end
  --Firemaking
  if item.firemakingID ~= nil or Shared.contains(itemUseArray.Firemaking, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Firemaking', type='skill'}))
  end
  --Fishing
  if Shared.contains(itemUseArray.Fishing, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Fishing', type='skill'}))
  end
  --Fletching
  if canFletch or Shared.contains(itemUseArray.Fletching, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Fletching', type='skill'}))
  end
  --Herblore
  if canHerblore or Shared.contains(itemUseArray.Herblore, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Herblore', type='skill'}))
  end
  --Mining
  if Shared.contains(itemUseArray.Mining, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Mining', type='skill'}))
  end
  --Prayer
  if item.prayerPoints ~= nil or (Shared.contains(itemUseArray.Prayer, item.name) and not Shared.contains(itemUseArray.Combat, item.name)) then
    if item.prayerPoints ~= nil then table.insert(categoryArray, '[[Category:Buriable Items]]') end
    table.insert(useArray, chr..Icons.Icon({'Prayer', type='skill'}))
  end
  --Runecrafting
  if canRunecraft or Shared.contains(itemUseArray.Runecrafting, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Runecrafting', type='skill'}))
  end
  --Slayer
  if isSlayerAreaReq or Shared.contains(itemUseArray.Slayer, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Slayer', type='skill'}))
  end
  --Smithing
  if item.type == 'Bar' or item.type == 'Ore' or Shared.contains(itemUseArray.Smithing, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Smithing', type='skill'}))
  end
  --Summoning
  if canSummon or (item.type == 'Shard' and item.category == 'Summoning') or item.type == 'Familiar' or Shared.contains(itemUseArray.Summoning, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Summoning', type='skill'}))
  end
  --Thieving
  if Shared.contains(itemUseArray.Thieving, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Thieving', type='skill'}))
  end
  --Woodcutting
  if Shared.contains(itemUseArray.Woodcutting, item.name) then
    table.insert(useArray, chr..Icons.Icon({'Woodcutting', type='skill'}))
  end

  --Other odds and ends:

  --Mastery Tokens are tied to 'Mastery'
  if item.isToken and item.skill ~= nil then
    table.insert(useArray, chr..Icons.Icon({'Mastery'}))
  end

  --Skillcapes are tied to the appropriate skill
  --Except Max Skillcape, which is tied to all skills. (And so is the Signet Ring)
  --And combat skillcapes, since combat skills don't get special treatment

  local ignoreCapes = {'Ranged Skillcape', 'Attack Skillcape', 'Strength Skillcape', 'Hitpoints Skillcape', 'Defence Skillcape'}
  if Shared.contains({'Max Skillcape', 'Aorpheat&apos;s Signet Ring', 'Cape of Completion'}, item.name) then
    table.insert(useArray, chr..'All skills')
  elseif item.name == 'Magic Skillcape' then
    table.insert(useArray, chr..Icons.Icon({'Magic', type='skill'}))
    table.insert(useArray, chr..Icons.Icon({'Alt. Magic', type='skill'}))
  elseif Shared.contains(item.name, 'Skillcape') and not Shared.contains(ignoreCapes, item.name) then
    local skillName = Shared.splitString(item.name, ' ')[1]
    --Avoiding double-listing the same skill twice
    if not Shared.contains(itemUseArray[skillName], item.name) then
      table.insert(useArray, chr..Icons.Icon({skillName, type='skill'}))
    end
  end

  if Shared.contains(item.name, 'Skillcape') or item.name == 'Cape of Completion' then table.insert(categoryArray, '[[Category:Skillcapes]]') end

  --Special note for Charge Stone of Rhaelyx
  if item.name == 'Charge Stone of Rhaelyx' then
    table.insert(useArray, chr..'Powering '..Icons.Icon({'Crown of Rhaelyx', type='item'}))
  end

  --Some items are needed to make shop purchases
  local shopArray = Shop.getItemCostArray(item.id)
  if Shared.tableCount(shopArray) > 0 then
    table.insert(useArray, chr..Icons.Icon({'Shop'}))
  end

  if canUpgrade then
    if item.canUpgrade or (item.type == 'Armour' and item.canUpgrade == nil) then
      table.insert(categoryArray, '[[Category:Upgradeable Items]]')
    end
    table.insert(useArray, chr..'[[Upgrading Items]]')
  end

  local result = asList and table.concat(useArray,'\r\n') or table.concat(useArray, '<br/>')
  if addCategories then result = result..table.concat(categoryArray, '') end
  return result
end

function p.getItemUses(frame)
  local itemName = frame.args ~= nil and frame.args[1] or frame
  local item = Items.getItem(itemName)
  local addCategories = false
  local asList = true
  if frame.args ~= nil then
    addCategories = frame.args.addCategories ~= nil and frame.args.addCategories ~= '' and frame.args.addCategories ~= 'false'
    asList = frame.args.addCategories == nil or frame.args.addCategories == '' or frame.args.addCategories == 'true'
  end
  if item == nil then
    return "ERROR: No item named "..itemName.." exists in the data module"
  end

  return p._getItemUses(item, asList, addCategories)
end

function p._getItemUseTable(item)
  local useArray = {}
  local potTierMastery = {[0] = 0, [1] = 20, [2] = 50, [3] = 90}

  --First, loop through all items to find anything that can be made or upgraded into using our source
  for i, item2 in pairs(ItemData.Items) do
    if item2.itemsRequired ~= nil then
      for j, req in pairs(item2.itemsRequired) do
        if req[1] == item.id then
          local mat = item2.itemsRequired
          local xp = 'N/A'
          local rowReq = 'None'
          --Potions do have upgrade requirements though
          if item2.potionTier ~= nil then
            rowReq = Icons._MasteryReq(item2.name, potTierMastery[item2.potionTier])
          end
          table.insert(useArray, {item = item2, qty = 1, mats = mat, skill = 'Upgrade', req = rowReq, xp = xp, gp = item2.trimmedGPCost})
          break
        end
      end
    end
    if item2.craftReq ~= nil then
      for j, req in pairs(item2.craftReq) do
        if req.id == item.id then
          local mat = item2.craftReq
          local xp = item2.craftingXP
          local rowReq = item2.craftingLevel
          local qty = item2.craftQty
          table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Crafting', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.fletchReq ~= nil then
      for j, req in pairs(item2.fletchReq) do
        if req.id == item.id then
          local xp = item2.fletchingXP
          local rowReq = item2.fletchingLevel
          --Arrow Shafts are special and have to be treated specially
          local qty = item2.fletchQty
          local mat = item2.fletchReq
          if item2.name == 'Arrow Shafts' then
            mat = {{id = item.id, qty = 1}}
            qty =  qty + (qty * item.id)
          end
          table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Fletching', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.smithReq ~= nil then
      for j, req in pairs(item2.smithReq) do
        if req.id == item.id then
          local mat = item2.smithReq
          local xp = item2.smithingXP
          local rowReq = item2.smithingLevel
          local qty = item2.smithingQty
          table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Smithing', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.runecraftReq ~= nil then
      for j, req in pairs(item2.runecraftReq) do
        if req.id == item.id then
          local mat = item2.runecraftReq
          local xp = item2.runecraftingXP
          local rowReq = item2.runecraftingLevel
          local qty = item2.runecraftQty
          table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Runecrafting', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.herbloreReq ~= nil then
      for j, req in pairs(item2.herbloreReq) do
        if req.id == item.id then
          local potionData = SkillData.Herblore.ItemData[item2.masteryID[2] + 1]
          local mat = item2.herbloreReq
          local xp = potionData.herbloreXP
          --Potions do have upgrade requirements though
          local rowReq = Icons._SkillReq('Herblore', potionData.herbloreLevel)
          local masteryLvl = potTierMastery[item2.potionTier]
          if masteryLvl > 0 then
            rowReq = rowReq..'<br/>'..Icons._MasteryReq(item2.name, masteryLvl)
          end
          local reqVal = potionData.herbloreLevel + (masteryLvl * 0.01)
          table.insert(useArray, {item = item2, qty = 1, mats = mat, skill = 'Herblore', reqVal = reqVal, req = rowReq, xp = xp})
          break
        end
      end
    end

    if item2.summoningReq ~= nil then
      for j, reqSet in pairs(item2.summoningReq) do
        for k, req in pairs(reqSet) do
          if req.id == item.id then
            local mat = Shared.clone(reqSet)
            mat[k].qty = math.max(math.floor(1000 / math.max(item.sellsFor, 20)), 1)
            local xp = 5 + 2 * math.floor(item2.summoningLevel * 0.2)
            local rowReq = item2.summoningLevel
            table.insert(useArray, {item = item2, qty = 25, mats = mat, skill = 'Summoning', req = rowReq, xp = xp})
          end
        end
      end
    end

  end
  if item.grownItemID ~= nil then
    local item2 = Items.getItemByID(item.grownItemID)
    local mat = {{id = item.id, qty = item.seedsRequired}}
    local xp = item.farmingXP
    local rowReq = item.farmingLevel
    local qty = 5
    table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Farming', req = rowReq, xp = xp})
  end
  if item.cookedItemID ~= nil then
    local item2 = Items.getItemByID(item.cookedItemID)
    local mat = {{id = item.id, qty = 1}}
    local xp = item.cookingXP
    local rowReq = item.cookingLevel
    local qty = 1
    table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Cooking', req = rowReq, xp = xp})
  end
  if item.burntItemID ~= nil then
    local item2 = Items.getItemByID(item.burntItemID)
    local mat = {{id = item.id, qty = 1}}
    local xp = 1
    local rowReq = item.cookingLevel
    local qty = 1
    table.insert(useArray, {item = item2, qty = qty, mats = mat, skill = 'Cooking', req = rowReq, xp = xp})
  end

  --Handle shop purchases using Module:Shop
  local shopUses = Shop.getItemCostArray(item.id)
  for i, purchase in Shared.skpairs(shopUses) do
    local rowReq = Shop.getRequirementString(purchase.unlockRequirements)
    local iconType = (purchase.contains.items ~= nil and Shared.tableCount(purchase.contains.items) > 0) and 'item' or 'upgrade'
    table.insert(useArray, {item = {name = purchase.name}, qty = 1, mats = purchase.cost.items, skill = 'Shop', req = rowReq, xp = 'N/A', gp = purchase.cost.gp, type = iconType})
  end

  --Finally build the table using what we've learned
  table.sort(useArray, function(a, b)
                         local aReqVal = a.reqVal ~= nil and a.reqVal or a.req
                         local bReqVal = b.reqVal ~= nil and b.reqVal or b.req
                         if a.skill ~= b.skill then
                           return a.skill < b.skill
                         elseif type(aReqVal) ~= type(bReqVal) then
                           return tostring(aReqVal) < tostring(bReqVal)
                         elseif aReqVal ~= bReqVal then
                           return aReqVal < bReqVal
                         else
                           return a.item.name < b.item.name
                         end end)

  local obstacles = Agility.getObstaclesForItem(item.id)

  local spellUseTable = p._getSpellUseTable(item)
  local result = ''
  if Shared.tableCount(useArray) == 0 and Shared.tableCount(obstacles) == 0 then
    if string.len(spellUseTable) > 0 then
      return '==Uses==\r\n==='..Icons.Icon({'Magic', type='skill', size='30'})..'===\r\n'..spellUseTable
    else
      return ''
    end
  end
  result = result..'{| class="wikitable sortable"'
  result = result..'\r\n!colspan=2|Item Created!!Type!!Requirements!!XP!!Ingredients'
  for i, row in pairs(useArray) do
    local qty = row.qty ~= nil and row.qty or 1
    local iconType = row.type ~= nil and row.type or 'item'
    result = result..'\r\n|-\r\n|data-sort-value="'..row.item.name..'"|'
    result = result..Icons.Icon({row.item.name, type=iconType, notext=true, size=50})..'||'
    if qty > 1 then result = result.."'''"..qty.."x''' " end
    result = result..'[['..row.item.name..']]'
    if row.skill == 'Upgrade' then
      result = result..'||data-sort-value="Upgrade"|[[Upgrading Items|Upgrade]]'
    elseif row.skill == 'Shop' then
      result = result..'||data-sort-value="Shop"|'..Icons.Icon({'Shop'})
    else
      result = result..'||data-sort-value="'..row.skill..'"|'..Icons.Icon({row.skill, type='skill'})
    end
    if type(row.req) == 'number' then
      result = result..'|| data-sort-value="'..row.req..'"|'..Icons._SkillReq(row.skill, row.req)
    elseif row.reqVal ~= nil then
      result = result..'|| data-sort-value="'..row.reqVal..'"|'..row.req
    else
      result = result..'||'..row.req
    end
    if type(row.xp) == 'string' then
      result = result..'||data-sort-value="0"|'..row.xp
    else
      result = result..'||data-sort-value="'..row.xp..'"|'..row.xp..' '..Icons.Icon({row.skill, type='skill', notext=true})..' XP'
    end
    result = result..'||'
    for i, mat in Shared.skpairs(row.mats) do
      local matID = mat.id ~= nil and mat.id or mat[1]
      local matQty = mat.qty ~= nil and mat.qty or mat[2]
      local matText = ''

      if i > 1 then result = result..'<br/>' end
      if matID >= 0 then
        -- Item
        local matItem = Items.getItemByID(matID)
        if matItem == nil then
          matText = 'ERROR: Failed to find item with ID ' .. matID .. '[[Category:Pages with Script Errors]]'
        else
          matText = Icons.Icon({matItem.name, type='item', qty=matQty})
        end
      elseif matID == -4 then
        -- GP
        matText = Icons.GP(SkillData.Summoning.Settings.recipeGPCost)
      elseif matID == -5 then
        -- Slayer Coins
        matText = Icons.SC(SkillData.Summoning.Settings.recipeGPCost)
      else
        matText = 'ERROR: Unknown item ID: ' .. matID .. ' [[Category:Pages with Script Errors]]'
      end
      result = result .. matText
    end
    if row.gp ~= nil then result = result..'<br/>'..Icons.GP(row.gp) end
  end

  --Agility obstacles are weird and get their own section
  for i, obst in Shared.skpairs(obstacles) do
    result = result..'\r\n|-\r\n|'
    result = result..Icons.Icon({"Agility", type="skill", size="50", notext=true})
    result = result..'||[[Agility#Obstacles|'..obst.name..']]'
    result = result..'||'..Icons.Icon({"Agility", type="skill"})

    --Adding the requirements for the Agility Obstacle
    local reqArray = {}
    if obst.category == nil then --nil category means this is a passive pillar
      table.insert(reqArray, Icons._SkillReq('Agility', 99))
    elseif obst.category > 0 then --Otherwise it's category * 10
      table.insert(reqArray, Icons._SkillReq('Agility', obst.category * 10))
    end
    --Then the other skill levels if any are added
    if obst.requirements ~= nil and obst.requirements.skillLevel ~= nil then
      for j, req in Shared.skpairs(obst.requirements.skillLevel) do
        table.insert(reqArray, Icons._SkillReq(Constants.getSkillName(req[1]), req[2]))
      end
    end
    result = result..'||'..table.concat(reqArray, '<br/>')

    --Just including 'N/A' for XP since it doesn't really apply for Agility Obstacles
    result = result..'||N/A'

    --Finally the cost
    local cost = obst.cost
    local costArray = {}
    if cost.gp ~= nil and cost.gp > 0 then
      table.insert(costArray, Icons.GP(cost.gp))
    end
    if cost.slayerCoins ~= nil and cost.slayerCoins > 0 then
      table.insert(costArray, Icons.SC(cost.slayerCoins))
    end
    for j, mat in Shared.skpairs(cost.items) do
      local item = Items.getItemByID(mat[1])
      table.insert(costArray, Icons.Icon({item.name, type="item", qty=mat[2]}))
    end

    result = result..'||'..table.concat(costArray, '<br/>')

  end

  result = result..'\r\n|}'
  if string.len(spellUseTable) > 0 then
    result = result..'\r\n==='..Icons.Icon({'Magic', type='skill', size='30'})..'===\r\n'..spellUseTable
  end
  return '==Uses==\r\n'..result
end

function p.getItemUseTable(frame)
  local itemName = frame.args ~= nil and frame.args[1] or frame
  local item = Items.getItem(itemName)
  if item == nil then
    return "ERROR: No item named "..itemName.." exists in the data module"
  end

  return p._getItemUseTable(item)
end

function p._getSpellUseTable(item)
  local spellList = Magic.getSpellsForRune(item.id)
  --Bail immediately if no spells are found
  if Shared.tableCount(spellList) == 0 then
    return ''
  end

  local result = '{|class="wikitable sortable"\r\n!colspan="2"|Spell'
  result = result..'!!Requirements'
  result = result..'!!Type!!style="width:275px"|Description'
  result = result..'!!Runes'
  for i, spell in pairs(spellList) do
    local rowTxt = '\r\n|-\r\n|data-sort-value="'..spell.name..'"|'
    if spell.type == 'Auroras' then
      rowTxt = rowTxt..Icons.Icon({spell.name, type='aurora', notext=true, size=50})
    elseif spell.type == 'Curses' then
      rowTxt = rowTxt..Icons.Icon({spell.name, type='curse', notext=true, size=50})
    else
      rowTxt = rowTxt..Icons.Icon({spell.name, type='spell', notext=true, size=50})
    end
    rowTxt = rowTxt..'||[['..spell.name..']]'
    rowTxt = rowTxt..'||data-sort-value="'..spell.magicLevelRequired..'"|'..Icons._SkillReq('Magic', spell.magicLevelRequired)
    --Handle required items/dungeon clears
    if spell.requiredItem ~= nil and spell.requiredItem >= 0 then
      local reqItem = Items.getItemByID(spell.requiredItem)
      rowTxt = rowTxt..'<br/>'..Icons.Icon({reqItem.name, type='item', notext=true})..' equipped'
    end
    if spell.requiredDungeonCompletion ~= nil then
      local dung = Areas.getAreaByID('dungeon', spell.requiredDungeonCompletion[1])
      rowTxt = rowTxt..'<br/>'..Icons.Icon({dung.name, type='dungeon', notext=true, qty=spell.requiredDungeonCompletion[2]})..' Clears'
    end
    rowTxt = rowTxt..'||data-sort-value="'..Magic.getSpellTypeIndex(spell.type)..'"|'
    rowTxt = rowTxt..Magic.getSpellTypeLink(spell.type)
    rowTxt = rowTxt..'||'..Magic._getSpellStat(spell, 'description')
    rowTxt = rowTxt..'||style="text-align:center"|'
    rowTxt = rowTxt..Magic._getSpellRunes(spell)
    result = result..rowTxt
  end
   --Add the table end and add the table to the result string
  result = result..'\r\n|}'
  return result
end

function p.getSpellUseTable(frame)
  local itemName = frame.args ~= nil and frame.args[1] or frame
  local item = Items.getItem(itemName)
  if item == nil then
    return "ERROR: No item named "..itemName.." exists in the data module"
  end

  return p._getSpellUseTable(item)
end

return p