From 682d76d7b358c847f2bb081c9098542396d43185 Mon Sep 17 00:00:00 2001 From: tenplus1 Date: Thu, 16 Nov 2023 10:00:10 +0000 Subject: [PATCH] add 'homing' setting to mob definition so that arrows can follow player when visible. --- api.lua | 26 +++++++++++++++++++++++++- api.txt | 3 ++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/api.lua b/api.lua index 5d84a54..756aa46 100644 --- a/api.lua +++ b/api.lua @@ -11,7 +11,7 @@ local use_mc2 = minetest.get_modpath("mcl_core") -- Global mobs = { mod = "redo", - version = "20231111", + version = "20231116", translate = S, invis = minetest.global_exists("invisibility") and invisibility or {}, node_snow = minetest.registered_aliases["mapgen_snow"] @@ -2653,6 +2653,12 @@ function mob_class:do_states(dtime) ent.switch = 1 ent.owner_id = tostring(self.object) -- add unique owner id to arrow + -- setup homing arrow and target + if self.homing then + ent._target = self.attack + ent.homing = true + end + -- offset makes shoot aim accurate vec.y = vec.y + self.shoot_offset vec.x = vec.x * (v / amount) @@ -3593,6 +3599,7 @@ minetest.register_entity(":" .. name, setmetatable({ arrow = def.arrow, arrow_override = def.arrow_override, shoot_interval = def.shoot_interval, + homing = def.homing, sounds = def.sounds, animation = def.animation, follow = def.follow, @@ -4224,6 +4231,23 @@ function mobs:register_arrow(name, def) end end + -- make homing arrows follow target if seen + if self.homing and self._target then + + local p = self._target:get_pos() + + if p then + + if minetest.line_of_sight(self.object:get_pos(), p) then + + self.object:set_velocity( + vector.direction(self.object:get_pos(), p) * self.velocity) + end + else + self._target = nil + end + end + if self.hit_player or self.hit_mob or self.hit_object then for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do diff --git a/api.txt b/api.txt index 988b22f..d959963 100644 --- a/api.txt +++ b/api.txt @@ -148,8 +148,9 @@ functions needed for the mob to work properly which contains the following: 'dogshoot_count2_max' contains how many seconds before switching from shoot to dogfight. 'shoot_interval' has the number of seconds between shots. - 'shoot_offset' holds the y position added as to where the + 'shoot_offset' holds the y position added as to where the arrow/fireball appears on mob. + 'homing' When True arrows will follow player when visible. 'specific_attack' has a table of entity names that mob can also attack e.g. {"player", "mobs_animal:chicken"}. 'friendly_fire` when set to false, mobs will not be able to harm other