Faction class
Part of Factions System
A faction that characters can belong to. It holds the name, description, color and image of the faction, its models by gender, its ranks from the lowest to the highest, and the template that character names are generated from. It can also limit how many of its members may be online at once and how many characters one player may have in it, give its members weapons, maximum health and armor when they spawn, and set how the NPCs of the map feel about them. A rank can carry a model, weapons, maximum health and armor of its own, a limit, the right to promote and demote other members, and the mark of the rank that new members start with.
Factions are normally defined in the files of a factions folder, where the object is
available as FACTION (see Factions.include_factions), and are registered with
Faction:register. A definition can override Faction:on_player_join,
Faction:on_player_leave, Faction:on_character_create and Faction:can_transfer, and
define make_name to build names itself. Unlike the other files of the models folder, this
is a plain class that is not stored in the database.
Summary
Methods
Factions.can_transfer checks whether a player may be moved into this faction, as the SetFaction command does.Faction:set_data.Methods
Adds a rank above the ranks added so far. Besides its ID and the text it puts into names, a rank can carry optional fields:
model: model that holders of the rank use instead of the model of their character, either a path or a table of paths keyed by 'male', 'female', 'no_gender' and 'universal'.model_class: animation class of that model (seeFlux.Anim:set_model_class); for a table of models it defaults to the model class the faction has for the same key.loadout: List<String> of weapon classes given on spawn on top of those of the faction.max_health,max_armor: replace the maximum health and armor of the faction.limit: how many holders of the rank may be online at once, 0 or nil for no limit.default: true makes it the rank that new members of the faction start with.can_promote: the highest rank a holder may promote other members to, as a rank index or a rank ID; true stands for the rank right below their own.can_demote: the highest rank a holder may demote other members from, in the same form.
FACTION:add_rank('recruit', 'Rct.', { default = true })
FACTION:add_rank('officer', 'Ofc.', { loadout = { 'weapon_pistol' }, max_armor = 50 })
FACTION:add_rank('sergeant', 'Sgt.', {
model = 'models/police_sergeant.mdl',
limit = 2,
can_promote = 'officer',
can_demote = 'officer'
})
Parameters
- id String rank ID; nothing is added when it is nil
- name_filter String/Map optional, defaults to
idtext that replaces {rank} in generated names. A table is taken as the data argument, and its name field as the text - data Map optional, defaults to
niloptional fields of the rank; the table itself becomes the rank table
Returns
- Map the rank table, or nil if nothing was added
Called on the server when Factions.can_transfer checks whether a player may be moved
into this faction, as the SetFaction command does. Player:set_faction itself does not
ask. Allows everyone by default; override it in the faction definition.
function FACTION:can_transfer(target, old_faction)
if target:get_gender() == 'no_gender' then
return false, 'error.faction.humans_only'
end
end
Parameters
- _target Player the player who is to be moved into the faction
- _old_faction Faction the faction the player is in, nil if it is not registered
Returns
- Boolean return false to refuse the transfer, optionally followed by a String language phrase that tells why and a Map of its arguments
Finds a rank of the faction by its position or by its ID.
local index, rank_table = faction_table:find_rank('officer')
Parameters
- rank Number/String rank index, or rank ID, whose letter case is ignored
Returns
- Number rank index and Map rank table, or nothing if there is no such rank
Faction:generate_name(target, rank, default_data)
shared plugins/factions/plugin/models/faction.lua:370Builds a character name for a player from the faction's name template, which may contain {name}, {rank}, {data:key} and {callback:method} placeholders. A faction that defines make_name(target, char_name, rank, default_data) bypasses the template.
FACTION.name_template = '{data:unit} {rank} {name}'
FACTION:set_data('unit', 'C17')
FACTION:add_rank('officer', 'Ofc.')
-- For a player named 'John Doe':
FACTION:generate_name(target, 'officer') -- 'C17 Ofc. John Doe'
Parameters
- target Player
- rank Number/String rank index or rank ID used for {rank}
- default_data Map optional, defaults to
nilvalues for {data:key} that override the faction's own data
Returns
- String the generated name; the player's current name if a ShouldNameGenerate hook returns false
Returns the team color of the faction.
Returns
- Color
Returns a value stored with set_data.
Parameters
- key String
Returns
- String the stored value, or nil if nothing is stored under the key
Returns the rank that new members of the faction start with: the first rank that was added with the default field set, or else the lowest rank.
Returns
- Number rank index; 1 as well when the faction has no ranks
Returns the description of the faction.
Returns
- String
Returns the models of the faction for a gender, falling back to the universal models when the gender has none.
Parameters
- gender String 'male', 'female', 'universal' or 'no_gender'
Returns
- List<String> model paths
Returns the path of the faction's image.
Returns
- String material path, or nil if the faction has no image
Returns the weapons that members of the faction are given when they spawn, in addition to the default loadout: the loadout of the faction followed by that of the rank.
Parameters
- rank Number optional, defaults to
nilrank index of the member; only the loadout of the faction is returned without it
Returns
- List<String> weapon classes, each one once
Returns the faction's image as a cached material. Client only.
Returns
- Material the material, or nil if the faction has no image
Returns the maximum armor of the members of the faction: that of the rank if the rank sets one, otherwise that of the faction.
Parameters
- rank Number optional, defaults to
nilrank index of the member
Returns
- Number maximum armor, or nil if neither the rank nor the faction sets one
Returns the maximum health of the members of the faction: that of the rank if the rank sets one, otherwise that of the faction.
Parameters
- rank Number optional, defaults to
nilrank index of the member
Returns
- Number maximum health, or nil if neither the rank nor the faction sets one
Returns all models of the faction.
Returns
- Map arrays of model paths keyed by 'male', 'female' and 'universal'
Returns the name of the faction.
Returns
- String
Returns how NPCs of a class feel about the members of the faction, as set in the npc_relations field of the faction. The result is the number of the D_* value, also where the game does not define these globals.
Parameters
- npc_class String entity class of the NPC, such as 'npc_metropolice'
Returns
- Number D_HT
- D_FR
- D_LI or D_NU, or nil if the faction does not change the feelings of that class or the value it gives is not a disposition
Returns the physical description that characters of the faction get when the faction does not let players write one (has_description is false).
Returns
- String description or its language phrase
Picks a random faction model that matches the player's gender.
Parameters
- target Player
Returns
- String model path
Returns a rank of the faction by its position.
Parameters
- number Number rank index, 1 being the lowest
Returns
- Map rank table with id and name fields, or nil if there is no such rank
Returns the model that holders of a rank use instead of the model of their character. The model field of a rank is either a single path, or a table of paths keyed by 'male', 'female', 'no_gender' and 'universal', the last one being the fallback for a gender that has no path of its own.
Parameters
- rank Number rank index
- gender String 'male', 'female' or 'no_gender', as
Player:get_genderreturns it
Returns
- String model path, or nil if the rank does not change the model
Returns the ID of a rank of the faction. Errors if there is no rank at that position.
Parameters
- number Number rank index, 1 being the lowest
Returns
- String rank ID
Returns the ranks of the faction, lowest first.
Returns
- List<Map> rank tables with id and name fields, and the optional fields described at
Faction:add_rank
Creates a faction with default settings: no ranks, no models, no limits, no loadout, name,
description and gender enabled, and the '{rank} {name}' name template, which may also
contain {data:key} to insert a value set with Faction:set_data. Fields are left unset
when no ID is given.
local faction = Faction.new('citizen')
faction.name = 'Citizen'
faction.has_description = false
faction.phys_desc = 'Wearing the worn out blue uniform of a citizen.'
faction.models.male = { 'models/humans/group01/male_02.mdl' }
faction.models.female = { 'models/humans/group01/female_01.mdl' }
faction:add_rank('citizen', 'Mr.')
faction:register()
The fields that limit and equip the members are all optional:
-- At most 8 members online at once, and 2 characters of the faction per player.
FACTION.limit = 8
FACTION.max_characters = 2
-- Given on every spawn on top of the default loadout.
FACTION.loadout = { 'weapon_stunstick' }
-- Maximum health and armor of the members, who spawn with both of them full.
FACTION.max_health = 100
FACTION.max_armor = 50
-- 'like', 'hate', 'fear', 'neutral' or a D_* value for each NPC class.
FACTION.npc_relations = {
npc_metropolice = 'like',
npc_citizen = 'fear'
}
Parameters
- id String faction ID, normalized with to_id
Called on the server when a character of this faction is about to be created, after the creation data has passed the checks of the Characters and Factions plugins. The callback may change the data in place, for instance to set the rank the character starts with, and may refuse the character. Does nothing by default; override it in the faction definition.
function FACTION:on_character_create(owner, data)
if #Factions.get_players(self.faction_id) == 0 then
data.rank = self:find_rank('sergeant')
end
end
Parameters
- _owner Player the player the character is created for
- _data Map creation data: name, phys_desc, gender (a CHAR_GENDER_* value), model, skin and faction; rank may be set
Returns
- Boolean/Number return false to refuse the character, optionally followed by a String language phrase that tells the player why and a Map of its arguments; or return a CHAR_ERR_* code to refuse it with the text of that code
Called on the server when a player is moved into this faction. Does nothing by default; override it in the faction definition.
Parameters
- _target Player
Called on the server when a player is moved out of this faction. Does nothing by default; override it in the faction definition.
Parameters
- _target Player
Registers the faction under its faction ID.
See also
Stores a value on the faction for use as {data:key} in the name template. Both the key and the value are converted to strings.
Parameters
- key Any
- value Any