forked from VoxeLibre/VoxeLibre
Merge pull request 'Villagers gather at lunch time, reset trade when they get to work.' (#2873) from feature/villagers_pt3 into master
Reviewed-on: MineClone2/MineClone2#2873 Reviewed-by: cora <cora@noreply.git.minetest.land>
This commit is contained in:
commit
ab00297077
|
@ -414,11 +414,14 @@ end
|
|||
|
||||
|
||||
local set_yaw = function(self, yaw, delay, dtime)
|
||||
|
||||
|
||||
if self.noyaw then return end
|
||||
|
||||
if self.state ~= PATHFINDING then
|
||||
self._turn_to = yaw
|
||||
end
|
||||
|
||||
--mcl_log("Yaw is: \t\t" .. tostring(math.deg(yaw)))
|
||||
--mcl_log("self.object:get_yaw() is: \t" .. tostring(math.deg(self.object:get_yaw())))
|
||||
|
||||
--clamp our yaw to a 360 range
|
||||
if math.deg(self.object:get_yaw()) > 360 then
|
||||
|
@ -2545,22 +2548,50 @@ local function go_to_pos(entity,b)
|
|||
mcl_mobs:set_animation(entity, "walk")
|
||||
end
|
||||
|
||||
local function check_doors(self)
|
||||
local function interact_with_door(self, action, target)
|
||||
local p = self.object:get_pos()
|
||||
local t = minetest.get_timeofday()
|
||||
local dd = minetest.find_nodes_in_area(vector.offset(p,-1,-1,-1),vector.offset(p,1,1,1),{"group:door"})
|
||||
for _,d in pairs(dd) do
|
||||
local n = minetest.get_node(d)
|
||||
if n.name:find("_b_") then
|
||||
--local t = minetest.get_timeofday()
|
||||
--local dd = minetest.find_nodes_in_area(vector.offset(p,-1,-1,-1),vector.offset(p,1,1,1),{"group:door"})
|
||||
--for _,d in pairs(dd) do
|
||||
if target then
|
||||
mcl_log("Door target is: ".. minetest.pos_to_string(target))
|
||||
|
||||
local n = minetest.get_node(target)
|
||||
if n.name:find("_b_") or n.name:find("_t_") then
|
||||
mcl_log("Door")
|
||||
local def = minetest.registered_nodes[n.name]
|
||||
local closed = n.name:find("_b_1")
|
||||
if self.state == PATHFINDING then
|
||||
if closed and def.on_rightclick then def.on_rightclick(d,n,self) end
|
||||
--if not closed and def.on_rightclick then def.on_rightclick(d,n,self) end
|
||||
else
|
||||
|
||||
local closed = n.name:find("_b_1") or n.name:find("_t_1")
|
||||
--if self.state == PATHFINDING then
|
||||
if closed and action == "open" and def.on_rightclick then
|
||||
mcl_log("Open door")
|
||||
def.on_rightclick(target,n,self)
|
||||
end
|
||||
if not closed and action == "close" and def.on_rightclick then
|
||||
mcl_log("Close door")
|
||||
def.on_rightclick(target,n,self)
|
||||
end
|
||||
--else
|
||||
else
|
||||
mcl_log("Not door")
|
||||
end
|
||||
else
|
||||
mcl_log("no target. cannot try and open or close door")
|
||||
end
|
||||
--end
|
||||
end
|
||||
|
||||
local function do_pathfind_action (self, action)
|
||||
if action then
|
||||
mcl_log("Action present")
|
||||
local type = action["type"]
|
||||
local action_val = action["action"]
|
||||
local target = action["target"]
|
||||
if target then
|
||||
mcl_log("Target: ".. minetest.pos_to_string(target))
|
||||
end
|
||||
if type and type == "door" then
|
||||
mcl_log("Type is door")
|
||||
interact_with_door(self, action_val, target)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -2569,6 +2600,10 @@ local gowp_etime = 0
|
|||
|
||||
local function check_gowp(self,dtime)
|
||||
gowp_etime = gowp_etime + dtime
|
||||
|
||||
-- 0.1 is optimal.
|
||||
--less frequently = villager will get sent back after passing a point.
|
||||
--more frequently = villager will fail points they shouldn't they just didn't get there yet
|
||||
if gowp_etime < 0.1 then return end
|
||||
gowp_etime = 0
|
||||
local p = self.object:get_pos()
|
||||
|
@ -2582,7 +2617,7 @@ local function check_gowp(self,dtime)
|
|||
|
||||
-- arrived at location, finish gowp
|
||||
local distance_to_targ = vector.distance(p,self._target)
|
||||
mcl_log("Distance to targ: ".. tostring(distance_to_targ))
|
||||
--mcl_log("Distance to targ: ".. tostring(distance_to_targ))
|
||||
if distance_to_targ < 2 then
|
||||
mcl_log("Arrived at _target")
|
||||
self.waypoints = nil
|
||||
|
@ -2597,26 +2632,41 @@ local function check_gowp(self,dtime)
|
|||
end
|
||||
|
||||
-- More pathing to be done
|
||||
if self.waypoints and #self.waypoints > 0 and ( not self.current_target or vector.distance(p,self.current_target) < 2 ) then
|
||||
-- We have waypoints, and no current target, or we're at it. We need a new current_target.
|
||||
local distance_to_current_target = 50
|
||||
if self.current_target and self.current_target["pos"] then
|
||||
distance_to_current_target = vector.distance(p,self.current_target["pos"])
|
||||
end
|
||||
|
||||
if not self.current_target then
|
||||
for i, j in pairs (self.waypoints) do
|
||||
mcl_log("Val: ".. tostring(j))
|
||||
end
|
||||
end
|
||||
-- 0.6 is working but too sensitive. sends villager back too frequently. 0.7 is quite good, but not with heights
|
||||
-- 0.8 is optimal for 0.025 frequency checks and also 1... Actually. 0.8 is winning
|
||||
-- 0.9 and 1.0 is also good. Stick with unless door open or closing issues
|
||||
if self.waypoints and #self.waypoints > 0 and ( not self.current_target or not self.current_target["pos"] or distance_to_current_target < 0.9 ) then
|
||||
-- We have waypoints, and no current target, or we're at it. We need a new current_target.
|
||||
do_pathfind_action (self, self.current_target["action"])
|
||||
|
||||
local failed_attempts = self.current_target["failed_attempts"]
|
||||
mcl_log("There after " .. failed_attempts .. " failed attempts. current target:".. minetest.pos_to_string(self.current_target["pos"]) .. ". Distance: " .. distance_to_current_target)
|
||||
|
||||
self.current_target = table.remove(self.waypoints, 1)
|
||||
mcl_log("current target:".. minetest.pos_to_string(self.current_target) )
|
||||
--mcl_log("type:".. type(self.current_target) )
|
||||
go_to_pos(self,self.current_target)
|
||||
go_to_pos(self, self.current_target["pos"])
|
||||
return
|
||||
elseif self.current_target then
|
||||
elseif self.current_target and self.current_target["pos"] then
|
||||
-- No waypoints left, but have current target. Potentially last waypoint to go to.
|
||||
self.current_target["failed_attempts"] = self.current_target["failed_attempts"] + 1
|
||||
local failed_attempts = self.current_target["failed_attempts"]
|
||||
if failed_attempts >= 50 then
|
||||
mcl_log("Failed to reach position (" .. minetest.pos_to_string(self.current_target["pos"]) .. ") too many times. Abandon route. Times tried: " .. failed_attempts)
|
||||
self.state = "stand"
|
||||
self.current_target = nil
|
||||
self.waypoints = nil
|
||||
self._target = nil
|
||||
self.object:set_velocity({x = 0, y = 0, z = 0})
|
||||
self.object:set_acceleration({x = 0, y = 0, z = 0})
|
||||
return
|
||||
end
|
||||
|
||||
mcl_log("self.current_target: ".. minetest.pos_to_string(self.current_target))
|
||||
mcl_log("pos: ".. minetest.pos_to_string(p))
|
||||
go_to_pos(self,self.current_target)
|
||||
--mcl_log("Not at pos with failed attempts ".. failed_attempts ..": ".. minetest.pos_to_string(p) .. "self.current_target: ".. minetest.pos_to_string(self.current_target["pos"]) .. ". Distance: ".. distance_to_current_target)
|
||||
go_to_pos(self, self.current_target["pos"])
|
||||
-- Do i just delete current_target, and return so we can find final path.
|
||||
else
|
||||
-- Not at target, no current waypoints or current_target. Through the door and should be able to path to target.
|
||||
|
@ -2626,55 +2676,40 @@ local function check_gowp(self,dtime)
|
|||
local final_wp = minetest.find_path(p,self._target,150,1,4)
|
||||
if final_wp then
|
||||
mcl_log("We might be able to get to target here.")
|
||||
self.waypoints = final_wp
|
||||
-- self.waypoints = final_wp
|
||||
--go_to_pos(self,self._target)
|
||||
else
|
||||
-- Abandon route?
|
||||
mcl_log("Cannot plot final route to target")
|
||||
end
|
||||
end
|
||||
|
||||
--if self.current_target and not minetest.line_of_sight(self.object:get_pos(),self.current_target) then
|
||||
if self.current_target and (self.waypoints and #self.waypoints == 0) then
|
||||
-- I don't think we need the following anymore, but test first.
|
||||
-- Maybe just need something to path to target if no waypoints left
|
||||
if self.current_target and self.current_target["pos"] and (self.waypoints and #self.waypoints == 0) then
|
||||
local updated_p = self.object:get_pos()
|
||||
local distance_to_cur_targ = vector.distance(updated_p,self.current_target)
|
||||
local distance_to_cur_targ = vector.distance(updated_p,self.current_target["pos"])
|
||||
|
||||
mcl_log("Distance to current target: ".. tostring(distance_to_cur_targ))
|
||||
mcl_log("Current p: ".. minetest.pos_to_string(updated_p))
|
||||
--if not minetest.line_of_sight(self.object:get_pos(),self._target) then
|
||||
|
||||
-- 1.6 is good. is 1.9 better? It could fail less, but will it path to door when it isn't after door
|
||||
if distance_to_cur_targ > 1.9 then
|
||||
mcl_log("no LOS to target: ".. minetest.pos_to_string(self.current_target))
|
||||
mcl_log("not close to current target: ".. minetest.pos_to_string(self.current_target["pos"]))
|
||||
go_to_pos(self,self._current_target)
|
||||
else
|
||||
mcl_log("Let's go to target: ".. minetest.pos_to_string(self.current_target))
|
||||
mcl_log("close to current target: ".. minetest.pos_to_string(self.current_target["pos"]))
|
||||
self.current_target = nil
|
||||
--go_to_pos(self,self._target)
|
||||
self.waypoints=minetest.find_path(updated_p,self._target,150,1,4)
|
||||
--if not self.waypoints then
|
||||
--mcl_log("Give up ")
|
||||
--self.state = "walk"
|
||||
--end --give up
|
||||
end
|
||||
|
||||
--self.waypoints=minetest.find_path(p,self._target,150,1,4)
|
||||
--if not self.waypoints then
|
||||
--mcl_log("Give up ")
|
||||
--self.state = "walk"
|
||||
--end --give up
|
||||
--self.current_target = nil
|
||||
return
|
||||
end
|
||||
--if not self.current_target then
|
||||
--mcl_log("no path. Give up")
|
||||
--self.state = "walk"
|
||||
--end
|
||||
end
|
||||
|
||||
-- execute current state (stand, walk, run, attacks)
|
||||
-- returns true if mob has died
|
||||
local do_states = function(self, dtime)
|
||||
if self.can_open_doors then check_doors(self) end
|
||||
--if self.can_open_doors then check_doors(self) end
|
||||
|
||||
local yaw = self.object:get_yaw() or 0
|
||||
|
||||
|
@ -3258,6 +3293,95 @@ local do_states = function(self, dtime)
|
|||
end
|
||||
end
|
||||
|
||||
function output_table (wp)
|
||||
if not wp then return end
|
||||
mcl_log("wp items: ".. tostring(#wp))
|
||||
for a,b in pairs(wp) do
|
||||
mcl_log(a.. ": ".. tostring(b))
|
||||
end
|
||||
end
|
||||
|
||||
function append_paths (wp1, wp2)
|
||||
mcl_log("Start append")
|
||||
if not wp1 or not wp2 then
|
||||
mcl_log("Cannot append wp's")
|
||||
return
|
||||
end
|
||||
output_table(wp1)
|
||||
output_table(wp2)
|
||||
for _,a in pairs (wp2) do
|
||||
table.insert(wp1, a)
|
||||
end
|
||||
mcl_log("End append")
|
||||
end
|
||||
|
||||
local function output_enriched (wp_out)
|
||||
mcl_log("Output enriched path")
|
||||
local i = 0
|
||||
for _,outy in pairs (wp_out) do
|
||||
i = i + 1
|
||||
mcl_log("Pos ".. i ..":" .. minetest.pos_to_string(outy["pos"]))
|
||||
|
||||
local action = outy["action"]
|
||||
if action then
|
||||
mcl_log("type: " .. action["type"])
|
||||
mcl_log("action: " .. action["action"])
|
||||
mcl_log("target: " .. minetest.pos_to_string(action["target"]))
|
||||
end
|
||||
mcl_log("failed attempts: " .. outy["failed_attempts"])
|
||||
end
|
||||
end
|
||||
|
||||
-- This function will take a list of paths, and enrich it with:
|
||||
-- a var for failed attempts
|
||||
-- an action, such as to open or close a door where we know that pos requires that action
|
||||
local function generate_enriched_path(wp_in, door_open_pos, door_close_pos, cur_door_pos)
|
||||
local wp_out = {}
|
||||
for i, cur_pos in pairs(wp_in) do
|
||||
local action = nil
|
||||
|
||||
local one_down = vector.new(0,-1,0)
|
||||
local cur_pos_to_add = vector.add(cur_pos, one_down)
|
||||
if door_open_pos and vector.equals (cur_pos, door_open_pos) then
|
||||
mcl_log ("Door open match")
|
||||
--action = {type = "door", action = "open"}
|
||||
action = {}
|
||||
action["type"] = "door"
|
||||
action["action"] = "open"
|
||||
action["target"] = cur_door_pos
|
||||
cur_pos_to_add = vector.add(cur_pos, one_down)
|
||||
elseif door_close_pos and vector.equals(cur_pos, door_close_pos) then
|
||||
mcl_log ("Door close match")
|
||||
--action = {type = "door", action = "closed"}
|
||||
action = {}
|
||||
action["type"] = "door"
|
||||
action["action"] = "close"
|
||||
action["target"] = cur_door_pos
|
||||
cur_pos_to_add = vector.add(cur_pos, one_down)
|
||||
elseif cur_door_pos and vector.equals(cur_pos, cur_door_pos) then
|
||||
mcl_log("Current door pos")
|
||||
cur_pos_to_add = vector.add(cur_pos, one_down)
|
||||
action = {}
|
||||
action["type"] = "door"
|
||||
action["action"] = "open"
|
||||
action["target"] = cur_door_pos
|
||||
else
|
||||
cur_pos_to_add = cur_pos
|
||||
--mcl_log ("Pos doesn't match")
|
||||
end
|
||||
|
||||
wp_out[i] = {}
|
||||
wp_out[i]["pos"] = cur_pos_to_add
|
||||
wp_out[i]["failed_attempts"] = 0
|
||||
wp_out[i]["action"] = action
|
||||
|
||||
--wp_out[i] = {"pos" = cur_pos, "failed_attempts" = 0, "action" = action}
|
||||
--output_pos(cur_pos, i)
|
||||
end
|
||||
output_enriched(wp_out)
|
||||
return wp_out
|
||||
end
|
||||
|
||||
local plane_adjacents = {
|
||||
vector.new(1,0,0),
|
||||
vector.new(-1,0,0),
|
||||
|
@ -3265,12 +3389,72 @@ local plane_adjacents = {
|
|||
vector.new(0,0,-1),
|
||||
}
|
||||
|
||||
-- This function is used to see if we can path. We could use to check a route, rather than making people move.
|
||||
local function calculate_path_through_door (p, t, target)
|
||||
-- target is the same as t, just 1 square difference. Maybe we don't need target
|
||||
mcl_log("Plot route from mob: " .. minetest.pos_to_string(p) .. ", to target: " .. minetest.pos_to_string(t))
|
||||
|
||||
local enriched_path = nil
|
||||
|
||||
local cur_door_pos = nil
|
||||
local pos_closest_to_door = nil
|
||||
local other_side_of_door = nil
|
||||
|
||||
--Path to door first
|
||||
local wp = minetest.find_path(p,t,150,1,4)
|
||||
if not wp then
|
||||
mcl_log("No direct path. Path through door")
|
||||
|
||||
local cur_door_pos = minetest.find_node_near(target,16,{"group:door"})
|
||||
if cur_door_pos then
|
||||
mcl_log("Found a door near: " .. minetest.pos_to_string(cur_door_pos))
|
||||
for _,v in pairs(plane_adjacents) do
|
||||
pos_closest_to_door = vector.add(cur_door_pos,v)
|
||||
|
||||
local n = minetest.get_node(pos_closest_to_door)
|
||||
if n.name == "air" then
|
||||
wp = minetest.find_path(p,pos_closest_to_door,150,1,4)
|
||||
if wp then
|
||||
mcl_log("Found a path to next to door".. minetest.pos_to_string(pos_closest_to_door))
|
||||
other_side_of_door = vector.add(cur_door_pos,-v)
|
||||
mcl_log("Opposite is: ".. minetest.pos_to_string(other_side_of_door))
|
||||
|
||||
local wp_otherside_door_to_target = minetest.find_path(other_side_of_door,t,150,1,4)
|
||||
if wp_otherside_door_to_target and #wp_otherside_door_to_target > 0 then
|
||||
table.insert(wp, cur_door_pos)
|
||||
append_paths (wp, wp_otherside_door_to_target)
|
||||
enriched_path = generate_enriched_path(wp, pos_closest_to_door, other_side_of_door, cur_door_pos)
|
||||
mcl_log("We have a path from outside door to target")
|
||||
else
|
||||
mcl_log("We cannot path from outside door to target")
|
||||
end
|
||||
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
|
||||
else
|
||||
mcl_log("No door found")
|
||||
end
|
||||
else
|
||||
mcl_log("We have a direct route")
|
||||
end
|
||||
|
||||
if wp and not enriched_path then
|
||||
enriched_path = generate_enriched_path(wp)
|
||||
end
|
||||
return enriched_path
|
||||
end
|
||||
|
||||
local gopath_last = os.time()
|
||||
function mcl_mobs:gopath(self,target,callback_arrived)
|
||||
if self.state == PATHFINDING then mcl_log("Already set as gowp, don't set another path until done.") return end
|
||||
if self.state == PATHFINDING then mcl_log("Already pathfinding, don't set another until done.") return end
|
||||
|
||||
if os.time() - gopath_last < 15 then
|
||||
if os.time() - gopath_last < 5 then
|
||||
mcl_log("Not ready to path yet")
|
||||
return
|
||||
end
|
||||
|
@ -3278,46 +3462,32 @@ function mcl_mobs:gopath(self,target,callback_arrived)
|
|||
|
||||
self.order = nil
|
||||
|
||||
mcl_log("gowp target: " .. minetest.pos_to_string(target))
|
||||
--mcl_log("gowp target: " .. minetest.pos_to_string(target))
|
||||
local p = self.object:get_pos()
|
||||
local t = vector.offset(target,0,1,0)
|
||||
local wp = minetest.find_path(p,t,150,1,4)
|
||||
|
||||
--Path to door first
|
||||
local wp = calculate_path_through_door(p, t, target)
|
||||
if not wp then
|
||||
--mcl_log("gowp. no wp. Look for door")
|
||||
local d = minetest.find_node_near(target,16,{"group:door"})
|
||||
if d then
|
||||
--mcl_log("Found a door near")
|
||||
for _,v in pairs(plane_adjacents) do
|
||||
local pos = vector.add(d,v)
|
||||
mcl_log("Could not calculate path")
|
||||
end
|
||||
--output_table(wp)
|
||||
|
||||
local n = minetest.get_node(pos)
|
||||
if n.name == "air" then
|
||||
wp = minetest.find_path(p,pos,150,1,4)
|
||||
if wp then
|
||||
mcl_log("Found a path to next to door".. minetest.pos_to_string(pos))
|
||||
local other_side_of_door = vector.add(d,-v)
|
||||
mcl_log("Opposite is: ".. minetest.pos_to_string(other_side_of_door))
|
||||
table.insert(wp, other_side_of_door)
|
||||
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
|
||||
else
|
||||
mcl_log("No door found")
|
||||
end
|
||||
end
|
||||
if wp and #wp > 0 then
|
||||
self._target = t
|
||||
self.callback_arrived = callback_arrived
|
||||
table.remove(wp,1)
|
||||
local current_location = table.remove(wp,1)
|
||||
if current_location and current_location["pos"] then
|
||||
mcl_log("Removing first co-ord? " .. tostring(current_location["pos"]))
|
||||
else
|
||||
mcl_log("Nil pos")
|
||||
end
|
||||
--current_location = table.remove(wp,1)
|
||||
--if current_location and current_location["pos"] then
|
||||
-- mcl_log("Removing first co-ord? " .. tostring(current_location["pos"]))
|
||||
--else
|
||||
-- mcl_log("Nil pos")
|
||||
--end
|
||||
self.current_target = current_location
|
||||
self.waypoints = wp
|
||||
self.state = PATHFINDING
|
||||
return true
|
||||
|
|
|
@ -107,7 +107,7 @@ local professions = {
|
|||
},
|
||||
|
||||
{
|
||||
{ { "mcl_farming:pumpkin", 8, 13 }, E1 },
|
||||
{ { "mcl_farming:pumpkin", 6, 7 }, E1 },
|
||||
{ E1, { "mcl_farming:pumpkin_pie", 2, 3} },
|
||||
{ E1, { "mcl_core:apple", 2, 3} },
|
||||
},
|
||||
|
@ -506,6 +506,7 @@ local professions = {
|
|||
|
||||
local WORK = "work"
|
||||
local SLEEP = "sleep"
|
||||
local GATHERING = "gathering"
|
||||
|
||||
local profession_names = {}
|
||||
for id, _ in pairs(professions) do
|
||||
|
@ -583,14 +584,16 @@ function get_activity(tod)
|
|||
local work_start = 8500
|
||||
local work_end = 16500
|
||||
|
||||
|
||||
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
|
||||
elseif mcl_beds.is_night() then
|
||||
activity = SLEEP
|
||||
elseif tod > lunch_start and tod < lunch_end then
|
||||
activity = "lunch"
|
||||
activity = GATHERING
|
||||
else
|
||||
activity = "chill"
|
||||
end
|
||||
|
@ -599,8 +602,29 @@ function get_activity(tod)
|
|||
|
||||
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)
|
||||
|
||||
if not check_bed (entity) then
|
||||
mcl_log("Cannot find bed, so cannot go home")
|
||||
end
|
||||
|
||||
local b = entity._bed
|
||||
if not b then
|
||||
return
|
||||
|
@ -647,22 +671,7 @@ local function go_home(entity, sleep)
|
|||
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
|
||||
|
@ -742,25 +751,30 @@ local function check_summon(self,dtime)
|
|||
self._summon_timer = self._summon_timer + dtime
|
||||
end
|
||||
|
||||
local function has_traded (self)
|
||||
--mcl_log("Checking name: " .. self._trades)
|
||||
local function debug_trades(self)
|
||||
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
|
||||
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
|
||||
|
@ -772,10 +786,33 @@ local function has_traded (self)
|
|||
end
|
||||
|
||||
local function unlock_trades (self)
|
||||
if self then
|
||||
--mcl_log("We should now try to unlock trades")
|
||||
else
|
||||
mcl_log("Missing self")
|
||||
if not self._trades then
|
||||
mcl_log("No trades set. has_traded is false")
|
||||
return false
|
||||
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
|
||||
trade.trade_counter = 0
|
||||
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
|
||||
|
||||
|
@ -808,13 +845,6 @@ 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
|
||||
|
@ -828,6 +858,7 @@ local function look_for_job(self, requested_jobsites)
|
|||
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
|
||||
local m = minetest.get_meta(n)
|
||||
--mcl_log("Job owner: ".. m:get_string("villager"))
|
||||
|
@ -859,31 +890,21 @@ local function look_for_job(self, requested_jobsites)
|
|||
end
|
||||
|
||||
|
||||
|
||||
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?")
|
||||
--self.order = JOB_HUNTING
|
||||
|
||||
local requested_jobsites = jobsites
|
||||
if has_traded (self) then
|
||||
mcl_log("Has traded so look for job of my type")
|
||||
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 ~= PATHFINDING then
|
||||
|
@ -894,17 +915,16 @@ end
|
|||
|
||||
local function retrieve_my_jobsite (self)
|
||||
if not self or not self._jobsite then
|
||||
--mcl_log("find_jobsite. Invalid params")
|
||||
mcl_log("find_jobsite. Invalid params. Should not happen")
|
||||
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")
|
||||
mcl_log("This isn't my jobsite")
|
||||
end
|
||||
return
|
||||
end
|
||||
|
@ -932,10 +952,12 @@ local function validate_jobsite(self)
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function do_work (self)
|
||||
if self.child then return end
|
||||
--debug_trades(self)
|
||||
if self.child then
|
||||
mcl_log("A child so don't send to work")
|
||||
return
|
||||
end
|
||||
--mcl_log("Time for work")
|
||||
|
||||
-- Don't try if looking_for_work, or gowp possibly
|
||||
|
@ -946,29 +968,23 @@ local function do_work (self)
|
|||
local jobsite = self._jobsite
|
||||
|
||||
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 vector.distance(self.object:get_pos(),self._jobsite) < 2 then
|
||||
--mcl_log("Made it to work ok!")
|
||||
|
||||
if not (self.state == PATHFINDING) then
|
||||
--mcl_log("Setting order to work.")
|
||||
if distance_to_jobsite < 2 then
|
||||
if self.state ~= PATHFINDING and self.order ~= WORK 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("Still pathfinding.")
|
||||
end
|
||||
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")
|
||||
|
@ -984,19 +1000,94 @@ local function do_work (self)
|
|||
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
|
||||
elseif self._profession == "unemployed" or has_traded(self) then
|
||||
get_a_job(self)
|
||||
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 = false
|
||||
if check_bed (self) then
|
||||
wandered_too_far = ( self.state ~= PATHFINDING ) and (vector.distance(self.object:get_pos(),self._bed) > 50 )
|
||||
end
|
||||
|
||||
|
||||
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)
|
||||
if not self._trades then
|
||||
return
|
||||
|
@ -1503,6 +1594,7 @@ local trade_inventory = {
|
|||
trader.health = math.min(trader.hp_max, trader.health + 4)
|
||||
end
|
||||
trade.trade_counter = trade.trade_counter + 1
|
||||
mcl_log("Trade counter is: ".. trade.trade_counter)
|
||||
-- Semi-randomly lock trade for repeated trade (not if there's only 1 trade)
|
||||
if trader._max_tradenum > 1 then
|
||||
if trade.trade_counter >= 12 then
|
||||
|
@ -1662,13 +1754,13 @@ mcl_mobs:register_mob("mobs_mc:villager", {
|
|||
self.attack = nil
|
||||
end
|
||||
-- Don't do at night. Go to bed? Maybe do_activity needs it's own method
|
||||
if validate_jobsite(self) then
|
||||
mcl_mobs:gopath(self,self._jobsite,function()
|
||||
--minetest.log("arrived at jobsite")
|
||||
end)
|
||||
if validate_jobsite(self) and not self.order == WORK then
|
||||
--mcl_mobs:gopath(self,self._jobsite,function()
|
||||
-- minetest.log("sent to jobsite")
|
||||
--end)
|
||||
else
|
||||
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
|
||||
end
|
||||
|
||||
-- Initiate trading
|
||||
|
@ -1676,11 +1768,13 @@ mcl_mobs:register_mob("mobs_mc:villager", {
|
|||
local name = clicker:get_player_name()
|
||||
self._trading_players[name] = true
|
||||
|
||||
if self._trades == nil then
|
||||
if self._trades == nil or self._trades == false then
|
||||
minetest.log("Trades is nil so init")
|
||||
init_trades(self)
|
||||
end
|
||||
update_max_tradenum(self)
|
||||
if self._trades == false then
|
||||
minetest.log("Trades is false. no right click op")
|
||||
-- Villager has no trades, rightclick is a no-op
|
||||
return
|
||||
end
|
||||
|
@ -1739,42 +1833,7 @@ mcl_mobs:register_mob("mobs_mc:villager", {
|
|||
self.jump = true
|
||||
end
|
||||
|
||||
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
|
||||
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
|
||||
do_activity (self)
|
||||
|
||||
end
|
||||
end,
|
||||
|
|
Loading…
Reference in New Issue