6 mobs
.version
= "20180523"
8 local MAX_MOB_NAME_LENGTH
= 30
11 local MP
= minetest
.get_modpath(minetest
.get_current_modname())
12 local S
, NS
= dofile(MP
.. "/intllib.lua")
17 local use_cmi
= minetest
.global_exists("cmi")
20 -- Invisibility mod check
22 if minetest
.global_exists("invisibility") then
23 mobs
.invis
= invisibility
28 local creative_mode_cache
= minetest
.settings
:get_bool("creative_mode")
29 function mobs
.is_creative(name
)
30 return creative_mode_cache
or minetest
.check_player_privs(name
, {creative
= true})
34 -- localize math functions
36 local square
= math
.sqrt
42 local atann
= math
.atan
43 local random = math
.random
44 local floor = math
.floor
45 local atan = function(x
)
46 if not x
or x
~= x
then
47 --error("atan bassed NaN")
56 local damage_enabled
= minetest
.settings
:get_bool("enable_damage")
57 local mobs_spawn
= minetest
.settings
:get_bool("mobs_spawn") ~= false
58 local peaceful_only
= minetest
.settings
:get_bool("only_peaceful_mobs")
59 local disable_blood
= minetest
.settings
:get_bool("mobs_disable_blood")
60 local mobs_drop_items
= minetest
.settings
:get_bool("mobs_drop_items") ~= false
61 local mobs_griefing
= minetest
.settings
:get_bool("mobs_griefing") ~= false
62 local creative
= minetest
.settings
:get_bool("creative_mode")
63 local spawn_protected
= minetest
.settings
:get_bool("mobs_spawn_protected") ~= false
64 local remove_far
= false
65 local difficulty
= tonumber(minetest
.settings
:get("mob_difficulty")) or 1.0
66 local show_health
= false
67 local max_per_block
= tonumber(minetest
.settings
:get("max_objects_per_block") or 99)
68 local mob_chance_multiplier
= tonumber(minetest
.settings
:get("mob_chance_multiplier") or 1)
70 -- Peaceful mode message so players will know there are no monsters
72 minetest
.register_on_joinplayer(function(player
)
73 minetest
.chat_send_player(player
:get_player_name(),
74 S("** Peaceful Mode Active - No Monsters Will Spawn"))
78 -- calculate aoc range for mob count
79 local aosrb
= tonumber(minetest
.settings
:get("active_object_send_range_blocks"))
80 local abr
= tonumber(minetest
.settings
:get("active_block_range"))
81 local aoc_range
= max(aosrb
, abr
) * 16
83 -- pathfinding settings
84 local enable_pathfinding
= true
85 local stuck_timeout
= 3 -- how long before mob gets stuck in place and starts searching
86 local stuck_path_timeout
= 10 -- how long will mob follow path before giving up
89 local node_fire
= "mcl_fire:fire"
90 local node_permanent_flame
= "mcl_fire:eternal_fire"
91 local node_ice
= "mcl_core:ice"
92 local node_snowblock
= "mcl_core:snowblock"
93 local node_snow
= "mcl_core:snow"
94 mobs
.fallback_node
= minetest
.registered_aliases
["mapgen_dirt"] or "mcl_core:dirt"
98 local mob_sound
= function(self
, sound
)
101 minetest
.sound_play(sound
, {
102 object
= self
.object
,
104 max_hear_distance
= self
.sounds
.distance
111 local do_attack
= function(self
, player
)
113 if self
.state
== "attack" then
118 self
.state
= "attack"
120 if random(0, 100) < 90 then
121 mob_sound(self
, self
.sounds
.war_cry
)
126 -- move mob in facing direction
127 local set_velocity
= function(self
, v
)
129 -- do not move if mob has been ordered to stay
130 if self
.order
== "stand" then
131 self
.object
:setvelocity({x
= 0, y
= 0, z
= 0})
135 local yaw
= (self
.object
:get_yaw() or 0) + self
.rotate
137 self
.object
:setvelocity({
139 y
= self
.object
:getvelocity().y
,
145 -- calculate mob velocity
146 local get_velocity
= function(self
)
148 local v
= self
.object
:getvelocity()
150 return (v
.x
* v
.x
+ v
.z
* v
.z
) ^
0.5
154 -- set and return valid yaw
155 local set_yaw
= function(self
, yaw
, delay
)
157 if not yaw
or yaw
~= yaw
then
164 self
.object
:set_yaw(yaw
)
168 self
.target_yaw
= yaw
171 return self
.target_yaw
174 -- global function to set mob yaw
175 function mobs
:yaw(self
, yaw
, delay
)
176 set_yaw(self
, yaw
, delay
)
180 -- set defined animation
181 local set_animation
= function(self
, anim
)
183 if not self
.animation
184 or not anim
then return end
186 self
.animation
.current
= self
.animation
.current
or ""
188 if anim
== self
.animation
.current
189 or not self
.animation
[anim
.. "_start"]
190 or not self
.animation
[anim
.. "_end"] then
194 self
.animation
.current
= anim
196 self
.object
:set_animation({
197 x
= self
.animation
[anim
.. "_start"],
198 y
= self
.animation
[anim
.. "_end"]},
199 self
.animation
[anim
.. "_speed"] or self
.animation
.speed_normal
or 15,
200 0, self
.animation
[anim
.. "_loop"] ~= false)
204 -- above function exported for mount.lua
205 function mobs
:set_animation(self
, anim
)
206 set_animation(self
, anim
)
210 -- calculate distance
211 local get_distance
= function(a
, b
)
213 local x
, y
, z
= a
.x
- b
.x
, a
.y
- b
.y
, a
.z
- b
.z
215 return square(x
* x
+ y
* y
+ z
* z
)
219 -- check line of sight (BrunoMine)
220 local line_of_sight
= function(self
, pos1
, pos2
, stepsize
)
222 stepsize
= stepsize
or 1
224 local s
, pos
= minetest
.line_of_sight(pos1
, pos2
, stepsize
)
226 -- normal walking and flying mobs can see you through air
231 -- New pos1 to be analyzed
232 local npos1
= {x
= pos1
.x
, y
= pos1
.y
, z
= pos1
.z
}
234 local r
, pos
= minetest
.line_of_sight(npos1
, pos2
, stepsize
)
237 if r
== true then return true end
240 local nn
= minetest
.get_node(pos
).name
242 -- Target Distance (td) to travel
243 local td
= get_distance(pos1
, pos2
)
245 -- Actual Distance (ad) traveled
248 -- It continues to advance in the line of sight in search of a real
249 -- obstruction which counts as 'normal' nodebox.
250 while minetest
.registered_nodes
[nn
]
251 and (minetest
.registered_nodes
[nn
].walkable
== false
252 or minetest
.registered_nodes
[nn
].drawtype
== "nodebox") do
254 -- Check if you can still move forward
255 if td
< ad
+ stepsize
then
256 return true -- Reached the target
259 -- Moves the analyzed pos
260 local d
= get_distance(pos1
, pos2
)
262 npos1
.x
= ((pos2
.x
- pos1
.x
) / d
* stepsize
) + pos1
.x
263 npos1
.y
= ((pos2
.y
- pos1
.y
) / d
* stepsize
) + pos1
.y
264 npos1
.z
= ((pos2
.z
- pos1
.z
) / d
* stepsize
) + pos1
.z
268 or npos1
.x
~= npos1
.x
269 or npos1
.y
~= npos1
.y
270 or npos1
.z
~= npos1
.z
then
277 r
, pos
= minetest
.line_of_sight(npos1
, pos2
, stepsize
)
279 if r
== true then return true end
281 -- New Nodename found
282 nn
= minetest
.get_node(pos
).name
290 -- are we flying in what we are suppose to? (taikedz)
291 local flight_check
= function(self
, pos_w
)
293 local nod
= self
.standing_in
294 local def
= minetest
.registered_nodes
[nod
]
296 if not def
then return false end -- nil check
298 if type(self
.fly_in
) == "string"
299 and nod
== self
.fly_in
then
303 elseif type(self
.fly_in
) == "table" then
305 for _
,fly_in
in pairs(self
.fly_in
) do
307 if nod
== fly_in
then
314 -- stops mobs getting stuck inside stairs and plantlike nodes
315 if def
.drawtype
~= "airlike"
316 and def
.drawtype
~= "liquid"
317 and def
.drawtype
~= "flowingliquid" then
325 -- custom particle effects
326 local effect
= function(pos
, amount
, texture
, min_size
, max_size
, radius
, gravity
, glow
)
329 min_size
= min_size
or 0.5
330 max_size
= max_size
or 1
331 gravity
= gravity
or -10
334 minetest
.add_particlespawner({
339 minvel
= {x
= -radius
, y
= -radius
, z
= -radius
},
340 maxvel
= {x
= radius
, y
= radius
, z
= radius
},
341 minacc
= {x
= 0, y
= gravity
, z
= 0},
342 maxacc
= {x
= 0, y
= gravity
, z
= 0},
353 local update_tag
= function(self
)
356 local col
= "#00FF00"
357 local qua
= self
.hp_max
/ 4
359 if self
.health
<= floor(qua
* 3) then
363 if self
.health
<= floor(qua
* 2) then
367 if self
.health
<= floor(qua
) then
372 self
.object
:set_properties({
373 nametag
= self
.nametag
,
380 local item_drop
= function(self
, cooked
)
382 -- no drops if disabled by setting
383 if not mobs_drop_items
then return end
385 -- no drops for child mobs
386 if self
.child
then return end
389 local pos
= self
.object
:get_pos()
391 self
.drops
= self
.drops
or {} -- nil check
393 for n
= 1, #self
.drops
do
395 if random(1, self
.drops
[n
].chance
) == 1 then
397 num
= random(self
.drops
[n
].min or 1, self
.drops
[n
].max or 1)
398 item
= self
.drops
[n
].name
400 -- cook items when true
403 local output
= minetest
.get_craft_result({
404 method
= "cooking", width
= 1, items
= {item
}})
406 if output
and output
.item
and not output
.item
:is_empty() then
407 item
= output
.item
:get_name()
411 -- add item if it exists
412 obj
= minetest
.add_item(pos
, ItemStack(item
.. " " .. num
))
414 if obj
and obj
:get_luaentity() then
417 x
= random(-10, 10) / 9,
419 z
= random(-10, 10) / 9,
422 obj
:remove() -- item does not exist
431 -- check if mob is dead or only hurt
432 local check_for_death
= function(self
, cause
, cmi_cause
)
434 -- has health actually changed?
435 if self
.health
== self
.old_health
and self
.health
> 0 then
439 self
.old_health
= self
.health
441 -- still got some health? play hurt sound
442 if self
.health
> 0 then
444 mob_sound(self
, self
.sounds
.damage
)
446 -- make sure health isn't higher than max
447 if self
.health
> self
.hp_max
then
448 self
.health
= self
.hp_max
451 -- backup nametag so we can show health stats
452 if not self
.nametag2
then
453 self
.nametag2
= self
.nametag
or ""
457 and (cmi_cause
and cmi_cause
.type == "punch") then
460 self
.nametag
= "♥ " .. self
.health
.. " / " .. self
.hp_max
468 -- dropped cooked item if mob died in lava
469 if cause
== "lava" then
470 item_drop(self
, true)
475 mob_sound(self
, self
.sounds
.death
)
477 local pos
= self
.object
:get_pos()
479 -- execute custom death function
482 self
.on_die(self
, pos
)
485 cmi
.notify_die(self
.object
, cmi_cause
)
493 -- default death function and die animation (if defined)
495 and self
.animation
.die_start
496 and self
.animation
.die_end
then
498 local frames
= self
.animation
.die_end
- self
.animation
.die_start
499 local speed
= self
.animation
.die_speed
or 15
500 local length
= max(frames
/ speed
, 0)
508 set_velocity(self
, 0)
509 set_animation(self
, "die")
511 minetest
.after(length
, function(self
)
514 cmi
.notify_die(self
.object
, cmi_cause
)
522 cmi
.notify_die(self
.object
, cmi_cause
)
528 effect(pos
, 20, "tnt_smoke.png")
534 -- check if within physical map limits (-30911 to 30927)
535 local within_limits
= function(pos
, radius
)
537 if (pos
.x
- radius
) > -30913
538 and (pos
.x
+ radius
) < 30928
539 and (pos
.y
- radius
) > -30913
540 and (pos
.y
+ radius
) < 30928
541 and (pos
.z
- radius
) > -30913
542 and (pos
.z
+ radius
) < 30928 then
543 return true -- within limits
546 return false -- beyond limits
550 -- is mob facing a cliff
551 local is_at_cliff
= function(self
)
553 if self
.fear_height
== 0 then -- 0 for no falling protection!
557 local yaw
= self
.object
:get_yaw()
558 local dir_x
= -sin(yaw
) * (self
.collisionbox
[4] + 0.5)
559 local dir_z
= cos(yaw
) * (self
.collisionbox
[4] + 0.5)
560 local pos
= self
.object
:get_pos()
561 local ypos
= pos
.y
+ self
.collisionbox
[2] -- just above floor
563 if minetest
.line_of_sight(
564 {x
= pos
.x
+ dir_x
, y
= ypos
, z
= pos
.z
+ dir_z
},
565 {x
= pos
.x
+ dir_x
, y
= ypos
- self
.fear_height
, z
= pos
.z
+ dir_z
}
575 -- get node but use fallback for nil or unknown
576 local node_ok
= function(pos
, fallback
)
578 fallback
= fallback
or mobs
.fallback_node
580 local node
= minetest
.get_node_or_nil(pos
)
582 if node
and minetest
.registered_nodes
[node
.name
] then
586 return minetest
.registered_nodes
[fallback
]
590 -- environmental damage (water, lava, fire, light etc.)
591 local do_env_damage
= function(self
)
593 -- feed/tame text timer (so mob 'full' messages dont spam chat)
594 if self
.htimer
> 0 then
595 self
.htimer
= self
.htimer
- 1
598 -- reset nametag after showing health stats
599 if self
.htimer
< 1 and self
.nametag2
then
601 self
.nametag
= self
.nametag2
607 local pos
= self
.object
:get_pos()
609 self
.time_of_day
= minetest
.get_timeofday()
611 -- remove mob if beyond map limits
612 if not within_limits(pos
, 0) then
617 -- bright light harms mob
618 if self
.light_damage
~= 0
620 -- and self.time_of_day > 0.2
621 -- and self.time_of_day < 0.8
622 and (minetest
.get_node_light(pos
) or 0) > 12 then
624 self
.health
= self
.health
- self
.light_damage
626 effect(pos
, 5, "tnt_smoke.png")
628 if check_for_death(self
, "light", {type = "light"}) then return end
631 local y_level
= self
.collisionbox
[2]
634 y_level
= self
.collisionbox
[2] * 0.5
637 -- what is mob standing in?
638 pos
.y
= pos
.y
+ y_level
+ 0.25 -- foot level
639 self
.standing_in
= node_ok(pos
, "air").name
640 -- print ("standing in " .. self.standing_in)
642 -- don't fall when on ignore, just stand still
643 if self
.standing_in
== "ignore" then
644 self
.object
:setvelocity({x
= 0, y
= 0, z
= 0})
647 local nodef
= minetest
.registered_nodes
[self
.standing_in
]
649 pos
.y
= pos
.y
+ 1 -- for particle effect position
653 and nodef
.groups
.water
then
655 if self
.water_damage
~= 0 then
657 self
.health
= self
.health
- self
.water_damage
659 effect(pos
, 5, "bubble.png", nil, nil, 1, nil)
661 if check_for_death(self
, "water", {type = "environment",
662 pos
= pos
, node
= self
.standing_in
}) then return end
666 elseif self
.lava_damage
667 and (nodef
.groups
.lava
668 or self
.standing_in
== node_fire
669 or self
.standing_in
== node_permanent_flame
) then
671 if self
.lava_damage
~= 0 then
673 self
.health
= self
.health
- self
.lava_damage
675 effect(pos
, 5, "fire_basic_flame.png", nil, nil, 1, nil)
677 if check_for_death(self
, "lava", {type = "environment",
678 pos
= pos
, node
= self
.standing_in
}) then return end
681 -- damage_per_second node check
682 elseif nodef
.damage_per_second
~= 0 then
684 self
.health
= self
.health
- nodef
.damage_per_second
686 effect(pos
, 5, "tnt_smoke.png")
688 if check_for_death(self
, "dps", {type = "environment",
689 pos
= pos
, node
= self
.standing_in
}) then return end
692 --- suffocation inside solid node
693 if self.suffocation ~= 0
694 and nodef.walkable == true
695 and nodef.groups.disable_suffocation ~= 1
696 and nodef.drawtype == "normal" then
698 self.health = self.health - self.suffocation
700 if check_for_death(self, "suffocation", {type = "environment",
701 pos = pos, node = self.standing_in}) then return end
704 check_for_death(self
, "", {type = "unknown"})
708 -- jump if facing a solid node (not fences or gates)
709 local do_jump
= function(self
)
712 or self
.jump_height
== 0
715 or self
.order
== "stand" then
719 self
.facing_fence
= false
721 -- something stopping us while moving?
722 if self
.state
~= "stand"
723 and get_velocity(self
) > 0.5
724 and self
.object
:getvelocity().y
~= 0 then
728 local pos
= self
.object
:get_pos()
729 local yaw
= self
.object
:get_yaw()
731 -- what is mob standing on?
732 pos
.y
= pos
.y
+ self
.collisionbox
[2] - 0.2
734 local nod
= node_ok(pos
)
736 --print ("standing on:", nod.name, pos.y)
738 if minetest
.registered_nodes
[nod
.name
].walkable
== false then
743 local dir_x
= -sin(yaw
) * (self
.collisionbox
[4] + 0.5)
744 local dir_z
= cos(yaw
) * (self
.collisionbox
[4] + 0.5)
746 -- what is in front of mob?
747 local nod
= node_ok({
753 -- thin blocks that do not need to be jumped
754 if nod
.name
== node_snow
then
758 --print ("in front:", nod.name, pos.y + 0.5)
760 if self
.walk_chance
== 0
761 or minetest
.registered_items
[nod
.name
].walkable
then
763 if not nod
.name
:find("fence")
764 and not nod
.name
:find("gate") then
766 local v
= self
.object
:getvelocity()
768 v
.y
= self
.jump_height
770 set_animation(self
, "jump") -- only when defined
772 self
.object
:setvelocity(v
)
774 -- when in air move forward
775 minetest
.after(0.3, function(self
, v
)
776 -- self.object:setvelocity({
777 self
.object
:set_acceleration({
784 if get_velocity(self
) > 0 then
785 mob_sound(self
, self
.sounds
.jump
)
788 self
.facing_fence
= true
798 -- blast damage to entities nearby (modified from TNT mod)
799 local entity_physics
= function(pos
, radius
)
803 local objs
= minetest
.get_objects_inside_radius(pos
, radius
)
808 obj_pos
= objs
[n
]:get_pos()
810 dist
= get_distance(pos
, obj_pos
)
811 if dist
< 1 then dist
= 1 end
813 local damage
= floor((4 / dist
) * radius
)
814 local ent
= objs
[n
]:get_luaentity()
816 -- punches work on entities AND players
817 objs
[n
]:punch(objs
[n
], 1.0, {
818 full_punch_interval
= 1.0,
819 damage_groups
= {fleshy
= damage
},
825 -- should mob follow what I'm holding ?
826 local follow_holding
= function(self
, clicker
)
828 if mobs
.invis
[clicker
:get_player_name()] then
832 local item
= clicker
:get_wielded_item()
833 local t
= type(self
.follow
)
837 and item
:get_name() == self
.follow
then
841 elseif t
== "table" then
843 for no
= 1, #self
.follow
do
845 if self
.follow
[no
] == item
:get_name() then
855 -- find two animals of same type and breed if nearby and horny
856 local breed
= function(self
)
858 -- child takes 240 seconds before growing into adult
859 if self
.child
== true then
861 self
.hornytimer
= self
.hornytimer
+ 1
863 if self
.hornytimer
> 240 then
868 self
.object
:set_properties({
869 textures
= self
.base_texture
,
870 mesh
= self
.base_mesh
,
871 visual_size
= self
.base_size
,
872 collisionbox
= self
.base_colbox
,
873 selectionbox
= self
.base_selbox
,
876 -- custom function when child grows up
877 if self
.on_grown
then
880 -- jump when fully grown so as not to fall into ground
881 self
.object
:setvelocity({
883 y
= self
.jump_height
,
892 -- horny animal can mate for 40 seconds,
893 -- afterwards horny animal cannot mate again for 200 seconds
894 if self
.horny
== true
895 and self
.hornytimer
< 240 then
897 self
.hornytimer
= self
.hornytimer
+ 1
899 if self
.hornytimer
>= 240 then
905 -- find another same animal who is also horny and mate if nearby
906 if self
.horny
== true
907 and self
.hornytimer
<= 40 then
909 local pos
= self
.object
:get_pos()
911 effect({x
= pos
.x
, y
= pos
.y
+ 1, z
= pos
.z
}, 8, "heart.png", 3, 4, 1, 0.1)
913 local objs
= minetest
.get_objects_inside_radius(pos
, 3)
919 ent
= objs
[n
]:get_luaentity()
921 -- check for same animal with different colour
922 local canmate
= false
926 if ent
.name
== self
.name
then
929 local entname
= string.split(ent
.name
,":")
930 local selfname
= string.split(self
.name
,":")
932 if entname
[1] == selfname
[1] then
933 entname
= string.split(entname
[2],"_")
934 selfname
= string.split(selfname
[2],"_")
936 if entname
[1] == selfname
[1] then
945 and ent
.horny
== true
946 and ent
.hornytimer
<= 40 then
950 -- found your mate? then have a baby
957 minetest
.after(5, function()
959 -- custom breed function
960 if self
.on_breed
then
962 -- when false skip going any further
963 if self
.on_breed(self
, ent
) == false then
967 effect(pos
, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
970 local mob
= minetest
.add_entity(pos
, self
.name
)
971 local ent2
= mob
:get_luaentity()
972 local textures
= self
.base_texture
974 -- using specific child texture (if found)
975 if self
.child_texture
then
976 textures
= self
.child_texture
[1]
979 -- and resize to half height
983 x
= self
.base_size
.x
* .5,
984 y
= self
.base_size
.y
* .5,
987 self
.base_colbox
[1] * .5,
988 self
.base_colbox
[2] * .5,
989 self
.base_colbox
[3] * .5,
990 self
.base_colbox
[4] * .5,
991 self
.base_colbox
[5] * .5,
992 self
.base_colbox
[6] * .5,
995 self
.base_selbox
[1] * .5,
996 self
.base_selbox
[2] * .5,
997 self
.base_selbox
[3] * .5,
998 self
.base_selbox
[4] * .5,
999 self
.base_selbox
[5] * .5,
1000 self
.base_selbox
[6] * .5,
1003 -- tamed and owned by parents' owner
1006 ent2
.owner
= self
.owner
1018 -- find and replace what mob is looking for (grass, wheat etc.)
1019 local replace
= function(self
, pos
)
1021 if not mobs_griefing
1022 or not self
.replace_rate
1023 or not self
.replace_what
1024 or self
.child
== true
1025 or self
.object
:getvelocity().y
~= 0
1026 or random(1, self
.replace_rate
) > 1 then
1030 local what
, with
, y_offset
1032 if type(self
.replace_what
[1]) == "table" then
1034 local num
= random(#self
.replace_what
)
1036 what
= self
.replace_what
[num
][1] or ""
1037 with
= self
.replace_what
[num
][2] or ""
1038 y_offset
= self
.replace_what
[num
][3] or 0
1040 what
= self
.replace_what
1041 with
= self
.replace_with
or ""
1042 y_offset
= self
.replace_offset
or 0
1045 pos
.y
= pos
.y
+ y_offset
1047 if #minetest
.find_nodes_in_area(pos
, pos
, what
) > 0 then
1049 -- print ("replace node = ".. minetest.get_node(pos).name, pos.y)
1051 local oldnode
= {name
= what
}
1052 local newnode
= {name
= with
}
1053 local on_replace_return
1055 if self
.on_replace
then
1056 on_replace_return
= self
.on_replace(self
, pos
, oldnode
, newnode
)
1059 if on_replace_return
~= false then
1061 minetest
.set_node(pos
, {name
= with
})
1063 -- when cow/sheep eats grass, replace wool and milk
1064 if self
.gotten
== true then
1066 self
.object
:set_properties(self
)
1073 -- check if daytime and also if mob is docile during daylight hours
1074 local day_docile
= function(self
)
1076 if self
.docile_by_day
== false then
1080 elseif self
.docile_by_day
== true
1081 and self
.time_of_day
> 0.2
1082 and self
.time_of_day
< 0.8 then
1089 local los_switcher
= false
1090 local height_switcher
= false
1092 -- path finding and smart mob routine by rnd, line_of_sight and other edits by Elkien3
1093 local smart_mobs
= function(self
, s
, p
, dist
, dtime
)
1095 local s1
= self
.path
.lastpos
1097 local target_pos
= self
.attack
:get_pos()
1099 -- is it becoming stuck?
1100 if abs(s1
.x
- s
.x
) + abs(s1
.z
- s
.z
) < .5 then
1101 self
.path
.stuck_timer
= self
.path
.stuck_timer
+ dtime
1103 self
.path
.stuck_timer
= 0
1106 self
.path
.lastpos
= {x
= s
.x
, y
= s
.y
, z
= s
.z
}
1108 local use_pathfind
= false
1109 local has_lineofsight
= minetest
.line_of_sight(
1110 {x
= s
.x
, y
= (s
.y
) + .5, z
= s
.z
},
1111 {x
= target_pos
.x
, y
= (target_pos
.y
) + 1.5, z
= target_pos
.z
}, .2)
1113 -- im stuck, search for path
1114 if not has_lineofsight
then
1116 if los_switcher
== true then
1118 los_switcher
= false
1119 end -- cannot see target!
1121 if los_switcher
== false then
1124 use_pathfind
= false
1126 minetest
.after(1, function(self
)
1127 if has_lineofsight
then self
.path
.following
= false end
1129 end -- can see target!
1132 if (self
.path
.stuck_timer
> stuck_timeout
and not self
.path
.following
) then
1135 self
.path
.stuck_timer
= 0
1137 minetest
.after(1, function(self
)
1138 if has_lineofsight
then self
.path
.following
= false end
1142 if (self
.path
.stuck_timer
> stuck_path_timeout
and self
.path
.following
) then
1145 self
.path
.stuck_timer
= 0
1147 minetest
.after(1, function(self
)
1148 if has_lineofsight
then self
.path
.following
= false end
1152 if math
.abs(vector
.subtract(s
,target_pos
).y
) > self
.stepheight
then
1154 if height_switcher
then
1156 height_switcher
= false
1159 if not height_switcher
then
1160 use_pathfind
= false
1161 height_switcher
= true
1165 if use_pathfind
then
1166 -- lets try find a path, first take care of positions
1167 -- since pathfinder is very sensitive
1168 local sheight
= self
.collisionbox
[5] - self
.collisionbox
[2]
1170 -- round position to center of node to avoid stuck in walls
1171 -- also adjust height for player models!
1172 s
.x
= floor(s
.x
+ 0.5)
1173 -- s.y = floor(s.y + 0.5) - sheight
1174 s
.z
= floor(s
.z
+ 0.5)
1176 local ssight
, sground
= minetest
.line_of_sight(s
, {
1177 x
= s
.x
, y
= s
.y
- 4, z
= s
.z
}, 1)
1179 -- determine node above ground
1184 local p1
= self
.attack
:get_pos()
1186 p1
.x
= floor(p1
.x
+ 0.5)
1187 p1
.y
= floor(p1
.y
+ 0.5)
1188 p1
.z
= floor(p1
.z
+ 0.5)
1190 local dropheight
= 6
1191 if self
.fear_height
~= 0 then dropheight
= self
.fear_height
end
1193 self
.path
.way
= minetest
.find_path(s
, p1
, 16, self
.stepheight
, dropheight
, "A*_noprefetch")
1195 -- show path using particles
1196 if self.path.way and #self.path.way > 0 then
1197 print ("-- path length:" .. tonumber(#self.path.way))
1198 for _,pos in pairs(self.path.way) do
1199 minetest.add_particle({
1201 velocity = {x=0, y=0, z=0},
1202 acceleration = {x=0, y=0, z=0},
1205 collisiondetection = false,
1207 texture = "heart.png",
1214 do_attack(self
, self
.attack
)
1216 -- no path found, try something else
1217 if not self
.path
.way
then
1219 self
.path
.following
= false
1221 -- lets make way by digging/building if not accessible
1222 if self
.pathfinding
== 2 and mobs_griefing
then
1224 -- is player higher than mob?
1228 if not minetest
.is_protected(s
, "") then
1230 local ndef1
= minetest
.registered_nodes
[self
.standing_in
]
1232 if ndef1
and (ndef1
.buildable_to
or ndef1
.groups
.liquid
) then
1234 minetest
.set_node(s
, {name
= mobs
.fallback_node
})
1238 local sheight
= math
.ceil(self
.collisionbox
[5]) + 1
1240 -- assume mob is 2 blocks high so it digs above its head
1243 -- remove one block above to make room to jump
1244 if not minetest
.is_protected(s
, "") then
1246 local node1
= node_ok(s
, "air").name
1247 local ndef1
= minetest
.registered_nodes
[node1
]
1250 and node1
~= "ignore"
1252 and not ndef1
.groups
.level
1253 and not ndef1
.groups
.unbreakable
1254 and not ndef1
.groups
.liquid
then
1256 minetest
.set_node(s
, {name
= "air"})
1257 minetest
.add_item(s
, ItemStack(node1
))
1263 self
.object
:setpos({x
= s
.x
, y
= s
.y
+ 2, z
= s
.z
})
1265 else -- dig 2 blocks to make door toward player direction
1267 local yaw1
= self
.object
:get_yaw() + pi
/ 2
1269 x
= s
.x
+ cos(yaw1
),
1274 if not minetest
.is_protected(p1
, "") then
1276 local node1
= node_ok(p1
, "air").name
1277 local ndef1
= minetest
.registered_nodes
[node1
]
1280 and node1
~= "ignore"
1282 and not ndef1
.groups
.level
1283 and not ndef1
.groups
.unbreakable
1284 and not ndef1
.groups
.liquid
then
1286 minetest
.add_item(p1
, ItemStack(node1
))
1287 minetest
.set_node(p1
, {name
= "air"})
1291 node1
= node_ok(p1
, "air").name
1292 ndef1
= minetest
.registered_nodes
[node1
]
1295 and node1
~= "ignore"
1297 and not ndef1
.groups
.level
1298 and not ndef1
.groups
.unbreakable
1299 and not ndef1
.groups
.liquid
then
1301 minetest
.add_item(p1
, ItemStack(node1
))
1302 minetest
.set_node(p1
, {name
= "air"})
1309 -- will try again in 2 second
1310 self
.path
.stuck_timer
= stuck_timeout
- 2
1312 -- frustration! cant find the damn path :(
1313 mob_sound(self
, self
.sounds
.random)
1316 mob_sound(self
, self
.sounds
.war_cry
)
1317 set_velocity(self
, self
.walk_velocity
)
1319 -- follow path now that it has it
1320 self
.path
.following
= true
1327 local specific_attack
= function(list
, what
)
1329 -- no list so attack default (player, animals etc.)
1334 -- found entity on list to attack?
1335 for no
= 1, #list
do
1337 if list
[no
] == what
then
1346 -- monster find someone to attack
1347 local monster_attack
= function(self
)
1349 if self
.type ~= "monster"
1350 or not damage_enabled
1352 or self
.state
== "attack"
1353 or day_docile(self
) then
1357 local s
= self
.object
:get_pos()
1359 local player
, obj
, min_player
1360 local type, name
= "", ""
1361 local min_dist
= self
.view_range
+ 1
1362 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1366 if objs
[n
]:is_player() then
1368 if mobs
.invis
[ objs
[n
]:get_player_name() ] then
1377 obj
= objs
[n
]:get_luaentity()
1382 name
= obj
.name
or ""
1386 -- find specific mob to attack, failing that attack player/npc/animal
1387 if specific_attack(self
.specific_attack
, name
)
1388 and (type == "player" or type == "npc"
1389 or (type == "animal" and self
.attack_animals
== true)) then
1391 p
= player
:get_pos()
1394 dist
= get_distance(p
, s
)
1396 -- aim higher to make looking up hills more realistic
1401 -- choose closest player to attack
1403 and line_of_sight(self
, sp
, p
, 2) == true then
1412 do_attack(self
, min_player
)
1417 -- npc, find closest monster to attack
1418 local npc_attack
= function(self
)
1420 if self
.type ~= "npc"
1421 or not self
.attacks_monsters
1422 or self
.state
== "attack" then
1426 local p
, sp
, obj
, min_player
1427 local s
= self
.object
:get_pos()
1428 local min_dist
= self
.view_range
+ 1
1429 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1433 obj
= objs
[n
]:get_luaentity()
1435 if obj
and obj
.type == "monster" then
1437 p
= obj
.object
:get_pos()
1440 dist
= get_distance(p
, s
)
1442 -- aim higher to make looking up hills more realistic
1447 and line_of_sight(self
, sp
, p
, 2) == true then
1449 min_player
= obj
.object
1455 do_attack(self
, min_player
)
1461 local specific_runaway
= function(list
, what
)
1463 -- no list so do not run
1468 -- found entity on list to attack?
1469 for no
= 1, #list
do
1471 if list
[no
] == what
then
1480 -- find someone to runaway from
1481 local runaway_from
= function(self
)
1483 if not self
.runaway_from
then
1487 local s
= self
.object
:get_pos()
1489 local player
, obj
, min_player
1490 local type, name
= "", ""
1491 local min_dist
= self
.view_range
+ 1
1492 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1496 if objs
[n
]:is_player() then
1498 if mobs
.invis
[ objs
[n
]:get_player_name() ]
1499 or self
.owner
== objs
[n
]:get_player_name() then
1508 obj
= objs
[n
]:get_luaentity()
1513 name
= obj
.name
or ""
1517 -- find specific mob to runaway from
1518 if name
~= "" and name
~= self
.name
1519 and specific_runaway(self
.runaway_from
, name
) then
1521 p
= player
:get_pos()
1524 -- aim higher to make looking up hills more realistic
1528 dist
= get_distance(p
, s
)
1531 -- choose closest player/mpb to runaway from
1533 and line_of_sight(self
, sp
, p
, 2) == true then
1542 local lp
= player
:get_pos()
1549 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
1555 yaw
= set_yaw(self
, yaw
, 4)
1556 self
.state
= "runaway"
1557 self
.runaway_timer
= 3
1558 self
.following
= nil
1563 -- follow player if owner or holding item, if fish outta water then flop
1564 local follow_flop
= function(self
)
1566 -- find player to follow
1567 if (self
.follow
~= ""
1568 or self
.order
== "follow")
1569 and not self
.following
1570 and self
.state
~= "attack"
1571 and self
.state
~= "runaway" then
1573 local s
= self
.object
:get_pos()
1574 local players
= minetest
.get_connected_players()
1576 for n
= 1, #players
do
1578 if get_distance(players
[n
]:get_pos(), s
) < self
.view_range
1579 and not mobs
.invis
[ players
[n
]:get_player_name() ] then
1581 self
.following
= players
[n
]
1588 if self
.type == "npc"
1589 and self
.order
== "follow"
1590 and self
.state
~= "attack"
1591 and self
.owner
~= "" then
1593 -- npc stop following player if not owner
1596 and self
.owner
~= self
.following
:get_player_name() then
1597 self
.following
= nil
1600 -- stop following player if not holding specific item
1602 and self
.following
:is_player()
1603 and follow_holding(self
, self
.following
) == false then
1604 self
.following
= nil
1609 -- follow that thing
1610 if self
.following
then
1612 local s
= self
.object
:get_pos()
1615 if self
.following
:is_player() then
1617 p
= self
.following
:get_pos()
1619 elseif self
.following
.object
then
1621 p
= self
.following
.object
:get_pos()
1626 local dist
= get_distance(p
, s
)
1628 -- dont follow if out of range
1629 if dist
> self
.view_range
then
1630 self
.following
= nil
1637 local yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1639 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
1641 yaw
= set_yaw(self
, yaw
, 6)
1643 -- anyone but standing npc's can move along
1644 if dist
> self
.reach
1645 and self
.order
~= "stand" then
1647 set_velocity(self
, self
.walk_velocity
)
1649 if self
.walk_chance
~= 0 then
1650 set_animation(self
, "walk")
1653 set_velocity(self
, 0)
1654 set_animation(self
, "stand")
1662 -- swimmers flop when out of their element, and swim again when back in
1664 local s
= self
.object
:get_pos()
1665 if not flight_check(self
, s
) then
1668 self
.object
:setvelocity({x
= 0, y
= -5, z
= 0})
1670 set_animation(self
, "stand")
1673 elseif self
.state
== "flop" then
1674 self
.state
= "stand"
1680 -- dogshoot attack switch and counter function
1681 local dogswitch
= function(self
, dtime
)
1683 -- switch mode not activated
1684 if not self
.dogshoot_switch
1689 self
.dogshoot_count
= self
.dogshoot_count
+ dtime
1691 if (self
.dogshoot_switch
== 1
1692 and self
.dogshoot_count
> self
.dogshoot_count_max
)
1693 or (self
.dogshoot_switch
== 2
1694 and self
.dogshoot_count
> self
.dogshoot_count2_max
) then
1696 self
.dogshoot_count
= 0
1698 if self
.dogshoot_switch
== 1 then
1699 self
.dogshoot_switch
= 2
1701 self
.dogshoot_switch
= 1
1705 return self
.dogshoot_switch
1709 -- execute current state (stand, walk, run, attacks)
1710 local do_states
= function(self
, dtime
)
1712 local yaw
= self
.object
:get_yaw() or 0
1714 if self
.state
== "stand" then
1716 if random(1, 4) == 1 then
1719 local s
= self
.object
:get_pos()
1720 local objs
= minetest
.get_objects_inside_radius(s
, 3)
1724 if objs
[n
]:is_player() then
1725 lp
= objs
[n
]:get_pos()
1730 -- look at any players nearby, otherwise turn randomly
1738 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1740 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1742 yaw
= yaw
+ random(-0.5, 0.5)
1745 yaw
= set_yaw(self
, yaw
, 8)
1748 set_velocity(self
, 0)
1749 set_animation(self
, "stand")
1751 -- npc's ordered to stand stay standing
1752 if self
.type ~= "npc"
1753 or self
.order
~= "stand" then
1755 if self
.walk_chance
~= 0
1756 and self
.facing_fence
~= true
1757 and random(1, 100) <= self
.walk_chance
1758 and is_at_cliff(self
) == false then
1760 set_velocity(self
, self
.walk_velocity
)
1762 set_animation(self
, "walk")
1764 --[[ fly up/down randomly for flying mobs
1765 if self.fly and random(1, 100) <= self.walk_chance then
1767 local v = self.object:getvelocity()
1768 local ud = random(-1, 2) / 9
1770 self.object:setvelocity({x = v.x, y = ud, z = v.z})
1775 elseif self
.state
== "walk" then
1777 local s
= self
.object
:get_pos()
1780 -- is there something I need to avoid?
1781 if self
.water_damage
> 0
1782 and self
.lava_damage
> 0 then
1784 lp
= minetest
.find_node_near(s
, 1, {"group:water", "group:lava"})
1786 elseif self
.water_damage
> 0 then
1788 lp
= minetest
.find_node_near(s
, 1, {"group:water"})
1790 elseif self
.lava_damage
> 0 then
1792 lp
= minetest
.find_node_near(s
, 1, {"group:lava"})
1797 -- if mob in water or lava then look for land
1798 if (self
.lava_damage
1799 and minetest
.registered_nodes
[self
.standing_in
].groups
.lava
)
1800 or (self
.water_damage
1801 and minetest
.registered_nodes
[self
.standing_in
].groups
.water
) then
1803 lp
= minetest
.find_node_near(s
, 5, {"group:soil", "group:stone",
1804 "group:sand", node_ice
, node_snowblock
})
1806 -- did we find land?
1814 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1816 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1818 -- look towards land and jump/move in that direction
1819 yaw
= set_yaw(self
, yaw
, 6)
1821 set_velocity(self
, self
.walk_velocity
)
1823 yaw
= yaw
+ random(-0.5, 0.5)
1833 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1835 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1838 yaw
= set_yaw(self
, yaw
, 8)
1840 -- otherwise randomly turn
1841 elseif random(1, 100) <= 30 then
1843 yaw
= yaw
+ random(-0.5, 0.5)
1845 yaw
= set_yaw(self
, yaw
, 8)
1848 -- stand for great fall in front
1849 local temp_is_cliff
= is_at_cliff(self
)
1851 if self
.facing_fence
== true
1853 or random(1, 100) <= 30 then
1855 set_velocity(self
, 0)
1856 self
.state
= "stand"
1857 set_animation(self
, "stand")
1859 set_velocity(self
, self
.walk_velocity
)
1861 if flight_check(self
)
1863 and self
.animation
.fly_start
1864 and self
.animation
.fly_end
then
1865 set_animation(self
, "fly")
1867 set_animation(self
, "walk")
1871 -- runaway when punched
1872 elseif self
.state
== "runaway" then
1874 self
.runaway_timer
= self
.runaway_timer
+ 1
1876 -- stop after 5 seconds or when at cliff
1877 if self
.runaway_timer
> 5
1878 or is_at_cliff(self
) then
1879 self
.runaway_timer
= 0
1880 set_velocity(self
, 0)
1881 self
.state
= "stand"
1882 set_animation(self
, "stand")
1884 set_velocity(self
, self
.run_velocity
)
1885 set_animation(self
, "walk")
1888 -- attack routines (explode, dogfight, shoot, dogshoot)
1889 elseif self
.state
== "attack" then
1891 -- calculate distance from mob and enemy
1892 local s
= self
.object
:get_pos()
1893 local p
= self
.attack
:get_pos() or s
1894 local dist
= get_distance(p
, s
)
1896 -- stop attacking if player invisible or out of range
1897 if dist
> self
.view_range
1899 or not self
.attack
:get_pos()
1900 or self
.attack
:get_hp() <= 0
1901 or (self
.attack
:is_player() and mobs
.invis
[ self
.attack
:get_player_name() ]) then
1903 -- print(" ** stop attacking **", dist, self.view_range)
1904 self
.state
= "stand"
1905 set_velocity(self
, 0)
1906 set_animation(self
, "stand")
1908 self
.v_start
= false
1916 if self
.attack_type
== "explode" then
1923 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1925 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
1927 yaw
= set_yaw(self
, yaw
)
1929 local node_break_radius
= self
.explosion_radius
or 1
1930 local entity_damage_radius
= self
.explosion_damage_radius
1931 or (node_break_radius
* 2)
1933 -- start timer when in reach and line of sight
1935 and dist
<= self
.reach
1936 and line_of_sight(self
, s
, p
, 2) then
1941 mob_sound(self
, self
.sounds
.fuse
)
1942 -- print ("=== explosion timer started", self.explosion_timer)
1944 -- stop timer if out of reach or direct line of sight
1945 elseif self
.allow_fuse_reset
1947 and (dist
> self
.reach
1948 or not line_of_sight(self
, s
, p
, 2)) then
1949 self
.v_start
= false
1952 self
.blinkstatus
= false
1953 self
.object
:settexturemod("")
1956 -- walk right up to player unless the timer is active
1957 if self
.v_start
and (self
.stop_to_explode
or dist
< 1.5) then
1958 set_velocity(self
, 0)
1960 set_velocity(self
, self
.run_velocity
)
1963 if self
.animation
and self
.animation
.run_start
then
1964 set_animation(self
, "run")
1966 set_animation(self
, "walk")
1969 if self
.v_start
then
1971 self
.timer
= self
.timer
+ dtime
1972 self
.blinktimer
= (self
.blinktimer
or 0) + dtime
1974 if self
.blinktimer
> 0.2 then
1978 if self
.blinkstatus
then
1979 self
.object
:settexturemod("")
1981 self
.object
:settexturemod("^[brighten")
1984 self
.blinkstatus
= not self
.blinkstatus
1987 -- print ("=== explosion timer", self.timer)
1989 if self
.timer
> self
.explosion_timer
then
1991 local pos
= self
.object
:get_pos()
1993 -- dont damage anything if area protected or next to water
1994 if minetest
.find_node_near(pos
, 1, {"group:water"})
1995 or minetest
.is_protected(pos
, "") then
1997 node_break_radius
= 1
2000 self
.object
:remove()
2002 if minetest
.get_modpath("mcl_tnt") and tnt
and tnt
.boom
2003 and not minetest
.is_protected(pos
, "") then
2006 radius
= node_break_radius
,
2007 damage_radius
= entity_damage_radius
,
2008 sound
= self
.sounds
.explode
,
2012 minetest
.sound_play(self
.sounds
.explode
, {
2015 max_hear_distance
= self
.sounds
.distance
or 32
2018 entity_physics(pos
, entity_damage_radius
)
2019 effect(pos
, 32, "tnt_smoke.png", nil, nil, node_break_radius
, 1, 0)
2026 elseif self
.attack_type
== "dogfight"
2027 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 2)
2028 or (self
.attack_type
== "dogshoot" and dist
<= self
.reach
and dogswitch(self
) == 0) then
2031 and dist
> self
.reach
then
2034 local me_y
= floor(p1
.y
)
2036 local p_y
= floor(p2
.y
+ 1)
2037 local v
= self
.object
:getvelocity()
2039 if flight_check(self
, s
) then
2043 self
.object
:setvelocity({
2045 y
= 1 * self
.walk_velocity
,
2049 elseif me_y
> p_y
then
2051 self
.object
:setvelocity({
2053 y
= -1 * self
.walk_velocity
,
2060 self
.object
:setvelocity({
2066 elseif me_y
> p_y
then
2068 self
.object
:setvelocity({
2078 -- rnd: new movement direction
2079 if self
.path
.following
2081 and self
.attack_type
~= "dogshoot" then
2083 -- no paths longer than 50
2084 if #self
.path
.way
> 50
2085 or dist
< self
.reach
then
2086 self
.path
.following
= false
2090 local p1
= self
.path
.way
[1]
2093 self
.path
.following
= false
2097 if abs(p1
.x
-s
.x
) + abs(p1
.z
- s
.z
) < 0.6 then
2098 -- reached waypoint, remove it from queue
2099 table.remove(self
.path
.way
, 1)
2102 -- set new temporary target
2103 p
= {x
= p1
.x
, y
= p1
.y
, z
= p1
.z
}
2111 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2113 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2115 yaw
= set_yaw(self
, yaw
)
2117 -- move towards enemy if beyond mob reach
2118 if dist
> self
.reach
then
2120 -- path finding by rnd
2121 if self
.pathfinding
-- only if mob has pathfinding enabled
2122 and enable_pathfinding
then
2124 smart_mobs(self
, s
, p
, dist
, dtime
)
2127 if is_at_cliff(self
) then
2129 set_velocity(self
, 0)
2130 set_animation(self
, "stand")
2133 if self
.path
.stuck
then
2134 set_velocity(self
, self
.walk_velocity
)
2136 set_velocity(self
, self
.run_velocity
)
2139 if self
.animation
and self
.animation
.run_start
then
2140 set_animation(self
, "run")
2142 set_animation(self
, "walk")
2146 else -- rnd: if inside reach range
2148 self
.path
.stuck
= false
2149 self
.path
.stuck_timer
= 0
2150 self
.path
.following
= false -- not stuck anymore
2152 set_velocity(self
, 0)
2154 if not self
.custom_attack
then
2156 if self
.timer
> 1 then
2160 if self
.double_melee_attack
2161 and random(1, 2) == 1 then
2162 set_animation(self
, "punch2")
2164 set_animation(self
, "punch")
2173 if line_of_sight(self
, p2
, s2
) == true then
2175 -- play attack sound
2176 mob_sound(self
, self
.sounds
.attack
)
2178 -- punch player (or what player is attached to)
2179 local attached
= self
.attack
:get_attach()
2181 self
.attack
= attached
2183 self
.attack
:punch(self
.object
, 1.0, {
2184 full_punch_interval
= 1.0,
2185 damage_groups
= {fleshy
= self
.damage
}
2189 else -- call custom attack every second
2190 if self
.custom_attack
2191 and self
.timer
> 1 then
2195 self
.custom_attack(self
, p
)
2200 elseif self
.attack_type
== "shoot"
2201 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 1)
2202 or (self
.attack_type
== "dogshoot" and dist
> self
.reach
and dogswitch(self
) == 0) then
2207 local dist
= get_distance(p
, s
)
2214 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2216 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2218 yaw
= set_yaw(self
, yaw
)
2220 set_velocity(self
, 0)
2222 if self
.shoot_interval
2223 and self
.timer
> self
.shoot_interval
2224 and random(1, 100) <= 60 then
2227 set_animation(self
, "shoot")
2229 -- play shoot attack sound
2230 mob_sound(self
, self
.sounds
.shoot_attack
)
2232 local p
= self
.object
:get_pos()
2234 p
.y
= p
.y
+ (self
.collisionbox
[2] + self
.collisionbox
[5]) / 2
2236 if minetest
.registered_entities
[self
.arrow
] then
2238 local obj
= minetest
.add_entity(p
, self
.arrow
)
2239 local ent
= obj
:get_luaentity()
2240 local amount
= (vec
.x
* vec
.x
+ vec
.y
* vec
.y
+ vec
.z
* vec
.z
) ^
0.5
2241 local v
= ent
.velocity
or 1 -- or set to default
2244 ent
.owner_id
= tostring(self
.object
) -- add unique owner id to arrow
2246 -- offset makes shoot aim accurate
2247 vec
.y
= vec
.y
+ self
.shoot_offset
2248 vec
.x
= vec
.x
* (v
/ amount
)
2249 vec
.y
= vec
.y
* (v
/ amount
)
2250 vec
.z
= vec
.z
* (v
/ amount
)
2252 obj
:setvelocity(vec
)
2260 -- falling and fall damage
2261 local falling
= function(self
, pos
)
2267 -- floating in water (or falling)
2268 local v
= self
.object
:getvelocity()
2272 -- apply gravity when moving up
2273 self
.object
:setacceleration({
2279 elseif v
.y
<= 0 and v
.y
> self
.fall_speed
then
2281 -- fall downwards at set speed
2282 self
.object
:setacceleration({
2284 y
= self
.fall_speed
,
2288 -- stop accelerating once max fall speed hit
2289 self
.object
:setacceleration({x
= 0, y
= 0, z
= 0})
2292 -- in water then float up
2293 if minetest
.registered_nodes
[node_ok(pos
).name
].groups
.water
then
2295 if self
.floats
== 1 then
2297 self
.object
:setacceleration({
2299 y
= -self
.fall_speed
/ (max(1, v
.y
) ^
2),
2305 -- fall damage onto solid ground
2306 if self
.fall_damage
== 1
2307 and self
.object
:getvelocity().y
== 0 then
2309 local d
= (self
.old_y
or 0) - self
.object
:get_pos().y
2313 self
.health
= self
.health
- floor(d
- 5)
2315 effect(pos
, 5, "tnt_smoke.png", 1, 2, 2, nil)
2317 if check_for_death(self
, "fall", {type = "fall"}) then
2322 self
.old_y
= self
.object
:get_pos().y
2328 -- deal damage and effects when mob punched
2329 local mob_punch
= function(self
, hitter
, tflp
, tool_capabilities
, dir
)
2331 -- custom punch function
2332 if self
.do_punch
then
2334 -- when false skip going any further
2335 if self
.do_punch(self
, hitter
, tflp
, tool_caps
, dir
) == false then
2341 -- if self.health <= 0 then
2345 -- error checking when mod profiling is enabled
2346 if not tool_capabilities
then
2347 minetest
.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2351 -- is mob protected?
2352 if self
.protected
and hitter
:is_player()
2353 and minetest
.is_protected(self
.object
:get_pos(), hitter
:get_player_name()) then
2354 minetest
.chat_send_player(hitter
:get_player_name(), S("Mob has been protected!"))
2360 local weapon
= hitter
:get_wielded_item()
2361 local punch_interval
= 1.4
2363 -- calculate mob damage
2365 local armor
= self
.object
:get_armor_groups() or {}
2368 -- quick error check incase it ends up 0 (serialize.h check test)
2374 damage
= cmi
.calculate_damage(self
.object
, hitter
, tflp
, tool_capabilities
, dir
)
2377 for group
,_
in pairs( (tool_capabilities
.damage_groups
or {}) ) do
2379 tmp
= tflp
/ (tool_capabilities
.full_punch_interval
or 1.4)
2387 damage
= damage
+ (tool_capabilities
.damage_groups
[group
] or 0)
2388 * tmp
* ((armor
[group
] or 0) / 100.0)
2392 -- check for tool immunity or special damage
2393 for n
= 1, #self
.immune_to
do
2395 if self
.immune_to
[n
][1] == weapon
:get_name() then
2397 damage
= self
.immune_to
[n
][2] or 0
2403 if damage
<= -1 then
2404 self
.health
= self
.health
- floor(damage
)
2408 -- print ("Mob Damage is", damage)
2412 local cancel
= cmi
.notify_punch(self
.object
, hitter
, tflp
, tool_capabilities
, dir
, damage
)
2414 if cancel
then return end
2418 if tool_capabilities
then
2419 punch_interval
= tool_capabilities
.full_punch_interval
or 1.4
2422 if weapon
:get_definition()
2423 and weapon
:get_definition().tool_capabilities
then
2425 weapon
:add_wear(floor((punch_interval
/ 75) * 9000))
2426 hitter
:set_wielded_item(weapon
)
2429 -- only play hit sound and show blood effects if damage is 1 or over
2433 if weapon
:get_definition().sounds
~= nil then
2435 local s
= random(0, #weapon
:get_definition().sounds
)
2437 minetest
.sound_play(weapon
:get_definition().sounds
[s
], {
2438 object
= self
.object
, --hitter,
2439 max_hear_distance
= 8
2442 minetest
.sound_play("default_punch", {
2443 object
= self
.object
, --hitter,
2444 max_hear_distance
= 5
2449 if self
.blood_amount
> 0
2450 and not disable_blood
then
2452 local pos
= self
.object
:get_pos()
2454 pos
.y
= pos
.y
+ (-self
.collisionbox
[2] + self
.collisionbox
[5]) * .5
2456 -- do we have a single blood texture or multiple?
2457 if type(self
.blood_texture
) == "table" then
2459 local blood
= self
.blood_texture
[random(1, #self
.blood_texture
)]
2461 effect(pos
, self
.blood_amount
, blood
, nil, nil, 1, nil)
2463 effect(pos
, self
.blood_amount
, self
.blood_texture
, nil, nil, 1, nil)
2468 self
.health
= self
.health
- floor(damage
)
2470 -- exit here if dead, special item check
2471 if weapon
:get_name() == "mobs:pick_lava" then
2472 if check_for_death(self
, "lava", {type = "punch",
2473 puncher
= hitter
}) then
2477 if check_for_death(self
, "hit", {type = "punch",
2478 puncher
= hitter
}) then
2483 --[[ add healthy afterglow when hit (can cause hit lag with larger textures)
2484 core.after(0.1, function()
2485 self.object:settexturemod("^[colorize:#c9900070")
2487 core.after(0.3, function()
2488 self.object:settexturemod("")
2492 -- knock back effect (only on full punch)
2494 and tflp
>= punch_interval
then
2496 local v
= self
.object
:getvelocity()
2497 local r
= 1.4 - min(punch_interval
, 1.4)
2501 -- if already in air then dont go up anymore when hit
2507 -- direction error check
2508 dir
= dir
or {x
= 0, y
= 0, z
= 0}
2510 -- check if tool already has specific knockback value
2511 if tool_capabilities
.damage_groups
["knockback"] then
2512 kb
= tool_capabilities
.damage_groups
["knockback"]
2517 self
.object
:setvelocity({
2523 self
.pause_timer
= 0.25
2525 end -- END if damage
2527 -- if skittish then run away
2528 if self
.runaway
== true then
2530 local lp
= hitter
:get_pos()
2531 local s
= self
.object
:get_pos()
2538 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
2544 yaw
= set_yaw(self
, yaw
, 6)
2545 self
.state
= "runaway"
2546 self
.runaway_timer
= 0
2547 self
.following
= nil
2550 local name
= hitter
:get_player_name() or ""
2552 -- attack puncher and call other mobs for help
2553 if self
.passive
== false
2554 and self
.state
~= "flop"
2555 and self
.child
== false
2556 and hitter
:get_player_name() ~= self
.owner
2557 and not mobs
.invis
[ name
] then
2559 -- attack whoever punched mob
2561 do_attack(self
, hitter
)
2563 -- alert others to the attack
2564 local objs
= minetest
.get_objects_inside_radius(hitter
:get_pos(), self
.view_range
)
2569 obj
= objs
[n
]:get_luaentity()
2573 -- only alert members of same mob
2574 if obj
.group_attack
== true
2575 and obj
.state
~= "attack"
2576 and obj
.owner
~= name
2577 and obj
.name
== self
.name
then
2578 do_attack(obj
, hitter
)
2581 -- have owned mobs attack player threat
2582 if obj
.owner
== name
and obj
.owner_loyal
then
2583 do_attack(obj
, self
.object
)
2591 -- get entity staticdata
2592 local mob_staticdata
= function(self
)
2594 -- remove mob when out of range unless tamed
2597 and self
.type ~= "npc"
2598 and self
.state
~= "attack"
2600 and self
.lifetimer
< 20000 then
2602 --print ("REMOVED " .. self.name)
2604 self
.object
:remove()
2609 self
.remove_ok
= true
2611 self
.following
= nil
2612 self
.state
= "stand"
2614 -- used to rotate older mobs
2616 and self
.drawtype
== "side" then
2617 self
.rotate
= math
.rad(90)
2621 self
.serialized_cmi_components
= cmi
.serialize_components(self
._cmi_components
)
2626 for _
,stat
in pairs(self
) do
2628 local t
= type(stat
)
2633 and _
~= "_cmi_components" then
2638 --print('===== '..self.name..'\n'.. dump(tmp)..'\n=====\n')
2639 return minetest
.serialize(tmp
)
2643 -- activate mob and reload settings
2644 local mob_activate
= function(self
, staticdata
, def
, dtime
)
2646 -- remove monsters in peaceful mode
2647 if self
.type == "monster"
2648 and peaceful_only
then
2650 self
.object
:remove()
2655 -- load entity variables
2656 local tmp
= minetest
.deserialize(staticdata
)
2659 for _
,stat
in pairs(tmp
) do
2664 -- select random texture, set model and size
2665 if not self
.base_texture
then
2667 -- compatiblity with old simple mobs textures
2668 if type(def
.textures
[1]) == "string" then
2669 def
.textures
= {def
.textures
}
2672 self
.base_texture
= def
.textures
[random(1, #def
.textures
)]
2673 self
.base_mesh
= def
.mesh
2674 self
.base_size
= self
.visual_size
2675 self
.base_colbox
= self
.collisionbox
2676 self
.base_selbox
= self
.selectionbox
2679 -- for current mobs that dont have this set
2680 if not self
.base_selbox
then
2681 self
.base_selbox
= self
.selectionbox
or self
.base_colbox
2684 -- set texture, model and size
2685 local textures
= self
.base_texture
2686 local mesh
= self
.base_mesh
2687 local vis_size
= self
.base_size
2688 local colbox
= self
.base_colbox
2689 local selbox
= self
.base_selbox
2691 -- specific texture if gotten
2692 if self
.gotten
== true
2693 and def
.gotten_texture
then
2694 textures
= def
.gotten_texture
2697 -- specific mesh if gotten
2698 if self
.gotten
== true
2699 and def
.gotten_mesh
then
2700 mesh
= def
.gotten_mesh
2703 -- set child objects to half size
2704 if self
.child
== true then
2707 x
= self
.base_size
.x
* .5,
2708 y
= self
.base_size
.y
* .5,
2711 if def
.child_texture
then
2712 textures
= def
.child_texture
[1]
2716 self
.base_colbox
[1] * .5,
2717 self
.base_colbox
[2] * .5,
2718 self
.base_colbox
[3] * .5,
2719 self
.base_colbox
[4] * .5,
2720 self
.base_colbox
[5] * .5,
2721 self
.base_colbox
[6] * .5
2724 self
.base_selbox
[1] * .5,
2725 self
.base_selbox
[2] * .5,
2726 self
.base_selbox
[3] * .5,
2727 self
.base_selbox
[4] * .5,
2728 self
.base_selbox
[5] * .5,
2729 self
.base_selbox
[6] * .5
2733 if self
.health
== 0 then
2734 self
.health
= random (self
.hp_min
, self
.hp_max
)
2739 self
.path
.way
= {} -- path to follow, table of positions
2740 self
.path
.lastpos
= {x
= 0, y
= 0, z
= 0}
2741 self
.path
.stuck
= false
2742 self
.path
.following
= false -- currently following path?
2743 self
.path
.stuck_timer
= 0 -- if stuck for too long search for path
2746 self
.object
:set_armor_groups({immortal
= 1, fleshy
= self
.armor
})
2747 self
.old_y
= self
.object
:get_pos().y
2748 self
.old_health
= self
.health
2749 self
.sounds
.distance
= self
.sounds
.distance
or 10
2750 self
.textures
= textures
2752 self
.collisionbox
= colbox
2753 self
.selectionbox
= selbox
2754 self
.visual_size
= vis_size
2755 self
.standing_in
= ""
2757 -- check existing nametag
2758 if not self
.nametag
then
2759 self
.nametag
= def
.nametag
2762 -- set anything changed above
2763 self
.object
:set_properties(self
)
2764 set_yaw(self
, (random(0, 360) - 180) / 180 * pi
, 6)
2766 set_animation(self
, "stand")
2768 -- run on_spawn function if found
2769 if self
.on_spawn
and not self
.on_spawn_run
then
2770 if self
.on_spawn(self
) then
2771 self
.on_spawn_run
= true -- if true, set flag to run once only
2775 -- run after_activate
2776 if def
.after_activate
then
2777 def
.after_activate(self
, staticdata
, def
, dtime
)
2781 self
._cmi_components
= cmi
.activate_components(self
.serialized_cmi_components
)
2782 cmi
.notify_activate(self
.object
, dtime
)
2787 -- main mob function
2788 local mob_step
= function(self
, dtime
)
2791 cmi
.notify_step(self
.object
, dtime
)
2794 local pos
= self
.object
:get_pos()
2797 -- when lifetimer expires remove mob (except npc and tamed)
2798 if self
.type ~= "npc"
2800 and self
.state
~= "attack"
2801 and remove_far
~= true
2802 and self
.lifetimer
< 20000 then
2804 self
.lifetimer
= self
.lifetimer
- dtime
2806 if self
.lifetimer
<= 0 then
2808 -- only despawn away from player
2809 local objs
= minetest
.get_objects_inside_radius(pos
, 15)
2813 if objs
[n
]:is_player() then
2821 -- minetest.log("action",
2822 -- S("lifetimer expired, removed @1", self.name))
2824 effect(pos
, 15, "tnt_smoke.png", 2, 4, 2, 0)
2826 self
.object
:remove()
2834 -- smooth rotation by ThomasMonroe314
2836 if self
.delay
and self
.delay
> 0 then
2838 local yaw
= self
.object
:get_yaw()
2840 if self
.delay
== 1 then
2841 yaw
= self
.target_yaw
2843 local dif
= abs(yaw
- self
.target_yaw
)
2845 if yaw
> self
.target_yaw
then
2848 dif
= 2 * pi
- dif
-- need to add
2849 yaw
= yaw
+ dif
/ self
.delay
2851 yaw
= yaw
- dif
/ self
.delay
-- need to subtract
2854 elseif yaw
< self
.target_yaw
then
2858 yaw
= yaw
- dif
/ self
.delay
-- need to subtract
2860 yaw
= yaw
+ dif
/ self
.delay
-- need to add
2864 if yaw
> (pi
* 2) then yaw
= yaw
- (pi
* 2) end
2865 if yaw
< 0 then yaw
= yaw
+ (pi
* 2) end
2868 self
.delay
= self
.delay
- 1
2869 self
.object
:set_yaw(yaw
)
2875 if self
.pause_timer
> 0 then
2877 self
.pause_timer
= self
.pause_timer
- dtime
2882 -- run custom function (defined in mob lua file)
2883 if self
.do_custom
then
2885 -- when false skip going any further
2886 if self
.do_custom(self
, dtime
) == false then
2892 self
.timer
= self
.timer
+ dtime
2894 if self
.state
~= "attack" then
2896 if self
.timer
< 1 then
2903 -- never go over 100
2904 if self
.timer
> 100 then
2908 -- mob plays random sound at times
2909 if random(1, 100) == 1 then
2910 mob_sound(self
, self
.sounds
.random)
2913 -- environmental damage timer (every 1 second)
2914 self
.env_damage_timer
= self
.env_damage_timer
+ dtime
2916 if (self
.state
== "attack" and self
.env_damage_timer
> 1)
2917 or self
.state
~= "attack" then
2919 self
.env_damage_timer
= 0
2921 -- check for environmental damage (water, fire, lava etc.)
2924 -- node replace check (cow eats grass etc.)
2928 monster_attack(self
)
2936 do_states(self
, dtime
)
2945 -- default function when mobs are blown up with TNT
2946 local do_tnt
= function(obj
, damage
)
2948 --print ("----- Damage", damage)
2950 obj
.object
:punch(obj
.object
, 1.0, {
2951 full_punch_interval
= 1.0,
2952 damage_groups
= {fleshy
= damage
},
2955 return false, true, {}
2959 mobs
.spawning_mobs
= {}
2961 -- register mob entity
2962 function mobs
:register_mob(name
, def
)
2964 mobs
.spawning_mobs
[name
] = true
2966 minetest
.register_entity(name
, {
2968 stepheight
= def
.stepheight
or 1.1, -- was 0.6
2971 attack_type
= def
.attack_type
,
2973 fly_in
= def
.fly_in
or "air",
2974 owner
= def
.owner
or "",
2975 order
= def
.order
or "",
2976 on_die
= def
.on_die
,
2977 do_custom
= def
.do_custom
,
2978 jump_height
= def
.jump_height
or 4, -- was 6
2979 drawtype
= def
.drawtype
, -- DEPRECATED, use rotate instead
2980 rotate
= math
.rad(def
.rotate
or 0), -- 0=front, 90=side, 180=back, 270=side2
2981 lifetimer
= def
.lifetimer
or 180, -- 3 minutes
2982 hp_min
= max(1, (def
.hp_min
or 5) * difficulty
),
2983 hp_max
= max(1, (def
.hp_max
or 10) * difficulty
),
2985 collisionbox
= def
.collisionbox
or {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25},
2986 selectionbox
= def
.selectionbox
or def
.collisionbox
,
2987 visual
= def
.visual
,
2988 visual_size
= def
.visual_size
or {x
= 1, y
= 1},
2990 makes_footstep_sound
= def
.makes_footstep_sound
or false,
2991 view_range
= def
.view_range
or 5,
2992 walk_velocity
= def
.walk_velocity
or 1,
2993 run_velocity
= def
.run_velocity
or 2,
2994 damage
= max(0, (def
.damage
or 0) * difficulty
),
2995 light_damage
= def
.light_damage
or 0,
2996 water_damage
= def
.water_damage
or 0,
2997 lava_damage
= def
.lava_damage
or 0,
2998 suffocation
= def
.suffocation
or 2,
2999 fall_damage
= def
.fall_damage
or 1,
3000 fall_speed
= def
.fall_speed
or -10, -- must be lower than -2 (default: -10)
3001 drops
= def
.drops
or {},
3002 armor
= def
.armor
or 100,
3003 on_rightclick
= def
.on_rightclick
,
3005 shoot_interval
= def
.shoot_interval
,
3006 sounds
= def
.sounds
or {},
3007 animation
= def
.animation
,
3008 follow
= def
.follow
,
3009 jump
= def
.jump
~= false,
3010 walk_chance
= def
.walk_chance
or 50,
3011 attacks_monsters
= def
.attacks_monsters
or false,
3012 group_attack
= def
.group_attack
or false,
3013 passive
= def
.passive
or false,
3014 knock_back
= def
.knock_back
~= false,
3015 blood_amount
= def
.blood_amount
or 5,
3016 blood_texture
= def
.blood_texture
or "mobs_blood.png",
3017 shoot_offset
= def
.shoot_offset
or 0,
3018 floats
= def
.floats
or 1, -- floats in water by default
3019 replace_rate
= def
.replace_rate
,
3020 replace_what
= def
.replace_what
,
3021 replace_with
= def
.replace_with
,
3022 replace_offset
= def
.replace_offset
or 0,
3023 on_replace
= def
.on_replace
,
3025 env_damage_timer
= 0, -- only used when state = "attack"
3033 reach
= def
.reach
or 3,
3035 texture_list
= def
.textures
,
3036 child_texture
= def
.child_texture
,
3037 docile_by_day
= def
.docile_by_day
or false,
3039 fear_height
= def
.fear_height
or 0,
3040 runaway
= def
.runaway
,
3042 pathfinding
= def
.pathfinding
,
3043 immune_to
= def
.immune_to
or {},
3044 explosion_radius
= def
.explosion_radius
,
3045 explosion_damage_radius
= def
.explosion_damage_radius
,
3046 explosion_timer
= def
.explosion_timer
or 3,
3047 allow_fuse_reset
= def
.allow_fuse_reset
~= false,
3048 stop_to_explode
= def
.stop_to_explode
~= false,
3049 custom_attack
= def
.custom_attack
,
3050 double_melee_attack
= def
.double_melee_attack
,
3051 dogshoot_switch
= def
.dogshoot_switch
,
3053 dogshoot_count_max
= def
.dogshoot_count_max
or 5,
3054 dogshoot_count2_max
= def
.dogshoot_count2_max
or (def
.dogshoot_count_max
or 5),
3055 attack_animals
= def
.attack_animals
or false,
3056 specific_attack
= def
.specific_attack
,
3057 runaway_from
= def
.runaway_from
,
3058 owner_loyal
= def
.owner_loyal
,
3059 facing_fence
= false,
3062 on_spawn
= def
.on_spawn
,
3064 on_blast
= def
.on_blast
or do_tnt
,
3068 do_punch
= def
.do_punch
,
3070 on_punch
= mob_punch
,
3072 on_breed
= def
.on_breed
,
3074 on_grown
= def
.on_grown
,
3076 on_activate
= function(self
, staticdata
, dtime
)
3077 return mob_activate(self
, staticdata
, def
, dtime
)
3080 get_staticdata
= function(self
)
3081 return mob_staticdata(self
)
3086 end -- END mobs:register_mob function
3089 -- count how many mobs of one type are inside an area
3090 local count_mobs
= function(pos
, type)
3094 local objs
= minetest
.get_objects_inside_radius(pos
, aoc_range
)
3098 if not objs
[n
]:is_player() then
3100 local obj
= objs
[n
]:get_luaentity()
3102 -- count mob type and add to total also
3103 if obj
and obj
.name
and obj
.name
== type then
3105 num_type
= num_type
+ 1
3106 num_total
= num_total
+ 1
3108 -- add to total mobs
3109 elseif obj
and obj
.name
and obj
.health
~= nil then
3111 num_total
= num_total
+ 1
3116 return num_type
, num_total
3122 function mobs
:spawn_abm_check(pos
, node
, name
)
3123 -- global function to add additional spawn checks
3124 -- return true to stop spawning mob
3128 function mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
,
3129 interval
, chance
, aoc
, min_height
, max_height
, day_toggle
, on_spawn
)
3131 -- Do mobs spawn at all?
3132 if not mobs_spawn
then
3136 -- chance/spawn number override in minetest.conf for registered mob
3137 local numbers
= minetest
.settings
:get(name
)
3140 numbers
= numbers
:split(",")
3141 chance
= tonumber(numbers
[1]) or chance
3142 aoc
= tonumber(numbers
[2]) or aoc
3145 minetest
.log("warning", string.format("[mobs] %s has spawning disabled", name
))
3149 minetest
.log("action",
3150 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name
, chance
, aoc
))
3154 minetest
.register_abm({
3156 label
= name
.. " spawning",
3158 neighbors
= neighbors
,
3159 interval
= interval
,
3160 chance
= max(1, (chance
* mob_chance_multiplier
)),
3163 action
= function(pos
, node
, active_object_count
, active_object_count_wider
)
3165 -- is mob actually registered?
3166 if not mobs
.spawning_mobs
[name
]
3167 or not minetest
.registered_entities
[name
] then
3168 --print ("--- mob doesn't exist", name)
3172 -- additional custom checks for spawning mob
3173 if mobs
:spawn_abm_check(pos
, node
, name
) == true then
3177 -- do not spawn if too many of same mob in area
3178 if active_object_count_wider
>= max_per_block
3179 or count_mobs(pos
, name
) >= aoc
then
3180 --print ("--- too many entities", name, aoc, active_object_count_wider)
3184 -- if toggle set to nil then ignore day/night check
3185 if day_toggle
~= nil then
3187 local tod
= (minetest
.get_timeofday() or 0) * 24000
3189 if tod
> 4500 and tod
< 19500 then
3190 -- daylight, but mob wants night
3191 if day_toggle
== false then
3192 --print ("--- mob needs night", name)
3196 -- night time but mob wants day
3197 if day_toggle
== true then
3198 --print ("--- mob needs day", name)
3207 -- only spawn away from player
3208 local objs
= minetest
.get_objects_inside_radius(pos
, 10)
3212 if objs
[n
]:is_player() then
3213 --print ("--- player too close", name)
3218 -- mobs cannot spawn in protected areas when enabled
3219 if not spawn_protected
3220 and minetest
.is_protected(pos
, "") then
3221 --print ("--- inside protected area", name)
3225 -- are we spawning within height limits?
3226 if pos
.y
> max_height
3227 or pos
.y
< min_height
then
3228 --print ("--- height limits not met", name, pos.y)
3232 -- are light levels ok?
3233 local light
= minetest
.get_node_light(pos
)
3235 or light
> max_light
3236 or light
< min_light
then
3237 --print ("--- light limits not met", name, light)
3241 -- do we have enough height clearance to spawn mob?
3242 local ent
= minetest
.registered_entities
[name
]
3243 local height
= max(0, math
.ceil(ent
.collisionbox
[5] - ent
.collisionbox
[2]) - 1)
3245 for n
= 0, height
do
3247 local pos2
= {x
= pos
.x
, y
= pos
.y
+ n
, z
= pos
.z
}
3249 if minetest
.registered_nodes
[node_ok(pos2
).name
].walkable
== true then
3250 --print ("--- inside block", name, node_ok(pos2).name)
3255 -- spawn mob half block higher than ground
3258 local mob
= minetest
.add_entity(pos
, name
)
3260 print ("[mobs] Spawned " .. name .. " at "
3261 .. minetest.pos_to_string(pos) .. " on "
3262 .. node.name .. " near " .. neighbors[1])
3266 local ent
= mob
:get_luaentity()
3275 -- compatibility with older mob registration
3276 function mobs
:register_spawn(name
, nodes
, max_light
, min_light
, chance
, active_object_count
, max_height
, day_toggle
)
3278 mobs
:spawn_specific(name
, nodes
, {"air"}, min_light
, max_light
, 30,
3279 chance
, active_object_count
, -31000, max_height
, day_toggle
)
3283 -- MarkBu's spawn function
3284 function mobs
:spawn(def
)
3286 local name
= def
.name
3287 local nodes
= def
.nodes
or {"group:soil", "group:stone"}
3288 local neighbors
= def
.neighbors
or {"air"}
3289 local min_light
= def
.min_light
or 0
3290 local max_light
= def
.max_light
or 15
3291 local interval
= def
.interval
or 30
3292 local chance
= def
.chance
or 5000
3293 local active_object_count
= def
.active_object_count
or 1
3294 local min_height
= def
.min_height
or -31000
3295 local max_height
= def
.max_height
or 31000
3296 local day_toggle
= def
.day_toggle
3297 local on_spawn
= def
.on_spawn
3299 mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
, interval
,
3300 chance
, active_object_count
, min_height
, max_height
, day_toggle
, on_spawn
)
3304 -- register arrow for shoot attack
3305 function mobs
:register_arrow(name
, def
)
3307 if not name
or not def
then return end -- errorcheck
3309 minetest
.register_entity(name
, {
3312 visual
= def
.visual
,
3313 visual_size
= def
.visual_size
,
3314 textures
= def
.textures
,
3315 velocity
= def
.velocity
,
3316 hit_player
= def
.hit_player
,
3317 hit_node
= def
.hit_node
,
3318 hit_mob
= def
.hit_mob
,
3319 drop
= def
.drop
or false, -- drops arrow as registered item when true
3320 collisionbox
= {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3323 owner_id
= def
.owner_id
,
3324 rotate
= def
.rotate
,
3325 automatic_face_movement_dir
= def
.rotate
3326 and (def
.rotate
- (pi
/ 180)) or false,
3328 on_activate
= def
.on_activate
,
3330 on_step
= def
.on_step
or function(self
, dtime
)
3332 self
.timer
= self
.timer
+ 1
3334 local pos
= self
.object
:get_pos()
3338 or not within_limits(pos
, 0) then
3340 self
.object
:remove() ; -- print ("removed arrow")
3345 -- does arrow have a tail (fireball)
3348 and def
.tail_texture
then
3350 minetest
.add_particle({
3352 velocity
= {x
= 0, y
= 0, z
= 0},
3353 acceleration
= {x
= 0, y
= 0, z
= 0},
3354 expirationtime
= def
.expire
or 0.25,
3355 collisiondetection
= false,
3356 texture
= def
.tail_texture
,
3357 size
= def
.tail_size
or 5,
3358 glow
= def
.glow
or 0,
3362 if self
.hit_node
then
3364 local node
= node_ok(pos
).name
3366 if minetest
.registered_nodes
[node
].walkable
then
3368 self
.hit_node(self
, pos
, node
)
3370 if self
.drop
== true then
3374 self
.lastpos
= (self
.lastpos
or pos
)
3376 minetest
.add_item(self
.lastpos
, self
.object
:get_luaentity().name
)
3379 self
.object
:remove() ; -- print ("hit node")
3385 if self
.hit_player
or self
.hit_mob
then
3387 for _
,player
in pairs(minetest
.get_objects_inside_radius(pos
, 1.0)) do
3390 and player
:is_player() then
3392 self
.hit_player(self
, player
)
3393 self
.object
:remove() ; -- print ("hit player")
3397 local entity
= player
:get_luaentity()
3401 and entity
._cmi_is_mob
== true
3402 and tostring(player
) ~= self
.owner_id
3403 and entity
.name
~= self
.object
:get_luaentity().name
then
3405 self
.hit_mob(self
, player
)
3407 self
.object
:remove() ; --print ("hit mob")
3420 -- compatibility function
3421 function mobs
:explosion(pos
, radius
)
3422 local self
= {sounds
= {}}
3423 self
.sounds
.explode
= "tnt_explode"
3424 mobs
:boom(self
, pos
, radius
)
3428 -- no damage to nodes explosion
3429 function mobs
:safe_boom(self
, pos
, radius
)
3431 minetest
.sound_play(self
.sounds
and self
.sounds
.explode
or "tnt_explode", {
3434 max_hear_distance
= self
.sounds
and self
.sounds
.distance
or 32
3437 entity_physics(pos
, radius
)
3438 effect(pos
, 32, "tnt_smoke.png", radius
* 3, radius
* 5, radius
, 1, 0)
3442 -- make explosion with protection and tnt mod check
3443 function mobs
:boom(self
, pos
, radius
)
3446 and minetest
.get_modpath("mcl_tnt") and tnt
and tnt
.boom
3447 and not minetest
.is_protected(pos
, "") then
3451 damage_radius
= radius
,
3452 sound
= self
.sounds
and self
.sounds
.explode
,
3453 explode_center
= true,
3456 mobs
:safe_boom(self
, pos
, radius
)
3461 -- Register spawn eggs
3463 -- Note: This also introduces the “spawn_egg” group:
3464 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3465 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3466 function mobs
:register_egg(mob
, desc
, background
, addegg
, no_creative
)
3468 local grp
= {spawn_egg
= 1}
3470 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3471 if creative
and no_creative
== true then
3472 grp
.not_in_creative_inventory
= 1
3475 local invimg
= background
3478 invimg
= "mobs_chicken_egg.png^(" .. invimg
..
3479 "^[mask:mobs_chicken_egg_overlay.png)"
3482 -- register new spawn egg containing mob information
3483 --[=[ DISABLED IN MCL2
3484 minetest
.register_craftitem(mob
.. "_set", {
3486 description
= S("@1 (Tamed)", desc
),
3487 inventory_image
= invimg
,
3488 groups
= {spawn_egg
= 2, not_in_creative_inventory
= 1},
3491 on_place
= function(itemstack
, placer
, pointed_thing
)
3493 local pos
= pointed_thing
.above
3495 -- am I clicking on something with existing on_rightclick function?
3496 local under
= minetest
.get_node(pointed_thing
.under
)
3497 local def
= minetest
.registered_nodes
[under
.name
]
3498 if def
and def
.on_rightclick
then
3499 return def
.on_rightclick(pointed_thing
.under
, under
, placer
, itemstack
)
3503 and within_limits(pos
, 0)
3504 and not minetest
.is_protected(pos
, placer
:get_player_name()) then
3506 if not minetest
.registered_entities
[mob
] then
3512 local data
= itemstack
:get_metadata()
3513 local mob
= minetest
.add_entity(pos
, mob
, data
)
3514 local ent
= mob
:get_luaentity()
3516 -- set owner if not a monster
3517 if ent
.type ~= "monster" then
3518 ent
.owner
= placer
:get_player_name()
3522 -- since mob is unique we remove egg once spawned
3523 itemstack
:take_item()
3531 -- register old stackable mob egg
3532 minetest
.register_craftitem(mob
, {
3535 inventory_image
= invimg
,
3538 _doc_items_longdesc
= "This allows you to place a single mob.",
3539 _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.",
3541 on_place
= function(itemstack
, placer
, pointed_thing
)
3543 local pos
= pointed_thing
.above
3545 -- am I clicking on something with existing on_rightclick function?
3546 local under
= minetest
.get_node(pointed_thing
.under
)
3547 local def
= minetest
.registered_nodes
[under
.name
]
3548 if def
and def
.on_rightclick
then
3549 return def
.on_rightclick(pointed_thing
.under
, under
, placer
, itemstack
)
3553 and within_limits(pos
, 0)
3554 and not minetest
.is_protected(pos
, placer
:get_player_name()) then
3556 local name
= placer
:get_player_name()
3557 local privs
= minetest
.get_player_privs(name
)
3558 if not privs
.maphack
then
3559 minetest
.chat_send_player(name
, "You need the “maphack” privilege to change the mob spawner.")
3562 if minetest
.get_modpath("mcl_mobspawners") and under
.name
== "mcl_mobspawners:spawner" then
3563 mcl_mobspawners
.setup_spawner(pointed_thing
.under
, itemstack
:get_name())
3564 if not minetest
.settings
:get_bool("creative_mode") then
3565 itemstack
:take_item()
3570 if not minetest
.registered_entities
[mob
] then
3576 local mob
= minetest
.add_entity(pos
, mob
)
3577 local ent
= mob
:get_luaentity()
3579 -- don't set owner if monster or sneak pressed
3580 if ent
.type ~= "monster"
3581 and not placer
:get_player_control().sneak
then
3582 ent
.owner
= placer
:get_player_name()
3587 local nametag
= itemstack
:get_meta():get_string("name")
3588 if nametag
~= "" then
3589 if string.len(nametag
) > MAX_MOB_NAME_LENGTH
then
3590 nametag
= string.sub(nametag
, 1, MAX_MOB_NAME_LENGTH
)
3592 ent
.nametag
= nametag
3596 -- if not in creative then take item
3597 if not mobs
.is_creative(placer
:get_player_name()) then
3598 itemstack
:take_item()
3609 -- capture critter (thanks to blert2112 for idea)
3610 function mobs
:capture_mob(self
, clicker
, chance_hand
, chance_net
, chance_lasso
, force_take
, replacewith
)
3612 --[=[ DISABLED IN MCL2
3614 or not clicker
:is_player()
3615 or not clicker
:get_inventory() then
3619 -- get name of clicked mob
3620 local mobname
= self
.name
3622 -- if not nil change what will be added to inventory
3624 mobname
= replacewith
3627 local name
= clicker
:get_player_name()
3628 local tool
= clicker
:get_wielded_item()
3630 -- are we using hand, net or lasso to pick up mob?
3631 if tool
:get_name() ~= ""
3632 and tool
:get_name() ~= "mobs:net"
3633 and tool
:get_name() ~= "mobs:lasso" then
3638 if self
.tamed
== false
3639 and force_take
== false then
3641 minetest
.chat_send_player(name
, S("Not tamed!"))
3643 return true -- false
3646 -- cannot pick up if not owner
3647 if self
.owner
~= name
3648 and force_take
== false then
3650 minetest
.chat_send_player(name
, S("@1 is owner!", self
.owner
))
3652 return true -- false
3655 if clicker
:get_inventory():room_for_item("main", mobname
) then
3657 -- was mob clicked with hand, net, or lasso?
3660 if tool
:get_name() == "" then
3661 chance
= chance_hand
3663 elseif tool
:get_name() == "mobs:net" then
3667 tool
:add_wear(4000) -- 17 uses
3669 clicker
:set_wielded_item(tool
)
3671 elseif tool
:get_name() == "mobs:lasso" then
3673 chance
= chance_lasso
3675 tool
:add_wear(650) -- 100 uses
3677 clicker
:set_wielded_item(tool
)
3681 -- calculate chance.. add to inventory if successful?
3682 if chance
> 0 and random(1, 100) <= chance
then
3685 local new_stack
= ItemStack(mobname
)
3687 -- add special mob egg with all mob information
3688 -- unless 'replacewith' contains new item to use
3689 if not replacewith
then
3691 new_stack
= ItemStack(mobname
.. "_set")
3695 for _
,stat
in pairs(self
) do
3696 local t
= type(stat
)
3699 and t
~= "userdata" then
3704 local data_str
= minetest
.serialize(tmp
)
3706 new_stack
:set_metadata(data_str
)
3709 local inv
= clicker
:get_inventory()
3711 if inv
:room_for_item("main", new_stack
) then
3712 inv
:add_item("main", new_stack
)
3714 minetest
.add_item(clicker
:get_pos(), new_stack
)
3717 self
.object
:remove()
3719 mob_sound(self
, "default_place_node_hard")
3721 elseif chance
~= 0 then
3722 minetest
.chat_send_player(name
, S("Missed!"))
3724 mob_sound(self
, "mobs_swing")
3733 -- protect tamed mob with rune item
3734 function mobs
:protect(self
, clicker
)
3735 local name
= clicker
:get_player_name()
3736 local tool
= clicker
:get_wielded_item()
3738 if tool
:get_name() ~= "mobs:protector" then
3742 if self
.tamed
== false then
3743 minetest
.chat_send_player(name
, S("Not tamed!"))
3744 return true -- false
3747 if self
.protected
== true then
3748 minetest
.chat_send_player(name
, S("Already protected!"))
3749 return true -- false
3752 if not mobs
.is_creative(clicker
:get_player_name()) then
3753 tool
:take_item() -- take 1 protection rune
3754 clicker
:set_wielded_item(tool
)
3757 self
.protected
= true
3759 local pos
= self
.object
:get_pos()
3760 pos
.y
= pos
.y
+ self
.collisionbox
[2] + 0.5
3762 effect(self
.object
:get_pos(), 25, "mobs_protect_particle.png", 0.5, 4, 2, 15)
3764 mob_sound(self
, "mobs_spell")
3773 -- feeding, taming and breeding (thanks blert2112)
3774 function mobs
:feed_tame(self
, clicker
, feed_count
, breed
, tame
)
3775 if not self
.follow
then
3779 -- can eat/tame with item in hand
3780 if follow_holding(self
, clicker
) then
3782 -- if not in creative then take item
3783 if not mobs
.is_creative(clicker
:get_player_name()) then
3785 local item
= clicker
:get_wielded_item()
3789 clicker
:set_wielded_item(item
)
3793 self
.health
= self
.health
+ 4
3795 if self
.health
>= self
.hp_max
then
3797 self
.health
= self
.hp_max
3799 if self
.htimer
< 1 then
3802 minetest
.chat_send_player(clicker
:get_player_name(),
3803 S("@1 at full health (@2)",
3804 self
.name
:split(":")[2], tostring(self
.health
)))
3810 self
.object
:set_hp(self
.health
)
3814 -- make children grow quicker
3815 if self
.child
== true then
3817 self
.hornytimer
= self
.hornytimer
+ 20
3823 self
.food
= (self
.food
or 0) + 1
3824 if self
.food
>= feed_count
then
3828 if breed
and self
.hornytimer
== 0 then
3836 if self
.tamed
== false then
3837 minetest
.chat_send_player(clicker
:get_player_name(),
3838 S("@1 has been tamed!",
3839 self
.name
:split(":")[2]))
3844 if not self
.owner
or self
.owner
== "" then
3845 self
.owner
= clicker
:get_player_name()
3849 -- make sound when fed so many times
3850 mob_sound(self
, self
.sounds
.random)
3856 local item
= clicker
:get_wielded_item()
3858 -- Name mob with nametag
3859 if item
:get_name() == "mobs:nametag" then
3861 local tag = item
:get_meta():get_string("name")
3863 if string.len(tag) > MAX_MOB_NAME_LENGTH
then
3864 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH
)
3870 if not mobs
.is_creative(name
) then
3872 player
:set_wielded_item(item
)
3884 -- inspired by blockmen's nametag mod
3885 minetest
.register_on_player_receive_fields(function(player
, formname
, fields
)
3887 -- right-clicked with nametag and name entered?
3888 if formname
== "mobs_nametag"
3890 and fields
.name
~= "" then
3892 local name
= player
:get_player_name()
3894 if not mob_obj
[name
]
3895 or not mob_obj
[name
].object
then
3899 -- make sure nametag is being used to name mob
3900 local item
= player
:get_wielded_item()
3902 if item
:get_name() ~= "mobs:nametag" then
3906 -- limit name entered to 64 characters long
3907 if string.len(fields
.name
) > 64 then
3908 fields
.name
= string.sub(fields
.name
, 1, 64)
3912 mob_obj
[name
].nametag
= fields
.name
3914 update_tag(mob_obj
[name
])
3916 -- if not in creative then take item
3917 if not mobs
.is_creative(name
) then
3919 mob_sta
[name
]:take_item()
3921 player
:set_wielded_item(mob_sta
[name
])
3924 -- reset external variables
3932 -- compatibility function for old entities to new modpack entities
3933 function mobs
:alias_mob(old_name
, new_name
)
3936 minetest
.register_alias(old_name
, new_name
)
3939 minetest
.register_entity(":" .. old_name
, {
3943 on_step
= function(self
)
3945 if minetest
.registered_entities
[new_name
] then
3946 minetest
.add_entity(self
.object
:get_pos(), new_name
)
3949 self
.object
:remove()