Update farming core

This commit is contained in:
tacigar 2016-12-22 15:44:33 +09:00
parent 0bd9ec423f
commit bac29556f0
5 changed files with 75 additions and 57 deletions

View File

@ -190,7 +190,7 @@ end
-- maidroid.maidroid.change_direction change direction to destination and velocity vector. -- maidroid.maidroid.change_direction change direction to destination and velocity vector.
function maidroid.maidroid.change_direction(self, destination) function maidroid.maidroid.change_direction(self, destination)
local position = self.object:getpos() local position = self.object:getpos()
local direction = vector.subtract(destination, posistion) local direction = vector.subtract(destination, position)
direction.y = 0 direction.y = 0
local velocity = vector.multiply(vector.normalize(direction), 3) local velocity = vector.multiply(vector.normalize(direction), 3)

View File

@ -5,13 +5,13 @@
maidroid_core._aux = {} maidroid_core._aux = {}
function maidroid_core._aux.search_surrounding(pred, searching_range) function maidroid_core._aux.search_surrounding(pos, pred, searching_range)
for x = -searching_range.x, searching_range.x do for x = -searching_range.x, searching_range.x do
for y = -searching_range.y, searching_range.y do for y = -searching_range.y, searching_range.y do
for z = -searching_range.z, searching_range.z do for z = -searching_range.z, searching_range.z do
local pos = {x = x, y = y, z = z} local p = vector.add(pos, {x = x, y = y, z = z})
if pred(pos) then if pred(p) then
return pos return p
end end
end end
end end

View File

