mat9117
2019-04-14 a9b10cc4b10110a9c54bd03ac07eea3151b10ede
commit | author | age
8e03d7 1 -- Configuration
R 2
9019eb 3 local chainsaw_max_charge      = 30000 -- Maximum charge of the saw
S 4 -- Gives 2500 nodes on a single charge (about 50 complete normal trees)
5 local chainsaw_charge_per_node = 12
6 -- Cut down tree leaves.  Leaf decay may cause slowness on large trees
7 -- if this is disabled.
8 local chainsaw_leaves = true
9
10 -- The default trees
11 local timber_nodenames = {
584cd8 12     ["default:acacia_tree"] = true,
N 13     ["default:aspen_tree"]  = true,
14     ["default:jungletree"]  = true,
15     ["default:papyrus"]     = true,
16     ["default:cactus"]      = true,
17     ["default:tree"]        = true,
18     ["default:apple"]       = true,
19     ["default:pine_tree"]    = true,
8e03d7 20 }
82cba9 21
9019eb 22 if chainsaw_leaves then
584cd8 23     timber_nodenames["default:acacia_leaves"] = true
N 24     timber_nodenames["default:aspen_leaves"] = true
918166 25     timber_nodenames["default:leaves"] = true
N 26     timber_nodenames["default:jungleleaves"] = true
0114fe 27     timber_nodenames["default:pine_needles"] = true
8e03d7 28 end
82cba9 29
ee0765 30 -- technic_worldgen defines rubber trees if moretrees isn't installed
S 31 if minetest.get_modpath("technic_worldgen") or
9019eb 32         minetest.get_modpath("moretrees") then
ee0765 33     timber_nodenames["moretrees:rubber_tree_trunk_empty"] = true
S 34     timber_nodenames["moretrees:rubber_tree_trunk"]       = true
35     if chainsaw_leaves then
c63658 36         timber_nodenames["moretrees:rubber_tree_leaves"] = true
ee0765 37     end
S 38 end
39
8e03d7 40 -- Support moretrees if it is there
9019eb 41 if minetest.get_modpath("moretrees") then
9e1c57 42     timber_nodenames["moretrees:acacia_trunk"]                = true
c63658 43     timber_nodenames["moretrees:apple_tree_trunk"]                 = true
P 44     timber_nodenames["moretrees:beech_trunk"]                      = true
45     timber_nodenames["moretrees:birch_trunk"]                      = true
46     timber_nodenames["moretrees:fir_trunk"]                        = true
47     timber_nodenames["moretrees:oak_trunk"]                        = true
48     timber_nodenames["moretrees:palm_trunk"]                       = true
49     timber_nodenames["moretrees:pine_trunk"]                       = true
50     timber_nodenames["moretrees:sequoia_trunk"]                    = true
51     timber_nodenames["moretrees:spruce_trunk"]                     = true
52     timber_nodenames["moretrees:willow_trunk"]                     = true
53     timber_nodenames["moretrees:jungletree_trunk"]                 = true
700fac 54     timber_nodenames["moretrees:poplar_trunk"]                     = true
82cba9 55
c63658 56     if chainsaw_leaves then
0114fe 57         timber_nodenames["moretrees:acacia_leaves"]            = true
c63658 58         timber_nodenames["moretrees:apple_tree_leaves"]        = true
P 59         timber_nodenames["moretrees:oak_leaves"]               = true
60         timber_nodenames["moretrees:fir_leaves"]               = true
61         timber_nodenames["moretrees:fir_leaves_bright"]        = true
62         timber_nodenames["moretrees:sequoia_leaves"]           = true
63         timber_nodenames["moretrees:birch_leaves"]             = true
64         timber_nodenames["moretrees:birch_leaves"]             = true
65         timber_nodenames["moretrees:palm_leaves"]              = true
66         timber_nodenames["moretrees:spruce_leaves"]            = true
67         timber_nodenames["moretrees:spruce_leaves"]            = true
68         timber_nodenames["moretrees:pine_leaves"]              = true
69         timber_nodenames["moretrees:willow_leaves"]            = true
70         timber_nodenames["moretrees:jungletree_leaves_green"]  = true
71         timber_nodenames["moretrees:jungletree_leaves_yellow"] = true
72         timber_nodenames["moretrees:jungletree_leaves_red"]    = true
dd3083 73         timber_nodenames["moretrees:acorn"]                    = true
N 74         timber_nodenames["moretrees:coconut"]                  = true
75         timber_nodenames["moretrees:spruce_cone"]              = true
76         timber_nodenames["moretrees:pine_cone"]                = true
77         timber_nodenames["moretrees:fir_cone"]                 = true
78         timber_nodenames["moretrees:apple_blossoms"]           = true
700fac 79         timber_nodenames["moretrees:poplar_leaves"]            = true
c63658 80     end
8e03d7 81 end
82cba9 82
9019eb 83 -- Support growing_trees
S 84 if minetest.get_modpath("growing_trees") then
c63658 85     timber_nodenames["growing_trees:trunk"]         = true
P 86     timber_nodenames["growing_trees:medium_trunk"]  = true
87     timber_nodenames["growing_trees:big_trunk"]     = true
88     timber_nodenames["growing_trees:trunk_top"]     = true
89     timber_nodenames["growing_trees:trunk_sprout"]  = true
90     timber_nodenames["growing_trees:branch_sprout"] = true
91     timber_nodenames["growing_trees:branch"]        = true
92     timber_nodenames["growing_trees:branch_xmzm"]   = true
93     timber_nodenames["growing_trees:branch_xpzm"]   = true
94     timber_nodenames["growing_trees:branch_xmzp"]   = true
95     timber_nodenames["growing_trees:branch_xpzp"]   = true
96     timber_nodenames["growing_trees:branch_zz"]     = true
97     timber_nodenames["growing_trees:branch_xx"]     = true
82cba9 98
9019eb 99     if chainsaw_leaves then
c63658 100         timber_nodenames["growing_trees:leaves"] = true
P 101     end
8e03d7 102 end
R 103
9019eb 104 -- Support growing_cactus
S 105 if minetest.get_modpath("growing_cactus") then
c63658 106     timber_nodenames["growing_cactus:sprout"]                       = true
P 107     timber_nodenames["growing_cactus:branch_sprout_vertical"]       = true
108     timber_nodenames["growing_cactus:branch_sprout_vertical_fixed"] = true
109     timber_nodenames["growing_cactus:branch_sprout_xp"]             = true
110     timber_nodenames["growing_cactus:branch_sprout_xm"]             = true
111     timber_nodenames["growing_cactus:branch_sprout_zp"]             = true
112     timber_nodenames["growing_cactus:branch_sprout_zm"]             = true
113     timber_nodenames["growing_cactus:trunk"]                        = true
114     timber_nodenames["growing_cactus:branch_trunk"]                 = true
115     timber_nodenames["growing_cactus:branch"]                       = true
116     timber_nodenames["growing_cactus:branch_xp"]                    = true
117     timber_nodenames["growing_cactus:branch_xm"]                    = true
118     timber_nodenames["growing_cactus:branch_zp"]                    = true
119     timber_nodenames["growing_cactus:branch_zm"]                    = true
120     timber_nodenames["growing_cactus:branch_zz"]                    = true
121     timber_nodenames["growing_cactus:branch_xx"]                    = true
8e03d7 122 end
R 123
9019eb 124 -- Support farming_plus
S 125 if minetest.get_modpath("farming_plus") then
126     if chainsaw_leaves then
0114fe 127         timber_nodenames["farming_plus:banana_leaves"] = true
T 128         timber_nodenames["farming_plus:banana"]        = true
129         timber_nodenames["farming_plus:cocoa_leaves"]  = true
130         timber_nodenames["farming_plus:cocoa"]         = true
c63658 131     end
8e03d7 132 end
R 133
dd3083 134 -- Support nature
N 135 if minetest.get_modpath("nature") then
136     if chainsaw_leaves then
137         timber_nodenames["nature:blossom"] = true
138     end
139 end
5cf765 140
0114fe 141 -- Support snow
T 142 if minetest.get_modpath("snow") then
143     if chainsaw_leaves then
144         timber_nodenames["snow:needles"] = true
145         timber_nodenames["snow:needles_decorated"] = true
146         timber_nodenames["snow:star"] = true
147     end
148 end
149
150 -- Support vines (also generated by moretrees if available)
151 if minetest.get_modpath("vines") then
152     if chainsaw_leaves then
153         timber_nodenames["vines:vines"] = true
154     end
155 end
156
72d541 157 if minetest.get_modpath("trunks") then
T 158     if chainsaw_leaves then
159         timber_nodenames["trunks:moss"] = true
160         timber_nodenames["trunks:moss_fungus"] = true
161         timber_nodenames["trunks:treeroot"] = true
162     end
163 end
164
5cf765 165 local S = technic.getter
S 166
167 technic.register_power_tool("technic:chainsaw", chainsaw_max_charge)
168
8e03d7 169 -- Table for saving what was sawed down
9019eb 170 local produced = {}
8e03d7 171
9019eb 172 -- Save the items sawed down so that we can drop them in a nice single stack
S 173 local function handle_drops(drops)
174     for _, item in ipairs(drops) do
175         local stack = ItemStack(item)
176         local name = stack:get_name()
177         local p = produced[name]
178         if not p then
179             produced[name] = stack
c63658 180         else
9019eb 181             p:set_count(p:get_count() + stack:get_count())
c63658 182         end
9019eb 183     end
S 184 end
185
186 --- Iterator over positions to try to saw around a sawed node.
ec069a 187 -- This returns positions in a 3x1x3 area around the position, plus the
S 188 -- position above it.  This does not return the bottom position to prevent
189 -- the chainsaw from cutting down nodes below the cutting position.
16302c 190 -- @param pos Sawing position.
9019eb 191 local function iterSawTries(pos)
16302c 192     -- Copy position to prevent mangling it
S 193     local pos = vector.new(pos)
ec069a 194     local i = 0
9019eb 195
S 196     return function()
ec069a 197         i = i + 1
S 198         -- Given a (top view) area like so (where 5 is the starting position):
199         -- X -->
200         -- Z 123
201         -- | 456
202         -- V 789
203         -- This will return positions 1, 4, 7, 2, 8 (skip 5), 3, 6, 9,
204         -- and the position above 5.
205         if i == 1 then
206             -- Move to starting position
207             pos.x = pos.x - 1
208             pos.z = pos.z - 1
209         elseif i == 4 or i == 7 then
210             -- Move to next X and back to start of Z when we reach
211             -- the end of a Z line.
9019eb 212             pos.x = pos.x + 1
ec069a 213             pos.z = pos.z - 2
S 214         elseif i == 5 then
215             -- Skip the middle position (we've already run on it)
216             -- and double-increment the counter.
217             pos.z = pos.z + 2
218             i = i + 1
219         elseif i <= 9 then
220             -- Go to next Z.
221             pos.z = pos.z + 1
222         elseif i == 10 then
223             -- Move back to center and up.
224             -- The Y+ position must be last so that we don't dig
225             -- straight upward and not come down (since the Y-
226             -- position isn't checked).
227             pos.x = pos.x - 1
228             pos.z = pos.z - 1
229             pos.y = pos.y + 1
9019eb 230         else
ec069a 231             return nil
9019eb 232         end
S 233         return pos
c63658 234     end
8e03d7 235 end
R 236
237 -- This function does all the hard work. Recursively we dig the node at hand
238 -- if it is in the table and then search the surroundings for more stuff to dig.
9019eb 239 local function recursive_dig(pos, remaining_charge)
S 240     if remaining_charge < chainsaw_charge_per_node then
c63658 241         return remaining_charge
P 242     end
9019eb 243     local node = minetest.get_node(pos)
S 244
245     if not timber_nodenames[node.name] then
246         return remaining_charge
247     end
248
ec069a 249     -- Wood found - cut it
9019eb 250     handle_drops(minetest.get_node_drops(node.name, ""))
S 251     minetest.remove_node(pos)
252     remaining_charge = remaining_charge - chainsaw_charge_per_node
253
254     -- Check surroundings and run recursively if any charge left
16302c 255     for npos in iterSawTries(pos) do
9019eb 256         if remaining_charge < chainsaw_charge_per_node then
S 257             break
258         end
16302c 259         if timber_nodenames[minetest.get_node(npos).name] then
S 260             remaining_charge = recursive_dig(npos, remaining_charge)
9019eb 261         end
S 262     end
c63658 263     return remaining_charge
P 264 end
265
266 -- Function to randomize positions for new node drops
267 local function get_drop_pos(pos)
268     local drop_pos = {}
269
270     for i = 0, 8 do
271         -- Randomize position for a new drop
272         drop_pos.x = pos.x + math.random(-3, 3)
273         drop_pos.y = pos.y - 1
274         drop_pos.z = pos.z + math.random(-3, 3)
275
276         -- Move the randomized position upwards until
277         -- the node is air or unloaded.
278         for y = drop_pos.y, drop_pos.y + 5 do
279             drop_pos.y = y
280             local node = minetest.get_node_or_nil(drop_pos)
281
282             if not node then
283                 -- If the node is not loaded yet simply drop
284                 -- the item at the original digging position.
285                 return pos
286             elseif node.name == "air" then
9019eb 287                 -- Add variation to the entity drop position,
S 288                 -- but don't let drops get too close to the edge
289                 drop_pos.x = drop_pos.x + (math.random() * 0.8) - 0.5
290                 drop_pos.z = drop_pos.z + (math.random() * 0.8) - 0.5
c63658 291                 return drop_pos
P 292             end
293         end
294     end
295
296     -- Return the original position if this takes too long
297     return pos
e23f87 298 end
19c9a0 299
9019eb 300 -- Chainsaw entry point
S 301 local function chainsaw_dig(pos, current_charge)
302     -- Start sawing things down
303     local remaining_charge = recursive_dig(pos, current_charge)
304     minetest.sound_play("chainsaw", {pos = pos, gain = 1.0,
305             max_hear_distance = 10})
5cf765 306
9019eb 307     -- Now drop items for the player
S 308     for name, stack in pairs(produced) do
309         -- Drop stacks of stack max or less
310         local count, max = stack:get_count(), stack:get_stack_max()
311         stack:set_count(max)
312         while count > max do
313             minetest.add_item(get_drop_pos(pos), stack)
314             count = count - max
c63658 315         end
9019eb 316         stack:set_count(count)
S 317         minetest.add_item(get_drop_pos(pos), stack)
318     end
319
320     -- Clean up
321     produced = {}
322
323     return remaining_charge
5cf765 324 end
S 325
326
327 minetest.register_tool("technic:chainsaw", {
328     description = S("Chainsaw"),
329     inventory_image = "technic_chainsaw.png",
330     stack_max = 1,
99fd5d 331     wear_represents = "technic_RE_charge",
00d7c9 332     on_refill = technic.refill_RE_charge,
5cf765 333     on_use = function(itemstack, user, pointed_thing)
S 334         if pointed_thing.type ~= "node" then
335             return itemstack
336         end
9019eb 337
5cf765 338         local meta = minetest.deserialize(itemstack:get_metadata())
9019eb 339         if not meta or not meta.charge or
S 340                 meta.charge < chainsaw_charge_per_node then
5cf765 341             return
S 342         end
343
9019eb 344         local name = user:get_player_name()
51c02e 345         if minetest.is_protected(pointed_thing.under, name) then
S 346             minetest.record_protection_violation(pointed_thing.under, name)
347             return
9019eb 348         end
S 349
350         -- Send current charge to digging function so that the
351         -- chainsaw will stop after digging a number of nodes
352         meta.charge = chainsaw_dig(pointed_thing.under, meta.charge)
b8c902 353         if not technic.creative_mode then
M 354             technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge)
355             itemstack:set_metadata(minetest.serialize(meta))
356         end
5cf765 357         return itemstack
S 358     end,
359 })
360
e4c34c 361 local mesecons_button = minetest.get_modpath("mesecons_button")
T 362 local trigger = mesecons_button and "mesecons_button:button_off" or "default:mese_crystal_fragment"
363
5cf765 364 minetest.register_craft({
9019eb 365     output = "technic:chainsaw",
c63658 366     recipe = {
e4c34c 367         {"technic:stainless_steel_ingot", trigger,                      "technic:battery"},
44cb8d 368         {"basic_materials:copper_wire",      "basic_materials:motor",              "technic:battery"},
5e4a87 369         {"",                              "",                           "technic:stainless_steel_ingot"},
44cb8d 370     },
VD 371     replacements = { {"basic_materials:copper_wire", "basic_materials:empty_spool"}, },
372
5cf765 373 })
9019eb 374