I consider trees_lib mostly ready for usage now. trees_lib will register all the nodes typically required for a tree (trunk, wood, leaves, sapling, fruit), create an abm to let saplings grow and actually turn the sapling into a tree using a variety of methods. It's an easy way to add a new tree to a game and to react to things tree-like. The mod does not create any trees at mapgen time.

Please see
trees_lib/trees.lua, which is based on
default/trees.lua for an example as to how it can be applied to the trees and related nodes from the default mod.

Another example can be found in
trees_lib/example_tree.lua. The example tree registered there demonstrates the diffrent growing methods supported by the mod: On grass or dirt, it will grow like the old apple tree (using a function); on desert sand, it'll grow like the acacia tree (using a schematic from a file); on normal sand, it'll grow like the banana tree from valleys mapgen (using a schematic in table format); on other types of soil, it'll grow like one of the birches from moretrees (randomly selected); and on group:stone, it'll be a very unhappy tree and use a function that places a very small tree. Other grounds like the mese lamp above lead to the sapling turning into dry shrub (which becomes a renewable ressource with this mod).
The absolute minimum to register a new tree would be calling
trees_lib.register_tree( "treename" ) from somewhere withhin your mod. You'd then have to create the following textures:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
MODNAME/textures/MODNAME_TREENAME_tree_top.png
MODNAME/textures/MODNAME_TREENAME_tree.png
MODNAME/textures/MODNAME_TREENAME_wood.png
MODNAME/textures/MODNAME_TREENAME_sapling.png
MODNAME/textures/MODNAME_TREENAME_leaves.png
MODNAME/textures/MODNAME_TREENAME_fruit.png
and, of course, your mod needs to depend on trees_lib. The register_tree function accepts quite a lot of further parameters:
trees_lib.register_tree(tree_name, nodes, growing_methods, grows_on_node_type_list, can_grow_function, select_how_to_grow_function, interval, chance
)- tree_name needs to be unique withhin each mod, but not necessarily withhin the entire game. This parameter is required.
- nodes may contain further information about the nodes the tree is composed of (tree, wood, leaves, leaves2, leaves3, leaves4, leaves5, sapling and fruit). Specify everything you want to override here (i.e. diffrent node name, textures, drawtype, ...). Specify node_name = "air" (see trees.lua) if you don't want a particular node to be created (mostly used for trees that have no fruit).
- growing_methods is a list of how the sapling can be grown into a full tree. Each entry needs exactly one of the following entries:
- use_function = function_to_grow_the_sapling
- or use_schematic = path_to_the_schematic_file
- or use_schematic = table_containing_the_schematic
- or use_lsystem = table_containing_lsystem_growth_data
In addition, the following values are usually needed: xoff = x_offset, zoff = z_offset, yoff = y_offset (how deep burried) and height = height (total height of the tree). These values describe which area the Voxelmanip needs to load or how far away from the sapling's position the schematic ought to be placed. - grows_on_node_type_list is a list of nodenames (or groups in the form of i.e "group:stone") on which the sapling will grow.
- can_grow_function will be called when the abm for a sapling fires: if can_grow( pos, node, ground_found ) returns 1, the tree will grow; if it returns 0, the tree will try again next time (i.e. too dark); and if it returns -1, then the tree really doesn't like this place and fails to grow, usually turning into dry shrub.
- select_how_to_grow_function can modify the way a tree will grow. It is called like this: select_how_to_grow( pos, node, growing.how_to_grow, ground_found ) The function can either return a number (thus choosing one of the growing methods from the growing_methods parameter), or its own new growing method.
- interval The interval parameter for the abm for the growing of the sapling. Default value is 10.
- chance The chance parameter for the abm for the growing of the sapling. Default value is 50.
Sometimes you may want to do something for each tree, i.e. register special furniture or craft receipes. The function
trees_lib.register_on_new_tree_type( new_tree_type_function
) exists for that purpose. Once you've registered your function
new_tree_type_function that way, your function will be called for each registered tree type once in the following way: new_tree_type_function( tree_name, mod_prefix, nodes ). See trees.lua for an example regarding crafting tree to wood. If trees have allready been registered when the new function is registered, it will be called for each known tree once, and of course for all subsequently registered trees.
trees_lib.failed_to_grow( pos, node
) is called when a tree failed to grow (i.e. because it does not like the ground it is placed on). The default action is to turn the sapling into dry shrub.
trees_lib.a_tree_has_grown( pos, node, how_to_grow
) is called whenever a tree has successfully grown. Useful i.e. for logging (see trees.lua), or for other purposes (like placing a house or lumberjack next to the newly grown tree).
trees_lib.tree_abm_grow_tree( pos, node, sapling_data, how_to_grow, force_grow
) actually grows the sapling into the tree.
A common library for trees will make it easier for other mods to handle them. Right now there are lots of diffrent implementations of trees which all boil down to the same basic mechanisms. It's a nightmare to support all these mods with their diffrent - sometimes even hidden(!) - functions for tree growing. I want to get rid of that old copy of default/trees.lua I had to keep inside mg_villages because the default trees use local functions and create their own voxelmanip area - whereas there's already one at hand at mapgen time and only the functionality for placing the tree missing.
Further advantages of this mod/library are that registration of new trees becomes easier (people often ask on the forum) - there really is no need for everyone writing his/her own code for the abm and the tree growing. And having to change each tree-related mod in order to i.e. make players be able to walk/climb through leaves or in order to change the drawtype of leaves isn't nice either. Tree registration gets easier the more these greenish things follow some sort of naming convention - which isn't bad either.
Leafdecay and group based craft receipes for fuel (so that you can burn all tree parts in a furnace) are currently not covered and up to the game to handle. Maybe leafdecay ought to be covered in the future.
Download:
https://github.com/Sokomine/trees_lib/a ... master.zipBrowse code:
https://github.com/Sokomine/trees_libVersion: 0.1 (alpha)
Licence: GPLv3 for my part/trees_lib.lua; the examples are partly from other mods
Depends on: nothing
My goal is to get trees_lib.lua to be accepted by other modders who are using trees and eventually to be part of some form of
standard library for Minetest so that all games can rely on its existence. Even if you don't like this particular implementation or would prefer to have something done differently, please make a suggestion how it could be better - and support the development of a standard library. Another part that ought to be included there is basic saving and restoring of table values into a file.