2 -- API for Mobs Redo: MineClone 2 Edition (MRM)
6 mobs
.version
= "20180531" -- don't rely too much on this, rarely updated, if ever
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
55 local damage_enabled
= minetest
.settings
:get_bool("enable_damage")
56 local mobs_spawn
= minetest
.settings
:get_bool("mobs_spawn") ~= false
57 local peaceful_only
= minetest
.settings
:get_bool("only_peaceful_mobs")
58 local disable_blood
= minetest
.settings
:get_bool("mobs_disable_blood")
59 local mobs_drop_items
= minetest
.settings
:get_bool("mobs_drop_items") ~= false
60 local mobs_griefing
= minetest
.settings
:get_bool("mobs_griefing") ~= false
61 local creative
= minetest
.settings
:get_bool("creative_mode")
62 local spawn_protected
= minetest
.settings
:get_bool("mobs_spawn_protected") ~= false
63 local remove_far
= false
64 local difficulty
= tonumber(minetest
.settings
:get("mob_difficulty")) or 1.0
65 local show_health
= false
66 local max_per_block
= tonumber(minetest
.settings
:get("max_objects_per_block") or 99)
67 local mob_chance_multiplier
= tonumber(minetest
.settings
:get("mob_chance_multiplier") or 1)
69 -- Peaceful mode message so players will know there are no monsters
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"))
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
88 local node_fire
= "mcl_fire:fire"
89 local node_permanent_flame
= "mcl_fire:eternal_fire"
90 local node_ice
= "mcl_core:ice"
91 local node_snowblock
= "mcl_core:snowblock"
92 local node_snow
= "mcl_core:snow"
93 mobs
.fallback_node
= minetest
.registered_aliases
["mapgen_dirt"] or "mcl_core:dirt"
95 local mod_weather
= minetest
.get_modpath("mcl_weather") ~= nil
96 local mod_tnt
= minetest
.get_modpath("mcl_tnt") ~= nil
97 local mod_mobspawners
= minetest
.get_modpath("mcl_mobspawners") ~= nil
100 local mob_sound
= function(self
, sound
)
103 minetest
.sound_play(sound
, {
104 object
= self
.object
,
106 max_hear_distance
= self
.sounds
.distance
113 local do_attack
= function(self
, player
)
115 if self
.state
== "attack" then
120 self
.state
= "attack"
122 if random(0, 100) < 90 then
123 mob_sound(self
, self
.sounds
.war_cry
)
128 -- move mob in facing direction
129 local set_velocity
= function(self
, v
)
131 -- do not move if mob has been ordered to stay
132 if self
.order
== "stand" then
133 self
.object
:setvelocity({x
= 0, y
= 0, z
= 0})
137 local yaw
= (self
.object
:get_yaw() or 0) + self
.rotate
139 self
.object
:setvelocity({
141 y
= self
.object
:getvelocity().y
,
147 -- calculate mob velocity
148 local get_velocity
= function(self
)
150 local v
= self
.object
:getvelocity()
152 return (v
.x
* v
.x
+ v
.z
* v
.z
) ^
0.5
156 -- set and return valid yaw
157 local set_yaw
= function(self
, yaw
, delay
)
159 if not yaw
or yaw
~= yaw
then
166 self
.object
:set_yaw(yaw
)
170 self
.target_yaw
= yaw
173 return self
.target_yaw
176 -- global function to set mob yaw
177 function mobs
:yaw(self
, yaw
, delay
)
178 set_yaw(self
, yaw
, delay
)
182 -- set defined animation
183 local set_animation
= function(self
, anim
)
185 if not self
.animation
186 or not anim
then return end
188 self
.animation
.current
= self
.animation
.current
or ""
190 if anim
== self
.animation
.current
191 or not self
.animation
[anim
.. "_start"]
192 or not self
.animation
[anim
.. "_end"] then
196 self
.animation
.current
= anim
198 self
.object
:set_animation({
199 x
= self
.animation
[anim
.. "_start"],
200 y
= self
.animation
[anim
.. "_end"]},
201 self
.animation
[anim
.. "_speed"] or self
.animation
.speed_normal
or 15,
202 0, self
.animation
[anim
.. "_loop"] ~= false)
206 -- above function exported for mount.lua
207 function mobs
:set_animation(self
, anim
)
208 set_animation(self
, anim
)
212 -- calculate distance
213 local get_distance
= function(a
, b
)
215 local x
, y
, z
= a
.x
- b
.x
, a
.y
- b
.y
, a
.z
- b
.z
217 return square(x
* x
+ y
* y
+ z
* z
)
221 -- check line of sight (BrunoMine)
222 local line_of_sight
= function(self
, pos1
, pos2
, stepsize
)
224 stepsize
= stepsize
or 1
226 local s
, pos
= minetest
.line_of_sight(pos1
, pos2
, stepsize
)
228 -- normal walking and flying mobs can see you through air
233 -- New pos1 to be analyzed
234 local npos1
= {x
= pos1
.x
, y
= pos1
.y
, z
= pos1
.z
}
236 local r
, pos
= minetest
.line_of_sight(npos1
, pos2
, stepsize
)
239 if r
== true then return true end
242 local nn
= minetest
.get_node(pos
).name
244 -- Target Distance (td) to travel
245 local td
= get_distance(pos1
, pos2
)
247 -- Actual Distance (ad) traveled
250 -- It continues to advance in the line of sight in search of a real
251 -- obstruction which counts as 'normal' nodebox.
252 while minetest
.registered_nodes
[nn
]
253 and (minetest
.registered_nodes
[nn
].walkable
== false
254 or minetest
.registered_nodes
[nn
].drawtype
== "nodebox") do
256 -- Check if you can still move forward
257 if td
< ad
+ stepsize
then
258 return true -- Reached the target
261 -- Moves the analyzed pos
262 local d
= get_distance(pos1
, pos2
)
264 npos1
.x
= ((pos2
.x
- pos1
.x
) / d
* stepsize
) + pos1
.x
265 npos1
.y
= ((pos2
.y
- pos1
.y
) / d
* stepsize
) + pos1
.y
266 npos1
.z
= ((pos2
.z
- pos1
.z
) / d
* stepsize
) + pos1
.z
270 or npos1
.x
~= npos1
.x
271 or npos1
.y
~= npos1
.y
272 or npos1
.z
~= npos1
.z
then
279 r
, pos
= minetest
.line_of_sight(npos1
, pos2
, stepsize
)
281 if r
== true then return true end
283 -- New Nodename found
284 nn
= minetest
.get_node(pos
).name
292 -- are we flying in what we are suppose to? (taikedz)
293 local flight_check
= function(self
, pos_w
)
295 local nod
= self
.standing_in
296 local def
= minetest
.registered_nodes
[nod
]
298 if not def
then return false end -- nil check
300 if type(self
.fly_in
) == "string"
301 and nod
== self
.fly_in
then
305 elseif type(self
.fly_in
) == "table" then
307 for _
,fly_in
in pairs(self
.fly_in
) do
309 if nod
== fly_in
then
316 -- stops mobs getting stuck inside stairs and plantlike nodes
317 if def
.drawtype
~= "airlike"
318 and def
.drawtype
~= "liquid"
319 and def
.drawtype
~= "flowingliquid" then
327 -- custom particle effects
328 local effect
= function(pos
, amount
, texture
, min_size
, max_size
, radius
, gravity
, glow
)
331 min_size
= min_size
or 0.5
332 max_size
= max_size
or 1
333 gravity
= gravity
or -10
336 minetest
.add_particlespawner({
341 minvel
= {x
= -radius
, y
= -radius
, z
= -radius
},
342 maxvel
= {x
= radius
, y
= radius
, z
= radius
},
343 minacc
= {x
= 0, y
= gravity
, z
= 0},
344 maxacc
= {x
= 0, y
= gravity
, z
= 0},
355 local update_tag
= function(self
)
356 self
.object
:set_properties({
357 nametag
= self
.nametag
,
364 local item_drop
= function(self
, cooked
)
366 -- no drops if disabled by setting
367 if not mobs_drop_items
then return end
369 -- no drops for child mobs
370 if self
.child
then return end
373 local pos
= self
.object
:get_pos()
375 self
.drops
= self
.drops
or {} -- nil check
377 for n
= 1, #self
.drops
do
379 if random(1, self
.drops
[n
].chance
) == 1 then
381 num
= random(self
.drops
[n
].min or 1, self
.drops
[n
].max or 1)
382 item
= self
.drops
[n
].name
384 -- cook items when true
387 local output
= minetest
.get_craft_result({
388 method
= "cooking", width
= 1, items
= {item
}})
390 if output
and output
.item
and not output
.item
:is_empty() then
391 item
= output
.item
:get_name()
395 -- add item if it exists
396 obj
= minetest
.add_item(pos
, ItemStack(item
.. " " .. num
))
398 if obj
and obj
:get_luaentity() then
401 x
= random(-10, 10) / 9,
403 z
= random(-10, 10) / 9,
406 obj
:remove() -- item does not exist
415 -- check if mob is dead or only hurt
416 local check_for_death
= function(self
, cause
, cmi_cause
)
418 -- has health actually changed?
419 if self
.health
== self
.old_health
and self
.health
> 0 then
423 self
.old_health
= self
.health
425 -- still got some health? play hurt sound
426 if self
.health
> 0 then
428 mob_sound(self
, self
.sounds
.damage
)
430 -- make sure health isn't higher than max
431 if self
.health
> self
.hp_max
then
432 self
.health
= self
.hp_max
435 -- backup nametag so we can show health stats
436 if not self
.nametag2
then
437 self
.nametag2
= self
.nametag
or ""
441 and (cmi_cause
and cmi_cause
.type == "punch") then
444 self
.nametag
= "♥ " .. self
.health
.. " / " .. self
.hp_max
452 -- dropped cooked item if mob died in lava
453 if cause
== "lava" then
454 item_drop(self
, true)
459 mob_sound(self
, self
.sounds
.death
)
461 local pos
= self
.object
:get_pos()
463 -- execute custom death function
466 self
.on_die(self
, pos
)
469 cmi
.notify_die(self
.object
, cmi_cause
)
477 -- default death function and die animation (if defined)
479 and self
.animation
.die_start
480 and self
.animation
.die_end
then
482 local frames
= self
.animation
.die_end
- self
.animation
.die_start
483 local speed
= self
.animation
.die_speed
or 15
484 local length
= max(frames
/ speed
, 0)
492 set_velocity(self
, 0)
493 set_animation(self
, "die")
495 minetest
.after(length
, function(self
)
496 if not self
.object
:get_luaentity() then
500 cmi
.notify_die(self
.object
, cmi_cause
)
508 cmi
.notify_die(self
.object
, cmi_cause
)
514 effect(pos
, 20, "tnt_smoke.png")
520 -- check if within physical map limits (-30911 to 30927)
521 local within_limits
= function(pos
, radius
)
523 if (pos
.x
- radius
) > -30913
524 and (pos
.x
+ radius
) < 30928
525 and (pos
.y
- radius
) > -30913
526 and (pos
.y
+ radius
) < 30928
527 and (pos
.z
- radius
) > -30913
528 and (pos
.z
+ radius
) < 30928 then
529 return true -- within limits
532 return false -- beyond limits
536 -- is mob facing a cliff
537 local is_at_cliff
= function(self
)
539 if self
.fear_height
== 0 then -- 0 for no falling protection!
543 local yaw
= self
.object
:get_yaw()
544 local dir_x
= -sin(yaw
) * (self
.collisionbox
[4] + 0.5)
545 local dir_z
= cos(yaw
) * (self
.collisionbox
[4] + 0.5)
546 local pos
= self
.object
:get_pos()
547 local ypos
= pos
.y
+ self
.collisionbox
[2] -- just above floor
549 if minetest
.line_of_sight(
550 {x
= pos
.x
+ dir_x
, y
= ypos
, z
= pos
.z
+ dir_z
},
551 {x
= pos
.x
+ dir_x
, y
= ypos
- self
.fear_height
, z
= pos
.z
+ dir_z
}
561 -- get node but use fallback for nil or unknown
562 local node_ok
= function(pos
, fallback
)
564 fallback
= fallback
or mobs
.fallback_node
566 local node
= minetest
.get_node_or_nil(pos
)
568 if node
and minetest
.registered_nodes
[node
.name
] then
572 return minetest
.registered_nodes
[fallback
]
576 -- environmental damage (water, lava, fire, light etc.)
577 local do_env_damage
= function(self
)
579 -- feed/tame text timer (so mob 'full' messages dont spam chat)
580 if self
.htimer
> 0 then
581 self
.htimer
= self
.htimer
- 1
584 -- reset nametag after showing health stats
585 if self
.htimer
< 1 and self
.nametag2
then
587 self
.nametag
= self
.nametag2
593 local pos
= self
.object
:get_pos()
595 self
.time_of_day
= minetest
.get_timeofday()
597 -- remove mob if beyond map limits
598 if not within_limits(pos
, 0) then
604 local deal_light_damage
= function(self
, pos
, damage
)
605 if not (mod_weather
and (mcl_weather
.rain
.raining
or mcl_weather
.state
== "snow") and mcl_weather
.is_outdoor(pos
)) then
606 self
.health
= self
.health
- damage
608 effect(pos
, 5, "tnt_smoke.png")
610 if check_for_death(self
, "light", {type = "light"}) then return end
614 -- bright light harms mob
615 if self
.light_damage
~= 0 and (minetest
.get_node_light(pos
) or 0) > 12 then
616 deal_light_damage(self
, pos
, self
.light_damage
)
618 local _
, dim
= mcl_worlds
.y_to_layer(pos
.y
)
619 if self
.sunlight_damage
~= 0 and (minetest
.get_node_light(pos
) or 0) >= minetest
.LIGHT_MAX
and dim
== "overworld" then
620 deal_light_damage(self
, pos
, self
.sunlight_damage
)
623 local y_level
= self
.collisionbox
[2]
626 y_level
= self
.collisionbox
[2] * 0.5
629 -- what is mob standing in?
630 pos
.y
= pos
.y
+ y_level
+ 0.25 -- foot level
631 self
.standing_in
= node_ok(pos
, "air").name
633 -- don't fall when on ignore, just stand still
634 if self
.standing_in
== "ignore" then
635 self
.object
:setvelocity({x
= 0, y
= 0, z
= 0})
638 local nodef
= minetest
.registered_nodes
[self
.standing_in
]
641 if self
.rain_damage
and mod_weather
then
642 if mcl_weather
.rain
.raining
and mcl_weather
.is_outdoor(pos
) then
644 self
.health
= self
.health
- self
.rain_damage
646 if check_for_death(self
, "rain", {type = "environment",
647 pos
= pos
, node
= self
.standing_in
}) then return end
651 pos
.y
= pos
.y
+ 1 -- for particle effect position
655 and nodef
.groups
.water
then
657 if self
.water_damage
~= 0 then
659 self
.health
= self
.health
- self
.water_damage
661 effect(pos
, 5, "bubble.png", nil, nil, 1, nil)
663 if check_for_death(self
, "water", {type = "environment",
664 pos
= pos
, node
= self
.standing_in
}) then return end
668 elseif self
.lava_damage
669 and (nodef
.groups
.lava
670 or self
.standing_in
== node_fire
671 or self
.standing_in
== node_permanent_flame
) then
673 if self
.lava_damage
~= 0 then
675 self
.health
= self
.health
- self
.lava_damage
677 effect(pos
, 5, "fire_basic_flame.png", nil, nil, 1, nil)
679 if check_for_death(self
, "lava", {type = "environment",
680 pos
= pos
, node
= self
.standing_in
}) then return end
683 -- damage_per_second node check
684 elseif nodef
.damage_per_second
~= 0 then
686 self
.health
= self
.health
- nodef
.damage_per_second
688 effect(pos
, 5, "tnt_smoke.png")
690 if check_for_death(self
, "dps", {type = "environment",
691 pos
= pos
, node
= self
.standing_in
}) then return end
694 --- suffocation inside solid node
695 if self.suffocation ~= 0
696 and nodef.walkable == true
697 and nodef.groups.disable_suffocation ~= 1
698 and nodef.drawtype == "normal" then
700 self.health = self.health - self.suffocation
702 if check_for_death(self, "suffocation", {type = "environment",
703 pos = pos, node = self.standing_in}) then return end
706 check_for_death(self
, "", {type = "unknown"})
710 -- jump if facing a solid node (not fences or gates)
711 local do_jump
= function(self
)
714 or self
.jump_height
== 0
717 or self
.order
== "stand" then
721 self
.facing_fence
= false
723 -- something stopping us while moving?
724 if self
.state
~= "stand"
725 and get_velocity(self
) > 0.5
726 and self
.object
:getvelocity().y
~= 0 then
730 local pos
= self
.object
:get_pos()
731 local yaw
= self
.object
:get_yaw()
733 -- what is mob standing on?
734 pos
.y
= pos
.y
+ self
.collisionbox
[2] - 0.2
736 local nod
= node_ok(pos
)
738 if minetest
.registered_nodes
[nod
.name
].walkable
== false then
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 if self
.walk_chance
== 0
759 or minetest
.registered_items
[nod
.name
].walkable
then
761 if not nod
.name
:find("fence")
762 and not nod
.name
:find("gate") then
764 local v
= self
.object
:getvelocity()
766 v
.y
= self
.jump_height
768 set_animation(self
, "jump") -- only when defined
770 self
.object
:setvelocity(v
)
772 -- when in air move forward
773 minetest
.after(0.3, function(self
, v
)
774 if not self
.object
:get_luaentity() then
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(parent1
, parent2
, pos
)
958 if not parent1
.object
:get_luaentity() then
961 if not parent2
.object
:get_luaentity() then
965 -- custom breed function
966 if parent1
.on_breed
then
967 -- when false, skip going any further
968 if parent1
.on_breed(parent1
, parent2
) == false then
973 local child
= mobs
:spawn_child(pos
, parent1
.name
)
975 local ent_c
= child
:get_luaentity()
978 -- Use texture of one of the parents
979 local p
= math
.random(1, 2)
981 ent_c
.base_texture
= parent1
.base_texture
983 ent_c
.base_texture
= parent2
.base_texture
985 child
:set_properties({
986 textures
= ent_c
.base_texture
989 -- tamed and owned by parents' owner
991 ent_c
.owner
= parent1
.owner
1003 -- find and replace what mob is looking for (grass, wheat etc.)
1004 local replace
= function(self
, pos
)
1006 if not mobs_griefing
1007 or not self
.replace_rate
1008 or not self
.replace_what
1009 or self
.child
== true
1010 or self
.object
:getvelocity().y
~= 0
1011 or random(1, self
.replace_rate
) > 1 then
1015 local what
, with
, y_offset
1017 if type(self
.replace_what
[1]) == "table" then
1019 local num
= random(#self
.replace_what
)
1021 what
= self
.replace_what
[num
][1] or ""
1022 with
= self
.replace_what
[num
][2] or ""
1023 y_offset
= self
.replace_what
[num
][3] or 0
1025 what
= self
.replace_what
1026 with
= self
.replace_with
or ""
1027 y_offset
= self
.replace_offset
or 0
1030 pos
.y
= pos
.y
+ y_offset
1032 if #minetest
.find_nodes_in_area(pos
, pos
, what
) > 0 then
1034 local oldnode
= {name
= what
}
1035 local newnode
= {name
= with
}
1036 local on_replace_return
1038 if self
.on_replace
then
1039 on_replace_return
= self
.on_replace(self
, pos
, oldnode
, newnode
)
1042 if on_replace_return
~= false then
1044 minetest
.set_node(pos
, {name
= with
})
1046 -- when cow/sheep eats grass, replace wool and milk
1047 if self
.gotten
== true then
1049 self
.object
:set_properties(self
)
1056 -- check if daytime and also if mob is docile during daylight hours
1057 local day_docile
= function(self
)
1059 if self
.docile_by_day
== false then
1063 elseif self
.docile_by_day
== true
1064 and self
.time_of_day
> 0.2
1065 and self
.time_of_day
< 0.8 then
1072 local los_switcher
= false
1073 local height_switcher
= false
1075 -- path finding and smart mob routine by rnd, line_of_sight and other edits by Elkien3
1076 local smart_mobs
= function(self
, s
, p
, dist
, dtime
)
1078 local s1
= self
.path
.lastpos
1080 local target_pos
= self
.attack
:get_pos()
1082 -- is it becoming stuck?
1083 if abs(s1
.x
- s
.x
) + abs(s1
.z
- s
.z
) < .5 then
1084 self
.path
.stuck_timer
= self
.path
.stuck_timer
+ dtime
1086 self
.path
.stuck_timer
= 0
1089 self
.path
.lastpos
= {x
= s
.x
, y
= s
.y
, z
= s
.z
}
1091 local use_pathfind
= false
1092 local has_lineofsight
= minetest
.line_of_sight(
1093 {x
= s
.x
, y
= (s
.y
) + .5, z
= s
.z
},
1094 {x
= target_pos
.x
, y
= (target_pos
.y
) + 1.5, z
= target_pos
.z
}, .2)
1096 -- im stuck, search for path
1097 if not has_lineofsight
then
1099 if los_switcher
== true then
1101 los_switcher
= false
1102 end -- cannot see target!
1104 if los_switcher
== false then
1107 use_pathfind
= false
1109 minetest
.after(1, function(self
)
1110 if not self
.object
:get_luaentity() then
1113 if has_lineofsight
then self
.path
.following
= false end
1115 end -- can see target!
1118 if (self
.path
.stuck_timer
> stuck_timeout
and not self
.path
.following
) then
1121 self
.path
.stuck_timer
= 0
1123 minetest
.after(1, function(self
)
1124 if not self
.object
:get_luaentity() then
1127 if has_lineofsight
then self
.path
.following
= false end
1131 if (self
.path
.stuck_timer
> stuck_path_timeout
and self
.path
.following
) then
1134 self
.path
.stuck_timer
= 0
1136 minetest
.after(1, function(self
)
1137 if not self
.object
:get_luaentity() then
1140 if has_lineofsight
then self
.path
.following
= false end
1144 if math
.abs(vector
.subtract(s
,target_pos
).y
) > self
.stepheight
then
1146 if height_switcher
then
1148 height_switcher
= false
1151 if not height_switcher
then
1152 use_pathfind
= false
1153 height_switcher
= true
1157 if use_pathfind
then
1158 -- lets try find a path, first take care of positions
1159 -- since pathfinder is very sensitive
1160 local sheight
= self
.collisionbox
[5] - self
.collisionbox
[2]
1162 -- round position to center of node to avoid stuck in walls
1163 -- also adjust height for player models!
1164 s
.x
= floor(s
.x
+ 0.5)
1165 s
.z
= floor(s
.z
+ 0.5)
1167 local ssight
, sground
= minetest
.line_of_sight(s
, {
1168 x
= s
.x
, y
= s
.y
- 4, z
= s
.z
}, 1)
1170 -- determine node above ground
1175 local p1
= self
.attack
:get_pos()
1177 p1
.x
= floor(p1
.x
+ 0.5)
1178 p1
.y
= floor(p1
.y
+ 0.5)
1179 p1
.z
= floor(p1
.z
+ 0.5)
1181 local dropheight
= 6
1182 if self
.fear_height
~= 0 then dropheight
= self
.fear_height
end
1184 self
.path
.way
= minetest
.find_path(s
, p1
, 16, self
.stepheight
, dropheight
, "A*_noprefetch")
1187 do_attack(self
, self
.attack
)
1189 -- no path found, try something else
1190 if not self
.path
.way
then
1192 self
.path
.following
= false
1194 -- lets make way by digging/building if not accessible
1195 if self
.pathfinding
== 2 and mobs_griefing
then
1197 -- is player higher than mob?
1201 if not minetest
.is_protected(s
, "") then
1203 local ndef1
= minetest
.registered_nodes
[self
.standing_in
]
1205 if ndef1
and (ndef1
.buildable_to
or ndef1
.groups
.liquid
) then
1207 minetest
.set_node(s
, {name
= mobs
.fallback_node
})
1211 local sheight
= math
.ceil(self
.collisionbox
[5]) + 1
1213 -- assume mob is 2 blocks high so it digs above its head
1216 -- remove one block above to make room to jump
1217 if not minetest
.is_protected(s
, "") then
1219 local node1
= node_ok(s
, "air").name
1220 local ndef1
= minetest
.registered_nodes
[node1
]
1223 and node1
~= "ignore"
1225 and not ndef1
.groups
.level
1226 and not ndef1
.groups
.unbreakable
1227 and not ndef1
.groups
.liquid
then
1229 minetest
.set_node(s
, {name
= "air"})
1230 minetest
.add_item(s
, ItemStack(node1
))
1236 self
.object
:setpos({x
= s
.x
, y
= s
.y
+ 2, z
= s
.z
})
1238 else -- dig 2 blocks to make door toward player direction
1240 local yaw1
= self
.object
:get_yaw() + pi
/ 2
1242 x
= s
.x
+ cos(yaw1
),
1247 if not minetest
.is_protected(p1
, "") then
1249 local node1
= node_ok(p1
, "air").name
1250 local ndef1
= minetest
.registered_nodes
[node1
]
1253 and node1
~= "ignore"
1255 and not ndef1
.groups
.level
1256 and not ndef1
.groups
.unbreakable
1257 and not ndef1
.groups
.liquid
then
1259 minetest
.add_item(p1
, ItemStack(node1
))
1260 minetest
.set_node(p1
, {name
= "air"})
1264 node1
= node_ok(p1
, "air").name
1265 ndef1
= minetest
.registered_nodes
[node1
]
1268 and node1
~= "ignore"
1270 and not ndef1
.groups
.level
1271 and not ndef1
.groups
.unbreakable
1272 and not ndef1
.groups
.liquid
then
1274 minetest
.add_item(p1
, ItemStack(node1
))
1275 minetest
.set_node(p1
, {name
= "air"})
1282 -- will try again in 2 second
1283 self
.path
.stuck_timer
= stuck_timeout
- 2
1285 -- frustration! cant find the damn path :(
1286 mob_sound(self
, self
.sounds
.random)
1289 mob_sound(self
, self
.sounds
.war_cry
)
1290 set_velocity(self
, self
.walk_velocity
)
1292 -- follow path now that it has it
1293 self
.path
.following
= true
1300 local specific_attack
= function(list
, what
)
1302 -- no list so attack default (player, animals etc.)
1307 -- found entity on list to attack?
1308 for no
= 1, #list
do
1310 if list
[no
] == what
then
1319 -- monster find someone to attack
1320 local monster_attack
= function(self
)
1322 if self
.type ~= "monster"
1323 or not damage_enabled
1325 or self
.state
== "attack"
1326 or day_docile(self
) then
1330 local s
= self
.object
:get_pos()
1332 local player
, obj
, min_player
1333 local type, name
= "", ""
1334 local min_dist
= self
.view_range
+ 1
1335 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1339 if objs
[n
]:is_player() then
1341 if mobs
.invis
[ objs
[n
]:get_player_name() ] then
1350 obj
= objs
[n
]:get_luaentity()
1355 name
= obj
.name
or ""
1359 -- find specific mob to attack, failing that attack player/npc/animal
1360 if specific_attack(self
.specific_attack
, name
)
1361 and (type == "player" or type == "npc"
1362 or (type == "animal" and self
.attack_animals
== true)) then
1364 p
= player
:get_pos()
1367 dist
= get_distance(p
, s
)
1369 -- aim higher to make looking up hills more realistic
1374 -- choose closest player to attack
1376 and line_of_sight(self
, sp
, p
, 2) == true then
1385 do_attack(self
, min_player
)
1390 -- npc, find closest monster to attack
1391 local npc_attack
= function(self
)
1393 if self
.type ~= "npc"
1394 or not self
.attacks_monsters
1395 or self
.state
== "attack" then
1399 local p
, sp
, obj
, min_player
1400 local s
= self
.object
:get_pos()
1401 local min_dist
= self
.view_range
+ 1
1402 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1406 obj
= objs
[n
]:get_luaentity()
1408 if obj
and obj
.type == "monster" then
1410 p
= obj
.object
:get_pos()
1413 local dist
= get_distance(p
, s
)
1415 -- aim higher to make looking up hills more realistic
1420 and line_of_sight(self
, sp
, p
, 2) == true then
1422 min_player
= obj
.object
1428 do_attack(self
, min_player
)
1434 local specific_runaway
= function(list
, what
)
1436 -- no list so do not run
1441 -- found entity on list to attack?
1442 for no
= 1, #list
do
1444 if list
[no
] == what
then
1453 -- find someone to runaway from
1454 local runaway_from
= function(self
)
1456 if not self
.runaway_from
then
1460 local s
= self
.object
:get_pos()
1462 local player
, obj
, min_player
1463 local type, name
= "", ""
1464 local min_dist
= self
.view_range
+ 1
1465 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1469 if objs
[n
]:is_player() then
1471 if mobs
.invis
[ objs
[n
]:get_player_name() ]
1472 or self
.owner
== objs
[n
]:get_player_name() then
1481 obj
= objs
[n
]:get_luaentity()
1486 name
= obj
.name
or ""
1490 -- find specific mob to runaway from
1491 if name
~= "" and name
~= self
.name
1492 and specific_runaway(self
.runaway_from
, name
) then
1494 p
= player
:get_pos()
1497 -- aim higher to make looking up hills more realistic
1501 dist
= get_distance(p
, s
)
1504 -- choose closest player/mpb to runaway from
1506 and line_of_sight(self
, sp
, p
, 2) == true then
1515 local lp
= player
:get_pos()
1522 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
1528 yaw
= set_yaw(self
, yaw
, 4)
1529 self
.state
= "runaway"
1530 self
.runaway_timer
= 3
1531 self
.following
= nil
1536 -- follow player if owner or holding item, if fish outta water then flop
1537 local follow_flop
= function(self
)
1539 -- find player to follow
1540 if (self
.follow
~= ""
1541 or self
.order
== "follow")
1542 and not self
.following
1543 and self
.state
~= "attack"
1544 and self
.state
~= "runaway" then
1546 local s
= self
.object
:get_pos()
1547 local players
= minetest
.get_connected_players()
1549 for n
= 1, #players
do
1551 if get_distance(players
[n
]:get_pos(), s
) < self
.view_range
1552 and not mobs
.invis
[ players
[n
]:get_player_name() ] then
1554 self
.following
= players
[n
]
1561 if self
.type == "npc"
1562 and self
.order
== "follow"
1563 and self
.state
~= "attack"
1564 and self
.owner
~= "" then
1566 -- npc stop following player if not owner
1569 and self
.owner
~= self
.following
:get_player_name() then
1570 self
.following
= nil
1573 -- stop following player if not holding specific item
1575 and self
.following
:is_player()
1576 and follow_holding(self
, self
.following
) == false then
1577 self
.following
= nil
1582 -- follow that thing
1583 if self
.following
then
1585 local s
= self
.object
:get_pos()
1588 if self
.following
:is_player() then
1590 p
= self
.following
:get_pos()
1592 elseif self
.following
.object
then
1594 p
= self
.following
.object
:get_pos()
1599 local dist
= get_distance(p
, s
)
1601 -- dont follow if out of range
1602 if dist
> self
.view_range
then
1603 self
.following
= nil
1610 local yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1612 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
1614 yaw
= set_yaw(self
, yaw
, 6)
1616 -- anyone but standing npc's can move along
1617 if dist
> self
.reach
1618 and self
.order
~= "stand" then
1620 set_velocity(self
, self
.walk_velocity
)
1622 if self
.walk_chance
~= 0 then
1623 set_animation(self
, "walk")
1626 set_velocity(self
, 0)
1627 set_animation(self
, "stand")
1635 -- swimmers flop when out of their element, and swim again when back in
1637 local s
= self
.object
:get_pos()
1638 if not flight_check(self
, s
) then
1641 self
.object
:setvelocity({x
= 0, y
= -5, z
= 0})
1643 set_animation(self
, "stand")
1646 elseif self
.state
== "flop" then
1647 self
.state
= "stand"
1653 -- dogshoot attack switch and counter function
1654 local dogswitch
= function(self
, dtime
)
1656 -- switch mode not activated
1657 if not self
.dogshoot_switch
1662 self
.dogshoot_count
= self
.dogshoot_count
+ dtime
1664 if (self
.dogshoot_switch
== 1
1665 and self
.dogshoot_count
> self
.dogshoot_count_max
)
1666 or (self
.dogshoot_switch
== 2
1667 and self
.dogshoot_count
> self
.dogshoot_count2_max
) then
1669 self
.dogshoot_count
= 0
1671 if self
.dogshoot_switch
== 1 then
1672 self
.dogshoot_switch
= 2
1674 self
.dogshoot_switch
= 1
1678 return self
.dogshoot_switch
1682 -- execute current state (stand, walk, run, attacks)
1683 local do_states
= function(self
, dtime
)
1685 local yaw
= self
.object
:get_yaw() or 0
1687 if self
.state
== "stand" then
1689 if random(1, 4) == 1 then
1692 local s
= self
.object
:get_pos()
1693 local objs
= minetest
.get_objects_inside_radius(s
, 3)
1697 if objs
[n
]:is_player() then
1698 lp
= objs
[n
]:get_pos()
1703 -- look at any players nearby, otherwise turn randomly
1711 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1713 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1715 yaw
= yaw
+ random(-0.5, 0.5)
1718 yaw
= set_yaw(self
, yaw
, 8)
1721 set_velocity(self
, 0)
1722 set_animation(self
, "stand")
1724 -- npc's ordered to stand stay standing
1725 if self
.type ~= "npc"
1726 or self
.order
~= "stand" then
1728 if self
.walk_chance
~= 0
1729 and self
.facing_fence
~= true
1730 and random(1, 100) <= self
.walk_chance
1731 and is_at_cliff(self
) == false then
1733 set_velocity(self
, self
.walk_velocity
)
1735 set_animation(self
, "walk")
1739 elseif self
.state
== "walk" then
1741 local s
= self
.object
:get_pos()
1744 -- is there something I need to avoid?
1745 if self
.water_damage
> 0
1746 and self
.lava_damage
> 0 then
1748 lp
= minetest
.find_node_near(s
, 1, {"group:water", "group:lava"})
1750 elseif self
.water_damage
> 0 then
1752 lp
= minetest
.find_node_near(s
, 1, {"group:water"})
1754 elseif self
.lava_damage
> 0 then
1756 lp
= minetest
.find_node_near(s
, 1, {"group:lava"})
1761 -- if mob in water or lava then look for land
1762 if (self
.lava_damage
1763 and minetest
.registered_nodes
[self
.standing_in
].groups
.lava
)
1764 or (self
.water_damage
1765 and minetest
.registered_nodes
[self
.standing_in
].groups
.water
) then
1767 lp
= minetest
.find_node_near(s
, 5, {"group:soil", "group:stone",
1768 "group:sand", node_ice
, node_snowblock
})
1770 -- did we find land?
1778 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1780 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1782 -- look towards land and jump/move in that direction
1783 yaw
= set_yaw(self
, yaw
, 6)
1785 set_velocity(self
, self
.walk_velocity
)
1787 yaw
= yaw
+ random(-0.5, 0.5)
1797 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1799 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1802 yaw
= set_yaw(self
, yaw
, 8)
1804 -- otherwise randomly turn
1805 elseif random(1, 100) <= 30 then
1807 yaw
= yaw
+ random(-0.5, 0.5)
1809 yaw
= set_yaw(self
, yaw
, 8)
1812 -- stand for great fall in front
1813 local temp_is_cliff
= is_at_cliff(self
)
1815 if self
.facing_fence
== true
1817 or random(1, 100) <= 30 then
1819 set_velocity(self
, 0)
1820 self
.state
= "stand"
1821 set_animation(self
, "stand")
1823 set_velocity(self
, self
.walk_velocity
)
1825 if flight_check(self
)
1827 and self
.animation
.fly_start
1828 and self
.animation
.fly_end
then
1829 set_animation(self
, "fly")
1831 set_animation(self
, "walk")
1835 -- runaway when punched
1836 elseif self
.state
== "runaway" then
1838 self
.runaway_timer
= self
.runaway_timer
+ 1
1840 -- stop after 5 seconds or when at cliff
1841 if self
.runaway_timer
> 5
1842 or is_at_cliff(self
) then
1843 self
.runaway_timer
= 0
1844 set_velocity(self
, 0)
1845 self
.state
= "stand"
1846 set_animation(self
, "stand")
1848 set_velocity(self
, self
.run_velocity
)
1849 set_animation(self
, "walk")
1852 -- attack routines (explode, dogfight, shoot, dogshoot)
1853 elseif self
.state
== "attack" then
1855 -- calculate distance from mob and enemy
1856 local s
= self
.object
:get_pos()
1857 local p
= self
.attack
:get_pos() or s
1858 local dist
= get_distance(p
, s
)
1860 -- stop attacking if player invisible or out of range
1861 if dist
> self
.view_range
1863 or not self
.attack
:get_pos()
1864 or self
.attack
:get_hp() <= 0
1865 or (self
.attack
:is_player() and mobs
.invis
[ self
.attack
:get_player_name() ]) then
1867 self
.state
= "stand"
1868 set_velocity(self
, 0)
1869 set_animation(self
, "stand")
1871 self
.v_start
= false
1879 if self
.attack_type
== "explode" then
1886 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1888 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
1890 yaw
= set_yaw(self
, yaw
)
1892 local node_break_radius
= self
.explosion_radius
or 1
1893 local entity_damage_radius
= self
.explosion_damage_radius
1894 or (node_break_radius
* 2)
1896 -- start timer when in reach and line of sight
1898 and dist
<= self
.reach
1899 and line_of_sight(self
, s
, p
, 2) then
1904 mob_sound(self
, self
.sounds
.fuse
)
1906 -- stop timer if out of reach or direct line of sight
1907 elseif self
.allow_fuse_reset
1909 and (dist
> self
.reach
1910 or not line_of_sight(self
, s
, p
, 2)) then
1911 self
.v_start
= false
1914 self
.blinkstatus
= false
1915 self
.object
:settexturemod("")
1918 -- walk right up to player unless the timer is active
1919 if self
.v_start
and (self
.stop_to_explode
or dist
< 1.5) then
1920 set_velocity(self
, 0)
1922 set_velocity(self
, self
.run_velocity
)
1925 if self
.animation
and self
.animation
.run_start
then
1926 set_animation(self
, "run")
1928 set_animation(self
, "walk")
1931 if self
.v_start
then
1933 self
.timer
= self
.timer
+ dtime
1934 self
.blinktimer
= (self
.blinktimer
or 0) + dtime
1936 if self
.blinktimer
> 0.2 then
1940 if self
.blinkstatus
then
1941 self
.object
:settexturemod("")
1943 self
.object
:settexturemod("^[brighten")
1946 self
.blinkstatus
= not self
.blinkstatus
1949 if self
.timer
> self
.explosion_timer
then
1951 local pos
= self
.object
:get_pos()
1953 -- dont damage anything if area protected or next to water
1954 if minetest
.find_node_near(pos
, 1, {"group:water"})
1955 or minetest
.is_protected(pos
, "") then
1957 node_break_radius
= 1
1960 self
.object
:remove()
1962 if mobs_griefing
and mod_tnt
and tnt
and tnt
.boom
1963 and not minetest
.is_protected(pos
, "") then
1966 radius
= node_break_radius
,
1967 damage_radius
= entity_damage_radius
,
1968 sound
= self
.sounds
.explode
,
1972 minetest
.sound_play(self
.sounds
.explode
, {
1975 max_hear_distance
= self
.sounds
.distance
or 32
1978 entity_physics(pos
, entity_damage_radius
)
1979 effect(pos
, 32, "tnt_smoke.png", nil, nil, node_break_radius
, 1, 0)
1986 elseif self
.attack_type
== "dogfight"
1987 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 2)
1988 or (self
.attack_type
== "dogshoot" and dist
<= self
.reach
and dogswitch(self
) == 0) then
1991 and dist
> self
.reach
then
1994 local me_y
= floor(p1
.y
)
1996 local p_y
= floor(p2
.y
+ 1)
1997 local v
= self
.object
:getvelocity()
1999 if flight_check(self
, s
) then
2003 self
.object
:setvelocity({
2005 y
= 1 * self
.walk_velocity
,
2009 elseif me_y
> p_y
then
2011 self
.object
:setvelocity({
2013 y
= -1 * self
.walk_velocity
,
2020 self
.object
:setvelocity({
2026 elseif me_y
> p_y
then
2028 self
.object
:setvelocity({
2038 -- rnd: new movement direction
2039 if self
.path
.following
2041 and self
.attack_type
~= "dogshoot" then
2043 -- no paths longer than 50
2044 if #self
.path
.way
> 50
2045 or dist
< self
.reach
then
2046 self
.path
.following
= false
2050 local p1
= self
.path
.way
[1]
2053 self
.path
.following
= false
2057 if abs(p1
.x
-s
.x
) + abs(p1
.z
- s
.z
) < 0.6 then
2058 -- reached waypoint, remove it from queue
2059 table.remove(self
.path
.way
, 1)
2062 -- set new temporary target
2063 p
= {x
= p1
.x
, y
= p1
.y
, z
= p1
.z
}
2071 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2073 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2075 yaw
= set_yaw(self
, yaw
)
2077 -- move towards enemy if beyond mob reach
2078 if dist
> self
.reach
then
2080 -- path finding by rnd
2081 if self
.pathfinding
-- only if mob has pathfinding enabled
2082 and enable_pathfinding
then
2084 smart_mobs(self
, s
, p
, dist
, dtime
)
2087 if is_at_cliff(self
) then
2089 set_velocity(self
, 0)
2090 set_animation(self
, "stand")
2093 if self
.path
.stuck
then
2094 set_velocity(self
, self
.walk_velocity
)
2096 set_velocity(self
, self
.run_velocity
)
2099 if self
.animation
and self
.animation
.run_start
then
2100 set_animation(self
, "run")
2102 set_animation(self
, "walk")
2106 else -- rnd: if inside reach range
2108 self
.path
.stuck
= false
2109 self
.path
.stuck_timer
= 0
2110 self
.path
.following
= false -- not stuck anymore
2112 set_velocity(self
, 0)
2114 if not self
.custom_attack
then
2116 if self
.timer
> 1 then
2120 if self
.double_melee_attack
2121 and random(1, 2) == 1 then
2122 set_animation(self
, "punch2")
2124 set_animation(self
, "punch")
2133 if line_of_sight(self
, p2
, s2
) == true then
2135 -- play attack sound
2136 mob_sound(self
, self
.sounds
.attack
)
2138 -- punch player (or what player is attached to)
2139 local attached
= self
.attack
:get_attach()
2141 self
.attack
= attached
2143 self
.attack
:punch(self
.object
, 1.0, {
2144 full_punch_interval
= 1.0,
2145 damage_groups
= {fleshy
= self
.damage
}
2149 else -- call custom attack every second
2150 if self
.custom_attack
2151 and self
.timer
> 1 then
2155 self
.custom_attack(self
, p
)
2160 elseif self
.attack_type
== "shoot"
2161 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 1)
2162 or (self
.attack_type
== "dogshoot" and dist
> self
.reach
and dogswitch(self
) == 0) then
2167 local dist
= get_distance(p
, s
)
2174 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2176 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2178 yaw
= set_yaw(self
, yaw
)
2180 set_velocity(self
, 0)
2182 if self
.shoot_interval
2183 and self
.timer
> self
.shoot_interval
2184 and random(1, 100) <= 60 then
2187 set_animation(self
, "shoot")
2189 -- play shoot attack sound
2190 mob_sound(self
, self
.sounds
.shoot_attack
)
2192 local p
= self
.object
:get_pos()
2194 p
.y
= p
.y
+ (self
.collisionbox
[2] + self
.collisionbox
[5]) / 2
2196 if minetest
.registered_entities
[self
.arrow
] then
2198 local obj
= minetest
.add_entity(p
, self
.arrow
)
2199 local ent
= obj
:get_luaentity()
2200 local amount
= (vec
.x
* vec
.x
+ vec
.y
* vec
.y
+ vec
.z
* vec
.z
) ^
0.5
2201 local v
= ent
.velocity
or 1 -- or set to default
2204 ent
.owner_id
= tostring(self
.object
) -- add unique owner id to arrow
2206 -- offset makes shoot aim accurate
2207 vec
.y
= vec
.y
+ self
.shoot_offset
2208 vec
.x
= vec
.x
* (v
/ amount
)
2209 vec
.y
= vec
.y
* (v
/ amount
)
2210 vec
.z
= vec
.z
* (v
/ amount
)
2212 obj
:setvelocity(vec
)
2220 -- falling and fall damage
2221 local falling
= function(self
, pos
)
2227 -- floating in water (or falling)
2228 local v
= self
.object
:getvelocity()
2232 -- apply gravity when moving up
2233 self
.object
:setacceleration({
2239 elseif v
.y
<= 0 and v
.y
> self
.fall_speed
then
2241 -- fall downwards at set speed
2242 self
.object
:setacceleration({
2244 y
= self
.fall_speed
,
2248 -- stop accelerating once max fall speed hit
2249 self
.object
:setacceleration({x
= 0, y
= 0, z
= 0})
2252 -- in water then float up
2253 if minetest
.registered_nodes
[node_ok(pos
).name
].groups
.water
then
2255 if self
.floats
== 1 then
2257 self
.object
:setacceleration({
2259 y
= -self
.fall_speed
/ (max(1, v
.y
) ^
2),
2265 -- fall damage onto solid ground
2266 if self
.fall_damage
== 1
2267 and self
.object
:getvelocity().y
== 0 then
2269 local d
= (self
.old_y
or 0) - self
.object
:get_pos().y
2273 self
.health
= self
.health
- floor(d
- 5)
2275 effect(pos
, 5, "tnt_smoke.png", 1, 2, 2, nil)
2277 if check_for_death(self
, "fall", {type = "fall"}) then
2282 self
.old_y
= self
.object
:get_pos().y
2288 -- deal damage and effects when mob punched
2289 local mob_punch
= function(self
, hitter
, tflp
, tool_capabilities
, dir
)
2291 -- custom punch function
2292 if self
.do_punch
then
2294 -- when false skip going any further
2295 if self
.do_punch(self
, hitter
, tflp
, tool_caps
, dir
) == false then
2300 -- error checking when mod profiling is enabled
2301 if not tool_capabilities
then
2302 minetest
.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2306 -- is mob protected?
2307 if self
.protected
and hitter
:is_player()
2308 and minetest
.is_protected(self
.object
:get_pos(), hitter
:get_player_name()) then
2309 minetest
.chat_send_player(hitter
:get_player_name(), S("Mob has been protected!"))
2315 local weapon
= hitter
:get_wielded_item()
2316 local punch_interval
= 1.4
2318 -- calculate mob damage
2320 local armor
= self
.object
:get_armor_groups() or {}
2323 -- quick error check incase it ends up 0 (serialize.h check test)
2329 damage
= cmi
.calculate_damage(self
.object
, hitter
, tflp
, tool_capabilities
, dir
)
2332 for group
,_
in pairs( (tool_capabilities
.damage_groups
or {}) ) do
2334 tmp
= tflp
/ (tool_capabilities
.full_punch_interval
or 1.4)
2342 damage
= damage
+ (tool_capabilities
.damage_groups
[group
] or 0)
2343 * tmp
* ((armor
[group
] or 0) / 100.0)
2347 -- check for tool immunity or special damage
2348 for n
= 1, #self
.immune_to
do
2350 if self
.immune_to
[n
][1] == weapon
:get_name() then
2352 damage
= self
.immune_to
[n
][2] or 0
2358 if damage
<= -1 then
2359 self
.health
= self
.health
- floor(damage
)
2365 local cancel
= cmi
.notify_punch(self
.object
, hitter
, tflp
, tool_capabilities
, dir
, damage
)
2367 if cancel
then return end
2371 if tool_capabilities
then
2372 punch_interval
= tool_capabilities
.full_punch_interval
or 1.4
2375 if weapon
:get_definition()
2376 and weapon
:get_definition().tool_capabilities
then
2378 weapon
:add_wear(floor((punch_interval
/ 75) * 9000))
2379 hitter
:set_wielded_item(weapon
)
2382 -- only play hit sound and show blood effects if damage is 1 or over
2386 if weapon
:get_definition().sounds
~= nil then
2388 local s
= random(0, #weapon
:get_definition().sounds
)
2390 minetest
.sound_play(weapon
:get_definition().sounds
[s
], {
2391 object
= self
.object
, --hitter,
2392 max_hear_distance
= 8
2395 minetest
.sound_play("default_punch", {
2396 object
= self
.object
, --hitter,
2397 max_hear_distance
= 5
2402 if self
.blood_amount
> 0
2403 and not disable_blood
then
2405 local pos
= self
.object
:get_pos()
2407 pos
.y
= pos
.y
+ (-self
.collisionbox
[2] + self
.collisionbox
[5]) * .5
2409 -- do we have a single blood texture or multiple?
2410 if type(self
.blood_texture
) == "table" then
2412 local blood
= self
.blood_texture
[random(1, #self
.blood_texture
)]
2414 effect(pos
, self
.blood_amount
, blood
, nil, nil, 1, nil)
2416 effect(pos
, self
.blood_amount
, self
.blood_texture
, nil, nil, 1, nil)
2421 self
.health
= self
.health
- floor(damage
)
2423 -- exit here if dead, special item check
2424 if weapon
:get_name() == "mobs:pick_lava" then
2425 if check_for_death(self
, "lava", {type = "punch",
2426 puncher
= hitter
}) then
2430 if check_for_death(self
, "hit", {type = "punch",
2431 puncher
= hitter
}) then
2436 -- knock back effect (only on full punch)
2438 and tflp
>= punch_interval
then
2440 local v
= self
.object
:getvelocity()
2441 local r
= 1.4 - min(punch_interval
, 1.4)
2445 -- if already in air then dont go up anymore when hit
2451 -- direction error check
2452 dir
= dir
or {x
= 0, y
= 0, z
= 0}
2454 -- check if tool already has specific knockback value
2455 if tool_capabilities
.damage_groups
["knockback"] then
2456 kb
= tool_capabilities
.damage_groups
["knockback"]
2461 self
.object
:setvelocity({
2467 self
.pause_timer
= 0.25
2469 end -- END if damage
2471 -- if skittish then run away
2472 if self
.runaway
== true then
2474 local lp
= hitter
:get_pos()
2475 local s
= self
.object
:get_pos()
2482 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
2488 yaw
= set_yaw(self
, yaw
, 6)
2489 self
.state
= "runaway"
2490 self
.runaway_timer
= 0
2491 self
.following
= nil
2494 local name
= hitter
:get_player_name() or ""
2496 -- attack puncher and call other mobs for help
2497 if self
.passive
== false
2498 and self
.state
~= "flop"
2499 and self
.child
== false
2500 and hitter
:get_player_name() ~= self
.owner
2501 and not mobs
.invis
[ name
] then
2503 -- attack whoever punched mob
2505 do_attack(self
, hitter
)
2507 -- alert others to the attack
2508 local objs
= minetest
.get_objects_inside_radius(hitter
:get_pos(), self
.view_range
)
2513 obj
= objs
[n
]:get_luaentity()
2517 -- only alert members of same mob
2518 if obj
.group_attack
== true
2519 and obj
.state
~= "attack"
2520 and obj
.owner
~= name
2521 and obj
.name
== self
.name
then
2522 do_attack(obj
, hitter
)
2525 -- have owned mobs attack player threat
2526 if obj
.owner
== name
and obj
.owner_loyal
then
2527 do_attack(obj
, self
.object
)
2535 -- get entity staticdata
2536 local mob_staticdata
= function(self
)
2538 -- remove mob when out of range unless tamed
2541 and self
.type ~= "npc"
2542 and self
.state
~= "attack"
2544 and self
.lifetimer
< 20000 then
2546 self
.object
:remove()
2551 self
.remove_ok
= true
2553 self
.following
= nil
2554 self
.state
= "stand"
2556 -- used to rotate older mobs
2558 and self
.drawtype
== "side" then
2559 self
.rotate
= math
.rad(90)
2563 self
.serialized_cmi_components
= cmi
.serialize_components(self
._cmi_components
)
2568 for _
,stat
in pairs(self
) do
2570 local t
= type(stat
)
2575 and _
~= "_cmi_components" then
2580 return minetest
.serialize(tmp
)
2584 -- activate mob and reload settings
2585 local mob_activate
= function(self
, staticdata
, def
, dtime
)
2587 -- remove monsters in peaceful mode
2588 if self
.type == "monster"
2589 and peaceful_only
then
2591 self
.object
:remove()
2596 -- load entity variables
2597 local tmp
= minetest
.deserialize(staticdata
)
2600 for _
,stat
in pairs(tmp
) do
2605 -- select random texture, set model and size
2606 if not self
.base_texture
then
2608 -- compatiblity with old simple mobs textures
2609 if type(def
.textures
[1]) == "string" then
2610 def
.textures
= {def
.textures
}
2613 self
.base_texture
= def
.textures
[random(1, #def
.textures
)]
2614 self
.base_mesh
= def
.mesh
2615 self
.base_size
= self
.visual_size
2616 self
.base_colbox
= self
.collisionbox
2617 self
.base_selbox
= self
.selectionbox
2620 -- for current mobs that dont have this set
2621 if not self
.base_selbox
then
2622 self
.base_selbox
= self
.selectionbox
or self
.base_colbox
2625 -- set texture, model and size
2626 local textures
= self
.base_texture
2627 local mesh
= self
.base_mesh
2628 local vis_size
= self
.base_size
2629 local colbox
= self
.base_colbox
2630 local selbox
= self
.base_selbox
2632 -- specific texture if gotten
2633 if self
.gotten
== true
2634 and def
.gotten_texture
then
2635 textures
= def
.gotten_texture
2638 -- specific mesh if gotten
2639 if self
.gotten
== true
2640 and def
.gotten_mesh
then
2641 mesh
= def
.gotten_mesh
2644 -- set child objects to half size
2645 if self
.child
== true then
2648 x
= self
.base_size
.x
* .5,
2649 y
= self
.base_size
.y
* .5,
2652 if def
.child_texture
then
2653 textures
= def
.child_texture
[1]
2657 self
.base_colbox
[1] * .5,
2658 self
.base_colbox
[2] * .5,
2659 self
.base_colbox
[3] * .5,
2660 self
.base_colbox
[4] * .5,
2661 self
.base_colbox
[5] * .5,
2662 self
.base_colbox
[6] * .5
2665 self
.base_selbox
[1] * .5,
2666 self
.base_selbox
[2] * .5,
2667 self
.base_selbox
[3] * .5,
2668 self
.base_selbox
[4] * .5,
2669 self
.base_selbox
[5] * .5,
2670 self
.base_selbox
[6] * .5
2674 if self
.health
== 0 then
2675 self
.health
= random (self
.hp_min
, self
.hp_max
)
2680 self
.path
.way
= {} -- path to follow, table of positions
2681 self
.path
.lastpos
= {x
= 0, y
= 0, z
= 0}
2682 self
.path
.stuck
= false
2683 self
.path
.following
= false -- currently following path?
2684 self
.path
.stuck_timer
= 0 -- if stuck for too long search for path
2687 self
.object
:set_armor_groups({immortal
= 1, fleshy
= self
.armor
})
2688 self
.old_y
= self
.object
:get_pos().y
2689 self
.old_health
= self
.health
2690 self
.sounds
.distance
= self
.sounds
.distance
or 10
2691 self
.textures
= textures
2693 self
.collisionbox
= colbox
2694 self
.selectionbox
= selbox
2695 self
.visual_size
= vis_size
2696 self
.standing_in
= ""
2698 -- check existing nametag
2699 if not self
.nametag
then
2700 self
.nametag
= def
.nametag
2703 -- set anything changed above
2704 self
.object
:set_properties(self
)
2705 set_yaw(self
, (random(0, 360) - 180) / 180 * pi
, 6)
2707 set_animation(self
, "stand")
2709 -- run on_spawn function if found
2710 if self
.on_spawn
and not self
.on_spawn_run
then
2711 if self
.on_spawn(self
) then
2712 self
.on_spawn_run
= true -- if true, set flag to run once only
2716 -- run after_activate
2717 if def
.after_activate
then
2718 def
.after_activate(self
, staticdata
, def
, dtime
)
2722 self
._cmi_components
= cmi
.activate_components(self
.serialized_cmi_components
)
2723 cmi
.notify_activate(self
.object
, dtime
)
2728 -- main mob function
2729 local mob_step
= function(self
, dtime
)
2732 cmi
.notify_step(self
.object
, dtime
)
2735 local pos
= self
.object
:get_pos()
2738 -- when lifetimer expires remove mob (except npc and tamed)
2739 if self
.type ~= "npc"
2741 and self
.state
~= "attack"
2742 and remove_far
~= true
2743 and self
.lifetimer
< 20000 then
2745 self
.lifetimer
= self
.lifetimer
- dtime
2747 if self
.lifetimer
<= 0 then
2749 -- only despawn away from player
2750 local objs
= minetest
.get_objects_inside_radius(pos
, 15)
2754 if objs
[n
]:is_player() then
2762 effect(pos
, 15, "tnt_smoke.png", 2, 4, 2, 0)
2764 self
.object
:remove()
2772 -- smooth rotation by ThomasMonroe314
2774 if self
.delay
and self
.delay
> 0 then
2776 local yaw
= self
.object
:get_yaw()
2778 if self
.delay
== 1 then
2779 yaw
= self
.target_yaw
2781 local dif
= abs(yaw
- self
.target_yaw
)
2783 if yaw
> self
.target_yaw
then
2786 dif
= 2 * pi
- dif
-- need to add
2787 yaw
= yaw
+ dif
/ self
.delay
2789 yaw
= yaw
- dif
/ self
.delay
-- need to subtract
2792 elseif yaw
< self
.target_yaw
then
2796 yaw
= yaw
- dif
/ self
.delay
-- need to subtract
2798 yaw
= yaw
+ dif
/ self
.delay
-- need to add
2802 if yaw
> (pi
* 2) then yaw
= yaw
- (pi
* 2) end
2803 if yaw
< 0 then yaw
= yaw
+ (pi
* 2) end
2806 self
.delay
= self
.delay
- 1
2807 self
.object
:set_yaw(yaw
)
2813 if self
.pause_timer
> 0 then
2815 self
.pause_timer
= self
.pause_timer
- dtime
2820 -- run custom function (defined in mob lua file)
2821 if self
.do_custom
then
2823 -- when false skip going any further
2824 if self
.do_custom(self
, dtime
) == false then
2830 self
.timer
= self
.timer
+ dtime
2832 if self
.state
~= "attack" then
2834 if self
.timer
< 1 then
2841 -- never go over 100
2842 if self
.timer
> 100 then
2846 -- mob plays random sound at times
2847 if random(1, 100) == 1 then
2848 mob_sound(self
, self
.sounds
.random)
2851 -- environmental damage timer (every 1 second)
2852 self
.env_damage_timer
= self
.env_damage_timer
+ dtime
2854 if (self
.state
== "attack" and self
.env_damage_timer
> 1)
2855 or self
.state
~= "attack" then
2857 self
.env_damage_timer
= 0
2859 -- check for environmental damage (water, fire, lava etc.)
2862 -- node replace check (cow eats grass etc.)
2866 monster_attack(self
)
2874 do_states(self
, dtime
)
2883 -- default function when mobs are blown up with TNT
2884 local do_tnt
= function(obj
, damage
)
2886 obj
.object
:punch(obj
.object
, 1.0, {
2887 full_punch_interval
= 1.0,
2888 damage_groups
= {fleshy
= damage
},
2891 return false, true, {}
2895 mobs
.spawning_mobs
= {}
2897 -- Code to execute before custom on_rightclick handling
2898 local on_rightclick_prefix
= function(self
, clicker
)
2899 local item
= clicker
:get_wielded_item()
2901 -- Name mob with nametag
2902 if not self
.ignores_nametag
and item
:get_name() == "mcl_mobs:nametag" then
2904 local tag = item
:get_meta():get_string("name")
2906 if string.len(tag) > MAX_MOB_NAME_LENGTH
then
2907 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH
)
2913 if not mobs
.is_creative(clicker
:get_player_name()) then
2915 clicker
:set_wielded_item(item
)
2924 local create_mob_on_rightclick
= function(on_rightclick
)
2925 return function(self
, clicker
)
2926 local stop
= on_rightclick_prefix(self
, clicker
)
2927 if (not stop
) and (on_rightclick
) then
2928 on_rightclick(self
, clicker
)
2933 -- register mob entity
2934 function mobs
:register_mob(name
, def
)
2936 mobs
.spawning_mobs
[name
] = true
2938 minetest
.register_entity(name
, {
2940 stepheight
= def
.stepheight
or 1.1, -- was 0.6
2943 attack_type
= def
.attack_type
,
2945 fly_in
= def
.fly_in
or "air",
2946 owner
= def
.owner
or "",
2947 order
= def
.order
or "",
2948 on_die
= def
.on_die
,
2949 do_custom
= def
.do_custom
,
2950 jump_height
= def
.jump_height
or 4, -- was 6
2951 drawtype
= def
.drawtype
, -- DEPRECATED, use rotate instead
2952 rotate
= math
.rad(def
.rotate
or 0), -- 0=front, 90=side, 180=back, 270=side2
2953 lifetimer
= def
.lifetimer
or 180, -- 3 minutes
2954 hp_min
= max(1, (def
.hp_min
or 5) * difficulty
),
2955 hp_max
= max(1, (def
.hp_max
or 10) * difficulty
),
2957 collisionbox
= def
.collisionbox
or {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25},
2958 selectionbox
= def
.selectionbox
or def
.collisionbox
,
2959 visual
= def
.visual
,
2960 visual_size
= def
.visual_size
or {x
= 1, y
= 1},
2962 makes_footstep_sound
= def
.makes_footstep_sound
or false,
2963 view_range
= def
.view_range
or 5,
2964 walk_velocity
= def
.walk_velocity
or 1,
2965 run_velocity
= def
.run_velocity
or 2,
2966 damage
= max(0, (def
.damage
or 0) * difficulty
),
2967 light_damage
= def
.light_damage
or 0,
2968 sunlight_damage
= def
.sunlight_damage
or 0,
2969 water_damage
= def
.water_damage
or 0,
2970 lava_damage
= def
.lava_damage
or 0,
2971 suffocation
= def
.suffocation
or 2,
2972 fall_damage
= def
.fall_damage
or 1,
2973 fall_speed
= def
.fall_speed
or -10, -- must be lower than -2 (default: -10)
2974 drops
= def
.drops
or {},
2975 armor
= def
.armor
or 100,
2976 on_rightclick
= create_mob_on_rightclick(def
.on_rightclick
),
2978 shoot_interval
= def
.shoot_interval
,
2979 sounds
= def
.sounds
or {},
2980 animation
= def
.animation
,
2981 follow
= def
.follow
,
2982 jump
= def
.jump
~= false,
2983 walk_chance
= def
.walk_chance
or 50,
2984 attacks_monsters
= def
.attacks_monsters
or false,
2985 group_attack
= def
.group_attack
or false,
2986 passive
= def
.passive
or false,
2987 knock_back
= def
.knock_back
~= false,
2988 blood_amount
= def
.blood_amount
or 5,
2989 blood_texture
= def
.blood_texture
or "mobs_blood.png",
2990 shoot_offset
= def
.shoot_offset
or 0,
2991 floats
= def
.floats
or 1, -- floats in water by default
2992 replace_rate
= def
.replace_rate
,
2993 replace_what
= def
.replace_what
,
2994 replace_with
= def
.replace_with
,
2995 replace_offset
= def
.replace_offset
or 0,
2996 on_replace
= def
.on_replace
,
2998 env_damage_timer
= 0, -- only used when state = "attack"
3006 reach
= def
.reach
or 3,
3008 texture_list
= def
.textures
,
3009 child_texture
= def
.child_texture
,
3010 docile_by_day
= def
.docile_by_day
or false,
3012 fear_height
= def
.fear_height
or 0,
3013 runaway
= def
.runaway
,
3015 pathfinding
= def
.pathfinding
,
3016 immune_to
= def
.immune_to
or {},
3017 explosion_radius
= def
.explosion_radius
,
3018 explosion_damage_radius
= def
.explosion_damage_radius
,
3019 explosion_timer
= def
.explosion_timer
or 3,
3020 allow_fuse_reset
= def
.allow_fuse_reset
~= false,
3021 stop_to_explode
= def
.stop_to_explode
~= false,
3022 custom_attack
= def
.custom_attack
,
3023 double_melee_attack
= def
.double_melee_attack
,
3024 dogshoot_switch
= def
.dogshoot_switch
,
3026 dogshoot_count_max
= def
.dogshoot_count_max
or 5,
3027 dogshoot_count2_max
= def
.dogshoot_count2_max
or (def
.dogshoot_count_max
or 5),
3028 attack_animals
= def
.attack_animals
or false,
3029 specific_attack
= def
.specific_attack
,
3030 runaway_from
= def
.runaway_from
,
3031 owner_loyal
= def
.owner_loyal
,
3032 facing_fence
= false,
3036 ignores_nametag
= def
.ignores_nametag
or false,
3037 rain_damage
= def
.rain_damage
or 0,
3039 on_spawn
= def
.on_spawn
,
3041 on_blast
= def
.on_blast
or do_tnt
,
3045 do_punch
= def
.do_punch
,
3047 on_punch
= mob_punch
,
3049 on_breed
= def
.on_breed
,
3051 on_grown
= def
.on_grown
,
3053 on_activate
= function(self
, staticdata
, dtime
)
3054 return mob_activate(self
, staticdata
, def
, dtime
)
3057 get_staticdata
= function(self
)
3058 return mob_staticdata(self
)
3063 end -- END mobs:register_mob function
3066 -- count how many mobs of one type are inside an area
3067 local count_mobs
= function(pos
, type)
3071 local objs
= minetest
.get_objects_inside_radius(pos
, aoc_range
)
3075 if not objs
[n
]:is_player() then
3077 local obj
= objs
[n
]:get_luaentity()
3079 -- count mob type and add to total also
3080 if obj
and obj
.name
and obj
.name
== type then
3082 num_type
= num_type
+ 1
3083 num_total
= num_total
+ 1
3085 -- add to total mobs
3086 elseif obj
and obj
.name
and obj
.health
~= nil then
3088 num_total
= num_total
+ 1
3093 return num_type
, num_total
3099 function mobs
:spawn_abm_check(pos
, node
, name
)
3100 -- global function to add additional spawn checks
3101 -- return true to stop spawning mob
3105 function mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
,
3106 interval
, chance
, aoc
, min_height
, max_height
, day_toggle
, on_spawn
)
3108 -- Do mobs spawn at all?
3109 if not mobs_spawn
then
3113 -- chance/spawn number override in minetest.conf for registered mob
3114 local numbers
= minetest
.settings
:get(name
)
3117 numbers
= numbers
:split(",")
3118 chance
= tonumber(numbers
[1]) or chance
3119 aoc
= tonumber(numbers
[2]) or aoc
3122 minetest
.log("warning", string.format("[mobs] %s has spawning disabled", name
))
3126 minetest
.log("action",
3127 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name
, chance
, aoc
))
3131 minetest
.register_abm({
3133 label
= name
.. " spawning",
3135 neighbors
= neighbors
,
3136 interval
= interval
,
3137 chance
= max(1, (chance
* mob_chance_multiplier
)),
3140 action
= function(pos
, node
, active_object_count
, active_object_count_wider
)
3142 -- is mob actually registered?
3143 if not mobs
.spawning_mobs
[name
]
3144 or not minetest
.registered_entities
[name
] then
3148 -- additional custom checks for spawning mob
3149 if mobs
:spawn_abm_check(pos
, node
, name
) == true then
3153 -- do not spawn if too many of same mob in area
3154 if active_object_count_wider
>= max_per_block
3155 or count_mobs(pos
, name
) >= aoc
then
3156 -- too many entities
3160 -- if toggle set to nil then ignore day/night check
3161 if day_toggle
~= nil then
3163 local tod
= (minetest
.get_timeofday() or 0) * 24000
3165 if tod
> 4500 and tod
< 19500 then
3166 -- daylight, but mob wants night
3167 if day_toggle
== false then
3172 -- night time but mob wants day
3173 if day_toggle
== true then
3183 -- only spawn away from player
3184 local objs
= minetest
.get_objects_inside_radius(pos
, 10)
3188 if objs
[n
]:is_player() then
3194 -- mobs cannot spawn in protected areas when enabled
3195 if not spawn_protected
3196 and minetest
.is_protected(pos
, "") then
3200 -- are we spawning within height limits?
3201 if pos
.y
> max_height
3202 or pos
.y
< min_height
then
3206 -- are light levels ok?
3207 local light
= minetest
.get_node_light(pos
)
3209 or light
> max_light
3210 or light
< min_light
then
3214 -- do we have enough height clearance to spawn mob?
3215 local ent
= minetest
.registered_entities
[name
]
3216 local height
= max(0, math
.ceil(ent
.collisionbox
[5] - ent
.collisionbox
[2]) - 1)
3218 for n
= 0, height
do
3220 local pos2
= {x
= pos
.x
, y
= pos
.y
+ n
, z
= pos
.z
}
3222 if minetest
.registered_nodes
[node_ok(pos2
).name
].walkable
== true then
3228 -- spawn mob half block higher than ground
3231 local mob
= minetest
.add_entity(pos
, name
)
3235 local ent
= mob
:get_luaentity()
3244 -- compatibility with older mob registration
3245 function mobs
:register_spawn(name
, nodes
, max_light
, min_light
, chance
, active_object_count
, max_height
, day_toggle
)
3247 mobs
:spawn_specific(name
, nodes
, {"air"}, min_light
, max_light
, 30,
3248 chance
, active_object_count
, -31000, max_height
, day_toggle
)
3252 -- MarkBu's spawn function
3253 function mobs
:spawn(def
)
3255 local name
= def
.name
3256 local nodes
= def
.nodes
or {"group:soil", "group:stone"}
3257 local neighbors
= def
.neighbors
or {"air"}
3258 local min_light
= def
.min_light
or 0
3259 local max_light
= def
.max_light
or 15
3260 local interval
= def
.interval
or 30
3261 local chance
= def
.chance
or 5000
3262 local active_object_count
= def
.active_object_count
or 1
3263 local min_height
= def
.min_height
or -31000
3264 local max_height
= def
.max_height
or 31000
3265 local day_toggle
= def
.day_toggle
3266 local on_spawn
= def
.on_spawn
3268 mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
, interval
,
3269 chance
, active_object_count
, min_height
, max_height
, day_toggle
, on_spawn
)
3273 -- register arrow for shoot attack
3274 function mobs
:register_arrow(name
, def
)
3276 if not name
or not def
then return end -- errorcheck
3278 minetest
.register_entity(name
, {
3281 visual
= def
.visual
,
3282 visual_size
= def
.visual_size
,
3283 textures
= def
.textures
,
3284 velocity
= def
.velocity
,
3285 hit_player
= def
.hit_player
,
3286 hit_node
= def
.hit_node
,
3287 hit_mob
= def
.hit_mob
,
3288 drop
= def
.drop
or false, -- drops arrow as registered item when true
3289 collisionbox
= {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3292 owner_id
= def
.owner_id
,
3293 rotate
= def
.rotate
,
3294 automatic_face_movement_dir
= def
.rotate
3295 and (def
.rotate
- (pi
/ 180)) or false,
3297 on_activate
= def
.on_activate
,
3299 on_step
= def
.on_step
or function(self
, dtime
)
3301 self
.timer
= self
.timer
+ 1
3303 local pos
= self
.object
:get_pos()
3307 or not within_limits(pos
, 0) then
3309 self
.object
:remove();
3314 -- does arrow have a tail (fireball)
3317 and def
.tail_texture
then
3319 minetest
.add_particle({
3321 velocity
= {x
= 0, y
= 0, z
= 0},
3322 acceleration
= {x
= 0, y
= 0, z
= 0},
3323 expirationtime
= def
.expire
or 0.25,
3324 collisiondetection
= false,
3325 texture
= def
.tail_texture
,
3326 size
= def
.tail_size
or 5,
3327 glow
= def
.glow
or 0,
3331 if self
.hit_node
then
3333 local node
= node_ok(pos
).name
3335 if minetest
.registered_nodes
[node
].walkable
then
3337 self
.hit_node(self
, pos
, node
)
3339 if self
.drop
== true then
3343 self
.lastpos
= (self
.lastpos
or pos
)
3345 minetest
.add_item(self
.lastpos
, self
.object
:get_luaentity().name
)
3348 self
.object
:remove();
3354 if self
.hit_player
or self
.hit_mob
then
3356 for _
,player
in pairs(minetest
.get_objects_inside_radius(pos
, 1.0)) do
3359 and player
:is_player() then
3361 self
.hit_player(self
, player
)
3362 self
.object
:remove();
3366 local entity
= player
:get_luaentity()
3370 and entity
._cmi_is_mob
== true
3371 and tostring(player
) ~= self
.owner_id
3372 and entity
.name
~= self
.object
:get_luaentity().name
then
3374 self
.hit_mob(self
, player
)
3376 self
.object
:remove();
3389 -- compatibility function
3390 function mobs
:explosion(pos
, radius
)
3391 local self
= {sounds
= {}}
3392 self
.sounds
.explode
= "tnt_explode"
3393 mobs
:boom(self
, pos
, radius
)
3397 -- no damage to nodes explosion
3398 function mobs
:safe_boom(self
, pos
, radius
)
3400 minetest
.sound_play(self
.sounds
and self
.sounds
.explode
or "tnt_explode", {
3403 max_hear_distance
= self
.sounds
and self
.sounds
.distance
or 32
3406 entity_physics(pos
, radius
)
3407 effect(pos
, 32, "tnt_smoke.png", radius
* 3, radius
* 5, radius
, 1, 0)
3411 -- make explosion with protection and tnt mod check
3412 function mobs
:boom(self
, pos
, radius
)
3415 and mod_tnt
and tnt
and tnt
.boom
3416 and not minetest
.is_protected(pos
, "") then
3420 damage_radius
= radius
,
3421 sound
= self
.sounds
and self
.sounds
.explode
,
3422 explode_center
= true,
3425 mobs
:safe_boom(self
, pos
, radius
)
3430 -- Register spawn eggs
3432 -- Note: This also introduces the “spawn_egg” group:
3433 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3434 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3435 function mobs
:register_egg(mob
, desc
, background
, addegg
, no_creative
)
3437 local grp
= {spawn_egg
= 1}
3439 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3440 if creative
and no_creative
== true then
3441 grp
.not_in_creative_inventory
= 1
3444 local invimg
= background
3447 invimg
= "mobs_chicken_egg.png^(" .. invimg
..
3448 "^[mask:mobs_chicken_egg_overlay.png)"
3451 -- register old stackable mob egg
3452 minetest
.register_craftitem(mob
, {
3455 inventory_image
= invimg
,
3458 _doc_items_longdesc
= "This allows you to place a single mob.",
3459 _doc_items_usagehelp
= "Just place it where you want the mob to appear. Animals will spawn tamed, unless you hold down the sneak key while placing. If you place this on a mob spawner, you change the mob it spawns.",
3461 on_place
= function(itemstack
, placer
, pointed_thing
)
3463 local pos
= pointed_thing
.above
3465 -- am I clicking on something with existing on_rightclick function?
3466 local under
= minetest
.get_node(pointed_thing
.under
)
3467 local def
= minetest
.registered_nodes
[under
.name
]
3468 if def
and def
.on_rightclick
then
3469 return def
.on_rightclick(pointed_thing
.under
, under
, placer
, itemstack
)
3473 and within_limits(pos
, 0)
3474 and not minetest
.is_protected(pos
, placer
:get_player_name()) then
3476 local name
= placer
:get_player_name()
3477 local privs
= minetest
.get_player_privs(name
)
3478 if not privs
.maphack
then
3479 minetest
.chat_send_player(name
, "You need the “maphack” privilege to change the mob spawner.")
3482 if mod_mobspawners
and under
.name
== "mcl_mobspawners:spawner" then
3483 mcl_mobspawners
.setup_spawner(pointed_thing
.under
, itemstack
:get_name())
3484 if not minetest
.settings
:get_bool("creative_mode") then
3485 itemstack
:take_item()
3490 if not minetest
.registered_entities
[mob
] then
3496 local mob
= minetest
.add_entity(pos
, mob
)
3497 local ent
= mob
:get_luaentity()
3499 -- don't set owner if monster or sneak pressed
3500 if ent
.type ~= "monster"
3501 and not placer
:get_player_control().sneak
then
3502 ent
.owner
= placer
:get_player_name()
3507 local nametag
= itemstack
:get_meta():get_string("name")
3508 if nametag
~= "" then
3509 if string.len(nametag
) > MAX_MOB_NAME_LENGTH
then
3510 nametag
= string.sub(nametag
, 1, MAX_MOB_NAME_LENGTH
)
3512 ent
.nametag
= nametag
3516 -- if not in creative then take item
3517 if not mobs
.is_creative(placer
:get_player_name()) then
3518 itemstack
:take_item()
3529 -- No-op in MCL2 (capturing mobs is not possible).
3530 -- Provided for compability with Mobs Redo
3531 function mobs
:capture_mob(self
, clicker
, chance_hand
, chance_net
, chance_lasso
, force_take
, replacewith
)
3536 -- protect tamed mob with rune item
3537 function mobs
:protect(self
, clicker
)
3538 local name
= clicker
:get_player_name()
3539 local tool
= clicker
:get_wielded_item()
3541 if tool
:get_name() ~= "mobs:protector" then
3545 if self
.tamed
== false then
3546 minetest
.chat_send_player(name
, S("Not tamed!"))
3547 return true -- false
3550 if self
.protected
== true then
3551 minetest
.chat_send_player(name
, S("Already protected!"))
3552 return true -- false
3555 if not mobs
.is_creative(clicker
:get_player_name()) then
3556 tool
:take_item() -- take 1 protection rune
3557 clicker
:set_wielded_item(tool
)
3560 self
.protected
= true
3562 local pos
= self
.object
:get_pos()
3563 pos
.y
= pos
.y
+ self
.collisionbox
[2] + 0.5
3565 effect(self
.object
:get_pos(), 25, "mobs_protect_particle.png", 0.5, 4, 2, 15)
3567 mob_sound(self
, "mobs_spell")
3576 -- feeding, taming and breeding (thanks blert2112)
3577 function mobs
:feed_tame(self
, clicker
, feed_count
, breed
, tame
)
3578 if not self
.follow
then
3582 -- can eat/tame with item in hand
3583 if follow_holding(self
, clicker
) then
3585 -- if not in creative then take item
3586 if not mobs
.is_creative(clicker
:get_player_name()) then
3588 local item
= clicker
:get_wielded_item()
3592 clicker
:set_wielded_item(item
)
3596 self
.health
= self
.health
+ 4
3598 if self
.health
>= self
.hp_max
then
3600 self
.health
= self
.hp_max
3602 if self
.htimer
< 1 then
3607 self
.object
:set_hp(self
.health
)
3611 -- make children grow quicker
3612 if self
.child
== true then
3614 self
.hornytimer
= self
.hornytimer
+ 20
3620 self
.food
= (self
.food
or 0) + 1
3621 if self
.food
>= feed_count
then
3625 if breed
and self
.hornytimer
== 0 then
3635 if not self
.owner
or self
.owner
== "" then
3636 self
.owner
= clicker
:get_player_name()
3640 -- make sound when fed so many times
3641 mob_sound(self
, self
.sounds
.random)
3651 function mobs
:spawn_child(pos
, mob_type
)
3652 local child
= minetest
.add_entity(pos
, mob_type
)
3657 local ent
= child
:get_luaentity()
3658 effect(pos
, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
3663 -- using specific child texture (if found)
3664 if ent
.child_texture
then
3665 textures
= ent
.child_texture
[1]
3668 -- and resize to half height
3669 child
:set_properties({
3670 textures
= textures
,
3672 x
= ent
.base_size
.x
* .5,
3673 y
= ent
.base_size
.y
* .5,
3676 ent
.base_colbox
[1] * .5,
3677 ent
.base_colbox
[2] * .5,
3678 ent
.base_colbox
[3] * .5,
3679 ent
.base_colbox
[4] * .5,
3680 ent
.base_colbox
[5] * .5,
3681 ent
.base_colbox
[6] * .5,
3684 ent
.base_selbox
[1] * .5,
3685 ent
.base_selbox
[2] * .5,
3686 ent
.base_selbox
[3] * .5,
3687 ent
.base_selbox
[4] * .5,
3688 ent
.base_selbox
[5] * .5,
3689 ent
.base_selbox
[6] * .5,
3697 -- compatibility function for old entities to new modpack entities
3698 function mobs
:alias_mob(old_name
, new_name
)
3701 minetest
.register_alias(old_name
, new_name
)
3704 minetest
.register_entity(":" .. old_name
, {
3708 on_step
= function(self
)
3710 if minetest
.registered_entities
[new_name
] then
3711 minetest
.add_entity(self
.object
:get_pos(), new_name
)
3714 self
.object
:remove()