Module:Items: Difference between revisions

1,714 bytes added ,  23 September 2020
Improvements to the getItemSources code to account for more things. Probably not everything quite yet, but getting there
(another attempt to fix formatting)
(Improvements to the getItemSources code to account for more things. Probably not everything quite yet, but getting there)
Line 201: Line 201:


function p._getItemSources(item)
function p._getItemSources(item)
   local result = ''
   local result = nil
  local lineArray = {}
 
   --Alright, time to go through all the ways you can get an item...
   --Alright, time to go through all the ways you can get an item...
   --First up: Can we kill somebody and take theirs?
   --First up: Can we kill somebody and take theirs?
   local killFound = false
   local killStr = ''
   for i, monster in pairs(MonsterData.Monsters) do
   for i, monster in pairs(MonsterData.Monsters) do
     if monster.lootTable ~= nil then
     if monster.lootTable ~= nil then
       for j, loot in pairs(monster.lootTable) do
       for j, loot in pairs(monster.lootTable) do
         if loot[1] == item.id then
         if loot[1] == item.id then
           if killFound then
           if string.len(killStr) > 0 then
             result = result..','..Icons.Icon({monster.name, type="monster", notext="true"})
             killStr = killStr..','..Icons.Icon({monster.name, type="monster", notext="true"})
           else
           else
             result = result..'Killing: '..Icons.Icon({monster.name, type="monster", notext="true"})
             killStr = killStr..'Killing: '..Icons.Icon({monster.name, type="monster", notext="true"})
            killFound = true
           end
           end
         end
         end
