From f8fc111b4a23568d2b5814bc517a493f347474e0 Mon Sep 17 00:00:00 2001 From: cora Date: Wed, 18 May 2022 00:03:40 +0200 Subject: [PATCH] pathfind to jobsites --- mods/ENTITIES/mcl_mobs/api.lua | 31 ++++++++++++++----- mods/ENTITIES/mobs_mc/villager.lua | 48 +++++++++++++++++++----------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index cebb4aaffc..5a48734380 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -2329,6 +2329,7 @@ local function go_to_pos(entity,b) if b.x > s.x then yaw = yaw + math.pi end entity.object:set_yaw(yaw) set_velocity(entity,entity.follow_velocity) + mobs:set_animation(entity, "walk") end -- execute current state (stand, walk, run, attacks) @@ -2387,9 +2388,9 @@ local do_states = function(self, dtime) end elseif self.state == "gowp" then - if not self.waypoints or not self._target then return end local p = self.object:get_pos() - if vector.distance(p,self._target) < 1 then + if not p or not self._target then return end + if vector.distance(p,self._target) < 2 or #self.waypoints == 0 then self.waypoints = nil self._target = nil self.current_target = nil @@ -2399,13 +2400,19 @@ local do_states = function(self, dtime) end if not self.current_target or vector.distance(p,self.current_target) < 1.5 then self.current_target = table.remove(self.waypoints, 1) - else + --minetest.log("nextwp:".. tostring(self.current_target) ) + elseif self.current_target then go_to_pos(self,self.current_target) end if self.current_target and not minetest.line_of_sight(self.object:get_pos(),self.current_target) then self.waypoints=minetest.find_path(p,self._target,150,1,4) self.current_target = nil + return + end + if not self.current_target then + --minetest.log("no path") + self.state = "walk" end elseif self.state == "walk" then @@ -2913,11 +2920,19 @@ local do_states = function(self, dtime) end function mobs:go_wplist(self,target,callback_arrived) - if not target then return end - self._target = target - self.waypoints = minetest.find_path(self.object:get_pos(),target,150,1,4) - self.callback_arrived = callback_arrived - self.state = "gowp" + local p = self.object:get_pos() + local t = vector.offset(target,0,1,0) + if not target or not p then return end + local wp = minetest.find_path(p,t,150,1,4) + if wp and #wp > 0 then + self._target = t + self.callback_arrived = callback_arrived + self.waypoints = wp + self.state = "gowp" + return true + else + --minetest.log("no path found") + end end diff --git a/mods/ENTITIES/mobs_mc/villager.lua b/mods/ENTITIES/mobs_mc/villager.lua index 64712971ed..76fc764156 100644 --- a/mods/ENTITIES/mobs_mc/villager.lua +++ b/mods/ENTITIES/mobs_mc/villager.lua @@ -69,6 +69,14 @@ local tiernames = { "Master", } +local badges = { + "mcl_core:wood", + "mcl_core:stone", + "mcl_core:goldblock", + "mcl_core:emeraldblock", + "mcl_core:diamondblock", +} + local professions = { unemployed = { name = N("Unemployed"), @@ -454,7 +462,7 @@ local professions = { "mobs_mc_villager_smith.png", "mobs_mc_villager_smith.png", }, - jobsite = "mcl_villages:stonebrickcarved", --FIXME: smithing table + jobsite = "mcl_anvils:anvil", --FIXME: smithing table trades = { { { { "mcl_core:coal_lump", 15, 15 }, E1 }, @@ -602,10 +610,21 @@ local function employ(self,jobsite_pos) end end -local function unemploy(self) - self._profession="unemployed" - self._jobsite = nil - self.object:set_properties({textures=professions[self._profession].textures}) +local function look_for_job(self) + local p = self.object:get_pos() + local nn = minetest.find_nodes_in_area(vector.offset(p,-48,-48,-48),vector.offset(p,48,48,48),jobsites) + for _,n in pairs(nn) do + local m=minetest.get_meta(n) + if m:get_string("villager") == "" then + --minetest.log("goingt to jobsite "..minetest.pos_to_string(n) ) + minetest.after(0,function() + mobs:go_wplist(self,n,function() + --minetest.log("arrived jobsite "..minetest.pos_to_string(n) ) + end) + end) + return + end + end end local function get_a_job(self) @@ -614,16 +633,7 @@ local function get_a_job(self) for _,n in pairs(nn) do if n and employ(self,n) then return true end end -end - -local function check_jobsite(self) - local n = minetest.get_node(self._jobsite) - local m = minetest.get_meta(self._jobsite) - if n.name ~= professions[self._profession].jobsite or m:get_string("villager") ~= self._id then - --unemploy(self) - return false - end - return true + if self.state ~= "gowp" then look_for_job(self) end end local update_max_tradenum = function(self) @@ -1231,9 +1241,11 @@ mobs:register_mob("mobs_mc:villager", { look_at_player = true, on_rightclick = function(self, clicker) local trg=vector.new(0,9,0) - mobs:go_wplist(self,trg,function() - minetest.log("arrived at "..minetest.pos_to_string(trg)) - end) + if self._jobsite then + mobs:go_wplist(self,self._jobsite,function() + --minetest.log("arrived at jobsite") + end) + end if clicker:get_wielded_item():get_name() == "mcl_farming:bread" then if mobs:feed_tame(self, clicker, 1, true, true) then return end if mobs:protect(self, clicker) then return end