Add MIT License
[minetest_playereffects.git] / README.md
blob9a8a2e84e21ea29bd3840916508b60c7ffd0ed81
1 # Player Effects
2 ## Summary
3 This is an framework for assigning temporary status effects to players. This mod is aimed to modders and maybe interested people. This framework is a work in progress and not finished.
5 ## Profile
6 * Name: Player Effects
7 * Short name: `playereffects`
8 * Current version: 1.1.1 (This is a [SemVer](http://semver.org/).)
9 * Dependencies: None!
10 * License of everything: MIT License
11 * Discussion page: [here](https://forum.minetest.net/viewtopic.php?f=11&t=9689)
13 ## Information for players
14 This mod alone is not aimed directly at players. Briefly, the point of this mod is to help other mods to implement temporary status effects for players in a clean and consistant way.
15 Here is the information which may be relevant to you: Your current status effects are shown on the HUD on the right side, along with a timer which shows the time until the effect gets disabled. It is possible for the server to disable this feature entirely. Some status effects may also be hidden and are never exposed to the HUD.
17 You only have to install this mod iff you have a mod which implements Player Effects. Here is a list of known mods which do:
19 * Magic Beans—Wuzzy’s Fork [`magicbeans_w`]
21 ## Information for server operators
22 By default, this mod stores the effects into the file `playereffects.mt` in the current world path every 10 seconds. On a regular server shutdown, this file is also written to. The data in this file is read when the mod is started.
24 It is save to delete `playereffects.mt` when the mod does currently not run. This simply erases all active and inactive effects when the server starts again.
26 You can disable the automatic saving in `settings.lua`.
28 ### Configuration
29 Player Effects can be configured. Just edit the file `settings.lua`. You find everything you need to know in that file. Be careful to not delete the lines, just edit them.
31 ## Information for modders
32 This is a framework for other mods to depend on. It provides means to define, apply and remove temporary status effects to players in a (hopefully) unobstrusive way.
33 A status effect is an effect for a player which changes some property of the player. This property can be practically everything. Currently, the framework supports one kind of effect, which I call “exclusive effects”. For each property (speed, gravity, whatver), there can be only one effect in place at the same time. Here are some examples for possible status effects:
35 * high walking speed (`speed` property)
36 * high jump height (`jump` property)
37 * low player gravity (`gravity` property)
38 * high player gravity (`gravity` property)
39 * having the X privilege granted (binary “do I have the property?” property) (yes, this is weird, but it works!)
41 The framework aims to provide means to define effect types and to apply and cancel effects to players. The framework aims to be a stable foundation stone. This means it needs a lot of testing.
43 ## Known bugs
44 ### Effect timers don’t stop when game gets paused
45 When you paused the game in singleplayer mode, the effect timers just continue as if nothing happened. Of course, all effect timers should be stopped while the game is paused, like everything else. Apparently this bug cannot be fixed with the current Lua API.
47 ## API documentation
48 ### Data types
49 #### Effect type (`effect_type`)
50 An effect type is a description of what is later to be concretely applied as an effect to a player. An effect type, however, is *not* assigned to a player. There are two kinds of effect types: Repeating and non-repeating. See the section on `effect` for more information.
52 `effect_type` is a table with these fields:
54 * `description`: Human-readable short description of the effect. Will be exposed to the HUD, iff `hidden` is `false`.
55 * `groups`: A table of groups to which this effect type belongs to.
56 * `apply`: Function to be called when effect is applied. See `playereffects.register_effect_type`.
57 * `cancel`: Function to be called when effect is cancelled. See `playereffects.register_effect_type`.
58 * `icon`: This is optional. It can be the file name of a texture. Should have a size of 16px×16px. Will be exposed to the HUD, iff `hidden` is `false`.
59 * `hidden`: Iff this is false, it will not be exposed to the HUD when this effect is active.
60 * `cancel_on_death`: Iff this is true, the effect will be cancelled automatically when the player dies.
61 * `repeat_interval` is an optional number. When specified, the effects of this type becomes a repeating effect. Repeating effects call `apply` an arbitrary number of times; non-repeating effects just call it once when the effect is created. The number specifies the interval in seconds between each call. Iff this parameter is `nil`, the effect type is a non-repeating effect.
63 Normally you don’t need to read or edit fields of this table. Use `playereffects.register_effect_type` to add a new effect type to Player Effects.
65 #### Effect group
66 An effect group is basically a concept. Any effect type can be member of any number of effect groups. The main point of effect groups is to find effects which affect the same property. For example, an effect which makes you faster and another effect which makes you slower both affect the same property: speed. The group for that then would be the string `"speed"`. See also `examples.lua`, which includes the effects `high_speed` and `low_speed`.
68 Currently, the main rule of Player Effects requires that there can only be one effect in place. Don’t worry, Player Effects already does that job for you. Back to the example: it is possible to be fast and it is possible to be slow. But it is not possible to be fast `and` slow at the same time. Player Effects ensures that by cancelling all conflicting concepts before applying a new one.
70 The concept of groups may be changed or extended in the future.
72 You can invent effect groups (like the groups in Minetest) on the fly. A group is just a string. Practically, you should use groups which other people use.
74 #### Effect (`effect`)
75 An effect is an current change of a player property (like speed, jump height, and so on). It is the realization of an effect type. All effects are temporary. There are currently two types of effects: Repeating and non-repeating. Non-repeating effects call their `apply` callback once when they are created. Repeating effects call their apply callback multiple times with a specified interval. By default, effects are non-repeating.
77 `effect` is a table with the following modding-relevant fields:
79 * `playername`: The name of the player to which the effect belongs to.
80 * `effect_id`: A globally unique identifier of the effect. It is a number and assigned automatically by Player Effects.
81 * `effect_type_id`: The identifier of the effect’s effect type. It is a string and assigned by `playereffects.register_effect_type`.
82 * `metadata`: An optional field which may contain a table with additional, modder-defined data to be “remembered” for later. The `apply` callback can set this field.
84 Internally, Player Effects also uses these fields:
86 * `start_time`: The operating system time (from `os.time()`) of when the effect has been started.
87 * `time_left`: The number of seconds left before the effect runs out. This number is only set when the effect starts or the effect is unfrozen because i.e. a player re-joins. You can’t use this field to blindly get the remaining time of the effect.
88 * `repeat_interval_start_time` and `repeat_interval_time_left`: Same as `start_time` and `time_left`, but for repeating effects.
90 You should normally not need to care about these internally used fields.
93 ### Functions
94 #### `playereffects.register_effect_type(effect_type_id, description, icon, groups, apply, cancel, hidden, cancel_on_death, repeat_interval)`
95 Adds a new effect type for the Player Effects mods, so it can be later be applied to players.
97 ##### Parameters
98 * `effect_type_id` is the identifier (a string) which is internally used by the mod. Later known as `effect_type_id`. You may choose the identifier at will, but please use only alphanumeric ASCII characters. The identifier must be unique along all mods.
99 * `description` is the text which is exposed to the HUD and visible to the player.
100 * `icon`: This is optional an can be `nil`. It can be the file name of a texture. Should have a size of 16px×16px. In this case, this is the icon for the HUD. Basically this is just eye-candy. If this is `nil`, no icon is shown. The icon will be exposed to the HUD, iff `hidden` is `false`.
101 * `groups` is a table of strings to which the effect type is assigned to.
102 * `apply`: See below.
103 * `cancel`: See below.
104 * `hidden` is an optional boolean value. Iff `true`, the effect description and icon will not be exposed to the player HUD. Otherwise, the effect is exposed. Default: `false`
105 * `cancel_on_death` is an optional boolean value. Iff true, the effect will be cancelled automatically when the player dies. Default: `true`.
106 * `repeat_interval` is an optional number. When specified, the effects of this type becomes a repeating effect. Repeating effects call `apply` an arbitrary number of times; non-repeating effects just call it once when the effect is created. The number specifies the interval in seconds between each call. Iff this parameter is `nil`, the effect type is a non-repeating effect.
109 ###### `apply` function
110 The `apply` function is a callback function which is called by Player Effects. Here the modder can put all the gameplay-relevant code.
112 `apply` takes a player object as its only argument. This is the player to which the effect is applied to.
114 The function may return a table. This table will be added as the field `metadata` to the resulting `effect`.
116 The function may return `false`. This is used to tell Player Effects that the effect could, for whatever reason, not be successfully applied. Currently, this feature is experimental and possibly broken.
118 The function may also return just `nil` on a normal success without metadata.
120 ###### `cancel` function
121 The `cancel` function is called by Player Effects when the effect is to be cancelled. Here the modder can do all the code which is needed to revert the changes an earlier `apply` call made.
123 `cancel` takes an `effect` as its first argument and a player object as its second argument. Remember, this `effect` may also contain a field called `metadata`, which may have been added by an earlier `apply` call. `player` is the player to which the effect is/was applied. This argument is just there for convenience reasons.
125 Player Effects does not care about the return value of this function.
127 ##### Return value
128 Always `nil`.
130 #### `playereffects.apply_effect_type(effect_type_id, duration, player, repeat_interval_time_left)`
131 Attempts to apply a new effect of a certain type for a certain duration to a certain player. This function can fail, although this should rarely happen. This function handles non-repeating effects and repeating effects as well.
133 ##### Parameters
134 * `effect_type_id`: The identifier of the effect type. This is the name which was used in `playereffects.register_effect_type` and always a string.
135 * `duration`: How long the effect. Please use only positive values and only integers. If a repeating effect type is specified, this number specifies the number of repetitions; for non-repeating effects this number specifies the effect duration in seconds.
136 * `player`: The player object to which the new effect should be applied to.
137 * `repeat_interval_time_left`: This parameter is optional and only for repeating effects. If it is a number, it specifies the time until the first call of the `apply` callback fires. By default, a full repeat interval is waited until the first call.
139 ##### Return value
140 The function either returns `false` or a number. Iff the function returns `false`, the effect was not successfully applied. The function may return `false` on these occasions:
142 * `player` is not a valid player object
143 * The `apply` function of the effect type returned `false`
145 On success, the function returns a number. This number is the effect ID of the effect which has been just created. This effect ID can be used later, for `playereffects.cancel_effect`, for example.
147 #### `playereffects.cancel_effect(effect_id)`
148 Cancels a single effect.
150 ##### Parameter
151 * `effect_id`: The effect ID of the effect which shall be cancelled.
153 ##### Return value
154 Always `nil`.
156 #### `playereffects.cancel_effect_group(groupname, playername)`
157 Cancels all a player’s effects which belong to a certain group.
159 ##### Parameters
160 * `groupname`: The name of the effect group (string) of which all active effects of the player shall be cancelled.
161 * `playername`: The name of the player to which the effects which are about to be cancelled belong to.
163 ##### Return value
164 Always `nil`.
166 #### `playereffects.cancel_effect_type(effect_type_id, cancel_all, playername)`
167 Cancels one or all player effect with a certain effect type
168 Careful! This function has *not* been tested yet!
170 ##### Parameters
171 * `effect_type_id`: Identifier of the effect type.
172 * `cancel_all`: Iff true, cancels all active effects with this effect type
173 * `playername`: Name of the player to which the effects belong to
175 ##### Return value
176 Always `nil`.
178 #### `playereffects.get_remaining_effect_time(effect_id)`
179 Returns the remaining time of an effect.
181 ##### Parameter
182 * `effect_id`: The effect identifier of the effect in question
184 ##### Return value
185 Iff the effect exists, the remaining effect time is returned in full seconds. Iff the effect does not exist, `nil` is returned.
187 #### `playereffects.get_player_effects(playername)`
188 Returns all active effects of a player.
190 ##### Parameter
191 `playername`: The name of the player from which the effects are requested.
193 ##### Return value
194 A table of all `effect`s which belong to the player. If the player does not exist, this function returns an empty table.
196 #### `playereffects.has_effect_type(playername, effect_type_id)`
197 Returns `true` iff the provided player has an effect of the specified effect type, `false` otherwise.
199 ##### Parameters
200 * `playername`: Name of the player to check the existance of the effect type for
201 * `effect_type_id`: Identifier of the effect type.
203 ## Examples
204 This mod comes with extensive examples. The examples are disabled by default. Edit `settings.lua` to enable the examples. See `examples.lua` to find out how they are programmed. The examples are only for demonstration purposes. They are not intended to be used in an actual game.
206 ### Chat commands
207 The examples are mainly accessible with chat commands. Since this is just an example, everyone can use these examples.
209 #### Apply effect
210 These commands apply (or try to) apply an effect to you. You will get a response in the chat which give you the `effect_id` on success. On failure, the example will tell you that it failed.
212 * `fast`: Makes you faster for 10 seconds.
213 * `slow`: Makes you slower for 120s.
214 * `hfast`: Makes you faster for 10s. This is a hidden effect and is not exposed to the HUD.
215 * `highjump`: Increases your jump height for 20s.
216 * `fly`: Gives you the `fly` privilege for a minute. You keep the privilege even when you die. Better don’t mess around with this privilege manually when you use this.
217 * `regen`: Gives you a half heart per second 10 times (5 hearts overall healing). This is an example of a repeating effect.
218 * `slowregen`: Gives you a half heart every 15 seconds, 10 times (5 hearts overall healing). This is an example of a repeating effect.
219 * `blind`: Tints the whole screen black for 5 seconds. This is highly experimental and will be drawn over many existing HUD elements. In other words, prepare your HUD to be messed up.
220 * `null`: Tries to apply an effect which always fails. This demonstrates the failure of effects.
222 #### Cancel effects
223 * `cancelall`: Cancels all your active effects.
225 #### Testing
226 * `stresstest [number]`: Applies `number` dummy effects which don’t do anything to you. Iff omitted, `number` is assumed to be 100. This command is there to test the performance of this mod for absurdly large effect numbers.