Shared helpers of the Stats plugin for what the stats of a character do in play. An effect of a stat changes some value by a fixed share for every level of the stat: a percentage of the value, read from one of the stats_ config keys, or a distance in meters. Levels above zero help the character, levels below zero hinder it, and a config of 0 turns the effect off. The hook handlers of the plugin ask Stats:get_player_effect_scale for the multiplier to apply, and the files of the attributes folder build the entries of their effects list with Stats:percent_effect and Stats:distance_effect, so that what the creation stage and the staff attribute viewer show is worked out the same way as what the server applies.

This file is included before the attributes folder and before sh_plugin.lua, which is why it makes the plugin available as the Stats global if no other file has done so yet.

Summary

Hooks

Adds the stats stage to character creation.
Adds the character sheet to the tab menu as its first and default page while the local player has a character.
Weakens the slowdown, the lower jumps and the aim drift that hurt limbs cause by the stats_determination_pain config for every level of the player's determination, and strengthens them for every level below zero.
Extends the hearing radius of in character messages by the stats_perception_hearing config, 1.5 meters by default, for every level of the listener's perception, and shortens it for every level below zero.
Raises the mass of the heaviest object a player can pick up by the stats_strength_lifting config for every level of their strength, and lowers it for every level below zero.
Lowers what a vendor asks of a customer by the stats_charisma_prices config for every level of the customer's charisma, and raises it for every level below zero.
Returns the error text for characters whose attribute points do not add up.
Raises the health a player regenerates at once by the stats_endurance_recovery config for every level of their endurance, and lowers it for every level below zero.
Raises what the hurt limbs of a player recover in a minute by the stats_endurance_recovery config for every level of their endurance, and lowers it for every level below zero.
Takes the networked attributes and skills of a character away from the player who drops it, and forgets when its next boost expires and the fractions of effects carried over.
Prepares the attributes and skills of the character that has become active: adds records for the attributes and skills that were registered after the character was created, removes the boosts that have expired or whose expiry time cannot be read, networks the attributes and skills to the player and gives the character the stat bonuses of its faction.
Gives or takes the faction bonuses of everyone online when the stats_faction_bonuses config changes.
Removes the boosts that have expired from the characters of the players who have one due, and networks their attributes again so that the clients stop counting them when the server does.
Replaces the stat bonuses of a player who has been moved to another faction.
Registers the stats character creation panel and the character sheet with the theme.
Rejects new characters unless each stat has a whole level between its minimum and its creation limit and the levels add up to the points a new character gets.
Makes a player throw the object they hold harder by the stats_strength_lifting config for every level of their strength, and weaker for every level below zero.
Adds an Attribute record for every registered attribute to a new character, using the level chosen during creation or the attribute's default, and a Skill record at rank 0 for every registered skill.
Lowers the fall damage a player takes by the stats_reflexes_fall config for every level of their reflexes, and raises it for every level below zero.
Registers the 'attribute' condition, which compares a player's attribute level to a value, and the 'skill' condition, which compares a player's skill rank to a value.
Makes the stamina of a running player drain slower by the stats_endurance_stamina config for every level of their endurance, and faster for every level below zero.
Makes the stamina of a player regenerate faster by the stats_endurance_stamina config for every level of their endurance, and slower for every level below zero.

Methods

