Cristiano Magro
2020-10-11 08b09aab94471133a80fb8bc984f4f0689350959
commit | author | age
08b09a 1 -- Configuration
CM 2
3 local xnotreetrap_max_charge      = 30000 -- Maximum charge of the saw
4 -- Gives 2500 nodes on a single charge (about 50 complete normal trees)
5 local xnotreetrap_charge_per_node = 12
6 -- Cut down tree leaves.  Leaf decay may cause slowness on large trees
7 -- if this is disabled.
8 local xnotreetrap_leaves = true
9
10 -- First value is node name; second is whether the node is considered even if chainsaw_leaves is false.
11 local nodes = {
12     -- Rubber trees from moretrees or technic_worldgen if moretrees isn't installed
13     {"moretrees:rubber_tree_trunk_empty", true},
14     {"moretrees:rubber_tree_trunk", true},
15     {"moretrees:rubber_tree_leaves", false},
16 }
17
18 local timber_nodenames = {}
19 for _, node in pairs(nodes) do
20     if chainsaw_leaves or node[2] then
21         timber_nodenames[node[1]] = true
22     end
23 end
24
482cb1 25
CM 26 local S = technic.getter
27 local mesecons_materials = minetest.get_modpath("mesecons_materials")
28
08b09a 29 technic.register_power_tool("technic:xnotreetap", xnotreetap_max_charge)
CM 30
31 -- This function checks if the specified node should be sawed
32 local function check_if_node_sawed(pos)
33     local node_name = minetest.get_node(pos).name
34     if timber_nodenames[node_name]
35             or (xnotreetap_leaves and minetest.get_item_group(node_name, "leaves") ~= 0)
36             or minetest.get_item_group(node_name, "tree") ~= 0 then
37         return true
38     end
39
40     return false
41 end
42
43 -- Table for saving what was sawed down
44 local produced = {}
45
46 -- Save the items sawed down so that we can drop them in a nice single stack
47 local function handle_drops(drops)
48     for _, item in ipairs(drops) do
49         local stack = ItemStack(item)
50         local name = stack:get_name()
51         local p = produced[name]
52         if not p then
53             produced[name] = stack
54         else
55             p:set_count(p:get_count() + stack:get_count())
56         end
57     end
58 end
59
60 --- Iterator over positions to try to saw around a sawed node.
61 -- This returns positions in a 3x1x3 area around the position, plus the
62 -- position above it.  This does not return the bottom position to prevent
63 -- the chainsaw from cutting down nodes below the cutting position.
64 -- @param pos Sawing position.
65 local function iterSawTries(pos)
66     -- Copy position to prevent mangling it
67     local pos = vector.new(pos)
68     local i = 0
69
70     return function()
71         i = i + 1
72         -- Given a (top view) area like so (where 5 is the starting position):
73         -- X -->
74         -- Z 123
75         -- | 456
76         -- V 789
77         -- This will return positions 1, 4, 7, 2, 8 (skip 5), 3, 6, 9,
78         -- and the position above 5.
79         if i == 1 then
80             -- Move to starting position
81             pos.x = pos.x - 1
82             pos.z = pos.z - 1
83         elseif i == 4 or i == 7 then
84             -- Move to next X and back to start of Z when we reach
85             -- the end of a Z line.
86             pos.x = pos.x + 1
87             pos.z = pos.z - 2
88         elseif i == 5 then
89             -- Skip the middle position (we've already run on it)
90             -- and double-increment the counter.
91             pos.z = pos.z + 2
92             i = i + 1
93         elseif i <= 9 then
94             -- Go to next Z.
95             pos.z = pos.z + 1
96         elseif i == 10 then
97             -- Move back to center and up.
98             -- The Y+ position must be last so that we don't dig
99             -- straight upward and not come down (since the Y-
100             -- position isn't checked).
101             pos.x = pos.x - 1
102             pos.z = pos.z - 1
103             pos.y = pos.y + 1
104         else
105             return nil
106         end
107         return pos
108     end
109 end
110
111
112
113
114 -- This function does all the hard work. Recursively we dig the node at hand
115 -- if it is in the table and then search the surroundings for more stuff to dig.
116 local function recursive_dig(pos, remaining_charge)
117     if remaining_charge < xnotreetap_charge_per_node then
118         return remaining_charge
119     end
120     local node = minetest.get_node(pos)
121
122     if not check_if_node_sawed(pos) then
123         return remaining_charge
124     end
125
126     -- Wood found - cut it
127     handle_drops(minetest.get_node_drops(node.name, ""))
128     minetest.remove_node(pos)
129     remaining_charge = remaining_charge - xnotreetap_charge_per_node
130
131     -- Check surroundings and run recursively if any charge left
132     for npos in iterSawTries(pos) do
133         if remaining_charge < xnotreetap_charge_per_node then
134             break
135         end
136         if check_if_node_sawed(npos) then
137             remaining_charge = recursive_dig(npos, remaining_charge)
138         else
139             minetest.check_for_falling(npos)
140         end
141     end
142     return remaining_charge
143 end
144
145 -- Function to randomize positions for new node drops
146 local function get_drop_pos(pos)
147     local drop_pos = {}
148
149     for i = 0, 8 do
150         -- Randomize position for a new drop
151         drop_pos.x = pos.x + math.random(-3, 3)
152         drop_pos.y = pos.y - 1
153         drop_pos.z = pos.z + math.random(-3, 3)
154
155         -- Move the randomized position upwards until
156         -- the node is air or unloaded.
157         for y = drop_pos.y, drop_pos.y + 5 do
158             drop_pos.y = y
159             local node = minetest.get_node_or_nil(drop_pos)
160
161             if not node then
162                 -- If the node is not loaded yet simply drop
163                 -- the item at the original digging position.
164                 return pos
165             elseif node.name == "air" then
166                 -- Add variation to the entity drop position,
167                 -- but don't let drops get too close to the edge
168                 drop_pos.x = drop_pos.x + (math.random() * 0.8) - 0.5
169                 drop_pos.z = drop_pos.z + (math.random() * 0.8) - 0.5
170                 return drop_pos
171             end
172         end
173     end
174
175     -- Return the original position if this takes too long
176     return pos
177 end
178
179 -- Chainsaw entry point
180 local function xnotreetap_dig(pos, current_charge)
181     -- Start sawing things down
182     local remaining_charge = recursive_dig(pos, current_charge)
183     minetest.sound_play("chainsaw", {pos = pos, gain = 1.0,
184             max_hear_distance = 10})
185
186     -- Now drop items for the player
187     for name, stack in pairs(produced) do
188         -- Drop stacks of stack max or less
189         local count, max = stack:get_count(), stack:get_stack_max()
190         stack:set_count(max)
191         while count > max do
192             minetest.add_item(get_drop_pos(pos), stack)
193             count = count - max
194         end
195         stack:set_count(count)
196         minetest.add_item(get_drop_pos(pos), stack)
197     end
198
199     -- Clean up
200     produced = {}
201
202     return remaining_charge
203 end
204
482cb1 205 minetest.register_tool("technic:xnotreetap", {
CM 206     description = S("Xno Tree Tap"),
207     inventory_image = "technic_tree_tap.png",
08b09a 208     stack_max = 1,
CM 209     wear_represents = "technic_RE_charge",
210     on_refill = technic.refill_RE_charge,
482cb1 211     on_use = function(itemstack, user, pointed_thing)
CM 212         if pointed_thing.type ~= "node" then
08b09a 213             return itemstack
CM 214         end
215
216         //check tool charge
217         local meta = minetest.deserialize(itemstack:get_metadata())
218         if not meta or not meta.charge or
219                 meta.charge < xnotreetap_charge_per_node then
482cb1 220             return
CM 221         end
08b09a 222
CM 223         //check node protection
482cb1 224         local pos = pointed_thing.under
CM 225         if minetest.is_protected(pos, user:get_player_name()) then
226             minetest.record_protection_violation(pos, user:get_player_name())
227             return
228         end
08b09a 229
CM 230
482cb1 231         local node = minetest.get_node(pos)
CM 232         local node_name = node.name
233         if node_name ~= "moretrees:rubber_tree_trunk" then
234             return
235         end
08b09a 236
CM 237         //raccolta gomma
482cb1 238         node.name = "moretrees:rubber_tree_trunk_empty"
CM 239         minetest.swap_node(pos, node)
240         minetest.handle_node_drops(pointed_thing.above, {"technic:raw_latex"}, user)
08b09a 241
482cb1 242         if not technic.creative_mode then
CM 243             local item_wear = tonumber(itemstack:get_wear())
244             item_wear = item_wear + 819
245             if item_wear > 65535 then
246                 itemstack:clear()
247                 return itemstack
248             end
249             itemstack:set_wear(item_wear)
250         end
251         return itemstack
08b09a 252
CM 253         -- Send current charge to digging function so that the
254         -- chainsaw will stop after digging a number of nodes
255         meta.charge = xnotreetap_dig(pointed_thing.under, meta.charge)
256         if not technic.creative_mode then
257             technic.set_RE_wear(itemstack, meta.charge, xnotreetap_max_charge)
258             itemstack:set_metadata(minetest.serialize(meta))
259         end
260         return itemstack
261
482cb1 262     end,
CM 263 })
264
265 minetest.register_craft({
266     output = "technic:xnotreetap",
267     recipe = {
268         {"pipeworks:tube_1", "group:wood",    "default:stick"},
08b09a 269         {"technic:battery",  "default:stick", "default:stick"}
CM 270         {"technic:battery",  "default:stick", "default:stick"}
482cb1 271     },
CM 272 })
273
274 minetest.register_abm({
275     label = "Tools: xno tree tap",
276     nodenames = {"moretrees:rubber_tree_trunk_empty"},
277     interval = 60,
278     chance = 15,
279     action = function(pos, node)
280         if minetest.find_node_near(pos, (moretrees and moretrees.leafdecay_radius) or 5, {"moretrees:rubber_tree_leaves"}) then
281             node.name = "moretrees:rubber_tree_trunk"
282             minetest.swap_node(pos, node)
283         end
284     end
285 })
286
08b09a 287
CM 288
289
290
291