forked from PilzAdam/farming_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
273 lines (251 loc) · 7.04 KB
/
init.lua
File metadata and controls
273 lines (251 loc) · 7.04 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
local load_time_start = os.clock()
if farming.add_plant
or farming.plant_index
or farming.generate_tree
or farming.seeds then
error("[farming_plus] some field(s) already exist")
end
farming.plant_index = {}
-- Boilerplate to support localized strings if intllib mod is installed.
if (minetest.get_modpath("intllib")) then
dofile(minetest.get_modpath("intllib").."/intllib.lua")
farming.S = intllib.Getter(minetest.get_current_modname())
else
farming.S = function ( s ) return s end
end
function farming.add_plant(full_grown, names, interval, chance)
minetest.register_abm({
nodenames = names,
interval = interval,
chance = chance,
action = function(pos, node)
pos.y = pos.y-1
if minetest.get_node(pos).name ~= "farming:soil_wet" then
return
end
pos.y = pos.y+1
local light = minetest.get_node_light(pos)
if not light
or light < 8 then
return
end
local step = nil
for i,name in ipairs(names) do
if name == node.name then
step = i
break
end
end
if step == nil then
return
end
local new_node = {name=names[step+1]}
if new_node.name == nil then
new_node.name = full_grown
end
minetest.set_node(pos, new_node)
end
})
table.insert(farming.plant_index, {
full_grown = full_grown,
names = names,
interval = interval,
chance = chance,
})
end
function farming.generate_tree(pos, trunk, leaves, underground, replacements)
pos.y = pos.y-1
local nodename = minetest.get_node(pos).name
local ret = true
for _,name in ipairs(underground) do
if nodename == name then
ret = false
break
end
end
pos.y = pos.y+1
if not minetest.get_node_light(pos) then
return
end
if ret or minetest.get_node_light(pos) < 8 then
return
end
local node = {name = ""}
for dy=1,4 do
pos.y = pos.y+dy
if minetest.get_node(pos).name ~= "air" then
return
end
pos.y = pos.y-dy
end
node.name = trunk
for dy=0,4 do
pos.y = pos.y+dy
minetest.set_node(pos, node)
pos.y = pos.y-dy
end
if not replacements then
replacements = {}
end
node.name = leaves
pos.y = pos.y+3
for dx=-2,2 do
for dz=-2,2 do
for dy=0,3 do
pos.x = pos.x+dx
pos.y = pos.y+dy
pos.z = pos.z+dz
if dx == 0 and dz == 0 and dy==3 then
if minetest.get_node(pos).name == "air" and math.random(1, 5) <= 4 then
minetest.set_node(pos, node)
for name,rarity in pairs(replacements) do
if math.random(1, rarity) == 1 then
minetest.set_node(pos, {name=name})
end
end
end
elseif dx == 0 and dz == 0 and dy==4 then
if minetest.get_node(pos).name == "air" and math.random(1, 5) <= 4 then
minetest.set_node(pos, node)
for name,rarity in pairs(replacements) do
if math.random(1, rarity) == 1 then
minetest.set_node(pos, {name=name})
end
end
end
elseif math.abs(dx) ~= 2 and math.abs(dz) ~= 2 then
if minetest.get_node(pos).name == "air" then
minetest.set_node(pos, node)
for name,rarity in pairs(replacements) do
if math.random(1, rarity) == 1 then
minetest.set_node(pos, {name=name})
end
end
end
else
if math.abs(dx) ~= 2 or math.abs(dz) ~= 2 then
if minetest.get_node(pos).name == "air" and math.random(1, 5) <= 4 then
minetest.set_node(pos, node)
for name,rarity in pairs(replacements) do
if math.random(1, rarity) == 1 then
minetest.set_node(pos, {name=name})
end
end
end
end
end
pos.x = pos.x-dx
pos.y = pos.y-dy
pos.z = pos.z-dz
end
end
end
end
farming.seeds = {
["farming:pumpkin_seed"]=60,
["farming_plus:strawberry_seed"]=30,
["farming_plus:rhubarb_seed"]=30,
["farming_plus:corn_seed"]=30,
["farming_plus:potatoe_seed"]=30,
["farming_plus:tomato_seed"]=30,
["farming_plus:orange_seed"]=30,
["farming_plus:carrot_seed"]=30,
}
-- ========= GENERATE PLANTS IN THE MAP =========
minetest.register_on_generated(function(minp, maxp, seed)
if maxp.y >= 2 and minp.y <= 0 then
-- Generate plants (code from flowers)
local perlin1 = minetest.get_perlin(974, 3, 0.6, 100)
-- Assume X and Z lengths are equal
local divlen = 16
local divs = (maxp.x-minp.x)/divlen+1;
for divx=0,divs-1 do
for divz=0,divs-1 do
local x0 = minp.x + math.floor((divx+0)*divlen)
local z0 = minp.z + math.floor((divz+0)*divlen)
local x1 = minp.x + math.floor((divx+1)*divlen)
local z1 = minp.z + math.floor((divz+1)*divlen)
-- Determine flowers amount from perlin noise
local grass_amount = math.floor(perlin1:get2d({x=x0, y=z0}) ^ 3 * 9)
-- Find random positions for flowers based on this random
local pr = PseudoRandom(seed+456)
for i=0,grass_amount do
local x = pr:next(x0, x1)
local z = pr:next(z0, z1)
-- Find ground level (0...15)
local ground_y = nil
for y=30,0,-1 do
if minetest.get_node({x=x,y=y,z=z}).name ~= "air" then
ground_y = y
break
end
end
if ground_y then
local p = {x=x,y=ground_y+1,z=z}
local nn = minetest.get_node(p).name
-- Check if the node can be replaced
if minetest.registered_nodes[nn] and
minetest.registered_nodes[nn].buildable_to then
nn = minetest.get_node({x=x,y=ground_y,z=z}).name
if nn == "default:dirt_with_grass" then
--local plant_choice = pr:next(1, #farming.plant_index)
local plant_choice = math.floor(perlin1:get2d({x=x,y=z})*(#farming.plant_index))
local plant = farming.plant_index[plant_choice]
if plant then
minetest.set_node(p, {name=plant.full_grown})
end
end
end
end
end
end
end
end
end)
--[[
-- ========= ALIASES FOR FARMING MOD BY SAPIER =========
-- potatoe -> potatoe
minetest.register_alias("farming:potatoe_node", "farming_plus:potatoe")
--minetest.register_alias("farming:potatoe", "farming:potatoe_item") cant do this
minetest.register_alias("farming:potatoe_straw", "farming_plus:potatoe")
minetest.register_alias("farming:seed_potatoe", "farming_plus:potatoe_seed")
for lvl = 1, 6, 1 do
minetest.register_entity(":farming:potatoe_lvl"..lvl, {
on_activate = function(self, staticdata)
minetest.set_node(self.object:getpos(), {name="farming_plus:potatoe_1"})
end
})
end
--minetest.register_alias("farming:cotton", "farming:cotton_3")
minetest.register_alias("farming:wheat_harvested", "farming:wheat")
minetest.register_abm({
nodenames = {"farming:wheat"},
interval = 1,
chance = 1,
action = function(pos)
minetest.set_node(pos, {name="farming:wheat_8"})
end,
})
]]--
local modpath = minetest.get_modpath('farming_plus')
dofile(modpath.."/bananas.lua")
dofile(modpath.."/carrots.lua")
dofile(modpath.."/cocoa.lua")
dofile(modpath.."/corn.lua")
dofile(modpath.."/cotton.lua")
dofile(modpath.."/strawberries.lua")
dofile(modpath.."/rhubarb.lua")
dofile(modpath.."/potatoes.lua")
dofile(modpath.."/tomatoes.lua")
dofile(modpath.."/oranges.lua")
dofile(modpath.."/pumpkin.lua")
dofile(modpath.."/weed.lua")
dofile(modpath.."/wheat.lua")
dofile(modpath.."/craft.lua")
minetest.log(
'action',
string.format(
'['..minetest.get_current_modname()..'] loaded in %.3fs',
os.clock() - load_time_start
)
)