Gives a player the stat bonuses of their faction and takes away the ones that their faction does not give: every bonus is a boost without expiry that carries the Stats.faction_boost_id identifier.
Returns the points a new character has to spread over its stats: the stats_creation_points config, never more than the stats can take at creation.
Builds an entry of the effects list of a stat for an effect that adds a distance per level.
Checks whether characters get the stat bonuses of their faction.
Finds the attribute or the skill that a player has named, for example as the argument of a command.
Formats a change as text with its sign in front: '+10%', '-1.5m' or '0%'.
Describes the result of a roll in short: the dice and the total as Dice.format_compact writes them ('d20 (14) + 3 = 17'), and a note when the roll is a critical success or failure.
Returns the color to show a change in: green for one that helps the character, pink for one that hinders it and white for none.
Returns the level of a character as rolls count it: the level from the Levels plugin, or 1 while that plugin is not loaded.
Returns the highest level a new character can pick for a stat: its creation_max field, or its max if it has none, never above max.
Returns the share of a value that one level of a stat changes, from the config of an effect.
Returns the multiplier that a level of a stat gives to the value an effect is about.
Returns the stat bonuses that a faction lists in its stats field.
Returns the bonus that the faction of a player currently adds to one of their stats: the value of the boost that carries the Stats.faction_boost_id identifier.
Returns the multiplier that the level of a player in a stat, boosts included, gives to the value an effect is about.
Returns the proficiency of a player in a skill: 0 while the skill is untrained, else the rank of the skill times the rank bonus, plus the level of the character.
Returns the proficiency that every rank of a trained skill adds to its rolls.
Returns the last roll of a stat or a skill that the local player made from the character sheet.
Builds the formula of a roll with a modifier: '1d20 + 3', '1d20 - 2', or '1d20' for a modifier of 0.
Builds an entry of the effects list of a stat for an effect that changes a value by a percentage per level.
Asks the server to roll a stat or a skill of the local player and starts the animation of its roll button.
Rolls an attribute or a skill of a player and shows the result to the players nearby, the way the roll command of the RolePlay Commands plugin shows a roll.

Hooks

Stats:AddCharacterCreationMenuStages(panel)

client plugins/stats/plugin/cl_hooks.lua:119

Implements hook AddCharacterCreationMenuStages

Adds the stats stage to character creation.

Parameters

  • panel Panel character creation menu

Stats:AddTabMenuItems(menu)

client plugins/stats/plugin/cl_hooks.lua:99

Implements hook AddTabMenuItems

Adds the character sheet to the tab menu as its first and default page while the local player has a character. Without one the sheet is also forgotten as the page to reopen, since the tab menu expects the page it reopens to exist.

Parameters

  • menu Panel the tab menu

Stats:AdjustLimbEffect(target, _effect, fraction)

shared plugins/stats/plugin/sh_hooks.lua:15

Implements hook AdjustLimbEffect

Weakens the slowdown, the lower jumps and the aim drift that hurt limbs cause by the stats_determination_pain config for every level of the player's determination, and strengthens them for every level below zero. Movement is predicted, which is why this handler is shared: both realms read the same networked level and config. Returning a strength keeps the handlers of the schema and of the plugins loaded later from being asked, so nothing is returned for a player whose determination changes nothing.

Parameters

  • target Player player the effect applies to
  • _effect String 'run', 'jump' or 'aim'
  • fraction Number strength from the damage of the limbs, above 0 and up to 1

Returns

  • Number strength to use instead, from 0 to 1; nil to keep it

Stats:AdjustMessageData(listener, message_data)

server plugins/stats/plugin/sv_hooks.lua:173

Implements hook AdjustMessageData

Extends the hearing radius of in character messages by the stats_perception_hearing config, 1.5 meters by default, for every level of the listener's perception, and shortens it for every level below zero. Only a message that has a hearing range is changed. The Chatbox plugin sends a message with a radius of 0 to everyone and one with a negative radius to nobody but the players that the PlayerCanHear hook lets through, which is how a radio transmission that is not overheard is sent, so those radii are left alone. For the same reason a shortened radius never falls below Stats.min_hearing_radius: at 0 the message would reach everyone.

Parameters

  • listener Player player hearing the message
  • message_data Map chat message data

Stats:AdjustPickupMassLimit(actor, _ent, info)

server plugins/stats/plugin/sv_hooks.lua:259

Implements hook AdjustPickupMassLimit

Raises the mass of the heaviest object a player can pick up by the stats_strength_lifting config for every level of their strength, and lowers it for every level below zero.

Parameters

  • actor Player player picking the object up
  • _ent Entity the object, or nil
  • info Map modified in place: limit (Number the mass limit)

Stats:AdjustVendorPrice(actor, vendor, item_obj, price_info)

server plugins/stats/plugin/sv_hooks.lua:336

Implements hook AdjustVendorPrice

Lowers what a vendor asks of a customer by the stats_charisma_prices config for every level of the customer's charisma, and raises it for every level below zero. A lowered price never falls below what the same vendor pays for the item, so that an item cannot be bought and sold back at a profit. What a vendor pays is left alone.

