Spdamageutil
Overview
This singleton component provides infrastructure for registering, querying, and manipulating special damage and defense types in DST’s combat system. It allows mods and core systems to define named damage types (e.g., "planar") with associated damage/defense calculation logic, and supports aggregating, merging, modifying, and applying such damage against entities.
Dependencies & Tags
This component does not add any components or tags to entities. It is a utility module with no direct dependency on other components—however, its default "planar" type implementation checks for the presence of planardamage and planardefense components.
Properties
No public instance-level properties are initialized. All state is held in module-scoped tables (SpTypeMap, Fallbacks), which are accessible via _SpTypeMap and _Fallbacks for internal or modding use (with caution).
Main Functions
DefineSpType(sptype, spdata)
- Description: Registers a new special damage type (e.g.,
"planar") by mapping its name to a table of callback functions (GetDamage,GetDefense). Fails if the type is already defined. - Parameters:
sptype(string): Unique identifier for the damage type.spdata(table): Table containing optionalGetDamage(entity)and/orGetDefense(entity)functions.
GetSpDamageForType(ent, sptype)
- Description: Returns the special damage value of type
sptypefor the given entity, using the registered callback. Falls back to0if no definition orGetDamageis missing. - Parameters:
ent(Entity): The entity whose damage is queried.sptype(string): The special damage type identifier.
GetSpDefenseForType(ent, sptype)
- Description: Returns the special defense value of type
sptypefor the given entity. Falls back to0if no definition orGetDefenseis missing. - Parameters:
ent(Entity): The entity whose defense is queried.sptype(string): The special damage type identifier.
CollectSpDamage(ent, tbl)
- Description: Iterates over all registered damage types, queries the entity’s damage for each, and populates a table mapping
sptype → damage_amountfor all types where damage > 0. Returns the populated table (ornilif no damage found). - Parameters:
ent(Entity): The entity to query.tbl(table?, optional): Existing table to populate; ifnil, a new table is created.
MergeSpDamage(tbl1, tbl2)
- Description: Combines two damage tables by summing values per damage type key. Returns
tbl1modified in-place if both are non-nil; otherwise returns whichever table is non-nil. - Parameters:
tbl1(table?, optional): First damage table.tbl2(table?, optional): Second damage table.
CalcTotalDamage(tbl)
- Description: Sums all values in the damage table to compute total damage.
- Parameters:
tbl(table?): Table of damage amounts per type.
ApplyMult(tbl, mult)
- Description: Multiplies all damage values in the table by
mult, setting entries tonil(removing them) if the result is zero ormultis zero. Returns the (possibly modified) table. - Parameters:
tbl(table?): Damage table to scale.mult(number): Multiplicative factor.
ApplySpDefense(ent, tbl)
- Description: Applies special defense from
entto reduce damage intbl. For each damage type intbl, subtracts the entity’s defense of that type (if > 0); damage entries are set tonilif reduced to ≤ 0. Returns the modified table. - Parameters:
ent(Entity): Entity providing defense.tbl(table?): Damage table to apply defense against.
Events & Listeners
None.