From 21af06d6335d6623ccdaa9ca569e025318f784c8 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Tue, 11 Apr 2017 22:36:28 +0200 Subject: [PATCH] moved examples into seperate mod and added LGPL license --- LICENSE.txt | 502 ++++++++++++++++++++++++ README.md | 36 +- example_tree.lua | 207 ---------- init.lua | 661 +++++++++++++++++++++++++++++++- trees.lua | 454 ---------------------- trees_lib.lua | 654 ------------------------------- trees_lib_growing_functions.lua | 139 ------- 7 files changed, 1181 insertions(+), 1472 deletions(-) create mode 100644 LICENSE.txt delete mode 100644 example_tree.lua delete mode 100644 trees.lua delete mode 100644 trees_lib.lua delete mode 100644 trees_lib_growing_functions.lua diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..e5ab03e --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,502 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/README.md b/README.md index d34bc15..98b86fe 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,36 @@ Trees are common. Yet they are all registered manually so far. And plugging in to their growing mechanism with mods is difficult. -List of files: -init.lua Loads all the needed other files (including examples) -trees_lib.lua The actual trees_lib. -trees_lib_growing_functions.lua Some tree growing functions as examples. -example_tree.lua A complex example for one tree. -trees.lua Replacement for default/trees.lua +Author: Sokomine +Contact: see https://github.com/Sokomine -Not covered in trees_lib: -* fuel receipes for the tree nodes -* crafting of tree into wood +License: LGPL (same as minetest_game) for the trees_lib from 11.04.2017 onward + (trees_lib_examples may contain code from other mods) + +List of files: + init.lua The actual trees_lib. + +If you want to see some examples, take a look at + https://github.com/Sokomine/trees_lib_examples +which contains the following additional files: + init.lua Just runs dofile on the other files. + trees_lib_growing_functions.lua Some tree growing functions as examples. + example_tree.lua A complex example for one tree. + trees.lua Replacement for default/trees.lua + +Not covered by trees_lib: +* fuel receipes for the tree, wood, leaves nodes +* crafting of tree into wood (use trees_lib.register_on_new_tree_type for those) * leafdecay If you want to test trees_lib with the default mod, do the following: 1. (optional) remove/comment out all receipes for tree trunk -> 4 wood in default/crafting.lua 2. (optional) remove/comment out all tree, wood, leaves and fruit nodes in default/nodes.lua -3. (optional) rename the file default/trees.lua -4. copy trees_lib/trees.lua to default/trees.lua + If doing so, also remove/comment out all calls of default.register_leafdecay plus all + leafdecay-related functions in default/functions.lua. +3. (optional) rename the file default/trees.lua in order to create a backup. +4. copy trees_lib_examples/trees.lua to default/trees.lua If you want to add a new tree using trees_lib, do the following: @@ -34,7 +46,7 @@ If you want to add a new tree using trees_lib, do the following: 4. Make your mod depend on trees_lib by adding a line trees_lib to MODNAME/depends.txt 5. Actually register the tree somewhere in your mod: - trees_lib.register_tree( TREENAME ) + trees_lib.register_tree( TREENAME, minetest.get_current_modname() ) All other parameters to trees_lib.register_tree are optional. diff --git a/example_tree.lua b/example_tree.lua deleted file mode 100644 index 24fea59..0000000 --- a/example_tree.lua +++ /dev/null @@ -1,207 +0,0 @@ --- --- This is an actual example of how trees_lib can be used --- - --- this is taken as an example from https://github.com/duane-r/valleys_c/blob/master/* --- Create and initialize a table for a schematic. -local function vmg_schematic_array(width, height, depth) - -- Dimensions of data array. - local s = {size={x=width, y=height, z=depth}} - s.data = {} - - for x = 0,width-1 do - for y = 0,height-1 do - for z = 0,depth-1 do - local i = x*width*height + y*width + z + 1 - s.data[i] = {} - s.data[i].name = "air" - s.data[i].param1 = 000 - end - end - end - - s.yslice_prob = {} - - return s -end - - --- this is taken as an example from https://github.com/duane-r/valleys_c/blob/master/deco_banana.lua --- A shock of leaves at the top and some fruit. -local function vmg_generate_banana_schematic(trunk_height) - local height = trunk_height + 3 - local radius = 1 - local width = 3 - local s = vmg_schematic_array(width, height, width) - - -- the main trunk - for y = 0,trunk_height do - local i = (0+radius)*width*height + y*width + (0+radius) + 1 - s.data[i].name = "trees_lib:example_tree" - s.data[i].param1 = 255 - end - - -- leaves at the top - for x = -1,1 do - for y = trunk_height+1, height-1 do - for z = -1,1 do - local i = (x+radius)*width*height + y*width + (z+radius) + 1 - if y > height - 2 then - s.data[i].name = "trees_lib:example_leaves" - if x == 0 and z == 0 then - s.data[i].param1 = 255 - else - s.data[i].param1 = 127 - end - elseif x == 0 and z == 0 then - s.data[i].name = "trees_lib:example_leaves" - s.data[i].param1 = 255 - elseif x ~= 0 or z ~= 0 then - s.data[i].name = "trees_lib:cfruit" - s.data[i].param1 = 75 - end - end - end - end - - return s -end - - --- this function allows the tree to chose between diffrent growth functions (or provide its own) -local example_tree_select_how_to_grow = function( pos, node, sapling_data_how_to_grow, ground_found ) - -- grow into a normal fruit tree on dirt or grass - if( ground_found == "default:dirt" - or ground_found == "default:dirt_with_grass" ) then - return 1; - - -- if growing on desert sand, then grow like an acacia - elseif( ground_found == "default:desert_sand" ) then - return 2; - -- on normal sand, grow like the banana tree from valleys_c - elseif( ground_found == "default:sand" ) then - return 3; - - -- on soil, grow like one of the birches from moretrees - elseif( ground_found == "group:soil" ) then - return math.random(4,5); - - -- stone is not the ideal ground to grow on... - elseif( ground_found == "group:stone" ) then - -- this shows that we can also return new tree types - return { - use_function = trees_lib.generate_unhappy_tree, - xoff = 1, zoff = 1, yoff = 0, height = 3, - }; - end -end - - ---- the actual example tree; it will grow in very diffrent ways depending on where it is placed; --- most of the node names and descriptions can be created automaticly; --- all we have to supply in such cases are the texture names -trees_lib.register_tree( "example", - { tree = { - --node_name = "trees_lib:example_tree", - --description = "example Tree", - tiles = {"default_tree_top.png^[colorize:#015dbb70", "default_tree_top.png^[colorize:#015dbb70", "default_tree.png^[colorize:#015dbb70"}, - }, wood = { - --node_name = "trees_lib:example_wood", - --description = "example Wooden Planks", - tiles = {"default_wood.png^[colorize:#015dbb70"}, - }, leaves = { - --node_name = "trees_lib:example_leaves", - --description = "example Leaves", - tiles = {"default_leaves.png^[colorize:#01ffd870"}, - special_tiles= {"default_leaves_simple.png^[colorize:#01ffd870"}, - }, sapling = { - --node_name = "trees_lib:example_sapling", - --description = "example Tree Sapling", - tiles = {"default_sapling.png^[colorize:#ff840170"}, - - --rarity = 20, - }, fruit = { - -- the fruit diverges in name and description from what would automaticly be created - node_name = "trees_lib:cfruit", - description = "Yellow Copper Fruit", - tiles = {"default_copper_lump.png^[colorize:#e3ff0070"}, - -- the fruit can be eaten - on_use = minetest.item_eat(2), - }}, - -- the diffrent ways of how a tree can be grown - { - -- version 1 - -- one of these methods will be choosen randomly - { - -- a function - like that used to create the trees/apple trees in mapgen v6 - use_function = trees_lib.generate_fruit_tree, - -- How far will the tree reach in each direction? We need to load a - -- sufficiently large voxelmanip area. - xoff = 2, zoff = 2, yoff = 0, height = 12, - }, { -- version 2 - -- schematics can be used as well - use_schematic = minetest.get_modpath("default").."/schematics/acacia_tree_from_sapling.mts", - -- TODO: determine these values automaticly - xoff = 4, zoff = 4, yoff = 0, height = 10, - -- use a schematic with diffrent nodes - use_replacements = { - {"default:acacia_tree", "trees_lib:example_tree"}, - {"default:acacia_leaves","trees_lib:example_leaves"}, - } - }, { -- version 3 - -- schematics in table form are also acceptable - use_schematic = vmg_generate_banana_schematic(3), - -- TODO: determine these values automaticly - xoff = 1, zoff = 1, yoff = 0, height = 8, - -- TODO: minetest.place_schematic does not apply replacements for tables - -- use a schematic with diffrent nodes - use_replacements = { - {"default:acacia_tree", "trees_lib:example_tree"}, - {"default:acacia_leaves","trees_lib:example_leaves"}, - } - }, { -- version 4 - -- this is moretrees.birch_model1 - use_lsystem = { - axiom="FFFFFdddccA/FFFFFFcA/FFFFFFcB", - rules_a="[&&&dddd^^ddddddd][&&&---dddd^^ddddddd][&&&+++dddd^^ddddddd][&&&++++++dddd^^ddddddd]", - rules_b="[&&&ddd^^ddddd][&&&---ddd^^ddddd][&&&+++ddd^^ddddd][&&&++++++ddd^^ddddd]", - rules_c="/", - rules_d="F", - trunk="trees_lib:example_tree", --"moretrees:birch_trunk", - leaves="trees_lib:example_leaves", --"moretrees:birch_leaves", - angle=30, - iterations=2, - random_level=0, - trunk_type="single", - thin_branches=true - } - },{ -- version 5 - -- this is moretrees.birch_model2 - use_lsystem = { - axiom="FFFdddccA/FFFFFccA/FFFFFccB", - rules_a="[&&&dFFF^^FFFdd][&&&---dFFF^^FFFdd][&&&+++dFFF^^FFFdd][&&&++++++dFFF^^FFFdd]", - rules_b="[&&&dFF^^FFFd][&&&---dFFF^^FFFd][&&&+++dFF^^FFFd][&&&++++++dFF^^FFFd]", - rules_c="/", - rules_d="F", - trunk="trees_lib:example_tree", --"moretrees:birch_trunk", - leaves="trees_lib:example_leaves", --"moretrees:birch_leaves", - angle=30, - iterations=2, - random_level=0, - trunk_type="single", - thin_branches=true - } - }, - }, - -- grows_on_node_type_list - the tree only grows on nodes of this type - {"default:cobble", "group:soil", "default:dirt", "default:dirt_with_grass", "default:desert_sand","default:sand","group:soil","group:stone"}, - -- no (additional) limits as to where the tree can grow (no can_grow_function) - nil, - -- the tree will grow in a diffrent way depending on the ground it grows on; see the function below for details - example_tree_select_how_to_grow, - -- the interval for the sapling-growing abm in seconds - 10, - -- the chance of the abm actually firing and growing the sapling (the higher the smaller) - 1 -); - diff --git a/init.lua b/init.lua index 41d6d2b..f32eca2 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,656 @@ --- the actual lib -dofile( minetest.get_modpath("trees_lib").."/trees_lib.lua") --- some examples as to how tree growing functions can be implemented -dofile( minetest.get_modpath("trees_lib").."/trees_lib_growing_functions.lua") --- as the name says: an example tree; just for documentation -dofile( minetest.get_modpath("trees_lib").."/example_tree.lua") +trees_lib = {} + +-- place this node if a sapling does not like the environment and refuses +-- to grow into a tree there +trees_lib.place_when_tree_cant_grow = {name="default:dry_shrub"}; + +----------------------------------------------------------------------------- +-- compatibility functions so that trees_lib can work without default mod +----------------------------------------------------------------------------- +trees_lib.sound_leaves = function() + if( default and default.node_sound_leaves_defaults) then + return default.node_sound_leaves_defaults(table) + else + return nil + end +end + +trees_lib.sound_wood = function() + if( default and default.node_sound_wood_defaults) then + return default.node_sound_wood_defaults(table) + else + return nil + end +end + +-- copy of default.after_place_leaves +trees_lib.after_place_leaves = function(pos, placer, itemstack, pointed_thing) + local node = minetest.get_node(pos) + node.param2 = 1 + minetest.set_node(pos, node) +end + +-- generalized function called after placing fruits +trees_lib.after_place_fruit = function(pos, placer, itemstack, pointed_thing) + if placer:is_player() then + -- find out the node name (this function is for all fruits that do not override it) + local node = minetest.get_node( pos ); + if( node and node.name) then + minetest.set_node(pos, {name = node.name, param2 = 1}) + end + end +end + + +----------------------------------------------------------------------------- +-- the various node definitions that are common to all nodes of that type +-- (unless overriden) +----------------------------------------------------------------------------- +trees_lib.node_def_tree = { + --description = "TREENAME Tree", + --tiles = nodes.tree.tiles, + paramtype2 = "facedir", + is_ground_content = false, + -- moretrees uses snappy=1 here as well + groups = {tree = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2}, + sounds = trees_lib.sound_wood, + on_place = minetest.rotate_node + } + +trees_lib.node_def_wood = { + --description = "TREENAME Wood Planks", + --tiles = nodes.wood.tiles, + is_ground_content = false, + -- moretrees uses snappy=1 here + groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 3, wood = 1}, + sounds = trees_lib.sound_wood, + } + +trees_lib.node_def_leaves = { + --description = "TREENAME Leaves", + --tiles = nodes[k].tiles, + --special_tiles = nodes[k].special_tiles, + + -- moretrees has some options for this + drawtype = "allfaces_optional", + waving = 1, + visual_scale = 1.3, + paramtype = "light", + is_ground_content = false, + -- moretrees sets moretrees_leaves=1, leafdecay = decay + groups = {snappy = 3, leafdecay = 3, flammable = 2, leaves = 1}, + sounds = trees_lib.sound_leaves, + after_place_node = trees_lib.after_place_leaves, + -- the drop part of the node definition needs to be constructed + -- for each leaf individually + } + + +trees_lib.node_def_fruit = { + --description = "TREENAME Fruit", + drawtype = "plantlike", + visual_scale = 1.0, + --tiles = nodes.fruit.tiles, + --inventory_image = nodes.fruit.tiles[1], + paramtype = "light", + sunlight_propagates = true, + walkable = false, + is_ground_content = false, + selection_box = { + type = "fixed", + fixed = {-0.2, -0.5, -0.2, 0.2, 0, 0.2} + }, + groups = {fleshy = 3, dig_immediate = 3, flammable = 2, + leafdecay = 3, leafdecay_drop = 1}, + sounds = trees_lib.sound_leaves, + -- store that this fruit has been placed manually + after_place_node = trees_lib.after_place_fruit, + -- a fruit that wants to be eatable ought to supply the following line in + -- its fruit definition: + --on_use = minetest.item_eat(food_points), + } + +trees_lib.node_def_sapling = { + --description = "TREENAME sapling", + drawtype = "plantlike", + visual_scale = 1.0, + --tiles = nodes.sapling.tiles, + --inventory_image = nodes.sapling.tiles[1], + --wield_image = nodes.sapling.tiles[1], + paramtype = "light", + sunlight_propagates = true, + walkable = false, + selection_box = { + type = "fixed", + fixed = {-0.3, -0.5, -0.3, 0.3, 0.35, 0.3} + }, + groups = {snappy = 2, dig_immediate = 3, flammable = 2, + attached_node = 1, sapling = 1}, + sounds = trees_lib.sound_leaves, + } + +----------------------------------------------------------------------------- +-- internal functions for handling identification of nodes (i.e. what is a +-- trunk, what is a leaf, which nodes can be replaced by new trees) +----------------------------------------------------------------------------- +-- those are helpful for fast lookup (either by name or content id) +trees_lib.is_tree = {} +trees_lib.is_wood = {} +trees_lib.is_leaf = {} +trees_lib.is_sapling = {} +trees_lib.is_fruit = {} +trees_lib.ignore = {} + +-- trees are allowed to replace these nodes when growing +-- (in addition to leaves, saplings and fruits from other trees) +trees_lib.ignore_list = { + "air","ignore", + "default:water_source","default:water_flowing", + "default:snow","default:ice"} + +trees_lib.build_lookup_table = function( node_name, node_type, value, allow_removal_by_other_saplings ) + if( not( node_name ) + or node_name == "" + or not( minetest.registered_nodes[ node_name ]) + or not( trees_lib[ node_type ])) then + return; + end + local id = minetest.get_content_id( node_name ); + -- we store by id and nodename both for faster lookup + trees_lib[ node_type ][ id ] = value; + trees_lib[ node_type ][ node_name ] = value; + -- if this is set, then other saplings can overwrite these nodes during + -- their growth (i.e. replacing leaves with tree trunks) + if( allow_removal_by_other_saplings ) then + trees_lib.ignore[ id ] = value; + trees_lib.ignore[ node_name ] = value; + end +end + +-- actually store the information from the ignore list +for _,v in ipairs( trees_lib.ignore_list ) do + trees_lib.build_lookup_table( v,"ignore", v, 1 ); +end + + + +----------------------------------------------------------------------------- +-- allow to call a function whenever a new tree has been registered +----------------------------------------------------------------------------- +-- contains a list of all functions that need to be called whenever a new tree +-- type is registered using trees_lib.register_tree(..) +trees_lib.register_on_new_tree_type_function_list = {} + +-- the function new_tree_type_function will be called once for each tree type; +-- use this for adding the 1 tree -> 4 wood craft receipe +trees_lib.register_on_new_tree_type = function( new_tree_type_function ) + + -- call the function for all tree types that have been registered up + -- until now + for k,v in pairs( trees_lib.is_sapling) do + new_tree_type_function( v.tree_name, v.mod_prefix, v.nodes ); + end + + -- store the function so that it will get called at all subsequent + -- registrations of new trees + table.insert( trees_lib.register_on_new_tree_type_function_list, + new_tree_type_function ); + +end + + +----------------------------------------------------------------------------- +-- mix basic node definition (see below), default constructed values (which +-- are derived from modname and treename) as fallback and user (=user of the +-- trees_lib) specified node defintion up into one node definition and +-- register the node (provided it does not exist yet) +----------------------------------------------------------------------------- +trees_lib.register_one_node = function( def_common, def_default, def_user, node_type, sapling_node_name, cid ) + if( not( def_user)) then + def_user = {}; + end + -- find out which node we are supposed to register + local node_name = def_user.node_name; + if( not( node_name )) then + node_name = def_default.node_name; + end + -- if the node is alrady registered, then abort + if( minetest.registered_nodes[ node_name ]) then + -- store the content id of this node for this type + cid[ node_type ] = minetest.get_content_id( node_name ); + def_user.node_name = node_name; + return def_user; + end + + -- create the new node definition + local node_def = {}; + -- first, add all entries from def_common + for k,v in pairs( def_common ) do + node_def[ k ] = v; + end + -- then try the default values + for k,v in pairs( def_default) do + node_def[ k ] = v; + end + -- last, apply all user supplied node definitions and thus overwrite + -- common and default definitions + for k,v in pairs( def_user ) do + node_def[ k ] = v; + end + + -- set inventory_image and wield_image for plantlike nodes + if( node_def.drawtype + and node_def.drawtype=="plantlike" + and not( node_def.inventory_image )) then + node_def.inventory_image = node_def.tiles[1]; + node_def.wield_image = node_def.tiles[1]; + end + + -- the node name does not belong into the node definition + node_def.node_name = nil; + + -- actually register the new node + minetest.register_node( node_name, node_def ); + + + -- make sure the node name gets saved + def_user.node_name = node_name; + + -- can this node (in theory) be overwritten by other trees? + local allow_overwrite = nil; + if( node_type=="leaf" or node_type=="fruit" or node_type=="sapling") then + allow_overwrite = 1; + end + -- enter all these node names into a table for fast lookup + -- (without having to resort to checking the group) + -- the trees_lib.is_sapling field will later on contain more helpful information; + -- the sapling needs to be the first node registered for this to work reliably + trees_lib.build_lookup_table( node_name, "is_"..node_type, sapling_node_name, allow_overwrite ); + + -- store the content id of the newly registered node + cid[ node_type ] = minetest.get_content_id( node_name ); + return def_user; +end + + + +----------------------------------------------------------------------------- +-- nodes for the trees: trunk, planks, leaves, sapling and fruit +----------------------------------------------------------------------------- +-- (internal function) +-- * there can be up to 5 diffrent leaves types +-- * Note: craft receipes for fuel (tree, wood, leaves) do not need to be +-- added because default already contains general craft receipes +-- based on the respective groups. +-- * Note: The craft receipe for 1 tree -> 4 wood can be done via +-- trees_lib.register_on_new_tree_type +trees_lib.register_tree_nodes = function( tree_name, mod_prefix, nodes ) + + -- gather all the relevant content ids + local cid = {}; + + local tree_name_upcase = tree_name:gsub("^%l", string.upper); + local texture_prefix = mod_prefix.."_"..tree_name; + + if( not( nodes )) then + nodes = {}; + end + + -- the sapling node name will act as a reference in the lookup table later on; + -- therefore, we need to determine how the node will be called early enough + local sapling_node_name = mod_prefix..':'..tree_name..'_sapling'; + if( nodes.sapling and nodes.sapling.node_name ) then + sapling_node_name = nodes.sapling.node_name; + end + + -- register the sapling + nodes.sapling = trees_lib.register_one_node( trees_lib.node_def_sapling, + { + node_name = sapling_node_name, + description = tree_name_upcase.." Sapling", + tiles = { texture_prefix.."_sapling.png", + }, + }, nodes.sapling, "sapling", sapling_node_name, cid ); + + + -- register the tree trunk + nodes.tree = trees_lib.register_one_node( trees_lib.node_def_tree, + { + node_name = mod_prefix..':'..tree_name..'_tree', + description = tree_name_upcase.." Tree", + tiles = { texture_prefix.."_tree_top.png", + texture_prefix.."_tree_top.png", + texture_prefix.."_tree.png", + }, + }, nodes.tree, "tree", sapling_node_name, cid ); + + -- register the wood that belongs to the tree + nodes.wood = trees_lib.register_one_node( trees_lib.node_def_wood, + { + node_name = mod_prefix..':'..tree_name..'_wood', + description = tree_name_upcase.." Planks", + tiles = { texture_prefix.."_wood.png", + }, + }, nodes.wood, "wood", sapling_node_name, cid ); + + + -- default drop rate for the sapling (1/20) + local sapling_rarity = 20; + if( nodes.sapling and nodes.sapling.rarity ) then + sapling_rarity = nodes.sapling.rarity; + end + + -- register the leaves; some trees may have more than one type of leaves (i.e. moretrees jungletrees) + local leaves_id = {'leaves','leaves2','leaves3','leaves4','leaves5'}; + for _,k in ipairs( leaves_id ) do + -- not all leaf types need to exist; we just expect at least one type of leaves to be there + if( nodes[ k ] or k=='leaves') then + -- we need to determine the node name now as leaves tend to drop themshelves sometimes + local leaves_node_name = mod_prefix..':'..tree_name..'_'..k; + if( nodes[ k ] and nodes[ k ].node_name ) then + leaves_node_name = nodes[ k ].node_name; + end + nodes[ k ] = trees_lib.register_one_node( trees_lib.node_def_leaves, + { + node_name = leaves_node_name, + description = tree_name_upcase.." Leaves", + tiles = { texture_prefix.."_"..k..".png", + }, +-- special_tiles = { texture_prefix.."_"..k.."_simple.png", }, + drop = { + max_items = 1, + items = { + { + -- player will get sapling with 1/20 chance + items = { sapling_node_name}, + rarity = sapling_rarity, + }, + { + -- player will get leaves only if he get no saplings, + -- this is because max_items is 1 + items = { leaves_node_name}, + } + } + }, + }, nodes[ k ], "leaf", sapling_node_name, cid); + + -- also store the content ids of all leaf types + cid[ k ] = cid[ "leaf" ]; + end + end + + -- register the fruit + nodes.fruit = trees_lib.register_one_node( trees_lib.node_def_fruit, + { + node_name = mod_prefix..':'..tree_name..'_fruit', + description = tree_name_upcase.." Fruit", + tiles = { texture_prefix.."_fruit.png", + }, + }, nodes.fruit, "fruit", sapling_node_name, cid ); + + -- return the ids we've gathered + return cid; +end + + + +----------------------------------------------------------------------------- +-- growing of the trees from saplings +----------------------------------------------------------------------------- +-- turn saplings that failed to grow (because they don't like the ground or +-- environment) into dry shrub, thus allowing dry shrub farming; +-- override this function if you don't like this feature +trees_lib.failed_to_grow = function( pos, node ) + minetest.set_node( pos, trees_lib.place_when_tree_cant_grow); +end + +-- this function is called just before a tree actually grows; +-- the function ought to return how_to_grow for normal tree growth; +-- returning another way of growing is also possible (i.e. diffrent +-- model when growing in flower pots/on stone) +trees_lib.change_tree_growth = function( pos, node, how_to_grow ) + return how_to_grow; +end + + +-- this function is called whenever a sapling of type node has grown +-- into a tree using how_to_grow at position pos +trees_lib.a_tree_has_grown = function( pos, node, how_to_grow ) + return; +end + +-- called by the abm running on the saplings; +-- if force is set, the tree will grow even if it usually wouldn't in that +-- environment +trees_lib.tree_abm_called = function( pos, node, active_object_count, active_object_count_wider, force_grow) + -- if we don't have further information about that sapling, then abort + if( not( node ) + or not( node.name ) + or not( trees_lib.is_sapling[ node.name ])) then + -- turn into dry shrub (because we don't know what to do with + -- this sapling) + trees_lib.failed_to_grow( pos, node ); + return; + end + + -- get information about what we're supposed to do with that sapling + local sapling_data = trees_lib.is_sapling[ node.name ]; + + -- the type of ground might be of intrest for further functions (i.e. can_grow, select_how_to_grow) + local ground_found = nil; + -- a quick check of the ground; sapling_data.grows_on has to be a list of all + -- ground types acceptable for the tree + if( not(force_grow) and sapling_data.grows_on and type( sapling_data.grows_on )=="table") then + local node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z}); + -- abort if this check cannot be done + if( not(node_under) or node_under.name=="ignore") then + return; + end + -- search all acceptable ground names + for _,g in ipairs( sapling_data.grows_on ) do + if( g==node_under.name ) then + ground_found = g; + elseif( not( ground_found) + and string.sub(g,1,6)=="group:" + and minetest.get_item_group( node_under.name, string.sub(g,7))~=0 ) then + ground_found = g; + end + end + -- abort if the tree does not like this type of ground + if( not( ground_found )) then + -- trun into dry shrub + trees_lib.failed_to_grow( pos, node ); + return; + end + end + + -- the tree may come with a more complex function that checks if it can grow there + if( not(force_grow) and sapling_data.can_grow and type( sapling_data.can_grow)=="function" ) then + -- the parameter ground_found is nil if the tree did not specify any demands for a particular ground + local ret = sapling_data.can_grow( pos, node, ground_found ); + if( ret < 1 ) then + -- trun into dry shrub if growing failed finally (and was not just delayed) + if( ret==0 ) then + trees_lib.failed_to_grow( pos, node, ground_found ); + end + return; + end + end + + -- each tree can have several ways of growing + local how_to_grow = nil; + -- the sapling may - depending on the circumstances - choose a specific growth function + -- instead of a random one + if( sapling_data.select_how_to_grow and type( sapling_data.select_how_to_grow)=="function") then + -- ground_found is nil if the tree did not specify any demands for a particular ground + how_to_grow = sapling_data.select_how_to_grow( pos, node, sapling_data.how_to_grow, ground_found ); + -- the select_how_to_grow function may either return a table or a number indicating which + -- growth method to select + if( how_to_grow and type(how_to_grow)=="number" and sapling_data.how_to_grow[ how_to_grow ]) then + how_to_grow = sapling_data.how_to_grow[ how_to_grow ]; + end + else -- else select a random one + how_to_grow = sapling_data.how_to_grow[ math.random( 1, #sapling_data.how_to_grow )]; + end + + -- this function may change the way the tree grows (i.e. select a special method for trees growing in flower pots or on stone ground) + how_to_grow = trees_lib.change_tree_growth( pos, node, how_to_grow ); + + -- actually grow the tree in a seperate function + trees_lib.tree_abm_grow_tree( pos, node, sapling_data, how_to_grow, force_grow ); +end + + +-- actually grow the tree from a sapling +trees_lib.tree_abm_grow_tree = function( pos, node, sapling_data, how_to_grow, force_grow ) + -- abort if no way was found to grow the tree + if( not( how_to_grow ) or type(how_to_grow)~="table") then + trees_lib.failed_to_grow( pos, node ); + return; + + -- grow the tree using a function (like the old apple trees did) + elseif( how_to_grow.use_function and type(how_to_grow.use_function)=="function") then + + -- get the voxelmanip data + local vm = minetest.get_voxel_manip() + local minp, maxp = vm:read_from_map( + {x = pos.x - how_to_grow.xoff, y = pos.y - how_to_grow.yoff, z = pos.z - how_to_grow.zoff}, + {x = pos.x + how_to_grow.xoff, y = pos.y + how_to_grow.height, z = pos.z + how_to_grow.zoff} + ) + local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp}) + local data = vm:get_data() + + how_to_grow.use_function( data, a, pos, sapling_data, how_to_grow.extra_params ); + + -- write the data back + vm:set_data(data) + vm:write_to_map() + vm:update_map() + + + -- grow the tree using a schematic + elseif( how_to_grow.use_schematic + and (type(how_to_grow.use_schematic)=="string" + or type(how_to_grow.use_schematic)=="table")) then + -- TODO: use voxelmanip + -- remove the sapling + minetest.set_node( pos, {name="air"}); + -- TODO: determine xoff, yoff and zoff when registering the tree + -- (if yoff is not given, then use 0) + minetest.place_schematic( + {x = pos.x - how_to_grow.xoff, + y = pos.y - how_to_grow.yoff, + z = pos.z - how_to_grow.zoff}, + how_to_grow.use_schematic, -- full path to the .mts file + "random", -- rotation + how_to_grow.use_replacements, -- use the same schematic for diffrent trees + false -- no overwriting of existing nodes + ); + + -- grow the tree using L-system + elseif( how_to_grow.use_lsystem and type(how_to_grow.use_lsystem)=="table") then + -- remove the sapling + minetest.set_node( pos, {name="air"}); + -- spawn the l-system tree + minetest.spawn_tree(pos, how_to_grow.use_lsystem ); + + -- else prevent call of success function below + else + return; + end + + trees_lib.a_tree_has_grown( pos, node, how_to_grow ); +end + + +----------------------------------------------------------------------------- +-- register a new tree +----------------------------------------------------------------------------- +trees_lib.register_tree = function( tree_name, nodes, growing_methods, grows_on_node_type_list, can_grow_function, select_how_to_grow_function, interval, chance ) + -- the tree name is the minimum needed in order to register a new tree + if( not( tree_name )) then + return; + end + + -- we may have to register nodes - and those have to be registered under the name of whichever mod called this function and is the "current" mod + local mod_prefix = ""; + -- set it to the current mod - this cannot be done in the line above as that would set mod_prefix to trees_lib and not to the calling mod + mod_prefix = minetest.get_current_modname(); + + + -- register tree trunk, wood, leaves and fruit (provided they are not defined yet) + local cid_list = trees_lib.register_tree_nodes( tree_name, mod_prefix, nodes ); + + -- a sapling will be needed for growing the tree + if( not( nodes.sapling ) + or not( nodes.sapling.node_name ) + or not( growing_methods )) then + return; + end + + -- store information about the new tree type in the sapling table + trees_lib.is_sapling[ nodes.sapling.node_name ] = { + + -- minetest.get_content_id for tree, wood, leaves1..n, sapling, fruit + cid = cid_list, + + -- node name of the sapling + sapling = nodes.sapling.node_name, + + -- values passed on to all functions registered via + -- trees_lib.register_on_new_tree_type = function( new_tree_type_function ) + tree_name = tree_name, + mod_prefix = mod_prefix, + nodes = nodes, + + -- list of node names (can contain groups, i.e. "group:soil") + -- on which the sapling will grow; + -- note: the parameter ground_found to the functions below can only be + -- passed on if grows_on has been specified (else the sapling does + -- not do any ground checks on its own) + grows_on = grows_on_node_type_list, + + -- are all the requirements met for growing at pos? + -- sapling will only grow if + -- growing.can_grow( pos, node, ground_found ) + -- returns a value >0; if 0 is returned, the sapling will fail to grow + -- and be turned into dry shrub; if a value <0 is returned, nothing will + -- happen (the sapling can try again later on) + -- (usful for i.e. requiring water nearby, or other + -- more complex requirements) + can_grow = can_grow_function, + + -- has to be either nil (for selecting a random way) + -- or return a specific growth function like the ones in + -- the list how_to_grow (see below) when called with + -- growing.select_how_to_grow( pos, node, growing.how_to_grow, ground_found ) + select_how_to_grow = select_how_to_grow_function, + + -- list of all methods that can turn the sapling into a + -- tree; can be a function, a file name containing a schematic + -- or a table for L-system trees; + -- this table/list is REQUIRED + how_to_grow = growing_methods, + }; + + -- a new tree was registered - call all functions that want to be told about new trees + for i,new_tree_type_function in ipairs( trees_lib.register_on_new_tree_type_function_list ) do + new_tree_type_function( tree_name, mod_prefix, nodes ); + end + + -- set default values for the tree-growing abm if none are set + if( not( interval)) then + interval = 10; + end + if( not( chance )) then + chance = 50; + end + -- now add the abm that lets the tree grow + minetest.register_abm({ + nodenames = { nodes.sapling.node_name }, + interval = interval, + chance = chance, + action = trees_lib.tree_abm_called, + }); +end diff --git a/trees.lua b/trees.lua deleted file mode 100644 index c3521f1..0000000 --- a/trees.lua +++ /dev/null @@ -1,454 +0,0 @@ --- --- Grow trees from saplings --- - --- add craft receipes for turning 1 tree into 4 corresponding wood; --- this function will be called for each registered tree -local default_craft_wood_from_tree = function( tree_name, mod_prefix, nodes ) - if( not( nodes ) - or not( nodes.tree ) or not( nodes.tree.node_name ) - or not( nodes.wood ) or not( nodes.wood.node_name )) then - return; - end - - minetest.register_craft({ - -- the amount of wood given might be a global config variable - output = nodes.wood.node_name..' 5', - recipe = { - { nodes.tree.node_name }, - } - }); -end - -trees_lib.register_on_new_tree_type( default_craft_wood_from_tree ); - - - --- 'Can grow' function - -local random = math.random - -local function can_grow(pos) - local ll = minetest.get_node_light(pos) - -- return -1 - no final abort, just don't grow yet - if not ll or ll < 13 then -- Minimum light level for growth - return -1 -- matches grass, wheat and cotton - end - return 1; -- the tree can grow -end - - - --- default trees grow using a function if they detect mapgen v6 - and a schematic otherwise -local default_select_how_to_grow = function( pos, node, how_to_grow, ground_found ) - local mapgen = minetest.get_mapgen_params().mgname - if mapgen == "v6" then - -- select growing method 1 (previously set to a function) - return 1; - else - -- select growing method 2 (previously set to a schematic) - return 2; - end -end - - --- log successful growth; for that, we override the a_tree_has_grown function -local old_a_tree_has_grown = trees_lib.a_tree_has_grown; -trees_lib.a_tree_has_grown = function( pos, node, how_to_grow ) - minetest.log("action", "A "..tostring( node.name ).." grows into a tree at ".. - minetest.pos_to_string(pos)) - old_a_tree_has_grown( pos, node, how_to_grow ); -end - - --- --- Tree generation --- - --- Apple tree and jungle tree trunk and leaves function - -local function add_trunk_and_leaves(data, a, pos, tree_cid, leaves_cid, - height, size, iters, is_apple_tree) - local x, y, z = pos.x, pos.y, pos.z - local c_air = minetest.get_content_id("air") - local c_ignore = minetest.get_content_id("ignore") - local c_apple = minetest.get_content_id("default:apple") - - -- Trunk - data[a:index(x, y, z)] = tree_cid -- Force-place lowest trunk node to replace sapling - for yy = y + 1, y + height - 1 do - local vi = a:index(x, yy, z) - local node_id = data[vi] - if node_id == c_air or node_id == c_ignore or node_id == leaves_cid then - data[vi] = tree_cid - end - end - - -- Force leaves near the trunk - for z_dist = -1, 1 do - for y_dist = -size, 1 do - local vi = a:index(x - 1, y + height + y_dist, z + z_dist) - for x_dist = -1, 1 do - if data[vi] == c_air or data[vi] == c_ignore then - if is_apple_tree and random(1, 8) == 1 then - data[vi] = c_apple - else - data[vi] = leaves_cid - end - end - vi = vi + 1 - end - end - end - - -- Randomly add leaves in 2x2x2 clusters. - for i = 1, iters do - local clust_x = x + random(-size, size - 1) - local clust_y = y + height + random(-size, 0) - local clust_z = z + random(-size, size - 1) - - for xi = 0, 1 do - for yi = 0, 1 do - for zi = 0, 1 do - local vi = a:index(clust_x + xi, clust_y + yi, clust_z + zi) - if data[vi] == c_air or data[vi] == c_ignore then - if is_apple_tree and random(1, 8) == 1 then - data[vi] = c_apple - else - data[vi] = leaves_cid - end - end - end - end - end - end -end - - --- Apple tree - -function default.grow_apple_tree( data, a, pos, sapling_data, extra_params ) - - -- translate parameter names - local c_tree = sapling_data.cid.tree; - local c_leaves = sapling_data.cid.leaves; - -- about every 4th tree is an apple tree - local is_apple_tree = (random(1, 4) == 1); - local height = random(4, 5) - - -- call the actual tree generation function - add_trunk_and_leaves(data, a, pos, c_tree, c_leaves, height, 2, 8, is_apple_tree) -end - - --- Jungle tree - -function default.grow_jungle_tree(data, a, pos, sapling_data, extra_params ) - --[[ - NOTE: Jungletree-placing code is currently duplicated in the engine - and in games that have saplings; both are deprecated but not - replaced yet - --]] - - -- translate parameter names - local c_jungletree = sapling_data.cid.tree; - local c_jungleleaves = sapling_data.cid.leaves; - - local height = random(8, 12) - - add_trunk_and_leaves(data, a, pos, c_jungletree, c_jungleleaves, height, 3, 30, false) - - -- further parameters for the roots - local x, y, z = pos.x, pos.y, pos.z - local c_air = minetest.get_content_id("air") - local c_ignore = minetest.get_content_id("ignore") - -- Roots - for z_dist = -1, 1 do - local vi_1 = a:index(x - 1, y - 1, z + z_dist) - local vi_2 = a:index(x - 1, y, z + z_dist) - for x_dist = -1, 1 do - if random(1, 3) >= 2 then - if data[vi_1] == c_air or data[vi_1] == c_ignore then - data[vi_1] = c_jungletree - elseif data[vi_2] == c_air or data[vi_2] == c_ignore then - data[vi_2] = c_jungletree - end - end - vi_1 = vi_1 + 1 - vi_2 = vi_2 + 1 - end - end -end - - --- Pine tree from mg mapgen mod, design by sfan5, pointy top added by paramat - -local function add_pine_needles(data, vi, c_air, c_ignore, c_snow, c_pine_needles) - local node_id = data[vi] - if node_id == c_air or node_id == c_ignore or node_id == c_snow then - data[vi] = c_pine_needles - end -end - -local function add_snow(data, vi, c_air, c_ignore, c_snow) - local node_id = data[vi] - if node_id == c_air or node_id == c_ignore then - data[vi] = c_snow - end -end - -function default.grow_pine_tree(data, a, pos, sapling_data, extra_params ) - - -- translate parameter names - local c_pine_tree = sapling_data.cid.tree; - local c_pine_needles = sapling_data.cid.leaves; - - -- other internal parameters - local x, y, z = pos.x, pos.y, pos.z - local maxy = y + random(9, 13) -- Trunk top - - local c_air = minetest.get_content_id("air") - local c_ignore = minetest.get_content_id("ignore") - local c_snow = minetest.get_content_id("default:snow") - local c_snowblock = minetest.get_content_id("default:snowblock") - local c_dirtsnow = minetest.get_content_id("default:dirt_with_snow") - - -- Scan for snow nodes near sapling to enable snow on branches - local snow = false - for yy = y - 1, y + 1 do - for zz = z - 1, z + 1 do - local vi = a:index(x - 1, yy, zz) - for xx = x - 1, x + 1 do - local nodid = data[vi] - if nodid == c_snow or nodid == c_snowblock or nodid == c_dirtsnow then - snow = true - end - vi = vi + 1 - end - end - end - - -- Upper branches layer - local dev = 3 - for yy = maxy - 1, maxy + 1 do - for zz = z - dev, z + dev do - local vi = a:index(x - dev, yy, zz) - local via = a:index(x - dev, yy + 1, zz) - for xx = x - dev, x + dev do - if random() < 0.95 - dev * 0.05 then - add_pine_needles(data, vi, c_air, c_ignore, c_snow, - c_pine_needles) - if snow then - add_snow(data, via, c_air, c_ignore, c_snow) - end - end - vi = vi + 1 - via = via + 1 - end - end - dev = dev - 1 - end - - -- Centre top nodes - add_pine_needles(data, a:index(x, maxy + 1, z), c_air, c_ignore, c_snow, - c_pine_needles) - add_pine_needles(data, a:index(x, maxy + 2, z), c_air, c_ignore, c_snow, - c_pine_needles) -- Paramat added a pointy top node - if snow then - add_snow(data, a:index(x, maxy + 3, z), c_air, c_ignore, c_snow) - end - - -- Lower branches layer - local my = 0 - for i = 1, 20 do -- Random 2x2 squares of needles - local xi = x + random(-3, 2) - local yy = maxy + random(-6, -5) - local zi = z + random(-3, 2) - if yy > my then - my = yy - end - for zz = zi, zi+1 do - local vi = a:index(xi, yy, zz) - local via = a:index(xi, yy + 1, zz) - for xx = xi, xi + 1 do - add_pine_needles(data, vi, c_air, c_ignore, c_snow, - c_pine_needles) - if snow then - add_snow(data, via, c_air, c_ignore, c_snow) - end - vi = vi + 1 - via = via + 1 - end - end - end - - local dev = 2 - for yy = my + 1, my + 2 do - for zz = z - dev, z + dev do - local vi = a:index(x - dev, yy, zz) - local via = a:index(x - dev, yy + 1, zz) - for xx = x - dev, x + dev do - if random() < 0.95 - dev * 0.05 then - add_pine_needles(data, vi, c_air, c_ignore, c_snow, - c_pine_needles) - if snow then - add_snow(data, via, c_air, c_ignore, c_snow) - end - end - vi = vi + 1 - via = via + 1 - end - end - dev = dev - 1 - end - - -- Trunk - data[a:index(x, y, z)] = c_pine_tree -- Force-place lowest trunk node to replace sapling - for yy = y + 1, maxy do - local vi = a:index(x, yy, z) - local node_id = data[vi] - if node_id == c_air or node_id == c_ignore or - node_id == c_pine_needles or node_id == c_snow then - data[vi] = c_pine_tree - end - end -end - - - - --- --- Trees --- - --- the normal tree does not have a special name and no space after it -trees_lib.register_tree( "normal", - { tree = { - node_name = "default:tree", - tiles = {"default_tree_top.png", "default_tree_top.png", "default_tree.png"}, - paramtype2 = "facedir", - }, wood = { - node_name = "default:wood", - description = "Wooden Planks", - tiles = {"default_wood.png"}, - }, sapling = { - node_name = "default:sapling", - description = "Sapling", - tiles = {"default_sapling.png"}, - }, leaves = { - node_name = "default:leaves", - description = "Leaves", - tiles = {"default_leaves.png"}, - special_tiles = {"default_leaves_simple.png"}, - }, fruit = { - node_name = "default:apple", - description = "Apple", - tiles = {"default_apple.png"}, - on_use = minetest.item_eat(2), - }}, - -- growing methods: - { - { -- the first growing method uses a function - use_function = default.grow_apple_tree, - xoff = 2, zoff = 2, yoff = 8, height = 15, - }, { -- the second growing method (when mapgen is not v6) uses a schematic - use_schematic = minetest.get_modpath("default") .. "/schematics/apple_tree_from_sapling.mts", - xoff = 2, zoff = 2, yoff = 1, height = 10, - } - }, - -- grows on nodes of this type: - {"group:soil"}, - -- can_grow_function: (in this case, checks if there is enough light) - can_grow, - -- select_how_to_grow_function: - default_select_how_to_grow, - -- interval (for the abm) - 10, - -- chance (for the abm) - 50 - ); - --- the jungletree has no space between tree name and fruther parts of the name -trees_lib.register_tree( "jungle", - { tree = { - node_name = "default:jungletree", - description = "Jungle Tree", - tiles = {"default_jungletree_top.png", "default_jungletree_top.png", - "default_jungletree.png"}, - }, wood = { - node_name = "default:junglewood", - description = "Junglewood Planks", - tiles = {"default_junglewood.png"}, - }, sapling = { - node_name = "default:junglesapling", - description = "Jungle Sapling", - tiles = {"default_junglesapling.png"}, - }, leaves = { - node_name = "default:jungleleaves", - description = "Jungle Leaves", - tiles = {"default_jungleleaves.png"}, - special_tiles = {"default_jungleleaves_simple.png"}, - -- the jungletree has no fruit - }, fruit = { - node_name = "air", - }}, - { { -- the first growing method uses a function - use_function = default.grow_jungle_tree, - xoff = 3, zoff = 3, yoff = 1, height = 15, - },{ -- new jungle tree (grown from schematic) - use_schematic = minetest.get_modpath("default") .. "/schematics/jungle_tree_from_sapling.mts", - xoff = 2, zoff = 2, yoff = 1, height = 10, - } - }, - {"group:soil"}, - can_grow, - default_select_how_to_grow, - 10, 50); - - --- the pine tree mostly follows naming conventions -trees_lib.register_tree( "pine", - -- ...except for the leaves, which are needles - { leaves = { - node_name = "default:pine_needles", - description = "Pine Needles", - tiles = {"default_pine_needles.png"}, - -- the pine tree also has no fruit - }, fruit = { - node_name = "air", - }}, - { - { -- the old pine tree (in mapgen v6) grows using a function - use_function = default.grow_pine_tree, - -- we need to know how much space will have to be loaded into voxelmanip - xoff = 3, zoff = 3, yoff = 1, height = 18, - }, { -- new pine tree (grown from schematic) - use_schematic = minetest.get_modpath("default") .. "/schematics/pine_tree_from_sapling.mts", - xoff = 2, zoff = 2, yoff = 1, height = 10, - } - }, - {"group:soil"}, - can_grow, - default_select_how_to_grow, - 10, 50); - - --- the acacia tree follows naming conventions -trees_lib.register_tree( "acacia", - -- the acacia tree has no fruit - { fruit = { - node_name = "air", - }}, - -- the acacia only knows to grow from a schematic - { - { -- new acacia tree (grown from schematic) - use_schematic = minetest.get_modpath("default") .. "/schematics/acacia_tree_from_sapling.mts", - xoff = 4, zoff = 4, yoff = 1, height = 10, - } - }, - {"group:soil","group:sand"}, - can_grow, - -- the acacia only has the schematic version - there is no other growing method to select - nil, - 10, 50); - diff --git a/trees_lib.lua b/trees_lib.lua deleted file mode 100644 index 8d6dd60..0000000 --- a/trees_lib.lua +++ /dev/null @@ -1,654 +0,0 @@ - -trees_lib = {} - --- place this node if a sapling does not like the environment and refuses --- to grow into a tree there -trees_lib.place_when_tree_cant_grow = {name="default:dry_shrub"}; - ------------------------------------------------------------------------------ --- compatibility functions so that trees_lib can work without default mod ------------------------------------------------------------------------------ -trees_lib.sound_leaves = function() - if( default and default.node_sound_leaves_defaults) then - return default.node_sound_leaves_defaults(table) - else - return nil - end -end - -trees_lib.sound_wood = function() - if( default and default.node_sound_wood_defaults) then - return default.node_sound_wood_defaults(table) - else - return nil - end -end - --- copy of default.after_place_leaves -trees_lib.after_place_leaves = function(pos, placer, itemstack, pointed_thing) - local node = minetest.get_node(pos) - node.param2 = 1 - minetest.set_node(pos, node) -end - --- generalized function called after placing fruits -trees_lib.after_place_fruit = function(pos, placer, itemstack, pointed_thing) - if placer:is_player() then - -- find out the node name (this function is for all fruits that do not override it) - local node = minetest.get_node( pos ); - if( node and node.name) then - minetest.set_node(pos, {name = node.name, param2 = 1}) - end - end -end - - ------------------------------------------------------------------------------ --- the various node definitions that are common to all nodes of that type --- (unless overriden) ------------------------------------------------------------------------------ -trees_lib.node_def_tree = { - --description = "TREENAME Tree", - --tiles = nodes.tree.tiles, - paramtype2 = "facedir", - is_ground_content = false, - -- moretrees uses snappy=1 here as well - groups = {tree = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2}, - sounds = trees_lib.sound_wood, - on_place = minetest.rotate_node - } - -trees_lib.node_def_wood = { - --description = "TREENAME Wood Planks", - --tiles = nodes.wood.tiles, - is_ground_content = false, - -- moretrees uses snappy=1 here - groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 3, wood = 1}, - sounds = trees_lib.sound_wood, - } - -trees_lib.node_def_leaves = { - --description = "TREENAME Leaves", - --tiles = nodes[k].tiles, - --special_tiles = nodes[k].special_tiles, - - -- moretrees has some options for this - drawtype = "allfaces_optional", - waving = 1, - visual_scale = 1.3, - paramtype = "light", - is_ground_content = false, - -- moretrees sets moretrees_leaves=1, leafdecay = decay - groups = {snappy = 3, leafdecay = 3, flammable = 2, leaves = 1}, - sounds = trees_lib.sound_leaves, - after_place_node = trees_lib.after_place_leaves, - -- the drop part of the node definition needs to be constructed - -- for each leaf individually - } - - -trees_lib.node_def_fruit = { - --description = "TREENAME Fruit", - drawtype = "plantlike", - visual_scale = 1.0, - --tiles = nodes.fruit.tiles, - --inventory_image = nodes.fruit.tiles[1], - paramtype = "light", - sunlight_propagates = true, - walkable = false, - is_ground_content = false, - selection_box = { - type = "fixed", - fixed = {-0.2, -0.5, -0.2, 0.2, 0, 0.2} - }, - groups = {fleshy = 3, dig_immediate = 3, flammable = 2, - leafdecay = 3, leafdecay_drop = 1}, - sounds = trees_lib.sound_leaves, - -- store that this fruit has been placed manually - after_place_node = trees_lib.after_place_fruit, - -- a fruit that wants to be eatable ought to supply the following line in - -- its fruit definition: - --on_use = minetest.item_eat(food_points), - } - -trees_lib.node_def_sapling = { - --description = "TREENAME sapling", - drawtype = "plantlike", - visual_scale = 1.0, - --tiles = nodes.sapling.tiles, - --inventory_image = nodes.sapling.tiles[1], - --wield_image = nodes.sapling.tiles[1], - paramtype = "light", - sunlight_propagates = true, - walkable = false, - selection_box = { - type = "fixed", - fixed = {-0.3, -0.5, -0.3, 0.3, 0.35, 0.3} - }, - groups = {snappy = 2, dig_immediate = 3, flammable = 2, - attached_node = 1, sapling = 1}, - sounds = trees_lib.sound_leaves, - } - ------------------------------------------------------------------------------ --- internal functions for handling identification of nodes (i.e. what is a --- trunk, what is a leaf, which nodes can be replaced by new trees) ------------------------------------------------------------------------------ --- those are helpful for fast lookup (either by name or content id) -trees_lib.is_tree = {} -trees_lib.is_wood = {} -trees_lib.is_leaf = {} -trees_lib.is_sapling = {} -trees_lib.is_fruit = {} -trees_lib.ignore = {} - --- trees are allowed to replace these nodes when growing --- (in addition to leaves, saplings and fruits from other trees) -trees_lib.ignore_list = { - "air","ignore", - "default:water_source","default:water_flowing", - "default:snow","default:ice"} - -trees_lib.build_lookup_table = function( node_name, node_type, value, allow_removal_by_other_saplings ) - if( not( node_name ) - or node_name == "" - or not( minetest.registered_nodes[ node_name ]) - or not( trees_lib[ node_type ])) then - return; - end - local id = minetest.get_content_id( node_name ); - -- we store by id and nodename both for faster lookup - trees_lib[ node_type ][ id ] = value; - trees_lib[ node_type ][ node_name ] = value; - -- if this is set, then other saplings can overwrite these nodes during - -- their growth (i.e. replacing leaves with tree trunks) - if( allow_removal_by_other_saplings ) then - trees_lib.ignore[ id ] = value; - trees_lib.ignore[ node_name ] = value; - end -end - --- actually store the information from the ignore list -for _,v in ipairs( trees_lib.ignore_list ) do - trees_lib.build_lookup_table( v,"ignore", v, 1 ); -end - - - ------------------------------------------------------------------------------ --- allow to call a function whenever a new tree has been registered ------------------------------------------------------------------------------ --- contains a list of all functions that need to be called whenever a new tree --- type is registered using trees_lib.register_tree(..) -trees_lib.register_on_new_tree_type_function_list = {} - --- the function new_tree_type_function will be called once for each tree type; --- use this for adding the 1 tree -> 4 wood craft receipe -trees_lib.register_on_new_tree_type = function( new_tree_type_function ) - - -- call the function for all tree types that have been registered up - -- until now - for k,v in pairs( trees_lib.is_sapling) do - new_tree_type_function( v.tree_name, v.mod_prefix, v.nodes ); - end - - -- store the function so that it will get called at all subsequent - -- registrations of new trees - table.insert( trees_lib.register_on_new_tree_type_function_list, - new_tree_type_function ); - -end - - ------------------------------------------------------------------------------ --- mix basic node definition (see below), default constructed values (which --- are derived from modname and treename) as fallback and user (=user of the --- trees_lib) specified node defintion up into one node definition and --- register the node (provided it does not exist yet) ------------------------------------------------------------------------------ -trees_lib.register_one_node = function( def_common, def_default, def_user, node_type, sapling_node_name, cid ) - if( not( def_user)) then - def_user = {}; - end - -- find out which node we are supposed to register - local node_name = def_user.node_name; - if( not( node_name )) then - node_name = def_default.node_name; - end - -- if the node is alrady registered, then abort - if( minetest.registered_nodes[ node_name ]) then - -- store the content id of this node for this type - cid[ node_type ] = minetest.get_content_id( node_name ); - def_user.node_name = node_name; - return def_user; - end - - -- create the new node definition - local node_def = {}; - -- first, add all entries from def_common - for k,v in pairs( def_common ) do - node_def[ k ] = v; - end - -- then try the default values - for k,v in pairs( def_default) do - node_def[ k ] = v; - end - -- last, apply all user supplied node definitions and thus overwrite - -- common and default definitions - for k,v in pairs( def_user ) do - node_def[ k ] = v; - end - - -- set inventory_image and wield_image for plantlike nodes - if( node_def.drawtype - and node_def.drawtype=="plantlike" - and not( node_def.inventory_image )) then - node_def.inventory_image = node_def.tiles[1]; - node_def.wield_image = node_def.tiles[1]; - end - - -- the node name does not belong into the node definition - node_def.node_name = nil; - - -- actually register the new node - minetest.register_node( node_name, node_def ); - - - -- make sure the node name gets saved - def_user.node_name = node_name; - - -- can this node (in theory) be overwritten by other trees? - local allow_overwrite = nil; - if( node_type=="leaf" or node_type=="fruit" or node_type=="sapling") then - allow_overwrite = 1; - end - -- enter all these node names into a table for fast lookup - -- (without having to resort to checking the group) - -- the trees_lib.is_sapling field will later on contain more helpful information; - -- the sapling needs to be the first node registered for this to work reliably - trees_lib.build_lookup_table( node_name, "is_"..node_type, sapling_node_name, allow_overwrite ); - - -- store the content id of the newly registered node - cid[ node_type ] = minetest.get_content_id( node_name ); - return def_user; -end - - - ------------------------------------------------------------------------------ --- nodes for the trees: trunk, planks, leaves, sapling and fruit ------------------------------------------------------------------------------ --- (internal function) --- * there can be up to 5 diffrent leaves types --- * Note: craft receipes for fuel (tree, wood, leaves) do not need to be --- added because default already contains general craft receipes --- based on the respective groups. --- * Note: The craft receipe for 1 tree -> 4 wood can be done via --- trees_lib.register_on_new_tree_type -trees_lib.register_tree_nodes = function( tree_name, mod_prefix, nodes ) - - -- gather all the relevant content ids - local cid = {}; - - local tree_name_upcase = tree_name:gsub("^%l", string.upper); - local texture_prefix = mod_prefix.."_"..tree_name; - - if( not( nodes )) then - nodes = {}; - end - - -- the sapling node name will act as a reference in the lookup table later on; - -- therefore, we need to determine how the node will be called early enough - local sapling_node_name = mod_prefix..':'..tree_name..'_sapling'; - if( nodes.sapling and nodes.sapling.node_name ) then - sapling_node_name = nodes.sapling.node_name; - end - - -- register the sapling - nodes.sapling = trees_lib.register_one_node( trees_lib.node_def_sapling, - { - node_name = sapling_node_name, - description = tree_name_upcase.." Sapling", - tiles = { texture_prefix.."_sapling.png", - }, - }, nodes.sapling, "sapling", sapling_node_name, cid ); - - - -- register the tree trunk - nodes.tree = trees_lib.register_one_node( trees_lib.node_def_tree, - { - node_name = mod_prefix..':'..tree_name..'_tree', - description = tree_name_upcase.." Tree", - tiles = { texture_prefix.."_tree_top.png", - texture_prefix.."_tree_top.png", - texture_prefix.."_tree.png", - }, - }, nodes.tree, "tree", sapling_node_name, cid ); - - -- register the wood that belongs to the tree - nodes.wood = trees_lib.register_one_node( trees_lib.node_def_wood, - { - node_name = mod_prefix..':'..tree_name..'_wood', - description = tree_name_upcase.." Planks", - tiles = { texture_prefix.."_wood.png", - }, - }, nodes.wood, "wood", sapling_node_name, cid ); - - - -- default drop rate for the sapling (1/20) - local sapling_rarity = 20; - if( nodes.sapling and nodes.sapling.rarity ) then - sapling_rarity = nodes.sapling.rarity; - end - - -- register the leaves; some trees may have more than one type of leaves (i.e. moretrees jungletrees) - local leaves_id = {'leaves','leaves2','leaves3','leaves4','leaves5'}; - for _,k in ipairs( leaves_id ) do - -- not all leaf types need to exist; we just expect at least one type of leaves to be there - if( nodes[ k ] or k=='leaves') then - -- we need to determine the node name now as leaves tend to drop themshelves sometimes - local leaves_node_name = mod_prefix..':'..tree_name..'_'..k; - if( nodes[ k ] and nodes[ k ].node_name ) then - leaves_node_name = nodes[ k ].node_name; - end - nodes[ k ] = trees_lib.register_one_node( trees_lib.node_def_leaves, - { - node_name = leaves_node_name, - description = tree_name_upcase.." Leaves", - tiles = { texture_prefix.."_"..k..".png", - }, --- special_tiles = { texture_prefix.."_"..k.."_simple.png", }, - drop = { - max_items = 1, - items = { - { - -- player will get sapling with 1/20 chance - items = { sapling_node_name}, - rarity = sapling_rarity, - }, - { - -- player will get leaves only if he get no saplings, - -- this is because max_items is 1 - items = { leaves_node_name}, - } - } - }, - }, nodes[ k ], "leaf", sapling_node_name, cid); - - -- also store the content ids of all leaf types - cid[ k ] = cid[ "leaf" ]; - end - end - - -- register the fruit - nodes.fruit = trees_lib.register_one_node( trees_lib.node_def_fruit, - { - node_name = mod_prefix..':'..tree_name..'_fruit', - description = tree_name_upcase.." Fruit", - tiles = { texture_prefix.."_fruit.png", - }, - }, nodes.fruit, "fruit", sapling_node_name, cid ); - - -- return the ids we've gathered - return cid; -end - - - ------------------------------------------------------------------------------ --- growing of the trees from saplings ------------------------------------------------------------------------------ --- turn saplings that failed to grow (because they don't like the ground or --- environment) into dry shrub, thus allowing dry shrub farming; --- override this function if you don't like this feature -trees_lib.failed_to_grow = function( pos, node ) - minetest.set_node( pos, trees_lib.place_when_tree_cant_grow); -end - --- this function is called just before a tree actually grows; --- the function ought to return how_to_grow for normal tree growth; --- returning another way of growing is also possible (i.e. diffrent --- model when growing in flower pots/on stone) -trees_lib.change_tree_growth = function( pos, node, how_to_grow ) - return how_to_grow; -end - - --- this function is called whenever a sapling of type node has grown --- into a tree using how_to_grow at position pos -trees_lib.a_tree_has_grown = function( pos, node, how_to_grow ) - return; -end - --- called by the abm running on the saplings; --- if force is set, the tree will grow even if it usually wouldn't in that --- environment -trees_lib.tree_abm_called = function( pos, node, active_object_count, active_object_count_wider, force_grow) - -- if we don't have further information about that sapling, then abort - if( not( node ) - or not( node.name ) - or not( trees_lib.is_sapling[ node.name ])) then - -- turn into dry shrub (because we don't know what to do with - -- this sapling) - trees_lib.failed_to_grow( pos, node ); - return; - end - - -- get information about what we're supposed to do with that sapling - local sapling_data = trees_lib.is_sapling[ node.name ]; - - -- the type of ground might be of intrest for further functions (i.e. can_grow, select_how_to_grow) - local ground_found = nil; - -- a quick check of the ground; sapling_data.grows_on has to be a list of all - -- ground types acceptable for the tree - if( not(force_grow) and sapling_data.grows_on and type( sapling_data.grows_on )=="table") then - local node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z}); - -- abort if this check cannot be done - if( not(node_under) or node_under.name=="ignore") then - return; - end - -- search all acceptable ground names - for _,g in ipairs( sapling_data.grows_on ) do - if( g==node_under.name ) then - ground_found = g; - elseif( not( ground_found) - and string.sub(g,1,6)=="group:" - and minetest.get_item_group( node_under.name, string.sub(g,7))~=0 ) then - ground_found = g; - end - end - -- abort if the tree does not like this type of ground - if( not( ground_found )) then - -- trun into dry shrub - trees_lib.failed_to_grow( pos, node ); - return; - end - end - - -- the tree may come with a more complex function that checks if it can grow there - if( not(force_grow) and sapling_data.can_grow and type( sapling_data.can_grow)=="function" ) then - -- the parameter ground_found is nil if the tree did not specify any demands for a particular ground - local ret = sapling_data.can_grow( pos, node, ground_found ); - if( ret < 1 ) then - -- trun into dry shrub if growing failed finally (and was not just delayed) - if( ret==0 ) then - trees_lib.failed_to_grow( pos, node, ground_found ); - end - return; - end - end - - -- each tree can have several ways of growing - local how_to_grow = nil; - -- the sapling may - depending on the circumstances - choose a specific growth function - -- instead of a random one - if( sapling_data.select_how_to_grow and type( sapling_data.select_how_to_grow)=="function") then - -- ground_found is nil if the tree did not specify any demands for a particular ground - how_to_grow = sapling_data.select_how_to_grow( pos, node, sapling_data.how_to_grow, ground_found ); - -- the select_how_to_grow function may either return a table or a number indicating which - -- growth method to select - if( how_to_grow and type(how_to_grow)=="number" and sapling_data.how_to_grow[ how_to_grow ]) then - how_to_grow = sapling_data.how_to_grow[ how_to_grow ]; - end - else -- else select a random one - how_to_grow = sapling_data.how_to_grow[ math.random( 1, #sapling_data.how_to_grow )]; - end - - -- this function may change the way the tree grows (i.e. select a special method for trees growing in flower pots or on stone ground) - how_to_grow = trees_lib.change_tree_growth( pos, node, how_to_grow ); - - -- actually grow the tree in a seperate function - trees_lib.tree_abm_grow_tree( pos, node, sapling_data, how_to_grow, force_grow ); -end - - --- actually grow the tree from a sapling -trees_lib.tree_abm_grow_tree = function( pos, node, sapling_data, how_to_grow, force_grow ) - -- abort if no way was found to grow the tree - if( not( how_to_grow ) or type(how_to_grow)~="table") then - trees_lib.failed_to_grow( pos, node ); - return; - - -- grow the tree using a function (like the old apple trees did) - elseif( how_to_grow.use_function and type(how_to_grow.use_function)=="function") then - - -- get the voxelmanip data - local vm = minetest.get_voxel_manip() - local minp, maxp = vm:read_from_map( - {x = pos.x - how_to_grow.xoff, y = pos.y - how_to_grow.yoff, z = pos.z - how_to_grow.zoff}, - {x = pos.x + how_to_grow.xoff, y = pos.y + how_to_grow.height, z = pos.z + how_to_grow.zoff} - ) - local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp}) - local data = vm:get_data() - - how_to_grow.use_function( data, a, pos, sapling_data, how_to_grow.extra_params ); - - -- write the data back - vm:set_data(data) - vm:write_to_map() - vm:update_map() - - - -- grow the tree using a schematic - elseif( how_to_grow.use_schematic - and (type(how_to_grow.use_schematic)=="string" - or type(how_to_grow.use_schematic)=="table")) then - -- TODO: use voxelmanip - -- remove the sapling - minetest.set_node( pos, {name="air"}); - -- TODO: determine xoff, yoff and zoff when registering the tree - -- (if yoff is not given, then use 0) - minetest.place_schematic( - {x = pos.x - how_to_grow.xoff, - y = pos.y - how_to_grow.yoff, - z = pos.z - how_to_grow.zoff}, - how_to_grow.use_schematic, -- full path to the .mts file - "random", -- rotation - how_to_grow.use_replacements, -- use the same schematic for diffrent trees - false -- no overwriting of existing nodes - ); - - -- grow the tree using L-system - elseif( how_to_grow.use_lsystem and type(how_to_grow.use_lsystem)=="table") then - -- remove the sapling - minetest.set_node( pos, {name="air"}); - -- spawn the l-system tree - minetest.spawn_tree(pos, how_to_grow.use_lsystem ); - - -- else prevent call of success function below - else - return; - end - - trees_lib.a_tree_has_grown( pos, node, how_to_grow ); -end - - ------------------------------------------------------------------------------ --- register a new tree ------------------------------------------------------------------------------ -trees_lib.register_tree = function( tree_name, nodes, growing_methods, grows_on_node_type_list, can_grow_function, select_how_to_grow_function, interval, chance ) - -- the tree name is the minimum needed in order to register a new tree - if( not( tree_name )) then - return; - end - - -- we may have to register nodes - and those have to be registered under the name of whichever mod called this function and is the "current" mod - local mod_prefix = minetest.get_current_modname(); - - - -- register tree trunk, wood, leaves and fruit (provided they are not defined yet) - local cid_list = trees_lib.register_tree_nodes( tree_name, mod_prefix, nodes ); - - -- a sapling will be needed for growing the tree - if( not( nodes.sapling ) - or not( nodes.sapling.node_name ) - or not( growing_methods )) then - return; - end - - -- store information about the new tree type in the sapling table - trees_lib.is_sapling[ nodes.sapling.node_name ] = { - - -- minetest.get_content_id for tree, wood, leaves1..n, sapling, fruit - cid = cid_list, - - -- node name of the sapling - sapling = nodes.sapling.node_name, - - -- values passed on to all functions registered via - -- trees_lib.register_on_new_tree_type = function( new_tree_type_function ) - tree_name = tree_name, - mod_prefix = mod_prefix, - nodes = nodes, - - -- list of node names (can contain groups, i.e. "group:soil") - -- on which the sapling will grow; - -- note: the parameter ground_found to the functions below can only be - -- passed on if grows_on has been specified (else the sapling does - -- not do any ground checks on its own) - grows_on = grows_on_node_type_list, - - -- are all the requirements met for growing at pos? - -- sapling will only grow if - -- growing.can_grow( pos, node, ground_found ) - -- returns a value >0; if 0 is returned, the sapling will fail to grow - -- and be turned into dry shrub; if a value <0 is returned, nothing will - -- happen (the sapling can try again later on) - -- (usful for i.e. requiring water nearby, or other - -- more complex requirements) - can_grow = can_grow_function, - - -- has to be either nil (for selecting a random way) - -- or return a specific growth function like the ones in - -- the list how_to_grow (see below) when called with - -- growing.select_how_to_grow( pos, node, growing.how_to_grow, ground_found ) - select_how_to_grow = select_how_to_grow_function, - - -- list of all methods that can turn the sapling into a - -- tree; can be a function, a file name containing a schematic - -- or a table for L-system trees; - -- this table/list is REQUIRED - how_to_grow = growing_methods, - }; - - -- a new tree was registered - call all functions that want to be told about new trees - for i,new_tree_type_function in ipairs( trees_lib.register_on_new_tree_type_function_list ) do - new_tree_type_function( tree_name, mod_prefix, nodes ); - end - - -- set default values for the tree-growing abm if none are set - if( not( interval)) then - interval = 10; - end - if( not( chance )) then - chance = 50; - end - -- now add the abm that lets the tree grow - minetest.register_abm({ - nodenames = { nodes.sapling.node_name }, - interval = interval, - chance = chance, - action = trees_lib.tree_abm_called, - }); -end diff --git a/trees_lib_growing_functions.lua b/trees_lib_growing_functions.lua deleted file mode 100644 index 004bec3..0000000 --- a/trees_lib_growing_functions.lua +++ /dev/null @@ -1,139 +0,0 @@ - - - ---[[ the version from mg_villages --- this is more or less the code to grow an apple tree with v6 -trees_lib.generate_fruit_tree = function(data, a, pos, is_apple_tree, seed, snow) - local leaves_type = c_leaves; - if( snow - or data[ a:index(pos.x, pos.y, pos.z) ] == c_snow - or data[ a:index(pos.x, pos.y+1, pos.z) ] == c_snow ) then - leaves_type = c_msnow_leaves2; - end - - local hight = math.random(4, 5) - for x_area = -2, 2 do - for y_area = -1, 2 do - for z_area = -2, 2 do - if math.random(1,30) < 23 then --randomize leaves - local area_l = a:index(pos.x+x_area, pos.y+hight+y_area-1, pos.z+z_area) --sets area for leaves - if data[area_l] == c_air or data[area_l] == c_ignore or data[area_l]== c_snow then --sets if it's air or ignore - if( snow and c_msnow_leaves1 and math.random( 1,5 )==1) then - data[area_l] = c_msnow_leaves1; - else - data[area_l] = leaves_type --add leaves now - end - end - -- put a snow top on some leaves - if ( snow and math.random(1,3)==1 )then - mg_villages.trees_add_snow(data, a:index(pos.x+x_area, pos.y+hight+y_area, pos.z+z_area), c_air, c_ignore, c_snow) - end - end - end - end - end - for tree_h = 0, hight-1 do -- add the trunk - local area_t = a:index(pos.x, pos.y+tree_h, pos.z) --set area for tree - if data[area_t] == c_air or data[area_t] == c_leaves or data[area_t] == c_sapling or data[area_t] == c_snow or data[area_t] == c_msnow_top or data[area_t] == c_msnow_leaves1 or data[area_t] == c_msnow_leaves2 then --sets if air - data[area_t] = c_tree --add tree now - end - end -end - ---]] - --- Apple tree and jungle tree trunk and leaves function - -trees_lib.generate_fruit_tree = function(data, a, pos, sapling_data, extra_params ) - - local tree_cid = sapling_data.cid.tree; - local leaves_cid = sapling_data.cid.leaves; - local fruit_cid = sapling_data.cid.fruit; - - local height = math.random(3,7); - local size = 2; --math.random(1,3); - local iters = 8; - - local x, y, z = pos.x, pos.y, pos.z - local c_air = minetest.get_content_id("air") - local c_ignore = minetest.get_content_id("ignore") - - -- Trunk - data[a:index(x, y, z)] = tree_cid -- Force-place lowest trunk node to replace sapling - for yy = y + 1, y + height - 1 do - local vi = a:index(x, yy, z) - local node_id = data[vi] - if node_id == c_air or node_id == c_ignore or node_id == leaves_cid then - data[vi] = tree_cid - end - end - - -- Force leaves near the trunk - for z_dist = -1, 1 do - for y_dist = -size, 1 do - local vi = a:index(x - 1, y + height + y_dist, z + z_dist) - for x_dist = -1, 1 do - if data[vi] == c_air or data[vi] == c_ignore then - if fruit_cid and fruit_cid ~= leaves_cid and math.random(1, 8) == 1 then - data[vi] = fruit_cid - else - data[vi] = leaves_cid - end - end - vi = vi + 1 - end - end - end - - -- Randomly add leaves in 2x2x2 clusters. - for i = 1, iters do - local clust_x = x + math.random(-size, size - 1) - local clust_y = y + height + math.random(-size, 0) - local clust_z = z + math.random(-size, size - 1) - - for xi = 0, 1 do - for yi = 0, 1 do - for zi = 0, 1 do - local vi = a:index(clust_x + xi, clust_y + yi, clust_z + zi) - if data[vi] == c_air or data[vi] == c_ignore then - if fruit_cid and fruit_cid ~= leaves_cid and math.random(1, 8) == 1 then - data[vi] = fruit_cid - else - data[vi] = leaves_cid - end - end - end - end - end - end -end - - --- a very small tree -trees_lib.generate_unhappy_tree = function(data, a, pos, sapling_data, extra_params ) - - local tree_cid = sapling_data.cid.tree; - local leaves_cid = sapling_data.cid.leaves; - - local x, y, z = pos.x, pos.y, pos.z - local c_air = minetest.get_content_id("air") - local c_ignore = minetest.get_content_id("ignore") - - -- Trunk - data[a:index(x, y, z)] = tree_cid; - local found = data[a:index(x, y+1, z )]; - if( found==c_air or found==c_ignore ) then - data[a:index(x, y+1, z)] = tree_cid; - end - for xv=-1,1 do - for zv=-1,1 do - if( math.random(1,2)==1 ) then - local found = data[a:index(x+xv, y+1, z+zv )] - if( found==c_air or found==c_ignore ) then - data[a:index(x+xv, y+1, z+zv )] = leaves_cid; - end - end - end - end -end -