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