From 83bcc91db40814ee1c3cdab435590d6939efbb59 Mon Sep 17 00:00:00 2001 From: tacigar Date: Wed, 21 Dec 2016 21:18:23 +0900 Subject: [PATCH 01/13] Add has_item_in_main method --- maidroid/api.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/maidroid/api.lua b/maidroid/api.lua index fc5cf27..c83d2e4 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -161,6 +161,19 @@ function maidroid.maidroid.is_named(self) return self.nametag ~= "" end +-- maidroid.maidroid.has_item_in_main reports whether the maidroid has item. +function maidroid.maidroid.has_item_in_main(self, pred) + local inv = self:get_inventory() + local stacks = inv:get_list("main") + + for _, stack in ipairs(stacks) do + local itemname = stack:get_name() + if pred(itemname) then + return true + end + end +end + --------------------------------------------------------------------- -- maidroid.manufacturing_data represents a table that contains manufacturing data. @@ -547,6 +560,7 @@ function maidroid.register_maidroid(product_name, def) get_wield_item_stack = maidroid.maidroid.get_wield_item_stack, move_main_to_wield = maidroid.maidroid.move_main_to_wield, is_named = maidroid.maidroid.is_named, + has_item_in_main = maidroid.maidroid.has_item_in_main, }) -- register maidroid egg. From cde514546cad8ad84225e7802b39b74fb42c17bc Mon Sep 17 00:00:00 2001 From: tacigar Date: Wed, 21 Dec 2016 23:18:41 +0900 Subject: [PATCH 02/13] Update farming core --- maidroid_core/cores/_aux.lua | 20 ++++ maidroid_core/cores/farming.lua | 165 ++++++++++++++++++++++++++++++++ maidroid_core/init.lua | 1 + 3 files changed, 186 insertions(+) create mode 100644 maidroid_core/cores/_aux.lua create mode 100644 maidroid_core/cores/farming.lua diff --git a/maidroid_core/cores/_aux.lua b/maidroid_core/cores/_aux.lua new file mode 100644 index 0000000..74679e7 --- /dev/null +++ b/maidroid_core/cores/_aux.lua @@ -0,0 +1,20 @@ +------------------------------------------------------------ +-- Copyright (c) 2016 tacigar. All rights reserved. +-- https://github.com/tacigar/maidroid +------------------------------------------------------------ + +maidroid_core._aux = {} + +function maidroid_core._aux.search_surrounding(pred, searching_range) + for x = -searching_range.x, searching_range.x do + for y = -searching_range.y, searching_range.y do + for z = -searching_range.z, searching_range.z do + local pos = {x = x, y = y, z = z} + if pred(pos) then + return pos + end + end + end + end + return nil +end diff --git a/maidroid_core/cores/farming.lua b/maidroid_core/cores/farming.lua new file mode 100644 index 0000000..19eb30d --- /dev/null +++ b/maidroid_core/cores/farming.lua @@ -0,0 +1,165 @@ +------------------------------------------------------------ +-- Copyright (c) 2016 tacigar. All rights reserved. +-- https://github.com/tacigar/maidroid +------------------------------------------------------------ + +local state = { + WALK_RANDOMLY = 0, + WALK_TO_PLANT = 1, + WALK_TO_MOW = 2, + PLANT = 3, + MOW = 4, +} + +local target_plants = { + "farming:cotton_8", + "farming:wheat_8", +} + +local _aux = maidroid_core._aux + +local FIND_PATH_TIME_INTERVAL = 20 +local CHANGE_DIRECTION_TIME_INTERVAL = 20 +local MAX_WALK_TIME = 120 + +-- is_plantable_place reports whether maidroid can plant any seed. +local function is_plantable_place(pos) + local node = minetest.get_node(pos) + local lpos = vector.sub(pos, {x = 0, y = -1, z = 0}) + local lnode = minetest.get_node(lpos) + return node.name == "air" + and minetest.get_item_group(lnode.name, "wet") > 0 +end + +-- is_mowable_place reports whether maidroid can mow. +local function is_mowable_place(pos) + local node = minetest.get_node(pos) + return maidroid_aux.table.find(target_plants, node.name) +end + +do -- register farming core + + local function on_start(self) + self.object:setacceleration{x = 0, y = -10, z = 0} + self.object:setvelocity{x = 0, y = 0, z = 0} + self.state = state.WALK_RANDOMLY + self.time_counters = {} + self.path = nil + end + + local function on_stop(self) + + end + + local searching_range = {x = 5, y = 2, z = 5} + + local function walk_randomly(self, dtime) + if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then + self.time_counters[1] = 0 + 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 + local destination = _aux.search_surrounding(is_plantable_place, searching_range) + if destination ~= nil then + local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1) + if path ~= nil then -- to walk to plant state. + to_walk_to_plant(self, path, destination) + return + end + end + end + -- if couldn't find path to plant, try to mow. + local destination = _aux.search_surrounding(is_mowable_place, searching_range) + if destination ~= nil then + local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1) + if path ~= nil then -- to walk to mow state. + to_walk_to_mow(self, path, destination) + return + end + end + -- else do nothing. + return + + elseif self.time_counters[2] >= CHANGE_DIRECTION_TIME_INTERVAL then + self.time_counters[1] = self.time_counters[1] + 1 + self.time_counters[2] = 0 + self:change_direction_randomly() + return + else + self.time_counters[1] = self.time_counters[1] + 1 + self.time_counters[2] = self.time_counters[2] + 1 + return + end + end + + local function to_walk_to_plant(self, path, destination) + self.state = state.WALK_TO_PLANT + self.path = path + self.destination = destination + self.time_counters[1] = 0 -- find path interval + self.time_counters[2] = 0 + self.pre_position = self.object:getpos() + end + + local function to_walk_to_mow(self, path, destination) + self.state = state.WALK_TO_MOW + self.path = path + self.destination = destination + self.time_counters[1] = 0 -- find path interval + self.time_counters[2] = 0 + self.pre_position = self.object:getpos() + end + + local function walk_to_plant_and_mow_common(self, dtime) + if self.time_counters[2] >= MAX_WALK_TIME then -- time over. + to_walk_randomly(self) + return + end + + if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then + self.time_counters[1] = 0 + self.time_counters[2] = self.time_counters[2] + 1 + local path = minetest.find_path(self.object:getpos(), self.destination, 10, 1, 1) + if path == nil then + to_walk_randomly(self) + return + end + self.path = path + end + + -- follow path + if vector.distance(path[1], self.object:getpos()) < 0.01 then + table.remove(path, 1) -- next step, follow next path. + else + -- if maidroid is stopped by obstacles, the maidroid must jump. + local velocity = self.object:getvelocity() + if velocity.y == 0 then + local front_node = self:get_front_node() + if front_node.name ~= "air" then + self.object:setvelocity{x = velocity.x, y = 3, z = velocity.z} + end + end + end + end + + local function on_step(self, dtime) + if self.state == state.WALK_RANDOMLY then + walk_randomly(self, dtime) + elseif self.state == state.WALK_TO_PLANT or self.state == state.WALK_TO_MOW then + walk_to_plant_and_mow_common(self, dtime) + elseif self.state == state.PLANT or self.state == state.MOW then + plant_and_mow_common(self, dtime) + end + end + + maidroid.register_core("maidroid_core:farming", { + description = "maidroid core : farming", + inventory_image = "maidroid_core_farming.png", + on_start = on_start, + on_stop = on_stop, + on_resume = on_start, + on_pause = on_stop, + on_step = on_step, + }) + +end -- register farming core diff --git a/maidroid_core/init.lua b/maidroid_core/init.lua index 98f0535..059188c 100644 --- a/maidroid_core/init.lua +++ b/maidroid_core/init.lua @@ -10,3 +10,4 @@ maidroid_core.modpath = minetest.get_modpath(maidroid_core.modname) dofile(maidroid_core.modpath .. "/cores/empty.lua") dofile(maidroid_core.modpath .. "/cores/basic.lua") +dofile(maidroid_core.modpath .. "/cores/farming.lua") From 472ba9b43a1743f31ef18bb54ae10f0d090f9452 Mon Sep 17 00:00:00 2001 From: tacigar Date: Thu, 22 Dec 2016 09:16:28 +0900 Subject: [PATCH 03/13] Add maidroid api --- maidroid/api.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/maidroid/api.lua b/maidroid/api.lua index c83d2e4..46655b6 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -174,6 +174,29 @@ function maidroid.maidroid.has_item_in_main(self, pred) end end +-- maidroid.maidroid.change_direction change direction to destination and velocity vector. +function maidroid.maidroid.change_direction(self, destination) + local position = self.object:getpos() + local direction = vector.subtract(destination, posistion) + direction.y = 0 + local velocity = vector.multiply(vector.normalize(direction), 3) + + self.object:setvelocity(velocity) + self:set_yaw_by_direction(direction) +end + +-- maidroid.maidroid.change_direction_randomly change direction randonly. +function maidroid.maidroid.change_direction_randomly(self) + local direction = { + x = math.random(0, 5) * 2 - 5, + y = 0, + z = math.random(0, 5) * 2 - 5, + } + local velocity = vector.multiply(vector.normalize(direction), 3) + self.object:setvelocity(velocity) + self:set_yaw_by_direction(direction) +end + --------------------------------------------------------------------- -- maidroid.manufacturing_data represents a table that contains manufacturing data. @@ -561,6 +584,8 @@ function maidroid.register_maidroid(product_name, def) move_main_to_wield = maidroid.maidroid.move_main_to_wield, is_named = maidroid.maidroid.is_named, has_item_in_main = maidroid.maidroid.has_item_in_main, + change_direction = maidroid.maidroid.change_direction, + change_direction_randomly = maidroid.maidroid.change_direction_randomly, }) -- register maidroid egg. From 1093fae79247c06a3ca82a21d428919f6ed0491b Mon Sep 17 00:00:00 2001 From: tacigar Date: Thu, 22 Dec 2016 09:29:22 +0900 Subject: [PATCH 04/13] Update main_to_wield --- maidroid/api.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maidroid/api.lua b/maidroid/api.lua index 46655b6..313e70a 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -140,13 +140,13 @@ end -- maidroid.maidroid.move_main_to_wield moves itemstack from main to wield. -- if this function fails then returns false, else returns true. -function maidroid.maidroid.move_main_to_wield(self, itemname) +function maidroid.maidroid.move_main_to_wield(self, pred) local inv = self:get_inventory() local main_size = inv:get_size("main") for i = 1, main_size do local stack = inv:get_stack("main", i) - if stack:get_name() == itemname then + if pred(stack:get_name()) then local wield_stack = inv:get_stack("wield_item", 1) inv:set_stack("wield_item", 1, stack) inv:set_stack("main", i, wield_stack) From 4480a2c3d711c6fa526a29d9135bb5fc9cbe5ef8 Mon Sep 17 00:00:00 2001 From: tacigar Date: Thu, 22 Dec 2016 09:48:50 +0900 Subject: [PATCH 05/13] Add set wield_item slot method --- maidroid/api.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/maidroid/api.lua b/maidroid/api.lua index 313e70a..9c61492 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -132,12 +132,18 @@ function maidroid.maidroid.set_yaw_by_direction(self, direction) self.object:setyaw(math.atan2(direction.z, direction.x) - math.pi / 2) end --- maidroid.maidroid.get_wield_item_info returns the maidroid's wield item's stack. +-- maidroid.maidroid.get_wield_item_stack returns the maidroid's wield item's stack. function maidroid.maidroid.get_wield_item_stack(self) local inv = self:get_inventory() return inv:get_stack("wield_item", 1) end +-- maidroid.maidroid.set_wield_item_stack sets maidroid's wield item stack. +function maidroid.maidroid.set_wield_item_stack(self, stack) + local inv = self:get_inventory() + inv:set_stack("wield_item", 1, stack) +end + -- maidroid.maidroid.move_main_to_wield moves itemstack from main to wield. -- if this function fails then returns false, else returns true. function maidroid.maidroid.move_main_to_wield(self, pred) @@ -581,6 +587,7 @@ function maidroid.register_maidroid(product_name, def) set_animation = maidroid.maidroid.set_animation, set_yaw_by_direction = maidroid.maidroid.set_yaw_by_direction, get_wield_item_stack = maidroid.maidroid.get_wield_item_stack, + set_wield_item_stack = maidroid.maidroid.set_wield_item_stack, move_main_to_wield = maidroid.maidroid.move_main_to_wield, is_named = maidroid.maidroid.is_named, has_item_in_main = maidroid.maidroid.has_item_in_main, From 9f977a48d5c3f4ad30e4de526c49eac000bcd73b Mon Sep 17 00:00:00 2001 From: tacigar Date: Thu, 22 Dec 2016 09:58:02 +0900 Subject: [PATCH 06/13] Add add_item_to_main method --- maidroid/api.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/maidroid/api.lua b/maidroid/api.lua index 9c61492..426ac85 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -144,6 +144,12 @@ function maidroid.maidroid.set_wield_item_stack(self, stack) inv:set_stack("wield_item", 1, stack) end +-- maidroid.maidroid.add_item_to_main add item to main slot. +function maidroid.maidroid.add_item_to_main(self, stack) + local inv = self:get_inventory() + inv:add_item("main", stack) +end + -- maidroid.maidroid.move_main_to_wield moves itemstack from main to wield. -- if this function fails then returns false, else returns true. function maidroid.maidroid.move_main_to_wield(self, pred) @@ -588,6 +594,7 @@ function maidroid.register_maidroid(product_name, def) set_yaw_by_direction = maidroid.maidroid.set_yaw_by_direction, get_wield_item_stack = maidroid.maidroid.get_wield_item_stack, set_wield_item_stack = maidroid.maidroid.set_wield_item_stack, + add_item_to_main = maidroid.maidroid.add_item_to_main, move_main_to_wield = maidroid.maidroid.move_main_to_wield, is_named = maidroid.maidroid.is_named, has_item_in_main = maidroid.maidroid.has_item_in_main, From 0bd9ec423f8642247bea4bae5935221a9fdba89d Mon Sep 17 00:00:00 2001 From: tacigar Date: Thu, 22 Dec 2016 10:02:37 +0900 Subject: [PATCH 07/13] Update farming core --- maidroid/api.lua | 3 +- maidroid_core/cores/farming.lua | 105 ++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/maidroid/api.lua b/maidroid/api.lua index 426ac85..4eb550b 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -145,9 +145,10 @@ function maidroid.maidroid.set_wield_item_stack(self, stack) end -- maidroid.maidroid.add_item_to_main add item to main slot. +-- and returns leftover. function maidroid.maidroid.add_item_to_main(self, stack) local inv = self:get_inventory() - inv:add_item("main", stack) + return inv:add_item("main", stack) end -- maidroid.maidroid.move_main_to_wield moves itemstack from main to wield. diff --git a/maidroid_core/cores/farming.lua b/maidroid_core/cores/farming.lua index 19eb30d..4cfdc25 100644 --- a/maidroid_core/cores/farming.lua +++ b/maidroid_core/cores/farming.lua @@ -92,13 +92,20 @@ do -- register farming core 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) self.state = state.WALK_TO_PLANT self.path = path self.destination = destination self.time_counters[1] = 0 -- find path interval self.time_counters[2] = 0 - self.pre_position = self.object:getpos() + self:change_direction(self.path[1]) end local function to_walk_to_mow(self, path, destination) @@ -107,7 +114,25 @@ do -- register farming core self.destination = destination self.time_counters[1] = 0 -- find path interval 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 local function walk_to_plant_and_mow_common(self, dtime) @@ -128,8 +153,21 @@ do -- register farming core end -- follow path - if vector.distance(path[1], self.object:getpos()) < 0.01 then - table.remove(path, 1) -- next step, follow next path. + if vector.distance(self.path[1], self.object:getpos()) < 0.01 then + 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 -- if maidroid is stopped by obstacles, the maidroid must jump. local velocity = self.object:getvelocity() @@ -142,6 +180,65 @@ do -- register farming core 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) if self.state == state.WALK_RANDOMLY then walk_randomly(self, dtime) From bac29556f026981a9a65b52feffce9cc7623e16d Mon Sep 17 00:00:00 2001 From: tacigar Date: Thu, 22 Dec 2016 15:44:33 +0900 Subject: [PATCH 08/13] Update farming core --- maidroid/api.lua | 2 +- maidroid_core/cores/_aux.lua | 8 +-- maidroid_core/cores/farming.lua | 120 ++++++++++++++++++-------------- maidroid_core/init.lua | 1 + maidroid_tool/core_writer.lua | 1 + 5 files changed, 75 insertions(+), 57 deletions(-) diff --git a/maidroid/api.lua b/maidroid/api.lua index 4eb550b..8ba33ec 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -190,7 +190,7 @@ end -- maidroid.maidroid.change_direction change direction to destination and velocity vector. function maidroid.maidroid.change_direction(self, destination) local position = self.object:getpos() - local direction = vector.subtract(destination, posistion) + local direction = vector.subtract(destination, position) direction.y = 0 local velocity = vector.multiply(vector.normalize(direction), 3) diff --git a/maidroid_core/cores/_aux.lua b/maidroid_core/cores/_aux.lua index 74679e7..2a4083c 100644 --- a/maidroid_core/cores/_aux.lua +++ b/maidroid_core/cores/_aux.lua @@ -5,13 +5,13 @@ 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 y = -searching_range.y, searching_range.y do for z = -searching_range.z, searching_range.z do - local pos = {x = x, y = y, z = z} - if pred(pos) then - return pos + local p = vector.add(pos, {x = x, y = y, z = z}) + if pred(p) then + return p end end end diff --git a/maidroid_core/cores/farming.lua b/maidroid_core/cores/farming.lua index 4cfdc25..569719d 100644 --- a/maidroid_core/cores/farming.lua +++ b/maidroid_core/cores/farming.lua @@ -19,13 +19,13 @@ local target_plants = { local _aux = maidroid_core._aux local FIND_PATH_TIME_INTERVAL = 20 -local CHANGE_DIRECTION_TIME_INTERVAL = 20 +local CHANGE_DIRECTION_TIME_INTERVAL = 30 local MAX_WALK_TIME = 120 -- is_plantable_place reports whether maidroid can plant any seed. local function is_plantable_place(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) return node.name == "air" and minetest.get_item_group(lnode.name, "wet") > 0 @@ -34,17 +34,26 @@ end -- is_mowable_place reports whether maidroid can mow. local function is_mowable_place(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 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) self.object:setacceleration{x = 0, y = -10, z = 0} self.object:setvelocity{x = 0, y = 0, z = 0} self.state = state.WALK_RANDOMLY self.time_counters = {} self.path = nil + to_walk_randomly(self) end local function on_stop(self) @@ -53,15 +62,16 @@ do -- register farming core 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 self.time_counters[1] = 0 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 - local destination = _aux.search_surrounding(is_plantable_place, searching_range) + if self:has_item_in_main(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0) end) then + local destination = _aux.search_surrounding(self.object:getpos(), is_plantable_place, searching_range) 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. to_walk_to_plant(self, path, destination) return @@ -69,10 +79,14 @@ do -- register farming core end end -- 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 - 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. + for _, p in ipairs(path) do + print(p.x, p.y, p.z) + end + to_walk_to_mow(self, path, destination) return end @@ -92,36 +106,44 @@ do -- register farming core end end - local function to_walk_randomly(self) + to_walk_randomly = function(self) + print("to walk randomly") self.state = state.WALK_RANDOMLY self.time_counters[1] = 0 self.time_counters[2] = 0 self:change_direction_randomly() + self:set_animation(maidroid.animation_frames.WALK) 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.path = path self.destination = destination self.time_counters[1] = 0 -- find path interval self.time_counters[2] = 0 self:change_direction(self.path[1]) + self:set_animation(maidroid.animation_frames.WALK) 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.path = path self.destination = destination self.time_counters[1] = 0 -- find path interval self.time_counters[2] = 0 self:change_direction(self.path[1]) + self:set_animation(maidroid.animation_frames.WALK) end - local function to_plant(self) - if self:move_main_to_wield(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0 end)) then + to_plant = function(self) + 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.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 else to_walk_randomly(self) @@ -129,13 +151,25 @@ do -- register farming core end end - local function to_mow(self) + to_mow = function(self) + print("to mow") self.state = state.MOW 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 - 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. to_walk_randomly(self) return @@ -144,7 +178,7 @@ do -- register farming core if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then self.time_counters[1] = 0 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 to_walk_randomly(self) return @@ -169,6 +203,7 @@ do -- register farming core end else + -- self:change_direction(self.path[1]) -- if maidroid is stopped by obstacles, the maidroid must jump. local velocity = self.object:getvelocity() if velocity.y == 0 then @@ -180,23 +215,23 @@ do -- register farming core end end - local function plant(self, dtime) - if self.time_counters[1] >= 5 then + plant = function(self, dtime) + if self.time_counters[1] >= 15 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() + to_walk_randomly(self) 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 + mow = function(self, dtime) + if self.time_counters[1] >= 15 then if is_mowable_place(self.destination) then local destnode = minetest.get_node(self.destination) minetest.remove_node(self.destination) @@ -207,45 +242,26 @@ do -- register farming core minetest.add_item(self.destination, leftover) end end - to_walk_randomly() + to_walk_randomly(self) 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) if self.state == state.WALK_RANDOMLY then +-- print("== now walk randomly") walk_randomly(self, dtime) 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) - elseif self.state == state.PLANT or self.state == state.MOW then - plant_and_mow_common(self, dtime) + elseif self.state == state.PLANT then +-- print("== now plant") + plant(self, dtime) + elseif self.state == state.MOW then +-- print("== now mow") + mow(self, dtime) end end diff --git a/maidroid_core/init.lua b/maidroid_core/init.lua index 059188c..a8af579 100644 --- a/maidroid_core/init.lua +++ b/maidroid_core/init.lua @@ -8,6 +8,7 @@ maidroid_core = {} maidroid_core.modname = "maidroid_core" 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/basic.lua") dofile(maidroid_core.modpath .. "/cores/farming.lua") diff --git a/maidroid_tool/core_writer.lua b/maidroid_tool/core_writer.lua index 3047939..77b3d29 100644 --- a/maidroid_tool/core_writer.lua +++ b/maidroid_tool/core_writer.lua @@ -7,6 +7,7 @@ do -- register core writer local dye_item_map = { ["dye:red"] = "maidroid_core:basic", + ["dye:green"] = "maidroid_core:farming", } local node_box = { From 0e9ca5a64bf7aac5df00965f313783008ae3952a Mon Sep 17 00:00:00 2001 From: tacigar Date: Thu, 22 Dec 2016 19:56:50 +0900 Subject: [PATCH 09/13] Update farming core --- maidroid_core/cores/farming.lua | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/maidroid_core/cores/farming.lua b/maidroid_core/cores/farming.lua index 569719d..eda3c96 100644 --- a/maidroid_core/cores/farming.lua +++ b/maidroid_core/cores/farming.lua @@ -57,7 +57,16 @@ do -- register farming core end local function on_stop(self) + self.object:setvelocity{x = 0, y = 0, z = 0} + self.state = nil + self.time_counters = nil + self.path = nil + end + local function is_near(self, pos, distance) + local p = self.object:getpos() + p.y = p.y + 0.5 + return vector.distance(p, pos) < distance end local searching_range = {x = 5, y = 2, z = 5} @@ -160,7 +169,7 @@ do -- register farming core end walk_to_plant_and_mow_common = function(self, dtime) - if vector.distance(self.object:getpos(), self.destination) < 1.0 then + if is_near(self, self.destination, 1.0) then if self.state == state.WALK_TO_PLANT then to_plant(self) return @@ -175,7 +184,11 @@ do -- register farming core return end + self.time_counters[1] = self.time_counters[1] + 1 + self.time_counters[2] = self.time_counters[2] + 1 + if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then + print("KOKOKOK") self.time_counters[1] = 0 self.time_counters[2] = self.time_counters[2] + 1 local path = minetest.find_path(self.object:getpos(), self.destination, 10, 1, 1, "A*") @@ -187,7 +200,8 @@ do -- register farming core end -- follow path - if vector.distance(self.path[1], self.object:getpos()) < 0.01 then + if is_near(self, self.path[1], 0.01) then + print("KOK") table.remove(self.path, 1) if #self.path == 0 then -- end of path @@ -205,6 +219,7 @@ do -- register farming core else -- self:change_direction(self.path[1]) -- if maidroid is stopped by obstacles, the maidroid must jump. + -- self:change_direction(self.path[1]) local velocity = self.object:getvelocity() if velocity.y == 0 then local front_node = self:get_front_node() @@ -219,9 +234,10 @@ do -- register farming core if self.time_counters[1] >= 15 then if is_plantable_place(self.destination) then local stack = self:get_wield_item_stack() + local itemname = stack:get_name() + minetest.add_node(self.destination, {name = itemname, param2 = 1}) stack:take_item(1) self:set_wield_item_stack(stack) - return end to_walk_randomly(self) return From 712efe7fee6ba08b0eb199a11149154b61bc5f73 Mon Sep 17 00:00:00 2001 From: tacigar Date: Fri, 23 Dec 2016 10:02:15 +0900 Subject: [PATCH 10/13] Update farming core --- maidroid_core/cores/farming.lua | 34 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/maidroid_core/cores/farming.lua b/maidroid_core/cores/farming.lua index eda3c96..4c9f102 100644 --- a/maidroid_core/cores/farming.lua +++ b/maidroid_core/cores/farming.lua @@ -65,7 +65,7 @@ do -- register farming core local function is_near(self, pos, distance) local p = self.object:getpos() - p.y = p.y + 0.5 + -- p.y = p.y + 0.5 return vector.distance(p, pos) < distance end @@ -92,10 +92,6 @@ do -- register farming core if destination ~= nil then local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1, "A*") 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) return end @@ -111,12 +107,19 @@ do -- register farming core else self.time_counters[1] = self.time_counters[1] + 1 self.time_counters[2] = self.time_counters[2] + 1 + + local velocity = self.object:getvelocity() + if velocity.y == 0 then + local front_node = self:get_front_node() + if front_node.name ~= "air" and minetest.registered_nodes[front_node.name].walkable then + self.object:setvelocity{x = velocity.x, y = 6, z = velocity.z} + end + end return end end to_walk_randomly = function(self) - print("to walk randomly") self.state = state.WALK_RANDOMLY self.time_counters[1] = 0 self.time_counters[2] = 0 @@ -125,7 +128,6 @@ do -- register farming core end to_walk_to_plant = function(self, path, destination) - print("to walk to plant") self.state = state.WALK_TO_PLANT self.path = path self.destination = destination @@ -136,7 +138,6 @@ do -- register farming core end to_walk_to_mow = function(self, path, destination) - print("to walk to mow") self.state = state.WALK_TO_MOW self.path = path self.destination = destination @@ -147,7 +148,6 @@ do -- register farming core end to_plant = function(self) - 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.time_counters[1] = 0 @@ -161,7 +161,6 @@ do -- register farming core end to_mow = function(self) - print("to mow") self.state = state.MOW self.time_counters[1] = 0 self.object:setvelocity{x = 0, y = 0, z = 0} @@ -169,7 +168,7 @@ do -- register farming core end walk_to_plant_and_mow_common = function(self, dtime) - if is_near(self, self.destination, 1.0) then + if is_near(self, self.destination, 1.5) then if self.state == state.WALK_TO_PLANT then to_plant(self) return @@ -188,9 +187,7 @@ do -- register farming core self.time_counters[2] = self.time_counters[2] + 1 if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then - print("KOKOKOK") self.time_counters[1] = 0 - self.time_counters[2] = self.time_counters[2] + 1 local path = minetest.find_path(self.object:getpos(), self.destination, 10, 1, 1, "A*") if path == nil then to_walk_randomly(self) @@ -200,8 +197,7 @@ do -- register farming core end -- follow path - if is_near(self, self.path[1], 0.01) then - print("KOK") + if is_near(self, self.path[1], 0.5) then table.remove(self.path, 1) if #self.path == 0 then -- end of path @@ -223,8 +219,8 @@ do -- register farming core local velocity = self.object:getvelocity() if velocity.y == 0 then local front_node = self:get_front_node() - if front_node.name ~= "air" then - self.object:setvelocity{x = velocity.x, y = 3, z = velocity.z} + if front_node.name ~= "air" and minetest.registered_nodes[front_node.name].walkable then + self.object:setvelocity{x = velocity.x, y = 6, z = velocity.z} end end end @@ -267,16 +263,12 @@ do -- register farming core local function on_step(self, dtime) if self.state == state.WALK_RANDOMLY then --- print("== now walk randomly") walk_randomly(self, dtime) 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) elseif self.state == state.PLANT then --- print("== now plant") plant(self, dtime) elseif self.state == state.MOW then --- print("== now mow") mow(self, dtime) end end From aae13499f1622ccae2f8e7207482cb0638479f64 Mon Sep 17 00:00:00 2001 From: tacigar Date: Fri, 23 Dec 2016 11:02:17 +0900 Subject: [PATCH 11/13] Update and bug fix --- maidroid_core/cores/farming.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/maidroid_core/cores/farming.lua b/maidroid_core/cores/farming.lua index 4c9f102..93e7eec 100644 --- a/maidroid_core/cores/farming.lua +++ b/maidroid_core/cores/farming.lua @@ -61,6 +61,7 @@ do -- register farming core self.state = nil self.time_counters = nil self.path = nil + self:set_animation(maidroid.animation_frames.STAND) end local function is_near(self, pos, distance) @@ -76,7 +77,9 @@ do -- register farming core self.time_counters[1] = 0 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) end) then + local wield_stack = self:get_wield_item_stack() + if minetest.get_item_group(wield_stack:get_name(), "seed") > 0 + or self:has_item_in_main(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0) end) then local destination = _aux.search_surrounding(self.object:getpos(), is_plantable_place, searching_range) if destination ~= nil then local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1, "A*") @@ -111,7 +114,8 @@ do -- register farming core local velocity = self.object:getvelocity() if velocity.y == 0 then local front_node = self:get_front_node() - if front_node.name ~= "air" and minetest.registered_nodes[front_node.name].walkable then + if front_node.name ~= "air" and minetest.registered_nodes[front_node.name].walkable + and not (minetest.get_item_group(front_node.name, "fence") > 0) then self.object:setvelocity{x = velocity.x, y = 6, z = velocity.z} end end @@ -148,7 +152,9 @@ do -- register farming core end to_plant = function(self) - if self:move_main_to_wield(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0) end) then + local wield_stack = self:get_wield_item_stack() + if minetest.get_item_group(wield_stack:get_name(), "seed") > 0 + or 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:setvelocity{x = 0, y = 0, z = 0} @@ -219,7 +225,8 @@ do -- register farming core local velocity = self.object:getvelocity() if velocity.y == 0 then local front_node = self:get_front_node() - if front_node.name ~= "air" and minetest.registered_nodes[front_node.name].walkable then + if front_node.name ~= "air" and minetest.registered_nodes[front_node.name].walkable + and not (minetest.get_item_group(front_node.name, "fence") > 0) then self.object:setvelocity{x = velocity.x, y = 6, z = velocity.z} end end From 058665db83a6c86371ad601c560e01a234976ed7 Mon Sep 17 00:00:00 2001 From: tacigar Date: Fri, 23 Dec 2016 12:00:10 +0900 Subject: [PATCH 12/13] Update farming core --- maidroid_core/cores/farming.lua | 405 ++++++++++++++++---------------- 1 file changed, 201 insertions(+), 204 deletions(-) diff --git a/maidroid_core/cores/farming.lua b/maidroid_core/cores/farming.lua index 93e7eec..5f71a76 100644 --- a/maidroid_core/cores/farming.lua +++ b/maidroid_core/cores/farming.lua @@ -42,139 +42,170 @@ local function is_mowable_place(pos) return false end -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 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) - self.object:setacceleration{x = 0, y = -10, z = 0} - self.object:setvelocity{x = 0, y = 0, z = 0} - self.state = state.WALK_RANDOMLY - self.time_counters = {} - self.path = nil - to_walk_randomly(self) - end +local function on_start(self) + self.object:setacceleration{x = 0, y = -10, z = 0} + self.object:setvelocity{x = 0, y = 0, z = 0} + self.state = state.WALK_RANDOMLY + self.time_counters = {} + self.path = nil + to_walk_randomly(self) +end - local function on_stop(self) - self.object:setvelocity{x = 0, y = 0, z = 0} - self.state = nil - self.time_counters = nil - self.path = nil - self:set_animation(maidroid.animation_frames.STAND) - end +local function on_stop(self) + self.object:setvelocity{x = 0, y = 0, z = 0} + self.state = nil + self.time_counters = nil + self.path = nil + self:set_animation(maidroid.animation_frames.STAND) +end - local function is_near(self, pos, distance) - local p = self.object:getpos() - -- p.y = p.y + 0.5 - return vector.distance(p, pos) < distance - end +local function is_near(self, pos, distance) + local p = self.object:getpos() + -- p.y = p.y + 0.5 + return vector.distance(p, pos) < distance +end - local searching_range = {x = 5, y = 2, z = 5} +local searching_range = {x = 5, y = 2, z = 5} - walk_randomly = function(self, dtime) - if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then - self.time_counters[1] = 0 - self.time_counters[2] = self.time_counters[2] + 1 +walk_randomly = function(self, dtime) + if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then + self.time_counters[1] = 0 + self.time_counters[2] = self.time_counters[2] + 1 - local wield_stack = self:get_wield_item_stack() - if minetest.get_item_group(wield_stack:get_name(), "seed") > 0 - or self:has_item_in_main(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0) end) then - local destination = _aux.search_surrounding(self.object:getpos(), is_plantable_place, searching_range) - if destination ~= nil then - local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1, "A*") - - if path ~= nil then -- to walk to plant state. - to_walk_to_plant(self, path, destination) - return - end - end - end - -- if couldn't find path to plant, try to mow. - local destination = _aux.search_surrounding(self.object:getpos(), is_mowable_place, searching_range) + local wield_stack = self:get_wield_item_stack() + if minetest.get_item_group(wield_stack:get_name(), "seed") > 0 + or self:has_item_in_main(function(itemname) return (minetest.get_item_group(itemname, "seed") > 0) end) then + local destination = _aux.search_surrounding(self.object:getpos(), is_plantable_place, searching_range) if destination ~= nil then local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1, "A*") - if path ~= nil then -- to walk to mow state. - to_walk_to_mow(self, path, destination) + + if path ~= nil then -- to walk to plant state. + to_walk_to_plant(self, path, destination) return end end - -- else do nothing. - return - - elseif self.time_counters[2] >= CHANGE_DIRECTION_TIME_INTERVAL then - self.time_counters[1] = self.time_counters[1] + 1 - self.time_counters[2] = 0 - self:change_direction_randomly() - return - else - self.time_counters[1] = self.time_counters[1] + 1 - self.time_counters[2] = self.time_counters[2] + 1 - - local velocity = self.object:getvelocity() - if velocity.y == 0 then - local front_node = self:get_front_node() - if front_node.name ~= "air" and minetest.registered_nodes[front_node.name].walkable - and not (minetest.get_item_group(front_node.name, "fence") > 0) then - self.object:setvelocity{x = velocity.x, y = 6, z = velocity.z} - end - end - return end - end + -- if couldn't find path to plant, try to mow. + local destination = _aux.search_surrounding(self.object:getpos(), is_mowable_place, searching_range) + if destination ~= nil then + local path = minetest.find_path(self.object:getpos(), destination, 10, 1, 1, "A*") + if path ~= nil then -- to walk to mow state. + to_walk_to_mow(self, path, destination) + return + end + end + -- else do nothing. + return - to_walk_randomly = function(self) - self.state = state.WALK_RANDOMLY - self.time_counters[1] = 0 + elseif self.time_counters[2] >= CHANGE_DIRECTION_TIME_INTERVAL then + self.time_counters[1] = self.time_counters[1] + 1 self.time_counters[2] = 0 self:change_direction_randomly() - self:set_animation(maidroid.animation_frames.WALK) - end + return + else + self.time_counters[1] = self.time_counters[1] + 1 + self.time_counters[2] = self.time_counters[2] + 1 - to_walk_to_plant = function(self, path, destination) - self.state = state.WALK_TO_PLANT - self.path = path - self.destination = destination - self.time_counters[1] = 0 -- find path interval - self.time_counters[2] = 0 - self:change_direction(self.path[1]) - self:set_animation(maidroid.animation_frames.WALK) - end - - to_walk_to_mow = function(self, path, destination) - self.state = state.WALK_TO_MOW - self.path = path - self.destination = destination - self.time_counters[1] = 0 -- find path interval - self.time_counters[2] = 0 - self:change_direction(self.path[1]) - self:set_animation(maidroid.animation_frames.WALK) - end - - to_plant = function(self) - local wield_stack = self:get_wield_item_stack() - if minetest.get_item_group(wield_stack:get_name(), "seed") > 0 - or 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:setvelocity{x = 0, y = 0, z = 0} - self:set_animation(maidroid.animation_frames.MINE) - return - else - to_walk_randomly(self) - return + local velocity = self.object:getvelocity() + if velocity.y == 0 then + local front_node = self:get_front_node() + if front_node.name ~= "air" and minetest.registered_nodes[front_node.name].walkable + and not (minetest.get_item_group(front_node.name, "fence") > 0) then + self.object:setvelocity{x = velocity.x, y = 6, z = velocity.z} + end end + return end +end - to_mow = function(self) - self.state = state.MOW +to_walk_randomly = function(self) + self.state = state.WALK_RANDOMLY + self.time_counters[1] = 0 + self.time_counters[2] = 0 + self:change_direction_randomly() + self:set_animation(maidroid.animation_frames.WALK) +end + +to_walk_to_plant = function(self, path, destination) + self.state = state.WALK_TO_PLANT + self.path = path + self.destination = destination + self.time_counters[1] = 0 -- find path interval + self.time_counters[2] = 0 + self:change_direction(self.path[1]) + self:set_animation(maidroid.animation_frames.WALK) +end + +to_walk_to_mow = function(self, path, destination) + self.state = state.WALK_TO_MOW + self.path = path + self.destination = destination + self.time_counters[1] = 0 -- find path interval + self.time_counters[2] = 0 + self:change_direction(self.path[1]) + self:set_animation(maidroid.animation_frames.WALK) +end + +to_plant = function(self) + local wield_stack = self:get_wield_item_stack() + if minetest.get_item_group(wield_stack:get_name(), "seed") > 0 + or 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:setvelocity{x = 0, y = 0, z = 0} self:set_animation(maidroid.animation_frames.MINE) + return + else + to_walk_randomly(self) + return + end +end + +to_mow = function(self) + self.state = state.MOW + self.time_counters[1] = 0 + self.object:setvelocity{x = 0, y = 0, z = 0} + self:set_animation(maidroid.animation_frames.MINE) +end + +walk_to_plant_and_mow_common = function(self, dtime) + if is_near(self, self.destination, 1.5) 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 - walk_to_plant_and_mow_common = function(self, dtime) - if is_near(self, self.destination, 1.5) then + if self.time_counters[2] >= MAX_WALK_TIME then -- time over. + to_walk_randomly(self) + return + end + + self.time_counters[1] = self.time_counters[1] + 1 + self.time_counters[2] = self.time_counters[2] + 1 + + if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then + self.time_counters[1] = 0 + local path = minetest.find_path(self.object:getpos(), self.destination, 10, 1, 1, "A*") + if path == nil then + to_walk_randomly(self) + return + end + self.path = path + end + + -- follow path + if is_near(self, self.path[1], 0.5) then + 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 @@ -182,112 +213,78 @@ do -- register farming core to_mow(self) return end + else -- else next step, follow next path. + self:change_direction(self.path[1]) end - if self.time_counters[2] >= MAX_WALK_TIME then -- time over. - to_walk_randomly(self) - return + else + -- self:change_direction(self.path[1]) + -- if maidroid is stopped by obstacles, the maidroid must jump. + -- self:change_direction(self.path[1]) + local velocity = self.object:getvelocity() + if velocity.y == 0 then + local front_node = self:get_front_node() + if front_node.name ~= "air" and minetest.registered_nodes[front_node.name].walkable + and not (minetest.get_item_group(front_node.name, "fence") > 0) then + self.object:setvelocity{x = velocity.x, y = 6, z = velocity.z} + end end + end +end +plant = function(self, dtime) + if self.time_counters[1] >= 15 then + if is_plantable_place(self.destination) then + local stack = self:get_wield_item_stack() + local itemname = stack:get_name() + minetest.add_node(self.destination, {name = itemname, param2 = 1}) + stack:take_item(1) + self:set_wield_item_stack(stack) + end + to_walk_randomly(self) + return + else self.time_counters[1] = self.time_counters[1] + 1 - self.time_counters[2] = self.time_counters[2] + 1 - - if self.time_counters[1] >= FIND_PATH_TIME_INTERVAL then - self.time_counters[1] = 0 - local path = minetest.find_path(self.object:getpos(), self.destination, 10, 1, 1, "A*") - if path == nil then - to_walk_randomly(self) - return - end - self.path = path - end - - -- follow path - if is_near(self, self.path[1], 0.5) then - 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 - -- self:change_direction(self.path[1]) - -- if maidroid is stopped by obstacles, the maidroid must jump. - -- self:change_direction(self.path[1]) - local velocity = self.object:getvelocity() - if velocity.y == 0 then - local front_node = self:get_front_node() - if front_node.name ~= "air" and minetest.registered_nodes[front_node.name].walkable - and not (minetest.get_item_group(front_node.name, "fence") > 0) then - self.object:setvelocity{x = velocity.x, y = 6, z = velocity.z} - end - end - end end +end - plant = function(self, dtime) - if self.time_counters[1] >= 15 then - if is_plantable_place(self.destination) then - local stack = self:get_wield_item_stack() - local itemname = stack:get_name() - minetest.add_node(self.destination, {name = itemname, param2 = 1}) - stack:take_item(1) - self:set_wield_item_stack(stack) +mow = function(self, dtime) + if self.time_counters[1] >= 15 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 - to_walk_randomly(self) - return - else - self.time_counters[1] = self.time_counters[1] + 1 end + to_walk_randomly(self) + return + else + self.time_counters[1] = self.time_counters[1] + 1 end +end - mow = function(self, dtime) - if self.time_counters[1] >= 15 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(self) - return - else - self.time_counters[1] = self.time_counters[1] + 1 - end +local function on_step(self, dtime) + if self.state == state.WALK_RANDOMLY then + walk_randomly(self, dtime) + elseif self.state == state.WALK_TO_PLANT or self.state == state.WALK_TO_MOW then + walk_to_plant_and_mow_common(self, dtime) + elseif self.state == state.PLANT then + plant(self, dtime) + elseif self.state == state.MOW then + mow(self, dtime) end +end - local function on_step(self, dtime) - if self.state == state.WALK_RANDOMLY then - walk_randomly(self, dtime) - elseif self.state == state.WALK_TO_PLANT or self.state == state.WALK_TO_MOW then - walk_to_plant_and_mow_common(self, dtime) - elseif self.state == state.PLANT then - plant(self, dtime) - elseif self.state == state.MOW then - mow(self, dtime) - end - end - - maidroid.register_core("maidroid_core:farming", { - description = "maidroid core : farming", - inventory_image = "maidroid_core_farming.png", - on_start = on_start, - on_stop = on_stop, - on_resume = on_start, - on_pause = on_stop, - on_step = on_step, - }) - -end -- register farming core +maidroid.register_core("maidroid_core:farming", { + description = "maidroid core : farming", + inventory_image = "maidroid_core_farming.png", + on_start = on_start, + on_stop = on_stop, + on_resume = on_start, + on_pause = on_stop, + on_step = on_step, +}) From 32d0ffbb9b5f28cf3ddeb7c01d80f708246892a6 Mon Sep 17 00:00:00 2001 From: tacigar Date: Fri, 23 Dec 2016 12:05:36 +0900 Subject: [PATCH 13/13] Add farming core imgs --- maidroid_core/textures/maidroid_core_farming.png | Bin 0 -> 864 bytes maidroid_core/textures/maidroid_core_farming.xcf | Bin 0 -> 1513 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 maidroid_core/textures/maidroid_core_farming.png create mode 100644 maidroid_core/textures/maidroid_core_farming.xcf diff --git a/maidroid_core/textures/maidroid_core_farming.png b/maidroid_core/textures/maidroid_core_farming.png new file mode 100644 index 0000000000000000000000000000000000000000..cec53dfdcd0fd5dd71eb03cabc07529dbadb39e0 GIT binary patch literal 864 zcmV-m1E2hfP)WFU8GbZ8()Nlj2>E@cM*00P8GL_t(I%XO1oOB-nv z$A8b$IO9B-U}oJ4f(F|qN&^uQL~)CeEcim(pbLJ1C0Bd7AHZ9M_8ZjhP5cPnc@Y8@ zUs8>N;7%?CyY5A7Bf*Zgm1Q8u(&dv4G9|2QxIBf>D`z9^MSUnMe`jJU5(1VMlh z;$GqQ_BMuLFfuaoU68oCxAXxlcCNQB|xVY<4yC>DzV3=Iu^1u7PcKS;-M za2$tLtHqydYXHp7&N4YUiR-$YpPw^6KF-X{3_!bPZ*NcC-rn|#YPCwaTn?2|Xqtv) zSzKOTqU$=Rr>6`K4sv#OMsIH~W;9Cc?hXLo_W_359lU$D&%+-dQm@yEMx(S^E!Nl9 zSzcadU|;}%Pma@!$KzaFTmT@YynfKv*B6Fi$cK*~NG6kfavZ`i*R#h)m5gZrdXI=WO43SetP_v zm6a7ywQV#_Bb7?AySocOc64;`{_popXEJzR6U(yL-`{6pae=6cp6u-GB*wrRQJxU@&3T>)E*g0000lp1KRZGQRg`0>d8jJXQVG+%jVkE_ z`B;`4boP1r$L_5}nLZjpJnWb=M0@m|U1-LVwd&pVNrYZ{`wuhdQV_US{3 z#2)YSl=s|$yvHYG-bChoDU+FYIir862&syMh(A2gn+*S9qz8d-L+L}98MMB(3SoM*5qj% literal 0 HcmV?d00001