Parameters

  • actor Player the customer, or nil when a price is asked for without one
  • vendor Entity the vendor
  • item_obj Item template of the item the vendor sells
  • price_info Map modified in place: price (Number), selling (Boolean true if the vendor sells the item)

Stats:GetCharCreationErrorText(_success, status)

client plugins/stats/plugin/cl_hooks.lua:127

Implements hook GetCharCreationErrorText

Returns the error text for characters whose attribute points do not add up.

Parameters

  • _success Boolean whether the character was created
  • status Number CHAR_ERR_ status code

Returns

  • String error text, nil for other statuses

Stats:GetHealthRegeneration(target, amount)

server plugins/stats/plugin/sv_hooks.lua:299

Implements hook GetHealthRegeneration

Raises the health a player regenerates at once by the stats_endurance_recovery config for every level of their endurance, and lowers it for every level below zero. Health comes in whole points, so the fraction that does not fit is kept for the next time. Returning an amount keeps the handlers of the schema and of the plugins loaded later from being asked, so nothing is returned for a player whose endurance changes nothing.

Parameters

  • target Player player about to be healed
  • amount Number health the player would gain

Returns

  • Number health to give instead, nil to keep the amount

Stats:GetLimbRecovery(actor, amount)

server plugins/stats/plugin/sv_hooks.lua:317

Implements hook GetLimbRecovery

Raises what the hurt limbs of a player recover in a minute by the stats_endurance_recovery config for every level of their endurance, and lowers it for every level below zero. Limbs recover in whole points, so the fraction that does not fit is kept for the next minute. Nothing is returned for a player whose endurance changes nothing, which leaves the answer to other handlers.

Parameters

  • actor Player player whose limbs are about to recover
  • amount Number damage that is about to be taken off each hurt limb

Returns

  • Number amount to take off instead, nil to keep it

Stats:OnActiveCharacterCleared(owner, _character)

server plugins/stats/plugin/sv_hooks.lua:118

Implements hook OnActiveCharacterCleared

Takes the networked attributes and skills of a character away from the player who drops it, and forgets when its next boost expires and the fractions of effects carried over.

Parameters

  • owner Player
  • _character Character the character that is being dropped

Stats:OnActiveCharacterSet(owner, character)

server plugins/stats/plugin/sv_hooks.lua:104

Implements hook OnActiveCharacterSet

Prepares the attributes and skills of the character that has become active: adds records for the attributes and skills that were registered after the character was created, removes the boosts that have expired or whose expiry time cannot be read, networks the attributes and skills to the player and gives the character the stat bonuses of its faction.

Parameters

  • owner Player
  • character Character the character that is now active

Stats:OnConfigSet(key, old_value, new_value)

server plugins/stats/plugin/sv_hooks.lua:155

Implements hook OnConfigSet

Gives or takes the faction bonuses of everyone online when the stats_faction_bonuses config changes. The hook runs before the new value is stored, so the value is passed on.

Parameters

  • key String config key
  • old_value Any
  • new_value Any

Stats:OneSecond()

server plugins/stats/plugin/sv_hooks.lua:129

Implements hook OneSecond

Removes the boosts that have expired from the characters of the players who have one due, and networks their attributes again so that the clients stop counting them when the server does.

Stats:OnPlayerFactionChanged(target, _faction_table, _old_faction)

server plugins/stats/plugin/sv_hooks.lua:146

Implements hook OnPlayerFactionChanged

Replaces the stat bonuses of a player who has been moved to another faction.

Parameters

  • target Player
  • _faction_table Faction the faction the player is in now
  • _old_faction Faction the faction the player was in before, or nil

Stats:OnThemeLoaded(current_theme)

client plugins/stats/plugin/cl_hooks.lua:85

Implements hook OnThemeLoaded

Registers the stats character creation panel and the character sheet with the theme.

Parameters

  • current_theme Theme theme that was loaded

Stats:PlayerCreateCharacter(_actor, data)

server plugins/stats/plugin/sv_hooks.lua:196

Implements hook PlayerCreateCharacter

Rejects new characters unless each stat has a whole level between its minimum and its creation limit and the levels add up to the points a new character gets.

