move movement code to mcl_mobs

This commit is contained in:
cora 2022-05-17 23:35:56 +02:00
parent ce7a882c86
commit dbc5564d02
2 changed files with 57 additions and 61 deletions

View File

@ -2317,17 +2317,28 @@ local dogswitch = function(self, dtime)
return self.dogshoot_switch
end
local function go_to_pos(entity,b)
if not entity then return end
local s=entity.object:get_pos()
if vector.distance(b,s) < 1 then
--set_velocity(entity,0)
return true
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)
end
-- execute current state (stand, walk, run, attacks)
-- returns true if mob has died
local do_states = function(self, dtime)
local yaw = self.object:get_yaw() or 0
if self.state == "stand" then
if random(1, 4) == 1 then
local lp = nil
local s = self.object:get_pos()
local objs = minetest.get_objects_inside_radius(s, 3)
@ -2340,7 +2351,7 @@ local do_states = function(self, dtime)
end
-- look at any players nearby, otherwise turn randomly
if lp then
if self.look_at_players then
local vec = {
x = lp.x - s.x,
@ -2375,8 +2386,29 @@ local do_states = function(self, dtime)
end
end
elseif self.state == "walk" then
elseif self.state == "gowp" then
if not self.waypoints or not self._target then return end
local p = self.object:get_pos()
if vector.distance(p,self._target) < 1 then
self.waypoints = nil
self._target = nil
self.current_target = nil
self.state = "walk"
if self.callback_arrived then return self.callback_arrived(self) end
return true
end
if not self.current_target or vector.distance(p,self.current_target) < 1.5 then
self.current_target = table.remove(self.waypoints, 1)
else
go_to_pos(self,self.current_target)
end
if self.current_target and not minetest.line_of_sight(self.object:get_pos(),self.current_target) then
self.waypoints=minetest.find_path(p,self._target,150,1,4)
self.current_target = nil
end
elseif self.state == "walk" then
local s = self.object:get_pos()
local lp = nil
@ -2880,6 +2912,14 @@ local do_states = function(self, dtime)
end
end
function mobs:go_wplist(self,target,callback_arrived)
if not target then return end
self._target = target
self.waypoints = minetest.find_path(self.object:get_pos(),target,150,1,4)
self.callback_arrived = callback_arrived
self.state = "gowp"
end
-- falling and fall damage
-- returns true if mob died
@ -3576,8 +3616,7 @@ local mob_step = function(self, dtime)
-- attack timer
self.timer = self.timer + dtime
if self.state ~= "attack" then
if self.state ~= "attack" and self.state ~= "gowp" then
if self.timer < 1 then
return
end

View File

@ -555,60 +555,19 @@ local function init_trader_vars(self)
end
end
--movement stuff - to be abstracted to mcl_mobs
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 set_texture(self)
local t = table.copy(professions[self._profession].textures)
--t[1] = "[combine:<w>x<h>:<x1>,<y1>="..t[1]..":30,50="..badges[self._max_trade_tier].."^[resize:16x16"
local function go_to_pos(entity,b)
if not entity then return end
local s=entity.object:get_pos()
if vector.distance(b,s) < 1 then
--set_velocity(entity,0)
return true
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)
end
local function check_gowp(self)
if not self.waypoints or not self._target then return end
local p = self.object:get_pos()
if vector.distance(p,self._target) < 1 then
self.waypoints = nil
self._target = nil
self.current_target = nil
if self.callback_arrived then return self.callback_arrived(self) end
return true
end
if not self.current_target or go_to_pos(self,self.current_target) then
self.current_target = table.remove(self.waypoints, 1)
end
if not minetest.line_of_sight(self.object:get_pos(),self.current_target) then
self.waypoints=minetest.find_path(p,self._target,150,1,4)
self.current_target = nil
end
end
local function go_wplist(self,target,wps,callback_arrived)
self.waypoints = wps
self._target = target
self.callback_arrived = callback_arrived
self.object:set_properties({textures=t})
end
local function go_home(entity)
entity.state = "go_home"
local b=entity.bed
if not b then return end
go_wplist(entity,b,minetest.find_path(entity.object:get_pos(),b,50,1,4),function(entity,b)
mobs:go_wplist(entity,b,function(entity,b)
if vector.distance(entity.object:get_pos(),b) < 2 then
entity.state = "stand"
set_velocity(entity,0)
@ -1269,9 +1228,11 @@ mobs:register_mob("mobs_mc:villager", {
bed = nil,
_id = nil,
_profession = "unemployed",
look_at_player = true,
on_rightclick = function(self, clicker)
go_wplist(self,minetest.find_path(self.object:get_pos(),vector.new(0,9,0),50,1,4),function(self,b)
minetest.log("arrived at 0,0")
local trg=vector.new(0,9,0)
mobs:go_wplist(self,trg,function()
minetest.log("arrived at "..minetest.pos_to_string(trg))
end)
if clicker:get_wielded_item():get_name() == "mcl_farming:bread" then
if mobs:feed_tame(self, clicker, 1, true, true) then return end
@ -1320,8 +1281,6 @@ mobs:register_mob("mobs_mc:villager", {
-- Stand still if player is nearby.
if not self._player_scan_timer then
self._player_scan_timer = 0
else
check_gowp(self)
end
self._player_scan_timer = self._player_scan_timer + dtime
@ -1350,8 +1309,6 @@ mobs:register_mob("mobs_mc:villager", {
end
if self._profession == "unemployed" then
get_a_job(self)
-- else
-- check_jobsite(self)
end
end
end,