Module:Damage: Difference between revisions

154 bytes removed ,  10 October 2023
no edit summary
No edit summary
No edit summary
Line 1: Line 1:
-- pystart
require('Module:CommonFunctions');
require('Module:CommonFunctions');
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
Line 262: Line 263:
     else
     else
         DAMAGE_CONFIG = BASE_DAMAGE_CONFIG
         DAMAGE_CONFIG = BASE_DAMAGE_CONFIG
    end
    -- Helper function to check if a table is not empty
    local function isTableNotEmpty(tbl)
        return next(tbl) ~= nil
    end
    -- Function to apply inheritance for a specific damage type and argument
    local function applyInheritance(mainArgValues, inheritArg, mainArgValue, inheritValue)
        if mainArgValue == '' then
            return inheritValue
        elseif mainArgValue and string.find(mainArgValue, 'i') and inheritValue then
            return eval(mainArgValue:gsub('i', inheritValue))
        end
        return mainArgValue
    end
    -- Function to apply inheritance for a specific argument key
    local function applyInheritanceForKey(args, prefix, mainKey, argTable, damageTypeIndex)
        local mainKeyPrefixed = prefix .. mainKey
        local mainArgValues = args[mainKeyPrefixed]
        if mainArgValues then
            local i = 1
            local cancelDmgLen = args.cancel_dmg and #args.cancel_dmg or 0
            while i <= (#args.dmg + cancelDmgLen) do
                local mainArgValue = mainArgValues[i]
                for ix, inheritKey in ipairs(argTable) do
                    local inheritArg = args[prefix .. inheritKey] or args[inheritKey]
                    if inheritArg and inheritArg[i] and inheritArg[i] ~= '' and
                        (damageTypeIndex == 1 and ix ~= 1 or damageTypeIndex ~= 1)
                    then
                        mainArgValues[i] = applyInheritance(mainArgValues, inheritArg, mainArgValue, inheritArg[i])
                        break
                    end
                end
                i = i + 1
            end
        end
     end
     end


Line 267: Line 311:
     function inherit(mode)
     function inherit(mode)
         local prefix = mode == 'PvE' and '' or string.lower(mode .. '_')
         local prefix = mode == 'PvE' and '' or string.lower(mode .. '_')
        for config_key, config_value in pairs(DAMAGE_CONFIG) do
            for arg_table_key, arg_table in pairs(config_value) do
                if arg_table_key ~= 'provided' and arg_table then
                    -- We only do this for the first (main) key
                    local main_key = arg_table[1]
                    local main_key_prefixed = prefix .. main_key
                    local main_arg_values = args[main_key_prefixed]
                    -- Only if the main argument values exist.
                    if main_arg_values then
                        local i = 1
                        --[[
                            Loop over all damage and attempt to inherit in chain.
                            Break the loop if a match was found. Note: For this to work, the value must be an empty string.
                            Alternatively, it can contain an "i" to template the value to inherit.
                        ]]
                        local cancel_dmg_len = args.cancel_dmg and #(args.cancel_dmg) or 0
                        while i <= (#(args.dmg) + cancel_dmg_len) do
                            local main_arg_value = main_arg_values[i]
                            for ix, inherit_key in ipairs(arg_table) do
                                local inherit_arg = args[prefix .. inherit_key] or args[inherit_key]
                                -- No inheritance from itself.
                                if inherit_arg and inherit_arg[i] and inherit_arg[i] ~= '' and ix ~= 1 then
                                    -- Only inherit if empty
                                    if main_arg_value == '' then
                                        args[main_key_prefixed][i] = inherit_arg[i]
                                        break
                                    elseif main_arg_value and string.find(main_arg_value, 'i') and inherit_arg[i] then
                                        args[main_key_prefixed][i] = eval(main_arg_value:gsub('i', inherit_arg[i]))
                                        break
                                    end
                                end
                            end


                            i = i + 1
        for configKey, configValue in pairs(DAMAGE_CONFIG) do
                         end
            for argTableKey, argTable in pairs(configValue) do
                if argTableKey ~= 'provided' and isTableNotEmpty(argTable) then
                    for damageTypeIndex, damageType in ipairs({ '', '_min', '_max' }) do
                         applyInheritanceForKey(args, prefix, argTable[1] .. damageType, argTable, damageTypeIndex)
                     end
                     end
                 end
                 end
Line 889: Line 902:
     if OPTIONS.do_table then
     if OPTIONS.do_table then
         doTable()
         doTable()
     end  
     end


     -- Dump all values if wanted.
     -- Dump all values if wanted.
Line 919: Line 932:


return p
return p
-- pyend