arena_lib/DOCS.md

159 lines
9.5 KiB
Markdown
Raw Normal View History

2020-04-02 14:23:11 -07:00
# Arena_lib docs
2020-04-03 09:22:21 -07:00
> Arena_lib is a library for Minetest working as a core for any arena-like minigame you have in mind
2020-04-02 14:23:11 -07:00
## 1. Arenas
2020-04-16 11:42:42 -07:00
It all starts with a table called `arena_lib.mods = {}`. This table allows `arena_lib` to be subdivided per mod and it has different parameters, one being `arena_lib.mods[yourmod].arenas`. Here is where every new arena created gets put.
2020-04-02 14:23:11 -07:00
An arena is a table having as a key an ID and as a value its parameters. They are:
* `name`: (string) the name of the arena, declared when creating it
* `sign`: (pos) the position of the sign associated with the arena.
* `players`: (table) where to store players
* `max_players`: (string) default is 4
* `min_players`: (string) default is 2. When this value is reached, a queue starts
* `kill_cap`: (int) the goal to win (it'll be expanded for games such as Capture the point)
* `kill_leader`: (string) who's the actual kill leader
* `in_queue`: (bool) about phases, look at "Arena phases" down below
* `in_loading`: (bool)
* `in_game`: (bool)
* `in_celebration`: (bool)
2020-04-16 07:48:15 -07:00
* `enabled`: (bool) by default an arena is disabled, to avoid any unwanted damage
2020-04-02 14:23:11 -07:00
2020-04-16 11:42:42 -07:00
Being arenas stored by ID, they can be easily retrieved by `arena_libs.mods[yourmod].arenas[THEARENAID]`.
2020-04-03 10:09:48 -07:00
There are two ways to know an arena ID: the first is in-game via the two debug utilities:
2020-04-16 07:48:15 -07:00
* `arena_lib.print_arenas(sender, mod)`: coincise
* `arena_lib.print_arena_info(sender, mod, arena_name)`: extended with much more information
2020-04-03 10:09:48 -07:00
The second is via code by the function `arena_libs.get_arenaID_by_player(p_name)`.
2020-04-02 14:23:11 -07:00
### 1.1 Creating and removing arenas
There are two functions for it and they all need to be connected to some command in your mod. The functions are
2020-04-16 07:48:15 -07:00
* `arena_lib.create_arena(sender, mod, arena_name, <min_players>, <max_players>)`: it doesn't accept duplicates. Sender is a string, fields between < > are optional
* `arena_lib.remove_arena(mod, arena_name)`: if a game is taking place in it, it won't go through
2020-04-02 14:23:11 -07:00
2020-04-03 10:09:48 -07:00
#### 1.1.1 Storing arenas
Arenas and their settings are stored inside the mod storage. What is *not* stored are players, their stats and such.
Better said, these kind of parameters are emptied every time the server starts. And not when it ends, because handling situations like crashes is simply not possible.
2020-04-02 14:23:11 -07:00
### 1.2 Setting up an arena
2020-04-03 10:09:48 -07:00
Two things are needed to have an arena up to go: spawners and signs. There are two functions for that:
2020-04-16 07:48:15 -07:00
* `arena_lib.set_spawner(sender, mod, arena_name, spawner_ID)`
* `arena_lib.set_sign(sender, mod, arena_name)`
2020-04-02 14:23:11 -07:00
2020-04-03 10:09:48 -07:00
#### 1.2.1 Spawners
`arena_lib.set_spawner(sender, arena_name, spawner_ID)` creates a spawner where the sender is standing, so be sure to stand where you want the spawn point to be.
`spawner_ID` is optional and it does make a difference: with it, it overrides an existing spawner (if exists), without it, it creates a new one. Spawners can't exceed the maximum players of an arena and, more specifically, the must be the same number.
I suggest you using [ChatCmdBuilder](https://rubenwardy.com/minetest_modding_book/en/players/chat_complex.html) by rubenwardy and connect the `set_spawner` function to two separate subcommands such as:
```
2020-04-16 07:48:15 -07:00
local mod = "mymod" --more about this later
2020-04-03 10:09:48 -07:00
ChatCmdBuilder.new("NAMEOFYOURCOMMAND", function(cmd)
2020-04-16 07:48:15 -07:00
cmd:sub("setspawn :arena", function(sender, arena)
arena_lib.set_spawner(sender, mod, arena)
2020-04-03 10:09:48 -07:00
end)
2020-04-02 14:23:11 -07:00
2020-04-16 07:48:15 -07:00
cmd:sub("setspawn :arena :spawnID:int", function(sender, arena, spawn_ID)
arena_lib.set_spawner(sender, mod, arena, spawn_ID)
2020-04-03 10:09:48 -07:00
end)
[etc.]
```
#### 1.2.2 Signs
2020-04-16 07:48:15 -07:00
`arena_lib.set_sign(sender, mod, arena_name)` gives the player an item to hit a sign with. There must be one and one only sign for arena, and when hit it becomes the access to the arena.
2020-04-03 10:09:48 -07:00
#### 1.2.3 Enabling an arena
When a sign has been set, it won't work. This because an arena must be enabled manually via
2020-04-16 07:48:15 -07:00
`arena_lib.enable_arena(sender, mod, arena_ID)`
If all the conditions are met, you'll receive a confirmation. If not, you'll receive a reason and the arena will remain disabled. Conditions are:
* all spawn points set
* sign placed
2020-04-03 10:09:48 -07:00
Arenas can be disabled too, via
2020-04-16 07:48:15 -07:00
`arena_lib.disable_arena(sender, mod, arena_ID)`
In order to do that, no game must be taking place in that specific arena.
2020-04-03 10:09:48 -07:00
### 1.3 Arena phases
2020-04-02 14:23:11 -07:00
An arena comes in 4 phases, each one of them linked to a specific function:
* `waiting phase`: it's the queuing process. People hit a sign waiting for other players to play with
* `loading phase`: it's the pre-match. By default players get teleported in the arena not being able to do anything but jump
* `fighting phase`: the actual game
* `celebration phase`: the after-match. By default people stroll around for the arena knowing who won, waiting to be teleported
The 4 functions, intertwined with the previously mentioned phases are:
2020-04-16 07:48:15 -07:00
* `arena_lib.load_arena(mod, arena_ID)`: between the waiting and the loading phase. Called when the queue timer reaches 0, it teleports people inside.
* `arena_lib.start_arena(mod_ref, arena)`: between the loading and the fighting phase. Called when the loading phase timer reaches 0.
* `arena_lib.load_celebration(mod, arena, winner_name)`: between the fighting and the celebration phase. Called when the winning conditions are met.
* `arena_lib.end_arena(mod_ref, arena)`: at the very end of the celebration phase. It teleports people outside the arena
2020-04-02 14:23:11 -07:00
2020-04-16 07:48:15 -07:00
Overriding these functions is not recommended. Instead, there are 4 respective callbacks made specifically to customize the behaviour of the formers, sharing (almost) the same variables. They are called *after* the function they're associated with and by default they are empty, so feel free to override them. They are `on_load`, `on_start`, `on_celebration` and `on_end`, and they are explained later in 2.2.
2020-04-02 14:23:11 -07:00
2020-04-16 07:48:15 -07:00
## 2. Configuration
2020-04-02 14:23:11 -07:00
2020-04-16 07:48:15 -07:00
First of all download the mod and put it in your mods folder.
2020-04-02 14:23:11 -07:00
2020-04-16 07:48:15 -07:00
Then, you need to initialise the mod for each mod you want to feature arena_lib in, inside the init.lua via:
2020-04-02 14:23:11 -07:00
```
2020-04-16 07:48:15 -07:00
arena_lib.initialize("yourmod")
```
**Be careful**: the string you put inside the round brackets will be how arena_lib stores your mod inside its memory and what it needs to understand you're refering to that specific mod (that's why almost every function contains "mod" as a parameter). You'll need it when calling for commands.
2020-04-02 14:23:11 -07:00
2020-04-16 07:48:15 -07:00
To customise your mod mod, you can then call `arena_lib.settings`, specifying the mod name as follows:
```
arena_lib.settings("mymod", {
--whatever parameter, such as
-- prefix = "[mymode] ",
})
```
2020-04-02 14:23:11 -07:00
The parameters are:
* `prefix`: what's gonna appear in most of the lines printed by your mod. Default is `[arena_lib] `
2020-04-16 07:48:15 -07:00
* `hub_spawn_point`: where players will be teleported when a match _in your mod_ ends. Default is `{ x = 0, y = 20, z = 0 }`
2020-04-02 14:23:11 -07:00
* `load_time`: the time between the loading state and the start of the match. Default is 3
* `celebration_time`: the time between the celebration state and the end of the match. Default is 3
* `immunity_time`: the duration of the immunity right after respawning. Default is 3
* `immunity_slot`: the slot whereto put the immunity item. Default is 9 (the first slot of the inventory minus the hotbar)
2020-04-16 07:48:15 -07:00
On the contrary, to customise the _global_ spawn point of your hub, meaning where to spawn players when they join the server, you need to edit the line
`local hub_spawn_point = { x = 0, y = 20, z = 0}`
in api.lua, inside the arena_lib folder.
2020-04-03 10:09:48 -07:00
### 2.1 Commands
2020-04-16 07:48:15 -07:00
You need to connect the functions of the library with your mod in order to use them. The best way is with commands and again I suggest you the [ChatCmdBuilder](https://rubenwardy.com/minetest_modding_book/en/players/chat_complex.html) by rubenwardy. [This](https://gitlab.com/zughy-friends-minetest/minetest-quake/-/blob/master/commands.lua) is what I came up with in my Quake minigame, which relies on arena_lib. As you can see, I declared a `local mod = "quake"` at the beginning, because it's how I stored my mod inside the library.
### 2.2 Callbacks
To customise your mod even more, there are a few empty callbacks you can use. They are:
* `arena_lib.on_load(mod, function(arena)` (we saw these 4 earlier)
* `arena_lib.on_start(mod, function(arena))`
* `arena_lib.on_celebration(mod, function(arena, winner_name)`
* `arena_lib.on_end(mod, function(arena, players))`
* `arena_lib.on_join(mod, function(p_name, arena))`: called when a player joins an ongoing match
* `arena_lib.on_death(mod, function(arena, p_name))`: called when a player dies
Beware: there is a default behaviour already for each one of these situations: for instance when a player dies, its deaths increase by 1. These callbacks exist just in case you want to add some extra behaviour to arena_lib's.
So for instance, if we want to add an object in the first slot when a player joins the pre-match, we can simply do:
```
arena_lib.on_load("mymod", function(arena)
local item = ItemStack("default:dirt")
for pl_name, stats in pairs(arena.players) do
pl_name:get_inventory():set_stack("main", 1, item)
end
end)
```
2020-04-03 10:09:48 -07:00
## 3. Collaborating
2020-04-16 07:48:15 -07:00
Something's wrong? Feel free to open an issue, go for a merge request and whatnot. I'd really appreciate it :)
2020-04-02 14:23:11 -07:00
2020-04-03 10:09:48 -07:00
## 4. About the author(s)
2020-04-03 09:21:19 -07:00
I'm Zughy (Marco), a professional Italian pixel artist who fights for FOSS and digital ethics. If this library spared you a lot of time and you want to support me somehow, please consider donating on [LiberaPay](https://it.liberapay.com/EticaDigitale/) directly to my educational Italian project (because not everyone speaks English and Italian media don't talk about these topics much). Also, this project wouldn't have been possible if it hadn't been for some friends who helped me testing through: `SonoMichele`, `_Zaizen_` and `Xx_Crazyminer_xX`