-
Notifications
You must be signed in to change notification settings - Fork 11
/
melon.lua
249 lines (227 loc) · 7.21 KB
/
melon.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
--[[
Copyright (C) 2015 - Auke Kok <sofar@foo-projects.org>
"crops" 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.
--]]
-- Intllib
local S = crops.intllib
local faces = {
[1] = { x = -1, z = 0, r = 3, o = 1, m = 14 },
[2] = { x = 1, z = 0, r = 1, o = 3, m = 16 },
[3] = { x = 0, z = -1, r = 2, o = 0, m = 5 },
[4] = { x = 0, z = 1, r = 0, o = 2, m = 11 }
}
minetest.register_node("crops:melon_seed", {
description = S("Melon seed"),
inventory_image = "crops_melon_seed.png",
wield_image = "crops_melon_seed.png",
tiles = { "crops_melon_plant_1.png" },
drawtype = "plantlike",
waving = 1,
sunlight_propagates = false,
use_texture_alpha = true,
walkable = false,
paramtype = "light",
node_placement_prediction = "crops:melon_plant_1",
groups = { snappy=3,flammable=3,flora=1,attached_node=1 },
on_place = function(itemstack, placer, pointed_thing)
local under = minetest.get_node(pointed_thing.under)
if minetest.get_item_group(under.name, "soil") <= 1 then
return
end
crops.plant(pointed_thing.above, {name="crops:melon_plant_1"})
if not minetest.settings:get_bool("creative_mode") then
itemstack:take_item()
end
return itemstack
end
})
for stage = 1, 6 do
minetest.register_node("crops:melon_plant_" .. stage , {
description = S("Melon plant"),
tiles = { "crops_melon_plant_" .. stage .. ".png" },
drawtype = "plantlike",
waving = 1,
sunlight_propagates = true,
use_texture_alpha = true,
walkable = false,
paramtype = "light",
groups = { snappy=3, flammable=3, flora=1, attached_node=1, not_in_creative_inventory=1 },
drop = "crops:melon_seed",
sounds = default.node_sound_leaves_defaults(),
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -0.5 + (((math.min(stage, 4)) + 1) / 5), 0.5}
}
})
end
minetest.register_node("crops:melon_plant_5_attached", {
visual = "mesh",
mesh = "crops_plant_extra_face.obj",
description = S("Melon plant"),
tiles = { "crops_melon_stem.png", "crops_melon_plant_4.png" },
drawtype = "mesh",
paramtype2 = "facedir",
sunlight_propagates = true,
use_texture_alpha = true,
walkable = false,
paramtype = "light",
groups = { snappy=3, flammable=3, flora=1, attached_node=1, not_in_creative_inventory=1 },
drop = "crops:melon_seed",
sounds = default.node_sound_leaves_defaults(),
})
minetest.register_craftitem("crops:melon_slice", {
description = S("Melon slice"),
inventory_image = "crops_melon_slice.png",
on_use = minetest.item_eat(1)
})
minetest.register_craft({
type = "shapeless",
output = "crops:melon_seed",
recipe = { "crops:melon_slice" }
})
--
-- the melon "block"
--
minetest.register_node("crops:melon", {
description = S("Melon"),
tiles = {
"crops_melon_top.png",
"crops_melon_bottom.png",
"crops_melon.png",
},
sunlight_propagates = false,
use_texture_alpha = false,
walkable = true,
groups = { snappy=3, flammable=3, oddly_breakable_by_hand=2 },
paramtype2 = "facedir",
drop = {},
sounds = default.node_sound_wood_defaults({
dig = { name = "default_dig_oddly_breakable_by_hand" },
dug = { name = "default_dig_choppy" }
}),
on_dig = function(pos, node, digger)
for face = 1, 4 do
local s = { x = pos.x + faces[face].x, y = pos.y, z = pos.z + faces[face].z }
local n = minetest.get_node(s)
if n.name == "crops:melon_plant_5_attached" then
-- make sure it was actually attached to this stem
if n.param2 == faces[face].o then
minetest.swap_node(s, { name = "crops:melon_plant_4" })
end
end
end
local meta = minetest.get_meta(pos)
local damage = meta:get_int("crops_damage")
local drops = {}
-- 0 dmg - 3-5
-- 50 dmg - 2-3
-- 100 dmg - 1-1
for i = 1,math.random(3 - (2 * (damage / 100)), 5 - (4 * (damage / 100))) do
table.insert(drops, ('crops:melon_slice'))
end
core.handle_node_drops(pos, drops, digger)
minetest.remove_node(pos)
end
})
--
-- grows a plant to mature size
--
minetest.register_abm({
nodenames = { "crops:melon_plant_1", "crops:melon_plant_2", "crops:melon_plant_3","crops:melon_plant_4" },
neighbors = { "group:soil" },
interval = crops.settings.interval,
chance = crops.settings.chance,
action = function(pos, node, active_object_count, active_object_count_wider)
if not crops.can_grow(pos) then
return
end
local n = string.gsub(node.name, "4", "5")
n = string.gsub(n, "3", "4")
n = string.gsub(n, "2", "3")
n = string.gsub(n, "1", "2")
minetest.swap_node(pos, { name = n })
end
})
--
-- grows a melon
--
minetest.register_abm({
nodenames = { "crops:melon_plant_5" },
neighbors = { "group:soil" },
interval = crops.settings.interval,
chance = crops.settings.chance,
action = function(pos, node, active_object_count, active_object_count_wider)
if not crops.can_grow(pos) then
return
end
for face = 1, 4 do
local t = { x = pos.x + faces[face].x, y = pos.y, z = pos.z + faces[face].z }
if minetest.get_node(t).name == "crops:melon" then
return
end
end
local r = math.random(1, 4)
local t = { x = pos.x + faces[r].x, y = pos.y, z = pos.z + faces[r].z }
local n = minetest.get_node(t)
if n.name == "ignore" then
return
end
if minetest.registered_nodes[minetest.get_node({ x = t.x, y = t.y - 1, z = t.z }).name].walkable == false then
return
end
if minetest.registered_nodes[n.name].drawtype == "plantlike" or
minetest.registered_nodes[n.name].groups.flora == 1 or
n.name == "air" then
minetest.swap_node(pos, {name = "crops:melon_plant_5_attached", param2 = faces[r].r})
minetest.set_node(t, {name = "crops:melon", param2 = faces[r].m})
local meta = minetest.get_meta(pos)
local damage = meta:get_int("crops_damage")
local water = meta:get_int("crops_water")
-- growing a melon costs 25 water!
meta:set_int("crops_water", math.max(0, water - 25))
meta = minetest.get_meta(t)
-- reflect plants' damage in the melon yield
meta:set_int("crops_damage", damage)
end
end
})
--
-- return a melon to a normal one if there is no melon attached, so it can
-- grow a new melon again
--
minetest.register_abm({
nodenames = { "crops:melon_plant_5_attached" },
interval = crops.settings.interval,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
for face = 1, 4 do
local t = { x = pos.x + faces[face].x, y = pos.y, z = pos.z + faces[face].z }
if minetest.get_node(t).name == "crops:melon" then
return
end
end
minetest.swap_node(pos, {name = "crops:melon_plant_4" })
end
})
crops.melon_die = function(pos)
minetest.set_node(pos, { name = "crops:melon_plant_6" })
end
local properties = {
die = crops.melon_die,
waterstart = 20,
wateruse = 1,
night = 5,
soak = 80,
soak_damage = 90,
wither = 20,
wither_damage = 10,
}
crops.register({ name = "crops:melon_plant_1", properties = properties })
crops.register({ name = "crops:melon_plant_2", properties = properties })
crops.register({ name = "crops:melon_plant_3", properties = properties })
crops.register({ name = "crops:melon_plant_4", properties = properties })
crops.register({ name = "crops:melon_plant_5", properties = properties })
crops.register({ name = "crops:melon_plant_5_attached", properties = properties })