From ff5aa10c4d93fa75d9331042eb0eec943320028f Mon Sep 17 00:00:00 2001 From: tacigar Date: Sat, 24 Dec 2016 09:23:31 +0900 Subject: [PATCH] Brush up code --- maidroid/api.lua | 9 ++++----- maidroid_core/cores/basic.lua | 11 ++++------- maidroid_core/cores/farming.lua | 10 +++++++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/maidroid/api.lua b/maidroid/api.lua index f5f4efc..0607007 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -264,7 +264,7 @@ do if maidroid.is_maidroid(luaentity.name) then local stack = luaentity:get_wield_item_stack() - if stack:get_name() ~= itemname then + if stack:get_name() ~= self.itemname then if stack:is_empty() then self.itemname = "" self.object:set_properties{textures={"maidroid:dummy_empty_craftitem"}} @@ -448,7 +448,7 @@ function maidroid.register_maidroid(product_name, def) local wield_item = data["inventory"]["wield_item"] if core_name ~= "" then -- set a core - core_stack = ItemStack(core_name) + local core_stack = ItemStack(core_name) core_stack:set_count(1) inventory:add_item("core", core_stack) self.core_name = core_name @@ -518,7 +518,7 @@ function maidroid.register_maidroid(product_name, def) end -- maidroid.maidroid.pickup_item pickup items placed and put it to main slot. - function pickup_item(self) + local function pickup_item(self) local pos = self.object:getpos() local radius = 1.0 local all_objects = minetest.get_objects_inside_radius(pos, radius) @@ -527,8 +527,7 @@ function maidroid.register_maidroid(product_name, def) if not obj:is_player() and obj:get_luaentity() then local itemstring = obj:get_luaentity().itemstring - if minetest.registered_nodes[itemstring] ~= nil or minetest.registered_items[itemstring] ~= nil - or minetest.registered_tools[itemstring] ~= nil or minetest.registered_craftitems[itemstring] ~= nil then + if minetest.registered_items[itemstring] ~= nil then local inv = self:get_inventory() local stack = ItemStack(itemstring) local leftover = inv:add_item("main", stack) diff --git a/maidroid_core/cores/basic.lua b/maidroid_core/cores/basic.lua index 3d4ecbd..6fb48bc 100644 --- a/maidroid_core/cores/basic.lua +++ b/maidroid_core/cores/basic.lua @@ -16,10 +16,6 @@ local function on_stop(self) self.object:setvelocity{x = 0, y = 0, z = 0} end -local on_resume = on_start - -local on_pause = on_stop - local function on_step(self, dtime) local player = self:get_nearest_player(10) if player == nil then @@ -50,7 +46,8 @@ local function on_step(self, dtime) -- if maidroid is stoped by obstacle, the maidroid must jump. if velocity.y == 0 and self.state == state.ACCOMPANY then local front_node = self:get_front_node() - if front_node.name ~= "air" then + if front_node.name ~= "air" and minetest.registered_nodes[front_node.name] ~= nil + and minetest.registered_nodes[front_node.name].walkable then self.object:setvelocity{x = direction.x, y = 5, z = direction.z} end end @@ -62,7 +59,7 @@ maidroid.register_core("maidroid_core:basic", { inventory_image = "maidroid_core_basic.png", on_start = on_start, on_stop = on_stop, - on_resume = on_resume, - on_pause = on_pause, + on_resume = on_start, + on_pause = on_stop, on_step = on_step, }) diff --git a/maidroid_core/cores/farming.lua b/maidroid_core/cores/farming.lua index b2a70f1..884e0ae 100644 --- a/maidroid_core/cores/farming.lua +++ b/maidroid_core/cores/farming.lua @@ -113,8 +113,11 @@ walk_randomly = function(self, dtime) 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 + + if minetest.get_item_group(front_node.name, "fence") > 0 then + self:change_direction_randomly() + elseif front_node.name ~= "air" and minetest.registered_nodes[front_node.name] ~= nil + and minetest.registered_nodes[front_node.name].walkable then self.object:setvelocity{x = velocity.x, y = 6, z = velocity.z} end end @@ -224,7 +227,8 @@ walk_to_plant_and_mow_common = function(self, dtime) 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 + if front_node.name ~= "air" and minetest.registered_nodes[front_node.name] ~= nil + 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