From 465a919f6b77048c7cd9903ae5c3965f651976ec Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Sat, 31 Dec 2022 19:41:42 +0000 Subject: [PATCH] Villager will now reclaim job during the day even when it isn't work time --- mods/ENTITIES/mobs_mc/villager.lua | 136 +++++++++++++++-------------- 1 file changed, 70 insertions(+), 66 deletions(-) diff --git a/mods/ENTITIES/mobs_mc/villager.lua b/mods/ENTITIES/mobs_mc/villager.lua index fc12899cd..ce1e76c72 100644 --- a/mods/ENTITIES/mobs_mc/villager.lua +++ b/mods/ENTITIES/mobs_mc/villager.lua @@ -770,7 +770,7 @@ local function check_bed (entity) local n = minetest.get_node(b) local is_bed_bottom = string.find(n.name,"_bottom") - mcl_log("" .. tostring(is_bed_bottom)) + --mcl_log("is bed bottom: " .. tostring(is_bed_bottom)) if n and not is_bed_bottom then mcl_log("Where did my bed go?!") entity._bed = nil --the stormtroopers have killed uncle owen @@ -1135,59 +1135,54 @@ local function validate_jobsite(self) end local function do_work (self) - --debug_trades(self) - if self.child then - mcl_log("A child so don't send to work") + + if not self or self.child then + mcl_log("No self, or a child so don't work") return end + --mcl_log("Time for work") + local jobsite_node = retrieve_my_jobsite (self) - -- Don't try if looking_for_work, or gowp possibly - if validate_jobsite(self) then - --mcl_log("My jobsite is valid. Do i need to travel?") - - local jobsite2 = retrieve_my_jobsite (self) + if jobsite_node then local jobsite = self._jobsite - if self and jobsite2 and self._jobsite then - local distance_to_jobsite = vector.distance(self.object:get_pos(),self._jobsite) - --mcl_log("Villager: ".. minetest.pos_to_string(self.object:get_pos()) .. ", jobsite: " .. minetest.pos_to_string(self._jobsite) .. ", distance to jobsite: ".. distance_to_jobsite) + local distance_to_jobsite = vector.distance(self.object:get_pos(), jobsite) + --mcl_log("Villager: ".. minetest.pos_to_string(self.object:get_pos()) .. ", jobsite: " .. minetest.pos_to_string(self._jobsite) .. ", distance to jobsite: ".. distance_to_jobsite) - if distance_to_jobsite < 2 then - if self.state ~= PATHFINDING and self.order ~= WORK then - mcl_log("Setting order to work.") - self.order = WORK - unlock_trades(self) - else - --mcl_log("Still pathfinding.") - end + if distance_to_jobsite < 2 then + if self.state ~= PATHFINDING and self.order ~= WORK then + mcl_log("Setting order to work.") + self.order = WORK + unlock_trades(self) else - mcl_log("Not at job block. Need to commute.") - if self.order == WORK then - self.order = nil - return - end - self:gopath(jobsite, function(self,jobsite) - if not self then - --mcl_log("missing self. not good") - return false - end - if not self._jobsite then - --mcl_log("Jobsite not valid") - return false - end - if vector.distance(self.object:get_pos(),self._jobsite) < 2 then - --mcl_log("Made it to work ok callback!") - return true - else - --mcl_log("Need to walk to work. Not sure we can get here.") - end - end) + --mcl_log("Still pathfinding.") end + else + mcl_log("Not at job block. Need to commute.") + if self.order == WORK then + self.order = nil + return + end + self:gopath(jobsite, function(self, jobsite) + if not self then + --mcl_log("missing self. not good") + return false + end + if not self._jobsite then + --mcl_log("Jobsite not valid") + return false + end + if vector.distance(self.object:get_pos(),self._jobsite) < 2 then + --mcl_log("Made it to work ok callback!") + return true + else + --mcl_log("Need to walk to work. Not sure we can get here.") + end + end) end - elseif self._profession == "unemployed" or has_traded(self) then - get_a_job(self) end + end local below_vec = vector.new(0, -1, 0) @@ -1214,17 +1209,10 @@ local function get_ground_below_floating_object (float_pos) end local function go_to_town_bell(self) - if self.order == GATHERING then - --mcl_log("Already gathering") - return - else - mcl_log("Current order" .. self.order) - end + if self.order == GATHERING then return + else mcl_log("Current order" .. self.order) end - if not self:ready_to_path() then - mcl_log("Negative response to go_path. Do not bother") - return - end + if not self:ready_to_path() then return end mcl_log("Go to town bell") @@ -1307,22 +1295,45 @@ local function validate_bed(self) end local function do_activity (self) - -- Maybe just check we're pathfinding first? + if self.following then mcl_log("Following, so do not do activity.") return end + if self.state == PATHFINDING then + mcl_log("Pathfinding, so do not do activity.") + return + end - if not validate_bed(self) and self.state ~= PATHFINDING then + local jobsite_valid = false + + if not mcl_beds.is_night() then if self.order == SLEEP then self.order = nil end - mcl_log("Villager has no bed. Currently at location: "..minetest.pos_to_string(self.object:get_pos())) - take_bed (self) + + if not validate_jobsite(self) then + --debug_trades(self) + if self._profession == "unemployed" or has_traded(self) then + get_a_job(self) + return + end + else + jobsite_valid = true + --mcl_log("My jobsite is valid. Do i need to travel?") + end + else + if self.order == WORK then self.order = nil end + + if not validate_bed(self) then + if self.order == SLEEP then self.order = nil end + mcl_log("Villager at this location has no bed: " .. minetest.pos_to_string(self.object:get_pos())) + take_bed (self) + end end -- Only check in day or during thunderstorm but wandered_too_far code won't work local wandered_too_far = false if check_bed (self) then - wandered_too_far = ( self.state ~= PATHFINDING ) and (vector.distance(self.object:get_pos(),self._bed) > 50 ) + wandered_too_far = vector.distance(self.object:get_pos(),self._bed) > 50 end if wandered_too_far then @@ -1330,7 +1341,7 @@ local function do_activity (self) go_home(self, false) elseif get_activity() == SLEEP then go_home(self, true) - elseif get_activity() == WORK then + elseif get_activity() == WORK and jobsite_valid then do_work(self) elseif get_activity() == GATHERING then go_to_town_bell(self) @@ -1339,13 +1350,6 @@ local function do_activity (self) self.order = nil end - -- Daytime is work and play time - if not mcl_beds.is_night() then - if self.order == SLEEP then self.order = nil end - else - if self.order == WORK then self.order = nil end - end - end local function update_max_tradenum(self)