Update farming core

This commit is contained in:
tacigar 2016-12-22 10:02:37 +09:00
parent 9f977a48d5
commit 0bd9ec423f
2 changed files with 103 additions and 5 deletions

View File

@ -145,9 +145,10 @@ function maidroid.maidroid.set_wield_item_stack(self, stack)
end end
-- maidroid.maidroid.add_item_to_main add item to main slot. -- maidroid.maidroid.add_item_to_main add item to main slot.
-- and returns leftover.
function maidroid.maidroid.add_item_to_main(self, stack) function maidroid.maidroid.add_item_to_main(self, stack)
local inv = self:get_inventory() local inv = self:get_inventory()
inv:add_item("main", stack) return inv:add_item("main", stack)
end end
-- maidroid.maidroid.move_main_to_wield moves itemstack from main to wield. -- maidroid.maidroid.move_main_to_wield moves itemstack from main to wield.

View File

@ -92,13 +92,20 @@ do -- register farming core
end end
end end
local function to_walk_randomly(self)
self.state = state.WALK_RANDOMLY
self.time_counters[1] = 0
self.time_counters[2] = 0
self:change_direction_randomly()
end
local function to_walk_to_plant(self, path, destination) local function to_walk_to_plant(self, path, destination)
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.pre_position = self.object:getpos() self:change_direction(self.path[1])
end end
local function to_walk_to_mow(self, path, destination) local function to_walk_to_mow(self, path, destination)
@ -107,7 +114,25 @@ do -- register farming core
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.pre_position = self.object:getpos() self:change_direction(self.path[1])
end
local function to_plant(self)
if self:move_main_to_wield(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0 end)) then
self.state = state.PLANT
self.time_counters[1] = 0
self.object:set_velocity{x = 0, y = 0, z = 0}
return
else
to_walk_randomly(self)
return
end
end
local function to_mow(self)
self.state = state.MOW
self.time_counters[1] = 0
self.object:set_velocity{x = 0, y = 0, z = 0}
end end
local function walk_to_plant_and_mow_common(self, dtime) local function walk_to_plant_and_mow_common(self, dtime)
@ -128,8 +153,21 @@ do -- register farming core
end end
-- follow path -- follow path
if vector.distance(path[1], self.object:getpos()) < 0.01 then if vector.distance(self.path[1], self.object:getpos()) < 0.01 then
table.remove(path, 1) -- next step, follow next path. table.remove(self.path, 1)
if #self.path == 0 then -- end of path
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
else -- else next step, follow next path.
self:change_direction(self.path[1])
end
else else
-- 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()
@ -142,6 +180,65 @@ do -- register farming core
end end
end end
local function plant(self, dtime)
if self.time_counters[1] >= 5 then
if is_plantable_place(self.destination) then
local stack = self:get_wield_item_stack()
stack:take_item(1)
self:set_wield_item_stack(stack)
return
end
to_walk_randomly()
return
else
self.time_counters[1] = self.time_counters[1] + 1
end
end
local function mow(self, dtime)
if self.time_counters[1] >= 5 then
if is_mowable_place(self.destination) then
local destnode = minetest.get_node(self.destination)
minetest.remove_node(self.destination)
local stacks = minetest.get_node_drops(destnode.name)
for _, stack in ipairs(stacks) do
local leftover = self:add_item_to_main(stack)
minetest.add_item(self.destination, leftover)
end
end
to_walk_randomly()
return
else
self.time_counters[1] = self.time_counters[1] + 1
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
walk_randomly(self, dtime) walk_randomly(self, dtime)