Remove intllib-related code
[minetest_hades/hades_revisited.git] / mods / mobs / api.lua
blobffb47718ed958abff7519499ca61cd96cc744670
2 -- Mobs Api
4 mobs = {}
5 mobs.mod = "redo"
6 mobs.version = "20180328"
9 -- Intllib
10 local MP = minetest.get_modpath(minetest.get_current_modname())
11 local S = minetest.get_translator("mobs")
13 -- CMI support check
14 local use_cmi = minetest.global_exists("cmi")
17 -- Invisibility mod check
18 mobs.invis = {}
19 if minetest.global_exists("invisibility") then
20 mobs.invis = invisibility
21 end
24 -- creative check
25 local creative_mode_cache = minetest.settings:get_bool("creative_mode")
26 function mobs.is_creative(name)
27 return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
28 end
31 -- localize math functions
32 local pi = math.pi
33 local square = math.sqrt
34 local sin = math.sin
35 local cos = math.cos
36 local abs = math.abs
37 local min = math.min
38 local max = math.max
39 local atann = math.atan
40 local random = math.random
41 local floor = math.floor
42 local atan = function(x)
43 if not x or x ~= x then
44 --error("atan bassed NaN")
45 return 0
46 else
47 return atann(x)
48 end
49 end
52 -- Load settings
53 local damage_enabled = minetest.settings:get_bool("enable_damage")
54 local mobs_spawn = minetest.settings:get_bool("mobs_spawn") ~= false
55 local peaceful_only = minetest.settings:get_bool("only_peaceful_mobs")
56 local disable_blood = minetest.settings:get_bool("mobs_disable_blood")
57 local mobs_drop_items = minetest.settings:get_bool("mobs_drop_items") ~= false
58 local mobs_griefing = minetest.settings:get_bool("mobs_griefing") ~= false
59 local creative = minetest.settings:get_bool("creative_mode")
60 local spawn_protected = minetest.settings:get_bool("mobs_spawn_protected") ~= false
61 local remove_far = minetest.settings:get_bool("remove_far_mobs")
62 local difficulty = tonumber(minetest.settings:get("mob_difficulty")) or 1.0
63 local show_health = false --minetest.settings:get_bool("mob_show_health") ~= false
64 local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 99)
65 local mob_chance_multiplier = tonumber(minetest.settings:get("mob_chance_multiplier") or 1)
67 -- Peaceful mode message so players will know there are no monsters
68 if peaceful_only then
69 minetest.register_on_joinplayer(function(player)
70 minetest.chat_send_player(player:get_player_name(),
71 S("** Peaceful Mode Active - No Monsters Will Spawn"))
72 end)
73 end
75 -- calculate aoc range for mob count
76 local aosrb = tonumber(minetest.settings:get("active_object_send_range_blocks"))
77 local abr = tonumber(minetest.settings:get("active_block_range"))
78 local aoc_range = max(aosrb, abr) * 16
80 -- pathfinding settings
81 local enable_pathfinding = true
82 local stuck_timeout = 3 -- how long before mob gets stuck in place and starts searching
83 local stuck_path_timeout = 10 -- how long will mob follow path before giving up
85 -- default nodes
86 local node_fire = "fire:basic_flame"
87 local node_permanent_flame = "fire:permanent_flame"
88 local node_ice = "hades_core:stone"
89 local node_snowblock = "hades_core:stone"
90 local node_snow = "hades_core:stone"
91 mobs.fallback_node = minetest.registered_aliases["mapgen_sand"] or "hades_core:ash"
94 -- play sound
95 local mob_sound = function(self, sound)
97 if sound then
98 minetest.sound_play(sound, {
99 object = self.object,
100 gain = 1.0,
101 max_hear_distance = self.sounds.distance
107 -- attack player/mob
108 local do_attack = function(self, player)
110 if self.state == "attack" then
111 return
114 self.attack = player
115 self.state = "attack"
117 if random(0, 100) < 90 then
118 mob_sound(self, self.sounds.war_cry)
123 -- move mob in facing direction
124 local set_velocity = function(self, v)
126 local yaw = (self.object:get_yaw() or 0) + self.rotate
128 self.object:set_velocity({
129 x = sin(yaw) * -v,
130 y = self.object:get_velocity().y,
131 z = cos(yaw) * v
136 -- calculate mob velocity
137 local get_velocity = function(self)
139 local v = self.object:get_velocity()
141 return (v.x * v.x + v.z * v.z) ^ 0.5
145 -- set and return valid yaw
146 local set_yaw = function(self, yaw)
148 if not yaw or yaw ~= yaw then
149 yaw = 0
152 self:set_yaw(yaw)
154 return yaw
158 -- set defined animation
159 local set_animation = function(self, anim)
161 if not self.animation
162 or not anim then return end
164 self.animation.current = self.animation.current or ""
166 if anim == self.animation.current
167 or not self.animation[anim .. "_start"]
168 or not self.animation[anim .. "_end"] then
169 return
172 self.animation.current = anim
174 self.object:set_animation({
175 x = self.animation[anim .. "_start"],
176 y = self.animation[anim .. "_end"]},
177 self.animation[anim .. "_speed"] or self.animation.speed_normal or 15,
178 0, self.animation[anim .. "_loop"] ~= false)
182 -- above function exported for mount.lua
183 function mobs:set_animation(self, anim)
184 set_animation(self, anim)
188 -- calculate distance
189 local get_distance = function(a, b)
191 local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z
193 return square(x * x + y * y + z * z)
197 -- check line of sight (BrunoMine)
198 local line_of_sight = function(self, pos1, pos2, stepsize)
200 stepsize = stepsize or 1
202 local s, pos = minetest.line_of_sight(pos1, pos2, stepsize)
204 -- normal walking and flying mobs can see you through air
205 if s == true then
206 return true
209 -- New pos1 to be analyzed
210 local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
212 local r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
214 -- Checks the return
215 if r == true then return true end
217 -- Nodename found
218 local nn = minetest.get_node(pos).name
220 -- Target Distance (td) to travel
221 local td = get_distance(pos1, pos2)
223 -- Actual Distance (ad) traveled
224 local ad = 0
226 -- It continues to advance in the line of sight in search of a real
227 -- obstruction which counts as 'normal' nodebox.
228 while minetest.registered_nodes[nn]
229 and (minetest.registered_nodes[nn].walkable == false
230 or minetest.registered_nodes[nn].drawtype == "nodebox") do
232 -- Check if you can still move forward
233 if td < ad + stepsize then
234 return true -- Reached the target
237 -- Moves the analyzed pos
238 local d = get_distance(pos1, pos2)
240 npos1.x = ((pos2.x - pos1.x) / d * stepsize) + pos1.x
241 npos1.y = ((pos2.y - pos1.y) / d * stepsize) + pos1.y
242 npos1.z = ((pos2.z - pos1.z) / d * stepsize) + pos1.z
244 -- NaN checks
245 if d == 0
246 or npos1.x ~= npos1.x
247 or npos1.y ~= npos1.y
248 or npos1.z ~= npos1.z then
249 return false
252 ad = ad + stepsize
254 -- scan again
255 r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
257 if r == true then return true end
259 -- New Nodename found
260 nn = minetest.get_node(pos).name
264 return false
268 -- are we flying in what we are suppose to? (taikedz)
269 local flight_check = function(self, pos_w)
271 local nod = self.standing_in
272 local def = minetest.registered_nodes[nod]
274 if not def then return false end -- nil check
276 if type(self.fly_in) == "string"
277 and nod == self.fly_in then
279 return true
281 elseif type(self.fly_in) == "table" then
283 for _,fly_in in pairs(self.fly_in) do
285 if nod == fly_in then
287 return true
292 -- stops mobs getting stuck inside stairs and plantlike nodes
293 if def.drawtype ~= "airlike"
294 and def.drawtype ~= "liquid"
295 and def.drawtype ~= "flowingliquid" then
296 return true
299 return false
303 -- custom particle effects
304 local effect = function(pos, amount, texture, min_size, max_size, radius, gravity, glow)
306 radius = radius or 2
307 min_size = min_size or 0.5
308 max_size = max_size or 1
309 gravity = gravity or -10
310 glow = glow or 0
312 minetest.add_particlespawner({
313 amount = amount,
314 time = 0.25,
315 minpos = pos,
316 maxpos = pos,
317 minvel = {x = -radius, y = -radius, z = -radius},
318 maxvel = {x = radius, y = radius, z = radius},
319 minacc = {x = 0, y = gravity, z = 0},
320 maxacc = {x = 0, y = gravity, z = 0},
321 minexptime = 0.1,
322 maxexptime = 1,
323 minsize = min_size,
324 maxsize = max_size,
325 texture = texture,
326 glow = glow,
331 -- update nametag colour
332 local update_tag = function(self)
334 local col = "#00FF00"
335 local qua = self.hp_max / 4
337 if self.health <= floor(qua * 3) then
338 col = "#FFFF00"
341 if self.health <= floor(qua * 2) then
342 col = "#FF6600"
345 if self.health <= floor(qua) then
346 col = "#FF0000"
349 self.object:set_properties({
350 nametag = self.nametag,
351 nametag_color = col
357 -- drop items
358 local item_drop = function(self, cooked)
360 -- no drops if disabled by setting
361 if not mobs_drop_items then return end
363 -- no drops for child mobs
364 if self.child then return end
366 local obj, item, num
367 local pos = self.object:get_pos()
369 self.drops = self.drops or {} -- nil check
371 for n = 1, #self.drops do
373 if random(1, self.drops[n].chance) == 1 then
375 num = random(self.drops[n].min or 1, self.drops[n].max or 1)
376 item = self.drops[n].name
378 -- cook items when true
379 if cooked then
381 local output = minetest.get_craft_result({
382 method = "cooking", width = 1, items = {item}})
384 if output and output.item and not output.item:is_empty() then
385 item = output.item:get_name()
389 -- add item if it exists
390 obj = minetest.add_item(pos, ItemStack(item .. " " .. num))
392 if obj and obj:get_luaentity() then
394 obj:set_velocity({
395 x = random(-10, 10) / 9,
396 y = 6,
397 z = random(-10, 10) / 9,
399 elseif obj then
400 obj:remove() -- item does not exist
405 self.drops = {}
409 -- check if mob is dead or only hurt
410 local check_for_death = function(self, cause, cmi_cause)
412 -- has health actually changed?
413 if self.health == self.old_health and self.health > 0 then
414 return
417 self.old_health = self.health
419 -- still got some health? play hurt sound
420 if self.health > 0 then
422 mob_sound(self, self.sounds.damage)
424 -- make sure health isn't higher than max
425 if self.health > self.hp_max then
426 self.health = self.hp_max
429 -- backup nametag so we can show health stats
430 if not self.nametag2 then
431 self.nametag2 = self.nametag or ""
434 if show_health
435 and (cmi_cause and cmi_cause.type == "punch") then
437 self.htimer = 2
438 self.nametag = "♥ " .. self.health .. " / " .. self.hp_max
440 update_tag(self)
443 return false
446 -- dropped cooked item if mob died in lava
447 if cause == "lava" then
448 item_drop(self, true)
449 else
450 item_drop(self, nil)
453 mob_sound(self, self.sounds.death)
455 local pos = self.object:get_pos()
457 -- execute custom death function
458 if self.on_die then
460 self.on_die(self, pos)
462 if use_cmi then
463 cmi.notify_die(self.object, cmi_cause)
466 self.object:remove()
468 return true
471 -- default death function and die animation (if defined)
472 if self.animation
473 and self.animation.die_start
474 and self.animation.die_end then
476 local frames = self.animation.die_end - self.animation.die_start
477 local speed = self.animation.die_speed or 15
478 local length = max(frames / speed, 0)
480 self.attack = nil
481 self.v_start = false
482 self.timer = 0
483 self.blinktimer = 0
484 self.passive = true
485 self.state = "die"
486 set_velocity(self, 0)
487 set_animation(self, "die")
489 minetest.after(length, function(self)
491 if use_cmi then
492 cmi.notify_die(self.object, cmi_cause)
495 self.object:remove()
496 end, self)
497 else
499 if use_cmi then
500 cmi.notify_die(self.object, cmi_cause)
503 self.object:remove()
506 effect(pos, 20, "tnt_smoke.png")
508 return true
512 -- check if within physical map limits (-30911 to 30927)
513 local within_limits = function(pos, radius)
515 if (pos.x - radius) > -30913
516 and (pos.x + radius) < 30928
517 and (pos.y - radius) > -30913
518 and (pos.y + radius) < 30928
519 and (pos.z - radius) > -30913
520 and (pos.z + radius) < 30928 then
521 return true -- within limits
524 return false -- beyond limits
528 -- is mob facing a cliff
529 local is_at_cliff = function(self)
531 if self.fear_height == 0 then -- 0 for no falling protection!
532 return false
535 local yaw = self.object:get_yaw()
536 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
537 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
538 local pos = self.object:get_pos()
539 local ypos = pos.y + self.collisionbox[2] -- just above floor
541 if minetest.line_of_sight(
542 {x = pos.x + dir_x, y = ypos, z = pos.z + dir_z},
543 {x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z}
544 , 1) then
546 return true
549 return false
553 -- get node but use fallback for nil or unknown
554 local node_ok = function(pos, fallback)
556 fallback = fallback or mobs.fallback_node
558 local node = minetest.get_node_or_nil(pos)
560 if node and minetest.registered_nodes[node.name] then
561 return node
564 return minetest.registered_nodes[fallback] -- {name = fallback}
568 -- environmental damage (water, lava, fire, light etc.)
569 local do_env_damage = function(self)
571 -- feed/tame text timer (so mob 'full' messages dont spam chat)
572 if self.htimer > 0 then
573 self.htimer = self.htimer - 1
576 -- reset nametag after showing health stats
577 if self.htimer < 1 and self.nametag2 then
579 self.nametag = self.nametag2
580 self.nametag2 = nil
582 update_tag(self)
585 local pos = self.object:get_pos()
587 self.time_of_day = minetest.get_timeofday()
589 -- remove mob if beyond map limits
590 if not within_limits(pos, 0) then
591 self.object:remove()
592 return
595 -- bright light harms mob
596 if self.light_damage ~= 0
597 -- and pos.y > 0
598 -- and self.time_of_day > 0.2
599 -- and self.time_of_day < 0.8
600 and (minetest.get_node_light(pos) or 0) > 12 then
602 self.health = self.health - self.light_damage
604 effect(pos, 5, "tnt_smoke.png")
606 if check_for_death(self, "light", {type = "light"}) then return end
609 local y_level = self.collisionbox[2]
611 if self.child then
612 y_level = self.collisionbox[2] * 0.5
615 -- what is mob standing in?
616 pos.y = pos.y + y_level + 0.25 -- foot level
617 self.standing_in = node_ok(pos, "air").name
618 -- print ("standing in " .. self.standing_in)
620 -- don't fall when on ignore, just stand still
621 if self.standing_in == "ignore" then
622 self.object:set_velocity({x = 0, y = 0, z = 0})
625 local nodef = minetest.registered_nodes[self.standing_in]
627 pos.y = pos.y + 1 -- for particle effect position
629 -- water
630 if self.water_damage
631 and nodef.groups.water then
633 if self.water_damage ~= 0 then
635 self.health = self.health - self.water_damage
637 effect(pos, 5, "bubble.png", nil, nil, 1, nil)
639 if check_for_death(self, "water", {type = "environment",
640 pos = pos, node = self.standing_in}) then return end
643 -- lava or fire
644 elseif self.lava_damage
645 and (nodef.groups.lava
646 or self.standing_in == node_fire
647 or self.standing_in == node_permanent_flame) then
649 if self.lava_damage ~= 0 then
651 self.health = self.health - self.lava_damage
653 effect(pos, 5, "fire_basic_flame.png", nil, nil, 1, nil)
655 if check_for_death(self, "lava", {type = "environment",
656 pos = pos, node = self.standing_in}) then return end
659 -- damage_per_second node check
660 elseif nodef.damage_per_second ~= 0 then
662 self.health = self.health - nodef.damage_per_second
664 effect(pos, 5, "tnt_smoke.png")
666 if check_for_death(self, "dps", {type = "environment",
667 pos = pos, node = self.standing_in}) then return end
669 --[[
670 --- suffocation inside solid node
671 if self.suffocation ~= 0
672 and nodef.walkable == true
673 and nodef.groups.disable_suffocation ~= 1
674 and nodef.drawtype == "normal" then
676 self.health = self.health - self.suffocation
678 if check_for_death(self, "suffocation", {type = "environment",
679 pos = pos, node = self.standing_in}) then return end
682 check_for_death(self, "", {type = "unknown"})
686 -- jump if facing a solid node (not fences or gates)
687 local do_jump = function(self)
689 if not self.jump
690 or self.jump_height == 0
691 or self.fly
692 or self.child then
693 return false
696 self.facing_fence = false
698 -- something stopping us while moving?
699 if self.state ~= "stand"
700 and get_velocity(self) > 0.5
701 and self.object:get_velocity().y ~= 0 then
702 return false
705 local pos = self.object:get_pos()
706 local yaw = self.object:get_yaw()
708 -- what is mob standing on?
709 pos.y = pos.y + self.collisionbox[2] - 0.2
711 local nod = node_ok(pos)
713 --print ("standing on:", nod.name, pos.y)
715 if minetest.registered_nodes[nod.name].walkable == false then
716 return false
719 -- where is front
720 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
721 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
723 -- what is in front of mob?
724 local nod = node_ok({
725 x = pos.x + dir_x,
726 y = pos.y + 0.5,
727 z = pos.z + dir_z
730 -- thin blocks that do not need to be jumped
731 if nod.name == node_snow then
732 return false
735 --print ("in front:", nod.name, pos.y + 0.5)
737 if self.walk_chance == 0
738 or minetest.registered_items[nod.name].walkable then
740 if not nod.name:find("fence")
741 and not nod.name:find("gate") then
743 local v = self.object:get_velocity()
745 v.y = self.jump_height
747 set_animation(self, "jump") -- only when defined
749 self.object:set_velocity(v)
751 if get_velocity(self) > 0 then
752 mob_sound(self, self.sounds.jump)
754 else
755 self.facing_fence = true
758 return true
761 return false
765 -- blast damage to entities nearby (modified from TNT mod)
766 local entity_physics = function(pos, radius)
768 radius = radius * 2
770 local objs = minetest.get_objects_inside_radius(pos, radius)
771 local obj_pos, dist
773 for n = 1, #objs do
775 obj_pos = objs[n]:get_pos()
777 dist = get_distance(pos, obj_pos)
778 if dist < 1 then dist = 1 end
780 local damage = floor((4 / dist) * radius)
781 local ent = objs[n]:get_luaentity()
783 -- punches work on entities AND players
784 objs[n]:punch(objs[n], 1.0, {
785 full_punch_interval = 1.0,
786 damage_groups = {fleshy = damage},
787 }, pos)
792 -- should mob follow what I'm holding ?
793 local follow_holding = function(self, clicker)
795 if mobs.invis[clicker:get_player_name()] then
796 return false
799 local item = clicker:get_wielded_item()
800 local t = type(self.follow)
802 -- single item
803 if t == "string"
804 and item:get_name() == self.follow then
805 return true
807 -- multiple items
808 elseif t == "table" then
810 for no = 1, #self.follow do
812 if self.follow[no] == item:get_name() then
813 return true
818 return false
822 -- find two animals of same type and breed if nearby and horny
823 local breed = function(self)
825 -- child takes 240 seconds before growing into adult
826 if self.child == true then
828 self.hornytimer = self.hornytimer + 1
830 if self.hornytimer > 240 then
832 self.child = false
833 self.hornytimer = 0
835 self.object:set_properties({
836 textures = self.base_texture,
837 mesh = self.base_mesh,
838 visual_size = self.base_size,
839 collisionbox = self.base_colbox,
840 selectionbox = self.base_selbox,
843 -- custom function when child grows up
844 if self.on_grown then
845 self.on_grown(self)
846 else
847 -- jump when fully grown so as not to fall into ground
848 self.object:set_velocity({
849 x = 0,
850 y = self.jump_height,
851 z = 0
856 return
859 -- horny animal can mate for 40 seconds,
860 -- afterwards horny animal cannot mate again for 200 seconds
861 if self.horny == true
862 and self.hornytimer < 240 then
864 self.hornytimer = self.hornytimer + 1
866 if self.hornytimer >= 240 then
867 self.hornytimer = 0
868 self.horny = false
872 -- find another same animal who is also horny and mate if nearby
873 if self.horny == true
874 and self.hornytimer <= 40 then
876 local pos = self.object:get_pos()
878 effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, "heart.png", 3, 4, 1, 0.1)
880 local objs = minetest.get_objects_inside_radius(pos, 3)
881 local num = 0
882 local ent = nil
884 for n = 1, #objs do
886 ent = objs[n]:get_luaentity()
888 -- check for same animal with different colour
889 local canmate = false
891 if ent then
893 if ent.name == self.name then
894 canmate = true
895 else
896 local entname = string.split(ent.name,":")
897 local selfname = string.split(self.name,":")
899 if entname[1] == selfname[1] then
900 entname = string.split(entname[2],"_")
901 selfname = string.split(selfname[2],"_")
903 if entname[1] == selfname[1] then
904 canmate = true
910 if ent
911 and canmate == true
912 and ent.horny == true
913 and ent.hornytimer <= 40 then
914 num = num + 1
917 -- found your mate? then have a baby
918 if num > 1 then
920 self.hornytimer = 41
921 ent.hornytimer = 41
923 -- spawn baby
924 minetest.after(5, function()
926 -- custom breed function
927 if self.on_breed then
929 -- when false skip going any further
930 if self.on_breed(self, ent) == false then
931 return
933 else
934 effect(pos, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
937 local mob = minetest.add_entity(pos, self.name)
938 local ent2 = mob:get_luaentity()
939 local textures = self.base_texture
941 -- using specific child texture (if found)
942 if self.child_texture then
943 textures = self.child_texture[1]
946 -- and resize to half height
947 mob:set_properties({
948 textures = textures,
949 visual_size = {
950 x = self.base_size.x * .5,
951 y = self.base_size.y * .5,
953 collisionbox = {
954 self.base_colbox[1] * .5,
955 self.base_colbox[2] * .5,
956 self.base_colbox[3] * .5,
957 self.base_colbox[4] * .5,
958 self.base_colbox[5] * .5,
959 self.base_colbox[6] * .5,
961 selectionbox = {
962 self.base_selbox[1] * .5,
963 self.base_selbox[2] * .5,
964 self.base_selbox[3] * .5,
965 self.base_selbox[4] * .5,
966 self.base_selbox[5] * .5,
967 self.base_selbox[6] * .5,
970 -- tamed and owned by parents' owner
971 ent2.child = true
972 ent2.tamed = true
973 ent2.owner = self.owner
974 end)
976 num = 0
978 break
985 -- find and replace what mob is looking for (grass, wheat etc.)
986 local replace = function(self, pos)
988 if not mobs_griefing
989 or not self.replace_rate
990 or not self.replace_what
991 or self.child == true
992 or self.object:get_velocity().y ~= 0
993 or random(1, self.replace_rate) > 1 then
994 return
997 local what, with, y_offset
999 if type(self.replace_what[1]) == "table" then
1001 local num = random(#self.replace_what)
1003 what = self.replace_what[num][1] or ""
1004 with = self.replace_what[num][2] or ""
1005 y_offset = self.replace_what[num][3] or 0
1006 else
1007 what = self.replace_what
1008 with = self.replace_with or ""
1009 y_offset = self.replace_offset or 0
1012 pos.y = pos.y + y_offset
1014 if #minetest.find_nodes_in_area(pos, pos, what) > 0 then
1016 -- print ("replace node = ".. minetest.get_node(pos).name, pos.y)
1018 local oldnode = {name = what}
1019 local newnode = {name = with}
1020 local on_replace_return
1022 if self.on_replace then
1023 on_replace_return = self.on_replace(self, pos, oldnode, newnode)
1026 if on_replace_return ~= false then
1028 minetest.set_node(pos, {name = with})
1030 -- when cow/sheep eats grass, replace wool and milk
1031 if self.gotten == true then
1032 self.gotten = false
1033 self.object:set_properties(self)
1040 -- check if daytime and also if mob is docile during daylight hours
1041 local day_docile = function(self)
1043 if self.docile_by_day == false then
1045 return false
1047 elseif self.docile_by_day == true
1048 and self.time_of_day > 0.2
1049 and self.time_of_day < 0.8 then
1051 return true
1056 -- path finding and smart mob routine by rnd
1057 local smart_mobs = function(self, s, p, dist, dtime)
1059 local s1 = self.path.lastpos
1061 -- is it becoming stuck?
1062 if abs(s1.x - s.x) + abs(s1.z - s.z) < 1.5 then
1063 self.path.stuck_timer = self.path.stuck_timer + dtime
1064 else
1065 self.path.stuck_timer = 0
1068 self.path.lastpos = {x = s.x, y = s.y, z = s.z}
1070 -- im stuck, search for path
1071 if (self.path.stuck_timer > stuck_timeout and not self.path.following)
1072 or (self.path.stuck_timer > stuck_path_timeout and self.path.following) then
1074 self.path.stuck_timer = 0
1076 -- lets try find a path, first take care of positions
1077 -- since pathfinder is very sensitive
1078 local sheight = self.collisionbox[5] - self.collisionbox[2]
1080 -- round position to center of node to avoid stuck in walls
1081 -- also adjust height for player models!
1082 s.x = floor(s.x + 0.5)
1083 -- s.y = floor(s.y + 0.5) - sheight
1084 s.z = floor(s.z + 0.5)
1086 local ssight, sground = minetest.line_of_sight(s, {
1087 x = s.x, y = s.y - 4, z = s.z}, 1)
1089 -- determine node above ground
1090 if not ssight then
1091 s.y = sground.y + 1
1094 local p1 = self.attack:get_pos()
1096 p1.x = floor(p1.x + 0.5)
1097 p1.y = floor(p1.y + 0.5)
1098 p1.z = floor(p1.z + 0.5)
1100 local dropheight = 6
1101 if self.fear_height ~= 0 then dropheight = self.fear_height end
1103 -- self.path.way = minetest.find_path(s, p1, 16, 2, 6, "Dijkstra")
1104 self.path.way = minetest.find_path(s, p1, 16, self.stepheight, dropheight, "A*_noprefetch")
1106 -- attempt to unstick mob that is "daydreaming"
1107 --[[ BUT NOT IN HADES REVISITED, SILLY!
1108 self.object:set_pos({
1109 x = s.x + 0.1 * (random() * 2 - 1),
1110 y = s.y + 1,
1111 z = s.z + 0.1 * (random() * 2 - 1)
1115 self.state = ""
1116 do_attack(self, self.attack)
1118 -- no path found, try something else
1119 if not self.path.way then
1121 self.path.following = false
1123 -- lets make way by digging/building if not accessible
1124 if self.pathfinding == 2 and mobs_griefing then
1126 -- is player higher than mob?
1127 if s.y < p1.y then
1129 -- build upwards
1130 if not minetest.is_protected(s, "") then
1132 local ndef1 = minetest.registered_nodes[self.standing_in]
1134 if ndef1 and (ndef1.buildable_to or ndef1.groups.liquid) then
1136 minetest.set_node(s, {name = mobs.fallback_node})
1140 local sheight = math.ceil(self.collisionbox[5]) + 1
1142 -- assume mob is 2 blocks high so it digs above its head
1143 s.y = s.y + sheight
1145 -- remove one block above to make room to jump
1146 if not minetest.is_protected(s, "") then
1148 local node1 = node_ok(s, "air").name
1149 local ndef1 = minetest.registered_nodes[node1]
1151 if node1 ~= "air"
1152 and node1 ~= "ignore"
1153 and ndef1
1154 and not ndef1.groups.level
1155 and not ndef1.groups.unbreakable
1156 and not ndef1.groups.liquid then
1158 minetest.set_node(s, {name = "air"})
1159 minetest.add_item(s, ItemStack(node1))
1164 s.y = s.y - sheight
1165 self.object:set_pos({x = s.x, y = s.y + 2, z = s.z})
1167 else -- dig 2 blocks to make door toward player direction
1169 local yaw1 = self.object:get_yaw() + pi / 2
1170 local p1 = {
1171 x = s.x + cos(yaw1),
1172 y = s.y,
1173 z = s.z + sin(yaw1)
1176 if not minetest.is_protected(p1, "") then
1178 local node1 = node_ok(p1, "air").name
1179 local ndef1 = minetest.registered_nodes[node1]
1181 if node1 ~= "air"
1182 and node1 ~= "ignore"
1183 and ndef1
1184 and not ndef1.groups.level
1185 and not ndef1.groups.unbreakable
1186 and not ndef1.groups.liquid then
1188 minetest.add_item(p1, ItemStack(node1))
1189 minetest.set_node(p1, {name = "air"})
1192 p1.y = p1.y + 1
1193 node1 = node_ok(p1, "air").name
1194 ndef1 = minetest.registered_nodes[node1]
1196 if node1 ~= "air"
1197 and node1 ~= "ignore"
1198 and ndef1
1199 and not ndef1.groups.level
1200 and not ndef1.groups.unbreakable
1201 and not ndef1.groups.liquid then
1203 minetest.add_item(p1, ItemStack(node1))
1204 minetest.set_node(p1, {name = "air"})
1211 -- will try again in 2 second
1212 self.path.stuck_timer = stuck_timeout - 2
1214 -- frustration! cant find the damn path :(
1215 mob_sound(self, self.sounds.random)
1216 else
1217 -- yay i found path
1218 mob_sound(self, self.sounds.war_cry)
1220 set_velocity(self, self.walk_velocity)
1222 -- follow path now that it has it
1223 self.path.following = true
1229 -- specific attacks
1230 local specific_attack = function(list, what)
1232 -- no list so attack default (player, animals etc.)
1233 if list == nil then
1234 return true
1237 -- found entity on list to attack?
1238 for no = 1, #list do
1240 if list[no] == what then
1241 return true
1245 return false
1249 -- monster find someone to attack
1250 local monster_attack = function(self)
1252 if self.type ~= "monster"
1253 or not damage_enabled
1254 or creative
1255 or self.state == "attack"
1256 or day_docile(self) then
1257 return
1260 local s = self.object:get_pos()
1261 local p, sp, dist
1262 local player, obj, min_player
1263 local type, name = "", ""
1264 local min_dist = self.view_range + 1
1265 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1267 for n = 1, #objs do
1269 if objs[n]:is_player() then
1271 if mobs.invis[ objs[n]:get_player_name() ] then
1273 type = ""
1274 else
1275 player = objs[n]
1276 type = "player"
1277 name = "player"
1279 else
1280 obj = objs[n]:get_luaentity()
1282 if obj then
1283 player = obj.object
1284 type = obj.type
1285 name = obj.name or ""
1289 -- find specific mob to attack, failing that attack player/npc/animal
1290 if specific_attack(self.specific_attack, name)
1291 and (type == "player" or type == "npc"
1292 or (type == "animal" and self.attack_animals == true)) then
1294 s = self.object:get_pos()
1295 p = player:get_pos()
1296 sp = s
1298 -- aim higher to make looking up hills more realistic
1299 p.y = p.y + 1
1300 sp.y = sp.y + 1
1302 dist = get_distance(p, s)
1304 if dist < self.view_range then
1305 -- field of view check goes here
1307 -- choose closest player to attack
1308 if line_of_sight(self, sp, p, 2) == true
1309 and dist < min_dist then
1310 min_dist = dist
1311 min_player = player
1317 -- attack player
1318 if min_player then
1319 do_attack(self, min_player)
1324 -- npc, find closest monster to attack
1325 local npc_attack = function(self)
1327 if self.type ~= "npc"
1328 or not self.attacks_monsters
1329 or self.state == "attack" then
1330 return
1333 local p, sp, obj, min_player
1334 local s = self.object:get_pos()
1335 local min_dist = self.view_range + 1
1336 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1338 for n = 1, #objs do
1340 obj = objs[n]:get_luaentity()
1342 if obj and obj.type == "monster" then
1344 p = obj.object:get_pos()
1346 dist = get_distance(p, s)
1348 if dist < min_dist then
1349 min_dist = dist
1350 min_player = obj.object
1355 if min_player then
1356 do_attack(self, min_player)
1361 -- specific runaway
1362 local specific_runaway = function(list, what)
1364 -- no list so do not run
1365 if list == nil then
1366 return false
1369 -- found entity on list to attack?
1370 for no = 1, #list do
1372 if list[no] == what or list[no] == "player" then
1373 return true
1377 return false
1381 -- find someone to runaway from
1382 local runaway_from = function(self)
1384 if not self.runaway_from then
1385 return
1388 local s = self.object:get_pos()
1389 local p, sp, dist
1390 local player, obj, min_player
1391 local type, name = "", ""
1392 local min_dist = self.view_range + 1
1393 local objs = minetest.get_objects_inside_radius(s, self.view_range)
1395 for n = 1, #objs do
1397 if objs[n]:is_player() then
1399 if mobs.invis[ objs[n]:get_player_name() ]
1400 or self.owner == objs[n]:get_player_name() then
1402 type = ""
1403 else
1404 player = objs[n]
1405 type = "player"
1406 name = "player"
1408 else
1409 obj = objs[n]:get_luaentity()
1411 if obj then
1412 player = obj.object
1413 type = obj.type
1414 name = obj.name or ""
1418 -- find specific mob to runaway from
1419 if name ~= "" and name ~= self.name
1420 and specific_runaway(self.runaway_from, name) then
1422 s = self.object:get_pos()
1423 p = player:get_pos()
1424 sp = s
1426 -- aim higher to make looking up hills more realistic
1427 p.y = p.y + 1
1428 sp.y = sp.y + 1
1430 dist = get_distance(p, s)
1432 if dist < self.view_range then
1433 -- field of view check goes here
1435 -- choose closest player/mpb to runaway from
1436 if line_of_sight(self, sp, p, 2) == true
1437 and dist < min_dist then
1438 min_dist = dist
1439 min_player = player
1445 if min_player then
1447 local lp = player:get_pos()
1448 local vec = {
1449 x = lp.x - s.x,
1450 y = lp.y - s.y,
1451 z = lp.z - s.z
1454 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
1456 if lp.x > s.x then
1457 yaw = yaw + pi
1460 yaw = set_yaw(self.object, yaw)
1461 self.state = "runaway"
1462 self.runaway_timer = 3
1463 self.following = nil
1468 -- follow player if owner or holding item, if fish outta water then flop
1469 local follow_flop = function(self)
1471 -- find player to follow
1472 if (self.follow ~= ""
1473 or self.order == "follow")
1474 and not self.following
1475 and self.state ~= "attack"
1476 and self.state ~= "runaway" then
1478 local s = self.object:get_pos()
1479 local players = minetest.get_connected_players()
1481 for n = 1, #players do
1483 if get_distance(players[n]:get_pos(), s) < self.view_range
1484 and not mobs.invis[ players[n]:get_player_name() ] then
1486 self.following = players[n]
1488 break
1493 if self.type == "npc"
1494 and self.order == "follow"
1495 and self.state ~= "attack"
1496 and self.owner ~= "" then
1498 -- npc stop following player if not owner
1499 if self.following
1500 and self.owner
1501 and self.owner ~= self.following:get_player_name() then
1502 self.following = nil
1504 else
1505 -- stop following player if not holding specific item
1506 if self.following
1507 and self.following:is_player()
1508 and follow_holding(self, self.following) == false then
1509 self.following = nil
1514 -- follow that thing
1515 if self.following then
1517 local s = self.object:get_pos()
1518 local p
1520 if self.following:is_player() then
1522 p = self.following:get_pos()
1524 elseif self.following.object then
1526 p = self.following.object:get_pos()
1529 if p then
1531 local dist = get_distance(p, s)
1533 -- dont follow if out of range
1534 if dist > self.view_range then
1535 self.following = nil
1536 else
1537 local vec = {
1538 x = p.x - s.x,
1539 z = p.z - s.z
1542 local yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1544 if p.x > s.x then yaw = yaw + pi end
1546 yaw = set_yaw(self.object, yaw)
1548 -- anyone but standing npc's can move along
1549 if dist > self.reach
1550 and self.order ~= "stand" then
1552 set_velocity(self, self.walk_velocity)
1554 if self.walk_chance ~= 0 then
1555 set_animation(self, "walk")
1557 else
1558 set_velocity(self, 0)
1559 set_animation(self, "stand")
1562 return
1567 -- swimmers flop when out of their element, and swim again when back in
1568 if self.fly then
1569 local s = self.object:get_pos()
1570 if not flight_check(self, s) then
1572 self.state = "flop"
1573 self.object:set_velocity({x = 0, y = -5, z = 0})
1575 set_animation(self, "stand")
1577 return
1578 elseif self.state == "flop" then
1579 self.state = "stand"
1585 -- dogshoot attack switch and counter function
1586 local dogswitch = function(self, dtime)
1588 -- switch mode not activated
1589 if not self.dogshoot_switch
1590 or not dtime then
1591 return 0
1594 self.dogshoot_count = self.dogshoot_count + dtime
1596 if (self.dogshoot_switch == 1
1597 and self.dogshoot_count > self.dogshoot_count_max)
1598 or (self.dogshoot_switch == 2
1599 and self.dogshoot_count > self.dogshoot_count2_max) then
1601 self.dogshoot_count = 0
1603 if self.dogshoot_switch == 1 then
1604 self.dogshoot_switch = 2
1605 else
1606 self.dogshoot_switch = 1
1610 return self.dogshoot_switch
1614 -- execute current state (stand, walk, run, attacks)
1615 local do_states = function(self, dtime)
1617 local yaw = self.object:get_yaw() or 0
1619 if self.state == "stand" then
1621 if random(1, 4) == 1 then
1623 local lp = nil
1624 local s = self.object:get_pos()
1625 local objs = minetest.get_objects_inside_radius(s, 3)
1627 for n = 1, #objs do
1629 if objs[n]:is_player() then
1630 lp = objs[n]:get_pos()
1631 break
1635 -- look at any players nearby, otherwise turn randomly
1636 if lp then
1638 local vec = {
1639 x = lp.x - s.x,
1640 z = lp.z - s.z
1643 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1645 if lp.x > s.x then yaw = yaw + pi end
1646 else
1647 yaw = yaw + random(-0.5, 0.5)
1650 yaw = set_yaw(self.object, yaw)
1653 set_velocity(self, 0)
1654 set_animation(self, "stand")
1656 -- npc's ordered to stand stay standing
1657 if self.type ~= "npc"
1658 or self.order ~= "stand" then
1660 if self.walk_chance ~= 0
1661 and self.facing_fence ~= true
1662 and random(1, 100) <= self.walk_chance
1663 and is_at_cliff(self) == false then
1665 set_velocity(self, self.walk_velocity)
1666 self.state = "walk"
1667 set_animation(self, "walk")
1669 -- fly up/down randomly for flying mobs
1670 if self.fly and random(1, 100) <= self.walk_chance then
1672 local v = self.object:get_velocity()
1673 local ud = random(-1, 2) / 9
1675 self.object:set_velocity({x = v.x, y = ud, z = v.z})
1680 elseif self.state == "walk" then
1682 local s = self.object:get_pos()
1683 local lp = nil
1685 -- is there something I need to avoid?
1686 if self.water_damage > 0
1687 and self.lava_damage > 0 then
1689 lp = minetest.find_node_near(s, 1, {"group:water", "group:lava"})
1691 elseif self.water_damage > 0 then
1693 lp = minetest.find_node_near(s, 1, {"group:water"})
1695 elseif self.lava_damage > 0 then
1697 lp = minetest.find_node_near(s, 1, {"group:lava"})
1700 if lp then
1702 -- if mob in water or lava then look for land
1703 if (self.lava_damage
1704 and minetest.registered_nodes[self.standing_in].groups.lava)
1705 or (self.water_damage
1706 and minetest.registered_nodes[self.standing_in].groups.water) then
1708 lp = minetest.find_node_near(s, 5, {"group:soil", "group:stone",
1709 "group:sand", "group:ash", node_ice, node_snowblock})
1711 -- did we find land?
1712 if lp then
1714 local vec = {
1715 x = lp.x - s.x,
1716 z = lp.z - s.z
1719 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1721 if lp.x > s.x then yaw = yaw + pi end
1723 -- look towards land and jump/move in that direction
1724 yaw = set_yaw(self.object, yaw)
1725 do_jump(self)
1726 set_velocity(self, self.walk_velocity)
1727 else
1728 yaw = yaw + random(-0.5, 0.5)
1731 else
1733 local vec = {
1734 x = lp.x - s.x,
1735 z = lp.z - s.z
1738 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1740 if lp.x > s.x then yaw = yaw + pi end
1743 yaw = set_yaw(self.object, yaw)
1745 -- otherwise randomly turn
1746 elseif random(1, 100) <= 30 then
1748 yaw = yaw + random(-0.5, 0.5)
1750 yaw = set_yaw(self.object, yaw)
1753 -- stand for great fall in front
1754 local temp_is_cliff = is_at_cliff(self)
1756 if self.facing_fence == true
1757 or temp_is_cliff
1758 or random(1, 100) <= 30 then
1760 set_velocity(self, 0)
1761 self.state = "stand"
1762 set_animation(self, "stand")
1763 else
1764 set_velocity(self, self.walk_velocity)
1766 if flight_check(self)
1767 and self.animation
1768 and self.animation.fly_start
1769 and self.animation.fly_end then
1770 set_animation(self, "fly")
1771 else
1772 set_animation(self, "walk")
1776 -- runaway when punched
1777 elseif self.state == "runaway" then
1779 self.runaway_timer = self.runaway_timer + 1
1781 -- stop after 5 seconds or when at cliff
1782 if self.runaway_timer > 5
1783 or is_at_cliff(self) then
1784 self.runaway_timer = 0
1785 set_velocity(self, 0)
1786 self.state = "stand"
1787 set_animation(self, "stand")
1788 else
1789 set_velocity(self, self.run_velocity)
1790 set_animation(self, "walk")
1793 -- attack routines (explode, dogfight, shoot, dogshoot)
1794 elseif self.state == "attack" then
1796 -- calculate distance from mob and enemy
1797 local s = self.object:get_pos()
1798 local p = self.attack:get_pos() or s
1799 local dist = get_distance(p, s)
1801 -- stop attacking if player invisible or out of range
1802 if dist > self.view_range
1803 or not self.attack
1804 or not self.attack:get_pos()
1805 or self.attack:get_hp() <= 0
1806 or (self.attack:is_player() and mobs.invis[ self.attack:get_player_name() ]) then
1808 -- print(" ** stop attacking **", dist, self.view_range)
1809 self.state = "stand"
1810 set_velocity(self, 0)
1811 set_animation(self, "stand")
1812 self.attack = nil
1813 self.v_start = false
1814 self.timer = 0
1815 self.blinktimer = 0
1817 return
1820 if self.attack_type == "explode" then
1822 local vec = {
1823 x = p.x - s.x,
1824 z = p.z - s.z
1827 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
1829 if p.x > s.x then yaw = yaw + pi end
1831 yaw = set_yaw(self.object, yaw)
1833 local node_break_radius = self.explosion_radius or 1
1834 local entity_damage_radius = self.explosion_damage_radius
1835 or (node_break_radius * 2)
1837 -- start timer when in reach and line of sight
1838 if not self.v_start
1839 and dist <= self.reach
1840 and line_of_sight(self, s, p, 2) then
1842 self.v_start = true
1843 self.timer = 0
1844 self.blinktimer = 0
1845 mob_sound(self, self.sounds.fuse)
1846 -- print ("=== explosion timer started", self.explosion_timer)
1848 -- stop timer if out of blast radius or direct line of sight
1849 elseif self.allow_fuse_reset
1850 and self.v_start
1851 and (dist > max(self.reach, entity_damage_radius) + 0.5
1852 or not line_of_sight(self, s, p, 2)) then
1853 self.v_start = false
1854 self.timer = 0
1855 self.blinktimer = 0
1856 self.blinkstatus = false
1857 self.object:settexturemod("")
1860 -- walk right up to player unless the timer is active
1861 if self.v_start and (self.stop_to_explode or dist < 1.5) then
1862 set_velocity(self, 0)
1863 else
1864 set_velocity(self, self.run_velocity)
1867 if self.animation and self.animation.run_start then
1868 set_animation(self, "run")
1869 else
1870 set_animation(self, "walk")
1873 if self.v_start then
1875 self.timer = self.timer + dtime
1876 self.blinktimer = (self.blinktimer or 0) + dtime
1878 if self.blinktimer > 0.2 then
1880 self.blinktimer = 0
1882 if self.blinkstatus then
1883 self.object:settexturemod("")
1884 else
1885 self.object:settexturemod("^[brighten")
1888 self.blinkstatus = not self.blinkstatus
1891 -- print ("=== explosion timer", self.timer)
1893 if self.timer > self.explosion_timer then
1895 local pos = self.object:get_pos()
1897 -- dont damage anything if area protected or next to water
1898 if minetest.find_node_near(pos, 1, {"group:water"})
1899 or minetest.is_protected(pos, "") then
1901 node_break_radius = 0
1904 self.object:remove()
1906 if minetest.get_modpath("tnt") and tnt and tnt.boom
1907 and not minetest.is_protected(pos, "") then
1909 tnt.boom(pos, {
1910 radius = node_break_radius,
1911 damage_radius = entity_damage_radius,
1912 sound = self.sounds.explode,
1914 else
1916 minetest.sound_play(self.sounds.explode, {
1917 pos = pos,
1918 gain = 1.0,
1919 max_hear_distance = self.sounds.distance or 32
1922 entity_physics(pos, entity_damage_radius)
1923 effect(pos, 32, "tnt_smoke.png", nil, nil, node_break_radius, 1, 0)
1926 return
1930 elseif self.attack_type == "dogfight"
1931 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 2)
1932 or (self.attack_type == "dogshoot" and dist <= self.reach and dogswitch(self) == 0) then
1934 if self.fly
1935 and dist > self.reach then
1937 local p1 = s
1938 local me_y = floor(p1.y)
1939 local p2 = p
1940 local p_y = floor(p2.y + 1)
1941 local v = self.object:get_velocity()
1943 if flight_check(self, s) then
1945 if me_y < p_y then
1947 self.object:set_velocity({
1948 x = v.x,
1949 y = 1 * self.walk_velocity,
1950 z = v.z
1953 elseif me_y > p_y then
1955 self.object:set_velocity({
1956 x = v.x,
1957 y = -1 * self.walk_velocity,
1958 z = v.z
1961 else
1962 if me_y < p_y then
1964 self.object:set_velocity({
1965 x = v.x,
1966 y = 0.01,
1967 z = v.z
1970 elseif me_y > p_y then
1972 self.object:set_velocity({
1973 x = v.x,
1974 y = -0.01,
1975 z = v.z
1982 -- rnd: new movement direction
1983 if self.path.following
1984 and self.path.way
1985 and self.attack_type ~= "dogshoot" then
1987 -- no paths longer than 50
1988 if #self.path.way > 50
1989 or dist < self.reach then
1990 self.path.following = false
1991 return
1994 local p1 = self.path.way[1]
1996 if not p1 then
1997 self.path.following = false
1998 return
2001 if abs(p1.x-s.x) + abs(p1.z - s.z) < 0.6 then
2002 -- reached waypoint, remove it from queue
2003 table.remove(self.path.way, 1)
2006 -- set new temporary target
2007 p = {x = p1.x, y = p1.y, z = p1.z}
2010 local vec = {
2011 x = p.x - s.x,
2012 z = p.z - s.z
2015 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2017 if p.x > s.x then yaw = yaw + pi end
2019 yaw = set_yaw(self.object, yaw)
2021 -- move towards enemy if beyond mob reach
2022 if dist > self.reach then
2024 -- path finding by rnd
2025 if self.pathfinding -- only if mob has pathfinding enabled
2026 and enable_pathfinding then
2028 smart_mobs(self, s, p, dist, dtime)
2031 if is_at_cliff(self) then
2033 set_velocity(self, 0)
2034 set_animation(self, "stand")
2035 else
2037 if self.path.stuck then
2038 set_velocity(self, self.walk_velocity)
2039 else
2040 set_velocity(self, self.run_velocity)
2043 if self.animation and self.animation.run_start then
2044 set_animation(self, "run")
2045 else
2046 set_animation(self, "walk")
2050 else -- rnd: if inside reach range
2052 self.path.stuck = false
2053 self.path.stuck_timer = 0
2054 self.path.following = false -- not stuck anymore
2056 set_velocity(self, 0)
2058 if not self.custom_attack then
2060 if self.timer > 1 then
2062 self.timer = 0
2064 if self.double_melee_attack
2065 and random(1, 2) == 1 then
2066 set_animation(self, "punch2")
2067 else
2068 set_animation(self, "punch")
2071 local p2 = p
2072 local s2 = s
2074 p2.y = p2.y + .5
2075 s2.y = s2.y + .5
2077 if line_of_sight(self, p2, s2) == true then
2079 -- play attack sound
2080 mob_sound(self, self.sounds.attack)
2082 -- punch player (or what player is attached to)
2083 local attached = self.attack:get_attach()
2084 if attached then
2085 self.attack = attached
2087 self.attack:punch(self.object, 1.0, {
2088 full_punch_interval = 1.0,
2089 damage_groups = {fleshy = self.damage}
2090 }, nil)
2093 else -- call custom attack every second
2094 if self.custom_attack
2095 and self.timer > 1 then
2097 self.timer = 0
2099 self.custom_attack(self, p)
2104 elseif self.attack_type == "shoot"
2105 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 1)
2106 or (self.attack_type == "dogshoot" and dist > self.reach and dogswitch(self) == 0) then
2108 p.y = p.y - .5
2109 s.y = s.y + .5
2111 local dist = get_distance(p, s)
2112 local vec = {
2113 x = p.x - s.x,
2114 y = p.y - s.y,
2115 z = p.z - s.z
2118 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2120 if p.x > s.x then yaw = yaw + pi end
2122 yaw = set_yaw(self.object, yaw)
2124 set_velocity(self, 0)
2126 if self.shoot_interval
2127 and self.timer > self.shoot_interval
2128 and random(1, 100) <= 60 then
2130 self.timer = 0
2131 set_animation(self, "shoot")
2133 -- play shoot attack sound
2134 mob_sound(self, self.sounds.shoot_attack)
2136 local p = self.object:get_pos()
2138 p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
2140 if minetest.registered_entities[self.arrow] then
2142 local obj = minetest.add_entity(p, self.arrow)
2143 local ent = obj:get_luaentity()
2144 local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5
2145 local v = ent.velocity or 1 -- or set to default
2147 ent.switch = 1
2148 ent.owner_id = tostring(self.object) -- add unique owner id to arrow
2150 -- offset makes shoot aim accurate
2151 vec.y = vec.y + self.shoot_offset
2152 vec.x = vec.x * (v / amount)
2153 vec.y = vec.y * (v / amount)
2154 vec.z = vec.z * (v / amount)
2156 obj:set_velocity(vec)
2164 -- falling and fall damage
2165 local falling = function(self, pos)
2167 if self.fly then
2168 return
2171 -- floating in water (or falling)
2172 local v = self.object:get_velocity()
2174 if v.y > 0 then
2176 -- apply gravity when moving up
2177 self.object:set_acceleration({
2178 x = 0,
2179 y = -10,
2180 z = 0
2183 elseif v.y <= 0 and v.y > self.fall_speed then
2185 -- fall downwards at set speed
2186 self.object:set_acceleration({
2187 x = 0,
2188 y = self.fall_speed,
2189 z = 0
2191 else
2192 -- stop accelerating once max fall speed hit
2193 self.object:set_acceleration({x = 0, y = 0, z = 0})
2196 -- in water then float up
2197 -- if minetest.registered_nodes[node_ok(pos).name].groups.liquid then
2198 if minetest.registered_nodes[node_ok(pos).name].groups.water then
2200 if self.floats == 1 then
2202 self.object:set_acceleration({
2203 x = 0,
2204 y = -self.fall_speed / (max(1, v.y) ^ 2),
2205 z = 0
2208 else
2210 -- fall damage onto solid ground
2211 if self.fall_damage == 1
2212 and self.object:get_velocity().y == 0 then
2214 local d = (self.old_y or 0) - self.object:get_pos().y
2216 if d > 5 then
2218 self.health = self.health - floor(d - 5)
2220 effect(pos, 5, "tnt_smoke.png", 1, 2, 2, nil)
2222 if check_for_death(self, "fall", {type = "fall"}) then
2223 return
2227 self.old_y = self.object:get_pos().y
2233 -- deal damage and effects when mob punched
2234 local mob_punch = function(self, hitter, tflp, tool_capabilities, dir)
2236 -- custom punch function
2237 if self.do_punch then
2239 -- when false skip going any further
2240 if self.do_punch(self, hitter, tflp, tool_caps, dir) == false then
2241 return
2245 -- mob health check
2246 -- if self.health <= 0 then
2247 -- return
2248 -- end
2250 -- error checking when mod profiling is enabled
2251 if not tool_capabilities then
2252 minetest.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2253 return
2256 -- is mob protected?
2257 if self.protected and hitter:is_player()
2258 and minetest.is_protected(self.object:get_pos(), hitter:get_player_name()) then
2259 minetest.chat_send_player(hitter:get_player_name(), S("Mob has been protected!"))
2260 return
2264 -- weapon wear
2265 local weapon = hitter:get_wielded_item()
2266 local punch_interval = 1.4
2268 -- calculate mob damage
2269 local damage = 0
2270 local armor = self.object:get_armor_groups() or {}
2271 local tmp
2273 -- quick error check incase it ends up 0 (serialize.h check test)
2274 if tflp == 0 then
2275 tflp = 0.2
2278 if use_cmi then
2279 damage = cmi.calculate_damage(self.object, hitter, tflp, tool_capabilities, dir)
2280 else
2282 for group,_ in pairs( (tool_capabilities.damage_groups or {}) ) do
2284 tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)
2286 if tmp < 0 then
2287 tmp = 0.0
2288 elseif tmp > 1 then
2289 tmp = 1.0
2292 damage = damage + (tool_capabilities.damage_groups[group] or 0)
2293 * tmp * ((armor[group] or 0) / 100.0)
2297 -- check for tool immunity or special damage
2298 for n = 1, #self.immune_to do
2300 if self.immune_to[n][1] == weapon:get_name() then
2302 damage = self.immune_to[n][2] or 0
2303 break
2307 -- healing
2308 if damage <= -1 then
2309 self.health = self.health - floor(damage)
2310 return
2313 -- print ("Mob Damage is", damage)
2315 if use_cmi then
2317 local cancel = cmi.notify_punch(self.object, hitter, tflp, tool_capabilities, dir, damage)
2319 if cancel then return end
2322 -- add weapon wear
2323 if tool_capabilities then
2324 punch_interval = tool_capabilities.full_punch_interval or 1.4
2327 if weapon:get_definition()
2328 and weapon:get_definition().tool_capabilities then
2330 weapon:add_wear(floor((punch_interval / 75) * 9000))
2331 hitter:set_wielded_item(weapon)
2334 -- only play hit sound and show blood effects if damage is 1 or over
2335 if damage >= 1 then
2337 -- weapon sounds
2338 if weapon:get_definition().sounds ~= nil then
2340 local s = random(0, #weapon:get_definition().sounds)
2342 minetest.sound_play(weapon:get_definition().sounds[s], {
2343 object = self.object, --hitter,
2344 max_hear_distance = 8
2346 else
2347 minetest.sound_play("default_punch", {
2348 object = self.object, --hitter,
2349 max_hear_distance = 5
2353 -- blood_particles
2354 if self.blood_amount > 0
2355 and not disable_blood then
2357 local pos = self.object:get_pos()
2359 pos.y = pos.y + (-self.collisionbox[2] + self.collisionbox[5]) * .5
2361 -- do we have a single blood texture or multiple?
2362 if type(self.blood_texture) == "table" then
2364 local blood = self.blood_texture[random(1, #self.blood_texture)]
2366 effect(pos, self.blood_amount, blood, nil, nil, 1, nil)
2367 else
2368 effect(pos, self.blood_amount, self.blood_texture, nil, nil, 1, nil)
2372 -- do damage
2373 self.health = self.health - floor(damage)
2375 -- exit here if dead, special item check
2376 if weapon:get_name() == "mobs:pick_lava" then
2377 if check_for_death(self, "lava", {type = "punch",
2378 puncher = hitter}) then
2379 return
2381 else
2382 if check_for_death(self, "hit", {type = "punch",
2383 puncher = hitter}) then
2384 return
2388 --[[ add healthy afterglow when hit (can cause hit lag with larger textures)
2389 core.after(0.1, function()
2390 self.object:settexturemod("^[colorize:#c9900070")
2392 core.after(0.3, function()
2393 self.object:settexturemod("")
2394 end)
2395 end) ]]
2397 -- knock back effect (only on full punch)
2398 if self.knock_back > 0
2399 and tflp >= punch_interval then
2401 local v = self.object:get_velocity()
2402 local r = 1.4 - min(punch_interval, 1.4)
2403 local kb = r * 5
2404 local up = 2
2406 -- if already in air then dont go up anymore when hit
2407 if v.y > 0
2408 or self.fly then
2409 up = 0
2412 -- direction error check
2413 dir = dir or {x = 0, y = 0, z = 0}
2415 -- check if tool already has specific knockback value
2416 if tool_capabilities.damage_groups["knockback"] then
2417 kb = tool_capabilities.damage_groups["knockback"]
2418 else
2419 kb = kb * 1.5
2422 self.object:set_velocity({
2423 x = dir.x * kb,
2424 y = up,
2425 z = dir.z * kb
2428 self.pause_timer = 0.25
2430 end -- END if damage
2432 -- if skittish then run away
2433 if self.runaway == true then
2435 local lp = hitter:get_pos()
2436 local s = self.object:get_pos()
2437 local vec = {
2438 x = lp.x - s.x,
2439 y = lp.y - s.y,
2440 z = lp.z - s.z
2443 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
2445 if lp.x > s.x then
2446 yaw = yaw + pi
2449 yaw = set_yaw(self.object, yaw)
2450 self.state = "runaway"
2451 self.runaway_timer = 0
2452 self.following = nil
2455 local name = hitter:get_player_name() or ""
2457 -- attack puncher and call other mobs for help
2458 if self.passive == false
2459 and self.state ~= "flop"
2460 and self.child == false
2461 and hitter:get_player_name() ~= self.owner
2462 and not mobs.invis[ name ] then
2464 -- attack whoever punched mob
2465 self.state = ""
2466 do_attack(self, hitter)
2468 -- alert others to the attack
2469 local objs = minetest.get_objects_inside_radius(hitter:get_pos(), self.view_range)
2470 local obj = nil
2472 for n = 1, #objs do
2474 obj = objs[n]:get_luaentity()
2476 if obj then
2478 -- only alert members of same mob
2479 if obj.group_attack == true
2480 and obj.state ~= "attack"
2481 and obj.owner ~= name
2482 and obj.name == self.name then
2483 do_attack(obj, hitter)
2486 -- have owned mobs attack player threat
2487 if obj.owner == name and obj.owner_loyal then
2488 do_attack(obj, self.object)
2496 -- get entity staticdata
2497 local mob_staticdata = function(self)
2499 -- remove mob when out of range unless tamed
2500 if remove_far
2501 and self.remove_ok
2502 and self.type ~= "npc"
2503 and self.state ~= "attack"
2504 and not self.tamed
2505 and self.lifetimer < 20000 then
2507 --print ("REMOVED " .. self.name)
2509 self.object:remove()
2511 return ""-- nil
2514 self.remove_ok = true
2515 self.attack = nil
2516 self.following = nil
2517 self.state = "stand"
2519 -- used to rotate older mobs
2520 if self.drawtype
2521 and self.drawtype == "side" then
2522 self.rotate = math.rad(90)
2525 if use_cmi then
2526 self.serialized_cmi_components = cmi.serialize_components(self._cmi_components)
2529 local tmp = {}
2531 for _,stat in pairs(self) do
2533 local t = type(stat)
2535 if t ~= "function"
2536 and t ~= "nil"
2537 and t ~= "userdata"
2538 and _ ~= "_cmi_components" then
2539 tmp[_] = self[_]
2543 --print('===== '..self.name..'\n'.. dump(tmp)..'\n=====\n')
2544 return minetest.serialize(tmp)
2548 -- activate mob and reload settings
2549 local mob_activate = function(self, staticdata, def, dtime)
2551 -- remove monsters in peaceful mode
2552 if self.type == "monster"
2553 and peaceful_only then
2555 self.object:remove()
2557 return
2560 -- load entity variables
2561 local tmp = minetest.deserialize(staticdata)
2563 if tmp then
2564 for _,stat in pairs(tmp) do
2565 self[_] = stat
2569 -- select random texture, set model and size
2570 if not self.base_texture then
2572 -- compatiblity with old simple mobs textures
2573 if type(def.textures[1]) == "string" then
2574 def.textures = {def.textures}
2577 self.base_texture = def.textures[random(1, #def.textures)]
2578 self.base_mesh = def.mesh
2579 self.base_size = self.visual_size
2580 self.base_colbox = self.collisionbox
2581 self.base_selbox = self.selectionbox
2584 -- for current mobs that dont have this set
2585 if not self.base_selbox then
2586 self.base_selbox = self.selectionbox or self.base_colbox
2589 -- set texture, model and size
2590 local textures = self.base_texture
2591 local mesh = self.base_mesh
2592 local vis_size = self.base_size
2593 local colbox = self.base_colbox
2594 local selbox = self.base_selbox
2596 -- specific texture if gotten
2597 if self.gotten == true
2598 and def.gotten_texture then
2599 textures = def.gotten_texture
2602 -- specific mesh if gotten
2603 if self.gotten == true
2604 and def.gotten_mesh then
2605 mesh = def.gotten_mesh
2608 -- set child objects to half size
2609 if self.child == true then
2611 vis_size = {
2612 x = self.base_size.x * .5,
2613 y = self.base_size.y * .5,
2616 if def.child_texture then
2617 textures = def.child_texture[1]
2620 colbox = {
2621 self.base_colbox[1] * .5,
2622 self.base_colbox[2] * .5,
2623 self.base_colbox[3] * .5,
2624 self.base_colbox[4] * .5,
2625 self.base_colbox[5] * .5,
2626 self.base_colbox[6] * .5
2628 selbox = {
2629 self.base_selbox[1] * .5,
2630 self.base_selbox[2] * .5,
2631 self.base_selbox[3] * .5,
2632 self.base_selbox[4] * .5,
2633 self.base_selbox[5] * .5,
2634 self.base_selbox[6] * .5
2638 if self.health == 0 then
2639 self.health = random (self.hp_min, self.hp_max)
2642 -- pathfinding init
2643 self.path = {}
2644 self.path.way = {} -- path to follow, table of positions
2645 self.path.lastpos = {x = 0, y = 0, z = 0}
2646 self.path.stuck = false
2647 self.path.following = false -- currently following path?
2648 self.path.stuck_timer = 0 -- if stuck for too long search for path
2650 -- mob defaults
2651 self.object:set_armor_groups({immortal = 1, fleshy = self.armor})
2652 self.old_y = self.object:get_pos().y
2653 self.old_health = self.health
2654 self.sounds.distance = self.sounds.distance or 10
2655 self.textures = textures
2656 self.mesh = mesh
2657 self.collisionbox = colbox
2658 self.selectionbox = selbox
2659 self.visual_size = vis_size
2660 self.standing_in = ""
2662 -- check existing nametag
2663 if not self.nametag then
2664 self.nametag = def.nametag
2667 -- set anything changed above
2668 self.object:set_properties(self)
2669 set_yaw(self.object, (random(0, 360) - 180) / 180 * pi)
2670 update_tag(self)
2671 set_animation(self, "stand")
2673 -- run on_spawn function if found
2674 if self.on_spawn and not self.on_spawn_run then
2675 if self.on_spawn(self) then
2676 self.on_spawn_run = true -- if true, set flag to run once only
2680 -- run after_activate
2681 if def.after_activate then
2682 def.after_activate(self, staticdata, def, dtime)
2685 if use_cmi then
2686 self._cmi_components = cmi.activate_components(self.serialized_cmi_components)
2687 cmi.notify_activate(self.object, dtime)
2692 -- main mob function
2693 local mob_step = function(self, dtime)
2695 if use_cmi then
2696 cmi.notify_step(self.object, dtime)
2699 local pos = self.object:get_pos()
2700 local yaw = 0
2702 -- when lifetimer expires remove mob (except npc and tamed)
2703 if self.type ~= "npc"
2704 and not self.tamed
2705 and self.state ~= "attack"
2706 and remove_far ~= true
2707 and self.lifetimer < 20000 then
2709 self.lifetimer = self.lifetimer - dtime
2711 if self.lifetimer <= 0 then
2713 -- only despawn away from player
2714 local objs = minetest.get_objects_inside_radius(pos, 15)
2716 for n = 1, #objs do
2718 if objs[n]:is_player() then
2720 self.lifetimer = 20
2722 return
2726 -- minetest.log("action",
2727 -- S("lifetimer expired, removed @1", self.name))
2729 effect(pos, 15, "tnt_smoke.png", 2, 4, 2, 0)
2731 self.object:remove()
2733 return
2737 falling(self, pos)
2739 -- knockback timer
2740 if self.pause_timer > 0 then
2742 self.pause_timer = self.pause_timer - dtime
2744 return
2747 -- run custom function (defined in mob lua file)
2748 if self.do_custom then
2750 -- when false skip going any further
2751 if self.do_custom(self, dtime) == false then
2752 return
2756 -- attack timer
2757 self.timer = self.timer + dtime
2759 if self.state ~= "attack" then
2761 if self.timer < 1 then
2762 return
2765 self.timer = 0
2768 -- never go over 100
2769 if self.timer > 100 then
2770 self.timer = 1
2773 -- node replace check (cow eats grass etc.)
2774 replace(self, pos)
2776 -- mob plays random sound at times
2777 if random(1, 100) == 1 then
2778 mob_sound(self, self.sounds.random)
2781 -- environmental damage timer (every 1 second)
2782 self.env_damage_timer = self.env_damage_timer + dtime
2784 if (self.state == "attack" and self.env_damage_timer > 1)
2785 or self.state ~= "attack" then
2787 self.env_damage_timer = 0
2789 do_env_damage(self)
2792 monster_attack(self)
2794 npc_attack(self)
2796 breed(self)
2798 follow_flop(self)
2800 do_states(self, dtime)
2802 do_jump(self)
2804 runaway_from(self)
2809 -- default function when mobs are blown up with TNT
2810 local do_tnt = function(obj, damage)
2812 --print ("----- Damage", damage)
2814 obj.object:punch(obj.object, 1.0, {
2815 full_punch_interval = 1.0,
2816 damage_groups = {fleshy = damage},
2817 }, nil)
2819 return false, true, {}
2823 mobs.spawning_mobs = {}
2825 -- register mob entity
2826 function mobs:register_mob(name, def)
2828 mobs.spawning_mobs[name] = true
2830 minetest.register_entity(name, {
2832 stepheight = def.stepheight or 1.1, -- was 0.6
2833 name = name,
2834 type = def.type,
2835 attack_type = def.attack_type,
2836 fly = def.fly,
2837 fly_in = def.fly_in or "air",
2838 owner = def.owner or "",
2839 order = def.order or "",
2840 on_die = def.on_die,
2841 do_custom = def.do_custom,
2842 jump_height = def.jump_height or 4, -- was 6
2843 drawtype = def.drawtype, -- DEPRECATED, use rotate instead
2844 rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
2845 lifetimer = def.lifetimer or 180, -- 3 minutes
2846 hp_min = max(1, (def.hp_min or 5) * difficulty),
2847 hp_max = max(1, (def.hp_max or 10) * difficulty),
2848 physical = true,
2849 collisionbox = def.collisionbox,
2850 selectionbox = def.selectionbox or def.collisionbox,
2851 visual = def.visual,
2852 visual_size = def.visual_size or {x = 1, y = 1},
2853 mesh = def.mesh,
2854 makes_footstep_sound = def.makes_footstep_sound or false,
2855 view_range = def.view_range or 5,
2856 walk_velocity = def.walk_velocity or 1,
2857 run_velocity = def.run_velocity or 2,
2858 damage = max(0, (def.damage or 0) * difficulty),
2859 light_damage = def.light_damage or 0,
2860 water_damage = def.water_damage or 0,
2861 lava_damage = def.lava_damage or 0,
2862 suffocation = def.suffocation or 2,
2863 fall_damage = def.fall_damage or 1,
2864 fall_speed = def.fall_speed or -10, -- must be lower than -2 (hades_core: -10)
2865 drops = def.drops or {},
2866 armor = def.armor or 100,
2867 on_rightclick = def.on_rightclick,
2868 arrow = def.arrow,
2869 shoot_interval = def.shoot_interval,
2870 sounds = def.sounds or {},
2871 animation = def.animation,
2872 follow = def.follow,
2873 jump = def.jump ~= false,
2874 walk_chance = def.walk_chance or 50,
2875 attacks_monsters = def.attacks_monsters or false,
2876 group_attack = def.group_attack or false,
2877 passive = def.passive or false,
2878 knock_back = def.knock_back or 3,
2879 blood_amount = def.blood_amount or 5,
2880 blood_texture = def.blood_texture or "mobs_blood.png",
2881 shoot_offset = def.shoot_offset or 0,
2882 floats = def.floats or 1, -- floats in water by default
2883 replace_rate = def.replace_rate,
2884 replace_what = def.replace_what,
2885 replace_with = def.replace_with,
2886 replace_offset = def.replace_offset or 0,
2887 on_replace = def.on_replace,
2888 timer = 0,
2889 env_damage_timer = 0, -- only used when state = "attack"
2890 tamed = false,
2891 pause_timer = 0,
2892 horny = false,
2893 hornytimer = 0,
2894 child = false,
2895 gotten = false,
2896 health = 0,
2897 reach = def.reach or 3,
2898 htimer = 0,
2899 texture_list = def.textures,
2900 child_texture = def.child_texture,
2901 docile_by_day = def.docile_by_day or false,
2902 time_of_day = 0.5,
2903 fear_height = def.fear_height or 0,
2904 runaway = def.runaway,
2905 runaway_timer = 0,
2906 pathfinding = def.pathfinding,
2907 immune_to = def.immune_to or {},
2908 explosion_radius = def.explosion_radius,
2909 explosion_damage_radius = def.explosion_damage_radius,
2910 explosion_timer = def.explosion_timer or 3,
2911 allow_fuse_reset = def.allow_fuse_reset ~= false,
2912 stop_to_explode = def.stop_to_explode ~= false,
2913 custom_attack = def.custom_attack,
2914 double_melee_attack = def.double_melee_attack,
2915 dogshoot_switch = def.dogshoot_switch,
2916 dogshoot_count = 0,
2917 dogshoot_count_max = def.dogshoot_count_max or 5,
2918 dogshoot_count2_max = def.dogshoot_count2_max or (def.dogshoot_count_max or 5),
2919 attack_animals = def.attack_animals or false,
2920 specific_attack = def.specific_attack,
2921 runaway_from = def.runaway_from,
2922 owner_loyal = def.owner_loyal,
2923 facing_fence = false,
2924 _cmi_is_mob = true,
2926 on_spawn = def.on_spawn,
2928 on_blast = def.on_blast or do_tnt,
2930 on_step = mob_step,
2932 do_punch = def.do_punch,
2934 on_punch = mob_punch,
2936 on_breed = def.on_breed,
2938 on_grown = def.on_grown,
2940 on_activate = function(self, staticdata, dtime)
2941 return mob_activate(self, staticdata, def, dtime)
2942 end,
2944 get_staticdata = function(self)
2945 return mob_staticdata(self)
2946 end,
2950 end -- END mobs:register_mob function
2953 -- count how many mobs of one type are inside an area
2954 local count_mobs = function(pos, type)
2956 local num_type = 0
2957 local num_total = 0
2958 local objs = minetest.get_objects_inside_radius(pos, aoc_range)
2960 for n = 1, #objs do
2962 if not objs[n]:is_player() then
2964 local obj = objs[n]:get_luaentity()
2966 -- count mob type and add to total also
2967 if obj and obj.name and obj.name == type then
2969 num_type = num_type + 1
2970 num_total = num_total + 1
2972 -- add to total mobs
2973 elseif obj and obj.name and obj.health ~= nil then
2975 num_total = num_total + 1
2980 return num_type, num_total
2984 -- global functions
2986 function mobs:spawn_abm_check(pos, node, name)
2987 -- global function to add additional spawn checks
2988 -- return true to stop spawning mob
2992 function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
2993 interval, chance, aoc, min_height, max_height, day_toggle, on_spawn)
2995 -- Do mobs spawn at all?
2996 if not mobs_spawn then
2997 return
3000 -- chance/spawn number override in minetest.conf for registered mob
3001 local numbers = minetest.settings:get(name)
3003 if numbers then
3004 numbers = numbers:split(",")
3005 chance = tonumber(numbers[1]) or chance
3006 aoc = tonumber(numbers[2]) or aoc
3008 if chance == 0 then
3009 minetest.log("warning", string.format("[mobs] %s has spawning disabled", name))
3010 return
3013 minetest.log("action",
3014 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name, chance, aoc))
3018 minetest.register_abm({
3020 label = name .. " spawning",
3021 nodenames = nodes,
3022 neighbors = neighbors,
3023 interval = interval,
3024 chance = max(1, (chance * mob_chance_multiplier)),
3025 catch_up = false,
3027 action = function(pos, node, active_object_count, active_object_count_wider)
3029 -- is mob actually registered?
3030 if not mobs.spawning_mobs[name]
3031 or not minetest.registered_entities[name] then
3032 --print ("--- mob doesn't exist", name)
3033 return
3036 -- additional custom checks for spawning mob
3037 if mobs:spawn_abm_check(pos, node, name) == true then
3038 return
3041 -- do not spawn if too many of same mob in area
3042 if active_object_count_wider >= max_per_block
3043 or count_mobs(pos, name) >= aoc then
3044 --print ("--- too many entities", name, aoc, active_object_count_wider)
3045 return
3048 -- if toggle set to nil then ignore day/night check
3049 if day_toggle ~= nil then
3051 local tod = (minetest.get_timeofday() or 0) * 24000
3053 if tod > 4500 and tod < 19500 then
3054 -- daylight, but mob wants night
3055 if day_toggle == false then
3056 --print ("--- mob needs night", name)
3057 return
3059 else
3060 -- night time but mob wants day
3061 if day_toggle == true then
3062 --print ("--- mob needs day", name)
3063 return
3068 -- spawn above node
3069 pos.y = pos.y + 1
3071 -- only spawn away from player
3072 local objs = minetest.get_objects_inside_radius(pos, 10)
3074 for n = 1, #objs do
3076 if objs[n]:is_player() then
3077 --print ("--- player too close", name)
3078 return
3082 -- mobs cannot spawn in protected areas when enabled
3083 if not spawn_protected
3084 and minetest.is_protected(pos, "") then
3085 --print ("--- inside protected area", name)
3086 return
3089 -- are we spawning within height limits?
3090 if pos.y > max_height
3091 or pos.y < min_height then
3092 --print ("--- height limits not met", name, pos.y)
3093 return
3096 -- are light levels ok?
3097 local light = minetest.get_node_light(pos)
3098 if not light
3099 or light > max_light
3100 or light < min_light then
3101 --print ("--- light limits not met", name, light)
3102 return
3105 -- do we have enough height clearance to spawn mob?
3106 local ent = minetest.registered_entities[name]
3107 local height = max(0, math.ceil(ent.collisionbox[5] - ent.collisionbox[2]) - 1)
3109 for n = 0, height do
3111 local pos2 = {x = pos.x, y = pos.y + n, z = pos.z}
3113 if minetest.registered_nodes[node_ok(pos2).name].walkable == true then
3114 --print ("--- inside block", name, node_ok(pos2).name)
3115 return
3119 -- spawn mob half block higher than ground
3120 pos.y = pos.y + 0.5
3122 local mob = minetest.add_entity(pos, name)
3123 --[[
3124 print ("[mobs] Spawned " .. name .. " at "
3125 .. minetest.pos_to_string(pos) .. " on "
3126 .. node.name .. " near " .. neighbors[1])
3128 if on_spawn then
3130 local ent = mob:get_luaentity()
3132 on_spawn(ent, pos)
3139 -- compatibility with older mob registration
3140 function mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
3142 mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
3143 chance, active_object_count, -31000, max_height, day_toggle)
3147 -- MarkBu's spawn function
3148 function mobs:spawn(def)
3150 local name = def.name
3151 local nodes = def.nodes or {"group:soil", "group:stone"}
3152 local neighbors = def.neighbors or {"air"}
3153 local min_light = def.min_light or 0
3154 local max_light = def.max_light or 15
3155 local interval = def.interval or 30
3156 local chance = def.chance or 5000
3157 local active_object_count = def.active_object_count or 1
3158 local min_height = def.min_height or -31000
3159 local max_height = def.max_height or 31000
3160 local day_toggle = def.day_toggle
3161 local on_spawn = def.on_spawn
3163 mobs:spawn_specific(name, nodes, neighbors, min_light, max_light, interval,
3164 chance, active_object_count, min_height, max_height, day_toggle, on_spawn)
3168 -- register arrow for shoot attack
3169 function mobs:register_arrow(name, def)
3171 if not name or not def then return end -- errorcheck
3173 minetest.register_entity(name, {
3175 physical = false,
3176 visual = def.visual,
3177 visual_size = def.visual_size,
3178 textures = def.textures,
3179 velocity = def.velocity,
3180 hit_player = def.hit_player,
3181 hit_node = def.hit_node,
3182 hit_mob = def.hit_mob,
3183 drop = def.drop or false, -- drops arrow as registered item when true
3184 collisionbox = {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3185 timer = 0,
3186 switch = 0,
3187 owner_id = def.owner_id,
3188 rotate = def.rotate,
3189 automatic_face_movement_dir = def.rotate
3190 and (def.rotate - (pi / 180)) or false,
3192 on_activate = def.on_activate,
3194 on_step = def.on_step or function(self, dtime)
3196 self.timer = self.timer + 1
3198 local pos = self.object:get_pos()
3200 if self.switch == 0
3201 or self.timer > 150
3202 or not within_limits(pos, 0) then
3204 self.object:remove() ; -- print ("removed arrow")
3206 return
3209 -- does arrow have a tail (fireball)
3210 if def.tail
3211 and def.tail == 1
3212 and def.tail_texture then
3214 minetest.add_particle({
3215 pos = pos,
3216 velocity = {x = 0, y = 0, z = 0},
3217 acceleration = {x = 0, y = 0, z = 0},
3218 expirationtime = def.expire or 0.25,
3219 collisiondetection = false,
3220 texture = def.tail_texture,
3221 size = def.tail_size or 5,
3222 glow = def.glow or 0,
3226 if self.hit_node then
3228 local node = node_ok(pos).name
3230 if minetest.registered_nodes[node].walkable then
3232 self.hit_node(self, pos, node)
3234 if self.drop == true then
3236 pos.y = pos.y + 1
3238 self.lastpos = (self.lastpos or pos)
3240 minetest.add_item(self.lastpos, self.object:get_luaentity().name)
3243 self.object:remove() ; -- print ("hit node")
3245 return
3249 if self.hit_player or self.hit_mob then
3251 for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do
3253 if self.hit_player
3254 and player:is_player() then
3256 self.hit_player(self, player)
3257 self.object:remove() ; -- print ("hit player")
3258 return
3261 local entity = player:get_luaentity()
3263 if entity
3264 and self.hit_mob
3265 and entity._cmi_is_mob == true
3266 and tostring(player) ~= self.owner_id
3267 and entity.name ~= self.object:get_luaentity().name then
3269 self.hit_mob(self, player)
3271 self.object:remove() ; --print ("hit mob")
3273 return
3278 self.lastpos = pos
3284 -- compatibility function
3285 function mobs:explosion(pos, radius)
3286 local self = {sounds = {}}
3287 self.sounds.explode = "tnt_explode"
3288 mobs:boom(self, pos, radius)
3292 -- no damage to nodes explosion
3293 function mobs:safe_boom(self, pos, radius)
3295 minetest.sound_play(self.sounds and self.sounds.explode or "tnt_explode", {
3296 pos = pos,
3297 gain = 1.0,
3298 max_hear_distance = self.sounds and self.sounds.distance or 32
3301 entity_physics(pos, radius)
3302 effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
3306 -- make explosion with protection and tnt mod check
3307 function mobs:boom(self, pos, radius)
3309 if mobs_griefing
3310 and minetest.get_modpath("tnt") and tnt and tnt.boom
3311 and not minetest.is_protected(pos, "") then
3313 tnt.boom(pos, {
3314 radius = radius,
3315 damage_radius = radius,
3316 sound = self.sounds and self.sounds.explode,
3317 explode_center = true,
3319 else
3320 mobs:safe_boom(self, pos, radius)
3325 -- Register spawn eggs
3327 -- Note: This also introduces the “spawn_egg” group:
3328 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3329 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3330 function mobs:register_egg(mob, desc, background, addegg, no_creative)
3332 local grp = {spawn_egg = 1}
3334 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3335 if creative and no_creative == true then
3336 grp.not_in_creative_inventory = 1
3339 local invimg = background
3341 if addegg == 1 then
3342 invimg = "mobs_chicken_egg.png^(" .. invimg ..
3343 "^[mask:mobs_chicken_egg_overlay.png)"
3346 -- register new spawn egg containing mob information
3347 minetest.register_craftitem(mob .. "_set", {
3349 description = S("@1 (Tamed)", desc),
3350 inventory_image = invimg,
3351 groups = {spawn_egg = 2, not_in_creative_inventory = 1},
3352 stack_max = 1,
3354 on_place = function(itemstack, placer, pointed_thing)
3356 local pos = pointed_thing.above
3358 -- am I clicking on something with existing on_rightclick function?
3359 local under = minetest.get_node(pointed_thing.under)
3360 local def = minetest.registered_nodes[under.name]
3361 if def and def.on_rightclick then
3362 return def.on_rightclick(pointed_thing.under, under, placer, itemstack)
3365 if pos
3366 and within_limits(pos, 0)
3367 and not minetest.is_protected(pos, placer:get_player_name()) then
3369 if not minetest.registered_entities[mob] then
3370 return
3373 pos.y = pos.y + 1
3375 local data = itemstack:get_metadata()
3376 local mob = minetest.add_entity(pos, mob, data)
3377 local ent = mob:get_luaentity()
3379 -- set owner if not a monster
3380 if ent.type ~= "monster" then
3381 ent.owner = placer:get_player_name()
3382 ent.tamed = true
3385 -- since mob is unique we remove egg once spawned
3386 itemstack:take_item()
3389 return itemstack
3390 end,
3394 -- register old stackable mob egg
3395 minetest.register_craftitem(mob, {
3397 description = desc,
3398 inventory_image = invimg,
3399 groups = grp,
3401 on_place = function(itemstack, placer, pointed_thing)
3403 local pos = pointed_thing.above
3405 -- am I clicking on something with existing on_rightclick function?
3406 local under = minetest.get_node(pointed_thing.under)
3407 local def = minetest.registered_nodes[under.name]
3408 if def and def.on_rightclick then
3409 return def.on_rightclick(pointed_thing.under, under, placer, itemstack)
3412 if pos
3413 and within_limits(pos, 0)
3414 and not minetest.is_protected(pos, placer:get_player_name()) then
3416 if not minetest.registered_entities[mob] then
3417 return
3420 pos.y = pos.y + 1
3422 local mob = minetest.add_entity(pos, mob)
3423 local ent = mob:get_luaentity()
3425 -- don't set owner if monster or sneak pressed
3426 if ent.type ~= "monster"
3427 and not placer:get_player_control().sneak then
3428 ent.owner = placer:get_player_name()
3429 ent.tamed = true
3432 -- if not in creative then take item
3433 if not mobs.is_creative(placer:get_player_name()) then
3434 itemstack:take_item()
3438 return itemstack
3439 end,
3445 -- capture critter (thanks to blert2112 for idea)
3446 function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
3448 if self.child
3449 or not clicker:is_player()
3450 or not clicker:get_inventory() then
3451 return false
3454 -- get name of clicked mob
3455 local mobname = self.name
3457 -- if not nil change what will be added to inventory
3458 if replacewith then
3459 mobname = replacewith
3462 local name = clicker:get_player_name()
3463 local tool = clicker:get_wielded_item()
3465 -- are we using hand, net or lasso to pick up mob?
3466 if tool:get_name() ~= ""
3467 and tool:get_name() ~= "mobs:net"
3468 and tool:get_name() ~= "mobs:lasso" then
3469 return false
3472 -- is mob tamed?
3473 if self.tamed == false
3474 and force_take == false then
3476 minetest.chat_send_player(name, S("Not tamed!"))
3478 return true -- false
3481 -- cannot pick up if not owner
3482 if self.owner ~= name
3483 and force_take == false then
3485 minetest.chat_send_player(name, S("@1 is owner!", self.owner))
3487 return true -- false
3490 if clicker:get_inventory():room_for_item("main", mobname) then
3492 -- was mob clicked with hand, net, or lasso?
3493 local chance = 0
3495 if tool:get_name() == "" then
3496 chance = chance_hand
3498 elseif tool:get_name() == "mobs:net" then
3500 chance = chance_net
3502 tool:add_wear(4000) -- 17 uses
3504 clicker:set_wielded_item(tool)
3506 elseif tool:get_name() == "mobs:lasso" then
3508 chance = chance_lasso
3510 tool:add_wear(650) -- 100 uses
3512 clicker:set_wielded_item(tool)
3516 -- calculate chance.. add to inventory if successful?
3517 if chance > 0 and random(1, 100) <= chance then
3519 -- default mob egg
3520 local new_stack = ItemStack(mobname)
3522 -- add special mob egg with all mob information
3523 -- unless 'replacewith' contains new item to use
3524 if not replacewith then
3526 new_stack = ItemStack(mobname .. "_set")
3528 local tmp = {}
3530 for _,stat in pairs(self) do
3531 local t = type(stat)
3532 if t ~= "function"
3533 and t ~= "nil"
3534 and t ~= "userdata" then
3535 tmp[_] = self[_]
3539 local data_str = minetest.serialize(tmp)
3541 new_stack:set_metadata(data_str)
3544 local inv = clicker:get_inventory()
3546 if inv:room_for_item("main", new_stack) then
3547 inv:add_item("main", new_stack)
3548 else
3549 minetest.add_item(clicker:get_pos(), new_stack)
3552 self.object:remove()
3554 mob_sound(self, "default_place_node_hard")
3557 else
3558 minetest.chat_send_player(name, S("Missed!"))
3560 mob_sound(self, "mobs_swing")
3564 return true
3568 -- protect tamed mob with rune item
3569 function mobs:protect(self, clicker)
3571 local name = clicker:get_player_name()
3572 local tool = clicker:get_wielded_item()
3574 if tool:get_name() ~= "mobs:protector" then
3575 return false
3578 if self.tamed == false then
3579 minetest.chat_send_player(name, S("Not tamed!"))
3580 return true -- false
3583 if self.protected == true then
3584 minetest.chat_send_player(name, S("Already protected!"))
3585 return true -- false
3588 if not mobs.is_creative(clicker:get_player_name()) then
3589 tool:take_item() -- take 1 protection rune
3590 clicker:set_wielded_item(tool)
3593 self.protected = true
3595 local pos = self.object:get_pos()
3596 pos.y = pos.y + self.collisionbox[2] + 0.5
3598 effect(self.object:get_pos(), 25, "mobs_protect_particle.png", 0.5, 4, 2, 15)
3600 mob_sound(self, "mobs_spell")
3602 return true
3606 local mob_obj = {}
3607 local mob_sta = {}
3609 -- feeding, taming and breeding (thanks blert2112)
3610 function mobs:feed_tame(self, clicker, feed_count, breed, tame)
3612 if not self.follow then
3613 return false
3616 -- can eat/tame with item in hand
3617 if follow_holding(self, clicker) then
3619 -- if not in creative then take item
3620 if not mobs.is_creative(clicker:get_player_name()) then
3622 local item = clicker:get_wielded_item()
3624 item:take_item()
3626 clicker:set_wielded_item(item)
3629 -- increase health
3630 self.health = self.health + 4
3632 if self.health >= self.hp_max then
3634 self.health = self.hp_max
3636 if self.htimer < 1 then
3638 minetest.chat_send_player(clicker:get_player_name(),
3639 S("@1 at full health (@2)",
3640 self.name:split(":")[2], tostring(self.health)))
3642 self.htimer = 5
3646 self.object:set_hp(self.health)
3648 update_tag(self)
3650 -- make children grow quicker
3651 if self.child == true then
3653 self.hornytimer = self.hornytimer + 20
3655 return true
3658 -- feed and tame
3659 self.food = (self.food or 0) + 1
3660 if self.food >= feed_count then
3662 self.food = 0
3664 if breed and self.hornytimer == 0 then
3665 self.horny = true
3668 self.gotten = false
3670 if tame then
3672 if self.tamed == false then
3673 minetest.chat_send_player(clicker:get_player_name(),
3674 S("@1 has been tamed!",
3675 self.name:split(":")[2]))
3678 self.tamed = true
3680 if not self.owner or self.owner == "" then
3681 self.owner = clicker:get_player_name()
3685 -- make sound when fed so many times
3686 mob_sound(self, self.sounds.random)
3689 return true
3692 local item = clicker:get_wielded_item()
3694 -- if mob has been tamed you can name it with a nametag
3695 if item:get_name() == "mobs:nametag"
3696 and clicker:get_player_name() == self.owner then
3698 local name = clicker:get_player_name()
3700 -- store mob and nametag stack in external variables
3701 mob_obj[name] = self
3702 mob_sta[name] = item
3704 local tag = self.nametag or ""
3706 minetest.show_formspec(name, "mobs_nametag", "size[8,4]"
3707 .. "field[0.5,1;7.5,0;name;" .. minetest.formspec_escape(S("Enter name:")) .. ";" .. tag .. "]"
3708 .. "button_exit[2.5,3.5;3,1;mob_rename;" .. minetest.formspec_escape(S("Rename")) .. "]")
3712 return false
3717 -- inspired by blockmen's nametag mod
3718 minetest.register_on_player_receive_fields(function(player, formname, fields)
3720 -- right-clicked with nametag and name entered?
3721 if formname == "mobs_nametag"
3722 and fields.name
3723 and fields.name ~= "" then
3725 local name = player:get_player_name()
3727 if not mob_obj[name]
3728 or not mob_obj[name].object then
3729 return
3732 -- make sure nametag is being used to name mob
3733 local item = player:get_wielded_item()
3735 if item:get_name() ~= "mobs:nametag" then
3736 return
3739 -- limit name entered to 64 characters long
3740 if string.len(fields.name) > 64 then
3741 fields.name = string.sub(fields.name, 1, 64)
3744 -- update nametag
3745 mob_obj[name].nametag = fields.name
3747 update_tag(mob_obj[name])
3749 -- if not in creative then take item
3750 if not mobs.is_creative(name) then
3752 mob_sta[name]:take_item()
3754 player:set_wielded_item(mob_sta[name])
3757 -- reset external variables
3758 mob_obj[name] = nil
3759 mob_sta[name] = nil
3762 end)
3765 -- compatibility function for old entities to new modpack entities
3766 function mobs:alias_mob(old_name, new_name)
3768 -- spawn egg
3769 minetest.register_alias(old_name, new_name)
3771 -- entity
3772 minetest.register_entity(":" .. old_name, {
3774 physical = false,
3776 on_step = function(self)
3778 local pos = self.object:get_pos()
3780 if minetest.registered_entities[new_name] then
3781 minetest.add_entity(pos, new_name)
3784 self.object:remove()