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
17 local S
= minetest
.get_translator("mcl_mobs")
20 local use_cmi
= minetest
.global_exists("cmi")
23 -- Invisibility mod check
25 if minetest
.global_exists("invisibility") then
26 mobs
.invis
= invisibility
31 local creative_mode_cache
= minetest
.settings
:get_bool("creative_mode")
32 function mobs
.is_creative(name
)
33 return creative_mode_cache
or minetest
.check_player_privs(name
, {creative
= true})
37 -- localize math functions
44 local atann
= math
.atan
45 local random = math
.random
46 local floor = math
.floor
47 local atan = function(x
)
48 if not x
or x
~= x
then
57 local damage_enabled
= minetest
.settings
:get_bool("enable_damage")
58 local mobs_spawn
= minetest
.settings
:get_bool("mobs_spawn", true) ~= false
60 local disable_blood
= minetest
.settings
:get_bool("mobs_disable_blood")
61 local mobs_drop_items
= minetest
.settings
:get_bool("mobs_drop_items") ~= false
62 local mobs_griefing
= minetest
.settings
:get_bool("mobs_griefing") ~= false
63 local creative
= minetest
.settings
:get_bool("creative_mode")
64 local spawn_protected
= minetest
.settings
:get_bool("mobs_spawn_protected") ~= false
65 local remove_far
= false
66 local difficulty
= tonumber(minetest
.settings
:get("mob_difficulty")) or 1.0
67 local show_health
= false
68 local max_per_block
= tonumber(minetest
.settings
:get("max_objects_per_block") or 64)
69 local mobs_spawn_chance
= tonumber(minetest
.settings
:get("mobs_spawn_chance") or 2.5)
71 -- Peaceful mode message so players will know there are no monsters
72 if minetest
.settings
:get_bool("only_peaceful_mobs", false) then
73 minetest
.register_on_joinplayer(function(player
)
74 minetest
.chat_send_player(player
:get_player_name(),
75 S("Peaceful mode active! No monsters will spawn."))
79 -- calculate aoc range for mob count
80 local aosrb
= tonumber(minetest
.settings
:get("active_object_send_range_blocks"))
81 local abr
= tonumber(minetest
.settings
:get("active_block_range"))
82 local aoc_range
= max(aosrb
, abr
) * 16
84 -- pathfinding settings
85 local enable_pathfinding
= true
86 local stuck_timeout
= 3 -- how long before mob gets stuck in place and starts searching
87 local stuck_path_timeout
= 10 -- how long will mob follow path before giving up
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_explosions
= minetest
.get_modpath("mcl_explosions") ~= nil
97 local mod_mobspawners
= minetest
.get_modpath("mcl_mobspawners") ~= nil
98 local mod_hunger
= minetest
.get_modpath("mcl_hunger") ~= nil
99 local mod_worlds
= minetest
.get_modpath("mcl_worlds") ~= nil
100 local mod_armor
= minetest
.get_modpath("mcl_armor") ~= nil
103 local mob_sound
= function(self
, soundname
, is_opinion
, fixed_pitch
)
106 if self
.sounds_child
and self
.child
then
107 soundinfo
= self
.sounds_child
108 elseif self
.sounds
then
109 soundinfo
= self
.sounds
111 if not soundinfo
then
114 local sound
= soundinfo
[soundname
]
116 if is_opinion
and self
.opinion_sound_cooloff
> 0 then
120 if not fixed_pitch
then
121 local base_pitch
= soundinfo
.base_pitch
122 if not base_pitch
then
125 if self
.child
and (not self
.sounds_child
) then
126 -- Children have higher pitch
127 pitch
= base_pitch
* 1.5
131 -- randomize the pitch a bit
132 pitch
= pitch
+ math
.random(-10, 10) * 0.005
134 minetest
.sound_play(sound
, {
135 object
= self
.object
,
137 max_hear_distance
= self
.sounds
.distance
,
140 self
.opinion_sound_cooloff
= 1
144 -- Reeturn true if object is in view_range
145 local function object_in_range(self
, object
)
150 -- Apply view range reduction for special player armor
151 if object
:is_player() and mod_armor
then
152 factor
= armor
:get_mob_view_range_factor(object
, self
.name
)
156 if factor
and factor
== 0 then
159 dist
= self
.view_range
* factor
161 dist
= self
.view_range
163 if vector
.distance(self
.object
:get_pos(), object
:get_pos()) > dist
then
171 local do_attack
= function(self
, player
)
173 if self
.state
== "attack" then
178 self
.state
= "attack"
180 -- TODO: Implement war_cry sound without being annoying
181 --if random(0, 100) < 90 then
182 --mob_sound(self, "war_cry", true)
187 -- move mob in facing direction
188 local set_velocity
= function(self
, v
)
190 -- do not move if mob has been ordered to stay
191 if self
.order
== "stand" then
192 self
.object
:set_velocity({x
= 0, y
= 0, z
= 0})
196 local yaw
= (self
.object
:get_yaw() or 0) + self
.rotate
197 local vel
= self
.object
:get_velocity()
198 self
.object
:set_velocity({
200 y
= (vel
and vel
.y
) or 0,
206 -- calculate mob velocity
207 local get_velocity
= function(self
)
209 local v
= self
.object
:get_velocity()
211 return (v
.x
* v
.x
+ v
.z
* v
.z
) ^
0.5
215 -- set and return valid yaw
216 local set_yaw
= function(self
, yaw
, delay
)
218 if not yaw
or yaw
~= yaw
then
225 self
.object
:set_yaw(yaw
)
229 self
.target_yaw
= yaw
232 return self
.target_yaw
235 -- global function to set mob yaw
236 function mobs
:yaw(self
, yaw
, delay
)
237 set_yaw(self
, yaw
, delay
)
240 local add_texture_mod
= function(self
, mod)
242 local already_added
= false
243 for i
=1, #self
.texture_mods
do
244 if mod == self
.texture_mods
[i
] then
247 full_mod
= full_mod
.. self
.texture_mods
[i
]
249 if not already_added
then
250 full_mod
= full_mod
.. mod
251 table.insert(self
.texture_mods
, mod)
253 self
.object
:set_texture_mod(full_mod
)
255 local remove_texture_mod
= function(self
, mod)
258 for i
=1, #self
.texture_mods
do
259 if self
.texture_mods
[i
] ~= mod then
260 full_mod
= full_mod
.. self
.texture_mods
[i
]
262 table.insert(remove, i
)
266 table.remove(self
.texture_mods
, remove[i
])
268 self
.object
:set_texture_mod(full_mod
)
271 -- set defined animation
272 local set_animation
= function(self
, anim
)
274 if not self
.animation
275 or not anim
then return end
277 self
.animation
.current
= self
.animation
.current
or ""
279 if anim
== self
.animation
.current
280 or not self
.animation
[anim
.. "_start"]
281 or not self
.animation
[anim
.. "_end"] then
285 self
.animation
.current
= anim
287 self
.object
:set_animation({
288 x
= self
.animation
[anim
.. "_start"],
289 y
= self
.animation
[anim
.. "_end"]},
290 self
.animation
[anim
.. "_speed"] or self
.animation
.speed_normal
or 15,
291 0, self
.animation
[anim
.. "_loop"] ~= false)
295 -- above function exported for mount.lua
296 function mobs
:set_animation(self
, anim
)
297 set_animation(self
, anim
)
300 -- Returns true is node can deal damage to self
301 local is_node_dangerous
= function(self
, nodename
)
303 if self
.water_damage
> 0 then
304 if minetest
.get_item_group(nn
, "water") ~= 0 then
308 if self
.lava_damage
> 0 then
309 if minetest
.get_item_group(nn
, "lava") ~= 0 then
313 if self
.fire_damage
> 0 then
314 if minetest
.get_item_group(nn
, "fire") ~= 0 then
318 if minetest
.registered_nodes
[nn
].drowning
> 0 then
319 if self
.breath_max
~= -1 then
323 if minetest
.registered_nodes
[nn
].damage_per_second
> 0 then
330 -- check line of sight (BrunoMine)
331 local line_of_sight
= function(self
, pos1
, pos2
, stepsize
)
333 stepsize
= stepsize
or 1
335 local s
, pos
= minetest
.line_of_sight(pos1
, pos2
, stepsize
)
337 -- normal walking and flying mobs can see you through air
342 -- New pos1 to be analyzed
343 local npos1
= {x
= pos1
.x
, y
= pos1
.y
, z
= pos1
.z
}
345 local r
, pos
= minetest
.line_of_sight(npos1
, pos2
, stepsize
)
348 if r
== true then return true end
351 local nn
= minetest
.get_node(pos
).name
353 -- Target Distance (td) to travel
354 local td
= vector
.distance(pos1
, pos2
)
356 -- Actual Distance (ad) traveled
359 -- It continues to advance in the line of sight in search of a real
360 -- obstruction which counts as 'normal' nodebox.
361 while minetest
.registered_nodes
[nn
]
362 and minetest
.registered_nodes
[nn
].walkable
== false do
364 -- Check if you can still move forward
365 if td
< ad
+ stepsize
then
366 return true -- Reached the target
369 -- Moves the analyzed pos
370 local d
= vector
.distance(pos1
, pos2
)
372 npos1
.x
= ((pos2
.x
- pos1
.x
) / d
* stepsize
) + pos1
.x
373 npos1
.y
= ((pos2
.y
- pos1
.y
) / d
* stepsize
) + pos1
.y
374 npos1
.z
= ((pos2
.z
- pos1
.z
) / d
* stepsize
) + pos1
.z
378 or npos1
.x
~= npos1
.x
379 or npos1
.y
~= npos1
.y
380 or npos1
.z
~= npos1
.z
then
387 r
, pos
= minetest
.line_of_sight(npos1
, pos2
, stepsize
)
389 if r
== true then return true end
391 -- New Nodename found
392 nn
= minetest
.get_node(pos
).name
400 -- are we flying in what we are suppose to? (taikedz)
401 local flight_check
= function(self
)
403 local nod
= self
.standing_in
404 local def
= minetest
.registered_nodes
[nod
]
406 if not def
then return false end -- nil check
409 if type(self
.fly_in
) == "string" then
410 fly_in
= { self
.fly_in
}
411 elseif type(self
.fly_in
) == "table" then
417 for _
,checknode
in pairs(fly_in
) do
418 if nod
== checknode
then
420 elseif checknode
== "__airlike" and def
.walkable
== false and
421 (def
.liquidtype
== "none" or minetest
.get_item_group(nod
, "fake_liquid") == 1) then
430 -- custom particle effects
431 local effect
= function(pos
, amount
, texture
, min_size
, max_size
, radius
, gravity
, glow
, go_down
)
434 min_size
= min_size
or 0.5
435 max_size
= max_size
or 1
436 gravity
= gravity
or -10
438 go_down
= go_down
or false
447 minetest
.add_particlespawner({
452 minvel
= {x
= -radius
, y
= ym
, z
= -radius
},
453 maxvel
= {x
= radius
, y
= radius
, z
= radius
},
454 minacc
= {x
= 0, y
= gravity
, z
= 0},
455 maxacc
= {x
= 0, y
= gravity
, z
= 0},
465 local damage_effect
= function(self
, damage
)
467 if (not disable_blood
) and damage
> 0 then
469 local amount_large
= math
.floor(damage
/ 2)
470 local amount_small
= damage
% 2
472 local pos
= self
.object
:get_pos()
474 pos
.y
= pos
.y
+ (self
.collisionbox
[5] - self
.collisionbox
[2]) * .5
476 local texture
= "mobs_blood.png"
477 -- full heart damage (one particle for each 2 HP damage)
478 if amount_large
> 0 then
479 effect(pos
, amount_large
, texture
, 2, 2, 1.75, 0, nil, true)
481 -- half heart damage (one additional particle if damage is an odd number)
482 if amount_small
> 0 then
483 -- TODO: Use "half heart"
484 effect(pos
, amount_small
, texture
, 1, 1, 1.75, 0, nil, true)
489 local update_tag
= function(self
)
490 self
.object
:set_properties({
491 nametag
= self
.nametag
,
498 local item_drop
= function(self
, cooked
)
500 -- no drops if disabled by setting
501 if not mobs_drop_items
then return end
503 -- no drops for child mobs (except monster)
504 if (self
.child
and self
.type ~= "monster") then
509 local pos
= self
.object
:get_pos()
511 self
.drops
= self
.drops
or {} -- nil check
513 for n
= 1, #self
.drops
do
515 if random(1, self
.drops
[n
].chance
) == 1 then
517 num
= random(self
.drops
[n
].min or 1, self
.drops
[n
].max or 1)
518 item
= self
.drops
[n
].name
520 -- cook items when true
523 local output
= minetest
.get_craft_result({
524 method
= "cooking", width
= 1, items
= {item
}})
526 if output
and output
.item
and not output
.item
:is_empty() then
527 item
= output
.item
:get_name()
531 -- add item if it exists
532 obj
= minetest
.add_item(pos
, ItemStack(item
.. " " .. num
))
534 if obj
and obj
:get_luaentity() then
537 x
= random(-10, 10) / 9,
539 z
= random(-10, 10) / 9,
542 obj
:remove() -- item does not exist
551 -- check if mob is dead or only hurt
552 local check_for_death
= function(self
, cause
, cmi_cause
)
554 -- has health actually changed?
555 if self
.health
== self
.old_health
and self
.health
> 0 then
559 local damaged
= self
.health
< self
.old_health
560 self
.old_health
= self
.health
562 -- still got some health?
563 if self
.health
> 0 then
565 -- make sure health isn't higher than max
566 if self
.health
> self
.hp_max
then
567 self
.health
= self
.hp_max
570 -- play damage sound if health was reduced and make mob flash red.
572 add_texture_mod(self
, "^[colorize:#FF000040")
573 minetest
.after(.2, function(self
)
574 if self
and self
.object
then
575 remove_texture_mod(self
, "^[colorize:#FF000040")
578 mob_sound(self
, "damage")
581 -- backup nametag so we can show health stats
582 if not self
.nametag2
then
583 self
.nametag2
= self
.nametag
or ""
587 and (cmi_cause
and cmi_cause
.type == "punch") then
590 self
.nametag
= "♥ " .. self
.health
.. " / " .. self
.hp_max
598 -- dropped cooked item if mob died in fire or lava
599 if cause
== "lava" or cause
== "fire" then
600 item_drop(self
, true)
605 mob_sound(self
, "death")
607 local pos
= self
.object
:get_pos()
609 -- execute custom death function
612 self
.on_die(self
, pos
)
615 cmi
.notify_die(self
.object
, cmi_cause
)
623 -- default death function and die animation (if defined)
625 and self
.animation
.die_start
626 and self
.animation
.die_end
then
628 local frames
= self
.animation
.die_end
- self
.animation
.die_start
629 local speed
= self
.animation
.die_speed
or 15
630 local length
= max(frames
/ speed
, 0)
638 set_velocity(self
, 0)
639 set_animation(self
, "die")
641 minetest
.after(length
, function(self
)
642 if not self
.object
:get_luaentity() then
646 cmi
.notify_die(self
.object
, cmi_cause
)
654 cmi
.notify_die(self
.object
, cmi_cause
)
660 effect(pos
, 20, "tnt_smoke.png")
666 -- check if within physical map limits (-30911 to 30927)
667 local within_limits
= function(pos
, radius
)
669 if (pos
.x
- radius
) > -30913
670 and (pos
.x
+ radius
) < 30928
671 and (pos
.y
- radius
) > -30913
672 and (pos
.y
+ radius
) < 30928
673 and (pos
.z
- radius
) > -30913
674 and (pos
.z
+ radius
) < 30928 then
675 return true -- within limits
678 return false -- beyond limits
682 -- is mob facing a cliff or danger
683 local is_at_cliff_or_danger
= function(self
)
685 if self
.fear_height
== 0 then -- 0 for no falling protection!
689 if not self
.object
:get_luaentity() then
692 local yaw
= self
.object
:get_yaw()
693 local dir_x
= -sin(yaw
) * (self
.collisionbox
[4] + 0.5)
694 local dir_z
= cos(yaw
) * (self
.collisionbox
[4] + 0.5)
695 local pos
= self
.object
:get_pos()
696 local ypos
= pos
.y
+ self
.collisionbox
[2] -- just above floor
698 local free_fall
, blocker
= minetest
.line_of_sight(
699 {x
= pos
.x
+ dir_x
, y
= ypos
, z
= pos
.z
+ dir_z
},
700 {x
= pos
.x
+ dir_x
, y
= ypos
- self
.fear_height
, z
= pos
.z
+ dir_z
})
704 local bnode
= minetest
.get_node(blocker
)
705 local danger
= is_node_dangerous(self
, bnode
.name
)
709 local def
= minetest
.registered_nodes
[bnode
.name
]
710 return (not def
and def
.walkable
)
718 -- get node but use fallback for nil or unknown
719 local node_ok
= function(pos
, fallback
)
721 fallback
= fallback
or mobs
.fallback_node
723 local node
= minetest
.get_node_or_nil(pos
)
725 if node
and minetest
.registered_nodes
[node
.name
] then
729 return minetest
.registered_nodes
[fallback
]
733 -- environmental damage (water, lava, fire, light etc.)
734 local do_env_damage
= function(self
)
736 -- feed/tame text timer (so mob 'full' messages dont spam chat)
737 if self
.htimer
> 0 then
738 self
.htimer
= self
.htimer
- 1
741 -- reset nametag after showing health stats
742 if self
.htimer
< 1 and self
.nametag2
then
744 self
.nametag
= self
.nametag2
750 local pos
= self
.object
:get_pos()
752 self
.time_of_day
= minetest
.get_timeofday()
754 -- remove mob if beyond map limits
755 if not within_limits(pos
, 0) then
761 -- Deal light damage to mob, returns true if mob died
762 local deal_light_damage
= function(self
, pos
, damage
)
763 if not (mod_weather
and (mcl_weather
.rain
.raining
or mcl_weather
.state
== "snow") and mcl_weather
.is_outdoor(pos
)) then
764 self
.health
= self
.health
- damage
766 effect(pos
, 5, "tnt_smoke.png")
768 if check_for_death(self
, "light", {type = "light"}) then
774 -- bright light harms mob
775 if self
.light_damage
~= 0 and (minetest
.get_node_light(pos
) or 0) > 12 then
776 if deal_light_damage(self
, pos
, self
.light_damage
) then
780 local _
, dim
= nil, "overworld"
782 _
, dim
= mcl_worlds
.y_to_layer(pos
.y
)
784 if self
.sunlight_damage
~= 0 and (minetest
.get_node_light(pos
) or 0) >= minetest
.LIGHT_MAX
and dim
== "overworld" then
785 if deal_light_damage(self
, pos
, self
.sunlight_damage
) then
790 local y_level
= self
.collisionbox
[2]
793 y_level
= self
.collisionbox
[2] * 0.5
796 -- what is mob standing in?
797 pos
.y
= pos
.y
+ y_level
+ 0.25 -- foot level
798 local pos2
= {x
=pos
.x
, y
=pos
.y
-1, z
=pos
.z
}
799 self
.standing_in
= node_ok(pos
, "air").name
800 self
.standing_on
= node_ok(pos2
, "air").name
802 -- don't fall when on ignore, just stand still
803 if self
.standing_in
== "ignore" then
804 self
.object
:set_velocity({x
= 0, y
= 0, z
= 0})
807 local nodef
= minetest
.registered_nodes
[self
.standing_in
]
810 if self
.rain_damage
> 0 and mod_weather
then
811 if mcl_weather
.rain
.raining
and mcl_weather
.is_outdoor(pos
) then
813 self
.health
= self
.health
- self
.rain_damage
815 if check_for_death(self
, "rain", {type = "environment",
816 pos
= pos
, node
= self
.standing_in
}) then
822 pos
.y
= pos
.y
+ 1 -- for particle effect position
825 if self
.water_damage
> 0
826 and nodef
.groups
.water
then
828 if self
.water_damage
~= 0 then
830 self
.health
= self
.health
- self
.water_damage
832 effect(pos
, 5, "tnt_smoke.png", nil, nil, 1, nil)
834 if check_for_death(self
, "water", {type = "environment",
835 pos
= pos
, node
= self
.standing_in
}) then
841 elseif self
.lava_damage
> 0
842 and (nodef
.groups
.lava
) then
844 if self
.lava_damage
~= 0 then
846 self
.health
= self
.health
- self
.lava_damage
848 effect(pos
, 5, "fire_basic_flame.png", nil, nil, 1, nil)
850 if check_for_death(self
, "lava", {type = "environment",
851 pos
= pos
, node
= self
.standing_in
}) then
857 elseif self
.fire_damage
> 0
858 and (nodef
.groups
.fire
) then
860 if self
.fire_damage
~= 0 then
862 self
.health
= self
.health
- self
.fire_damage
864 effect(pos
, 5, "fire_basic_flame.png", nil, nil, 1, nil)
866 if check_for_death(self
, "fire", {type = "environment",
867 pos
= pos
, node
= self
.standing_in
}) then
872 -- damage_per_second node check
873 elseif nodef
.damage_per_second
~= 0 then
875 self
.health
= self
.health
- nodef
.damage_per_second
877 effect(pos
, 5, "tnt_smoke.png")
879 if check_for_death(self
, "dps", {type = "environment",
880 pos
= pos
, node
= self
.standing_in
}) then
886 if self
.breath_max
~= -1 then
887 local drowning
= false
888 if self
.breathes_in_water
then
889 if minetest
.get_item_group(self
.standing_in
, "water") == 0 then
892 elseif nodef
.drowning
> 0 then
897 self
.breath
= math
.max(0, self
.breath
- 1)
899 effect(pos
, 2, "bubble.png", nil, nil, 1, nil)
900 if self
.breath
<= 0 then
902 if nodef
.drowning
> 0 then
907 damage_effect(self
, dmg
)
908 self
.health
= self
.health
- dmg
910 if check_for_death(self
, "drowning", {type = "environment",
911 pos
= pos
, node
= self
.standing_in
}) then
915 self
.breath
= math
.min(self
.breath_max
, self
.breath
+ 1)
919 --- suffocation inside solid node
920 -- FIXME: Redundant with mcl_playerplus
921 if (self
.suffocation
== true)
922 and (nodef
.walkable
== nil or nodef
.walkable
== true)
923 and (nodef
.collision_box
== nil or nodef
.collision_box
.type == "regular")
924 and (nodef
.node_box
== nil or nodef
.node_box
.type == "regular")
925 and (nodef
.groups
.disable_suffocation
~= 1)
926 and (nodef
.groups
.opaque
== 1) then
928 -- 2 damage per second
929 -- TODO: Deal this damage once every 1/2 second
930 self
.health
= self
.health
- 2
932 if check_for_death(self
, "suffocation", {type = "environment",
933 pos
= pos
, node
= self
.standing_in
}) then
938 return check_for_death(self
, "", {type = "unknown"})
942 -- jump if facing a solid node (not fences or gates)
943 local do_jump
= function(self
)
946 or self
.jump_height
== 0
948 or (self
.child
and self
.type ~= "monster")
949 or self
.order
== "stand" then
953 self
.facing_fence
= false
955 -- something stopping us while moving?
956 if self
.state
~= "stand"
957 and get_velocity(self
) > 0.5
958 and self
.object
:get_velocity().y
~= 0 then
962 local pos
= self
.object
:get_pos()
963 local yaw
= self
.object
:get_yaw()
965 -- what is mob standing on?
966 pos
.y
= pos
.y
+ self
.collisionbox
[2] - 0.2
968 local nod
= node_ok(pos
)
970 if minetest
.registered_nodes
[nod
.name
].walkable
== false then
975 local dir_x
= -sin(yaw
) * (self
.collisionbox
[4] + 0.5)
976 local dir_z
= cos(yaw
) * (self
.collisionbox
[4] + 0.5)
978 -- what is in front of mob?
985 -- this is used to detect if there's a block on top of the block in front of the mob.
986 -- If there is, there is no point in jumping as we won't manage.
987 local nodTop
= node_ok({
993 -- we don't attempt to jump if there's a stack of blocks blocking
994 if minetest
.registered_nodes
[nodTop
.name
] == true then
998 -- thin blocks that do not need to be jumped
999 if nod
.name
== node_snow
then
1003 if self
.walk_chance
== 0
1004 or minetest
.registered_items
[nod
.name
].walkable
then
1006 if minetest
.get_item_group(nod
.name
, "fence") == 0
1007 and minetest
.get_item_group(nod
.name
, "fence_gate") == 0
1008 and minetest
.get_item_group(nod
.name
, "wall") == 0 then
1010 local v
= self
.object
:get_velocity()
1012 v
.y
= self
.jump_height
1014 set_animation(self
, "jump") -- only when defined
1016 self
.object
:set_velocity(v
)
1018 -- when in air move forward
1019 minetest
.after(0.3, function(self
, v
)
1020 if not self
.object
or not self
.object
:get_luaentity() then
1023 self
.object
:set_acceleration({
1030 if self
.jump_sound_cooloff
<= 0 then
1031 mob_sound(self
, "jump")
1032 self
.jump_sound_cooloff
= 0.5
1035 self
.facing_fence
= true
1038 -- if we jumped against a block/wall 4 times then turn
1039 if self
.object
:get_velocity().x
~= 0
1040 and self
.object
:get_velocity().z
~= 0 then
1042 self
.jump_count
= (self
.jump_count
or 0) + 1
1044 if self
.jump_count
== 4 then
1046 local yaw
= self
.object
:get_yaw() or 0
1048 yaw
= set_yaw(self
, yaw
+ 1.35, 8)
1061 -- blast damage to entities nearby (modified from TNT mod)
1062 local entity_physics
= function(pos
, radius
)
1066 local objs
= minetest
.get_objects_inside_radius(pos
, radius
)
1071 obj_pos
= objs
[n
]:get_pos()
1073 dist
= vector
.distance(pos
, obj_pos
)
1074 if dist
< 1 then dist
= 1 end
1076 local damage
= floor((4 / dist
) * radius
)
1077 local ent
= objs
[n
]:get_luaentity()
1079 -- punches work on entities AND players
1080 objs
[n
]:punch(objs
[n
], 1.0, {
1081 full_punch_interval
= 1.0,
1082 damage_groups
= {fleshy
= damage
},
1088 -- should mob follow what I'm holding ?
1089 local follow_holding
= function(self
, clicker
)
1091 if mobs
.invis
[clicker
:get_player_name()] then
1095 local item
= clicker
:get_wielded_item()
1096 local t
= type(self
.follow
)
1100 and item
:get_name() == self
.follow
then
1104 elseif t
== "table" then
1106 for no
= 1, #self
.follow
do
1108 if self
.follow
[no
] == item
:get_name() then
1118 -- find two animals of same type and breed if nearby and horny
1119 local breed
= function(self
)
1121 -- child takes 240 seconds before growing into adult
1122 if self
.child
== true then
1124 self
.hornytimer
= self
.hornytimer
+ 1
1126 if self
.hornytimer
> 240 then
1131 self
.object
:set_properties({
1132 textures
= self
.base_texture
,
1133 mesh
= self
.base_mesh
,
1134 visual_size
= self
.base_size
,
1135 collisionbox
= self
.base_colbox
,
1136 selectionbox
= self
.base_selbox
,
1139 -- custom function when child grows up
1140 if self
.on_grown
then
1143 -- jump when fully grown so as not to fall into ground
1144 self
.object
:set_velocity({
1146 y
= self
.jump_height
,
1155 -- horny animal can mate for 40 seconds,
1156 -- afterwards horny animal cannot mate again for 200 seconds
1157 if self
.horny
== true
1158 and self
.hornytimer
< 240 then
1160 self
.hornytimer
= self
.hornytimer
+ 1
1162 if self
.hornytimer
>= 240 then
1168 -- find another same animal who is also horny and mate if nearby
1169 if self
.horny
== true
1170 and self
.hornytimer
<= 40 then
1172 local pos
= self
.object
:get_pos()
1174 effect({x
= pos
.x
, y
= pos
.y
+ 1, z
= pos
.z
}, 8, "heart.png", 3, 4, 1, 0.1)
1176 local objs
= minetest
.get_objects_inside_radius(pos
, 3)
1182 ent
= objs
[n
]:get_luaentity()
1184 -- check for same animal with different colour
1185 local canmate
= false
1189 if ent
.name
== self
.name
then
1192 local entname
= string.split(ent
.name
,":")
1193 local selfname
= string.split(self
.name
,":")
1195 if entname
[1] == selfname
[1] then
1196 entname
= string.split(entname
[2],"_")
1197 selfname
= string.split(selfname
[2],"_")
1199 if entname
[1] == selfname
[1] then
1208 and ent
.horny
== true
1209 and ent
.hornytimer
<= 40 then
1213 -- found your mate? then have a baby
1216 self
.hornytimer
= 41
1220 minetest
.after(5, function(parent1
, parent2
, pos
)
1221 if not parent1
.object
:get_luaentity() then
1224 if not parent2
.object
:get_luaentity() then
1228 -- custom breed function
1229 if parent1
.on_breed
then
1230 -- when false, skip going any further
1231 if parent1
.on_breed(parent1
, parent2
) == false then
1236 local child
= mobs
:spawn_child(pos
, parent1
.name
)
1238 local ent_c
= child
:get_luaentity()
1241 -- Use texture of one of the parents
1242 local p
= math
.random(1, 2)
1244 ent_c
.base_texture
= parent1
.base_texture
1246 ent_c
.base_texture
= parent2
.base_texture
1248 child
:set_properties({
1249 textures
= ent_c
.base_texture
1252 -- tamed and owned by parents' owner
1254 ent_c
.owner
= parent1
.owner
1255 end, self
, ent
, pos
)
1266 -- find and replace what mob is looking for (grass, wheat etc.)
1267 local replace
= function(self
, pos
)
1269 if not self
.replace_rate
1270 or not self
.replace_what
1271 or self
.child
== true
1272 or self
.object
:get_velocity().y
~= 0
1273 or random(1, self
.replace_rate
) > 1 then
1277 local what
, with
, y_offset
1279 if type(self
.replace_what
[1]) == "table" then
1281 local num
= random(#self
.replace_what
)
1283 what
= self
.replace_what
[num
][1] or ""
1284 with
= self
.replace_what
[num
][2] or ""
1285 y_offset
= self
.replace_what
[num
][3] or 0
1287 what
= self
.replace_what
1288 with
= self
.replace_with
or ""
1289 y_offset
= self
.replace_offset
or 0
1292 pos
.y
= pos
.y
+ y_offset
1294 if #minetest
.find_nodes_in_area(pos
, pos
, what
) > 0 then
1296 local oldnode
= {name
= what
}
1297 local newnode
= {name
= with
}
1298 local on_replace_return
1300 if self
.on_replace
then
1301 on_replace_return
= self
.on_replace(self
, pos
, oldnode
, newnode
)
1304 if on_replace_return
~= false then
1306 if mobs_griefing
then
1307 minetest
.set_node(pos
, {name
= with
})
1315 -- check if daytime and also if mob is docile during daylight hours
1316 local day_docile
= function(self
)
1318 if self
.docile_by_day
== false then
1322 elseif self
.docile_by_day
== true
1323 and self
.time_of_day
> 0.2
1324 and self
.time_of_day
< 0.8 then
1331 local los_switcher
= false
1332 local height_switcher
= false
1334 -- path finding and smart mob routine by rnd, line_of_sight and other edits by Elkien3
1335 local smart_mobs
= function(self
, s
, p
, dist
, dtime
)
1337 local s1
= self
.path
.lastpos
1339 local target_pos
= self
.attack
:get_pos()
1341 -- is it becoming stuck?
1342 if abs(s1
.x
- s
.x
) + abs(s1
.z
- s
.z
) < .5 then
1343 self
.path
.stuck_timer
= self
.path
.stuck_timer
+ dtime
1345 self
.path
.stuck_timer
= 0
1348 self
.path
.lastpos
= {x
= s
.x
, y
= s
.y
, z
= s
.z
}
1350 local use_pathfind
= false
1351 local has_lineofsight
= minetest
.line_of_sight(
1352 {x
= s
.x
, y
= (s
.y
) + .5, z
= s
.z
},
1353 {x
= target_pos
.x
, y
= (target_pos
.y
) + 1.5, z
= target_pos
.z
}, .2)
1355 -- im stuck, search for path
1356 if not has_lineofsight
then
1358 if los_switcher
== true then
1360 los_switcher
= false
1361 end -- cannot see target!
1363 if los_switcher
== false then
1366 use_pathfind
= false
1368 minetest
.after(1, function(self
)
1369 if not self
.object
:get_luaentity() then
1372 if has_lineofsight
then self
.path
.following
= false end
1374 end -- can see target!
1377 if (self
.path
.stuck_timer
> stuck_timeout
and not self
.path
.following
) then
1380 self
.path
.stuck_timer
= 0
1382 minetest
.after(1, function(self
)
1383 if not self
.object
:get_luaentity() then
1386 if has_lineofsight
then self
.path
.following
= false end
1390 if (self
.path
.stuck_timer
> stuck_path_timeout
and self
.path
.following
) then
1393 self
.path
.stuck_timer
= 0
1395 minetest
.after(1, function(self
)
1396 if not self
.object
:get_luaentity() then
1399 if has_lineofsight
then self
.path
.following
= false end
1403 if math
.abs(vector
.subtract(s
,target_pos
).y
) > self
.stepheight
then
1405 if height_switcher
then
1407 height_switcher
= false
1410 if not height_switcher
then
1411 use_pathfind
= false
1412 height_switcher
= true
1416 if use_pathfind
then
1417 -- lets try find a path, first take care of positions
1418 -- since pathfinder is very sensitive
1419 local sheight
= self
.collisionbox
[5] - self
.collisionbox
[2]
1421 -- round position to center of node to avoid stuck in walls
1422 -- also adjust height for player models!
1423 s
.x
= floor(s
.x
+ 0.5)
1424 s
.z
= floor(s
.z
+ 0.5)
1426 local ssight
, sground
= minetest
.line_of_sight(s
, {
1427 x
= s
.x
, y
= s
.y
- 4, z
= s
.z
}, 1)
1429 -- determine node above ground
1434 local p1
= self
.attack
:get_pos()
1436 p1
.x
= floor(p1
.x
+ 0.5)
1437 p1
.y
= floor(p1
.y
+ 0.5)
1438 p1
.z
= floor(p1
.z
+ 0.5)
1440 local dropheight
= 12
1441 if self
.fear_height
~= 0 then dropheight
= self
.fear_height
end
1442 local jumpheight
= 0
1443 if self
.jump
and self
.jump_height
>= 4 then
1444 jumpheight
= math
.min(math
.ceil(self
.jump_height
/ 4), 4)
1445 elseif self
.stepheight
> 0.5 then
1448 self
.path
.way
= minetest
.find_path(s
, p1
, 16, jumpheight
, dropheight
, "A*_noprefetch")
1451 do_attack(self
, self
.attack
)
1453 -- no path found, try something else
1454 if not self
.path
.way
then
1456 self
.path
.following
= false
1458 -- lets make way by digging/building if not accessible
1459 if self
.pathfinding
== 2 and mobs_griefing
then
1461 -- is player higher than mob?
1465 if not minetest
.is_protected(s
, "") then
1467 local ndef1
= minetest
.registered_nodes
[self
.standing_in
]
1469 if ndef1
and (ndef1
.buildable_to
or ndef1
.groups
.liquid
) then
1471 minetest
.set_node(s
, {name
= mobs
.fallback_node
})
1475 local sheight
= math
.ceil(self
.collisionbox
[5]) + 1
1477 -- assume mob is 2 blocks high so it digs above its head
1480 -- remove one block above to make room to jump
1481 if not minetest
.is_protected(s
, "") then
1483 local node1
= node_ok(s
, "air").name
1484 local ndef1
= minetest
.registered_nodes
[node1
]
1487 and node1
~= "ignore"
1489 and not ndef1
.groups
.level
1490 and not ndef1
.groups
.unbreakable
1491 and not ndef1
.groups
.liquid
then
1493 minetest
.set_node(s
, {name
= "air"})
1494 minetest
.add_item(s
, ItemStack(node1
))
1500 self
.object
:set_pos({x
= s
.x
, y
= s
.y
+ 2, z
= s
.z
})
1502 else -- dig 2 blocks to make door toward player direction
1504 local yaw1
= self
.object
:get_yaw() + pi
/ 2
1506 x
= s
.x
+ cos(yaw1
),
1511 if not minetest
.is_protected(p1
, "") then
1513 local node1
= node_ok(p1
, "air").name
1514 local ndef1
= minetest
.registered_nodes
[node1
]
1517 and node1
~= "ignore"
1519 and not ndef1
.groups
.level
1520 and not ndef1
.groups
.unbreakable
1521 and not ndef1
.groups
.liquid
then
1523 minetest
.add_item(p1
, ItemStack(node1
))
1524 minetest
.set_node(p1
, {name
= "air"})
1528 node1
= node_ok(p1
, "air").name
1529 ndef1
= minetest
.registered_nodes
[node1
]
1532 and node1
~= "ignore"
1534 and not ndef1
.groups
.level
1535 and not ndef1
.groups
.unbreakable
1536 and not ndef1
.groups
.liquid
then
1538 minetest
.add_item(p1
, ItemStack(node1
))
1539 minetest
.set_node(p1
, {name
= "air"})
1546 -- will try again in 2 seconds
1547 self
.path
.stuck_timer
= stuck_timeout
- 2
1548 elseif s
.y
< p1
.y
and (not self
.fly
) then
1549 do_jump(self
) --add jump to pathfinding
1550 self
.path
.following
= true
1551 -- Yay, I found path!
1552 -- TODO: Implement war_cry sound without being annoying
1553 --mob_sound(self, "war_cry", true)
1555 set_velocity(self
, self
.walk_velocity
)
1557 -- follow path now that it has it
1558 self
.path
.following
= true
1565 local specific_attack
= function(list
, what
)
1567 -- no list so attack default (player, animals etc.)
1572 -- found entity on list to attack?
1573 for no
= 1, #list
do
1575 if list
[no
] == what
then
1583 -- monster find someone to attack
1584 local monster_attack
= function(self
)
1586 if self
.type ~= "monster"
1587 or not damage_enabled
1589 or self
.state
== "attack"
1590 or day_docile(self
) then
1594 local s
= self
.object
:get_pos()
1596 local player
, obj
, min_player
1597 local type, name
= "", ""
1598 local min_dist
= self
.view_range
+ 1
1599 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1603 if objs
[n
]:is_player() then
1605 if mobs
.invis
[ objs
[n
]:get_player_name() ] or (not object_in_range(self
, objs
[n
])) then
1613 obj
= objs
[n
]:get_luaentity()
1618 name
= obj
.name
or ""
1622 -- find specific mob to attack, failing that attack player/npc/animal
1623 if specific_attack(self
.specific_attack
, name
)
1624 and (type == "player" or type == "npc"
1625 or (type == "animal" and self
.attack_animals
== true)) then
1627 p
= player
:get_pos()
1630 dist
= vector
.distance(p
, s
)
1632 -- aim higher to make looking up hills more realistic
1637 -- choose closest player to attack
1639 and line_of_sight(self
, sp
, p
, 2) == true then
1648 do_attack(self
, min_player
)
1653 -- npc, find closest monster to attack
1654 local npc_attack
= function(self
)
1656 if self
.type ~= "npc"
1657 or not self
.attacks_monsters
1658 or self
.state
== "attack" then
1662 local p
, sp
, obj
, min_player
1663 local s
= self
.object
:get_pos()
1664 local min_dist
= self
.view_range
+ 1
1665 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1669 obj
= objs
[n
]:get_luaentity()
1671 if obj
and obj
.type == "monster" then
1673 p
= obj
.object
:get_pos()
1676 local dist
= vector
.distance(p
, s
)
1678 -- aim higher to make looking up hills more realistic
1683 and line_of_sight(self
, sp
, p
, 2) == true then
1685 min_player
= obj
.object
1691 do_attack(self
, min_player
)
1697 local specific_runaway
= function(list
, what
)
1699 -- no list so do not run
1704 -- found entity on list to attack?
1705 for no
= 1, #list
do
1707 if list
[no
] == what
then
1716 -- find someone to runaway from
1717 local runaway_from
= function(self
)
1719 if not self
.runaway_from
then
1723 local s
= self
.object
:get_pos()
1725 local player
, obj
, min_player
1726 local type, name
= "", ""
1727 local min_dist
= self
.view_range
+ 1
1728 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1732 if objs
[n
]:is_player() then
1734 if mobs
.invis
[ objs
[n
]:get_player_name() ]
1735 or self
.owner
== objs
[n
]:get_player_name()
1736 or (not object_in_range(self
, objs
[n
])) then
1744 obj
= objs
[n
]:get_luaentity()
1749 name
= obj
.name
or ""
1753 -- find specific mob to runaway from
1754 if name
~= "" and name
~= self
.name
1755 and specific_runaway(self
.runaway_from
, name
) then
1757 p
= player
:get_pos()
1760 -- aim higher to make looking up hills more realistic
1764 dist
= vector
.distance(p
, s
)
1767 -- choose closest player/mpb to runaway from
1769 and line_of_sight(self
, sp
, p
, 2) == true then
1778 local lp
= player
:get_pos()
1785 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
1791 yaw
= set_yaw(self
, yaw
, 4)
1792 self
.state
= "runaway"
1793 self
.runaway_timer
= 3
1794 self
.following
= nil
1799 -- follow player if owner or holding item, if fish outta water then flop
1800 local follow_flop
= function(self
)
1802 -- find player to follow
1803 if (self
.follow
~= ""
1804 or self
.order
== "follow")
1805 and not self
.following
1806 and self
.state
~= "attack"
1807 and self
.state
~= "runaway" then
1809 local s
= self
.object
:get_pos()
1810 local players
= minetest
.get_connected_players()
1812 for n
= 1, #players
do
1814 if (object_in_range(self
, players
[n
]))
1815 and not mobs
.invis
[ players
[n
]:get_player_name() ] then
1817 self
.following
= players
[n
]
1824 if self
.type == "npc"
1825 and self
.order
== "follow"
1826 and self
.state
~= "attack"
1827 and self
.owner
~= "" then
1829 -- npc stop following player if not owner
1832 and self
.owner
~= self
.following
:get_player_name() then
1833 self
.following
= nil
1836 -- stop following player if not holding specific item
1838 and self
.following
:is_player()
1839 and follow_holding(self
, self
.following
) == false then
1840 self
.following
= nil
1845 -- follow that thing
1846 if self
.following
then
1848 local s
= self
.object
:get_pos()
1851 if self
.following
:is_player() then
1853 p
= self
.following
:get_pos()
1855 elseif self
.following
.object
then
1857 p
= self
.following
.object
:get_pos()
1862 local dist
= vector
.distance(p
, s
)
1864 -- dont follow if out of range
1865 if (not object_in_range(self
, self
.following
)) then
1866 self
.following
= nil
1873 local yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1875 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
1877 set_yaw(self
, yaw
, 6)
1879 -- anyone but standing npc's can move along
1880 if dist
> self
.reach
1881 and self
.order
~= "stand" then
1883 set_velocity(self
, self
.walk_velocity
)
1885 if self
.walk_chance
~= 0 then
1886 set_animation(self
, "walk")
1889 set_velocity(self
, 0)
1890 set_animation(self
, "stand")
1898 -- swimmers flop when out of their element, and swim again when back in
1900 local s
= self
.object
:get_pos()
1901 if not flight_check(self
, s
) then
1904 self
.object
:set_velocity({x
= 0, y
= -5, z
= 0})
1906 set_animation(self
, "stand")
1909 elseif self
.state
== "flop" then
1910 self
.state
= "stand"
1916 -- dogshoot attack switch and counter function
1917 local dogswitch
= function(self
, dtime
)
1919 -- switch mode not activated
1920 if not self
.dogshoot_switch
1925 self
.dogshoot_count
= self
.dogshoot_count
+ dtime
1927 if (self
.dogshoot_switch
== 1
1928 and self
.dogshoot_count
> self
.dogshoot_count_max
)
1929 or (self
.dogshoot_switch
== 2
1930 and self
.dogshoot_count
> self
.dogshoot_count2_max
) then
1932 self
.dogshoot_count
= 0
1934 if self
.dogshoot_switch
== 1 then
1935 self
.dogshoot_switch
= 2
1937 self
.dogshoot_switch
= 1
1941 return self
.dogshoot_switch
1945 -- execute current state (stand, walk, run, attacks)
1946 -- returns true if mob has died
1947 local do_states
= function(self
, dtime
)
1949 local yaw
= self
.object
:get_yaw() or 0
1951 if self
.state
== "stand" then
1953 if random(1, 4) == 1 then
1956 local s
= self
.object
:get_pos()
1957 local objs
= minetest
.get_objects_inside_radius(s
, 3)
1961 if objs
[n
]:is_player() then
1962 lp
= objs
[n
]:get_pos()
1967 -- look at any players nearby, otherwise turn randomly
1975 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1977 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1979 yaw
= yaw
+ random(-0.5, 0.5)
1982 yaw
= set_yaw(self
, yaw
, 8)
1985 set_velocity(self
, 0)
1986 set_animation(self
, "stand")
1988 -- npc's ordered to stand stay standing
1989 if self
.type ~= "npc"
1990 or self
.order
~= "stand" then
1992 if self
.walk_chance
~= 0
1993 and self
.facing_fence
~= true
1994 and random(1, 100) <= self
.walk_chance
1995 and is_at_cliff_or_danger(self
) == false then
1997 set_velocity(self
, self
.walk_velocity
)
1999 set_animation(self
, "walk")
2003 elseif self
.state
== "walk" then
2005 local s
= self
.object
:get_pos()
2008 -- is there something I need to avoid?
2009 if (self
.water_damage
> 0
2010 and self
.lava_damage
> 0)
2011 or self
.breath_max
~= -1 then
2013 lp
= minetest
.find_node_near(s
, 1, {"group:water", "group:lava"})
2015 elseif self
.water_damage
> 0 then
2017 lp
= minetest
.find_node_near(s
, 1, {"group:water"})
2019 elseif self
.lava_damage
> 0 then
2021 lp
= minetest
.find_node_near(s
, 1, {"group:lava"})
2023 elseif self
.fire_damage
> 0 then
2025 lp
= minetest
.find_node_near(s
, 1, {"group:fire"})
2029 local is_in_danger
= false
2031 -- If mob in or on dangerous block, look for land
2032 if (is_node_dangerous(self
, self
.standing_in
) or
2033 is_node_dangerous(self
, self
.standing_on
)) then
2036 lp
= minetest
.find_node_near(s
, 5, {"group:solid"})
2038 -- did we find land?
2046 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2048 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
2050 -- look towards land and jump/move in that direction
2051 yaw
= set_yaw(self
, yaw
, 6)
2053 set_velocity(self
, self
.walk_velocity
)
2055 yaw
= yaw
+ random(-0.5, 0.5)
2058 -- A danger is near but mob is not inside
2062 if random(1, 100) <= 30 then
2063 yaw
= yaw
+ random(-0.5, 0.5)
2064 yaw
= set_yaw(self
, yaw
, 8)
2068 yaw
= set_yaw(self
, yaw
, 8)
2070 -- otherwise randomly turn
2071 elseif random(1, 100) <= 30 then
2073 yaw
= yaw
+ random(-0.5, 0.5)
2074 yaw
= set_yaw(self
, yaw
, 8)
2077 -- stand for great fall or danger or fence in front
2078 local cliff_or_danger
= false
2079 if is_in_danger
then
2080 cliff_or_danger
= is_at_cliff_or_danger(self
)
2082 if self
.facing_fence
== true
2084 or random(1, 100) <= 30 then
2086 set_velocity(self
, 0)
2087 self
.state
= "stand"
2088 set_animation(self
, "stand")
2091 set_velocity(self
, self
.walk_velocity
)
2093 if flight_check(self
)
2095 and self
.animation
.fly_start
2096 and self
.animation
.fly_end
then
2097 set_animation(self
, "fly")
2099 set_animation(self
, "walk")
2103 -- runaway when punched
2104 elseif self
.state
== "runaway" then
2106 self
.runaway_timer
= self
.runaway_timer
+ 1
2108 -- stop after 5 seconds or when at cliff
2109 if self
.runaway_timer
> 5
2110 or is_at_cliff_or_danger(self
) then
2111 self
.runaway_timer
= 0
2112 set_velocity(self
, 0)
2113 self
.state
= "stand"
2114 set_animation(self
, "stand")
2116 set_velocity(self
, self
.run_velocity
)
2117 set_animation(self
, "walk")
2120 -- attack routines (explode, dogfight, shoot, dogshoot)
2121 elseif self
.state
== "attack" then
2123 -- calculate distance from mob and enemy
2124 local s
= self
.object
:get_pos()
2125 local p
= self
.attack
:get_pos() or s
2126 local dist
= vector
.distance(p
, s
)
2128 -- stop attacking if player invisible or out of range
2130 or not self
.attack
:get_pos()
2131 or not object_in_range(self
, self
.attack
)
2132 or self
.attack
:get_hp() <= 0
2133 or (self
.attack
:is_player() and mobs
.invis
[ self
.attack
:get_player_name() ]) then
2135 self
.state
= "stand"
2136 set_velocity(self
, 0)
2137 set_animation(self
, "stand")
2139 self
.v_start
= false
2147 if self
.attack_type
== "explode" then
2154 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2156 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2158 yaw
= set_yaw(self
, yaw
)
2160 local node_break_radius
= self
.explosion_radius
or 1
2161 local entity_damage_radius
= self
.explosion_damage_radius
2162 or (node_break_radius
* 2)
2164 -- start timer when in reach and line of sight
2166 and dist
<= self
.reach
2167 and line_of_sight(self
, s
, p
, 2) then
2172 mob_sound(self
, "fuse", nil, false)
2174 -- stop timer if out of reach or direct line of sight
2175 elseif self
.allow_fuse_reset
2177 and (dist
> self
.reach
2178 or not line_of_sight(self
, s
, p
, 2)) then
2179 self
.v_start
= false
2182 self
.blinkstatus
= false
2183 remove_texture_mod(self
, "^[brighten")
2186 -- walk right up to player unless the timer is active
2187 if self
.v_start
and (self
.stop_to_explode
or dist
< 1.5) then
2188 set_velocity(self
, 0)
2190 set_velocity(self
, self
.run_velocity
)
2193 if self
.animation
and self
.animation
.run_start
then
2194 set_animation(self
, "run")
2196 set_animation(self
, "walk")
2199 if self
.v_start
then
2201 self
.timer
= self
.timer
+ dtime
2202 self
.blinktimer
= (self
.blinktimer
or 0) + dtime
2204 if self
.blinktimer
> 0.2 then
2208 if self
.blinkstatus
then
2209 remove_texture_mod(self
, "^[brighten")
2211 add_texture_mod(self
, "^[brighten")
2214 self
.blinkstatus
= not self
.blinkstatus
2217 if self
.timer
> self
.explosion_timer
then
2219 local pos
= self
.object
:get_pos()
2221 if mod_explosions
then
2222 if mobs_griefing
and not minetest
.is_protected(pos
, "") then
2223 mcl_explosions
.explode(self
.object
:get_pos(), self
.explosion_strength
, { drop_chance
= 1.0 }, self
.object
)
2225 minetest
.sound_play(self
.sounds
.explode
, {
2228 max_hear_distance
= self
.sounds
.distance
or 32
2231 entity_physics(pos
, entity_damage_radius
)
2232 effect(pos
, 32, "tnt_smoke.png", nil, nil, node_break_radius
, 1, 0)
2235 self
.object
:remove()
2241 elseif self
.attack_type
== "dogfight"
2242 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 2)
2243 or (self
.attack_type
== "dogshoot" and dist
<= self
.reach
and dogswitch(self
) == 0) then
2246 and dist
> self
.reach
then
2249 local me_y
= floor(p1
.y
)
2251 local p_y
= floor(p2
.y
+ 1)
2252 local v
= self
.object
:get_velocity()
2254 if flight_check(self
, s
) then
2258 self
.object
:set_velocity({
2260 y
= 1 * self
.walk_velocity
,
2264 elseif me_y
> p_y
then
2266 self
.object
:set_velocity({
2268 y
= -1 * self
.walk_velocity
,
2275 self
.object
:set_velocity({
2281 elseif me_y
> p_y
then
2283 self
.object
:set_velocity({
2293 -- rnd: new movement direction
2294 if self
.path
.following
2296 and self
.attack_type
~= "dogshoot" then
2298 -- no paths longer than 50
2299 if #self
.path
.way
> 50
2300 or dist
< self
.reach
then
2301 self
.path
.following
= false
2305 local p1
= self
.path
.way
[1]
2308 self
.path
.following
= false
2312 if abs(p1
.x
-s
.x
) + abs(p1
.z
- s
.z
) < 0.6 then
2313 -- reached waypoint, remove it from queue
2314 table.remove(self
.path
.way
, 1)
2317 -- set new temporary target
2318 p
= {x
= p1
.x
, y
= p1
.y
, z
= p1
.z
}
2326 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2328 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2330 yaw
= set_yaw(self
, yaw
)
2332 -- move towards enemy if beyond mob reach
2333 if dist
> self
.reach
then
2335 -- path finding by rnd
2336 if self
.pathfinding
-- only if mob has pathfinding enabled
2337 and enable_pathfinding
then
2339 smart_mobs(self
, s
, p
, dist
, dtime
)
2342 if is_at_cliff_or_danger(self
) then
2344 set_velocity(self
, 0)
2345 set_animation(self
, "stand")
2348 if self
.path
.stuck
then
2349 set_velocity(self
, self
.walk_velocity
)
2351 set_velocity(self
, self
.run_velocity
)
2354 if self
.animation
and self
.animation
.run_start
then
2355 set_animation(self
, "run")
2357 set_animation(self
, "walk")
2361 else -- rnd: if inside reach range
2363 self
.path
.stuck
= false
2364 self
.path
.stuck_timer
= 0
2365 self
.path
.following
= false -- not stuck anymore
2367 set_velocity(self
, 0)
2369 if not self
.custom_attack
then
2371 if self
.timer
> 1 then
2375 if self
.double_melee_attack
2376 and random(1, 2) == 1 then
2377 set_animation(self
, "punch2")
2379 set_animation(self
, "punch")
2388 if line_of_sight(self
, p2
, s2
) == true then
2390 -- play attack sound
2391 mob_sound(self
, "attack")
2393 -- punch player (or what player is attached to)
2394 local attached
= self
.attack
:get_attach()
2396 self
.attack
= attached
2398 self
.attack
:punch(self
.object
, 1.0, {
2399 full_punch_interval
= 1.0,
2400 damage_groups
= {fleshy
= self
.damage
}
2404 else -- call custom attack every second
2405 if self
.custom_attack
2406 and self
.timer
> 1 then
2410 self
.custom_attack(self
, p
)
2415 elseif self
.attack_type
== "shoot"
2416 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 1)
2417 or (self
.attack_type
== "dogshoot" and dist
> self
.reach
and dogswitch(self
) == 0) then
2422 local dist
= vector
.distance(p
, s
)
2429 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2431 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2433 yaw
= set_yaw(self
, yaw
)
2435 set_velocity(self
, 0)
2437 if self
.shoot_interval
2438 and self
.timer
> self
.shoot_interval
2439 and random(1, 100) <= 60 then
2442 set_animation(self
, "shoot")
2444 -- play shoot attack sound
2445 mob_sound(self
, "shoot_attack")
2447 local p
= self
.object
:get_pos()
2449 p
.y
= p
.y
+ (self
.collisionbox
[2] + self
.collisionbox
[5]) / 2
2452 if minetest
.registered_entities
[self
.arrow
] then
2456 if not self
.shoot_arrow
then
2457 arrow
= minetest
.add_entity(p
, self
.arrow
)
2458 ent
= arrow
:get_luaentity()
2459 if ent
.velocity
then
2463 ent
.owner_id
= tostring(self
.object
) -- add unique owner id to arrow
2466 local amount
= (vec
.x
* vec
.x
+ vec
.y
* vec
.y
+ vec
.z
* vec
.z
) ^
0.5
2467 -- offset makes shoot aim accurate
2468 vec
.y
= vec
.y
+ self
.shoot_offset
2469 vec
.x
= vec
.x
* (v
/ amount
)
2470 vec
.y
= vec
.y
* (v
/ amount
)
2471 vec
.z
= vec
.z
* (v
/ amount
)
2472 if self
.shoot_arrow
then
2473 vec
= vector
.normalize(vec
)
2474 self
:shoot_arrow(p
, vec
)
2476 arrow
:set_velocity(vec
)
2485 -- falling and fall damage
2486 -- returns true if mob died
2487 local falling
= function(self
, pos
)
2493 -- floating in water (or falling)
2494 local v
= self
.object
:get_velocity()
2498 -- apply gravity when moving up
2499 self
.object
:set_acceleration({
2505 elseif v
.y
<= 0 and v
.y
> self
.fall_speed
then
2507 -- fall downwards at set speed
2508 self
.object
:set_acceleration({
2510 y
= self
.fall_speed
,
2514 -- stop accelerating once max fall speed hit
2515 self
.object
:set_acceleration({x
= 0, y
= 0, z
= 0})
2518 -- in water then float up
2519 if minetest
.registered_nodes
[node_ok(pos
).name
].groups
.water
then
2521 if self
.floats
== 1 then
2523 self
.object
:set_acceleration({
2525 y
= -self
.fall_speed
/ (max(1, v
.y
) ^
2),
2531 -- fall damage onto solid ground
2532 if self
.fall_damage
== 1
2533 and self
.object
:get_velocity().y
== 0 then
2535 local d
= (self
.old_y
or 0) - self
.object
:get_pos().y
2539 local add
= minetest
.get_item_group(self
.standing_on
, "fall_damage_add_percent")
2540 local damage
= d
- 5
2542 damage
= damage
+ damage
* (add
/100)
2544 damage
= floor(damage
)
2546 self
.health
= self
.health
- damage
2548 effect(pos
, 5, "tnt_smoke.png", 1, 2, 2, nil)
2550 if check_for_death(self
, "fall", {type = "fall"}) then
2556 self
.old_y
= self
.object
:get_pos().y
2562 -- deal damage and effects when mob punched
2563 local mob_punch
= function(self
, hitter
, tflp
, tool_capabilities
, dir
)
2565 -- custom punch function
2566 if self
.do_punch
then
2568 -- when false skip going any further
2569 if self
.do_punch(self
, hitter
, tflp
, tool_caps
, dir
) == false then
2574 -- error checking when mod profiling is enabled
2575 if not tool_capabilities
then
2576 minetest
.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2580 -- is mob protected?
2581 if self
.protected
and hitter
:is_player()
2582 and minetest
.is_protected(self
.object
:get_pos(), hitter
:get_player_name()) then
2588 local weapon
= hitter
:get_wielded_item()
2589 local punch_interval
= 1.4
2592 if mod_hunger
and hitter
:is_player() then
2593 mcl_hunger
.exhaust(hitter
:get_player_name(), mcl_hunger
.EXHAUST_ATTACK
)
2596 -- calculate mob damage
2598 local armor
= self
.object
:get_armor_groups() or {}
2601 -- quick error check incase it ends up 0 (serialize.h check test)
2607 damage
= cmi
.calculate_damage(self
.object
, hitter
, tflp
, tool_capabilities
, dir
)
2610 for group
,_
in pairs( (tool_capabilities
.damage_groups
or {}) ) do
2612 tmp
= tflp
/ (tool_capabilities
.full_punch_interval
or 1.4)
2620 damage
= damage
+ (tool_capabilities
.damage_groups
[group
] or 0)
2621 * tmp
* ((armor
[group
] or 0) / 100.0)
2625 -- check for tool immunity or special damage
2626 for n
= 1, #self
.immune_to
do
2628 if self
.immune_to
[n
][1] == weapon
:get_name() then
2630 damage
= self
.immune_to
[n
][2] or 0
2636 if damage
<= -1 then
2637 self
.health
= self
.health
- floor(damage
)
2643 local cancel
= cmi
.notify_punch(self
.object
, hitter
, tflp
, tool_capabilities
, dir
, damage
)
2645 if cancel
then return end
2648 if tool_capabilities
then
2649 punch_interval
= tool_capabilities
.full_punch_interval
or 1.4
2652 -- add weapon wear manually
2653 -- Required because we have custom health handling ("health" property)
2654 if minetest
.settings
:get_bool("creative_mode") ~= true
2655 and tool_capabilities
then
2656 if tool_capabilities
.punch_attack_uses
then
2657 -- Without this delay, the wear does not work. Quite hacky ...
2658 minetest
.after(0, function(name
)
2659 local player
= minetest
.get_player_by_name(name
)
2660 if not player
then return end
2661 local weapon
= hitter
:get_wielded_item(player
)
2662 local def
= weapon
:get_definition()
2663 if def
.tool_capabilities
and def
.tool_capabilities
.punch_attack_uses
then
2664 local wear
= floor(65535/tool_capabilities
.punch_attack_uses
)
2665 weapon
:add_wear(wear
)
2666 hitter
:set_wielded_item(weapon
)
2668 end, hitter
:get_player_name())
2674 -- only play hit sound and show blood effects if damage is 1 or over
2678 if weapon
:get_definition().sounds
~= nil then
2680 local s
= random(0, #weapon
:get_definition().sounds
)
2682 minetest
.sound_play(weapon
:get_definition().sounds
[s
], {
2683 object
= self
.object
, --hitter,
2684 max_hear_distance
= 8
2687 minetest
.sound_play("default_punch", {
2688 object
= self
.object
, --hitter,
2689 max_hear_distance
= 5
2693 damage_effect(self
, damage
)
2696 self
.health
= self
.health
- floor(damage
)
2698 -- skip future functions if dead, except alerting others
2699 if check_for_death(self
, "hit", {type = "punch", puncher
= hitter
}) then
2703 -- knock back effect (only on full punch)
2706 and tflp
>= punch_interval
then
2708 local v
= self
.object
:get_velocity()
2709 local r
= 1.4 - min(punch_interval
, 1.4)
2713 -- if already in air then dont go up anymore when hit
2719 -- direction error check
2720 dir
= dir
or {x
= 0, y
= 0, z
= 0}
2722 -- check if tool already has specific knockback value
2723 if tool_capabilities
.damage_groups
["knockback"] then
2724 kb
= tool_capabilities
.damage_groups
["knockback"]
2729 self
.object
:set_velocity({
2731 y
= dir
.y
* kb
+ up
,
2735 self
.pause_timer
= 0.25
2737 end -- END if damage
2739 -- if skittish then run away
2740 if not die
and self
.runaway
== true then
2742 local lp
= hitter
:get_pos()
2743 local s
= self
.object
:get_pos()
2750 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
2756 yaw
= set_yaw(self
, yaw
, 6)
2757 self
.state
= "runaway"
2758 self
.runaway_timer
= 0
2759 self
.following
= nil
2762 local name
= hitter
:get_player_name() or ""
2764 -- attack puncher and call other mobs for help
2765 if self
.passive
== false
2766 and self
.state
~= "flop"
2767 and (self
.child
== false or self
.type == "monster")
2768 and hitter
:get_player_name() ~= self
.owner
2769 and not mobs
.invis
[ name
] then
2772 -- attack whoever punched mob
2774 do_attack(self
, hitter
)
2777 -- alert others to the attack
2778 local objs
= minetest
.get_objects_inside_radius(hitter
:get_pos(), self
.view_range
)
2783 obj
= objs
[n
]:get_luaentity()
2787 -- only alert members of same mob or friends
2789 and obj
.state
~= "attack"
2790 and obj
.owner
~= name
then
2791 if obj
.name
== self
.name
then
2792 do_attack(obj
, hitter
)
2793 elseif type(obj
.group_attack
) == "table" then
2794 for i
=1, #obj
.group_attack
do
2795 if obj
.name
== obj
.group_attack
[i
] then
2796 do_attack(obj
, hitter
)
2803 -- have owned mobs attack player threat
2804 if obj
.owner
== name
and obj
.owner_loyal
then
2805 do_attack(obj
, self
.object
)
2812 local mob_detach_child
= function(self
, child
)
2814 if self
.driver
== child
then
2820 -- get entity staticdata
2821 local mob_staticdata
= function(self
)
2823 -- remove mob when out of range unless tamed
2825 and self
.can_despawn
2827 and ((not self
.nametag
) or (self
.nametag
== ""))
2828 and self
.lifetimer
<= 20 then
2830 minetest
.log("action", "Mob "..name
.." despawns in mob_staticdata at "..minetest
.pos_to_string(self
.object
.get_pos()))
2831 self
.object
:remove()
2836 self
.remove_ok
= true
2838 self
.following
= nil
2839 self
.state
= "stand"
2842 self
.serialized_cmi_components
= cmi
.serialize_components(self
._cmi_components
)
2847 for _
,stat
in pairs(self
) do
2849 local t
= type(stat
)
2854 and _
~= "_cmi_components" then
2859 return minetest
.serialize(tmp
)
2863 -- activate mob and reload settings
2864 local mob_activate
= function(self
, staticdata
, def
, dtime
)
2866 -- remove monsters in peaceful mode
2867 if self
.type == "monster"
2868 and minetest
.settings
:get_bool("only_peaceful_mobs", false) then
2870 self
.object
:remove()
2875 -- load entity variables
2876 local tmp
= minetest
.deserialize(staticdata
)
2879 for _
,stat
in pairs(tmp
) do
2884 -- select random texture, set model and size
2885 if not self
.base_texture
then
2887 -- compatiblity with old simple mobs textures
2888 if type(def
.textures
[1]) == "string" then
2889 def
.textures
= {def
.textures
}
2892 self
.base_texture
= def
.textures
[random(1, #def
.textures
)]
2893 self
.base_mesh
= def
.mesh
2894 self
.base_size
= self
.visual_size
2895 self
.base_colbox
= self
.collisionbox
2896 self
.base_selbox
= self
.selectionbox
2899 -- for current mobs that dont have this set
2900 if not self
.base_selbox
then
2901 self
.base_selbox
= self
.selectionbox
or self
.base_colbox
2904 -- set texture, model and size
2905 local textures
= self
.base_texture
2906 local mesh
= self
.base_mesh
2907 local vis_size
= self
.base_size
2908 local colbox
= self
.base_colbox
2909 local selbox
= self
.base_selbox
2911 -- specific texture if gotten
2912 if self
.gotten
== true
2913 and def
.gotten_texture
then
2914 textures
= def
.gotten_texture
2917 -- specific mesh if gotten
2918 if self
.gotten
== true
2919 and def
.gotten_mesh
then
2920 mesh
= def
.gotten_mesh
2923 -- set child objects to half size
2924 if self
.child
== true then
2927 x
= self
.base_size
.x
* .5,
2928 y
= self
.base_size
.y
* .5,
2931 if def
.child_texture
then
2932 textures
= def
.child_texture
[1]
2936 self
.base_colbox
[1] * .5,
2937 self
.base_colbox
[2] * .5,
2938 self
.base_colbox
[3] * .5,
2939 self
.base_colbox
[4] * .5,
2940 self
.base_colbox
[5] * .5,
2941 self
.base_colbox
[6] * .5
2944 self
.base_selbox
[1] * .5,
2945 self
.base_selbox
[2] * .5,
2946 self
.base_selbox
[3] * .5,
2947 self
.base_selbox
[4] * .5,
2948 self
.base_selbox
[5] * .5,
2949 self
.base_selbox
[6] * .5
2953 if self
.health
== 0 then
2954 self
.health
= random (self
.hp_min
, self
.hp_max
)
2956 if self
.breath
== nil then
2957 self
.breath
= self
.breath_max
2962 self
.path
.way
= {} -- path to follow, table of positions
2963 self
.path
.lastpos
= {x
= 0, y
= 0, z
= 0}
2964 self
.path
.stuck
= false
2965 self
.path
.following
= false -- currently following path?
2966 self
.path
.stuck_timer
= 0 -- if stuck for too long search for path
2969 -- immortal=1 because we use custom health
2970 -- handling (using "health" property)
2972 if type(self
.armor
) == "table" then
2973 armor
= table.copy(self
.armor
)
2976 armor
= {immortal
=1, fleshy
= self
.armor
}
2978 self
.object
:set_armor_groups(armor
)
2979 self
.old_y
= self
.object
:get_pos().y
2980 self
.old_health
= self
.health
2981 self
.sounds
.distance
= self
.sounds
.distance
or 10
2982 self
.textures
= textures
2984 self
.collisionbox
= colbox
2985 self
.selectionbox
= selbox
2986 self
.visual_size
= vis_size
2987 self
.standing_in
= "ignore"
2988 self
.standing_on
= "ignore"
2989 self
.jump_sound_cooloff
= 0 -- used to prevent jump sound from being played too often in short time
2990 self
.opinion_sound_cooloff
= 0 -- used to prevent sound spam of particular sound types
2992 self
.texture_mods
= {}
2993 self
.object
:set_texture_mod("")
2995 self
.v_start
= false
2998 self
.blinkstatus
= false
3000 -- check existing nametag
3001 if not self
.nametag
then
3002 self
.nametag
= def
.nametag
3005 -- set anything changed above
3006 self
.object
:set_properties(self
)
3007 set_yaw(self
, (random(0, 360) - 180) / 180 * pi
, 6)
3009 set_animation(self
, "stand")
3011 -- run on_spawn function if found
3012 if self
.on_spawn
and not self
.on_spawn_run
then
3013 if self
.on_spawn(self
) then
3014 self
.on_spawn_run
= true -- if true, set flag to run once only
3018 -- run after_activate
3019 if def
.after_activate
then
3020 def
.after_activate(self
, staticdata
, def
, dtime
)
3024 self
._cmi_components
= cmi
.activate_components(self
.serialized_cmi_components
)
3025 cmi
.notify_activate(self
.object
, dtime
)
3030 -- main mob function
3031 local mob_step
= function(self
, dtime
)
3034 cmi
.notify_step(self
.object
, dtime
)
3037 local pos
= self
.object
:get_pos()
3040 -- Despawning: when lifetimer expires, remove mob
3042 and self
.can_despawn
== true
3043 and ((not self
.nametag
) or (self
.nametag
== "")) then
3045 -- TODO: Finish up implementation of despawning rules
3047 self
.lifetimer
= self
.lifetimer
- dtime
3049 if self
.lifetimer
<= 0 then
3051 -- only despawn away from player
3052 local objs
= minetest
.get_objects_inside_radius(pos
, 32)
3056 if objs
[n
]:is_player() then
3064 minetest
.log("action", "Mob "..name
.." despawns in mob_step at "..minetest
.pos_to_string(pos
))
3065 self
.object
:remove()
3071 if self
.jump_sound_cooloff
> 0 then
3072 self
.jump_sound_cooloff
= self
.jump_sound_cooloff
- dtime
3074 if self
.opinion_sound_cooloff
> 0 then
3075 self
.opinion_sound_cooloff
= self
.opinion_sound_cooloff
- dtime
3077 if falling(self
, pos
) then
3078 -- Return if mob died after falling
3082 -- smooth rotation by ThomasMonroe314
3084 if self
.delay
and self
.delay
> 0 then
3086 local yaw
= self
.object
:get_yaw() or 0
3088 if self
.delay
== 1 then
3089 yaw
= self
.target_yaw
3091 local dif
= abs(yaw
- self
.target_yaw
)
3093 if yaw
> self
.target_yaw
then
3096 dif
= 2 * pi
- dif
-- need to add
3097 yaw
= yaw
+ dif
/ self
.delay
3099 yaw
= yaw
- dif
/ self
.delay
-- need to subtract
3102 elseif yaw
< self
.target_yaw
then
3106 yaw
= yaw
- dif
/ self
.delay
-- need to subtract
3108 yaw
= yaw
+ dif
/ self
.delay
-- need to add
3112 if yaw
> (pi
* 2) then yaw
= yaw
- (pi
* 2) end
3113 if yaw
< 0 then yaw
= yaw
+ (pi
* 2) end
3116 self
.delay
= self
.delay
- 1
3117 self
.object
:set_yaw(yaw
)
3123 if self
.pause_timer
> 0 then
3125 self
.pause_timer
= self
.pause_timer
- dtime
3130 -- run custom function (defined in mob lua file)
3131 if self
.do_custom
then
3133 -- when false skip going any further
3134 if self
.do_custom(self
, dtime
) == false then
3140 self
.timer
= self
.timer
+ dtime
3142 if self
.state
~= "attack" then
3144 if self
.timer
< 1 then
3151 -- never go over 100
3152 if self
.timer
> 100 then
3156 -- mob plays random sound at times
3157 if random(1, 100) == 1 then
3158 mob_sound(self
, "random", true)
3161 -- environmental damage timer (every 1 second)
3162 self
.env_damage_timer
= self
.env_damage_timer
+ dtime
3164 if (self
.state
== "attack" and self
.env_damage_timer
> 1)
3165 or self
.state
~= "attack" then
3167 self
.env_damage_timer
= 0
3169 -- check for environmental damage (water, fire, lava etc.)
3170 if do_env_damage(self
) then
3174 -- node replace check (cow eats grass etc.)
3178 monster_attack(self
)
3186 if do_states(self
, dtime
) then
3197 -- default function when mobs are blown up with TNT
3198 local do_tnt
= function(obj
, damage
)
3200 obj
.object
:punch(obj
.object
, 1.0, {
3201 full_punch_interval
= 1.0,
3202 damage_groups
= {fleshy
= damage
},
3205 return false, true, {}
3209 mobs
.spawning_mobs
= {}
3211 -- Code to execute before custom on_rightclick handling
3212 local on_rightclick_prefix
= function(self
, clicker
)
3213 local item
= clicker
:get_wielded_item()
3215 -- Name mob with nametag
3216 if not self
.ignores_nametag
and item
:get_name() == "mcl_mobs:nametag" then
3218 local tag = item
:get_meta():get_string("name")
3220 if string.len(tag) > MAX_MOB_NAME_LENGTH
then
3221 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH
)
3227 if not mobs
.is_creative(clicker
:get_player_name()) then
3229 clicker
:set_wielded_item(item
)
3238 local create_mob_on_rightclick
= function(on_rightclick
)
3239 return function(self
, clicker
)
3240 local stop
= on_rightclick_prefix(self
, clicker
)
3241 if (not stop
) and (on_rightclick
) then
3242 on_rightclick(self
, clicker
)
3247 -- register mob entity
3248 function mobs
:register_mob(name
, def
)
3250 mobs
.spawning_mobs
[name
] = true
3253 if def
.can_despawn
~= nil then
3254 can_despawn
= def
.can_despawn
3259 local function scale_difficulty(value
, default
, min, special
)
3260 if (not value
) or (value
== default
) or (value
== special
) then
3263 return max(min, value
* difficulty
)
3267 local collisionbox
= def
.collisionbox
or {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25}
3268 -- Workaround for <https://github.com/minetest/minetest/issues/5966>:
3269 -- Increase upper Y limit to avoid mobs glitching through solid nodes.
3270 -- FIXME: Remove workaround if it's no longer needed.
3271 if collisionbox
[5] < 0.79 then
3272 collisionbox
[5] = 0.79
3275 minetest
.register_entity(name
, {
3277 stepheight
= def
.stepheight
or 0.6,
3280 attack_type
= def
.attack_type
,
3282 fly_in
= def
.fly_in
or {"air", "__airlike"},
3283 owner
= def
.owner
or "",
3284 order
= def
.order
or "",
3285 on_die
= def
.on_die
,
3286 spawn_small_alternative
= def
.spawn_small_alternative
,
3287 do_custom
= def
.do_custom
,
3288 jump_height
= def
.jump_height
or 4, -- was 6
3289 rotate
= math
.rad(def
.rotate
or 0), -- 0=front, 90=side, 180=back, 270=side2
3290 lifetimer
= def
.lifetimer
or 57.73,
3291 hp_min
= scale_difficulty(def
.hp_min
, 5, 1),
3292 hp_max
= scale_difficulty(def
.hp_max
, 10, 1),
3293 breath_max
= def
.breath_max
or 15,
3294 breathes_in_water
= def
.breathes_in_water
or false,
3296 collisionbox
= collisionbox
,
3297 selectionbox
= def
.selectionbox
or def
.collisionbox
,
3298 visual
= def
.visual
,
3299 visual_size
= def
.visual_size
or {x
= 1, y
= 1},
3301 makes_footstep_sound
= def
.makes_footstep_sound
or false,
3302 view_range
= def
.view_range
or 16,
3303 walk_velocity
= def
.walk_velocity
or 1,
3304 run_velocity
= def
.run_velocity
or 2,
3305 damage
= scale_difficulty(def
.damage
, 0, 0),
3306 light_damage
= def
.light_damage
or 0,
3307 sunlight_damage
= def
.sunlight_damage
or 0,
3308 water_damage
= def
.water_damage
or 0,
3309 lava_damage
= def
.lava_damage
or 8,
3310 fire_damage
= def
.fire_damage
or 1,
3311 suffocation
= def
.suffocation
or true,
3312 fall_damage
= def
.fall_damage
or 1,
3313 fall_speed
= def
.fall_speed
or -10, -- must be lower than -2 (default: -10)
3314 drops
= def
.drops
or {},
3315 armor
= def
.armor
or 100,
3316 on_rightclick
= create_mob_on_rightclick(def
.on_rightclick
),
3318 shoot_interval
= def
.shoot_interval
,
3319 sounds
= def
.sounds
or {},
3320 animation
= def
.animation
,
3321 follow
= def
.follow
,
3322 jump
= def
.jump
~= false,
3323 walk_chance
= def
.walk_chance
or 50,
3324 attacks_monsters
= def
.attacks_monsters
or false,
3325 group_attack
= def
.group_attack
or false,
3326 passive
= def
.passive
or false,
3327 knock_back
= def
.knock_back
~= false,
3328 shoot_offset
= def
.shoot_offset
or 0,
3329 floats
= def
.floats
or 1, -- floats in water by default
3330 replace_rate
= def
.replace_rate
,
3331 replace_what
= def
.replace_what
,
3332 replace_with
= def
.replace_with
,
3333 replace_offset
= def
.replace_offset
or 0,
3334 on_replace
= def
.on_replace
,
3336 env_damage_timer
= 0, -- only used when state = "attack"
3343 reach
= def
.reach
or 3,
3345 texture_list
= def
.textures
,
3346 child_texture
= def
.child_texture
,
3347 docile_by_day
= def
.docile_by_day
or false,
3349 fear_height
= def
.fear_height
or 0,
3350 runaway
= def
.runaway
,
3352 pathfinding
= def
.pathfinding
,
3353 immune_to
= def
.immune_to
or {},
3354 explosion_radius
= def
.explosion_radius
, -- LEGACY
3355 explosion_damage_radius
= def
.explosion_damage_radius
, -- LEGACY
3356 explosion_timer
= def
.explosion_timer
or 3,
3357 allow_fuse_reset
= def
.allow_fuse_reset
~= false,
3358 stop_to_explode
= def
.stop_to_explode
~= false,
3359 custom_attack
= def
.custom_attack
,
3360 double_melee_attack
= def
.double_melee_attack
,
3361 dogshoot_switch
= def
.dogshoot_switch
,
3363 dogshoot_count_max
= def
.dogshoot_count_max
or 5,
3364 dogshoot_count2_max
= def
.dogshoot_count2_max
or (def
.dogshoot_count_max
or 5),
3365 attack_animals
= def
.attack_animals
or false,
3366 specific_attack
= def
.specific_attack
,
3367 runaway_from
= def
.runaway_from
,
3368 owner_loyal
= def
.owner_loyal
,
3369 facing_fence
= false,
3373 spawn_class
= def
.spawn_class
,
3374 ignores_nametag
= def
.ignores_nametag
or false,
3375 rain_damage
= def
.rain_damage
or 0,
3377 can_despawn
= can_despawn
,
3378 child
= def
.child
or false,
3380 shoot_arrow
= def
.shoot_arrow
,
3381 sounds_child
= def
.sounds_child
,
3382 explosion_strength
= def
.explosion_strength
,
3383 -- End of MCL2 extensions
3385 on_spawn
= def
.on_spawn
,
3387 on_blast
= def
.on_blast
or do_tnt
,
3391 do_punch
= def
.do_punch
,
3393 on_punch
= mob_punch
,
3395 on_breed
= def
.on_breed
,
3397 on_grown
= def
.on_grown
,
3399 on_detach_child
= mob_detach_child
,
3401 on_activate
= function(self
, staticdata
, dtime
)
3402 return mob_activate(self
, staticdata
, def
, dtime
)
3405 get_staticdata
= function(self
)
3406 return mob_staticdata(self
)
3411 if minetest
.get_modpath("doc_identifier") ~= nil then
3412 doc
.sub
.identifier
.register_object(name
, "basics", "mobs")
3415 end -- END mobs:register_mob function
3418 -- count how many mobs of one type are inside an area
3419 local count_mobs
= function(pos
, mobtype
)
3422 local objs
= minetest
.get_objects_inside_radius(pos
, aoc_range
)
3426 local obj
= objs
[n
]:get_luaentity()
3428 if obj
and obj
.name
and obj
._cmi_is_mob
then
3430 -- count passive mobs only
3431 if mobtype
== "!passive" then
3432 if obj
.spawn_class
== "passive" then
3435 -- count hostile mobs only
3436 elseif mobtype
== "!hostile" then
3437 if obj
.spawn_class
== "hostile" then
3440 -- count ambient mobs only
3441 elseif mobtype
== "!ambient" then
3442 if obj
.spawn_class
== "ambient" then
3445 -- count water mobs only
3446 elseif mobtype
== "!water" then
3447 if obj
.spawn_class
== "water" then
3451 elseif mobtype
and obj
.name
== mobtype
then
3454 elseif not mobtype
then
3466 function mobs
:spawn_abm_check(pos
, node
, name
)
3467 -- global function to add additional spawn checks
3468 -- return true to stop spawning mob
3472 function mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
,
3473 interval
, chance
, aoc
, min_height
, max_height
, day_toggle
, on_spawn
)
3475 -- Do mobs spawn at all?
3476 if not mobs_spawn
then
3480 -- chance/spawn number override in minetest.conf for registered mob
3481 local numbers
= minetest
.settings
:get(name
)
3484 numbers
= numbers
:split(",")
3485 chance
= tonumber(numbers
[1]) or chance
3486 aoc
= tonumber(numbers
[2]) or aoc
3489 minetest
.log("warning", string.format("[mobs] %s has spawning disabled", name
))
3493 minetest
.log("action",
3494 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name
, chance
, aoc
))
3499 spawn_action
= function(pos
, node
, active_object_count
, active_object_count_wider
, name
)
3501 local orig_pos
= table.copy(pos
)
3502 -- is mob actually registered?
3503 if not mobs
.spawning_mobs
[name
]
3504 or not minetest
.registered_entities
[name
] then
3505 minetest
.log("warning", "Mob spawn of "..name
.." failed, unknown entity or mob is not registered for spawning!")
3509 -- additional custom checks for spawning mob
3510 if mobs
:spawn_abm_check(pos
, node
, name
) == true then
3511 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, ABM check rejected!")
3515 -- count nearby mobs in same spawn class
3516 local entdef
= minetest
.registered_entities
[name
]
3517 local spawn_class
= entdef
and entdef
.spawn_class
3518 if not spawn_class
then
3519 if entdef
.type == "monster" then
3520 spawn_class
= "hostile"
3522 spawn_class
= "passive"
3525 local in_class_cap
= count_mobs(pos
, "!"..spawn_class
) < MOB_CAP
[spawn_class
]
3526 -- do not spawn if too many of same mob in area
3527 if active_object_count_wider
>= max_per_block
-- large-range mob cap
3528 or (not in_class_cap
) -- spawn class mob cap
3529 or count_mobs(pos
, name
) >= aoc
then -- per-mob mob cap
3530 -- too many entities
3531 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, too crowded!")
3535 -- if toggle set to nil then ignore day/night check
3536 if day_toggle
~= nil then
3538 local tod
= (minetest
.get_timeofday() or 0) * 24000
3540 if tod
> 4500 and tod
< 19500 then
3541 -- daylight, but mob wants night
3542 if day_toggle
== false then
3544 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, mob needs light!")
3548 -- night time but mob wants day
3549 if day_toggle
== true then
3551 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, mob needs daylight!")
3560 -- only spawn away from player
3561 local objs
= minetest
.get_objects_inside_radius(pos
, 10)
3565 if objs
[n
]:is_player() then
3567 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, player too close!")
3572 -- mobs cannot spawn in protected areas when enabled
3573 if not spawn_protected
3574 and minetest
.is_protected(pos
, "") then
3575 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, position is protected!")
3579 -- are we spawning within height limits?
3580 if pos
.y
> max_height
3581 or pos
.y
< min_height
then
3582 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, out of height limit!")
3586 -- are light levels ok?
3587 local light
= minetest
.get_node_light(pos
)
3589 or light
> max_light
3590 or light
< min_light
then
3591 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, bad light!")
3595 -- do we have enough space to spawn mob?
3596 local ent
= minetest
.registered_entities
[name
]
3597 local width_x
= max(1, math
.ceil(ent
.collisionbox
[4] - ent
.collisionbox
[1]))
3599 if width_x
% 2 == 0 then
3600 max_x
= math
.floor(width_x
/2)
3603 max_x
= math
.floor(width_x
/2)
3607 local width_z
= max(1, math
.ceil(ent
.collisionbox
[6] - ent
.collisionbox
[3]))
3609 if width_z
% 2 == 0 then
3610 max_z
= math
.floor(width_z
/2)
3613 max_z
= math
.floor(width_z
/2)
3617 local max_y
= max(0, math
.ceil(ent
.collisionbox
[5] - ent
.collisionbox
[2]) - 1)
3620 for x
= min_x
, max_x
do
3621 for z
= min_z
, max_z
do
3622 local pos2
= {x
= pos
.x
+x
, y
= pos
.y
+y
, z
= pos
.z
+z
}
3623 if minetest
.registered_nodes
[node_ok(pos2
).name
].walkable
== true then
3625 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, too little space!")
3626 if ent
.spawn_small_alternative
~= nil and (not minetest
.registered_nodes
[node_ok(pos
).name
].walkable
) then
3627 minetest
.log("info", "Trying to spawn smaller alternative mob: "..ent
.spawn_small_alternative
)
3628 spawn_action(orig_pos
, node
, active_object_count
, active_object_count_wider
, ent
.spawn_small_alternative
)
3636 -- spawn mob 1/2 node above ground
3638 -- tweak X/Z spawn pos
3639 if width_x
% 2 == 0 then
3642 if width_z
% 2 == 0 then
3646 local mob
= minetest
.add_entity(pos
, name
)
3647 minetest
.log("action", "Mob spawned: "..name
.." at "..minetest
.pos_to_string(pos
))
3651 local ent
= mob
:get_luaentity()
3657 local function spawn_abm_action(pos
, node
, active_object_count
, active_object_count_wider
)
3658 spawn_action(pos
, node
, active_object_count
, active_object_count_wider
, name
)
3661 minetest
.register_abm({
3662 label
= name
.. " spawning",
3664 neighbors
= neighbors
,
3665 interval
= interval
,
3666 chance
= floor(max(1, chance
* mobs_spawn_chance
)),
3668 action
= spawn_abm_action
,
3673 -- compatibility with older mob registration
3674 function mobs
:register_spawn(name
, nodes
, max_light
, min_light
, chance
, active_object_count
, max_height
, day_toggle
)
3676 mobs
:spawn_specific(name
, nodes
, {"air"}, min_light
, max_light
, 30,
3677 chance
, active_object_count
, -31000, max_height
, day_toggle
)
3681 -- MarkBu's spawn function
3682 function mobs
:spawn(def
)
3684 local name
= def
.name
3685 local nodes
= def
.nodes
or {"group:soil", "group:stone"}
3686 local neighbors
= def
.neighbors
or {"air"}
3687 local min_light
= def
.min_light
or 0
3688 local max_light
= def
.max_light
or 15
3689 local interval
= def
.interval
or 30
3690 local chance
= def
.chance
or 5000
3691 local active_object_count
= def
.active_object_count
or 1
3692 local min_height
= def
.min_height
or -31000
3693 local max_height
= def
.max_height
or 31000
3694 local day_toggle
= def
.day_toggle
3695 local on_spawn
= def
.on_spawn
3697 mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
, interval
,
3698 chance
, active_object_count
, min_height
, max_height
, day_toggle
, on_spawn
)
3702 -- register arrow for shoot attack
3703 function mobs
:register_arrow(name
, def
)
3705 if not name
or not def
then return end -- errorcheck
3707 minetest
.register_entity(name
, {
3710 visual
= def
.visual
,
3711 visual_size
= def
.visual_size
,
3712 textures
= def
.textures
,
3713 velocity
= def
.velocity
,
3714 hit_player
= def
.hit_player
,
3715 hit_node
= def
.hit_node
,
3716 hit_mob
= def
.hit_mob
,
3717 hit_object
= def
.hit_object
,
3718 drop
= def
.drop
or false, -- drops arrow as registered item when true
3719 collisionbox
= {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3722 owner_id
= def
.owner_id
,
3723 rotate
= def
.rotate
,
3724 automatic_face_movement_dir
= def
.rotate
3725 and (def
.rotate
- (pi
/ 180)) or false,
3727 on_activate
= def
.on_activate
,
3729 on_step
= def
.on_step
or function(self
, dtime
)
3731 self
.timer
= self
.timer
+ 1
3733 local pos
= self
.object
:get_pos()
3737 or not within_limits(pos
, 0) then
3739 self
.object
:remove();
3744 -- does arrow have a tail (fireball)
3747 and def
.tail_texture
then
3749 minetest
.add_particle({
3751 velocity
= {x
= 0, y
= 0, z
= 0},
3752 acceleration
= {x
= 0, y
= 0, z
= 0},
3753 expirationtime
= def
.expire
or 0.25,
3754 collisiondetection
= false,
3755 texture
= def
.tail_texture
,
3756 size
= def
.tail_size
or 5,
3757 glow
= def
.glow
or 0,
3761 if self
.hit_node
then
3763 local node
= node_ok(pos
).name
3765 if minetest
.registered_nodes
[node
].walkable
then
3767 self
.hit_node(self
, pos
, node
)
3769 if self
.drop
== true then
3773 self
.lastpos
= (self
.lastpos
or pos
)
3775 minetest
.add_item(self
.lastpos
, self
.object
:get_luaentity().name
)
3778 self
.object
:remove();
3784 if self
.hit_player
or self
.hit_mob
or self
.hit_object
then
3786 for _
,player
in pairs(minetest
.get_objects_inside_radius(pos
, 1.0)) do
3789 and player
:is_player() then
3791 self
.hit_player(self
, player
)
3792 self
.object
:remove();
3796 local entity
= player
:get_luaentity()
3800 and entity
._cmi_is_mob
== true
3801 and tostring(player
) ~= self
.owner_id
3802 and entity
.name
~= self
.object
:get_luaentity().name
then
3803 self
.hit_mob(self
, player
)
3804 self
.object
:remove();
3810 and (not entity
._cmi_is_mob
)
3811 and tostring(player
) ~= self
.owner_id
3812 and entity
.name
~= self
.object
:get_luaentity().name
then
3813 self
.hit_object(self
, player
)
3814 self
.object
:remove();
3826 -- no damage to nodes explosion
3827 function mobs
:safe_boom(self
, pos
, radius
)
3828 minetest
.sound_play(self
.sounds
and self
.sounds
.explode
or "tnt_explode", {
3831 max_hear_distance
= self
.sounds
and self
.sounds
.distance
or 32
3834 entity_physics(pos
, radius
)
3835 effect(pos
, 32, "tnt_smoke.png", radius
* 3, radius
* 5, radius
, 1, 0)
3839 -- make explosion with protection and tnt mod check
3840 function mobs
:boom(self
, pos
, radius
)
3842 if mod_explosions
then
3843 if mobs_griefing
and not minetest
.is_protected(pos
, "") then
3844 mcl_explosions
.explode(pos
, self
.explosion_strength
, { drop_chance
= 1.0 }, self
.object
)
3846 mobs
:safe_boom(self
, pos
, radius
)
3849 mobs
:safe_boom(self
, pos
, radius
)
3854 -- Register spawn eggs
3856 -- Note: This also introduces the “spawn_egg” group:
3857 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3858 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3859 function mobs
:register_egg(mob
, desc
, background
, addegg
, no_creative
)
3861 local grp
= {spawn_egg
= 1}
3863 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3864 if creative
and no_creative
== true then
3865 grp
.not_in_creative_inventory
= 1
3868 local invimg
= background
3871 invimg
= "mobs_chicken_egg.png^(" .. invimg
..
3872 "^[mask:mobs_chicken_egg_overlay.png)"
3875 -- register old stackable mob egg
3876 minetest
.register_craftitem(mob
, {
3879 inventory_image
= invimg
,
3882 _doc_items_longdesc
= S("This allows you to place a single mob."),
3883 _doc_items_usagehelp
= S("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."),
3885 on_place
= function(itemstack
, placer
, pointed_thing
)
3887 local pos
= pointed_thing
.above
3889 -- am I clicking on something with existing on_rightclick function?
3890 local under
= minetest
.get_node(pointed_thing
.under
)
3891 local def
= minetest
.registered_nodes
[under
.name
]
3892 if def
and def
.on_rightclick
then
3893 return def
.on_rightclick(pointed_thing
.under
, under
, placer
, itemstack
)
3897 and within_limits(pos
, 0)
3898 and not minetest
.is_protected(pos
, placer
:get_player_name()) then
3900 local name
= placer
:get_player_name()
3901 local privs
= minetest
.get_player_privs(name
)
3902 if mod_mobspawners
and under
.name
== "mcl_mobspawners:spawner" then
3903 if minetest
.is_protected(pointed_thing
.under
, name
) then
3904 minetest
.record_protection_violation(pointed_thing
.under
, name
)
3907 if not privs
.maphack
then
3908 minetest
.chat_send_player(name
, S("You need the “maphack” privilege to change the mob spawner."))
3911 mcl_mobspawners
.setup_spawner(pointed_thing
.under
, itemstack
:get_name())
3912 if not minetest
.settings
:get_bool("creative_mode") then
3913 itemstack
:take_item()
3918 if not minetest
.registered_entities
[mob
] then
3922 if minetest
.settings
:get_bool("only_peaceful_mobs", false)
3923 and minetest
.registered_entities
[mob
].type == "monster" then
3924 minetest
.chat_send_player(name
, S("Only peaceful mobs allowed!"))
3930 local mob
= minetest
.add_entity(pos
, mob
)
3931 local ent
= mob
:get_luaentity()
3933 -- don't set owner if monster or sneak pressed
3934 if ent
.type ~= "monster"
3935 and not placer
:get_player_control().sneak
then
3936 ent
.owner
= placer
:get_player_name()
3941 local nametag
= itemstack
:get_meta():get_string("name")
3942 if nametag
~= "" then
3943 if string.len(nametag
) > MAX_MOB_NAME_LENGTH
then
3944 nametag
= string.sub(nametag
, 1, MAX_MOB_NAME_LENGTH
)
3946 ent
.nametag
= nametag
3950 -- if not in creative then take item
3951 if not mobs
.is_creative(placer
:get_player_name()) then
3952 itemstack
:take_item()
3963 -- No-op in MCL2 (capturing mobs is not possible).
3964 -- Provided for compability with Mobs Redo
3965 function mobs
:capture_mob(self
, clicker
, chance_hand
, chance_net
, chance_lasso
, force_take
, replacewith
)
3970 -- No-op in MCL2 (protecting mobs is not possible).
3971 function mobs
:protect(self
, clicker
)
3976 -- feeding, taming and breeding (thanks blert2112)
3977 function mobs
:feed_tame(self
, clicker
, feed_count
, breed
, tame
)
3978 if not self
.follow
then
3982 -- can eat/tame with item in hand
3983 if follow_holding(self
, clicker
) then
3985 -- if not in creative then take item
3986 if not mobs
.is_creative(clicker
:get_player_name()) then
3988 local item
= clicker
:get_wielded_item()
3992 clicker
:set_wielded_item(item
)
3996 self
.health
= self
.health
+ 4
3998 if self
.health
>= self
.hp_max
then
4000 self
.health
= self
.hp_max
4002 if self
.htimer
< 1 then
4007 self
.object
:set_hp(self
.health
)
4011 -- make children grow quicker
4012 if self
.child
== true then
4014 self
.hornytimer
= self
.hornytimer
+ 20
4020 self
.food
= (self
.food
or 0) + 1
4021 if self
.food
>= feed_count
then
4025 if breed
and self
.hornytimer
== 0 then
4033 if not self
.owner
or self
.owner
== "" then
4034 self
.owner
= clicker
:get_player_name()
4038 -- make sound when fed so many times
4039 mob_sound(self
, "random", true)
4049 function mobs
:spawn_child(pos
, mob_type
)
4050 local child
= minetest
.add_entity(pos
, mob_type
)
4055 local ent
= child
:get_luaentity()
4056 effect(pos
, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
4061 -- using specific child texture (if found)
4062 if ent
.child_texture
then
4063 textures
= ent
.child_texture
[1]
4066 -- and resize to half height
4067 child
:set_properties({
4068 textures
= textures
,
4070 x
= ent
.base_size
.x
* .5,
4071 y
= ent
.base_size
.y
* .5,
4074 ent
.base_colbox
[1] * .5,
4075 ent
.base_colbox
[2] * .5,
4076 ent
.base_colbox
[3] * .5,
4077 ent
.base_colbox
[4] * .5,
4078 ent
.base_colbox
[5] * .5,
4079 ent
.base_colbox
[6] * .5,
4082 ent
.base_selbox
[1] * .5,
4083 ent
.base_selbox
[2] * .5,
4084 ent
.base_selbox
[3] * .5,
4085 ent
.base_selbox
[4] * .5,
4086 ent
.base_selbox
[5] * .5,
4087 ent
.base_selbox
[6] * .5,
4095 -- compatibility function for old entities to new modpack entities
4096 function mobs
:alias_mob(old_name
, new_name
)
4099 minetest
.register_alias(old_name
, new_name
)
4102 minetest
.register_entity(":" .. old_name
, {
4106 on_step
= function(self
)
4108 if minetest
.registered_entities
[new_name
] then
4109 minetest
.add_entity(self
.object
:get_pos(), new_name
)
4112 self
.object
:remove()