1 local pr
= PseudoRandom(os
.time()*5)
6 table.insert(offsets
, {x
=x
, y
=0, z
=z
})
10 --[[ Periodically check and teleport mob to owner if not sitting (order ~= "sit") and
11 the owner is too far away. To be used with do_custom. Note: Optimized for mobs smaller than 1×1×1.
12 Larger mobs might have space problems after teleportation.
14 * dist: Minimum required distance from owner to teleport. Default: 12
15 * teleport_check_interval: Optional. Interval in seconds to check the mob teleportation. Default: 4 ]]
16 mobs_mc
.make_owner_teleport_function
= function(dist
, teleport_check_interval
)
17 return function(self
, dtime
)
18 -- No teleportation if no owner or if sitting
19 if not self
.owner
or self
.order
== "sit" then
22 if not teleport_check_interval
then
23 teleport_check_interval
= 4
28 if self
._teleport_timer
== nil then
29 self
._teleport_timer
= teleport_check_interval
32 self
._teleport_timer
= self
._teleport_timer
- dtime
33 if self
._teleport_timer
<= 0 then
34 self
._teleport_timer
= teleport_check_interval
35 local mob_pos
= self
.object
:getpos()
36 local owner
= minetest
.get_player_by_name(self
.owner
)
38 -- No owner found, no teleportation
41 local owner_pos
= owner
:getpos()
42 local dist_from_owner
= vector
.distance(owner_pos
, mob_pos
)
43 if dist_from_owner
> dist
then
44 -- Check for nodes below air in a 5×1×5 area around the owner position
45 local check_offsets
= table.copy(offsets
)
46 -- Attempt to place mob near player. Must be placed on walkable node below a non-walkable one. Place inside that air node.
47 while #check_offsets
> 0 do
48 local r
= pr
:next(1, #check_offsets
)
49 local telepos
= vector
.add(owner_pos
, check_offsets
[r
])
50 local telepos_below
= {x
=telepos
.x
, y
=telepos
.y
-1, z
=telepos
.z
}
51 table.remove(check_offsets
, r
)
52 -- Long story short, spawn on a platform
53 if minetest
.registered_nodes
[minetest
.get_node(telepos
).name
].walkable
== false and
54 minetest
.registered_nodes
[minetest
.get_node(telepos_below
).name
].walkable
== true then
55 -- Correct position found! Let's teleport.
56 self
.object
:setpos(telepos
)