Fix race condition where many villagers are fighting for same job

This commit is contained in:
ancientmarinerdev 2022-11-08 23:42:44 +00:00 committed by cora
parent 3000a378f3
commit 59808c5190
1 changed files with 14 additions and 1 deletions

View File

@ -843,14 +843,24 @@ local function take_bed (entity)
mcl_log("Can we path to bed: "..minetest.pos_to_string(closest_block) ) mcl_log("Can we path to bed: "..minetest.pos_to_string(closest_block) )
local distance_to_block = vector.distance(p, closest_block) local distance_to_block = vector.distance(p, closest_block)
mcl_log("Distance: " .. distance_to_block) mcl_log("Distance: " .. distance_to_block)
if distance_to_block < 2 then if distance_to_block < 2 then
local m = minetest.get_meta(closest_block)
local owner = m:get_string("villager")
mcl_log("owner: ".. owner)
if owner and owner ~= "" and owner ~= entity._id then
mcl_log("Already taken")
if entity.order == "stand" then entity.order = nil end
return
end
if entity.order ~= SLEEP then if entity.order ~= SLEEP then
mcl_log("Sleepy time" ) mcl_log("Sleepy time" )
entity.order = SLEEP entity.order = SLEEP
local m = minetest.get_meta(closest_block)
m:set_string("villager", entity._id) m:set_string("villager", entity._id)
entity._bed = closest_block entity._bed = closest_block
else else
--entity.order = nil
mcl_log("Set as sleep already..." ) mcl_log("Set as sleep already..." )
end end
else else
@ -861,6 +871,9 @@ local function take_bed (entity)
mcl_log("Awww. I can't find my bed.") mcl_log("Awww. I can't find my bed.")
end end
end end
else
mcl_log("Cannot find a bed to claim.")
if entity.order == "stand" then entity.order = nil end
end end
end end