Update Mobs Redo, fix crash
[MineClone/MineClone2.git] / mods / ENTITIES / mobs / api.lua
blobc1cb22a20688c19842ab88733977331a36db12ae
2 -- Mobs Api
4 mobs = {}
5 mobs.mod = "redo"
6 mobs.version = "20180126"
9 -- Intllib
10 local MP = minetest.get_modpath(minetest.get_current_modname())
11 local S, NS = dofile(MP .. "/intllib.lua")
12 mobs.intllib = S
15 -- CMI support check
16 local use_cmi = minetest.global_exists("cmi")
19 -- Invisibility mod check
20 mobs.invis = {}
21 if minetest.global_exists("invisibility") then
22 mobs.invis = invisibility
23 end
26 -- creative check
27 local creative_mode_cache = minetest.settings:get_bool("creative_mode")
28 function mobs.is_creative(name)
29 return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
30 end
33 -- localize math functions
34 local pi = math.pi
35 local square = math.sqrt
36 local sin = math.sin
37 local cos = math.cos
38 local abs = math.abs
39 local min = math.min
40 local max = math.max
41 local atann = math.atan
42 local random = math.random
43 local floor = math.floor
44 local atan = function(x)
45 if not x or x ~= x then
46 --error("atan bassed NaN")
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 = minetest.settings:get_bool("remove_far_mobs")
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"
96 -- play sound
97 local mob_sound = function(self, sound)
99 if sound then
100 minetest.sound_play(sound, {
101 object = self.object,
102 gain = 1.0,
103 max_hear_distance = self.sounds.distance
109 -- attack player/mob
110 local do_attack = function(self, player)
112 if self.state == "attack" then
113 return
116 self.attack = player
117 self.state = "attack"
119 if random(0, 100) < 90 then
120 mob_sound(self, self.sounds.war_cry)
125 -- move mob in facing direction
126 local set_velocity = function(self, v)
128 local yaw = (self.object:get_yaw() or 0) + self.rotate
130 self.object:setvelocity({
131 x = sin(yaw) * -v,
132 y = self.object:getvelocity().y,
133 z = cos(yaw) * v
138 -- calculate mob velocity
139 local get_velocity = function(self)
141 local v = self.object:getvelocity()
143 return (v.x * v.x + v.z * v.z) ^ 0.5
147 -- set and return valid yaw
148 local set_yaw = function(self, yaw)
150 if not yaw or yaw ~= yaw then
151 yaw = 0
154 self:setyaw(yaw)
156 return yaw
160 -- set defined animation
161 local set_animation = function(self, anim)
163 if not self.animation
164 or not anim then return end
166 self.animation.current = self.animation.current or ""
168 if anim == self.animation.current
169 or not self.animation[anim .. "_start"]
170 or not self.animation[anim .. "_end"] then
171 return
174 self.animation.current = anim
176 self.object:set_animation({
177 x = self.animation[anim .. "_start"],
178 y = self.animation[anim .. "_end"]},
179 self.animation[anim .. "_speed"] or self.animation.speed_normal or 15,
180 0, self.animation[anim .. "_loop"] ~= false)
184 -- above function exported for mount.lua
185 function mobs:set_animation(self, anim)
186 set_animation(self, anim)
190 -- calculate distance
191 local get_distance = function(a, b)
193 local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z
195 return square(x * x + y * y + z * z)
199 -- check line of sight (BrunoMine)
200 local line_of_sight = function(self, pos1, pos2, stepsize)
202 stepsize = stepsize or 1
204 local s, pos = minetest.line_of_sight(pos1, pos2, stepsize)
206 -- normal walking and flying mobs can see you through air
207 if s == true then
208 return true
211 -- New pos1 to be analyzed
212 local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
214 local r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
216 -- Checks the return
217 if r == true then return true end
219 -- Nodename found
220 local nn = minetest.get_node(pos).name
222 -- Target Distance (td) to travel
223 local td = get_distance(pos1, pos2)
225 -- Actual Distance (ad) traveled
226 local ad = 0
228 -- It continues to advance in the line of sight in search of a real
229 -- obstruction which counts as 'normal' nodebox.
230 while minetest.registered_nodes[nn]
231 and (minetest.registered_nodes[nn].walkable == false
232 or minetest.registered_nodes[nn].drawtype == "nodebox") do
234 -- Check if you can still move forward
235 if td < ad + stepsize then
236 return true -- Reached the target
239 -- Moves the analyzed pos
240 local d = get_distance(pos1, pos2)
242 npos1.x = ((pos2.x - pos1.x) / d * stepsize) + pos1.x
243 npos1.y = ((pos2.y - pos1.y) / d * stepsize) + pos1.y
244 npos1.z = ((pos2.z - pos1.z) / d * stepsize) + pos1.z
246 -- NaN checks
247 if d == 0
248 or npos1.x ~= npos1.x
249 or npos1.y ~= npos1.y
250 or npos1.z ~= npos1.z then
251 return false
254 ad = ad + stepsize
256 -- scan again
257 r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
259 if r == true then return true end
261 -- New Nodename found
262 nn = minetest.get_node(pos).name
266 return false
270 -- are we flying in what we are suppose to? (taikedz)
271 local flight_check = function(self, pos_w)
273 local nod = self.standing_in
274 local def = minetest.registered_nodes[nod]
276 if not def then return false end -- nil check
278 if type(self.fly_in) == "string"
279 and nod == self.fly_in then
281 return true
283 elseif type(self.fly_in) == "table" then
285 for _,fly_in in pairs(self.fly_in) do
287 if nod == fly_in then
289 return true
294 -- stops mobs getting stuck inside stairs and plantlike nodes
295 if def.drawtype ~= "airlike"
296 and def.drawtype ~= "liquid"
297 and def.drawtype ~= "flowingliquid" then
298 return true
301 return false
305 -- custom particle effects
306 local effect = function(pos, amount, texture, min_size, max_size, radius, gravity, glow)
308 radius = radius or 2
309 min_size = min_size or 0.5
310 max_size = max_size or 1
311 gravity = gravity or -10
312 glow = glow or 0
314 minetest.add_particlespawner({
315 amount = amount,
316 time = 0.25,
317 minpos = pos,
318 maxpos = pos,
319 minvel = {x = -radius, y = -radius, z = -radius},
320 maxvel = {x = radius, y = radius, z = radius},
321 minacc = {x = 0, y = gravity, z = 0},
322 maxacc = {x = 0, y = gravity, z = 0},
323 minexptime = 0.1,
324 maxexptime = 1,
325 minsize = min_size,
326 maxsize = max_size,
327 texture = texture,
328 glow = glow,
333 -- update nametag colour
334 local update_tag = function(self)
336 local col = "#00FF00"
337 local qua = self.hp_max / 4
339 if self.health <= floor(qua * 3) then
340 col = "#FFFF00"
343 if self.health <= floor(qua * 2) then
344 col = "#FF6600"
347 if self.health <= floor(qua) then
348 col = "#FF0000"
351 self.object:set_properties({
352 nametag = self.nametag,
353 nametag_color = col
359 -- drop items
360 local item_drop = function(self, cooked)
362 -- no drops if disabled by setting
363 if not mobs_drop_items then return end
365 -- no drops for child mobs
366 if self.child then return end
368 local obj, item, num
369 local pos = self.object:get_pos()
371 self.drops = self.drops or {} -- nil check
373 for n = 1, #self.drops do
375 if random(1, self.drops[n].chance) == 1 then
377 num = random(self.drops[n].min or 1, self.drops[n].max or 1)
378 item = self.drops[n].name
380 -- cook items when true
381 if cooked then
383 local output = minetest.get_craft_result({
384 method = "cooking", width = 1, items = {item}})
386 if output and output.item and not output.item:is_empty() then
387 item = output.item:get_name()
391 -- add item if it exists
392 obj = minetest.add_item(pos, ItemStack(item .. " " .. num))
394 if obj and obj:get_luaentity() then
396 obj:setvelocity({
397 x = random(-10, 10) / 9,
398 y = 6,
399 z = random(-10, 10) / 9,
401 elseif obj then
402 obj:remove() -- item does not exist
407 self.drops = {}
411 -- check if mob is dead or only hurt
412 local check_for_death = function(self, cause, cmi_cause)
414 -- has health actually changed?
415 if self.health == self.old_health and self.health > 0 then
416 return
419 self.old_health = self.health
421 -- still got some health? play hurt sound
422 if self.health > 0 then
424 mob_sound(self, self.sounds.damage)
426 -- make sure health isn't higher than max
427 if self.health > self.hp_max then
428 self.health = self.hp_max
431 -- backup nametag so we can show health stats
432 if not self.nametag2 then
433 self.nametag2 = self.nametag or ""
436 if show_health
437 and (cmi_cause and cmi_cause.type == "punch") then
439 self.htimer = 2
440 self.nametag = "♥ " .. self.health .. " / " .. self.hp_max
442 update_tag(self)
445 return false
448 -- dropped cooked item if mob died in lava
449 if cause == "lava" then
450 item_drop(self, true)
451 else
452 item_drop(self, nil)
455 mob_sound(self, self.sounds.death)
457 local pos = self.object:get_pos()
459 -- execute custom death function
460 if self.on_die then
462 self.on_die(self, pos)
464 if use_cmi then
465 cmi.notify_die(self.object, cmi_cause)
468 self.object:remove()
470 return true
473 -- default death function and die animation (if defined)
474 if self.animation
475 and self.animation.die_start
476 and self.animation.die_end then
478 local frames = self.animation.die_end - self.animation.die_start
479 local speed = self.animation.die_speed or 15
480 local length = max(frames / speed, 0)
482 self.attack = nil
483 self.v_start = false
484 self.timer = 0
485 self.blinktimer = 0
486 self.passive = true
487 self.state = "die"
488 set_velocity(self, 0)
489 set_animation(self, "die")
491 minetest.after(length, function(self)
493 if use_cmi then
494 cmi.notify_die(self.object, cmi_cause)
497 self.object:remove()
498 end, self)
499 else
501 if use_cmi then
502 cmi.notify_die(self.object, cmi_cause)
505 self.object:remove()
508 effect(pos, 20, "tnt_smoke.png")
510 return true
514 -- check if within physical map limits (-30911 to 30927)
515 local within_limits = function(pos, radius)
517 if (pos.x - radius) > -30913
518 and (pos.x + radius) < 30928
519 and (pos.y - radius) > -30913
520 and (pos.y + radius) < 30928
521 and (pos.z - radius) > -30913
522 and (pos.z + radius) < 30928 then
523 return true -- within limits
526 return false -- beyond limits
530 -- is mob facing a cliff
531 local is_at_cliff = function(self)
533 if self.fear_height == 0 then -- 0 for no falling protection!
534 return false
537 local yaw = self.object:get_yaw()
538 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
539 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
540 local pos = self.object:get_pos()
541 local ypos = pos.y + self.collisionbox[2] -- just above floor
543 if minetest.line_of_sight(
544 {x = pos.x + dir_x, y = ypos, z = pos.z + dir_z},
545 {x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z}
546 , 1) then
548 return true
551 return false
555 -- get node but use fallback for nil or unknown
556 local node_ok = function(pos, fallback)
558 fallback = fallback or mobs.fallback_node
560 local node = minetest.get_node_or_nil(pos)
562 if node and minetest.registered_nodes[node.name] then
563 return node
566 return minetest.registered_nodes[fallback] -- {name = fallback}
570 -- environmental damage (water, lava, fire, light etc.)
571 local do_env_damage = function(self)
573 -- feed/tame text timer (so mob 'full' messages dont spam chat)
574 if self.htimer > 0 then
575 self.htimer = self.htimer - 1
578 -- reset nametag after showing health stats
579 if self.htimer < 1 and self.nametag2 then
581 self.nametag = self.nametag2
582 self.nametag2 = nil
584 update_tag(self)
587 local pos = self.object:get_pos()
589 self.time_of_day = minetest.get_timeofday()
591 -- remove mob if beyond map limits
592 if not within_limits(pos, 0) then
593 self.object:remove()
594 return
597 -- bright light harms mob
598 if self.light_damage ~= 0
599 -- and pos.y > 0
600 -- and self.time_of_day > 0.2
601 -- and self.time_of_day < 0.8
602 and (minetest.get_node_light(pos) or 0) > 12 then
604 self.health = self.health - self.light_damage
606 effect(pos, 5, "tnt_smoke.png")
608 if check_for_death(self, "light", {type = "light"}) then return end
611 local y_level = self.collisionbox[2]
613 if self.child then
614 y_level = self.collisionbox[2] * 0.5
617 -- what is mob standing in?
618 pos.y = pos.y + y_level + 0.25 -- foot level
619 self.standing_in = node_ok(pos, "air").name
620 -- print ("standing in " .. self.standing_in)
622 -- don't fall when on ignore, just stand still
623 if self.standing_in == "ignore" then
624 self.object:setvelocity({x = 0, y = 0, z = 0})
627 local nodef = minetest.registered_nodes[self.standing_in]
629 pos.y = pos.y + 1 -- for particle effect position
631 -- water
632 if self.water_damage
633 and nodef.groups.water then
635 if self.water_damage ~= 0 then
637 self.health = self.health - self.water_damage
639 effect(pos, 5, "bubble.png", nil, nil, 1, nil)
641 if check_for_death(self, "water", {type = "environment",
642 pos = pos, node = self.standing_in}) then return end
645 -- lava or fire
646 elseif self.lava_damage
647 and (nodef.groups.lava
648 or self.standing_in == node_fire
649 or self.standing_in == node_permanent_flame) then
651 if self.lava_damage ~= 0 then
653 self.health = self.health - self.lava_damage
655 effect(pos, 5, "fire_basic_flame.png", nil, nil, 1, nil)
657 if check_for_death(self, "lava", {type = "environment",
658 pos = pos, node = self.standing_in}) then return end
661 -- damage_per_second node check
662 elseif nodef.damage_per_second ~= 0 then
664 self.health = self.health - nodef.damage_per_second
666 effect(pos, 5, "tnt_smoke.png")
668 if check_for_death(self, "dps", {type = "environment",
669 pos = pos, node = self.standing_in}) then return end
671 --[[
672 --- suffocation inside solid node
673 if self.suffocation ~= 0
674 and nodef.walkable == true
675 and nodef.groups.disable_suffocation ~= 1
676 and nodef.drawtype == "normal" then
678 self.health = self.health - self.suffocation
680 if check_for_death(self, "suffocation", {type = "environment",
681 pos = pos, node = self.standing_in}) then return end
684 check_for_death(self, "", {type = "unknown"})
688 -- jump if facing a solid node (not fences or gates)
689 local do_jump = function(self)
691 if not self.jump
692 or self.jump_height == 0
693 or self.fly
694 or self.child then
695 return false
698 self.facing_fence = false
700 -- something stopping us while moving?
701 if self.state ~= "stand"
702 and get_velocity(self) > 0.5
703 and self.object:getvelocity().y ~= 0 then
704 return false
707 local pos = self.object:get_pos()
708 local yaw = self.object:get_yaw()
710 -- what is mob standing on?
711 pos.y = pos.y + self.collisionbox[2] - 0.2
713 local nod = node_ok(pos)
715 --print ("standing on:", nod.name, pos.y)
717 if minetest.registered_nodes[nod.name].walkable == false then
718 return false
721 -- where is front
722 local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
723 local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
725 -- what is in front of mob?
726 local nod = node_ok({
727 x = pos.x + dir_x,
728 y = pos.y + 0.5,
729 z = pos.z + dir_z
732 -- thin blocks that do not need to be jumped
733 if nod.name == node_snow then
734 return false
737 --print ("in front:", nod.name, pos.y + 0.5)
739 if self.walk_chance == 0
740 or minetest.registered_items[nod.name].walkable then
742 if not nod.name:find("fence")
743 and not nod.name:find("gate") then
745 local v = self.object:getvelocity()
747 v.y = self.jump_height
749 set_animation(self, "jump") -- only when defined
751 self.object:setvelocity(v)
753 if get_velocity(self) > 0 then
754 mob_sound(self, self.sounds.jump)
756 else
757 self.facing_fence = true
760 return true
763 return false
767 -- blast damage to entities nearby (modified from TNT mod)
768 local entity_physics = function(pos, radius)
770 radius = radius * 2
772 local objs = minetest.get_objects_inside_radius(pos, radius)
773 local obj_pos, dist
775 for n = 1, #objs do
777 obj_pos = objs[n]:get_pos()
779 dist = get_distance(pos, obj_pos)
780 if dist < 1 then dist = 1 end
782 local damage = floor((4 / dist) * radius)
783 local ent = objs[n]:get_luaentity()
785 -- punches work on entities AND players
786 objs[n]:punch(objs[n], 1.0, {
787 full_punch_interval = 1.0,
788 damage_groups = {fleshy = damage},
789 }, pos)
794 -- should mob follow what I'm holding ?
795 local follow_holding = function(self, clicker)
797 if mobs.invis[clicker:get_player_name()] then
798 return false
801 local item = clicker:get_wielded_item()
802 local t = type(self.follow)
804 -- single item
805 if t == "string"
806 and item:get_name() == self.follow then
807 return true
809 -- multiple items
810 elseif t == "table" then
812 for no = 1, #self.follow do
814 if self.follow[no] == item:get_name() then
815 return true
820 return false
824 -- find two animals of same type and breed if nearby and horny
825 local breed = function(self)
827 -- child takes 240 seconds before growing into adult
828 if self.child == true then
830 self.hornytimer = self.hornytimer + 1
832 if self.hornytimer > 240 then
834 self.child = false
835 self.hornytimer = 0
837 self.object:set_properties({
838 textures = self.base_texture,
839 mesh = self.base_mesh,
840 visual_size = self.base_size,
841 collisionbox = self.base_colbox,
842 selectionbox = self.base_selbox,
845 -- custom function when child grows up
846 if self.on_grown then
847 self.on_grown(self)
848 else
849 -- jump when fully grown so as not to fall into ground
850 self.object:setvelocity({
851 x = 0,
852 y = self.jump_height,
853 z = 0
858 return
861 -- horny animal can mate for 40 seconds,
862 -- afterwards horny animal cannot mate again for 200 seconds
863 if self.horny == true
864 and self.hornytimer < 240 then
866 self.hornytimer = self.hornytimer + 1
868 if self.hornytimer >= 240 then
869 self.hornytimer = 0
870 self.horny = false
874 -- find another same animal who is also horny and mate if nearby
875 if self.horny == true
876 and self.hornytimer <= 40 then
878 local pos = self.object:get_pos()
880 effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, "heart.png", 3, 4, 1, 0.1)
882 local objs = minetest.get_objects_inside_radius(pos, 3)
883 local num = 0
884 local ent = nil
886 for n = 1, #objs do
888 ent = objs[n]:get_luaentity()
890 -- check for same animal with different colour
891 local canmate = false
893 if ent then
895 if ent.name == self.name then
896 canmate = true
897 else
898 local entname = string.split(ent.name,":")
899 local selfname = string.split(self.name,":")
901 if entname[1] == selfname[1] then
902 entname = string.split(entname[2],"_")
903 selfname = string.split(selfname[2],"_")
905 if entname[1] == selfname[1] then
906 canmate = true
912 if ent
913 and canmate == true
914 and ent.horny == true
915 and ent.hornytimer <= 40 then
916 num = num + 1
919 -- found your mate? then have a baby
920 if num > 1 then
922 self.hornytimer = 41
923 ent.hornytimer = 41
925 -- spawn baby
926 minetest.after(5, function()
928 -- custom breed function
929 if self.on_breed then
931 -- when false skip going any further
932 if self.on_breed(self, ent) == false then
933 return
935 else
936 effect(pos, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
939 local mob = minetest.add_entity(pos, self.name)
940 local ent2 = mob:get_luaentity()
941 local textures = self.base_texture
943 -- using specific child texture (if found)
944 if self.child_texture then
945 textures = self.child_texture[1]
948 -- and resize to half height
949 mob:set_properties({
950 textures = textures,
951 visual_size = {
952 x = self.base_size.x * .5,
953 y = self.base_size.y * .5,
955 collisionbox = {
956 self.base_colbox[1] * .5,
957 self.base_colbox[2] * .5,
958 self.base_colbox[3] * .5,
959 self.base_colbox[4] * .5,
960 self.base_colbox[5] * .5,
961 self.base_colbox[6] * .5,
963 selectionbox = {
964 self.base_selbox[1] * .5,
965 self.base_selbox[2] * .5,
966 self.base_selbox[3] * .5,
967 self.base_selbox[4] * .5,
968 self.base_selbox[5] * .5,
969 self.base_selbox[6] * .5,
972 -- tamed and owned by parents' owner
973 ent2.child = true
974 ent2.tamed = true
975 ent2.owner = self.owner
976 end)
978 num = 0
980 break
987 -- find and replace what mob is looking for (grass, wheat etc.)
988 local replace = function(self, pos)
990 if not mobs_griefing
991 or not self.replace_rate
992 or not self.replace_what
993 or self.child == true
994 or self.object:getvelocity().y ~= 0
995 or random(1, self.replace_rate) > 1 then
996 return
999 local what, with, y_offset
1001 if type(self.replace_what[1]) == "table" then
1003 local num = random(#self.replace_what)
1005 what = self.replace_what[num][1] or ""
1006 with = self.replace_what[num][2] or ""
1007 y_offset = self.replace_what[num][3] or 0
1008 else
1009 what = self.replace_what
1010 with = self.replace_with or ""
1011 y_offset = self.replace_offset or 0
1014 pos.y = pos.y + y_offset
1016 if #minetest.find_nodes_in_area(pos, pos, what) > 0 then
1018 -- print ("replace node = ".. minetest.get_node(pos).name, pos.y)
1020 local oldnode = {name = what}
1021 local newnode = {name = with}
1022 local on_replace_return
1024 if self.on_replace then
1025 on_replace_return = self.on_replace(self, pos, oldnode, newnode)
1028 if on_replace_return ~= false then
1030 minetest.set_node(pos, {name = with})
1032 -- when cow/sheep eats grass, replace wool and milk
1033 if self.gotten == true then
1034 self.gotten = false
1035 self.object:set_properties(self)
1042 -- check if daytime and also if mob is docile during daylight hours
1043 local day_docile = function(self)
1045 if self.docile_by_day == false then
1047 return false
1049 elseif self.docile_by_day == true
1050 and self.time_of_day > 0.2
1051 and self.time_of_day < 0.8 then
1053 return true
1058 -- path finding and smart mob routine by rnd
1059 local smart_mobs = function(self, s, p, dist, dtime)
1061 local s1 = self.path.lastpos
1063 -- is it becoming stuck?
1064 if abs(s1.x - s.x) + abs(s1.z - s.z) < 1.5 then
1065 self.path.stuck_timer = self.path.stuck_timer + dtime
1066 else
1067 self.path.stuck_timer = 0
1070 self.path.lastpos = {x = s.x, y = s.y, z = s.z}
1072 -- im stuck, search for path
1073 if (self.path.stuck_timer > stuck_timeout and not self.path.following)
1074 or (self.path.stuck_timer > stuck_path_timeout and self.path.following) then
1076 self.path.stuck_timer = 0
1078 -- lets try find a path, first take care of positions
1079 -- since pathfinder is very sensitive
1080 local sheight = self.collisionbox[5] - self.collisionbox[2]
1082 -- round position to center of node to avoid stuck in walls
1083 -- also adjust height for player models!
1084 s.x = floor(s.x + 0.5)
1085 -- s.y = floor(s.y + 0.5) - sheight
1086 s.z = floor(s.z + 0.5)
1088 local ssight, sground = minetest.line_of_sight(s, {
1089 x = s.x, y = s.y - 4, z = s.z}, 1)
1091 -- determine node above ground
1092 if not ssight then
1093 s.y = sground.y + 1
1096 local p1 = self.attack:get_pos()
1098 p1.x = floor(p1.x + 0.5)
1099 p1.y = floor(p1.y + 0.5)
1100 p1.z = floor(p1.z + 0.5)
1102 local dropheight = 6
1103 if self.fear_height ~= 0 then dropheight = self.fear_height end
1105 -- self.path.way = minetest.find_path(s, p1, 16, 2, 6, "Dijkstra")
1106 self.path.way = minetest.find_path(s, p1, 16, self.stepheight, dropheight, "A*_noprefetch")
1108 -- attempt to unstick mob that is "daydreaming"
1109 self.object:setpos({
1110 x = s.x + 0.1 * (random() * 2 - 1),
1111 y = s.y + 1,
1112 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:setpos({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.attack)
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() ] then
1401 type = ""
1402 else
1403 player = objs[n]
1404 type = "player"
1405 name = "player"
1407 else
1408 obj = objs[n]:get_luaentity()
1410 if obj then
1411 player = obj.object
1412 type = obj.type
1413 name = obj.name or ""
1417 -- find specific mob to runaway from
1418 if name ~= "" and name ~= self.name
1419 and specific_runaway(self.runaway_from, name) then
1421 s = self.object:get_pos()
1422 p = player:get_pos()
1423 sp = s
1425 -- aim higher to make looking up hills more realistic
1426 p.y = p.y + 1
1427 sp.y = sp.y + 1
1429 dist = get_distance(p, s)
1431 if dist < self.view_range then
1432 -- field of view check goes here
1434 -- choose closest player/mpb to runaway from
1435 if line_of_sight(self, sp, p, 2) == true
1436 and dist < min_dist then
1437 min_dist = dist
1438 min_player = player
1444 -- attack 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 = 0
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:setvelocity({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:getvelocity()
1673 local ud = random(-1, 2) / 9
1675 self.object:setvelocity({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", 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 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 -- start timer when inside reach
1834 if dist < self.reach and not self.v_start then
1835 self.v_start = true
1836 self.timer = 0
1837 self.blinktimer = 0
1838 -- print ("=== explosion timer started", self.explosion_timer)
1841 -- walk right up to player when timer active
1842 if dist < 1.5 and self.v_start then
1843 set_velocity(self, 0)
1844 else
1845 set_velocity(self, self.run_velocity)
1848 if self.animation and self.animation.run_start then
1849 set_animation(self, "run")
1850 else
1851 set_animation(self, "walk")
1854 if self.v_start then
1856 self.timer = self.timer + dtime
1857 self.blinktimer = (self.blinktimer or 0) + dtime
1859 if self.blinktimer > 0.2 then
1861 self.blinktimer = 0
1863 if self.blinkstatus then
1864 self.object:settexturemod("")
1865 else
1866 self.object:settexturemod("^[brighten")
1869 self.blinkstatus = not self.blinkstatus
1872 -- print ("=== explosion timer", self.timer)
1874 if self.timer > self.explosion_timer then
1876 local pos = self.object:get_pos()
1877 local radius = self.explosion_radius or 1
1878 local damage_radius = radius
1880 -- dont damage anything if area protected or next to water
1881 if minetest.find_node_near(pos, 1, {"group:water"})
1882 or minetest.is_protected(pos, "") then
1884 damage_radius = 0
1887 self.object:remove()
1889 if minetest.get_modpath("mcl_tnt") and tnt and tnt.boom
1890 and not minetest.is_protected(pos, "") then
1892 tnt.boom(pos, {
1893 radius = radius,
1894 damage_radius = damage_radius,
1895 sound = self.sounds.explode,
1897 else
1899 minetest.sound_play(self.sounds.explode, {
1900 pos = pos,
1901 gain = 1.0,
1902 max_hear_distance = self.sounds.distance or 32
1905 entity_physics(pos, damage_radius)
1906 effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
1909 return
1913 elseif self.attack_type == "dogfight"
1914 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 2)
1915 or (self.attack_type == "dogshoot" and dist <= self.reach and dogswitch(self) == 0) then
1917 if self.fly
1918 and dist > self.reach then
1920 local p1 = s
1921 local me_y = floor(p1.y)
1922 local p2 = p
1923 local p_y = floor(p2.y + 1)
1924 local v = self.object:getvelocity()
1926 if flight_check(self, s) then
1928 if me_y < p_y then
1930 self.object:setvelocity({
1931 x = v.x,
1932 y = 1 * self.walk_velocity,
1933 z = v.z
1936 elseif me_y > p_y then
1938 self.object:setvelocity({
1939 x = v.x,
1940 y = -1 * self.walk_velocity,
1941 z = v.z
1944 else
1945 if me_y < p_y then
1947 self.object:setvelocity({
1948 x = v.x,
1949 y = 0.01,
1950 z = v.z
1953 elseif me_y > p_y then
1955 self.object:setvelocity({
1956 x = v.x,
1957 y = -0.01,
1958 z = v.z
1965 -- rnd: new movement direction
1966 if self.path.following
1967 and self.path.way
1968 and self.attack_type ~= "dogshoot" then
1970 -- no paths longer than 50
1971 if #self.path.way > 50
1972 or dist < self.reach then
1973 self.path.following = false
1974 return
1977 local p1 = self.path.way[1]
1979 if not p1 then
1980 self.path.following = false
1981 return
1984 if abs(p1.x-s.x) + abs(p1.z - s.z) < 0.6 then
1985 -- reached waypoint, remove it from queue
1986 table.remove(self.path.way, 1)
1989 -- set new temporary target
1990 p = {x = p1.x, y = p1.y, z = p1.z}
1993 local vec = {
1994 x = p.x - s.x,
1995 z = p.z - s.z
1998 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2000 if p.x > s.x then yaw = yaw + pi end
2002 yaw = set_yaw(self.object, yaw)
2004 -- move towards enemy if beyond mob reach
2005 if dist > self.reach then
2007 -- path finding by rnd
2008 if self.pathfinding -- only if mob has pathfinding enabled
2009 and enable_pathfinding then
2011 smart_mobs(self, s, p, dist, dtime)
2014 if is_at_cliff(self) then
2016 set_velocity(self, 0)
2017 set_animation(self, "stand")
2018 else
2020 if self.path.stuck then
2021 set_velocity(self, self.walk_velocity)
2022 else
2023 set_velocity(self, self.run_velocity)
2026 if self.animation and self.animation.run_start then
2027 set_animation(self, "run")
2028 else
2029 set_animation(self, "walk")
2033 else -- rnd: if inside reach range
2035 self.path.stuck = false
2036 self.path.stuck_timer = 0
2037 self.path.following = false -- not stuck anymore
2039 set_velocity(self, 0)
2041 if not self.custom_attack then
2043 if self.timer > 1 then
2045 self.timer = 0
2047 if self.double_melee_attack
2048 and random(1, 2) == 1 then
2049 set_animation(self, "punch2")
2050 else
2051 set_animation(self, "punch")
2054 local p2 = p
2055 local s2 = s
2057 p2.y = p2.y + .5
2058 s2.y = s2.y + .5
2060 if line_of_sight(self, p2, s2) == true then
2062 -- play attack sound
2063 mob_sound(self, self.sounds.attack)
2065 -- punch player (or what player is attached to)
2066 local attached = self.attack:get_attach()
2067 if attached then
2068 self.attack = attached
2070 self.attack:punch(self.object, 1.0, {
2071 full_punch_interval = 1.0,
2072 damage_groups = {fleshy = self.damage}
2073 }, nil)
2076 else -- call custom attack every second
2077 if self.custom_attack
2078 and self.timer > 1 then
2080 self.timer = 0
2082 self.custom_attack(self, p)
2087 elseif self.attack_type == "shoot"
2088 or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 1)
2089 or (self.attack_type == "dogshoot" and dist > self.reach and dogswitch(self) == 0) then
2091 p.y = p.y - .5
2092 s.y = s.y + .5
2094 local dist = get_distance(p, s)
2095 local vec = {
2096 x = p.x - s.x,
2097 y = p.y - s.y,
2098 z = p.z - s.z
2101 yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
2103 if p.x > s.x then yaw = yaw + pi end
2105 yaw = set_yaw(self.object, yaw)
2107 set_velocity(self, 0)
2109 if self.shoot_interval
2110 and self.timer > self.shoot_interval
2111 and random(1, 100) <= 60 then
2113 self.timer = 0
2114 set_animation(self, "shoot")
2116 -- play shoot attack sound
2117 mob_sound(self, self.sounds.shoot_attack)
2119 local p = self.object:get_pos()
2121 p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
2123 if minetest.registered_entities[self.arrow] then
2125 local obj = minetest.add_entity(p, self.arrow)
2126 local ent = obj:get_luaentity()
2127 local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5
2128 local v = ent.velocity or 1 -- or set to default
2130 ent.switch = 1
2131 ent.owner_id = tostring(self.object) -- add unique owner id to arrow
2133 -- offset makes shoot aim accurate
2134 vec.y = vec.y + self.shoot_offset
2135 vec.x = vec.x * (v / amount)
2136 vec.y = vec.y * (v / amount)
2137 vec.z = vec.z * (v / amount)
2139 obj:setvelocity(vec)
2147 -- falling and fall damage
2148 local falling = function(self, pos)
2150 if self.fly then
2151 return
2154 -- floating in water (or falling)
2155 local v = self.object:getvelocity()
2157 if v.y > 0 then
2159 -- apply gravity when moving up
2160 self.object:setacceleration({
2161 x = 0,
2162 y = -10,
2163 z = 0
2166 elseif v.y <= 0 and v.y > self.fall_speed then
2168 -- fall downwards at set speed
2169 self.object:setacceleration({
2170 x = 0,
2171 y = self.fall_speed,
2172 z = 0
2174 else
2175 -- stop accelerating once max fall speed hit
2176 self.object:setacceleration({x = 0, y = 0, z = 0})
2179 -- in water then float up
2180 -- if minetest.registered_nodes[node_ok(pos).name].groups.liquid then
2181 if minetest.registered_nodes[node_ok(pos).name].groups.water then
2183 if self.floats == 1 then
2185 self.object:setacceleration({
2186 x = 0,
2187 y = -self.fall_speed / (max(1, v.y) ^ 2),
2188 z = 0
2191 else
2193 -- fall damage onto solid ground
2194 if self.fall_damage == 1
2195 and self.object:getvelocity().y == 0 then
2197 local d = (self.old_y or 0) - self.object:get_pos().y
2199 if d > 5 then
2201 self.health = self.health - floor(d - 5)
2203 effect(pos, 5, "tnt_smoke.png", 1, 2, 2, nil)
2205 if check_for_death(self, "fall", {type = "fall"}) then
2206 return
2210 self.old_y = self.object:get_pos().y
2216 -- deal damage and effects when mob punched
2217 local mob_punch = function(self, hitter, tflp, tool_capabilities, dir)
2219 -- custom punch function
2220 if self.do_punch then
2222 -- when false skip going any further
2223 if self.do_punch(self, hitter, tflp, tool_caps, dir) == false then
2224 return
2228 -- mob health check
2229 -- if self.health <= 0 then
2230 -- return
2231 -- end
2233 -- error checking when mod profiling is enabled
2234 if not tool_capabilities then
2235 minetest.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2236 return
2239 -- is mob protected?
2240 if self.protected and hitter:is_player()
2241 and minetest.is_protected(self.object:get_pos(), hitter:get_player_name()) then
2242 minetest.chat_send_player(hitter:get_player_name(), S("Mob has been protected!"))
2243 return
2247 -- weapon wear
2248 local weapon = hitter:get_wielded_item()
2249 local punch_interval = 1.4
2251 -- calculate mob damage
2252 local damage = 0
2253 local armor = self.object:get_armor_groups() or {}
2254 local tmp
2256 -- quick error check incase it ends up 0 (serialize.h check test)
2257 if tflp == 0 then
2258 tflp = 0.2
2261 if use_cmi then
2262 damage = cmi.calculate_damage(self.object, hitter, tflp, tool_capabilities, dir)
2263 else
2265 for group,_ in pairs( (tool_capabilities.damage_groups or {}) ) do
2267 tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)
2269 if tmp < 0 then
2270 tmp = 0.0
2271 elseif tmp > 1 then
2272 tmp = 1.0
2275 damage = damage + (tool_capabilities.damage_groups[group] or 0)
2276 * tmp * ((armor[group] or 0) / 100.0)
2280 -- check for tool immunity or special damage
2281 for n = 1, #self.immune_to do
2283 if self.immune_to[n][1] == weapon:get_name() then
2285 damage = self.immune_to[n][2] or 0
2286 break
2290 -- healing
2291 if damage <= -1 then
2292 self.health = self.health - floor(damage)
2293 return
2296 -- print ("Mob Damage is", damage)
2298 if use_cmi then
2300 local cancel = cmi.notify_punch(self.object, hitter, tflp, tool_capabilities, dir, damage)
2302 if cancel then return end
2305 -- add weapon wear
2306 if tool_capabilities then
2307 punch_interval = tool_capabilities.full_punch_interval or 1.4
2310 if weapon:get_definition()
2311 and weapon:get_definition().tool_capabilities then
2313 weapon:add_wear(floor((punch_interval / 75) * 9000))
2314 hitter:set_wielded_item(weapon)
2317 -- only play hit sound and show blood effects if damage is 1 or over
2318 if damage >= 1 then
2320 -- weapon sounds
2321 if weapon:get_definition().sounds ~= nil then
2323 local s = random(0, #weapon:get_definition().sounds)
2325 minetest.sound_play(weapon:get_definition().sounds[s], {
2326 object = self.object, --hitter,
2327 max_hear_distance = 8
2329 else
2330 minetest.sound_play("default_punch", {
2331 object = self.object, --hitter,
2332 max_hear_distance = 5
2336 -- blood_particles
2337 if self.blood_amount > 0
2338 and not disable_blood then
2340 local pos = self.object:get_pos()
2342 pos.y = pos.y + (-self.collisionbox[2] + self.collisionbox[5]) * .5
2344 -- do we have a single blood texture or multiple?
2345 if type(self.blood_texture) == "table" then
2347 local blood = self.blood_texture[random(1, #self.blood_texture)]
2349 effect(pos, self.blood_amount, blood, nil, nil, 1, nil)
2350 else
2351 effect(pos, self.blood_amount, self.blood_texture, nil, nil, 1, nil)
2355 -- do damage
2356 self.health = self.health - floor(damage)
2358 -- exit here if dead, special item check
2359 if weapon:get_name() == "mobs:pick_lava" then
2360 if check_for_death(self, "lava", {type = "punch",
2361 puncher = hitter}) then
2362 return
2364 else
2365 if check_for_death(self, "hit", {type = "punch",
2366 puncher = hitter}) then
2367 return
2371 --[[ add healthy afterglow when hit (can cause hit lag with larger textures)
2372 core.after(0.1, function()
2373 self.object:settexturemod("^[colorize:#c9900070")
2375 core.after(0.3, function()
2376 self.object:settexturemod("")
2377 end)
2378 end) ]]
2380 -- knock back effect (only on full punch)
2381 if self.knock_back > 0
2382 and tflp >= punch_interval then
2384 local v = self.object:getvelocity()
2385 local r = 1.4 - min(punch_interval, 1.4)
2386 local kb = r * 5
2387 local up = 2
2389 -- if already in air then dont go up anymore when hit
2390 if v.y > 0
2391 or self.fly then
2392 up = 0
2395 -- direction error check
2396 dir = dir or {x = 0, y = 0, z = 0}
2398 -- check if tool already has specific knockback value
2399 if tool_capabilities.damage_groups["knockback"] then
2400 kb = tool_capabilities.damage_groups["knockback"]
2401 else
2402 kb = kb * 1.5
2405 self.object:setvelocity({
2406 x = dir.x * kb,
2407 y = up,
2408 z = dir.z * kb
2411 self.pause_timer = 0.25
2413 end -- END if damage
2415 -- if skittish then run away
2416 if self.runaway == true then
2418 local lp = hitter:get_pos()
2419 local s = self.object:get_pos()
2420 local vec = {
2421 x = lp.x - s.x,
2422 y = lp.y - s.y,
2423 z = lp.z - s.z
2426 local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
2428 if lp.x > s.x then
2429 yaw = yaw + pi
2432 yaw = set_yaw(self.object, yaw)
2433 self.state = "runaway"
2434 self.runaway_timer = 0
2435 self.following = nil
2438 local name = hitter:get_player_name() or ""
2440 -- attack puncher and call other mobs for help
2441 if self.passive == false
2442 and self.state ~= "flop"
2443 and self.child == false
2444 and hitter:get_player_name() ~= self.owner
2445 and not mobs.invis[ name ] then
2447 -- attack whoever punched mob
2448 self.state = ""
2449 do_attack(self, hitter)
2451 -- alert others to the attack
2452 local objs = minetest.get_objects_inside_radius(hitter:get_pos(), self.view_range)
2453 local obj = nil
2455 for n = 1, #objs do
2457 obj = objs[n]:get_luaentity()
2459 if obj then
2461 -- only alert members of same mob
2462 if obj.group_attack == true
2463 and obj.state ~= "attack"
2464 and obj.owner ~= name
2465 and obj.name == self.name then
2466 do_attack(obj, hitter)
2469 -- have owned mobs attack player threat
2470 if obj.owner == name and obj.owner_loyal then
2471 do_attack(obj, self.object)
2479 -- get entity staticdata
2480 local mob_staticdata = function(self)
2482 -- remove mob when out of range unless tamed
2483 if remove_far
2484 and self.remove_ok
2485 and self.type ~= "npc"
2486 and self.state ~= "attack"
2487 and not self.tamed
2488 and self.lifetimer < 20000 then
2490 --print ("REMOVED " .. self.name)
2492 self.object:remove()
2494 return ""-- nil
2497 self.remove_ok = true
2498 self.attack = nil
2499 self.following = nil
2500 self.state = "stand"
2502 -- used to rotate older mobs
2503 if self.drawtype
2504 and self.drawtype == "side" then
2505 self.rotate = math.rad(90)
2508 if use_cmi then
2509 self.serialized_cmi_components = cmi.serialize_components(self._cmi_components)
2512 local tmp = {}
2514 for _,stat in pairs(self) do
2516 local t = type(stat)
2518 if t ~= "function"
2519 and t ~= "nil"
2520 and t ~= "userdata"
2521 and _ ~= "_cmi_components" then
2522 tmp[_] = self[_]
2526 --print('===== '..self.name..'\n'.. dump(tmp)..'\n=====\n')
2527 return minetest.serialize(tmp)
2531 -- activate mob and reload settings
2532 local mob_activate = function(self, staticdata, def, dtime)
2534 -- remove monsters in peaceful mode
2535 if self.type == "monster"
2536 and peaceful_only then
2538 self.object:remove()
2540 return
2543 -- load entity variables
2544 local tmp = minetest.deserialize(staticdata)
2546 if tmp then
2547 for _,stat in pairs(tmp) do
2548 self[_] = stat
2552 -- select random texture, set model and size
2553 if not self.base_texture then
2555 -- compatiblity with old simple mobs textures
2556 if type(def.textures[1]) == "string" then
2557 def.textures = {def.textures}
2560 self.base_texture = def.textures[random(1, #def.textures)]
2561 self.base_mesh = def.mesh
2562 self.base_size = self.visual_size
2563 self.base_colbox = self.collisionbox
2564 self.base_selbox = self.selectionbox
2567 -- for current mobs that dont have this set
2568 if not self.base_selbox then
2569 self.base_selbox = self.selectionbox or self.base_colbox
2572 -- set texture, model and size
2573 local textures = self.base_texture
2574 local mesh = self.base_mesh
2575 local vis_size = self.base_size
2576 local colbox = self.base_colbox
2577 local selbox = self.base_selbox
2579 -- specific texture if gotten
2580 if self.gotten == true
2581 and def.gotten_texture then
2582 textures = def.gotten_texture
2585 -- specific mesh if gotten
2586 if self.gotten == true
2587 and def.gotten_mesh then
2588 mesh = def.gotten_mesh
2591 -- set child objects to half size
2592 if self.child == true then
2594 vis_size = {
2595 x = self.base_size.x * .5,
2596 y = self.base_size.y * .5,
2599 if def.child_texture then
2600 textures = def.child_texture[1]
2603 colbox = {
2604 self.base_colbox[1] * .5,
2605 self.base_colbox[2] * .5,
2606 self.base_colbox[3] * .5,
2607 self.base_colbox[4] * .5,
2608 self.base_colbox[5] * .5,
2609 self.base_colbox[6] * .5
2611 selbox = {
2612 self.base_selbox[1] * .5,
2613 self.base_selbox[2] * .5,
2614 self.base_selbox[3] * .5,
2615 self.base_selbox[4] * .5,
2616 self.base_selbox[5] * .5,
2617 self.base_selbox[6] * .5
2621 if self.health == 0 then
2622 self.health = random (self.hp_min, self.hp_max)
2625 -- pathfinding init
2626 self.path = {}
2627 self.path.way = {} -- path to follow, table of positions
2628 self.path.lastpos = {x = 0, y = 0, z = 0}
2629 self.path.stuck = false
2630 self.path.following = false -- currently following path?
2631 self.path.stuck_timer = 0 -- if stuck for too long search for path
2633 -- mob defaults
2634 self.object:set_armor_groups({immortal = 1, fleshy = self.armor})
2635 self.old_y = self.object:get_pos().y
2636 self.old_health = self.health
2637 self.sounds.distance = self.sounds.distance or 10
2638 self.textures = textures
2639 self.mesh = mesh
2640 self.collisionbox = colbox
2641 self.selectionbox = selbox
2642 self.visual_size = vis_size
2643 self.standing_in = ""
2645 -- check existing nametag
2646 if not self.nametag then
2647 self.nametag = def.nametag
2650 -- set anything changed above
2651 self.object:set_properties(self)
2652 set_yaw(self.object, (random(0, 360) - 180) / 180 * pi)
2653 update_tag(self)
2654 set_animation(self, "stand")
2656 -- run on_spawn function if found
2657 if self.on_spawn and not self.on_spawn_run then
2658 if self.on_spawn(self) then
2659 self.on_spawn_run = true -- if true, set flag to run once only
2663 -- run after_activate
2664 if def.after_activate then
2665 def.after_activate(self, staticdata, def, dtime)
2668 if use_cmi then
2669 self._cmi_components = cmi.activate_components(self.serialized_cmi_components)
2670 cmi.notify_activate(self.object, dtime)
2675 -- main mob function
2676 local mob_step = function(self, dtime)
2678 if use_cmi then
2679 cmi.notify_step(self.object, dtime)
2682 local pos = self.object:get_pos()
2683 local yaw = 0
2685 -- when lifetimer expires remove mob (except npc and tamed)
2686 if self.type ~= "npc"
2687 and not self.tamed
2688 and self.state ~= "attack"
2689 and remove_far ~= true
2690 and self.lifetimer < 20000 then
2692 self.lifetimer = self.lifetimer - dtime
2694 if self.lifetimer <= 0 then
2696 -- only despawn away from player
2697 local objs = minetest.get_objects_inside_radius(pos, 15)
2699 for n = 1, #objs do
2701 if objs[n]:is_player() then
2703 self.lifetimer = 20
2705 return
2709 -- minetest.log("action",
2710 -- S("lifetimer expired, removed @1", self.name))
2712 effect(pos, 15, "tnt_smoke.png", 2, 4, 2, 0)
2714 self.object:remove()
2716 return
2720 falling(self, pos)
2722 -- knockback timer
2723 if self.pause_timer > 0 then
2725 self.pause_timer = self.pause_timer - dtime
2727 return
2730 -- run custom function (defined in mob lua file)
2731 if self.do_custom then
2733 -- when false skip going any further
2734 if self.do_custom(self, dtime) == false then
2735 return
2739 -- attack timer
2740 self.timer = self.timer + dtime
2742 if self.state ~= "attack" then
2744 if self.timer < 1 then
2745 return
2748 self.timer = 0
2751 -- never go over 100
2752 if self.timer > 100 then
2753 self.timer = 1
2756 -- node replace check (cow eats grass etc.)
2757 replace(self, pos)
2759 -- mob plays random sound at times
2760 if random(1, 100) == 1 then
2761 mob_sound(self, self.sounds.random)
2764 -- environmental damage timer (every 1 second)
2765 self.env_damage_timer = self.env_damage_timer + dtime
2767 if (self.state == "attack" and self.env_damage_timer > 1)
2768 or self.state ~= "attack" then
2770 self.env_damage_timer = 0
2772 do_env_damage(self)
2775 monster_attack(self)
2777 npc_attack(self)
2779 breed(self)
2781 follow_flop(self)
2783 do_states(self, dtime)
2785 do_jump(self)
2787 runaway_from(self)
2792 -- default function when mobs are blown up with TNT
2793 local do_tnt = function(obj, damage)
2795 --print ("----- Damage", damage)
2797 obj.object:punch(obj.object, 1.0, {
2798 full_punch_interval = 1.0,
2799 damage_groups = {fleshy = damage},
2800 }, nil)
2802 return false, true, {}
2806 mobs.spawning_mobs = {}
2808 -- register mob entity
2809 function mobs:register_mob(name, def)
2811 mobs.spawning_mobs[name] = true
2813 minetest.register_entity(name, {
2815 stepheight = def.stepheight or 1.1, -- was 0.6
2816 name = name,
2817 type = def.type,
2818 attack_type = def.attack_type,
2819 fly = def.fly,
2820 fly_in = def.fly_in or "air",
2821 owner = def.owner or "",
2822 order = def.order or "",
2823 on_die = def.on_die,
2824 do_custom = def.do_custom,
2825 jump_height = def.jump_height or 4, -- was 6
2826 drawtype = def.drawtype, -- DEPRECATED, use rotate instead
2827 rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
2828 lifetimer = def.lifetimer or 180, -- 3 minutes
2829 hp_min = max(1, (def.hp_min or 5) * difficulty),
2830 hp_max = max(1, (def.hp_max or 10) * difficulty),
2831 physical = true,
2832 collisionbox = def.collisionbox,
2833 selectionbox = def.selectionbox or def.collisionbox,
2834 visual = def.visual,
2835 visual_size = def.visual_size or {x = 1, y = 1},
2836 mesh = def.mesh,
2837 makes_footstep_sound = def.makes_footstep_sound or false,
2838 view_range = def.view_range or 5,
2839 walk_velocity = def.walk_velocity or 1,
2840 run_velocity = def.run_velocity or 2,
2841 damage = max(0, (def.damage or 0) * difficulty),
2842 light_damage = def.light_damage or 0,
2843 water_damage = def.water_damage or 0,
2844 lava_damage = def.lava_damage or 0,
2845 suffocation = def.suffocation or 2,
2846 fall_damage = def.fall_damage or 1,
2847 fall_speed = def.fall_speed or -10, -- must be lower than -2 (default: -10)
2848 drops = def.drops or {},
2849 armor = def.armor or 100,
2850 on_rightclick = def.on_rightclick,
2851 arrow = def.arrow,
2852 shoot_interval = def.shoot_interval,
2853 sounds = def.sounds or {},
2854 animation = def.animation,
2855 follow = def.follow,
2856 jump = def.jump ~= false,
2857 walk_chance = def.walk_chance or 50,
2858 attacks_monsters = def.attacks_monsters or false,
2859 group_attack = def.group_attack or false,
2860 passive = def.passive or false,
2861 knock_back = def.knock_back or 3,
2862 blood_amount = def.blood_amount or 5,
2863 blood_texture = def.blood_texture or "mobs_blood.png",
2864 shoot_offset = def.shoot_offset or 0,
2865 floats = def.floats or 1, -- floats in water by default
2866 replace_rate = def.replace_rate,
2867 replace_what = def.replace_what,
2868 replace_with = def.replace_with,
2869 replace_offset = def.replace_offset or 0,
2870 on_replace = def.on_replace,
2871 timer = 0,
2872 env_damage_timer = 0, -- only used when state = "attack"
2873 tamed = false,
2874 pause_timer = 0,
2875 horny = false,
2876 hornytimer = 0,
2877 child = false,
2878 gotten = false,
2879 health = 0,
2880 reach = def.reach or 3,
2881 htimer = 0,
2882 texture_list = def.textures,
2883 child_texture = def.child_texture,
2884 docile_by_day = def.docile_by_day or false,
2885 time_of_day = 0.5,
2886 fear_height = def.fear_height or 0,
2887 runaway = def.runaway,
2888 runaway_timer = 0,
2889 pathfinding = def.pathfinding,
2890 immune_to = def.immune_to or {},
2891 explosion_radius = def.explosion_radius,
2892 explosion_timer = def.explosion_timer or 3,
2893 custom_attack = def.custom_attack,
2894 double_melee_attack = def.double_melee_attack,
2895 dogshoot_switch = def.dogshoot_switch,
2896 dogshoot_count = 0,
2897 dogshoot_count_max = def.dogshoot_count_max or 5,
2898 dogshoot_count2_max = def.dogshoot_count2_max or (def.dogshoot_count_max or 5),
2899 attack_animals = def.attack_animals or false,
2900 specific_attack = def.specific_attack,
2901 runaway_from = def.runaway_from,
2902 owner_loyal = def.owner_loyal,
2903 facing_fence = false,
2904 _cmi_is_mob = true,
2906 on_spawn = def.on_spawn,
2908 on_blast = def.on_blast or do_tnt,
2910 on_step = mob_step,
2912 do_punch = def.do_punch,
2914 on_punch = mob_punch,
2916 on_breed = def.on_breed,
2918 on_grown = def.on_grown,
2920 on_activate = function(self, staticdata, dtime)
2921 return mob_activate(self, staticdata, def, dtime)
2922 end,
2924 get_staticdata = function(self)
2925 return mob_staticdata(self)
2926 end,
2930 end -- END mobs:register_mob function
2933 -- count how many mobs of one type are inside an area
2934 local count_mobs = function(pos, type)
2936 local num_type = 0
2937 local num_total = 0
2938 local objs = minetest.get_objects_inside_radius(pos, aoc_range)
2940 for n = 1, #objs do
2942 if not objs[n]:is_player() then
2944 local obj = objs[n]:get_luaentity()
2946 -- count mob type and add to total also
2947 if obj and obj.name and obj.name == type then
2949 num_type = num_type + 1
2950 num_total = num_total + 1
2952 -- add to total mobs
2953 elseif obj and obj.name and obj.health ~= nil then
2955 num_total = num_total + 1
2960 return num_type, num_total
2964 -- global functions
2966 function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
2967 interval, chance, aoc, min_height, max_height, day_toggle, on_spawn)
2969 -- Do mobs spawn at all?
2970 if not mobs_spawn then
2971 return
2974 -- chance/spawn number override in minetest.conf for registered mob
2975 local numbers = minetest.settings:get(name)
2977 if numbers then
2978 numbers = numbers:split(",")
2979 chance = tonumber(numbers[1]) or chance
2980 aoc = tonumber(numbers[2]) or aoc
2982 if chance == 0 then
2983 minetest.log("warning", string.format("[mobs] %s has spawning disabled", name))
2984 return
2987 minetest.log("action",
2988 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name, chance, aoc))
2992 minetest.register_abm({
2994 label = name .. " spawning",
2995 nodenames = nodes,
2996 neighbors = neighbors,
2997 interval = interval,
2998 chance = max(1, (chance * mob_chance_multiplier)),
2999 catch_up = false,
3001 action = function(pos, node, active_object_count, active_object_count_wider)
3003 -- is mob actually registered?
3004 if not mobs.spawning_mobs[name]
3005 or not minetest.registered_entities[name] then
3006 --print ("--- mob doesn't exist", name)
3007 return
3010 -- do not spawn if too many of same mob in area
3011 if active_object_count_wider >= max_per_block
3012 or count_mobs(pos, name) >= aoc then
3013 --print ("--- too many entities", name, aoc, active_object_count_wider)
3014 return
3017 -- if toggle set to nil then ignore day/night check
3018 if day_toggle ~= nil then
3020 local tod = (minetest.get_timeofday() or 0) * 24000
3022 if tod > 4500 and tod < 19500 then
3023 -- daylight, but mob wants night
3024 if day_toggle == false then
3025 --print ("--- mob needs night", name)
3026 return
3028 else
3029 -- night time but mob wants day
3030 if day_toggle == true then
3031 --print ("--- mob needs day", name)
3032 return
3037 -- spawn above node
3038 pos.y = pos.y + 1
3040 -- only spawn away from player
3041 local objs = minetest.get_objects_inside_radius(pos, 10)
3043 for n = 1, #objs do
3045 if objs[n]:is_player() then
3046 --print ("--- player too close", name)
3047 return
3051 -- mobs cannot spawn in protected areas when enabled
3052 if not spawn_protected
3053 and minetest.is_protected(pos, "") then
3054 --print ("--- inside protected area", name)
3055 return
3058 -- are we spawning within height limits?
3059 if pos.y > max_height
3060 or pos.y < min_height then
3061 --print ("--- height limits not met", name, pos.y)
3062 return
3065 -- are light levels ok?
3066 local light = minetest.get_node_light(pos)
3067 if not light
3068 or light > max_light
3069 or light < min_light then
3070 --print ("--- light limits not met", name, light)
3071 return
3074 -- do we have enough height clearance to spawn mob?
3075 local ent = minetest.registered_entities[name]
3076 local height = max(0, math.ceil(ent.collisionbox[5] - ent.collisionbox[2]) - 1)
3078 for n = 0, height do
3080 local pos2 = {x = pos.x, y = pos.y + n, z = pos.z}
3082 if minetest.registered_nodes[node_ok(pos2).name].walkable == true then
3083 --print ("--- inside block", name, node_ok(pos2).name)
3084 return
3088 -- spawn mob half block higher than ground
3089 pos.y = pos.y + 0.5
3091 local mob = minetest.add_entity(pos, name)
3092 --[[
3093 print ("[mobs] Spawned " .. name .. " at "
3094 .. minetest.pos_to_string(pos) .. " on "
3095 .. node.name .. " near " .. neighbors[1])
3097 if on_spawn then
3099 local ent = mob:get_luaentity()
3101 on_spawn(ent, pos)
3108 -- compatibility with older mob registration
3109 function mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
3111 mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
3112 chance, active_object_count, -31000, max_height, day_toggle)
3116 -- MarkBu's spawn function
3117 function mobs:spawn(def)
3119 local name = def.name
3120 local nodes = def.nodes or {"group:soil", "group:stone"}
3121 local neighbors = def.neighbors or {"air"}
3122 local min_light = def.min_light or 0
3123 local max_light = def.max_light or 15
3124 local interval = def.interval or 30
3125 local chance = def.chance or 5000
3126 local active_object_count = def.active_object_count or 1
3127 local min_height = def.min_height or -31000
3128 local max_height = def.max_height or 31000
3129 local day_toggle = def.day_toggle
3130 local on_spawn = def.on_spawn
3132 mobs:spawn_specific(name, nodes, neighbors, min_light, max_light, interval,
3133 chance, active_object_count, min_height, max_height, day_toggle, on_spawn)
3137 -- register arrow for shoot attack
3138 function mobs:register_arrow(name, def)
3140 if not name or not def then return end -- errorcheck
3142 minetest.register_entity(name, {
3144 physical = false,
3145 visual = def.visual,
3146 visual_size = def.visual_size,
3147 textures = def.textures,
3148 velocity = def.velocity,
3149 hit_player = def.hit_player,
3150 hit_node = def.hit_node,
3151 hit_mob = def.hit_mob,
3152 drop = def.drop or false, -- drops arrow as registered item when true
3153 collisionbox = {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3154 timer = 0,
3155 switch = 0,
3156 owner_id = def.owner_id,
3157 rotate = def.rotate,
3158 automatic_face_movement_dir = def.rotate
3159 and (def.rotate - (pi / 180)) or false,
3161 on_activate = def.on_activate,
3163 on_step = def.on_step or function(self, dtime)
3165 self.timer = self.timer + 1
3167 local pos = self.object:get_pos()
3169 if self.switch == 0
3170 or self.timer > 150
3171 or not within_limits(pos, 0) then
3173 self.object:remove() ; -- print ("removed arrow")
3175 return
3178 -- does arrow have a tail (fireball)
3179 if def.tail
3180 and def.tail == 1
3181 and def.tail_texture then
3183 minetest.add_particle({
3184 pos = pos,
3185 velocity = {x = 0, y = 0, z = 0},
3186 acceleration = {x = 0, y = 0, z = 0},
3187 expirationtime = def.expire or 0.25,
3188 collisiondetection = false,
3189 texture = def.tail_texture,
3190 size = def.tail_size or 5,
3191 glow = def.glow or 0,
3195 if self.hit_node then
3197 local node = node_ok(pos).name
3199 if minetest.registered_nodes[node].walkable then
3201 self.hit_node(self, pos, node)
3203 if self.drop == true then
3205 pos.y = pos.y + 1
3207 self.lastpos = (self.lastpos or pos)
3209 minetest.add_item(self.lastpos, self.object:get_luaentity().name)
3212 self.object:remove() ; -- print ("hit node")
3214 return
3218 if self.hit_player or self.hit_mob then
3220 for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do
3222 if self.hit_player
3223 and player:is_player() then
3225 self.hit_player(self, player)
3226 self.object:remove() ; -- print ("hit player")
3227 return
3230 local entity = player:get_luaentity()
3232 if entity
3233 and self.hit_mob
3234 and entity._cmi_is_mob == true
3235 and tostring(player) ~= self.owner_id
3236 and entity.name ~= self.object:get_luaentity().name then
3238 self.hit_mob(self, player)
3240 self.object:remove() ; --print ("hit mob")
3242 return
3247 self.lastpos = pos
3253 -- compatibility function
3254 function mobs:explosion(pos, radius)
3255 local self = {sounds = {}}
3256 self.sounds.explode = "tnt_explode"
3257 mobs:boom(self, pos, radius)
3261 -- no damage to nodes explosion
3262 function mobs:safe_boom(self, pos, radius)
3264 minetest.sound_play(self.sounds and self.sounds.explode or "tnt_explode", {
3265 pos = pos,
3266 gain = 1.0,
3267 max_hear_distance = self.sounds and self.sounds.distance or 32
3270 entity_physics(pos, radius)
3271 effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
3275 -- make explosion with protection and tnt mod check
3276 function mobs:boom(self, pos, radius)
3278 if mobs_griefing
3279 and minetest.get_modpath("mcl_tnt") and tnt and tnt.boom
3280 and not minetest.is_protected(pos, "") then
3282 tnt.boom(pos, {
3283 radius = radius,
3284 damage_radius = radius,
3285 sound = self.sounds and self.sounds.explode,
3286 explode_center = true,
3288 else
3289 mobs:safe_boom(self, pos, radius)
3294 -- Register spawn eggs
3296 -- Note: This also introduces the “spawn_egg” group:
3297 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3298 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3299 function mobs:register_egg(mob, desc, background, addegg, no_creative)
3301 local grp = {spawn_egg = 1}
3303 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3304 if creative and no_creative == true then
3305 grp.not_in_creative_inventory = 1
3308 local invimg = background
3310 if addegg == 1 then
3311 invimg = "mobs_chicken_egg.png^(" .. invimg ..
3312 "^[mask:mobs_chicken_egg_overlay.png)"
3315 -- register old stackable mob egg
3316 minetest.register_craftitem(mob, {
3318 description = desc,
3319 inventory_image = invimg,
3320 groups = grp,
3321 _doc_items_longdesc = "This allows you to place a single mob.",
3322 _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.",
3324 on_place = function(itemstack, placer, pointed_thing)
3326 local pos = pointed_thing.above
3328 -- am I clicking on something with existing on_rightclick function?
3329 local under = minetest.get_node(pointed_thing.under)
3330 local def = minetest.registered_nodes[under.name]
3331 if def and def.on_rightclick then
3332 return def.on_rightclick(pointed_thing.under, under, placer, itemstack)
3335 if pos
3336 and within_limits(pos, 0)
3337 and not minetest.is_protected(pos, placer:get_player_name()) then
3339 local name = placer:get_player_name()
3340 local privs = minetest.get_player_privs(name)
3341 if not privs.maphack then
3342 minetest.chat_send_player(name, "You need the “maphack” privilege to change the mob spawner.")
3343 return itemstack
3345 if minetest.get_modpath("mcl_mobspawners") and under.name == "mcl_mobspawners:spawner" then
3346 mcl_mobspawners.setup_spawner(pointed_thing.under, itemstack:get_name())
3347 if not minetest.settings:get_bool("creative_mode") then
3348 itemstack:take_item()
3350 return itemstack
3353 if not minetest.registered_entities[mob] then
3354 return
3357 pos.y = pos.y + 1
3359 local mob = minetest.add_entity(pos, mob)
3360 local ent = mob:get_luaentity()
3362 -- don't set owner if monster or sneak pressed
3363 if ent.type ~= "monster"
3364 and not placer:get_player_control().sneak then
3365 ent.owner = placer:get_player_name()
3366 ent.tamed = true
3369 -- if not in creative then take item
3370 if not mobs.is_creative(placer:get_player_name()) then
3371 itemstack:take_item()
3375 return itemstack
3376 end,
3382 -- capture critter (thanks to blert2112 for idea)
3383 function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
3385 if self.child
3386 or not clicker:is_player()
3387 or not clicker:get_inventory() then
3388 return false
3391 -- get name of clicked mob
3392 local mobname = self.name
3394 -- if not nil change what will be added to inventory
3395 if replacewith then
3396 mobname = replacewith
3399 local name = clicker:get_player_name()
3400 local tool = clicker:get_wielded_item()
3402 -- are we using hand, net or lasso to pick up mob?
3403 if tool:get_name() ~= ""
3404 and tool:get_name() ~= "mobs:net"
3405 and tool:get_name() ~= "mobs:lasso" then
3406 return false
3409 -- is mob tamed?
3410 if self.tamed == false
3411 and force_take == false then
3413 minetest.chat_send_player(name, S("Not tamed!"))
3415 return true -- false
3418 -- cannot pick up if not owner
3419 if self.owner ~= name
3420 and force_take == false then
3422 minetest.chat_send_player(name, S("@1 is owner!", self.owner))
3424 return true -- false
3427 if clicker:get_inventory():room_for_item("main", mobname) then
3429 -- was mob clicked with hand, net, or lasso?
3430 local chance = 0
3432 if tool:get_name() == "" then
3433 chance = chance_hand
3435 elseif tool:get_name() == "mobs:net" then
3437 chance = chance_net
3439 tool:add_wear(4000) -- 17 uses
3441 clicker:set_wielded_item(tool)
3443 elseif tool:get_name() == "mobs:lasso" then
3445 chance = chance_lasso
3447 tool:add_wear(650) -- 100 uses
3449 clicker:set_wielded_item(tool)
3453 -- calculate chance.. add to inventory if successful?
3454 if chance > 0 and random(1, 100) <= chance then
3456 -- default mob egg
3457 local new_stack = ItemStack(mobname)
3459 -- add special mob egg with all mob information
3460 -- unless 'replacewith' contains new item to use
3461 if not replacewith then
3463 new_stack = ItemStack(mobname .. "_set")
3465 local tmp = {}
3467 for _,stat in pairs(self) do
3468 local t = type(stat)
3469 if t ~= "function"
3470 and t ~= "nil"
3471 and t ~= "userdata" then
3472 tmp[_] = self[_]
3476 local data_str = minetest.serialize(tmp)
3478 new_stack:set_metadata(data_str)
3481 local inv = clicker:get_inventory()
3483 if inv:room_for_item("main", new_stack) then
3484 inv:add_item("main", new_stack)
3485 else
3486 minetest.add_item(clicker:get_pos(), new_stack)
3489 self.object:remove()
3491 mob_sound(self, "default_place_node_hard")
3494 else
3495 minetest.chat_send_player(name, S("Missed!"))
3497 mob_sound(self, "mobs_swing")
3501 return true
3505 -- protect tamed mob with rune item
3506 function mobs:protect(self, clicker)
3508 local name = clicker:get_player_name()
3509 local tool = clicker:get_wielded_item()
3511 if tool:get_name() ~= "mobs:protector" then
3512 return false
3515 if self.tamed == false then
3516 minetest.chat_send_player(name, S("Not tamed!"))
3517 return true -- false
3520 if self.protected == true then
3521 minetest.chat_send_player(name, S("Already protected!"))
3522 return true -- false
3525 if not mobs.is_creative(clicker:get_player_name()) then
3526 tool:take_item() -- take 1 protection rune
3527 clicker:set_wielded_item(tool)
3530 self.protected = true
3532 local pos = self.object:get_pos()
3533 pos.y = pos.y + self.collisionbox[2] + 0.5
3535 effect(self.object:get_pos(), 25, "mobs_protect_particle.png", 0.5, 4, 2, 15)
3537 mob_sound(self, "mobs_spell")
3539 return true
3543 local mob_obj = {}
3544 local mob_sta = {}
3546 -- feeding, taming and breeding (thanks blert2112)
3547 function mobs:feed_tame(self, clicker, feed_count, breed, tame)
3549 if not self.follow then
3550 return false
3553 -- can eat/tame with item in hand
3554 if follow_holding(self, clicker) then
3556 -- if not in creative then take item
3557 if not mobs.is_creative(clicker:get_player_name()) then
3559 local item = clicker:get_wielded_item()
3561 item:take_item()
3563 clicker:set_wielded_item(item)
3566 -- increase health
3567 self.health = self.health + 4
3569 if self.health >= self.hp_max then
3571 self.health = self.hp_max
3573 if self.htimer < 1 then
3575 self.htimer = 5
3579 self.object:set_hp(self.health)
3581 update_tag(self)
3583 -- make children grow quicker
3584 if self.child == true then
3586 self.hornytimer = self.hornytimer + 20
3588 return true
3591 -- feed and tame
3592 self.food = (self.food or 0) + 1
3593 if self.food >= feed_count then
3595 self.food = 0
3597 if breed and self.hornytimer == 0 then
3598 self.horny = true
3601 self.gotten = false
3603 if tame then
3605 if self.tamed == false then
3606 minetest.chat_send_player(clicker:get_player_name(),
3607 S("@1 has been tamed!",
3608 self.name:split(":")[2]))
3611 self.tamed = true
3613 if not self.owner or self.owner == "" then
3614 self.owner = clicker:get_player_name()
3618 -- make sound when fed so many times
3619 mob_sound(self, self.sounds.random)
3622 return true
3625 local item = clicker:get_wielded_item()
3627 -- if mob has been tamed you can name it with a nametag
3628 if item:get_name() == "mobs:nametag"
3629 and clicker:get_player_name() == self.owner then
3631 local name = clicker:get_player_name()
3633 -- store mob and nametag stack in external variables
3634 mob_obj[name] = self
3635 mob_sta[name] = item
3637 local tag = self.nametag or ""
3639 minetest.show_formspec(name, "mobs_nametag", "size[8,4]"
3640 .. mcl_vars.gui_bg
3641 .. mcl_vars.gui_bg_img
3642 .. "field[0.5,1;7.5,0;name;" .. minetest.formspec_escape(S("Enter name:")) .. ";" .. tag .. "]"
3643 .. "button_exit[2.5,3.5;3,1;mob_rename;" .. minetest.formspec_escape(S("Rename")) .. "]")
3647 return false
3652 -- inspired by blockmen's nametag mod
3653 minetest.register_on_player_receive_fields(function(player, formname, fields)
3655 -- right-clicked with nametag and name entered?
3656 if formname == "mobs_nametag"
3657 and fields.name
3658 and fields.name ~= "" then
3660 local name = player:get_player_name()
3662 if not mob_obj[name]
3663 or not mob_obj[name].object then
3664 return
3667 -- limit name entered to 64 characters long
3668 if string.len(fields.name) > 64 then
3669 fields.name = string.sub(fields.name, 1, 64)
3672 -- update nametag
3673 mob_obj[name].nametag = fields.name
3675 update_tag(mob_obj[name])
3677 -- if not in creative then take item
3678 if not mobs.is_creative(name) then
3680 mob_sta[name]:take_item()
3682 player:set_wielded_item(mob_sta[name])
3685 -- reset external variables
3686 mob_obj[name] = nil
3687 mob_sta[name] = nil
3690 end)
3693 -- compatibility function for old entities to new modpack entities
3694 function mobs:alias_mob(old_name, new_name)
3696 -- spawn egg
3697 minetest.register_alias(old_name, new_name)
3699 -- entity
3700 minetest.register_entity(":" .. old_name, {
3702 physical = false,
3704 on_step = function(self)
3706 local pos = self.object:get_pos()
3708 if minetest.registered_entities[new_name] then
3709 minetest.add_entity(pos, new_name)
3712 self.object:remove()