Parameters

  • _actor Player player creating the character
  • data Map character creation data

Returns

  • Number CHAR_ERR_ATTRIBUTE_SUM if the stat levels are not valid, nil otherwise

See also

Stats:PlayerThrowObject(actor, _ent, info)

server plugins/stats/plugin/sv_hooks.lua:270

Implements hook PlayerThrowObject

Makes a player throw the object they hold harder by the stats_strength_lifting config for every level of their strength, and weaker for every level below zero.

Parameters

  • actor Player player throwing the object
  • _ent Entity the object being thrown
  • info Map modified in place: force (Number the force of the throw)

Stats:PostCreateCharacter(_owner, char, char_data)

server plugins/stats/plugin/sv_hooks.lua:92

Implements hook PostCreateCharacter

Adds an Attribute record for every registered attribute to a new character, using the level chosen during creation or the attribute's default, and a Skill record at rank 0 for every registered skill. A chosen level is rounded down and kept between the attribute's min and max, as the creation data comes from the client.

Parameters

  • _owner Player
  • char Character the character being created
  • char_data Map character creation data; levels are read from its attributes field

Stats:PrePlayerTakeDamage(victim, damage_info, _hitgroup)

server plugins/stats/plugin/sv_hooks.lua:281

Implements hook PrePlayerTakeDamage

Lowers the fall damage a player takes by the stats_reflexes_fall config for every level of their reflexes, and raises it for every level below zero.

Parameters

  • victim Player player about to take the damage
  • damage_info CTakeDamageInfo the damage, scaled in place
  • _hitgroup Number HITGROUP_ enum of the hit location

Stats:RegisterConditions()

shared plugins/stats/plugin/sh_plugin.lua:201

Implements hook RegisterConditions

Registers the 'attribute' condition, which compares a player's attribute level to a value, and the 'skill' condition, which compares a player's skill rank to a value.

Stats:StaminaAdjustDrainScale(target)

server plugins/stats/plugin/sv_hooks.lua:233

Implements hook StaminaAdjustDrainScale

Makes the stamina of a running player drain slower by the stats_endurance_stamina config for every level of their endurance, and faster for every level below zero. The Stamina plugin takes the first multiplier a plugin returns, so nothing is returned for a player whose endurance changes nothing.

Parameters

  • target Player player who is running

Returns

  • Number multiplier of the drain rate, nil to leave it alone

Stats:StaminaAdjustRegenScale(target)

server plugins/stats/plugin/sv_hooks.lua:245

Implements hook StaminaAdjustRegenScale

Makes the stamina of a player regenerate faster by the stats_endurance_stamina config for every level of their endurance, and slower for every level below zero.

Parameters

  • target Player player who is recovering

Returns

  • Number multiplier of the regeneration rate, nil to leave it alone

Methods

Stats:apply_faction_bonuses(target, enabled)

server plugins/stats/plugin/sv_hooks.lua:51

Gives a player the stat bonuses of their faction and takes away the ones that their faction does not give: every bonus is a boost without expiry that carries the Stats.faction_boost_id identifier. Boosts that are right already are left alone, so loading a character does not rewrite them. Without the Factions plugin, or with the bonuses turned off, all of them are removed.

Parameters

  • target Player
  • enabled Boolean optional, defaults to nil whether bonuses are given; the stats_faction_bonuses config when nil

Returns

  • Number amount of stats whose bonus was added, replaced or removed

Stats:default_attribute_points()

shared plugins/stats/plugin/sh_plugin.lua:137

Returns the points a new character has to spread over its stats: the stats_creation_points config, never more than the stats can take at creation. The levels a player picks have to add up to this number.

Returns

  • Number attribute points

See also

Stats:distance_effect(text, key)

shared plugins/stats/plugin/lib/sh_stat_effects.lua:174

Builds an entry of the effects list of a stat for an effect that adds a distance per level. The entry has the same fields as the one of Stats:percent_effect; its get_change returns meters.

Parameters

  • text String phrase that names the distance
  • key String config key of the effect, meters per level

Returns

  • Map entry for the effects list

See also

Stats:faction_bonuses_enabled()

shared plugins/stats/plugin/sh_plugin.lua:151

Checks whether characters get the stat bonuses of their faction.

