From 3774044f86b5176a9f1cba09a601050064157617 Mon Sep 17 00:00:00 2001 From: cora Date: Mon, 9 May 2022 01:00:34 +0200 Subject: [PATCH] Walk back to bed instead of teleporting --- mods/ENTITIES/mobs_mc/villager.lua | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/mods/ENTITIES/mobs_mc/villager.lua b/mods/ENTITIES/mobs_mc/villager.lua index 7efc5d29e8..28a489e758 100644 --- a/mods/ENTITIES/mobs_mc/villager.lua +++ b/mods/ENTITIES/mobs_mc/villager.lua @@ -924,6 +924,31 @@ minetest.register_on_joinplayer(function(player) inv:set_size("offered", 1) 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_home(entity) + local b=entity.bed + local s=entity.object:get_pos() + if not b then return end + 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) + entity.state = "go_home" + if vector.distance(b,s) < 10 then + entity.state = "stand" + set_velocity(entity,0) + end +end + --[=======[ MOB REGISTRATION AND SPAWNING ]=======] mobs:register_mob("mobs_mc:villager", { @@ -1029,8 +1054,8 @@ mobs:register_mob("mobs_mc:villager", { _player_scan_timer = 0, _trading_players = {}, -- list of playernames currently trading with villager (open formspec) do_custom = function(self, dtime) - if self.bed and vector.distance(self.object:get_pos(),self.bed) > 50 then - self.object:set_pos(self.bed) + if self.bed and ( self.state == "go_home" or vector.distance(self.object:get_pos(),self.bed) > 50 ) then + go_home(self) end -- Stand still if player is nearby. if not self._player_scan_timer then