use atan2, added attached attack, added swim to shore

This commit is contained in:
TenPlus1 2016-12-20 18:17:56 +00:00
parent d720114462
commit 12503180f4
1 changed files with 47 additions and 50 deletions

97
api.lua
View File

@ -1,5 +1,5 @@
-- Mobs Api (6th December 2016) -- Mobs Api (16th December 2016)
mobs = {} mobs = {}
mobs.mod = "redo" mobs.mod = "redo"
@ -72,6 +72,7 @@ local atan = function(x)
return atann(x) return atann(x)
end end
end end
local atan2 = math.atan2
do_attack = function(self, player) do_attack = function(self, player)
@ -362,7 +363,7 @@ function check_for_death(self)
obj:setvelocity({ obj:setvelocity({
x = random(-10, 10) / 9, x = random(-10, 10) / 9,
y = 5, y = 6,
z = random(-10, 10) / 9, z = random(-10, 10) / 9,
}) })
end end
@ -1211,16 +1212,10 @@ local follow_flop = function(self)
else else
local vec = { local vec = {
x = p.x - s.x, x = p.x - s.x,
y = p.y - s.y,
z = p.z - s.z z = p.z - s.z
} }
local yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate local yaw = (atan2(vec.z, vec.x) - pi / 2) - self.rotate
if p.x > s.x then
yaw = yaw + pi
end
self.object:setyaw(yaw) self.object:setyaw(yaw)
-- anyone but standing npc's can move along -- anyone but standing npc's can move along
@ -1320,17 +1315,12 @@ local do_states = function(self, dtime)
local vec = { local vec = {
x = lp.x - s.x, x = lp.x - s.x,
y = lp.y - s.y,
z = lp.z - s.z z = lp.z - s.z
} }
yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate yaw = (atan2(vec.z, vec.x) - pi / 2) - self.rotate
if lp.x > s.x then
yaw = yaw + pi
end
else else
yaw = (random(0, 360) - 180) / 180 * pi yaw = random() * 2 * pi
end end
self.object:setyaw(yaw) self.object:setyaw(yaw)
@ -1373,19 +1363,36 @@ local do_states = function(self, dtime)
lp = minetest.find_node_near(s, 1, {"group:lava"}) lp = minetest.find_node_near(s, 1, {"group:lava"})
end end
-- if something then avoid
if lp then if lp then
local vec = { -- if mob in water or lava then look for land
x = lp.x - s.x, if (self.lava_damage and minetest.registered_nodes[self.standing_in].groups.lava)
y = lp.y - s.y, or (self.water_damage and minetest.registered_nodes[self.standing_in].groups.water) then
z = lp.z - s.z
}
yaw = atan(vec.z / vec.x) + 3 * pi / 2 - self.rotate lp = minetest.find_node_near(s, 5, {"group:soil", "group:stone",
"group:sand", "default:ice", "default:snowblock"})
if lp.x > s.x then -- did we find land?
yaw = yaw + pi if lp then
local vec = {
x = lp.x - s.x,
z = lp.z - s.z
}
yaw = atan2(vec.z, vec.x) + pi / 2 - self.rotate
else
yaw = random() * 2 * pi
end
else
local vec = {
x = lp.x - s.x,
z = lp.z - s.z
}
yaw = atan2(vec.z, vec.x) + pi / 2 - self.rotate
end end
self.object:setyaw(yaw) self.object:setyaw(yaw)
@ -1393,7 +1400,7 @@ local do_states = function(self, dtime)
-- otherwise randomly turn -- otherwise randomly turn
elseif random(1, 100) <= 30 then elseif random(1, 100) <= 30 then
local yaw = (random(0, 360) - 180) / 180 * pi yaw = random() * 2 * pi
self.object:setyaw(yaw) self.object:setyaw(yaw)
end end
@ -1477,15 +1484,10 @@ local do_states = function(self, dtime)
local vec = { local vec = {
x = p.x - s.x, x = p.x - s.x,
y = p.y - s.y,
z = p.z - s.z z = p.z - s.z
} }
yaw = atan(vec.z / vec.x) + pi / 2 - self.rotate yaw = atan2(vec.z, vec.x) - pi / 2 - self.rotate
if p.x > s.x then
yaw = yaw + pi
end
self.object:setyaw(yaw) self.object:setyaw(yaw)
@ -1652,15 +1654,10 @@ local do_states = function(self, dtime)
local vec = { local vec = {
x = p.x - s.x, x = p.x - s.x,
y = p.y - s.y,
z = p.z - s.z z = p.z - s.z
} }
yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate yaw = (atan2(vec.z, vec.x) - pi / 2) - self.rotate
if p.x > s.x then
yaw = yaw + pi
end
self.object:setyaw(yaw) self.object:setyaw(yaw)
@ -1737,7 +1734,11 @@ local do_states = function(self, dtime)
}) })
end end
-- punch player -- punch player (or what player is attached to)
local attached = self.attack:get_attach()
if attached then
self.attack = attached
end
self.attack:punch(self.object, 1.0, { self.attack:punch(self.object, 1.0, {
full_punch_interval = 1.0, full_punch_interval = 1.0,
damage_groups = {fleshy = self.damage} damage_groups = {fleshy = self.damage}
@ -1769,11 +1770,7 @@ local do_states = function(self, dtime)
z = p.z - s.z z = p.z - s.z
} }
yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate yaw = (atan2(vec.z, vec.x) - pi / 2) - self.rotate
if p.x > s.x then
yaw = yaw + pi
end
self.object:setyaw(yaw) self.object:setyaw(yaw)
@ -1887,12 +1884,12 @@ local mob_punch = function(self, hitter, tflp, tool_capabilities, dir)
return return
end end
-- is mob protected? -- is mob protected?
if self.protected and hitter:is_player() if self.protected and hitter:is_player()
and minetest.is_protected(self.object:getpos(), hitter:get_player_name()) then and minetest.is_protected(self.object:getpos(), hitter:get_player_name()) then
minetest.chat_send_player(hitter:get_player_name(), "Mob has been protected!") minetest.chat_send_player(hitter:get_player_name(), "Mob has been protected!")
return return
end end
-- weapon wear -- weapon wear