VoxeLibre/mods/ENTITIES/mcl_mobs/backup_code_api.lua
2021-04-13 08:07:32 -04:00

1466 lines
33 KiB
Lua

local disable_physics = function(object, luaentity, ignore_check, reset_movement)
if luaentity.physical_state == true or ignore_check == true then
luaentity.physical_state = false
object:set_properties({
physical = false
})
if reset_movement ~= false then
object:set_velocity({x=0,y=0,z=0})
object:set_acceleration({x=0,y=0,z=0})
end
end
end
----For Water Flowing:
local enable_physics = function(object, luaentity, ignore_check)
if luaentity.physical_state == false or ignore_check == true then
luaentity.physical_state = true
object:set_properties({
physical = true
})
object:set_velocity({x=0,y=0,z=0})
object:set_acceleration({x=0,y=-9.81,z=0})
end
end
--[[
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer < 1 then return end
for _, player in pairs(minetest.get_connected_players()) do
local pos = player:get_pos()
for _, obj in pairs(minetest_get_objects_inside_radius(pos, 47)) do
local lua = obj:get_luaentity()
if lua and lua._cmi_is_mob then
lua.lifetimer = math.max(20, lua.lifetimer)
lua.despawn_immediately = false
end
end
end
timer = 0
end)
]]--
-- compatibility function for old entities to new modpack entities
function mobs:alias_mob(old_name, new_name)
-- spawn egg
minetest.register_alias(old_name, new_name)
-- entity
minetest.register_entity(":" .. old_name, {
physical = false,
on_step = function(self)
if minetest_registered_entities[new_name] then
minetest_add_entity(self.object:get_pos(), new_name)
end
self.object:remove()
end
})
end
-- Spawn a child
function mobs:spawn_child(pos, mob_type)
local child = minetest_add_entity(pos, mob_type)
if not child then
return
end
local ent = child:get_luaentity()
effect(pos, 15, "mcl_particles_smoke.png", 1, 2, 2, 15, 5)
ent.child = true
local textures
-- using specific child texture (if found)
if ent.child_texture then
textures = ent.child_texture[1]
end
-- and resize to half height
child:set_properties({
textures = textures,
visual_size = {
x = ent.base_size.x * .5,
y = ent.base_size.y * .5,
},
collisionbox = {
ent.base_colbox[1] * .5,
ent.base_colbox[2] * .5,
ent.base_colbox[3] * .5,
ent.base_colbox[4] * .5,
ent.base_colbox[5] * .5,
ent.base_colbox[6] * .5,
},
selectionbox = {
ent.base_selbox[1] * .5,
ent.base_selbox[2] * .5,
ent.base_selbox[3] * .5,
ent.base_selbox[4] * .5,
ent.base_selbox[5] * .5,
ent.base_selbox[6] * .5,
},
})
return child
end
-- No-op in MCL2 (capturing mobs is not possible).
-- Provided for compability with Mobs Redo
function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
return false
end
-- No-op in MCL2 (protecting mobs is not possible).
function mobs:protect(self, clicker)
return false
end
-- feeding, taming and breeding (thanks blert2112)
function mobs:feed_tame(self, clicker, feed_count, breed, tame)
if not self.follow then
return false
end
-- can eat/tame with item in hand
if follow_holding(self, clicker) then
-- if not in creative then take item
if not mobs.is_creative(clicker:get_player_name()) then
local item = clicker:get_wielded_item()
item:take_item()
clicker:set_wielded_item(item)
end
mob_sound(self, "eat", nil, true)
-- increase health
self.health = self.health + 4
if self.health >= self.hp_max then
self.health = self.hp_max
if self.htimer < 1 then
self.htimer = 5
end
end
self.object:set_hp(self.health)
update_tag(self)
-- make children grow quicker
if self.child == true then
-- deduct 10% of the time to adulthood
self.hornytimer = self.hornytimer + ((CHILD_GROW_TIME - self.hornytimer) * 0.1)
return true
end
-- feed and tame
self.food = (self.food or 0) + 1
if self.food >= feed_count then
self.food = 0
if breed and self.hornytimer == 0 then
self.horny = true
end
if tame then
self.tamed = true
if not self.owner or self.owner == "" then
self.owner = clicker:get_player_name()
end
end
-- make sound when fed so many times
mob_sound(self, "random", true)
end
return true
end
return false
end
-- no damage to nodes explosion
function mobs:safe_boom(self, pos, strength)
minetest_sound_play(self.sounds and self.sounds.explode or "tnt_explode", {
pos = pos,
gain = 1.0,
max_hear_distance = self.sounds and self.sounds.distance or 32
}, true)
local radius = strength
entity_physics(pos, radius)
effect(pos, 32, "mcl_particles_smoke.png", radius * 3, radius * 5, radius, 1, 0)
end
-- make explosion with protection and tnt mod check
function mobs:boom(self, pos, strength, fire)
self.object:remove()
if mod_explosions then
if mobs_griefing and not minetest_is_protected(pos, "") then
mcl_explosions.explode(pos, strength, { drop_chance = 1.0, fire = fire }, self.object)
else
mobs:safe_boom(self, pos, strength)
end
else
mobs:safe_boom(self, pos, strength)
end
end
-- falling and fall damage
-- returns true if mob died
local falling = function(self, pos)
if self.fly and self.state ~= "die" then
return
end
if mcl_portals ~= nil then
if mcl_portals.nether_portal_cooloff(self.object) then
return false -- mob has teleported through Nether portal - it's 99% not falling
end
end
-- floating in water (or falling)
local v = self.object:get_velocity()
if v.y > 0 then
-- apply gravity when moving up
self.object:set_acceleration({
x = 0,
y = -10,
z = 0
})
elseif v.y <= 0 and v.y > self.fall_speed then
-- fall downwards at set speed
self.object:set_acceleration({
x = 0,
y = self.fall_speed,
z = 0
})
else
-- stop accelerating once max fall speed hit
self.object:set_acceleration({x = 0, y = 0, z = 0})
end
if minetest_registered_nodes[node_ok(pos).name].groups.lava then
if self.floats_on_lava == 1 then
self.object:set_acceleration({
x = 0,
y = -self.fall_speed / (max(1, v.y) ^ 2),
z = 0
})
end
end
-- in water then float up
if minetest_registered_nodes[node_ok(pos).name].groups.water then
if self.floats == 1 then
self.object:set_acceleration({
x = 0,
y = -self.fall_speed / (math_max(1, v.y) ^ 2),
z = 0
})
end
else
-- fall damage onto solid ground
if self.fall_damage == 1
and self.object:get_velocity().y == 0 then
local d = (self.old_y or 0) - self.object:get_pos().y
if d > 5 then
local add = minetest_get_item_group(self.standing_on, "fall_damage_add_percent")
local damage = d - 5
if add ~= 0 then
damage = damage + damage * (add/100)
end
damage = math_floor(damage)
if damage > 0 then
self.health = self.health - damage
effect(pos, 5, "mcl_particles_smoke.png", 1, 2, 2, nil)
if check_for_death(self, "fall", {type = "fall"}) then
return true
end
end
end
self.old_y = self.object:get_pos().y
end
end
end
local teleport = function(self, target)
if self.do_teleport then
if self.do_teleport(self, target) == false then
return
end
end
end
-- find someone to runaway from
local runaway_from = function(self)
if not self.runaway_from and self.state ~= "flop" then
return
end
local s = self.object:get_pos()
local p, sp, dist
local player, obj, min_player
local type, name = "", ""
local min_dist = self.view_range + 1
local objs = minetest_get_objects_inside_radius(s, self.view_range)
for n = 1, #objs do
if objs[n]:is_player() then
if mobs.invis[ objs[n]:get_player_name() ]
or self.owner == objs[n]:get_player_name()
or (not object_in_range(self, objs[n])) then
type = ""
else
player = objs[n]
type = "player"
name = "player"
end
else
obj = objs[n]:get_luaentity()
if obj then
player = obj.object
type = obj.type
name = obj.name or ""
end
end
-- find specific mob to runaway from
if name ~= "" and name ~= self.name
and specific_runaway(self.runaway_from, name) then
p = player:get_pos()
sp = s
-- aim higher to make looking up hills more realistic
p.y = p.y + 1
sp.y = sp.y + 1
dist = vector.distance(p, s)
-- choose closest player/mpb to runaway from
if dist < min_dist
and line_of_sight(self, sp, p, 2) == true then
min_dist = dist
min_player = player
end
end
end
if min_player then
local lp = player:get_pos()
local vec = {
x = lp.x - s.x,
y = lp.y - s.y,
z = lp.z - s.z
}
local yaw = (atan(vec.z / vec.x) + 3 * math_pi / 2) - self.rotate
if lp.x > s.x then
yaw = yaw + math_pi
end
yaw = set_yaw(self, yaw, 4)
self.state = "runaway"
self.runaway_timer = 3
self.following = nil
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
-- follow player if owner or holding item, if fish outta water then flop
local follow_flop = function(self)
-- find player to follow
if (self.follow ~= ""
or self.order == "follow")
and not self.following
and self.state ~= "attack"
and self.order ~= "sit"
and self.state ~= "runaway" then
local s = self.object:get_pos()
local players = minetest.get_connected_players()
for n = 1, #players do
if (object_in_range(self, players[n]))
and not mobs.invis[ players[n]:get_player_name() ] then
self.following = players[n]
break
end
end
end
if self.type == "npc"
and self.order == "follow"
and self.state ~= "attack"
and self.order ~= "sit"
and self.owner ~= "" then
-- npc stop following player if not owner
if self.following
and self.owner
and self.owner ~= self.following:get_player_name() then
self.following = nil
end
else
-- stop following player if not holding specific item,
-- mob is horny, fleeing or attacking
if self.following
and self.following:is_player()
and (follow_holding(self, self.following) == false or
self.horny or self.state == "runaway") then
self.following = nil
end
end
-- follow that thing
if self.following then
local s = self.object:get_pos()
local p
if self.following:is_player() then
p = self.following:get_pos()
elseif self.following.object then
p = self.following.object:get_pos()
end
if p then
local dist = vector.distance(p, s)
-- dont follow if out of range
if (not object_in_range(self, self.following)) then
self.following = nil
else
local vec = {
x = p.x - s.x,
z = p.z - s.z
}
local yaw = (atan(vec.z / vec.x) + math_pi / 2) - self.rotate
if p.x > s.x then yaw = yaw + math_pi end
set_yaw(self, yaw, 2.35)
-- anyone but standing npc's can move along
if dist > 3
and self.order ~= "stand" then
set_velocity(self, self.follow_velocity)
if self.walk_chance ~= 0 then
set_animation(self, "run")
end
else
set_velocity(self, 0)
set_animation(self, "stand")
end
return
end
end
end
-- swimmers flop when out of their element, and swim again when back in
if self.fly then
local s = self.object:get_pos()
if not flight_check(self, s) then
self.state = "flop"
self.object:set_acceleration({x = 0, y = DEFAULT_FALL_SPEED, z = 0})
local sdef = minetest_registered_nodes[self.standing_on]
-- Flop on ground
if sdef and sdef.walkable then
mob_sound(self, "flop")
self.object:set_velocity({
x = math_random(-FLOP_HOR_SPEED, FLOP_HOR_SPEED),
y = FLOP_HEIGHT,
z = math_random(-FLOP_HOR_SPEED, FLOP_HOR_SPEED),
})
end
set_animation(self, "stand", true)
return
elseif self.state == "flop" then
self.state = "stand"
self.object:set_acceleration({x = 0, y = 0, z = 0})
set_velocity(self, 0)
end
end
end
-- npc, find closest monster to attack
local npc_attack = function(self)
if self.type ~= "npc"
or not self.attacks_monsters
or self.state == "attack" then
return
end
local p, sp, obj, min_player
local s = self.object:get_pos()
local min_dist = self.view_range + 1
local objs = minetest_get_objects_inside_radius(s, self.view_range)
for n = 1, #objs do
obj = objs[n]:get_luaentity()
if obj and obj.type == "monster" then
p = obj.object:get_pos()
sp = s
local dist = vector.distance(p, s)
-- aim higher to make looking up hills more realistic
p.y = p.y + 1
sp.y = sp.y + 1
if dist < min_dist
and line_of_sight(self, sp, p, 2) == true then
min_dist = dist
min_player = obj.object
end
end
end
if min_player then
do_attack(self, min_player)
end
end
-- monster find someone to attack
local monster_attack = function(self)
if self.type ~= "monster"
or not damage_enabled
or minetest_is_creative_enabled("")
or self.passive
or self.state == "attack"
or day_docile(self) then
return
end
local s = self.object:get_pos()
local p, sp, dist
local player, obj, min_player
local type, name = "", ""
local min_dist = self.view_range + 1
local objs = minetest_get_objects_inside_radius(s, self.view_range)
for n = 1, #objs do
if objs[n]:is_player() then
if mobs.invis[ objs[n]:get_player_name() ] or (not object_in_range(self, objs[n])) then
type = ""
else
player = objs[n]
type = "player"
name = "player"
end
else
obj = objs[n]:get_luaentity()
if obj then
player = obj.object
type = obj.type
name = obj.name or ""
end
end
-- find specific mob to attack, failing that attack player/npc/animal
if specific_attack(self.specific_attack, name)
and (type == "player" or type == "npc"
or (type == "animal" and self.attack_animals == true)) then
p = player:get_pos()
sp = s
dist = vector.distance(p, s)
-- aim higher to make looking up hills more realistic
p.y = p.y + 1
sp.y = sp.y + 1
-- choose closest player to attack
if dist < min_dist
and line_of_sight(self, sp, p, 2) == true then
min_dist = dist
min_player = player
end
end
end
-- attack player
if min_player then
do_attack(self, min_player)
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
-- dogfight attack switch and counter function
local dogswitch = function(self, dtime)
-- switch mode not activated
if not self.dogshoot_switch
or not dtime then
return 0
end
self.dogshoot_count = self.dogshoot_count + dtime
if (self.dogshoot_switch == 1
and self.dogshoot_count > self.dogshoot_count_max)
or (self.dogshoot_switch == 2
and self.dogshoot_count > self.dogshoot_count2_max) then
self.dogshoot_count = 0
if self.dogshoot_switch == 1 then
self.dogshoot_switch = 2
else
self.dogshoot_switch = 1
end
end
return self.dogshoot_switch
end
-- path finding and smart mob routine by rnd, line_of_sight and other edits by Elkien3
local smart_mobs = function(self, s, p, dist, dtime)
local s1 = self.path.lastpos
local target_pos = self.attack:get_pos()
-- is it becoming stuck?
if math_abs(s1.x - s.x) + math_abs(s1.z - s.z) < .5 then
self.path.stuck_timer = self.path.stuck_timer + dtime
else
self.path.stuck_timer = 0
end
self.path.lastpos = {x = s.x, y = s.y, z = s.z}
local use_pathfind = false
local has_lineofsight = minetest_line_of_sight(
{x = s.x, y = (s.y) + .5, z = s.z},
{x = target_pos.x, y = (target_pos.y) + 1.5, z = target_pos.z}, .2)
-- im stuck, search for path
if not has_lineofsight then
if los_switcher == true then
use_pathfind = true
los_switcher = false
end -- cannot see target!
else
if los_switcher == false then
los_switcher = true
use_pathfind = false
minetest_after(1, function(self)
if not self.object:get_luaentity() then
return
end
if has_lineofsight then self.path.following = false end
end, self)
end -- can see target!
end
if (self.path.stuck_timer > stuck_timeout and not self.path.following) then
use_pathfind = true
self.path.stuck_timer = 0
minetest_after(1, function(self)
if not self.object:get_luaentity() then
return
end
if has_lineofsight then self.path.following = false end
end, self)
end
if (self.path.stuck_timer > stuck_path_timeout and self.path.following) then
use_pathfind = true
self.path.stuck_timer = 0
minetest_after(1, function(self)
if not self.object:get_luaentity() then
return
end
if has_lineofsight then self.path.following = false end
end, self)
end
if math_abs(vector.subtract(s,target_pos).y) > self.stepheight then
if height_switcher then
use_pathfind = true
height_switcher = false
end
else
if not height_switcher then
use_pathfind = false
height_switcher = true
end
end
if use_pathfind then
-- lets try find a path, first take care of positions
-- since pathfinder is very sensitive
local sheight = self.collisionbox[5] - self.collisionbox[2]
-- round position to center of node to avoid stuck in walls
-- also adjust height for player models!
s.x = math_floor(s.x + 0.5)
s.z = math_floor(s.z + 0.5)
local ssight, sground = minetest_line_of_sight(s, {
x = s.x, y = s.y - 4, z = s.z}, 1)
-- determine node above ground
if not ssight then
s.y = sground.y + 1
end
local p1 = self.attack:get_pos()
p1.x = math_floor(p1.x + 0.5)
p1.y = math_floor(p1.y + 0.5)
p1.z = math_floor(p1.z + 0.5)
local dropheight = 12
if self.fear_height ~= 0 then dropheight = self.fear_height end
local jumpheight = 0
if self.jump and self.jump_height >= 4 then
jumpheight = math.min(math.ceil(self.jump_height / 4), 4)
elseif self.stepheight > 0.5 then
jumpheight = 1
end
self.path.way = minetest_find_path(s, p1, 16, jumpheight, dropheight, "A*_noprefetch")
self.state = ""
do_attack(self, self.attack)
-- no path found, try something else
if not self.path.way then
self.path.following = false
-- lets make way by digging/building if not accessible
if self.pathfinding == 2 and mobs_griefing then
-- is player higher than mob?
if s.y < p1.y then
-- build upwards
if not minetest_is_protected(s, "") then
local ndef1 = minetest_registered_nodes[self.standing_in]
if ndef1 and (ndef1.buildable_to or ndef1.groups.liquid) then
minetest_set_node(s, {name = mobs.fallback_node})
end
end
local sheight = math.ceil(self.collisionbox[5]) + 1
-- assume mob is 2 blocks high so it digs above its head
s.y = s.y + sheight
-- remove one block above to make room to jump
if not minetest_is_protected(s, "") then
local node1 = node_ok(s, "air").name
local ndef1 = minetest_registered_nodes[node1]
if node1 ~= "air"
and node1 ~= "ignore"
and ndef1
and not ndef1.groups.level
and not ndef1.groups.unbreakable
and not ndef1.groups.liquid then
minetest_set_node(s, {name = "air"})
minetest_add_item(s, ItemStack(node1))
end
end
s.y = s.y - sheight
self.object:set_pos({x = s.x, y = s.y + 2, z = s.z})
else -- dig 2 blocks to make door toward player direction
local yaw1 = self.object:get_yaw() + math_pi / 2
local p1 = {
x = s.x + math_cos(yaw1),
y = s.y,
z = s.z + math_sin(yaw1)
}
if not minetest_is_protected(p1, "") then
local node1 = node_ok(p1, "air").name
local ndef1 = minetest_registered_nodes[node1]
if node1 ~= "air"
and node1 ~= "ignore"
and ndef1
and not ndef1.groups.level
and not ndef1.groups.unbreakable
and not ndef1.groups.liquid then
minetest_add_item(p1, ItemStack(node1))
minetest_set_node(p1, {name = "air"})
end
p1.y = p1.y + 1
node1 = node_ok(p1, "air").name
ndef1 = minetest_registered_nodes[node1]
if node1 ~= "air"
and node1 ~= "ignore"
and ndef1
and not ndef1.groups.level
and not ndef1.groups.unbreakable
and not ndef1.groups.liquid then
minetest_add_item(p1, ItemStack(node1))
minetest_set_node(p1, {name = "air"})
end
end
end
end
-- will try again in 2 seconds
self.path.stuck_timer = stuck_timeout - 2
elseif s.y < p1.y and (not self.fly) then
do_jump(self) --add jump to pathfinding
self.path.following = true
-- Yay, I found path!
-- TODO: Implement war_cry sound without being annoying
--mob_sound(self, "war_cry", true)
else
set_velocity(self, self.walk_velocity)
-- follow path now that it has it
self.path.following = true
end
end
end
local update_tag = function(self)
local tag
if mobs_debug then
tag = "nametag = '"..tostring(self.nametag).."'\n"..
"state = '"..tostring(self.state).."'\n"..
"order = '"..tostring(self.order).."'\n"..
"attack = "..tostring(self.attack).."\n"..
"health = "..tostring(self.health).."\n"..
"breath = "..tostring(self.breath).."\n"..
"gotten = "..tostring(self.gotten).."\n"..
"tamed = "..tostring(self.tamed).."\n"..
"horny = "..tostring(self.horny).."\n"..
"hornytimer = "..tostring(self.hornytimer).."\n"..
"runaway_timer = "..tostring(self.runaway_timer).."\n"..
"following = "..tostring(self.following)
else
tag = self.nametag
end
self.object:set_properties({
nametag = tag,
})
update_roll(self)
end
-- drop items
local item_drop = function(self, cooked, looting_level)
-- no drops if disabled by setting
if not mobs_drop_items then return end
looting_level = looting_level or 0
-- no drops for child mobs (except monster)
if (self.child and self.type ~= "monster") then
return
end
local obj, item, num
local pos = self.object:get_pos()
self.drops = self.drops or {} -- nil check
for n = 1, #self.drops do
local dropdef = self.drops[n]
local chance = 1 / dropdef.chance
local looting_type = dropdef.looting
if looting_level > 0 then
local chance_function = dropdef.looting_chance_function
if chance_function then
chance = chance_function(looting_level)
elseif looting_type == "rare" then
chance = chance + (dropdef.looting_factor or 0.01) * looting_level
end
end
local num = 0
local do_common_looting = (looting_level > 0 and looting_type == "common")
if math_random() < chance then
num = math_random(dropdef.min or 1, dropdef.max or 1)
elseif not dropdef.looting_ignore_chance then
do_common_looting = false
end
if do_common_looting then
num = num + math_floor(math_random(0, looting_level) + 0.5)
end
if num > 0 then
item = dropdef.name
-- cook items when true
if cooked then
local output = minetest_get_craft_result({
method = "cooking", width = 1, items = {item}})
if output and output.item and not output.item:is_empty() then
item = output.item:get_name()
end
end
-- add item if it exists
for x = 1, num do
obj = minetest_add_item(pos, ItemStack(item .. " " .. 1))
end
if obj and obj:get_luaentity() then
obj:set_velocity({
x = math_random(-10, 10) / 9,
y = 6,
z = math_random(-10, 10) / 9,
})
elseif obj then
obj:remove() -- item does not exist
end
end
end
self.drops = {}
end
-- check if mob is dead or only hurt
local check_for_death = function(self, cause, cmi_cause)
if self.state == "die" then
return true
end
-- has health actually changed?
if self.health == self.old_health and self.health > 0 then
return false
end
local damaged = self.health < self.old_health
self.old_health = self.health
-- still got some health?
if self.health > 0 then
-- make sure health isn't higher than max
if self.health > self.hp_max then
self.health = self.hp_max
end
-- play damage sound if health was reduced and make mob flash red.
if damaged then
add_texture_mod(self, "^[colorize:red:130")
minetest_after(.2, function(self)
if self and self.object then
remove_texture_mod(self, "^[colorize:red:130")
end
end, self)
mob_sound(self, "damage")
end
-- backup nametag so we can show health stats
if not self.nametag2 then
self.nametag2 = self.nametag or ""
end
if show_health
and (cmi_cause and cmi_cause.type == "punch") then
self.htimer = 2
self.nametag = "" .. self.health .. " / " .. self.hp_max
update_tag(self)
end
return false
end
mob_sound(self, "death")
local function death_handle(self)
-- dropped cooked item if mob died in fire or lava
if cause == "lava" or cause == "fire" then
item_drop(self, true, 0)
else
local wielditem = ItemStack()
if cause == "hit" then
local puncher = cmi_cause.puncher
if puncher then
wielditem = puncher:get_wielded_item()
end
end
local cooked = mcl_burning.is_burning(self.object) or mcl_enchanting.has_enchantment(wielditem, "fire_aspect")
local looting = mcl_enchanting.get_enchantment(wielditem, "looting")
item_drop(self, cooked, looting)
if mod_experience and ((not self.child) or self.type ~= "animal") and (minetest_get_us_time() - self.xp_timestamp <= 5000000) then
mcl_experience.throw_experience(self.object:get_pos(), math_random(self.xp_min, self.xp_max))
end
end
end
-- execute custom death function
if self.on_die then
local pos = self.object:get_pos()
local on_die_exit = self.on_die(self, pos, cmi_cause)
if on_die_exit ~= true then
death_handle(self)
end
if use_cmi then
cmi.notify_die(self.object, cmi_cause)
end
if on_die_exit == true then
self.state = "die"
mcl_burning.extinguish(self.object)
self.object:remove()
return true
end
end
local collisionbox
if self.collisionbox then
collisionbox = table.copy(self.collisionbox)
end
self.state = "die"
self.attack = nil
self.v_start = false
self.fall_speed = DEFAULT_FALL_SPEED
self.timer = 0
self.blinktimer = 0
remove_texture_mod(self, "^[colorize:#FF000040")
remove_texture_mod(self, "^[brighten")
self.passive = true
self.object:set_properties({
pointable = false,
collide_with_objects = false,
})
set_velocity(self, 0)
local acc = self.object:get_acceleration()
acc.x, acc.y, acc.z = 0, DEFAULT_FALL_SPEED, 0
self.object:set_acceleration(acc)
local length
-- default death function and die animation (if defined)
if self.instant_death then
length = 0
elseif self.animation
and self.animation.die_start
and self.animation.die_end then
local frames = self.animation.die_end - self.animation.die_start
local speed = self.animation.die_speed or 15
length = max(frames / speed, 0) + DEATH_DELAY
set_animation(self, "die")
else
local rot = self.object:get_rotation()
rot.z = math_pi/2
self.object:set_rotation(rot)
length = 1 + DEATH_DELAY
set_animation(self, "stand", true)
end
-- Remove body after a few seconds and drop stuff
local kill = function(self)
if not self.object:get_luaentity() then
return
end
if use_cmi then
cmi.notify_die(self.object, cmi_cause)
end
death_handle(self)
local dpos = self.object:get_pos()
local cbox = self.collisionbox
local yaw = self.object:get_rotation().y
mcl_burning.extinguish(self.object)
self.object:remove()
mobs.death_effect(dpos, yaw, cbox, not self.instant_death)
end
if length <= 0 then
kill(self)
else
minetest_after(length, kill, self)
end
return true
end
local damage_effect = function(self, damage)
-- damage particles
if (not disable_blood) and damage > 0 then
local amount_large = math_floor(damage / 2)
local amount_small = damage % 2
local pos = self.object:get_pos()
pos.y = pos.y + (self.collisionbox[5] - self.collisionbox[2]) * .5
local texture = "mobs_blood.png"
-- full heart damage (one particle for each 2 HP damage)
if amount_large > 0 then
effect(pos, amount_large, texture, 2, 2, 1.75, 0, nil, true)
end
-- half heart damage (one additional particle if damage is an odd number)
if amount_small > 0 then
-- TODO: Use "half heart"
effect(pos, amount_small, texture, 1, 1, 1.75, 0, nil, true)
end
end
end
-- custom particle effects
local effect = function(pos, amount, texture, min_size, max_size, radius, gravity, glow, go_down)
radius = radius or 2
min_size = min_size or 0.5
max_size = max_size or 1
gravity = gravity or -10
glow = glow or 0
go_down = go_down or false
local ym
if go_down then
ym = 0
else
ym = -radius
end
minetest_add_particlespawner({
amount = amount,
time = 0.25,
minpos = pos,
maxpos = pos,
minvel = {x = -radius, y = ym, z = -radius},
maxvel = {x = radius, y = radius, z = radius},
minacc = {x = 0, y = gravity, z = 0},
maxacc = {x = 0, y = gravity, z = 0},
minexptime = 0.1,
maxexptime = 1,
minsize = min_size,
maxsize = max_size,
texture = texture,
glow = glow,
})
end
-- are we flying in what we are suppose to? (taikedz)
local flight_check = function(self)
local nod = self.standing_in
local def = minetest_registered_nodes[nod]
if not def then return false end -- nil check
local fly_in
if type(self.fly_in) == "string" then
fly_in = { self.fly_in }
elseif type(self.fly_in) == "table" then
fly_in = self.fly_in
else
return false
end
for _,checknode in pairs(fly_in) do
if nod == checknode then
return true
elseif checknode == "__airlike" and def.walkable == false and
(def.liquidtype == "none" or minetest_get_item_group(nod, "fake_liquid") == 1) then
return true
end
end
return false
end
-- check line of sight (BrunoMine)
local line_of_sight = function(self, pos1, pos2, stepsize)
stepsize = stepsize or 1
local s, pos = minetest_line_of_sight(pos1, pos2, stepsize)
-- normal walking and flying mobs can see you through air
if s == true then
return true
end
-- New pos1 to be analyzed
local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
local r, pos = minetest_line_of_sight(npos1, pos2, stepsize)
-- Checks the return
if r == true then return true end
-- Nodename found
local nn = minetest_get_node(pos).name
-- Target Distance (td) to travel
local td = vector.distance(pos1, pos2)
-- Actual Distance (ad) traveled
local ad = 0
-- It continues to advance in the line of sight in search of a real
-- obstruction which counts as 'normal' nodebox.
while minetest_registered_nodes[nn]
and minetest_registered_nodes[nn].walkable == false do
-- Check if you can still move forward
if td < ad + stepsize then
return true -- Reached the target
end
-- Moves the analyzed pos
local d = vector.distance(pos1, pos2)
npos1.x = ((pos2.x - pos1.x) / d * stepsize) + pos1.x
npos1.y = ((pos2.y - pos1.y) / d * stepsize) + pos1.y
npos1.z = ((pos2.z - pos1.z) / d * stepsize) + pos1.z
-- NaN checks
if d == 0
or npos1.x ~= npos1.x
or npos1.y ~= npos1.y
or npos1.z ~= npos1.z then
return false
end
ad = ad + stepsize
-- scan again
r, pos = minetest_line_of_sight(npos1, pos2, stepsize)
if r == true then return true end
-- New Nodename found
nn = minetest_get_node(pos).name
end
return false
end
-- Returns true if node is a water hazard
local is_node_waterhazard = function(self, nodename)
local nn = nodename
if self.water_damage > 0 then
if minetest_get_item_group(nn, "water") ~= 0 then
return true
end
end
if minetest_registered_nodes[nn] and minetest_registered_nodes[nn].drowning and minetest_registered_nodes[nn].drowning > 0 then
if self.breath_max ~= -1 then
-- check if the mob is water-breathing _and_ the block is water; only return true if neither is the case
-- this will prevent water-breathing mobs to classify water or e.g. sand below them as dangerous
if not self.breathes_in_water and minetest_get_item_group(nn, "water") ~= 0 then
return true
end
end
end
return false
end
-- Returns true is node can deal damage to self
local is_node_dangerous = function(self, nodename)
local nn = nodename
if self.lava_damage > 0 then
if minetest_get_item_group(nn, "lava") ~= 0 then
return true
end
end
if self.fire_damage > 0 then
if minetest_get_item_group(nn, "fire") ~= 0 then
return true
end
end
if minetest_registered_nodes[nn] and minetest_registered_nodes[nn].damage_per_second and minetest_registered_nodes[nn].damage_per_second > 0 then
return true
end
return false
end
local add_texture_mod = function(self, mod)
local full_mod = ""
local already_added = false
for i=1, #self.texture_mods do
if mod == self.texture_mods[i] then
already_added = true
end
full_mod = full_mod .. self.texture_mods[i]
end
if not already_added then
full_mod = full_mod .. mod
table.insert(self.texture_mods, mod)
end
self.object:set_texture_mod(full_mod)
end
local remove_texture_mod = function(self, mod)
local full_mod = ""
local remove = {}
for i=1, #self.texture_mods do
if self.texture_mods[i] ~= mod then
full_mod = full_mod .. self.texture_mods[i]
else
table.insert(remove, i)
end
end
for i=#remove, 1 do
table.remove(self.texture_mods, remove[i])
end
self.object:set_texture_mod(full_mod)
end