Make mobs and bed use mcl_explosions
[MineClone/MineClone2.git] / mods / ENTITIES / mcl_mobs / api.lua
blobdd0d2e5dd38fd3fa08236ea2de6dda39a029e659
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 local MOB_CAP = {}
11 MOB_CAP.hostile = 70
12 MOB_CAP.passive = 10
13 MOB_CAP.ambient = 15
14 MOB_CAP.water = 15
16 -- Localize
17 local S = minetest.get_translator("mcl_mobs")
19 -- CMI support check
20 local use_cmi = minetest.global_exists("cmi")
23 -- Invisibility mod check
24 mobs.invis = {}
25 if minetest.global_exists("invisibility") then
26 mobs.invis = invisibility
27 end
30 -- creative check
31 local creative_mode_cache = minetest.settings:get_bool("creative_mode")
32 function mobs.is_creative(name)
33 return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
34 end
37 -- localize math functions
38 local pi = math.pi
39 local sin = math.sin
40 local cos = math.cos
41 local abs = math.abs
42 local min = math.min
43 local max = math.max
44 local atann = math.atan
45 local random = math.random
46 local floor = math.floor
47 local atan = function(x)
48 if not x or x ~= x then
49 return 0
50 else
51 return atann(x)
52 end
53 end
56 -- Load settings
57 local damage_enabled = minetest.settings:get_bool("enable_damage")
58 local mobs_spawn = minetest.settings:get_bool("mobs_spawn", true) ~= false
60 local disable_blood = minetest.settings:get_bool("mobs_disable_blood")
61 local mobs_drop_items = minetest.settings:get_bool("mobs_drop_items") ~= false
62 local mobs_griefing = minetest.settings:get_bool("mobs_griefing") ~= false
63 local creative = minetest.settings:get_bool("creative_mode")
64 local spawn_protected = minetest.settings:get_bool("mobs_spawn_protected") ~= false
65 local remove_far = false
66 local difficulty = tonumber(minetest.settings:get("mob_difficulty")) or 1.0
67 local show_health = false
68 local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 64)
69 local mobs_spawn_chance = tonumber(minetest.settings:get("mobs_spawn_chance") or 2.5)
71 -- Peaceful mode message so players will know there are no monsters
72 if minetest.settings:get_bool("only_peaceful_mobs", false) then
73 minetest.register_on_joinplayer(function(player)
74 minetest.chat_send_player(player:get_player_name(),
75 S("Peaceful mode active! No monsters will spawn."))
76 end)
77 end
79 -- calculate aoc range for mob count
80 local aosrb = tonumber(minetest.settings:get("active_object_send_range_blocks"))
81 local abr = tonumber(minetest.settings:get("active_block_range"))
82 local aoc_range = max(aosrb, abr) * 16
84 -- pathfinding settings
85 local enable_pathfinding = true
86 local stuck_timeout = 3 -- how long before mob gets stuck in place and starts searching
87 local stuck_path_timeout = 10 -- how long will mob follow path before giving up
89 -- default nodes
90 local node_ice = "mcl_core:ice"
91 local node_snowblock = "mcl_core:snowblock"
92 local node_snow = "mcl_core:snow"
93 mobs.fallback_node = minetest.registered_aliases["mapgen_dirt"] or "mcl_core:dirt"
95 local mod_weather = minetest.get_modpath("mcl_weather") ~= nil
96 local mod_explosions = minetest.get_modpath("mcl_explosions") ~= nil
97 local mod_mobspawners = minetest.get_modpath("mcl_mobspawners") ~= nil
98 local mod_hunger = minetest.get_modpath("mcl_hunger") ~= nil
99 local mod_worlds = minetest.get_modpath("mcl_worlds") ~= nil
100 local mod_armor = minetest.get_modpath("mcl_armor") ~= nil
102 -- play sound
103 local mob_sound = function(self, soundname, is_opinion, fixed_pitch)
105 local soundinfo
106 if self.sounds_child and self.child then
107 soundinfo = self.sounds_child
108 elseif self.sounds then
109 soundinfo = self.sounds
111 if not soundinfo then
112 return
114 local sound = soundinfo[soundname]
115 if sound then
116 if is_opinion and self.opinion_sound_cooloff > 0 then
117 return
119 local pitch
120 if not fixed_pitch then
121 local base_pitch = soundinfo.base_pitch
122 if not base_pitch then
123 base_pitch = 1
125 if self.child and (not self.sounds_child) then
126 -- Children have higher pitch
127 pitch = base_pitch * 1.5
128 else
129 pitch = base_pitch
131 -- randomize the pitch a bit
132 pitch = pitch + math.random(-10, 10) * 0.005
134 minetest.sound_play(sound, {
135 object = self.object,
136 gain = 1.0,
137 max_hear_distance = self.sounds.distance,
138 pitch = pitch,
139 }, true)
140 self.opinion_sound_cooloff = 1
144 -- Reeturn true if object is in view_range
145 local function object_in_range(self, object)
146 if not object then
147 return false
149 local factor
150 -- Apply view range reduction for special player armor
151 if object:is_player() and mod_armor then
152 factor = armor:get_mob_view_range_factor(object, self.name)
154 -- Distance check
155 local dist
156 if factor and factor == 0 then
157 return false
158 elseif factor then
159 dist = self.view_range * factor
160 else
161 dist = self.view_range
163 if vector.distance(self.object:get_pos(), object:get_pos()) > dist then
164 return false
165 else
166 return true
170 -- attack player/mob
171 local do_attack = function(self, player)
173 if self.state == "attack" then
174 return
177 self.attack = player
178 self.state = "attack"
180 -- TODO: Implement war_cry sound without being annoying
181 --if random(0, 100) < 90 then
182 --mob_sound(self, "war_cry", true)
183 --end
187 -- move mob in facing direction
188 local set_velocity = function(self, v)
190 -- do not move if mob has been ordered to stay
191 if self.order == "stand" then
192 self.object:set_velocity({x = 0, y = 0, z = 0})
193 return
196 local yaw = (self.object:get_yaw() or 0) + self.rotate
197 local vel = self.object:get_velocity()
198 self.object:set_velocity({
199 x = sin(yaw) * -v,
200 y = (vel and vel.y) or 0,
201 z = cos(yaw) * v
206 -- calculate mob velocity
207 local get_velocity = function(self)
209 local v = self.object:get_velocity()
211 return (v.x * v.x + v.z * v.z) ^ 0.5
215 -- set and return valid yaw
216 local set_yaw = function(self, yaw, delay)
218 if not yaw or yaw ~= yaw then
219 yaw = 0
222 delay = delay or 0
224 if delay == 0 then
225 self.object:set_yaw(yaw)
226 return yaw
229 self.target_yaw = yaw
230 self.delay = delay
232 return self.target_yaw
235 -- global function to set mob yaw
236 function mobs:yaw(self, yaw, delay)
237 set_yaw(self, yaw, delay)
240 local add_texture_mod = function(self, mod)
241 local full_mod = ""
242 local already_added = false
243 for i=1, #self.texture_mods do
244 if mod == self.texture_mods[i] then
245 already_added = true
247 full_mod = full_mod .. self.texture_mods[i]
249 if not already_added then
250 full_mod = full_mod .. mod
251 table.insert(self.texture_mods, mod)
253 self.object:set_texture_mod(full_mod)
255 local remove_texture_mod = function(self, mod)
256 local full_mod = ""
257 local remove = {}
258 for i=1, #self.texture_mods do
259 if self.texture_mods[i] ~= mod then
260 full_mod = full_mod .. self.texture_mods[i]
261 else
262 table.insert(remove, i)
265 for i=#remove, 1 do
266 table.remove(self.texture_mods, remove[i])
268 self.object:set_texture_mod(full_mod)
271 -- set defined animation
272 local set_animation = function(self, anim)
274 if not self.animation
275 or not anim then return end
277 self.animation.current = self.animation.current or ""
279 if anim == self.animation.current
280 or not self.animation[anim .. "_start"]
281 or not self.animation[anim .. "_end"] then
282 return
285 self.animation.current = anim
287 self.object:set_animation({
288 x = self.animation[anim .. "_start"],
289 y = self.animation[anim .. "_end"]},
290 self.animation[anim .. "_speed"] or self.animation.speed_normal or 15,
291 0, self.animation[anim .. "_loop"] ~= false)
295 -- above function exported for mount.lua
296 function mobs:set_animation(self, anim)
297 set_animation(self, anim)
300 -- Returns true is node can deal damage to self
301 local is_node_dangerous = function(self, nodename)
302 local nn = nodename
303 if self.water_damage > 0 then
304 if minetest.get_item_group(nn, "water") ~= 0 then
305 return true
308 if self.lava_damage > 0 then
309 if minetest.get_item_group(nn, "lava") ~= 0 then
310 return true
313 if self.fire_damage > 0 then
314 if minetest.get_item_group(nn, "fire") ~= 0 then
315 return true
318 if minetest.registered_nodes[nn].drowning > 0 then
319 if self.breath_max ~= -1 then
320 return true
323 if minetest.registered_nodes[nn].damage_per_second > 0 then
324 return true
326 return false
330 -- check line of sight (BrunoMine)
331 local line_of_sight = function(self, pos1, pos2, stepsize)
333 stepsize = stepsize or 1
335 local s, pos = minetest.line_of_sight(pos1, pos2, stepsize)
337 -- normal walking and flying mobs can see you through air
338 if s == true then
339 return true
342 -- New pos1 to be analyzed
343 local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
345 local r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
347 -- Checks the return
348 if r == true then return true end
350 -- Nodename found
351 local nn = minetest.get_node(pos).name
353 -- Target Distance (td) to travel
354 local td = vector.distance(pos1, pos2)
356 -- Actual Distance (ad) traveled
357 local ad = 0
359 -- It continues to advance in the line of sight in search of a real
360 -- obstruction which counts as 'normal' nodebox.
361 while minetest.registered_nodes[nn]
362 and minetest.registered_nodes[nn].walkable == false do
364 -- Check if you can still move forward
365 if td < ad + stepsize then
366 return true -- Reached the target
369 -- Moves the analyzed pos
370 local d = vector.distance(pos1, pos2)
372 npos1.x = ((pos2.x - pos1.x) / d * stepsize) + pos1.x
373 npos1.y = ((pos2.y - pos1.y) / d * stepsize) + pos1.y
374 npos1.z = ((pos2.z - pos1.z) / d * stepsize) + pos1.z
376 -- NaN checks
377 if d == 0
378 or npos1.x ~= npos1.x
379 or npos1.y ~= npos1.y
380 or npos1.z ~= npos1.z then
381 return false
384 ad = ad + stepsize
386 -- scan again
387 r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
389 if r == true then return true end
391 -- New Nodename found
392 nn = minetest.get_node(pos).name
396 return false
400 -- are we flying in what we are suppose to? (taikedz)
401 local flight_check = function(self)
403 local nod = self.standing_in
404 local def = minetest.registered_nodes[nod]
406 if not def then return false end -- nil check
408 local fly_in
409 if type(self.fly_in) == "string" then
410 fly_in = { self.fly_in }
411 elseif type(self.fly_in) == "table" then
412 fly_in = self.fly_in
413 else
414 return false
417 for _,checknode in pairs(fly_in) do
418 if nod == checknode then
419 return true
420 elseif checknode == "__airlike" and def.walkable == false and
421 (def.liquidtype == "none" or minetest.get_item_group(nod, "fake_liquid") == 1) then
422 return true
426 return false
430 -- custom particle effects
431 local effect = function(pos, amount, texture, min_size, max_size, radius, gravity, glow, go_down)
433 radius = radius or 2
434 min_size = min_size or 0.5
435 max_size = max_size or 1
436 gravity = gravity or -10
437 glow = glow or 0
438 go_down = go_down or false
440 local ym
441 if go_down then
442 ym = 0
443 else
444 ym = -radius
447 minetest.add_particlespawner({
448 amount = amount,
449 time = 0.25,
450 minpos = pos,
451 maxpos = pos,
452 minvel = {x = -radius, y = ym, z = -radius},
453 maxvel = {x = radius, y = radius, z = radius},
454 minacc = {x = 0, y = gravity, z = 0},
455 maxacc = {x = 0, y = gravity, z = 0},
456 minexptime = 0.1,
457 maxexptime = 1,
458 minsize = min_size,
459 maxsize = max_size,
460 texture = texture,
461 glow = glow,
465 local damage_effect = function(self, damage)
466 -- damage particles
467 if (not disable_blood) and damage > 0 then
469 local amount_large = math.floor(damage / 2)
470 local amount_small = damage % 2
472 local pos = self.object:get_pos()
474 pos.y = pos.y + (self.collisionbox[5] - self.collisionbox[2]) * .5
476 local texture = "mobs_blood.png"
477 -- full heart damage (one particle for each 2 HP damage)
478 if amount_large > 0 then
479 effect(pos, amount_large, texture, 2, 2, 1.75, 0, nil, true)
481 -- half heart damage (one additional particle if damage is an odd number)
482 if amount_small > 0 then
483 -- TODO: Use "half heart"
484 effect(pos, amount_small, texture, 1, 1, 1.75, 0, nil, true)
489 local update_tag = function(self)
490 self.object:set_properties({
491 nametag = self.nametag,
497 -- drop items
498 local item_drop = function(self, cooked)
500 -- no drops if disabled by setting
501 if not mobs_drop_items then return end
503 -- no drops for child mobs (except monster)
504 if (self.child and self.type ~= "monster") then
505 return
508 local obj, item, num
509 local pos = self.object:get_pos()
511 self.drops = self.drops or {} -- nil check
513 for n = 1, #self.drops do
515 if random(1, self.drops[n].chance) == 1 then
517 num = random(self.drops[n].min or 1, self.drops[n].max or 1)
518 item = self.drops[n].name
520 -- cook items when true
521 if cooked then
523 local output = minetest.get_craft_result({
524 method = "cooking", width = 1, items = {item}})
526 if output and output.item and not output.item:is_empty() then
527 item = output.item:get_name()
531 -- add item if it exists
532 obj = minetest.add_item(pos, ItemStack(item .. " " .. num))
534 if obj and obj:get_luaentity() then
536 obj:set_velocity({
537 x = random(-10, 10) / 9,
538 y = 6,
539 z = random(-10, 10) / 9,
541 elseif obj then
542 obj:remove() -- item does not exist
547 self.drops = {}
551 -- check if mob is dead or only hurt
552 local check_for_death = function(self, cause, cmi_cause)
554 -- has health actually changed?
555 if self.health == self.old_health and self.health > 0 then
556 return false
559 local damaged = self.health < self.old_health
560 self.old_health = self.health
562 -- still got some health?
563 if self.health > 0 then
565 -- make sure health isn't higher than max
566 if self.health > self.hp_max then
567 self.health = self.hp_max
570 -- play damage sound if health was reduced and make mob flash red.
571 if damaged then
572 add_texture_mod(self, "^[colorize:#FF000040")
573 minetest.after(.2, function(self)
574 if self and self.object then
575 remove_texture_mod(self, "^[colorize:#FF000040")
577 end, self)
578 mob_sound(self, "damage")
581 -- backup nametag so we can show health stats
582 if not self.nametag2 then
583 self.nametag2 = self.nametag or ""
586 if show_health
587 and (cmi_cause and cmi_cause.type == "punch") then
589 self.htimer = 2
590 self.nametag = "♥ " .. self.health .. " / " .. self.hp_max
592 update_tag(self)
595 return false
598 -- dropped cooked item if mob died in fire or lava
599 if cause == "lava" or cause == "fire" then
600 item_drop(self, true)
601 else
602 item_drop(self, nil)
605 mob_sound(self, "death")
607 local pos = self.object:get_pos()
609 -- execute custom death function
610 if self.on_die then
612 self.on_die(self, pos)
614 if use_cmi then
615 cmi.notify_die(self.object, cmi_cause)
618 self.object:remove()
620 return true
623 -- default death function and die animation (if defined)
624 if self.animation
625 and self.animation.die_start
626 and self.animation.die_end then
628 local frames = self.animation.die_end - self.animation.die_start
629 local speed = self.animation.die_speed or 15
630 local length = max(frames / speed, 0)
632 self.attack = nil
633 self.v_start = false
634 self.timer = 0
635 self.blinktimer = 0
636 self.passive = true
637 self.state = "die"
638 set_velocity(self, 0)
639 set_animation(self, "die")
641 minetest.after(length, function(self)
642 if not self.object:get_luaentity() then
643 return
645 if use_cmi then
646 cmi.notify_die(self.object, cmi_cause)
649 self.object:remove()
650 end, self)
651 else
653 if use_cmi then
654 cmi.notify_die(self.object, cmi_cause)
657 self.object:remove()
660 effect(pos, 20, "tnt_smoke.png")
662 return true
666 -- check if within physical map limits (-30911 to 30927)
667 local within_limits = function(pos, radius)
669 if (pos.x - radius) > -30913
670 and (pos.x + radius) < 30928
671 and (pos.y - radius) > -30913
672 and (pos.y + radius) < 30928
673 and (pos.z - radius) > -30913
674 and (pos.z + radius) < 30928 then
675 return true -- within limits
678 return false -- beyond limits
682 -- is mob facing a cliff or danger
683 local is_at_cliff_or_danger = function(self)
685 if self.fear_height == 0 then -- 0 for no falling protection!
686 return false
689 if not self.object:get_luaentity() then
690 return false
692 local yaw = self.object:get_yaw()
693 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
694 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
695 local pos = self.object:get_pos()
696 local ypos = pos.y + self.collisionbox[2] -- just above floor
698 local free_fall, blocker = minetest.line_of_sight(
699 {x = pos.x + dir_x, y = ypos, z = pos.z + dir_z},
700 {x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z})
701 if free_fall then
702 return true
703 else
704 local bnode = minetest.get_node(blocker)
705 local danger = is_node_dangerous(self, bnode.name)
706 if danger then
707 return true
708 else
709 local def = minetest.registered_nodes[bnode.name]
710 return (not def and def.walkable)
714 return false
718 -- get node but use fallback for nil or unknown
719 local node_ok = function(pos, fallback)
721 fallback = fallback or mobs.fallback_node
723 local node = minetest.get_node_or_nil(pos)
725 if node and minetest.registered_nodes[node.name] then
726 return node
729 return minetest.registered_nodes[fallback]
733 -- environmental damage (water, lava, fire, light etc.)
734 local do_env_damage = function(self)
736 -- feed/tame text timer (so mob 'full' messages dont spam chat)
737 if self.htimer > 0 then
738 self.htimer = self.htimer - 1
741 -- reset nametag after showing health stats
742 if self.htimer < 1 and self.nametag2 then
744 self.nametag = self.nametag2
745 self.nametag2 = nil
747 update_tag(self)
750 local pos = self.object:get_pos()
752 self.time_of_day = minetest.get_timeofday()
754 -- remove mob if beyond map limits
755 if not within_limits(pos, 0) then
756 self.object:remove()
757 return true
761 -- Deal light damage to mob, returns true if mob died
762 local deal_light_damage = function(self, pos, damage)
763 if not (mod_weather and (mcl_weather.rain.raining or mcl_weather.state == "snow") and mcl_weather.is_outdoor(pos)) then
764 self.health = self.health - damage
766 effect(pos, 5, "tnt_smoke.png")
768 if check_for_death(self, "light", {type = "light"}) then
769 return true
774 -- bright light harms mob
775 if self.light_damage ~= 0 and (minetest.get_node_light(pos) or 0) > 12 then
776 if deal_light_damage(self, pos, self.light_damage) then
777 return true
780 local _, dim = nil, "overworld"
781 if mod_worlds then
782 _, dim = mcl_worlds.y_to_layer(pos.y)
784 if self.sunlight_damage ~= 0 and (minetest.get_node_light(pos) or 0) >= minetest.LIGHT_MAX and dim == "overworld" then
785 if deal_light_damage(self, pos, self.sunlight_damage) then
786 return true
790 local y_level = self.collisionbox[2]
792 if self.child then
793 y_level = self.collisionbox[2] * 0.5
796 -- what is mob standing in?
797 pos.y = pos.y + y_level + 0.25 -- foot level
798 local pos2 = {x=pos.x, y=pos.y-1, z=pos.z}
799 self.standing_in = node_ok(pos, "air").name
800 self.standing_on = node_ok(pos2, "air").name
802 -- don't fall when on ignore, just stand still
803 if self.standing_in == "ignore" then
804 self.object:set_velocity({x = 0, y = 0, z = 0})
807 local nodef = minetest.registered_nodes[self.standing_in]
809 -- rain
810 if self.rain_damage > 0 and mod_weather then
811 if mcl_weather.rain.raining and mcl_weather.is_outdoor(pos) then
813 self.health = self.health - self.rain_damage
815 if check_for_death(self, "rain", {type = "environment",
816 pos = pos, node = self.standing_in}) then
817 return true
822 pos.y = pos.y + 1 -- for particle effect position
824 -- water damage
825 if self.water_damage > 0
826 and nodef.groups.water then
828 if self.water_damage ~= 0 then
830 self.health = self.health - self.water_damage
832 effect(pos, 5, "tnt_smoke.png", nil, nil, 1, nil)
834 if check_for_death(self, "water", {type = "environment",
835 pos = pos, node = self.standing_in}) then
836 return true
840 -- lava damage
841 elseif self.lava_damage > 0
842 and (nodef.groups.lava) then
844 if self.lava_damage ~= 0 then
846 self.health = self.health - self.lava_damage
848 effect(pos, 5, "fire_basic_flame.png", nil, nil, 1, nil)
850 if check_for_death(self, "lava", {type = "environment",
851 pos = pos, node = self.standing_in}) then
852 return true
856 -- fire damage
857 elseif self.fire_damage > 0
858 and (nodef.groups.fire) then
860 if self.fire_damage ~= 0 then
862 self.health = self.health - self.fire_damage
864 effect(pos, 5, "fire_basic_flame.png", nil, nil, 1, nil)
866 if check_for_death(self, "fire", {type = "environment",
867 pos = pos, node = self.standing_in}) then
868 return true
872 -- damage_per_second node check
873 elseif nodef.damage_per_second ~= 0 then
875 self.health = self.health - nodef.damage_per_second
877 effect(pos, 5, "tnt_smoke.png")
879 if check_for_death(self, "dps", {type = "environment",
880 pos = pos, node = self.standing_in}) then
881 return true
885 -- Drowning damage
886 if self.breath_max ~= -1 then
887 local drowning = false
888 if self.breathes_in_water then
889 if minetest.get_item_group(self.standing_in, "water") == 0 then
890 drowning = true
892 elseif nodef.drowning > 0 then
893 drowning = true
895 if drowning then
897 self.breath = math.max(0, self.breath - 1)
899 effect(pos, 2, "bubble.png", nil, nil, 1, nil)
900 if self.breath <= 0 then
901 local dmg
902 if nodef.drowning > 0 then
903 dmg = nodef.drowning
904 else
905 dmg = 4
907 damage_effect(self, dmg)
908 self.health = self.health - dmg
910 if check_for_death(self, "drowning", {type = "environment",
911 pos = pos, node = self.standing_in}) then
912 return true
914 else
915 self.breath = math.min(self.breath_max, self.breath + 1)
919 --- suffocation inside solid node
920 -- FIXME: Redundant with mcl_playerplus
921 if (self.suffocation == true)
922 and (nodef.walkable == nil or nodef.walkable == true)
923 and (nodef.collision_box == nil or nodef.collision_box.type == "regular")
924 and (nodef.node_box == nil or nodef.node_box.type == "regular")
925 and (nodef.groups.disable_suffocation ~= 1)
926 and (nodef.groups.opaque == 1) then
928 -- 2 damage per second
929 -- TODO: Deal this damage once every 1/2 second
930 self.health = self.health - 2
932 if check_for_death(self, "suffocation", {type = "environment",
933 pos = pos, node = self.standing_in}) then
934 return true
938 return check_for_death(self, "", {type = "unknown"})
942 -- jump if facing a solid node (not fences or gates)
943 local do_jump = function(self)
945 if not self.jump
946 or self.jump_height == 0
947 or self.fly
948 or (self.child and self.type ~= "monster")
949 or self.order == "stand" then
950 return false
953 self.facing_fence = false
955 -- something stopping us while moving?
956 if self.state ~= "stand"
957 and get_velocity(self) > 0.5
958 and self.object:get_velocity().y ~= 0 then
959 return false
962 local pos = self.object:get_pos()
963 local yaw = self.object:get_yaw()
965 -- what is mob standing on?
966 pos.y = pos.y + self.collisionbox[2] - 0.2
968 local nod = node_ok(pos)
970 if minetest.registered_nodes[nod.name].walkable == false then
971 return false
974 -- where is front
975 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
976 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
978 -- what is in front of mob?
979 nod = node_ok({
980 x = pos.x + dir_x,
981 y = pos.y + 0.5,
982 z = pos.z + dir_z
985 -- this is used to detect if there's a block on top of the block in front of the mob.
986 -- If there is, there is no point in jumping as we won't manage.
987 local nodTop = node_ok({
988 x = pos.x + dir_x,
989 y = pos.y + 1.5,
990 z = pos.z + dir_z
991 }, "air")
993 -- we don't attempt to jump if there's a stack of blocks blocking
994 if minetest.registered_nodes[nodTop.name] == true then
995 return false
998 -- thin blocks that do not need to be jumped
999 if nod.name == node_snow then
1000 return false
1003 if self.walk_chance == 0
1004 or minetest.registered_items[nod.name].walkable then
1006 if minetest.get_item_group(nod.name, "fence") == 0
1007 and minetest.get_item_group(nod.name, "fence_gate") == 0
1008 and minetest.get_item_group(nod.name, "wall") == 0 then
1010 local v = self.object:get_velocity()
1012 v.y = self.jump_height
1014 set_animation(self, "jump") -- only when defined
1016 self.object:set_velocity(v)
1018 -- when in air move forward
1019 minetest.after(0.3, function(self, v)
1020 if not self.object or not self.object:get_luaentity() then
1021 return
1023 self.object:set_acceleration({
1024 x = v.x * 2,
1025 y = 0,
1026 z = v.z * 2,
1028 end, self, v)
1030 if self.jump_sound_cooloff <= 0 then
1031 mob_sound(self, "jump")
1032 self.jump_sound_cooloff = 0.5
1034 else
1035 self.facing_fence = true
1038 -- if we jumped against a block/wall 4 times then turn
1039 if self.object:get_velocity().x ~= 0
1040 and self.object:get_velocity().z ~= 0 then
1042 self.jump_count = (self.jump_count or 0) + 1
1044 if self.jump_count == 4 then
1046 local yaw = self.object:get_yaw() or 0
1048 yaw = set_yaw(self, yaw + 1.35, 8)
1050 self.jump_count = 0
1054 return true
1057 return false
1061 -- blast damage to entities nearby (modified from TNT mod)
1062 local entity_physics = function(pos, radius)
1064 radius = radius * 2
1066 local objs = minetest.get_objects_inside_radius(pos, radius)
1067 local obj_pos, dist
1069 for n = 1, #objs do
1071 obj_pos = objs[n]:get_pos()
1073 dist = vector.distance(pos, obj_pos)
1074 if dist < 1 then dist = 1 end
1076 local damage = floor((4 / dist) * radius)
1077 local ent = objs[n]:get_luaentity()
1079 -- punches work on entities AND players
1080 objs[n]:punch(objs[n], 1.0, {
1081 full_punch_interval = 1.0,
1082 damage_groups = {fleshy = damage},
1083 }, pos)
1088 -- should mob follow what I'm holding ?
1089 local follow_holding = function(self, clicker)
1091 if mobs.invis[clicker:get_player_name()] then
1092 return false
1095 local item = clicker:get_wielded_item()
1096 local t = type(self.follow)
1098 -- single item
1099 if t == "string"
1100 and item:get_name() == self.follow then
1101 return true
1103 -- multiple items
1104 elseif t == "table" then
1106 for no = 1, #self.follow do
1108 if self.follow[no] == item:get_name() then
1109 return true
1114 return false
1118 -- find two animals of same type and breed if nearby and horny
1119 local breed = function(self)
1121 -- child takes 240 seconds before growing into adult
1122 if self.child == true then
1124 self.hornytimer = self.hornytimer + 1
1126 if self.hornytimer > 240 then
1128 self.child = false
1129 self.hornytimer = 0
1131 self.object:set_properties({
1132 textures = self.base_texture,
1133 mesh = self.base_mesh,
1134 visual_size = self.base_size,
1135 collisionbox = self.base_colbox,
1136 selectionbox = self.base_selbox,
1139 -- custom function when child grows up
1140 if self.on_grown then
1141 self.on_grown(self)
1142 else
1143 -- jump when fully grown so as not to fall into ground
1144 self.object:set_velocity({
1145 x = 0,
1146 y = self.jump_height,
1147 z = 0
1152 return
1155 -- horny animal can mate for 40 seconds,
1156 -- afterwards horny animal cannot mate again for 200 seconds
1157 if self.horny == true
1158 and self.hornytimer < 240 then
1160 self.hornytimer = self.hornytimer + 1
1162 if self.hornytimer >= 240 then
1163 self.hornytimer = 0
1164 self.horny = false
1168 -- find another same animal who is also horny and mate if nearby
1169 if self.horny == true
1170 and self.hornytimer <= 40 then
1172 local pos = self.object:get_pos()
1174 effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, "heart.png", 3, 4, 1, 0.1)
1176 local objs = minetest.get_objects_inside_radius(pos, 3)
1177 local num = 0
1178 local ent = nil
1180 for n = 1, #objs do
1182 ent = objs[n]:get_luaentity()
1184 -- check for same animal with different colour
1185 local canmate = false
1187 if ent then
1189 if ent.name == self.name then
1190 canmate = true
1191 else
1192 local entname = string.split(ent.name,":")
1193 local selfname = string.split(self.name,":")
1195 if entname[1] == selfname[1] then
1196 entname = string.split(entname[2],"_")
1197 selfname = string.split(selfname[2],"_")
1199 if entname[1] == selfname[1] then
1200 canmate = true
1206 if ent
1207 and canmate == true
1208 and ent.horny == true
1209 and ent.hornytimer <= 40 then
1210 num = num + 1
1213 -- found your mate? then have a baby
1214 if num > 1 then
1216 self.hornytimer = 41
1217 ent.hornytimer = 41
1219 -- spawn baby
1220 minetest.after(5, function(parent1, parent2, pos)
1221 if not parent1.object:get_luaentity() then
1222 return
1224 if not parent2.object:get_luaentity() then
1225 return
1228 -- custom breed function
1229 if parent1.on_breed then
1230 -- when false, skip going any further
1231 if parent1.on_breed(parent1, parent2) == false then
1232 return
1236 local child = mobs:spawn_child(pos, parent1.name)
1238 local ent_c = child:get_luaentity()
1241 -- Use texture of one of the parents
1242 local p = math.random(1, 2)
1243 if p == 1 then
1244 ent_c.base_texture = parent1.base_texture
1245 else
1246 ent_c.base_texture = parent2.base_texture
1248 child:set_properties({
1249 textures = ent_c.base_texture
1252 -- tamed and owned by parents' owner
1253 ent_c.tamed = true
1254 ent_c.owner = parent1.owner
1255 end, self, ent, pos)
1257 num = 0
1259 break
1266 -- find and replace what mob is looking for (grass, wheat etc.)
1267 local replace = function(self, pos)
1269 if not self.replace_rate
1270 or not self.replace_what
1271 or self.child == true
1272 or self.object:get_velocity().y ~= 0
1273 or random(1, self.replace_rate) > 1 then
1274 return
1277 local what, with, y_offset
1279 if type(self.replace_what[1]) == "table" then
1281 local num = random(#self.replace_what)
1283 what = self.replace_what[num][1] or ""
1284 with = self.replace_what[num][2] or ""
1285 y_offset = self.replace_what[num][3] or 0
1286 else
1287 what = self.replace_what
1288 with = self.replace_with or ""
1289 y_offset = self.replace_offset or 0
1292 pos.y = pos.y + y_offset
1294 if #minetest.find_nodes_in_area(pos, pos, what) > 0 then
1296 local oldnode = {name = what}
1297 local newnode = {name = with}
1298 local on_replace_return
1300 if self.on_replace then
1301 on_replace_return = self.on_replace(self, pos, oldnode, newnode)
1304 if on_replace_return ~= false then
1306 if mobs_griefing then
1307 minetest.set_node(pos, {name = with})
1315 -- check if daytime and also if mob is docile during daylight hours
1316 local day_docile = function(self)
1318 if self.docile_by_day == false then
1320 return false
1322 elseif self.docile_by_day == true
1323 and self.time_of_day > 0.2
1324 and self.time_of_day < 0.8 then
1326 return true
1331 local los_switcher = false
1332 local height_switcher = false
1334 -- path finding and smart mob routine by rnd, line_of_sight and other edits by Elkien3
1335 local smart_mobs = function(self, s, p, dist, dtime)
1337 local s1 = self.path.lastpos
1339 local target_pos = self.attack:get_pos()
1341 -- is it becoming stuck?
1342 if abs(s1.x - s.x) + abs(s1.z - s.z) < .5 then
1343 self.path.stuck_timer = self.path.stuck_timer + dtime
1344 else
1345 self.path.stuck_timer = 0
1348 self.path.lastpos = {x = s.x, y = s.y, z = s.z}
1350 local use_pathfind = false
1351 local has_lineofsight = minetest.line_of_sight(
1352 {x = s.x, y = (s.y) + .5, z = s.z},
1353 {x = target_pos.x, y = (target_pos.y) + 1.5, z = target_pos.z}, .2)
1355 -- im stuck, search for path
1356 if not has_lineofsight then
1358 if los_switcher == true then
1359 use_pathfind = true
1360 los_switcher = false
1361 end -- cannot see target!
1362 else
1363 if los_switcher == false then
1365 los_switcher = true
1366 use_pathfind = false
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)
1374 end -- can see target!
1377 if (self.path.stuck_timer > stuck_timeout and not self.path.following) then
1379 use_pathfind = true
1380 self.path.stuck_timer = 0
1382 minetest.after(1, function(self)
1383 if not self.object:get_luaentity() then
1384 return
1386 if has_lineofsight then self.path.following = false end
1387 end, self)
1390 if (self.path.stuck_timer > stuck_path_timeout and self.path.following) then
1392 use_pathfind = true
1393 self.path.stuck_timer = 0
1395 minetest.after(1, function(self)
1396 if not self.object:get_luaentity() then
1397 return
1399 if has_lineofsight then self.path.following = false end
1400 end, self)
1403 if math.abs(vector.subtract(s,target_pos).y) > self.stepheight then
1405 if height_switcher then
1406 use_pathfind = true
1407 height_switcher = false
1409 else
1410 if not height_switcher then
1411 use_pathfind = false
1412 height_switcher = true
1416 if use_pathfind then
1417 -- lets try find a path, first take care of positions
1418 -- since pathfinder is very sensitive
1419 local sheight = self.collisionbox[5] - self.collisionbox[2]
1421 -- round position to center of node to avoid stuck in walls
1422 -- also adjust height for player models!
1423 s.x = floor(s.x + 0.5)
1424 s.z = floor(s.z + 0.5)
1426 local ssight, sground = minetest.line_of_sight(s, {
1427 x = s.x, y = s.y - 4, z = s.z}, 1)
1429 -- determine node above ground
1430 if not ssight then
1431 s.y = sground.y + 1
1434 local p1 = self.attack:get_pos()
1436 p1.x = floor(p1.x + 0.5)
1437 p1.y = floor(p1.y + 0.5)
1438 p1.z = floor(p1.z + 0.5)
1440 local dropheight = 12
1441 if self.fear_height ~= 0 then dropheight = self.fear_height end
1442 local jumpheight = 0
1443 if self.jump and self.jump_height >= 4 then
1444 jumpheight = math.min(math.ceil(self.jump_height / 4), 4)
1445 elseif self.stepheight > 0.5 then
1446 jumpheight = 1
1448 self.path.way = minetest.find_path(s, p1, 16, jumpheight, dropheight, "A*_noprefetch")
1450 self.state = ""
1451 do_attack(self, self.attack)
1453 -- no path found, try something else
1454 if not self.path.way then
1456 self.path.following = false
1458 -- lets make way by digging/building if not accessible
1459 if self.pathfinding == 2 and mobs_griefing then
1461 -- is player higher than mob?
1462 if s.y < p1.y then
1464 -- build upwards
1465 if not minetest.is_protected(s, "") then
1467 local ndef1 = minetest.registered_nodes[self.standing_in]
1469 if ndef1 and (ndef1.buildable_to or ndef1.groups.liquid) then
1471 minetest.set_node(s, {name = mobs.fallback_node})
1475 local sheight = math.ceil(self.collisionbox[5]) + 1
1477 -- assume mob is 2 blocks high so it digs above its head
1478 s.y = s.y + sheight
1480 -- remove one block above to make room to jump
1481 if not minetest.is_protected(s, "") then
1483 local node1 = node_ok(s, "air").name
1484 local ndef1 = minetest.registered_nodes[node1]
1486 if node1 ~= "air"
1487 and node1 ~= "ignore"
1488 and ndef1
1489 and not ndef1.groups.level
1490 and not ndef1.groups.unbreakable
1491 and not ndef1.groups.liquid then
1493 minetest.set_node(s, {name = "air"})
1494 minetest.add_item(s, ItemStack(node1))
1499 s.y = s.y - sheight
1500 self.object:set_pos({x = s.x, y = s.y + 2, z = s.z})
1502 else -- dig 2 blocks to make door toward player direction
1504 local yaw1 = self.object:get_yaw() + pi / 2
1505 local p1 = {
1506 x = s.x + cos(yaw1),
1507 y = s.y,
1508 z = s.z + sin(yaw1)
1511 if not minetest.is_protected(p1, "") then
1513 local node1 = node_ok(p1, "air").name
1514 local ndef1 = minetest.registered_nodes[node1]
1516 if node1 ~= "air"
1517 and node1 ~= "ignore"
1518 and ndef1
1519 and not ndef1.groups.level
1520 and not ndef1.groups.unbreakable
1521 and not ndef1.groups.liquid then
1523 minetest.add_item(p1, ItemStack(node1))
1524 minetest.set_node(p1, {name = "air"})
1527 p1.y = p1.y + 1
1528 node1 = node_ok(p1, "air").name
1529 ndef1 = minetest.registered_nodes[node1]
1531 if node1 ~= "air"
1532 and node1 ~= "ignore"
1533 and ndef1
1534 and not ndef1.groups.level
1535 and not ndef1.groups.unbreakable
1536 and not ndef1.groups.liquid then
1538 minetest.add_item(p1, ItemStack(node1))
1539 minetest.set_node(p1, {name = "air"})
1546 -- will try again in 2 seconds
1547 self.path.stuck_timer = stuck_timeout - 2
1548 elseif s.y < p1.y and (not self.fly) then
1549 do_jump(self) --add jump to pathfinding
1550 self.path.following = true
1551 -- Yay, I found path!
1552 -- TODO: Implement war_cry sound without being annoying
1553 --mob_sound(self, "war_cry", true)
1554 else
1555 set_velocity(self, self.walk_velocity)
1557 -- follow path now that it has it
1558 self.path.following = true
1564 -- specific attacks
1565 local specific_attack = function(list, what)
1567 -- no list so attack default (player, animals etc.)
1568 if list == nil then
1569 return true
1572 -- found entity on list to attack?
1573 for no = 1, #list do
1575 if list[no] == what then
1576 return true
1580 return false
1583 -- monster find someone to attack
1584 local monster_attack = function(self)
1586 if self.type ~= "monster"
1587 or not damage_enabled
1588 or creative
1589 or self.state == "attack"
1590 or day_docile(self) then
1591 return
1594 local s = self.object:get_pos()
1595 local p, sp, dist
1596 local player, obj, min_player
1597 local type, name = "", ""
1598 local min_dist = self.view_range + 1
1599 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1601 for n = 1, #objs do
1603 if objs[n]:is_player() then
1605 if mobs.invis[ objs[n]:get_player_name() ] or (not object_in_range(self, objs[n])) then
1606 type = ""
1607 else
1608 player = objs[n]
1609 type = "player"
1610 name = "player"
1612 else
1613 obj = objs[n]:get_luaentity()
1615 if obj then
1616 player = obj.object
1617 type = obj.type
1618 name = obj.name or ""
1622 -- find specific mob to attack, failing that attack player/npc/animal
1623 if specific_attack(self.specific_attack, name)
1624 and (type == "player" or type == "npc"
1625 or (type == "animal" and self.attack_animals == true)) then
1627 p = player:get_pos()
1628 sp = s
1630 dist = vector.distance(p, s)
1632 -- aim higher to make looking up hills more realistic
1633 p.y = p.y + 1
1634 sp.y = sp.y + 1
1637 -- choose closest player to attack
1638 if dist < min_dist
1639 and line_of_sight(self, sp, p, 2) == true then
1640 min_dist = dist
1641 min_player = player
1646 -- attack player
1647 if min_player then
1648 do_attack(self, min_player)
1653 -- npc, find closest monster to attack
1654 local npc_attack = function(self)
1656 if self.type ~= "npc"
1657 or not self.attacks_monsters
1658 or self.state == "attack" then
1659 return
1662 local p, sp, obj, min_player
1663 local s = self.object:get_pos()
1664 local min_dist = self.view_range + 1
1665 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1667 for n = 1, #objs do
1669 obj = objs[n]:get_luaentity()
1671 if obj and obj.type == "monster" then
1673 p = obj.object:get_pos()
1674 sp = s
1676 local dist = vector.distance(p, s)
1678 -- aim higher to make looking up hills more realistic
1679 p.y = p.y + 1
1680 sp.y = sp.y + 1
1682 if dist < min_dist
1683 and line_of_sight(self, sp, p, 2) == true then
1684 min_dist = dist
1685 min_player = obj.object
1690 if min_player then
1691 do_attack(self, min_player)
1696 -- specific runaway
1697 local specific_runaway = function(list, what)
1699 -- no list so do not run
1700 if list == nil then
1701 return false
1704 -- found entity on list to attack?
1705 for no = 1, #list do
1707 if list[no] == what then
1708 return true
1712 return false
1716 -- find someone to runaway from
1717 local runaway_from = function(self)
1719 if not self.runaway_from then
1720 return
1723 local s = self.object:get_pos()
1724 local p, sp, dist
1725 local player, obj, min_player
1726 local type, name = "", ""
1727 local min_dist = self.view_range + 1
1728 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1730 for n = 1, #objs do
1732 if objs[n]:is_player() then
1734 if mobs.invis[ objs[n]:get_player_name() ]
1735 or self.owner == objs[n]:get_player_name()
1736 or (not object_in_range(self, objs[n])) then
1737 type = ""
1738 else
1739 player = objs[n]
1740 type = "player"
1741 name = "player"
1743 else
1744 obj = objs[n]:get_luaentity()
1746 if obj then
1747 player = obj.object
1748 type = obj.type
1749 name = obj.name or ""
1753 -- find specific mob to runaway from
1754 if name ~= "" and name ~= self.name
1755 and specific_runaway(self.runaway_from, name) then
1757 p = player:get_pos()
1758 sp = s
1760 -- aim higher to make looking up hills more realistic
1761 p.y = p.y + 1
1762 sp.y = sp.y + 1
1764 dist = vector.distance(p, s)
1767 -- choose closest player/mpb to runaway from
1768 if dist < min_dist
1769 and line_of_sight(self, sp, p, 2) == true then
1770 min_dist = dist
1771 min_player = player
1776 if min_player then
1778 local lp = player:get_pos()
1779 local vec = {
1780 x = lp.x - s.x,
1781 y = lp.y - s.y,
1782 z = lp.z - s.z
1785 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
1787 if lp.x > s.x then
1788 yaw = yaw + pi
1791 yaw = set_yaw(self, yaw, 4)
1792 self.state = "runaway"
1793 self.runaway_timer = 3
1794 self.following = nil
1799 -- follow player if owner or holding item, if fish outta water then flop
1800 local follow_flop = function(self)
1802 -- find player to follow
1803 if (self.follow ~= ""
1804 or self.order == "follow")
1805 and not self.following
1806 and self.state ~= "attack"
1807 and self.state ~= "runaway" then
1809 local s = self.object:get_pos()
1810 local players = minetest.get_connected_players()
1812 for n = 1, #players do
1814 if (object_in_range(self, players[n]))
1815 and not mobs.invis[ players[n]:get_player_name() ] then
1817 self.following = players[n]
1819 break
1824 if self.type == "npc"
1825 and self.order == "follow"
1826 and self.state ~= "attack"
1827 and self.owner ~= "" then
1829 -- npc stop following player if not owner
1830 if self.following
1831 and self.owner
1832 and self.owner ~= self.following:get_player_name() then
1833 self.following = nil
1835 else
1836 -- stop following player if not holding specific item
1837 if self.following
1838 and self.following:is_player()
1839 and follow_holding(self, self.following) == false then
1840 self.following = nil
1845 -- follow that thing
1846 if self.following then
1848 local s = self.object:get_pos()
1849 local p
1851 if self.following:is_player() then
1853 p = self.following:get_pos()
1855 elseif self.following.object then
1857 p = self.following.object:get_pos()
1860 if p then
1862 local dist = vector.distance(p, s)
1864 -- dont follow if out of range
1865 if (not object_in_range(self, self.following)) then
1866 self.following = nil
1867 else
1868 local vec = {
1869 x = p.x - s.x,
1870 z = p.z - s.z
1873 local yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1875 if p.x > s.x then yaw = yaw + pi end
1877 set_yaw(self, yaw, 6)
1879 -- anyone but standing npc's can move along
1880 if dist > self.reach
1881 and self.order ~= "stand" then
1883 set_velocity(self, self.walk_velocity)
1885 if self.walk_chance ~= 0 then
1886 set_animation(self, "walk")
1888 else
1889 set_velocity(self, 0)
1890 set_animation(self, "stand")
1893 return
1898 -- swimmers flop when out of their element, and swim again when back in
1899 if self.fly then
1900 local s = self.object:get_pos()
1901 if not flight_check(self, s) then
1903 self.state = "flop"
1904 self.object:set_velocity({x = 0, y = -5, z = 0})
1906 set_animation(self, "stand")
1908 return
1909 elseif self.state == "flop" then
1910 self.state = "stand"
1916 -- dogshoot attack switch and counter function
1917 local dogswitch = function(self, dtime)
1919 -- switch mode not activated
1920 if not self.dogshoot_switch
1921 or not dtime then
1922 return 0
1925 self.dogshoot_count = self.dogshoot_count + dtime
1927 if (self.dogshoot_switch == 1
1928 and self.dogshoot_count > self.dogshoot_count_max)
1929 or (self.dogshoot_switch == 2
1930 and self.dogshoot_count > self.dogshoot_count2_max) then
1932 self.dogshoot_count = 0
1934 if self.dogshoot_switch == 1 then
1935 self.dogshoot_switch = 2
1936 else
1937 self.dogshoot_switch = 1
1941 return self.dogshoot_switch
1945 -- execute current state (stand, walk, run, attacks)
1946 -- returns true if mob has died
1947 local do_states = function(self, dtime)
1949 local yaw = self.object:get_yaw() or 0
1951 if self.state == "stand" then
1953 if random(1, 4) == 1 then
1955 local lp = nil
1956 local s = self.object:get_pos()
1957 local objs = minetest.get_objects_inside_radius(s, 3)
1959 for n = 1, #objs do
1961 if objs[n]:is_player() then
1962 lp = objs[n]:get_pos()
1963 break
1967 -- look at any players nearby, otherwise turn randomly
1968 if lp then
1970 local vec = {
1971 x = lp.x - s.x,
1972 z = lp.z - s.z
1975 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1977 if lp.x > s.x then yaw = yaw + pi end
1978 else
1979 yaw = yaw + random(-0.5, 0.5)
1982 yaw = set_yaw(self, yaw, 8)
1985 set_velocity(self, 0)
1986 set_animation(self, "stand")
1988 -- npc's ordered to stand stay standing
1989 if self.type ~= "npc"
1990 or self.order ~= "stand" then
1992 if self.walk_chance ~= 0
1993 and self.facing_fence ~= true
1994 and random(1, 100) <= self.walk_chance
1995 and is_at_cliff_or_danger(self) == false then
1997 set_velocity(self, self.walk_velocity)
1998 self.state = "walk"
1999 set_animation(self, "walk")
2003 elseif self.state == "walk" then
2005 local s = self.object:get_pos()
2006 local lp = nil
2008 -- is there something I need to avoid?
2009 if (self.water_damage > 0
2010 and self.lava_damage > 0)
2011 or self.breath_max ~= -1 then
2013 lp = minetest.find_node_near(s, 1, {"group:water", "group:lava"})
2015 elseif self.water_damage > 0 then
2017 lp = minetest.find_node_near(s, 1, {"group:water"})
2019 elseif self.lava_damage > 0 then
2021 lp = minetest.find_node_near(s, 1, {"group:lava"})
2023 elseif self.fire_damage > 0 then
2025 lp = minetest.find_node_near(s, 1, {"group:fire"})
2029 local is_in_danger = false
2030 if lp then
2031 -- If mob in or on dangerous block, look for land
2032 if (is_node_dangerous(self, self.standing_in) or
2033 is_node_dangerous(self, self.standing_on)) then
2034 is_in_danger = true
2036 lp = minetest.find_node_near(s, 5, {"group:solid"})
2038 -- did we find land?
2039 if lp then
2041 local vec = {
2042 x = lp.x - s.x,
2043 z = lp.z - s.z
2046 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2048 if lp.x > s.x then yaw = yaw + pi end
2050 -- look towards land and jump/move in that direction
2051 yaw = set_yaw(self, yaw, 6)
2052 do_jump(self)
2053 set_velocity(self, self.walk_velocity)
2054 else
2055 yaw = yaw + random(-0.5, 0.5)
2058 -- A danger is near but mob is not inside
2059 else
2061 -- Randomly turn
2062 if random(1, 100) <= 30 then
2063 yaw = yaw + random(-0.5, 0.5)
2064 yaw = set_yaw(self, yaw, 8)
2068 yaw = set_yaw(self, yaw, 8)
2070 -- otherwise randomly turn
2071 elseif random(1, 100) <= 30 then
2073 yaw = yaw + random(-0.5, 0.5)
2074 yaw = set_yaw(self, yaw, 8)
2077 -- stand for great fall or danger or fence in front
2078 local cliff_or_danger = false
2079 if is_in_danger then
2080 cliff_or_danger = is_at_cliff_or_danger(self)
2082 if self.facing_fence == true
2083 or cliff_or_danger
2084 or random(1, 100) <= 30 then
2086 set_velocity(self, 0)
2087 self.state = "stand"
2088 set_animation(self, "stand")
2089 else
2091 set_velocity(self, self.walk_velocity)
2093 if flight_check(self)
2094 and self.animation
2095 and self.animation.fly_start
2096 and self.animation.fly_end then
2097 set_animation(self, "fly")
2098 else
2099 set_animation(self, "walk")
2103 -- runaway when punched
2104 elseif self.state == "runaway" then
2106 self.runaway_timer = self.runaway_timer + 1
2108 -- stop after 5 seconds or when at cliff
2109 if self.runaway_timer > 5
2110 or is_at_cliff_or_danger(self) then
2111 self.runaway_timer = 0
2112 set_velocity(self, 0)
2113 self.state = "stand"
2114 set_animation(self, "stand")
2115 else
2116 set_velocity(self, self.run_velocity)
2117 set_animation(self, "walk")
2120 -- attack routines (explode, dogfight, shoot, dogshoot)
2121 elseif self.state == "attack" then
2123 -- calculate distance from mob and enemy
2124 local s = self.object:get_pos()
2125 local p = self.attack:get_pos() or s
2126 local dist = vector.distance(p, s)
2128 -- stop attacking if player invisible or out of range
2129 if not self.attack
2130 or not self.attack:get_pos()
2131 or not object_in_range(self, self.attack)
2132 or self.attack:get_hp() <= 0
2133 or (self.attack:is_player() and mobs.invis[ self.attack:get_player_name() ]) then
2135 self.state = "stand"
2136 set_velocity(self, 0)
2137 set_animation(self, "stand")
2138 self.attack = nil
2139 self.v_start = false
2140 self.timer = 0
2141 self.blinktimer = 0
2142 self.path.way = nil
2144 return
2147 if self.attack_type == "explode" then
2149 local vec = {
2150 x = p.x - s.x,
2151 z = p.z - s.z
2154 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2156 if p.x > s.x then yaw = yaw + pi end
2158 yaw = set_yaw(self, yaw)
2160 local node_break_radius = self.explosion_radius or 1
2161 local entity_damage_radius = self.explosion_damage_radius
2162 or (node_break_radius * 2)
2164 -- start timer when in reach and line of sight
2165 if not self.v_start
2166 and dist <= self.reach
2167 and line_of_sight(self, s, p, 2) then
2169 self.v_start = true
2170 self.timer = 0
2171 self.blinktimer = 0
2172 mob_sound(self, "fuse", nil, false)
2174 -- stop timer if out of reach or direct line of sight
2175 elseif self.allow_fuse_reset
2176 and self.v_start
2177 and (dist > self.reach
2178 or not line_of_sight(self, s, p, 2)) then
2179 self.v_start = false
2180 self.timer = 0
2181 self.blinktimer = 0
2182 self.blinkstatus = false
2183 remove_texture_mod(self, "^[brighten")
2186 -- walk right up to player unless the timer is active
2187 if self.v_start and (self.stop_to_explode or dist < 1.5) then
2188 set_velocity(self, 0)
2189 else
2190 set_velocity(self, self.run_velocity)
2193 if self.animation and self.animation.run_start then
2194 set_animation(self, "run")
2195 else
2196 set_animation(self, "walk")
2199 if self.v_start then
2201 self.timer = self.timer + dtime
2202 self.blinktimer = (self.blinktimer or 0) + dtime
2204 if self.blinktimer > 0.2 then
2206 self.blinktimer = 0
2208 if self.blinkstatus then
2209 remove_texture_mod(self, "^[brighten")
2210 else
2211 add_texture_mod(self, "^[brighten")
2214 self.blinkstatus = not self.blinkstatus
2217 if self.timer > self.explosion_timer then
2219 local pos = self.object:get_pos()
2221 if mod_explosions then
2222 if mobs_griefing and not minetest.is_protected(pos, "") then
2223 mcl_explosions.explode(self.object:get_pos(), self.explosion_strength, { drop_chance = 1.0 }, self.object)
2224 else
2225 minetest.sound_play(self.sounds.explode, {
2226 pos = pos,
2227 gain = 1.0,
2228 max_hear_distance = self.sounds.distance or 32
2229 }, true)
2231 entity_physics(pos, entity_damage_radius)
2232 effect(pos, 32, "tnt_smoke.png", nil, nil, node_break_radius, 1, 0)
2235 self.object:remove()
2237 return true
2241 elseif self.attack_type == "dogfight"
2242 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 2)
2243 or (self.attack_type == "dogshoot" and dist <= self.reach and dogswitch(self) == 0) then
2245 if self.fly
2246 and dist > self.reach then
2248 local p1 = s
2249 local me_y = floor(p1.y)
2250 local p2 = p
2251 local p_y = floor(p2.y + 1)
2252 local v = self.object:get_velocity()
2254 if flight_check(self, s) then
2256 if me_y < p_y then
2258 self.object:set_velocity({
2259 x = v.x,
2260 y = 1 * self.walk_velocity,
2261 z = v.z
2264 elseif me_y > p_y then
2266 self.object:set_velocity({
2267 x = v.x,
2268 y = -1 * self.walk_velocity,
2269 z = v.z
2272 else
2273 if me_y < p_y then
2275 self.object:set_velocity({
2276 x = v.x,
2277 y = 0.01,
2278 z = v.z
2281 elseif me_y > p_y then
2283 self.object:set_velocity({
2284 x = v.x,
2285 y = -0.01,
2286 z = v.z
2293 -- rnd: new movement direction
2294 if self.path.following
2295 and self.path.way
2296 and self.attack_type ~= "dogshoot" then
2298 -- no paths longer than 50
2299 if #self.path.way > 50
2300 or dist < self.reach then
2301 self.path.following = false
2302 return
2305 local p1 = self.path.way[1]
2307 if not p1 then
2308 self.path.following = false
2309 return
2312 if abs(p1.x-s.x) + abs(p1.z - s.z) < 0.6 then
2313 -- reached waypoint, remove it from queue
2314 table.remove(self.path.way, 1)
2317 -- set new temporary target
2318 p = {x = p1.x, y = p1.y, z = p1.z}
2321 local vec = {
2322 x = p.x - s.x,
2323 z = p.z - s.z
2326 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2328 if p.x > s.x then yaw = yaw + pi end
2330 yaw = set_yaw(self, yaw)
2332 -- move towards enemy if beyond mob reach
2333 if dist > self.reach then
2335 -- path finding by rnd
2336 if self.pathfinding -- only if mob has pathfinding enabled
2337 and enable_pathfinding then
2339 smart_mobs(self, s, p, dist, dtime)
2342 if is_at_cliff_or_danger(self) then
2344 set_velocity(self, 0)
2345 set_animation(self, "stand")
2346 else
2348 if self.path.stuck then
2349 set_velocity(self, self.walk_velocity)
2350 else
2351 set_velocity(self, self.run_velocity)
2354 if self.animation and self.animation.run_start then
2355 set_animation(self, "run")
2356 else
2357 set_animation(self, "walk")
2361 else -- rnd: if inside reach range
2363 self.path.stuck = false
2364 self.path.stuck_timer = 0
2365 self.path.following = false -- not stuck anymore
2367 set_velocity(self, 0)
2369 if not self.custom_attack then
2371 if self.timer > 1 then
2373 self.timer = 0
2375 if self.double_melee_attack
2376 and random(1, 2) == 1 then
2377 set_animation(self, "punch2")
2378 else
2379 set_animation(self, "punch")
2382 local p2 = p
2383 local s2 = s
2385 p2.y = p2.y + .5
2386 s2.y = s2.y + .5
2388 if line_of_sight(self, p2, s2) == true then
2390 -- play attack sound
2391 mob_sound(self, "attack")
2393 -- punch player (or what player is attached to)
2394 local attached = self.attack:get_attach()
2395 if attached then
2396 self.attack = attached
2398 self.attack:punch(self.object, 1.0, {
2399 full_punch_interval = 1.0,
2400 damage_groups = {fleshy = self.damage}
2401 }, nil)
2404 else -- call custom attack every second
2405 if self.custom_attack
2406 and self.timer > 1 then
2408 self.timer = 0
2410 self.custom_attack(self, p)
2415 elseif self.attack_type == "shoot"
2416 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 1)
2417 or (self.attack_type == "dogshoot" and dist > self.reach and dogswitch(self) == 0) then
2419 p.y = p.y - .5
2420 s.y = s.y + .5
2422 local dist = vector.distance(p, s)
2423 local vec = {
2424 x = p.x - s.x,
2425 y = p.y - s.y,
2426 z = p.z - s.z
2429 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2431 if p.x > s.x then yaw = yaw + pi end
2433 yaw = set_yaw(self, yaw)
2435 set_velocity(self, 0)
2437 if self.shoot_interval
2438 and self.timer > self.shoot_interval
2439 and random(1, 100) <= 60 then
2441 self.timer = 0
2442 set_animation(self, "shoot")
2444 -- play shoot attack sound
2445 mob_sound(self, "shoot_attack")
2447 local p = self.object:get_pos()
2449 p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
2451 -- Shoot arrow
2452 if minetest.registered_entities[self.arrow] then
2454 local arrow, ent
2455 local v = 1
2456 if not self.shoot_arrow then
2457 arrow = minetest.add_entity(p, self.arrow)
2458 ent = arrow:get_luaentity()
2459 if ent.velocity then
2460 v = ent.velocity
2462 ent.switch = 1
2463 ent.owner_id = tostring(self.object) -- add unique owner id to arrow
2466 local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5
2467 -- offset makes shoot aim accurate
2468 vec.y = vec.y + self.shoot_offset
2469 vec.x = vec.x * (v / amount)
2470 vec.y = vec.y * (v / amount)
2471 vec.z = vec.z * (v / amount)
2472 if self.shoot_arrow then
2473 vec = vector.normalize(vec)
2474 self:shoot_arrow(p, vec)
2475 else
2476 arrow:set_velocity(vec)
2485 -- falling and fall damage
2486 -- returns true if mob died
2487 local falling = function(self, pos)
2489 if self.fly then
2490 return
2493 -- floating in water (or falling)
2494 local v = self.object:get_velocity()
2496 if v.y > 0 then
2498 -- apply gravity when moving up
2499 self.object:set_acceleration({
2500 x = 0,
2501 y = -10,
2502 z = 0
2505 elseif v.y <= 0 and v.y > self.fall_speed then
2507 -- fall downwards at set speed
2508 self.object:set_acceleration({
2509 x = 0,
2510 y = self.fall_speed,
2511 z = 0
2513 else
2514 -- stop accelerating once max fall speed hit
2515 self.object:set_acceleration({x = 0, y = 0, z = 0})
2518 -- in water then float up
2519 if minetest.registered_nodes[node_ok(pos).name].groups.water then
2521 if self.floats == 1 then
2523 self.object:set_acceleration({
2524 x = 0,
2525 y = -self.fall_speed / (max(1, v.y) ^ 2),
2526 z = 0
2529 else
2531 -- fall damage onto solid ground
2532 if self.fall_damage == 1
2533 and self.object:get_velocity().y == 0 then
2535 local d = (self.old_y or 0) - self.object:get_pos().y
2537 if d > 5 then
2539 local add = minetest.get_item_group(self.standing_on, "fall_damage_add_percent")
2540 local damage = d - 5
2541 if add ~= 0 then
2542 damage = damage + damage * (add/100)
2544 damage = floor(damage)
2545 if damage > 0 then
2546 self.health = self.health - damage
2548 effect(pos, 5, "tnt_smoke.png", 1, 2, 2, nil)
2550 if check_for_death(self, "fall", {type = "fall"}) then
2551 return true
2556 self.old_y = self.object:get_pos().y
2562 -- deal damage and effects when mob punched
2563 local mob_punch = function(self, hitter, tflp, tool_capabilities, dir)
2565 -- custom punch function
2566 if self.do_punch then
2568 -- when false skip going any further
2569 if self.do_punch(self, hitter, tflp, tool_caps, dir) == false then
2570 return
2574 -- error checking when mod profiling is enabled
2575 if not tool_capabilities then
2576 minetest.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2577 return
2580 -- is mob protected?
2581 if self.protected and hitter:is_player()
2582 and minetest.is_protected(self.object:get_pos(), hitter:get_player_name()) then
2583 return
2587 -- punch interval
2588 local weapon = hitter:get_wielded_item()
2589 local punch_interval = 1.4
2591 -- exhaust attacker
2592 if mod_hunger and hitter:is_player() then
2593 mcl_hunger.exhaust(hitter:get_player_name(), mcl_hunger.EXHAUST_ATTACK)
2596 -- calculate mob damage
2597 local damage = 0
2598 local armor = self.object:get_armor_groups() or {}
2599 local tmp
2601 -- quick error check incase it ends up 0 (serialize.h check test)
2602 if tflp == 0 then
2603 tflp = 0.2
2606 if use_cmi then
2607 damage = cmi.calculate_damage(self.object, hitter, tflp, tool_capabilities, dir)
2608 else
2610 for group,_ in pairs( (tool_capabilities.damage_groups or {}) ) do
2612 tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)
2614 if tmp < 0 then
2615 tmp = 0.0
2616 elseif tmp > 1 then
2617 tmp = 1.0
2620 damage = damage + (tool_capabilities.damage_groups[group] or 0)
2621 * tmp * ((armor[group] or 0) / 100.0)
2625 -- check for tool immunity or special damage
2626 for n = 1, #self.immune_to do
2628 if self.immune_to[n][1] == weapon:get_name() then
2630 damage = self.immune_to[n][2] or 0
2631 break
2635 -- healing
2636 if damage <= -1 then
2637 self.health = self.health - floor(damage)
2638 return
2641 if use_cmi then
2643 local cancel = cmi.notify_punch(self.object, hitter, tflp, tool_capabilities, dir, damage)
2645 if cancel then return end
2648 if tool_capabilities then
2649 punch_interval = tool_capabilities.full_punch_interval or 1.4
2652 -- add weapon wear manually
2653 -- Required because we have custom health handling ("health" property)
2654 if minetest.settings:get_bool("creative_mode") ~= true
2655 and tool_capabilities then
2656 if tool_capabilities.punch_attack_uses then
2657 -- Without this delay, the wear does not work. Quite hacky ...
2658 minetest.after(0, function(name)
2659 local player = minetest.get_player_by_name(name)
2660 if not player then return end
2661 local weapon = hitter:get_wielded_item(player)
2662 local def = weapon:get_definition()
2663 if def.tool_capabilities and def.tool_capabilities.punch_attack_uses then
2664 local wear = floor(65535/tool_capabilities.punch_attack_uses)
2665 weapon:add_wear(wear)
2666 hitter:set_wielded_item(weapon)
2668 end, hitter:get_player_name())
2672 local die = false
2674 -- only play hit sound and show blood effects if damage is 1 or over
2675 if damage >= 1 then
2677 -- weapon sounds
2678 if weapon:get_definition().sounds ~= nil then
2680 local s = random(0, #weapon:get_definition().sounds)
2682 minetest.sound_play(weapon:get_definition().sounds[s], {
2683 object = self.object, --hitter,
2684 max_hear_distance = 8
2685 }, true)
2686 else
2687 minetest.sound_play("default_punch", {
2688 object = self.object, --hitter,
2689 max_hear_distance = 5
2690 }, true)
2693 damage_effect(self, damage)
2695 -- do damage
2696 self.health = self.health - floor(damage)
2698 -- skip future functions if dead, except alerting others
2699 if check_for_death(self, "hit", {type = "punch", puncher = hitter}) then
2700 die = true
2703 -- knock back effect (only on full punch)
2704 if not die
2705 and self.knock_back
2706 and tflp >= punch_interval then
2708 local v = self.object:get_velocity()
2709 local r = 1.4 - min(punch_interval, 1.4)
2710 local kb = r * 2.0
2711 local up = 2
2713 -- if already in air then dont go up anymore when hit
2714 if v.y > 0
2715 or self.fly then
2716 up = 0
2719 -- direction error check
2720 dir = dir or {x = 0, y = 0, z = 0}
2722 -- check if tool already has specific knockback value
2723 if tool_capabilities.damage_groups["knockback"] then
2724 kb = tool_capabilities.damage_groups["knockback"]
2725 else
2726 kb = kb * 1.5
2729 self.object:set_velocity({
2730 x = dir.x * kb,
2731 y = dir.y * kb + up,
2732 z = dir.z * kb
2735 self.pause_timer = 0.25
2737 end -- END if damage
2739 -- if skittish then run away
2740 if not die and self.runaway == true then
2742 local lp = hitter:get_pos()
2743 local s = self.object:get_pos()
2744 local vec = {
2745 x = lp.x - s.x,
2746 y = lp.y - s.y,
2747 z = lp.z - s.z
2750 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
2752 if lp.x > s.x then
2753 yaw = yaw + pi
2756 yaw = set_yaw(self, yaw, 6)
2757 self.state = "runaway"
2758 self.runaway_timer = 0
2759 self.following = nil
2762 local name = hitter:get_player_name() or ""
2764 -- attack puncher and call other mobs for help
2765 if self.passive == false
2766 and self.state ~= "flop"
2767 and (self.child == false or self.type == "monster")
2768 and hitter:get_player_name() ~= self.owner
2769 and not mobs.invis[ name ] then
2771 if not die then
2772 -- attack whoever punched mob
2773 self.state = ""
2774 do_attack(self, hitter)
2777 -- alert others to the attack
2778 local objs = minetest.get_objects_inside_radius(hitter:get_pos(), self.view_range)
2779 local obj = nil
2781 for n = 1, #objs do
2783 obj = objs[n]:get_luaentity()
2785 if obj then
2787 -- only alert members of same mob or friends
2788 if obj.group_attack
2789 and obj.state ~= "attack"
2790 and obj.owner ~= name then
2791 if obj.name == self.name then
2792 do_attack(obj, hitter)
2793 elseif type(obj.group_attack) == "table" then
2794 for i=1, #obj.group_attack do
2795 if obj.name == obj.group_attack[i] then
2796 do_attack(obj, hitter)
2797 break
2803 -- have owned mobs attack player threat
2804 if obj.owner == name and obj.owner_loyal then
2805 do_attack(obj, self.object)
2812 local mob_detach_child = function(self, child)
2814 if self.driver == child then
2815 self.driver = nil
2820 -- get entity staticdata
2821 local mob_staticdata = function(self)
2823 -- remove mob when out of range unless tamed
2824 if remove_far
2825 and self.can_despawn
2826 and self.remove_ok
2827 and ((not self.nametag) or (self.nametag == ""))
2828 and self.lifetimer <= 20 then
2830 minetest.log("action", "Mob "..name.." despawns in mob_staticdata at "..minetest.pos_to_string(self.object.get_pos()))
2831 self.object:remove()
2833 return ""-- nil
2836 self.remove_ok = true
2837 self.attack = nil
2838 self.following = nil
2839 self.state = "stand"
2841 if use_cmi then
2842 self.serialized_cmi_components = cmi.serialize_components(self._cmi_components)
2845 local tmp = {}
2847 for _,stat in pairs(self) do
2849 local t = type(stat)
2851 if t ~= "function"
2852 and t ~= "nil"
2853 and t ~= "userdata"
2854 and _ ~= "_cmi_components" then
2855 tmp[_] = self[_]
2859 return minetest.serialize(tmp)
2863 -- activate mob and reload settings
2864 local mob_activate = function(self, staticdata, def, dtime)
2866 -- remove monsters in peaceful mode
2867 if self.type == "monster"
2868 and minetest.settings:get_bool("only_peaceful_mobs", false) then
2870 self.object:remove()
2872 return
2875 -- load entity variables
2876 local tmp = minetest.deserialize(staticdata)
2878 if tmp then
2879 for _,stat in pairs(tmp) do
2880 self[_] = stat
2884 -- select random texture, set model and size
2885 if not self.base_texture then
2887 -- compatiblity with old simple mobs textures
2888 if type(def.textures[1]) == "string" then
2889 def.textures = {def.textures}
2892 self.base_texture = def.textures[random(1, #def.textures)]
2893 self.base_mesh = def.mesh
2894 self.base_size = self.visual_size
2895 self.base_colbox = self.collisionbox
2896 self.base_selbox = self.selectionbox
2899 -- for current mobs that dont have this set
2900 if not self.base_selbox then
2901 self.base_selbox = self.selectionbox or self.base_colbox
2904 -- set texture, model and size
2905 local textures = self.base_texture
2906 local mesh = self.base_mesh
2907 local vis_size = self.base_size
2908 local colbox = self.base_colbox
2909 local selbox = self.base_selbox
2911 -- specific texture if gotten
2912 if self.gotten == true
2913 and def.gotten_texture then
2914 textures = def.gotten_texture
2917 -- specific mesh if gotten
2918 if self.gotten == true
2919 and def.gotten_mesh then
2920 mesh = def.gotten_mesh
2923 -- set child objects to half size
2924 if self.child == true then
2926 vis_size = {
2927 x = self.base_size.x * .5,
2928 y = self.base_size.y * .5,
2931 if def.child_texture then
2932 textures = def.child_texture[1]
2935 colbox = {
2936 self.base_colbox[1] * .5,
2937 self.base_colbox[2] * .5,
2938 self.base_colbox[3] * .5,
2939 self.base_colbox[4] * .5,
2940 self.base_colbox[5] * .5,
2941 self.base_colbox[6] * .5
2943 selbox = {
2944 self.base_selbox[1] * .5,
2945 self.base_selbox[2] * .5,
2946 self.base_selbox[3] * .5,
2947 self.base_selbox[4] * .5,
2948 self.base_selbox[5] * .5,
2949 self.base_selbox[6] * .5
2953 if self.health == 0 then
2954 self.health = random (self.hp_min, self.hp_max)
2956 if self.breath == nil then
2957 self.breath = self.breath_max
2960 -- pathfinding init
2961 self.path = {}
2962 self.path.way = {} -- path to follow, table of positions
2963 self.path.lastpos = {x = 0, y = 0, z = 0}
2964 self.path.stuck = false
2965 self.path.following = false -- currently following path?
2966 self.path.stuck_timer = 0 -- if stuck for too long search for path
2968 -- Armor groups
2969 -- immortal=1 because we use custom health
2970 -- handling (using "health" property)
2971 local armor
2972 if type(self.armor) == "table" then
2973 armor = table.copy(self.armor)
2974 armor.immortal = 1
2975 else
2976 armor = {immortal=1, fleshy = self.armor}
2978 self.object:set_armor_groups(armor)
2979 self.old_y = self.object:get_pos().y
2980 self.old_health = self.health
2981 self.sounds.distance = self.sounds.distance or 10
2982 self.textures = textures
2983 self.mesh = mesh
2984 self.collisionbox = colbox
2985 self.selectionbox = selbox
2986 self.visual_size = vis_size
2987 self.standing_in = "ignore"
2988 self.standing_on = "ignore"
2989 self.jump_sound_cooloff = 0 -- used to prevent jump sound from being played too often in short time
2990 self.opinion_sound_cooloff = 0 -- used to prevent sound spam of particular sound types
2992 self.texture_mods = {}
2993 self.object:set_texture_mod("")
2995 self.v_start = false
2996 self.timer = 0
2997 self.blinktimer = 0
2998 self.blinkstatus = false
3000 -- check existing nametag
3001 if not self.nametag then
3002 self.nametag = def.nametag
3005 -- set anything changed above
3006 self.object:set_properties(self)
3007 set_yaw(self, (random(0, 360) - 180) / 180 * pi, 6)
3008 update_tag(self)
3009 set_animation(self, "stand")
3011 -- run on_spawn function if found
3012 if self.on_spawn and not self.on_spawn_run then
3013 if self.on_spawn(self) then
3014 self.on_spawn_run = true -- if true, set flag to run once only
3018 -- run after_activate
3019 if def.after_activate then
3020 def.after_activate(self, staticdata, def, dtime)
3023 if use_cmi then
3024 self._cmi_components = cmi.activate_components(self.serialized_cmi_components)
3025 cmi.notify_activate(self.object, dtime)
3030 -- main mob function
3031 local mob_step = function(self, dtime)
3033 if use_cmi then
3034 cmi.notify_step(self.object, dtime)
3037 local pos = self.object:get_pos()
3038 local yaw = 0
3040 -- Despawning: when lifetimer expires, remove mob
3041 if remove_far
3042 and self.can_despawn == true
3043 and ((not self.nametag) or (self.nametag == "")) then
3045 -- TODO: Finish up implementation of despawning rules
3047 self.lifetimer = self.lifetimer - dtime
3049 if self.lifetimer <= 0 then
3051 -- only despawn away from player
3052 local objs = minetest.get_objects_inside_radius(pos, 32)
3054 for n = 1, #objs do
3056 if objs[n]:is_player() then
3058 self.lifetimer = 20
3060 return
3064 minetest.log("action", "Mob "..name.." despawns in mob_step at "..minetest.pos_to_string(pos))
3065 self.object:remove()
3067 return
3071 if self.jump_sound_cooloff > 0 then
3072 self.jump_sound_cooloff = self.jump_sound_cooloff - dtime
3074 if self.opinion_sound_cooloff > 0 then
3075 self.opinion_sound_cooloff = self.opinion_sound_cooloff - dtime
3077 if falling(self, pos) then
3078 -- Return if mob died after falling
3079 return
3082 -- smooth rotation by ThomasMonroe314
3084 if self.delay and self.delay > 0 then
3086 local yaw = self.object:get_yaw() or 0
3088 if self.delay == 1 then
3089 yaw = self.target_yaw
3090 else
3091 local dif = abs(yaw - self.target_yaw)
3093 if yaw > self.target_yaw then
3095 if dif > pi then
3096 dif = 2 * pi - dif -- need to add
3097 yaw = yaw + dif / self.delay
3098 else
3099 yaw = yaw - dif / self.delay -- need to subtract
3102 elseif yaw < self.target_yaw then
3104 if dif > pi then
3105 dif = 2 * pi - dif
3106 yaw = yaw - dif / self.delay -- need to subtract
3107 else
3108 yaw = yaw + dif / self.delay -- need to add
3112 if yaw > (pi * 2) then yaw = yaw - (pi * 2) end
3113 if yaw < 0 then yaw = yaw + (pi * 2) end
3116 self.delay = self.delay - 1
3117 self.object:set_yaw(yaw)
3120 -- end rotation
3122 -- knockback timer
3123 if self.pause_timer > 0 then
3125 self.pause_timer = self.pause_timer - dtime
3127 return
3130 -- run custom function (defined in mob lua file)
3131 if self.do_custom then
3133 -- when false skip going any further
3134 if self.do_custom(self, dtime) == false then
3135 return
3139 -- attack timer
3140 self.timer = self.timer + dtime
3142 if self.state ~= "attack" then
3144 if self.timer < 1 then
3145 return
3148 self.timer = 0
3151 -- never go over 100
3152 if self.timer > 100 then
3153 self.timer = 1
3156 -- mob plays random sound at times
3157 if random(1, 100) == 1 then
3158 mob_sound(self, "random", true)
3161 -- environmental damage timer (every 1 second)
3162 self.env_damage_timer = self.env_damage_timer + dtime
3164 if (self.state == "attack" and self.env_damage_timer > 1)
3165 or self.state ~= "attack" then
3167 self.env_damage_timer = 0
3169 -- check for environmental damage (water, fire, lava etc.)
3170 if do_env_damage(self) then
3171 return
3174 -- node replace check (cow eats grass etc.)
3175 replace(self, pos)
3178 monster_attack(self)
3180 npc_attack(self)
3182 breed(self)
3184 follow_flop(self)
3186 if do_states(self, dtime) then
3187 return
3190 do_jump(self)
3192 runaway_from(self)
3197 -- default function when mobs are blown up with TNT
3198 local do_tnt = function(obj, damage)
3200 obj.object:punch(obj.object, 1.0, {
3201 full_punch_interval = 1.0,
3202 damage_groups = {fleshy = damage},
3203 }, nil)
3205 return false, true, {}
3209 mobs.spawning_mobs = {}
3211 -- Code to execute before custom on_rightclick handling
3212 local on_rightclick_prefix = function(self, clicker)
3213 local item = clicker:get_wielded_item()
3215 -- Name mob with nametag
3216 if not self.ignores_nametag and item:get_name() == "mcl_mobs:nametag" then
3218 local tag = item:get_meta():get_string("name")
3219 if tag ~= "" then
3220 if string.len(tag) > MAX_MOB_NAME_LENGTH then
3221 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH)
3223 self.nametag = tag
3225 update_tag(self)
3227 if not mobs.is_creative(clicker:get_player_name()) then
3228 item:take_item()
3229 clicker:set_wielded_item(item)
3231 return true
3235 return false
3238 local create_mob_on_rightclick = function(on_rightclick)
3239 return function(self, clicker)
3240 local stop = on_rightclick_prefix(self, clicker)
3241 if (not stop) and (on_rightclick) then
3242 on_rightclick(self, clicker)
3247 -- register mob entity
3248 function mobs:register_mob(name, def)
3250 mobs.spawning_mobs[name] = true
3252 local can_despawn
3253 if def.can_despawn ~= nil then
3254 can_despawn = def.can_despawn
3255 else
3256 can_despawn = true
3259 local function scale_difficulty(value, default, min, special)
3260 if (not value) or (value == default) or (value == special) then
3261 return default
3262 else
3263 return max(min, value * difficulty)
3267 local collisionbox = def.collisionbox or {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25}
3268 -- Workaround for <https://github.com/minetest/minetest/issues/5966>:
3269 -- Increase upper Y limit to avoid mobs glitching through solid nodes.
3270 -- FIXME: Remove workaround if it's no longer needed.
3271 if collisionbox[5] < 0.79 then
3272 collisionbox[5] = 0.79
3275 minetest.register_entity(name, {
3277 stepheight = def.stepheight or 0.6,
3278 name = name,
3279 type = def.type,
3280 attack_type = def.attack_type,
3281 fly = def.fly,
3282 fly_in = def.fly_in or {"air", "__airlike"},
3283 owner = def.owner or "",
3284 order = def.order or "",
3285 on_die = def.on_die,
3286 spawn_small_alternative = def.spawn_small_alternative,
3287 do_custom = def.do_custom,
3288 jump_height = def.jump_height or 4, -- was 6
3289 rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
3290 lifetimer = def.lifetimer or 57.73,
3291 hp_min = scale_difficulty(def.hp_min, 5, 1),
3292 hp_max = scale_difficulty(def.hp_max, 10, 1),
3293 breath_max = def.breath_max or 15,
3294 breathes_in_water = def.breathes_in_water or false,
3295 physical = true,
3296 collisionbox = collisionbox,
3297 selectionbox = def.selectionbox or def.collisionbox,
3298 visual = def.visual,
3299 visual_size = def.visual_size or {x = 1, y = 1},
3300 mesh = def.mesh,
3301 makes_footstep_sound = def.makes_footstep_sound or false,
3302 view_range = def.view_range or 16,
3303 walk_velocity = def.walk_velocity or 1,
3304 run_velocity = def.run_velocity or 2,
3305 damage = scale_difficulty(def.damage, 0, 0),
3306 light_damage = def.light_damage or 0,
3307 sunlight_damage = def.sunlight_damage or 0,
3308 water_damage = def.water_damage or 0,
3309 lava_damage = def.lava_damage or 8,
3310 fire_damage = def.fire_damage or 1,
3311 suffocation = def.suffocation or true,
3312 fall_damage = def.fall_damage or 1,
3313 fall_speed = def.fall_speed or -10, -- must be lower than -2 (default: -10)
3314 drops = def.drops or {},
3315 armor = def.armor or 100,
3316 on_rightclick = create_mob_on_rightclick(def.on_rightclick),
3317 arrow = def.arrow,
3318 shoot_interval = def.shoot_interval,
3319 sounds = def.sounds or {},
3320 animation = def.animation,
3321 follow = def.follow,
3322 jump = def.jump ~= false,
3323 walk_chance = def.walk_chance or 50,
3324 attacks_monsters = def.attacks_monsters or false,
3325 group_attack = def.group_attack or false,
3326 passive = def.passive or false,
3327 knock_back = def.knock_back ~= false,
3328 shoot_offset = def.shoot_offset or 0,
3329 floats = def.floats or 1, -- floats in water by default
3330 replace_rate = def.replace_rate,
3331 replace_what = def.replace_what,
3332 replace_with = def.replace_with,
3333 replace_offset = def.replace_offset or 0,
3334 on_replace = def.on_replace,
3335 timer = 0,
3336 env_damage_timer = 0, -- only used when state = "attack"
3337 tamed = false,
3338 pause_timer = 0,
3339 horny = false,
3340 hornytimer = 0,
3341 gotten = false,
3342 health = 0,
3343 reach = def.reach or 3,
3344 htimer = 0,
3345 texture_list = def.textures,
3346 child_texture = def.child_texture,
3347 docile_by_day = def.docile_by_day or false,
3348 time_of_day = 0.5,
3349 fear_height = def.fear_height or 0,
3350 runaway = def.runaway,
3351 runaway_timer = 0,
3352 pathfinding = def.pathfinding,
3353 immune_to = def.immune_to or {},
3354 explosion_radius = def.explosion_radius, -- LEGACY
3355 explosion_damage_radius = def.explosion_damage_radius, -- LEGACY
3356 explosion_timer = def.explosion_timer or 3,
3357 allow_fuse_reset = def.allow_fuse_reset ~= false,
3358 stop_to_explode = def.stop_to_explode ~= false,
3359 custom_attack = def.custom_attack,
3360 double_melee_attack = def.double_melee_attack,
3361 dogshoot_switch = def.dogshoot_switch,
3362 dogshoot_count = 0,
3363 dogshoot_count_max = def.dogshoot_count_max or 5,
3364 dogshoot_count2_max = def.dogshoot_count2_max or (def.dogshoot_count_max or 5),
3365 attack_animals = def.attack_animals or false,
3366 specific_attack = def.specific_attack,
3367 runaway_from = def.runaway_from,
3368 owner_loyal = def.owner_loyal,
3369 facing_fence = false,
3370 _cmi_is_mob = true,
3372 -- MCL2 extensions
3373 spawn_class = def.spawn_class,
3374 ignores_nametag = def.ignores_nametag or false,
3375 rain_damage = def.rain_damage or 0,
3376 glow = def.glow,
3377 can_despawn = can_despawn,
3378 child = def.child or false,
3379 texture_mods = {},
3380 shoot_arrow = def.shoot_arrow,
3381 sounds_child = def.sounds_child,
3382 explosion_strength = def.explosion_strength,
3383 -- End of MCL2 extensions
3385 on_spawn = def.on_spawn,
3387 on_blast = def.on_blast or do_tnt,
3389 on_step = mob_step,
3391 do_punch = def.do_punch,
3393 on_punch = mob_punch,
3395 on_breed = def.on_breed,
3397 on_grown = def.on_grown,
3399 on_detach_child = mob_detach_child,
3401 on_activate = function(self, staticdata, dtime)
3402 return mob_activate(self, staticdata, def, dtime)
3403 end,
3405 get_staticdata = function(self)
3406 return mob_staticdata(self)
3407 end,
3411 if minetest.get_modpath("doc_identifier") ~= nil then
3412 doc.sub.identifier.register_object(name, "basics", "mobs")
3415 end -- END mobs:register_mob function
3418 -- count how many mobs of one type are inside an area
3419 local count_mobs = function(pos, mobtype)
3421 local num = 0
3422 local objs = minetest.get_objects_inside_radius(pos, aoc_range)
3424 for n = 1, #objs do
3426 local obj = objs[n]:get_luaentity()
3428 if obj and obj.name and obj._cmi_is_mob then
3430 -- count passive mobs only
3431 if mobtype == "!passive" then
3432 if obj.spawn_class == "passive" then
3433 num = num + 1
3435 -- count hostile mobs only
3436 elseif mobtype == "!hostile" then
3437 if obj.spawn_class == "hostile" then
3438 num = num + 1
3440 -- count ambient mobs only
3441 elseif mobtype == "!ambient" then
3442 if obj.spawn_class == "ambient" then
3443 num = num + 1
3445 -- count water mobs only
3446 elseif mobtype == "!water" then
3447 if obj.spawn_class == "water" then
3448 num = num + 1
3450 -- count mob type
3451 elseif mobtype and obj.name == mobtype then
3452 num = num + 1
3453 -- count total mobs
3454 elseif not mobtype then
3455 num = num + 1
3460 return num
3464 -- global functions
3466 function mobs:spawn_abm_check(pos, node, name)
3467 -- global function to add additional spawn checks
3468 -- return true to stop spawning mob
3472 function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
3473 interval, chance, aoc, min_height, max_height, day_toggle, on_spawn)
3475 -- Do mobs spawn at all?
3476 if not mobs_spawn then
3477 return
3480 -- chance/spawn number override in minetest.conf for registered mob
3481 local numbers = minetest.settings:get(name)
3483 if numbers then
3484 numbers = numbers:split(",")
3485 chance = tonumber(numbers[1]) or chance
3486 aoc = tonumber(numbers[2]) or aoc
3488 if chance == 0 then
3489 minetest.log("warning", string.format("[mobs] %s has spawning disabled", name))
3490 return
3493 minetest.log("action",
3494 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name, chance, aoc))
3498 local spawn_action
3499 spawn_action = function(pos, node, active_object_count, active_object_count_wider, name)
3501 local orig_pos = table.copy(pos)
3502 -- is mob actually registered?
3503 if not mobs.spawning_mobs[name]
3504 or not minetest.registered_entities[name] then
3505 minetest.log("warning", "Mob spawn of "..name.." failed, unknown entity or mob is not registered for spawning!")
3506 return
3509 -- additional custom checks for spawning mob
3510 if mobs:spawn_abm_check(pos, node, name) == true then
3511 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, ABM check rejected!")
3512 return
3515 -- count nearby mobs in same spawn class
3516 local entdef = minetest.registered_entities[name]
3517 local spawn_class = entdef and entdef.spawn_class
3518 if not spawn_class then
3519 if entdef.type == "monster" then
3520 spawn_class = "hostile"
3521 else
3522 spawn_class = "passive"
3525 local in_class_cap = count_mobs(pos, "!"..spawn_class) < MOB_CAP[spawn_class]
3526 -- do not spawn if too many of same mob in area
3527 if active_object_count_wider >= max_per_block -- large-range mob cap
3528 or (not in_class_cap) -- spawn class mob cap
3529 or count_mobs(pos, name) >= aoc then -- per-mob mob cap
3530 -- too many entities
3531 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, too crowded!")
3532 return
3535 -- if toggle set to nil then ignore day/night check
3536 if day_toggle ~= nil then
3538 local tod = (minetest.get_timeofday() or 0) * 24000
3540 if tod > 4500 and tod < 19500 then
3541 -- daylight, but mob wants night
3542 if day_toggle == false then
3543 -- mob needs night
3544 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, mob needs light!")
3545 return
3547 else
3548 -- night time but mob wants day
3549 if day_toggle == true then
3550 -- mob needs day
3551 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, mob needs daylight!")
3552 return
3557 -- spawn above node
3558 pos.y = pos.y + 1
3560 -- only spawn away from player
3561 local objs = minetest.get_objects_inside_radius(pos, 10)
3563 for n = 1, #objs do
3565 if objs[n]:is_player() then
3566 -- player too close
3567 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, player too close!")
3568 return
3572 -- mobs cannot spawn in protected areas when enabled
3573 if not spawn_protected
3574 and minetest.is_protected(pos, "") then
3575 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, position is protected!")
3576 return
3579 -- are we spawning within height limits?
3580 if pos.y > max_height
3581 or pos.y < min_height then
3582 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, out of height limit!")
3583 return
3586 -- are light levels ok?
3587 local light = minetest.get_node_light(pos)
3588 if not light
3589 or light > max_light
3590 or light < min_light then
3591 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, bad light!")
3592 return
3595 -- do we have enough space to spawn mob?
3596 local ent = minetest.registered_entities[name]
3597 local width_x = max(1, math.ceil(ent.collisionbox[4] - ent.collisionbox[1]))
3598 local min_x, max_x
3599 if width_x % 2 == 0 then
3600 max_x = math.floor(width_x/2)
3601 min_x = -(max_x-1)
3602 else
3603 max_x = math.floor(width_x/2)
3604 min_x = -max_x
3607 local width_z = max(1, math.ceil(ent.collisionbox[6] - ent.collisionbox[3]))
3608 local min_z, max_z
3609 if width_z % 2 == 0 then
3610 max_z = math.floor(width_z/2)
3611 min_z = -(max_z-1)
3612 else
3613 max_z = math.floor(width_z/2)
3614 min_z = -max_z
3617 local max_y = max(0, math.ceil(ent.collisionbox[5] - ent.collisionbox[2]) - 1)
3619 for y = 0, max_y do
3620 for x = min_x, max_x do
3621 for z = min_z, max_z do
3622 local pos2 = {x = pos.x+x, y = pos.y+y, z = pos.z+z}
3623 if minetest.registered_nodes[node_ok(pos2).name].walkable == true then
3624 -- inside block
3625 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, too little space!")
3626 if ent.spawn_small_alternative ~= nil and (not minetest.registered_nodes[node_ok(pos).name].walkable) then
3627 minetest.log("info", "Trying to spawn smaller alternative mob: "..ent.spawn_small_alternative)
3628 spawn_action(orig_pos, node, active_object_count, active_object_count_wider, ent.spawn_small_alternative)
3630 return
3636 -- spawn mob 1/2 node above ground
3637 pos.y = pos.y + 0.5
3638 -- tweak X/Z spawn pos
3639 if width_x % 2 == 0 then
3640 pos.x = pos.x + 0.5
3642 if width_z % 2 == 0 then
3643 pos.z = pos.z + 0.5
3646 local mob = minetest.add_entity(pos, name)
3647 minetest.log("action", "Mob spawned: "..name.." at "..minetest.pos_to_string(pos))
3649 if on_spawn then
3651 local ent = mob:get_luaentity()
3653 on_spawn(ent, pos)
3657 local function spawn_abm_action(pos, node, active_object_count, active_object_count_wider)
3658 spawn_action(pos, node, active_object_count, active_object_count_wider, name)
3661 minetest.register_abm({
3662 label = name .. " spawning",
3663 nodenames = nodes,
3664 neighbors = neighbors,
3665 interval = interval,
3666 chance = floor(max(1, chance * mobs_spawn_chance)),
3667 catch_up = false,
3668 action = spawn_abm_action,
3673 -- compatibility with older mob registration
3674 function mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
3676 mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
3677 chance, active_object_count, -31000, max_height, day_toggle)
3681 -- MarkBu's spawn function
3682 function mobs:spawn(def)
3684 local name = def.name
3685 local nodes = def.nodes or {"group:soil", "group:stone"}
3686 local neighbors = def.neighbors or {"air"}
3687 local min_light = def.min_light or 0
3688 local max_light = def.max_light or 15
3689 local interval = def.interval or 30
3690 local chance = def.chance or 5000
3691 local active_object_count = def.active_object_count or 1
3692 local min_height = def.min_height or -31000
3693 local max_height = def.max_height or 31000
3694 local day_toggle = def.day_toggle
3695 local on_spawn = def.on_spawn
3697 mobs:spawn_specific(name, nodes, neighbors, min_light, max_light, interval,
3698 chance, active_object_count, min_height, max_height, day_toggle, on_spawn)
3702 -- register arrow for shoot attack
3703 function mobs:register_arrow(name, def)
3705 if not name or not def then return end -- errorcheck
3707 minetest.register_entity(name, {
3709 physical = false,
3710 visual = def.visual,
3711 visual_size = def.visual_size,
3712 textures = def.textures,
3713 velocity = def.velocity,
3714 hit_player = def.hit_player,
3715 hit_node = def.hit_node,
3716 hit_mob = def.hit_mob,
3717 hit_object = def.hit_object,
3718 drop = def.drop or false, -- drops arrow as registered item when true
3719 collisionbox = {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3720 timer = 0,
3721 switch = 0,
3722 owner_id = def.owner_id,
3723 rotate = def.rotate,
3724 automatic_face_movement_dir = def.rotate
3725 and (def.rotate - (pi / 180)) or false,
3727 on_activate = def.on_activate,
3729 on_step = def.on_step or function(self, dtime)
3731 self.timer = self.timer + 1
3733 local pos = self.object:get_pos()
3735 if self.switch == 0
3736 or self.timer > 150
3737 or not within_limits(pos, 0) then
3739 self.object:remove();
3741 return
3744 -- does arrow have a tail (fireball)
3745 if def.tail
3746 and def.tail == 1
3747 and def.tail_texture then
3749 minetest.add_particle({
3750 pos = pos,
3751 velocity = {x = 0, y = 0, z = 0},
3752 acceleration = {x = 0, y = 0, z = 0},
3753 expirationtime = def.expire or 0.25,
3754 collisiondetection = false,
3755 texture = def.tail_texture,
3756 size = def.tail_size or 5,
3757 glow = def.glow or 0,
3761 if self.hit_node then
3763 local node = node_ok(pos).name
3765 if minetest.registered_nodes[node].walkable then
3767 self.hit_node(self, pos, node)
3769 if self.drop == true then
3771 pos.y = pos.y + 1
3773 self.lastpos = (self.lastpos or pos)
3775 minetest.add_item(self.lastpos, self.object:get_luaentity().name)
3778 self.object:remove();
3780 return
3784 if self.hit_player or self.hit_mob or self.hit_object then
3786 for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do
3788 if self.hit_player
3789 and player:is_player() then
3791 self.hit_player(self, player)
3792 self.object:remove();
3793 return
3796 local entity = player:get_luaentity()
3798 if entity
3799 and self.hit_mob
3800 and entity._cmi_is_mob == true
3801 and tostring(player) ~= self.owner_id
3802 and entity.name ~= self.object:get_luaentity().name then
3803 self.hit_mob(self, player)
3804 self.object:remove();
3805 return
3808 if entity
3809 and self.hit_object
3810 and (not entity._cmi_is_mob)
3811 and tostring(player) ~= self.owner_id
3812 and entity.name ~= self.object:get_luaentity().name then
3813 self.hit_object(self, player)
3814 self.object:remove();
3815 return
3820 self.lastpos = pos
3826 -- no damage to nodes explosion
3827 function mobs:safe_boom(self, pos, radius)
3828 minetest.sound_play(self.sounds and self.sounds.explode or "tnt_explode", {
3829 pos = pos,
3830 gain = 1.0,
3831 max_hear_distance = self.sounds and self.sounds.distance or 32
3832 }, true)
3834 entity_physics(pos, radius)
3835 effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
3839 -- make explosion with protection and tnt mod check
3840 function mobs:boom(self, pos, radius)
3842 if mod_explosions then
3843 if mobs_griefing and not minetest.is_protected(pos, "") then
3844 mcl_explosions.explode(pos, self.explosion_strength, { drop_chance = 1.0 }, self.object)
3845 else
3846 mobs:safe_boom(self, pos, radius)
3848 else
3849 mobs:safe_boom(self, pos, radius)
3854 -- Register spawn eggs
3856 -- Note: This also introduces the “spawn_egg” group:
3857 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3858 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3859 function mobs:register_egg(mob, desc, background, addegg, no_creative)
3861 local grp = {spawn_egg = 1}
3863 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3864 if creative and no_creative == true then
3865 grp.not_in_creative_inventory = 1
3868 local invimg = background
3870 if addegg == 1 then
3871 invimg = "mobs_chicken_egg.png^(" .. invimg ..
3872 "^[mask:mobs_chicken_egg_overlay.png)"
3875 -- register old stackable mob egg
3876 minetest.register_craftitem(mob, {
3878 description = desc,
3879 inventory_image = invimg,
3880 groups = grp,
3882 _doc_items_longdesc = S("This allows you to place a single mob."),
3883 _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."),
3885 on_place = function(itemstack, placer, pointed_thing)
3887 local pos = pointed_thing.above
3889 -- am I clicking on something with existing on_rightclick function?
3890 local under = minetest.get_node(pointed_thing.under)
3891 local def = minetest.registered_nodes[under.name]
3892 if def and def.on_rightclick then
3893 return def.on_rightclick(pointed_thing.under, under, placer, itemstack)
3896 if pos
3897 and within_limits(pos, 0)
3898 and not minetest.is_protected(pos, placer:get_player_name()) then
3900 local name = placer:get_player_name()
3901 local privs = minetest.get_player_privs(name)
3902 if mod_mobspawners and under.name == "mcl_mobspawners:spawner" then
3903 if minetest.is_protected(pointed_thing.under, name) then
3904 minetest.record_protection_violation(pointed_thing.under, name)
3905 return itemstack
3907 if not privs.maphack then
3908 minetest.chat_send_player(name, S("You need the “maphack” privilege to change the mob spawner."))
3909 return itemstack
3911 mcl_mobspawners.setup_spawner(pointed_thing.under, itemstack:get_name())
3912 if not minetest.settings:get_bool("creative_mode") then
3913 itemstack:take_item()
3915 return itemstack
3918 if not minetest.registered_entities[mob] then
3919 return itemstack
3922 if minetest.settings:get_bool("only_peaceful_mobs", false)
3923 and minetest.registered_entities[mob].type == "monster" then
3924 minetest.chat_send_player(name, S("Only peaceful mobs allowed!"))
3925 return itemstack
3928 pos.y = pos.y + 1
3930 local mob = minetest.add_entity(pos, mob)
3931 local ent = mob:get_luaentity()
3933 -- don't set owner if monster or sneak pressed
3934 if ent.type ~= "monster"
3935 and not placer:get_player_control().sneak then
3936 ent.owner = placer:get_player_name()
3937 ent.tamed = true
3940 -- set nametag
3941 local nametag = itemstack:get_meta():get_string("name")
3942 if nametag ~= "" then
3943 if string.len(nametag) > MAX_MOB_NAME_LENGTH then
3944 nametag = string.sub(nametag, 1, MAX_MOB_NAME_LENGTH)
3946 ent.nametag = nametag
3947 update_tag(ent)
3950 -- if not in creative then take item
3951 if not mobs.is_creative(placer:get_player_name()) then
3952 itemstack:take_item()
3956 return itemstack
3957 end,
3963 -- No-op in MCL2 (capturing mobs is not possible).
3964 -- Provided for compability with Mobs Redo
3965 function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
3966 return false
3970 -- No-op in MCL2 (protecting mobs is not possible).
3971 function mobs:protect(self, clicker)
3972 return false
3976 -- feeding, taming and breeding (thanks blert2112)
3977 function mobs:feed_tame(self, clicker, feed_count, breed, tame)
3978 if not self.follow then
3979 return false
3982 -- can eat/tame with item in hand
3983 if follow_holding(self, clicker) then
3985 -- if not in creative then take item
3986 if not mobs.is_creative(clicker:get_player_name()) then
3988 local item = clicker:get_wielded_item()
3990 item:take_item()
3992 clicker:set_wielded_item(item)
3995 -- increase health
3996 self.health = self.health + 4
3998 if self.health >= self.hp_max then
4000 self.health = self.hp_max
4002 if self.htimer < 1 then
4003 self.htimer = 5
4007 self.object:set_hp(self.health)
4009 update_tag(self)
4011 -- make children grow quicker
4012 if self.child == true then
4014 self.hornytimer = self.hornytimer + 20
4016 return true
4019 -- feed and tame
4020 self.food = (self.food or 0) + 1
4021 if self.food >= feed_count then
4023 self.food = 0
4025 if breed and self.hornytimer == 0 then
4026 self.horny = true
4029 if tame then
4031 self.tamed = true
4033 if not self.owner or self.owner == "" then
4034 self.owner = clicker:get_player_name()
4038 -- make sound when fed so many times
4039 mob_sound(self, "random", true)
4042 return true
4045 return false
4048 -- Spawn a child
4049 function mobs:spawn_child(pos, mob_type)
4050 local child = minetest.add_entity(pos, mob_type)
4051 if not child then
4052 return
4055 local ent = child:get_luaentity()
4056 effect(pos, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
4058 ent.child = true
4060 local textures
4061 -- using specific child texture (if found)
4062 if ent.child_texture then
4063 textures = ent.child_texture[1]
4066 -- and resize to half height
4067 child:set_properties({
4068 textures = textures,
4069 visual_size = {
4070 x = ent.base_size.x * .5,
4071 y = ent.base_size.y * .5,
4073 collisionbox = {
4074 ent.base_colbox[1] * .5,
4075 ent.base_colbox[2] * .5,
4076 ent.base_colbox[3] * .5,
4077 ent.base_colbox[4] * .5,
4078 ent.base_colbox[5] * .5,
4079 ent.base_colbox[6] * .5,
4081 selectionbox = {
4082 ent.base_selbox[1] * .5,
4083 ent.base_selbox[2] * .5,
4084 ent.base_selbox[3] * .5,
4085 ent.base_selbox[4] * .5,
4086 ent.base_selbox[5] * .5,
4087 ent.base_selbox[6] * .5,
4091 return child
4095 -- compatibility function for old entities to new modpack entities
4096 function mobs:alias_mob(old_name, new_name)
4098 -- spawn egg
4099 minetest.register_alias(old_name, new_name)
4101 -- entity
4102 minetest.register_entity(":" .. old_name, {
4104 physical = false,
4106 on_step = function(self)
4108 if minetest.registered_entities[new_name] then
4109 minetest.add_entity(self.object:get_pos(), new_name)
4112 self.object:remove()