From 583819b8ef063d500d8d276daca4d2643ab176bd Mon Sep 17 00:00:00 2001 From: cora Date: Thu, 12 May 2022 02:05:30 +0200 Subject: [PATCH] refactor go home logic --- mods/ENTITIES/mobs_mc/villager.lua | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/mods/ENTITIES/mobs_mc/villager.lua b/mods/ENTITIES/mobs_mc/villager.lua index f038fc751..f108b7648 100644 --- a/mods/ENTITIES/mobs_mc/villager.lua +++ b/mods/ENTITIES/mobs_mc/villager.lua @@ -347,6 +347,42 @@ local stand_still = function(self) self.jump = false end +local function set_velocity(self, v) + local yaw = (self.object:get_yaw() or 0) + self.rotate + self.object:set_velocity({ + x = (math.sin(yaw) * -v), + y = self.object:get_velocity().y, + z = (math.cos(yaw) * v), + }) +end + +local function go_to_pos(entity,b) + local s=entity.object:get_pos() + local v = { x = b.x - s.x, z = b.z - s.z } + local yaw = (math.atan(v.z / v.x) + math.pi / 2) - entity.rotate + if b.x > s.x then yaw = yaw + math.pi end + entity.object:set_yaw(yaw) + set_velocity(entity,entity.follow_velocity) + if vector.distance(b,s) < 5 then + return true + end +end + +local function go_home(entity) + entity.state = "go_home" + local b=entity.bed + if not b then return end + if go_to_pos(entity,b) then + entity.state = "stand" + set_velocity(entity,0) + entity.object:set_pos(b) + local n=minetest.get_node(b) + if n and n.name ~= "mcl_beds:bed_red_bottom" then + entity.bed=nil --the stormtroopers have killed uncle owen + end + end +end + local update_max_tradenum = function(self) if not self._trades then return @@ -1042,6 +1078,11 @@ mobs:register_mob("mobs_mc:villager", { if not self._player_scan_timer then self._player_scan_timer = 0 end + + if self.bed and ( self.state == "go_home" or vector.distance(self.object:get_pos(),self.bed) > 50 ) then + go_home(self) + end + self._player_scan_timer = self._player_scan_timer + dtime -- Check infrequently to keep CPU load low if self._player_scan_timer > PLAYER_SCAN_INTERVAL then