Returns

  • Boolean the stats_faction_bonuses config; true while it has no value

Stats:find_rollable(name)

shared plugins/stats/plugin/lib/sh_rolls.lua:140

Finds the attribute or the skill that a player has named, for example as the argument of a command. The name may be the ID of an attribute or a skill, or its title or its short name (the phrase attribute.<id>.short or skill.<id>.short) in any language, in any case and with spaces or underscores. An attribute ID wins over a skill ID, and an ID wins over a title or a short name.

Stats:find_rollable('Close Combat')
-- { id = 'close_combat', kind = 'skill', name = 'skill.close_combat.title' }
Stats:find_rollable('str')
-- { id = 'strength', kind = 'attribute', name = 'attribute.strength.title' }

Parameters

  • name String

Returns

  • Map id (String ID of the attribute or the skill), kind ('attribute' or 'skill') and name (String phrase of its title); nil if no attribute or skill goes by that name

Stats:format_change(amount, suffix)

shared plugins/stats/plugin/lib/sh_stat_effects.lua:82

Formats a change as text with its sign in front: '+10%', '-1.5m' or '0%'.

Parameters

  • amount Number the change
  • suffix String optional, defaults to '' text to put right after the number, such as '%'

Returns

  • String

Stats:format_roll(result)

shared plugins/stats/plugin/lib/sh_rolls.lua:92

Describes the result of a roll in short: the dice and the total as Dice.format_compact writes them ('d20 (14) + 3 = 17'), and a note when the roll is a critical success or failure.

Parameters

  • result Map result of Dice.roll, or the part of it that has the formula, total, rolls and critical fields

Returns

  • String

Stats:get_change_color(change, inverse)

shared plugins/stats/plugin/lib/sh_stat_effects.lua:99

Returns the color to show a change in: green for one that helps the character, pink for one that hinders it and white for none.

Parameters

  • change Number the change
  • inverse Boolean optional, defaults to false a lower value is the better one

Returns

  • Color

Stats:get_character_level(target)

shared plugins/stats/plugin/lib/sh_rolls.lua:45

Returns the level of a character as rolls count it: the level from the Levels plugin, or 1 while that plugin is not loaded.

Parameters

  • target Player

Returns

  • Number

Stats:get_creation_max(attribute_table)

shared plugins/stats/plugin/sh_plugin.lua:122

Returns the highest level a new character can pick for a stat: its creation_max field, or its max if it has none, never above max.

Parameters

  • attribute_table AttributeBase definition of the stat

Returns

  • Number

Stats:get_effect_rate(key)

shared plugins/stats/plugin/lib/sh_stat_effects.lua:31

Returns the share of a value that one level of a stat changes, from the config of an effect.

Parameters

  • key String config key of the effect, a percentage per level

Returns

  • Number share per level, 0.1 for a config of 10; 0 while the config has no value

Stats:get_effect_scale(level, key, inverse)

shared plugins/stats/plugin/lib/sh_stat_effects.lua:54

Returns the multiplier that a level of a stat gives to the value an effect is about. The multiplier never falls below Stats.min_effect_scale, whatever boosts add to the level.

-- With the 'stats_strength_lifting' config at 10:
Stats:get_effect_scale(2, 'stats_strength_lifting')       -- 1.2
Stats:get_effect_scale(-3, 'stats_strength_lifting')      -- 0.7
Stats:get_effect_scale(2, 'stats_strength_lifting', true) -- 0.8

Parameters

  • level Number level of the stat, boosts included
  • key String config key of the effect, a percentage per level
  • inverse Boolean optional, defaults to false the stat lowers the value instead of raising it

Returns

  • Number multiplier, 1 when the level or the config is 0

Stats:get_faction_bonuses(faction_table)

shared plugins/stats/plugin/sh_plugin.lua:166

Returns the stat bonuses that a faction lists in its stats field. Entries that do not name a registered stat or do not hold a number other than zero are left out.

-- factions/sh_cca.lua
FACTION.stats = {
  ['strength'] = 1,
  ['endurance'] = 1
}

Parameters

  • faction_table Faction faction to read the bonuses of; anything else has none

Returns

  • Map bonus levels keyed by stat ID

