Another nil check in mcl_mobs
[MineClone/MineClone2.git] / mods / ENTITIES / mcl_mobs / api.lua
blob8244624c209bafe416e7ff10026966274a6f2436
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 false
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 fire or lava
593 if cause == "lava" or cause == "fire" 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 true
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
759 return true
764 -- bright light harms mob
765 if self.light_damage ~= 0 and (minetest.get_node_light(pos) or 0) > 12 then
766 deal_light_damage(self, pos, self.light_damage)
768 local _, dim = nil, "overworld"
769 if mod_worlds then
770 _, dim = mcl_worlds.y_to_layer(pos.y)
772 if self.sunlight_damage ~= 0 and (minetest.get_node_light(pos) or 0) >= minetest.LIGHT_MAX and dim == "overworld" then
773 deal_light_damage(self, pos, self.sunlight_damage)
776 local y_level = self.collisionbox[2]
778 if self.child then
779 y_level = self.collisionbox[2] * 0.5
782 -- what is mob standing in?
783 pos.y = pos.y + y_level + 0.25 -- foot level
784 local pos2 = {x=pos.x, y=pos.y-1, z=pos.z}
785 self.standing_in = node_ok(pos, "air").name
786 self.standing_on = node_ok(pos2, "air").name
788 -- don't fall when on ignore, just stand still
789 if self.standing_in == "ignore" then
790 self.object:set_velocity({x = 0, y = 0, z = 0})
793 local nodef = minetest.registered_nodes[self.standing_in]
795 -- rain
796 if self.rain_damage > 0 and mod_weather then
797 if mcl_weather.rain.raining and mcl_weather.is_outdoor(pos) then
799 self.health = self.health - self.rain_damage
801 if check_for_death(self, "rain", {type = "environment",
802 pos = pos, node = self.standing_in}) then
803 return true
808 pos.y = pos.y + 1 -- for particle effect position
810 -- water damage
811 if self.water_damage > 0
812 and nodef.groups.water then
814 if self.water_damage ~= 0 then
816 self.health = self.health - self.water_damage
818 effect(pos, 5, "tnt_smoke.png", nil, nil, 1, nil)
820 if check_for_death(self, "water", {type = "environment",
821 pos = pos, node = self.standing_in}) then
822 return true
826 -- lava damage
827 elseif self.lava_damage > 0
828 and (nodef.groups.lava) then
830 if self.lava_damage ~= 0 then
832 self.health = self.health - self.lava_damage
834 effect(pos, 5, "fire_basic_flame.png", nil, nil, 1, nil)
836 if check_for_death(self, "lava", {type = "environment",
837 pos = pos, node = self.standing_in}) then
838 return true
842 -- fire damage
843 elseif self.fire_damage > 0
844 and (nodef.groups.fire) then
846 if self.fire_damage ~= 0 then
848 self.health = self.health - self.fire_damage
850 effect(pos, 5, "fire_basic_flame.png", nil, nil, 1, nil)
852 if check_for_death(self, "fire", {type = "environment",
853 pos = pos, node = self.standing_in}) then
854 return true
858 -- damage_per_second node check
859 elseif nodef.damage_per_second ~= 0 then
861 self.health = self.health - nodef.damage_per_second
863 effect(pos, 5, "tnt_smoke.png")
865 if check_for_death(self, "dps", {type = "environment",
866 pos = pos, node = self.standing_in}) then
867 return true
871 -- Drowning damage
872 if self.breath_max ~= -1 then
873 local drowning = false
874 if self.breathes_in_water then
875 if minetest.get_item_group(self.standing_in, "water") == 0 then
876 drowning = true
878 elseif nodef.drowning > 0 then
879 drowning = true
881 if drowning then
883 self.breath = math.max(0, self.breath - 1)
885 effect(pos, 2, "bubble.png", nil, nil, 1, nil)
886 if self.breath <= 0 then
887 local dmg
888 if nodef.drowning > 0 then
889 dmg = nodef.drowning
890 else
891 dmg = 4
893 damage_effect(self, dmg)
894 self.health = self.health - dmg
896 if check_for_death(self, "drowning", {type = "environment",
897 pos = pos, node = self.standing_in}) then
898 return true
900 else
901 self.breath = math.min(self.breath_max, self.breath + 1)
905 --- suffocation inside solid node
906 -- FIXME: Redundant with mcl_playerplus
907 if (self.suffocation == true)
908 and (nodef.walkable == nil or nodef.walkable == true)
909 and (nodef.collision_box == nil or nodef.collision_box.type == "regular")
910 and (nodef.node_box == nil or nodef.node_box.type == "regular")
911 and (nodef.groups.disable_suffocation ~= 1)
912 and (nodef.groups.opaque == 1) then
914 -- 2 damage per second
915 -- TODO: Deal this damage once every 1/2 second
916 self.health = self.health - 2
918 if check_for_death(self, "suffocation", {type = "environment",
919 pos = pos, node = self.standing_in}) then
920 return true
924 return check_for_death(self, "", {type = "unknown"})
928 -- jump if facing a solid node (not fences or gates)
929 local do_jump = function(self)
931 if not self.jump
932 or self.jump_height == 0
933 or self.fly
934 or (self.child and self.type ~= "monster")
935 or self.order == "stand" then
936 return false
939 self.facing_fence = false
941 -- something stopping us while moving?
942 if self.state ~= "stand"
943 and get_velocity(self) > 0.5
944 and self.object:get_velocity().y ~= 0 then
945 return false
948 local pos = self.object:get_pos()
949 local yaw = self.object:get_yaw()
951 -- what is mob standing on?
952 pos.y = pos.y + self.collisionbox[2] - 0.2
954 local nod = node_ok(pos)
956 if minetest.registered_nodes[nod.name].walkable == false then
957 return false
960 -- where is front
961 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
962 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
964 -- what is in front of mob?
965 nod = node_ok({
966 x = pos.x + dir_x,
967 y = pos.y + 0.5,
968 z = pos.z + dir_z
971 -- this is used to detect if there's a block on top of the block in front of the mob.
972 -- If there is, there is no point in jumping as we won't manage.
973 local nodTop = node_ok({
974 x = pos.x + dir_x,
975 y = pos.y + 1.5,
976 z = pos.z + dir_z
977 }, "air")
979 -- we don't attempt to jump if there's a stack of blocks blocking
980 if minetest.registered_nodes[nodTop.name] == true then
981 return false
984 -- thin blocks that do not need to be jumped
985 if nod.name == node_snow then
986 return false
989 if self.walk_chance == 0
990 or minetest.registered_items[nod.name].walkable then
992 if minetest.get_item_group(nod.name, "fence") == 0
993 and minetest.get_item_group(nod.name, "fence_gate") == 0
994 and minetest.get_item_group(nod.name, "wall") == 0 then
996 local v = self.object:get_velocity()
998 v.y = self.jump_height
1000 set_animation(self, "jump") -- only when defined
1002 self.object:set_velocity(v)
1004 -- when in air move forward
1005 minetest.after(0.3, function(self, v)
1006 if not self.object or not self.object:get_luaentity() then
1007 return
1009 self.object:set_acceleration({
1010 x = v.x * 2,
1011 y = 0,
1012 z = v.z * 2,
1014 end, self, v)
1016 if self.jump_sound_cooloff <= 0 then
1017 mob_sound(self, "jump")
1018 self.jump_sound_cooloff = 0.5
1020 else
1021 self.facing_fence = true
1024 -- if we jumped against a block/wall 4 times then turn
1025 if self.object:get_velocity().x ~= 0
1026 and self.object:get_velocity().z ~= 0 then
1028 self.jump_count = (self.jump_count or 0) + 1
1030 if self.jump_count == 4 then
1032 local yaw = self.object:get_yaw() or 0
1034 yaw = set_yaw(self, yaw + 1.35, 8)
1036 self.jump_count = 0
1040 return true
1043 return false
1047 -- blast damage to entities nearby (modified from TNT mod)
1048 local entity_physics = function(pos, radius)
1050 radius = radius * 2
1052 local objs = minetest.get_objects_inside_radius(pos, radius)
1053 local obj_pos, dist
1055 for n = 1, #objs do
1057 obj_pos = objs[n]:get_pos()
1059 dist = vector.distance(pos, obj_pos)
1060 if dist < 1 then dist = 1 end
1062 local damage = floor((4 / dist) * radius)
1063 local ent = objs[n]:get_luaentity()
1065 -- punches work on entities AND players
1066 objs[n]:punch(objs[n], 1.0, {
1067 full_punch_interval = 1.0,
1068 damage_groups = {fleshy = damage},
1069 }, pos)
1074 -- should mob follow what I'm holding ?
1075 local follow_holding = function(self, clicker)
1077 if mobs.invis[clicker:get_player_name()] then
1078 return false
1081 local item = clicker:get_wielded_item()
1082 local t = type(self.follow)
1084 -- single item
1085 if t == "string"
1086 and item:get_name() == self.follow then
1087 return true
1089 -- multiple items
1090 elseif t == "table" then
1092 for no = 1, #self.follow do
1094 if self.follow[no] == item:get_name() then
1095 return true
1100 return false
1104 -- find two animals of same type and breed if nearby and horny
1105 local breed = function(self)
1107 -- child takes 240 seconds before growing into adult
1108 if self.child == true then
1110 self.hornytimer = self.hornytimer + 1
1112 if self.hornytimer > 240 then
1114 self.child = false
1115 self.hornytimer = 0
1117 self.object:set_properties({
1118 textures = self.base_texture,
1119 mesh = self.base_mesh,
1120 visual_size = self.base_size,
1121 collisionbox = self.base_colbox,
1122 selectionbox = self.base_selbox,
1125 -- custom function when child grows up
1126 if self.on_grown then
1127 self.on_grown(self)
1128 else
1129 -- jump when fully grown so as not to fall into ground
1130 self.object:set_velocity({
1131 x = 0,
1132 y = self.jump_height,
1133 z = 0
1138 return
1141 -- horny animal can mate for 40 seconds,
1142 -- afterwards horny animal cannot mate again for 200 seconds
1143 if self.horny == true
1144 and self.hornytimer < 240 then
1146 self.hornytimer = self.hornytimer + 1
1148 if self.hornytimer >= 240 then
1149 self.hornytimer = 0
1150 self.horny = false
1154 -- find another same animal who is also horny and mate if nearby
1155 if self.horny == true
1156 and self.hornytimer <= 40 then
1158 local pos = self.object:get_pos()
1160 effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, "heart.png", 3, 4, 1, 0.1)
1162 local objs = minetest.get_objects_inside_radius(pos, 3)
1163 local num = 0
1164 local ent = nil
1166 for n = 1, #objs do
1168 ent = objs[n]:get_luaentity()
1170 -- check for same animal with different colour
1171 local canmate = false
1173 if ent then
1175 if ent.name == self.name then
1176 canmate = true
1177 else
1178 local entname = string.split(ent.name,":")
1179 local selfname = string.split(self.name,":")
1181 if entname[1] == selfname[1] then
1182 entname = string.split(entname[2],"_")
1183 selfname = string.split(selfname[2],"_")
1185 if entname[1] == selfname[1] then
1186 canmate = true
1192 if ent
1193 and canmate == true
1194 and ent.horny == true
1195 and ent.hornytimer <= 40 then
1196 num = num + 1
1199 -- found your mate? then have a baby
1200 if num > 1 then
1202 self.hornytimer = 41
1203 ent.hornytimer = 41
1205 -- spawn baby
1206 minetest.after(5, function(parent1, parent2, pos)
1207 if not parent1.object:get_luaentity() then
1208 return
1210 if not parent2.object:get_luaentity() then
1211 return
1214 -- custom breed function
1215 if parent1.on_breed then
1216 -- when false, skip going any further
1217 if parent1.on_breed(parent1, parent2) == false then
1218 return
1222 local child = mobs:spawn_child(pos, parent1.name)
1224 local ent_c = child:get_luaentity()
1227 -- Use texture of one of the parents
1228 local p = math.random(1, 2)
1229 if p == 1 then
1230 ent_c.base_texture = parent1.base_texture
1231 else
1232 ent_c.base_texture = parent2.base_texture
1234 child:set_properties({
1235 textures = ent_c.base_texture
1238 -- tamed and owned by parents' owner
1239 ent_c.tamed = true
1240 ent_c.owner = parent1.owner
1241 end, self, ent, pos)
1243 num = 0
1245 break
1252 -- find and replace what mob is looking for (grass, wheat etc.)
1253 local replace = function(self, pos)
1255 if not self.replace_rate
1256 or not self.replace_what
1257 or self.child == true
1258 or self.object:get_velocity().y ~= 0
1259 or random(1, self.replace_rate) > 1 then
1260 return
1263 local what, with, y_offset
1265 if type(self.replace_what[1]) == "table" then
1267 local num = random(#self.replace_what)
1269 what = self.replace_what[num][1] or ""
1270 with = self.replace_what[num][2] or ""
1271 y_offset = self.replace_what[num][3] or 0
1272 else
1273 what = self.replace_what
1274 with = self.replace_with or ""
1275 y_offset = self.replace_offset or 0
1278 pos.y = pos.y + y_offset
1280 if #minetest.find_nodes_in_area(pos, pos, what) > 0 then
1282 local oldnode = {name = what}
1283 local newnode = {name = with}
1284 local on_replace_return
1286 if self.on_replace then
1287 on_replace_return = self.on_replace(self, pos, oldnode, newnode)
1290 if on_replace_return ~= false then
1292 if mobs_griefing then
1293 minetest.set_node(pos, {name = with})
1301 -- check if daytime and also if mob is docile during daylight hours
1302 local day_docile = function(self)
1304 if self.docile_by_day == false then
1306 return false
1308 elseif self.docile_by_day == true
1309 and self.time_of_day > 0.2
1310 and self.time_of_day < 0.8 then
1312 return true
1317 local los_switcher = false
1318 local height_switcher = false
1320 -- path finding and smart mob routine by rnd, line_of_sight and other edits by Elkien3
1321 local smart_mobs = function(self, s, p, dist, dtime)
1323 local s1 = self.path.lastpos
1325 local target_pos = self.attack:get_pos()
1327 -- is it becoming stuck?
1328 if abs(s1.x - s.x) + abs(s1.z - s.z) < .5 then
1329 self.path.stuck_timer = self.path.stuck_timer + dtime
1330 else
1331 self.path.stuck_timer = 0
1334 self.path.lastpos = {x = s.x, y = s.y, z = s.z}
1336 local use_pathfind = false
1337 local has_lineofsight = minetest.line_of_sight(
1338 {x = s.x, y = (s.y) + .5, z = s.z},
1339 {x = target_pos.x, y = (target_pos.y) + 1.5, z = target_pos.z}, .2)
1341 -- im stuck, search for path
1342 if not has_lineofsight then
1344 if los_switcher == true then
1345 use_pathfind = true
1346 los_switcher = false
1347 end -- cannot see target!
1348 else
1349 if los_switcher == false then
1351 los_switcher = true
1352 use_pathfind = false
1354 minetest.after(1, function(self)
1355 if not self.object:get_luaentity() then
1356 return
1358 if has_lineofsight then self.path.following = false end
1359 end, self)
1360 end -- can see target!
1363 if (self.path.stuck_timer > stuck_timeout and not self.path.following) then
1365 use_pathfind = true
1366 self.path.stuck_timer = 0
1368 minetest.after(1, function(self)
1369 if not self.object:get_luaentity() then
1370 return
1372 if has_lineofsight then self.path.following = false end
1373 end, self)
1376 if (self.path.stuck_timer > stuck_path_timeout and self.path.following) then
1378 use_pathfind = true
1379 self.path.stuck_timer = 0
1381 minetest.after(1, function(self)
1382 if not self.object:get_luaentity() then
1383 return
1385 if has_lineofsight then self.path.following = false end
1386 end, self)
1389 if math.abs(vector.subtract(s,target_pos).y) > self.stepheight then
1391 if height_switcher then
1392 use_pathfind = true
1393 height_switcher = false
1395 else
1396 if not height_switcher then
1397 use_pathfind = false
1398 height_switcher = true
1402 if use_pathfind then
1403 -- lets try find a path, first take care of positions
1404 -- since pathfinder is very sensitive
1405 local sheight = self.collisionbox[5] - self.collisionbox[2]
1407 -- round position to center of node to avoid stuck in walls
1408 -- also adjust height for player models!
1409 s.x = floor(s.x + 0.5)
1410 s.z = floor(s.z + 0.5)
1412 local ssight, sground = minetest.line_of_sight(s, {
1413 x = s.x, y = s.y - 4, z = s.z}, 1)
1415 -- determine node above ground
1416 if not ssight then
1417 s.y = sground.y + 1
1420 local p1 = self.attack:get_pos()
1422 p1.x = floor(p1.x + 0.5)
1423 p1.y = floor(p1.y + 0.5)
1424 p1.z = floor(p1.z + 0.5)
1426 local dropheight = 12
1427 if self.fear_height ~= 0 then dropheight = self.fear_height end
1428 local jumpheight = 0
1429 if self.jump and self.jump_height >= 4 then
1430 jumpheight = math.min(math.ceil(self.jump_height / 4), 4)
1431 elseif self.stepheight > 0.5 then
1432 jumpheight = 1
1434 self.path.way = minetest.find_path(s, p1, 16, jumpheight, dropheight, "A*_noprefetch")
1436 self.state = ""
1437 do_attack(self, self.attack)
1439 -- no path found, try something else
1440 if not self.path.way then
1442 self.path.following = false
1444 -- lets make way by digging/building if not accessible
1445 if self.pathfinding == 2 and mobs_griefing then
1447 -- is player higher than mob?
1448 if s.y < p1.y then
1450 -- build upwards
1451 if not minetest.is_protected(s, "") then
1453 local ndef1 = minetest.registered_nodes[self.standing_in]
1455 if ndef1 and (ndef1.buildable_to or ndef1.groups.liquid) then
1457 minetest.set_node(s, {name = mobs.fallback_node})
1461 local sheight = math.ceil(self.collisionbox[5]) + 1
1463 -- assume mob is 2 blocks high so it digs above its head
1464 s.y = s.y + sheight
1466 -- remove one block above to make room to jump
1467 if not minetest.is_protected(s, "") then
1469 local node1 = node_ok(s, "air").name
1470 local ndef1 = minetest.registered_nodes[node1]
1472 if node1 ~= "air"
1473 and node1 ~= "ignore"
1474 and ndef1
1475 and not ndef1.groups.level
1476 and not ndef1.groups.unbreakable
1477 and not ndef1.groups.liquid then
1479 minetest.set_node(s, {name = "air"})
1480 minetest.add_item(s, ItemStack(node1))
1485 s.y = s.y - sheight
1486 self.object:set_pos({x = s.x, y = s.y + 2, z = s.z})
1488 else -- dig 2 blocks to make door toward player direction
1490 local yaw1 = self.object:get_yaw() + pi / 2
1491 local p1 = {
1492 x = s.x + cos(yaw1),
1493 y = s.y,
1494 z = s.z + sin(yaw1)
1497 if not minetest.is_protected(p1, "") then
1499 local node1 = node_ok(p1, "air").name
1500 local ndef1 = minetest.registered_nodes[node1]
1502 if node1 ~= "air"
1503 and node1 ~= "ignore"
1504 and ndef1
1505 and not ndef1.groups.level
1506 and not ndef1.groups.unbreakable
1507 and not ndef1.groups.liquid then
1509 minetest.add_item(p1, ItemStack(node1))
1510 minetest.set_node(p1, {name = "air"})
1513 p1.y = p1.y + 1
1514 node1 = node_ok(p1, "air").name
1515 ndef1 = minetest.registered_nodes[node1]
1517 if node1 ~= "air"
1518 and node1 ~= "ignore"
1519 and ndef1
1520 and not ndef1.groups.level
1521 and not ndef1.groups.unbreakable
1522 and not ndef1.groups.liquid then
1524 minetest.add_item(p1, ItemStack(node1))
1525 minetest.set_node(p1, {name = "air"})
1532 -- will try again in 2 seconds
1533 self.path.stuck_timer = stuck_timeout - 2
1534 elseif s.y < p1.y and (not self.fly) then
1535 do_jump(self) --add jump to pathfinding
1536 self.path.following = true
1537 -- Yay, I found path!
1538 -- TODO: Implement war_cry sound without being annoying
1539 --mob_sound(self, "war_cry", true)
1540 else
1541 set_velocity(self, self.walk_velocity)
1543 -- follow path now that it has it
1544 self.path.following = true
1550 -- specific attacks
1551 local specific_attack = function(list, what)
1553 -- no list so attack default (player, animals etc.)
1554 if list == nil then
1555 return true
1558 -- found entity on list to attack?
1559 for no = 1, #list do
1561 if list[no] == what then
1562 return true
1566 return false
1569 -- monster find someone to attack
1570 local monster_attack = function(self)
1572 if self.type ~= "monster"
1573 or not damage_enabled
1574 or creative
1575 or self.state == "attack"
1576 or day_docile(self) then
1577 return
1580 local s = self.object:get_pos()
1581 local p, sp, dist
1582 local player, obj, min_player
1583 local type, name = "", ""
1584 local min_dist = self.view_range + 1
1585 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1587 for n = 1, #objs do
1589 if objs[n]:is_player() then
1591 if mobs.invis[ objs[n]:get_player_name() ] or (not object_in_range(self, objs[n])) then
1592 type = ""
1593 else
1594 player = objs[n]
1595 type = "player"
1596 name = "player"
1598 else
1599 obj = objs[n]:get_luaentity()
1601 if obj then
1602 player = obj.object
1603 type = obj.type
1604 name = obj.name or ""
1608 -- find specific mob to attack, failing that attack player/npc/animal
1609 if specific_attack(self.specific_attack, name)
1610 and (type == "player" or type == "npc"
1611 or (type == "animal" and self.attack_animals == true)) then
1613 p = player:get_pos()
1614 sp = s
1616 dist = vector.distance(p, s)
1618 -- aim higher to make looking up hills more realistic
1619 p.y = p.y + 1
1620 sp.y = sp.y + 1
1623 -- choose closest player to attack
1624 if dist < min_dist
1625 and line_of_sight(self, sp, p, 2) == true then
1626 min_dist = dist
1627 min_player = player
1632 -- attack player
1633 if min_player then
1634 do_attack(self, min_player)
1639 -- npc, find closest monster to attack
1640 local npc_attack = function(self)
1642 if self.type ~= "npc"
1643 or not self.attacks_monsters
1644 or self.state == "attack" then
1645 return
1648 local p, sp, obj, min_player
1649 local s = self.object:get_pos()
1650 local min_dist = self.view_range + 1
1651 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1653 for n = 1, #objs do
1655 obj = objs[n]:get_luaentity()
1657 if obj and obj.type == "monster" then
1659 p = obj.object:get_pos()
1660 sp = s
1662 local dist = vector.distance(p, s)
1664 -- aim higher to make looking up hills more realistic
1665 p.y = p.y + 1
1666 sp.y = sp.y + 1
1668 if dist < min_dist
1669 and line_of_sight(self, sp, p, 2) == true then
1670 min_dist = dist
1671 min_player = obj.object
1676 if min_player then
1677 do_attack(self, min_player)
1682 -- specific runaway
1683 local specific_runaway = function(list, what)
1685 -- no list so do not run
1686 if list == nil then
1687 return false
1690 -- found entity on list to attack?
1691 for no = 1, #list do
1693 if list[no] == what then
1694 return true
1698 return false
1702 -- find someone to runaway from
1703 local runaway_from = function(self)
1705 if not self.runaway_from then
1706 return
1709 local s = self.object:get_pos()
1710 local p, sp, dist
1711 local player, obj, min_player
1712 local type, name = "", ""
1713 local min_dist = self.view_range + 1
1714 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1716 for n = 1, #objs do
1718 if objs[n]:is_player() then
1720 if mobs.invis[ objs[n]:get_player_name() ]
1721 or self.owner == objs[n]:get_player_name()
1722 or (not object_in_range(self, objs[n])) then
1723 type = ""
1724 else
1725 player = objs[n]
1726 type = "player"
1727 name = "player"
1729 else
1730 obj = objs[n]:get_luaentity()
1732 if obj then
1733 player = obj.object
1734 type = obj.type
1735 name = obj.name or ""
1739 -- find specific mob to runaway from
1740 if name ~= "" and name ~= self.name
1741 and specific_runaway(self.runaway_from, name) then
1743 p = player:get_pos()
1744 sp = s
1746 -- aim higher to make looking up hills more realistic
1747 p.y = p.y + 1
1748 sp.y = sp.y + 1
1750 dist = vector.distance(p, s)
1753 -- choose closest player/mpb to runaway from
1754 if dist < min_dist
1755 and line_of_sight(self, sp, p, 2) == true then
1756 min_dist = dist
1757 min_player = player
1762 if min_player then
1764 local lp = player:get_pos()
1765 local vec = {
1766 x = lp.x - s.x,
1767 y = lp.y - s.y,
1768 z = lp.z - s.z
1771 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
1773 if lp.x > s.x then
1774 yaw = yaw + pi
1777 yaw = set_yaw(self, yaw, 4)
1778 self.state = "runaway"
1779 self.runaway_timer = 3
1780 self.following = nil
1785 -- follow player if owner or holding item, if fish outta water then flop
1786 local follow_flop = function(self)
1788 -- find player to follow
1789 if (self.follow ~= ""
1790 or self.order == "follow")
1791 and not self.following
1792 and self.state ~= "attack"
1793 and self.state ~= "runaway" then
1795 local s = self.object:get_pos()
1796 local players = minetest.get_connected_players()
1798 for n = 1, #players do
1800 if (object_in_range(self, players[n]))
1801 and not mobs.invis[ players[n]:get_player_name() ] then
1803 self.following = players[n]
1805 break
1810 if self.type == "npc"
1811 and self.order == "follow"
1812 and self.state ~= "attack"
1813 and self.owner ~= "" then
1815 -- npc stop following player if not owner
1816 if self.following
1817 and self.owner
1818 and self.owner ~= self.following:get_player_name() then
1819 self.following = nil
1821 else
1822 -- stop following player if not holding specific item
1823 if self.following
1824 and self.following:is_player()
1825 and follow_holding(self, self.following) == false then
1826 self.following = nil
1831 -- follow that thing
1832 if self.following then
1834 local s = self.object:get_pos()
1835 local p
1837 if self.following:is_player() then
1839 p = self.following:get_pos()
1841 elseif self.following.object then
1843 p = self.following.object:get_pos()
1846 if p then
1848 local dist = vector.distance(p, s)
1850 -- dont follow if out of range
1851 if (not object_in_range(self, self.following)) then
1852 self.following = nil
1853 else
1854 local vec = {
1855 x = p.x - s.x,
1856 z = p.z - s.z
1859 local yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1861 if p.x > s.x then yaw = yaw + pi end
1863 set_yaw(self, yaw, 6)
1865 -- anyone but standing npc's can move along
1866 if dist > self.reach
1867 and self.order ~= "stand" then
1869 set_velocity(self, self.walk_velocity)
1871 if self.walk_chance ~= 0 then
1872 set_animation(self, "walk")
1874 else
1875 set_velocity(self, 0)
1876 set_animation(self, "stand")
1879 return
1884 -- swimmers flop when out of their element, and swim again when back in
1885 if self.fly then
1886 local s = self.object:get_pos()
1887 if not flight_check(self, s) then
1889 self.state = "flop"
1890 self.object:set_velocity({x = 0, y = -5, z = 0})
1892 set_animation(self, "stand")
1894 return
1895 elseif self.state == "flop" then
1896 self.state = "stand"
1902 -- dogshoot attack switch and counter function
1903 local dogswitch = function(self, dtime)
1905 -- switch mode not activated
1906 if not self.dogshoot_switch
1907 or not dtime then
1908 return 0
1911 self.dogshoot_count = self.dogshoot_count + dtime
1913 if (self.dogshoot_switch == 1
1914 and self.dogshoot_count > self.dogshoot_count_max)
1915 or (self.dogshoot_switch == 2
1916 and self.dogshoot_count > self.dogshoot_count2_max) then
1918 self.dogshoot_count = 0
1920 if self.dogshoot_switch == 1 then
1921 self.dogshoot_switch = 2
1922 else
1923 self.dogshoot_switch = 1
1927 return self.dogshoot_switch
1931 -- execute current state (stand, walk, run, attacks)
1932 local do_states = function(self, dtime)
1934 local yaw = self.object:get_yaw() or 0
1936 if self.state == "stand" then
1938 if random(1, 4) == 1 then
1940 local lp = nil
1941 local s = self.object:get_pos()
1942 local objs = minetest.get_objects_inside_radius(s, 3)
1944 for n = 1, #objs do
1946 if objs[n]:is_player() then
1947 lp = objs[n]:get_pos()
1948 break
1952 -- look at any players nearby, otherwise turn randomly
1953 if lp then
1955 local vec = {
1956 x = lp.x - s.x,
1957 z = lp.z - s.z
1960 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1962 if lp.x > s.x then yaw = yaw + pi end
1963 else
1964 yaw = yaw + random(-0.5, 0.5)
1967 yaw = set_yaw(self, yaw, 8)
1970 set_velocity(self, 0)
1971 set_animation(self, "stand")
1973 -- npc's ordered to stand stay standing
1974 if self.type ~= "npc"
1975 or self.order ~= "stand" then
1977 if self.walk_chance ~= 0
1978 and self.facing_fence ~= true
1979 and random(1, 100) <= self.walk_chance
1980 and is_at_cliff_or_danger(self) == false then
1982 set_velocity(self, self.walk_velocity)
1983 self.state = "walk"
1984 set_animation(self, "walk")
1988 elseif self.state == "walk" then
1990 local s = self.object:get_pos()
1991 local lp = nil
1993 -- is there something I need to avoid?
1994 if (self.water_damage > 0
1995 and self.lava_damage > 0)
1996 or self.breath_max ~= -1 then
1998 lp = minetest.find_node_near(s, 1, {"group:water", "group:lava"})
2000 elseif self.water_damage > 0 then
2002 lp = minetest.find_node_near(s, 1, {"group:water"})
2004 elseif self.lava_damage > 0 then
2006 lp = minetest.find_node_near(s, 1, {"group:lava"})
2008 elseif self.fire_damage > 0 then
2010 lp = minetest.find_node_near(s, 1, {"group:fire"})
2014 local is_in_danger = false
2015 if lp then
2016 -- If mob in or on dangerous block, look for land
2017 if (is_node_dangerous(self, self.standing_in) or
2018 is_node_dangerous(self, self.standing_on)) then
2019 is_in_danger = true
2021 lp = minetest.find_node_near(s, 5, {"group:solid"})
2023 -- did we find land?
2024 if lp then
2026 local vec = {
2027 x = lp.x - s.x,
2028 z = lp.z - s.z
2031 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2033 if lp.x > s.x then yaw = yaw + pi end
2035 -- look towards land and jump/move in that direction
2036 yaw = set_yaw(self, yaw, 6)
2037 do_jump(self)
2038 set_velocity(self, self.walk_velocity)
2039 else
2040 yaw = yaw + random(-0.5, 0.5)
2043 -- A danger is near but mob is not inside
2044 else
2046 -- Randomly turn
2047 if random(1, 100) <= 30 then
2048 yaw = yaw + random(-0.5, 0.5)
2049 yaw = set_yaw(self, yaw, 8)
2053 yaw = set_yaw(self, yaw, 8)
2055 -- otherwise randomly turn
2056 elseif random(1, 100) <= 30 then
2058 yaw = yaw + random(-0.5, 0.5)
2059 yaw = set_yaw(self, yaw, 8)
2062 -- stand for great fall or danger or fence in front
2063 local cliff_or_danger = false
2064 if is_in_danger then
2065 cliff_or_danger = is_at_cliff_or_danger(self)
2067 if self.facing_fence == true
2068 or cliff_or_danger
2069 or random(1, 100) <= 30 then
2071 set_velocity(self, 0)
2072 self.state = "stand"
2073 set_animation(self, "stand")
2074 else
2076 set_velocity(self, self.walk_velocity)
2078 if flight_check(self)
2079 and self.animation
2080 and self.animation.fly_start
2081 and self.animation.fly_end then
2082 set_animation(self, "fly")
2083 else
2084 set_animation(self, "walk")
2088 -- runaway when punched
2089 elseif self.state == "runaway" then
2091 self.runaway_timer = self.runaway_timer + 1
2093 -- stop after 5 seconds or when at cliff
2094 if self.runaway_timer > 5
2095 or is_at_cliff_or_danger(self) then
2096 self.runaway_timer = 0
2097 set_velocity(self, 0)
2098 self.state = "stand"
2099 set_animation(self, "stand")
2100 else
2101 set_velocity(self, self.run_velocity)
2102 set_animation(self, "walk")
2105 -- attack routines (explode, dogfight, shoot, dogshoot)
2106 elseif self.state == "attack" then
2108 -- calculate distance from mob and enemy
2109 local s = self.object:get_pos()
2110 local p = self.attack:get_pos() or s
2111 local dist = vector.distance(p, s)
2113 -- stop attacking if player invisible or out of range
2114 if not self.attack
2115 or not self.attack:get_pos()
2116 or not object_in_range(self, self.attack)
2117 or self.attack:get_hp() <= 0
2118 or (self.attack:is_player() and mobs.invis[ self.attack:get_player_name() ]) then
2120 self.state = "stand"
2121 set_velocity(self, 0)
2122 set_animation(self, "stand")
2123 self.attack = nil
2124 self.v_start = false
2125 self.timer = 0
2126 self.blinktimer = 0
2127 self.path.way = nil
2129 return
2132 if self.attack_type == "explode" then
2134 local vec = {
2135 x = p.x - s.x,
2136 z = p.z - s.z
2139 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2141 if p.x > s.x then yaw = yaw + pi end
2143 yaw = set_yaw(self, yaw)
2145 local node_break_radius = self.explosion_radius or 1
2146 local entity_damage_radius = self.explosion_damage_radius
2147 or (node_break_radius * 2)
2149 -- start timer when in reach and line of sight
2150 if not self.v_start
2151 and dist <= self.reach
2152 and line_of_sight(self, s, p, 2) then
2154 self.v_start = true
2155 self.timer = 0
2156 self.blinktimer = 0
2157 mob_sound(self, "fuse", nil, false)
2159 -- stop timer if out of reach or direct line of sight
2160 elseif self.allow_fuse_reset
2161 and self.v_start
2162 and (dist > self.reach
2163 or not line_of_sight(self, s, p, 2)) then
2164 self.v_start = false
2165 self.timer = 0
2166 self.blinktimer = 0
2167 self.blinkstatus = false
2168 remove_texture_mod(self, "^[brighten")
2171 -- walk right up to player unless the timer is active
2172 if self.v_start and (self.stop_to_explode or dist < 1.5) then
2173 set_velocity(self, 0)
2174 else
2175 set_velocity(self, self.run_velocity)
2178 if self.animation and self.animation.run_start then
2179 set_animation(self, "run")
2180 else
2181 set_animation(self, "walk")
2184 if self.v_start then
2186 self.timer = self.timer + dtime
2187 self.blinktimer = (self.blinktimer or 0) + dtime
2189 if self.blinktimer > 0.2 then
2191 self.blinktimer = 0
2193 if self.blinkstatus then
2194 remove_texture_mod(self, "^[brighten")
2195 else
2196 add_texture_mod(self, "^[brighten")
2199 self.blinkstatus = not self.blinkstatus
2202 if self.timer > self.explosion_timer then
2204 local pos = self.object:get_pos()
2206 -- dont damage anything if area protected or next to water
2207 if minetest.find_node_near(pos, 1, {"group:water"})
2208 or minetest.is_protected(pos, "") then
2210 node_break_radius = 1
2213 self.object:remove()
2215 if mobs_griefing and mod_tnt and tnt and tnt.boom
2216 and not minetest.is_protected(pos, "") then
2218 tnt.boom(pos, {
2219 radius = node_break_radius,
2220 damage_radius = entity_damage_radius,
2221 sound = self.sounds.explode,
2222 is_tnt = false,
2224 else
2226 minetest.sound_play(self.sounds.explode, {
2227 pos = pos,
2228 gain = 1.0,
2229 max_hear_distance = self.sounds.distance or 32
2232 entity_physics(pos, entity_damage_radius)
2233 effect(pos, 32, "tnt_smoke.png", nil, nil, node_break_radius, 1, 0)
2236 return
2240 elseif self.attack_type == "dogfight"
2241 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 2)
2242 or (self.attack_type == "dogshoot" and dist <= self.reach and dogswitch(self) == 0) then
2244 if self.fly
2245 and dist > self.reach then
2247 local p1 = s
2248 local me_y = floor(p1.y)
2249 local p2 = p
2250 local p_y = floor(p2.y + 1)
2251 local v = self.object:get_velocity()
2253 if flight_check(self, s) then
2255 if me_y < p_y then
2257 self.object:set_velocity({
2258 x = v.x,
2259 y = 1 * self.walk_velocity,
2260 z = v.z
2263 elseif me_y > p_y then
2265 self.object:set_velocity({
2266 x = v.x,
2267 y = -1 * self.walk_velocity,
2268 z = v.z
2271 else
2272 if me_y < p_y then
2274 self.object:set_velocity({
2275 x = v.x,
2276 y = 0.01,
2277 z = v.z
2280 elseif me_y > p_y then
2282 self.object:set_velocity({
2283 x = v.x,
2284 y = -0.01,
2285 z = v.z
2292 -- rnd: new movement direction
2293 if self.path.following
2294 and self.path.way
2295 and self.attack_type ~= "dogshoot" then
2297 -- no paths longer than 50
2298 if #self.path.way > 50
2299 or dist < self.reach then
2300 self.path.following = false
2301 return
2304 local p1 = self.path.way[1]
2306 if not p1 then
2307 self.path.following = false
2308 return
2311 if abs(p1.x-s.x) + abs(p1.z - s.z) < 0.6 then
2312 -- reached waypoint, remove it from queue
2313 table.remove(self.path.way, 1)
2316 -- set new temporary target
2317 p = {x = p1.x, y = p1.y, z = p1.z}
2320 local vec = {
2321 x = p.x - s.x,
2322 z = p.z - s.z
2325 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2327 if p.x > s.x then yaw = yaw + pi end
2329 yaw = set_yaw(self, yaw)
2331 -- move towards enemy if beyond mob reach
2332 if dist > self.reach then
2334 -- path finding by rnd
2335 if self.pathfinding -- only if mob has pathfinding enabled
2336 and enable_pathfinding then
2338 smart_mobs(self, s, p, dist, dtime)
2341 if is_at_cliff_or_danger(self) then
2343 set_velocity(self, 0)
2344 set_animation(self, "stand")
2345 else
2347 if self.path.stuck then
2348 set_velocity(self, self.walk_velocity)
2349 else
2350 set_velocity(self, self.run_velocity)
2353 if self.animation and self.animation.run_start then
2354 set_animation(self, "run")
2355 else
2356 set_animation(self, "walk")
2360 else -- rnd: if inside reach range
2362 self.path.stuck = false
2363 self.path.stuck_timer = 0
2364 self.path.following = false -- not stuck anymore
2366 set_velocity(self, 0)
2368 if not self.custom_attack then
2370 if self.timer > 1 then
2372 self.timer = 0
2374 if self.double_melee_attack
2375 and random(1, 2) == 1 then
2376 set_animation(self, "punch2")
2377 else
2378 set_animation(self, "punch")
2381 local p2 = p
2382 local s2 = s
2384 p2.y = p2.y + .5
2385 s2.y = s2.y + .5
2387 if line_of_sight(self, p2, s2) == true then
2389 -- play attack sound
2390 mob_sound(self, "attack")
2392 -- punch player (or what player is attached to)
2393 local attached = self.attack:get_attach()
2394 if attached then
2395 self.attack = attached
2397 self.attack:punch(self.object, 1.0, {
2398 full_punch_interval = 1.0,
2399 damage_groups = {fleshy = self.damage}
2400 }, nil)
2403 else -- call custom attack every second
2404 if self.custom_attack
2405 and self.timer > 1 then
2407 self.timer = 0
2409 self.custom_attack(self, p)
2414 elseif self.attack_type == "shoot"
2415 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 1)
2416 or (self.attack_type == "dogshoot" and dist > self.reach and dogswitch(self) == 0) then
2418 p.y = p.y - .5
2419 s.y = s.y + .5
2421 local dist = vector.distance(p, s)
2422 local vec = {
2423 x = p.x - s.x,
2424 y = p.y - s.y,
2425 z = p.z - s.z
2428 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2430 if p.x > s.x then yaw = yaw + pi end
2432 yaw = set_yaw(self, yaw)
2434 set_velocity(self, 0)
2436 if self.shoot_interval
2437 and self.timer > self.shoot_interval
2438 and random(1, 100) <= 60 then
2440 self.timer = 0
2441 set_animation(self, "shoot")
2443 -- play shoot attack sound
2444 mob_sound(self, "shoot_attack")
2446 local p = self.object:get_pos()
2448 p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
2450 -- Shoot arrow
2451 if minetest.registered_entities[self.arrow] then
2453 local arrow, ent
2454 local v = 1
2455 if not self.shoot_arrow then
2456 arrow = minetest.add_entity(p, self.arrow)
2457 ent = arrow:get_luaentity()
2458 if ent.velocity then
2459 v = ent.velocity
2461 ent.switch = 1
2462 ent.owner_id = tostring(self.object) -- add unique owner id to arrow
2465 local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5
2466 -- offset makes shoot aim accurate
2467 vec.y = vec.y + self.shoot_offset
2468 vec.x = vec.x * (v / amount)
2469 vec.y = vec.y * (v / amount)
2470 vec.z = vec.z * (v / amount)
2471 if self.shoot_arrow then
2472 vec = vector.normalize(vec)
2473 self:shoot_arrow(p, vec)
2474 else
2475 arrow:set_velocity(vec)
2484 -- falling and fall damage
2485 local falling = function(self, pos)
2487 if self.fly then
2488 return
2491 -- floating in water (or falling)
2492 local v = self.object:get_velocity()
2494 if v.y > 0 then
2496 -- apply gravity when moving up
2497 self.object:set_acceleration({
2498 x = 0,
2499 y = -10,
2500 z = 0
2503 elseif v.y <= 0 and v.y > self.fall_speed then
2505 -- fall downwards at set speed
2506 self.object:set_acceleration({
2507 x = 0,
2508 y = self.fall_speed,
2509 z = 0
2511 else
2512 -- stop accelerating once max fall speed hit
2513 self.object:set_acceleration({x = 0, y = 0, z = 0})
2516 -- in water then float up
2517 if minetest.registered_nodes[node_ok(pos).name].groups.water then
2519 if self.floats == 1 then
2521 self.object:set_acceleration({
2522 x = 0,
2523 y = -self.fall_speed / (max(1, v.y) ^ 2),
2524 z = 0
2527 else
2529 -- fall damage onto solid ground
2530 if self.fall_damage == 1
2531 and self.object:get_velocity().y == 0 then
2533 local d = (self.old_y or 0) - self.object:get_pos().y
2535 if d > 5 then
2537 local add = minetest.get_item_group(self.standing_on, "fall_damage_add_percent")
2538 local damage = d - 5
2539 if add ~= 0 then
2540 damage = damage + damage * (add/100)
2542 damage = floor(damage)
2543 if damage > 0 then
2544 self.health = self.health - damage
2546 effect(pos, 5, "tnt_smoke.png", 1, 2, 2, nil)
2548 if check_for_death(self, "fall", {type = "fall"}) then
2549 return
2554 self.old_y = self.object:get_pos().y
2560 -- deal damage and effects when mob punched
2561 local mob_punch = function(self, hitter, tflp, tool_capabilities, dir)
2563 -- custom punch function
2564 if self.do_punch then
2566 -- when false skip going any further
2567 if self.do_punch(self, hitter, tflp, tool_caps, dir) == false then
2568 return
2572 -- error checking when mod profiling is enabled
2573 if not tool_capabilities then
2574 minetest.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2575 return
2578 -- is mob protected?
2579 if self.protected and hitter:is_player()
2580 and minetest.is_protected(self.object:get_pos(), hitter:get_player_name()) then
2581 return
2585 -- punch interval
2586 local weapon = hitter:get_wielded_item()
2587 local punch_interval = 1.4
2589 -- exhaust attacker
2590 if mod_hunger and hitter:is_player() then
2591 mcl_hunger.exhaust(hitter:get_player_name(), mcl_hunger.EXHAUST_ATTACK)
2594 -- calculate mob damage
2595 local damage = 0
2596 local armor = self.object:get_armor_groups() or {}
2597 local tmp
2599 -- quick error check incase it ends up 0 (serialize.h check test)
2600 if tflp == 0 then
2601 tflp = 0.2
2604 if use_cmi then
2605 damage = cmi.calculate_damage(self.object, hitter, tflp, tool_capabilities, dir)
2606 else
2608 for group,_ in pairs( (tool_capabilities.damage_groups or {}) ) do
2610 tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)
2612 if tmp < 0 then
2613 tmp = 0.0
2614 elseif tmp > 1 then
2615 tmp = 1.0
2618 damage = damage + (tool_capabilities.damage_groups[group] or 0)
2619 * tmp * ((armor[group] or 0) / 100.0)
2623 -- check for tool immunity or special damage
2624 for n = 1, #self.immune_to do
2626 if self.immune_to[n][1] == weapon:get_name() then
2628 damage = self.immune_to[n][2] or 0
2629 break
2633 -- healing
2634 if damage <= -1 then
2635 self.health = self.health - floor(damage)
2636 return
2639 if use_cmi then
2641 local cancel = cmi.notify_punch(self.object, hitter, tflp, tool_capabilities, dir, damage)
2643 if cancel then return end
2646 if tool_capabilities then
2647 punch_interval = tool_capabilities.full_punch_interval or 1.4
2650 -- add weapon wear manually
2651 -- Required because we have custom health handling ("health" property)
2652 if minetest.settings:get_bool("creative_mode") ~= true
2653 and tool_capabilities then
2654 if tool_capabilities.punch_attack_uses then
2655 -- Without this delay, the wear does not work. Quite hacky ...
2656 minetest.after(0, function(name)
2657 local player = minetest.get_player_by_name(name)
2658 if not player then return end
2659 local weapon = hitter:get_wielded_item(player)
2660 local def = weapon:get_definition()
2661 if def.tool_capabilities and def.tool_capabilities.punch_attack_uses then
2662 local wear = floor(65535/tool_capabilities.punch_attack_uses)
2663 weapon:add_wear(wear)
2664 hitter:set_wielded_item(weapon)
2666 end, hitter:get_player_name())
2670 local die = false
2672 -- only play hit sound and show blood effects if damage is 1 or over
2673 if damage >= 1 then
2675 -- weapon sounds
2676 if weapon:get_definition().sounds ~= nil then
2678 local s = random(0, #weapon:get_definition().sounds)
2680 minetest.sound_play(weapon:get_definition().sounds[s], {
2681 object = self.object, --hitter,
2682 max_hear_distance = 8
2684 else
2685 minetest.sound_play("default_punch", {
2686 object = self.object, --hitter,
2687 max_hear_distance = 5
2691 damage_effect(self, damage)
2693 -- do damage
2694 self.health = self.health - floor(damage)
2696 -- skip future functions if dead, except alerting others
2697 if check_for_death(self, "hit", {type = "punch", puncher = hitter}) then
2698 die = true
2701 -- knock back effect (only on full punch)
2702 if not die
2703 and self.knock_back
2704 and tflp >= punch_interval then
2706 local v = self.object:get_velocity()
2707 local r = 1.4 - min(punch_interval, 1.4)
2708 local kb = r * 2.0
2709 local up = 2
2711 -- if already in air then dont go up anymore when hit
2712 if v.y > 0
2713 or self.fly then
2714 up = 0
2717 -- direction error check
2718 dir = dir or {x = 0, y = 0, z = 0}
2720 -- check if tool already has specific knockback value
2721 if tool_capabilities.damage_groups["knockback"] then
2722 kb = tool_capabilities.damage_groups["knockback"]
2723 else
2724 kb = kb * 1.5
2727 self.object:set_velocity({
2728 x = dir.x * kb,
2729 y = up,
2730 z = dir.z * kb
2733 self.pause_timer = 0.25
2735 end -- END if damage
2737 -- if skittish then run away
2738 if not die and self.runaway == true then
2740 local lp = hitter:get_pos()
2741 local s = self.object:get_pos()
2742 local vec = {
2743 x = lp.x - s.x,
2744 y = lp.y - s.y,
2745 z = lp.z - s.z
2748 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
2750 if lp.x > s.x then
2751 yaw = yaw + pi
2754 yaw = set_yaw(self, yaw, 6)
2755 self.state = "runaway"
2756 self.runaway_timer = 0
2757 self.following = nil
2760 local name = hitter:get_player_name() or ""
2762 -- attack puncher and call other mobs for help
2763 if self.passive == false
2764 and self.state ~= "flop"
2765 and (self.child == false or self.type == "monster")
2766 and hitter:get_player_name() ~= self.owner
2767 and not mobs.invis[ name ] then
2769 if not die then
2770 -- attack whoever punched mob
2771 self.state = ""
2772 do_attack(self, hitter)
2775 -- alert others to the attack
2776 local objs = minetest.get_objects_inside_radius(hitter:get_pos(), self.view_range)
2777 local obj = nil
2779 for n = 1, #objs do
2781 obj = objs[n]:get_luaentity()
2783 if obj then
2785 -- only alert members of same mob or friends
2786 if obj.group_attack
2787 and obj.state ~= "attack"
2788 and obj.owner ~= name then
2789 if obj.name == self.name then
2790 do_attack(obj, hitter)
2791 elseif type(obj.group_attack) == "table" then
2792 for i=1, #obj.group_attack do
2793 if obj.name == obj.group_attack[i] then
2794 do_attack(obj, hitter)
2795 break
2801 -- have owned mobs attack player threat
2802 if obj.owner == name and obj.owner_loyal then
2803 do_attack(obj, self.object)
2810 local mob_detach_child = function(self, child)
2812 if self.driver == child then
2813 self.driver = nil
2818 -- get entity staticdata
2819 local mob_staticdata = function(self)
2821 -- remove mob when out of range unless tamed
2822 if remove_far
2823 and self.can_despawn
2824 and self.remove_ok
2825 and ((not self.nametag) or (self.nametag == ""))
2826 and self.lifetimer <= 20 then
2828 minetest.log("action", "Mob "..name.." despawns in mob_staticdata at "..minetest.pos_to_string(self.object.get_pos()))
2829 self.object:remove()
2831 return ""-- nil
2834 self.remove_ok = true
2835 self.attack = nil
2836 self.following = nil
2837 self.state = "stand"
2839 if use_cmi then
2840 self.serialized_cmi_components = cmi.serialize_components(self._cmi_components)
2843 local tmp = {}
2845 for _,stat in pairs(self) do
2847 local t = type(stat)
2849 if t ~= "function"
2850 and t ~= "nil"
2851 and t ~= "userdata"
2852 and _ ~= "_cmi_components" then
2853 tmp[_] = self[_]
2857 return minetest.serialize(tmp)
2861 -- activate mob and reload settings
2862 local mob_activate = function(self, staticdata, def, dtime)
2864 -- remove monsters in peaceful mode
2865 if self.type == "monster"
2866 and minetest.settings:get_bool("only_peaceful_mobs", false) then
2868 self.object:remove()
2870 return
2873 -- load entity variables
2874 local tmp = minetest.deserialize(staticdata)
2876 if tmp then
2877 for _,stat in pairs(tmp) do
2878 self[_] = stat
2882 -- select random texture, set model and size
2883 if not self.base_texture then
2885 -- compatiblity with old simple mobs textures
2886 if type(def.textures[1]) == "string" then
2887 def.textures = {def.textures}
2890 self.base_texture = def.textures[random(1, #def.textures)]
2891 self.base_mesh = def.mesh
2892 self.base_size = self.visual_size
2893 self.base_colbox = self.collisionbox
2894 self.base_selbox = self.selectionbox
2897 -- for current mobs that dont have this set
2898 if not self.base_selbox then
2899 self.base_selbox = self.selectionbox or self.base_colbox
2902 -- set texture, model and size
2903 local textures = self.base_texture
2904 local mesh = self.base_mesh
2905 local vis_size = self.base_size
2906 local colbox = self.base_colbox
2907 local selbox = self.base_selbox
2909 -- specific texture if gotten
2910 if self.gotten == true
2911 and def.gotten_texture then
2912 textures = def.gotten_texture
2915 -- specific mesh if gotten
2916 if self.gotten == true
2917 and def.gotten_mesh then
2918 mesh = def.gotten_mesh
2921 -- set child objects to half size
2922 if self.child == true then
2924 vis_size = {
2925 x = self.base_size.x * .5,
2926 y = self.base_size.y * .5,
2929 if def.child_texture then
2930 textures = def.child_texture[1]
2933 colbox = {
2934 self.base_colbox[1] * .5,
2935 self.base_colbox[2] * .5,
2936 self.base_colbox[3] * .5,
2937 self.base_colbox[4] * .5,
2938 self.base_colbox[5] * .5,
2939 self.base_colbox[6] * .5
2941 selbox = {
2942 self.base_selbox[1] * .5,
2943 self.base_selbox[2] * .5,
2944 self.base_selbox[3] * .5,
2945 self.base_selbox[4] * .5,
2946 self.base_selbox[5] * .5,
2947 self.base_selbox[6] * .5
2951 if self.health == 0 then
2952 self.health = random (self.hp_min, self.hp_max)
2954 if self.breath == nil then
2955 self.breath = self.breath_max
2958 -- pathfinding init
2959 self.path = {}
2960 self.path.way = {} -- path to follow, table of positions
2961 self.path.lastpos = {x = 0, y = 0, z = 0}
2962 self.path.stuck = false
2963 self.path.following = false -- currently following path?
2964 self.path.stuck_timer = 0 -- if stuck for too long search for path
2966 -- Armor groups
2967 -- immortal=1 because we use custom health
2968 -- handling (using "health" property)
2969 local armor
2970 if type(self.armor) == "table" then
2971 armor = table.copy(self.armor)
2972 armor.immortal = 1
2973 else
2974 armor = {immortal=1, fleshy = self.armor}
2976 self.object:set_armor_groups(armor)
2977 self.old_y = self.object:get_pos().y
2978 self.old_health = self.health
2979 self.sounds.distance = self.sounds.distance or 10
2980 self.textures = textures
2981 self.mesh = mesh
2982 self.collisionbox = colbox
2983 self.selectionbox = selbox
2984 self.visual_size = vis_size
2985 self.standing_in = "ignore"
2986 self.standing_on = "ignore"
2987 self.jump_sound_cooloff = 0 -- used to prevent jump sound from being played too often in short time
2988 self.opinion_sound_cooloff = 0 -- used to prevent sound spam of particular sound types
2990 self.texture_mods = {}
2991 self.object:set_texture_mod("")
2993 self.v_start = false
2994 self.timer = 0
2995 self.blinktimer = 0
2996 self.blinkstatus = false
2998 -- check existing nametag
2999 if not self.nametag then
3000 self.nametag = def.nametag
3003 -- set anything changed above
3004 self.object:set_properties(self)
3005 set_yaw(self, (random(0, 360) - 180) / 180 * pi, 6)
3006 update_tag(self)
3007 set_animation(self, "stand")
3009 -- run on_spawn function if found
3010 if self.on_spawn and not self.on_spawn_run then
3011 if self.on_spawn(self) then
3012 self.on_spawn_run = true -- if true, set flag to run once only
3016 -- run after_activate
3017 if def.after_activate then
3018 def.after_activate(self, staticdata, def, dtime)
3021 if use_cmi then
3022 self._cmi_components = cmi.activate_components(self.serialized_cmi_components)
3023 cmi.notify_activate(self.object, dtime)
3028 -- main mob function
3029 local mob_step = function(self, dtime)
3031 if use_cmi then
3032 cmi.notify_step(self.object, dtime)
3035 local pos = self.object:get_pos()
3036 local yaw = 0
3038 -- Despawning: when lifetimer expires, remove mob
3039 if remove_far
3040 and self.can_despawn == true
3041 and ((not self.nametag) or (self.nametag == "")) then
3043 -- TODO: Finish up implementation of despawning rules
3045 self.lifetimer = self.lifetimer - dtime
3047 if self.lifetimer <= 0 then
3049 -- only despawn away from player
3050 local objs = minetest.get_objects_inside_radius(pos, 32)
3052 for n = 1, #objs do
3054 if objs[n]:is_player() then
3056 self.lifetimer = 20
3058 return
3062 minetest.log("action", "Mob "..name.." despawns in mob_step at "..minetest.pos_to_string(pos))
3063 self.object:remove()
3065 return
3069 if self.jump_sound_cooloff > 0 then
3070 self.jump_sound_cooloff = self.jump_sound_cooloff - dtime
3072 if self.opinion_sound_cooloff > 0 then
3073 self.opinion_sound_cooloff = self.opinion_sound_cooloff - dtime
3075 falling(self, pos)
3077 -- smooth rotation by ThomasMonroe314
3079 if self.delay and self.delay > 0 then
3081 local yaw = self.object:get_yaw()
3083 if self.delay == 1 then
3084 yaw = self.target_yaw
3085 else
3086 local dif = abs(yaw - self.target_yaw)
3088 if yaw > self.target_yaw then
3090 if dif > pi then
3091 dif = 2 * pi - dif -- need to add
3092 yaw = yaw + dif / self.delay
3093 else
3094 yaw = yaw - dif / self.delay -- need to subtract
3097 elseif yaw < self.target_yaw then
3099 if dif > pi then
3100 dif = 2 * pi - dif
3101 yaw = yaw - dif / self.delay -- need to subtract
3102 else
3103 yaw = yaw + dif / self.delay -- need to add
3107 if yaw > (pi * 2) then yaw = yaw - (pi * 2) end
3108 if yaw < 0 then yaw = yaw + (pi * 2) end
3111 self.delay = self.delay - 1
3112 self.object:set_yaw(yaw)
3115 -- end rotation
3117 -- knockback timer
3118 if self.pause_timer > 0 then
3120 self.pause_timer = self.pause_timer - dtime
3122 return
3125 -- run custom function (defined in mob lua file)
3126 if self.do_custom then
3128 -- when false skip going any further
3129 if self.do_custom(self, dtime) == false then
3130 return
3134 -- attack timer
3135 self.timer = self.timer + dtime
3137 if self.state ~= "attack" then
3139 if self.timer < 1 then
3140 return
3143 self.timer = 0
3146 -- never go over 100
3147 if self.timer > 100 then
3148 self.timer = 1
3151 -- mob plays random sound at times
3152 if random(1, 100) == 1 then
3153 mob_sound(self, "random", true)
3156 -- environmental damage timer (every 1 second)
3157 self.env_damage_timer = self.env_damage_timer + dtime
3159 if (self.state == "attack" and self.env_damage_timer > 1)
3160 or self.state ~= "attack" then
3162 self.env_damage_timer = 0
3164 -- check for environmental damage (water, fire, lava etc.)
3165 if do_env_damage(self) then
3166 return
3169 -- node replace check (cow eats grass etc.)
3170 replace(self, pos)
3173 monster_attack(self)
3175 npc_attack(self)
3177 breed(self)
3179 follow_flop(self)
3181 do_states(self, dtime)
3183 do_jump(self)
3185 runaway_from(self)
3190 -- default function when mobs are blown up with TNT
3191 local do_tnt = function(obj, damage)
3193 obj.object:punch(obj.object, 1.0, {
3194 full_punch_interval = 1.0,
3195 damage_groups = {fleshy = damage},
3196 }, nil)
3198 return false, true, {}
3202 mobs.spawning_mobs = {}
3204 -- Code to execute before custom on_rightclick handling
3205 local on_rightclick_prefix = function(self, clicker)
3206 local item = clicker:get_wielded_item()
3208 -- Name mob with nametag
3209 if not self.ignores_nametag and item:get_name() == "mcl_mobs:nametag" then
3211 local tag = item:get_meta():get_string("name")
3212 if tag ~= "" then
3213 if string.len(tag) > MAX_MOB_NAME_LENGTH then
3214 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH)
3216 self.nametag = tag
3218 update_tag(self)
3220 if not mobs.is_creative(clicker:get_player_name()) then
3221 item:take_item()
3222 clicker:set_wielded_item(item)
3224 return true
3228 return false
3231 local create_mob_on_rightclick = function(on_rightclick)
3232 return function(self, clicker)
3233 local stop = on_rightclick_prefix(self, clicker)
3234 if (not stop) and (on_rightclick) then
3235 on_rightclick(self, clicker)
3240 -- register mob entity
3241 function mobs:register_mob(name, def)
3243 mobs.spawning_mobs[name] = true
3245 local can_despawn
3246 if def.can_despawn ~= nil then
3247 can_despawn = def.can_despawn
3248 else
3249 can_despawn = true
3252 local function scale_difficulty(value, default, min, special)
3253 if (not value) or (value == default) or (value == special) then
3254 return default
3255 else
3256 return max(min, value * difficulty)
3260 local collisionbox = def.collisionbox or {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25}
3261 -- Workaround for <https://github.com/minetest/minetest/issues/5966>:
3262 -- Increase upper Y limit to avoid mobs glitching through solid nodes.
3263 -- FIXME: Remove workaround if it's no longer needed.
3264 if collisionbox[5] < 0.79 then
3265 collisionbox[5] = 0.79
3268 minetest.register_entity(name, {
3270 stepheight = def.stepheight or 0.6,
3271 name = name,
3272 type = def.type,
3273 attack_type = def.attack_type,
3274 fly = def.fly,
3275 fly_in = def.fly_in or {"air", "__airlike"},
3276 owner = def.owner or "",
3277 order = def.order or "",
3278 on_die = def.on_die,
3279 spawn_small_alternative = def.spawn_small_alternative,
3280 do_custom = def.do_custom,
3281 jump_height = def.jump_height or 4, -- was 6
3282 rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
3283 lifetimer = def.lifetimer or 57.73,
3284 hp_min = scale_difficulty(def.hp_min, 5, 1),
3285 hp_max = scale_difficulty(def.hp_max, 10, 1),
3286 breath_max = def.breath_max or 15,
3287 breathes_in_water = def.breathes_in_water or false,
3288 physical = true,
3289 collisionbox = collisionbox,
3290 selectionbox = def.selectionbox or def.collisionbox,
3291 visual = def.visual,
3292 visual_size = def.visual_size or {x = 1, y = 1},
3293 mesh = def.mesh,
3294 makes_footstep_sound = def.makes_footstep_sound or false,
3295 view_range = def.view_range or 16,
3296 walk_velocity = def.walk_velocity or 1,
3297 run_velocity = def.run_velocity or 2,
3298 damage = scale_difficulty(def.damage, 0, 0),
3299 light_damage = def.light_damage or 0,
3300 sunlight_damage = def.sunlight_damage or 0,
3301 water_damage = def.water_damage or 0,
3302 lava_damage = def.lava_damage or 8,
3303 fire_damage = def.fire_damage or 1,
3304 suffocation = def.suffocation or true,
3305 fall_damage = def.fall_damage or 1,
3306 fall_speed = def.fall_speed or -10, -- must be lower than -2 (default: -10)
3307 drops = def.drops or {},
3308 armor = def.armor or 100,
3309 on_rightclick = create_mob_on_rightclick(def.on_rightclick),
3310 arrow = def.arrow,
3311 shoot_interval = def.shoot_interval,
3312 sounds = def.sounds or {},
3313 animation = def.animation,
3314 follow = def.follow,
3315 jump = def.jump ~= false,
3316 walk_chance = def.walk_chance or 50,
3317 attacks_monsters = def.attacks_monsters or false,
3318 group_attack = def.group_attack or false,
3319 passive = def.passive or false,
3320 knock_back = def.knock_back ~= false,
3321 shoot_offset = def.shoot_offset or 0,
3322 floats = def.floats or 1, -- floats in water by default
3323 replace_rate = def.replace_rate,
3324 replace_what = def.replace_what,
3325 replace_with = def.replace_with,
3326 replace_offset = def.replace_offset or 0,
3327 on_replace = def.on_replace,
3328 timer = 0,
3329 env_damage_timer = 0, -- only used when state = "attack"
3330 tamed = false,
3331 pause_timer = 0,
3332 horny = false,
3333 hornytimer = 0,
3334 gotten = false,
3335 health = 0,
3336 reach = def.reach or 3,
3337 htimer = 0,
3338 texture_list = def.textures,
3339 child_texture = def.child_texture,
3340 docile_by_day = def.docile_by_day or false,
3341 time_of_day = 0.5,
3342 fear_height = def.fear_height or 0,
3343 runaway = def.runaway,
3344 runaway_timer = 0,
3345 pathfinding = def.pathfinding,
3346 immune_to = def.immune_to or {},
3347 explosion_radius = def.explosion_radius,
3348 explosion_damage_radius = def.explosion_damage_radius,
3349 explosion_timer = def.explosion_timer or 3,
3350 allow_fuse_reset = def.allow_fuse_reset ~= false,
3351 stop_to_explode = def.stop_to_explode ~= false,
3352 custom_attack = def.custom_attack,
3353 double_melee_attack = def.double_melee_attack,
3354 dogshoot_switch = def.dogshoot_switch,
3355 dogshoot_count = 0,
3356 dogshoot_count_max = def.dogshoot_count_max or 5,
3357 dogshoot_count2_max = def.dogshoot_count2_max or (def.dogshoot_count_max or 5),
3358 attack_animals = def.attack_animals or false,
3359 specific_attack = def.specific_attack,
3360 runaway_from = def.runaway_from,
3361 owner_loyal = def.owner_loyal,
3362 facing_fence = false,
3363 _cmi_is_mob = true,
3365 -- MCL2 extensions
3366 ignores_nametag = def.ignores_nametag or false,
3367 rain_damage = def.rain_damage or 0,
3368 glow = def.glow,
3369 can_despawn = can_despawn,
3370 child = def.child or false,
3371 texture_mods = {},
3372 shoot_arrow = def.shoot_arrow,
3373 sounds_child = def.sounds_child,
3374 -- End of MCL2 extensions
3376 on_spawn = def.on_spawn,
3378 on_blast = def.on_blast or do_tnt,
3380 on_step = mob_step,
3382 do_punch = def.do_punch,
3384 on_punch = mob_punch,
3386 on_breed = def.on_breed,
3388 on_grown = def.on_grown,
3390 on_detach_child = mob_detach_child,
3392 on_activate = function(self, staticdata, dtime)
3393 return mob_activate(self, staticdata, def, dtime)
3394 end,
3396 get_staticdata = function(self)
3397 return mob_staticdata(self)
3398 end,
3402 if minetest.get_modpath("doc_identifier") ~= nil then
3403 doc.sub.identifier.register_object(name, "basics", "mobs")
3406 end -- END mobs:register_mob function
3409 -- count how many mobs of one type are inside an area
3410 local count_mobs = function(pos, type)
3412 local num_type = 0
3413 local num_total = 0
3414 local objs = minetest.get_objects_inside_radius(pos, aoc_range)
3416 for n = 1, #objs do
3418 if not objs[n]:is_player() then
3420 local obj = objs[n]:get_luaentity()
3422 -- count mob type and add to total also
3423 if obj and obj.name and obj.name == type then
3425 num_type = num_type + 1
3426 num_total = num_total + 1
3428 -- add to total mobs
3429 elseif obj and obj.name and obj.health ~= nil then
3431 num_total = num_total + 1
3436 return num_type, num_total
3440 -- global functions
3442 function mobs:spawn_abm_check(pos, node, name)
3443 -- global function to add additional spawn checks
3444 -- return true to stop spawning mob
3448 function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
3449 interval, chance, aoc, min_height, max_height, day_toggle, on_spawn)
3451 -- Do mobs spawn at all?
3452 if not mobs_spawn then
3453 return
3456 -- chance/spawn number override in minetest.conf for registered mob
3457 local numbers = minetest.settings:get(name)
3459 if numbers then
3460 numbers = numbers:split(",")
3461 chance = tonumber(numbers[1]) or chance
3462 aoc = tonumber(numbers[2]) or aoc
3464 if chance == 0 then
3465 minetest.log("warning", string.format("[mobs] %s has spawning disabled", name))
3466 return
3469 minetest.log("action",
3470 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name, chance, aoc))
3474 local spawn_action
3475 spawn_action = function(pos, node, active_object_count, active_object_count_wider, name)
3477 local orig_pos = table.copy(pos)
3478 -- is mob actually registered?
3479 if not mobs.spawning_mobs[name]
3480 or not minetest.registered_entities[name] then
3481 minetest.log("warning", "Mob spawn of "..name.." failed, unknown entity or mob is not registered for spawning!")
3482 return
3485 -- additional custom checks for spawning mob
3486 if mobs:spawn_abm_check(pos, node, name) == true then
3487 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, ABM check rejected!")
3488 return
3491 -- do not spawn if too many of same mob in area
3492 if active_object_count_wider >= max_per_block
3493 or count_mobs(pos, name) >= aoc then
3494 -- too many entities
3495 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, too crowded!")
3496 return
3499 -- if toggle set to nil then ignore day/night check
3500 if day_toggle ~= nil then
3502 local tod = (minetest.get_timeofday() or 0) * 24000
3504 if tod > 4500 and tod < 19500 then
3505 -- daylight, but mob wants night
3506 if day_toggle == false then
3507 -- mob needs night
3508 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, mob needs light!")
3509 return
3511 else
3512 -- night time but mob wants day
3513 if day_toggle == true then
3514 -- mob needs day
3515 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, mob needs daylight!")
3516 return
3521 -- spawn above node
3522 pos.y = pos.y + 1
3524 -- only spawn away from player
3525 local objs = minetest.get_objects_inside_radius(pos, 10)
3527 for n = 1, #objs do
3529 if objs[n]:is_player() then
3530 -- player too close
3531 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, player too close!")
3532 return
3536 -- mobs cannot spawn in protected areas when enabled
3537 if not spawn_protected
3538 and minetest.is_protected(pos, "") then
3539 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, position is protected!")
3540 return
3543 -- are we spawning within height limits?
3544 if pos.y > max_height
3545 or pos.y < min_height then
3546 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, out of height limit!")
3547 return
3550 -- are light levels ok?
3551 local light = minetest.get_node_light(pos)
3552 if not light
3553 or light > max_light
3554 or light < min_light then
3555 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, bad light!")
3556 return
3559 -- do we have enough space to spawn mob?
3560 local ent = minetest.registered_entities[name]
3561 local width_x = max(1, math.ceil(ent.collisionbox[4] - ent.collisionbox[1]))
3562 local min_x, max_x
3563 if width_x % 2 == 0 then
3564 max_x = math.floor(width_x/2)
3565 min_x = -(max_x-1)
3566 else
3567 max_x = math.floor(width_x/2)
3568 min_x = -max_x
3571 local width_z = max(1, math.ceil(ent.collisionbox[6] - ent.collisionbox[3]))
3572 local min_z, max_z
3573 if width_z % 2 == 0 then
3574 max_z = math.floor(width_z/2)
3575 min_z = -(max_z-1)
3576 else
3577 max_z = math.floor(width_z/2)
3578 min_z = -max_z
3581 local max_y = max(0, math.ceil(ent.collisionbox[5] - ent.collisionbox[2]) - 1)
3583 for y = 0, max_y do
3584 for x = min_x, max_x do
3585 for z = min_z, max_z do
3586 local pos2 = {x = pos.x+x, y = pos.y+y, z = pos.z+z}
3587 if minetest.registered_nodes[node_ok(pos2).name].walkable == true then
3588 -- inside block
3589 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, too little space!")
3590 if ent.spawn_small_alternative ~= nil and (not minetest.registered_nodes[node_ok(pos).name].walkable) then
3591 minetest.log("info", "Trying to spawn smaller alternative mob: "..ent.spawn_small_alternative)
3592 spawn_action(orig_pos, node, active_object_count, active_object_count_wider, ent.spawn_small_alternative)
3594 return
3600 -- spawn mob 1/2 node above ground
3601 pos.y = pos.y + 0.5
3602 -- tweak X/Z spawn pos
3603 if width_x % 2 == 0 then
3604 pos.x = pos.x + 0.5
3606 if width_z % 2 == 0 then
3607 pos.z = pos.z + 0.5
3610 local mob = minetest.add_entity(pos, name)
3611 minetest.log("action", "Mob spawned: "..name.." at "..minetest.pos_to_string(pos))
3613 if on_spawn then
3615 local ent = mob:get_luaentity()
3617 on_spawn(ent, pos)
3621 local function spawn_abm_action(pos, node, active_object_count, active_object_count_wider)
3622 spawn_action(pos, node, active_object_count, active_object_count_wider, name)
3625 minetest.register_abm({
3626 label = name .. " spawning",
3627 nodenames = nodes,
3628 neighbors = neighbors,
3629 interval = interval,
3630 chance = floor(max(1, chance * mobs_spawn_chance)),
3631 catch_up = false,
3632 action = spawn_abm_action,
3637 -- compatibility with older mob registration
3638 function mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
3640 mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
3641 chance, active_object_count, -31000, max_height, day_toggle)
3645 -- MarkBu's spawn function
3646 function mobs:spawn(def)
3648 local name = def.name
3649 local nodes = def.nodes or {"group:soil", "group:stone"}
3650 local neighbors = def.neighbors or {"air"}
3651 local min_light = def.min_light or 0
3652 local max_light = def.max_light or 15
3653 local interval = def.interval or 30
3654 local chance = def.chance or 5000
3655 local active_object_count = def.active_object_count or 1
3656 local min_height = def.min_height or -31000
3657 local max_height = def.max_height or 31000
3658 local day_toggle = def.day_toggle
3659 local on_spawn = def.on_spawn
3661 mobs:spawn_specific(name, nodes, neighbors, min_light, max_light, interval,
3662 chance, active_object_count, min_height, max_height, day_toggle, on_spawn)
3666 -- register arrow for shoot attack
3667 function mobs:register_arrow(name, def)
3669 if not name or not def then return end -- errorcheck
3671 minetest.register_entity(name, {
3673 physical = false,
3674 visual = def.visual,
3675 visual_size = def.visual_size,
3676 textures = def.textures,
3677 velocity = def.velocity,
3678 hit_player = def.hit_player,
3679 hit_node = def.hit_node,
3680 hit_mob = def.hit_mob,
3681 hit_object = def.hit_object,
3682 drop = def.drop or false, -- drops arrow as registered item when true
3683 collisionbox = {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3684 timer = 0,
3685 switch = 0,
3686 owner_id = def.owner_id,
3687 rotate = def.rotate,
3688 automatic_face_movement_dir = def.rotate
3689 and (def.rotate - (pi / 180)) or false,
3691 on_activate = def.on_activate,
3693 on_step = def.on_step or function(self, dtime)
3695 self.timer = self.timer + 1
3697 local pos = self.object:get_pos()
3699 if self.switch == 0
3700 or self.timer > 150
3701 or not within_limits(pos, 0) then
3703 self.object:remove();
3705 return
3708 -- does arrow have a tail (fireball)
3709 if def.tail
3710 and def.tail == 1
3711 and def.tail_texture then
3713 minetest.add_particle({
3714 pos = pos,
3715 velocity = {x = 0, y = 0, z = 0},
3716 acceleration = {x = 0, y = 0, z = 0},
3717 expirationtime = def.expire or 0.25,
3718 collisiondetection = false,
3719 texture = def.tail_texture,
3720 size = def.tail_size or 5,
3721 glow = def.glow or 0,
3725 if self.hit_node then
3727 local node = node_ok(pos).name
3729 if minetest.registered_nodes[node].walkable then
3731 self.hit_node(self, pos, node)
3733 if self.drop == true then
3735 pos.y = pos.y + 1
3737 self.lastpos = (self.lastpos or pos)
3739 minetest.add_item(self.lastpos, self.object:get_luaentity().name)
3742 self.object:remove();
3744 return
3748 if self.hit_player or self.hit_mob or self.hit_object then
3750 for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do
3752 if self.hit_player
3753 and player:is_player() then
3755 self.hit_player(self, player)
3756 self.object:remove();
3757 return
3760 local entity = player:get_luaentity()
3762 if entity
3763 and self.hit_mob
3764 and entity._cmi_is_mob == true
3765 and tostring(player) ~= self.owner_id
3766 and entity.name ~= self.object:get_luaentity().name then
3767 self.hit_mob(self, player)
3768 self.object:remove();
3769 return
3772 if entity
3773 and self.hit_object
3774 and (not entity._cmi_is_mob)
3775 and tostring(player) ~= self.owner_id
3776 and entity.name ~= self.object:get_luaentity().name then
3777 self.hit_object(self, player)
3778 self.object:remove();
3779 return
3784 self.lastpos = pos
3790 -- no damage to nodes explosion
3791 function mobs:safe_boom(self, pos, radius)
3793 minetest.sound_play(self.sounds and self.sounds.explode or "tnt_explode", {
3794 pos = pos,
3795 gain = 1.0,
3796 max_hear_distance = self.sounds and self.sounds.distance or 32
3799 entity_physics(pos, radius)
3800 effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
3804 -- make explosion with protection and tnt mod check
3805 function mobs:boom(self, pos, radius)
3807 if mobs_griefing
3808 and mod_tnt and tnt and tnt.boom
3809 and not minetest.is_protected(pos, "") then
3811 tnt.boom(pos, {
3812 radius = radius,
3813 damage_radius = radius,
3814 sound = self.sounds and self.sounds.explode,
3815 explode_center = true,
3816 is_tnt = false,
3818 else
3819 mobs:safe_boom(self, pos, radius)
3824 -- Register spawn eggs
3826 -- Note: This also introduces the “spawn_egg” group:
3827 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3828 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3829 function mobs:register_egg(mob, desc, background, addegg, no_creative)
3831 local grp = {spawn_egg = 1}
3833 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3834 if creative and no_creative == true then
3835 grp.not_in_creative_inventory = 1
3838 local invimg = background
3840 if addegg == 1 then
3841 invimg = "mobs_chicken_egg.png^(" .. invimg ..
3842 "^[mask:mobs_chicken_egg_overlay.png)"
3845 -- register old stackable mob egg
3846 minetest.register_craftitem(mob, {
3848 description = desc,
3849 inventory_image = invimg,
3850 groups = grp,
3852 _doc_items_longdesc = S("This allows you to place a single mob."),
3853 _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."),
3855 on_place = function(itemstack, placer, pointed_thing)
3857 local pos = pointed_thing.above
3859 -- am I clicking on something with existing on_rightclick function?
3860 local under = minetest.get_node(pointed_thing.under)
3861 local def = minetest.registered_nodes[under.name]
3862 if def and def.on_rightclick then
3863 return def.on_rightclick(pointed_thing.under, under, placer, itemstack)
3866 if pos
3867 and within_limits(pos, 0)
3868 and not minetest.is_protected(pos, placer:get_player_name()) then
3870 local name = placer:get_player_name()
3871 local privs = minetest.get_player_privs(name)
3872 if mod_mobspawners and under.name == "mcl_mobspawners:spawner" then
3873 if minetest.is_protected(pointed_thing.under, name) then
3874 minetest.record_protection_violation(pointed_thing.under, name)
3875 return itemstack
3877 if not privs.maphack then
3878 minetest.chat_send_player(name, S("You need the “maphack” privilege to change the mob spawner."))
3879 return itemstack
3881 mcl_mobspawners.setup_spawner(pointed_thing.under, itemstack:get_name())
3882 if not minetest.settings:get_bool("creative_mode") then
3883 itemstack:take_item()
3885 return itemstack
3888 if not minetest.registered_entities[mob] then
3889 return itemstack
3892 if minetest.settings:get_bool("only_peaceful_mobs", false)
3893 and minetest.registered_entities[mob].type == "monster" then
3894 minetest.chat_send_player(name, S("Only peaceful mobs allowed!"))
3895 return itemstack
3898 pos.y = pos.y + 1
3900 local mob = minetest.add_entity(pos, mob)
3901 local ent = mob:get_luaentity()
3903 -- don't set owner if monster or sneak pressed
3904 if ent.type ~= "monster"
3905 and not placer:get_player_control().sneak then
3906 ent.owner = placer:get_player_name()
3907 ent.tamed = true
3910 -- set nametag
3911 local nametag = itemstack:get_meta():get_string("name")
3912 if nametag ~= "" then
3913 if string.len(nametag) > MAX_MOB_NAME_LENGTH then
3914 nametag = string.sub(nametag, 1, MAX_MOB_NAME_LENGTH)
3916 ent.nametag = nametag
3917 update_tag(ent)
3920 -- if not in creative then take item
3921 if not mobs.is_creative(placer:get_player_name()) then
3922 itemstack:take_item()
3926 return itemstack
3927 end,
3933 -- No-op in MCL2 (capturing mobs is not possible).
3934 -- Provided for compability with Mobs Redo
3935 function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
3936 return false
3940 -- No-op in MCL2 (protecting mobs is not possible).
3941 function mobs:protect(self, clicker)
3942 return false
3946 -- feeding, taming and breeding (thanks blert2112)
3947 function mobs:feed_tame(self, clicker, feed_count, breed, tame)
3948 if not self.follow then
3949 return false
3952 -- can eat/tame with item in hand
3953 if follow_holding(self, clicker) then
3955 -- if not in creative then take item
3956 if not mobs.is_creative(clicker:get_player_name()) then
3958 local item = clicker:get_wielded_item()
3960 item:take_item()
3962 clicker:set_wielded_item(item)
3965 -- increase health
3966 self.health = self.health + 4
3968 if self.health >= self.hp_max then
3970 self.health = self.hp_max
3972 if self.htimer < 1 then
3973 self.htimer = 5
3977 self.object:set_hp(self.health)
3979 update_tag(self)
3981 -- make children grow quicker
3982 if self.child == true then
3984 self.hornytimer = self.hornytimer + 20
3986 return true
3989 -- feed and tame
3990 self.food = (self.food or 0) + 1
3991 if self.food >= feed_count then
3993 self.food = 0
3995 if breed and self.hornytimer == 0 then
3996 self.horny = true
3999 if tame then
4001 self.tamed = true
4003 if not self.owner or self.owner == "" then
4004 self.owner = clicker:get_player_name()
4008 -- make sound when fed so many times
4009 mob_sound(self, "random", true)
4012 return true
4015 return false
4018 -- Spawn a child
4019 function mobs:spawn_child(pos, mob_type)
4020 local child = minetest.add_entity(pos, mob_type)
4021 if not child then
4022 return
4025 local ent = child:get_luaentity()
4026 effect(pos, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
4028 ent.child = true
4030 local textures
4031 -- using specific child texture (if found)
4032 if ent.child_texture then
4033 textures = ent.child_texture[1]
4036 -- and resize to half height
4037 child:set_properties({
4038 textures = textures,
4039 visual_size = {
4040 x = ent.base_size.x * .5,
4041 y = ent.base_size.y * .5,
4043 collisionbox = {
4044 ent.base_colbox[1] * .5,
4045 ent.base_colbox[2] * .5,
4046 ent.base_colbox[3] * .5,
4047 ent.base_colbox[4] * .5,
4048 ent.base_colbox[5] * .5,
4049 ent.base_colbox[6] * .5,
4051 selectionbox = {
4052 ent.base_selbox[1] * .5,
4053 ent.base_selbox[2] * .5,
4054 ent.base_selbox[3] * .5,
4055 ent.base_selbox[4] * .5,
4056 ent.base_selbox[5] * .5,
4057 ent.base_selbox[6] * .5,
4061 return child
4065 -- compatibility function for old entities to new modpack entities
4066 function mobs:alias_mob(old_name, new_name)
4068 -- spawn egg
4069 minetest.register_alias(old_name, new_name)
4071 -- entity
4072 minetest.register_entity(":" .. old_name, {
4074 physical = false,
4076 on_step = function(self)
4078 if minetest.registered_entities[new_name] then
4079 minetest.add_entity(self.object:get_pos(), new_name)
4082 self.object:remove()