ShadowNinja
2014-06-26 51c02e9c8a7b95befb5134a71f61f8ae92804ee5
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,
8e03d7 17 }
82cba9 18
9019eb 19 if chainsaw_leaves then
918166 20     timber_nodenames["default:leaves"] = true
N 21     timber_nodenames["default:jungleleaves"] = true
8e03d7 22 end
82cba9 23
ee0765 24 -- technic_worldgen defines rubber trees if moretrees isn't installed
S 25 if minetest.get_modpath("technic_worldgen") or
9019eb 26         minetest.get_modpath("moretrees") then
ee0765 27     timber_nodenames["moretrees:rubber_tree_trunk_empty"] = true
S 28     timber_nodenames["moretrees:rubber_tree_trunk"]       = true
29     if chainsaw_leaves then
c63658 30         timber_nodenames["moretrees:rubber_tree_leaves"] = true
ee0765 31     end
S 32 end
33
8e03d7 34 -- Support moretrees if it is there
9019eb 35 if minetest.get_modpath("moretrees") then
c63658 36     timber_nodenames["moretrees:apple_tree_trunk"]                 = true
P 37     timber_nodenames["moretrees:apple_tree_trunk_sideways"]        = true
38     timber_nodenames["moretrees:beech_trunk"]                      = true
39     timber_nodenames["moretrees:beech_trunk_sideways"]             = true
40     timber_nodenames["moretrees:birch_trunk"]                      = true
41     timber_nodenames["moretrees:birch_trunk_sideways"]             = true
42     timber_nodenames["moretrees:fir_trunk"]                        = true
43     timber_nodenames["moretrees:fir_trunk_sideways"]               = true
44     timber_nodenames["moretrees:oak_trunk"]                        = true
45     timber_nodenames["moretrees:oak_trunk_sideways"]               = true
46     timber_nodenames["moretrees:palm_trunk"]                       = true
47     timber_nodenames["moretrees:palm_trunk_sideways"]              = true
48     timber_nodenames["moretrees:pine_trunk"]                       = true
49     timber_nodenames["moretrees:pine_trunk_sideways"]              = true
50     timber_nodenames["moretrees:rubber_tree_trunk_sideways"]       = true
51     timber_nodenames["moretrees:rubber_tree_trunk_sideways_empty"] = true
52     timber_nodenames["moretrees:sequoia_trunk"]                    = true
53     timber_nodenames["moretrees:sequoia_trunk_sideways"]           = true
54     timber_nodenames["moretrees:spruce_trunk"]                     = true
55     timber_nodenames["moretrees:spruce_trunk_sideways"]            = true
56     timber_nodenames["moretrees:willow_trunk"]                     = true
57     timber_nodenames["moretrees:willow_trunk_sideways"]            = true
58     timber_nodenames["moretrees:jungletree_trunk"]                 = true
59     timber_nodenames["moretrees:jungletree_trunk_sideways"]        = true
82cba9 60
c63658 61     if chainsaw_leaves then
P 62         timber_nodenames["moretrees:apple_tree_leaves"]        = true
63         timber_nodenames["moretrees:oak_leaves"]               = true
64         timber_nodenames["moretrees:fir_leaves"]               = true
65         timber_nodenames["moretrees:fir_leaves_bright"]        = true
66         timber_nodenames["moretrees:sequoia_leaves"]           = true
67         timber_nodenames["moretrees:birch_leaves"]             = true
68         timber_nodenames["moretrees:birch_leaves"]             = true
69         timber_nodenames["moretrees:palm_leaves"]              = true
70         timber_nodenames["moretrees:spruce_leaves"]            = true
71         timber_nodenames["moretrees:spruce_leaves"]            = true
72         timber_nodenames["moretrees:pine_leaves"]              = true
73         timber_nodenames["moretrees:willow_leaves"]            = true
74         timber_nodenames["moretrees:jungletree_leaves_green"]  = true
75         timber_nodenames["moretrees:jungletree_leaves_yellow"] = true
76         timber_nodenames["moretrees:jungletree_leaves_red"]    = true
77     end
8e03d7 78 end
82cba9 79
9019eb 80 -- Support growing_trees
S 81 if minetest.get_modpath("growing_trees") then
c63658 82     timber_nodenames["growing_trees:trunk"]         = true
P 83     timber_nodenames["growing_trees:medium_trunk"]  = true
84     timber_nodenames["growing_trees:big_trunk"]     = true
85     timber_nodenames["growing_trees:trunk_top"]     = true
86     timber_nodenames["growing_trees:trunk_sprout"]  = true
87     timber_nodenames["growing_trees:branch_sprout"] = true
88     timber_nodenames["growing_trees:branch"]        = true
89     timber_nodenames["growing_trees:branch_xmzm"]   = true
90     timber_nodenames["growing_trees:branch_xpzm"]   = true
91     timber_nodenames["growing_trees:branch_xmzp"]   = true
92     timber_nodenames["growing_trees:branch_xpzp"]   = true
93     timber_nodenames["growing_trees:branch_zz"]     = true
94     timber_nodenames["growing_trees:branch_xx"]     = true
82cba9 95
9019eb 96     if chainsaw_leaves then
c63658 97         timber_nodenames["growing_trees:leaves"] = true
P 98     end
8e03d7 99 end
R 100
9019eb 101 -- Support growing_cactus
S 102 if minetest.get_modpath("growing_cactus") then
c63658 103     timber_nodenames["growing_cactus:sprout"]                       = true
P 104     timber_nodenames["growing_cactus:branch_sprout_vertical"]       = true
105     timber_nodenames["growing_cactus:branch_sprout_vertical_fixed"] = true
106     timber_nodenames["growing_cactus:branch_sprout_xp"]             = true
107     timber_nodenames["growing_cactus:branch_sprout_xm"]             = true
108     timber_nodenames["growing_cactus:branch_sprout_zp"]             = true
109     timber_nodenames["growing_cactus:branch_sprout_zm"]             = true
110     timber_nodenames["growing_cactus:trunk"]                        = true
111     timber_nodenames["growing_cactus:branch_trunk"]                 = true
112     timber_nodenames["growing_cactus:branch"]                       = true
113     timber_nodenames["growing_cactus:branch_xp"]                    = true
114     timber_nodenames["growing_cactus:branch_xm"]                    = true
115     timber_nodenames["growing_cactus:branch_zp"]                    = true
116     timber_nodenames["growing_cactus:branch_zm"]                    = true
117     timber_nodenames["growing_cactus:branch_zz"]                    = true
118     timber_nodenames["growing_cactus:branch_xx"]                    = true
8e03d7 119 end
R 120
9019eb 121 -- Support farming_plus
S 122 if minetest.get_modpath("farming_plus") then
123     if chainsaw_leaves then
c63658 124         timber_nodenames["farming_plus:cocoa_leaves"] = true
P 125     end
8e03d7 126 end
R 127
5cf765 128
S 129 local S = technic.getter
130
131 technic.register_power_tool("technic:chainsaw", chainsaw_max_charge)
132
8e03d7 133 -- Table for saving what was sawed down
9019eb 134 local produced = {}
8e03d7 135
9019eb 136 -- Save the items sawed down so that we can drop them in a nice single stack
S 137 local function handle_drops(drops)
138     for _, item in ipairs(drops) do
139         local stack = ItemStack(item)
140         local name = stack:get_name()
141         local p = produced[name]
142         if not p then
143             produced[name] = stack
c63658 144         else
9019eb 145             p:set_count(p:get_count() + stack:get_count())
c63658 146         end
9019eb 147     end
S 148 end
149
150 --- Iterator over positions to try to saw around a sawed node.
151 -- This returns nodes in a 3x2x3 area. It does not return lower (y) positions
152 -- to prevent the chainsaw from cutting down nodes below the cutting position.
153 -- @param pos Reference to sawing position.  Note that this is overridden.
154 local function iterSawTries(pos)
155     -- Shift the position down on the x and z axes
156     pos.x, pos.z = pos.x - 1, pos.z - 1
157     -- Save our starting position for reseting it later
158     local startx, startz = pos.x, pos.z
159     -- We will move out by one in every direction except -y
160     local endx, endy, endz = pos.x + 2, pos.y + 1, pos.z + 2
161     -- Adjust for initial increment
162     pos.x = pos.x - 1
163
164     return function()
165         if pos.x < endx then
166             pos.x = pos.x + 1
167         else
168             pos.x = startx
169             if pos.z < endz then
170                 pos.z = pos.z + 1
171             else
172                 pos.z = startz
173                 if pos.y < endy then
174                     pos.y = pos.y + 1
175                 else
176                     return nil
177                 end
178             end
179         end
180         return pos
c63658 181     end
8e03d7 182 end
R 183
184 -- This function does all the hard work. Recursively we dig the node at hand
185 -- if it is in the table and then search the surroundings for more stuff to dig.
9019eb 186 local function recursive_dig(pos, remaining_charge)
S 187     if remaining_charge < chainsaw_charge_per_node then
c63658 188         return remaining_charge
P 189     end
9019eb 190     local node = minetest.get_node(pos)
S 191
192     if not timber_nodenames[node.name] then
193         return remaining_charge
194     end
195
196     -- wood found - cut it
197     handle_drops(minetest.get_node_drops(node.name, ""))
198     minetest.remove_node(pos)
199     remaining_charge = remaining_charge - chainsaw_charge_per_node
200
201     -- Check surroundings and run recursively if any charge left
202     for pos in iterSawTries(pos) do
203         if remaining_charge < chainsaw_charge_per_node then
204             break
205         end
206         if timber_nodenames[minetest.get_node(pos).name] then
207             remaining_charge = recursive_dig(pos, remaining_charge)
208         end
209     end
c63658 210     return remaining_charge
P 211 end
212
213 -- Function to randomize positions for new node drops
214 local function get_drop_pos(pos)
215     local drop_pos = {}
216
217     for i = 0, 8 do
218         -- Randomize position for a new drop
219         drop_pos.x = pos.x + math.random(-3, 3)
220         drop_pos.y = pos.y - 1
221         drop_pos.z = pos.z + math.random(-3, 3)
222
223         -- Move the randomized position upwards until
224         -- the node is air or unloaded.
225         for y = drop_pos.y, drop_pos.y + 5 do
226             drop_pos.y = y
227             local node = minetest.get_node_or_nil(drop_pos)
228
229             if not node then
230                 -- If the node is not loaded yet simply drop
231                 -- the item at the original digging position.
232                 return pos
233             elseif node.name == "air" then
9019eb 234                 -- Add variation to the entity drop position,
S 235                 -- but don't let drops get too close to the edge
236                 drop_pos.x = drop_pos.x + (math.random() * 0.8) - 0.5
237                 drop_pos.z = drop_pos.z + (math.random() * 0.8) - 0.5
c63658 238                 return drop_pos
P 239             end
240         end
241     end
242
243     -- Return the original position if this takes too long
244     return pos
e23f87 245 end
19c9a0 246
9019eb 247 -- Chainsaw entry point
S 248 local function chainsaw_dig(pos, current_charge)
249     -- Start sawing things down
250     local remaining_charge = recursive_dig(pos, current_charge)
251     minetest.sound_play("chainsaw", {pos = pos, gain = 1.0,
252             max_hear_distance = 10})
5cf765 253
9019eb 254     -- Now drop items for the player
S 255     for name, stack in pairs(produced) do
256         -- Drop stacks of stack max or less
257         local count, max = stack:get_count(), stack:get_stack_max()
258         stack:set_count(max)
259         while count > max do
260             minetest.add_item(get_drop_pos(pos), stack)
261             count = count - max
c63658 262         end
9019eb 263         stack:set_count(count)
S 264         minetest.add_item(get_drop_pos(pos), stack)
265     end
266
267     -- Clean up
268     produced = {}
269
270     return remaining_charge
5cf765 271 end
S 272
273
274 minetest.register_tool("technic:chainsaw", {
275     description = S("Chainsaw"),
276     inventory_image = "technic_chainsaw.png",
277     stack_max = 1,
99fd5d 278     wear_represents = "technic_RE_charge",
00d7c9 279     on_refill = technic.refill_RE_charge,
5cf765 280     on_use = function(itemstack, user, pointed_thing)
S 281         if pointed_thing.type ~= "node" then
282             return itemstack
283         end
9019eb 284
5cf765 285         local meta = minetest.deserialize(itemstack:get_metadata())
9019eb 286         if not meta or not meta.charge or
S 287                 meta.charge < chainsaw_charge_per_node then
5cf765 288             return
S 289         end
290
9019eb 291         local name = user:get_player_name()
51c02e 292         if minetest.is_protected(pointed_thing.under, name) then
S 293             minetest.record_protection_violation(pointed_thing.under, name)
294             return
9019eb 295         end
S 296
297         -- Send current charge to digging function so that the
298         -- chainsaw will stop after digging a number of nodes
299         meta.charge = chainsaw_dig(pointed_thing.under, meta.charge)
300
5cf765 301         technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge)
S 302         itemstack:set_metadata(minetest.serialize(meta))
303         return itemstack
304     end,
305 })
306
307 minetest.register_craft({
9019eb 308     output = "technic:chainsaw",
c63658 309     recipe = {
9019eb 310         {"technic:stainless_steel_ingot", "technic:stainless_steel_ingot", "technic:battery"},
S 311         {"technic:stainless_steel_ingot", "technic:motor",                 "technic:battery"},
312         {"",                              "",                              "default:copper_ingot"},
c63658 313     }
5cf765 314 })
9019eb 315