MT-Modder
2015-03-01 b8c902868115a0a607da35e208a422e430d46317
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,
91e12c 17     ["default:pinetree"]   = 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
72d541 151 if minetest.get_modpath("trunks") then
T 152     if chainsaw_leaves then
153         timber_nodenames["trunks:moss"] = true
154         timber_nodenames["trunks:moss_fungus"] = true
155         timber_nodenames["trunks:treeroot"] = true
156     end
157 end
158
5cf765 159 local S = technic.getter
S 160
161 technic.register_power_tool("technic:chainsaw", chainsaw_max_charge)
162
8e03d7 163 -- Table for saving what was sawed down
9019eb 164 local produced = {}
8e03d7 165
9019eb 166 -- Save the items sawed down so that we can drop them in a nice single stack
S 167 local function handle_drops(drops)
168     for _, item in ipairs(drops) do
169         local stack = ItemStack(item)
170         local name = stack:get_name()
171         local p = produced[name]
172         if not p then
173             produced[name] = stack
c63658 174         else
9019eb 175             p:set_count(p:get_count() + stack:get_count())
c63658 176         end
9019eb 177     end
S 178 end
179
180 --- Iterator over positions to try to saw around a sawed node.
ec069a 181 -- This returns positions in a 3x1x3 area around the position, plus the
S 182 -- position above it.  This does not return the bottom position to prevent
183 -- the chainsaw from cutting down nodes below the cutting position.
16302c 184 -- @param pos Sawing position.
9019eb 185 local function iterSawTries(pos)
16302c 186     -- Copy position to prevent mangling it
S 187     local pos = vector.new(pos)
ec069a 188     local i = 0
9019eb 189
S 190     return function()
ec069a 191         i = i + 1
S 192         -- Given a (top view) area like so (where 5 is the starting position):
193         -- X -->
194         -- Z 123
195         -- | 456
196         -- V 789
197         -- This will return positions 1, 4, 7, 2, 8 (skip 5), 3, 6, 9,
198         -- and the position above 5.
199         if i == 1 then
200             -- Move to starting position
201             pos.x = pos.x - 1
202             pos.z = pos.z - 1
203         elseif i == 4 or i == 7 then
204             -- Move to next X and back to start of Z when we reach
205             -- the end of a Z line.
9019eb 206             pos.x = pos.x + 1
ec069a 207             pos.z = pos.z - 2
S 208         elseif i == 5 then
209             -- Skip the middle position (we've already run on it)
210             -- and double-increment the counter.
211             pos.z = pos.z + 2
212             i = i + 1
213         elseif i <= 9 then
214             -- Go to next Z.
215             pos.z = pos.z + 1
216         elseif i == 10 then
217             -- Move back to center and up.
218             -- The Y+ position must be last so that we don't dig
219             -- straight upward and not come down (since the Y-
220             -- position isn't checked).
221             pos.x = pos.x - 1
222             pos.z = pos.z - 1
223             pos.y = pos.y + 1
9019eb 224         else
ec069a 225             return nil
9019eb 226         end
S 227         return pos
c63658 228     end
8e03d7 229 end
R 230
231 -- This function does all the hard work. Recursively we dig the node at hand
232 -- if it is in the table and then search the surroundings for more stuff to dig.
9019eb 233 local function recursive_dig(pos, remaining_charge)
S 234     if remaining_charge < chainsaw_charge_per_node then
c63658 235         return remaining_charge
P 236     end
9019eb 237     local node = minetest.get_node(pos)
S 238
239     if not timber_nodenames[node.name] then
240         return remaining_charge
241     end
242
ec069a 243     -- Wood found - cut it
9019eb 244     handle_drops(minetest.get_node_drops(node.name, ""))
S 245     minetest.remove_node(pos)
246     remaining_charge = remaining_charge - chainsaw_charge_per_node
247
248     -- Check surroundings and run recursively if any charge left
16302c 249     for npos in iterSawTries(pos) do
9019eb 250         if remaining_charge < chainsaw_charge_per_node then
S 251             break
252         end
16302c 253         if timber_nodenames[minetest.get_node(npos).name] then
S 254             remaining_charge = recursive_dig(npos, remaining_charge)
9019eb 255         end
S 256     end
c63658 257     return remaining_charge
P 258 end
259
260 -- Function to randomize positions for new node drops
261 local function get_drop_pos(pos)
262     local drop_pos = {}
263
264     for i = 0, 8 do
265         -- Randomize position for a new drop
266         drop_pos.x = pos.x + math.random(-3, 3)
267         drop_pos.y = pos.y - 1
268         drop_pos.z = pos.z + math.random(-3, 3)
269
270         -- Move the randomized position upwards until
271         -- the node is air or unloaded.
272         for y = drop_pos.y, drop_pos.y + 5 do
273             drop_pos.y = y
274             local node = minetest.get_node_or_nil(drop_pos)
275
276             if not node then
277                 -- If the node is not loaded yet simply drop
278                 -- the item at the original digging position.
279                 return pos
280             elseif node.name == "air" then
9019eb 281                 -- Add variation to the entity drop position,
S 282                 -- but don't let drops get too close to the edge
283                 drop_pos.x = drop_pos.x + (math.random() * 0.8) - 0.5
284                 drop_pos.z = drop_pos.z + (math.random() * 0.8) - 0.5
c63658 285                 return drop_pos
P 286             end
287         end
288     end
289
290     -- Return the original position if this takes too long
291     return pos
e23f87 292 end
19c9a0 293
9019eb 294 -- Chainsaw entry point
S 295 local function chainsaw_dig(pos, current_charge)
296     -- Start sawing things down
297     local remaining_charge = recursive_dig(pos, current_charge)
298     minetest.sound_play("chainsaw", {pos = pos, gain = 1.0,
299             max_hear_distance = 10})
5cf765 300
9019eb 301     -- Now drop items for the player
S 302     for name, stack in pairs(produced) do
303         -- Drop stacks of stack max or less
304         local count, max = stack:get_count(), stack:get_stack_max()
305         stack:set_count(max)
306         while count > max do
307             minetest.add_item(get_drop_pos(pos), stack)
308             count = count - max
c63658 309         end
9019eb 310         stack:set_count(count)
S 311         minetest.add_item(get_drop_pos(pos), stack)
312     end
313
314     -- Clean up
315     produced = {}
316
317     return remaining_charge
5cf765 318 end
S 319
320
321 minetest.register_tool("technic:chainsaw", {
322     description = S("Chainsaw"),
323     inventory_image = "technic_chainsaw.png",
324     stack_max = 1,
99fd5d 325     wear_represents = "technic_RE_charge",
00d7c9 326     on_refill = technic.refill_RE_charge,
5cf765 327     on_use = function(itemstack, user, pointed_thing)
S 328         if pointed_thing.type ~= "node" then
329             return itemstack
330         end
9019eb 331
5cf765 332         local meta = minetest.deserialize(itemstack:get_metadata())
9019eb 333         if not meta or not meta.charge or
S 334                 meta.charge < chainsaw_charge_per_node then
5cf765 335             return
S 336         end
337
9019eb 338         local name = user:get_player_name()
51c02e 339         if minetest.is_protected(pointed_thing.under, name) then
S 340             minetest.record_protection_violation(pointed_thing.under, name)
341             return
9019eb 342         end
S 343
344         -- Send current charge to digging function so that the
345         -- chainsaw will stop after digging a number of nodes
346         meta.charge = chainsaw_dig(pointed_thing.under, meta.charge)
b8c902 347         if not technic.creative_mode then
M 348             technic.set_RE_wear(itemstack, meta.charge, chainsaw_max_charge)
349             itemstack:set_metadata(minetest.serialize(meta))
350         end
5cf765 351         return itemstack
S 352     end,
353 })
354
e4c34c 355 local mesecons_button = minetest.get_modpath("mesecons_button")
T 356 local trigger = mesecons_button and "mesecons_button:button_off" or "default:mese_crystal_fragment"
357
5cf765 358 minetest.register_craft({
9019eb 359     output = "technic:chainsaw",
c63658 360     recipe = {
e4c34c 361         {"technic:stainless_steel_ingot", trigger,                      "technic:battery"},
5e4a87 362         {"technic:fine_copper_wire",      "technic:motor",              "technic:battery"},
Z 363         {"",                              "",                           "technic:stainless_steel_ingot"},
c63658 364     }
5cf765 365 })
9019eb 366