-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathharvester.lua
359 lines (299 loc) · 13 KB
/
harvester.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
-- The harvester
local S = farmingNG.S
-- farming
farmingNG.harvester_names["farming:artichoke_5"] = true
farmingNG.harvester_names["farming:wheat_8"] = true
farmingNG.harvester_names["farming:wheat_7"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:wheat_6"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:cotton_8"] = true
farmingNG.harvester_names["farming:cotton_7"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:cotton_6"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:weed"] = false
farmingNG.harvester_names["farming:pumpkin"] = true
farmingNG.harvester_names["farming:barley_8"] = true
farmingNG.harvester_names["farming:barley_7"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:barley_6"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:blueberry_4"] = true
farmingNG.harvester_names["farming:carrot_8"] = true
farmingNG.harvester_names["farming:chili_8"] = true
farmingNG.harvester_names["farming:coffee_5"] = true
farmingNG.harvester_names["farming:corn_8"] = true
farmingNG.harvester_names["farming:cucumber_4"] = true
farmingNG.harvester_names["farming:garlic_5"] = true
farmingNG.harvester_names["farming:hemp_8"] = true
farmingNG.harvester_names["farming:melon_8"] = true
farmingNG.harvester_names["farming:onion_5"] = true
farmingNG.harvester_names["farming:parsley_3"] = true
farmingNG.harvester_names["farming:pepper_5"] = true -- green
farmingNG.harvester_names["farming:pepper_6"] = true -- yellow
farmingNG.harvester_names["farming:pepper_7"] = true -- red
farmingNG.harvester_names["farming:pineapple_8"] = true
farmingNG.harvester_names["farming:potato_4"] = true
farmingNG.harvester_names["farming:pumpkin_8"] = true
farmingNG.harvester_names["farming:raspberry_4"] = true
farmingNG.harvester_names["farming:rhubarb_3"] = true
farmingNG.harvester_names["farming:tomato_8"] = true
farmingNG.harvester_names["farming:grapes_8"] = true
farmingNG.harvester_names["farming:beanpole_5"] = true
farmingNG.harvester_names["farming:pea_5"] = true
farmingNG.harvester_names["farming:beetroot_5"] = true
farmingNG.harvester_names["farming:rice_8"] = true
farmingNG.harvester_names["farming:rice_7"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:rice_6"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:oat_8"] = true
farmingNG.harvester_names["farming:oat_7"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:oat_6"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:rye_8"] = true
farmingNG.harvester_names["farming:rye_7"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:rye_6"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:cabbage_6"] = true
farmingNG.harvester_names["farming:mint_4"] = true
farmingNG.harvester_names["farming:lettuce_5"] = true
farmingNG.harvester_names["farming:blackberry_4"] = true
farmingNG.harvester_names["farming:soy_6"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:soy_7"] = true
farmingNG.harvester_names["farming:sunflower_8"] = true
farmingNG.harvester_names["farming:vanilla_7"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:vanilla_8"] = true
farmingNG.harvester_names["farming:spinach_3"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:spinach_4"] = true
farmingNG.harvester_names["farming:ginger_3"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:ginger_4"] = true
farmingNG.harvester_names["farming:eggplant_3"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:eggplant_4"] = true
farmingNG.harvester_names["farming:asparagus_4"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["farming:asparagus_5"] = true
-- farming redo also adds strawberry with ethereal mod name
farmingNG.harvester_names["ethereal:strawberry_6"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["ethereal:strawberry_7"] = farmingNG.harvester_nofullg
farmingNG.harvester_names["ethereal:strawberry_8"] = true
-- beer_test
farmingNG.harvester_names["beer_test:oats_8"] = true
-- farming_plus
farmingNG.harvester_names["farming_plus:melon"] = true
farmingNG.harvester_names["farming_plus:tomato"] = true
farmingNG.harvester_names["farming_plus:chilli"] = true
farmingNG.harvester_names["farming_plus:garlic"] = true
farmingNG.harvester_names["farming_plus:soy_plant"] = true
farmingNG.harvester_names["farming_plus:cucumber"] = true
farmingNG.harvester_names["farming_plus:coffee"] = true
farmingNG.harvester_names["farming_plus:potato"] = true
farmingNG.harvester_names["farming_plus:carrot"] = true
farmingNG.harvester_names["farming_plus:garlic"] = true
farmingNG.harvester_names["farming_plus:rhubarb"]= true
farmingNG.harvester_names["farming_plus:barley_7"]= true
farmingNG.harvester_names["farming_plus:hemp_8"]= true
farmingNG.harvester_names["farming_plus:beanpole_5"]= true
farmingNG.harvester_names["farming_plus:grapes_8"]= true
farmingNG.harvester_names["farming_plus:corn"] = true
farmingNG.harvester_names["farming_plus:cornb"] = true
farmingNG.harvester_names["farming_plus:cornc"] = true
if not farmingNG.havetech then
farmingNG.harvester_charge_per_node = math.floor(65535 / farmingNG.harvester_max_charge * farmingNG.harvester_charge_per_node)
farmingNG.harvester_max_charge = 65535
end
-- Table for saving what was sawed down
local produced = {}
-- Save the items sawed down so that we can drop them in a nice single stack
local function handle_drops(drops)
for _, item in ipairs(drops) do
local stack = ItemStack(item)
local name = stack:get_name()
local p = produced[name]
if not p then
produced[name] = stack
else
p:set_count(p:get_count() + stack:get_count())
end
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_harvest(pos, remaining_charge)
if remaining_charge < farmingNG.harvester_charge_per_node then
return remaining_charge
end
local node = minetest.get_node(pos)
if not farmingNG.harvester_names[node.name] then
return remaining_charge
end
handle_drops(minetest.get_node_drops(node.name, ""))
minetest.remove_node(pos)
remaining_charge = remaining_charge - farmingNG.harvester_charge_per_node
if remaining_charge < 1 then remaining_charge = 1 end
-- Check surroundings and run recursively if any charge left
for npos in farmingNG.iterSawTries(pos) do
if remaining_charge < farmingNG.harvester_charge_per_node then
break
end
if farmingNG.harvester_names[minetest.get_node(npos).name] then
remaining_charge = recursive_harvest(npos, remaining_charge)
end
end
return remaining_charge
end
-- Function to randomize positions for new node drops
local function get_drop_pos(pos)
local drop_pos = {}
for i = 0, 8 do
-- Randomize position for a new drop
drop_pos.x = pos.x + math.random(-3, 3)
drop_pos.y = pos.y - 1
drop_pos.z = pos.z + math.random(-3, 3)
-- Move the randomized position upwards until
-- the node is air or unloaded.
for y = drop_pos.y, drop_pos.y + 5 do
drop_pos.y = y
local node = minetest.get_node_or_nil(drop_pos)
if not node then
-- If the node is not loaded yet simply drop
-- the item at the original digging position.
return pos
elseif node.name == "air" then
-- Add variation to the entity drop position,
-- but don't let drops get too close to the edge
drop_pos.x = drop_pos.x + (math.random() * 0.8) - 0.5
drop_pos.z = drop_pos.z + (math.random() * 0.8) - 0.5
return drop_pos
end
end
end
-- Return the original position if this takes too long
return pos
end
-- entry point
local function harvester_dig(pos, current_charge)
-- Start sawing things down
local remaining_charge = recursive_harvest(pos, current_charge)
minetest.sound_play("farming_nextgen_seeder", {pos = pos, gain = farmingNG.gain,
max_hear_distance = 10})
-- Now drop items for the player
for name, stack in pairs(produced) do
-- Drop stacks of stack max or less
local count, max = stack:get_count(), stack:get_stack_max()
stack:set_count(max)
while count > max do
minetest.add_item(get_drop_pos(pos), stack)
count = count - max
end
stack:set_count(count)
minetest.add_item(get_drop_pos(pos), stack)
end
-- Clean up
produced = {}
return remaining_charge
end
if farmingNG.havetech then
if technic.plus then
technic.register_power_tool("farming_nextgen:harvester", {
description = S("Harvester"),
inventory_image = "farming_nextgen_harvester.png",
max_charge = farmingNG.harvester_max_charge,
on_use = function(itemstack, user, pointed_thing)
local name = user:get_player_name()
if pointed_thing.type ~= "node" then
return
end
local charge = technic.get_RE_charge(itemstack)
if charge < farmingNG.harvester_charge_per_node then
return
end
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
-- harvester will stop after digging a number of nodes
charge = harvester_dig(pointed_thing.under, charge)
if not technic.creative_mode then
technic.set_RE_charge(itemstack, charge)
return itemstack
end
end,
})
else
technic.register_power_tool("farming_nextgen:harvester", farmingNG.harvester_max_charge)
minetest.register_tool("farming_nextgen:harvester", {
description = S("Harvester"),
inventory_image = "farming_nextgen_harvester.png",
stack_max = 1,
wear_represents = "technic_RE_charge",
on_refill = technic.refill_RE_charge,
on_use = function(itemstack, user, pointed_thing)
local name = user:get_player_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.harvester_charge_per_node then
return
end
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
-- harvester will stop after digging a number of nodes
meta.charge = harvester_dig(pointed_thing.under, meta.charge)
if not technic.creative_mode then
technic.set_RE_wear(itemstack, meta.charge, farmingNG.harvester_max_charge)
itemstack:set_metadata(minetest.serialize(meta))
end
return itemstack
end,
})
end
local mesecons_button = minetest.get_modpath("mesecons_button")
local trigger = mesecons_button and "mesecons_button:button_off" or "default:mese_crystal_fragment"
minetest.register_craft({
output = "farming_nextgen:harvester",
recipe = {
{"technic:battery", trigger, "technic:battery"},
{"technic:diamond_drill_head", "technic:machine_casing", "technic:diamond_drill_head"},
{"technic:rubber", "", "technic:rubber"},
}
})
else
minetest.register_tool("farming_nextgen:harvester", {
description = S("Harvester"),
groups = {soil=3,soil=2},
inventory_image = "farming_nextgen_harvester.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.harvester_charge_per_node then
minetest.chat_send_player(name,S(" *** Your device needs to be serviced"))
return
end
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
charge = harvester_dig(pointed_thing.under, charge)
itemstack:set_wear(65535-charge)
return itemstack
end,
})
minetest.register_craft({
output = "farming_nextgen:harvester",
recipe = {
{"default:diamondblock", "default:mese_crystal_fragment", "default:diamondblock" },
{"default:gold_ingot", "default:bronze_ingot", "default:gold_ingot"},
{"default:mese_crystal_fragment", "", "default:mese_crystal_fragment"},
}
})
end