H3lp Yours3lfDocs
api

spellUtil

Reuse OpenMW-derived spell, enchantment, potion, and casting calculations from Lua.

Used in the St4sh
  • S3maphore: music/core.lua — A production music loop combining actor state, transitions, cleanup, magic checks, and randomized selection.
  • H4ND: TTTH/hud.lua — A HUD consumer combining attached actor data, settings state, atlas frames, magic calculations, and randomized presentation.
require 'scripts.s3.spellUtil' → MagicHelper

spellUtil collects calculations that otherwise get duplicated in HUDs, gameplay helpers, and magic-aware systems. It is available in local and player scripts and requires H3's installed S3lf interface to read the current actor's skills and fatigue.

local Magic = require 'scripts.s3.spellUtil'

local castable = Magic.getCastable(actor)
local icon = castable and Magic.getCastableIcon(actor)
local chance = Magic.getSpellCastChance(spell, actor, true, true)

Public functions

FunctionBehavior
getCastable(actor)Return the selected enchanted item, selected spell, or nil.
getEffectCost(effect, baseEffect?, method)Calculate one effect's cost using CostMethod.
getEffectListCost(effects, method?)Sum effect costs; target-range effects receive the OpenMW multiplier.
getSpellCost(spell)Return authored cost or calculate an autocalculated spell cost.
getBaseCastChance(spell)Return base chance and the lowest effective magic school for the current S3lf actor.
getSpellCastChance(spell, actor, checkMagicka?, cap?)Return effective cast chance, optionally checking magicka and clamping to 0..100.
getSpellSchool(spell) / getSpellIdSchool(id)Return the effective school identifier, or nil when an ID is unknown.
spellIncreasesSkill(spell) / spellIdIncreasesSkill(id)Test whether a normal, non-always-succeed spell trains skill.
getFatigueTerm()Calculate the current actor's fatigue multiplier.
getEnchantmentCharge(enchantment)Return authored or autocalculated total enchantment capacity.
getCastableIcon(actor)Return the selected castable's icon VFS path, or nil.
getEnchantmentBaseCost(cost, actor)Apply the actor's Enchant skill adjustment, with a minimum result of 1.
getEffectiveEnchantCost(enchantment, actor)Calculate an enchantment's effective cast cost.
getPotionValue(potion)Return authored or autocalculated potion value.
rollIngredientEffect(caster, ingredient, index)Roll one of the four ingredient effects and return effect parameters, or nil when the roll fails.

CostMethod contains GameSpell, PlayerSpell, Enchantment, and Potion. Autocalculation reads OpenMW game settings and magic-effect records. Invalid spell, enchantment, effect, actor, or ingredient inputs raise rather than becoming silently empty results. The helper performs ordinary temporary work for effect lists and ingredient rolls; it is intended for calculations, not per-frame polling without a caller-owned budget.

Relationship to OpenMW

The formulas follow the engine's mechanics implementation, but the Lua boundary is not a promise that every detail is identical. In particular, rollIngredientEffect uses Lua's math.random, while current OpenMW uses its world PRNG. Read the OpenMW spell utility implementation, especially cost and charge calculations, ingredient rolls, and casting chance, when engine-version details matter.