Module:Attacks/Tables: Difference between revisions

From Melvor Idle
(Fixed a thing where some special attacks have a scalable number of attacks so total duration shouldn't be shown)
m (Fixed Spell category and interval between attacks will only show if attack count > 1)
 
(15 intermediate revisions by 4 users not shown)
Line 3: Line 3:
local Constants = require('Module:Constants')
local Constants = require('Module:Constants')
local Shared = require('Module:Shared')
local Shared = require('Module:Shared')
local GameData = require('Module:GameData')
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Items = require('Module:Items')
local Items = require('Module:Items')
local Monsters = require('Module:Monsters')
local Attacks = require('Module:Attacks')
local Num = require('Module:Number')
local Magic = require('Module:Magic')
local Magic = require('Module:Magic')
local Attacks = require('Module:Attacks')


function p._getSpecialAttackTable(effectDefn, categories, sourceHeaderLabel, includeSource)
function p._getSpecialAttackTable(effectDefn, categories, sourceHeaderLabel, includeSource)
    local spAttTable = {}
-- TODO Spells and Familiars need fixing for V1.3
    local attacks = Attacks.getAttacks(function(attack)
-- Is going to be incredibly broken following combat effects & modifiers overhaul
                                          if effectDefn == nil then
 
                                              return true
local spAttTable = {}
                                          else
local attacks = Attacks.getAttacks(function(attack)
                                              return Attacks.attackHasEffect(attack, effectDefn)
if effectDefn == nil then
                                          end
return true
                                      end)
else
    local includeCat = {}
return Attacks.attackHasEffect(attack, effectDefn)
    for i, category in ipairs(categories) do
end
        includeCat[category] = true
end)
local includeCat = {}
for i, category in ipairs(categories) do
includeCat[category] = true
end
 
    -- Build index so we can tell which to include later
    local includedAttacks = {}
    for i, attack in ipairs(attacks) do
        includedAttacks[attack.id] = true
     end
     end


    -- Compile a list of monsters, items, spells, etc. for each included attack
-- Compile a list of monsters, items, spells, etc. for each included attack
     for i, spAtt in ipairs(attacks) do
     -- Monsters
        -- Monsters
    if includeCat['Monster'] then
        if includeCat['Monster'] then
        for i, monster in ipairs(GameData.rawData.monsters) do
            for j, monsterID in ipairs(spAtt.monsters) do
            if monster.specialAttacks ~= nil and not Shared.tableIsEmpty(monster.specialAttacks) then
                local monster = Monsters.getMonsterByID(monsterID)
                 local overrideChance = (monster.overrideSpecialChances ~= nil and not Shared.tableIsEmpty(monster.overrideSpecialChances))
                 local overrideChance = (monster.overrideSpecialChances ~= nil and Shared.tableCount(monster.overrideSpecialChances) > 0)
                 for j, spAttID in ipairs(monster.specialAttacks) do
                 local attChance = spAtt.defaultChance
                    if includedAttacks[spAttID] then
                if overrideChance then
                         local spAtt = GameData.getEntityByID('attacks', spAttID)
                    local attIdx = nil
                         if spAtt ~= nil then
                    for k, monsterAttack in ipairs(monster.specialAttacks) do
                             local attChance = (overrideChance and monster.overrideSpecialChances[j]) or spAtt.defaultChance
                         local attID = (type(monsterAttack) == 'table' and monsterAttack.id) or monsterAttack
                             table.insert(spAttTable, { id = spAttID, source = 'Monster', sourceSort = monster.name, sourceText = Icons.Icon({ monster.name, type = 'monster' }), chance = attChance, descType = 'monster' })
                         if spAtt.id == attID then
                             attIdx = k
                             break
                         end
                         end
                    end
                    if attIdx ~= nil then
                        attChance = monster.overrideSpecialChances[attIdx]
                     end
                     end
                 end
                 end
                table.insert(spAttTable, { idx = i, source = 'Monster', sourceSort = monster.name, sourceText = Icons.Icon({ monster.name, type = 'monster' }), chance = attChance, descType = 'monster' })
             end
             end
         end
         end
    end


        -- Items/Weapons
    -- Items/Weapons
        if includeCat['Item'] then
    if includeCat['Item'] then
            for j, itemID in ipairs(spAtt.items) do
        for i, item in ipairs(GameData.rawData.items) do
                local item = Items.getItemByID(itemID)
            if item.specialAttacks ~= nil and not item.golbinRaidExclusive and not Shared.tableIsEmpty(item.specialAttacks) then
                 table.insert(spAttTable, { idx = i, source = 'Weapon', sourceSort = item.name, sourceText = Icons.Icon({ item.name, type = 'item' }), chance = spAtt.defaultChance, descType = 'player' })
                 local overrideChance = (item.overrideSpecialChances ~= nil and not Shared.tableIsEmpty(item.overrideSpecialChances))
            end
                for j, spAttID in ipairs(item.specialAttacks) do
        end
                    if includedAttacks[spAttID] then
 
                        local spAtt = GameData.getEntityByID('attacks', spAttID)
        -- Spells
                        if spAtt ~= nil then
        if includeCat['Spell'] then
                            local attChance = (overrideChance and item.overrideSpecialChances[j]) or spAtt.defaultChance
            for j, spellID in ipairs(spAtt.spells) do
                            table.insert(spAttTable, { id = spAttID, source = 'Weapon', sourceSort = item.name, sourceText = Icons.Icon({ item.name, type = 'item' }), chance = attChance, descType = 'player' })
                local spell = Magic.getSpellByID(spellID[1], spellID[2])
                        end
                table.insert(spAttTable, { idx = i, source = 'Spell', sourceSort = spell.name, sourceText = Icons.Icon({ spell.name, type = 'spell' }), chance = spAtt.defaultChance, descType = 'player' })
                    end
                end
             end
             end
         end
         end
     end
     end


     -- Summoning familiars. Any effects inflicted by combat familiars aren't actually special
     -- Spells
    -- attacks, therefore the handling here is a bit different and outside of the above attack loop
     if includeCat['Spell'] then
     if includeCat['Familiar'] then
         local spellCats = { 'standard', 'ancient', 'archaic', 'abyssal' }
         local famIdx = Shared.tableCount(attacks) + 1
         for i, spellCat in ipairs(spellCats) do
        local familiars = Items.getItems(function(item)
             for j, spell in ipairs(Magic.getSpellsBySpellBook(spellCat)) do
                                            if item.type == 'Familiar' and Items._getItemStat(item, 'summoningMaxhit') ~= nil and item.modifiers ~= nil then
            local spAttID = spell.specialAttack or spell.specialAttackID
                                                local famAttack = { prehitEffects = {}, onhitEffects = { { type = 'Modifier', subtype = 'Familiar', modifiers = item.modifiers } } }
                 if spAttID ~= nil and includedAttacks[spAttID] then
                                                if effectDefn == nil then
                    local spAtt = GameData.getEntityByID('attacks', spAttID)
                                                    return Shared.tableCount(Attacks.getAttackEffects(famAttack)) > 0
                     if spAtt ~= nil then
                                                else
                        table.insert(spAttTable, {id = spAtt.id, source = 'Spell', sourceSort = spell.name, sourceText = Icons.Icon({ spell.name, type = 'spell' }), chance = spAtt.defaultChance, descType = 'player' })
                                                    return Attacks.attackHasEffect(famAttack, effectDefn)
                     end
                                                end
                                            end
                                            return false
                                        end)
         for j, familiar in ipairs(familiars) do
            -- For chance, assume the first modifier we come across has the chance, which is pretty lazy
            local famChance, famDesc = 0, ''
             for modName, modVal in pairs(familiar.modifiers) do
                 if type(modVal) == 'table' and type(modVal[1]) == 'number' then
                     famChance = modVal[1]
                elseif type(modVal) == 'number' then
                     famChance = modVal
                else
                    famChance = 0
                 end
                 end
                famDesc = Constants._getModifierText(modName, modVal, false)
                break
             end
             end
            table.insert(spAttTable, { idx = famIdx, source = 'Familiar', sourceSort = familiar.name, sourceText = Icons.Icon({ familiar.name, type = 'item' }), chance = famChance or 0, descType = 'player' })
            -- Slap a dummy entry into the attacks table for this familiar
            attacks[famIdx] = { name = familiar.name .. ' (Familiar)', description = { player = famDesc } }
            famIdx = famIdx + 1
         end
         end
     end
     end
--[[
-- Summoning familiars. Any effects inflicted by combat familiars aren't actually special
-- attacks, therefore the handling here is a bit different
if includeCat['Familiar'] then
local famIdx = Shared.tableCount(attacks) + 1
local familiars = Items.getItems(
            function(item)
                if item.type == 'Familiar' and item.modifiers ~= nil and Items._getItemStat(item, 'summoningMaxhit') ~= nil then
                    local famAttack = { prehitEffects = {}, onhitEffects = { { type = 'Modifier', subtype = 'Familiar', modifiers = item.modifiers } } }
                    if effectDefn == nil then
                        return Shared.tableIsEmpty(Attacks.getAttackEffects(famAttack))
                    else
                        return Attacks.attackHasEffect(famAttack, effectDefn)
                    end
                end
                return false
            end)
for i, familiar in ipairs(familiars) do
-- For chance, assume the first modifier we come across has the chance, which is pretty lazy
local famChance, famDesc = 0, ''
for modName, modVal in pairs(familiar.modifiers) do
if type(modVal) == 'table' and type(modVal[1]) == 'number' then
famChance = modVal[1]
elseif type(modVal) == 'number' then
famChance = modVal
else
famChance = 0
end
famDesc = Constants._getModifierText(modName, modVal, false)
break
end


    -- Nothing to output if there are no row definitions
table.insert(spAttTable, { id = familiar.id, source = 'Familiar', sourceSort = familiar.name, sourceText = Icons.Icon({ familiar.name, type = 'item' }), chance = famChance or 0, descType = 'player' })
    if Shared.tableCount(spAttTable) == 0 then
-- Slap a dummy entry into the attacks table for this familiar
    return ''
attacks[famIdx] = { id = familiar.id, name = familiar.name .. ' (Familiar)', description = famDesc }
    end
famIdx = famIdx + 1
end
end
--]]
-- Nothing to output if there are no row definitions
if Shared.tableIsEmpty(spAttTable) then
return ''
end


    -- Sort entries into desired order and generate stats to determine row spans:
-- Sort entries into desired order and generate stats to determine row spans:
    -- By attack index, description type (monster/player), chance, source, then source name (weapon/item/etc.)
-- By attack index, description type (monster/player), chance, source, then source name (weapon/item/etc.)
    table.sort(spAttTable, function (a, b)
table.sort(spAttTable, function (a, b)
                              local sortKeys = { 'idx', 'descType', 'chance', 'source', 'sourceSort' }
local sortKeys = { 'id', 'descType', 'chance', 'source', 'sourceSort' }
                              for i, key in ipairs(sortKeys) do
for i, key in ipairs(sortKeys) do
                                  if a[key] ~= b[key] then
if a[key] ~= b[key] then
                                      return a[key] < b[key]
return a[key] < b[key]
                                  end
end
                              end
end
                              return false
return false
                          end)
end)
    -- Determine row counts for grouping/rowspans
-- Determine row counts for grouping/rowspans
    local rowCounts = {}
local rowCounts = {}
    for i, rowDefn in ipairs(spAttTable) do
for i, rowDefn in ipairs(spAttTable) do
        local idx, dt, chance = rowDefn.idx, rowDefn.descType, rowDefn.chance
local id, dt, chance = rowDefn.id, rowDefn.descType, rowDefn.chance
        if rowCounts[idx] == nil then
if rowCounts[id] == nil then
            rowCounts[idx] = { rows = 0 }
rowCounts[id] = { rows = 0 }
        end
end
        if rowCounts[idx][dt] == nil then
if rowCounts[id][dt] == nil then
            rowCounts[idx][dt] = { rows = 0 }
rowCounts[id][dt] = { rows = 0 }
        end
end
        if rowCounts[idx][dt][chance] == nil then
if rowCounts[id][dt][chance] == nil then
            rowCounts[idx][dt][chance] = 0
rowCounts[id][dt][chance] = 0
        end
end
        rowCounts[idx]['rows'] = rowCounts[idx]['rows'] + 1
rowCounts[id]['rows'] = rowCounts[id]['rows'] + 1
        rowCounts[idx][dt]['rows'] = rowCounts[idx][dt]['rows'] + 1
rowCounts[id][dt]['rows'] = rowCounts[id][dt]['rows'] + 1
        rowCounts[idx][dt][chance] = rowCounts[idx][dt][chance] + 1
rowCounts[id][dt][chance] = rowCounts[id][dt][chance] + 1
    end
end


    -- Generate output table
-- Generate output table
    local resultPart = {}
local resultPart = {}
    table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
    table.insert(resultPart, '\r\n|- class="headerRow-0"')
table.insert(resultPart, '\r\n|- class="headerRow-0"')
    table.insert(resultPart, '\r\n!Name!!style="min-width:225px"| ' .. sourceHeaderLabel .. (includeSource and '!!Type' or '') .. '!!Chance!!Effect')
table.insert(resultPart, '\r\n!Name!!style="min-width:225px"| ' .. sourceHeaderLabel .. (includeSource and '!!Type' or '') .. '!!Chance!!Effect')


    local firstRow = { idx = true, descType = true, chance = true }
local firstRow = { id = true, descType = true, chance = true }
    local prevRowVal = { idx = 0, descType = '', chance = 0 }
local prevRowVal = { id = 0, descType = '', chance = 0 }
    local resetOnChange = {
local resetOnChange = {
        idx = { 'idx', 'descType', 'chance' },
id = { 'id', 'descType', 'chance' },
        descType = { 'descType', 'chance' },
descType = { 'descType', 'chance' },
        chance = { 'chance' }
chance = { 'chance' }
    }
}
    local rowSuffix = ''
local rowSuffix = ''
    for i, spAttRow in ipairs(spAttTable) do
for i, spAttRow in ipairs(spAttTable) do
        local spIdx = spAttRow.idx
local spIdx = spAttRow.id
        local spAtt = attacks[spIdx]
local spAtt = GameData.getEntityByID(attacks, spIdx)
        -- Determine if it's the first row for any of our groupings
-- Determine if it's the first row for any of our groupings
        local resetKeys = {}
local resetKeys = {}
        for key, val in pairs(prevRowVal) do
for key, val in pairs(prevRowVal) do
            if spAttRow[key] ~= prevRowVal[key] then
if spAttRow[key] ~= prevRowVal[key] then
                for j, keyName in ipairs(resetOnChange[key]) do
for j, keyName in ipairs(resetOnChange[key]) do
                    resetKeys[keyName] = true
resetKeys[keyName] = true
                end
end
            end
end
            prevRowVal[key] = spAttRow[key]
prevRowVal[key] = spAttRow[key]
        end
end
        for key, val in pairs(firstRow) do
for key, val in pairs(firstRow) do
            firstRow[key] = (resetKeys[key] ~= nil)
firstRow[key] = (resetKeys[key] ~= nil)
        end
end


        table.insert(resultPart, '\r\n|-')
table.insert(resultPart, '\r\n|-')
        if firstRow.idx then
if firstRow.id then
            rowSuffix = (rowCounts[spIdx]['rows'] > 1 and '|rowspan="' .. rowCounts[spIdx]['rows'] .. '"') or ''
rowSuffix = (rowCounts[spIdx]['rows'] > 1 and '|rowspan="' .. rowCounts[spIdx]['rows'] .. '"') or ''
            table.insert(resultPart, '\r\n' .. rowSuffix .. '| ' .. spAtt.name)
table.insert(resultPart, '\r\n' .. rowSuffix .. '| ' .. spAtt.name)
        end
end
        table.insert(resultPart, '\r\n|data-sort-value="' .. spAttRow.sourceSort .. '"| ' .. spAttRow.sourceText)
table.insert(resultPart, '\r\n|data-sort-value="' .. spAttRow.sourceSort .. '"| ' .. spAttRow.sourceText)
        if includeSource then
if includeSource then
            table.insert(resultPart, '\r\n| ' .. spAttRow.source)
table.insert(resultPart, '\r\n| ' .. spAttRow.source)
        end
end
        if firstRow.chance then
if firstRow.chance then
            rowSuffix = (rowCounts[spIdx][spAttRow.descType][spAttRow.chance] > 1 and 'rowspan="' .. rowCounts[spIdx][spAttRow.descType][spAttRow.chance] .. '" ') or ''
rowSuffix = (rowCounts[spIdx][spAttRow.descType][spAttRow.chance] > 1 and 'rowspan="' .. rowCounts[spIdx][spAttRow.descType][spAttRow.chance] .. '" ') or ''
            table.insert(resultPart, '\r\n|' .. rowSuffix .. 'data-sort-value="' .. spAttRow.chance .. '" style="text-align:right;"| ' .. Shared.round(spAttRow.chance, 2, 0) .. '%')
table.insert(resultPart, '\r\n|' .. rowSuffix .. 'data-sort-value="' .. spAttRow.chance .. '" style="text-align:right;"| ' .. Num.round(spAttRow.chance, 2, 0) .. '%')
        end
end
        if firstRow.descType then
if firstRow.descType then
            rowSuffix = (rowCounts[spIdx][spAttRow.descType]['rows'] > 1 and '|rowspan="' .. rowCounts[spIdx][spAttRow.descType]['rows'] .. '"') or ''
rowSuffix = (rowCounts[spIdx][spAttRow.descType]['rows'] > 1 and '|rowspan="' .. rowCounts[spIdx][spAttRow.descType]['rows'] .. '"') or ''
            local spAttDesc = spAtt['description'][spAttRow.descType]
local spAttDesc = spAtt.description
            --Adding the time between hits and total duration as a note at the end of the special attack description
--Adding the time between hits and total duration as a note at the end of the special attack description
            local spAttInterval = spAtt.attackInterval ~= nil and spAtt.attackInterval or -1
local spAttInterval = spAtt.attackInterval ~= nil and spAtt.attackInterval or -1
            if(spAttInterval ~= -1 and spAtt.damage ~= nil and Shared.tableCount(spAtt.damage) > 0) then
if(spAttInterval ~= -1 and spAtt.damage ~= nil and Shared.tableCount(spAtt.damage) > 0 and spAtt.attackCount > 1) then
            spAttDesc = spAttDesc..'<br/>('
spAttDesc = spAttDesc..'<br/>('
            local spAttDuration = spAttInterval * (spAtt.attackCount - 1)
local spAttDuration = spAttInterval * (spAtt.attackCount - 1)
            spAttDesc = spAttDesc..Shared.round(spAttInterval / 1000, 2, 2)..'s delay between attacks.'
spAttDesc = spAttDesc..Num.round(spAttInterval / 1000, 2, 2)..'s delay between attacks.'
            if spAtt.attackCount ~= nil and spAtt.attackCount > 1 then
if spAtt.attackCount ~= nil and spAtt.attackCount > 2 then
            spAttDesc = spAttDesc..' '..Shared.round(spAttDuration / 1000, 2, 2)..'s total duration'
spAttDesc = spAttDesc..' '..Num.round(spAttDuration / 1000, 2, 2)..'s total duration'
            end
end
            spAttDesc = spAttDesc..')'
spAttDesc = spAttDesc..')'
            end
end
            table.insert(resultPart, '\r\n' .. rowSuffix .. '| ' .. spAttDesc)
table.insert(resultPart, '\r\n' .. rowSuffix .. '| ' .. spAttDesc)
        end
end
    end
end
    table.insert(resultPart, '\r\n|}')
table.insert(resultPart, '\r\n|}')


    return table.concat(resultPart)
return table.concat(resultPart)
end
end


function p.getSpecialAttackTable(frame)
function p.getSpecialAttackTable(frame)
    local args = frame.args ~= nil and frame.args or frame
local args = frame.args ~= nil and frame.args or frame
    local tableCategories = {'Monster', 'Item', 'Spell', 'Familiar'}
local tableCategories = {'Monster', 'Item', 'Spell', 'Familiar'}
    if args[1] ~= nil and args[1] ~= '' then
if args[1] ~= nil and args[1] ~= '' then
        tableCategories = Shared.splitString(args[1], ',')
tableCategories = Shared.splitString(args[1], ',')
    end
    local effectName = args['effect']
    local sourceHeaderLabel = (args['sourceHeader'] ~= '' and args['sourceHeader']) or 'Source'
    local includeSource = true
    if args['includeSource'] ~= nil and string.lower(args['includeSource']) == 'false' then
        includeSource = false
    end
    local effectDefn = nil
 
    if effectName ~= nil and effectName ~= '' then
        effectDefn = Attacks.effectDefinition[effectName]
        if effectDefn == nil then
            local validEffectNames = {}
            for k, v in pairs(Attacks.effectDefinition) do
                table.insert(validEffectNames, k)
            end
            table.sort(validEffectNames, function(a, b) return a < b end)
 
            return 'ERROR: Invalid effect name "' .. effectName .. '", must be one of: ' .. table.concat(validEffectNames, ', ') .. '[[Category:Pages with script errors]]'
        end
    end
 
    return p._getSpecialAttackTable(effectDefn, tableCategories, sourceHeaderLabel, includeSource)
end
 
-- Generates a table showing the damage/DR multipliers for each combat triangle
function p.getCombatTriangleTable()
local triangleAttributes = {
{
["name"] = 'damageModifier',
["head"] = 'DMG',
["func"] = function(val)
local outVal = 100 * (val - 1)
return { outVal, (outVal < 0 and '' or '+') .. string.format(outVal, '%.0f') .. '%' }
end
},
{
["name"] = 'reductionModifier',
["head"] = 'DR',
["func"] = function(val) return { (val - 1), string.format('%.2fx', val) } end
}
}
local combatStyles = {
{ 'melee', Icons.Icon({ 'Attack', 'Melee', type = 'skill' }) },
{ 'ranged', Icons.Icon({ 'Ranged', type = 'skill' }) },
{ 'magic', Icons.Icon({ 'Magic', type = 'skill' }) }
}
local gameMode = {
{ 'Standard', 'Standard' },
{ 'Hardcore', Icons.Icon({ 'Hardcore' }) }
}
local attrCount = Shared.tableCount(triangleAttributes)
local styleCount = Shared.tableCount(combatStyles)
local modeCount = Shared.tableCount(gameMode)
local resultPart = {}
-- Generate header
table.insert(resultPart, '{| class="wikitable"\r\n|-')
table.insert(resultPart, '\r\n!rowspan="2"| Player Style')
table.insert(resultPart, '\r\n!rowspan="2"| Game Mode')
for i, style in ipairs(combatStyles) do
table.insert(resultPart, '\r\n!colspan="' .. attrCount .. '"| VS ' .. style[2])
end
end
local attrHeader = ''
local effectName = args['effect']
for i, attr in ipairs(triangleAttributes) do
local sourceHeaderLabel = (args['sourceHeader'] ~= '' and args['sourceHeader']) or 'Source'
attrHeader = attrHeader .. '\r\n! ' .. attr.head
local includeSource = true
if args['includeSource'] ~= nil and string.lower(args['includeSource']) == 'false' then
includeSource = false
end
end
table.insert(resultPart, '\r\n|-' .. string.rep(attrHeader, styleCount))
local effectDefn = nil
 
-- Generate table body
if effectName ~= nil and effectName ~= '' then
for i, attStyle in ipairs(combatStyles) do
effectDefn = Attacks.effectDefinition[effectName]
local borderStyle = (i < styleCount and 'style="border-bottom:solid lightgrey"' or '')
if effectDefn == nil then
for j, mode in ipairs(gameMode) do
local validEffectNames = {}
table.insert(resultPart, '\r\n|-')
for k, v in pairs(Attacks.effectDefinition) do
if j == 1 then
table.insert(validEffectNames, k)
table.insert(resultPart, '\r\n|rowspan="' .. modeCount .. '" ' .. borderStyle .. '| ' .. attStyle[2])
elseif j == modeCount and borderStyle ~= '' then
table.insert(resultPart, ' ' .. borderStyle)
end
table.insert(resultPart, '\r\n| ' .. mode[2])
for k, targStyle in ipairs(combatStyles) do
for m, attr in ipairs(triangleAttributes) do
local cellStyle = nil
local attValRaw = Constants.getTriangleAttribute(attr.name, attStyle[1], targStyle[1], mode[1])
local attrVal = attr.func(attValRaw)
if attrVal[1] > 0 then
cellStyle = 'background-color:lightgreen;'
elseif attrVal[1] < 0 then
cellStyle = 'background-color:lightpink;'
end
table.insert(resultPart, '\r\n|' .. (cellStyle ~= nil and 'style="' .. cellStyle .. '"| ' or ' ') .. attrVal[2])
end
end
end
table.sort(validEffectNames, function(a, b) return a < b end)
return Shared.printError('Invalid effect name "' .. effectName .. '", must be one of: ' .. table.concat(validEffectNames, ', '))
end
end
end
end
table.insert(resultPart, '\r\n|}')
 
return table.concat(resultPart)
return p._getSpecialAttackTable(effectDefn, tableCategories, sourceHeaderLabel, includeSource)
end
end


return p
return p

Latest revision as of 01:45, 28 August 2024

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

local p = {}

local Constants = require('Module:Constants')
local Shared = require('Module:Shared')
local GameData = require('Module:GameData')
local Icons = require('Module:Icons')
local Items = require('Module:Items')
local Attacks = require('Module:Attacks')
local Num = require('Module:Number')
local Magic = require('Module:Magic')

function p._getSpecialAttackTable(effectDefn, categories, sourceHeaderLabel, includeSource)
	-- TODO Spells and Familiars need fixing for V1.3
	-- Is going to be incredibly broken following combat effects & modifiers overhaul

	local spAttTable = {}
	local attacks = Attacks.getAttacks(function(attack)
		if effectDefn == nil then
			return true
		else
			return Attacks.attackHasEffect(attack, effectDefn)
		end
	end)
	local includeCat = {}
	for i, category in ipairs(categories) do
		includeCat[category] = true
	end

    -- Build index so we can tell which to include later
    local includedAttacks = {}
    for i, attack in ipairs(attacks) do
        includedAttacks[attack.id] = true
    end

	-- Compile a list of monsters, items, spells, etc. for each included attack
    -- Monsters
    if includeCat['Monster'] then
        for i, monster in ipairs(GameData.rawData.monsters) do
            if monster.specialAttacks ~= nil and not Shared.tableIsEmpty(monster.specialAttacks) then
                local overrideChance = (monster.overrideSpecialChances ~= nil and not Shared.tableIsEmpty(monster.overrideSpecialChances))
                for j, spAttID in ipairs(monster.specialAttacks) do
                    if includedAttacks[spAttID] then
                        local spAtt = GameData.getEntityByID('attacks', spAttID)
                        if spAtt ~= nil then
                            local attChance = (overrideChance and monster.overrideSpecialChances[j]) or spAtt.defaultChance
                            table.insert(spAttTable, { id = spAttID, source = 'Monster', sourceSort = monster.name, sourceText = Icons.Icon({ monster.name, type = 'monster' }), chance = attChance, descType = 'monster' })
                        end
                    end
                end
            end
        end
    end

    -- Items/Weapons
    if includeCat['Item'] then
        for i, item in ipairs(GameData.rawData.items) do
            if item.specialAttacks ~= nil and not item.golbinRaidExclusive and not Shared.tableIsEmpty(item.specialAttacks) then
                local overrideChance = (item.overrideSpecialChances ~= nil and not Shared.tableIsEmpty(item.overrideSpecialChances))
                for j, spAttID in ipairs(item.specialAttacks) do
                    if includedAttacks[spAttID] then
                        local spAtt = GameData.getEntityByID('attacks', spAttID)
                        if spAtt ~= nil then
                            local attChance = (overrideChance and item.overrideSpecialChances[j]) or spAtt.defaultChance
                            table.insert(spAttTable, { id = spAttID, source = 'Weapon', sourceSort = item.name, sourceText = Icons.Icon({ item.name, type = 'item' }), chance = attChance, descType = 'player' })
                        end
                    end
                end
            end
        end
    end

    -- Spells
    if includeCat['Spell'] then
        local spellCats = { 'standard', 'ancient', 'archaic', 'abyssal' }
        for i, spellCat in ipairs(spellCats) do
            for j, spell in ipairs(Magic.getSpellsBySpellBook(spellCat)) do
            	local spAttID = spell.specialAttack or spell.specialAttackID
                if spAttID ~= nil and includedAttacks[spAttID] then
                    local spAtt = GameData.getEntityByID('attacks', spAttID)
                    if spAtt ~= nil then
                        table.insert(spAttTable, {id = spAtt.id, source = 'Spell', sourceSort = spell.name, sourceText = Icons.Icon({ spell.name, type = 'spell' }), chance = spAtt.defaultChance, descType = 'player' })
                    end
                end
            end
        end
    end
	--[[
	-- Summoning familiars. Any effects inflicted by combat familiars aren't actually special
	-- attacks, therefore the handling here is a bit different
	if includeCat['Familiar'] then
		local famIdx = Shared.tableCount(attacks) + 1
		local familiars = Items.getItems(
            function(item)
                if item.type == 'Familiar' and item.modifiers ~= nil and Items._getItemStat(item, 'summoningMaxhit') ~= nil then
                    local famAttack = { prehitEffects = {}, onhitEffects = { { type = 'Modifier', subtype = 'Familiar', modifiers = item.modifiers } } }
                    if effectDefn == nil then
                        return Shared.tableIsEmpty(Attacks.getAttackEffects(famAttack))
                    else
                        return Attacks.attackHasEffect(famAttack, effectDefn)
                    end
                end
                return false
            end)
		for i, familiar in ipairs(familiars) do
			-- For chance, assume the first modifier we come across has the chance, which is pretty lazy
			local famChance, famDesc = 0, ''
			for modName, modVal in pairs(familiar.modifiers) do
				if type(modVal) == 'table' and type(modVal[1]) == 'number' then
					famChance = modVal[1]
				elseif type(modVal) == 'number' then
					famChance = modVal
				else
					famChance = 0
				end
				famDesc = Constants._getModifierText(modName, modVal, false)
				break
			end

			table.insert(spAttTable, { id = familiar.id, source = 'Familiar', sourceSort = familiar.name, sourceText = Icons.Icon({ familiar.name, type = 'item' }), chance = famChance or 0, descType = 'player' })
			-- Slap a dummy entry into the attacks table for this familiar
			attacks[famIdx] = { id = familiar.id, name = familiar.name .. ' (Familiar)', description = famDesc }
			famIdx = famIdx + 1
		end
	end
	--]]
	-- Nothing to output if there are no row definitions
	if Shared.tableIsEmpty(spAttTable) then
		return ''
	end

	-- Sort entries into desired order and generate stats to determine row spans:
	-- By attack index, description type (monster/player), chance, source, then source name (weapon/item/etc.)
	table.sort(spAttTable, function (a, b)
		local sortKeys = { 'id', 'descType', 'chance', 'source', 'sourceSort' }
		for i, key in ipairs(sortKeys) do
			if a[key] ~= b[key] then
				return a[key] < b[key]
			end
		end
		return false
	end)
	-- Determine row counts for grouping/rowspans
	local rowCounts = {}
	for i, rowDefn in ipairs(spAttTable) do
		local id, dt, chance = rowDefn.id, rowDefn.descType, rowDefn.chance
		if rowCounts[id] == nil then
			rowCounts[id] = { rows = 0 }
		end
		if rowCounts[id][dt] == nil then
			rowCounts[id][dt] = { rows = 0 }
		end
		if rowCounts[id][dt][chance] == nil then
			rowCounts[id][dt][chance] = 0
		end
		rowCounts[id]['rows'] = rowCounts[id]['rows'] + 1
		rowCounts[id][dt]['rows'] = rowCounts[id][dt]['rows'] + 1
		rowCounts[id][dt][chance] = rowCounts[id][dt][chance] + 1
	end

	-- Generate output table
	local resultPart = {}
	table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
	table.insert(resultPart, '\r\n|- class="headerRow-0"')
	table.insert(resultPart, '\r\n!Name!!style="min-width:225px"| ' .. sourceHeaderLabel .. (includeSource and '!!Type' or '') .. '!!Chance!!Effect')

	local firstRow = { id = true, descType = true, chance = true }
	local prevRowVal = { id = 0, descType = '', chance = 0 }
	local resetOnChange = {
		id = { 'id', 'descType', 'chance' },
		descType = { 'descType', 'chance' },
		chance = { 'chance' }
	}
	local rowSuffix = ''
	for i, spAttRow in ipairs(spAttTable) do
		local spIdx = spAttRow.id
		local spAtt = GameData.getEntityByID(attacks, spIdx)
		-- Determine if it's the first row for any of our groupings
		local resetKeys = {}
		for key, val in pairs(prevRowVal) do
			if spAttRow[key] ~= prevRowVal[key] then
				for j, keyName in ipairs(resetOnChange[key]) do
					resetKeys[keyName] = true
				end
			end
			prevRowVal[key] = spAttRow[key]
		end
		for key, val in pairs(firstRow) do
			firstRow[key] = (resetKeys[key] ~= nil)
		end

		table.insert(resultPart, '\r\n|-')
		if firstRow.id then
			rowSuffix = (rowCounts[spIdx]['rows'] > 1 and '|rowspan="' .. rowCounts[spIdx]['rows'] .. '"') or ''
			table.insert(resultPart, '\r\n' .. rowSuffix .. '| ' .. spAtt.name)
		end
		table.insert(resultPart, '\r\n|data-sort-value="' .. spAttRow.sourceSort .. '"| ' .. spAttRow.sourceText)
		if includeSource then
			table.insert(resultPart, '\r\n| ' .. spAttRow.source)
		end
		if firstRow.chance then
			rowSuffix = (rowCounts[spIdx][spAttRow.descType][spAttRow.chance] > 1 and 'rowspan="' .. rowCounts[spIdx][spAttRow.descType][spAttRow.chance] .. '" ') or ''
			table.insert(resultPart, '\r\n|' .. rowSuffix .. 'data-sort-value="' .. spAttRow.chance .. '" style="text-align:right;"| ' .. Num.round(spAttRow.chance, 2, 0) .. '%')
		end
		if firstRow.descType then
			rowSuffix = (rowCounts[spIdx][spAttRow.descType]['rows'] > 1 and '|rowspan="' .. rowCounts[spIdx][spAttRow.descType]['rows'] .. '"') or ''
			local spAttDesc = spAtt.description
			--Adding the time between hits and total duration as a note at the end of the special attack description
			local spAttInterval = spAtt.attackInterval ~= nil and spAtt.attackInterval or -1
			if(spAttInterval ~= -1 and spAtt.damage ~= nil and Shared.tableCount(spAtt.damage) > 0 and spAtt.attackCount > 1) then
				spAttDesc = spAttDesc..'<br/>('
				local spAttDuration = spAttInterval * (spAtt.attackCount - 1)
				spAttDesc = spAttDesc..Num.round(spAttInterval / 1000, 2, 2)..'s delay between attacks.'
				if spAtt.attackCount ~= nil and spAtt.attackCount > 2 then
					spAttDesc = spAttDesc..' '..Num.round(spAttDuration / 1000, 2, 2)..'s total duration'
				end
				spAttDesc = spAttDesc..')'
			end
			table.insert(resultPart, '\r\n' .. rowSuffix .. '| ' .. spAttDesc)
		end
	end
	table.insert(resultPart, '\r\n|}')

	return table.concat(resultPart)
end

function p.getSpecialAttackTable(frame)
	local args = frame.args ~= nil and frame.args or frame
	local tableCategories = {'Monster', 'Item', 'Spell', 'Familiar'}
	if args[1] ~= nil and args[1] ~= '' then
		tableCategories = Shared.splitString(args[1], ',')
	end
	local effectName = args['effect']
	local sourceHeaderLabel = (args['sourceHeader'] ~= '' and args['sourceHeader']) or 'Source'
	local includeSource = true
	if args['includeSource'] ~= nil and string.lower(args['includeSource']) == 'false' then
		includeSource = false
	end
	local effectDefn = nil

	if effectName ~= nil and effectName ~= '' then
		effectDefn = Attacks.effectDefinition[effectName]
		if effectDefn == nil then
			local validEffectNames = {}
			for k, v in pairs(Attacks.effectDefinition) do
				table.insert(validEffectNames, k)
			end
			table.sort(validEffectNames, function(a, b) return a < b end)

			return Shared.printError('Invalid effect name "' .. effectName .. '", must be one of: ' .. table.concat(validEffectNames, ', '))
		end
	end

	return p._getSpecialAttackTable(effectDefn, tableCategories, sourceHeaderLabel, includeSource)
end

return p