@ -19,13 +19,13 @@ local target_plants = {
local _aux = maidroid_core._aux local _aux = maidroid_core._aux
local FIND_PATH_TIME_INTERVAL = 20 local FIND_PATH_TIME_INTERVAL = 20
local CHANGE_DIRECTION_TIME_INTERVAL = 20 local CHANGE_DIRECTION_TIME_INTERVAL = 30
local MAX_WALK_TIME = 120 local MAX_WALK_TIME = 120
-- is_plantable_place reports whether maidroid can plant any seed. -- is_plantable_place reports whether maidroid can plant any seed.
local function is_plantable_place(pos) local function is_plantable_place(pos)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
local lpos = vector.sub(pos, {x = 0, y = -1, z = 0}) local lpos = vector.add(pos, {x = 0, y = -1, z = 0})
local lnode = minetest.get_node(lpos) local lnode = minetest.get_node(lpos)
return node.name == "air" return node.name == "air"
and minetest.get_item_group(lnode.name, "wet") > 0 and minetest.get_item_group(lnode.name, "wet") > 0
@ -34,17 +34,26 @@ end
-- is_mowable_place reports whether maidroid can mow. -- is_mowable_place reports whether maidroid can mow.
local function is_mowable_place(pos) local function is_mowable_place(pos)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
return maidroid_aux.table.find(target_plants, node.name) for _, plant in ipairs(target_plants) do
if plant == node.name then
return true
end
end
return false
end end
do -- register farming core do -- register farming core
local walk_randomly, walk_to_plant_and_mow_common, plant, mow
local to_walk_randomly, to_walk_to_plant, to_walk_to_mow, to_plant, to_mow
local function on_start(self) local function on_start(self)
self.object:setacceleration{x = 0, y = -10, z = 0} self.object:setacceleration{x = 0, y = -10, z = 0}
self.object:setvelocity{x = 0, y = 0, z = 0} self.object:setvelocity{x = 0, y = 0, z = 0}
self.state = state.WALK_RANDOMLY self.state = state.WALK_RANDOMLY
self.time_counters = {} self.time_counters = {}
self.path = nil self.path = nil
to_walk_randomly(self)
end end
local function on_stop(self) local function on_stop(self)
@ -53,15 +62,16 @@ do -- register farming core
local searching_range = {x = 5, y = 2, z = 5} local searching_range = {x = 5, y = 2, z = 5}
local function walk_randomly(self, dtime) walk_randomly = function(self, dtime)
if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then
self.time_counters[1] = 0 self.time_counters[1] = 0
self.time_counters[2] = self.time_counters[2] + 1 self.time_counters[2] = self.time_counters[2] + 1
if self:has_item_in_main(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0)) then if self:has_item_in_main(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0) end) then
local destination = _aux.search_surrounding(is_plantable_place, searching_range) local destination = _aux.search_surrounding(self.object:getpos(), is_plantable_place, searching_range)
if destination ~= nil then if destination ~= nil then
local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1) local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1, "A*")
if path ~= nil then -- to walk to plant state. if path ~= nil then -- to walk to plant state.
to_walk_to_plant(self, path, destination) to_walk_to_plant(self, path, destination)
return return
@ -69,10 +79,14 @@ do -- register farming core
end end
end end
-- if couldn't find path to plant, try to mow. -- if couldn't find path to plant, try to mow.
local destination = _aux.search_surrounding(is_mowable_place, searching_range) local destination = _aux.search_surrounding(self.object:getpos(), is_mowable_place, searching_range)
if destination ~= nil then if destination ~= nil then
local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1) local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1, "A*")
if path ~= nil then -- to walk to mow state. if path ~= nil then -- to walk to mow state.
for _, p in ipairs(path) do
print(p.x, p.y, p.z)
end
to_walk_to_mow(self, path, destination) to_walk_to_mow(self, path, destination)
return return
end end
@ -92,36 +106,44 @@ do -- register farming core
end end
end end
local function to_walk_randomly(self) to_walk_randomly = function(self)
print("to walk randomly")
self.state = state.WALK_RANDOMLY self.state = state.WALK_RANDOMLY
self.time_counters[1] = 0 self.time_counters[1] = 0
self.time_counters[2] = 0 self.time_counters[2] = 0
self:change_direction_randomly() self:change_direction_randomly()
self:set_animation(maidroid.animation_frames.WALK)
end end
local function to_walk_to_plant(self, path, destination) to_walk_to_plant = function(self, path, destination)
print("to walk to plant")
self.state = state.WALK_TO_PLANT self.state = state.WALK_TO_PLANT
self.path = path self.path = path
self.destination = destination self.destination = destination
self.time_counters[1] = 0 -- find path interval self.time_counters[1] = 0 -- find path interval
self.time_counters[2] = 0 self.time_counters[2] = 0
self:change_direction(self.path[1]) self:change_direction(self.path[1])
self:set_animation(maidroid.animation_frames.WALK)
end end
local function to_walk_to_mow(self, path, destination) to_walk_to_mow = function(self, path, destination)
print("to walk to mow")
self.state = state.WALK_TO_MOW self.state = state.WALK_TO_MOW
self.path = path self.path = path
self.destination = destination self.destination = destination
self.time_counters[1] = 0 -- find path interval self.time_counters[1] = 0 -- find path interval
self.time_counters[2] = 0 self.time_counters[2] = 0
self:change_direction(self.path[1]) self:change_direction(self.path[1])
self:set_animation(maidroid.animation_frames.WALK)
end end
local function to_plant(self) to_plant = function(self)
if self:move_main_to_wield(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0 end)) then print("to plant")
if self:move_main_to_wield(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0) end) then
self.state = state.PLANT self.state = state.PLANT
self.time_counters[1] = 0 self.time_counters[1] = 0
self.object:set_velocity{x = 0, y = 0, z = 0} self.object:setvelocity{x = 0, y = 0, z = 0}
self:set_animation(maidroid.animation_frames.MINE)
return return
else else
to_walk_randomly(self) to_walk_randomly(self)
@ -129,13 +151,25 @@ do -- register farming core
end end
end end
local function to_mow(self) to_mow = function(self)
print("to mow")
self.state = state.MOW self.state = state.MOW
self.time_counters[1] = 0 self.time_counters[1] = 0
self.object:set_velocity{x = 0, y = 0, z = 0} self.object:setvelocity{x = 0, y = 0, z = 0}
self:set_animation(maidroid.animation_frames.MINE)
end end
local function walk_to_plant_and_mow_common(self, dtime) walk_to_plant_and_mow_common = function(self, dtime)
if vector.distance(self.object:getpos(), self.destination) < 1.0 then
if self.state == state.WALK_TO_PLANT then
to_plant(self)
return
elseif self.state == state.WALK_TO_MOW then
to_mow(self)
return
end
end
if self.time_counters[2] >= MAX_WALK_TIME then -- time over. if self.time_counters[2] >= MAX_WALK_TIME then -- time over.
to_walk_randomly(self) to_walk_randomly(self)
return return
@ -144,7 +178,7 @@ do -- register farming core
if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then
self.time_counters[1] = 0 self.time_counters[1] = 0
self.time_counters[2] = self.time_counters[2] + 1 self.time_counters[2] = self.time_counters[2] + 1
local path = minetest.find_path(self.object:getpos(), self.destination, 10, 1, 1) local path = minetest.find_path(self.object:getpos(), self.destination, 10, 1, 1, "A*")
if path == nil then if path == nil then
to_walk_randomly(self) to_walk_randomly(self)
return return
@ -169,6 +203,7 @@ do -- register farming core
end end
else else
-- self:change_direction(self.path[1])
-- if maidroid is stopped by obstacles, the maidroid must jump. -- if maidroid is stopped by obstacles, the maidroid must jump.
local velocity = self.object:getvelocity() local velocity = self.object:getvelocity()
if velocity.y == 0 then if velocity.y == 0 then
@ -180,23 +215,23 @@ do -- register farming core
end end
end end
local function plant(self, dtime) plant = function(self, dtime)
if self.time_counters[1] >= 5 then if self.time_counters[1] >= 15 then
if is_plantable_place(self.destination) then if is_plantable_place(self.destination) then
local stack = self:get_wield_item_stack() local stack = self:get_wield_item_stack()
stack:take_item(1) stack:take_item(1)
self:set_wield_item_stack(stack) self:set_wield_item_stack(stack)
return return
end end
to_walk_randomly() to_walk_randomly(self)
return return
else else
self.time_counters[1] = self.time_counters[1] + 1 self.time_counters[1] = self.time_counters[1] + 1
end end
end end
local function mow(self, dtime) mow = function(self, dtime)
if self.time_counters[1] >= 5 then if self.time_counters[1] >= 15 then
if is_mowable_place(self.destination) then if is_mowable_place(self.destination) then
local destnode = minetest.get_node(self.destination) local destnode = minetest.get_node(self.destination)
minetest.remove_node(self.destination) minetest.remove_node(self.destination)
@ -207,45 +242,26 @@ do -- register farming core
minetest.add_item(self.destination, leftover) minetest.add_item(self.destination, leftover)
end end
end end
to_walk_randomly() to_walk_randomly(self)
return return
else else
self.time_counters[1] = self.time_counters[1] + 1 self.time_counters[1] = self.time_counters[1] + 1
end end
end end
local function plant_and_mow_common(self, dtime)
if self.time_counters[1] >= 5 then
if (self.state == state.PLANT and not is_plantable_place(self.destination)
or (self.state == state.MOW and not is_mowable_place(self.destination)) then
local inv = self:get_inventory()
local stacks = inv:get_list("main")
for idx, stack in ipairs(stacks) do
local item_name = stack:get_name()
if minetest.get_item_group(item_name, "seed") > 0 then
minetest.add_node(self.destination, {name = item_name, param2 = 1})
stack:take_item(1)
inv:set_stack("main", idx, stack)
break
end
end
end
to_walk(self)
else
self.time_count = self.time_count + 1
end
end
local function on_step(self, dtime) local function on_step(self, dtime)
if self.state == state.WALK_RANDOMLY then if self.state == state.WALK_RANDOMLY then
-- print("== now walk randomly")
walk_randomly(self, dtime) walk_randomly(self, dtime)
elseif self.state == state.WALK_TO_PLANT or self.state == state.WALK_TO_MOW then elseif self.state == state.WALK_TO_PLANT or self.state == state.WALK_TO_MOW then
-- print("== now walk to *")
walk_to_plant_and_mow_common(self, dtime) walk_to_plant_and_mow_common(self, dtime)
elseif self.state == state.PLANT or self.state == state.MOW then elseif self.state == state.PLANT then
plant_and_mow_common(self, dtime) -- print("== now plant")
plant(self, dtime)
elseif self.state == state.MOW then
-- print("== now mow")
mow(self, dtime)
end end
end end

View File

@ -8,6 +8,7 @@ maidroid_core = {}
maidroid_core.modname = "maidroid_core" maidroid_core.modname = "maidroid_core"
maidroid_core.modpath = minetest.get_modpath(maidroid_core.modname) maidroid_core.modpath = minetest.get_modpath(maidroid_core.modname)
dofile(maidroid_core.modpath .. "/cores/_aux.lua")
dofile(maidroid_core.modpath .. "/cores/empty.lua") dofile(maidroid_core.modpath .. "/cores/empty.lua")
dofile(maidroid_core.modpath .. "/cores/basic.lua") dofile(maidroid_core.modpath .. "/cores/basic.lua")
dofile(maidroid_core.modpath .. "/cores/farming.lua") dofile(maidroid_core.modpath .. "/cores/farming.lua")

View File

@ -7,6 +7,7 @@ do -- register core writer
local dye_item_map = { local dye_item_map = {
["dye:red"] = "maidroid_core:basic", ["dye:red"] = "maidroid_core:basic",
["dye:green"] = "maidroid_core:farming",
} }
local node_box = { local node_box = {