17,030
edits
(Use printError function) |
(_getSpellRequirements: Refactor) |
||
Line 5: | Line 5: | ||
local GameData = require('Module:GameData') | local GameData = require('Module:GameData') | ||
local SkillData = GameData.skillData | local SkillData = GameData.skillData | ||
local Common = require('Module:Common') | |||
local Attacks = require('Module:Attacks') | local Attacks = require('Module:Attacks') | ||
local Icons = require('Module:Icons') | local Icons = require('Module:Icons') | ||
Line 10: | Line 11: | ||
p.spellBooks = { | p.spellBooks = { | ||
{ id = 'standard', dataID = 'standardSpells', name = 'Standard Magic', imgType = 'spell' }, | |||
{ id = 'ancient', dataID = 'ancientSpells', name = 'Ancient Magick', imgType = 'spell' }, | |||
{ id = 'archaic', dataID = 'archaicSpells', name = 'Archaic Magick', imgType = 'spell' }, | |||
{ id = 'curse', dataID = 'curseSpells', name = 'Curse', imgType = 'curse' }, | |||
{ id = 'aurora', dataID = 'auroraSpells', name = 'Aurora', imgType = 'aurora' }, | |||
{ id = 'altMagic', dataID = 'altSpells', name = 'Alt. Magic', imgType = 'spell', dataBySkill = true } | |||
} | } | ||
p.spellBookIndex = {} | p.spellBookIndex = {} | ||
for i, spellBook in ipairs(p.spellBooks) do | for i, spellBook in ipairs(p.spellBooks) do | ||
p.spellBookIndex[spellBook.id] = i | |||
end | end | ||
function p.getSpellBookID(sectionName) | function p.getSpellBookID(sectionName) | ||
if sectionName == 'Spell' or sectionName == 'Standard' then | |||
return 'standard' | |||
elseif sectionName == 'Ancient' then | |||
return 'ancient' | |||
elseif sectionName == 'Archaic' then | |||
return 'archaic' | |||
elseif sectionName == 'Curse' then | |||
return 'curse' | |||
elseif sectionName == 'Aurora' then | |||
return 'aurora' | |||
elseif Shared.contains({'Alt Magic', 'Alt. Magic', 'Alternative Magic'}, sectionName) then | |||
return 'altMagic' | |||
else | |||
return sectionName | |||
end | |||
end | end | ||
-- Retrieves all spells within the given spellbook | -- Retrieves all spells within the given spellbook | ||
function p.getSpellsBySpellBook(spellBookID) | function p.getSpellsBySpellBook(spellBookID) | ||
if type(spellBookID) == 'string' then | |||
local spellBook = GameData.getEntityByID(p.spellBooks, spellBookID) | |||
if spellBook ~= nil then | |||
if spellBook.dataBySkill then | |||
-- Data is part of the Magic skill object | |||
local magicData = GameData.getSkillData('melvorD:Magic') | |||
if magicData ~= nil then | |||
return magicData[spellBook.dataID] | |||
end | |||
else | |||
-- Data is at the root of GameData | |||
return GameData.rawData[spellBook.dataID] | |||
end | |||
end | |||
end | |||
end | end | ||
Line 64: | Line 65: | ||
name = Shared.fixPagename(name) | name = Shared.fixPagename(name) | ||
for i, spellBook in ipairs(p.spellBooks) do | |||
if spellBookID == nil or spellBookID == spellBook.id then | |||
local spells = p.getSpellsBySpellBook(spellBook.id) | |||
local spell = GameData.getEntityByName(spells, name) | |||
if spell ~= nil then | |||
return spell | |||
end | |||
end | |||
end | |||
end | end | ||
Line 78: | Line 79: | ||
local spellBookID = p.getSpellBookID(spellType) | local spellBookID = p.getSpellBookID(spellType) | ||
if spellType == nil or spellBookID ~= nil then | |||
for i, spellBook in ipairs(p.spellBooks) do | |||
if spellType == nil or spellBookID == spellBook.id then | |||
if spellBook.dataBySkill then | |||
return GameData.getEntityByID(p.getSpellsBySpellBook(spellBook.id), id) | |||
else | |||
return GameData.getEntityByID(spellBook.dataID, id) | |||
end | |||
end | |||
end | |||
end | |||
end | end | ||
Line 103: | Line 104: | ||
function p.getTypeString(spellType) | function p.getTypeString(spellType) | ||
local spellBookID = p.getSpellBookID(spellType) | |||
if spellBookID ~= nil then | |||
local spellBook = GameData.getEntityByID(p.spellBooks, spellBookID) | |||
if spellBook ~= nil then | |||
return spellBook.name | |||
end | |||
end | |||
end | end | ||
function p._getSpellIconType(spell) | function p._getSpellIconType(spell) | ||
local spellBook = GameData.getEntityByID(p.spellBooks, spell.spellBook) | |||
if spellBook == nil then | |||
-- Pick a suitable default | |||
return 'spell' | |||
else | |||
return spellBook.imgType | |||
end | |||
end | end | ||
Line 134: | Line 135: | ||
function p._getSpellIcon(spell, size) | function p._getSpellIcon(spell, size) | ||
if size == nil then size = 50 end | if size == nil then size = 50 end | ||
local imgType = p._getSpellIconType(spell) | |||
return Icons.Icon({spell.name, type=imgType, notext=true, size=size}) | |||
end | end | ||
function p._getSpellRequirements(spell) | function p._getSpellRequirements(spell) | ||
-- All spells have a Magic level requirement | |||
local extraReqs = { | |||
{ ['type'] = 'SkillLevel', ['skillID'] = 'melvorD:Magic', ['level'] = spell.level } | |||
} | |||
if spell.itemRequiredID ~= nil then | |||
table.insert(extraReqs, { ['type'] = 'SlayerItem', ['itemID'] = spell.itemRequiredID }) | |||
end | |||
local resultPart = {} | |||
for i, reqs in ipairs({ extraReqs, spell.requirements }) do | |||
local reqStr = Common.getRequirementString(extraReqs) | |||
if reqStr ~= nil then | |||
table.insert(resultPart, reqStr) | |||
end | |||
end | |||
if Shared.tableIsEmpty(resultPart) then | |||
return 'None' | |||
else | |||
return table.concat(resultPart, '<br/>') | |||
end | |||
end | end | ||
local function formatRuneList(runes) | local function formatRuneList(runes) | ||
local runeList = {} | |||
for i, req in ipairs(runes) do | |||
local rune = Items.getItemByID(req.id) | |||
if rune ~= nil then | |||
table.insert(runeList, Icons.Icon({rune.name, type='item', notext=true, qty=req.quantity})) | |||
end | |||
end | |||
return table.concat(runeList, ', ') | |||
end | end | ||
Line 229: | Line 223: | ||
-- Generates description template data. See: altMagic.js, description() | -- Generates description template data. See: altMagic.js, description() | ||
function p._getSpellTemplateData(spell) | function p._getSpellTemplateData(spell) | ||
local templateData = nil | |||
if spell.spellBook == 'altMagic' then | |||
if spell.produces ~= nil then | |||
-- Item produced varies depending on items consumed | |||
if spell.produces == 'Bar' then | |||
templateData = { | |||
["barAmount"] = spell.productionRatio, | |||
["oreAmount"] = spell.specialCost.quantity | |||
} | |||
elseif spell.produces == 'GP' then | |||
templateData = { | |||
["percent"] = spell.productionRatio * 100 | |||
} | |||
else | |||
local itemProduced = Items.getItemByID(spell.produces) | |||
if itemProduced ~= nil and itemProduced.prayerPoints ~= nil and type(spell.fixedItemCosts) == 'table' then | |||
-- Item produced is a bone | |||
local costItem = Items.getItemByID(spell.fixedItemCosts[1].id) | |||
if costItem ~= nil then | |||
templateData = { | |||
["itemName"] = costItem.name, | |||
["qty1"] = spell.fixedItemCosts[1].quantity, | |||
["qty2"] = itemProduced.prayerPoints | |||
} | |||
end | |||
end | |||
end | |||
end | |||
if templateData == nil then | |||
templateData = { | |||
["amount"] = spell.productionRatio, | |||
["percent"] = spell.productionRatio * 100, | |||
["specialCostQty"] = spell.specialCost.quantity | |||
} | |||
if type(spell.fixedItemCosts) == 'table' then | if type(spell.fixedItemCosts) == 'table' then | ||
for i, fixedCost in ipairs(spell.fixedItemCosts) do | for i, fixedCost in ipairs(spell.fixedItemCosts) do | ||
Line 272: | Line 266: | ||
end | end | ||
end | end | ||
end | |||
end | |||
return (templateData or {}) | |||
end | end | ||
Line 281: | Line 275: | ||
local connector = inline and '<br/>' or ' and ' | local connector = inline and '<br/>' or ' and ' | ||
if spell.description ~= nil then | if spell.description ~= nil then | ||
return Shared.applyTemplateData(spell.description, p._getSpellTemplateData(spell)) | |||
elseif spell.modifiers ~= nil or spell.targetModifiers ~= nil then | |||
local resultPart = {} | |||
if spell.modifiers ~= nil then | |||
table.insert(resultPart, Constants.getModifiersText(spell.modifiers, false)) | |||
end | |||
if spell.targetModifiers ~= nil then | |||
local targetModText = Constants.getModifiersText(spell.targetModifiers, false, inline) | |||
if inline then | |||
table.insert(resultPart, targetModText) | |||
else | |||
table.insert(resultPart, 'Enemies are inflicted with:<br/>' .. targetModText) | |||
end | |||
end | |||
return table.concat(resultPart, connector) | |||
elseif spell.specialAttackID ~= nil or spell.specialAttack ~= nil then | |||
local spAtt = Attacks.getAttackByID(spell.specialAttackID or spell.specialAttack) | |||
if spAtt ~= nil then | |||
return spAtt.description | |||
end | |||
elseif spell.spellBook == 'standard' then | |||
return 'Combat spell with a max hit of ' .. Shared.formatnum(spell.maxHit * 10) | |||
else | |||
return '' | |||
end | |||
end | end | ||
Line 376: | Line 370: | ||
function p._getAltSpellCostText(spell) | function p._getAltSpellCostText(spell) | ||
if spell.specialCost ~= nil then | |||
local costType = spell.specialCost.type | |||
if costType == nil or costType == 'None' then | |||
if type(spell.fixedItemCosts) == 'table' then | |||
local costText = {} | |||
for i, itemCost in ipairs(spell.fixedItemCosts) do | |||
local item = Items.getItemByID(itemCost.id) | |||
if item ~= nil then | |||
table.insert(costText, Icons.Icon({item.name, type='item', qty=itemCost.quantity})) | |||
end | |||
end | |||
if not Shared.tableIsEmpty(costText) then | |||
return table.concat(costText, ', ') | |||
end | |||
else | |||
return nil | |||
end | |||
else | |||
local qty = Shared.formatnum(spell.specialCost.quantity) | |||
local typeString = { | |||
['AnyItem'] = qty .. ' of any item', | |||
['BarIngredientsWithCoal'] = qty .. ' x required ores for the chosen bar', | |||
['BarIngredientsWithoutCoal'] = qty .. ' x required ores (except ' .. Icons.Icon({'Coal Ore', type='item'}) .. ') for the chosen bar', | |||
['JunkItem'] = qty .. ' of any [[Fishing#Junk|Junk]] item', | |||
['SuperiorGem'] = qty .. ' of any superior gem', | |||
['AnyNormalFood'] = qty .. ' x non-perfect food' | |||
} | |||
return typeString[costType] | |||
end | |||
end | |||
end | end | ||
function p.getSpellsProducingItem(itemID) | function p.getSpellsProducingItem(itemID) | ||
-- Only need to check Alt. Magic spells | |||
local spellList = {} | |||
-- Classify whether the item fits into various categories | |||
local isBar, isShard, isGem, isSuperiorGem, isPerfectFood = false, false, false, false, false | |||
local item = Items.getItemByID(itemID) | |||
if item ~= nil then | |||
isBar = item.type == 'Bar' | |||
isShard = GameData.getEntityByProperty(SkillData.Magic.randomShards, 'itemID', item.id) ~= nil | |||
isGem = GameData.getEntityByProperty('randomGems', 'itemID', itemID) ~= nil | |||
--Runestone can't be created by Alt Magic spells that make random superior gems. | |||
isSuperiorGem = item.type == 'Superior Gem' and item.id ~= SkillData.Mining.runestoneItemID | |||
if item.healsFor ~= nil then | |||
-- Item is food, but is it a product of perfect cooking? | |||
local cookData = GameData.getSkillData('melvorD:Cooking') | |||
if cookData ~= nil and cookData.recipes ~= nil then | |||
isPerfectFood = GameData.getEntityByProperty(cookData.recipes, 'perfectCookID', itemID) ~= nil | |||
end | |||
end | |||
end | |||
for i, spell in ipairs(p.getSpellsBySpellBook('altMagic')) do | |||
local includeSpell = false | |||
if spell.produces ~= nil then | |||
if spell.produces == itemID then | |||
includeSpell = true | |||
else | |||
includeSpell = ((isBar and spell.produces == 'Bar') or | |||
(isShard and spell.produces == 'RandomShards') or | |||
(isGem and spell.produces == 'RandomGem') or | |||
(isSuperiorGem and spell.produces == 'RandomSuperiorGem') or | |||
(isPerfectFood and spell.produces == 'PerfectFood')) | |||
end | |||
if includeSpell then | |||
table.insert(spellList, spell) | |||
end | |||
end | |||
end | |||
table.sort(spellList, function(a, b) return a.level < b.level end) | |||
return spellList | return spellList | ||
end | end | ||
Line 458: | Line 452: | ||
includeConsumes = false | includeConsumes = false | ||
end | end | ||
local runeKeys = { 'runesRequired', 'runesRequiredAlt' } | |||
local spellList = {} | |||
-- Initialize some vars & only populate if we're including resource consumptions | |||
local isJunkItem, isSuperiorGem, isNormalFood, isCoal, isBarIngredient = false, false, false, false, false | |||
if includeConsumes then | |||
local thisItem = Items.getItemByID(itemID) | |||
local junkItemIDs = GameData.getSkillData('melvorD:Fishing').junkItemIDs | |||
isJunkItem = Shared.contains(junkItemIDs, itemID) | |||
isSuperiorGem = thisItem.type == 'Superior Gem' | |||
if thisItem.healsFor ~= nil then | |||
-- Item is food, but is it from cooking & is it normal or perfect? | |||
local cookData = GameData.getSkillData('melvorD:Cooking') | |||
if cookData ~= nil and cookData.recipes ~= nil then | |||
isNormalFood = GameData.getEntityByProperty(cookData.recipes, 'productID', itemID) ~= nil | |||
end | |||
end | |||
isCoal = itemID == 'melvorD:Coal_Ore' | |||
if not isCoal then | |||
-- Don't need to check if the item is another bar ingredient if we already know it is coal | |||
local smithingRecipes = GameData.getSkillData('melvorD:Smithing').recipes | |||
for i, recipe in ipairs(smithingRecipes) do | |||
if recipe.categoryID == 'melvorD:Bars' then | |||
for k, itemCost in ipairs(recipe.itemCosts) do | |||
if itemCost.id == itemID then | |||
isBarIngredient = true | |||
break | |||
end | |||
end | |||
if isBarIngredient then | |||
break | |||
end | |||
end | |||
end | |||
end | |||
end | |||
-- Find applicable spells | |||
for i, spellBook in ipairs(p.spellBooks) do | |||
local spells = p.getSpellsBySpellBook(spellBook.id) | |||
for j, spell in ipairs(spells) do | |||
local foundSpell = false | |||
-- Check runes first | |||
for k, runeKey in ipairs(runeKeys) do | |||
if spell[runeKey] ~= nil then | |||
for m, req in ipairs(spell[runeKey]) do | |||
if req.id == itemID then | |||
foundSpell = true | |||
break | |||
end | |||
end | |||
end | |||
if foundSpell then | |||
break | |||
end | |||
end | |||
if includeConsumes and not foundSpell then | |||
-- Check items consumed by the spell | |||
-- Fixed costs first, as that is a well-defined list of item IDs | |||
if spell.fixedItemCosts ~= nil then | |||
for k, itemCost in ipairs(spell.fixedItemCosts) do | |||
if itemCost.id == itemID then | |||
foundSpell = true | |||
break | |||
end | |||
end | |||
end | |||
if not foundSpell and spell.specialCost ~= nil then | |||
local costType = spell.specialCost.type | |||
foundSpell = (isJunkItem and costType == 'JunkItem') or | |||
(isSuperiorGem and costType == 'AnySuperiorGem') or | |||
(isNormalFood and costType == 'AnyNormalFood') or | |||
((isCoal or isBarIngredient) and costType == 'BarIngredientsWithCoal') or | |||
(isBarIngredient and costType == 'BarIngredientsWithoutCoal') | |||
end | |||
end | |||
if foundSpell then | |||
table.insert(spellList, spell) | |||
end | |||
end | |||
end | |||
table.sort(spellList, function(a, b) | |||
if a.spellBook ~= b.spellBook then | |||
return p.spellBookIndex[a.spellBook] < p.spellBookIndex[b.spellBook] | |||
else | |||
return a.level < b.level | |||
end | |||
end) | |||
return spellList | return spellList | ||
end | end | ||
Line 557: | Line 551: | ||
function p.getSpellTypeLink(spellBookID) | function p.getSpellTypeLink(spellBookID) | ||
if spellBookID == 'standard' then | |||
return Icons.Icon({'Standard Magic', 'Standard', img='Standard', type='spellType'}) | return Icons.Icon({'Standard Magic', 'Standard', img='Standard', type='spellType'}) | ||
elseif spellBookID == 'ancient' then | |||
return Icons.Icon({'Ancient Magicks', 'Ancient', img='Ancient', type='spellType'}) | |||
elseif spellBookID == 'archaic' then | |||
return Icons.Icon({'Archaic Magicks', 'Archaic', img='Archaic', type='spellType'}) | |||
elseif spellBookID == 'curse' then | |||
return Icons.Icon({'Curses', 'Curse', img='Curse', type='spellType'}) | return Icons.Icon({'Curses', 'Curse', img='Curse', type='spellType'}) | ||
elseif spellBookID == 'aurora' then | |||
return Icons.Icon({'Auroras', 'Aurora', img='Aurora', type='spellType'}) | return Icons.Icon({'Auroras', 'Aurora', img='Aurora', type='spellType'}) | ||
elseif spellBookID == 'altMagic' then | |||
return Icons.Icon({'Alt. Magic', type='skill'}) | return Icons.Icon({'Alt. Magic', type='skill'}) | ||
end | end | ||
Line 595: | Line 589: | ||
function p._getSpellRow(spell, includeTypeColumn, includeItems, includeDamage, includeExperience) | function p._getSpellRow(spell, includeTypeColumn, includeItems, includeDamage, includeExperience) | ||
local spellBookIdx = p.spellBookIndex[spell.spellBook] | |||
local spellBook = p.spellBooks[spellBookIdx] | |||
local rowPart = {'\n|-\n|data-sort-value="' .. spell.name .. '"| '} | |||
table.insert(rowPart, Icons.Icon({spell.name, type=spellBook.imgType, notext=true, size=50})) | |||
table.insert(rowPart, '|| ' .. Icons.getExpansionIcon(spell.id) .. Icons.Icon({spell.name, type=spellBook.imgType, noicon=true})) | table.insert(rowPart, '|| ' .. Icons.getExpansionIcon(spell.id) .. Icons.Icon({spell.name, type=spellBook.imgType, noicon=true})) | ||
if includeTypeColumn then | if includeTypeColumn then | ||
Line 620: | Line 614: | ||
local hits = spAtt.attackCount ~= nil and spAtt.attackCount or 1 | local hits = spAtt.attackCount ~= nil and spAtt.attackCount or 1 | ||
if interval ~= nil and hits > 1 then | if interval ~= nil and hits > 1 then | ||
table.insert(rowPart, '<br/>(' .. Shared.round(interval / 1000, 2, 2) .. 's delay between attacks.') | |||
if hits > 2 then | if hits > 2 then | ||
table.insert(rowPart, ' ' .. Shared.round(interval * (hits - 1) / 1000, 2, 2) .. 's total duration.') | table.insert(rowPart, ' ' .. Shared.round(interval * (hits - 1) / 1000, 2, 2) .. 's total duration.') | ||
Line 663: | Line 657: | ||
table.insert(resultPart, '\n|}') | table.insert(resultPart, '\n|}') | ||
return table.concat(resultPart) | |||
end | end | ||
end | end | ||
Line 692: | Line 686: | ||
-- Included below for backwards compatibility | -- Included below for backwards compatibility | ||
function p.getStandardSpellsTable(frame) | function p.getStandardSpellsTable(frame) | ||
return p._getSpellTable(p.getSpellsBySpellBook('standard'), false) | |||
end | end | ||
function p.getAncientTable(frame) | function p.getAncientTable(frame) | ||
return p._getSpellTable(p.getSpellsBySpellBook('ancient'), false) | |||
end | end | ||
function p.getCurseTable(frame) | function p.getCurseTable(frame) | ||
return p._getSpellTable(p.getSpellsBySpellBook('curse'), false) | |||
end | end | ||
function p.getAuroraTable(frame) | function p.getAuroraTable(frame) | ||
return p._getSpellTable(p.getSpellsBySpellBook('aurora'), false) | |||
end | end | ||
function p.getAltSpellsTable(frame) | function p.getAltSpellsTable(frame) | ||
return p._getSpellTable(p.getSpellsBySpellBook('altMagic'), false) | |||
end | end | ||
return p | return p |