Update lumberjack module

This commit is contained in:
tacigar 2016-06-09 23:54:33 +09:00
parent 7775940177
commit 7897d0d9a1
2 changed files with 69 additions and 18 deletions

View File

@ -83,7 +83,7 @@ function maidroid.modules._aux.search_surrounding(self, lenvec, pred)
for yi = -lenvec.y, lenvec.y do for yi = -lenvec.y, lenvec.y do
for zi = -lenvec.z, lenvec.z do for zi = -lenvec.z, lenvec.z do
local p = {x = pos.x + xi, y = pos.y + yi, z = pos.z + zi} local p = {x = pos.x + xi, y = pos.y + yi, z = pos.z + zi}
local node = minetest.get_node(pos) local node = minetest.get_node(p)
if pred(self, p, node) then return true, p, node end if pred(self, p, node) then return true, p, node end
end end
end end

View File

@ -6,9 +6,18 @@
local util = maidroid.util local util = maidroid.util
local _aux = maidroid.modules._aux local _aux = maidroid.modules._aux
local state = {walk = 0, plant = 1, punch = 2} local state = {
walk = 0,
plant = 1,
punch = 2,
walk_to_tree = 3,
walk_avoid = 4,
}
local find_lenvec = {x = 3, y = 0, z = 3}
local plant_lenvec = {x = 2, y = 0, z = 2}
local max_punch_time = 20 local max_punch_time = 20
local max_plant_time = 20 local max_plant_time = 20
local max_avoid_time = 15
local target_tree_list = { "default:tree" } local target_tree_list = { "default:tree" }
local target_sapling_list = { "default:sapling" } local target_sapling_list = { "default:sapling" }
@ -48,6 +57,7 @@ maidroid.register_module("maidroid:lumberjack_module", {
self.object:setacceleration{x = 0, y = -10, z = 0} self.object:setacceleration{x = 0, y = -10, z = 0}
self.object:set_animation(maidroid.animations.walk, 15, 0) self.object:set_animation(maidroid.animations.walk, 15, 0)
self.preposition = self.object:getpos() self.preposition = self.object:getpos()
self.destination = nil
_aux.change_dir(self) _aux.change_dir(self)
end, end,
@ -56,6 +66,7 @@ maidroid.register_module("maidroid:lumberjack_module", {
self.time_count = nil self.time_count = nil
self.preposition = nil self.preposition = nil
self.object:setvelocity{x = 0, y = 0, z = 0} self.object:setvelocity{x = 0, y = 0, z = 0}
self.destination = nil
end, end,
on_step = function(self, dtime) on_step = function(self, dtime)
@ -70,28 +81,38 @@ maidroid.register_module("maidroid:lumberjack_module", {
local forward_under_node = minetest.get_node(forward_under_pos) local forward_under_node = minetest.get_node(forward_under_pos)
if self.state == state.walk then if self.state == state.walk then
if check_punch_flag(forward_pos) then -- punch tree node local b, dest = _aux.search_surrounding(self, find_lenvec, function(self, pos, node)
self.state = state.punch return util.table_find_value(target_tree_list, node.name)
end)
if b then -- walk to tree
self.state = state.walk_to_tree
self.destination = dest
_aux.change_dir_to(self, dest)
-- to plant sapling
elseif forward_node.name == "air"
and minetest.get_item_group(forward_under_node.name, "soil") > 0
and not _aux.search_surrounding(self, plant_lenvec, function(self, pos, node) -- no tree around
return util.table_find_value(target_tree_list, node.name)
or util.table_find_value(target_sapling_list, node.name)
end)
and has_sapling_item(self) then
self.state = state.plant
self.object:set_animation(maidroid.animations.mine, 15, 0) self.object:set_animation(maidroid.animations.mine, 15, 0)
self.object:setvelocity{x = 0, y = 0, z = 0} self.object:setvelocity{x = 0, y = 0, z = 0}
elseif pos.x == self.preposition.x or pos.z == self.preposition.z then -- else continue to walk
_aux.change_dir(self) else
elseif forward_node.name == "air" if pos.x == self.preposition.x or pos.z == self.preposition.z then
and minetest.get_item_group(forward_under_node.name, "soil") > 0 _aux.change_dir(self)
and has_sapling_item(self) then
self.state = state.plant
self.object:set_animation(maidroid.animations.mine, 15, 0)
self.object:setvelocity{x = 0, y = 0, z = 0}
end end
-- pickup sapling items end
_aux.pickup_item(self, 1.5, function(itemstring) -- pickup sapling items
return util.table_find_value(target_sapling_list, itemstring) _aux.pickup_item(self, 1.5, function(itemstring)
end) return util.table_find_value(target_sapling_list, itemstring)
end)
elseif self.state == state.punch then elseif self.state == state.punch then
if self.time_count >= max_punch_time then if self.time_count >= max_punch_time then
local punch_flag, forward_upper_pos, forward_upper_node local punch_flag, forward_upper_pos, forward_upper_node = check_punch_flag(self.destination)
= check_punch_flag(forward_pos)
if punch_flag then if punch_flag then
minetest.remove_node(forward_upper_pos) minetest.remove_node(forward_upper_pos)
local inv = minetest.get_inventory{type = "detached", name = self.invname} local inv = minetest.get_inventory{type = "detached", name = self.invname}
@ -135,6 +156,36 @@ maidroid.register_module("maidroid:lumberjack_module", {
else else
self.time_count = self.time_count + 1 self.time_count = self.time_count + 1
end end
elseif self.state == state.walk_to_tree then
if vector.distance(pos, self.destination) < 1.5 then -- to punch state
local destnode = minetest.get_node(self.destination)
if (util.table_find_value(target_tree_list, destnode.name)) then
self.state = state.punch
self.object:set_animation(maidroid.animations.mine, 15, 0)
self.object:setvelocity{x = 0, y = 0, z = 0}
else
self.state = state.walk
self.object:set_animation(maidroid.animations.walk, 15, 0)
self.time_count = 0
_aux.change_dir(self)
end
else
if pos.x == self.preposition.x or pos.z == self.preposition.z then
self.state = state.walk_avoid
self.object:set_animation(maidroid.animations.walk, 15, 0)
self.time_count = 0
_aux.change_dir(self)
end
end
elseif self.state == state.walk_avoid then
if self.time_count > max_avoid_time then
self.state = state.walk
self.time_count = 0
else
self.time_count = self.time_count + 1
end
end end
self.preposition = pos self.preposition = pos
end end