Tweak chorus flower aging
[MineClone/MineClone2.git] / mods / ITEMS / mcl_end / chorus_plant.lua
blob1d30d5c53c35995f0a0651433b26d188db85e57b
1 -- Chorus plants.
2 -- This includes chorus flowers, chorus plant stem nodes and chorus fruit
4 local chorus_flower_box = {
5 type = "fixed",
6 fixed = {
7 {-0.5, -0.375, -0.375, 0.5, 0.375, 0.375},
8 {-0.375, -0.375, 0.375, 0.375, 0.375, 0.5},
9 {-0.375, -0.375, -0.5, 0.375, 0.375, -0.375},
10 {-0.375, 0.375, -0.375, 0.375, 0.5, 0.375},
11 {-0.375, -0.5, -0.375, 0.375, -0.375, 0.375},
15 minetest.register_node("mcl_end:chorus_flower", {
16 description = "Chorus Flower",
17 tiles = {
18 "mcl_end_chorus_flower.png",
19 "mcl_end_chorus_flower.png",
20 "mcl_end_chorus_flower.png",
21 "mcl_end_chorus_flower.png",
22 "mcl_end_chorus_flower.png",
23 "mcl_end_chorus_flower.png",
25 drawtype = "nodebox",
26 paramtype = "light",
27 sunlight_propagates = true,
28 node_box = chorus_flower_box,
29 selection_box = { type = "regular" },
30 sounds = mcl_sounds.node_sound_wood_defaults(),
31 groups = {handy=1,axey=1, deco_block = 1, dig_by_piston = 1, destroy_by_lava_flow = 1,},
33 node_placement_prediction = "",
34 on_place = function(itemstack, placer, pointed_thing)
35 local node_under = minetest.get_node(pointed_thing.under)
36 local node_above = minetest.get_node(pointed_thing.above)
37 if placer and not placer:get_player_control().sneak then
38 -- Use pointed node's on_rightclick function first, if present
39 if minetest.registered_nodes[node_under.name] and minetest.registered_nodes[node_under.name].on_rightclick then
40 return minetest.registered_nodes[node_under.name].on_rightclick(pointed_thing.under, node_under, placer, itemstack) or itemstack
41 end
42 end
44 --[[ Part 1: Check placement rules. Placement is legal is one of the following
45 conditions is met:
46 1) On top of end stone or chorus plant
47 2) On top of air and horizontally adjacent to exactly 1 chorus plant ]]
48 local pos
49 if minetest.registered_nodes[node_under.name].buildable_to then
50 pos = pointed_thing.under
51 else
52 pos = pointed_thing.above
53 end
56 local below = {x=pos.x, y=pos.y-1, z=pos.z}
57 local node_below = minetest.get_node(below)
58 local plant_ok = false
59 -- Condition 1
60 if node_below.name == "mcl_end:chorus_plant" or node_below.name == "mcl_end:end_stone" then
61 plant_ok = true
62 -- Condition 2
63 elseif node_below.name == "air" then
64 local around = {
65 { x= 1, y=0, z= 0 },
66 { x=-1, y=0, z= 0 },
67 { x= 0, y=0, z= 1 },
68 { x= 0, y=0, z=-1 },
70 local around_count = 0
71 for a=1, #around do
72 local pos_side = vector.add(pos, around[a])
73 local node_side = minetest.get_node(pos_side)
74 if node_side.name == "mcl_end:chorus_plant" then
75 around_count = around_count + 1
76 if around_count > 1 then
77 break
78 end
79 end
80 end
81 if around_count == 1 then
82 plant_ok = true
83 end
84 end
85 if plant_ok then
86 -- Placement OK! Proceed normally
87 return minetest.item_place(itemstack, placer, pointed_thing)
88 else
89 return itemstack
90 end
91 end,
92 _mcl_blast_resistance = 2,
93 _mcl_hardness = 0.4,
96 minetest.register_abm({
97 label = "Chorus plant growth",
98 nodenames = { "mcl_end:chorus_flower" },
99 interval = 35.0,
100 chance = 4.0,
101 action = function(pos, node, active_object_count, active_object_count_wider)
102 local above = { x = pos.x, y = pos.y + 1, z = pos.z }
103 local node_above = minetest.get_node(above)
104 local around = {
105 { x=-1, y=0, z= 0 },
106 { x= 1, y=0, z= 0 },
107 { x= 0, y=0, z=-1 },
108 { x= 0, y=0, z= 1 },
110 local air_around = true
111 for a=1, #around do
112 if minetest.get_node(vector.add(above, around[a])).name ~= "air" then
113 air_around = false
114 break
117 local grown = false
118 if node_above.name == "air" and air_around then
119 local branching = false
120 local h = 0
121 for y=1, 4 do
122 local checkpos = {x=pos.x, y=pos.y-y, z=pos.z}
123 local node = minetest.get_node(checkpos)
124 if node.name == "mcl_end:chorus_plant" then
125 h = y
126 if not branching then
127 for a=1, #around do
128 local node_side = minetest.get_node(vector.add(checkpos, around[a]))
129 if node_side.name == "mcl_end:chorus_plant" then
130 branching = true
134 else
135 break
139 local grow_chance
140 if h <= 1 then
141 grow_chance = 100
142 elseif h == 2 and branching == false then
143 grow_chance = 60
144 elseif h == 2 and branching == true then
145 grow_chance = 50
146 elseif h == 3 and branching == false then
147 grow_chance = 40
148 elseif h == 3 and branching == true then
149 grow_chance = 25
150 elseif h == 4 and branching == false then
151 grow_chance = 20
154 if grow_chance then
155 local new_flowers = {}
156 local r = math.random(1, 100)
157 local age = node.param2
158 if r <= grow_chance then
159 table.insert(new_flowers, above)
160 else
161 age = age + 1
162 local branches
163 if branching == false then
164 branches = math.random(1, 4)
165 elseif branching == true then
166 branches = math.random(0, 3)
168 local branch_grown = false
169 for b=1, branches do
170 local next_branch = math.random(1, #around)
171 local branch = vector.add(pos, around[next_branch])
172 local below_branch = vector.add(branch, {x=0,y=-1,z=0})
173 if minetest.get_node(below_branch).name == "air" then
174 table.insert(new_flowers, branch)
179 for _, f in ipairs(new_flowers) do
180 if age >= 5 then
181 minetest.set_node(f, {name="mcl_end:chorus_flower_dead"})
182 grown = true
183 else
184 minetest.set_node(f, {name="mcl_end:chorus_flower", param2 = age})
185 grown = true
188 if #new_flowers >= 1 then
189 minetest.set_node(pos, {name="mcl_end:chorus_plant"})
190 grown = true
194 if not grown then
195 minetest.set_node(pos, {name = "mcl_end:chorus_flower_dead"})
197 end,
200 minetest.register_node("mcl_end:chorus_flower_dead", {
201 description = "Dead Chorus Flower",
202 tiles = {
203 "mcl_end_chorus_flower_dead.png",
204 "mcl_end_chorus_flower_dead.png",
205 "mcl_end_chorus_flower_dead.png",
206 "mcl_end_chorus_flower_dead.png",
207 "mcl_end_chorus_flower_dead.png",
208 "mcl_end_chorus_flower_dead.png",
210 drawtype = "nodebox",
211 paramtype = "light",
212 sunlight_propagates = true,
213 node_box = chorus_flower_box,
214 selection_box = { type = "regular" },
215 sounds = mcl_sounds.node_sound_wood_defaults(),
216 drop = "mcl_end:chorus_flower",
217 groups = {handy=1,axey=1, deco_block = 1, dig_by_piston = 1, destroy_by_lava_flow = 1,},
218 _mcl_blast_resistance = 2,
219 _mcl_hardness = 0.4,
222 minetest.register_node("mcl_end:chorus_plant", {
223 description = "Chorus Plant Stem",
224 tiles = {
225 "mcl_end_chorus_plant.png",
226 "mcl_end_chorus_plant.png",
227 "mcl_end_chorus_plant.png",
228 "mcl_end_chorus_plant.png",
229 "mcl_end_chorus_plant.png",
230 "mcl_end_chorus_plant.png",
232 drawtype = "nodebox",
233 paramtype = "light",
234 sunlight_propagates = true,
235 -- TODO: Maybe improve nodebox a bit to look more “natural”
236 node_box = {
237 type = "connected",
238 fixed = { -0.25, -0.25, -0.25, 0.25, 0.25, 0.25 }, -- Core
239 connect_top = { -0.1875, 0.25, -0.1875, 0.1875, 0.5, 0.1875 },
240 connect_left = { -0.5, -0.1875, -0.1875, -0.25, 0.1875, 0.1875 },
241 connect_right = { 0.25, -0.1875, -0.1875, 0.5, 0.1875, 0.1875 },
242 connect_bottom = { -0.1875, -0.5, -0.25, 0.1875, -0.25, 0.25 },
243 connect_front = { -0.1875, -0.1875, -0.5, 0.1875, 0.1875, -0.25 },
244 connect_back = { -0.1875, -0.1875, 0.25, 0.1875, 0.1875, 0.5 },
246 connect_sides = { "top", "bottom", "front", "back", "left", "right" },
247 connects_to = {"mcl_end:chorus_plant", "mcl_end:chorus_flower", "mcl_end:chorus_flower_dead", "mcl_end:end_stone"},
248 sounds = mcl_sounds.node_sound_wood_defaults(),
249 drop = {
250 items = {
251 { items = { "mcl_end:chorus_fruit"}, rarity = 2 },
254 groups = {handy=1,axey=1, not_in_creative_inventory = 1, dig_by_piston = 1, destroy_by_lava_flow = 1 },
255 _mcl_blast_resistance = 2,
256 _mcl_hardness = 0.4,
259 -- Craftitems
260 minetest.register_craftitem("mcl_end:chorus_fruit", {
261 description = "Chorus Fruit",
262 _doc_items_longdesc = "Chorus fruits are the fruits of the chorus plant which is home to the End. They can be eaten to restore a few hunger points.",
263 wield_image = "mcl_end_chorus_fruit.png",
264 inventory_image = "mcl_end_chorus_fruit.png",
265 -- TODO: Teleport player
266 on_place = minetest.item_eat(4),
267 on_secondary_use = minetest.item_eat(4),
268 groups = { food = 2, eatable = 4, can_eat_when_full = 1 },
269 _mcl_saturation = 2.4,
270 stack_max = 64,
273 minetest.register_craftitem("mcl_end:chorus_fruit_popped", {
274 description = "Popped Chorus Fruit",
275 _doc_items_longdesc = doc.sub.items.temp.craftitem,
276 wield_image = "mcl_end_chorus_fruit_popped.png",
277 inventory_image = "mcl_end_chorus_fruit_popped.png",
278 groups = { craftitem = 1 },
279 stack_max = 64,
282 minetest.register_craft({
283 type = "cooking",
284 output = "mcl_end:chorus_fruit_popped",
285 recipe = "mcl_end:chorus_fruit",
286 cooktime = 10,