Allow villagers to resettle and not run back to job and and old bed

This commit is contained in:
ancientmarinerdev 2022-11-15 22:32:09 +00:00 committed by cora
parent cdf28ec684
commit 1a670bc41b
1 changed files with 41 additions and 11 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 ]=======]
@ -1089,25 +1091,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 +1237,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 +1255,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 +1271,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