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