1
0
Fork 0

Villagers gather at lunch time, reset trade when they get to work.

turtlestriderbeefox
ancientmarinerdev 2022-10-27 22:27:19 +01:00 committed by cora
parent 6851a53c6c
commit 04cac7156e
1 changed files with 166 additions and 115 deletions

View File

@ -506,6 +506,7 @@ local professions = {
local WORK = "work" local WORK = "work"
local SLEEP = "sleep" local SLEEP = "sleep"
local GATHERING = "gathering"
local profession_names = {} local profession_names = {}
for id, _ in pairs(professions) do for id, _ in pairs(professions) do
@ -583,14 +584,16 @@ function get_activity(tod)
local work_start = 8500 local work_start = 8500
local work_end = 16500 local work_end = 16500
local activity = nil local activity = nil
if (tod > work_start and tod < lunch_start) or (tod > lunch_end and tod < work_end) then if weather_mod and mcl_weather.get_weather() == "thunder" then
mcl_log("Better get to bed. Weather is: " .. mcl_weather.get_weather())
activity = SLEEP
elseif (tod > work_start and tod < lunch_start) or (tod > lunch_end and tod < work_end) then
activity = WORK activity = WORK
elseif mcl_beds.is_night() then elseif mcl_beds.is_night() then
activity = SLEEP activity = SLEEP
elseif tod > lunch_start and tod < lunch_end then elseif tod > lunch_start and tod < lunch_end then
activity = "lunch" activity = GATHERING
else else
activity = "chill" activity = "chill"
end end
@ -599,8 +602,29 @@ function get_activity(tod)
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 go_home(entity, sleep) local function go_home(entity, sleep)
if not check_bed (entity) then
mcl_log("Cannot find bed, so cannot go home")
end
local b = entity._bed local b = entity._bed
if not b then if not b then
return return
@ -647,22 +671,7 @@ local function go_home(entity, sleep)
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) local function take_bed (entity)
if not entity then return end if not entity then return end
@ -742,25 +751,30 @@ 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) local function debug_trades(self)
--mcl_log("Checking name: " .. self._trades) mcl_log("Start debug trades")
if not self or not self._trades then return end
local trades = minetest.deserialize(self._trades)
if trades and type(trades) == "table" then
for trader, trade in pairs(trades) do
--mcl_log("Current record: ".. tostring(trader))
for tr3, tr4 in pairs (trade) do
mcl_log("Key: ".. tostring(tr3))
mcl_log("Value: ".. tostring(tr4))
end
end
end
mcl_log("End debug trades")
end
local function has_traded (self)
if not self._trades then if not self._trades then
mcl_log("No trades set. has_traded is false") mcl_log("No trades set. has_traded is false")
return false return false
end end
local cur_trades_tab = minetest.deserialize(self._trades) local cur_trades_tab = minetest.deserialize(self._trades)
if cur_trades_tab and type(cur_trades_tab) == "table" then if cur_trades_tab and type(cur_trades_tab) == "table" then
for trader, trades in pairs(cur_trades_tab) do 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 if trades.traded_once then
mcl_log("Villager has traded before. Returning true") mcl_log("Villager has traded before. Returning true")
return true return true
@ -772,10 +786,32 @@ local function has_traded (self)
end end
local function unlock_trades (self) local function unlock_trades (self)
if self then if not self._trades then
--mcl_log("We should now try to unlock trades") mcl_log("No trades set. has_traded is false")
else return false
mcl_log("Missing self") end
mcl_log("Unlocking trades")
local has_unlocked = false
local trades = minetest.deserialize(self._trades)
if trades and type(trades) == "table" then
for trader, trade in pairs(trades) do
local trade_tier_too_high = trade.tier > self._max_trade_tier
mcl_log("Max trade tier of villager: ".. tostring(self._max_trade_tier))
mcl_log("current trade.tier: ".. tostring(trade.tier))
mcl_log("trade tier too high: ".. tostring(trade_tier_too_high))
mcl_log("locked: ".. tostring(trade["locked"]))
if not trade_tier_too_high then
if trade["locked"] == true then
trade.locked = false
has_unlocked = true
mcl_log("Villager has a locked trade. Unlocking")
end
end
end
if has_unlocked then
self._trades = minetest.serialize(trades)
end
end end
end end
@ -808,13 +844,6 @@ end
local function look_for_job(self, requested_jobsites) 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") mcl_log("Looking for jobs")
local looking_for_type = jobsites local looking_for_type = jobsites
@ -828,6 +857,7 @@ local function look_for_job(self, requested_jobsites)
local p = self.object:get_pos() 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) local nn = minetest.find_nodes_in_area(vector.offset(p,-48,-48,-48),vector.offset(p,48,48,48), looking_for_type)
--Ideally should check for closest available. It'll make pathing easier.
for _,n in pairs(nn) do for _,n in pairs(nn) do
local m = minetest.get_meta(n) local m = minetest.get_meta(n)
--mcl_log("Job owner: ".. m:get_string("villager")) --mcl_log("Job owner: ".. m:get_string("villager"))
@ -859,31 +889,21 @@ local function look_for_job(self, requested_jobsites)
end end
local function get_a_job(self) local function get_a_job(self)
if self.order == WORK then self.order = nil end
mcl_log("I'm unemployed or lost my job block and have traded. Can I get a job?") 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 local requested_jobsites = jobsites
if has_traded (self) then if has_traded (self) then
mcl_log("Has traded so look for job of my type") mcl_log("Has traded so look for job of my type")
requested_jobsites = populate_jobsites(self._profession) requested_jobsites = populate_jobsites(self._profession)
-- Only pass in my jobsite to two functions here -- Only pass in my jobsite to two functions here
else
mcl_log("Has not traded")
end end
local p = self.object:get_pos() local p = self.object:get_pos()
local n = minetest.find_node_near(p,1,requested_jobsites) 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 n and employ(self,n) then return true end
if self.state ~= PATHFINDING then if self.state ~= PATHFINDING then
@ -894,17 +914,16 @@ end
local function retrieve_my_jobsite (self) local function retrieve_my_jobsite (self)
if not self or not self._jobsite then if not self or not self._jobsite then
--mcl_log("find_jobsite. Invalid params") mcl_log("find_jobsite. Invalid params. Should not happen")
return return
end end
local n = mcl_vars.get_node(self._jobsite) local n = mcl_vars.get_node(self._jobsite)
local m = minetest.get_meta(self._jobsite) local m = minetest.get_meta(self._jobsite)
if m:get_string("villager") == self._id then if m:get_string("villager") == self._id then
--mcl_log("find_jobsite. is my job.") --mcl_log("find_jobsite. is my job.")
return n return n
else else
--mcl_log("find_jobsite. Not my job") mcl_log("This isn't my jobsite")
end end
return return
end end
@ -932,10 +951,12 @@ local function validate_jobsite(self)
end end
end end
local function do_work (self) local function do_work (self)
if self.child then return end debug_trades(self)
if self.child or self.order == WORK then
mcl_log("A child or already working so don't send to work")
return
end
--mcl_log("Time for work") --mcl_log("Time for work")
-- Don't try if looking_for_work, or gowp possibly -- Don't try if looking_for_work, or gowp possibly
@ -946,29 +967,23 @@ local function do_work (self)
local jobsite = self._jobsite local jobsite = self._jobsite
if self and jobsite2 and self._jobsite then if self and jobsite2 and self._jobsite then
local distance_to_jobsite = vector.distance(self.object:get_pos(),self._jobsite)
--mcl_log("Villager: ".. minetest.pos_to_string(self.object:get_pos()) .. ", jobsite: " .. minetest.pos_to_string(self._jobsite) .. ", distance to jobsite: ".. distance_to_jobsite)
--mcl_log("Villager: ".. minetest.pos_to_string(self.object:get_pos()) .. ", jobsite: " .. minetest.pos_to_string(self._jobsite)) if distance_to_jobsite < 2 then
if vector.distance(self.object:get_pos(),self._jobsite) < 2 then
--mcl_log("Made it to work ok!")
if not (self.state == PATHFINDING) then if not (self.state == PATHFINDING) then
--mcl_log("Setting order to work.") mcl_log("Setting order to work.")
self.order = 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) unlock_trades(self)
else
--self.state = "stand" --mcl_log("Still pathfinding.")
--self.object:set_velocity({x = 0, y = 0, z = 0}) end
else else
mcl_log("Not at job block. Need to commute.") mcl_log("Not at job block. Need to commute.")
if self.order == WORK then if self.order == WORK then
self.order = nil self.order = nil
return return
end end
--self.state = "go_to_work"
mcl_mobs:gopath(self, jobsite, function(self,jobsite) mcl_mobs:gopath(self, jobsite, function(self,jobsite)
if not self then if not self then
--mcl_log("missing self. not good") --mcl_log("missing self. not good")
@ -984,19 +999,90 @@ local function do_work (self)
else else
--mcl_log("Need to walk to work. Not sure we can get here.") --mcl_log("Need to walk to work. Not sure we can get here.")
end end
end) end)
end end
end end
elseif self._profession == "unemployed" then elseif self._profession == "unemployed" or has_traded(self) 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) get_a_job(self)
end end
end end
local function go_to_town_bell(self)
if self.order == GATHERING then
mcl_log("Already gathering")
return
else
mcl_log("Current order" .. self.order)
end
mcl_log("Go to town bell")
local looking_for_type={}
table.insert(looking_for_type, "mcl_bells:bell")
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)
--Ideally should check for closest available. It'll make pathing easier.
for _,n in pairs(nn) do
mcl_log("Found bell")
local gp = mcl_mobs:gopath(self,n,function(self)
if self then
self.order = GATHERING
mcl_log("Callback has a self")
end
mcl_log("Arrived at block callback")
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
return nil
end
local function do_activity (self)
-- Maybe just check we're pathfinding first?
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
-- Only check in day or during thunderstorm but wandered_too_far code won't work
local wandered_too_far = ( self.state ~= PATHFINDING ) and (vector.distance(self.object:get_pos(),self._bed) > 50 )
if wandered_too_far then
--mcl_log("Wandered too far! Return home ")
go_home(self, false)
elseif get_activity() == SLEEP then
go_home(self, true)
elseif get_activity() == WORK then
do_work(self)
elseif get_activity() == GATHERING then
go_to_town_bell(self)
else
-- gossip at town bell or stroll around
mcl_log("No order, so remove it.")
self.order = nil
end
-- Daytime is work and play time
if not mcl_beds.is_night() then
if self.order == SLEEP then self.order = nil end
else
if self.order == WORK then self.order = nil end
end
end
local function update_max_tradenum(self) local function update_max_tradenum(self)
if not self._trades then if not self._trades then
return return
@ -1663,9 +1749,9 @@ mcl_mobs:register_mob("mobs_mc:villager", {
end end
-- Don't do at night. Go to bed? Maybe do_activity needs it's own method -- Don't do at night. Go to bed? Maybe do_activity needs it's own method
if validate_jobsite(self) then 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 else
self.state = "stand" -- cancel gowp in case it has messed up self.state = "stand" -- cancel gowp in case it has messed up
self.order = nil -- cancel work if working self.order = nil -- cancel work if working
@ -1739,42 +1825,7 @@ mcl_mobs:register_mob("mobs_mc:villager", {
self.jump = true self.jump = true
end end
if not self._bed then do_activity (self)
--mcl_log("Villager has no bed. Currently at location: "..minetest.pos_to_string(self.object:get_pos()))
take_bed (self)
end
-- Only check in day or during thunderstorm but wandered_too_far code won't work
if check_bed (self) then
--self.state ~= "go_home"
local wandered_too_far = ( self.state ~= PATHFINDING ) 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
--mcl_log("check bed failed ")
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,