forked from berengma/farming_nextgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeder.lua
512 lines (413 loc) · 17 KB
/
seeder.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
--*******************************************************
--**** The Seeder
--**** All coding made by Gundul except recursive_dig
--**** was taken from technic:chainsaw mod coded by
--**** Maciej Kasatkin (RealBadAngel)
--*******************************************************
local farm_redo = false
local S = farmingNG.S
-- different values if technic not present
if not farmingNG.havetech then
farmingNG.seeder_charge_per_node = math.floor(65535 / farmingNG.seeder_max_charge * farmingNG.seeder_charge_per_node)
farmingNG.seeder_max_charge = 65535
end
--check for farming redo mod
if minetest.global_exists("farming") then
if farming.mod == "redo" then farm_redo = true end
end
-- grapes and beans from farming_plus need helpers to grow
if minetest.get_modpath("farming_plus") or farm_redo then
minetest.register_craftitem("farming_nextgen:grape_seedling", {
description = S("A grape seedling for the seeder"),
inventory_image = "farming_grapes_1.png"
})
minetest.register_craftitem("farming_nextgen:bean_seedling", {
description = S("A bean seedling for the seeder"),
inventory_image = "farming_beanpole_1.png"
})
if not farm_redo then
minetest.register_craft({
type = "shapeless",
output = "farming_nextgen:grape_seedling",
recipe = {"farming_plus:trellis","farming_plus:grapes"}
})
minetest.register_craft({
type = "shapeless",
output = "farming_nextgen:bean_seedling",
recipe = {"farming_plus:beanpole","farming_plus:beans"}
})
else
minetest.register_craft({
type = "shapeless",
output = "farming_nextgen:grape_seedling",
recipe = {"farming:trellis","farming:grapes"}
})
minetest.register_craft({
type = "shapeless",
output = "farming_nextgen:bean_seedling",
recipe = {"farming:beanpole","farming:beans"}
})
end
end
-- The default soils
local soil_nodenames = {
["farming:soil"] = true,
["farming:soil_wet"] = true,
["farming:desert_sand_soil"] = true,
["farming:desert_sand_soil_wet"]= true,
["farming:dry_soil"] = true,
["farming:dry_soil_wet"] = true
}
--support compost mod
if minetest.get_modpath("compost") then
soil_nodenames["compost:garden_soil"] = true
end
farmingNG.seeder_seed = {
-- *** farming
{"farming:artichoke", "farming:artichoke_1"},
{"farming:seed_barley", "farming:barley_1"},
{"farming:seed_hemp", "farming:hemp_1"},
{"farming:pumpkin_seed", "farming:pumpkin_1"},
{"farming:coffee_beans", "farming:coffee_1"},
{"farming:chili_pepper", "farming:chili_1"},
{"farming:carrot", "farming:carrot_1"},
{"farming:corn", "farming:corn_1"},
{"farming:cucumber", "farming:cucumber_1"},
{"farming:garlic_clove", "farming:garlic_1"},
{"farming:melon_slice", "farming:melon_1"},
{"farming:onion", "farming:onion_1"},
{"farming:parsley", "farming:parsley_1"},
{"farming:peppercorn", "farming:pepper_1"},
{"farming:pineapple_top", "farming:pineapple_1"},
{"farming:potato", "farming:potato_1"},
{"farming:pumpkin_slice", "farming:pumpkin_1"},
{"farming:raspberries", "farming:raspberry_1"},
{"farming:rhubarb", "farming:rhubarb_1"},
{"farming:tomato", "farming:tomato_1"},
{"farming:blueberries", "farming:blueberry_1"},
{"farming:pea_pod", "farming:pea_1"},
{"farming:beetroot", "farming:beetroot_1"},
{"farming:seed_rice", "farming:rice_1"},
{"farming:seed_oat", "farming:oat_1"},
{"farming:seed_rye", "farming:rye_1"},
{"farming:cabbage", "farming:cabbage_1"},
{"farming:seed_mint", "farming:mint_1"},
{"farming:blackberry", "farming:blackberry_1"},
{"farming:lettuce", "farming:lettuce_1"},
{"farming:soy_pod", "farming:soy_1"},
{"farming:seed_sunflower", "farming:sunflower_1"},
{"farming:vanilla", "farming:vanilla_1"},
-- *** farming_plus
{"farming_plus:carrot_seed", "farming_plus:carrot_1"},
{"farming_plus:chilli_seeds", "farming_plus:chilli_1"},
{"farming_plus:coffee_beans", "farming_plus:coffee_1"},
{"farming_plus:corn_seed", "farming_plus:corn_1"},
{"farming_plus:cucumber_seed", "farming_plus:cucumber_1"},
{"farming_plus:garlic_seed", "farming_plus:garlic_1"},
{"farming_plus:seed_hemp", "farming_plus:hemp_1"},
{"farming_plus:melon_seed", "farming_plus:melon_1"},
{"farming_plus:potato_seed", "farming_plus:potato_1"},
{"farming_plus:rhubarb_seed", "farming_plus:rhubarb_1"},
{"farming_plus:raspberry_seed", "farming_plus:raspberry_1"},
{"farming_plus:strawberry_seed", "farming_plus:strawberry_1"},
{"farming_plus:soy_seed", "farming_plus:soy_plant_1"},
{"farming_plus:tomato_seed", "farming_plus:tomato_1"},
{"farming_plus:walnut_item", "farming_plus:walnut_1"},
{"farming_plus:orange_seed", "farming_plus:orange_1"},
{"farming_plus:lemon_seed", "farming_plus:lemon_1"},
{"farming_plus:peach_seed", "farming_plus:peach_1"},
{"farming_plus:seed_barley", "farming_plus:barley_1"},
{"farming_plus:coffee_beans", "farming_plus:coffee_1"},
-- *** beer_test
{"beer_test:seed_oats", "beer_test:seed_oats"},
-- If you have Problems with growing wheat and cotton, then try the two lines below
{"farming:seed_wheat", "farming:wheat_1"},
{"farming:seed_cotton", "farming:cotton_1"}
-- If you haven't any Problems with growing wheat and cotton, then use the two lines below
-- {"farming:seed_wheat", "farming:seed_wheat"},
-- {"farming:seed_cotton", "farming:seed_cotton"}
}
-- wine and beans need climbing utilities
farmingNG.seeder_utils = {
{"farming_nextgen:grape_seedling", "farming_plus:grapes_1"},
{"farming_nextgen:bean_seedling", "farming_plus:beanpole_1"}
}
if farm_redo then
farmingNG.seeder_utils = {
{"farming_nextgen:grape_seedling", "farming:grapes_1"},
{"farming_nextgen:bean_seedling", "farming:beanpole_1"}
}
end
local function check_valid_util(sname)
for i in ipairs(farmingNG.seeder_utils) do
if sname == farmingNG.seeder_utils[i][1] then return true end
end
return false
end
-- function to check for valid seeds
local function check_valid_seed(sname)
for i in ipairs(farmingNG.seeder_seed) do
if sname == farmingNG.seeder_seed[i][1] then return true end
end
return false
end
--function to give name of seedlings
local function give_seedling(sname, util)
if not util then
for i in ipairs(farmingNG.seeder_seed) do
if sname == farmingNG.seeder_seed[i][1] then return farmingNG.seeder_seed[i][2] end
end
else
for i in ipairs(farmingNG.seeder_utils) do
if sname ==farmingNG.seeder_utils[i][1] then return farmingNG.seeder_utils[i][2] end
end
end
return nil
end
--- Iterator over positions to try to saw around a sawed node.
-- This returns positions in a 3x1x3 area around the position, plus the
-- position above it. This does not return the bottom position to prevent
-- the seeder from cutting down nodes below the cutting position.
-- @param pos Sawing position.
function farmingNG.iterSawTries(pos)
-- Copy position to prevent mangling it
local pos = vector.new(pos)
local i = 0
return function()
i = i + 1
-- Given a (top view) area like so (where 5 is the starting position):
-- X -->
-- Z 123
-- | 456
-- V 789
-- This will return positions 1, 4, 7, 2, 8 (skip 5), 3, 6, 9,
-- and the position above 5.
if i == 1 then
-- Move to starting position
pos.x = pos.x - 1
pos.z = pos.z - 1
elseif i == 4 or i == 7 then
-- Move to next X and back to start of Z when we reach
-- the end of a Z line.
pos.x = pos.x + 1
pos.z = pos.z - 2
elseif i == 5 then
-- Skip the middle position (we've already run on it)
-- and double-increment the counter.
pos.z = pos.z + 2
i = i + 1
elseif i <= 9 then
-- Go to next Z.
pos.z = pos.z + 1
elseif i == 10 then
-- Move back to center and up.
-- The Y+ position must be last so that we don't dig
-- straight upward and not come down (since the Y-
-- position isn't checked).
pos.x = pos.x - 1
pos.z = pos.z - 1
pos.y = pos.y + 1
else
return nil
end
return pos
end
end
-- This function does all the hard work. Recursively we dig the node at hand
-- if it is in the table and then search the surroundings for more stuff to dig.
local function recursive_dig(pos, remaining_charge, seednum,seedstack, user)
local uppos = {x =pos.x, y =(pos.y) +1,z =pos.z}
local toppos = {x =pos.x, y =(pos.y) +2,z =pos.z}
local name = user:get_player_name()
if remaining_charge < farmingNG.seeder_charge_per_node or seedstack:is_empty() then
return remaining_charge, seednum, seedstack
end
local node = minetest.get_node(pos)
local upper = minetest.get_node(uppos)
local top = minetest.get_node(toppos)
local seedname = seedstack:get_name()
local helpers = check_valid_util(upper.name)
if node.name == "farming:weed" then
remaining_charge, seednum, seedstack = recursive_dig( {x=pos.x, y=pos.y-1, z=pos.z}, remaining_charge, seednum, seedstack, user)
return remaining_charge, seednum, seedstack
end
if not soil_nodenames[node.name] then
return remaining_charge, seednum, seedstack
end
if not check_valid_util(seedname) then
if upper.name == "air" or upper.name == "farming:weed" or string.match(upper.name,"default:grass")then
minetest.set_node(uppos, {name="air"})
remaining_charge = remaining_charge - farmingNG.seeder_charge_per_node
seednum = seednum +1
seedstack:take_item()
if remaining_charge < 1 then remaining_charge = 1 end
if give_seedling(seedname,false) then
minetest.add_node(uppos, {name = give_seedling(seedname,false), param2 = 1})
minetest.get_node_timer(uppos):start(math.random(166, 286))
end
else
return remaining_charge, seednum, seedstack
end
else
if (upper.name == "air" or upper.name == "farming:weed" or string.match(upper.name,"default:grass")) and top.name == "air" then
minetest.set_node(uppos, {name="air"})
remaining_charge = remaining_charge - farmingNG.seeder_charge_per_node
seednum = seednum +1
seedstack:take_item()
if remaining_charge < 1 then remaining_charge = 1 end
if give_seedling(seedname, true) then
minetest.add_node(uppos, {name = give_seedling(seedname, true), param2 = 1})
minetest.get_node_timer(uppos):start(math.random(166, 286))
end
else
return remaining_charge, seednum, seedstack
end
end
-- Check surroundings and run recursively if any charge left
for npos in farmingNG.iterSawTries(pos) do
if remaining_charge < farmingNG.seeder_charge_per_node then
break
end
if soil_nodenames[minetest.get_node(npos).name] then
remaining_charge, seednum, seedstack = recursive_dig(npos, remaining_charge, seednum, seedstack, user)
end
end
return remaining_charge, seednum, seedstack
end
-- Seeder entry point
local function seeder_dig(pos, current_charge, seednum, seedstack, user)
-- Start sawing things down
local remaining_charge, seednum, seedstack = recursive_dig(pos, current_charge, seednum, seedstack, user)
minetest.sound_play("farming_nextgen_seeder", {pos = pos, gain = farmingNG.gain, max_hear_distance = 10})
return remaining_charge, seednum, seedstack
end
if farmingNG.havetech then
--local S = technic.getter
technic.register_power_tool("farming_nextgen:seeder", farmingNG.seeder_max_charge)
minetest.register_tool("farming_nextgen:seeder", {
description = S("Seed Machine"),
inventory_image = "farming_nextgen_seeder.png",
stack_max = 1,
wear_represents = "technic_RE_charge",
on_refill = technic.refill_RE_charge,
on_use = function(itemstack, user, pointed_thing)
local seednum=0
local name = user:get_player_name()
local privs = minetest.get_player_privs(name)
if pointed_thing.type ~= "node" then
return itemstack
end
local meta = minetest.deserialize(itemstack:get_metadata())
if not meta or not meta.charge or
meta.charge < farmingNG.seeder_charge_per_node then
return
end
local inv = user:get_inventory()
local indexnumber = user:get_wield_index()+1
local seedstack = inv:get_stack("main", indexnumber)
local seedname = seedstack:get_name()
local pos_above_soil = vector.add(pointed_thing.under,
{ x = 0, y = 1, z = 0 })
if minetest.is_protected(pos_above_soil, name) then
minetest.record_protection_violation(pos_above_soil, name)
return
end
-- Send current charge to digging function so that the
-- seeder will stop after digging a number of nodes
if seedname then
if check_valid_seed(seedname) or check_valid_util(seedname) then
meta.charge, seednum, seedstack = seeder_dig(pointed_thing.under, meta.charge, seednum, seedstack, user)
minetest.chat_send_player(name,S("*** You used : @1 seeds",seednum))
else
minetest.chat_send_player(name,S(" *** you need valid seeds on the right side of your device"))
end
else
minetest.chat_send_player(name,S(" *** you need valid seeds on the right side of your device"))
end
if not technic.creative_mode then
technic.set_RE_wear(itemstack, meta.charge, farmingNG.seeder_max_charge)
itemstack:set_metadata(minetest.serialize(meta))
end
inv:set_stack("main", indexnumber, seedstack)
return itemstack
end,
})
local mesecons_button = minetest.get_modpath("mesecons_button")
local trigger = mesecons_button and "mesecons_button:button_off" or "default:mese_crystal_fragment"
if farmingNG.easy then
minetest.register_craft({
output = "farming_nextgen:seeder",
recipe = {
{"technic:battery", trigger, "technic:battery" },
{"technic:stainless_steel_ingot", "technic:stainless_steel_ingot", "technic:stainless_steel_ingot"},
{"default:diamond", "", "default:diamond"},
}
})
else
minetest.register_craft({
output = "farming_nextgen:seeder",
recipe = {
{"technic:red_energy_crystal", trigger, "technic:red_energy_crystal" },
{"technic:composite_plate", "technic:composite_plate", "technic:composite_plate"},
{"technic:rubber", "", "technic:rubber"},
}
})
end
else
minetest.register_tool("farming_nextgen:seeder", {
description = S("Automatik seeding tool"),
groups = {soil=3,soil=2},
inventory_image = "farming_nextgen_seeder.png",
stack_max=1,
liquids_pointable = false,
on_use = function(itemstack, user, pointed_thing)
local seednum=0
local name = user:get_player_name()
local privs = minetest.get_player_privs(name)
if pointed_thing.type ~= "node" then
return itemstack
end
local charge = 65535 - itemstack:get_wear()
if not charge or
charge < farmingNG.seeder_charge_per_node then
minetest.chat_send_player(name,S(" *** Your device needs to be serviced"))
return
end
local inv = user:get_inventory()
local indexnumber = user:get_wield_index()+1
local seedstack = inv:get_stack("main", indexnumber)
local seedname = seedstack:get_name()
local pos_above_soil = vector.add(pointed_thing.under,
{ x = 0, y = 1, z = 0 })
if minetest.is_protected(pos_above_soil, name) then
minetest.record_protection_violation(pos_above_soil, name)
return
end
if seedname then
if check_valid_seed(seedname) or check_valid_util(seedname) then
charge, seednum, seedstack = seeder_dig(pointed_thing.under, charge, seednum, seedstack, user)
if farmingNG.chaton then
minetest.chat_send_player(name,S("*** You used : @1 seeds.",seednum) .. "\n" ..
S("Charge for @1 seeds left.",math.floor(charge/farmingNG.seeder_charge_per_node))
)
end
else
minetest.chat_send_player(name,S(" *** you need valid seeds on the right side of your device"))
end
else
minetest.chat_send_player(name,S(" *** you need valid seeds on the right side of your device"))
end
itemstack:set_wear(65535-charge)
inv:set_stack("main", indexnumber, seedstack)
return itemstack
end,
})
minetest.register_craft({
output = "farming_nextgen:seeder",
recipe = {
{"default:mese", "default:mese_crystal_fragment", "default:mese" },
{"default:gold_ingot", "default:bronze_ingot", "default:gold_ingot"},
{"default:diamond", "", "default:diamond"},
}
})
end