Merge branch 'mcl_explosions'
[MineClone/MineClone2.git] / mods / ENTITIES / mcl_mobs / api.lua
blob6cfd4fc7183e340cacbef48051735dcea35d931f
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_tnt = minetest.get_modpath("mcl_tnt") ~= 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 -- dont damage anything if area protected or next to water
2222 if minetest.find_node_near(pos, 1, {"group:water"})
2223 or minetest.is_protected(pos, "") then
2225 node_break_radius = 1
2228 self.object:remove()
2230 if mobs_griefing and mod_tnt and tnt and tnt.boom
2231 and not minetest.is_protected(pos, "") then
2233 tnt.boom(pos, {
2234 radius = node_break_radius,
2235 damage_radius = entity_damage_radius,
2236 sound = self.sounds.explode,
2237 is_tnt = false,
2239 else
2241 minetest.sound_play(self.sounds.explode, {
2242 pos = pos,
2243 gain = 1.0,
2244 max_hear_distance = self.sounds.distance or 32
2245 }, true)
2247 entity_physics(pos, entity_damage_radius)
2248 effect(pos, 32, "tnt_smoke.png", nil, nil, node_break_radius, 1, 0)
2251 return true
2255 elseif self.attack_type == "dogfight"
2256 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 2)
2257 or (self.attack_type == "dogshoot" and dist <= self.reach and dogswitch(self) == 0) then
2259 if self.fly
2260 and dist > self.reach then
2262 local p1 = s
2263 local me_y = floor(p1.y)
2264 local p2 = p
2265 local p_y = floor(p2.y + 1)
2266 local v = self.object:get_velocity()
2268 if flight_check(self, s) then
2270 if me_y < p_y then
2272 self.object:set_velocity({
2273 x = v.x,
2274 y = 1 * self.walk_velocity,
2275 z = v.z
2278 elseif me_y > p_y then
2280 self.object:set_velocity({
2281 x = v.x,
2282 y = -1 * self.walk_velocity,
2283 z = v.z
2286 else
2287 if me_y < p_y then
2289 self.object:set_velocity({
2290 x = v.x,
2291 y = 0.01,
2292 z = v.z
2295 elseif me_y > p_y then
2297 self.object:set_velocity({
2298 x = v.x,
2299 y = -0.01,
2300 z = v.z
2307 -- rnd: new movement direction
2308 if self.path.following
2309 and self.path.way
2310 and self.attack_type ~= "dogshoot" then
2312 -- no paths longer than 50
2313 if #self.path.way > 50
2314 or dist < self.reach then
2315 self.path.following = false
2316 return
2319 local p1 = self.path.way[1]
2321 if not p1 then
2322 self.path.following = false
2323 return
2326 if abs(p1.x-s.x) + abs(p1.z - s.z) < 0.6 then
2327 -- reached waypoint, remove it from queue
2328 table.remove(self.path.way, 1)
2331 -- set new temporary target
2332 p = {x = p1.x, y = p1.y, z = p1.z}
2335 local vec = {
2336 x = p.x - s.x,
2337 z = p.z - s.z
2340 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2342 if p.x > s.x then yaw = yaw + pi end
2344 yaw = set_yaw(self, yaw)
2346 -- move towards enemy if beyond mob reach
2347 if dist > self.reach then
2349 -- path finding by rnd
2350 if self.pathfinding -- only if mob has pathfinding enabled
2351 and enable_pathfinding then
2353 smart_mobs(self, s, p, dist, dtime)
2356 if is_at_cliff_or_danger(self) then
2358 set_velocity(self, 0)
2359 set_animation(self, "stand")
2360 else
2362 if self.path.stuck then
2363 set_velocity(self, self.walk_velocity)
2364 else
2365 set_velocity(self, self.run_velocity)
2368 if self.animation and self.animation.run_start then
2369 set_animation(self, "run")
2370 else
2371 set_animation(self, "walk")
2375 else -- rnd: if inside reach range
2377 self.path.stuck = false
2378 self.path.stuck_timer = 0
2379 self.path.following = false -- not stuck anymore
2381 set_velocity(self, 0)
2383 if not self.custom_attack then
2385 if self.timer > 1 then
2387 self.timer = 0
2389 if self.double_melee_attack
2390 and random(1, 2) == 1 then
2391 set_animation(self, "punch2")
2392 else
2393 set_animation(self, "punch")
2396 local p2 = p
2397 local s2 = s
2399 p2.y = p2.y + .5
2400 s2.y = s2.y + .5
2402 if line_of_sight(self, p2, s2) == true then
2404 -- play attack sound
2405 mob_sound(self, "attack")
2407 -- punch player (or what player is attached to)
2408 local attached = self.attack:get_attach()
2409 if attached then
2410 self.attack = attached
2412 self.attack:punch(self.object, 1.0, {
2413 full_punch_interval = 1.0,
2414 damage_groups = {fleshy = self.damage}
2415 }, nil)
2418 else -- call custom attack every second
2419 if self.custom_attack
2420 and self.timer > 1 then
2422 self.timer = 0
2424 self.custom_attack(self, p)
2429 elseif self.attack_type == "shoot"
2430 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 1)
2431 or (self.attack_type == "dogshoot" and dist > self.reach and dogswitch(self) == 0) then
2433 p.y = p.y - .5
2434 s.y = s.y + .5
2436 local dist = vector.distance(p, s)
2437 local vec = {
2438 x = p.x - s.x,
2439 y = p.y - s.y,
2440 z = p.z - s.z
2443 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2445 if p.x > s.x then yaw = yaw + pi end
2447 yaw = set_yaw(self, yaw)
2449 set_velocity(self, 0)
2451 if self.shoot_interval
2452 and self.timer > self.shoot_interval
2453 and random(1, 100) <= 60 then
2455 self.timer = 0
2456 set_animation(self, "shoot")
2458 -- play shoot attack sound
2459 mob_sound(self, "shoot_attack")
2461 local p = self.object:get_pos()
2463 p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
2465 -- Shoot arrow
2466 if minetest.registered_entities[self.arrow] then
2468 local arrow, ent
2469 local v = 1
2470 if not self.shoot_arrow then
2471 arrow = minetest.add_entity(p, self.arrow)
2472 ent = arrow:get_luaentity()
2473 if ent.velocity then
2474 v = ent.velocity
2476 ent.switch = 1
2477 ent.owner_id = tostring(self.object) -- add unique owner id to arrow
2480 local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5
2481 -- offset makes shoot aim accurate
2482 vec.y = vec.y + self.shoot_offset
2483 vec.x = vec.x * (v / amount)
2484 vec.y = vec.y * (v / amount)
2485 vec.z = vec.z * (v / amount)
2486 if self.shoot_arrow then
2487 vec = vector.normalize(vec)
2488 self:shoot_arrow(p, vec)
2489 else
2490 arrow:set_velocity(vec)
2499 -- falling and fall damage
2500 -- returns true if mob died
2501 local falling = function(self, pos)
2503 if self.fly then
2504 return
2507 -- floating in water (or falling)
2508 local v = self.object:get_velocity()
2510 if v.y > 0 then
2512 -- apply gravity when moving up
2513 self.object:set_acceleration({
2514 x = 0,
2515 y = -10,
2516 z = 0
2519 elseif v.y <= 0 and v.y > self.fall_speed then
2521 -- fall downwards at set speed
2522 self.object:set_acceleration({
2523 x = 0,
2524 y = self.fall_speed,
2525 z = 0
2527 else
2528 -- stop accelerating once max fall speed hit
2529 self.object:set_acceleration({x = 0, y = 0, z = 0})
2532 -- in water then float up
2533 if minetest.registered_nodes[node_ok(pos).name].groups.water then
2535 if self.floats == 1 then
2537 self.object:set_acceleration({
2538 x = 0,
2539 y = -self.fall_speed / (max(1, v.y) ^ 2),
2540 z = 0
2543 else
2545 -- fall damage onto solid ground
2546 if self.fall_damage == 1
2547 and self.object:get_velocity().y == 0 then
2549 local d = (self.old_y or 0) - self.object:get_pos().y
2551 if d > 5 then
2553 local add = minetest.get_item_group(self.standing_on, "fall_damage_add_percent")
2554 local damage = d - 5
2555 if add ~= 0 then
2556 damage = damage + damage * (add/100)
2558 damage = floor(damage)
2559 if damage > 0 then
2560 self.health = self.health - damage
2562 effect(pos, 5, "tnt_smoke.png", 1, 2, 2, nil)
2564 if check_for_death(self, "fall", {type = "fall"}) then
2565 return true
2570 self.old_y = self.object:get_pos().y
2576 -- deal damage and effects when mob punched
2577 local mob_punch = function(self, hitter, tflp, tool_capabilities, dir)
2579 -- custom punch function
2580 if self.do_punch then
2582 -- when false skip going any further
2583 if self.do_punch(self, hitter, tflp, tool_caps, dir) == false then
2584 return
2588 -- error checking when mod profiling is enabled
2589 if not tool_capabilities then
2590 minetest.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2591 return
2594 -- is mob protected?
2595 if self.protected and hitter:is_player()
2596 and minetest.is_protected(self.object:get_pos(), hitter:get_player_name()) then
2597 return
2601 -- punch interval
2602 local weapon = hitter:get_wielded_item()
2603 local punch_interval = 1.4
2605 -- exhaust attacker
2606 if mod_hunger and hitter:is_player() then
2607 mcl_hunger.exhaust(hitter:get_player_name(), mcl_hunger.EXHAUST_ATTACK)
2610 -- calculate mob damage
2611 local damage = 0
2612 local armor = self.object:get_armor_groups() or {}
2613 local tmp
2615 -- quick error check incase it ends up 0 (serialize.h check test)
2616 if tflp == 0 then
2617 tflp = 0.2
2620 if use_cmi then
2621 damage = cmi.calculate_damage(self.object, hitter, tflp, tool_capabilities, dir)
2622 else
2624 for group,_ in pairs( (tool_capabilities.damage_groups or {}) ) do
2626 tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)
2628 if tmp < 0 then
2629 tmp = 0.0
2630 elseif tmp > 1 then
2631 tmp = 1.0
2634 damage = damage + (tool_capabilities.damage_groups[group] or 0)
2635 * tmp * ((armor[group] or 0) / 100.0)
2639 -- check for tool immunity or special damage
2640 for n = 1, #self.immune_to do
2642 if self.immune_to[n][1] == weapon:get_name() then
2644 damage = self.immune_to[n][2] or 0
2645 break
2649 -- healing
2650 if damage <= -1 then
2651 self.health = self.health - floor(damage)
2652 return
2655 if use_cmi then
2657 local cancel = cmi.notify_punch(self.object, hitter, tflp, tool_capabilities, dir, damage)
2659 if cancel then return end
2662 if tool_capabilities then
2663 punch_interval = tool_capabilities.full_punch_interval or 1.4
2666 -- add weapon wear manually
2667 -- Required because we have custom health handling ("health" property)
2668 if minetest.settings:get_bool("creative_mode") ~= true
2669 and tool_capabilities then
2670 if tool_capabilities.punch_attack_uses then
2671 -- Without this delay, the wear does not work. Quite hacky ...
2672 minetest.after(0, function(name)
2673 local player = minetest.get_player_by_name(name)
2674 if not player then return end
2675 local weapon = hitter:get_wielded_item(player)
2676 local def = weapon:get_definition()
2677 if def.tool_capabilities and def.tool_capabilities.punch_attack_uses then
2678 local wear = floor(65535/tool_capabilities.punch_attack_uses)
2679 weapon:add_wear(wear)
2680 hitter:set_wielded_item(weapon)
2682 end, hitter:get_player_name())
2686 local die = false
2688 -- only play hit sound and show blood effects if damage is 1 or over
2689 if damage >= 1 then
2691 -- weapon sounds
2692 if weapon:get_definition().sounds ~= nil then
2694 local s = random(0, #weapon:get_definition().sounds)
2696 minetest.sound_play(weapon:get_definition().sounds[s], {
2697 object = self.object, --hitter,
2698 max_hear_distance = 8
2699 }, true)
2700 else
2701 minetest.sound_play("default_punch", {
2702 object = self.object, --hitter,
2703 max_hear_distance = 5
2704 }, true)
2707 damage_effect(self, damage)
2709 -- do damage
2710 self.health = self.health - floor(damage)
2712 -- skip future functions if dead, except alerting others
2713 if check_for_death(self, "hit", {type = "punch", puncher = hitter}) then
2714 die = true
2717 -- knock back effect (only on full punch)
2718 if not die
2719 and self.knock_back
2720 and tflp >= punch_interval then
2722 local v = self.object:get_velocity()
2723 local r = 1.4 - min(punch_interval, 1.4)
2724 local kb = r * 2.0
2725 local up = 2
2727 -- if already in air then dont go up anymore when hit
2728 if v.y > 0
2729 or self.fly then
2730 up = 0
2733 -- direction error check
2734 dir = dir or {x = 0, y = 0, z = 0}
2736 -- check if tool already has specific knockback value
2737 if tool_capabilities.damage_groups["knockback"] then
2738 kb = tool_capabilities.damage_groups["knockback"]
2739 else
2740 kb = kb * 1.5
2743 self.object:set_velocity({
2744 x = dir.x * kb,
2745 y = dir.y * kb + up,
2746 z = dir.z * kb
2749 self.pause_timer = 0.25
2751 end -- END if damage
2753 -- if skittish then run away
2754 if not die and self.runaway == true then
2756 local lp = hitter:get_pos()
2757 local s = self.object:get_pos()
2758 local vec = {
2759 x = lp.x - s.x,
2760 y = lp.y - s.y,
2761 z = lp.z - s.z
2764 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
2766 if lp.x > s.x then
2767 yaw = yaw + pi
2770 yaw = set_yaw(self, yaw, 6)
2771 self.state = "runaway"
2772 self.runaway_timer = 0
2773 self.following = nil
2776 local name = hitter:get_player_name() or ""
2778 -- attack puncher and call other mobs for help
2779 if self.passive == false
2780 and self.state ~= "flop"
2781 and (self.child == false or self.type == "monster")
2782 and hitter:get_player_name() ~= self.owner
2783 and not mobs.invis[ name ] then
2785 if not die then
2786 -- attack whoever punched mob
2787 self.state = ""
2788 do_attack(self, hitter)
2791 -- alert others to the attack
2792 local objs = minetest.get_objects_inside_radius(hitter:get_pos(), self.view_range)
2793 local obj = nil
2795 for n = 1, #objs do
2797 obj = objs[n]:get_luaentity()
2799 if obj then
2801 -- only alert members of same mob or friends
2802 if obj.group_attack
2803 and obj.state ~= "attack"
2804 and obj.owner ~= name then
2805 if obj.name == self.name then
2806 do_attack(obj, hitter)
2807 elseif type(obj.group_attack) == "table" then
2808 for i=1, #obj.group_attack do
2809 if obj.name == obj.group_attack[i] then
2810 do_attack(obj, hitter)
2811 break
2817 -- have owned mobs attack player threat
2818 if obj.owner == name and obj.owner_loyal then
2819 do_attack(obj, self.object)
2826 local mob_detach_child = function(self, child)
2828 if self.driver == child then
2829 self.driver = nil
2834 -- get entity staticdata
2835 local mob_staticdata = function(self)
2837 -- remove mob when out of range unless tamed
2838 if remove_far
2839 and self.can_despawn
2840 and self.remove_ok
2841 and ((not self.nametag) or (self.nametag == ""))
2842 and self.lifetimer <= 20 then
2844 minetest.log("action", "Mob "..name.." despawns in mob_staticdata at "..minetest.pos_to_string(self.object.get_pos()))
2845 self.object:remove()
2847 return ""-- nil
2850 self.remove_ok = true
2851 self.attack = nil
2852 self.following = nil
2853 self.state = "stand"
2855 if use_cmi then
2856 self.serialized_cmi_components = cmi.serialize_components(self._cmi_components)
2859 local tmp = {}
2861 for _,stat in pairs(self) do
2863 local t = type(stat)
2865 if t ~= "function"
2866 and t ~= "nil"
2867 and t ~= "userdata"
2868 and _ ~= "_cmi_components" then
2869 tmp[_] = self[_]
2873 return minetest.serialize(tmp)
2877 -- activate mob and reload settings
2878 local mob_activate = function(self, staticdata, def, dtime)
2880 -- remove monsters in peaceful mode
2881 if self.type == "monster"
2882 and minetest.settings:get_bool("only_peaceful_mobs", false) then
2884 self.object:remove()
2886 return
2889 -- load entity variables
2890 local tmp = minetest.deserialize(staticdata)
2892 if tmp then
2893 for _,stat in pairs(tmp) do
2894 self[_] = stat
2898 -- select random texture, set model and size
2899 if not self.base_texture then
2901 -- compatiblity with old simple mobs textures
2902 if type(def.textures[1]) == "string" then
2903 def.textures = {def.textures}
2906 self.base_texture = def.textures[random(1, #def.textures)]
2907 self.base_mesh = def.mesh
2908 self.base_size = self.visual_size
2909 self.base_colbox = self.collisionbox
2910 self.base_selbox = self.selectionbox
2913 -- for current mobs that dont have this set
2914 if not self.base_selbox then
2915 self.base_selbox = self.selectionbox or self.base_colbox
2918 -- set texture, model and size
2919 local textures = self.base_texture
2920 local mesh = self.base_mesh
2921 local vis_size = self.base_size
2922 local colbox = self.base_colbox
2923 local selbox = self.base_selbox
2925 -- specific texture if gotten
2926 if self.gotten == true
2927 and def.gotten_texture then
2928 textures = def.gotten_texture
2931 -- specific mesh if gotten
2932 if self.gotten == true
2933 and def.gotten_mesh then
2934 mesh = def.gotten_mesh
2937 -- set child objects to half size
2938 if self.child == true then
2940 vis_size = {
2941 x = self.base_size.x * .5,
2942 y = self.base_size.y * .5,
2945 if def.child_texture then
2946 textures = def.child_texture[1]
2949 colbox = {
2950 self.base_colbox[1] * .5,
2951 self.base_colbox[2] * .5,
2952 self.base_colbox[3] * .5,
2953 self.base_colbox[4] * .5,
2954 self.base_colbox[5] * .5,
2955 self.base_colbox[6] * .5
2957 selbox = {
2958 self.base_selbox[1] * .5,
2959 self.base_selbox[2] * .5,
2960 self.base_selbox[3] * .5,
2961 self.base_selbox[4] * .5,
2962 self.base_selbox[5] * .5,
2963 self.base_selbox[6] * .5
2967 if self.health == 0 then
2968 self.health = random (self.hp_min, self.hp_max)
2970 if self.breath == nil then
2971 self.breath = self.breath_max
2974 -- pathfinding init
2975 self.path = {}
2976 self.path.way = {} -- path to follow, table of positions
2977 self.path.lastpos = {x = 0, y = 0, z = 0}
2978 self.path.stuck = false
2979 self.path.following = false -- currently following path?
2980 self.path.stuck_timer = 0 -- if stuck for too long search for path
2982 -- Armor groups
2983 -- immortal=1 because we use custom health
2984 -- handling (using "health" property)
2985 local armor
2986 if type(self.armor) == "table" then
2987 armor = table.copy(self.armor)
2988 armor.immortal = 1
2989 else
2990 armor = {immortal=1, fleshy = self.armor}
2992 self.object:set_armor_groups(armor)
2993 self.old_y = self.object:get_pos().y
2994 self.old_health = self.health
2995 self.sounds.distance = self.sounds.distance or 10
2996 self.textures = textures
2997 self.mesh = mesh
2998 self.collisionbox = colbox
2999 self.selectionbox = selbox
3000 self.visual_size = vis_size
3001 self.standing_in = "ignore"
3002 self.standing_on = "ignore"
3003 self.jump_sound_cooloff = 0 -- used to prevent jump sound from being played too often in short time
3004 self.opinion_sound_cooloff = 0 -- used to prevent sound spam of particular sound types
3006 self.texture_mods = {}
3007 self.object:set_texture_mod("")
3009 self.v_start = false
3010 self.timer = 0
3011 self.blinktimer = 0
3012 self.blinkstatus = false
3014 -- check existing nametag
3015 if not self.nametag then
3016 self.nametag = def.nametag
3019 -- set anything changed above
3020 self.object:set_properties(self)
3021 set_yaw(self, (random(0, 360) - 180) / 180 * pi, 6)
3022 update_tag(self)
3023 set_animation(self, "stand")
3025 -- run on_spawn function if found
3026 if self.on_spawn and not self.on_spawn_run then
3027 if self.on_spawn(self) then
3028 self.on_spawn_run = true -- if true, set flag to run once only
3032 -- run after_activate
3033 if def.after_activate then
3034 def.after_activate(self, staticdata, def, dtime)
3037 if use_cmi then
3038 self._cmi_components = cmi.activate_components(self.serialized_cmi_components)
3039 cmi.notify_activate(self.object, dtime)
3044 -- main mob function
3045 local mob_step = function(self, dtime)
3047 if use_cmi then
3048 cmi.notify_step(self.object, dtime)
3051 local pos = self.object:get_pos()
3052 local yaw = 0
3054 -- Despawning: when lifetimer expires, remove mob
3055 if remove_far
3056 and self.can_despawn == true
3057 and ((not self.nametag) or (self.nametag == "")) then
3059 -- TODO: Finish up implementation of despawning rules
3061 self.lifetimer = self.lifetimer - dtime
3063 if self.lifetimer <= 0 then
3065 -- only despawn away from player
3066 local objs = minetest.get_objects_inside_radius(pos, 32)
3068 for n = 1, #objs do
3070 if objs[n]:is_player() then
3072 self.lifetimer = 20
3074 return
3078 minetest.log("action", "Mob "..name.." despawns in mob_step at "..minetest.pos_to_string(pos))
3079 self.object:remove()
3081 return
3085 if self.jump_sound_cooloff > 0 then
3086 self.jump_sound_cooloff = self.jump_sound_cooloff - dtime
3088 if self.opinion_sound_cooloff > 0 then
3089 self.opinion_sound_cooloff = self.opinion_sound_cooloff - dtime
3091 if falling(self, pos) then
3092 -- Return if mob died after falling
3093 return
3096 -- smooth rotation by ThomasMonroe314
3098 if self.delay and self.delay > 0 then
3100 local yaw = self.object:get_yaw() or 0
3102 if self.delay == 1 then
3103 yaw = self.target_yaw
3104 else
3105 local dif = abs(yaw - self.target_yaw)
3107 if yaw > self.target_yaw then
3109 if dif > pi then
3110 dif = 2 * pi - dif -- need to add
3111 yaw = yaw + dif / self.delay
3112 else
3113 yaw = yaw - dif / self.delay -- need to subtract
3116 elseif yaw < self.target_yaw then
3118 if dif > pi then
3119 dif = 2 * pi - dif
3120 yaw = yaw - dif / self.delay -- need to subtract
3121 else
3122 yaw = yaw + dif / self.delay -- need to add
3126 if yaw > (pi * 2) then yaw = yaw - (pi * 2) end
3127 if yaw < 0 then yaw = yaw + (pi * 2) end
3130 self.delay = self.delay - 1
3131 self.object:set_yaw(yaw)
3134 -- end rotation
3136 -- knockback timer
3137 if self.pause_timer > 0 then
3139 self.pause_timer = self.pause_timer - dtime
3141 return
3144 -- run custom function (defined in mob lua file)
3145 if self.do_custom then
3147 -- when false skip going any further
3148 if self.do_custom(self, dtime) == false then
3149 return
3153 -- attack timer
3154 self.timer = self.timer + dtime
3156 if self.state ~= "attack" then
3158 if self.timer < 1 then
3159 return
3162 self.timer = 0
3165 -- never go over 100
3166 if self.timer > 100 then
3167 self.timer = 1
3170 -- mob plays random sound at times
3171 if random(1, 100) == 1 then
3172 mob_sound(self, "random", true)
3175 -- environmental damage timer (every 1 second)
3176 self.env_damage_timer = self.env_damage_timer + dtime
3178 if (self.state == "attack" and self.env_damage_timer > 1)
3179 or self.state ~= "attack" then
3181 self.env_damage_timer = 0
3183 -- check for environmental damage (water, fire, lava etc.)
3184 if do_env_damage(self) then
3185 return
3188 -- node replace check (cow eats grass etc.)
3189 replace(self, pos)
3192 monster_attack(self)
3194 npc_attack(self)
3196 breed(self)
3198 follow_flop(self)
3200 if do_states(self, dtime) then
3201 return
3204 do_jump(self)
3206 runaway_from(self)
3211 -- default function when mobs are blown up with TNT
3212 local do_tnt = function(obj, damage)
3214 obj.object:punch(obj.object, 1.0, {
3215 full_punch_interval = 1.0,
3216 damage_groups = {fleshy = damage},
3217 }, nil)
3219 return false, true, {}
3223 mobs.spawning_mobs = {}
3225 -- Code to execute before custom on_rightclick handling
3226 local on_rightclick_prefix = function(self, clicker)
3227 local item = clicker:get_wielded_item()
3229 -- Name mob with nametag
3230 if not self.ignores_nametag and item:get_name() == "mcl_mobs:nametag" then
3232 local tag = item:get_meta():get_string("name")
3233 if tag ~= "" then
3234 if string.len(tag) > MAX_MOB_NAME_LENGTH then
3235 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH)
3237 self.nametag = tag
3239 update_tag(self)
3241 if not mobs.is_creative(clicker:get_player_name()) then
3242 item:take_item()
3243 clicker:set_wielded_item(item)
3245 return true
3249 return false
3252 local create_mob_on_rightclick = function(on_rightclick)
3253 return function(self, clicker)
3254 local stop = on_rightclick_prefix(self, clicker)
3255 if (not stop) and (on_rightclick) then
3256 on_rightclick(self, clicker)
3261 -- register mob entity
3262 function mobs:register_mob(name, def)
3264 mobs.spawning_mobs[name] = true
3266 local can_despawn
3267 if def.can_despawn ~= nil then
3268 can_despawn = def.can_despawn
3269 else
3270 can_despawn = true
3273 local function scale_difficulty(value, default, min, special)
3274 if (not value) or (value == default) or (value == special) then
3275 return default
3276 else
3277 return max(min, value * difficulty)
3281 local collisionbox = def.collisionbox or {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25}
3282 -- Workaround for <https://github.com/minetest/minetest/issues/5966>:
3283 -- Increase upper Y limit to avoid mobs glitching through solid nodes.
3284 -- FIXME: Remove workaround if it's no longer needed.
3285 if collisionbox[5] < 0.79 then
3286 collisionbox[5] = 0.79
3289 minetest.register_entity(name, {
3291 stepheight = def.stepheight or 0.6,
3292 name = name,
3293 type = def.type,
3294 attack_type = def.attack_type,
3295 fly = def.fly,
3296 fly_in = def.fly_in or {"air", "__airlike"},
3297 owner = def.owner or "",
3298 order = def.order or "",
3299 on_die = def.on_die,
3300 spawn_small_alternative = def.spawn_small_alternative,
3301 do_custom = def.do_custom,
3302 jump_height = def.jump_height or 4, -- was 6
3303 rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
3304 lifetimer = def.lifetimer or 57.73,
3305 hp_min = scale_difficulty(def.hp_min, 5, 1),
3306 hp_max = scale_difficulty(def.hp_max, 10, 1),
3307 breath_max = def.breath_max or 15,
3308 breathes_in_water = def.breathes_in_water or false,
3309 physical = true,
3310 collisionbox = collisionbox,
3311 selectionbox = def.selectionbox or def.collisionbox,
3312 visual = def.visual,
3313 visual_size = def.visual_size or {x = 1, y = 1},
3314 mesh = def.mesh,
3315 makes_footstep_sound = def.makes_footstep_sound or false,
3316 view_range = def.view_range or 16,
3317 walk_velocity = def.walk_velocity or 1,
3318 run_velocity = def.run_velocity or 2,
3319 damage = scale_difficulty(def.damage, 0, 0),
3320 light_damage = def.light_damage or 0,
3321 sunlight_damage = def.sunlight_damage or 0,
3322 water_damage = def.water_damage or 0,
3323 lava_damage = def.lava_damage or 8,
3324 fire_damage = def.fire_damage or 1,
3325 suffocation = def.suffocation or true,
3326 fall_damage = def.fall_damage or 1,
3327 fall_speed = def.fall_speed or -10, -- must be lower than -2 (default: -10)
3328 drops = def.drops or {},
3329 armor = def.armor or 100,
3330 on_rightclick = create_mob_on_rightclick(def.on_rightclick),
3331 arrow = def.arrow,
3332 shoot_interval = def.shoot_interval,
3333 sounds = def.sounds or {},
3334 animation = def.animation,
3335 follow = def.follow,
3336 jump = def.jump ~= false,
3337 walk_chance = def.walk_chance or 50,
3338 attacks_monsters = def.attacks_monsters or false,
3339 group_attack = def.group_attack or false,
3340 passive = def.passive or false,
3341 knock_back = def.knock_back ~= false,
3342 shoot_offset = def.shoot_offset or 0,
3343 floats = def.floats or 1, -- floats in water by default
3344 replace_rate = def.replace_rate,
3345 replace_what = def.replace_what,
3346 replace_with = def.replace_with,
3347 replace_offset = def.replace_offset or 0,
3348 on_replace = def.on_replace,
3349 timer = 0,
3350 env_damage_timer = 0, -- only used when state = "attack"
3351 tamed = false,
3352 pause_timer = 0,
3353 horny = false,
3354 hornytimer = 0,
3355 gotten = false,
3356 health = 0,
3357 reach = def.reach or 3,
3358 htimer = 0,
3359 texture_list = def.textures,
3360 child_texture = def.child_texture,
3361 docile_by_day = def.docile_by_day or false,
3362 time_of_day = 0.5,
3363 fear_height = def.fear_height or 0,
3364 runaway = def.runaway,
3365 runaway_timer = 0,
3366 pathfinding = def.pathfinding,
3367 immune_to = def.immune_to or {},
3368 explosion_radius = def.explosion_radius,
3369 explosion_damage_radius = def.explosion_damage_radius,
3370 explosion_timer = def.explosion_timer or 3,
3371 allow_fuse_reset = def.allow_fuse_reset ~= false,
3372 stop_to_explode = def.stop_to_explode ~= false,
3373 custom_attack = def.custom_attack,
3374 double_melee_attack = def.double_melee_attack,
3375 dogshoot_switch = def.dogshoot_switch,
3376 dogshoot_count = 0,
3377 dogshoot_count_max = def.dogshoot_count_max or 5,
3378 dogshoot_count2_max = def.dogshoot_count2_max or (def.dogshoot_count_max or 5),
3379 attack_animals = def.attack_animals or false,
3380 specific_attack = def.specific_attack,
3381 runaway_from = def.runaway_from,
3382 owner_loyal = def.owner_loyal,
3383 facing_fence = false,
3384 _cmi_is_mob = true,
3386 -- MCL2 extensions
3387 spawn_class = def.spawn_class,
3388 ignores_nametag = def.ignores_nametag or false,
3389 rain_damage = def.rain_damage or 0,
3390 glow = def.glow,
3391 can_despawn = can_despawn,
3392 child = def.child or false,
3393 texture_mods = {},
3394 shoot_arrow = def.shoot_arrow,
3395 sounds_child = def.sounds_child,
3396 -- End of MCL2 extensions
3398 on_spawn = def.on_spawn,
3400 on_blast = def.on_blast or do_tnt,
3402 on_step = mob_step,
3404 do_punch = def.do_punch,
3406 on_punch = mob_punch,
3408 on_breed = def.on_breed,
3410 on_grown = def.on_grown,
3412 on_detach_child = mob_detach_child,
3414 on_activate = function(self, staticdata, dtime)
3415 return mob_activate(self, staticdata, def, dtime)
3416 end,
3418 get_staticdata = function(self)
3419 return mob_staticdata(self)
3420 end,
3424 if minetest.get_modpath("doc_identifier") ~= nil then
3425 doc.sub.identifier.register_object(name, "basics", "mobs")
3428 end -- END mobs:register_mob function
3431 -- count how many mobs of one type are inside an area
3432 local count_mobs = function(pos, mobtype)
3434 local num = 0
3435 local objs = minetest.get_objects_inside_radius(pos, aoc_range)
3437 for n = 1, #objs do
3439 local obj = objs[n]:get_luaentity()
3441 if obj and obj.name and obj._cmi_is_mob then
3443 -- count passive mobs only
3444 if mobtype == "!passive" then
3445 if obj.spawn_class == "passive" then
3446 num = num + 1
3448 -- count hostile mobs only
3449 elseif mobtype == "!hostile" then
3450 if obj.spawn_class == "hostile" then
3451 num = num + 1
3453 -- count ambient mobs only
3454 elseif mobtype == "!ambient" then
3455 if obj.spawn_class == "ambient" then
3456 num = num + 1
3458 -- count water mobs only
3459 elseif mobtype == "!water" then
3460 if obj.spawn_class == "water" then
3461 num = num + 1
3463 -- count mob type
3464 elseif mobtype and obj.name == mobtype then
3465 num = num + 1
3466 -- count total mobs
3467 elseif not mobtype then
3468 num = num + 1
3473 return num
3477 -- global functions
3479 function mobs:spawn_abm_check(pos, node, name)
3480 -- global function to add additional spawn checks
3481 -- return true to stop spawning mob
3485 function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
3486 interval, chance, aoc, min_height, max_height, day_toggle, on_spawn)
3488 -- Do mobs spawn at all?
3489 if not mobs_spawn then
3490 return
3493 -- chance/spawn number override in minetest.conf for registered mob
3494 local numbers = minetest.settings:get(name)
3496 if numbers then
3497 numbers = numbers:split(",")
3498 chance = tonumber(numbers[1]) or chance
3499 aoc = tonumber(numbers[2]) or aoc
3501 if chance == 0 then
3502 minetest.log("warning", string.format("[mobs] %s has spawning disabled", name))
3503 return
3506 minetest.log("action",
3507 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name, chance, aoc))
3511 local spawn_action
3512 spawn_action = function(pos, node, active_object_count, active_object_count_wider, name)
3514 local orig_pos = table.copy(pos)
3515 -- is mob actually registered?
3516 if not mobs.spawning_mobs[name]
3517 or not minetest.registered_entities[name] then
3518 minetest.log("warning", "Mob spawn of "..name.." failed, unknown entity or mob is not registered for spawning!")
3519 return
3522 -- additional custom checks for spawning mob
3523 if mobs:spawn_abm_check(pos, node, name) == true then
3524 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, ABM check rejected!")
3525 return
3528 -- count nearby mobs in same spawn class
3529 local entdef = minetest.registered_entities[name]
3530 local spawn_class = entdef and entdef.spawn_class
3531 if not spawn_class then
3532 if entdef.type == "monster" then
3533 spawn_class = "hostile"
3534 else
3535 spawn_class = "passive"
3538 local in_class_cap = count_mobs(pos, "!"..spawn_class) < MOB_CAP[spawn_class]
3539 -- do not spawn if too many of same mob in area
3540 if active_object_count_wider >= max_per_block -- large-range mob cap
3541 or (not in_class_cap) -- spawn class mob cap
3542 or count_mobs(pos, name) >= aoc then -- per-mob mob cap
3543 -- too many entities
3544 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, too crowded!")
3545 return
3548 -- if toggle set to nil then ignore day/night check
3549 if day_toggle ~= nil then
3551 local tod = (minetest.get_timeofday() or 0) * 24000
3553 if tod > 4500 and tod < 19500 then
3554 -- daylight, but mob wants night
3555 if day_toggle == false then
3556 -- mob needs night
3557 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, mob needs light!")
3558 return
3560 else
3561 -- night time but mob wants day
3562 if day_toggle == true then
3563 -- mob needs day
3564 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, mob needs daylight!")
3565 return
3570 -- spawn above node
3571 pos.y = pos.y + 1
3573 -- only spawn away from player
3574 local objs = minetest.get_objects_inside_radius(pos, 10)
3576 for n = 1, #objs do
3578 if objs[n]:is_player() then
3579 -- player too close
3580 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, player too close!")
3581 return
3585 -- mobs cannot spawn in protected areas when enabled
3586 if not spawn_protected
3587 and minetest.is_protected(pos, "") then
3588 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, position is protected!")
3589 return
3592 -- are we spawning within height limits?
3593 if pos.y > max_height
3594 or pos.y < min_height then
3595 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, out of height limit!")
3596 return
3599 -- are light levels ok?
3600 local light = minetest.get_node_light(pos)
3601 if not light
3602 or light > max_light
3603 or light < min_light then
3604 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, bad light!")
3605 return
3608 -- do we have enough space to spawn mob?
3609 local ent = minetest.registered_entities[name]
3610 local width_x = max(1, math.ceil(ent.collisionbox[4] - ent.collisionbox[1]))
3611 local min_x, max_x
3612 if width_x % 2 == 0 then
3613 max_x = math.floor(width_x/2)
3614 min_x = -(max_x-1)
3615 else
3616 max_x = math.floor(width_x/2)
3617 min_x = -max_x
3620 local width_z = max(1, math.ceil(ent.collisionbox[6] - ent.collisionbox[3]))
3621 local min_z, max_z
3622 if width_z % 2 == 0 then
3623 max_z = math.floor(width_z/2)
3624 min_z = -(max_z-1)
3625 else
3626 max_z = math.floor(width_z/2)
3627 min_z = -max_z
3630 local max_y = max(0, math.ceil(ent.collisionbox[5] - ent.collisionbox[2]) - 1)
3632 for y = 0, max_y do
3633 for x = min_x, max_x do
3634 for z = min_z, max_z do
3635 local pos2 = {x = pos.x+x, y = pos.y+y, z = pos.z+z}
3636 if minetest.registered_nodes[node_ok(pos2).name].walkable == true then
3637 -- inside block
3638 minetest.log("info", "Mob spawn of "..name.." at "..minetest.pos_to_string(pos).." failed, too little space!")
3639 if ent.spawn_small_alternative ~= nil and (not minetest.registered_nodes[node_ok(pos).name].walkable) then
3640 minetest.log("info", "Trying to spawn smaller alternative mob: "..ent.spawn_small_alternative)
3641 spawn_action(orig_pos, node, active_object_count, active_object_count_wider, ent.spawn_small_alternative)
3643 return
3649 -- spawn mob 1/2 node above ground
3650 pos.y = pos.y + 0.5
3651 -- tweak X/Z spawn pos
3652 if width_x % 2 == 0 then
3653 pos.x = pos.x + 0.5
3655 if width_z % 2 == 0 then
3656 pos.z = pos.z + 0.5
3659 local mob = minetest.add_entity(pos, name)
3660 minetest.log("action", "Mob spawned: "..name.." at "..minetest.pos_to_string(pos))
3662 if on_spawn then
3664 local ent = mob:get_luaentity()
3666 on_spawn(ent, pos)
3670 local function spawn_abm_action(pos, node, active_object_count, active_object_count_wider)
3671 spawn_action(pos, node, active_object_count, active_object_count_wider, name)
3674 minetest.register_abm({
3675 label = name .. " spawning",
3676 nodenames = nodes,
3677 neighbors = neighbors,
3678 interval = interval,
3679 chance = floor(max(1, chance * mobs_spawn_chance)),
3680 catch_up = false,
3681 action = spawn_abm_action,
3686 -- compatibility with older mob registration
3687 function mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
3689 mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
3690 chance, active_object_count, -31000, max_height, day_toggle)
3694 -- MarkBu's spawn function
3695 function mobs:spawn(def)
3697 local name = def.name
3698 local nodes = def.nodes or {"group:soil", "group:stone"}
3699 local neighbors = def.neighbors or {"air"}
3700 local min_light = def.min_light or 0
3701 local max_light = def.max_light or 15
3702 local interval = def.interval or 30
3703 local chance = def.chance or 5000
3704 local active_object_count = def.active_object_count or 1
3705 local min_height = def.min_height or -31000
3706 local max_height = def.max_height or 31000
3707 local day_toggle = def.day_toggle
3708 local on_spawn = def.on_spawn
3710 mobs:spawn_specific(name, nodes, neighbors, min_light, max_light, interval,
3711 chance, active_object_count, min_height, max_height, day_toggle, on_spawn)
3715 -- register arrow for shoot attack
3716 function mobs:register_arrow(name, def)
3718 if not name or not def then return end -- errorcheck
3720 minetest.register_entity(name, {
3722 physical = false,
3723 visual = def.visual,
3724 visual_size = def.visual_size,
3725 textures = def.textures,
3726 velocity = def.velocity,
3727 hit_player = def.hit_player,
3728 hit_node = def.hit_node,
3729 hit_mob = def.hit_mob,
3730 hit_object = def.hit_object,
3731 drop = def.drop or false, -- drops arrow as registered item when true
3732 collisionbox = {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3733 timer = 0,
3734 switch = 0,
3735 owner_id = def.owner_id,
3736 rotate = def.rotate,
3737 automatic_face_movement_dir = def.rotate
3738 and (def.rotate - (pi / 180)) or false,
3740 on_activate = def.on_activate,
3742 on_step = def.on_step or function(self, dtime)
3744 self.timer = self.timer + 1
3746 local pos = self.object:get_pos()
3748 if self.switch == 0
3749 or self.timer > 150
3750 or not within_limits(pos, 0) then
3752 self.object:remove();
3754 return
3757 -- does arrow have a tail (fireball)
3758 if def.tail
3759 and def.tail == 1
3760 and def.tail_texture then
3762 minetest.add_particle({
3763 pos = pos,
3764 velocity = {x = 0, y = 0, z = 0},
3765 acceleration = {x = 0, y = 0, z = 0},
3766 expirationtime = def.expire or 0.25,
3767 collisiondetection = false,
3768 texture = def.tail_texture,
3769 size = def.tail_size or 5,
3770 glow = def.glow or 0,
3774 if self.hit_node then
3776 local node = node_ok(pos).name
3778 if minetest.registered_nodes[node].walkable then
3780 self.hit_node(self, pos, node)
3782 if self.drop == true then
3784 pos.y = pos.y + 1
3786 self.lastpos = (self.lastpos or pos)
3788 minetest.add_item(self.lastpos, self.object:get_luaentity().name)
3791 self.object:remove();
3793 return
3797 if self.hit_player or self.hit_mob or self.hit_object then
3799 for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do
3801 if self.hit_player
3802 and player:is_player() then
3804 self.hit_player(self, player)
3805 self.object:remove();
3806 return
3809 local entity = player:get_luaentity()
3811 if entity
3812 and self.hit_mob
3813 and entity._cmi_is_mob == true
3814 and tostring(player) ~= self.owner_id
3815 and entity.name ~= self.object:get_luaentity().name then
3816 self.hit_mob(self, player)
3817 self.object:remove();
3818 return
3821 if entity
3822 and self.hit_object
3823 and (not entity._cmi_is_mob)
3824 and tostring(player) ~= self.owner_id
3825 and entity.name ~= self.object:get_luaentity().name then
3826 self.hit_object(self, player)
3827 self.object:remove();
3828 return
3833 self.lastpos = pos
3839 -- no damage to nodes explosion
3840 function mobs:safe_boom(self, pos, radius)
3842 minetest.sound_play(self.sounds and self.sounds.explode or "tnt_explode", {
3843 pos = pos,
3844 gain = 1.0,
3845 max_hear_distance = self.sounds and self.sounds.distance or 32
3846 }, true)
3848 entity_physics(pos, radius)
3849 effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
3853 -- make explosion with protection and tnt mod check
3854 function mobs:boom(self, pos, radius)
3856 if mobs_griefing
3857 and mod_tnt and tnt and tnt.boom
3858 and not minetest.is_protected(pos, "") then
3860 tnt.boom(pos, {
3861 radius = radius,
3862 damage_radius = radius,
3863 sound = self.sounds and self.sounds.explode,
3864 explode_center = true,
3865 is_tnt = false,
3867 else
3868 mobs:safe_boom(self, pos, radius)
3873 -- Register spawn eggs
3875 -- Note: This also introduces the “spawn_egg” group:
3876 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3877 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3878 function mobs:register_egg(mob, desc, background, addegg, no_creative)
3880 local grp = {spawn_egg = 1}
3882 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3883 if creative and no_creative == true then
3884 grp.not_in_creative_inventory = 1
3887 local invimg = background
3889 if addegg == 1 then
3890 invimg = "mobs_chicken_egg.png^(" .. invimg ..
3891 "^[mask:mobs_chicken_egg_overlay.png)"
3894 -- register old stackable mob egg
3895 minetest.register_craftitem(mob, {
3897 description = desc,
3898 inventory_image = invimg,
3899 groups = grp,
3901 _doc_items_longdesc = S("This allows you to place a single mob."),
3902 _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."),
3904 on_place = function(itemstack, placer, pointed_thing)
3906 local pos = pointed_thing.above
3908 -- am I clicking on something with existing on_rightclick function?
3909 local under = minetest.get_node(pointed_thing.under)
3910 local def = minetest.registered_nodes[under.name]
3911 if def and def.on_rightclick then
3912 return def.on_rightclick(pointed_thing.under, under, placer, itemstack)
3915 if pos
3916 and within_limits(pos, 0)
3917 and not minetest.is_protected(pos, placer:get_player_name()) then
3919 local name = placer:get_player_name()
3920 local privs = minetest.get_player_privs(name)
3921 if mod_mobspawners and under.name == "mcl_mobspawners:spawner" then
3922 if minetest.is_protected(pointed_thing.under, name) then
3923 minetest.record_protection_violation(pointed_thing.under, name)
3924 return itemstack
3926 if not privs.maphack then
3927 minetest.chat_send_player(name, S("You need the “maphack” privilege to change the mob spawner."))
3928 return itemstack
3930 mcl_mobspawners.setup_spawner(pointed_thing.under, itemstack:get_name())
3931 if not minetest.settings:get_bool("creative_mode") then
3932 itemstack:take_item()
3934 return itemstack
3937 if not minetest.registered_entities[mob] then
3938 return itemstack
3941 if minetest.settings:get_bool("only_peaceful_mobs", false)
3942 and minetest.registered_entities[mob].type == "monster" then
3943 minetest.chat_send_player(name, S("Only peaceful mobs allowed!"))
3944 return itemstack
3947 pos.y = pos.y + 1
3949 local mob = minetest.add_entity(pos, mob)
3950 local ent = mob:get_luaentity()
3952 -- don't set owner if monster or sneak pressed
3953 if ent.type ~= "monster"
3954 and not placer:get_player_control().sneak then
3955 ent.owner = placer:get_player_name()
3956 ent.tamed = true
3959 -- set nametag
3960 local nametag = itemstack:get_meta():get_string("name")
3961 if nametag ~= "" then
3962 if string.len(nametag) > MAX_MOB_NAME_LENGTH then
3963 nametag = string.sub(nametag, 1, MAX_MOB_NAME_LENGTH)
3965 ent.nametag = nametag
3966 update_tag(ent)
3969 -- if not in creative then take item
3970 if not mobs.is_creative(placer:get_player_name()) then
3971 itemstack:take_item()
3975 return itemstack
3976 end,
3982 -- No-op in MCL2 (capturing mobs is not possible).
3983 -- Provided for compability with Mobs Redo
3984 function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
3985 return false
3989 -- No-op in MCL2 (protecting mobs is not possible).
3990 function mobs:protect(self, clicker)
3991 return false
3995 -- feeding, taming and breeding (thanks blert2112)
3996 function mobs:feed_tame(self, clicker, feed_count, breed, tame)
3997 if not self.follow then
3998 return false
4001 -- can eat/tame with item in hand
4002 if follow_holding(self, clicker) then
4004 -- if not in creative then take item
4005 if not mobs.is_creative(clicker:get_player_name()) then
4007 local item = clicker:get_wielded_item()
4009 item:take_item()
4011 clicker:set_wielded_item(item)
4014 -- increase health
4015 self.health = self.health + 4
4017 if self.health >= self.hp_max then
4019 self.health = self.hp_max
4021 if self.htimer < 1 then
4022 self.htimer = 5
4026 self.object:set_hp(self.health)
4028 update_tag(self)
4030 -- make children grow quicker
4031 if self.child == true then
4033 self.hornytimer = self.hornytimer + 20
4035 return true
4038 -- feed and tame
4039 self.food = (self.food or 0) + 1
4040 if self.food >= feed_count then
4042 self.food = 0
4044 if breed and self.hornytimer == 0 then
4045 self.horny = true
4048 if tame then
4050 self.tamed = true
4052 if not self.owner or self.owner == "" then
4053 self.owner = clicker:get_player_name()
4057 -- make sound when fed so many times
4058 mob_sound(self, "random", true)
4061 return true
4064 return false
4067 -- Spawn a child
4068 function mobs:spawn_child(pos, mob_type)
4069 local child = minetest.add_entity(pos, mob_type)
4070 if not child then
4071 return
4074 local ent = child:get_luaentity()
4075 effect(pos, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
4077 ent.child = true
4079 local textures
4080 -- using specific child texture (if found)
4081 if ent.child_texture then
4082 textures = ent.child_texture[1]
4085 -- and resize to half height
4086 child:set_properties({
4087 textures = textures,
4088 visual_size = {
4089 x = ent.base_size.x * .5,
4090 y = ent.base_size.y * .5,
4092 collisionbox = {
4093 ent.base_colbox[1] * .5,
4094 ent.base_colbox[2] * .5,
4095 ent.base_colbox[3] * .5,
4096 ent.base_colbox[4] * .5,
4097 ent.base_colbox[5] * .5,
4098 ent.base_colbox[6] * .5,
4100 selectionbox = {
4101 ent.base_selbox[1] * .5,
4102 ent.base_selbox[2] * .5,
4103 ent.base_selbox[3] * .5,
4104 ent.base_selbox[4] * .5,
4105 ent.base_selbox[5] * .5,
4106 ent.base_selbox[6] * .5,
4110 return child
4114 -- compatibility function for old entities to new modpack entities
4115 function mobs:alias_mob(old_name, new_name)
4117 -- spawn egg
4118 minetest.register_alias(old_name, new_name)
4120 -- entity
4121 minetest.register_entity(":" .. old_name, {
4123 physical = false,
4125 on_step = function(self)
4127 if minetest.registered_entities[new_name] then
4128 minetest.add_entity(self.object:get_pos(), new_name)
4131 self.object:remove()