]> git.lizzy.rs Git - Crafter.git/blob - mods/farming/plant_api.lua
remove server debug
[Crafter.git] / mods / farming / plant_api.lua
1  --plant growth time contants (in seconds)
2  local plant_min = 60
3  local plant_max = 240
4  
5  minetest.register_plant = function(name,def)
6         local max = 1
7          if def.stages then
8                  max = def.stages
9          end
10          for i = 1,max do
11                 local nodename
12                 if def.stages then
13                         nodename = "farming:"..name.."_"..i
14                 else
15                         nodename = "farming:"..name
16                 end
17          
18          
19                  local after_dig_node
20                  local on_abm
21                  local on_construct
22                  local after_destruct
23                  local after_place_node
24                  --do custom functions for each node
25                  --wether growing in place or up
26                  if def.grows == "up" then
27                          after_dig_node = function(pos, node, metadata, digger)
28                                 if digger == nil then return end
29                                 local np = {x = pos.x, y = pos.y + 1, z = pos.z}
30                                 local nn = minetest.get_node(np)
31                                 if nn.name == node.name then
32                                         minetest.node_dig(np, nn, digger)
33                                         minetest.sound_play("dirt",{pos=pos,gain=0.2})
34                                 end
35                         end
36                         
37                         on_abm = function(pos)
38                                 if minetest.get_node_light(pos, nil) < 10 then
39                                         --print("failed to grow at "..dump(pos))
40                                         return
41                                 end
42                                 
43                                 local found = minetest.find_node_near(pos, 3, {"main:water","main:waterflow"})
44                                 pos.y = pos.y - 1
45                                 local noder = minetest.get_node(pos).name
46                                 local found_soil = minetest.get_item_group(noder, "soil") > 0
47                                 local found_self--[[this is deep]]= (noder == nodename)
48                                 
49                                 if found and (found_soil or found_self) then
50                                         pos.y = pos.y + 2
51                                         if minetest.get_node(pos).name == "air" then
52                                                 minetest.set_node(pos,{name="farming:"..name})
53                                         end
54                                 elseif not found_self then
55                                         pos.y = pos.y + 1
56                                         minetest.dig_node(pos)
57                                         minetest.sound_play("dirt",{pos=pos,gain=0.2})
58                                 end
59                         end
60                         
61                         after_place_node = function(pos, placer, itemstack, pointed_thing)
62                                 pos.y = pos.y - 1
63                                 local noder = minetest.get_node(pos).name
64                                 local found = minetest.get_node_group(noder, "soil") > 0
65                                 if not found then
66                                         pos.y = pos.y + 1
67                                         minetest.dig_node(pos)
68                                 end
69                         end
70                         
71                 --for plants that grow in place
72                 elseif def.grows == "in_place" then
73                         on_abm = function(pos)
74                                 if minetest.get_node_light(pos, nil) < 10 then
75                                         minetest.dig_node(pos)
76                                         minetest.sound_play("dirt",{pos=pos,gain=0.2})
77                                         --print("failed to grow at "..dump(pos))
78                                         return
79                                 end
80                                 
81                                 pos.y = pos.y - 1
82                                 local found = minetest.get_node_group(minetest.get_node(pos).name, "farmland") > 0
83                                 --if found farmland below
84                                 if found then   
85                                         if i < max then
86                                                 pos.y = pos.y + 1
87                                                 minetest.set_node(pos,{name="farming:"..name.."_"..(i+1)})
88                                         end
89                                 --if not found farmland
90                                 else
91                                         minetest.dig_node(pos)
92                                         minetest.sound_play("dirt",{pos=pos,gain=0.2})
93                                 end
94                         end
95                         after_place_node = function(pos, placer, itemstack, pointed_thing)
96                                 pos.y = pos.y - 1
97                                 local noder = minetest.get_node(pos).name
98                                 local found = minetest.get_node_group(noder, "farmland") > 0
99                                 if not found then
100                                         pos.y = pos.y + 1
101                                         minetest.dig_node(pos)
102                                 end
103                         end
104                 elseif def.grows == "in_place_yields" then
105                         on_abm = function(pos)
106                                 if minetest.get_node_light(pos, nil) < 10 then
107                                         minetest.dig_node(pos)
108                                         minetest.sound_play("dirt",{pos=pos,gain=0.2})
109                                         --print("failed to grow at "..dump(pos))
110                                         return
111                                 end
112                                 pos.y = pos.y - 1
113                                 local found = minetest.get_node_group(minetest.get_node(pos).name, "farmland") > 0
114                                 --if found farmland below
115                                 if found then   
116                                         if i < max then
117                                                 pos.y = pos.y + 1
118                                                 minetest.set_node(pos,{name="farming:"..name.."_"..(i+1)})
119                                         else
120                                                 pos.y = pos.y + 1
121                                                 local found = false
122                                                 local add_node = nil
123                                                 for x = -1,1 do
124                                                         if found == false then
125                                                                 for z = -1,1 do
126                                                                         if math.abs(x)+math.abs(z) == 1 then
127                                                                                 local node_get = minetest.get_node(vector.new(pos.x-x,pos.y,pos.z-z)).name == "air"
128                                                                                 if node_get then
129                                                                                         add_node = vector.new(pos.x-x,pos.y,pos.z-z)
130                                                                                         found = true
131                                                                                 end
132                                                                         end
133                                                                 end
134                                                         end
135                                                 end
136                                                 
137                                                 if found == true and add_node then
138                                                         local param2 = minetest.dir_to_facedir(vector.direction(pos,add_node))
139                                                         minetest.add_node(add_node,{name=def.grown_node,param2=param2})
140                                                         
141                                                         local facedir = minetest.facedir_to_dir(param2)
142                                                         
143                                                         local inverted_facedir = vector.multiply(facedir,-1)
144                                                         minetest.set_node(vector.add(inverted_facedir,add_node), {name="farming:"..name.."_complete", param2=minetest.dir_to_facedir(facedir)})
145                                                 end
146                                         end
147                                 --if not found farmland
148                                 else
149                                         minetest.dig_node(pos)
150                                         minetest.sound_play("dirt",{pos=pos,gain=0.2})
151                                 end
152                         end
153                         after_place_node = function(pos, placer, itemstack, pointed_thing)
154                                 pos.y = pos.y - 1
155                                 local noder = minetest.get_node(pos).name
156                                 local found = minetest.get_node_group(noder, "farmland") > 0
157                                 if not found then
158                                         pos.y = pos.y + 1
159                                         minetest.dig_node(pos)
160                                 end
161                         end
162                 end
163                 
164                 --allow plants to only drop item at max stage
165                 local drop
166                 if i == max and def.grows ~= "in_place_yields" then
167                         drop = def.drop
168                 elseif max == 1 then
169                         drop = def.drop
170                 else
171                         drop = ""
172                 end
173                 
174                 local tiles
175                 if max > 1 then
176                         tiles = {def.tiles[1].."_"..i..".png"}
177                 else
178                         tiles = def.tiles
179                 end
180                 
181                 def.groups.plants = 1
182                 
183                 minetest.register_node(nodename, {
184                         description               = def.description,
185                         drawtype                  = def.drawtype,
186                         waving                    = def.waving,
187                         inventory_image           = def.inventory_image,
188                         walkable                  = def.walkable,
189                         climbable                 = def.climbable,
190                         paramtype                 = def.paramtype,
191                         tiles                     = tiles,
192                         paramtype2                = def.paramtype2,
193                         buildable_to              = def.buildable_to,
194                         groups                    = def.groups,
195                         sounds                    = def.sounds,
196                         selection_box             = def.selection_box,
197                         drop                      = drop,
198                         sunlight_propagates       = def.sunlight_propagates,
199                         node_box                  = def.node_box,
200                         node_placement_prediction = "",
201                         is_ground_content         = false,
202                         
203                         --flooding function
204                         floodable         = true,
205                         on_flood = function(pos, oldnode, newnode)
206                                  minetest.dig_node(pos)
207                         end,
208                         
209                         after_dig_node   = after_dig_node,
210                         after_place_node = after_place_node,
211                         on_construct     = on_construct,
212                         after_destruct   = after_destruct,
213                 })
214                 if on_abm then
215                         minetest.register_abm({
216                                 label = nodename.." Grow",
217                                 nodenames = {nodename},
218                                 neighbors = {"air"},
219                                 interval = 6,
220                                 chance = 250,
221                                 catch_up = true,
222                                 action = function(pos)
223                                         on_abm(pos)
224                                 end,
225                         })
226                 end
227         end
228         
229         --create final stage for grow in place plant stems that create food
230         if def.grows == "in_place_yields" then
231                 minetest.register_node("farming:"..name.."_complete", {
232                     description         = def.stem_description,
233                     tiles               = def.stem_tiles,
234                     drawtype            = def.stem_drawtype,
235                     walkable            = def.stem_walkable,
236                     sunlight_propagates = def.stem_sunlight_propagates,
237                     paramtype           = def.stem_paramtype,
238                     drop                = def.stem_drop,
239                         groups              = def.stem_groups,
240                         sounds              = def.stem_sounds,
241                     node_box            = def.stem_node_box,
242                     selection_box       = def.stem_selection_box,
243                     paramtype2          = "facedir",
244                 })
245                 minetest.register_node("farming:"..def.fruit_name, {
246                     description = def.fruit_description,
247                     tiles       = def.fruit_tiles,
248                     groups      = def.fruit_groups,
249                     sounds      = def.fruit_sounds,
250                     drop        = def.fruit_drop,
251                     --this is hardcoded to work no matter what
252                     paramtype2  = "facedir",
253                     after_destruct = function(pos,oldnode)
254                             local facedir = oldnode.param2
255                             facedir = minetest.facedir_to_dir(facedir)
256                             local dir = vector.multiply(facedir,-1)
257                             local stem_pos = vector.add(dir,pos)
258                             
259                             if minetest.get_node(stem_pos).name == "farming:"..name.."_complete" then
260                                     minetest.set_node(stem_pos, {name = "farming:"..name.."_1"})
261                             end
262                     end
263                 })
264         end
265         
266         if def.seed_name then
267                 minetest.register_craftitem("farming:"..def.seed_name.."_seeds", {
268                         description = def.seed_description,
269                         inventory_image = def.seed_inventory_image,
270                         on_place = function(itemstack, placer, pointed_thing)
271                                 if pointed_thing.type ~= "node" then
272                                         return itemstack
273                                 end
274                                 local pointed_thing_diff = pointed_thing.above.y - pointed_thing.under.y
275                                 
276                                 if pointed_thing_diff < 1 then return end
277                                  
278                                 if minetest.get_node(pointed_thing.above).name ~= "air" then return end
279                                 local pb = pointed_thing.above
280                                 if minetest.get_node_group(minetest.get_node(vector.new(pb.x,pb.y-1,pb.z)).name, "farmland") == 0 or minetest.get_node(pointed_thing.above).name ~= "air"  then
281                                         return itemstack
282                                 end
283
284                                 local wdir = minetest.dir_to_wallmounted(vector.subtract(pointed_thing.under,pointed_thing.above))
285
286                                 local fakestack = itemstack
287                                 local retval = false
288
289                                 retval = fakestack:set_name(def.seed_plants)
290
291                                 if not retval then
292                                         return itemstack
293                                 end
294                                 itemstack, retval = minetest.item_place(fakestack, placer, pointed_thing, wdir)
295                                 itemstack:set_name("farming:"..def.seed_name.."_seeds")
296
297                                 if retval then
298                                         minetest.sound_play("leaves", {pos=pointed_thing.above, gain = 1.0})
299                                 end
300
301                                 return itemstack
302                         end
303                 })
304         end
305 end