Carts: Merge boost_cart as "carts" mod
[oweals/minetest_game.git] / game_api.txt
1 Minetest Game API
2 =================
3 GitHub Repo: https://github.com/minetest/minetest_game
4
5 Introduction
6 ------------
7
8 The Minetest Game subgame offers multiple new possibilities in addition to the Minetest engine's built-in API,
9 allowing you to add new plants to farming mod, buckets for new liquids, new stairs and custom panes.
10 For information on the Minetest API, visit https://github.com/minetest/minetest/blob/master/doc/lua_api.txt
11 Please note:
12
13  * [XYZ] refers to a section the Minetest API
14  * [#ABC] refers to a section in this document
15  * [pos] refers to a position table `{x = -5, y = 0, z = 200}`
16
17 Bucket API
18 ----------
19
20 The bucket API allows registering new types of buckets for non-default liquids.
21
22         bucket.register_liquid(
23                 "default:lava_source",   -- name of the source node
24                 "default:lava_flowing",  -- name of the flowing node
25                 "bucket:bucket_lava",    -- name of the new bucket item (or nil if liquid is not takeable)
26                 "bucket_lava.png",       -- texture of the new bucket item (ignored if itemname == nil)
27                 "Lava Bucket",           -- text description of the bucket item
28                 {lava_bucket = 1},       -- groups of the bucket item, OPTIONAL
29                 false                    -- force-renew, OPTIONAL. Force the liquid source to renew if it has
30                                          -- a source neighbour, even if defined as 'liquid_renewable = false'.
31                                          -- Needed to avoid creating holes in sloping rivers.
32         )
33
34 The filled bucket item is returned to the player that uses an empty bucket pointing to the given liquid source.
35 When punching with an empty bucket pointing to an entity or a non-liquid node, the on_punch of the entity or node will be triggered.
36
37 Beds API
38 --------
39
40         beds.register_bed(
41                 "beds:bed",    -- Bed name
42                 def            -- See [#Bed definition]
43         )
44
45  * `beds.read_spawns() `   Returns a table containing players respawn positions
46  * `beds.kick_players()`  Forces all players to leave bed
47  * `beds.skip_night()`   Sets world time to morning and saves respawn position of all players currently sleeping
48
49 ### Bed definition
50
51         {
52                 description = "Simple Bed",
53                 inventory_image = "beds_bed.png",
54                 wield_image = "beds_bed.png",
55                 tiles = {
56                         bottom = {'Tile definition'}, -- the tiles of the bottom part of the bed.
57                         top = {Tile definition} -- the tiles of the bottom part of the bed.
58                 },
59                 nodebox = {
60                         bottom = 'regular nodebox',     -- bottom part of bed (see [Node boxes])
61                         top = 'regular nodebox',        -- top part of bed (see [Node boxes])
62                 },
63                 selectionbox = 'regular nodebox',  -- for both nodeboxes (see [Node boxes])
64                 recipe = {                                         -- Craft recipe
65                         {"group:wool", "group:wool", "group:wool"},
66                         {"group:wood", "group:wood", "group:wood"}
67                 }
68         }
69
70 Creative API
71 ------------
72
73 A global string called `creative.formspec_add` was added which allows mods to add additional formspec elements onto the default creative inventory formspec to be drawn after each update.
74
75 Doors API
76 ---------
77
78 The doors mod allows modders to register custom doors and trapdoors.
79
80 `doors.register_door(name, def)`
81
82  * Registers new door
83  * `name` Name for door
84  * `def`  See [#Door definition]
85
86 `doors.register_trapdoor(name, def)`
87
88  * Registers new trapdoor
89  * `name` Name for trapdoor
90  * `def`  See [#Trapdoor definition]
91
92 `doors.register_fencegate(name, def)`
93
94  * Registers new fence gate
95  * `name` Name for fence gate
96  * `def`  See [#Fence gate definition]
97
98 `doors.get(pos)`
99
100  * `pos` A position as a table, e.g `{x = 1, y = 1, z = 1}`
101  * Returns an ObjectRef to a door, or nil if the position does not contain a door
102
103     ### Methods
104
105         :open(player)   -- Open the door object, returns if door was opened
106         :close(player)  -- Close the door object, returns if door was closed
107         :toggle(player) -- Toggle the door state, returns if state was toggled
108         :state()        -- returns the door state, true = open, false = closed
109
110     the "player" parameter can be omitted in all methods. If passed then
111     the usual permission checks will be performed to make sure the player
112     has the permissions needed to open this door. If omitted then no
113     permission checks are performed.
114
115 ### Door definition
116
117         description = "Door description",
118         inventory_image = "mod_door_inv.png",
119         groups = {choppy = 2},
120         tiles = {"mod_door.png"}, -- UV map.
121         recipe = craftrecipe,
122         sounds = default.node_sound_wood_defaults(), -- optional
123         sound_open = sound play for open door, -- optional
124         sound_close = sound play for close door, -- optional
125         protected = false, -- If true, only placer can open the door (locked for others)
126
127 ### Trapdoor definition
128
129         description = "Trapdoor description",
130         inventory_image = "mod_trapdoor_inv.png",
131         groups = {choppy = 2},
132         tile_front = "doors_trapdoor.png", -- the texture for the front and back of the trapdoor
133         tile_side = "doors_trapdoor_side.png", -- the tiles of the four side parts of the trapdoor
134         sounds = default.node_sound_wood_defaults(), -- optional
135         sound_open = sound play for open door, -- optional
136         sound_close = sound play for close door, -- optional
137         protected = false, -- If true, only placer can open the door (locked for others)
138
139 ### Fence gate definition
140
141         description = "Wooden Fence Gate",
142         texture = "default_wood.png",
143         material = "default:wood",
144         groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
145         sounds = default.node_sound_wood_defaults(), -- optional
146
147 Fence API
148 ---------
149
150 Allows creation of new fences with "fencelike" drawtype.
151
152 `default.register_fence(name, item definition)`
153
154  Registers a new fence. Custom fields texture and material are required, as
155  are name and description. The rest is optional. You can pass most normal
156  nodedef fields here except drawtype. The fence group will always be added
157  for this node.
158
159 ### fence definition
160
161         name = "default:fence_wood",
162         description = "Wooden Fence",
163         texture = "default_wood.png",
164         material = "default:wood",
165         groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
166         sounds = default.node_sound_wood_defaults(),
167
168 Walls API
169 ---------
170
171 The walls API allows easy addition of stone auto-connecting wall nodes.
172
173 walls.register(name, desc, texture, mat, sounds)
174 ^ name = "walls:stone_wall". Node name.
175 ^ desc = "A Stone wall"
176 ^ texture = "default_stone.png"
177 ^ mat = "default:stone". Used to auto-generate crafting recipe.
178 ^ sounds = sounds: see [#Default sounds]
179
180 Farming API
181 -----------
182
183 The farming API allows you to easily register plants and hoes.
184
185 `farming.register_hoe(name, hoe definition)`
186  * Register a new hoe, see [#hoe definition]
187
188 `farming.register_plant(name, Plant definition)`
189  * Register a new growing plant, see [#Plant definition]
190
191 `farming.registered_plants[name] = definition`
192  * Table of registered plants, indexed by plant name
193
194 ### Hoe Definition
195
196
197         {
198                 description = "",                      -- Description for tooltip
199                 inventory_image = "unknown_item.png",  -- Image to be used as wield- and inventory image
200                 max_uses = 30,                         -- Uses until destroyed
201                 material = "",                         -- Material for recipes
202                 recipe = {                             -- Craft recipe, if material isn't used
203                         {"air", "air", "air"},
204                         {"", "group:stick"},
205                         {"", "group:stick"},
206                 }
207         }
208
209 ### Plant definition
210
211         {
212                 description = "",                      -- Description of seed item
213                 inventory_image = "unknown_item.png",  -- Image to be used as seed's wield- and inventory image
214                 steps = 8,                             -- How many steps the plant has to grow, until it can be harvested
215                 -- ^ Always provide a plant texture for each step, format: modname_plantname_i.png (i = stepnumber)
216                 minlight = 13,                         -- Minimum light to grow
217                 maxlight = default.LIGHT_MAX           -- Maximum light to grow
218         }
219
220 Fire API
221 --------
222
223 New node def property:
224
225 `on_burn(pos)`
226
227  * Called when fire attempts to remove a burning node.
228  * `pos` Position of the burning node.
229
230  `on_ignite(pos, igniter)`
231
232   * Called when Flint and steel (or a mod defined ignitor) is used on a node.
233     Defining it may prevent the default action (spawning flames) from triggering.
234   * `pos` Position of the ignited node.
235   * `igniter` Player that used the tool, when available.
236
237
238 Give Initial Stuff API
239 ----------------------
240
241 `give_initial_stuff.give(player)`
242
243 ^ Give initial stuff to "player"
244
245 `give_initial_stuff.add(stack)`
246
247 ^ Add item to the initial stuff
248 ^ Stack can be an ItemStack or a item name eg: "default:dirt 99"
249 ^ Can be called after the game has loaded
250
251 `give_initial_stuff.clear()`
252
253 ^ Removes all items from the initial stuff
254 ^ Can be called after the game has loaded
255
256 `give_initial_stuff.get_list()`
257
258 ^ returns list of item stacks
259
260 `give_initial_stuff.set_list(list)`
261
262 ^ List of initial items with numeric indices.
263
264 `give_initial_stuff.add_from_csv(str)`
265
266 ^ str is a comma separated list of initial stuff
267 ^ Adds items to the list of items to be given
268
269 Nyancat API
270 -----------
271
272 `nyancat.place(pos, facedir, length)`
273
274 ^ Place a cat at `pos` facing `facedir` with tail length `length`
275   Only accepts facedir 0-3, if facedir > 3 then it will be interpreted as facedir = 0
276
277 `nyancat.generate(minp, maxp, seed)`
278
279 ^ Called by `minetest.register_on_generated`. To disable nyancat generation,
280   you can redefine nyancat.generate() to be an empty function
281
282 TNT API
283 ----------
284
285 `tnt.register_tnt(definition)`
286
287 ^ Register a new type of tnt.
288
289  * `name` The name of the node. If no prefix is given `tnt` is used.
290  * `description` A description for your TNT.
291  * `radius` The radius within which the TNT can destroy nodes. The default is 3.
292  * `damage_radius` The radius within which the TNT can damage players and mobs. By default it is twice the `radius`.
293  * `disable_drops` Disable drops. By default it is set to false.
294  * `ignore_protection` Don't check `minetest.is_protected` before removing a node.
295  * `ignore_on_blast` Don't call `on_blast` even if a node has one.
296  * `tiles` Textures for node
297   * `side`  Side tiles. By default the name of the tnt with a suffix of `_side.png`.
298   * `top`  Top tile. By default the name of the tnt with a suffix of `_top.png`.
299   * `bottom` Bottom tile. By default the name of the tnt with a suffix of `_bottom.png`.
300   * `burning` Top tile when lit. By default the name of the tnt with a suffix of `_top_burning_animated.png".
301
302 `tnt.boom(position, definition)`
303
304 ^ Create an explosion.
305
306 * `position` The center of explosion.
307 * `definition` The TNT definion as passed to `tnt.register`
308
309 `tnt.burn(position, [nodename])`
310
311 ^ Ignite TNT at position, nodename isn't required unless already known.
312
313
314 To make dropping items from node inventories easier, you can use the
315 following helper function from 'default':
316
317 default.get_inventory_drops(pos, inventory, drops)
318
319 ^ Return drops from node inventory "inventory" in drops.
320
321 * `pos` - the node position
322 * `inventory` - the name of the inventory (string)
323 * `drops` - an initialized list
324
325 The function returns no values. The drops are returned in the `drops`
326 parameter, and drops is not reinitialized so you can call it several
327 times in a row to add more inventory items to it.
328
329
330 `on_blast` callbacks:
331
332 Both nodedefs and entitydefs can provide an `on_blast()` callback
333
334 `nodedef.on_blast(pos, intensity)`
335 ^ Allow drop and node removal overriding
336 * `pos` - node position
337 * `intensity` - TNT explosion measure. larger or equal to 1.0
338 ^ Should return a list of drops (e.g. {"default:stone"})
339 ^ Should perform node removal itself. If callback exists in the nodedef
340 ^ then the TNT code will not destroy this node.
341
342 `entitydef.on_blast(luaobj, damage)`
343 ^ Allow TNT effects on entities to be overridden
344 * `luaobj` - LuaEntityRef of the entity
345 * `damage` - suggested HP damage value
346 ^ Should return a list of (bool do_damage, bool do_knockback, table drops)
347 * `do_damage` - if true then TNT mod wil damage the entity
348 * `do_knockback` - if true then TNT mod will knock the entity away
349 * `drops` - a list of drops, e.g. {"wool:red"}
350
351
352 Screwdriver API
353 ---------------
354
355 The screwdriver API allows you to control a node's behaviour when a screwdriver is used on it.
356 To use it, add the `on_screwdriver` function to the node definition.
357
358 `on_rotate(pos, node, user, mode, new_param2)`
359
360  * `pos` Position of the node that the screwdriver is being used on
361  * `node` that node
362  * `user` The player who used the screwdriver
363  * `mode` screwdriver.ROTATE_FACE or screwdriver.ROTATE_AXIS
364  * `new_param2` the new value of param2 that would have been set if on_rotate wasn't there
365  * return value: false to disallow rotation, nil to keep default behaviour, true to allow
366         it but to indicate that changed have already been made (so the screwdriver will wear out)
367  * use `on_rotate = false` to always disallow rotation
368  * use `on_rotate = screwdriver.rotate_simple` to allow only face rotation
369
370
371 Sethome API
372 -----------
373
374 The sethome API adds three global functions to allow mods to read a players home position,
375 set a players home position and teleport a player to home position.
376
377 `sethome.get(name)`
378
379  * `name` Player who's home position you wish to get
380  * return value: false if no player home coords exist, position table if true
381
382 `sethome.set(name, pos)`
383
384  * `name` Player who's home position you wish to set
385  * `pos` Position table containing coords of home position
386  * return value: false if unable to set and save new home position, otherwise true
387
388 `sethome.go(name)`
389
390  * `name` Player you wish to teleport to their home position
391  * return value: false if player cannot be sent home, otherwise true
392
393
394 Stairs API
395 ----------
396
397 The stairs API lets you register stairs and slabs and ensures that they are registered the same way as those
398 delivered with Minetest Game, to keep them compatible with other mods.
399
400 `stairs.register_stair(subname, recipeitem, groups, images, description, sounds)`
401
402  * Registers a stair.
403  * `subname`: Basically the material name (e.g. cobble) used for the stair name. Nodename pattern: "stairs:stair_subname"
404  * `recipeitem`: Item used in the craft recipe, e.g. "default:cobble", may be `nil`
405  * `groups`: see [Known damage and digging time defining groups]
406  * `images`: see [Tile definition]
407  * `description`: used for the description field in the stair's definition
408  * `sounds`: see [#Default sounds]
409
410 `stairs.register_slab(subname, recipeitem, groups, images, description, sounds)`
411
412  * Registers a slabs
413  * `subname`: Basically the material name (e.g. cobble) used for the stair name. Nodename pattern: "stairs:stair_subname"
414  * `recipeitem`: Item used in the craft recipe, e.g. "default:cobble"
415  * `groups`: see [Known damage and digging time defining groups]
416  * `images`: see [Tile definition]
417  * `description`: used for the description field in the stair's definition
418  * `sounds`: see [#Default sounds]
419
420 `stairs.register_stair_and_slab(subname, recipeitem, groups, images, desc_stair, desc_slab, sounds)`
421
422  * A wrapper for stairs.register_stair and stairs.register_slab
423  * Uses almost the same arguments as stairs.register_stair
424  * `desc_stair`: Description for stair node
425  * `desc_slab`: Description for slab node
426
427 Xpanes API
428 ----------
429
430 Creates panes that automatically connect to each other
431
432 `xpanes.register_pane(subname, def)`
433
434  * `subname`: used for nodename. Result: "xpanes:subname" and "xpanes:subname_{2..15}"
435  * `def`: See [#Pane definition]
436
437 ### Pane definition
438
439         {
440                 textures = {"texture for sides", (unused), "texture for top and bottom"}, -- More tiles aren't supported
441                 groups = {group = rating}, -- Uses the known node groups, see [Known damage and digging time defining groups]
442                 sounds = SoundSpec,        -- See [#Default sounds]
443                 recipe = {{"","","","","","","","",""}}, -- Recipe field only
444         }
445
446 Raillike definitions
447 --------------------
448
449 The following nodes use the group `connect_to_raillike` and will only connect to
450 raillike nodes within this group and the same group value.
451 Use `minetest.raillike_group(<Name>)` to get the group value.
452
453 | Node type             | Raillike group name
454 |-----------------------|---------------------
455 | default:rail          | "rail"
456 | tnt:gunpowder         | "gunpowder"
457 | tnt:gunpowder_burning | "gunpowder"
458
459 Example:
460 If you want to add a new rail type and want it to connect with default:rail,
461 add `connect_to_raillike=minetest.raillike_group("rail")` into the `groups` table
462 of your node.
463
464
465 Default sounds
466 --------------
467
468 Sounds inside the default table can be used within the sounds field of node definitions.
469
470  * `default.node_sound_defaults()`
471  * `default.node_sound_stone_defaults()`
472  * `default.node_sound_dirt_defaults()`
473  * `default.node_sound_sand_defaults()`
474  * `default.node_sound_wood_defaults()`
475  * `default.node_sound_leaves_defaults()`
476  * `default.node_sound_glass_defaults()`
477  * `default.node_sound_metal_defaults()`
478
479 Default constants
480 -----------------
481
482 `default.LIGHT_MAX`  The maximum light level (see [Node definition] light_source)
483
484 Player API
485 ----------
486
487 The player API can register player models and update the player's appearence
488
489 `default.player_register_model(name, def)`
490
491  * Register a new model to be used by players.
492  * name: model filename such as "character.x", "foo.b3d", etc.
493  * def: See [#Model definition]
494
495 `default.registered_player_models[name]`
496
497  * Get a model's definition
498  * see [#Model definition]
499
500 `default.player_set_model(player, model_name)`
501
502  * Change a player's model
503  * `player`: PlayerRef
504  * `model_name`: model registered with player_register_model()
505
506 `default.player_set_animation(player, anim_name [, speed])`
507
508  * Applies an animation to a player
509  * anim_name: name of the animation.
510  * speed: frames per second. If nil, default from the model is used
511
512 `default.player_set_textures(player, textures)`
513
514  * Sets player textures
515  * `player`: PlayerRef
516  * `textures`: array of textures, If `textures` is nil, the default textures from the model def are used
517
518 default.player_get_animation(player)
519
520  * Returns a table containing fields `model`, `textures` and `animation`.
521  * Any of the fields of the returned table may be nil.
522  * player: PlayerRef
523
524 ### Model Definition
525
526         {
527                 animation_speed = 30,            -- Default animation speed, in FPS.
528                 textures = {"character.png", },  -- Default array of textures.
529                 visual_size = {x = 1, y = 1},    -- Used to scale the model.
530                 animations = {
531                         -- <anim_name> = {x = <start_frame>, y = <end_frame>},
532                         foo = {x = 0, y = 19},
533                         bar = {x = 20, y = 39},
534                 -- ...
535                 },
536         }
537
538 Leafdecay
539 ---------
540
541 To enable leaf decay for a node, add it to the `leafdecay` group.
542
543 The rating of the group determines how far from a node in the group `tree`
544 the node can be without decaying.
545
546 If `param2` of the node is ~= 0, the node will always be preserved. Thus, if
547 the player places a node of that kind, you will want to set `param2 = 1` or so.
548
549 The function `default.after_place_leaves` can be set as `after_place_node of a node`
550 to set param2 to 1 if the player places the node (should not be used for nodes
551 that use param2 otherwise (e.g. facedir)).
552
553 If the node is in the `leafdecay_drop` group then it will always be dropped as an
554 item.
555
556 Dyes
557 ----
558
559 To make recipes that will work with any dye ever made by anybody, define
560 them based on groups. You can select any group of groups, based on your need for
561 amount of colors.
562
563 ### Color groups
564
565 Base color groups:
566
567  * `basecolor_white`
568  * `basecolor_grey`
569  * `basecolor_black`
570  * `basecolor_red`
571  * `basecolor_yellow`
572  * `basecolor_green`
573  * `basecolor_cyan`
574  * `basecolor_blue`
575  * `basecolor_magenta`
576
577 Extended color groups ( * means also base color )
578
579  * `excolor_white` *
580  * `excolor_lightgrey`
581  * `excolor_grey` *
582  * `excolor_darkgrey`
583  * `excolor_black` *
584  * `excolor_red` *
585  * `excolor_orange`
586  * `excolor_yellow` *
587  * `excolor_lime`
588  * `excolor_green` *
589  * `excolor_aqua`
590  * `excolor_cyan` *
591  * `excolor_sky_blue`
592  * `excolor_blue` *
593  * `excolor_violet`
594  * `excolor_magenta` *
595  * `excolor_red_violet`
596
597 The whole unifieddyes palette as groups:
598
599  * `unicolor_<excolor>`
600
601 For the following, no white/grey/black is allowed:
602
603  * `unicolor_medium_<excolor>`
604  * `unicolor_dark_<excolor>`
605  * `unicolor_light_<excolor>`
606  * `unicolor_<excolor>_s50`
607  * `unicolor_medium_<excolor>_s50`
608  * `unicolor_dark_<excolor>_s50`
609
610 Example of one shapeless recipe using a color group:
611
612         minetest.register_craft({
613                 type = "shapeless",
614                 output = '<mod>:item_yellow',
615                 recipe = {'<mod>:item_no_color', 'group:basecolor_yellow'},
616         })
617
618 ### Color lists
619
620  * `dye.basecolors` are an array containing the names of available base colors
621
622  * `dye.excolors` are an array containing the names of the available extended colors
623
624 Trees
625 -----
626
627  * `default.grow_tree(pos, is_apple_tree)`
628   * Grows a mgv6 tree or apple tree at pos
629
630  * `default.grow_jungle_tree(pos)`
631   * Grows a mgv6 jungletree at pos
632
633  * `default.grow_pine_tree(pos)`
634   * Grows a mgv6 pinetree at pos
635
636  * `default.grow_new_apple_tree(pos)`
637   * Grows a new design apple tree at pos
638
639  * `default.grow_new_jungle_tree(pos)`
640   * Grows a new design jungle tree at pos
641
642  * `default.grow_new_pine_tree(pos)`
643   * Grows a new design pine tree at pos
644
645  * `default.grow_new_acacia_tree(pos)`
646   * Grows a new design acacia tree at pos
647
648  * `default.grow_new_aspen_tree(pos)`
649   * Grows a new design aspen tree at pos
650
651  * `default.grow_new_snowy_pine_tree(pos)`
652   * Grows a new design snowy pine tree at pos
653
654 Carts
655 -----
656
657         carts.register_rail(
658                 "mycarts:myrail", -- Rail name
659                 nodedef,          -- standard nodedef
660                 railparams        -- rail parameter struct (optional)
661         )
662
663         railparams = {
664                 on_step(obj, dtime), -- Event handler called when
665                                      -- cart is on rail
666                 acceleration, -- integer acceleration factor (negative
667                               -- values to brake)
668         }
669
670         The event handler is called after all default calculations
671         are made, so the custom on_step handler can override things
672         like speed, acceleration, player attachment. The handler will
673         likely be called many times per second, so the function needs
674         to make sure that the event is handled properly.