Fix crash in mcl_mobs
[MineClone/MineClone2.git] / mods / ENTITIES / mcl_mobs / api.lua
blob648fef2b4bc4ac7cae68fe603e6e03173e0f005f
2 -- API for Mobs Redo: MineClone 2 Edition (MRM)
4 mobs = {}
5 mobs.mod = "mrm"
6 mobs.version = "20180531" -- don't rely too much on this, rarely updated, if ever
8 local MAX_MOB_NAME_LENGTH = 30
10 -- Localize
11 local S = minetest.get_translator("mcl_mobs")
13 -- CMI support check
14 local use_cmi = minetest.global_exists("cmi")
17 -- Invisibility mod check
18 mobs.invis = {}
19 if minetest.global_exists("invisibility") then
20 mobs.invis = invisibility
21 end
24 -- creative check
25 local creative_mode_cache = minetest.settings:get_bool("creative_mode")
26 function mobs.is_creative(name)
27 return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
28 end
31 -- localize math functions
32 local pi = math.pi
33 local sin = math.sin
34 local cos = math.cos
35 local abs = math.abs
36 local min = math.min
37 local max = math.max
38 local atann = math.atan
39 local random = math.random
40 local floor = math.floor
41 local atan = function(x)
42 if not x or x ~= x then
43 return 0
44 else
45 return atann(x)
46 end
47 end
50 -- Load settings
51 local damage_enabled = minetest.settings:get_bool("enable_damage")
52 local mobs_spawn = minetest.settings:get_bool("mobs_spawn", true) ~= false
54 local disable_blood = minetest.settings:get_bool("mobs_disable_blood")
55 local mobs_drop_items = minetest.settings:get_bool("mobs_drop_items") ~= false
56 local mobs_griefing = minetest.settings:get_bool("mobs_griefing") ~= false
57 local creative = minetest.settings:get_bool("creative_mode")
58 local spawn_protected = minetest.settings:get_bool("mobs_spawn_protected") ~= false
59 local remove_far = false
60 local difficulty = tonumber(minetest.settings:get("mob_difficulty")) or 1.0
61 local show_health = false
62 local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 99)
63 local mobs_spawn_chance = tonumber(minetest.settings:get("mobs_spawn_chance") or 2.5)
65 -- Peaceful mode message so players will know there are no monsters
66 if minetest.settings:get_bool("only_peaceful_mobs", false) then
67 minetest.register_on_joinplayer(function(player)
68 minetest.chat_send_player(player:get_player_name(),
69 S("Peaceful mode active! No monsters will spawn."))
70 end)
71 end
73 -- calculate aoc range for mob count
74 local aosrb = tonumber(minetest.settings:get("active_object_send_range_blocks"))
75 local abr = tonumber(minetest.settings:get("active_block_range"))
76 local aoc_range = max(aosrb, abr) * 16
78 -- pathfinding settings
79 local enable_pathfinding = true
80 local stuck_timeout = 3 -- how long before mob gets stuck in place and starts searching
81 local stuck_path_timeout = 10 -- how long will mob follow path before giving up
83 -- default nodes
84 local node_ice = "mcl_core:ice"
85 local node_snowblock = "mcl_core:snowblock"
86 local node_snow = "mcl_core:snow"
87 mobs.fallback_node = minetest.registered_aliases["mapgen_dirt"] or "mcl_core:dirt"
89 local mod_weather = minetest.get_modpath("mcl_weather") ~= nil
90 local mod_tnt = minetest.get_modpath("mcl_tnt") ~= nil
91 local mod_mobspawners = minetest.get_modpath("mcl_mobspawners") ~= nil
92 local mod_hunger = minetest.get_modpath("mcl_hunger") ~= nil
93 local mod_worlds = minetest.get_modpath("mcl_worlds") ~= nil
94 local mod_armor = minetest.get_modpath("mcl_armor") ~= nil
96 -- play sound
97 local mob_sound = function(self, soundname, is_opinion, fixed_pitch)
99 local soundinfo
100 if self.sounds_child and self.child then
101 soundinfo = self.sounds_child
102 elseif self.sounds then
103 soundinfo = self.sounds
105 if not soundinfo then
106 return
108 local sound = soundinfo[soundname]
109 if sound then
110 if is_opinion and self.opinion_sound_cooloff > 0 then
111 return
113 local pitch
114 if not fixed_pitch then
115 local base_pitch = soundinfo.base_pitch
116 if not base_pitch then
117 base_pitch = 1
119 if self.child and (not self.sounds_child) then
120 -- Children have higher pitch
121 pitch = base_pitch * 1.5
122 else
123 pitch = base_pitch
125 -- randomize the pitch a bit
126 pitch = pitch + math.random(-10, 10) * 0.005
128 minetest.sound_play(sound, {
129 object = self.object,
130 gain = 1.0,
131 max_hear_distance = self.sounds.distance,
132 pitch = pitch,
134 self.opinion_sound_cooloff = 1
138 -- Reeturn true if object is in view_range
139 local function object_in_range(self, object)
140 if not object then
141 return false
143 local factor
144 -- Apply view range reduction for special player armor
145 if object:is_player() and mod_armor then
146 factor = armor:get_mob_view_range_factor(object, self.name)
148 -- Distance check
149 local dist
150 if factor and factor == 0 then
151 return false
152 elseif factor then
153 dist = self.view_range * factor
154 else
155 dist = self.view_range
157 if vector.distance(self.object:get_pos(), object:get_pos()) > dist then
158 return false
159 else
160 return true
164 -- attack player/mob
165 local do_attack = function(self, player)
167 if self.state == "attack" then
168 return
171 self.attack = player
172 self.state = "attack"
174 -- TODO: Implement war_cry sound without being annoying
175 --if random(0, 100) < 90 then
176 --mob_sound(self, "war_cry", true)
177 --end
181 -- move mob in facing direction
182 local set_velocity = function(self, v)
184 -- do not move if mob has been ordered to stay
185 if self.order == "stand" then
186 self.object:set_velocity({x = 0, y = 0, z = 0})
187 return
190 local yaw = (self.object:get_yaw() or 0) + self.rotate
192 self.object:set_velocity({
193 x = sin(yaw) * -v,
194 y = self.object:get_velocity().y,
195 z = cos(yaw) * v
200 -- calculate mob velocity
201 local get_velocity = function(self)
203 local v = self.object:get_velocity()
205 return (v.x * v.x + v.z * v.z) ^ 0.5
209 -- set and return valid yaw
210 local set_yaw = function(self, yaw, delay)
212 if not yaw or yaw ~= yaw then
213 yaw = 0
216 delay = delay or 0
218 if delay == 0 then
219 self.object:set_yaw(yaw)
220 return yaw
223 self.target_yaw = yaw
224 self.delay = delay
226 return self.target_yaw
229 -- global function to set mob yaw
230 function mobs:yaw(self, yaw, delay)
231 set_yaw(self, yaw, delay)
234 local add_texture_mod = function(self, mod)
235 local full_mod = ""
236 local already_added = false
237 for i=1, #self.texture_mods do
238 if mod == self.texture_mods[i] then
239 already_added = true
241 full_mod = full_mod .. self.texture_mods[i]
243 if not already_added then
244 full_mod = full_mod .. mod
245 table.insert(self.texture_mods, mod)
247 self.object:set_texture_mod(full_mod)
249 local remove_texture_mod = function(self, mod)
250 local full_mod = ""
251 local remove = {}
252 for i=1, #self.texture_mods do
253 if self.texture_mods[i] ~= mod then
254 full_mod = full_mod .. self.texture_mods[i]
255 else
256 table.insert(remove, i)
259 for i=#remove, 1 do
260 table.remove(self.texture_mods, remove[i])
262 self.object:set_texture_mod(full_mod)
265 -- set defined animation
266 local set_animation = function(self, anim)
268 if not self.animation
269 or not anim then return end
271 self.animation.current = self.animation.current or ""
273 if anim == self.animation.current
274 or not self.animation[anim .. "_start"]
275 or not self.animation[anim .. "_end"] then
276 return
279 self.animation.current = anim
281 self.object:set_animation({
282 x = self.animation[anim .. "_start"],
283 y = self.animation[anim .. "_end"]},
284 self.animation[anim .. "_speed"] or self.animation.speed_normal or 15,
285 0, self.animation[anim .. "_loop"] ~= false)
289 -- above function exported for mount.lua
290 function mobs:set_animation(self, anim)
291 set_animation(self, anim)
294 -- Returns true is node can deal damage to self
295 local is_node_dangerous = function(self, nodename)
296 local nn = nodename
297 if self.water_damage > 0 then
298 if minetest.get_item_group(nn, "water") ~= 0 then
299 return true
302 if self.lava_damage > 0 then
303 if minetest.get_item_group(nn, "lava") ~= 0 then
304 return true
307 if self.fire_damage > 0 then
308 if minetest.get_item_group(nn, "fire") ~= 0 then
309 return true
312 if minetest.registered_nodes[nn].drowning > 0 then
313 if self.breath_max ~= -1 then
314 return true
317 if minetest.registered_nodes[nn].damage_per_second > 0 then
318 return true
320 return false
324 -- check line of sight (BrunoMine)
325 local line_of_sight = function(self, pos1, pos2, stepsize)
327 stepsize = stepsize or 1
329 local s, pos = minetest.line_of_sight(pos1, pos2, stepsize)
331 -- normal walking and flying mobs can see you through air
332 if s == true then
333 return true
336 -- New pos1 to be analyzed
337 local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
339 local r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
341 -- Checks the return
342 if r == true then return true end
344 -- Nodename found
345 local nn = minetest.get_node(pos).name
347 -- Target Distance (td) to travel
348 local td = vector.distance(pos1, pos2)
350 -- Actual Distance (ad) traveled
351 local ad = 0
353 -- It continues to advance in the line of sight in search of a real
354 -- obstruction which counts as 'normal' nodebox.
355 while minetest.registered_nodes[nn]
356 and minetest.registered_nodes[nn].walkable == false do
358 -- Check if you can still move forward
359 if td < ad + stepsize then
360 return true -- Reached the target
363 -- Moves the analyzed pos
364 local d = vector.distance(pos1, pos2)
366 npos1.x = ((pos2.x - pos1.x) / d * stepsize) + pos1.x
367 npos1.y = ((pos2.y - pos1.y) / d * stepsize) + pos1.y
368 npos1.z = ((pos2.z - pos1.z) / d * stepsize) + pos1.z
370 -- NaN checks
371 if d == 0
372 or npos1.x ~= npos1.x
373 or npos1.y ~= npos1.y
374 or npos1.z ~= npos1.z then
375 return false
378 ad = ad + stepsize
380 -- scan again
381 r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
383 if r == true then return true end
385 -- New Nodename found
386 nn = minetest.get_node(pos).name
390 return false
394 -- are we flying in what we are suppose to? (taikedz)
395 local flight_check = function(self)
397 local nod = self.standing_in
398 local def = minetest.registered_nodes[nod]
400 if not def then return false end -- nil check
402 local fly_in
403 if type(self.fly_in) == "string" then
404 fly_in = { self.fly_in }
405 elseif type(self.fly_in) == "table" then
406 fly_in = self.fly_in
407 else
408 return false
411 for _,checknode in pairs(fly_in) do
412 if nod == checknode then
413 return true
414 elseif checknode == "__airlike" and def.walkable == false and
415 (def.liquidtype == "none" or minetest.get_item_group(nod, "fake_liquid") == 1) then
416 return true
420 return false
424 -- custom particle effects
425 local effect = function(pos, amount, texture, min_size, max_size, radius, gravity, glow, go_down)
427 radius = radius or 2
428 min_size = min_size or 0.5
429 max_size = max_size or 1
430 gravity = gravity or -10
431 glow = glow or 0
432 go_down = go_down or false
434 local ym
435 if go_down then
436 ym = 0
437 else
438 ym = -radius
441 minetest.add_particlespawner({
442 amount = amount,
443 time = 0.25,
444 minpos = pos,
445 maxpos = pos,
446 minvel = {x = -radius, y = ym, z = -radius},
447 maxvel = {x = radius, y = radius, z = radius},
448 minacc = {x = 0, y = gravity, z = 0},
449 maxacc = {x = 0, y = gravity, z = 0},
450 minexptime = 0.1,
451 maxexptime = 1,
452 minsize = min_size,
453 maxsize = max_size,
454 texture = texture,
455 glow = glow,
459 local damage_effect = function(self, damage)
460 -- damage particles
461 if (not disable_blood) and damage > 0 then
463 local amount_large = math.floor(damage / 2)
464 local amount_small = damage % 2
466 local pos = self.object:get_pos()
468 pos.y = pos.y + (self.collisionbox[5] - self.collisionbox[2]) * .5
470 local texture = "mobs_blood.png"
471 -- full heart damage (one particle for each 2 HP damage)
472 if amount_large > 0 then
473 effect(pos, amount_large, texture, 2, 2, 1.75, 0, nil, true)
475 -- half heart damage (one additional particle if damage is an odd number)
476 if amount_small > 0 then
477 -- TODO: Use "half heart"
478 effect(pos, amount_small, texture, 1, 1, 1.75, 0, nil, true)
483 local update_tag = function(self)
484 self.object:set_properties({
485 nametag = self.nametag,
491 -- drop items
492 local item_drop = function(self, cooked)
494 -- no drops if disabled by setting
495 if not mobs_drop_items then return end
497 -- no drops for child mobs (except monster)
498 if (self.child and self.type ~= "monster") then
499 return
502 local obj, item, num
503 local pos = self.object:get_pos()
505 self.drops = self.drops or {} -- nil check
507 for n = 1, #self.drops do
509 if random(1, self.drops[n].chance) == 1 then
511 num = random(self.drops[n].min or 1, self.drops[n].max or 1)
512 item = self.drops[n].name
514 -- cook items when true
515 if cooked then
517 local output = minetest.get_craft_result({
518 method = "cooking", width = 1, items = {item}})
520 if output and output.item and not output.item:is_empty() then
521 item = output.item:get_name()
525 -- add item if it exists
526 obj = minetest.add_item(pos, ItemStack(item .. " " .. num))
528 if obj and obj:get_luaentity() then
530 obj:set_velocity({
531 x = random(-10, 10) / 9,
532 y = 6,
533 z = random(-10, 10) / 9,
535 elseif obj then
536 obj:remove() -- item does not exist
541 self.drops = {}
545 -- check if mob is dead or only hurt
546 local check_for_death = function(self, cause, cmi_cause)
548 -- has health actually changed?
549 if self.health == self.old_health and self.health > 0 then
550 return
553 local damaged = self.health < self.old_health
554 self.old_health = self.health
556 -- still got some health?
557 if self.health > 0 then
559 -- make sure health isn't higher than max
560 if self.health > self.hp_max then
561 self.health = self.hp_max
564 -- play damage sound if health was reduced and make mob flash red.
565 if damaged then
566 add_texture_mod(self, "^[colorize:#FF000040")
567 minetest.after(.2, function(self)
568 if self and self.object then
569 remove_texture_mod(self, "^[colorize:#FF000040")
571 end, self)
572 mob_sound(self, "damage")
575 -- backup nametag so we can show health stats
576 if not self.nametag2 then
577 self.nametag2 = self.nametag or ""
580 if show_health
581 and (cmi_cause and cmi_cause.type == "punch") then
583 self.htimer = 2
584 self.nametag = "♥ " .. self.health .. " / " .. self.hp_max
586 update_tag(self)
589 return false
592 -- dropped cooked item if mob died in lava
593 if cause == "lava" then
594 item_drop(self, true)
595 else
596 item_drop(self, nil)
599 mob_sound(self, "death")
601 local pos = self.object:get_pos()
603 -- execute custom death function
604 if self.on_die then
606 self.on_die(self, pos)
608 if use_cmi then
609 cmi.notify_die(self.object, cmi_cause)
612 self.object:remove()
614 return true
617 -- default death function and die animation (if defined)
618 if self.animation
619 and self.animation.die_start
620 and self.animation.die_end then
622 local frames = self.animation.die_end - self.animation.die_start
623 local speed = self.animation.die_speed or 15
624 local length = max(frames / speed, 0)
626 self.attack = nil
627 self.v_start = false
628 self.timer = 0
629 self.blinktimer = 0
630 self.passive = true
631 self.state = "die"
632 set_velocity(self, 0)
633 set_animation(self, "die")
635 minetest.after(length, function(self)
636 if not self.object:get_luaentity() then
637 return
639 if use_cmi then
640 cmi.notify_die(self.object, cmi_cause)
643 self.object:remove()
644 end, self)
645 else
647 if use_cmi then
648 cmi.notify_die(self.object, cmi_cause)
651 self.object:remove()
654 effect(pos, 20, "tnt_smoke.png")
656 return true
660 -- check if within physical map limits (-30911 to 30927)
661 local within_limits = function(pos, radius)
663 if (pos.x - radius) > -30913
664 and (pos.x + radius) < 30928
665 and (pos.y - radius) > -30913
666 and (pos.y + radius) < 30928
667 and (pos.z - radius) > -30913
668 and (pos.z + radius) < 30928 then
669 return true -- within limits
672 return false -- beyond limits
676 -- is mob facing a cliff or danger
677 local is_at_cliff_or_danger = function(self)
679 if self.fear_height == 0 then -- 0 for no falling protection!
680 return false
683 local yaw = self.object:get_yaw()
684 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
685 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
686 local pos = self.object:get_pos()
687 local ypos = pos.y + self.collisionbox[2] -- just above floor
689 local free_fall, blocker = minetest.line_of_sight(
690 {x = pos.x + dir_x, y = ypos, z = pos.z + dir_z},
691 {x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z})
692 if free_fall then
693 return true
694 else
695 local bnode = minetest.get_node(blocker)
696 local danger = is_node_dangerous(self, bnode.name)
697 if danger then
698 return true
699 else
700 local def = minetest.registered_nodes[bnode.name]
701 return (not def and def.walkable)
705 return false
709 -- get node but use fallback for nil or unknown
710 local node_ok = function(pos, fallback)
712 fallback = fallback or mobs.fallback_node
714 local node = minetest.get_node_or_nil(pos)
716 if node and minetest.registered_nodes[node.name] then
717 return node
720 return minetest.registered_nodes[fallback]
724 -- environmental damage (water, lava, fire, light etc.)
725 local do_env_damage = function(self)
727 -- feed/tame text timer (so mob 'full' messages dont spam chat)
728 if self.htimer > 0 then
729 self.htimer = self.htimer - 1
732 -- reset nametag after showing health stats
733 if self.htimer < 1 and self.nametag2 then
735 self.nametag = self.nametag2
736 self.nametag2 = nil
738 update_tag(self)
741 local pos = self.object:get_pos()
743 self.time_of_day = minetest.get_timeofday()
745 -- remove mob if beyond map limits
746 if not within_limits(pos, 0) then
747 self.object:remove()
748 return
752 local deal_light_damage = function(self, pos, damage)
753 if not (mod_weather and (mcl_weather.rain.raining or mcl_weather.state == "snow") and mcl_weather.is_outdoor(pos)) then
754 self.health = self.health - damage
756 effect(pos, 5, "tnt_smoke.png")
758 if check_for_death(self, "light", {type = "light"}) then return end
762 -- bright light harms mob
763 if self.light_damage ~= 0 and (minetest.get_node_light(pos) or 0) > 12 then
764 deal_light_damage(self, pos, self.light_damage)
766 local _, dim = nil, "overworld"
767 if mod_worlds then
768 _, dim = mcl_worlds.y_to_layer(pos.y)
770 if self.sunlight_damage ~= 0 and (minetest.get_node_light(pos) or 0) >= minetest.LIGHT_MAX and dim == "overworld" then
771 deal_light_damage(self, pos, self.sunlight_damage)
774 local y_level = self.collisionbox[2]
776 if self.child then
777 y_level = self.collisionbox[2] * 0.5
780 -- what is mob standing in?
781 pos.y = pos.y + y_level + 0.25 -- foot level
782 local pos2 = {x=pos.x, y=pos.y-1, z=pos.z}
783 self.standing_in = node_ok(pos, "air").name
784 self.standing_on = node_ok(pos2, "air").name
786 -- don't fall when on ignore, just stand still
787 if self.standing_in == "ignore" then
788 self.object:set_velocity({x = 0, y = 0, z = 0})
791 local nodef = minetest.registered_nodes[self.standing_in]
793 -- rain
794 if self.rain_damage > 0 and mod_weather then
795 if mcl_weather.rain.raining and mcl_weather.is_outdoor(pos) then
797 self.health = self.health - self.rain_damage
799 if check_for_death(self, "rain", {type = "environment",
800 pos = pos, node = self.standing_in}) then return end
804 pos.y = pos.y + 1 -- for particle effect position
806 -- water damage
807 if self.water_damage > 0
808 and nodef.groups.water then
810 if self.water_damage ~= 0 then
812 self.health = self.health - self.water_damage
814 effect(pos, 5, "tnt_smoke.png", nil, nil, 1, nil)
816 if check_for_death(self, "water", {type = "environment",
817 pos = pos, node = self.standing_in}) then return end
820 -- lava damage
821 elseif self.lava_damage > 0
822 and (nodef.groups.lava) then
824 if self.lava_damage ~= 0 then
826 self.health = self.health - self.lava_damage
828 effect(pos, 5, "fire_basic_flame.png", nil, nil, 1, nil)
830 if check_for_death(self, "lava", {type = "environment",
831 pos = pos, node = self.standing_in}) then return end
834 -- fire damage
835 elseif self.fire_damage > 0
836 and (nodef.groups.fire) then
838 if self.fire_damage ~= 0 then
840 self.health = self.health - self.fire_damage
842 effect(pos, 5, "fire_basic_flame.png", nil, nil, 1, nil)
844 if check_for_death(self, "fire", {type = "environment",
845 pos = pos, node = self.standing_in}) then return end
848 -- damage_per_second node check
849 elseif nodef.damage_per_second ~= 0 then
851 self.health = self.health - nodef.damage_per_second
853 effect(pos, 5, "tnt_smoke.png")
855 if check_for_death(self, "dps", {type = "environment",
856 pos = pos, node = self.standing_in}) then return end
859 -- Drowning damage
860 if self.breath_max ~= -1 then
861 local drowning = false
862 if self.breathes_in_water then
863 if minetest.get_item_group(self.standing_in, "water") == 0 then
864 drowning = true
866 elseif nodef.drowning > 0 then
867 drowning = true
869 if drowning then
871 self.breath = math.max(0, self.breath - 1)
873 effect(pos, 2, "bubble.png", nil, nil, 1, nil)
874 if self.breath <= 0 then
875 local dmg
876 if nodef.drowning > 0 then
877 dmg = nodef.drowning
878 else
879 dmg = 4
881 damage_effect(self, dmg)
882 self.health = self.health - dmg
884 if check_for_death(self, "drowning", {type = "environment",
885 pos = pos, node = self.standing_in}) then return end
886 else
887 self.breath = math.min(self.breath_max, self.breath + 1)
891 --- suffocation inside solid node
892 -- FIXME: Redundant with mcl_playerplus
893 if (self.suffocation == true)
894 and (nodef.walkable == nil or nodef.walkable == true)
895 and (nodef.collision_box == nil or nodef.collision_box.type == "regular")
896 and (nodef.node_box == nil or nodef.node_box.type == "regular")
897 and (nodef.groups.disable_suffocation ~= 1)
898 and (nodef.groups.opaque == 1) then
900 -- 2 damage per second
901 -- TODO: Deal this damage once every 1/2 second
902 self.health = self.health - 2
904 if check_for_death(self, "suffocation", {type = "environment",
905 pos = pos, node = self.standing_in}) then return end
908 check_for_death(self, "", {type = "unknown"})
912 -- jump if facing a solid node (not fences or gates)
913 local do_jump = function(self)
915 if not self.jump
916 or self.jump_height == 0
917 or self.fly
918 or (self.child and self.type ~= "monster")
919 or self.order == "stand" then
920 return false
923 self.facing_fence = false
925 -- something stopping us while moving?
926 if self.state ~= "stand"
927 and get_velocity(self) > 0.5
928 and self.object:get_velocity().y ~= 0 then
929 return false
932 local pos = self.object:get_pos()
933 local yaw = self.object:get_yaw()
935 -- what is mob standing on?
936 pos.y = pos.y + self.collisionbox[2] - 0.2
938 local nod = node_ok(pos)
940 if minetest.registered_nodes[nod.name].walkable == false then
941 return false
944 -- where is front
945 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
946 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
948 -- what is in front of mob?
949 nod = node_ok({
950 x = pos.x + dir_x,
951 y = pos.y + 0.5,
952 z = pos.z + dir_z
955 -- this is used to detect if there's a block on top of the block in front of the mob.
956 -- If there is, there is no point in jumping as we won't manage.
957 local nodTop = node_ok({
958 x = pos.x + dir_x,
959 y = pos.y + 1.5,
960 z = pos.z + dir_z
961 }, "air")
963 -- we don't attempt to jump if there's a stack of blocks blocking
964 if minetest.registered_nodes[nodTop.name] == true then
965 return false
968 -- thin blocks that do not need to be jumped
969 if nod.name == node_snow then
970 return false
973 if self.walk_chance == 0
974 or minetest.registered_items[nod.name].walkable then
976 if minetest.get_item_group(nod.name, "fence") == 0
977 and minetest.get_item_group(nod.name, "fence_gate") == 0
978 and minetest.get_item_group(nod.name, "wall") == 0 then
980 local v = self.object:get_velocity()
982 v.y = self.jump_height
984 set_animation(self, "jump") -- only when defined
986 self.object:set_velocity(v)
988 -- when in air move forward
989 minetest.after(0.3, function(self, v)
990 if not self.object or not self.object:get_luaentity() then
991 return
993 self.object:set_acceleration({
994 x = v.x * 2,
995 y = 0,
996 z = v.z * 2,
998 end, self, v)
1000 if self.jump_sound_cooloff <= 0 then
1001 mob_sound(self, "jump")
1002 self.jump_sound_cooloff = 0.5
1004 else
1005 self.facing_fence = true
1008 -- if we jumped against a block/wall 4 times then turn
1009 if self.object:get_velocity().x ~= 0
1010 and self.object:get_velocity().z ~= 0 then
1012 self.jump_count = (self.jump_count or 0) + 1
1014 if self.jump_count == 4 then
1016 local yaw = self.object:get_yaw() or 0
1018 yaw = set_yaw(self, yaw + 1.35, 8)
1020 self.jump_count = 0
1024 return true
1027 return false
1031 -- blast damage to entities nearby (modified from TNT mod)
1032 local entity_physics = function(pos, radius)
1034 radius = radius * 2
1036 local objs = minetest.get_objects_inside_radius(pos, radius)
1037 local obj_pos, dist
1039 for n = 1, #objs do
1041 obj_pos = objs[n]:get_pos()
1043 dist = vector.distance(pos, obj_pos)
1044 if dist < 1 then dist = 1 end
1046 local damage = floor((4 / dist) * radius)
1047 local ent = objs[n]:get_luaentity()
1049 -- punches work on entities AND players
1050 objs[n]:punch(objs[n], 1.0, {
1051 full_punch_interval = 1.0,
1052 damage_groups = {fleshy = damage},
1053 }, pos)
1058 -- should mob follow what I'm holding ?
1059 local follow_holding = function(self, clicker)
1061 if mobs.invis[clicker:get_player_name()] then
1062 return false
1065 local item = clicker:get_wielded_item()
1066 local t = type(self.follow)
1068 -- single item
1069 if t == "string"
1070 and item:get_name() == self.follow then
1071 return true
1073 -- multiple items
1074 elseif t == "table" then
1076 for no = 1, #self.follow do
1078 if self.follow[no] == item:get_name() then
1079 return true
1084 return false
1088 -- find two animals of same type and breed if nearby and horny
1089 local breed = function(self)
1091 -- child takes 240 seconds before growing into adult
1092 if self.child == true then
1094 self.hornytimer = self.hornytimer + 1
1096 if self.hornytimer > 240 then
1098 self.child = false
1099 self.hornytimer = 0
1101 self.object:set_properties({
1102 textures = self.base_texture,
1103 mesh = self.base_mesh,
1104 visual_size = self.base_size,
1105 collisionbox = self.base_colbox,
1106 selectionbox = self.base_selbox,
1109 -- custom function when child grows up
1110 if self.on_grown then
1111 self.on_grown(self)
1112 else
1113 -- jump when fully grown so as not to fall into ground
1114 self.object:set_velocity({
1115 x = 0,
1116 y = self.jump_height,
1117 z = 0
1122 return
1125 -- horny animal can mate for 40 seconds,
1126 -- afterwards horny animal cannot mate again for 200 seconds
1127 if self.horny == true
1128 and self.hornytimer < 240 then
1130 self.hornytimer = self.hornytimer + 1
1132 if self.hornytimer >= 240 then
1133 self.hornytimer = 0
1134 self.horny = false
1138 -- find another same animal who is also horny and mate if nearby
1139 if self.horny == true
1140 and self.hornytimer <= 40 then
1142 local pos = self.object:get_pos()
1144 effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, "heart.png", 3, 4, 1, 0.1)
1146 local objs = minetest.get_objects_inside_radius(pos, 3)
1147 local num = 0
1148 local ent = nil
1150 for n = 1, #objs do
1152 ent = objs[n]:get_luaentity()
1154 -- check for same animal with different colour
1155 local canmate = false
1157 if ent then
1159 if ent.name == self.name then
1160 canmate = true
1161 else
1162 local entname = string.split(ent.name,":")
1163 local selfname = string.split(self.name,":")
1165 if entname[1] == selfname[1] then
1166 entname = string.split(entname[2],"_")
1167 selfname = string.split(selfname[2],"_")
1169 if entname[1] == selfname[1] then
1170 canmate = true
1176 if ent
1177 and canmate == true
1178 and ent.horny == true
1179 and ent.hornytimer <= 40 then
1180 num = num + 1
1183 -- found your mate? then have a baby
1184 if num > 1 then
1186 self.hornytimer = 41
1187 ent.hornytimer = 41
1189 -- spawn baby
1190 minetest.after(5, function(parent1, parent2, pos)
1191 if not parent1.object:get_luaentity() then
1192 return
1194 if not parent2.object:get_luaentity() then
1195 return
1198 -- custom breed function
1199 if parent1.on_breed then
1200 -- when false, skip going any further
1201 if parent1.on_breed(parent1, parent2) == false then
1202 return
1206 local child = mobs:spawn_child(pos, parent1.name)
1208 local ent_c = child:get_luaentity()
1211 -- Use texture of one of the parents
1212 local p = math.random(1, 2)
1213 if p == 1 then
1214 ent_c.base_texture = parent1.base_texture
1215 else
1216 ent_c.base_texture = parent2.base_texture
1218 child:set_properties({
1219 textures = ent_c.base_texture
1222 -- tamed and owned by parents' owner
1223 ent_c.tamed = true
1224 ent_c.owner = parent1.owner
1225 end, self, ent, pos)
1227 num = 0
1229 break
1236 -- find and replace what mob is looking for (grass, wheat etc.)
1237 local replace = function(self, pos)
1239 if not self.replace_rate
1240 or not self.replace_what
1241 or self.child == true
1242 or self.object:get_velocity().y ~= 0
1243 or random(1, self.replace_rate) > 1 then
1244 return
1247 local what, with, y_offset
1249 if type(self.replace_what[1]) == "table" then
1251 local num = random(#self.replace_what)
1253 what = self.replace_what[num][1] or ""
1254 with = self.replace_what[num][2] or ""
1255 y_offset = self.replace_what[num][3] or 0
1256 else
1257 what = self.replace_what
1258 with = self.replace_with or ""
1259 y_offset = self.replace_offset or 0
1262 pos.y = pos.y + y_offset
1264 if #minetest.find_nodes_in_area(pos, pos, what) > 0 then
1266 local oldnode = {name = what}
1267 local newnode = {name = with}
1268 local on_replace_return
1270 if self.on_replace then
1271 on_replace_return = self.on_replace(self, pos, oldnode, newnode)
1274 if on_replace_return ~= false then
1276 if mobs_griefing then
1277 minetest.set_node(pos, {name = with})
1285 -- check if daytime and also if mob is docile during daylight hours
1286 local day_docile = function(self)
1288 if self.docile_by_day == false then
1290 return false
1292 elseif self.docile_by_day == true
1293 and self.time_of_day > 0.2
1294 and self.time_of_day < 0.8 then
1296 return true
1301 local los_switcher = false
1302 local height_switcher = false
1304 -- path finding and smart mob routine by rnd, line_of_sight and other edits by Elkien3
1305 local smart_mobs = function(self, s, p, dist, dtime)
1307 local s1 = self.path.lastpos
1309 local target_pos = self.attack:get_pos()
1311 -- is it becoming stuck?
1312 if abs(s1.x - s.x) + abs(s1.z - s.z) < .5 then
1313 self.path.stuck_timer = self.path.stuck_timer + dtime
1314 else
1315 self.path.stuck_timer = 0
1318 self.path.lastpos = {x = s.x, y = s.y, z = s.z}
1320 local use_pathfind = false
1321 local has_lineofsight = minetest.line_of_sight(
1322 {x = s.x, y = (s.y) + .5, z = s.z},
1323 {x = target_pos.x, y = (target_pos.y) + 1.5, z = target_pos.z}, .2)
1325 -- im stuck, search for path
1326 if not has_lineofsight then
1328 if los_switcher == true then
1329 use_pathfind = true
1330 los_switcher = false
1331 end -- cannot see target!
1332 else
1333 if los_switcher == false then
1335 los_switcher = true
1336 use_pathfind = false
1338 minetest.after(1, function(self)
1339 if not self.object:get_luaentity() then
1340 return
1342 if has_lineofsight then self.path.following = false end
1343 end, self)
1344 end -- can see target!
1347 if (self.path.stuck_timer > stuck_timeout and not self.path.following) then
1349 use_pathfind = true
1350 self.path.stuck_timer = 0
1352 minetest.after(1, function(self)
1353 if not self.object:get_luaentity() then
1354 return
1356 if has_lineofsight then self.path.following = false end
1357 end, self)
1360 if (self.path.stuck_timer > stuck_path_timeout and self.path.following) then
1362 use_pathfind = true
1363 self.path.stuck_timer = 0
1365 minetest.after(1, function(self)
1366 if not self.object:get_luaentity() then
1367 return
1369 if has_lineofsight then self.path.following = false end
1370 end, self)
1373 if math.abs(vector.subtract(s,target_pos).y) > self.stepheight then
1375 if height_switcher then
1376 use_pathfind = true
1377 height_switcher = false
1379 else
1380 if not height_switcher then
1381 use_pathfind = false
1382 height_switcher = true
1386 if use_pathfind then
1387 -- lets try find a path, first take care of positions
1388 -- since pathfinder is very sensitive
1389 local sheight = self.collisionbox[5] - self.collisionbox[2]
1391 -- round position to center of node to avoid stuck in walls
1392 -- also adjust height for player models!
1393 s.x = floor(s.x + 0.5)
1394 s.z = floor(s.z + 0.5)
1396 local ssight, sground = minetest.line_of_sight(s, {
1397 x = s.x, y = s.y - 4, z = s.z}, 1)
1399 -- determine node above ground
1400 if not ssight then
1401 s.y = sground.y + 1
1404 local p1 = self.attack:get_pos()
1406 p1.x = floor(p1.x + 0.5)
1407 p1.y = floor(p1.y + 0.5)
1408 p1.z = floor(p1.z + 0.5)
1410 local dropheight = 12
1411 if self.fear_height ~= 0 then dropheight = self.fear_height end
1412 local jumpheight = 0
1413 if self.jump and self.jump_height >= 4 then
1414 jumpheight = math.min(math.ceil(self.jump_height / 4), 4)
1415 elseif self.stepheight > 0.5 then
1416 jumpheight = 1
1418 self.path.way = minetest.find_path(s, p1, 16, jumpheight, dropheight, "A*_noprefetch")
1420 self.state = ""
1421 do_attack(self, self.attack)
1423 -- no path found, try something else
1424 if not self.path.way then
1426 self.path.following = false
1428 -- lets make way by digging/building if not accessible
1429 if self.pathfinding == 2 and mobs_griefing then
1431 -- is player higher than mob?
1432 if s.y < p1.y then
1434 -- build upwards
1435 if not minetest.is_protected(s, "") then
1437 local ndef1 = minetest.registered_nodes[self.standing_in]
1439 if ndef1 and (ndef1.buildable_to or ndef1.groups.liquid) then
1441 minetest.set_node(s, {name = mobs.fallback_node})
1445 local sheight = math.ceil(self.collisionbox[5]) + 1
1447 -- assume mob is 2 blocks high so it digs above its head
1448 s.y = s.y + sheight
1450 -- remove one block above to make room to jump
1451 if not minetest.is_protected(s, "") then
1453 local node1 = node_ok(s, "air").name
1454 local ndef1 = minetest.registered_nodes[node1]
1456 if node1 ~= "air"
1457 and node1 ~= "ignore"
1458 and ndef1
1459 and not ndef1.groups.level
1460 and not ndef1.groups.unbreakable
1461 and not ndef1.groups.liquid then
1463 minetest.set_node(s, {name = "air"})
1464 minetest.add_item(s, ItemStack(node1))
1469 s.y = s.y - sheight
1470 self.object:set_pos({x = s.x, y = s.y + 2, z = s.z})
1472 else -- dig 2 blocks to make door toward player direction
1474 local yaw1 = self.object:get_yaw() + pi / 2
1475 local p1 = {
1476 x = s.x + cos(yaw1),
1477 y = s.y,
1478 z = s.z + sin(yaw1)
1481 if not minetest.is_protected(p1, "") then
1483 local node1 = node_ok(p1, "air").name
1484 local ndef1 = minetest.registered_nodes[node1]
1486 if node1 ~= "air"
1487 and node1 ~= "ignore"
1488 and ndef1
1489 and not ndef1.groups.level
1490 and not ndef1.groups.unbreakable
1491 and not ndef1.groups.liquid then
1493 minetest.add_item(p1, ItemStack(node1))
1494 minetest.set_node(p1, {name = "air"})
1497 p1.y = p1.y + 1
1498 node1 = node_ok(p1, "air").name
1499 ndef1 = minetest.registered_nodes[node1]
1501 if node1 ~= "air"
1502 and node1 ~= "ignore"
1503 and ndef1
1504 and not ndef1.groups.level
1505 and not ndef1.groups.unbreakable
1506 and not ndef1.groups.liquid then
1508 minetest.add_item(p1, ItemStack(node1))
1509 minetest.set_node(p1, {name = "air"})
1516 -- will try again in 2 seconds
1517 self.path.stuck_timer = stuck_timeout - 2
1518 elseif s.y < p1.y and (not self.fly) then
1519 do_jump(self) --add jump to pathfinding
1520 self.path.following = true
1521 -- Yay, I found path!
1522 -- TODO: Implement war_cry sound without being annoying
1523 --mob_sound(self, "war_cry", true)
1524 else
1525 set_velocity(self, self.walk_velocity)
1527 -- follow path now that it has it
1528 self.path.following = true
1534 -- specific attacks
1535 local specific_attack = function(list, what)
1537 -- no list so attack default (player, animals etc.)
1538 if list == nil then
1539 return true
1542 -- found entity on list to attack?
1543 for no = 1, #list do
1545 if list[no] == what then
1546 return true
1550 return false
1553 -- monster find someone to attack
1554 local monster_attack = function(self)
1556 if self.type ~= "monster"
1557 or not damage_enabled
1558 or creative
1559 or self.state == "attack"
1560 or day_docile(self) then
1561 return
1564 local s = self.object:get_pos()
1565 local p, sp, dist
1566 local player, obj, min_player
1567 local type, name = "", ""
1568 local min_dist = self.view_range + 1
1569 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1571 for n = 1, #objs do
1573 if objs[n]:is_player() then
1575 if mobs.invis[ objs[n]:get_player_name() ] or (not object_in_range(self, objs[n])) then
1576 type = ""
1577 else
1578 player = objs[n]
1579 type = "player"
1580 name = "player"
1582 else
1583 obj = objs[n]:get_luaentity()
1585 if obj then
1586 player = obj.object
1587 type = obj.type
1588 name = obj.name or ""
1592 -- find specific mob to attack, failing that attack player/npc/animal
1593 if specific_attack(self.specific_attack, name)
1594 and (type == "player" or type == "npc"
1595 or (type == "animal" and self.attack_animals == true)) then
1597 p = player:get_pos()
1598 sp = s
1600 dist = vector.distance(p, s)
1602 -- aim higher to make looking up hills more realistic
1603 p.y = p.y + 1
1604 sp.y = sp.y + 1
1607 -- choose closest player to attack
1608 if dist < min_dist
1609 and line_of_sight(self, sp, p, 2) == true then
1610 min_dist = dist
1611 min_player = player
1616 -- attack player
1617 if min_player then
1618 do_attack(self, min_player)
1623 -- npc, find closest monster to attack
1624 local npc_attack = function(self)
1626 if self.type ~= "npc"
1627 or not self.attacks_monsters
1628 or self.state == "attack" then
1629 return
1632 local p, sp, obj, min_player
1633 local s = self.object:get_pos()
1634 local min_dist = self.view_range + 1
1635 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1637 for n = 1, #objs do
1639 obj = objs[n]:get_luaentity()
1641 if obj and obj.type == "monster" then
1643 p = obj.object:get_pos()
1644 sp = s
1646 local dist = vector.distance(p, s)
1648 -- aim higher to make looking up hills more realistic
1649 p.y = p.y + 1
1650 sp.y = sp.y + 1
1652 if dist < min_dist
1653 and line_of_sight(self, sp, p, 2) == true then
1654 min_dist = dist
1655 min_player = obj.object
1660 if min_player then
1661 do_attack(self, min_player)
1666 -- specific runaway
1667 local specific_runaway = function(list, what)
1669 -- no list so do not run
1670 if list == nil then
1671 return false
1674 -- found entity on list to attack?
1675 for no = 1, #list do
1677 if list[no] == what then
1678 return true
1682 return false
1686 -- find someone to runaway from
1687 local runaway_from = function(self)
1689 if not self.runaway_from then
1690 return
1693 local s = self.object:get_pos()
1694 local p, sp, dist
1695 local player, obj, min_player
1696 local type, name = "", ""
1697 local min_dist = self.view_range + 1
1698 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1700 for n = 1, #objs do
1702 if objs[n]:is_player() then
1704 if mobs.invis[ objs[n]:get_player_name() ]
1705 or self.owner == objs[n]:get_player_name()
1706 or (not object_in_range(self, objs[n])) then
1707 type = ""
1708 else
1709 player = objs[n]
1710 type = "player"
1711 name = "player"
1713 else
1714 obj = objs[n]:get_luaentity()
1716 if obj then
1717 player = obj.object
1718 type = obj.type
1719 name = obj.name or ""
1723 -- find specific mob to runaway from
1724 if name ~= "" and name ~= self.name
1725 and specific_runaway(self.runaway_from, name) then
1727 p = player:get_pos()
1728 sp = s
1730 -- aim higher to make looking up hills more realistic
1731 p.y = p.y + 1
1732 sp.y = sp.y + 1
1734 dist = vector.distance(p, s)
1737 -- choose closest player/mpb to runaway from
1738 if dist < min_dist
1739 and line_of_sight(self, sp, p, 2) == true then
1740 min_dist = dist
1741 min_player = player
1746 if min_player then
1748 local lp = player:get_pos()
1749 local vec = {
1750 x = lp.x - s.x,
1751 y = lp.y - s.y,
1752 z = lp.z - s.z
1755 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
1757 if lp.x > s.x then
1758 yaw = yaw + pi
1761 yaw = set_yaw(self, yaw, 4)
1762 self.state = "runaway"
1763 self.runaway_timer = 3
1764 self.following = nil
1769 -- follow player if owner or holding item, if fish outta water then flop
1770 local follow_flop = function(self)
1772 -- find player to follow
1773 if (self.follow ~= ""
1774 or self.order == "follow")
1775 and not self.following
1776 and self.state ~= "attack"
1777 and self.state ~= "runaway" then
1779 local s = self.object:get_pos()
1780 local players = minetest.get_connected_players()
1782 for n = 1, #players do
1784 if (object_in_range(self, players[n]))
1785 and not mobs.invis[ players[n]:get_player_name() ] then
1787 self.following = players[n]
1789 break
1794 if self.type == "npc"
1795 and self.order == "follow"
1796 and self.state ~= "attack"
1797 and self.owner ~= "" then
1799 -- npc stop following player if not owner
1800 if self.following
1801 and self.owner
1802 and self.owner ~= self.following:get_player_name() then
1803 self.following = nil
1805 else
1806 -- stop following player if not holding specific item
1807 if self.following
1808 and self.following:is_player()
1809 and follow_holding(self, self.following) == false then
1810 self.following = nil
1815 -- follow that thing
1816 if self.following then
1818 local s = self.object:get_pos()
1819 local p
1821 if self.following:is_player() then
1823 p = self.following:get_pos()
1825 elseif self.following.object then
1827 p = self.following.object:get_pos()
1830 if p then
1832 local dist = vector.distance(p, s)
1834 -- dont follow if out of range
1835 if (not object_in_range(self, self.following)) then
1836 self.following = nil
1837 else
1838 local vec = {
1839 x = p.x - s.x,
1840 z = p.z - s.z
1843 local yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1845 if p.x > s.x then yaw = yaw + pi end
1847 set_yaw(self, yaw, 6)
1849 -- anyone but standing npc's can move along
1850 if dist > self.reach
1851 and self.order ~= "stand" then
1853 set_velocity(self, self.walk_velocity)
1855 if self.walk_chance ~= 0 then
1856 set_animation(self, "walk")
1858 else
1859 set_velocity(self, 0)
1860 set_animation(self, "stand")
1863 return
1868 -- swimmers flop when out of their element, and swim again when back in
1869 if self.fly then
1870 local s = self.object:get_pos()
1871 if not flight_check(self, s) then
1873 self.state = "flop"
1874 self.object:set_velocity({x = 0, y = -5, z = 0})
1876 set_animation(self, "stand")
1878 return
1879 elseif self.state == "flop" then
1880 self.state = "stand"
1886 -- dogshoot attack switch and counter function
1887 local dogswitch = function(self, dtime)
1889 -- switch mode not activated
1890 if not self.dogshoot_switch
1891 or not dtime then
1892 return 0
1895 self.dogshoot_count = self.dogshoot_count + dtime
1897 if (self.dogshoot_switch == 1
1898 and self.dogshoot_count > self.dogshoot_count_max)
1899 or (self.dogshoot_switch == 2
1900 and self.dogshoot_count > self.dogshoot_count2_max) then
1902 self.dogshoot_count = 0
1904 if self.dogshoot_switch == 1 then
1905 self.dogshoot_switch = 2
1906 else
1907 self.dogshoot_switch = 1
1911 return self.dogshoot_switch
1915 -- execute current state (stand, walk, run, attacks)
1916 local do_states = function(self, dtime)
1918 local yaw = self.object:get_yaw() or 0
1920 if self.state == "stand" then
1922 if random(1, 4) == 1 then
1924 local lp = nil
1925 local s = self.object:get_pos()
1926 local objs = minetest.get_objects_inside_radius(s, 3)
1928 for n = 1, #objs do
1930 if objs[n]:is_player() then
1931 lp = objs[n]:get_pos()
1932 break
1936 -- look at any players nearby, otherwise turn randomly
1937 if lp then
1939 local vec = {
1940 x = lp.x - s.x,
1941 z = lp.z - s.z
1944 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1946 if lp.x > s.x then yaw = yaw + pi end
1947 else
1948 yaw = yaw + random(-0.5, 0.5)
1951 yaw = set_yaw(self, yaw, 8)
1954 set_velocity(self, 0)
1955 set_animation(self, "stand")
1957 -- npc's ordered to stand stay standing
1958 if self.type ~= "npc"
1959 or self.order ~= "stand" then
1961 if self.walk_chance ~= 0
1962 and self.facing_fence ~= true
1963 and random(1, 100) <= self.walk_chance
1964 and is_at_cliff_or_danger(self) == false then
1966 set_velocity(self, self.walk_velocity)
1967 self.state = "walk"
1968 set_animation(self, "walk")
1972 elseif self.state == "walk" then
1974 local s = self.object:get_pos()
1975 local lp = nil
1977 -- is there something I need to avoid?
1978 if (self.water_damage > 0
1979 and self.lava_damage > 0)
1980 or self.breath_max ~= -1 then
1982 lp = minetest.find_node_near(s, 1, {"group:water", "group:lava"})
1984 elseif self.water_damage > 0 then
1986 lp = minetest.find_node_near(s, 1, {"group:water"})
1988 elseif self.lava_damage > 0 then
1990 lp = minetest.find_node_near(s, 1, {"group:lava"})
1992 elseif self.fire_damage > 0 then
1994 lp = minetest.find_node_near(s, 1, {"group:fire"})
1998 local is_in_danger = false
1999 if lp then
2000 -- If mob in or on dangerous block, look for land
2001 if (is_node_dangerous(self, self.standing_in) or
2002 is_node_dangerous(self, self.standing_on)) then
2003 is_in_danger = true
2005 lp = minetest.find_node_near(s, 5, {"group:solid"})
2007 -- did we find land?
2008 if lp then
2010 local vec = {
2011 x = lp.x - s.x,
2012 z = lp.z - s.z
2015 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2017 if lp.x > s.x then yaw = yaw + pi end
2019 -- look towards land and jump/move in that direction
2020 yaw = set_yaw(self, yaw, 6)
2021 do_jump(self)
2022 set_velocity(self, self.walk_velocity)
2023 else
2024 yaw = yaw + random(-0.5, 0.5)
2027 -- A danger is near but mob is not inside
2028 else
2030 -- Randomly turn
2031 if random(1, 100) <= 30 then
2032 yaw = yaw + random(-0.5, 0.5)
2033 yaw = set_yaw(self, yaw, 8)
2037 yaw = set_yaw(self, yaw, 8)
2039 -- otherwise randomly turn
2040 elseif random(1, 100) <= 30 then
2042 yaw = yaw + random(-0.5, 0.5)
2043 yaw = set_yaw(self, yaw, 8)
2046 -- stand for great fall or danger or fence in front
2047 local cliff_or_danger = false
2048 if is_in_danger then
2049 cliff_or_danger = is_at_cliff_or_danger(self)
2051 if self.facing_fence == true
2052 or cliff_or_danger
2053 or random(1, 100) <= 30 then
2055 set_velocity(self, 0)
2056 self.state = "stand"
2057 set_animation(self, "stand")
2058 else
2060 set_velocity(self, self.walk_velocity)
2062 if flight_check(self)
2063 and self.animation
2064 and self.animation.fly_start
2065 and self.animation.fly_end then
2066 set_animation(self, "fly")
2067 else
2068 set_animation(self, "walk")
2072 -- runaway when punched
2073 elseif self.state == "runaway" then
2075 self.runaway_timer = self.runaway_timer + 1
2077 -- stop after 5 seconds or when at cliff
2078 if self.runaway_timer > 5
2079 or is_at_cliff_or_danger(self) then
2080 self.runaway_timer = 0
2081 set_velocity(self, 0)
2082 self.state = "stand"
2083 set_animation(self, "stand")
2084 else
2085 set_velocity(self, self.run_velocity)
2086 set_animation(self, "walk")
2089 -- attack routines (explode, dogfight, shoot, dogshoot)
2090 elseif self.state == "attack" then
2092 -- calculate distance from mob and enemy
2093 local s = self.object:get_pos()
2094 local p = self.attack:get_pos() or s
2095 local dist = vector.distance(p, s)
2097 -- stop attacking if player invisible or out of range
2098 if not self.attack
2099 or not self.attack:get_pos()
2100 or not object_in_range(self, self.attack)
2101 or self.attack:get_hp() <= 0
2102 or (self.attack:is_player() and mobs.invis[ self.attack:get_player_name() ]) then
2104 self.state = "stand"
2105 set_velocity(self, 0)
2106 set_animation(self, "stand")
2107 self.attack = nil
2108 self.v_start = false
2109 self.timer = 0
2110 self.blinktimer = 0
2111 self.path.way = nil
2113 return
2116 if self.attack_type == "explode" then
2118 local vec = {
2119 x = p.x - s.x,
2120 z = p.z - s.z
2123 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2125 if p.x > s.x then yaw = yaw + pi end
2127 yaw = set_yaw(self, yaw)
2129 local node_break_radius = self.explosion_radius or 1
2130 local entity_damage_radius = self.explosion_damage_radius
2131 or (node_break_radius * 2)
2133 -- start timer when in reach and line of sight
2134 if not self.v_start
2135 and dist <= self.reach
2136 and line_of_sight(self, s, p, 2) then
2138 self.v_start = true
2139 self.timer = 0
2140 self.blinktimer = 0
2141 mob_sound(self, "fuse", nil, false)
2143 -- stop timer if out of reach or direct line of sight
2144 elseif self.allow_fuse_reset
2145 and self.v_start
2146 and (dist > self.reach
2147 or not line_of_sight(self, s, p, 2)) then
2148 self.v_start = false
2149 self.timer = 0
2150 self.blinktimer = 0
2151 self.blinkstatus = false
2152 remove_texture_mod(self, "^[brighten")
2155 -- walk right up to player unless the timer is active
2156 if self.v_start and (self.stop_to_explode or dist < 1.5) then
2157 set_velocity(self, 0)
2158 else
2159 set_velocity(self, self.run_velocity)
2162 if self.animation and self.animation.run_start then
2163 set_animation(self, "run")
2164 else
2165 set_animation(self, "walk")
2168 if self.v_start then
2170 self.timer = self.timer + dtime
2171 self.blinktimer = (self.blinktimer or 0) + dtime
2173 if self.blinktimer > 0.2 then
2175 self.blinktimer = 0
2177 if self.blinkstatus then
2178 remove_texture_mod(self, "^[brighten")
2179 else
2180 add_texture_mod(self, "^[brighten")
2183 self.blinkstatus = not self.blinkstatus
2186 if self.timer > self.explosion_timer then
2188 local pos = self.object:get_pos()
2190 -- dont damage anything if area protected or next to water
2191 if minetest.find_node_near(pos, 1, {"group:water"})
2192 or minetest.is_protected(pos, "") then
2194 node_break_radius = 1
2197 self.object:remove()
2199 if mobs_griefing and mod_tnt and tnt and tnt.boom
2200 and not minetest.is_protected(pos, "") then
2202 tnt.boom(pos, {
2203 radius = node_break_radius,
2204 damage_radius = entity_damage_radius,
2205 sound = self.sounds.explode,
2206 is_tnt = false,
2208 else
2210 minetest.sound_play(self.sounds.explode, {
2211 pos = pos,
2212 gain = 1.0,
2213 max_hear_distance = self.sounds.distance or 32
2216 entity_physics(pos, entity_damage_radius)
2217 effect(pos, 32, "tnt_smoke.png", nil, nil, node_break_radius, 1, 0)
2220 return
2224 elseif self.attack_type == "dogfight"
2225 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 2)
2226 or (self.attack_type == "dogshoot" and dist <= self.reach and dogswitch(self) == 0) then
2228 if self.fly
2229 and dist > self.reach then
2231 local p1 = s
2232 local me_y = floor(p1.y)
2233 local p2 = p
2234 local p_y = floor(p2.y + 1)
2235 local v = self.object:get_velocity()
2237 if flight_check(self, s) then
2239 if me_y < p_y then
2241 self.object:set_velocity({
2242 x = v.x,
2243 y = 1 * self.walk_velocity,
2244 z = v.z
2247 elseif me_y > p_y then
2249 self.object:set_velocity({
2250 x = v.x,
2251 y = -1 * self.walk_velocity,
2252 z = v.z
2255 else
2256 if me_y < p_y then
2258 self.object:set_velocity({
2259 x = v.x,
2260 y = 0.01,
2261 z = v.z
2264 elseif me_y > p_y then
2266 self.object:set_velocity({
2267 x = v.x,
2268 y = -0.01,
2269 z = v.z
2276 -- rnd: new movement direction
2277 if self.path.following
2278 and self.path.way
2279 and self.attack_type ~= "dogshoot" then
2281 -- no paths longer than 50
2282 if #self.path.way > 50
2283 or dist < self.reach then
2284 self.path.following = false
2285 return
2288 local p1 = self.path.way[1]
2290 if not p1 then
2291 self.path.following = false
2292 return
2295 if abs(p1.x-s.x) + abs(p1.z - s.z) < 0.6 then
2296 -- reached waypoint, remove it from queue
2297 table.remove(self.path.way, 1)
2300 -- set new temporary target
2301 p = {x = p1.x, y = p1.y, z = p1.z}
2304 local vec = {
2305 x = p.x - s.x,
2306 z = p.z - s.z
2309 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2311 if p.x > s.x then yaw = yaw + pi end
2313 yaw = set_yaw(self, yaw)
2315 -- move towards enemy if beyond mob reach
2316 if dist > self.reach then
2318 -- path finding by rnd
2319 if self.pathfinding -- only if mob has pathfinding enabled
2320 and enable_pathfinding then
2322 smart_mobs(self, s, p, dist, dtime)
2325 if is_at_cliff_or_danger(self) then
2327 set_velocity(self, 0)
2328 set_animation(self, "stand")
2329 else
2331 if self.path.stuck then
2332 set_velocity(self, self.walk_velocity)
2333 else
2334 set_velocity(self, self.run_velocity)
2337 if self.animation and self.animation.run_start then
2338 set_animation(self, "run")
2339 else
2340 set_animation(self, "walk")
2344 else -- rnd: if inside reach range
2346 self.path.stuck = false
2347 self.path.stuck_timer = 0
2348 self.path.following = false -- not stuck anymore
2350 set_velocity(self, 0)
2352 if not self.custom_attack then
2354 if self.timer > 1 then
2356 self.timer = 0
2358 if self.double_melee_attack
2359 and random(1, 2) == 1 then
2360 set_animation(self, "punch2")
2361 else
2362 set_animation(self, "punch")
2365 local p2 = p
2366 local s2 = s
2368 p2.y = p2.y + .5
2369 s2.y = s2.y + .5
2371 if line_of_sight(self, p2, s2) == true then
2373 -- play attack sound
2374 mob_sound(self, "attack")
2376 -- punch player (or what player is attached to)
2377 local attached = self.attack:get_attach()
2378 if attached then
2379 self.attack = attached
2381 self.attack:punch(self.object, 1.0, {
2382 full_punch_interval = 1.0,
2383 damage_groups = {fleshy = self.damage}
2384 }, nil)
2387 else -- call custom attack every second
2388 if self.custom_attack
2389 and self.timer > 1 then
2391 self.timer = 0
2393 self.custom_attack(self, p)
2398 elseif self.attack_type == "shoot"
2399 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 1)
2400 or (self.attack_type == "dogshoot" and dist > self.reach and dogswitch(self) == 0) then
2402 p.y = p.y - .5
2403 s.y = s.y + .5
2405 local dist = vector.distance(p, s)
2406 local vec = {
2407 x = p.x - s.x,
2408 y = p.y - s.y,
2409 z = p.z - s.z
2412 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2414 if p.x > s.x then yaw = yaw + pi end
2416 yaw = set_yaw(self, yaw)
2418 set_velocity(self, 0)
2420 if self.shoot_interval
2421 and self.timer > self.shoot_interval
2422 and random(1, 100) <= 60 then
2424 self.timer = 0
2425 set_animation(self, "shoot")
2427 -- play shoot attack sound
2428 mob_sound(self, "shoot_attack")
2430 local p = self.object:get_pos()
2432 p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
2434 -- Shoot arrow
2435 if minetest.registered_entities[self.arrow] then
2437 local arrow, ent
2438 local v = 1
2439 if not self.shoot_arrow then
2440 arrow = minetest.add_entity(p, self.arrow)
2441 ent = arrow:get_luaentity()
2442 if ent.velocity then
2443 v = ent.velocity
2445 ent.switch = 1
2446 ent.owner_id = tostring(self.object) -- add unique owner id to arrow
2449 local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5
2450 -- offset makes shoot aim accurate
2451 vec.y = vec.y + self.shoot_offset
2452 vec.x = vec.x * (v / amount)
2453 vec.y = vec.y * (v / amount)
2454 vec.z = vec.z * (v / amount)
2455 if self.shoot_arrow then
2456 vec = vector.normalize(vec)
2457 self:shoot_arrow(p, vec)
2458 else
2459 arrow:set_velocity(vec)
2468 -- falling and fall damage
2469 local falling = function(self, pos)
2471 if self.fly then
2472 return
2475 -- floating in water (or falling)
2476 local v = self.object:get_velocity()
2478 if v.y > 0 then
2480 -- apply gravity when moving up
2481 self.object:set_acceleration({
2482 x = 0,
2483 y = -10,
2484 z = 0
2487 elseif v.y <= 0 and v.y > self.fall_speed then
2489 -- fall downwards at set speed
2490 self.object:set_acceleration({
2491 x = 0,
2492 y = self.fall_speed,
2493 z = 0
2495 else
2496 -- stop accelerating once max fall speed hit
2497 self.object:set_acceleration({x = 0, y = 0, z = 0})
2500 -- in water then float up
2501 if minetest.registered_nodes[node_ok(pos).name].groups.water then
2503 if self.floats == 1 then
2505 self.object:set_acceleration({
2506 x = 0,
2507 y = -self.fall_speed / (max(1, v.y) ^ 2),
2508 z = 0
2511 else
2513 -- fall damage onto solid ground
2514 if self.fall_damage == 1
2515 and self.object:get_velocity().y == 0 then
2517 local d = (self.old_y or 0) - self.object:get_pos().y
2519 if d > 5 then
2521 local add = minetest.get_item_group(self.standing_on, "fall_damage_add_percent")
2522 local damage = d - 5
2523 if add ~= 0 then
2524 damage = damage + damage * (add/100)
2526 damage = floor(damage)
2527 if damage > 0 then
2528 self.health = self.health - damage
2530 effect(pos, 5, "tnt_smoke.png", 1, 2, 2, nil)
2532 if check_for_death(self, "fall", {type = "fall"}) then
2533 return
2538 self.old_y = self.object:get_pos().y
2544 -- deal damage and effects when mob punched
2545 local mob_punch = function(self, hitter, tflp, tool_capabilities, dir)
2547 -- custom punch function
2548 if self.do_punch then
2550 -- when false skip going any further
2551 if self.do_punch(self, hitter, tflp, tool_caps, dir) == false then
2552 return
2556 -- error checking when mod profiling is enabled
2557 if not tool_capabilities then
2558 minetest.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2559 return
2562 -- is mob protected?
2563 if self.protected and hitter:is_player()
2564 and minetest.is_protected(self.object:get_pos(), hitter:get_player_name()) then
2565 return
2569 -- punch interval
2570 local weapon = hitter:get_wielded_item()
2571 local punch_interval = 1.4
2573 -- exhaust attacker
2574 if mod_hunger and hitter:is_player() then
2575 mcl_hunger.exhaust(hitter:get_player_name(), mcl_hunger.EXHAUST_ATTACK)
2578 -- calculate mob damage
2579 local damage = 0
2580 local armor = self.object:get_armor_groups() or {}
2581 local tmp
2583 -- quick error check incase it ends up 0 (serialize.h check test)
2584 if tflp == 0 then
2585 tflp = 0.2
2588 if use_cmi then
2589 damage = cmi.calculate_damage(self.object, hitter, tflp, tool_capabilities, dir)
2590 else
2592 for group,_ in pairs( (tool_capabilities.damage_groups or {}) ) do
2594 tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)
2596 if tmp < 0 then
2597 tmp = 0.0
2598 elseif tmp > 1 then
2599 tmp = 1.0
2602 damage = damage + (tool_capabilities.damage_groups[group] or 0)
2603 * tmp * ((armor[group] or 0) / 100.0)
2607 -- check for tool immunity or special damage
2608 for n = 1, #self.immune_to do
2610 if self.immune_to[n][1] == weapon:get_name() then
2612 damage = self.immune_to[n][2] or 0
2613 break
2617 -- healing
2618 if damage <= -1 then
2619 self.health = self.health - floor(damage)
2620 return
2623 if use_cmi then
2625 local cancel = cmi.notify_punch(self.object, hitter, tflp, tool_capabilities, dir, damage)
2627 if cancel then return end
2630 if tool_capabilities then
2631 punch_interval = tool_capabilities.full_punch_interval or 1.4
2634 -- add weapon wear manually
2635 -- Required because we have custom health handling ("health" property)
2636 if minetest.settings:get_bool("creative_mode") ~= true
2637 and tool_capabilities then
2638 if tool_capabilities.punch_attack_uses then
2639 -- Without this delay, the wear does not work. Quite hacky ...
2640 minetest.after(0, function(name)
2641 local player = minetest.get_player_by_name(name)
2642 if not player then return end
2643 local weapon = hitter:get_wielded_item(player)
2644 local def = weapon:get_definition()
2645 if def.tool_capabilities and def.tool_capabilities.punch_attack_uses then
2646 local wear = floor(65535/tool_capabilities.punch_attack_uses)
2647 weapon:add_wear(wear)
2648 hitter:set_wielded_item(weapon)
2650 end, hitter:get_player_name())
2654 local die = false
2656 -- only play hit sound and show blood effects if damage is 1 or over
2657 if damage >= 1 then
2659 -- weapon sounds
2660 if weapon:get_definition().sounds ~= nil then
2662 local s = random(0, #weapon:get_definition().sounds)
2664 minetest.sound_play(weapon:get_definition().sounds[s], {
2665 object = self.object, --hitter,
2666 max_hear_distance = 8
2668 else
2669 minetest.sound_play("default_punch", {
2670 object = self.object, --hitter,
2671 max_hear_distance = 5
2675 damage_effect(self, damage)
2677 -- do damage
2678 self.health = self.health - floor(damage)
2680 -- skip future functions if dead, except alerting others
2681 if check_for_death(self, "hit", {type = "punch", puncher = hitter}) then
2682 die = true
2685 -- knock back effect (only on full punch)
2686 if not die
2687 and self.knock_back
2688 and tflp >= punch_interval then
2690 local v = self.object:get_velocity()
2691 local r = 1.4 - min(punch_interval, 1.4)
2692 local kb = r * 2.0
2693 local up = 2
2695 -- if already in air then dont go up anymore when hit
2696 if v.y > 0
2697 or self.fly then
2698 up = 0
2701 -- direction error check
2702 dir = dir or {x = 0, y = 0, z = 0}
2704 -- check if tool already has specific knockback value
2705 if tool_capabilities.damage_groups["knockback"] then
2706 kb = tool_capabilities.damage_groups["knockback"]
2707 else
2708 kb = kb * 1.5
2711 self.object:set_velocity({
2712 x = dir.x * kb,
2713 y = up,
2714 z = dir.z * kb
2717 self.pause_timer = 0.25
2719 end -- END if damage
2721 -- if skittish then run away
2722 if not die and self.runaway == true then
2724 local lp = hitter:get_pos()
2725 local s = self.object:get_pos()
2726 local vec = {
2727 x = lp.x - s.x,
2728 y = lp.y - s.y,
2729 z = lp.z - s.z
2732 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
2734 if lp.x > s.x then
2735 yaw = yaw + pi
2738 yaw = set_yaw(self, yaw, 6)
2739 self.state = "runaway"
2740 self.runaway_timer = 0
2741 self.following = nil
2744 local name = hitter:get_player_name() or ""
2746 -- attack puncher and call other mobs for help
2747 if self.passive == false
2748 and self.state ~= "flop"
2749 and (self.child == false or self.type == "monster")
2750 and hitter:get_player_name() ~= self.owner
2751 and not mobs.invis[ name ] then
2753 if not die then
2754 -- attack whoever punched mob
2755 self.state = ""
2756 do_attack(self, hitter)
2759 -- alert others to the attack
2760 local objs = minetest.get_objects_inside_radius(hitter:get_pos(), self.view_range)
2761 local obj = nil
2763 for n = 1, #objs do
2765 obj = objs[n]:get_luaentity()
2767 if obj then
2769 -- only alert members of same mob or friends
2770 if obj.group_attack
2771 and obj.state ~= "attack"
2772 and obj.owner ~= name then
2773 if obj.name == self.name then
2774 do_attack(obj, hitter)
2775 elseif type(obj.group_attack) == "table" then
2776 for i=1, #obj.group_attack do
2777 if obj.name == obj.group_attack[i] then
2778 do_attack(obj, hitter)
2779 break
2785 -- have owned mobs attack player threat
2786 if obj.owner == name and obj.owner_loyal then
2787 do_attack(obj, self.object)
2794 local mob_detach_child = function(self, child)
2796 if self.driver == child then
2797 self.driver = nil
2802 -- get entity staticdata
2803 local mob_staticdata = function(self)
2805 -- remove mob when out of range unless tamed
2806 if remove_far
2807 and self.can_despawn
2808 and self.remove_ok
2809 and ((not self.nametag) or (self.nametag == ""))
2810 and self.lifetimer <= 20 then
2812 minetest.log("action", "Mob "..name.." despawns in mob_staticdata at "..minetest.pos_to_string(self.object.get_pos()))
2813 self.object:remove()
2815 return ""-- nil
2818 self.remove_ok = true
2819 self.attack = nil
2820 self.following = nil
2821 self.state = "stand"
2823 if use_cmi then
2824 self.serialized_cmi_components = cmi.serialize_components(self._cmi_components)
2827 local tmp = {}
2829 for _,stat in pairs(self) do
2831 local t = type(stat)
2833 if t ~= "function"
2834 and t ~= "nil"
2835 and t ~= "userdata"
2836 and _ ~= "_cmi_components" then
2837 tmp[_] = self[_]
2841 return minetest.serialize(tmp)
2845 -- activate mob and reload settings
2846 local mob_activate = function(self, staticdata, def, dtime)
2848 -- remove monsters in peaceful mode
2849 if self.type == "monster"
2850 and minetest.settings:get_bool("only_peaceful_mobs", false) then
2852 self.object:remove()
2854 return
2857 -- load entity variables
2858 local tmp = minetest.deserialize(staticdata)
2860 if tmp then
2861 for _,stat in pairs(tmp) do
2862 self[_] = stat
2866 -- select random texture, set model and size
2867 if not self.base_texture then
2869 -- compatiblity with old simple mobs textures
2870 if type(def.textures[1]) == "string" then
2871 def.textures = {def.textures}
2874 self.base_texture = def.textures[random(1, #def.textures)]
2875 self.base_mesh = def.mesh
2876 self.base_size = self.visual_size
2877 self.base_colbox = self.collisionbox
2878 self.base_selbox = self.selectionbox
2881 -- for current mobs that dont have this set
2882 if not self.base_selbox then
2883 self.base_selbox = self.selectionbox or self.base_colbox
2886 -- set texture, model and size
2887 local textures = self.base_texture
2888 local mesh = self.base_mesh
2889 local vis_size = self.base_size
2890 local colbox = self.base_colbox
2891 local selbox = self.base_selbox
2893 -- specific texture if gotten
2894 if self.gotten == true
2895 and def.gotten_texture then
2896 textures = def.gotten_texture
2899 -- specific mesh if gotten
2900 if self.gotten == true
2901 and def.gotten_mesh then
2902 mesh = def.gotten_mesh
2905 -- set child objects to half size
2906 if self.child == true then
2908 vis_size = {
2909 x = self.base_size.x * .5,
2910 y = self.base_size.y * .5,
2913 if def.child_texture then
2914 textures = def.child_texture[1]
2917 colbox = {
2918 self.base_colbox[1] * .5,
2919 self.base_colbox[2] * .5,
2920 self.base_colbox[3] * .5,
2921 self.base_colbox[4] * .5,
2922 self.base_colbox[5] * .5,
2923 self.base_colbox[6] * .5
2925 selbox = {
2926 self.base_selbox[1] * .5,
2927 self.base_selbox[2] * .5,
2928 self.base_selbox[3] * .5,
2929 self.base_selbox[4] * .5,
2930 self.base_selbox[5] * .5,
2931 self.base_selbox[6] * .5
2935 if self.health == 0 then
2936 self.health = random (self.hp_min, self.hp_max)
2938 if self.breath == nil then
2939 self.breath = self.breath_max
2942 -- pathfinding init
2943 self.path = {}
2944 self.path.way = {} -- path to follow, table of positions
2945 self.path.lastpos = {x = 0, y = 0, z = 0}
2946 self.path.stuck = false
2947 self.path.following = false -- currently following path?
2948 self.path.stuck_timer = 0 -- if stuck for too long search for path
2950 -- Armor groups
2951 -- immortal=1 because we use custom health
2952 -- handling (using "health" property)
2953 local armor
2954 if type(self.armor) == "table" then
2955 armor = table.copy(self.armor)
2956 armor.immortal = 1
2957 else
2958 armor = {immortal=1, fleshy = self.armor}
2960 self.object:set_armor_groups(armor)
2961 self.old_y = self.object:get_pos().y
2962 self.old_health = self.health
2963 self.sounds.distance = self.sounds.distance or 10
2964 self.textures = textures
2965 self.mesh = mesh
2966 self.collisionbox = colbox
2967 self.selectionbox = selbox
2968 self.visual_size = vis_size
2969 self.standing_in = "ignore"
2970 self.standing_on = "ignore"
2971 self.jump_sound_cooloff = 0 -- used to prevent jump sound from being played too often in short time
2972 self.opinion_sound_cooloff = 0 -- used to prevent sound spam of particular sound types
2974 self.texture_mods = {}
2975 self.object:set_texture_mod("")
2977 self.v_start = false
2978 self.timer = 0
2979 self.blinktimer = 0
2980 self.blinkstatus = false
2982 -- check existing nametag
2983 if not self.nametag then
2984 self.nametag = def.nametag
2987 -- set anything changed above
2988 self.object:set_properties(self)
2989 set_yaw(self, (random(0, 360) - 180) / 180 * pi, 6)
2990 update_tag(self)
2991 set_animation(self, "stand")
2993 -- run on_spawn function if found
2994 if self.on_spawn and not self.on_spawn_run then
2995 if self.on_spawn(self) then
2996 self.on_spawn_run = true -- if true, set flag to run once only
3000 -- run after_activate
3001 if def.after_activate then
3002 def.after_activate(self, staticdata, def, dtime)
3005 if use_cmi then
3006 self._cmi_components = cmi.activate_components(self.serialized_cmi_components)
3007 cmi.notify_activate(self.object, dtime)
3012 -- main mob function
3013 local mob_step = function(self, dtime)
3015 if use_cmi then
3016 cmi.notify_step(self.object, dtime)
3019 local pos = self.object:get_pos()
3020 local yaw = 0
3022 -- Despawning: when lifetimer expires, remove mob
3023 if remove_far
3024 and self.can_despawn == true
3025 and ((not self.nametag) or (self.nametag == "")) then
3027 -- TODO: Finish up implementation of despawning rules
3029 self.lifetimer = self.lifetimer - dtime
3031 if self.lifetimer <= 0 then
3033 -- only despawn away from player
3034 local objs = minetest.get_objects_inside_radius(pos, 32)
3036 for n = 1, #objs do
3038 if objs[n]:is_player() then
3040 self.lifetimer = 20
3042 return
3046 minetest.log("action", "Mob "..name.." despawns in mob_step at "..minetest.pos_to_string(pos))
3047 self.object:remove()
3049 return
3053 if self.jump_sound_cooloff > 0 then
3054 self.jump_sound_cooloff = self.jump_sound_cooloff - dtime
3056 if self.opinion_sound_cooloff > 0 then
3057 self.opinion_sound_cooloff = self.opinion_sound_cooloff - dtime
3059 falling(self, pos)
3061 -- smooth rotation by ThomasMonroe314
3063 if self.delay and self.delay > 0 then
3065 local yaw = self.object:get_yaw()
3067 if self.delay == 1 then
3068 yaw = self.target_yaw
3069 else
3070 local dif = abs(yaw - self.target_yaw)
3072 if yaw > self.target_yaw then
3074 if dif > pi then
3075 dif = 2 * pi - dif -- need to add
3076 yaw = yaw + dif / self.delay
3077 else
3078 yaw = yaw - dif / self.delay -- need to subtract
3081 elseif yaw < self.target_yaw then
3083 if dif > pi then
3084 dif = 2 * pi - dif
3085 yaw = yaw - dif / self.delay -- need to subtract
3086 else
3087 yaw = yaw + dif / self.delay -- need to add
3091 if yaw > (pi * 2) then yaw = yaw - (pi * 2) end
3092 if yaw < 0 then yaw = yaw + (pi * 2) end
3095 self.delay = self.delay - 1
3096 self.object:set_yaw(yaw)
3099 -- end rotation
3101 -- knockback timer
3102 if self.pause_timer > 0 then
3104 self.pause_timer = self.pause_timer - dtime
3106 return
3109 -- run custom function (defined in mob lua file)
3110 if self.do_custom then
3112 -- when false skip going any further
3113 if self.do_custom(self, dtime) == false then
3114 return
3118 -- attack timer
3119 self.timer = self.timer + dtime
3121 if self.state ~= "attack" then
3123 if self.timer < 1 then
3124 return
3127 self.timer = 0
3130 -- never go over 100
3131 if self.timer > 100 then
3132 self.timer = 1
3135 -- mob plays random sound at times
3136 if random(1, 100) == 1 then
3137 mob_sound(self, "random", true)
3140 -- environmental damage timer (every 1 second)
3141 self.env_damage_timer = self.env_damage_timer + dtime
3143 if (self.state == "attack" and self.env_damage_timer > 1)
3144 or self.state ~= "attack" then
3146 self.env_damage_timer = 0
3148 -- check for environmental damage (water, fire, lava etc.)
3149 do_env_damage(self)
3151 -- node replace check (cow eats grass etc.)
3152 replace(self, pos)
3155 monster_attack(self)
3157 npc_attack(self)
3159 breed(self)
3161 follow_flop(self)
3163 do_states(self, dtime)
3165 do_jump(self)
3167 runaway_from(self)
3172 -- default function when mobs are blown up with TNT
3173 local do_tnt = function(obj, damage)
3175 obj.object:punch(obj.object, 1.0, {
3176 full_punch_interval = 1.0,
3177 damage_groups = {fleshy = damage},
3178 }, nil)
3180 return false, true, {}
3184 mobs.spawning_mobs = {}
3186 -- Code to execute before custom on_rightclick handling
3187 local on_rightclick_prefix = function(self, clicker)
3188 local item = clicker:get_wielded_item()
3190 -- Name mob with nametag
3191 if not self.ignores_nametag and item:get_name() == "mcl_mobs:nametag" then
3193 local tag = item:get_meta():get_string("name")
3194 if tag ~= "" then
3195 if string.len(tag) > MAX_MOB_NAME_LENGTH then
3196 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH)
3198 self.nametag = tag
3200 update_tag(self)
3202 if not mobs.is_creative(clicker:get_player_name()) then
3203 item:take_item()
3204 clicker:set_wielded_item(item)
3206 return true
3210 return false
3213 local create_mob_on_rightclick = function(on_rightclick)
3214 return function(self, clicker)
3215 local stop = on_rightclick_prefix(self, clicker)
3216 if (not stop) and (on_rightclick) then
3217 on_rightclick(self, clicker)
3222 -- register mob entity
3223 function mobs:register_mob(name, def)
3225 mobs.spawning_mobs[name] = true
3227 local can_despawn
3228 if def.can_despawn ~= nil then
3229 can_despawn = def.can_despawn
3230 else
3231 can_despawn = true
3234 local function scale_difficulty(value, default, min, special)
3235 if (not value) or (value == default) or (value == special) then
3236 return default
3237 else
3238 return max(min, value * difficulty)
3242 local collisionbox = def.collisionbox or {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25}
3243 -- Workaround for <https://github.com/minetest/minetest/issues/5966>:
3244 -- Increase upper Y limit to avoid mobs glitching through solid nodes.
3245 -- FIXME: Remove workaround if it's no longer needed.
3246 if collisionbox[5] < 0.79 then
3247 collisionbox[5] = 0.79
3250 minetest.register_entity(name, {
3252 stepheight = def.stepheight or 0.6,
3253 name = name,
3254 type = def.type,
3255 attack_type = def.attack_type,
3256 fly = def.fly,
3257 fly_in = def.fly_in or {"air", "__airlike"},
3258 owner = def.owner or "",
3259 order = def.order or "",
3260 on_die = def.on_die,
3261 spawn_small_alternative = def.spawn_small_alternative,
3262 do_custom = def.do_custom,
3263 jump_height = def.jump_height or 4, -- was 6
3264 rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
3265 lifetimer = def.lifetimer or 57.73,
3266 hp_min = scale_difficulty(def.hp_min, 5, 1),
3267 hp_max = scale_difficulty(def.hp_max, 10, 1),
3268 breath_max = def.breath_max or 15,
3269 breathes_in_water = def.breathes_in_water or false,
3270 physical = true,
3271 collisionbox = collisionbox,
3272 selectionbox = def.selectionbox or def.collisionbox,
3273 visual = def.visual,
3274 visual_size = def.visual_size or {x = 1, y = 1},
3275 mesh = def.mesh,
3276 makes_footstep_sound = def.makes_footstep_sound or false,
3277 view_range = def.view_range or 16,
3278 walk_velocity = def.walk_velocity or 1,
3279 run_velocity = def.run_velocity or 2,
3280 damage = scale_difficulty(def.damage, 0, 0),
3281 light_damage = def.light_damage or 0,
3282 sunlight_damage = def.sunlight_damage or 0,
3283 water_damage = def.water_damage or 0,
3284 lava_damage = def.lava_damage or 8,
3285 fire_damage = def.fire_damage or 1,
3286 suffocation = def.suffocation or true,
3287 fall_damage = def.fall_damage or 1,
3288 fall_speed = def.fall_speed or -10, -- must be lower than -2 (default: -10)
3289 drops = def.drops or {},
3290 armor = def.armor or 100,
3291 on_rightclick = create_mob_on_rightclick(def.on_rightclick),
3292 arrow = def.arrow,
3293 shoot_interval = def.shoot_interval,
3294 sounds = def.sounds or {},
3295 animation = def.animation,
3296 follow = def.follow,
3297 jump = def.jump ~= false,
3298 walk_chance = def.walk_chance or 50,
3299 attacks_monsters = def.attacks_monsters or false,
3300 group_attack = def.group_attack or false,
3301 passive = def.passive or false,
3302 knock_back = def.knock_back ~= false,
3303 shoot_offset = def.shoot_offset or 0,
3304 floats = def.floats or 1, -- floats in water by default
3305 replace_rate = def.replace_rate,
3306 replace_what = def.replace_what,
3307 replace_with = def.replace_with,
3308 replace_offset = def.replace_offset or 0,
3309 on_replace = def.on_replace,
3310 timer = 0,
3311 env_damage_timer = 0, -- only used when state = "attack"
3312 tamed = false,
3313 pause_timer = 0,
3314 horny = false,
3315 hornytimer = 0,
3316 gotten = false,
3317 health = 0,
3318 reach = def.reach or 3,
3319 htimer = 0,
3320 texture_list = def.textures,
3321 child_texture = def.child_texture,
3322 docile_by_day = def.docile_by_day or false,
3323 time_of_day = 0.5,
3324 fear_height = def.fear_height or 0,
3325 runaway = def.runaway,
3326 runaway_timer = 0,
3327 pathfinding = def.pathfinding,
3328 immune_to = def.immune_to or {},
3329 explosion_radius = def.explosion_radius,
3330 explosion_damage_radius = def.explosion_damage_radius,
3331 explosion_timer = def.explosion_timer or 3,
3332 allow_fuse_reset = def.allow_fuse_reset ~= false,
3333 stop_to_explode = def.stop_to_explode ~= false,
3334 custom_attack = def.custom_attack,
3335 double_melee_attack = def.double_melee_attack,
3336 dogshoot_switch = def.dogshoot_switch,
3337 dogshoot_count = 0,
3338 dogshoot_count_max = def.dogshoot_count_max or 5,
3339 dogshoot_count2_max = def.dogshoot_count2_max or (def.dogshoot_count_max or 5),
3340 attack_animals = def.attack_animals or false,
3341 specific_attack = def.specific_attack,
3342 runaway_from = def.runaway_from,
3343 owner_loyal = def.owner_loyal,
3344 facing_fence = false,
3345 _cmi_is_mob = true,
3347 -- MCL2 extensions
3348 ignores_nametag = def.ignores_nametag or false,
3349 rain_damage = def.rain_damage or 0,
3350 glow = def.glow,
3351 can_despawn = can_despawn,
3352 child = def.child or false,
3353 texture_mods = {},
3354 shoot_arrow = def.shoot_arrow,
3355 sounds_child = def.sounds_child,
3356 -- End of MCL2 extensions
3358 on_spawn = def.on_spawn,
3360 on_blast = def.on_blast or do_tnt,
3362 on_step = mob_step,
3364 do_punch = def.do_punch,
3366 on_punch = mob_punch,
3368 on_breed = def.on_breed,
3370 on_grown = def.on_grown,
3372 on_detach_child = mob_detach_child,
3374 on_activate = function(self, staticdata, dtime)
3375 return mob_activate(self, staticdata, def, dtime)
3376 end,
3378 get_staticdata = function(self)
3379 return mob_staticdata(self)
3380 end,
3384 if minetest.get_modpath("doc_identifier") ~= nil then
3385 doc.sub.identifier.register_object(name, "basics", "mobs")
3388 end -- END mobs:register_mob function
3391 -- count how many mobs of one type are inside an area
3392 local count_mobs = function(pos, type)
3394 local num_type = 0
3395 local num_total = 0
3396 local objs = minetest.get_objects_inside_radius(pos, aoc_range)
3398 for n = 1, #objs do
3400 if not objs[n]:is_player() then
3402 local obj = objs[n]:get_luaentity()
3404 -- count mob type and add to total also
3405 if obj and obj.name and obj.name == type then
3407 num_type = num_type + 1
3408 num_total = num_total + 1
3410 -- add to total mobs
3411 elseif obj and obj.name and obj.health ~= nil then
3413 num_total = num_total + 1
3418 return num_type, num_total
3422 -- global functions
3424 function mobs:spawn_abm_check(pos, node, name)
3425 -- global function to add additional spawn checks
3426 -- return true to stop spawning mob
3430 function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
3431 interval, chance, aoc, min_height, max_height, day_toggle, on_spawn)
3433 -- Do mobs spawn at all?
3434 if not mobs_spawn then
3435 return
3438 -- chance/spawn number override in minetest.conf for registered mob
3439 local numbers = minetest.settings:get(name)
3441 if numbers then
3442 numbers = numbers:split(",")
3443 chance = tonumber(numbers[1]) or chance
3444 aoc = tonumber(numbers[2]) or aoc
3446 if chance == 0 then
3447 minetest.log("warning", string.format("[mobs] %s has spawning disabled", name))
3448 return
3451 minetest.log("action",
3452 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name, chance, aoc))
3456 local spawn_action
3457 spawn_action = function(pos, node, active_object_count, active_object_count_wider, name)
3459 local orig_pos = table.copy(pos)
3460 -- is mob actually registered?
3461 if not mobs.spawning_mobs[name]
3462 or not minetest.registered_entities[name] then
3463 minetest.log("warning", "Mob spawn of "..name.." failed, unknown entity or mob is not registered for spawning!")
3464 return
3467 -- additional custom checks for spawning mob
3468 if mobs:spawn_abm_check(pos, node, name) == true then
3469 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, ABM check rejected!")
3470 return
3473 -- do not spawn if too many of same mob in area
3474 if active_object_count_wider >= max_per_block
3475 or count_mobs(pos, name) >= aoc then
3476 -- too many entities
3477 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, too crowded!")
3478 return
3481 -- if toggle set to nil then ignore day/night check
3482 if day_toggle ~= nil then
3484 local tod = (minetest.get_timeofday() or 0) * 24000
3486 if tod > 4500 and tod < 19500 then
3487 -- daylight, but mob wants night
3488 if day_toggle == false then
3489 -- mob needs night
3490 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, mob needs light!")
3491 return
3493 else
3494 -- night time but mob wants day
3495 if day_toggle == true then
3496 -- mob needs day
3497 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, mob needs daylight!")
3498 return
3503 -- spawn above node
3504 pos.y = pos.y + 1
3506 -- only spawn away from player
3507 local objs = minetest.get_objects_inside_radius(pos, 10)
3509 for n = 1, #objs do
3511 if objs[n]:is_player() then
3512 -- player too close
3513 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, player too close!")
3514 return
3518 -- mobs cannot spawn in protected areas when enabled
3519 if not spawn_protected
3520 and minetest.is_protected(pos, "") then
3521 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, position is protected!")
3522 return
3525 -- are we spawning within height limits?
3526 if pos.y > max_height
3527 or pos.y < min_height then
3528 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, out of height limit!")
3529 return
3532 -- are light levels ok?
3533 local light = minetest.get_node_light(pos)
3534 if not light
3535 or light > max_light
3536 or light < min_light then
3537 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, bad light!")
3538 return
3541 -- do we have enough space to spawn mob?
3542 local ent = minetest.registered_entities[name]
3543 local width_x = max(1, math.ceil(ent.collisionbox[4] - ent.collisionbox[1]))
3544 local min_x, max_x
3545 if width_x % 2 == 0 then
3546 max_x = math.floor(width_x/2)
3547 min_x = -(max_x-1)
3548 else
3549 max_x = math.floor(width_x/2)
3550 min_x = -max_x
3553 local width_z = max(1, math.ceil(ent.collisionbox[6] - ent.collisionbox[3]))
3554 local min_z, max_z
3555 if width_z % 2 == 0 then
3556 max_z = math.floor(width_z/2)
3557 min_z = -(max_z-1)
3558 else
3559 max_z = math.floor(width_z/2)
3560 min_z = -max_z
3563 local max_y = max(0, math.ceil(ent.collisionbox[5] - ent.collisionbox[2]) - 1)
3565 for y = 0, max_y do
3566 for x = min_x, max_x do
3567 for z = min_z, max_z do
3568 local pos2 = {x = pos.x+x, y = pos.y+y, z = pos.z+z}
3569 if minetest.registered_nodes[node_ok(pos2).name].walkable == true then
3570 -- inside block
3571 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, too little space!")
3572 if ent.spawn_small_alternative ~= nil and (not minetest.registered_nodes[node_ok(pos).name].walkable) then
3573 minetest.log("info", "Trying to spawn smaller alternative mob: "..ent.spawn_small_alternative)
3574 spawn_action(orig_pos, node, active_object_count, active_object_count_wider, ent.spawn_small_alternative)
3576 return
3582 -- spawn mob 1/2 node above ground
3583 pos.y = pos.y + 0.5
3584 -- tweak X/Z spawn pos
3585 if width_x % 2 == 0 then
3586 pos.x = pos.x + 0.5
3588 if width_z % 2 == 0 then
3589 pos.z = pos.z + 0.5
3592 local mob = minetest.add_entity(pos, name)
3593 minetest.log("action", "Mob spawned: "..name.." at "..minetest.pos_to_string(pos))
3595 if on_spawn then
3597 local ent = mob:get_luaentity()
3599 on_spawn(ent, pos)
3603 local function spawn_abm_action(pos, node, active_object_count, active_object_count_wider)
3604 spawn_action(pos, node, active_object_count, active_object_count_wider, name)
3607 minetest.register_abm({
3608 label = name .. " spawning",
3609 nodenames = nodes,
3610 neighbors = neighbors,
3611 interval = interval,
3612 chance = floor(max(1, chance * mobs_spawn_chance)),
3613 catch_up = false,
3614 action = spawn_abm_action,
3619 -- compatibility with older mob registration
3620 function mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
3622 mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
3623 chance, active_object_count, -31000, max_height, day_toggle)
3627 -- MarkBu's spawn function
3628 function mobs:spawn(def)
3630 local name = def.name
3631 local nodes = def.nodes or {"group:soil", "group:stone"}
3632 local neighbors = def.neighbors or {"air"}
3633 local min_light = def.min_light or 0
3634 local max_light = def.max_light or 15
3635 local interval = def.interval or 30
3636 local chance = def.chance or 5000
3637 local active_object_count = def.active_object_count or 1
3638 local min_height = def.min_height or -31000
3639 local max_height = def.max_height or 31000
3640 local day_toggle = def.day_toggle
3641 local on_spawn = def.on_spawn
3643 mobs:spawn_specific(name, nodes, neighbors, min_light, max_light, interval,
3644 chance, active_object_count, min_height, max_height, day_toggle, on_spawn)
3648 -- register arrow for shoot attack
3649 function mobs:register_arrow(name, def)
3651 if not name or not def then return end -- errorcheck
3653 minetest.register_entity(name, {
3655 physical = false,
3656 visual = def.visual,
3657 visual_size = def.visual_size,
3658 textures = def.textures,
3659 velocity = def.velocity,
3660 hit_player = def.hit_player,
3661 hit_node = def.hit_node,
3662 hit_mob = def.hit_mob,
3663 hit_object = def.hit_object,
3664 drop = def.drop or false, -- drops arrow as registered item when true
3665 collisionbox = {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3666 timer = 0,
3667 switch = 0,
3668 owner_id = def.owner_id,
3669 rotate = def.rotate,
3670 automatic_face_movement_dir = def.rotate
3671 and (def.rotate - (pi / 180)) or false,
3673 on_activate = def.on_activate,
3675 on_step = def.on_step or function(self, dtime)
3677 self.timer = self.timer + 1
3679 local pos = self.object:get_pos()
3681 if self.switch == 0
3682 or self.timer > 150
3683 or not within_limits(pos, 0) then
3685 self.object:remove();
3687 return
3690 -- does arrow have a tail (fireball)
3691 if def.tail
3692 and def.tail == 1
3693 and def.tail_texture then
3695 minetest.add_particle({
3696 pos = pos,
3697 velocity = {x = 0, y = 0, z = 0},
3698 acceleration = {x = 0, y = 0, z = 0},
3699 expirationtime = def.expire or 0.25,
3700 collisiondetection = false,
3701 texture = def.tail_texture,
3702 size = def.tail_size or 5,
3703 glow = def.glow or 0,
3707 if self.hit_node then
3709 local node = node_ok(pos).name
3711 if minetest.registered_nodes[node].walkable then
3713 self.hit_node(self, pos, node)
3715 if self.drop == true then
3717 pos.y = pos.y + 1
3719 self.lastpos = (self.lastpos or pos)
3721 minetest.add_item(self.lastpos, self.object:get_luaentity().name)
3724 self.object:remove();
3726 return
3730 if self.hit_player or self.hit_mob or self.hit_object then
3732 for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do
3734 if self.hit_player
3735 and player:is_player() then
3737 self.hit_player(self, player)
3738 self.object:remove();
3739 return
3742 local entity = player:get_luaentity()
3744 if entity
3745 and self.hit_mob
3746 and entity._cmi_is_mob == true
3747 and tostring(player) ~= self.owner_id
3748 and entity.name ~= self.object:get_luaentity().name then
3749 self.hit_mob(self, player)
3750 self.object:remove();
3751 return
3754 if entity
3755 and self.hit_object
3756 and (not entity._cmi_is_mob)
3757 and tostring(player) ~= self.owner_id
3758 and entity.name ~= self.object:get_luaentity().name then
3759 self.hit_object(self, player)
3760 self.object:remove();
3761 return
3766 self.lastpos = pos
3772 -- no damage to nodes explosion
3773 function mobs:safe_boom(self, pos, radius)
3775 minetest.sound_play(self.sounds and self.sounds.explode or "tnt_explode", {
3776 pos = pos,
3777 gain = 1.0,
3778 max_hear_distance = self.sounds and self.sounds.distance or 32
3781 entity_physics(pos, radius)
3782 effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
3786 -- make explosion with protection and tnt mod check
3787 function mobs:boom(self, pos, radius)
3789 if mobs_griefing
3790 and mod_tnt and tnt and tnt.boom
3791 and not minetest.is_protected(pos, "") then
3793 tnt.boom(pos, {
3794 radius = radius,
3795 damage_radius = radius,
3796 sound = self.sounds and self.sounds.explode,
3797 explode_center = true,
3798 is_tnt = false,
3800 else
3801 mobs:safe_boom(self, pos, radius)
3806 -- Register spawn eggs
3808 -- Note: This also introduces the “spawn_egg” group:
3809 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3810 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3811 function mobs:register_egg(mob, desc, background, addegg, no_creative)
3813 local grp = {spawn_egg = 1}
3815 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3816 if creative and no_creative == true then
3817 grp.not_in_creative_inventory = 1
3820 local invimg = background
3822 if addegg == 1 then
3823 invimg = "mobs_chicken_egg.png^(" .. invimg ..
3824 "^[mask:mobs_chicken_egg_overlay.png)"
3827 -- register old stackable mob egg
3828 minetest.register_craftitem(mob, {
3830 description = desc,
3831 inventory_image = invimg,
3832 groups = grp,
3834 _doc_items_longdesc = S("This allows you to place a single mob."),
3835 _doc_items_usagehelp = S("Just place it where you want the mob to appear. Animals will spawn tamed, unless you hold down the sneak key while placing. If you place this on a mob spawner, you change the mob it spawns."),
3837 on_place = function(itemstack, placer, pointed_thing)
3839 local pos = pointed_thing.above
3841 -- am I clicking on something with existing on_rightclick function?
3842 local under = minetest.get_node(pointed_thing.under)
3843 local def = minetest.registered_nodes[under.name]
3844 if def and def.on_rightclick then
3845 return def.on_rightclick(pointed_thing.under, under, placer, itemstack)
3848 if pos
3849 and within_limits(pos, 0)
3850 and not minetest.is_protected(pos, placer:get_player_name()) then
3852 local name = placer:get_player_name()
3853 local privs = minetest.get_player_privs(name)
3854 if mod_mobspawners and under.name == "mcl_mobspawners:spawner" then
3855 if minetest.is_protected(pointed_thing.under, name) then
3856 minetest.record_protection_violation(pointed_thing.under, name)
3857 return itemstack
3859 if not privs.maphack then
3860 minetest.chat_send_player(name, S("You need the “maphack” privilege to change the mob spawner."))
3861 return itemstack
3863 mcl_mobspawners.setup_spawner(pointed_thing.under, itemstack:get_name())
3864 if not minetest.settings:get_bool("creative_mode") then
3865 itemstack:take_item()
3867 return itemstack
3870 if not minetest.registered_entities[mob] then
3871 return itemstack
3874 if minetest.settings:get_bool("only_peaceful_mobs", false)
3875 and minetest.registered_entities[mob].type == "monster" then
3876 minetest.chat_send_player(name, S("Only peaceful mobs allowed!"))
3877 return itemstack
3880 pos.y = pos.y + 1
3882 local mob = minetest.add_entity(pos, mob)
3883 local ent = mob:get_luaentity()
3885 -- don't set owner if monster or sneak pressed
3886 if ent.type ~= "monster"
3887 and not placer:get_player_control().sneak then
3888 ent.owner = placer:get_player_name()
3889 ent.tamed = true
3892 -- set nametag
3893 local nametag = itemstack:get_meta():get_string("name")
3894 if nametag ~= "" then
3895 if string.len(nametag) > MAX_MOB_NAME_LENGTH then
3896 nametag = string.sub(nametag, 1, MAX_MOB_NAME_LENGTH)
3898 ent.nametag = nametag
3899 update_tag(ent)
3902 -- if not in creative then take item
3903 if not mobs.is_creative(placer:get_player_name()) then
3904 itemstack:take_item()
3908 return itemstack
3909 end,
3915 -- No-op in MCL2 (capturing mobs is not possible).
3916 -- Provided for compability with Mobs Redo
3917 function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
3918 return false
3922 -- No-op in MCL2 (protecting mobs is not possible).
3923 function mobs:protect(self, clicker)
3924 return false
3928 -- feeding, taming and breeding (thanks blert2112)
3929 function mobs:feed_tame(self, clicker, feed_count, breed, tame)
3930 if not self.follow then
3931 return false
3934 -- can eat/tame with item in hand
3935 if follow_holding(self, clicker) then
3937 -- if not in creative then take item
3938 if not mobs.is_creative(clicker:get_player_name()) then
3940 local item = clicker:get_wielded_item()
3942 item:take_item()
3944 clicker:set_wielded_item(item)
3947 -- increase health
3948 self.health = self.health + 4
3950 if self.health >= self.hp_max then
3952 self.health = self.hp_max
3954 if self.htimer < 1 then
3955 self.htimer = 5
3959 self.object:set_hp(self.health)
3961 update_tag(self)
3963 -- make children grow quicker
3964 if self.child == true then
3966 self.hornytimer = self.hornytimer + 20
3968 return true
3971 -- feed and tame
3972 self.food = (self.food or 0) + 1
3973 if self.food >= feed_count then
3975 self.food = 0
3977 if breed and self.hornytimer == 0 then
3978 self.horny = true
3981 if tame then
3983 self.tamed = true
3985 if not self.owner or self.owner == "" then
3986 self.owner = clicker:get_player_name()
3990 -- make sound when fed so many times
3991 mob_sound(self, "random", true)
3994 return true
3997 return false
4000 -- Spawn a child
4001 function mobs:spawn_child(pos, mob_type)
4002 local child = minetest.add_entity(pos, mob_type)
4003 if not child then
4004 return
4007 local ent = child:get_luaentity()
4008 effect(pos, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
4010 ent.child = true
4012 local textures
4013 -- using specific child texture (if found)
4014 if ent.child_texture then
4015 textures = ent.child_texture[1]
4018 -- and resize to half height
4019 child:set_properties({
4020 textures = textures,
4021 visual_size = {
4022 x = ent.base_size.x * .5,
4023 y = ent.base_size.y * .5,
4025 collisionbox = {
4026 ent.base_colbox[1] * .5,
4027 ent.base_colbox[2] * .5,
4028 ent.base_colbox[3] * .5,
4029 ent.base_colbox[4] * .5,
4030 ent.base_colbox[5] * .5,
4031 ent.base_colbox[6] * .5,
4033 selectionbox = {
4034 ent.base_selbox[1] * .5,
4035 ent.base_selbox[2] * .5,
4036 ent.base_selbox[3] * .5,
4037 ent.base_selbox[4] * .5,
4038 ent.base_selbox[5] * .5,
4039 ent.base_selbox[6] * .5,
4043 return child
4047 -- compatibility function for old entities to new modpack entities
4048 function mobs:alias_mob(old_name, new_name)
4050 -- spawn egg
4051 minetest.register_alias(old_name, new_name)
4053 -- entity
4054 minetest.register_entity(":" .. old_name, {
4056 physical = false,
4058 on_step = function(self)
4060 if minetest.registered_entities[new_name] then
4061 minetest.add_entity(self.object:get_pos(), new_name)
4064 self.object:remove()