Rewrite Formula page for new Formula system.

Andreas Troelsen 2021-04-07 20:43:57 +02:00
parent 991153575b
commit 519e5eb90b

@ -1,47 +1,142 @@
This page holds an overview of all the formulas used in the MobArena waves system. Customizing the different properties of the waves should be somewhat easier if they can be calculated, so here they all are!
**On this page:**
* [Introduction](#introduction)
* [Macros](#macros)
* [Reference](#reference)
* [Constants](#constants)
* [Variables](#variables)
* [Operators](#operators)
* [Functions](#functions)
* [Extensions](#extensions)
About notation: Each variable used in the formulas will have its own name. A variable that starts with a `#` denotes "number (of)", so `#players` means "number of players", and `#wave` means "wave number". The function `min(a,b)` returns the lowest of the values `a` and `b`, and `max(a,b)` returns the highest.
### Wave growth ###
The wave growth node `growth`, used in default waves, denotes how fast monster amounts grow over time. The base is calculated by half of the number of players, but at most 13 (i.e. there is no difference between 25 and 50 players). The amounts can be altered further using the `amount-multiplier` (see the [[wave setup page|Setting up the waves]]).
## Introduction
MobArena supports custom _formulas_ in certain parts of the wave configurations:
#monsters = base * #wave^exp
base = min(#players/2 + 1 , 13)
The `exp` variable is defined by the growth node, and has the following values:
- Wave growth in Default Waves
- Swarm amounts in Swarm Waves
- Boss health in Boss Waves
slow = 0.5
medium = 0.65
fast = 0.8
psycho = 1.2
A formula is just a mathematical expression like `2+3`. Formulas are resolved at runtime during arena sessions, and they support fixed value [constants](#constants) and runtime [variables](#variables), as well as various mathematical [operators](#operators) and [functions](#functions).
Note that with the node value `old` (which is the default), the monster count is `#wave + #players`.
Here is an example of a Swarm Wave that spawns twice as many slimes as there are players currently alive in the arena, up to a maximum of 20:
### Swarm Amount ###
```yml
swarm2:
type: swarm
wave: 12
monster: slime
amount: max(20, <live-players> * 2)
```
The swarm amount node `amount`, used in swarm waves, denotes how many monsters should spawn in the swarm waves. There will always be at least 10 monsters due to the max function and the lowest multiplier value being 10, however this can be further customized with the `amount-multiplier` (see the [[wave setup page|Setting up the waves]]).
MobArena comes with a small bag of predefined formulas in `formulas.yml` called [macros](#macros). These can be used instead of manually typing in the same formula multiple times all over the config-file. It is possible to redefine or remove existing formulas, and to add entirely new, custom formulas.
#monsters = max(1, #players/2) * multiplier
The `multiplier` variable is defined by the amount node, and has the following values:
low = 10
medium = 20
high = 30
psycho = 60
## Macros
Formula macros are the best way to reuse formulas, because they allow you to write a given formula once and then just reference it by name later. Macros are defined in the `formulas.yml` file, which consists of four different sections:
### Boss Health ###
- `global`: globally available macros that can be referenced from anywhere
- `wave-growth`: can only be used in the `growth` property of Default Waves
- `swarm-amount`: can only be used in the `amount` property of Swarm Waves
- `boss-health`: can only be used in the `health` property of Boss Waves
The boss health node `health`, used in boss waves, denotes how much health the boss has. Note that the `health-multiplier` node (see the [[wave setup page|Setting up the waves]]) **does NOT** affect boss waves at all, so these are the only values that can be used. The minimum health a boss can have is 160 health points (~80 hearts), which is with `verylow` health and only 1 player fighting. With 10 players and `high` health, the boss will have 5500 health points (~2750 hearts).
The `global` section defines macros that can be referenced anywhere a formula can be used. The other three sections are only available to a specific property. This means that the predefined `top-up` macro in the `global` section can be used as a wave growth formula or a boss health formula - both will work the same. However, because the macro `low` is defined in both the `swarm-amount` section and the `boss-health` section, the macro will resolve different formulas depending on whether it is used as a swarm amount in a Swarm Wave or as boss health in a Boss Wave. This also means that using it for the wave growth property in a Default Wave will result in an error, because `low` isn't defined in the `wave-growth` section, _nor_ in the `global` section.
health = (#players + 1) * 20 * multiplier
The `multiplier` variable is defined by the health node, and has the following values:
verylow = 4
low = 8
medium = 15
high = 25
veryhigh = 40
psycho = 60
## Reference
The following sections describe the constants, variables, operators, and functions available for use in formulas.
### Constants
Constants are _keywords_ (`constant`) that are bound to fixed numeric values.
- `pi`: 3.141592...
- `e`: Euler's number
### Variables
Variables are _words in angled brackets_ (`<variable>`) that resolve a numeric value at runtime. They resolve to different values depending on the state of the arena session.
- `<current-wave>`: the number of the current wave, e.g. `1` for the first wave, `2` for the second, etc.
- `<final-wave>`: the number of the final wave as given in the arena settings
- `<initial-players>`: the number of players alive at the start of the session
- `<live-players>`: the number of players currently alive in the session
- `<dead-players>`: the number of players _not_ currently alive in the session
- `<min-players>`: the minimum number of players required to start the arena as specified in the arena settings
- `<max-players>`: the maximum number of players allowed in the arena as specified in the arena settings
- `<max-players>`: the maximum number of players allowed in the arena as specified in the arena settings
- `<live-monsters>`: the number of monsters currently alive in the session
### Operators
Simple mathematical infix binary operators like addition, subtraction, etc.
- `+`: addition
- `-`: subtraction
- `*`: multiplication
- `/`: division
- `%`: modulo
- `^`: exponentiation
Additionally, the following _unary_ operators are supported:
- `-`: negation
### Functions
Unary mathematical functions like square root, trigonometric functions, etc.
- `sqrt(x)`: square root
- `abs(x)`: absolute value
- `ceil(x)`: round up
- `floor(x)`: round down
- `round(x)`: round to closest integer
- `sin(x)`: sine
- `cos(x)`: cosine
- `tan(x)`: tangent
Additionally, the following _binary_ functions are supported:
- `min(a, b)`: minimum value of `a` and `b`
- `max(a, b)`: maximum value of `a` and `b`
## Extensions
The following parts of the Formula system can be extended by other plugins:
- **Constants:** Bind a fixed numeric value to a given keyword.
- **Variables:** Resolve custom variables from an arena instance at runtime.
- **Operators:** Introduce new unary or binary operators.
- **Functions:** Extend the function catalog with more unary or binary functions.
To register extensions, grab a hold of MobArena's FormulaManager instance via the main plugin class:
```java
MobArena plugin = (MobArena) Bukkit.getPluginManager().getPlugin("MobArena");
FormulaManager formulas = plugin.getFormulaManager();
```
Then register the desired extensions:
```java
// Register `tau` as a constant
formulas.registerConstant("tau", Math.PI * 2);
// Register `<exploding-sheep>` as a variable
formulas.registerVariable("exploding-sheep", arena -> {
MonsterManager monsters = arena.getMonsterManager();
Set<LivingEntity> sheep = monsters.getExplodingSheep();
return sheep.size();
});
// Register `@` as a new binary operator that works like normal
// subtraction, except with flipped operands.
formulas.registerBinaryOperator("@", 2, true, (a, b) -> b - a);
// Register `up` as a new unary function that adds 1 to its input.
formulas.registerUnaryFunction("square", v -> v + 1);
```
Note that extensions must be registered _before_ MobArena's `onEnable()` runs for the registrations to take effect. The FormulaManager is available after MobArena's `onLoad()` has executed. This means that plugins extending MobArena's Formula system should have `loadbefore: [MobArena]` in their `plugin.yml` file, and they should register extensions during their own `onEnable()` method.