Zefram
2014-06-20 c394984ae5a33a24730fb5e05081b80490f0694e
commit | author | age
8e03d7 1 -- Configuration
R 2 local chainsaw_max_charge      = 30000 -- 30000 - Maximum charge of the saw
3 local chainsaw_charge_per_node = 12    -- 12    - Gives 2500 nodes on a single charge (about 50 complete normal trees)
4 local chainsaw_leaves          = true  -- true  - Cut down entire trees, leaves and all
5
6 -- The default stuff
7 local timber_nodenames={["default:jungletree"] = true,
8                         ["default:papyrus"]    = true,
9                         ["default:cactus"]     = true,
10                         ["default:tree"]       = true,
11                         ["default:apple"]      = true
12 }
82cba9 13
8e03d7 14 if chainsaw_leaves == true then
918166 15     timber_nodenames["default:leaves"] = true
N 16     timber_nodenames["default:jungleleaves"] = true
8e03d7 17 end
82cba9 18
ee0765 19 -- technic_worldgen defines rubber trees if moretrees isn't installed
S 20 if minetest.get_modpath("technic_worldgen") or
21    minetest.get_modpath("moretrees") then
22     timber_nodenames["moretrees:rubber_tree_trunk_empty"] = true
23     timber_nodenames["moretrees:rubber_tree_trunk"]       = true
24     if chainsaw_leaves then
25                 timber_nodenames["moretrees:rubber_tree_leaves"] = true
26     end
27 end
28
8e03d7 29 -- Support moretrees if it is there
R 30 if( minetest.get_modpath("moretrees") ~= nil ) then
31         timber_nodenames["moretrees:apple_tree_trunk"]                 = true
32         timber_nodenames["moretrees:apple_tree_trunk_sideways"]        = true
33         timber_nodenames["moretrees:beech_trunk"]                      = true
34         timber_nodenames["moretrees:beech_trunk_sideways"]             = true
35         timber_nodenames["moretrees:birch_trunk"]                      = true
36         timber_nodenames["moretrees:birch_trunk_sideways"]             = true
37         timber_nodenames["moretrees:fir_trunk"]                        = true
38         timber_nodenames["moretrees:fir_trunk_sideways"]               = true
39         timber_nodenames["moretrees:oak_trunk"]                        = true
40         timber_nodenames["moretrees:oak_trunk_sideways"]               = true
41         timber_nodenames["moretrees:palm_trunk"]                       = true
42         timber_nodenames["moretrees:palm_trunk_sideways"]              = true
43         timber_nodenames["moretrees:pine_trunk"]                       = true
44         timber_nodenames["moretrees:pine_trunk_sideways"]              = true
45         timber_nodenames["moretrees:rubber_tree_trunk_sideways"]       = true
46         timber_nodenames["moretrees:rubber_tree_trunk_sideways_empty"] = true
47         timber_nodenames["moretrees:sequoia_trunk"]                    = true
48         timber_nodenames["moretrees:sequoia_trunk_sideways"]           = true
49         timber_nodenames["moretrees:spruce_trunk"]                     = true
50         timber_nodenames["moretrees:spruce_trunk_sideways"]            = true
51         timber_nodenames["moretrees:willow_trunk"]                     = true
52         timber_nodenames["moretrees:willow_trunk_sideways"]            = true
53         timber_nodenames["moretrees:jungletree_trunk"]                 = true
54         timber_nodenames["moretrees:jungletree_trunk_sideways"]        = true
82cba9 55
ee0765 56         if chainsaw_leaves then
8e03d7 57                 timber_nodenames["moretrees:apple_tree_leaves"]        = true
R 58                 timber_nodenames["moretrees:oak_leaves"]               = true
628a18 59                 timber_nodenames["moretrees:fir_leaves"]               = true
A 60                 timber_nodenames["moretrees:fir_leaves_bright"]        = true
8e03d7 61                 timber_nodenames["moretrees:sequoia_leaves"]           = true
R 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
72         end
73 end
82cba9 74
8e03d7 75 -- Support growing_trees if it is there
R 76 if( minetest.get_modpath("growing_trees") ~= nil ) then
77         timber_nodenames["growing_trees:trunk"]         = true
78         timber_nodenames["growing_trees:medium_trunk"]  = true
79         timber_nodenames["growing_trees:big_trunk"]     = true
80         timber_nodenames["growing_trees:trunk_top"]     = true
81         timber_nodenames["growing_trees:trunk_sprout"]  = true
82         timber_nodenames["growing_trees:branch_sprout"] = true
83         timber_nodenames["growing_trees:branch"]        = true
84         timber_nodenames["growing_trees:branch_xmzm"]   = true
85         timber_nodenames["growing_trees:branch_xpzm"]   = true
86         timber_nodenames["growing_trees:branch_xmzp"]   = true
87         timber_nodenames["growing_trees:branch_xpzp"]   = true
88         timber_nodenames["growing_trees:branch_zz"]     = true
89         timber_nodenames["growing_trees:branch_xx"]     = true
82cba9 90
8e03d7 91         if chainsaw_leaves == true then
R 92                 timber_nodenames["growing_trees:leaves"] = true
93         end
94 end
95
96 -- Support growing_cactus if it is there
97 if( minetest.get_modpath("growing_cactus") ~= nil ) then
98         timber_nodenames["growing_cactus:sprout"]                       = true
99         timber_nodenames["growing_cactus:branch_sprout_vertical"]       = true
100         timber_nodenames["growing_cactus:branch_sprout_vertical_fixed"] = true
101         timber_nodenames["growing_cactus:branch_sprout_xp"]             = true
102         timber_nodenames["growing_cactus:branch_sprout_xm"]             = true
103         timber_nodenames["growing_cactus:branch_sprout_zp"]             = true
104         timber_nodenames["growing_cactus:branch_sprout_zm"]             = true
105         timber_nodenames["growing_cactus:trunk"]                        = true
106         timber_nodenames["growing_cactus:branch_trunk"]                 = true
107         timber_nodenames["growing_cactus:branch"]                       = true
108         timber_nodenames["growing_cactus:branch_xp"]                    = true
109         timber_nodenames["growing_cactus:branch_xm"]                    = true
110         timber_nodenames["growing_cactus:branch_zp"]                    = true
111         timber_nodenames["growing_cactus:branch_zm"]                    = true
112         timber_nodenames["growing_cactus:branch_zz"]                    = true
113         timber_nodenames["growing_cactus:branch_xx"]                    = true
114 end
115
116 -- Support farming_plus if it is there
117 if( minetest.get_modpath("farming_plus") ~= nil ) then
118    if chainsaw_leaves == true then
119       timber_nodenames["farming_plus:cocoa_leaves"] = true
120    end
121 end
122
5cf765 123
S 124 local S = technic.getter
125
126 technic.register_power_tool("technic:chainsaw", chainsaw_max_charge)
127
8e03d7 128 -- Table for saving what was sawed down
5cf765 129 local produced = nil
8e03d7 130
R 131 -- Override the default handling routine to be able to count up the
132 -- items sawed down so that we can drop them i an nice single stack
5cf765 133 local function chainsaw_handle_node_drops(pos, drops, digger)
8e03d7 134         -- Add dropped items to list of collected nodes
R 135         local _, dropped_item
136         for _, dropped_item in ipairs(drops) do
137                 if produced[dropped_item] == nil then
138                         produced[dropped_item] = 1
139                 else
140                         produced[dropped_item] = produced[dropped_item] + 1
141                 end
142         end
143 end
144
145 -- This function does all the hard work. Recursively we dig the node at hand
146 -- if it is in the table and then search the surroundings for more stuff to dig.
5cf765 147 local function recursive_dig(pos, remaining_charge, player)
8e03d7 148         local node=minetest.env:get_node(pos)
R 149         local i=1
150         -- Lookup node name in timber table:
151         if timber_nodenames[node.name] ~= nil then
152                 -- Return if we are out of power
153                 if remaining_charge < chainsaw_charge_per_node then
154                         return 0
155                 end
156                 local np
157                 -- wood found - cut it.
158                 minetest.env:dig_node(pos)
159
160                 remaining_charge=remaining_charge-chainsaw_charge_per_node
161                 -- check surroundings and run recursively if any charge left
162                 np={x=pos.x+1, y=pos.y, z=pos.z}
163                 if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
164                         remaining_charge = recursive_dig(np, remaining_charge)
165                 end
166                 np={x=pos.x+1, y=pos.y, z=pos.z+1}
167                 if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
168                         remaining_charge = recursive_dig(np, remaining_charge)
169                 end
170                 np={x=pos.x+1, y=pos.y, z=pos.z-1}
171                 if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
172                         remaining_charge = recursive_dig(np, remaining_charge)
173                 end
174
175                 np={x=pos.x-1, y=pos.y, z=pos.z}
176                 if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
177                         remaining_charge = recursive_dig(np, remaining_charge)
178                 end
179                 np={x=pos.x-1, y=pos.y, z=pos.z+1}
180                 if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
181                         remaining_charge = recursive_dig(np, remaining_charge)
182                 end
183                 np={x=pos.x-1, y=pos.y, z=pos.z-1}
184                 if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
185                         remaining_charge = recursive_dig(np, remaining_charge)
186                 end
187
188                 np={x=pos.x, y=pos.y+1, z=pos.z}
189                 if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
190                         remaining_charge = recursive_dig(np, remaining_charge)
191                 end
192
193                 np={x=pos.x, y=pos.y, z=pos.z+1}
194                 if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
195                         remaining_charge = recursive_dig(np, remaining_charge)
196                 end
197                 np={x=pos.x, y=pos.y, z=pos.z-1}
198                 if timber_nodenames[minetest.env:get_node(np).name] ~= nil then
199                         remaining_charge = recursive_dig(np, remaining_charge)
200                 end
201                 return remaining_charge
202         end
203         -- Nothing sawed down
204         return remaining_charge
e23f87 205 end
19c9a0 206
5cf765 207 -- Saw down trees entry point
S 208 local function chainsaw_dig_it(pos, player,current_charge)
bab851 209     if minetest.is_protected(pos, player:get_player_name()) then
S 210         minetest.record_protection_violation(pos, player:get_player_name())
211         return current_charge
212     end
5cf765 213         local remaining_charge=current_charge
S 214
215         -- Save the currently installed dropping mechanism so we can restore it.
216     local original_handle_node_drops = minetest.handle_node_drops
217
218         -- A bit of trickery here: use a different node drop callback
219         -- and restore the original afterwards.
220         minetest.handle_node_drops = chainsaw_handle_node_drops
221
222         -- clear result and start sawing things down
223         produced = {}
224         remaining_charge = recursive_dig(pos, remaining_charge, player)
225         minetest.sound_play("chainsaw", {pos = pos, gain = 1.0, max_hear_distance = 10,})
226
227         -- Restore the original noder drop handler
228         minetest.handle_node_drops = original_handle_node_drops
229
230         -- Now drop items for the player
231         local number, produced_item, p
232         for produced_item,number in pairs(produced) do
233                 --print("ADDING ITEM: " .. produced_item .. " " .. number)
234                 -- Drop stacks of 99 or less
235                 p = {
236                         x = pos.x + math.random()*4,
237                         y = pos.y,
238                         z = pos.z + math.random()*4
239                 }
240                 while number > 99 do
241                         minetest.env:add_item(p, produced_item .. " 99")
242                         p = {
243                                 x = pos.x + math.random()*4,
244                                 y = pos.y,
245                                 z = pos.z + math.random()*4
246                         }
247                         number = number - 99
248                 end
249                 minetest.env:add_item(p, produced_item .. " " .. number)
250         end
251         return remaining_charge
252 end
253
254
255 minetest.register_tool("technic:chainsaw", {
256     description = S("Chainsaw"),
257     inventory_image = "technic_chainsaw.png",
258     stack_max = 1,
99fd5d 259     wear_represents = "technic_RE_charge",
00d7c9 260     on_refill = technic.refill_RE_charge,
5cf765 261     on_use = function(itemstack, user, pointed_thing)
S 262         if pointed_thing.type ~= "node" then
263             return itemstack
264         end
265         local meta = minetest.deserialize(itemstack:get_metadata())
266         if not meta or not meta.charge then
267             return
268         end
269         -- Send current charge to digging function so that the chainsaw will stop after digging a number of nodes.
270         if meta.charge < chainsaw_charge_per_node then
271             return
272         end
273
274         local pos = minetest.get_pointed_thing_position(pointed_thing, above)
275         meta.charge = chainsaw_dig_it(pos, user, meta.charge)
276         technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge)
277         itemstack:set_metadata(minetest.serialize(meta))
278         return itemstack
279     end,
280 })
281
282 minetest.register_craft({
283         output = 'technic:chainsaw',
284         recipe = {
285                 {'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot', 'technic:battery'},
286                 {'technic:stainless_steel_ingot', 'technic:motor',                 'technic:battery'},
287                 {'',                               '',                             'default:copper_ingot'},
288         }
289 })
290