1
0
Fork 0

Merge pull request 'Villagers - Work, sleep and wander. Check and claim beds etc.' (#2816) from ancientmarinerdev/MineClone2:feature/villager_improvement_rb into master

Reviewed-on: MineClone2/MineClone2#2816
Reviewed-by: cora <cora@noreply.git.minetest.land>
This commit is contained in:
cora 2022-10-23 23:36:49 +00:00
commit ae0b5381e4
3 changed files with 531 additions and 68 deletions

View File

@ -16,6 +16,14 @@ local CRAMMING_DAMAGE = 3
-- Localize -- Localize
local S = minetest.get_translator("mcl_mobs") local S = minetest.get_translator("mcl_mobs")
local LOGGING_ON = minetest.settings:get_bool("mcl_logging_mobs_villager",false)
local LOG_MODULE = "[Mobs]"
local function mcl_log (message)
if LOGGING_ON and message then
minetest.log(LOG_MODULE .. " " .. message)
end
end
local function shortest_term_of_yaw_rotatoin(self, rot_origin, rot_target, nums) local function shortest_term_of_yaw_rotatoin(self, rot_origin, rot_target, nums)
if not rot_origin or not rot_target then if not rot_origin or not rot_target then
@ -1562,6 +1570,7 @@ end
-- find two animals of same type and breed if nearby and horny -- find two animals of same type and breed if nearby and horny
local breed = function(self) local breed = function(self)
--mcl_log("In breed function")
-- child takes a long time before growing into adult -- child takes a long time before growing into adult
if self.child == true then if self.child == true then
@ -1619,6 +1628,8 @@ local breed = function(self)
if self.horny == true if self.horny == true
and self.hornytimer <= HORNY_TIME then and self.hornytimer <= HORNY_TIME then
mcl_log("In breed function. All good. Do the magic.")
local pos = self.object:get_pos() local pos = self.object:get_pos()
effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, "heart.png", 3, 4, 1, 0.1) effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, "heart.png", 3, 4, 1, 0.1)
@ -1653,6 +1664,8 @@ local breed = function(self)
end end
end end
if canmate then mcl_log("In breed function. Can mate.") end
if ent if ent
and canmate == true and canmate == true
and ent.horny == true and ent.horny == true
@ -1667,6 +1680,8 @@ local breed = function(self)
ent.hornytimer = HORNY_TIME + 1 ent.hornytimer = HORNY_TIME + 1
-- spawn baby -- spawn baby
minetest.after(5, function(parent1, parent2, pos) minetest.after(5, function(parent1, parent2, pos)
if not parent1.object:get_luaentity() then if not parent1.object:get_luaentity() then
return return
@ -2480,18 +2495,42 @@ local function check_gowp(self,dtime)
if gowp_etime < 0.2 then return end if gowp_etime < 0.2 then return end
gowp_etime = 0 gowp_etime = 0
local p = self.object:get_pos() local p = self.object:get_pos()
if not p or not self._target then return end
if vector.distance(p,self._target) < 1 then -- no destination
if not p or not self._target then
mcl_log("p: ".. tostring(p))
mcl_log("self._target: ".. tostring(self._target))
return
end
-- arrived at location
local distance_to_targ = vector.distance(p,self._target)
mcl_log("Distance to targ: ".. tostring(distance_to_targ))
if distance_to_targ < 2 then
mcl_log("Arrived at _target")
self.waypoints = nil self.waypoints = nil
self._target = nil self._target = nil
self.current_target = nil self.current_target = nil
self.state = "stand" self.state = "stand"
self.order = "stand"
self.object:set_velocity({x = 0, y = 0, z = 0})
self.object:set_acceleration({x = 0, y = 0, z = 0})
if self.callback_arrived then return self.callback_arrived(self) end if self.callback_arrived then return self.callback_arrived(self) end
return true return true
end end
if self.waypoints and ( not self.current_target or vector.distance(p,self.current_target) < 2 ) then if self.waypoints and ( not self.current_target or vector.distance(p,self.current_target) < 2 ) then
if not self.current_target then
for i, j in pairs (self.waypoints) do
mcl_log("Way: ".. tostring(i))
mcl_log("Val: ".. tostring(j))
end
--mcl_log("nextwp:".. tostring(self.waypoints) )
end
self.current_target = table.remove(self.waypoints, 1) self.current_target = table.remove(self.waypoints, 1)
--minetest.log("nextwp:".. tostring(self.current_target) ) mcl_log("current target:".. tostring(self.current_target) )
--mcl_log("type:".. type(self.current_target) )
go_to_pos(self,self.current_target) go_to_pos(self,self.current_target)
return return
elseif self.current_target then elseif self.current_target then
@ -2505,7 +2544,7 @@ local function check_gowp(self,dtime)
return return
end end
if not self.current_target then if not self.current_target then
--minetest.log("no path") --mcl_log("no path")
self.state = "walk" self.state = "walk"
end end
end end
@ -2556,9 +2595,9 @@ local do_states = function(self, dtime)
end end
-- npc's ordered to stand stay standing -- npc's ordered to stand stay standing
if self.type ~= "npc" if self.type == "npc" or (self.order == "stand" or self.order == "sleep" or self.order == "work") then
or self.order ~= "stand" then
else
if self.walk_chance ~= 0 if self.walk_chance ~= 0
and self.facing_fence ~= true and self.facing_fence ~= true
and random(1, 100) <= self.walk_chance and random(1, 100) <= self.walk_chance
@ -3071,6 +3110,8 @@ local do_states = function(self, dtime)
end end
end end
end end
else
end end
end end
end end
@ -3085,23 +3126,46 @@ local plane_adjacents = {
local gopath_last = os.time() local gopath_last = os.time()
function mcl_mobs:gopath(self,target,callback_arrived) function mcl_mobs:gopath(self,target,callback_arrived)
if os.time() - gopath_last < 15 then return end if self.state == "gowp" then mcl_log("Already set as gowp, don't set another path until done.") return end
if os.time() - gopath_last < 15 then
mcl_log("Not ready to path yet")
return
end
gopath_last = os.time() gopath_last = os.time()
--minetest.log("gowp")
self.order = nil
mcl_log("gowp target: " .. minetest.pos_to_string(target))
local p = self.object:get_pos() local p = self.object:get_pos()
local t = vector.offset(target,0,1,0) local t = vector.offset(target,0,1,0)
local wp = minetest.find_path(p,t,150,1,4) local wp = minetest.find_path(p,t,150,1,4)
--Path to door first
if not wp then if not wp then
--mcl_log("gowp. no wp. Look for door")
local d = minetest.find_node_near(target,16,{"group:door"}) local d = minetest.find_node_near(target,16,{"group:door"})
if d then if d then
--mcl_log("Found a door near")
for _,v in pairs(plane_adjacents) do for _,v in pairs(plane_adjacents) do
local pos = vector.add(d,v) local pos = vector.add(d,v)
local n = minetest.get_node(pos) local n = minetest.get_node(pos)
if n.name == "air" then if n.name == "air" then
wp = minetest.find_path(p,pos,150,1,4) wp = minetest.find_path(p,pos,150,1,4)
if wp then break end if wp then
--mcl_log("Found a path to next to door".. minetest.pos_to_string(pos))
break
else
--mcl_log("This block next to door doesn't work.")
end
else
--mcl_log("Block is not air, it is: ".. n.name)
end end
end end
else
mcl_log("No door found")
end end
end end
if wp and #wp > 0 then if wp and #wp > 0 then

View File

@ -6,6 +6,7 @@ local cow_def = {
description = S("Cow"), description = S("Cow"),
type = "animal", type = "animal",
spawn_class = "passive", spawn_class = "passive",
passive = true,
hp_min = 10, hp_min = 10,
hp_max = 10, hp_max = 10,
xp_min = 1, xp_min = 1,

View File

@ -15,6 +15,8 @@
-- TODO: Internal inventory, trade with other villagers -- TODO: Internal inventory, trade with other villagers
-- TODO: Schedule stuff (work,sleep,father) -- TODO: Schedule stuff (work,sleep,father)
local weather_mod = minetest.get_modpath("mcl_weather")
local S = minetest.get_translator("mobs_mc") local S = minetest.get_translator("mobs_mc")
local N = function(s) return s end local N = function(s) return s end
local F = minetest.formspec_escape local F = minetest.formspec_escape
@ -40,6 +42,14 @@ local PLAYER_SCAN_RADIUS = 4 -- scan radius for looking for nearby players
-- these items should be implemented as single items, then everything -- these items should be implemented as single items, then everything
-- will be much easier. -- will be much easier.
local LOGGING_ON = minetest.settings:get_bool("mcl_logging_mobs_villager",false)
local LOG_MODULE = "[Mobs - Villager]"
local function mcl_log (message)
if LOGGING_ON and message then
minetest.log(LOG_MODULE .. " " .. message)
end
end
local COMPASS = "mcl_compass:compass" local COMPASS = "mcl_compass:compass"
if minetest.registered_aliases[COMPASS] then if minetest.registered_aliases[COMPASS] then
COMPASS = minetest.registered_aliases[COMPASS] COMPASS = minetest.registered_aliases[COMPASS]
@ -492,16 +502,35 @@ local professions = {
} }
} }
local WORK = "work"
local SLEEP = "sleep"
local profession_names = {} local profession_names = {}
for id, _ in pairs(professions) do for id, _ in pairs(professions) do
table.insert(profession_names, id) table.insert(profession_names, id)
end end
local jobsites={} local function populate_jobsites (profession)
for _,n in pairs(profession_names) do if profession then
table.insert(jobsites,professions[n].jobsite) mcl_log("populate_jobsites: ".. tostring(profession))
end
local jobsites_requested={}
for _,n in pairs(profession_names) do
if n and professions[n].jobsite then
if not profession or (profession and profession == n) then
--minetest.log("populate_jobsites. Adding: ".. tostring(n))
table.insert(jobsites_requested,professions[n].jobsite)
end
end
end
return jobsites_requested
end end
jobsites = populate_jobsites()
local spawnable_bed={}
table.insert(spawnable_bed, "mcl_beds:bed_red_bottom")
local function stand_still(self) local function stand_still(self)
self.walk_chance = 0 self.walk_chance = 0
self.jump = false self.jump = false
@ -521,6 +550,11 @@ end
local function get_badge_textures(self) local function get_badge_textures(self)
local t = professions[self._profession].texture local t = professions[self._profession].texture
if self._profession == "unemployed" then
t = professions[self._profession].textures -- ideally both scenarios should be textures with a list containing 1 or multiple
--mcl_log("t: " .. tostring(t))
end
if self._profession == "unemployed" or self._profession == "nitwit" then return t end if self._profession == "unemployed" or self._profession == "nitwit" then return t end
local tier = self._max_trade_tier or 1 local tier = self._max_trade_tier or 1
return { return {
@ -529,26 +563,136 @@ local function get_badge_textures(self)
end end
local function set_textures(self) local function set_textures(self)
self.object:set_properties({textures=get_badge_textures(self)}) local badge_textures = get_badge_textures(self)
mcl_log("Setting textures: " .. tostring(badge_textures))
self.object:set_properties({textures=badge_textures})
end end
local function go_home(entity) function get_activity(tod)
entity.state = "go_home" -- night hours = tod > 18541 or tod < 5458
local b=entity._bed if not tod then
if not b then return end tod = minetest.get_timeofday()
mcl_mobs:gopath(entity,b,function(entity,b) end
if vector.distance(entity.object:get_pos(),b) < 2 then tod = ( tod * 24000 ) % 24000
entity.state = "stand"
set_velocity(entity,0)
entity.object:set_pos(b) local lunch_start = 12000
local n=minetest.get_node(b) local lunch_end = 13500
if n and n.name ~= "mcl_beds:bed_red_bottom" then local work_start = 8500
entity._bed=nil --the stormtroopers have killed uncle owen local work_end = 16300
local activity = nil
if (tod > work_start and tod < lunch_start) or (tod > lunch_end and tod < work_end) then
activity = WORK
elseif mcl_beds.is_night() then
activity = SLEEP
elseif tod > lunch_start and tod < lunch_end then
activity = "lunch"
else
activity = "chill"
end
mcl_log("Time is " .. tod ..". Activity is: ".. activity)
return activity
end
local function go_home(entity, sleep)
local b = entity._bed
if not b then
return
end
local bed_node = minetest.get_node(b)
if not bed_node then
entity._bed = nil
mcl_log("Cannot find bed. Unset it")
return
end
if vector.distance(entity.object:get_pos(),b) < 2 then
if sleep then
entity.order = SLEEP
mcl_log("Sleep time!")
end
else
if sleep and entity.order == SLEEP then
entity.order = nil
return
end
mcl_mobs:gopath(entity,b,function(entity,b)
local b = entity._bed
if not b then
--minetest.log("NO BED, problem")
return false return false
end end
return true
if not minetest.get_node(b) then
--minetest.log("NO BED NODE, problem")
return false
end
if vector.distance(entity.object:get_pos(),b) < 2 then
--minetest.log("Managed to walk home callback!")
return true
else
--minetest.log("Need to walk to home")
end
end)
end
end
local function check_bed (entity)
local b = entity._bed
if not b then
--minetest.log("No bed set on villager")
return false
end
local n = minetest.get_node(b)
if n and n.name ~= "mcl_beds:bed_red_bottom" then
mcl_log("Where did my bed go?!")
entity._bed = nil --the stormtroopers have killed uncle owen
return false
else
return true
end
end
local function take_bed (entity)
if not entity then return end
local p = entity.object:get_pos()
local nn = minetest.find_nodes_in_area(vector.offset(p,-48,-48,-48), vector.offset(p,48,48,48), spawnable_bed)
for _,n in pairs(nn) do
local m=minetest.get_meta(n)
--mcl_log("Bed owner: ".. m:get_string("villager"))
if m:get_string("villager") == "" and not (entity.state == "gowp") then
mcl_log("Can we path to bed: "..minetest.pos_to_string(n) )
local gp = mcl_mobs:gopath(entity,n,function(self)
if self then
self.order = "sleep"
mcl_log("Sleepy time" )
else
mcl_log("Can't sleep, no self in the callback" )
end
end)
if gp then
mcl_log("Nice bed. I'll defintely take it as I can path")
m:set_string("villager", entity._id)
entity._bed = n
break
else
mcl_log("Awww. I can't find my bed.")
end
else
mcl_log("Currently gowp, or it's taken: ".. m:get_string("villager"))
end end
end) end
end end
local function has_golem(pos) local function has_golem(pos)
@ -596,6 +740,43 @@ local function check_summon(self,dtime)
self._summon_timer = self._summon_timer + dtime self._summon_timer = self._summon_timer + dtime
end end
local function has_traded (self)
--mcl_log("Checking name: " .. self._trades)
if not self._trades then
mcl_log("No trades set. has_traded is false")
return false
end
local cur_trades_tab = minetest.deserialize(self._trades)
if cur_trades_tab and type(cur_trades_tab) == "table" then
for trader, trades in pairs(cur_trades_tab) do
--mcl_log("Current record: ".. tostring(trader))
--for tr3, tr4 in pairs (tab_val) do
--mcl_log("Key: ".. tostring(tr3))
--mcl_log("Value: ".. tostring(tr4))
--end
--mcl_log("traded once: ".. tostring(trades.traded_once))
if trades.traded_once then
mcl_log("Villager has traded before. Returning true")
return true
end
end
end
mcl_log("Villager has not traded before")
return false
end
local function unlock_trades (self)
if self then
--mcl_log("We should now try to unlock trades")
else
mcl_log("Missing self")
end
end
----- JOBSITE LOGIC ----- JOBSITE LOGIC
local function get_profession_by_jobsite(js) local function get_profession_by_jobsite(js)
for k,v in pairs(professions) do for k,v in pairs(professions) do
@ -608,47 +789,209 @@ local function employ(self,jobsite_pos)
local m = minetest.get_meta(jobsite_pos) local m = minetest.get_meta(jobsite_pos)
local p = get_profession_by_jobsite(n.name) local p = get_profession_by_jobsite(n.name)
if p and m:get_string("villager") == "" then if p and m:get_string("villager") == "" then
self._profession=p mcl_log("Taking this jobsite")
m:set_string("villager",self._id) m:set_string("villager",self._id)
self._jobsite = jobsite_pos self._jobsite = jobsite_pos
set_textures(self)
if not has_traded(self) then
self._profession=p
set_textures(self)
end
return true
else
mcl_log("I can not steal someone's job!")
end
end
local function look_for_job(self, requested_jobsites)
--if self.last_jobhunt and os.time() - self.last_jobhunt < 15 then
-- mcl_log("Is time less than 40?" .. tostring(os.time() - self.last_jobhunt))
-- return
--end
--self.last_jobhunt = os.time() + math.random(0,30)
mcl_log("Looking for jobs")
local looking_for_type = jobsites
if requested_jobsites then
mcl_log("Looking for jobs of my type: " .. tostring(requested_jobsites))
looking_for_type = requested_jobsites
else
mcl_log("Looking for any job type")
end
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), looking_for_type)
for _,n in pairs(nn) do
local m = minetest.get_meta(n)
--mcl_log("Job owner: ".. m:get_string("villager"))
if m:get_string("villager") == "" then
mcl_log("It's a free job for me (".. minetest.pos_to_string(p) .. ")! I might be interested: "..minetest.pos_to_string(n) )
local gp = mcl_mobs:gopath(self,n,function(self)
mcl_log("Arrived at block callback")
if self and self.state == "stand" then
self.order = WORK
else
mcl_log("no self. passing param to callback failed")
end
end)
if gp then
if n then
mcl_log("We can path to this block.. " .. tostring(n))
end
return n
else
mcl_log("We could not path to block or it's not ready to path yet.")
end
end
end
return nil
end
local function get_a_job(self)
mcl_log("I'm unemployed or lost my job block and have traded. Can I get a job?")
--self.order = JOB_HUNTING
local requested_jobsites = jobsites
if has_traded (self) then
--mcl_log("Has traded")
requested_jobsites = populate_jobsites(self._profession)
-- Only pass in my jobsite to two functions here
else
mcl_log("Has not traded")
end
local p = self.object:get_pos()
local n = minetest.find_node_near(p,1,requested_jobsites)
--Ideally should check for closest available. It'll make pathing easier.
--local n = look_for_job(self)
if not n then
--mcl_log("Job hunt failed. Could not find block I have walked to")
end
if n and employ(self,n) then return true end
if self.state ~= "gowp" then
mcl_log("Nothing near. Need to look for a job")
look_for_job(self, requested_jobsites)
end
end
local function retrieve_my_jobsite (self)
if not self or not self._jobsite then
--mcl_log("find_jobsite. Invalid params")
return
end
local n = mcl_vars.get_node(self._jobsite)
local m = minetest.get_meta(self._jobsite)
if m:get_string("villager") == self._id then
--mcl_log("find_jobsite. is my job.")
return n
else
--mcl_log("find_jobsite. Not my job")
end
return
end
local function validate_jobsite(self)
if self._profession == "unemployed" then return false end
if not retrieve_my_jobsite (self) then
self._jobsite = nil
if self.order == WORK then
self.order = nil
end
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
return false
else
return true return true
end end
end end
local function look_for_job(self)
if self.last_jobhunt and os.time() - self.last_jobhunt < 360 then return end
self.last_jobhunt = os.time() + math.random(0,60)
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) )
local gp = mcl_mobs:gopath(self,n,function()
--minetest.log("arrived jobsite "..minetest.pos_to_string(n) )
end)
if gp then return end
end
end
end
local function get_a_job(self)
local function do_work (self)
if self.child then return end if self.child then return end
local p = self.object:get_pos() --mcl_log("Time for work")
local n = minetest.find_node_near(p,1,jobsites)
if n and employ(self,n) then return true end
if self.state ~= "gowp" then look_for_job(self) end
end
local function check_jobsite(self) -- Don't try if looking_for_work, or gowp possibly
if self._traded or not self._jobsite then return end if validate_jobsite(self) then
local n = mcl_vars.get_node(self._jobsite) mcl_log("My jobsite is valid. Do i need to travel?")
local m = minetest.get_meta(self._jobsite)
if m:get_string("villager") ~= self._id then local jobsite2 = retrieve_my_jobsite (self)
self._profession = "unemployed" local jobsite = self._jobsite
self._trades = nil
set_textures(self) if self and jobsite2 and self._jobsite then
--mcl_log("Villager: ".. minetest.pos_to_string(self.object:get_pos()) .. ", jobsite: " .. minetest.pos_to_string(self._jobsite))
if vector.distance(self.object:get_pos(),self._jobsite) < 2 then
mcl_log("Made it to work ok!")
if not (self.state == "gowp") then
--mcl_log("Setting order to work.")
self.order = WORK
else
mcl_log("Not gowp. What is it: " .. self.state)
end
-- Once we arrive at job block, we should unlock trades
unlock_trades(self)
--self.state = "stand"
--self.object:set_velocity({x = 0, y = 0, z = 0})
else
mcl_log("Not at job block. Need to commute.")
if self.order == WORK then
self.order = nil
return
end
--self.state = "go_to_work"
mcl_mobs:gopath(self, 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
end
elseif self._profession == "unemployed" then
get_a_job(self)
elseif has_traded(self) then
mcl_log("My job site is invalid or gone. I cannot work.")
if self.order == WORK then self.order = nil end
get_a_job(self)
end end
end end
@ -724,7 +1067,7 @@ local function set_trade(trader, player, inv, concrete_tradenum)
init_trades(trader) init_trades(trader)
trades = minetest.deserialize(trader._trades) trades = minetest.deserialize(trader._trades)
if not trades then if not trades then
minetest.log("error", "[mobs_mc] Failed to select villager trade!") --minetest.log("error", "Failed to select villager trade!")
return return
end end
end end
@ -1233,6 +1576,7 @@ mcl_mobs:register_mob("mobs_mc:villager", {
description = S("Villager"), description = S("Villager"),
type = "npc", type = "npc",
spawn_class = "passive", spawn_class = "passive",
passive = true,
hp_min = 20, hp_min = 20,
hp_max = 20, hp_max = 20,
head_swivel = "head.control", head_swivel = "head.control",
@ -1299,11 +1643,20 @@ mcl_mobs:register_mob("mobs_mc:villager", {
return it return it
end, end,
on_rightclick = function(self, clicker) on_rightclick = function(self, clicker)
if self._jobsite then if self.state == "attack" then
mcl_log("Somehow villager got into an invalid attack state. Removed.")
-- Need to stop villager getting in attack state. This is a workaround to allow players to fix broken villager.
self.state = "stand"
self.attack = nil
end
if validate_jobsite(self) then
mcl_mobs:gopath(self,self._jobsite,function() mcl_mobs:gopath(self,self._jobsite,function()
--minetest.log("arrived at jobsite") --minetest.log("arrived at jobsite")
end) end)
else
self.state = "stand" -- cancel gowp in case it has messed up
end end
if self.child or self._profession == "unemployed" or self._profession == "nitwit" then if self.child or self._profession == "unemployed" or self._profession == "nitwit" then
return return
end end
@ -1345,18 +1698,21 @@ mcl_mobs:register_mob("mobs_mc:villager", {
_trading_players = {}, -- list of playernames currently trading with villager (open formspec) _trading_players = {}, -- list of playernames currently trading with villager (open formspec)
do_custom = function(self, dtime) do_custom = function(self, dtime)
check_summon(self,dtime) check_summon(self,dtime)
-- Stand still if player is nearby. -- Stand still if player is nearby.
if not self._player_scan_timer then if not self._player_scan_timer then
self._player_scan_timer = 0 self._player_scan_timer = 0
end end
self._player_scan_timer = self._player_scan_timer + dtime self._player_scan_timer = self._player_scan_timer + dtime
-- Check infrequently to keep CPU load low -- Check infrequently to keep CPU load low
if self._player_scan_timer > PLAYER_SCAN_INTERVAL then if self._player_scan_timer > PLAYER_SCAN_INTERVAL then
self._player_scan_timer = 0 self._player_scan_timer = 0
local selfpos = self.object:get_pos() local selfpos = self.object:get_pos()
local objects = minetest.get_objects_inside_radius(selfpos, PLAYER_SCAN_RADIUS) local objects = minetest.get_objects_inside_radius(selfpos, PLAYER_SCAN_RADIUS)
local has_player = false local has_player = false
for o, obj in pairs(objects) do for o, obj in pairs(objects) do
if obj:is_player() then if obj:is_player() then
has_player = true has_player = true
@ -1367,18 +1723,47 @@ mcl_mobs:register_mob("mobs_mc:villager", {
minetest.log("verbose", "[mobs_mc] Player near villager found!") minetest.log("verbose", "[mobs_mc] Player near villager found!")
stand_still(self) stand_still(self)
else else
minetest.log("verbose", "[mobs_mc] No player near villager found!") --minetest.log("verbose", "[mobs_mc] No player near villager found!")
self.walk_chance = DEFAULT_WALK_CHANCE self.walk_chance = DEFAULT_WALK_CHANCE
self.jump = true self.jump = true
end end
if self._bed and ( self.state ~= "go_home" and vector.distance(self.object:get_pos(),self._bed) > 50 ) then
go_home(self) if not self._bed then
--mcl_log("Villager has no bed. Currently at location: "..minetest.pos_to_string(self.object:get_pos()))
take_bed (self)
end end
if self._profession == "unemployed" then
get_a_job(self) if check_bed (self) then
--self.state ~= "go_home"
local wandered_too_far = ( self.state ~= "gowp" ) and (vector.distance(self.object:get_pos(),self._bed) > 50 )
--if wandered_too_far then minetest.log("Wandered too far! Return home ") end
if wandered_too_far then
go_home(self, false)
return
elseif mcl_beds.is_night() or (weather_mod and mcl_weather.get_weather() == "thunder") then
mcl_log("It's night or thunderstorm. Better get to bed. Weather is: " .. mcl_weather.get_weather())
go_home(self, true)
return
end
else else
check_jobsite(self) --mcl_log("check bed failed ")
end end
-- Daytime is work and play time
if not mcl_beds.is_night() then
if self.order == SLEEP then self.order = nil end
if get_activity() == WORK then
do_work(self)
else
-- gossip at town bell or stroll around
self.order = nil
end
else
if self.order == WORK then self.order = nil end
end
end end
end, end,
@ -1408,6 +1793,19 @@ mcl_mobs:register_mob("mobs_mc:villager", {
end end
end end
end end
local bed = self._bed
if bed then
local bed_meta = minetest.get_meta(bed)
bed_meta:set_string("villager", nil)
mcl_log("Died, so bye bye bed")
end
local jobsite = self._jobsite
if jobsite then
local jobsite_meta = minetest.get_meta(jobsite)
jobsite_meta:set_string("villager", nil)
mcl_log("Died, so bye bye jobsite")
end
end, end,
}) })