Module:Constants: Difference between revisions

From Melvor Idle
(Created page with p.getModifierText)
 
No edit summary
 
(162 intermediate revisions by 7 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


local ConstantData = mw.loadData('Module:Constants/data')
local Shared = require('Module:Shared')
local GameData = require('Module:GameData')
local Modifiers = require('Module:Modifiers')


local modifierTypes = {
function p.getTriangleAttribute(attribute, attackerStyle, targetStyle, modeName)
  ["increasedGlobalAccuracy"] = { text =  "+{V}% Global Accuracy", },
if type(attribute) ~= 'string' then
  ["increasedMeleeAccuracyBonus"] = { text =  "+{V}% Melee Accuracy Bonus", },
error("Parameter 'attribute' must be a string", 2)
  ["increasedMeleeStrengthBonus"] = { text =  "+{V}% Melee Strength Bonus", },
elseif type(attackerStyle) ~= 'string' then
  ["increasedMeleeEvasion"] = { text = "+{V}% Melee Evasion", },
error("Parameter 'attackerStyle' must be a string", 2)
  ["increasedRangedAccuracyBonus"] = { text =  "+{V}% Ranged Accuracy Bonus", },
elseif type(targetStyle) ~= 'string' then
  ["increasedRangedStrengthBonus"] = { text = "+{V}% Ranged Strength Bonus", },
error("Parameter 'targetStyle' must be a string", 2)
  ["increasedRangedEvasion"] = { text =  "+{V}% Ranged Evasion", },
elseif type(modeName) ~= 'string' then
  ["increasedMagicAccuracyBonus"] = { text =  "+{V}% Magic Accuracy Bonus", },
error("Parameter 'modeName' must be a string", 2)
  ["increasedMagicDamageBonus"] = { text =  "+{V}% Magic Damage Bonus", },
end
  ["increasedMagicEvasion"] = { text =  "+{V}% Magic Evasion", },
  ["increasedMaxHitFlat"] = { text =  "+{VX} Max Hit", },
local mode = GameData.getEntityByName('gamemodes', modeName)
  ["increasedMaxHitPercent"] = { text =  "+{V}% Max Hit", },
if mode == nil then
  ["increasedDamageReduction"] = { text = "+{V}% Damage Reduction", },
error("Invalid gamemode '" .. modeName .. "'", 2)
  ["increasedChanceToDoubleLootCombat"] = { text =  "+{V}% Chance To Double Loot in Combat", },
end
  ["increasedSlayerCoins"] = { text =  "+{V}% Slayer Coins", },
  ["increasedSlayerXP"] = { text =  "+{V}% Slayer XP", },
local attStyle, targStyle = string.lower(attackerStyle), string.lower(targetStyle)
  ["increasedHPRegenFlat"] = { text =  "+{V} Flat HP Regen", },
local validStyles = { 'magic', 'melee', 'ranged' }
  ["increasedGPGlobal"] = { text = "+{V}% Global GP (except Item Sales)", },
if not Shared.contains(validStyles, string.lower(attStyle)) then
  ["increasedGPFromMonsters"] = { text =  "+{V}% GP From Monsters", },
error("Invalid value for parameter 'attackerStyle'", 2)
  ["increasedGPFromMonstersFlat"] = { text =  "+{V} GP From Monsters", },
elseif not Shared.contains(validStyles, string.lower(targStyle)) then
  ["increasedGPFromThieving"] = { text =  "+{V}% GP From Thieving", },
error("Invalid value for parameter 'targetStyle'", 2)
  ["increasedGPFromThievingFlat"] = { text =  "+{V} GP From Thieving", },
end
  ["increasedGPFromAgility"] = { text =  "+{V}% GP From Agility", },
  ["decreasedGPFromAgility"] = { text =  "-{V}% GP From Agility", isNegative = true },
local combatTriangle = GameData.getEntityByID('combatTriangles', mode.combatTriangle)
  ["increasedDamageToBosses"] = { text =  "+{V}% Damage To Bosses", },
if combatTriangle == nil then
  ["increasedDamageToSlayerTasks"] = { text =  "+{V}% Damage To Slayer Tasks", },
error("No such combat triangle: " .. mode.combatTriangle)
  ["increasedDamageToSlayerAreaMonsters"] = { text =  "+{V}% Damage To Slayer Area Monsters", },
end
  ["increasedDamageToCombatAreaMonsters"] = { text = "+{V}% Damage To Combat Area Monsters", },
local attrData = combatTriangle[attribute]
  ["increasedDamageToDungeonMonsters"] = { text =  "+{V}% Damage To Dungeon Monsters", },
if attrData == nil then
  ["increasedDamageToAllMonsters"] = { text =  "+{V}% Damage To All Monsters", },
error("No such attribute: " .. attribute)
  ["increasedAutoEatEfficiency"] = { text = "+{V}% Auto Eat Efficiency", },
else
  ["increasedAutoEatThreshold"] = { text =  "+{V}% Auto Eat Threshold", },
return attrData[attStyle][targStyle]
  ["increasedAutoEatHPLimit"] = { text = "+{V}% Auto Eat HP Limit", },
end
  ["increasedFoodHealingValue"] = { text =  "+{V}% Food Healing Value", },
end
  ["increasedChanceToPreservePrayerPoints"] = { text =  "+{V}% Chance To Preserve Prayer Points", },
 
  ["increasedFlatPrayerCostReduction"] = { text =  "+{V} Flat Prayer Cost Reduction", },
function p.getTriangleDamageModifier(attackerStyle, targetStyle, mode)
  ["increasedMinAirSpellDmg"] = { text =  "+{V} Min Air Spell Dmg", },
return p.getTriangleAttribute('damageModifier', attackerStyle, targetStyle, mode)
  ["increasedMinWaterSpellDmg"] = { text =  "+{V} Min Water Spell Dmg", },
end
  ["increasedMinEarthSpellDmg"] = { text =  "+{V} Min Earth Spell Dmg", },
 
  ["increasedMinFireSpellDmg"] = { text =  "+{V} Min Fire Spell Dmg", },
--Syntax is like p.getTriangleDRModifier('Melee', 'Ranged', 'Normal')
  ["increasedAmmoPreservation"] = { text =  "+{V}% Ammo Preservation", },
--Returns a multiplier that can be multiplied with base DR to get the post-Triangle result
  ["increasedRunePreservation"] = { text =  "+{V}% Rune Preservation", },
function p.getTriangleDRModifier(attackerStyle, targetStyle, mode)
  ["increasedHiddenSkillLevel"] = { text =  "+{V1} Hidden {SV0} Level", },
return p.getTriangleAttribute('reductionModifier', attackerStyle, targetStyle, mode)
  ["decreasedPlayerAttackSpeed"] = { text =  "-{V} Player Attack Speed", },
end
  ["decreasedPlayerAttackSpeedPercent"] = { text =  "-{V}% Player Attack Speed", },
 
  ["increasedSlayerAreaEffectNegationFlat"] = { text =  "+{V}% Flat Slayer Area Effect Negation", },
function p.getDifficultyString(difficulty)
  ["decreasedMonsterRespawnTimer"] = { text =  "-{VMS}s Monster Respawn Timer", },
return GameData.rawData.combatAreaDifficulties[difficulty + 1]
  ["decreasedGlobalAccuracy"] = { text =  "-{V}% Global Accuracy", isNegative = true },
end
  ["decreasedMeleeAccuracyBonus"] = { text =  "-{V}% Melee Accuracy Bonus", isNegative = true },
  ["decreasedMeleeStrengthBonus"] = { text =  "-{V}% Melee Strength Bonus", isNegative = true },
  ["decreasedMeleeEvasion"] = { text =  "-{V}% Melee Evasion", isNegative = true },
  ["decreasedRangedAccuracyBonus"] = { text =  "-{V} Ranged Accuracy Bonus", isNegative = true },
  ["decreasedRangedStrengthBonus"] = { text =  "-{V}% Ranged Strength Bonus", isNegative = true },
  ["decreasedRangedEvasion"] = { text =  "-{V}% Ranged Evasion", isNegative = true },
  ["decreasedMagicAccuracyBonus"] = { text =  "-{V}% Magic Accuracy Bonus", isNegative = true },
  ["decreasedMagicDamageBonus"] = { text =  "-{V}% Magic Damage Bonus", isNegative = true },
  ["decreasedMagicEvasion"] = { text =  "-{V}% Magic Evasion", isNegative = true },
  ["decreasedMaxHitFlat"] = { text =  "-{V} Max Hit", isNegative = true },
  ["decreasedMaxHitPercent"] = { text =  "-{V}% Max Hit", isNegative = true },
  ["decreasedDamageReduction"] = { text =  "-{V}% Damage Reduction", isNegative = true },
  ["decreasedChanceToDoubleLootCombat"] = { text =  "-{V}% Chance To Double Loot in Combat", isNegative = true },
  ["decreasedSlayerCoins"] = { text =  "-{V}% Slayer Coins", isNegative = true },
  ["decreasedSlayerXP"] = { text =  "-{V}% Slayer XP", isNegative = true },
  ["decreasedHPRegenFlat"] = { text =  "-{V} Flat HP Regen", isNegative = true },
  ["decreasedGPGlobal"] = { text =  "-{V}% GP from all sources (Except Item Selling)", isNegative = true },
  ["decreasedGPFromMonsters"] = { text =  "-{V}% GP From Monsters", isNegative = true },
  ["decreasedGPFromMonstersFlat"] = { text =  "-{V} GP From Monsters", isNegative = true },
  ["decreasedDamageToBosses"] = { text =  "-{V}% Damage To Bosses", isNegative = true },
  ["decreasedDamageToSlayerTasks"] = { text =  "-{V}% Damage To Slayer Tasks", isNegative = true },
  ["decreasedDamageToSlayerAreaMonsters"] = { text =  "-{V}% Damage To Slayer Area Monsters", isNegative = true },
  ["decreasedDamageToCombatAreaMonsters"] = { text =  "-{V}% Damage To Combat Area Monsters", isNegative = true },
  ["decreasedDamageToDungeonMonsters"] = { text =  "-{V}% Damage To Dungeon Monsters", isNegative = true },
  ["decreasedDamageToAllMonsters"] = { text =  "-{V}% Damage To All Monsters", isNegative = true },
  ["decreasedAutoEatEfficiency"] = { text =  "-{V}% Auto Eat Efficiency", isNegative = true },
  ["decreasedAutoEatThreshold"] = { text =  "-{V}% Auto Eat Threshold", isNegative = true },
  ["decreasedAutoEatHPLimit"] = { text =  "-{V}% Auto Eat HP Limit", isNegative = true },
  ["decreasedFoodHealingValue"] = { text =  "-{V}% Food Healing Value", isNegative = true },
  ["decreasedChanceToPreservePrayerPoints"] = { text =  "-{V}% Chance To Preserve Prayer Points", isNegative = true },
  ["decreasedFlatPrayerCostReduction"] = { text =  "-{V} Flat Prayer Cost Reduction", isNegative = true },
  ["decreasedMinAirSpellDmg"] = { text =  "-{V} Min Air Spell Dmg", isNegative = true },
  ["decreasedMinWaterSpellDmg"] = { text =  "-{V} Min Water Spell Dmg", isNegative = true },
  ["decreasedMinEarthSpellDmg"] = { text =  "-{V} Min Earth Spell Dmg", isNegative = true },
  ["decreasedMinFireSpellDmg"] = { text =  "-{V} Min Fire Spell Dmg", isNegative = true },
  ["decreasedAmmoPreservation"] = { text =  "-{V}% Ammo Preservation", isNegative = true },
  ["decreasedRunePreservation"] = { text =  "-{V}% Rune Preservation", isNegative = true },
  ["decreasedHiddenSkillLevel"] = { text =  "-{V1} Hidden {SV0} Level", isNegative = true },
  ["increasedPlayerAttackSpeed"] = { text =  "+{V} Player Attack Speed", isNegative = true },
  ["increasedPlayerAttackSpeedPercent"] = { text =  "+{V}% Player Attack Speed", isNegative = true },
  ["increasedMonsterRespawnTimer"] = { text =  "+{VMS}s Monster Respawn Timer", isNegative = true },
  ["decreasedSlayerAreaEffectNegationFlat"] = { text =  "-{V}% Flat Slayer Area Effect Negation", isNegative = true },
  ["increasedGPFromSales"] = { text =  "+{V}% GP From Sales", },
  ["increasedBankSpace"] = { text =  "+{V} Bank Space", },
  ["increasedBankSpaceShop"] = { text =  "+{V} Bank Space from Shop", },
  ["increasedChanceToPreservePotionCharge"] = { text =  "+{V}% Chance To Preserve Potion Charge", },
  ["decreasedGPFromSales"] = { text =  "-{V}% GP From Sales", isNegative = true },
  ["decreasedBankSpace"] = { text =  "-{V} Bank Space", isNegative = true },
  ["decreasedBankSpaceShop"] = { text =  "-{V} Bank Space from Shop", isNegative = true },
  ["decreasedChanceToPreservePotionCharge"] = { text =  "-{V}% Chance To Preserve Potion Charge", isNegative = true },
  ["decreasedSkillInterval"] = { text =  "-{V1} {SV0} Interval", },
  ["decreasedSkillIntervalPercent"] = { text =  "-{V1}% {SV0} Interval", },
  ["increasedChanceToDoubleLootThieving"] = { text =  "+{V}% Chance To Double Loot in Thieving", },
  ["increasedPreservationChance"] = { text =  "+{V}% Chance to Preserve Resources", },
  ["increasedStaminaPreservationChance"] = { text =  "+{V}% Chance to Preserve Stamina", },
  ["increasedGlobalMasteryXP"] = { text =  "+{V}% Global Mastery XP", },
  ["increasedGlobalSkillXP"] = { text =  "+{V}% Global Skill XP", },
  ["decreasedGlobalSkillXP"] = { text =  "-{V}% Global Skill XP", isNegative = true },
  ["increasedMasteryXP"] = { text =  "+{V1}% {SV0} Mastery XP", },
  ["increasedSkillXP"] = { text =  "+{V1}% {SV0} Skill XP", },
  ["increasedMaxStamina"] = { text =  "+{V}% Max Stamina", },
  ["increasedMiningNodeHP"] = { text =  "+{V} Mining Node HP", },
  ["increasedSkillInterval"] = { text =  "+{V1} {SV0} Interval", isNegative = true },
  ["increasedSkillIntervalPercent"] = { text =  "+{V1}% {SV0} Interval", isNegative = true },
  ["decreasedChanceToDoubleLootThieving"] = { text =  "-{V}% Chance To Double Loot in Thieving", isNegative = true },
  ["decreasedPreservationChance"] = { text =  "-{V}% Chance to Preserve Resources", isNegative = true },
  ["decreasedStaminaPreservationChance"] = { text =  "-{V}% Chance to Preserve Stamina", isNegative = true },
  ["decreasedGlobalMasteryXP"] = { text =  "-{V}% Global Mastery XP", isNegative = true },
  ["decreasedMasteryXP"] = { text =  "-{V1}% {SV0} Mastery XP", isNegative = true },
  ["decreasedSkillXP"] = { text =  "-{V1}% {SV0} Skill XP", isNegative = true },
  ["decreasedMaxStamina"] = { text =  "-{V} Max Stamina", isNegative = true },
  ["decreasedMiningNodeHP"] = { text =  "-{V} Mining Node HP", isNegative = true },
  ["decreasedGPFromThieving"] = { text =  "-{V}% GP From Thieving", isNegative = true },
  ["decreasedGPFromThievingFlat"] = { text =  "-{V} GP From Thieving", isNegative = true },
  ["dungeonEquipmentSwapping"] = { text =  "{V} Dungeon Equipment Swapping", },
  ["increasedEquipmentSets"] = { text =  "+{V} Equipment Sets", },
  ["autoSlayerUnlocked"] = { text =  "{V} Auto Slayer Unlocked", },
  ["increasedTreeCutLimit"] = { text =  "+{V} Tree Cut Limit", },
  ["increasedChanceToDoubleItems"] = { text =  "+{V} Chance To Double Items", },
  ["decreasedChanceToDoubleItems"] = { text =  "-{V} Chance To Double Items", isNegative = true },
  ["increasedGlobalPreservationChance"] = { text =  "+{V}% Chance to Preserve Resources in Skills", },
  ["decreasedGlobalPreservationChance"] = { text =  "-{V}% Chance to Preserve Resources in Skills", isNegative = true },
  ["increasedFarmingYield"] = { text =  "+{V}% Farming Yield", },
  ["decreasedFarmingYield"] = { text =  "-{V}% Farming Yield", isNegative = true },
  ["increasedMaxHitpoints"] = { text =  "+{VX} Maximum Hitpoints", },
  ["decreasedMaxHitpoints"] = { text =  "-{VX} Maximum Hitpoints", isNegative = true },
  ["increasedStaminaPerObstacle"] = { text =  "+{V} Stamina per Agility Obstacle Completion", },
  ["decreasedStaminaPerObstacle"] = { text =  "+{V} Stamina per Agility Obstacle Completion", isNegative = true },
  ["increasedSlayerTaskLength"] = { text =  "+{V}% Slayer Task Length/Qty", },
  ["decreasedSlayerTaskLength"] = { text =  "+{V}% Slayer Task Length/Qty", isNegative = true },
  ["decreasedFlatPrayerCostReduction"] = { text =  "+{V} Prayer Point Cost for Prayers", isNegative = true },
  ["increasedFlatPrayerCostReduction"] = { text =  "+{V} Prayer Point Cost for Prayers", },
  ["increasedStaminaCost"] = { text =  "+{V} Stamina Cost per action", isNegative = true },
  ["decreasedStaminaCost"] = { text =  "+{V} Stamina Cost per action", },
  ["increasedChanceToDoubleItemsSkill"] = { text =  "+{V1}% Chance to Double Items in {SV0}", },
  ["decreasedChanceToDoubleItemsSkill"] = { text =  "+{V1}% Chance to Double Items in {SV0}", isNegative = true },
  ["increasedChanceToDoubleItemsGlobal"] = { text =  "+{V}% Chance to Double Items Globally", },
  ["decreasedChanceToDoubleItemsGlobal"] = { text =  "+{V}% Chance to Double Items Globally", isNegative = true },
  ["increasedLifesteal"] = { text =  "+{V}% Lifesteal", },
  ["decreasedLifesteal"] = { text =  "+{V}% Lifesteal", isNegative = true },
  ["increasedReflectDamage"] = { text =  "+{V}% Reflect Damage", },
  ["decreasedReflectDamage"] = { text =  "+{V}% Reflect Damage", isNegative = true },
}


function p.getSkillName(skillID)
function p.getSkillName(skillID)
  for skName, ID in Shared.skpairs(ConstantData.skill) do
local skill = GameData.getSkillData(skillID)
    if ID == skillID then
if skill ~= nil then
      return skName
return skill.name
    end
end
  end
end
  return nil
 
function p.getSkillID(skillName)
for i, skillData in ipairs(GameData.rawData.skillData) do
if skillData.data.name == skillName then
return skillData.skillID
end
end
end
 
function p.getSkillIDText(frame)
local skillName = frame.args ~= nil and frame.args[1] or frame[1]
local id = p.getSkillID(skillName)
if id == nil or id == '' then
return "Unknown"
else
return id
end
end
 
function p.getEquipmentSlotName(id)
local slotData = GameData.getEntityByID('equipmentSlots', id)
if slotData ~= nil then
return slotData.emptyName
end
end
 
function p.getEquipmentSlotID(name)
local slotData = GameData.getEntityByProperty('equipmentSlots', 'emptyName', name)
if slotData ~= nil then
return slotData.id
end
end
 
function p.getCombatStyleName(styleNum)
if type(styleNum) == 'number' then
local styleName = GameData.rawData.attackTypes[styleNum]
if styleName ~= nil then
return Shared.titleCase(styleName)
end
elseif type(styleNum) == 'string' and type(GameData.rawData.attackTypes[string.lower(styleNum)]) == 'number' then
return Shared.titleCase(styleNum)
end
return Shared.printError('Invalid combat style')
end
 
 
--- Slayer functions
--
function p.getSlayerTierByID(tierID)
if type(tierID) ~= 'number' then
return nil
else
return GameData.rawData.slayerTaskCategories[tierID + 1]
end
end
 
function p.getSlayerTier(name)
return GameData.getEntityByProperty('slayerTiers', 'display', name)
end
 
function p.getSlayerTierByLevel(level)
if type(level) ~= 'number' or level < 1 then
return Shared.printError('Invalid Slayer level')
end
 
for i, tier in ipairs(GameData.rawData.slayerTaskCategories) do
if tier.type == 'CombatLevel' and tier.minLevel <= level and (tier.maxLevel == nil or tier.maxLevel >= level) then
return tier
end
end
end
 
--
-- the following functions just return subsets of the slayer functions above
--
 
function p.getSlayerTierName(tierID)
if type(tierID) == 'number' then
local tier = p.getSlayerTierByID(tierID)
if tier ~= nil then
return tier.display
end
end
return Shared.printError('Invalid Slayer tier')
end
 
function p.getSlayerTierNameByLevel(lvl)
local tier = p.getSlayerTierByLevel(lvl)
if type(tier) == 'table' then
return tier.display
else
return Shared.printError('Invalid Slayer tier')
end
end
 
--
--- End of slayer functions
 
--Turns a modifier name like 'increasedHPRegenFlat' into several pieces of data:
--Base Name, Description, IsNegative, and modifyValue
--ex. "HPRegenFlat", "+${value} Flat Hitpoints Regeneration", false, "multiplyByNumberMultiplier"
function p.getModifierDetails(modifierName)
-- Unsupported
error('Function is no longer supported', 2)
--[==[
local baseName = modifierName
local modifier = GameData.rawData.modifierData[modifierName]
 
if modifier == nil then
return nil
end
 
if Shared.startsWith(modifierName, "increased") or Shared.startsWith(modifierName, "decreased") then
baseName = string.sub(modifierName, 10)
end
 
return baseName, modifier.description, modifier.isNegative, modifier.modifyValue
--]==]
end
 
function p._getModifierText(modifier, value, doColor)
return Modifiers.getModifierText(modifier, value, doColor)
end
 
function p.getModifierText(frame)
local modifier = frame.args ~= nil and frame.args[1] or frame[1]
local value = frame.args ~= nil and frame.args[2] or frame[2]
local skill = frame.args ~= nil and frame.args.skill or frame.skill
local doColor = frame.args ~= nil and frame.args[3] or frame[3]
 
if doColor ~= nil then
doColor = string.upper(doColor) ~= 'FALSE'
end
 
if skill ~= nil and skill ~= '' then
value = { { ["skillID"] = p.getSkillID(skill), ["value"] = value } }
end
 
return p._getModifierText(modifier, value, doColor)
end
 
function p.getModifiersText(modifiers, doColor, inline, maxVisible)
return Modifiers.getModifiersText(modifiers, doColor, inline, maxVisible)
end
 
function p.getModifierSkills(modifiers)
return Modifiers.getModifierSkills(modifiers)
end
end


-- Returns a modifiers table indicating modifiersNew less modifiersOld
-- The returned table can be passed to getModifiersText to obtain the
-- result in a human readable format
function p.getModifiersDifference(modifiersOld, modifiersNew)
local modHasPrefix = {}
local modDiff, modDiffBase = {}, {}
local allMods = {
{ ["mods"] = (modifiersNew or {}), ["mult"] = 1 },
{ ["mods"] = (modifiersOld or {}), ["mult"] = -1 }
}


function p.getModifierText(modifier, value, doColor)
-- Generate modifiers table containing only variances
  if doColor == nil then doColor = true end
-- Create modDiffBase with mod base names (less 'increased'/'decreased' prefixes),
  local template = modifierTypes[modifier]
for i, modDef in ipairs(allMods) do
for modName, value in pairs(modDef.mods) do
local modBaseName, modIsIncrease = modName, true
if Shared.startsWith(modName, "increased") or Shared.startsWith(modName, "decreased") then
modBaseName = string.sub(modName, 10)
modIsIncrease = Shared.startsWith(modName, "increased")
modHasPrefix[modBaseName] = true
end
local modMult = (modIsIncrease and 1 or -1) * modDef.mult


  if template == nil then
if type(value) == 'table' then
    return 'ERROR: Invalid modifier type [[Category:Pages with script errors]]'
-- Skill specific modifier
  end
if modDiffBase[modBaseName] == nil then
modDiffBase[modBaseName] = {}
end
for j, subVal in ipairs(value) do
if type(subVal) == 'table' and subVal.skillID ~= nil then
modDiffBase[modBaseName][subVal.skillID] = (modDiffBase[modBaseName][subVal.skillID] or 0) + subVal.value * modMult
end
end
else
modDiffBase[modBaseName] = (modDiffBase[modBaseName] or 0) + value * modMult
end
end
end


  local result = template.text
-- Transform modDiffBase into modDiff with the appropriate mod name within the return value
for modBaseName, value in pairs(modDiffBase) do
if type(value) == 'table' then
-- Skill specific modifier
for skillID, subVal in pairs(value) do
if subVal ~= 0 then
local modName = nil
if modHasPrefix[modBaseName] then
modName = (subVal < 0 and 'decreased' or 'increased') .. modBaseName
else
modName = modBaseName
end
if modDiff[modName] == nil then
modDiff[modName] = {}
end
table.insert(modDiff[modName], { ["skillID"] = skillID, ["value"] = math.abs(subVal) })
end
end
elseif value ~= 0 then
local modName = nil
if modHasPrefix[modBaseName] then
modName = (value < 0 and 'decreased' or 'increased') .. modBaseName
else
modName = modBaseName
end
modDiff[modName] = (modDiff[modName] or 0) + math.abs(value)
if GameData.rawData.modifierData[modName] == nil then
modDiff[modName] = nil
end
end
end


  if type(value) == 'table' then
return modDiff
    if value[1] ~= nil then
end
      result = string.gsub(result, '{SV0}', p.getSkillName(value[1]))
    end
    if value[2] ~= nil then
      result = string.gsub(result, '{V1}', value[2])
    end
  else
    result = string.gsub(result, '{V}', value)
    result = string.gsub(result, '{VMS}', value / 1000)
    result = string.gsub(result, '{VX}', value * 10)
  end


  return result
-- From game version 1.1 onwards, some entities have custom descriptions, while
-- many are generated based on the modifiers associated to that entity. This
-- function handles that logic given a custom description (may be nil) and
-- a modifiers object
function p.getDescription(customDescription, modifiers)
if customDescription ~= nil then
return customDescription
else
return p.getModifiersText(modifiers, false)
end
end
end


return p
return p

Latest revision as of 22:37, 22 June 2024

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

local p = {}

local Shared = require('Module:Shared')
local GameData = require('Module:GameData')
local Modifiers = require('Module:Modifiers')

function p.getTriangleAttribute(attribute, attackerStyle, targetStyle, modeName)
	if type(attribute) ~= 'string' then
		error("Parameter 'attribute' must be a string", 2)
	elseif type(attackerStyle) ~= 'string' then
		error("Parameter 'attackerStyle' must be a string", 2)
	elseif type(targetStyle) ~= 'string' then
		error("Parameter 'targetStyle' must be a string", 2)
	elseif type(modeName) ~= 'string' then
		error("Parameter 'modeName' must be a string", 2)
	end
	
	local mode = GameData.getEntityByName('gamemodes', modeName)
	if mode == nil then
		error("Invalid gamemode '" .. modeName .. "'", 2)
	end
	
	local attStyle, targStyle = string.lower(attackerStyle), string.lower(targetStyle)
	local validStyles = { 'magic', 'melee', 'ranged' }
	if not Shared.contains(validStyles, string.lower(attStyle)) then
		error("Invalid value for parameter 'attackerStyle'", 2)
	elseif not Shared.contains(validStyles, string.lower(targStyle)) then
		error("Invalid value for parameter 'targetStyle'", 2)
	end
	
	local combatTriangle = GameData.getEntityByID('combatTriangles', mode.combatTriangle)
	if combatTriangle == nil then
		error("No such combat triangle: " .. mode.combatTriangle)
	end
	local attrData = combatTriangle[attribute]
	if attrData == nil then
		error("No such attribute: " .. attribute)
	else
		return attrData[attStyle][targStyle]
	end
end

function p.getTriangleDamageModifier(attackerStyle, targetStyle, mode)
	return p.getTriangleAttribute('damageModifier', attackerStyle, targetStyle, mode)
end

--Syntax is like p.getTriangleDRModifier('Melee', 'Ranged', 'Normal')
--Returns a multiplier that can be multiplied with base DR to get the post-Triangle result
function p.getTriangleDRModifier(attackerStyle, targetStyle, mode)
	return p.getTriangleAttribute('reductionModifier', attackerStyle, targetStyle, mode)
end

function p.getDifficultyString(difficulty)
	return GameData.rawData.combatAreaDifficulties[difficulty + 1]
end

function p.getSkillName(skillID)
	local skill = GameData.getSkillData(skillID)
	if skill ~= nil then
		return skill.name
	end
end

function p.getSkillID(skillName)
	for i, skillData in ipairs(GameData.rawData.skillData) do
		if skillData.data.name == skillName then
			return skillData.skillID
		end
	end
end

function p.getSkillIDText(frame)
	local skillName = frame.args ~= nil and frame.args[1] or frame[1]
	local id = p.getSkillID(skillName)
	
	if id == nil or id == '' then
		return "Unknown"
	else
		return id
	end
end

function p.getEquipmentSlotName(id)
	local slotData = GameData.getEntityByID('equipmentSlots', id)
	if slotData ~= nil then
		return slotData.emptyName
	end
end

function p.getEquipmentSlotID(name)
	local slotData = GameData.getEntityByProperty('equipmentSlots', 'emptyName', name)
	if slotData ~= nil then
		return slotData.id
	end
end

function p.getCombatStyleName(styleNum)
	if type(styleNum) == 'number' then
		local styleName = GameData.rawData.attackTypes[styleNum]
		if styleName ~= nil then
			return Shared.titleCase(styleName)
		end
	elseif type(styleNum) == 'string' and type(GameData.rawData.attackTypes[string.lower(styleNum)]) == 'number' then
		return Shared.titleCase(styleNum)
	end
	return Shared.printError('Invalid combat style')
end


--- Slayer functions
--
function p.getSlayerTierByID(tierID)
	if type(tierID) ~= 'number' then
		return nil
	else
		return GameData.rawData.slayerTaskCategories[tierID + 1]
	end
end

function p.getSlayerTier(name)
	return GameData.getEntityByProperty('slayerTiers', 'display', name)
end

function p.getSlayerTierByLevel(level)
	if type(level) ~= 'number' or level < 1 then
		return Shared.printError('Invalid Slayer level')
	end

	for i, tier in ipairs(GameData.rawData.slayerTaskCategories) do
		if tier.type == 'CombatLevel' and tier.minLevel <= level and (tier.maxLevel == nil or tier.maxLevel >= level) then
			return tier
		end
	end
end

--
-- the following functions just return subsets of the slayer functions above
--

function p.getSlayerTierName(tierID)
	if type(tierID) == 'number' then
		local tier = p.getSlayerTierByID(tierID)
		if tier ~= nil then
			return tier.display
		end
	end
	return Shared.printError('Invalid Slayer tier')
end

function p.getSlayerTierNameByLevel(lvl)
	local tier = p.getSlayerTierByLevel(lvl)
	if type(tier) == 'table' then
		return tier.display
	else
		return Shared.printError('Invalid Slayer tier')
	end
end

--
--- End of slayer functions

--Turns a modifier name like 'increasedHPRegenFlat' into several pieces of data:
--Base Name, Description, IsNegative, and modifyValue
--ex. "HPRegenFlat", "+${value} Flat Hitpoints Regeneration", false, "multiplyByNumberMultiplier"
function p.getModifierDetails(modifierName)
	-- Unsupported
	error('Function is no longer supported', 2)
	--[==[
	local baseName = modifierName
	local modifier = GameData.rawData.modifierData[modifierName]

	if modifier == nil then
		return nil
	end

	if Shared.startsWith(modifierName, "increased") or Shared.startsWith(modifierName, "decreased") then
		baseName = string.sub(modifierName, 10)
	end

	return baseName, modifier.description, modifier.isNegative, modifier.modifyValue
	--]==]
end

function p._getModifierText(modifier, value, doColor)
	return Modifiers.getModifierText(modifier, value, doColor)
end

function p.getModifierText(frame)
	local modifier = frame.args ~= nil and frame.args[1] or frame[1]
	local value = frame.args ~= nil and frame.args[2] or frame[2]
	local skill = frame.args ~= nil and frame.args.skill or frame.skill
	local doColor = frame.args ~= nil and frame.args[3] or frame[3]

	if doColor ~= nil then
		doColor = string.upper(doColor) ~= 'FALSE'
	end

	if skill ~= nil and skill ~= '' then
		value = { { ["skillID"] = p.getSkillID(skill), ["value"] = value } }
	end

	return p._getModifierText(modifier, value, doColor)
end

function p.getModifiersText(modifiers, doColor, inline, maxVisible)
	return Modifiers.getModifiersText(modifiers, doColor, inline, maxVisible)
end

function p.getModifierSkills(modifiers)
	return Modifiers.getModifierSkills(modifiers)
end

-- Returns a modifiers table indicating modifiersNew less modifiersOld
-- The returned table can be passed to getModifiersText to obtain the
-- result in a human readable format
function p.getModifiersDifference(modifiersOld, modifiersNew)
	local modHasPrefix = {}
	local modDiff, modDiffBase = {}, {}
	local allMods = {
		{ ["mods"] = (modifiersNew or {}), ["mult"] = 1 },
		{ ["mods"] = (modifiersOld or {}), ["mult"] = -1 }
	}

	-- Generate modifiers table containing only variances
	-- Create modDiffBase with mod base names (less 'increased'/'decreased' prefixes),
	for i, modDef in ipairs(allMods) do
		for modName, value in pairs(modDef.mods) do
			local modBaseName, modIsIncrease = modName, true
			if Shared.startsWith(modName, "increased") or Shared.startsWith(modName, "decreased") then
				modBaseName = string.sub(modName, 10)
				modIsIncrease = Shared.startsWith(modName, "increased")
				modHasPrefix[modBaseName] = true
			end
			local modMult = (modIsIncrease and 1 or -1) * modDef.mult

			if type(value) == 'table' then
				-- Skill specific modifier
				if modDiffBase[modBaseName] == nil then
					modDiffBase[modBaseName] = {}
				end
				for j, subVal in ipairs(value) do
					if type(subVal) == 'table' and subVal.skillID ~= nil then
						modDiffBase[modBaseName][subVal.skillID] = (modDiffBase[modBaseName][subVal.skillID] or 0) + subVal.value * modMult
					end
				end
			else
				modDiffBase[modBaseName] = (modDiffBase[modBaseName] or 0) + value * modMult
			end
		end
	end

	-- Transform modDiffBase into modDiff with the appropriate mod name within the return value
	for modBaseName, value in pairs(modDiffBase) do
		if type(value) == 'table' then
			-- Skill specific modifier
			for skillID, subVal in pairs(value) do
				if subVal ~= 0 then
					local modName = nil
					if modHasPrefix[modBaseName] then
						modName = (subVal < 0 and 'decreased' or 'increased') .. modBaseName
					else
						modName = modBaseName
					end
					if modDiff[modName] == nil then
						modDiff[modName] = {}
					end
					table.insert(modDiff[modName], { ["skillID"] = skillID, ["value"] = math.abs(subVal) })
				end
			end
		elseif value ~= 0 then
			local modName = nil
			if modHasPrefix[modBaseName] then
				modName = (value < 0 and 'decreased' or 'increased') .. modBaseName
			else
				modName = modBaseName
			end
			modDiff[modName] = (modDiff[modName] or 0) + math.abs(value)
			if GameData.rawData.modifierData[modName] == nil then
				modDiff[modName] = nil
			end
		end
	end

	return modDiff
end

-- From game version 1.1 onwards, some entities have custom descriptions, while
-- many are generated based on the modifiers associated to that entity. This
-- function handles that logic given a custom description (may be nil) and
-- a modifiers object
function p.getDescription(customDescription, modifiers)
	if customDescription ~= nil then
		return customDescription
	else
		return p.getModifiersText(modifiers, false)
	end
end

return p