Fix crash when using nametag
[MineClone/MineClone2.git] / mods / ENTITIES / mcl_mobs / api.lua
blob9549b05dc229f01ab754b192c37e6007ac67c767
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 -- Intllib
11 local MP = minetest.get_modpath(minetest.get_current_modname())
12 local S, NS = dofile(MP .. "/intllib.lua")
13 mobs.intllib = S
16 -- CMI support check
17 local use_cmi = minetest.global_exists("cmi")
20 -- Invisibility mod check
21 mobs.invis = {}
22 if minetest.global_exists("invisibility") then
23 mobs.invis = invisibility
24 end
27 -- creative check
28 local creative_mode_cache = minetest.settings:get_bool("creative_mode")
29 function mobs.is_creative(name)
30 return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
31 end
34 -- localize math functions
35 local pi = math.pi
36 local square = math.sqrt
37 local sin = math.sin
38 local cos = math.cos
39 local abs = math.abs
40 local min = math.min
41 local max = math.max
42 local atann = math.atan
43 local random = math.random
44 local floor = math.floor
45 local atan = function(x)
46 if not x or x ~= x then
47 return 0
48 else
49 return atann(x)
50 end
51 end
54 -- Load settings
55 local damage_enabled = minetest.settings:get_bool("enable_damage")
56 local mobs_spawn = minetest.settings:get_bool("mobs_spawn") ~= false
57 local peaceful_only = minetest.settings:get_bool("only_peaceful_mobs")
58 local disable_blood = minetest.settings:get_bool("mobs_disable_blood")
59 local mobs_drop_items = minetest.settings:get_bool("mobs_drop_items") ~= false
60 local mobs_griefing = minetest.settings:get_bool("mobs_griefing") ~= false
61 local creative = minetest.settings:get_bool("creative_mode")
62 local spawn_protected = minetest.settings:get_bool("mobs_spawn_protected") ~= false
63 local remove_far = false
64 local difficulty = tonumber(minetest.settings:get("mob_difficulty")) or 1.0
65 local show_health = false
66 local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 99)
67 local mob_chance_multiplier = tonumber(minetest.settings:get("mob_chance_multiplier") or 1)
69 -- Peaceful mode message so players will know there are no monsters
70 if peaceful_only then
71 minetest.register_on_joinplayer(function(player)
72 minetest.chat_send_player(player:get_player_name(),
73 S("** Peaceful Mode Active - No Monsters Will Spawn"))
74 end)
75 end
77 -- calculate aoc range for mob count
78 local aosrb = tonumber(minetest.settings:get("active_object_send_range_blocks"))
79 local abr = tonumber(minetest.settings:get("active_block_range"))
80 local aoc_range = max(aosrb, abr) * 16
82 -- pathfinding settings
83 local enable_pathfinding = true
84 local stuck_timeout = 3 -- how long before mob gets stuck in place and starts searching
85 local stuck_path_timeout = 10 -- how long will mob follow path before giving up
87 -- default nodes
88 local node_fire = "mcl_fire:fire"
89 local node_permanent_flame = "mcl_fire:eternal_fire"
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
99 -- play sound
100 local mob_sound = function(self, sound)
102 if sound then
103 minetest.sound_play(sound, {
104 object = self.object,
105 gain = 1.0,
106 max_hear_distance = self.sounds.distance
112 -- attack player/mob
113 local do_attack = function(self, player)
115 if self.state == "attack" then
116 return
119 self.attack = player
120 self.state = "attack"
122 if random(0, 100) < 90 then
123 mob_sound(self, self.sounds.war_cry)
128 -- move mob in facing direction
129 local set_velocity = function(self, v)
131 -- do not move if mob has been ordered to stay
132 if self.order == "stand" then
133 self.object:setvelocity({x = 0, y = 0, z = 0})
134 return
137 local yaw = (self.object:get_yaw() or 0) + self.rotate
139 self.object:setvelocity({
140 x = sin(yaw) * -v,
141 y = self.object:getvelocity().y,
142 z = cos(yaw) * v
147 -- calculate mob velocity
148 local get_velocity = function(self)
150 local v = self.object:getvelocity()
152 return (v.x * v.x + v.z * v.z) ^ 0.5
156 -- set and return valid yaw
157 local set_yaw = function(self, yaw, delay)
159 if not yaw or yaw ~= yaw then
160 yaw = 0
163 delay = delay or 0
165 if delay == 0 then
166 self.object:set_yaw(yaw)
167 return yaw
170 self.target_yaw = yaw
171 self.delay = delay
173 return self.target_yaw
176 -- global function to set mob yaw
177 function mobs:yaw(self, yaw, delay)
178 set_yaw(self, yaw, delay)
182 -- set defined animation
183 local set_animation = function(self, anim)
185 if not self.animation
186 or not anim then return end
188 self.animation.current = self.animation.current or ""
190 if anim == self.animation.current
191 or not self.animation[anim .. "_start"]
192 or not self.animation[anim .. "_end"] then
193 return
196 self.animation.current = anim
198 self.object:set_animation({
199 x = self.animation[anim .. "_start"],
200 y = self.animation[anim .. "_end"]},
201 self.animation[anim .. "_speed"] or self.animation.speed_normal or 15,
202 0, self.animation[anim .. "_loop"] ~= false)
206 -- above function exported for mount.lua
207 function mobs:set_animation(self, anim)
208 set_animation(self, anim)
212 -- calculate distance
213 local get_distance = function(a, b)
215 local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z
217 return square(x * x + y * y + z * z)
221 -- check line of sight (BrunoMine)
222 local line_of_sight = function(self, pos1, pos2, stepsize)
224 stepsize = stepsize or 1
226 local s, pos = minetest.line_of_sight(pos1, pos2, stepsize)
228 -- normal walking and flying mobs can see you through air
229 if s == true then
230 return true
233 -- New pos1 to be analyzed
234 local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
236 local r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
238 -- Checks the return
239 if r == true then return true end
241 -- Nodename found
242 local nn = minetest.get_node(pos).name
244 -- Target Distance (td) to travel
245 local td = get_distance(pos1, pos2)
247 -- Actual Distance (ad) traveled
248 local ad = 0
250 -- It continues to advance in the line of sight in search of a real
251 -- obstruction which counts as 'normal' nodebox.
252 while minetest.registered_nodes[nn]
253 and (minetest.registered_nodes[nn].walkable == false
254 or minetest.registered_nodes[nn].drawtype == "nodebox") do
256 -- Check if you can still move forward
257 if td < ad + stepsize then
258 return true -- Reached the target
261 -- Moves the analyzed pos
262 local d = get_distance(pos1, pos2)
264 npos1.x = ((pos2.x - pos1.x) / d * stepsize) + pos1.x
265 npos1.y = ((pos2.y - pos1.y) / d * stepsize) + pos1.y
266 npos1.z = ((pos2.z - pos1.z) / d * stepsize) + pos1.z
268 -- NaN checks
269 if d == 0
270 or npos1.x ~= npos1.x
271 or npos1.y ~= npos1.y
272 or npos1.z ~= npos1.z then
273 return false
276 ad = ad + stepsize
278 -- scan again
279 r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
281 if r == true then return true end
283 -- New Nodename found
284 nn = minetest.get_node(pos).name
288 return false
292 -- are we flying in what we are suppose to? (taikedz)
293 local flight_check = function(self, pos_w)
295 local nod = self.standing_in
296 local def = minetest.registered_nodes[nod]
298 if not def then return false end -- nil check
300 if type(self.fly_in) == "string"
301 and nod == self.fly_in then
303 return true
305 elseif type(self.fly_in) == "table" then
307 for _,fly_in in pairs(self.fly_in) do
309 if nod == fly_in then
311 return true
316 -- stops mobs getting stuck inside stairs and plantlike nodes
317 if def.drawtype ~= "airlike"
318 and def.drawtype ~= "liquid"
319 and def.drawtype ~= "flowingliquid" then
320 return true
323 return false
327 -- custom particle effects
328 local effect = function(pos, amount, texture, min_size, max_size, radius, gravity, glow)
330 radius = radius or 2
331 min_size = min_size or 0.5
332 max_size = max_size or 1
333 gravity = gravity or -10
334 glow = glow or 0
336 minetest.add_particlespawner({
337 amount = amount,
338 time = 0.25,
339 minpos = pos,
340 maxpos = pos,
341 minvel = {x = -radius, y = -radius, z = -radius},
342 maxvel = {x = radius, y = radius, z = radius},
343 minacc = {x = 0, y = gravity, z = 0},
344 maxacc = {x = 0, y = gravity, z = 0},
345 minexptime = 0.1,
346 maxexptime = 1,
347 minsize = min_size,
348 maxsize = max_size,
349 texture = texture,
350 glow = glow,
355 local update_tag = function(self)
356 self.object:set_properties({
357 nametag = self.nametag,
363 -- drop items
364 local item_drop = function(self, cooked)
366 -- no drops if disabled by setting
367 if not mobs_drop_items then return end
369 -- no drops for child mobs
370 if self.child then return end
372 local obj, item, num
373 local pos = self.object:get_pos()
375 self.drops = self.drops or {} -- nil check
377 for n = 1, #self.drops do
379 if random(1, self.drops[n].chance) == 1 then
381 num = random(self.drops[n].min or 1, self.drops[n].max or 1)
382 item = self.drops[n].name
384 -- cook items when true
385 if cooked then
387 local output = minetest.get_craft_result({
388 method = "cooking", width = 1, items = {item}})
390 if output and output.item and not output.item:is_empty() then
391 item = output.item:get_name()
395 -- add item if it exists
396 obj = minetest.add_item(pos, ItemStack(item .. " " .. num))
398 if obj and obj:get_luaentity() then
400 obj:setvelocity({
401 x = random(-10, 10) / 9,
402 y = 6,
403 z = random(-10, 10) / 9,
405 elseif obj then
406 obj:remove() -- item does not exist
411 self.drops = {}
415 -- check if mob is dead or only hurt
416 local check_for_death = function(self, cause, cmi_cause)
418 -- has health actually changed?
419 if self.health == self.old_health and self.health > 0 then
420 return
423 self.old_health = self.health
425 -- still got some health? play hurt sound
426 if self.health > 0 then
428 mob_sound(self, self.sounds.damage)
430 -- make sure health isn't higher than max
431 if self.health > self.hp_max then
432 self.health = self.hp_max
435 -- backup nametag so we can show health stats
436 if not self.nametag2 then
437 self.nametag2 = self.nametag or ""
440 if show_health
441 and (cmi_cause and cmi_cause.type == "punch") then
443 self.htimer = 2
444 self.nametag = "♥ " .. self.health .. " / " .. self.hp_max
446 update_tag(self)
449 return false
452 -- dropped cooked item if mob died in lava
453 if cause == "lava" then
454 item_drop(self, true)
455 else
456 item_drop(self, nil)
459 mob_sound(self, self.sounds.death)
461 local pos = self.object:get_pos()
463 -- execute custom death function
464 if self.on_die then
466 self.on_die(self, pos)
468 if use_cmi then
469 cmi.notify_die(self.object, cmi_cause)
472 self.object:remove()
474 return true
477 -- default death function and die animation (if defined)
478 if self.animation
479 and self.animation.die_start
480 and self.animation.die_end then
482 local frames = self.animation.die_end - self.animation.die_start
483 local speed = self.animation.die_speed or 15
484 local length = max(frames / speed, 0)
486 self.attack = nil
487 self.v_start = false
488 self.timer = 0
489 self.blinktimer = 0
490 self.passive = true
491 self.state = "die"
492 set_velocity(self, 0)
493 set_animation(self, "die")
495 minetest.after(length, function(self)
496 if not self.object:get_luaentity() then
497 return
499 if use_cmi then
500 cmi.notify_die(self.object, cmi_cause)
503 self.object:remove()
504 end, self)
505 else
507 if use_cmi then
508 cmi.notify_die(self.object, cmi_cause)
511 self.object:remove()
514 effect(pos, 20, "tnt_smoke.png")
516 return true
520 -- check if within physical map limits (-30911 to 30927)
521 local within_limits = function(pos, radius)
523 if (pos.x - radius) > -30913
524 and (pos.x + radius) < 30928
525 and (pos.y - radius) > -30913
526 and (pos.y + radius) < 30928
527 and (pos.z - radius) > -30913
528 and (pos.z + radius) < 30928 then
529 return true -- within limits
532 return false -- beyond limits
536 -- is mob facing a cliff
537 local is_at_cliff = function(self)
539 if self.fear_height == 0 then -- 0 for no falling protection!
540 return false
543 local yaw = self.object:get_yaw()
544 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
545 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
546 local pos = self.object:get_pos()
547 local ypos = pos.y + self.collisionbox[2] -- just above floor
549 if minetest.line_of_sight(
550 {x = pos.x + dir_x, y = ypos, z = pos.z + dir_z},
551 {x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z}
552 , 1) then
554 return true
557 return false
561 -- get node but use fallback for nil or unknown
562 local node_ok = function(pos, fallback)
564 fallback = fallback or mobs.fallback_node
566 local node = minetest.get_node_or_nil(pos)
568 if node and minetest.registered_nodes[node.name] then
569 return node
572 return minetest.registered_nodes[fallback]
576 -- environmental damage (water, lava, fire, light etc.)
577 local do_env_damage = function(self)
579 -- feed/tame text timer (so mob 'full' messages dont spam chat)
580 if self.htimer > 0 then
581 self.htimer = self.htimer - 1
584 -- reset nametag after showing health stats
585 if self.htimer < 1 and self.nametag2 then
587 self.nametag = self.nametag2
588 self.nametag2 = nil
590 update_tag(self)
593 local pos = self.object:get_pos()
595 self.time_of_day = minetest.get_timeofday()
597 -- remove mob if beyond map limits
598 if not within_limits(pos, 0) then
599 self.object:remove()
600 return
604 local deal_light_damage = function(self, pos, damage)
605 if not (mod_weather and (mcl_weather.rain.raining or mcl_weather.state == "snow") and mcl_weather.is_outdoor(pos)) then
606 self.health = self.health - damage
608 effect(pos, 5, "tnt_smoke.png")
610 if check_for_death(self, "light", {type = "light"}) then return end
614 -- bright light harms mob
615 if self.light_damage ~= 0 and (minetest.get_node_light(pos) or 0) > 12 then
616 deal_light_damage(self, pos, self.light_damage)
618 local _, dim = mcl_worlds.y_to_layer(pos.y)
619 if self.sunlight_damage ~= 0 and (minetest.get_node_light(pos) or 0) >= minetest.LIGHT_MAX and dim == "overworld" then
620 deal_light_damage(self, pos, self.sunlight_damage)
623 local y_level = self.collisionbox[2]
625 if self.child then
626 y_level = self.collisionbox[2] * 0.5
629 -- what is mob standing in?
630 pos.y = pos.y + y_level + 0.25 -- foot level
631 self.standing_in = node_ok(pos, "air").name
633 -- don't fall when on ignore, just stand still
634 if self.standing_in == "ignore" then
635 self.object:setvelocity({x = 0, y = 0, z = 0})
638 local nodef = minetest.registered_nodes[self.standing_in]
640 -- rain
641 if self.rain_damage and mod_weather then
642 if mcl_weather.rain.raining and mcl_weather.is_outdoor(pos) then
644 self.health = self.health - self.rain_damage
646 if check_for_death(self, "rain", {type = "environment",
647 pos = pos, node = self.standing_in}) then return end
651 pos.y = pos.y + 1 -- for particle effect position
653 -- water
654 if self.water_damage
655 and nodef.groups.water then
657 if self.water_damage ~= 0 then
659 self.health = self.health - self.water_damage
661 effect(pos, 5, "bubble.png", nil, nil, 1, nil)
663 if check_for_death(self, "water", {type = "environment",
664 pos = pos, node = self.standing_in}) then return end
667 -- lava or fire
668 elseif self.lava_damage
669 and (nodef.groups.lava
670 or self.standing_in == node_fire
671 or self.standing_in == node_permanent_flame) then
673 if self.lava_damage ~= 0 then
675 self.health = self.health - self.lava_damage
677 effect(pos, 5, "fire_basic_flame.png", nil, nil, 1, nil)
679 if check_for_death(self, "lava", {type = "environment",
680 pos = pos, node = self.standing_in}) then return end
683 -- damage_per_second node check
684 elseif nodef.damage_per_second ~= 0 then
686 self.health = self.health - nodef.damage_per_second
688 effect(pos, 5, "tnt_smoke.png")
690 if check_for_death(self, "dps", {type = "environment",
691 pos = pos, node = self.standing_in}) then return end
693 --[[
694 --- suffocation inside solid node
695 if self.suffocation ~= 0
696 and nodef.walkable == true
697 and nodef.groups.disable_suffocation ~= 1
698 and nodef.drawtype == "normal" then
700 self.health = self.health - self.suffocation
702 if check_for_death(self, "suffocation", {type = "environment",
703 pos = pos, node = self.standing_in}) then return end
706 check_for_death(self, "", {type = "unknown"})
710 -- jump if facing a solid node (not fences or gates)
711 local do_jump = function(self)
713 if not self.jump
714 or self.jump_height == 0
715 or self.fly
716 or self.child
717 or self.order == "stand" then
718 return false
721 self.facing_fence = false
723 -- something stopping us while moving?
724 if self.state ~= "stand"
725 and get_velocity(self) > 0.5
726 and self.object:getvelocity().y ~= 0 then
727 return false
730 local pos = self.object:get_pos()
731 local yaw = self.object:get_yaw()
733 -- what is mob standing on?
734 pos.y = pos.y + self.collisionbox[2] - 0.2
736 local nod = node_ok(pos)
738 if minetest.registered_nodes[nod.name].walkable == false then
739 return false
742 -- where is front
743 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
744 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
746 -- what is in front of mob?
747 local nod = node_ok({
748 x = pos.x + dir_x,
749 y = pos.y + 0.5,
750 z = pos.z + dir_z
753 -- thin blocks that do not need to be jumped
754 if nod.name == node_snow then
755 return false
758 if self.walk_chance == 0
759 or minetest.registered_items[nod.name].walkable then
761 if not nod.name:find("fence")
762 and not nod.name:find("gate") then
764 local v = self.object:getvelocity()
766 v.y = self.jump_height
768 set_animation(self, "jump") -- only when defined
770 self.object:setvelocity(v)
772 -- when in air move forward
773 minetest.after(0.3, function(self, v)
774 if not self.object:get_luaentity() then
775 return
777 self.object:set_acceleration({
778 x = v.x * 2,
779 y = 0,
780 z = v.z * 2,
782 end, self, v)
784 if get_velocity(self) > 0 then
785 mob_sound(self, self.sounds.jump)
787 else
788 self.facing_fence = true
791 return true
794 return false
798 -- blast damage to entities nearby (modified from TNT mod)
799 local entity_physics = function(pos, radius)
801 radius = radius * 2
803 local objs = minetest.get_objects_inside_radius(pos, radius)
804 local obj_pos, dist
806 for n = 1, #objs do
808 obj_pos = objs[n]:get_pos()
810 dist = get_distance(pos, obj_pos)
811 if dist < 1 then dist = 1 end
813 local damage = floor((4 / dist) * radius)
814 local ent = objs[n]:get_luaentity()
816 -- punches work on entities AND players
817 objs[n]:punch(objs[n], 1.0, {
818 full_punch_interval = 1.0,
819 damage_groups = {fleshy = damage},
820 }, pos)
825 -- should mob follow what I'm holding ?
826 local follow_holding = function(self, clicker)
828 if mobs.invis[clicker:get_player_name()] then
829 return false
832 local item = clicker:get_wielded_item()
833 local t = type(self.follow)
835 -- single item
836 if t == "string"
837 and item:get_name() == self.follow then
838 return true
840 -- multiple items
841 elseif t == "table" then
843 for no = 1, #self.follow do
845 if self.follow[no] == item:get_name() then
846 return true
851 return false
855 -- find two animals of same type and breed if nearby and horny
856 local breed = function(self)
858 -- child takes 240 seconds before growing into adult
859 if self.child == true then
861 self.hornytimer = self.hornytimer + 1
863 if self.hornytimer > 240 then
865 self.child = false
866 self.hornytimer = 0
868 self.object:set_properties({
869 textures = self.base_texture,
870 mesh = self.base_mesh,
871 visual_size = self.base_size,
872 collisionbox = self.base_colbox,
873 selectionbox = self.base_selbox,
876 -- custom function when child grows up
877 if self.on_grown then
878 self.on_grown(self)
879 else
880 -- jump when fully grown so as not to fall into ground
881 self.object:setvelocity({
882 x = 0,
883 y = self.jump_height,
884 z = 0
889 return
892 -- horny animal can mate for 40 seconds,
893 -- afterwards horny animal cannot mate again for 200 seconds
894 if self.horny == true
895 and self.hornytimer < 240 then
897 self.hornytimer = self.hornytimer + 1
899 if self.hornytimer >= 240 then
900 self.hornytimer = 0
901 self.horny = false
905 -- find another same animal who is also horny and mate if nearby
906 if self.horny == true
907 and self.hornytimer <= 40 then
909 local pos = self.object:get_pos()
911 effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, "heart.png", 3, 4, 1, 0.1)
913 local objs = minetest.get_objects_inside_radius(pos, 3)
914 local num = 0
915 local ent = nil
917 for n = 1, #objs do
919 ent = objs[n]:get_luaentity()
921 -- check for same animal with different colour
922 local canmate = false
924 if ent then
926 if ent.name == self.name then
927 canmate = true
928 else
929 local entname = string.split(ent.name,":")
930 local selfname = string.split(self.name,":")
932 if entname[1] == selfname[1] then
933 entname = string.split(entname[2],"_")
934 selfname = string.split(selfname[2],"_")
936 if entname[1] == selfname[1] then
937 canmate = true
943 if ent
944 and canmate == true
945 and ent.horny == true
946 and ent.hornytimer <= 40 then
947 num = num + 1
950 -- found your mate? then have a baby
951 if num > 1 then
953 self.hornytimer = 41
954 ent.hornytimer = 41
956 -- spawn baby
957 minetest.after(5, function(parent1, parent2, pos)
958 if not parent1.object:get_luaentity() then
959 return
961 if not parent2.object:get_luaentity() then
962 return
965 -- custom breed function
966 if parent1.on_breed then
967 -- when false, skip going any further
968 if parent1.on_breed(parent1, parent2) == false then
969 return
973 local child = mobs:spawn_child(pos, parent1.name)
975 local ent_c = child:get_luaentity()
978 -- Use texture of one of the parents
979 local p = math.random(1, 2)
980 if p == 1 then
981 ent_c.base_texture = parent1.base_texture
982 else
983 ent_c.base_texture = parent2.base_texture
985 child:set_properties({
986 textures = ent_c.base_texture
989 -- tamed and owned by parents' owner
990 ent_c.tamed = true
991 ent_c.owner = parent1.owner
992 end, self, ent, pos)
994 num = 0
996 break
1003 -- find and replace what mob is looking for (grass, wheat etc.)
1004 local replace = function(self, pos)
1006 if not mobs_griefing
1007 or not self.replace_rate
1008 or not self.replace_what
1009 or self.child == true
1010 or self.object:getvelocity().y ~= 0
1011 or random(1, self.replace_rate) > 1 then
1012 return
1015 local what, with, y_offset
1017 if type(self.replace_what[1]) == "table" then
1019 local num = random(#self.replace_what)
1021 what = self.replace_what[num][1] or ""
1022 with = self.replace_what[num][2] or ""
1023 y_offset = self.replace_what[num][3] or 0
1024 else
1025 what = self.replace_what
1026 with = self.replace_with or ""
1027 y_offset = self.replace_offset or 0
1030 pos.y = pos.y + y_offset
1032 if #minetest.find_nodes_in_area(pos, pos, what) > 0 then
1034 local oldnode = {name = what}
1035 local newnode = {name = with}
1036 local on_replace_return
1038 if self.on_replace then
1039 on_replace_return = self.on_replace(self, pos, oldnode, newnode)
1042 if on_replace_return ~= false then
1044 minetest.set_node(pos, {name = with})
1046 -- when cow/sheep eats grass, replace wool and milk
1047 if self.gotten == true then
1048 self.gotten = false
1049 self.object:set_properties(self)
1056 -- check if daytime and also if mob is docile during daylight hours
1057 local day_docile = function(self)
1059 if self.docile_by_day == false then
1061 return false
1063 elseif self.docile_by_day == true
1064 and self.time_of_day > 0.2
1065 and self.time_of_day < 0.8 then
1067 return true
1072 local los_switcher = false
1073 local height_switcher = false
1075 -- path finding and smart mob routine by rnd, line_of_sight and other edits by Elkien3
1076 local smart_mobs = function(self, s, p, dist, dtime)
1078 local s1 = self.path.lastpos
1080 local target_pos = self.attack:get_pos()
1082 -- is it becoming stuck?
1083 if abs(s1.x - s.x) + abs(s1.z - s.z) < .5 then
1084 self.path.stuck_timer = self.path.stuck_timer + dtime
1085 else
1086 self.path.stuck_timer = 0
1089 self.path.lastpos = {x = s.x, y = s.y, z = s.z}
1091 local use_pathfind = false
1092 local has_lineofsight = minetest.line_of_sight(
1093 {x = s.x, y = (s.y) + .5, z = s.z},
1094 {x = target_pos.x, y = (target_pos.y) + 1.5, z = target_pos.z}, .2)
1096 -- im stuck, search for path
1097 if not has_lineofsight then
1099 if los_switcher == true then
1100 use_pathfind = true
1101 los_switcher = false
1102 end -- cannot see target!
1103 else
1104 if los_switcher == false then
1106 los_switcher = true
1107 use_pathfind = false
1109 minetest.after(1, function(self)
1110 if not self.object:get_luaentity() then
1111 return
1113 if has_lineofsight then self.path.following = false end
1114 end, self)
1115 end -- can see target!
1118 if (self.path.stuck_timer > stuck_timeout and not self.path.following) then
1120 use_pathfind = true
1121 self.path.stuck_timer = 0
1123 minetest.after(1, function(self)
1124 if not self.object:get_luaentity() then
1125 return
1127 if has_lineofsight then self.path.following = false end
1128 end, self)
1131 if (self.path.stuck_timer > stuck_path_timeout and self.path.following) then
1133 use_pathfind = true
1134 self.path.stuck_timer = 0
1136 minetest.after(1, function(self)
1137 if not self.object:get_luaentity() then
1138 return
1140 if has_lineofsight then self.path.following = false end
1141 end, self)
1144 if math.abs(vector.subtract(s,target_pos).y) > self.stepheight then
1146 if height_switcher then
1147 use_pathfind = true
1148 height_switcher = false
1150 else
1151 if not height_switcher then
1152 use_pathfind = false
1153 height_switcher = true
1157 if use_pathfind then
1158 -- lets try find a path, first take care of positions
1159 -- since pathfinder is very sensitive
1160 local sheight = self.collisionbox[5] - self.collisionbox[2]
1162 -- round position to center of node to avoid stuck in walls
1163 -- also adjust height for player models!
1164 s.x = floor(s.x + 0.5)
1165 s.z = floor(s.z + 0.5)
1167 local ssight, sground = minetest.line_of_sight(s, {
1168 x = s.x, y = s.y - 4, z = s.z}, 1)
1170 -- determine node above ground
1171 if not ssight then
1172 s.y = sground.y + 1
1175 local p1 = self.attack:get_pos()
1177 p1.x = floor(p1.x + 0.5)
1178 p1.y = floor(p1.y + 0.5)
1179 p1.z = floor(p1.z + 0.5)
1181 local dropheight = 6
1182 if self.fear_height ~= 0 then dropheight = self.fear_height end
1184 self.path.way = minetest.find_path(s, p1, 16, self.stepheight, dropheight, "A*_noprefetch")
1186 self.state = ""
1187 do_attack(self, self.attack)
1189 -- no path found, try something else
1190 if not self.path.way then
1192 self.path.following = false
1194 -- lets make way by digging/building if not accessible
1195 if self.pathfinding == 2 and mobs_griefing then
1197 -- is player higher than mob?
1198 if s.y < p1.y then
1200 -- build upwards
1201 if not minetest.is_protected(s, "") then
1203 local ndef1 = minetest.registered_nodes[self.standing_in]
1205 if ndef1 and (ndef1.buildable_to or ndef1.groups.liquid) then
1207 minetest.set_node(s, {name = mobs.fallback_node})
1211 local sheight = math.ceil(self.collisionbox[5]) + 1
1213 -- assume mob is 2 blocks high so it digs above its head
1214 s.y = s.y + sheight
1216 -- remove one block above to make room to jump
1217 if not minetest.is_protected(s, "") then
1219 local node1 = node_ok(s, "air").name
1220 local ndef1 = minetest.registered_nodes[node1]
1222 if node1 ~= "air"
1223 and node1 ~= "ignore"
1224 and ndef1
1225 and not ndef1.groups.level
1226 and not ndef1.groups.unbreakable
1227 and not ndef1.groups.liquid then
1229 minetest.set_node(s, {name = "air"})
1230 minetest.add_item(s, ItemStack(node1))
1235 s.y = s.y - sheight
1236 self.object:setpos({x = s.x, y = s.y + 2, z = s.z})
1238 else -- dig 2 blocks to make door toward player direction
1240 local yaw1 = self.object:get_yaw() + pi / 2
1241 local p1 = {
1242 x = s.x + cos(yaw1),
1243 y = s.y,
1244 z = s.z + sin(yaw1)
1247 if not minetest.is_protected(p1, "") then
1249 local node1 = node_ok(p1, "air").name
1250 local ndef1 = minetest.registered_nodes[node1]
1252 if node1 ~= "air"
1253 and node1 ~= "ignore"
1254 and ndef1
1255 and not ndef1.groups.level
1256 and not ndef1.groups.unbreakable
1257 and not ndef1.groups.liquid then
1259 minetest.add_item(p1, ItemStack(node1))
1260 minetest.set_node(p1, {name = "air"})
1263 p1.y = p1.y + 1
1264 node1 = node_ok(p1, "air").name
1265 ndef1 = minetest.registered_nodes[node1]
1267 if node1 ~= "air"
1268 and node1 ~= "ignore"
1269 and ndef1
1270 and not ndef1.groups.level
1271 and not ndef1.groups.unbreakable
1272 and not ndef1.groups.liquid then
1274 minetest.add_item(p1, ItemStack(node1))
1275 minetest.set_node(p1, {name = "air"})
1282 -- will try again in 2 second
1283 self.path.stuck_timer = stuck_timeout - 2
1285 -- frustration! cant find the damn path :(
1286 mob_sound(self, self.sounds.random)
1287 else
1288 -- yay i found path
1289 mob_sound(self, self.sounds.war_cry)
1290 set_velocity(self, self.walk_velocity)
1292 -- follow path now that it has it
1293 self.path.following = true
1299 -- specific attacks
1300 local specific_attack = function(list, what)
1302 -- no list so attack default (player, animals etc.)
1303 if list == nil then
1304 return true
1307 -- found entity on list to attack?
1308 for no = 1, #list do
1310 if list[no] == what then
1311 return true
1315 return false
1319 -- monster find someone to attack
1320 local monster_attack = function(self)
1322 if self.type ~= "monster"
1323 or not damage_enabled
1324 or creative
1325 or self.state == "attack"
1326 or day_docile(self) then
1327 return
1330 local s = self.object:get_pos()
1331 local p, sp, dist
1332 local player, obj, min_player
1333 local type, name = "", ""
1334 local min_dist = self.view_range + 1
1335 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1337 for n = 1, #objs do
1339 if objs[n]:is_player() then
1341 if mobs.invis[ objs[n]:get_player_name() ] then
1343 type = ""
1344 else
1345 player = objs[n]
1346 type = "player"
1347 name = "player"
1349 else
1350 obj = objs[n]:get_luaentity()
1352 if obj then
1353 player = obj.object
1354 type = obj.type
1355 name = obj.name or ""
1359 -- find specific mob to attack, failing that attack player/npc/animal
1360 if specific_attack(self.specific_attack, name)
1361 and (type == "player" or type == "npc"
1362 or (type == "animal" and self.attack_animals == true)) then
1364 p = player:get_pos()
1365 sp = s
1367 dist = get_distance(p, s)
1369 -- aim higher to make looking up hills more realistic
1370 p.y = p.y + 1
1371 sp.y = sp.y + 1
1374 -- choose closest player to attack
1375 if dist < min_dist
1376 and line_of_sight(self, sp, p, 2) == true then
1377 min_dist = dist
1378 min_player = player
1383 -- attack player
1384 if min_player then
1385 do_attack(self, min_player)
1390 -- npc, find closest monster to attack
1391 local npc_attack = function(self)
1393 if self.type ~= "npc"
1394 or not self.attacks_monsters
1395 or self.state == "attack" then
1396 return
1399 local p, sp, obj, min_player
1400 local s = self.object:get_pos()
1401 local min_dist = self.view_range + 1
1402 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1404 for n = 1, #objs do
1406 obj = objs[n]:get_luaentity()
1408 if obj and obj.type == "monster" then
1410 p = obj.object:get_pos()
1411 sp = s
1413 dist = get_distance(p, s)
1415 -- aim higher to make looking up hills more realistic
1416 p.y = p.y + 1
1417 sp.y = sp.y + 1
1419 if dist < min_dist
1420 and line_of_sight(self, sp, p, 2) == true then
1421 min_dist = dist
1422 min_player = obj.object
1427 if min_player then
1428 do_attack(self, min_player)
1433 -- specific runaway
1434 local specific_runaway = function(list, what)
1436 -- no list so do not run
1437 if list == nil then
1438 return false
1441 -- found entity on list to attack?
1442 for no = 1, #list do
1444 if list[no] == what then
1445 return true
1449 return false
1453 -- find someone to runaway from
1454 local runaway_from = function(self)
1456 if not self.runaway_from then
1457 return
1460 local s = self.object:get_pos()
1461 local p, sp, dist
1462 local player, obj, min_player
1463 local type, name = "", ""
1464 local min_dist = self.view_range + 1
1465 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1467 for n = 1, #objs do
1469 if objs[n]:is_player() then
1471 if mobs.invis[ objs[n]:get_player_name() ]
1472 or self.owner == objs[n]:get_player_name() then
1474 type = ""
1475 else
1476 player = objs[n]
1477 type = "player"
1478 name = "player"
1480 else
1481 obj = objs[n]:get_luaentity()
1483 if obj then
1484 player = obj.object
1485 type = obj.type
1486 name = obj.name or ""
1490 -- find specific mob to runaway from
1491 if name ~= "" and name ~= self.name
1492 and specific_runaway(self.runaway_from, name) then
1494 p = player:get_pos()
1495 sp = s
1497 -- aim higher to make looking up hills more realistic
1498 p.y = p.y + 1
1499 sp.y = sp.y + 1
1501 dist = get_distance(p, s)
1504 -- choose closest player/mpb to runaway from
1505 if dist < min_dist
1506 and line_of_sight(self, sp, p, 2) == true then
1507 min_dist = dist
1508 min_player = player
1513 if min_player then
1515 local lp = player:get_pos()
1516 local vec = {
1517 x = lp.x - s.x,
1518 y = lp.y - s.y,
1519 z = lp.z - s.z
1522 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
1524 if lp.x > s.x then
1525 yaw = yaw + pi
1528 yaw = set_yaw(self, yaw, 4)
1529 self.state = "runaway"
1530 self.runaway_timer = 3
1531 self.following = nil
1536 -- follow player if owner or holding item, if fish outta water then flop
1537 local follow_flop = function(self)
1539 -- find player to follow
1540 if (self.follow ~= ""
1541 or self.order == "follow")
1542 and not self.following
1543 and self.state ~= "attack"
1544 and self.state ~= "runaway" then
1546 local s = self.object:get_pos()
1547 local players = minetest.get_connected_players()
1549 for n = 1, #players do
1551 if get_distance(players[n]:get_pos(), s) < self.view_range
1552 and not mobs.invis[ players[n]:get_player_name() ] then
1554 self.following = players[n]
1556 break
1561 if self.type == "npc"
1562 and self.order == "follow"
1563 and self.state ~= "attack"
1564 and self.owner ~= "" then
1566 -- npc stop following player if not owner
1567 if self.following
1568 and self.owner
1569 and self.owner ~= self.following:get_player_name() then
1570 self.following = nil
1572 else
1573 -- stop following player if not holding specific item
1574 if self.following
1575 and self.following:is_player()
1576 and follow_holding(self, self.following) == false then
1577 self.following = nil
1582 -- follow that thing
1583 if self.following then
1585 local s = self.object:get_pos()
1586 local p
1588 if self.following:is_player() then
1590 p = self.following:get_pos()
1592 elseif self.following.object then
1594 p = self.following.object:get_pos()
1597 if p then
1599 local dist = get_distance(p, s)
1601 -- dont follow if out of range
1602 if dist > self.view_range then
1603 self.following = nil
1604 else
1605 local vec = {
1606 x = p.x - s.x,
1607 z = p.z - s.z
1610 local yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1612 if p.x > s.x then yaw = yaw + pi end
1614 yaw = set_yaw(self, yaw, 6)
1616 -- anyone but standing npc's can move along
1617 if dist > self.reach
1618 and self.order ~= "stand" then
1620 set_velocity(self, self.walk_velocity)
1622 if self.walk_chance ~= 0 then
1623 set_animation(self, "walk")
1625 else
1626 set_velocity(self, 0)
1627 set_animation(self, "stand")
1630 return
1635 -- swimmers flop when out of their element, and swim again when back in
1636 if self.fly then
1637 local s = self.object:get_pos()
1638 if not flight_check(self, s) then
1640 self.state = "flop"
1641 self.object:setvelocity({x = 0, y = -5, z = 0})
1643 set_animation(self, "stand")
1645 return
1646 elseif self.state == "flop" then
1647 self.state = "stand"
1653 -- dogshoot attack switch and counter function
1654 local dogswitch = function(self, dtime)
1656 -- switch mode not activated
1657 if not self.dogshoot_switch
1658 or not dtime then
1659 return 0
1662 self.dogshoot_count = self.dogshoot_count + dtime
1664 if (self.dogshoot_switch == 1
1665 and self.dogshoot_count > self.dogshoot_count_max)
1666 or (self.dogshoot_switch == 2
1667 and self.dogshoot_count > self.dogshoot_count2_max) then
1669 self.dogshoot_count = 0
1671 if self.dogshoot_switch == 1 then
1672 self.dogshoot_switch = 2
1673 else
1674 self.dogshoot_switch = 1
1678 return self.dogshoot_switch
1682 -- execute current state (stand, walk, run, attacks)
1683 local do_states = function(self, dtime)
1685 local yaw = self.object:get_yaw() or 0
1687 if self.state == "stand" then
1689 if random(1, 4) == 1 then
1691 local lp = nil
1692 local s = self.object:get_pos()
1693 local objs = minetest.get_objects_inside_radius(s, 3)
1695 for n = 1, #objs do
1697 if objs[n]:is_player() then
1698 lp = objs[n]:get_pos()
1699 break
1703 -- look at any players nearby, otherwise turn randomly
1704 if lp then
1706 local vec = {
1707 x = lp.x - s.x,
1708 z = lp.z - s.z
1711 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1713 if lp.x > s.x then yaw = yaw + pi end
1714 else
1715 yaw = yaw + random(-0.5, 0.5)
1718 yaw = set_yaw(self, yaw, 8)
1721 set_velocity(self, 0)
1722 set_animation(self, "stand")
1724 -- npc's ordered to stand stay standing
1725 if self.type ~= "npc"
1726 or self.order ~= "stand" then
1728 if self.walk_chance ~= 0
1729 and self.facing_fence ~= true
1730 and random(1, 100) <= self.walk_chance
1731 and is_at_cliff(self) == false then
1733 set_velocity(self, self.walk_velocity)
1734 self.state = "walk"
1735 set_animation(self, "walk")
1739 elseif self.state == "walk" then
1741 local s = self.object:get_pos()
1742 local lp = nil
1744 -- is there something I need to avoid?
1745 if self.water_damage > 0
1746 and self.lava_damage > 0 then
1748 lp = minetest.find_node_near(s, 1, {"group:water", "group:lava"})
1750 elseif self.water_damage > 0 then
1752 lp = minetest.find_node_near(s, 1, {"group:water"})
1754 elseif self.lava_damage > 0 then
1756 lp = minetest.find_node_near(s, 1, {"group:lava"})
1759 if lp then
1761 -- if mob in water or lava then look for land
1762 if (self.lava_damage
1763 and minetest.registered_nodes[self.standing_in].groups.lava)
1764 or (self.water_damage
1765 and minetest.registered_nodes[self.standing_in].groups.water) then
1767 lp = minetest.find_node_near(s, 5, {"group:soil", "group:stone",
1768 "group:sand", node_ice, node_snowblock})
1770 -- did we find land?
1771 if lp then
1773 local vec = {
1774 x = lp.x - s.x,
1775 z = lp.z - s.z
1778 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1780 if lp.x > s.x then yaw = yaw + pi end
1782 -- look towards land and jump/move in that direction
1783 yaw = set_yaw(self, yaw, 6)
1784 do_jump(self)
1785 set_velocity(self, self.walk_velocity)
1786 else
1787 yaw = yaw + random(-0.5, 0.5)
1790 else
1792 local vec = {
1793 x = lp.x - s.x,
1794 z = lp.z - s.z
1797 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1799 if lp.x > s.x then yaw = yaw + pi end
1802 yaw = set_yaw(self, yaw, 8)
1804 -- otherwise randomly turn
1805 elseif random(1, 100) <= 30 then
1807 yaw = yaw + random(-0.5, 0.5)
1809 yaw = set_yaw(self, yaw, 8)
1812 -- stand for great fall in front
1813 local temp_is_cliff = is_at_cliff(self)
1815 if self.facing_fence == true
1816 or temp_is_cliff
1817 or random(1, 100) <= 30 then
1819 set_velocity(self, 0)
1820 self.state = "stand"
1821 set_animation(self, "stand")
1822 else
1823 set_velocity(self, self.walk_velocity)
1825 if flight_check(self)
1826 and self.animation
1827 and self.animation.fly_start
1828 and self.animation.fly_end then
1829 set_animation(self, "fly")
1830 else
1831 set_animation(self, "walk")
1835 -- runaway when punched
1836 elseif self.state == "runaway" then
1838 self.runaway_timer = self.runaway_timer + 1
1840 -- stop after 5 seconds or when at cliff
1841 if self.runaway_timer > 5
1842 or is_at_cliff(self) then
1843 self.runaway_timer = 0
1844 set_velocity(self, 0)
1845 self.state = "stand"
1846 set_animation(self, "stand")
1847 else
1848 set_velocity(self, self.run_velocity)
1849 set_animation(self, "walk")
1852 -- attack routines (explode, dogfight, shoot, dogshoot)
1853 elseif self.state == "attack" then
1855 -- calculate distance from mob and enemy
1856 local s = self.object:get_pos()
1857 local p = self.attack:get_pos() or s
1858 local dist = get_distance(p, s)
1860 -- stop attacking if player invisible or out of range
1861 if dist > self.view_range
1862 or not self.attack
1863 or not self.attack:get_pos()
1864 or self.attack:get_hp() <= 0
1865 or (self.attack:is_player() and mobs.invis[ self.attack:get_player_name() ]) then
1867 self.state = "stand"
1868 set_velocity(self, 0)
1869 set_animation(self, "stand")
1870 self.attack = nil
1871 self.v_start = false
1872 self.timer = 0
1873 self.blinktimer = 0
1874 self.path.way = nil
1876 return
1879 if self.attack_type == "explode" then
1881 local vec = {
1882 x = p.x - s.x,
1883 z = p.z - s.z
1886 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1888 if p.x > s.x then yaw = yaw + pi end
1890 yaw = set_yaw(self, yaw)
1892 local node_break_radius = self.explosion_radius or 1
1893 local entity_damage_radius = self.explosion_damage_radius
1894 or (node_break_radius * 2)
1896 -- start timer when in reach and line of sight
1897 if not self.v_start
1898 and dist <= self.reach
1899 and line_of_sight(self, s, p, 2) then
1901 self.v_start = true
1902 self.timer = 0
1903 self.blinktimer = 0
1904 mob_sound(self, self.sounds.fuse)
1906 -- stop timer if out of reach or direct line of sight
1907 elseif self.allow_fuse_reset
1908 and self.v_start
1909 and (dist > self.reach
1910 or not line_of_sight(self, s, p, 2)) then
1911 self.v_start = false
1912 self.timer = 0
1913 self.blinktimer = 0
1914 self.blinkstatus = false
1915 self.object:settexturemod("")
1918 -- walk right up to player unless the timer is active
1919 if self.v_start and (self.stop_to_explode or dist < 1.5) then
1920 set_velocity(self, 0)
1921 else
1922 set_velocity(self, self.run_velocity)
1925 if self.animation and self.animation.run_start then
1926 set_animation(self, "run")
1927 else
1928 set_animation(self, "walk")
1931 if self.v_start then
1933 self.timer = self.timer + dtime
1934 self.blinktimer = (self.blinktimer or 0) + dtime
1936 if self.blinktimer > 0.2 then
1938 self.blinktimer = 0
1940 if self.blinkstatus then
1941 self.object:settexturemod("")
1942 else
1943 self.object:settexturemod("^[brighten")
1946 self.blinkstatus = not self.blinkstatus
1949 if self.timer > self.explosion_timer then
1951 local pos = self.object:get_pos()
1953 -- dont damage anything if area protected or next to water
1954 if minetest.find_node_near(pos, 1, {"group:water"})
1955 or minetest.is_protected(pos, "") then
1957 node_break_radius = 1
1960 self.object:remove()
1962 if mobs_griefing and mod_tnt and tnt and tnt.boom
1963 and not minetest.is_protected(pos, "") then
1965 tnt.boom(pos, {
1966 radius = node_break_radius,
1967 damage_radius = entity_damage_radius,
1968 sound = self.sounds.explode,
1970 else
1972 minetest.sound_play(self.sounds.explode, {
1973 pos = pos,
1974 gain = 1.0,
1975 max_hear_distance = self.sounds.distance or 32
1978 entity_physics(pos, entity_damage_radius)
1979 effect(pos, 32, "tnt_smoke.png", nil, nil, node_break_radius, 1, 0)
1982 return
1986 elseif self.attack_type == "dogfight"
1987 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 2)
1988 or (self.attack_type == "dogshoot" and dist <= self.reach and dogswitch(self) == 0) then
1990 if self.fly
1991 and dist > self.reach then
1993 local p1 = s
1994 local me_y = floor(p1.y)
1995 local p2 = p
1996 local p_y = floor(p2.y + 1)
1997 local v = self.object:getvelocity()
1999 if flight_check(self, s) then
2001 if me_y < p_y then
2003 self.object:setvelocity({
2004 x = v.x,
2005 y = 1 * self.walk_velocity,
2006 z = v.z
2009 elseif me_y > p_y then
2011 self.object:setvelocity({
2012 x = v.x,
2013 y = -1 * self.walk_velocity,
2014 z = v.z
2017 else
2018 if me_y < p_y then
2020 self.object:setvelocity({
2021 x = v.x,
2022 y = 0.01,
2023 z = v.z
2026 elseif me_y > p_y then
2028 self.object:setvelocity({
2029 x = v.x,
2030 y = -0.01,
2031 z = v.z
2038 -- rnd: new movement direction
2039 if self.path.following
2040 and self.path.way
2041 and self.attack_type ~= "dogshoot" then
2043 -- no paths longer than 50
2044 if #self.path.way > 50
2045 or dist < self.reach then
2046 self.path.following = false
2047 return
2050 local p1 = self.path.way[1]
2052 if not p1 then
2053 self.path.following = false
2054 return
2057 if abs(p1.x-s.x) + abs(p1.z - s.z) < 0.6 then
2058 -- reached waypoint, remove it from queue
2059 table.remove(self.path.way, 1)
2062 -- set new temporary target
2063 p = {x = p1.x, y = p1.y, z = p1.z}
2066 local vec = {
2067 x = p.x - s.x,
2068 z = p.z - s.z
2071 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2073 if p.x > s.x then yaw = yaw + pi end
2075 yaw = set_yaw(self, yaw)
2077 -- move towards enemy if beyond mob reach
2078 if dist > self.reach then
2080 -- path finding by rnd
2081 if self.pathfinding -- only if mob has pathfinding enabled
2082 and enable_pathfinding then
2084 smart_mobs(self, s, p, dist, dtime)
2087 if is_at_cliff(self) then
2089 set_velocity(self, 0)
2090 set_animation(self, "stand")
2091 else
2093 if self.path.stuck then
2094 set_velocity(self, self.walk_velocity)
2095 else
2096 set_velocity(self, self.run_velocity)
2099 if self.animation and self.animation.run_start then
2100 set_animation(self, "run")
2101 else
2102 set_animation(self, "walk")
2106 else -- rnd: if inside reach range
2108 self.path.stuck = false
2109 self.path.stuck_timer = 0
2110 self.path.following = false -- not stuck anymore
2112 set_velocity(self, 0)
2114 if not self.custom_attack then
2116 if self.timer > 1 then
2118 self.timer = 0
2120 if self.double_melee_attack
2121 and random(1, 2) == 1 then
2122 set_animation(self, "punch2")
2123 else
2124 set_animation(self, "punch")
2127 local p2 = p
2128 local s2 = s
2130 p2.y = p2.y + .5
2131 s2.y = s2.y + .5
2133 if line_of_sight(self, p2, s2) == true then
2135 -- play attack sound
2136 mob_sound(self, self.sounds.attack)
2138 -- punch player (or what player is attached to)
2139 local attached = self.attack:get_attach()
2140 if attached then
2141 self.attack = attached
2143 self.attack:punch(self.object, 1.0, {
2144 full_punch_interval = 1.0,
2145 damage_groups = {fleshy = self.damage}
2146 }, nil)
2149 else -- call custom attack every second
2150 if self.custom_attack
2151 and self.timer > 1 then
2153 self.timer = 0
2155 self.custom_attack(self, p)
2160 elseif self.attack_type == "shoot"
2161 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 1)
2162 or (self.attack_type == "dogshoot" and dist > self.reach and dogswitch(self) == 0) then
2164 p.y = p.y - .5
2165 s.y = s.y + .5
2167 local dist = get_distance(p, s)
2168 local vec = {
2169 x = p.x - s.x,
2170 y = p.y - s.y,
2171 z = p.z - s.z
2174 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2176 if p.x > s.x then yaw = yaw + pi end
2178 yaw = set_yaw(self, yaw)
2180 set_velocity(self, 0)
2182 if self.shoot_interval
2183 and self.timer > self.shoot_interval
2184 and random(1, 100) <= 60 then
2186 self.timer = 0
2187 set_animation(self, "shoot")
2189 -- play shoot attack sound
2190 mob_sound(self, self.sounds.shoot_attack)
2192 local p = self.object:get_pos()
2194 p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
2196 if minetest.registered_entities[self.arrow] then
2198 local obj = minetest.add_entity(p, self.arrow)
2199 local ent = obj:get_luaentity()
2200 local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5
2201 local v = ent.velocity or 1 -- or set to default
2203 ent.switch = 1
2204 ent.owner_id = tostring(self.object) -- add unique owner id to arrow
2206 -- offset makes shoot aim accurate
2207 vec.y = vec.y + self.shoot_offset
2208 vec.x = vec.x * (v / amount)
2209 vec.y = vec.y * (v / amount)
2210 vec.z = vec.z * (v / amount)
2212 obj:setvelocity(vec)
2220 -- falling and fall damage
2221 local falling = function(self, pos)
2223 if self.fly then
2224 return
2227 -- floating in water (or falling)
2228 local v = self.object:getvelocity()
2230 if v.y > 0 then
2232 -- apply gravity when moving up
2233 self.object:setacceleration({
2234 x = 0,
2235 y = -10,
2236 z = 0
2239 elseif v.y <= 0 and v.y > self.fall_speed then
2241 -- fall downwards at set speed
2242 self.object:setacceleration({
2243 x = 0,
2244 y = self.fall_speed,
2245 z = 0
2247 else
2248 -- stop accelerating once max fall speed hit
2249 self.object:setacceleration({x = 0, y = 0, z = 0})
2252 -- in water then float up
2253 if minetest.registered_nodes[node_ok(pos).name].groups.water then
2255 if self.floats == 1 then
2257 self.object:setacceleration({
2258 x = 0,
2259 y = -self.fall_speed / (max(1, v.y) ^ 2),
2260 z = 0
2263 else
2265 -- fall damage onto solid ground
2266 if self.fall_damage == 1
2267 and self.object:getvelocity().y == 0 then
2269 local d = (self.old_y or 0) - self.object:get_pos().y
2271 if d > 5 then
2273 self.health = self.health - floor(d - 5)
2275 effect(pos, 5, "tnt_smoke.png", 1, 2, 2, nil)
2277 if check_for_death(self, "fall", {type = "fall"}) then
2278 return
2282 self.old_y = self.object:get_pos().y
2288 -- deal damage and effects when mob punched
2289 local mob_punch = function(self, hitter, tflp, tool_capabilities, dir)
2291 -- custom punch function
2292 if self.do_punch then
2294 -- when false skip going any further
2295 if self.do_punch(self, hitter, tflp, tool_caps, dir) == false then
2296 return
2300 -- error checking when mod profiling is enabled
2301 if not tool_capabilities then
2302 minetest.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2303 return
2306 -- is mob protected?
2307 if self.protected and hitter:is_player()
2308 and minetest.is_protected(self.object:get_pos(), hitter:get_player_name()) then
2309 minetest.chat_send_player(hitter:get_player_name(), S("Mob has been protected!"))
2310 return
2314 -- weapon wear
2315 local weapon = hitter:get_wielded_item()
2316 local punch_interval = 1.4
2318 -- calculate mob damage
2319 local damage = 0
2320 local armor = self.object:get_armor_groups() or {}
2321 local tmp
2323 -- quick error check incase it ends up 0 (serialize.h check test)
2324 if tflp == 0 then
2325 tflp = 0.2
2328 if use_cmi then
2329 damage = cmi.calculate_damage(self.object, hitter, tflp, tool_capabilities, dir)
2330 else
2332 for group,_ in pairs( (tool_capabilities.damage_groups or {}) ) do
2334 tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)
2336 if tmp < 0 then
2337 tmp = 0.0
2338 elseif tmp > 1 then
2339 tmp = 1.0
2342 damage = damage + (tool_capabilities.damage_groups[group] or 0)
2343 * tmp * ((armor[group] or 0) / 100.0)
2347 -- check for tool immunity or special damage
2348 for n = 1, #self.immune_to do
2350 if self.immune_to[n][1] == weapon:get_name() then
2352 damage = self.immune_to[n][2] or 0
2353 break
2357 -- healing
2358 if damage <= -1 then
2359 self.health = self.health - floor(damage)
2360 return
2363 if use_cmi then
2365 local cancel = cmi.notify_punch(self.object, hitter, tflp, tool_capabilities, dir, damage)
2367 if cancel then return end
2370 -- add weapon wear
2371 if tool_capabilities then
2372 punch_interval = tool_capabilities.full_punch_interval or 1.4
2375 if weapon:get_definition()
2376 and weapon:get_definition().tool_capabilities then
2378 weapon:add_wear(floor((punch_interval / 75) * 9000))
2379 hitter:set_wielded_item(weapon)
2382 -- only play hit sound and show blood effects if damage is 1 or over
2383 if damage >= 1 then
2385 -- weapon sounds
2386 if weapon:get_definition().sounds ~= nil then
2388 local s = random(0, #weapon:get_definition().sounds)
2390 minetest.sound_play(weapon:get_definition().sounds[s], {
2391 object = self.object, --hitter,
2392 max_hear_distance = 8
2394 else
2395 minetest.sound_play("default_punch", {
2396 object = self.object, --hitter,
2397 max_hear_distance = 5
2401 -- blood_particles
2402 if self.blood_amount > 0
2403 and not disable_blood then
2405 local pos = self.object:get_pos()
2407 pos.y = pos.y + (-self.collisionbox[2] + self.collisionbox[5]) * .5
2409 -- do we have a single blood texture or multiple?
2410 if type(self.blood_texture) == "table" then
2412 local blood = self.blood_texture[random(1, #self.blood_texture)]
2414 effect(pos, self.blood_amount, blood, nil, nil, 1, nil)
2415 else
2416 effect(pos, self.blood_amount, self.blood_texture, nil, nil, 1, nil)
2420 -- do damage
2421 self.health = self.health - floor(damage)
2423 -- exit here if dead, special item check
2424 if weapon:get_name() == "mobs:pick_lava" then
2425 if check_for_death(self, "lava", {type = "punch",
2426 puncher = hitter}) then
2427 return
2429 else
2430 if check_for_death(self, "hit", {type = "punch",
2431 puncher = hitter}) then
2432 return
2436 -- knock back effect (only on full punch)
2437 if self.knock_back
2438 and tflp >= punch_interval then
2440 local v = self.object:getvelocity()
2441 local r = 1.4 - min(punch_interval, 1.4)
2442 local kb = r * 5
2443 local up = 2
2445 -- if already in air then dont go up anymore when hit
2446 if v.y > 0
2447 or self.fly then
2448 up = 0
2451 -- direction error check
2452 dir = dir or {x = 0, y = 0, z = 0}
2454 -- check if tool already has specific knockback value
2455 if tool_capabilities.damage_groups["knockback"] then
2456 kb = tool_capabilities.damage_groups["knockback"]
2457 else
2458 kb = kb * 1.5
2461 self.object:setvelocity({
2462 x = dir.x * kb,
2463 y = up,
2464 z = dir.z * kb
2467 self.pause_timer = 0.25
2469 end -- END if damage
2471 -- if skittish then run away
2472 if self.runaway == true then
2474 local lp = hitter:get_pos()
2475 local s = self.object:get_pos()
2476 local vec = {
2477 x = lp.x - s.x,
2478 y = lp.y - s.y,
2479 z = lp.z - s.z
2482 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
2484 if lp.x > s.x then
2485 yaw = yaw + pi
2488 yaw = set_yaw(self, yaw, 6)
2489 self.state = "runaway"
2490 self.runaway_timer = 0
2491 self.following = nil
2494 local name = hitter:get_player_name() or ""
2496 -- attack puncher and call other mobs for help
2497 if self.passive == false
2498 and self.state ~= "flop"
2499 and self.child == false
2500 and hitter:get_player_name() ~= self.owner
2501 and not mobs.invis[ name ] then
2503 -- attack whoever punched mob
2504 self.state = ""
2505 do_attack(self, hitter)
2507 -- alert others to the attack
2508 local objs = minetest.get_objects_inside_radius(hitter:get_pos(), self.view_range)
2509 local obj = nil
2511 for n = 1, #objs do
2513 obj = objs[n]:get_luaentity()
2515 if obj then
2517 -- only alert members of same mob
2518 if obj.group_attack == true
2519 and obj.state ~= "attack"
2520 and obj.owner ~= name
2521 and obj.name == self.name then
2522 do_attack(obj, hitter)
2525 -- have owned mobs attack player threat
2526 if obj.owner == name and obj.owner_loyal then
2527 do_attack(obj, self.object)
2535 -- get entity staticdata
2536 local mob_staticdata = function(self)
2538 -- remove mob when out of range unless tamed
2539 if remove_far
2540 and self.remove_ok
2541 and self.type ~= "npc"
2542 and self.state ~= "attack"
2543 and not self.tamed
2544 and self.lifetimer < 20000 then
2546 self.object:remove()
2548 return ""-- nil
2551 self.remove_ok = true
2552 self.attack = nil
2553 self.following = nil
2554 self.state = "stand"
2556 -- used to rotate older mobs
2557 if self.drawtype
2558 and self.drawtype == "side" then
2559 self.rotate = math.rad(90)
2562 if use_cmi then
2563 self.serialized_cmi_components = cmi.serialize_components(self._cmi_components)
2566 local tmp = {}
2568 for _,stat in pairs(self) do
2570 local t = type(stat)
2572 if t ~= "function"
2573 and t ~= "nil"
2574 and t ~= "userdata"
2575 and _ ~= "_cmi_components" then
2576 tmp[_] = self[_]
2580 return minetest.serialize(tmp)
2584 -- activate mob and reload settings
2585 local mob_activate = function(self, staticdata, def, dtime)
2587 -- remove monsters in peaceful mode
2588 if self.type == "monster"
2589 and peaceful_only then
2591 self.object:remove()
2593 return
2596 -- load entity variables
2597 local tmp = minetest.deserialize(staticdata)
2599 if tmp then
2600 for _,stat in pairs(tmp) do
2601 self[_] = stat
2605 -- select random texture, set model and size
2606 if not self.base_texture then
2608 -- compatiblity with old simple mobs textures
2609 if type(def.textures[1]) == "string" then
2610 def.textures = {def.textures}
2613 self.base_texture = def.textures[random(1, #def.textures)]
2614 self.base_mesh = def.mesh
2615 self.base_size = self.visual_size
2616 self.base_colbox = self.collisionbox
2617 self.base_selbox = self.selectionbox
2620 -- for current mobs that dont have this set
2621 if not self.base_selbox then
2622 self.base_selbox = self.selectionbox or self.base_colbox
2625 -- set texture, model and size
2626 local textures = self.base_texture
2627 local mesh = self.base_mesh
2628 local vis_size = self.base_size
2629 local colbox = self.base_colbox
2630 local selbox = self.base_selbox
2632 -- specific texture if gotten
2633 if self.gotten == true
2634 and def.gotten_texture then
2635 textures = def.gotten_texture
2638 -- specific mesh if gotten
2639 if self.gotten == true
2640 and def.gotten_mesh then
2641 mesh = def.gotten_mesh
2644 -- set child objects to half size
2645 if self.child == true then
2647 vis_size = {
2648 x = self.base_size.x * .5,
2649 y = self.base_size.y * .5,
2652 if def.child_texture then
2653 textures = def.child_texture[1]
2656 colbox = {
2657 self.base_colbox[1] * .5,
2658 self.base_colbox[2] * .5,
2659 self.base_colbox[3] * .5,
2660 self.base_colbox[4] * .5,
2661 self.base_colbox[5] * .5,
2662 self.base_colbox[6] * .5
2664 selbox = {
2665 self.base_selbox[1] * .5,
2666 self.base_selbox[2] * .5,
2667 self.base_selbox[3] * .5,
2668 self.base_selbox[4] * .5,
2669 self.base_selbox[5] * .5,
2670 self.base_selbox[6] * .5
2674 if self.health == 0 then
2675 self.health = random (self.hp_min, self.hp_max)
2678 -- pathfinding init
2679 self.path = {}
2680 self.path.way = {} -- path to follow, table of positions
2681 self.path.lastpos = {x = 0, y = 0, z = 0}
2682 self.path.stuck = false
2683 self.path.following = false -- currently following path?
2684 self.path.stuck_timer = 0 -- if stuck for too long search for path
2686 -- mob defaults
2687 self.object:set_armor_groups({immortal = 1, fleshy = self.armor})
2688 self.old_y = self.object:get_pos().y
2689 self.old_health = self.health
2690 self.sounds.distance = self.sounds.distance or 10
2691 self.textures = textures
2692 self.mesh = mesh
2693 self.collisionbox = colbox
2694 self.selectionbox = selbox
2695 self.visual_size = vis_size
2696 self.standing_in = ""
2698 -- check existing nametag
2699 if not self.nametag then
2700 self.nametag = def.nametag
2703 -- set anything changed above
2704 self.object:set_properties(self)
2705 set_yaw(self, (random(0, 360) - 180) / 180 * pi, 6)
2706 update_tag(self)
2707 set_animation(self, "stand")
2709 -- run on_spawn function if found
2710 if self.on_spawn and not self.on_spawn_run then
2711 if self.on_spawn(self) then
2712 self.on_spawn_run = true -- if true, set flag to run once only
2716 -- run after_activate
2717 if def.after_activate then
2718 def.after_activate(self, staticdata, def, dtime)
2721 if use_cmi then
2722 self._cmi_components = cmi.activate_components(self.serialized_cmi_components)
2723 cmi.notify_activate(self.object, dtime)
2728 -- main mob function
2729 local mob_step = function(self, dtime)
2731 if use_cmi then
2732 cmi.notify_step(self.object, dtime)
2735 local pos = self.object:get_pos()
2736 local yaw = 0
2738 -- when lifetimer expires remove mob (except npc and tamed)
2739 if self.type ~= "npc"
2740 and not self.tamed
2741 and self.state ~= "attack"
2742 and remove_far ~= true
2743 and self.lifetimer < 20000 then
2745 self.lifetimer = self.lifetimer - dtime
2747 if self.lifetimer <= 0 then
2749 -- only despawn away from player
2750 local objs = minetest.get_objects_inside_radius(pos, 15)
2752 for n = 1, #objs do
2754 if objs[n]:is_player() then
2756 self.lifetimer = 20
2758 return
2762 effect(pos, 15, "tnt_smoke.png", 2, 4, 2, 0)
2764 self.object:remove()
2766 return
2770 falling(self, pos)
2772 -- smooth rotation by ThomasMonroe314
2774 if self.delay and self.delay > 0 then
2776 local yaw = self.object:get_yaw()
2778 if self.delay == 1 then
2779 yaw = self.target_yaw
2780 else
2781 local dif = abs(yaw - self.target_yaw)
2783 if yaw > self.target_yaw then
2785 if dif > pi then
2786 dif = 2 * pi - dif -- need to add
2787 yaw = yaw + dif / self.delay
2788 else
2789 yaw = yaw - dif / self.delay -- need to subtract
2792 elseif yaw < self.target_yaw then
2794 if dif > pi then
2795 dif = 2 * pi - dif
2796 yaw = yaw - dif / self.delay -- need to subtract
2797 else
2798 yaw = yaw + dif / self.delay -- need to add
2802 if yaw > (pi * 2) then yaw = yaw - (pi * 2) end
2803 if yaw < 0 then yaw = yaw + (pi * 2) end
2806 self.delay = self.delay - 1
2807 self.object:set_yaw(yaw)
2810 -- end rotation
2812 -- knockback timer
2813 if self.pause_timer > 0 then
2815 self.pause_timer = self.pause_timer - dtime
2817 return
2820 -- run custom function (defined in mob lua file)
2821 if self.do_custom then
2823 -- when false skip going any further
2824 if self.do_custom(self, dtime) == false then
2825 return
2829 -- attack timer
2830 self.timer = self.timer + dtime
2832 if self.state ~= "attack" then
2834 if self.timer < 1 then
2835 return
2838 self.timer = 0
2841 -- never go over 100
2842 if self.timer > 100 then
2843 self.timer = 1
2846 -- mob plays random sound at times
2847 if random(1, 100) == 1 then
2848 mob_sound(self, self.sounds.random)
2851 -- environmental damage timer (every 1 second)
2852 self.env_damage_timer = self.env_damage_timer + dtime
2854 if (self.state == "attack" and self.env_damage_timer > 1)
2855 or self.state ~= "attack" then
2857 self.env_damage_timer = 0
2859 -- check for environmental damage (water, fire, lava etc.)
2860 do_env_damage(self)
2862 -- node replace check (cow eats grass etc.)
2863 replace(self, pos)
2866 monster_attack(self)
2868 npc_attack(self)
2870 breed(self)
2872 follow_flop(self)
2874 do_states(self, dtime)
2876 do_jump(self)
2878 runaway_from(self)
2883 -- default function when mobs are blown up with TNT
2884 local do_tnt = function(obj, damage)
2886 obj.object:punch(obj.object, 1.0, {
2887 full_punch_interval = 1.0,
2888 damage_groups = {fleshy = damage},
2889 }, nil)
2891 return false, true, {}
2895 mobs.spawning_mobs = {}
2897 -- Code to execute before custom on_rightclick handling
2898 local on_rightclick_prefix = function(self, clicker)
2899 local item = clicker:get_wielded_item()
2901 -- Name mob with nametag
2902 if not self.ignores_nametag and item:get_name() == "mcl_mobs:nametag" then
2904 local tag = item:get_meta():get_string("name")
2905 if tag ~= "" then
2906 if string.len(tag) > MAX_MOB_NAME_LENGTH then
2907 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH)
2909 self.nametag = tag
2911 update_tag(self)
2913 if not mobs.is_creative(clicker:get_player_name()) then
2914 item:take_item()
2915 player:set_wielded_item(item)
2917 return true
2921 return false
2924 local create_mob_on_rightclick = function(on_rightclick)
2925 return function(self, clicker)
2926 local stop = on_rightclick_prefix(self, clicker)
2927 if (not stop) and (on_rightclick) then
2928 on_rightclick(self, clicker)
2933 -- register mob entity
2934 function mobs:register_mob(name, def)
2936 mobs.spawning_mobs[name] = true
2938 minetest.register_entity(name, {
2940 stepheight = def.stepheight or 1.1, -- was 0.6
2941 name = name,
2942 type = def.type,
2943 attack_type = def.attack_type,
2944 fly = def.fly,
2945 fly_in = def.fly_in or "air",
2946 owner = def.owner or "",
2947 order = def.order or "",
2948 on_die = def.on_die,
2949 do_custom = def.do_custom,
2950 jump_height = def.jump_height or 4, -- was 6
2951 drawtype = def.drawtype, -- DEPRECATED, use rotate instead
2952 rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
2953 lifetimer = def.lifetimer or 180, -- 3 minutes
2954 hp_min = max(1, (def.hp_min or 5) * difficulty),
2955 hp_max = max(1, (def.hp_max or 10) * difficulty),
2956 physical = true,
2957 collisionbox = def.collisionbox or {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25},
2958 selectionbox = def.selectionbox or def.collisionbox,
2959 visual = def.visual,
2960 visual_size = def.visual_size or {x = 1, y = 1},
2961 mesh = def.mesh,
2962 makes_footstep_sound = def.makes_footstep_sound or false,
2963 view_range = def.view_range or 5,
2964 walk_velocity = def.walk_velocity or 1,
2965 run_velocity = def.run_velocity or 2,
2966 damage = max(0, (def.damage or 0) * difficulty),
2967 light_damage = def.light_damage or 0,
2968 sunlight_damage = def.sunlight_damage or 0,
2969 water_damage = def.water_damage or 0,
2970 lava_damage = def.lava_damage or 0,
2971 suffocation = def.suffocation or 2,
2972 fall_damage = def.fall_damage or 1,
2973 fall_speed = def.fall_speed or -10, -- must be lower than -2 (default: -10)
2974 drops = def.drops or {},
2975 armor = def.armor or 100,
2976 on_rightclick = create_mob_on_rightclick(def.on_rightclick),
2977 arrow = def.arrow,
2978 shoot_interval = def.shoot_interval,
2979 sounds = def.sounds or {},
2980 animation = def.animation,
2981 follow = def.follow,
2982 jump = def.jump ~= false,
2983 walk_chance = def.walk_chance or 50,
2984 attacks_monsters = def.attacks_monsters or false,
2985 group_attack = def.group_attack or false,
2986 passive = def.passive or false,
2987 knock_back = def.knock_back ~= false,
2988 blood_amount = def.blood_amount or 5,
2989 blood_texture = def.blood_texture or "mobs_blood.png",
2990 shoot_offset = def.shoot_offset or 0,
2991 floats = def.floats or 1, -- floats in water by default
2992 replace_rate = def.replace_rate,
2993 replace_what = def.replace_what,
2994 replace_with = def.replace_with,
2995 replace_offset = def.replace_offset or 0,
2996 on_replace = def.on_replace,
2997 timer = 0,
2998 env_damage_timer = 0, -- only used when state = "attack"
2999 tamed = false,
3000 pause_timer = 0,
3001 horny = false,
3002 hornytimer = 0,
3003 child = false,
3004 gotten = false,
3005 health = 0,
3006 reach = def.reach or 3,
3007 htimer = 0,
3008 texture_list = def.textures,
3009 child_texture = def.child_texture,
3010 docile_by_day = def.docile_by_day or false,
3011 time_of_day = 0.5,
3012 fear_height = def.fear_height or 0,
3013 runaway = def.runaway,
3014 runaway_timer = 0,
3015 pathfinding = def.pathfinding,
3016 immune_to = def.immune_to or {},
3017 explosion_radius = def.explosion_radius,
3018 explosion_damage_radius = def.explosion_damage_radius,
3019 explosion_timer = def.explosion_timer or 3,
3020 allow_fuse_reset = def.allow_fuse_reset ~= false,
3021 stop_to_explode = def.stop_to_explode ~= false,
3022 custom_attack = def.custom_attack,
3023 double_melee_attack = def.double_melee_attack,
3024 dogshoot_switch = def.dogshoot_switch,
3025 dogshoot_count = 0,
3026 dogshoot_count_max = def.dogshoot_count_max or 5,
3027 dogshoot_count2_max = def.dogshoot_count2_max or (def.dogshoot_count_max or 5),
3028 attack_animals = def.attack_animals or false,
3029 specific_attack = def.specific_attack,
3030 runaway_from = def.runaway_from,
3031 owner_loyal = def.owner_loyal,
3032 facing_fence = false,
3033 _cmi_is_mob = true,
3035 -- MCL2 extensions
3036 ignores_nametag = def.ignores_nametag or false,
3037 rain_damage = def.rain_damage or 0,
3039 on_spawn = def.on_spawn,
3041 on_blast = def.on_blast or do_tnt,
3043 on_step = mob_step,
3045 do_punch = def.do_punch,
3047 on_punch = mob_punch,
3049 on_breed = def.on_breed,
3051 on_grown = def.on_grown,
3053 on_activate = function(self, staticdata, dtime)
3054 return mob_activate(self, staticdata, def, dtime)
3055 end,
3057 get_staticdata = function(self)
3058 return mob_staticdata(self)
3059 end,
3063 end -- END mobs:register_mob function
3066 -- count how many mobs of one type are inside an area
3067 local count_mobs = function(pos, type)
3069 local num_type = 0
3070 local num_total = 0
3071 local objs = minetest.get_objects_inside_radius(pos, aoc_range)
3073 for n = 1, #objs do
3075 if not objs[n]:is_player() then
3077 local obj = objs[n]:get_luaentity()
3079 -- count mob type and add to total also
3080 if obj and obj.name and obj.name == type then
3082 num_type = num_type + 1
3083 num_total = num_total + 1
3085 -- add to total mobs
3086 elseif obj and obj.name and obj.health ~= nil then
3088 num_total = num_total + 1
3093 return num_type, num_total
3097 -- global functions
3099 function mobs:spawn_abm_check(pos, node, name)
3100 -- global function to add additional spawn checks
3101 -- return true to stop spawning mob
3105 function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
3106 interval, chance, aoc, min_height, max_height, day_toggle, on_spawn)
3108 -- Do mobs spawn at all?
3109 if not mobs_spawn then
3110 return
3113 -- chance/spawn number override in minetest.conf for registered mob
3114 local numbers = minetest.settings:get(name)
3116 if numbers then
3117 numbers = numbers:split(",")
3118 chance = tonumber(numbers[1]) or chance
3119 aoc = tonumber(numbers[2]) or aoc
3121 if chance == 0 then
3122 minetest.log("warning", string.format("[mobs] %s has spawning disabled", name))
3123 return
3126 minetest.log("action",
3127 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name, chance, aoc))
3131 minetest.register_abm({
3133 label = name .. " spawning",
3134 nodenames = nodes,
3135 neighbors = neighbors,
3136 interval = interval,
3137 chance = max(1, (chance * mob_chance_multiplier)),
3138 catch_up = false,
3140 action = function(pos, node, active_object_count, active_object_count_wider)
3142 -- is mob actually registered?
3143 if not mobs.spawning_mobs[name]
3144 or not minetest.registered_entities[name] then
3145 return
3148 -- additional custom checks for spawning mob
3149 if mobs:spawn_abm_check(pos, node, name) == true then
3150 return
3153 -- do not spawn if too many of same mob in area
3154 if active_object_count_wider >= max_per_block
3155 or count_mobs(pos, name) >= aoc then
3156 -- too many entities
3157 return
3160 -- if toggle set to nil then ignore day/night check
3161 if day_toggle ~= nil then
3163 local tod = (minetest.get_timeofday() or 0) * 24000
3165 if tod > 4500 and tod < 19500 then
3166 -- daylight, but mob wants night
3167 if day_toggle == false then
3168 -- mob needs night
3169 return
3171 else
3172 -- night time but mob wants day
3173 if day_toggle == true then
3174 -- mob needs day
3175 return
3180 -- spawn above node
3181 pos.y = pos.y + 1
3183 -- only spawn away from player
3184 local objs = minetest.get_objects_inside_radius(pos, 10)
3186 for n = 1, #objs do
3188 if objs[n]:is_player() then
3189 -- player too close
3190 return
3194 -- mobs cannot spawn in protected areas when enabled
3195 if not spawn_protected
3196 and minetest.is_protected(pos, "") then
3197 return
3200 -- are we spawning within height limits?
3201 if pos.y > max_height
3202 or pos.y < min_height then
3203 return
3206 -- are light levels ok?
3207 local light = minetest.get_node_light(pos)
3208 if not light
3209 or light > max_light
3210 or light < min_light then
3211 return
3214 -- do we have enough height clearance to spawn mob?
3215 local ent = minetest.registered_entities[name]
3216 local height = max(0, math.ceil(ent.collisionbox[5] - ent.collisionbox[2]) - 1)
3218 for n = 0, height do
3220 local pos2 = {x = pos.x, y = pos.y + n, z = pos.z}
3222 if minetest.registered_nodes[node_ok(pos2).name].walkable == true then
3223 -- inside block
3224 return
3228 -- spawn mob half block higher than ground
3229 pos.y = pos.y + 0.5
3231 local mob = minetest.add_entity(pos, name)
3233 if on_spawn then
3235 local ent = mob:get_luaentity()
3237 on_spawn(ent, pos)
3244 -- compatibility with older mob registration
3245 function mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
3247 mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
3248 chance, active_object_count, -31000, max_height, day_toggle)
3252 -- MarkBu's spawn function
3253 function mobs:spawn(def)
3255 local name = def.name
3256 local nodes = def.nodes or {"group:soil", "group:stone"}
3257 local neighbors = def.neighbors or {"air"}
3258 local min_light = def.min_light or 0
3259 local max_light = def.max_light or 15
3260 local interval = def.interval or 30
3261 local chance = def.chance or 5000
3262 local active_object_count = def.active_object_count or 1
3263 local min_height = def.min_height or -31000
3264 local max_height = def.max_height or 31000
3265 local day_toggle = def.day_toggle
3266 local on_spawn = def.on_spawn
3268 mobs:spawn_specific(name, nodes, neighbors, min_light, max_light, interval,
3269 chance, active_object_count, min_height, max_height, day_toggle, on_spawn)
3273 -- register arrow for shoot attack
3274 function mobs:register_arrow(name, def)
3276 if not name or not def then return end -- errorcheck
3278 minetest.register_entity(name, {
3280 physical = false,
3281 visual = def.visual,
3282 visual_size = def.visual_size,
3283 textures = def.textures,
3284 velocity = def.velocity,
3285 hit_player = def.hit_player,
3286 hit_node = def.hit_node,
3287 hit_mob = def.hit_mob,
3288 drop = def.drop or false, -- drops arrow as registered item when true
3289 collisionbox = {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3290 timer = 0,
3291 switch = 0,
3292 owner_id = def.owner_id,
3293 rotate = def.rotate,
3294 automatic_face_movement_dir = def.rotate
3295 and (def.rotate - (pi / 180)) or false,
3297 on_activate = def.on_activate,
3299 on_step = def.on_step or function(self, dtime)
3301 self.timer = self.timer + 1
3303 local pos = self.object:get_pos()
3305 if self.switch == 0
3306 or self.timer > 150
3307 or not within_limits(pos, 0) then
3309 self.object:remove();
3311 return
3314 -- does arrow have a tail (fireball)
3315 if def.tail
3316 and def.tail == 1
3317 and def.tail_texture then
3319 minetest.add_particle({
3320 pos = pos,
3321 velocity = {x = 0, y = 0, z = 0},
3322 acceleration = {x = 0, y = 0, z = 0},
3323 expirationtime = def.expire or 0.25,
3324 collisiondetection = false,
3325 texture = def.tail_texture,
3326 size = def.tail_size or 5,
3327 glow = def.glow or 0,
3331 if self.hit_node then
3333 local node = node_ok(pos).name
3335 if minetest.registered_nodes[node].walkable then
3337 self.hit_node(self, pos, node)
3339 if self.drop == true then
3341 pos.y = pos.y + 1
3343 self.lastpos = (self.lastpos or pos)
3345 minetest.add_item(self.lastpos, self.object:get_luaentity().name)
3348 self.object:remove();
3350 return
3354 if self.hit_player or self.hit_mob then
3356 for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do
3358 if self.hit_player
3359 and player:is_player() then
3361 self.hit_player(self, player)
3362 self.object:remove();
3363 return
3366 local entity = player:get_luaentity()
3368 if entity
3369 and self.hit_mob
3370 and entity._cmi_is_mob == true
3371 and tostring(player) ~= self.owner_id
3372 and entity.name ~= self.object:get_luaentity().name then
3374 self.hit_mob(self, player)
3376 self.object:remove();
3378 return
3383 self.lastpos = pos
3389 -- compatibility function
3390 function mobs:explosion(pos, radius)
3391 local self = {sounds = {}}
3392 self.sounds.explode = "tnt_explode"
3393 mobs:boom(self, pos, radius)
3397 -- no damage to nodes explosion
3398 function mobs:safe_boom(self, pos, radius)
3400 minetest.sound_play(self.sounds and self.sounds.explode or "tnt_explode", {
3401 pos = pos,
3402 gain = 1.0,
3403 max_hear_distance = self.sounds and self.sounds.distance or 32
3406 entity_physics(pos, radius)
3407 effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
3411 -- make explosion with protection and tnt mod check
3412 function mobs:boom(self, pos, radius)
3414 if mobs_griefing
3415 and mod_tnt and tnt and tnt.boom
3416 and not minetest.is_protected(pos, "") then
3418 tnt.boom(pos, {
3419 radius = radius,
3420 damage_radius = radius,
3421 sound = self.sounds and self.sounds.explode,
3422 explode_center = true,
3424 else
3425 mobs:safe_boom(self, pos, radius)
3430 -- Register spawn eggs
3432 -- Note: This also introduces the “spawn_egg” group:
3433 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3434 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3435 function mobs:register_egg(mob, desc, background, addegg, no_creative)
3437 local grp = {spawn_egg = 1}
3439 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3440 if creative and no_creative == true then
3441 grp.not_in_creative_inventory = 1
3444 local invimg = background
3446 if addegg == 1 then
3447 invimg = "mobs_chicken_egg.png^(" .. invimg ..
3448 "^[mask:mobs_chicken_egg_overlay.png)"
3451 -- register old stackable mob egg
3452 minetest.register_craftitem(mob, {
3454 description = desc,
3455 inventory_image = invimg,
3456 groups = grp,
3458 _doc_items_longdesc = "This allows you to place a single mob.",
3459 _doc_items_usagehelp = "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.",
3461 on_place = function(itemstack, placer, pointed_thing)
3463 local pos = pointed_thing.above
3465 -- am I clicking on something with existing on_rightclick function?
3466 local under = minetest.get_node(pointed_thing.under)
3467 local def = minetest.registered_nodes[under.name]
3468 if def and def.on_rightclick then
3469 return def.on_rightclick(pointed_thing.under, under, placer, itemstack)
3472 if pos
3473 and within_limits(pos, 0)
3474 and not minetest.is_protected(pos, placer:get_player_name()) then
3476 local name = placer:get_player_name()
3477 local privs = minetest.get_player_privs(name)
3478 if not privs.maphack then
3479 minetest.chat_send_player(name, "You need the “maphack” privilege to change the mob spawner.")
3480 return itemstack
3482 if mod_mobspawners and under.name == "mcl_mobspawners:spawner" then
3483 mcl_mobspawners.setup_spawner(pointed_thing.under, itemstack:get_name())
3484 if not minetest.settings:get_bool("creative_mode") then
3485 itemstack:take_item()
3487 return itemstack
3490 if not minetest.registered_entities[mob] then
3491 return itemstack
3494 pos.y = pos.y + 1
3496 local mob = minetest.add_entity(pos, mob)
3497 local ent = mob:get_luaentity()
3499 -- don't set owner if monster or sneak pressed
3500 if ent.type ~= "monster"
3501 and not placer:get_player_control().sneak then
3502 ent.owner = placer:get_player_name()
3503 ent.tamed = true
3506 -- set nametag
3507 local nametag = itemstack:get_meta():get_string("name")
3508 if nametag ~= "" then
3509 if string.len(nametag) > MAX_MOB_NAME_LENGTH then
3510 nametag = string.sub(nametag, 1, MAX_MOB_NAME_LENGTH)
3512 ent.nametag = nametag
3513 update_tag(ent)
3516 -- if not in creative then take item
3517 if not mobs.is_creative(placer:get_player_name()) then
3518 itemstack:take_item()
3522 return itemstack
3523 end,
3529 -- No-op in MCL2 (capturing mobs is not possible).
3530 -- Provided for compability with Mobs Redo
3531 function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
3532 return false
3536 -- protect tamed mob with rune item
3537 function mobs:protect(self, clicker)
3538 local name = clicker:get_player_name()
3539 local tool = clicker:get_wielded_item()
3541 if tool:get_name() ~= "mobs:protector" then
3542 return false
3545 if self.tamed == false then
3546 minetest.chat_send_player(name, S("Not tamed!"))
3547 return true -- false
3550 if self.protected == true then
3551 minetest.chat_send_player(name, S("Already protected!"))
3552 return true -- false
3555 if not mobs.is_creative(clicker:get_player_name()) then
3556 tool:take_item() -- take 1 protection rune
3557 clicker:set_wielded_item(tool)
3560 self.protected = true
3562 local pos = self.object:get_pos()
3563 pos.y = pos.y + self.collisionbox[2] + 0.5
3565 effect(self.object:get_pos(), 25, "mobs_protect_particle.png", 0.5, 4, 2, 15)
3567 mob_sound(self, "mobs_spell")
3569 return true
3573 local mob_obj = {}
3574 local mob_sta = {}
3576 -- feeding, taming and breeding (thanks blert2112)
3577 function mobs:feed_tame(self, clicker, feed_count, breed, tame)
3578 if not self.follow then
3579 return false
3582 -- can eat/tame with item in hand
3583 if follow_holding(self, clicker) then
3585 -- if not in creative then take item
3586 if not mobs.is_creative(clicker:get_player_name()) then
3588 local item = clicker:get_wielded_item()
3590 item:take_item()
3592 clicker:set_wielded_item(item)
3595 -- increase health
3596 self.health = self.health + 4
3598 if self.health >= self.hp_max then
3600 self.health = self.hp_max
3602 if self.htimer < 1 then
3603 self.htimer = 5
3607 self.object:set_hp(self.health)
3609 update_tag(self)
3611 -- make children grow quicker
3612 if self.child == true then
3614 self.hornytimer = self.hornytimer + 20
3616 return true
3619 -- feed and tame
3620 self.food = (self.food or 0) + 1
3621 if self.food >= feed_count then
3623 self.food = 0
3625 if breed and self.hornytimer == 0 then
3626 self.horny = true
3629 self.gotten = false
3631 if tame then
3633 self.tamed = true
3635 if not self.owner or self.owner == "" then
3636 self.owner = clicker:get_player_name()
3640 -- make sound when fed so many times
3641 mob_sound(self, self.sounds.random)
3644 return true
3647 return false
3650 -- Spawn a child
3651 function mobs:spawn_child(pos, mob_type)
3652 local child = minetest.add_entity(pos, mob_type)
3653 if not child then
3654 return
3657 local ent = child:get_luaentity()
3658 effect(pos, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
3660 ent.child = true
3662 local textures
3663 -- using specific child texture (if found)
3664 if ent.child_texture then
3665 textures = ent.child_texture[1]
3668 -- and resize to half height
3669 child:set_properties({
3670 textures = textures,
3671 visual_size = {
3672 x = ent.base_size.x * .5,
3673 y = ent.base_size.y * .5,
3675 collisionbox = {
3676 ent.base_colbox[1] * .5,
3677 ent.base_colbox[2] * .5,
3678 ent.base_colbox[3] * .5,
3679 ent.base_colbox[4] * .5,
3680 ent.base_colbox[5] * .5,
3681 ent.base_colbox[6] * .5,
3683 selectionbox = {
3684 ent.base_selbox[1] * .5,
3685 ent.base_selbox[2] * .5,
3686 ent.base_selbox[3] * .5,
3687 ent.base_selbox[4] * .5,
3688 ent.base_selbox[5] * .5,
3689 ent.base_selbox[6] * .5,
3693 return child
3697 -- compatibility function for old entities to new modpack entities
3698 function mobs:alias_mob(old_name, new_name)
3700 -- spawn egg
3701 minetest.register_alias(old_name, new_name)
3703 -- entity
3704 minetest.register_entity(":" .. old_name, {
3706 physical = false,
3708 on_step = function(self)
3710 if minetest.registered_entities[new_name] then
3711 minetest.add_entity(self.object:get_pos(), new_name)
3714 self.object:remove()