code tweak and tidy

This commit is contained in:
tenplus1 2020-07-25 10:52:29 +01:00
parent 469e35d6e0
commit cb465559a1
2 changed files with 56 additions and 88 deletions

135
api.lua
View File

@ -6,7 +6,7 @@ local use_cmi = minetest.global_exists("cmi")
mobs = { mobs = {
mod = "redo", mod = "redo",
version = "20200723", version = "20200725",
intllib = S, intllib = S,
invis = minetest.global_exists("invisibility") and invisibility or {} invis = minetest.global_exists("invisibility") and invisibility or {}
} }
@ -230,7 +230,7 @@ function mob_class:collision()
end end
-- check string against another string or table -- check if string exists in another string or table
local check_for = function(look_for, look_inside) local check_for = function(look_for, look_inside)
if type(look_inside) == "string" and look_inside == look_for then if type(look_inside) == "string" and look_inside == look_for then
@ -291,8 +291,7 @@ function mob_class:set_velocity(v)
local new_vel = { local new_vel = {
x = (sin(yaw) * -v) + c_x, x = (sin(yaw) * -v) + c_x,
y = vel.y, y = vel.y,
z = (cos(yaw) * v) + c_y z = (cos(yaw) * v) + c_y}
}
self.object:set_velocity(new_vel) self.object:set_velocity(new_vel)
end end
@ -353,9 +352,9 @@ function mob_class:set_animation(anim, force)
return return
end end
-- check for more than one animation
local num = 0 local num = 0
-- check for more than one animation (max 4)
for n = 1, 4 do for n = 1, 4 do
if self.animation[anim .. n .. "_start"] if self.animation[anim .. n .. "_start"]
@ -388,7 +387,7 @@ end
-- above function exported for mount.lua -- above function exported for mount.lua
function mobs:set_animation(entity, anim) function mobs:set_animation(entity, anim)
mob_class.set_animation(entity, anim) entity.set_animation(entity, anim)
end end
@ -750,7 +749,7 @@ function mob_class:item_drop()
-- was mob killed by player? -- was mob killed by player?
local death_by_player = self.cause_of_death local death_by_player = self.cause_of_death
and self.cause_of_death.puncher and self.cause_of_death.puncher
and self.cause_of_death.puncher:is_player() or nil and self.cause_of_death.puncher:is_player()
local obj, item, num local obj, item, num
@ -970,12 +969,11 @@ function mob_class:is_at_cliff()
return false return false
end end
-- if object no longer exists then return -- get yaw but if nil returned object no longer exists
if not self.object:get_luaentity() then
return false
end
local yaw = self.object:get_yaw() local yaw = self.object:get_yaw()
if not yaw then return false end
local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5) local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5) local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
local pos = self.object:get_pos() local pos = self.object:get_pos()
@ -1035,24 +1033,6 @@ function mob_class:do_env_damage()
-- particle appears at random mob height -- particle appears at random mob height
pos.y = pos.y + random(self.collisionbox[2], self.collisionbox[5]) pos.y = pos.y + random(self.collisionbox[2], self.collisionbox[5])
-- is mob light sensative, or scared of the dark :P
if self.light_damage ~= 0 then
local light = minetest.get_node_light(pos) or 0
if light >= self.light_damage_min
and light <= self.light_damage_max then
self.health = self.health - self.light_damage
effect(pos, 5, "tnt_smoke.png")
if self:check_for_death({type = "light"}) then
return true
end
end
end
local nodef = minetest.registered_nodes[self.standing_in] local nodef = minetest.registered_nodes[self.standing_in]
-- water -- water
@ -1098,6 +1078,24 @@ function mob_class:do_env_damage()
end end
end end
-- is mob light sensative, or scared of the dark :P
if self.light_damage ~= 0 then
local light = minetest.get_node_light(pos) or 0
if light >= self.light_damage_min
and light <= self.light_damage_max then
self.health = self.health - self.light_damage
effect(pos, 5, "tnt_smoke.png")
if self:check_for_death({type = "light"}) then
return true
end
end
end
--- suffocation inside solid node --- suffocation inside solid node
if (self.suffocation and self.suffocation ~= 0) if (self.suffocation and self.suffocation ~= 0)
and (nodef.walkable == nil or nodef.walkable == true) and (nodef.walkable == nil or nodef.walkable == true)
@ -1545,8 +1543,7 @@ local height_switcher = false
function mob_class:smart_mobs(s, p, dist, dtime) function mob_class:smart_mobs(s, p, dist, dtime)
local s1 = self.path.lastpos local s1 = self.path.lastpos
-- local target_pos = self.attack:get_pos() local target_pos = p
local target_pos = p
-- is it becoming stuck? -- is it becoming stuck?
@ -1693,9 +1690,9 @@ local target_pos = p
self.state = "" self.state = ""
if self.attack then if self.attack then
self:do_attack(self.attack) self:do_attack(self.attack)
end end
-- no path found, try something else -- no path found, try something else
if not self.path.way then if not self.path.way then
@ -1785,7 +1782,6 @@ end
minetest.add_item(p1, ItemStack(node1)) minetest.add_item(p1, ItemStack(node1))
minetest.set_node(p1, {name = "air"}) minetest.set_node(p1, {name = "air"})
end end
end end
end end
end end
@ -1798,11 +1794,12 @@ end
self.path.following = true self.path.following = true
else else
-- yay i found path -- yay i found path
if self.attack then if self.attack then
self:mob_sound(self.sounds.war_cry) self:mob_sound(self.sounds.war_cry)
else else
self:mob_sound(self.sounds.random) self:mob_sound(self.sounds.random)
end end
self:set_velocity(self.walk_velocity) self:set_velocity(self.walk_velocity)
-- follow path now that it has it -- follow path now that it has it
@ -1812,26 +1809,6 @@ end
end end
-- specific attacks
local specific_attack = function(list, what)
-- no list so attack default (player, animals etc.)
if list == nil then
return true
end
-- found entity on list to attack?
for no = 1, #list do
if list[no] == what then
return true
end
end
return false
end
-- general attack function for all mobs -- general attack function for all mobs
function mob_class:general_attack() function mob_class:general_attack()
@ -1859,7 +1836,8 @@ function mob_class:general_attack()
or self.attack_players == false or self.attack_players == false
or (self.owner and self.type ~= "monster") or (self.owner and self.type ~= "monster")
or mobs.invis[objs[n]:get_player_name()] or mobs.invis[objs[n]:get_player_name()]
or not specific_attack(self.specific_attack, "player") then or (self.specific_attack
and not check_for("player", self.specific_attack)) then
objs[n] = nil objs[n] = nil
--print("- pla", n) --print("- pla", n)
end end
@ -1872,7 +1850,8 @@ function mob_class:general_attack()
or (not self.attack_animals and ent.type == "animal") or (not self.attack_animals and ent.type == "animal")
or (not self.attack_monsters and ent.type == "monster") or (not self.attack_monsters and ent.type == "monster")
or (not self.attack_npcs and ent.type == "npc") or (not self.attack_npcs and ent.type == "npc")
or not specific_attack(self.specific_attack, ent.name) then or (self.specific_attack
and not check_for(ent.name, self.specific_attack)) then
objs[n] = nil objs[n] = nil
--print("- mob", n, self.name, ent.name) --print("- mob", n, self.name, ent.name)
end end
@ -1915,26 +1894,6 @@ function mob_class:general_attack()
end end
-- specific runaway
local specific_runaway = function(list, what)
-- no list so do not run
if list == nil then
return false
end
-- found entity on list to attack?
for no = 1, #list do
if list[no] == what then
return true
end
end
return false
end
-- find someone to runaway from -- find someone to runaway from
function mob_class:do_runaway_from() function mob_class:do_runaway_from()
@ -1973,7 +1932,7 @@ function mob_class:do_runaway_from()
-- find specific mob to runaway from -- find specific mob to runaway from
if name ~= "" and name ~= self.name if name ~= "" and name ~= self.name
and specific_runaway(self.runaway_from, name) then and (self.runaway_from and check_for(name, self.runaway_from)) then
sp = s sp = s
p = player and player:get_pos() or s p = player and player:get_pos() or s
@ -2506,7 +2465,7 @@ function mob_class:do_states(dtime)
return return
end end
if abs(p1.x-s.x) + abs(p1.z - s.z) < 0.6 then if abs(p1.x - s.x) + abs(p1.z - s.z) < 0.6 then
-- reached waypoint, remove it from queue -- reached waypoint, remove it from queue
table.remove(self.path.way, 1) table.remove(self.path.way, 1)
end end
@ -2664,9 +2623,9 @@ function mob_class:falling(pos)
-- in water then use liquid viscosity for float/sink speed -- in water then use liquid viscosity for float/sink speed
if (self.standing_in if (self.standing_in
and minetest.registered_nodes[self.standing_in].groups.liquid) --water) and minetest.registered_nodes[self.standing_in].groups.liquid)
or (self.standing_on or (self.standing_on
and minetest.registered_nodes[self.standing_in].groups.liquid) then -- water) then and minetest.registered_nodes[self.standing_in].groups.liquid) then
local visc = min( local visc = min(
minetest.registered_nodes[self.standing_in].liquid_viscosity, 7) minetest.registered_nodes[self.standing_in].liquid_viscosity, 7)
@ -2928,7 +2887,7 @@ function mob_class:on_punch(hitter, tflp, tool_capabilities, dir, damage)
dir = dir or {x = 0, y = 0, z = 0} dir = dir or {x = 0, y = 0, z = 0}
-- use tool knockback value or default -- use tool knockback value or default
kb = tool_capabilities.damage_groups["knockback"] or kb -- (kb * 1.5) kb = tool_capabilities.damage_groups["knockback"] or kb
self.object:set_velocity({ self.object:set_velocity({
x = dir.x * kb, x = dir.x * kb,

View File

@ -344,6 +344,15 @@ Adding Mobs in World
Returns false if mob could not be added, returns mob object if spawned ok. Returns false if mob could not be added, returns mob object if spawned ok.
Removing Mob from World
-----------------------
mobs:remove(self, decrease)
Removes mob 'self' from the world and if 'decrease' is true then the mob counter
will also be decreased by one.
Spawning Mobs in World Spawning Mobs in World
---------------------- ----------------------