Compare commits
10 Commits
a9d4160ae8
...
b265a9707a
Author | SHA1 | Date | |
---|---|---|---|
|
b265a9707a | ||
|
94eb6691cb | ||
|
9500658b0b | ||
|
17ebc111c6 | ||
|
52ce571bf9 | ||
|
df96750d19 | ||
|
c5d4534014 | ||
|
5f43976b7a | ||
|
258d798d44 | ||
|
82c54bac7b |
@ -9,7 +9,8 @@ Forum: https://forum.minetest.net/viewtopic.php?f=9&t=18023
|
||||
## Highlights / Features
|
||||
- Lag-free because the work is not done at once. You can observe the woodcutting.
|
||||
- You can stop the woodcutting by press sneak key second time
|
||||
- You can add additional trees to process by digging other tree nodes manually
|
||||
- Works with all trees. The cutting just follow connected tree (trunk) blocks in all directions for each cutted node and add them to the processing queue
|
||||
- You can add additional trees to process by digging other tree nodes manually (without sneak key)
|
||||
- The distance to the player is used to prefer next node so the player can partially influence the work direction on big areas
|
||||
- The auto-mining speed is dependent on wielded tool, so the diamond axe is still advantageously than empty hand
|
||||
- All checks and functionalities are processed (like hunger damage and tool wear) as if the player did the mining manually
|
||||
@ -28,6 +29,7 @@ woodcutting.settings = {
|
||||
leaves_distance = 2, -- do not touch leaves around the not removed trees with this distance
|
||||
player_distance = 80, -- Allow cutting tree nodes with this maximum distance away from player
|
||||
dig_leaves = true, -- Dig dacayable leaves after tree node is digged - can be changed trough woodcutting_dig_leaves in minetest.conf
|
||||
wear_limit = 65535, -- Maximum tool wear that allows cutting
|
||||
}
|
||||
```
|
||||
|
||||
@ -49,6 +51,7 @@ See (default) Settings
|
||||
- process.leaves_distance - used in process:process_leaves(pos) - can be adjusted each step in on_after_dig_hook()
|
||||
- process.player_distance - used in process:check_processing_allowed(pos) - can be adjusted each step in on_step_hook()
|
||||
- process.dig_leaves - used at end of on_dignode function - can be adjusted each step in on_after_dig_hook()
|
||||
- process.wear_limit - used in process:check_processing_allowed(pos) - can be adjusted each step in on_step_hook()
|
||||
|
||||
#### Methods
|
||||
Note:this methods could be redefined in on_new_process_hook, in a different way for each new process
|
||||
|
@ -1,2 +0,0 @@
|
||||
Asynchrounus tree cutter. Mine the first tree node from a
|
||||
tree while the sneak key is pressed to start the woodcutting process.
|
94
init.lua
94
init.lua
@ -1,10 +1,14 @@
|
||||
woodcutting = {}
|
||||
|
||||
local mod_storage = minetest.get_mod_storage()
|
||||
local disabled_by_player = {}
|
||||
|
||||
woodcutting.settings = {
|
||||
tree_distance = 1, -- Apply tree nodes with this distance to the queue. 1 means touching tree nodes only
|
||||
leaves_distance = 2, -- do not touch leaves around the not removed trees with this distance
|
||||
player_distance = 80, -- Allow cutting tree nodes with this maximum distance away from player
|
||||
dig_leaves = true, -- Dig dacayable leaves after tree node is digged
|
||||
tree_distance = tonumber(minetest.settings:get("woodcutting_tree_distance")) or 1,
|
||||
leaves_distance = tonumber(minetest.settings:get("woodcutting_leaves_distance")) or 2,
|
||||
player_distance = tonumber(minetest.settings:get("woodcutting_player_distance")) or 80,
|
||||
dig_leaves = minetest.settings:get_bool("woodcutting_dig_leaves", true),
|
||||
wear_limit = tonumber(minetest.settings:get("woodcutting_wear_limit")) or 65535,
|
||||
|
||||
on_new_process_hook = function(process) return true end, -- do not start the process if set to nil or return false
|
||||
on_step_hook = function(process) return true end, -- if false is returned finish the process
|
||||
@ -12,11 +16,6 @@ woodcutting.settings = {
|
||||
on_after_dig_hook = function(process, pos, oldnode) return true end, -- if false is returned do nothing after digging node
|
||||
}
|
||||
|
||||
local _woodcutting_dig_leaves = minetest.settings:get_bool("woodcutting_dig_leaves")
|
||||
if _woodcutting_dig_leaves ~= nil then
|
||||
woodcutting.settings.dig_leaves = _woodcutting_dig_leaves
|
||||
end
|
||||
|
||||
woodcutting.tree_content_ids = {}
|
||||
woodcutting.leaves_content_ids = {}
|
||||
woodcutting.process_runtime = {}
|
||||
@ -36,6 +35,7 @@ function woodcutting.new_process(playername, template)
|
||||
process.tree_distance = process.tree_distance or woodcutting.settings.tree_distance
|
||||
process.leaves_distance = process.leaves_distance or woodcutting.settings.leaves_distance
|
||||
process.player_distance = process.player_distance or woodcutting.settings.player_distance
|
||||
process.wear_limit = process.wear_limit or woodcutting.settings.wear_limit
|
||||
|
||||
if process.dig_leaves == nil then --bool value with default value true
|
||||
if woodcutting.settings.dig_leaves == nil then
|
||||
@ -112,8 +112,7 @@ end
|
||||
----------------------------------
|
||||
--- Get the delay time before processing the node at pos
|
||||
----------------------------------
|
||||
function woodcutting_class:get_delay_time(pos)
|
||||
local poshash = minetest.hash_node_position(pos)
|
||||
function woodcutting_class:get_delay_time(pos, poshash)
|
||||
local nodedef = minetest.registered_nodes[self.treenodes_hashed[poshash]]
|
||||
local capabilities = self._player:get_wielded_item():get_tool_capabilities()
|
||||
local dig_params = minetest.get_dig_params(nodedef.groups, capabilities)
|
||||
@ -131,8 +130,9 @@ end
|
||||
----------------------------------
|
||||
--- Check node removal allowed
|
||||
----------------------------------
|
||||
function woodcutting_class:check_processing_allowed(pos)
|
||||
function woodcutting_class:check_processing_allowed(pos, poshash)
|
||||
return vector.distance(pos, self._player:get_pos()) < self.player_distance
|
||||
and self._player:get_wielded_item():get_wear() <= self.wear_limit
|
||||
end
|
||||
|
||||
----------------------------------
|
||||
@ -150,7 +150,20 @@ function woodcutting_class:select_next_tree_node()
|
||||
end
|
||||
return aval < bval
|
||||
end)
|
||||
return self.treenodes_sorted[1]
|
||||
|
||||
-- Search for first unprocessed node. Cleanup tables
|
||||
while true do
|
||||
local pos = self.treenodes_sorted[1]
|
||||
if not pos then --Finished
|
||||
return
|
||||
end
|
||||
local poshash = minetest.hash_node_position(pos)
|
||||
if not self.treenodes_hashed[poshash] then
|
||||
table.remove(self.treenodes_sorted, 1)
|
||||
else
|
||||
return pos, poshash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
----------------------------------
|
||||
@ -169,14 +182,13 @@ function woodcutting_class:process_woodcut_step()
|
||||
return
|
||||
end
|
||||
|
||||
local pos = process:select_next_tree_node()
|
||||
local pos, poshash = process:select_next_tree_node()
|
||||
process:show_hud(pos)
|
||||
if pos then
|
||||
if process:check_processing_allowed(pos) then
|
||||
if process:check_processing_allowed(pos, poshash) then
|
||||
-- dig the node
|
||||
local delaytime = process:get_delay_time(pos)
|
||||
local delaytime = process:get_delay_time(pos, poshash)
|
||||
if delaytime then
|
||||
table.remove(process.treenodes_sorted, 1)
|
||||
process:woodcut_node(pos, delaytime)
|
||||
else
|
||||
-- wait for right tool is used, try again
|
||||
@ -184,8 +196,6 @@ function woodcutting_class:process_woodcut_step()
|
||||
end
|
||||
else
|
||||
-- just remove from hashed table and trigger the next step
|
||||
local poshash = minetest.hash_node_position(pos)
|
||||
table.remove(process.treenodes_sorted, 1)
|
||||
process.treenodes_hashed[poshash] = nil
|
||||
process:process_woodcut_step()
|
||||
end
|
||||
@ -311,8 +321,12 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
|
||||
return
|
||||
end
|
||||
|
||||
-- Get the process or create new one
|
||||
local playername = digger:get_player_name()
|
||||
if disabled_by_player[playername] then
|
||||
return
|
||||
end
|
||||
|
||||
-- Get the process or create new one
|
||||
local sneak = digger:get_player_control().sneak
|
||||
local process = woodcutting.get_process(playername)
|
||||
if not process and sneak then
|
||||
@ -378,4 +392,42 @@ minetest.register_on_dieplayer(function(player)
|
||||
end
|
||||
end)
|
||||
|
||||
--dofile(minetest.get_modpath(minetest.get_current_modname()).."/hook_examples.lua")
|
||||
----------------------------
|
||||
-- Command to toggle whether cutting is enabled, per-player
|
||||
----------------------------
|
||||
minetest.register_chatcommand("toggle_woodcutting", {
|
||||
description = "Toggle whether woodcutting is enabled",
|
||||
func = function(player_name)
|
||||
local is_currently_disabled = disabled_by_player[player_name]
|
||||
if is_currently_disabled then
|
||||
disabled_by_player[player_name] = nil
|
||||
mod_storage:set_string(player_name .. "_disabled", "")
|
||||
return true, "Woodcutting is now enabled."
|
||||
else
|
||||
disabled_by_player[player_name] = true
|
||||
mod_storage:set_string(player_name .. "_disabled", "true")
|
||||
local process = woodcutting.get_process(player_name)
|
||||
if process then
|
||||
process:stop_process()
|
||||
end
|
||||
return true, "Woodcutting is now disabled."
|
||||
end
|
||||
end
|
||||
})
|
||||
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
-- load player settings
|
||||
local player_name = player:get_player_name()
|
||||
if mod_storage:get_string(player_name .. "_disabled") == "true" then
|
||||
disabled_by_player[player_name] = true
|
||||
end
|
||||
end)
|
||||
|
||||
minetest.register_on_leaveplayer(function(player)
|
||||
local player_name = player:get_player_name()
|
||||
disabled_by_player[player_name] = nil
|
||||
local process = woodcutting.get_process(player_name)
|
||||
if process then
|
||||
process:stop_process()
|
||||
end
|
||||
end)
|
||||
|
1
mod.conf
1
mod.conf
@ -1 +1,2 @@
|
||||
name = woodcutting
|
||||
description = Asynchrounus tree cutter. Mine the first tree node from a tree while the sneak key is pressed to start the woodcutting process.
|
@ -1,3 +1,16 @@
|
||||
# If enabled the woodcutting dig not connected leaves
|
||||
# after tree nodes removed
|
||||
woodcutting_dig_leaves (Dig leaves) bool true
|
||||
woodcutting_dig_leaves (Woodcutting: Dig leaves) bool true
|
||||
|
||||
# Apply tree nodes with this distance to the queue. 1 means touching tree nodes only
|
||||
woodcutting_tree_distance (Woodcutting: Tree distance) int 1
|
||||
|
||||
# Do not touch leaves around non-removed trees with this distance
|
||||
woodcutting_leaves_distance (Woodcutting: Leaves distance) int 2
|
||||
|
||||
# Allow cutting tree nodes with this maximum distance away from player
|
||||
woodcutting_player_distance (Woodcutting: Player distance) int 80
|
||||
|
||||
# Maximum tool wear that allows cutting
|
||||
woodcutting_wear_limit (Woodcutting: Wear limit) int 65535
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user