Stats:get_faction_boost(target, attribute_id)

shared plugins/stats/plugin/sh_plugin.lua:191

Returns the bonus that the faction of a player currently adds to one of their stats: the value of the boost that carries the Stats.faction_boost_id identifier.

Parameters

  • target Player
  • attribute_id String ID of the stat

Returns

  • Number bonus levels, or nil if the stat has no faction bonus

Stats:get_player_effect_scale(target, attribute_id, key, inverse)

shared plugins/stats/plugin/lib/sh_stat_effects.lua:72

Returns the multiplier that the level of a player in a stat, boosts included, gives to the value an effect is about.

Parameters

  • target Player
  • attribute_id String ID of the stat
  • key String config key of the effect, a percentage per level
  • inverse Boolean optional, defaults to false the stat lowers the value instead of raising it

Returns

  • Number multiplier, 1 when the stat has no effect on the player

See also

Stats:get_proficiency(target, skill_id)

shared plugins/stats/plugin/lib/sh_rolls.lua:60

Returns the proficiency of a player in a skill: 0 while the skill is untrained, else the rank of the skill times the rank bonus, plus the level of the character.

Parameters

  • target Player
  • skill_id String ID of the skill

Returns

  • Number

See also

Stats:get_rank_bonus()

shared plugins/stats/plugin/lib/sh_rolls.lua:31

Returns the proficiency that every rank of a trained skill adds to its rolls.

Returns

  • Number the stats_rank_bonus config, 2 while it has no value

Stats:get_roll(id)

client plugins/stats/plugin/cl_hooks.lua:44

Returns the last roll of a stat or a skill that the local player made from the character sheet.

Parameters

  • id String ID of the stat or the skill

Returns

  • Map started (Number RealTime of the click), pending (Boolean while the server has not answered), result (Map formula, total, natural, critical and rolls, once it has), landed (Number RealTime at which the button stops tumbling and shows the result) and text (String the result in words); nil if there is none

Stats:get_roll_formula(modifier)

shared plugins/stats/plugin/lib/sh_rolls.lua:74

Builds the formula of a roll with a modifier: '1d20 + 3', '1d20 - 2', or '1d20' for a modifier of 0.

Parameters

  • modifier Number

Returns

  • String

Stats:percent_effect(text, key, inverse, is_available)

shared plugins/stats/plugin/lib/sh_stat_effects.lua:129

Builds an entry of the effects list of a stat for an effect that changes a value by a percentage per level. The entry has what the creation stage and the staff attribute viewer read (text, get_value and get_color, the last two taking a level), plus is_active, which tells whether the effect does anything at the moment, and get_change, which returns the change at a level in percent. An effect that is not active shows as '0%'.

ATTRIBUTE.effects = {
  Stats:percent_effect('ui.effect.stamina_drain', 'stats_endurance_stamina', true, function()
    return Stamina != nil
  end)
}

Parameters

  • text String phrase that names the value
  • key String config key of the effect, a percentage per level
  • inverse Boolean optional, defaults to false the stat lowers the value instead of raising it
  • is_available Function optional, defaults to nil called without arguments; a false or nil result means that what the effect relies on, such as another plugin, is missing

Returns

  • Map entry for the effects list

Stats:request_roll(id)

client plugins/stats/plugin/cl_hooks.lua:22

Asks the server to roll a stat or a skill of the local player and starts the animation of its roll button. Does nothing sooner than Stats.roll_interval after the last roll, which the server would refuse.

Parameters

  • id String ID of the stat or the skill

Returns

  • Boolean whether the roll was asked for

Stats:roll_and_announce(actor, id)

server plugins/stats/plugin/sv_hooks.lua:368

Rolls an attribute or a skill of a player and shows the result to the players nearby, the way the roll command of the RolePlay Commands plugin shows a roll. Without that plugin the result is shown to the player alone, as a notification. A roll that fails, such as one whose formula a handler of the AdjustAttributeRoll hook has broken, is reported to the player.

Parameters

  • actor Player player who rolls
  • id String ID of an attribute or a skill; an attribute wins over a skill with the same ID

Returns

  • Map the Dice result, or nil and String the reason if nothing was rolled

See also

Defined in