Line 219: Line 220:
     end
     end
   end
   end
  if string.len(killStr) > 0 then table.insert(lineArray, killStr) end


   --Next: Can we find it in a box?
   --Next: Can we find it in a box?
   local boxFound = false
  --While we're here, check for upgrades, cooking, and growing
   for i, chest in pairs(ItemData) do
  local lootStr = ''
     if chest.dropTable ~= nil then
  local upgradeStr = ''
       for j, loot in pairs(chest.dropTable) do
  local cookStr = ''
  local burnStr = ''
   local growStr = ''
   for i, item2 in pairs(ItemData) do
     if item2.dropTable ~= nil then
       for j, loot in pairs(item2.dropTable) do
         if loot[1] == item.id then
         if loot[1] == item.id then
           if boxFound then
           if string.len(lootStr) > 0 then
             result = result..','..Icons.Icon({chest.name, type="item", notext="true"})
             lootStr = lootStr..','..Icons.Icon({item2.name, type="item", notext="true"})
           else
           else
             if string.len(result) > 0 then result = result..'<br/>' end
             lootStr = lootStr..'Opening: '..Icons.Icon({item2.name, type="item", notext="true"})
            result = result..'Opening: '..Icons.Icon({chest.name, type="item", notext="true"})
            boxFound = true
           end
           end
         end
         end
       end
       end
    end
    if item2.trimmedItemID == item.id then
        if string.len(upgradeStr) > 0 then
          upgradeStr = upgradeStr..','..Icons.Icon({item2.name, type="item", notext="true"})
        else
          upgradeStr = upgradeStr..'Upgrading: '..Icons.Icon({item2.name, type="item", notext="true"})
        end
    end
    if item2.cookedItemID == item.id then
        if string.len(cookStr) > 0 then
          cookStr = cookStr..','..Icons.Icon({item2.name, type="item", notext="true"})
        else
          cookStr = cookStr..'Cooking: '..Icons.Icon({item2.name, type="item", notext="true"})
        end
    end
    if item2.burntItemID == item.id then
        if string.len(burnStr) > 0 then
          burnStr = burnStr..','..Icons.Icon({item2.name, type="item", notext="true"})
        else
          burnStr = burnStr..'Burning: '..Icons.Icon({item2.name, type="item", notext="true"})
        end
    end
    if item2.grownItemID == item.id then
        if string.len(growStr) > 0 then
          growStr = growStr..','..Icons.Icon({item2.name, type="item", notext="true"})
        else
          growStr = growStr..'Growing: '..Icons.Icon({item2.name, type="item", notext="true"})
        end
     end
     end
   end
   end
  if string.len(lootStr) > 0 then table.insert(lineArray, lootStr) end
  if string.len(upgradeStr) > 0 then table.insert(lineArray, upgradeStr) end
  if string.len(cookStr) > 0 then table.insert(lineArray, cookStr) end
  if string.len(burnStr) > 0 then table.insert(lineArray, burnStr) end
  if string.len(growStr) > 0 then table.insert(lineArray, growStr) end


   --Next: Can we take it from somebody else -without- killing them?
   --Next: Can we take it from somebody else -without- killing them?
  local thiefStr = ''
   for i, npc in pairs(SkillData.Thieving) do
   for i, npc in pairs(SkillData.Thieving) do
     if npc.lootTable ~= nil then
     if npc.lootTable ~= nil then
       for j, loot in pairs(npc.lootTable) do
       for j, loot in pairs(npc.lootTable) do
         if loot[1] == item.id then
         if loot[1] == item.id then
           if string.len(result) > 0 then result = result..'<br/>' end
           if string.len(thiefStr) > 0 then
           result = result..'Pickpocketing: '..Icons.Icon({npc.name, type="thieving", notext="true"})
            thiefStr = thiefStr..','..Icons.Icon({npc.name, type="item", notext="true"})
           boxFound = true
           else
            thiefStr = thiefStr..'Pickpocketing: '..Icons.Icon({npc.name, type="item", notext="true"})
           end
         end
         end
       end
       end
     end
     end
   end
   end
 
   if string.len(thiefStr) > 0 then table.insert(lineArray, thiefStr) end
   --Next: Can we get to it via upgrading?
  local upgradeFound = false
  if item.itemsRequired ~= nil then
    if string.len(result) > 0 then result = result..'<br/>' end
    result = result..'Upgrading: '
    for i, req in pairs(item.itemsRequired) do
      local reqItem = p.getItemByID(req[1])
      if reqItem.canUpgrade then
        if upgradeFound then result = result..', ' else upgradeFound = true end
        result = result..Icons.Icon({reqItem.name, type='item', notext='true'})
      end
    end
  end


   --If all else fails, I guess we should check if we can make it ourselves
   --If all else fails, I guess we should check if we can make it ourselves
   --SmithCheck:
   --SmithCheck:
   if item.smithingLevel ~= nil then
   if item.smithingLevel ~= nil then
     if string.len(result) > 0 then result = result..'<br/>' end
     table.insert(lineArray, Icons._SkillReq("Smithing", item.smithingLevel))
    result = result..Icons._SkillReq("Smithing", item.smithingLevel)
   end
   end


   --CraftCheck:
   --CraftCheck:
   if item.craftingLevel ~= nil then
   if item.craftingLevel ~= nil then
     if string.len(result) > 0 then result = result..'<br/>' end
     table.insert(lineArray, Icons._SkillReq("Crafting", item.craftingLevel))
    result = result..Icons._SkillReq("Crafting", item.craftingLevel)
   end
   end


   --FletchCheck:
   --FletchCheck:
   if item.fletchingLevel ~= nil then
   if item.fletchingLevel ~= nil then
     if string.len(result) > 0 then result = result..'<br/>' end
     table.insert(lineArray, Icons._SkillReq("Fletching", item.fletchingLevel))
    result = result..Icons._SkillReq("Fletching", item.fletchingLevel)
   end
   end


   --RunecraftCheck:
   --RunecraftCheck:
   if item.runecraftingLevel ~= nil then
   if item.runecraftingLevel ~= nil then
     if string.len(result) > 0 then result = result..'<br/>' end
     table.insert(lineArray, Icons._SkillReq("Runecrafting", item.runecraftingLevel))
     result = result..Icons._SkillReq("Runecrafting", item.runecraftingLevel)
  end
 
  --MineCheck:
  if item.miningLevel ~= nil then
     table.insert(lineArray, Icons._SkillReq("Mining", item.miningLevel))
   end
   end


   --FishCheck:
   --FishCheck:
   if (item.category == "Fishing" and (item.type == "Junk" or item.type == "Special")) or item.category == "Gem" then
   if (item.category == "Fishing" and (item.type == "Junk" or item.type == "Special")) then
     if string.len(result) > 0 then result = result..'<br/>' end
     table.insert(lineArray, Icons._SkillReq("Fishing", 1))
    result = result..Icons._SkillReq("Fishing", 1)
   elseif item.fishingLevel ~= nil then
   elseif item.category == 'Fishing' then
     table.insert(lineArray, Icons._SkillReq("Fishing", item.fishingLevel))
     if string.len(result) > 0 then result = result..'<br/>' end
  end
     result = result..Icons._SkillReq("Fishing", item.fishingLevel)
 
  --HerbCheck:
  if item.herbloreMasteryID ~= nil then
    local potionData = SkillData.Herblore.ItemData[item.herbloreMasteryID + 1].herbloreLevel
     table.insert(lineArray, Icons._SkillReq("Herblore", potionData))
   end
   end


   --Finally there are some weird exceptions:
   --Finally there are some weird exceptions:
   --Shop items
   --Coal can be acquired via firemaking
   if item.slayerCost ~= nil or item.buysFor ~= nil then
   if item.name == "Coal Ore" then
     if string.len(result) > 0 then result = result..'<br/>' end
    table.insert(lineArray, Icons._SkillReq("Firemaking", 1))
     result = result..'[[Shop]]'
  end
 
  --Gems can be acquired from both mining & fishing
  if item.type == 'Gem' then
     table.insert(lineArray, Icons._SkillReq("Fishing", 1))
    table.insert(lineArray, Icons._SkillReq("Mining", 1))
  end
 
  --Tokens are from the appropriate skill
  if item.isToken then
     for skill, id in pairs(Constants.skill) do
      if id == item.skill then
        table.insert(lineArray, Icons._SkillReq(skill, 1))
      end
    end
   end
   end


   --Special Shop Items (ie gloves, which don't technically exist in the shop)
   --Shop items (including special items like gloves that aren't otherwise listed)
   if Shared.contains(OtherShopItems, item.name) then
   if item.slayerCost ~= nil or item.buysFor ~= nil or Shared.contains(OtherShopItems, item.name) then
     if string.len(result) > 0 then result = result..'<br/>' end
     table.insert(lineArray, '[[Shop]]')
    result = result..'[[Shop]]'
   end
   end


   --Easter Eggs (manual list 'cause don't have a better way to do that)
   --Easter Eggs (manual list 'cause don't have a better way to do that)
   if Shared.contains(EasterEggs, item.name) then
   if Shared.contains(EasterEggs, item.name) then
     if string.len(result) > 0 then result = result..'<br/>' end
     table.insert(lineArray, '[[Easter Eggs]]')
    result = result..'[[Easter Eggs]]'
   end
   end


   return result
   return table.concat(lineArray, "<br/>")
end
end