Merge pull request 'Allow villagers to resettle and not run back to job and and old bed' (#2967) from feature/villager_pt_6 into master

Reviewed-on: MineClone2/MineClone2#2967
Reviewed-by: cora <cora@noreply.git.minetest.land>
This commit is contained in:
cora 2022-11-17 01:52:52 +00:00
commit 4dba6a62cf
1 changed files with 51 additions and 13 deletions

View File

@ -30,6 +30,8 @@ local DEFAULT_WALK_CHANCE = 33 -- chance to walk in percent, if no player nearby
local PLAYER_SCAN_INTERVAL = 5 -- every X seconds, villager looks for players nearby local PLAYER_SCAN_INTERVAL = 5 -- every X seconds, villager looks for players nearby
local PLAYER_SCAN_RADIUS = 4 -- scan radius for looking for nearby players local PLAYER_SCAN_RADIUS = 4 -- scan radius for looking for nearby players
local RESETTLE_DISTANCE = 100 -- If a mob is transported this far from home, it gives up bed and job and resettles
local PATHFINDING = "gowp" local PATHFINDING = "gowp"
--[=======[ TRADING ]=======] --[=======[ TRADING ]=======]
@ -885,11 +887,18 @@ local function has_golem(pos)
end end
end end
local function monsters_near(self)
for _,o in pairs(minetest.get_objects_inside_radius(self.object:get_pos(),10)) do
local l = o:get_luaentity()
if l and l.type =="monster" then return true end
end
end
local function has_summon_participants(self) local function has_summon_participants(self)
local r = 0 local r = 0
for _,o in pairs(minetest.get_objects_inside_radius(self.object:get_pos(),10)) do for _,o in pairs(minetest.get_objects_inside_radius(self.object:get_pos(),10)) do
local l = o:get_luaentity() local l = o:get_luaentity()
--TODO check for panicking or gossiping --TODO check for gossiping
if l and l.name == "mobs_mc:villager" then r = r + 1 end if l and l.name == "mobs_mc:villager" then r = r + 1 end
end end
return r > 2 return r > 2
@ -913,7 +922,8 @@ local function check_summon(self,dtime)
if self._summon_timer and self._summon_timer > 30 then if self._summon_timer and self._summon_timer > 30 then
local pos = self.object:get_pos() local pos = self.object:get_pos()
self._summon_timer = 0 self._summon_timer = 0
if has_golem(pos) then return false end if has_golem(pos) then return end
if not monsters_near(self) then return end
if not has_summon_participants(self) then return end if not has_summon_participants(self) then return end
summon_golem(self) summon_golem(self)
elseif self._summon_timer == nil then elseif self._summon_timer == nil then
@ -1089,25 +1099,38 @@ local function retrieve_my_jobsite (self)
return return
end end
local function remove_job (self)
self._jobsite = nil
if not has_traded(self) then
mcl_log("Cannot retrieve my jobsite. I am now unemployed.")
self._profession = "unemployed"
self._trades = nil
set_textures(self)
else
mcl_log("Cannot retrieve my jobsite but I've traded so only remove jobsite.")
end
end
local function validate_jobsite(self) local function validate_jobsite(self)
if self._profession == "unemployed" then return false end if self._profession == "unemployed" then return false end
if not retrieve_my_jobsite (self) then local job_block = retrieve_my_jobsite (self)
self._jobsite = nil if not job_block then
if self.order == WORK then if self.order == WORK then
self.order = nil self.order = nil
end end
if not has_traded(self) then remove_job (self)
mcl_log("Cannot retrieve my jobsite. I am now unemployed.")
self._profession = "unemployed"
self._trades = nil
set_textures(self)
else
mcl_log("Cannot retrieve my jobsite but I've traded so only remove jobsite.")
end
return false return false
else else
local resettle = vector.distance(self.object:get_pos(),self._jobsite) > RESETTLE_DISTANCE
mcl_log("Jobsite far, so resettle: " .. tostring(resettle))
if resettle then
local m = minetest.get_meta(self._jobsite)
m:set_string("villager", nil)
remove_job (self)
return false
end
return true return true
end end
end end
@ -1222,6 +1245,17 @@ local function validate_bed(self)
local bed_valid = true local bed_valid = true
local m = minetest.get_meta(self._bed) local m = minetest.get_meta(self._bed)
local resettle = vector.distance(self.object:get_pos(),self._bed) > RESETTLE_DISTANCE
mcl_log("Bed far, so resettle: " .. tostring(resettle))
if resettle then
mcl_log("Resettled. Ditch bed.")
m:set_string("villager", nil)
self._bed = nil
bed_valid = false
return false
end
local owned_by_player = m:get_string("player") local owned_by_player = m:get_string("player")
mcl_log("Player owner: " .. owned_by_player) mcl_log("Player owner: " .. owned_by_player)
if owned_by_player ~= "" then if owned_by_player ~= "" then
@ -1229,7 +1263,7 @@ local function validate_bed(self)
m:set_string("villager", nil) m:set_string("villager", nil)
self._bed = nil self._bed = nil
bed_valid = false bed_valid = false
return return false
end end
if m:get_string("villager") ~= self._id then if m:get_string("villager") ~= self._id then
@ -1245,6 +1279,10 @@ end
local function do_activity (self) local function do_activity (self)
-- Maybe just check we're pathfinding first? -- Maybe just check we're pathfinding first?
if self.following then
mcl_log("Following, so do not do activity.")
return
end
if not validate_bed(self) and self.state ~= PATHFINDING then if not validate_bed(self) and self.state ~= PATHFINDING then
if self.order == SLEEP then self.order = nil end if self.order == SLEEP then self.order = nil end