D20 rolls dice for damage. Every hit on a player or an NPC deals the total of a dice
formula, rolled with Dice.roll, instead of the damage the engine has worked out, while
the 'd20_enabled' config is on.
The formula of a hit is the first of these that applies (D20:get_damage_formula):
- The
damage_diceof the weapon item that a player attacker holds, such asITEM.damage_dice = '1d8', as long as the damage comes from that weapon and not from a projectile of its own (a grenade, a bolt, a rocket). - The dice of the class of the weapon or projectile that has dealt the damage, by
D20:get_weapon_dice: the inflictor when it is an entity of its own, otherwise the weapon the attacker holds. Players and NPCs holding the same weapon roll the same dice. - The dice of the class of an NPC attacker, by
D20:get_npc_dice, for NPCs that attack with their own body. - The damage type: 1d4 for burns, 1d12 for explosions, 1d10 for crushing blows. Fall damage and drowning are left to their own rules (see below).
- The damage of the engine itself: 1d4 up to 10, 1d6 up to 20, 1d10 up to 40, 1d12 above that, and the 'd20_default_dice' config (1d6) when the engine damage is 0.
The dice of the weapons that come with the game: fists and stunstick 1d4, crowbar, pistol
and SMG 1d6, .357, AR2 and shotgun 1d10, crossbow, RPG and grenades 1d12, and the same for
their projectiles. The dice of the NPCs that fight with their own body: zombies, fast
zombies and zombines 1d6, poison zombies 1d10, headcrabs 1d4, antlions 1d6, antlion guards
1d12, manhacks 1d4, hunters 1d10 and striders 1d12. A schema changes and adds dice with
D20:set_weapon_dice and D20:set_npc_dice, and staff override them in game with the
'd20_weapon_dice' and 'd20_npc_dice' configs, lists of entries like
'weapon_pistol = 1d8'.
-- In the schema: a heavier pistol and a tougher zombie.
D20:set_weapon_dice('weapon_pistol', '1d8')
D20:set_npc_dice('npc_zombie', '2d6')
-- On a weapon item.
ITEM.damage_dice = '1d10 + strength'
Formulas may use the variables damage (the damage of the engine), level (the level of
a player attacker, 1 for anybody else) and the stats of a player attacker by their ID,
such as strength; the AdjustDamageDice hook adds more or changes the formula before
the roll, and PlayerDamageRolled reports every result.
Damage events from the same attacker and inflictor that hit the same victim in the same
tick, such as the pellets of a shotgun or an explosion that touches several hitboxes, are
one attack: the first one rolls and the others are blocked. The rolled total is rounded,
and the hit location multipliers still apply to it: for players the 'damage_scale_'
configs of the Damage plugin, whose ResolvePlayerDamage hook is where a hit on a player
is rolled, so that the PrePlayerTakeDamage hook and every damage handler after it see
the rolled damage; for NPCs every multiplier that the ScaleNPCDamage handlers of the
schema and of the gamemode have applied to the hit.
Fall damage rolls the 'd20_fall_dice' config, {steps}d6 by default, where steps is 1
for the slowest fall that hurts and grows by 1 for every 150 units per second of fall
speed above 250, through the FLGetFallDamage hook; the 'damage_scale_fall' config of the
Damage plugin still multiplies it. Drowning rolls the 'drowning_damage' formula of the
Damage plugin (1d4).
NPCs have their maximum health multiplied by the 'd20_npc_health_scale' config (0.25) when they spawn, as rolled hits deal far less than the weapons of the engine.
Attack rolls decide whether an attack hits at all, for the systems that ask (such as
turn-based combat): D20:attack_roll rolls a d20 plus the attack modifier of the attacker
(D20:get_attack_modifier: the marksmanship or close combat skill of a player, by the
weapon they hold, or the 'd20_npc_attack_bonus' config for NPCs) against the armor class
of the target. Player:get_armor_class is 10, plus the reflexes of the player, plus 2
while they wear armor, and the AdjustArmorClass hook changes it; NPCs have the
'd20_npc_armor_class' config (12). A natural 20 always hits and a natural 1 always misses,
and the AdjustAttackRoll hook changes the formula or the armor class before the roll.
local hit, result, armor_class = D20:attack_roll(attacker, target)
if hit then
attacker:notify(Dice.format(result)..' against '..armor_class..': a hit!')
end
The level and the stats come from the Levels and Stats plugins when they are loaded; the plugin works without them, with a level of 1 and modifiers of 0.
Summary
Hooks
ScaleNPCDamage handlers to the rolled total.D20:roll_fall_damage.PrePlayerTakeDamage hook of the Damage plugin see it, and applies the 'damage_scale_' config of the hit location to the rolled total again, as the Damage plugin has applied it to the damage of the engine in ScalePlayerDamage already.Methods
D20:get_attack_modifier) and any bonus of the info table, after the AdjustAttackRoll hook, against the armor class of the target.damage_dice of the weapon item the hit was dealt with, the dice of the class of the weapon or projectile (D20:get_weapon_dice), the dice of the class of an NPC attacker (D20:get_npc_dice), the dice of the damage type (1d4 for burns, 1d12 for explosions, 1d10 for crushing blows), and last the dice of the damage of the engine: 1d4 up to 10, 1d6 up to 20, 1d10 up to 40, 1d12 above, and the 'd20_default_dice' config when it is 0.D20:set_npc_dice gives it.damage, the damage of the engine; level, the level of a player attacker when the Levels plugin is loaded, 1 otherwise; and every stat by its ID, such as strength: the modifier of a player attacker when the Stats plugin is loaded, 0 for anybody else, so that a formula with a stat in it can be rolled for an NPC holding the weapon as well.D20:set_weapon_dice gives it.ScaleNPCDamage handlers of the schema and of the gamemode multiply it, so that their multiplier can be applied to the rolled damage.D20:get_damage_formula), after the AdjustDamageDice hook, rounded and never below 0, times the hit location multiplier.{steps}d6 by default, where steps is the fall speed above 250 in steps of 150, at least 1.damage_dice of its own.ScaleNPCDamage handlers have applied to the damage an NPC is taking, and forgets the damage that D20:remember_npc_damage has kept for it.Hooks
Rolls the damage of a hit on an NPC and applies the hit location multiplier of the
ScaleNPCDamage handlers to the rolled total. Hits on players are rolled through the
ResolvePlayerDamage hook of the Damage plugin instead.
Parameters
- entity Entity the entity about to take the damage
- damage_info CTakeDamageInfo the damage, changed in place
Returns
- Boolean true to block an extra damage event of an attack that has already been rolled, nil otherwise
Implements hook FLGetFallDamage
Rolls the fall damage of a player, see D20:roll_fall_damage.
Parameters
- actor Player the player who has hit the ground
- speed Number fall speed
Returns
- Number damage to deal, nil while the plugin is disabled
Implements hook OnConfigSet
Checks the dice formulas of the configs of the plugin when they are set.
Parameters
- key String config key
- _old_value Any
- new_value Any
Scales the health of an NPC that has spawned, on the next tick, once the NPC has its health set.
Parameters
- entity Entity the entity that was created
Implements hook ResolvePlayerDamage
Rolls the damage of a hit on a player, before the fall damage multiplier and the
PrePlayerTakeDamage hook of the Damage plugin see it, and applies the 'damage_scale_'
config of the hit location to the rolled total again, as the Damage plugin has applied it
to the damage of the engine in ScalePlayerDamage already.
Parameters
- victim Player the player about to take the damage
- damage_info CTakeDamageInfo the damage, changed in place
- hitgroup Number HITGROUP_ enum of the hit location
Returns
- Boolean true to block an extra damage event of an attack that has already been rolled, nil otherwise
Remembers the damage of a bullet or melee hit on an NPC before the schema and the gamemode multiply it by hit location. The damage is left as it is.
Parameters
- npc NPC the NPC that was hit
- _hitgroup Number HITGROUP_ enum of the body part that was hit
- damage_info CTakeDamageInfo
Methods
Rolls whether an attack hits: a d20 plus the attack modifier of the attacker (see
D20:get_attack_modifier) and any bonus of the info table, after the AdjustAttackRoll
hook, against the armor class of the target. The attack hits when the total reaches the
armor class; a natural 20 always hits and a natural 1 always misses. A formula that cannot
be rolled falls back to a plain d20 plus the modifier. Serverside only.
local hit, result, armor_class = D20:attack_roll(attacker, target, { bonus = 2 })
attacker:notify(Dice.format(result)..(hit and ' hits' or ' misses')..' AC '..armor_class)
Parameters
- attacker Entity player or NPC making the attack
- target Entity player, NPC or other entity being attacked
- info Map optional, defaults to
{}optional: bonus (Number added to the roll), formula (String to roll instead of the d20 plus the modifier), armor_class (Number to roll against instead of the armor class of the target), variables (Map or function of the formula variables); filled in and passed to the hook
Returns
- Boolean whether the attack hits
- Map the Dice result of the roll
- Number the armor class it was rolled against
Checks the dice formulas of the configs of the plugin, and prints those that cannot be rolled in development mode. Serverside only.
Parameters
- key String config key
- value Any value of the config
Builds the formula of an attack roll: a d20 plus a modifier.
Parameters
- modifier Number
Returns
- String formula such as '1d20 + 5', '1d20 - 2' or '1d20'
Returns what an attacker adds to the d20 of an attack roll. A player adds the modifier of
their marksmanship skill for a ranged attack and of their close combat skill for a melee
attack (see D20:is_ranged_attack), when the Stats plugin is loaded; an NPC adds the
'd20_npc_attack_bonus' config; anything else adds nothing.
Parameters
- attacker Entity
Returns
- Number attack modifier
D20:get_attack_weapon_item(attacker, victim, weapon_class)
server plugins/d20/plugin/sv_plugin.lua:211Returns the weapon item that a hit was dealt with: the item of the weapon a player attacker holds, as long as the damage comes from that weapon and not from a projectile. Needs the Inventory plugin. Serverside only.
Parameters
- attacker Entity
- victim Entity
- weapon_class String class of what has dealt the damage, see
Damage:get_weapon_class
Returns
- Item the weapon item, nil if there is none
Works out the dice formula that a hit rolls, in this order: the damage_dice of the
weapon item the hit was dealt with, the dice of the class of the weapon or projectile
(D20:get_weapon_dice), the dice of the class of an NPC attacker (D20:get_npc_dice),
the dice of the damage type (1d4 for burns, 1d12 for explosions, 1d10 for crushing blows),
and last the dice of the damage of the engine: 1d4 up to 10, 1d6 up to 20, 1d10 up to 40,
1d12 above, and the 'd20_default_dice' config when it is 0. Fall damage and drowning have
no formula here: falls roll in D20:roll_fall_damage and drowning in the Damage plugin.
Serverside only.
D20:get_damage_formula(attacker, victim, damage_info)
-- '1d6', 'weapon:weapon_pistol'
Parameters
- attacker Entity entity responsible for the damage
- victim Entity entity taking the damage
- damage_info CTakeDamageInfo the damage
Returns
- String dice formula
- String where it comes from: 'item:<item id>', 'weapon:<class>', 'npc:<class>', 'type:<burn, blast or crush>', 'damage' or 'default'; nothing for fall damage and drowning
Returns the dice formula of the attacks of an NPC class: the entry of the 'd20_npc_dice'
config for it if there is one, otherwise what the plugin or D20:set_npc_dice gives it.
Serverside only.
Parameters
- class String NPC class
Returns
- String dice formula, nil if the class has none
Returns the variables that a damage formula can use: damage, the damage of the engine;
level, the level of a player attacker when the Levels plugin is loaded, 1 otherwise; and
every stat by its ID, such as strength: the modifier of a player attacker when the Stats
plugin is loaded, 0 for anybody else, so that a formula with a stat in it can be rolled
for an NPC holding the weapon as well. Serverside only.
D20:get_variables(attacker, damage_info)
-- { damage = 12, level = 3, strength = 2, reflexes = 1, ... }
Parameters
- attacker Entity optional, defaults to
nilentity responsible for the damage - damage_info CTakeDamageInfo optional, defaults to
nilthe damage
Returns
- Map numbers by variable name
Returns the dice formula of a weapon or projectile class: the entry of the
'd20_weapon_dice' config for it if there is one, otherwise what the plugin or
D20:set_weapon_dice gives it. Serverside only.
Parameters
- class String entity class
Returns
- String dice formula, nil if the class has none
Checks whether dice damage is turned on, through the 'd20_enabled' config.
Returns
- Boolean
Checks whether a player attacks from a distance with the weapon they hold. The weapon
item decides when it has a weapon_type that is WEAPON_RANGED or WEAPON_MELEE (enums
that a schema may define); otherwise a weapon that uses primary ammo is ranged and any
other weapon, or no weapon at all, is melee.
Parameters
- attacker Player
Returns
- Boolean true for a ranged attack, false for a melee attack
Remembers the damage of a bullet or melee hit on an NPC before the ScaleNPCDamage
handlers of the schema and of the gamemode multiply it, so that their multiplier can be
applied to the rolled damage. Damage that several pellets deal in the same tick adds up.
Serverside only.
Parameters
- npc NPC
- damage_info CTakeDamageInfo the damage before the multipliers
Replaces the damage of a hit on a player or an NPC with the total of its dice formula
(D20:get_damage_formula), after the AdjustDamageDice hook, rounded and never below 0,
times the hit location multiplier. Runs the PlayerDamageRolled hook with the result.
Damage events from the same attacker and inflictor that hit the same victim in the same
tick count as one attack: the first one rolls and the others are to be blocked. Does
nothing while the plugin is disabled, for fall damage and for drowning. Serverside only.
Parameters
- victim Entity player or NPC taking the damage
- damage_info CTakeDamageInfo the damage, changed in place
- scale Number optional, defaults to
1hit location multiplier to apply to the rolled total
Returns
- Boolean true when the damage is part of an attack that has already been rolled and has to be blocked, nil otherwise
Rolls the fall damage of a player: the 'd20_fall_dice' config, {steps}d6 by default,
where steps is the fall speed above 250 in steps of 150, at least 1. The
'damage_scale_fall' config of the Damage plugin multiplies the total afterwards. A formula
that cannot be rolled falls back to steps d6. Serverside only.
D20:roll_fall_damage(actor, 700) -- 3d6
Parameters
- _actor Player the player who has hit the ground
- speed Number fall speed
Returns
- Number damage, nil while the plugin is disabled
Multiplies the maximum health of an NPC by the 'd20_npc_health_scale' config and sets its health to the new maximum, at least 1. Every NPC is scaled once; NPCs without a maximum health are left alone. Does nothing while the plugin is disabled. Serverside only.
Parameters
- npc NPC
Returns
- Boolean whether the health of the NPC was scaled
Sets the dice formula of the attacks of an NPC class, which its hits roll when they do not come from a weapon that has dice. An entry of the 'd20_npc_dice' config for the class still wins. Serverside only.
D20:set_npc_dice('npc_zombie', '2d6')
Parameters
- class String NPC class, matched in lower case
- formula String dice formula, or nil to remove the dice of the class
Returns
- Boolean whether the dice were set
- String the error if the formula cannot be rolled
Sets the dice formula of a weapon or projectile class, which a hit dealt with it rolls
unless the weapon item of the attacker has damage_dice of its own. An entry of the
'd20_weapon_dice' config for the class still wins. Serverside only.
D20:set_weapon_dice('weapon_pistol', '1d8')
D20:set_weapon_dice('weapon_crowbar', nil) -- falls back to the damage type and bands
Parameters
- class String entity class, matched in lower case
- formula String dice formula, or nil to remove the dice of the class
Returns
- Boolean whether the dice were set
- String the error if the formula cannot be rolled
Returns the hit location multiplier that the ScaleNPCDamage handlers have applied to
the damage an NPC is taking, and forgets the damage that D20:remember_npc_damage has
kept for it. Serverside only.
Parameters
- npc NPC
- damage_info CTakeDamageInfo the damage after the multipliers
Returns
- Number multiplier; 1 for damage that was not a bullet or melee hit