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_tnt
= minetest
.get_modpath("mcl_tnt") ~= 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 -- dont damage anything if area protected or next to water
2222 if minetest
.find_node_near(pos
, 1, {"group:water"})
2223 or minetest
.is_protected(pos
, "") then
2225 node_break_radius
= 1
2228 self
.object
:remove()
2230 if mobs_griefing
and mod_tnt
and tnt
and tnt
.boom
2231 and not minetest
.is_protected(pos
, "") then
2234 radius
= node_break_radius
,
2235 damage_radius
= entity_damage_radius
,
2236 sound
= self
.sounds
.explode
,
2241 minetest
.sound_play(self
.sounds
.explode
, {
2244 max_hear_distance
= self
.sounds
.distance
or 32
2247 entity_physics(pos
, entity_damage_radius
)
2248 effect(pos
, 32, "tnt_smoke.png", nil, nil, node_break_radius
, 1, 0)
2255 elseif self
.attack_type
== "dogfight"
2256 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 2)
2257 or (self
.attack_type
== "dogshoot" and dist
<= self
.reach
and dogswitch(self
) == 0) then
2260 and dist
> self
.reach
then
2263 local me_y
= floor(p1
.y
)
2265 local p_y
= floor(p2
.y
+ 1)
2266 local v
= self
.object
:get_velocity()
2268 if flight_check(self
, s
) then
2272 self
.object
:set_velocity({
2274 y
= 1 * self
.walk_velocity
,
2278 elseif me_y
> p_y
then
2280 self
.object
:set_velocity({
2282 y
= -1 * self
.walk_velocity
,
2289 self
.object
:set_velocity({
2295 elseif me_y
> p_y
then
2297 self
.object
:set_velocity({
2307 -- rnd: new movement direction
2308 if self
.path
.following
2310 and self
.attack_type
~= "dogshoot" then
2312 -- no paths longer than 50
2313 if #self
.path
.way
> 50
2314 or dist
< self
.reach
then
2315 self
.path
.following
= false
2319 local p1
= self
.path
.way
[1]
2322 self
.path
.following
= false
2326 if abs(p1
.x
-s
.x
) + abs(p1
.z
- s
.z
) < 0.6 then
2327 -- reached waypoint, remove it from queue
2328 table.remove(self
.path
.way
, 1)
2331 -- set new temporary target
2332 p
= {x
= p1
.x
, y
= p1
.y
, z
= p1
.z
}
2340 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2342 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2344 yaw
= set_yaw(self
, yaw
)
2346 -- move towards enemy if beyond mob reach
2347 if dist
> self
.reach
then
2349 -- path finding by rnd
2350 if self
.pathfinding
-- only if mob has pathfinding enabled
2351 and enable_pathfinding
then
2353 smart_mobs(self
, s
, p
, dist
, dtime
)
2356 if is_at_cliff_or_danger(self
) then
2358 set_velocity(self
, 0)
2359 set_animation(self
, "stand")
2362 if self
.path
.stuck
then
2363 set_velocity(self
, self
.walk_velocity
)
2365 set_velocity(self
, self
.run_velocity
)
2368 if self
.animation
and self
.animation
.run_start
then
2369 set_animation(self
, "run")
2371 set_animation(self
, "walk")
2375 else -- rnd: if inside reach range
2377 self
.path
.stuck
= false
2378 self
.path
.stuck_timer
= 0
2379 self
.path
.following
= false -- not stuck anymore
2381 set_velocity(self
, 0)
2383 if not self
.custom_attack
then
2385 if self
.timer
> 1 then
2389 if self
.double_melee_attack
2390 and random(1, 2) == 1 then
2391 set_animation(self
, "punch2")
2393 set_animation(self
, "punch")
2402 if line_of_sight(self
, p2
, s2
) == true then
2404 -- play attack sound
2405 mob_sound(self
, "attack")
2407 -- punch player (or what player is attached to)
2408 local attached
= self
.attack
:get_attach()
2410 self
.attack
= attached
2412 self
.attack
:punch(self
.object
, 1.0, {
2413 full_punch_interval
= 1.0,
2414 damage_groups
= {fleshy
= self
.damage
}
2418 else -- call custom attack every second
2419 if self
.custom_attack
2420 and self
.timer
> 1 then
2424 self
.custom_attack(self
, p
)
2429 elseif self
.attack_type
== "shoot"
2430 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 1)
2431 or (self
.attack_type
== "dogshoot" and dist
> self
.reach
and dogswitch(self
) == 0) then
2436 local dist
= vector
.distance(p
, s
)
2443 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2445 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2447 yaw
= set_yaw(self
, yaw
)
2449 set_velocity(self
, 0)
2451 if self
.shoot_interval
2452 and self
.timer
> self
.shoot_interval
2453 and random(1, 100) <= 60 then
2456 set_animation(self
, "shoot")
2458 -- play shoot attack sound
2459 mob_sound(self
, "shoot_attack")
2461 local p
= self
.object
:get_pos()
2463 p
.y
= p
.y
+ (self
.collisionbox
[2] + self
.collisionbox
[5]) / 2
2466 if minetest
.registered_entities
[self
.arrow
] then
2470 if not self
.shoot_arrow
then
2471 arrow
= minetest
.add_entity(p
, self
.arrow
)
2472 ent
= arrow
:get_luaentity()
2473 if ent
.velocity
then
2477 ent
.owner_id
= tostring(self
.object
) -- add unique owner id to arrow
2480 local amount
= (vec
.x
* vec
.x
+ vec
.y
* vec
.y
+ vec
.z
* vec
.z
) ^
0.5
2481 -- offset makes shoot aim accurate
2482 vec
.y
= vec
.y
+ self
.shoot_offset
2483 vec
.x
= vec
.x
* (v
/ amount
)
2484 vec
.y
= vec
.y
* (v
/ amount
)
2485 vec
.z
= vec
.z
* (v
/ amount
)
2486 if self
.shoot_arrow
then
2487 vec
= vector
.normalize(vec
)
2488 self
:shoot_arrow(p
, vec
)
2490 arrow
:set_velocity(vec
)
2499 -- falling and fall damage
2500 -- returns true if mob died
2501 local falling
= function(self
, pos
)
2507 -- floating in water (or falling)
2508 local v
= self
.object
:get_velocity()
2512 -- apply gravity when moving up
2513 self
.object
:set_acceleration({
2519 elseif v
.y
<= 0 and v
.y
> self
.fall_speed
then
2521 -- fall downwards at set speed
2522 self
.object
:set_acceleration({
2524 y
= self
.fall_speed
,
2528 -- stop accelerating once max fall speed hit
2529 self
.object
:set_acceleration({x
= 0, y
= 0, z
= 0})
2532 -- in water then float up
2533 if minetest
.registered_nodes
[node_ok(pos
).name
].groups
.water
then
2535 if self
.floats
== 1 then
2537 self
.object
:set_acceleration({
2539 y
= -self
.fall_speed
/ (max(1, v
.y
) ^
2),
2545 -- fall damage onto solid ground
2546 if self
.fall_damage
== 1
2547 and self
.object
:get_velocity().y
== 0 then
2549 local d
= (self
.old_y
or 0) - self
.object
:get_pos().y
2553 local add
= minetest
.get_item_group(self
.standing_on
, "fall_damage_add_percent")
2554 local damage
= d
- 5
2556 damage
= damage
+ damage
* (add
/100)
2558 damage
= floor(damage
)
2560 self
.health
= self
.health
- damage
2562 effect(pos
, 5, "tnt_smoke.png", 1, 2, 2, nil)
2564 if check_for_death(self
, "fall", {type = "fall"}) then
2570 self
.old_y
= self
.object
:get_pos().y
2576 -- deal damage and effects when mob punched
2577 local mob_punch
= function(self
, hitter
, tflp
, tool_capabilities
, dir
)
2579 -- custom punch function
2580 if self
.do_punch
then
2582 -- when false skip going any further
2583 if self
.do_punch(self
, hitter
, tflp
, tool_caps
, dir
) == false then
2588 -- error checking when mod profiling is enabled
2589 if not tool_capabilities
then
2590 minetest
.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2594 -- is mob protected?
2595 if self
.protected
and hitter
:is_player()
2596 and minetest
.is_protected(self
.object
:get_pos(), hitter
:get_player_name()) then
2602 local weapon
= hitter
:get_wielded_item()
2603 local punch_interval
= 1.4
2606 if mod_hunger
and hitter
:is_player() then
2607 mcl_hunger
.exhaust(hitter
:get_player_name(), mcl_hunger
.EXHAUST_ATTACK
)
2610 -- calculate mob damage
2612 local armor
= self
.object
:get_armor_groups() or {}
2615 -- quick error check incase it ends up 0 (serialize.h check test)
2621 damage
= cmi
.calculate_damage(self
.object
, hitter
, tflp
, tool_capabilities
, dir
)
2624 for group
,_
in pairs( (tool_capabilities
.damage_groups
or {}) ) do
2626 tmp
= tflp
/ (tool_capabilities
.full_punch_interval
or 1.4)
2634 damage
= damage
+ (tool_capabilities
.damage_groups
[group
] or 0)
2635 * tmp
* ((armor
[group
] or 0) / 100.0)
2639 -- check for tool immunity or special damage
2640 for n
= 1, #self
.immune_to
do
2642 if self
.immune_to
[n
][1] == weapon
:get_name() then
2644 damage
= self
.immune_to
[n
][2] or 0
2650 if damage
<= -1 then
2651 self
.health
= self
.health
- floor(damage
)
2657 local cancel
= cmi
.notify_punch(self
.object
, hitter
, tflp
, tool_capabilities
, dir
, damage
)
2659 if cancel
then return end
2662 if tool_capabilities
then
2663 punch_interval
= tool_capabilities
.full_punch_interval
or 1.4
2666 -- add weapon wear manually
2667 -- Required because we have custom health handling ("health" property)
2668 if minetest
.settings
:get_bool("creative_mode") ~= true
2669 and tool_capabilities
then
2670 if tool_capabilities
.punch_attack_uses
then
2671 -- Without this delay, the wear does not work. Quite hacky ...
2672 minetest
.after(0, function(name
)
2673 local player
= minetest
.get_player_by_name(name
)
2674 if not player
then return end
2675 local weapon
= hitter
:get_wielded_item(player
)
2676 local def
= weapon
:get_definition()
2677 if def
.tool_capabilities
and def
.tool_capabilities
.punch_attack_uses
then
2678 local wear
= floor(65535/tool_capabilities
.punch_attack_uses
)
2679 weapon
:add_wear(wear
)
2680 hitter
:set_wielded_item(weapon
)
2682 end, hitter
:get_player_name())
2688 -- only play hit sound and show blood effects if damage is 1 or over
2692 if weapon
:get_definition().sounds
~= nil then
2694 local s
= random(0, #weapon
:get_definition().sounds
)
2696 minetest
.sound_play(weapon
:get_definition().sounds
[s
], {
2697 object
= self
.object
, --hitter,
2698 max_hear_distance
= 8
2701 minetest
.sound_play("default_punch", {
2702 object
= self
.object
, --hitter,
2703 max_hear_distance
= 5
2707 damage_effect(self
, damage
)
2710 self
.health
= self
.health
- floor(damage
)
2712 -- skip future functions if dead, except alerting others
2713 if check_for_death(self
, "hit", {type = "punch", puncher
= hitter
}) then
2717 -- knock back effect (only on full punch)
2720 and tflp
>= punch_interval
then
2722 local v
= self
.object
:get_velocity()
2723 local r
= 1.4 - min(punch_interval
, 1.4)
2727 -- if already in air then dont go up anymore when hit
2733 -- direction error check
2734 dir
= dir
or {x
= 0, y
= 0, z
= 0}
2736 -- check if tool already has specific knockback value
2737 if tool_capabilities
.damage_groups
["knockback"] then
2738 kb
= tool_capabilities
.damage_groups
["knockback"]
2743 self
.object
:set_velocity({
2745 y
= dir
.y
* kb
+ up
,
2749 self
.pause_timer
= 0.25
2751 end -- END if damage
2753 -- if skittish then run away
2754 if not die
and self
.runaway
== true then
2756 local lp
= hitter
:get_pos()
2757 local s
= self
.object
:get_pos()
2764 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
2770 yaw
= set_yaw(self
, yaw
, 6)
2771 self
.state
= "runaway"
2772 self
.runaway_timer
= 0
2773 self
.following
= nil
2776 local name
= hitter
:get_player_name() or ""
2778 -- attack puncher and call other mobs for help
2779 if self
.passive
== false
2780 and self
.state
~= "flop"
2781 and (self
.child
== false or self
.type == "monster")
2782 and hitter
:get_player_name() ~= self
.owner
2783 and not mobs
.invis
[ name
] then
2786 -- attack whoever punched mob
2788 do_attack(self
, hitter
)
2791 -- alert others to the attack
2792 local objs
= minetest
.get_objects_inside_radius(hitter
:get_pos(), self
.view_range
)
2797 obj
= objs
[n
]:get_luaentity()
2801 -- only alert members of same mob or friends
2803 and obj
.state
~= "attack"
2804 and obj
.owner
~= name
then
2805 if obj
.name
== self
.name
then
2806 do_attack(obj
, hitter
)
2807 elseif type(obj
.group_attack
) == "table" then
2808 for i
=1, #obj
.group_attack
do
2809 if obj
.name
== obj
.group_attack
[i
] then
2810 do_attack(obj
, hitter
)
2817 -- have owned mobs attack player threat
2818 if obj
.owner
== name
and obj
.owner_loyal
then
2819 do_attack(obj
, self
.object
)
2826 local mob_detach_child
= function(self
, child
)
2828 if self
.driver
== child
then
2834 -- get entity staticdata
2835 local mob_staticdata
= function(self
)
2837 -- remove mob when out of range unless tamed
2839 and self
.can_despawn
2841 and ((not self
.nametag
) or (self
.nametag
== ""))
2842 and self
.lifetimer
<= 20 then
2844 minetest
.log("action", "Mob "..name
.." despawns in mob_staticdata at "..minetest
.pos_to_string(self
.object
.get_pos()))
2845 self
.object
:remove()
2850 self
.remove_ok
= true
2852 self
.following
= nil
2853 self
.state
= "stand"
2856 self
.serialized_cmi_components
= cmi
.serialize_components(self
._cmi_components
)
2861 for _
,stat
in pairs(self
) do
2863 local t
= type(stat
)
2868 and _
~= "_cmi_components" then
2873 return minetest
.serialize(tmp
)
2877 -- activate mob and reload settings
2878 local mob_activate
= function(self
, staticdata
, def
, dtime
)
2880 -- remove monsters in peaceful mode
2881 if self
.type == "monster"
2882 and minetest
.settings
:get_bool("only_peaceful_mobs", false) then
2884 self
.object
:remove()
2889 -- load entity variables
2890 local tmp
= minetest
.deserialize(staticdata
)
2893 for _
,stat
in pairs(tmp
) do
2898 -- select random texture, set model and size
2899 if not self
.base_texture
then
2901 -- compatiblity with old simple mobs textures
2902 if type(def
.textures
[1]) == "string" then
2903 def
.textures
= {def
.textures
}
2906 self
.base_texture
= def
.textures
[random(1, #def
.textures
)]
2907 self
.base_mesh
= def
.mesh
2908 self
.base_size
= self
.visual_size
2909 self
.base_colbox
= self
.collisionbox
2910 self
.base_selbox
= self
.selectionbox
2913 -- for current mobs that dont have this set
2914 if not self
.base_selbox
then
2915 self
.base_selbox
= self
.selectionbox
or self
.base_colbox
2918 -- set texture, model and size
2919 local textures
= self
.base_texture
2920 local mesh
= self
.base_mesh
2921 local vis_size
= self
.base_size
2922 local colbox
= self
.base_colbox
2923 local selbox
= self
.base_selbox
2925 -- specific texture if gotten
2926 if self
.gotten
== true
2927 and def
.gotten_texture
then
2928 textures
= def
.gotten_texture
2931 -- specific mesh if gotten
2932 if self
.gotten
== true
2933 and def
.gotten_mesh
then
2934 mesh
= def
.gotten_mesh
2937 -- set child objects to half size
2938 if self
.child
== true then
2941 x
= self
.base_size
.x
* .5,
2942 y
= self
.base_size
.y
* .5,
2945 if def
.child_texture
then
2946 textures
= def
.child_texture
[1]
2950 self
.base_colbox
[1] * .5,
2951 self
.base_colbox
[2] * .5,
2952 self
.base_colbox
[3] * .5,
2953 self
.base_colbox
[4] * .5,
2954 self
.base_colbox
[5] * .5,
2955 self
.base_colbox
[6] * .5
2958 self
.base_selbox
[1] * .5,
2959 self
.base_selbox
[2] * .5,
2960 self
.base_selbox
[3] * .5,
2961 self
.base_selbox
[4] * .5,
2962 self
.base_selbox
[5] * .5,
2963 self
.base_selbox
[6] * .5
2967 if self
.health
== 0 then
2968 self
.health
= random (self
.hp_min
, self
.hp_max
)
2970 if self
.breath
== nil then
2971 self
.breath
= self
.breath_max
2976 self
.path
.way
= {} -- path to follow, table of positions
2977 self
.path
.lastpos
= {x
= 0, y
= 0, z
= 0}
2978 self
.path
.stuck
= false
2979 self
.path
.following
= false -- currently following path?
2980 self
.path
.stuck_timer
= 0 -- if stuck for too long search for path
2983 -- immortal=1 because we use custom health
2984 -- handling (using "health" property)
2986 if type(self
.armor
) == "table" then
2987 armor
= table.copy(self
.armor
)
2990 armor
= {immortal
=1, fleshy
= self
.armor
}
2992 self
.object
:set_armor_groups(armor
)
2993 self
.old_y
= self
.object
:get_pos().y
2994 self
.old_health
= self
.health
2995 self
.sounds
.distance
= self
.sounds
.distance
or 10
2996 self
.textures
= textures
2998 self
.collisionbox
= colbox
2999 self
.selectionbox
= selbox
3000 self
.visual_size
= vis_size
3001 self
.standing_in
= "ignore"
3002 self
.standing_on
= "ignore"
3003 self
.jump_sound_cooloff
= 0 -- used to prevent jump sound from being played too often in short time
3004 self
.opinion_sound_cooloff
= 0 -- used to prevent sound spam of particular sound types
3006 self
.texture_mods
= {}
3007 self
.object
:set_texture_mod("")
3009 self
.v_start
= false
3012 self
.blinkstatus
= false
3014 -- check existing nametag
3015 if not self
.nametag
then
3016 self
.nametag
= def
.nametag
3019 -- set anything changed above
3020 self
.object
:set_properties(self
)
3021 set_yaw(self
, (random(0, 360) - 180) / 180 * pi
, 6)
3023 set_animation(self
, "stand")
3025 -- run on_spawn function if found
3026 if self
.on_spawn
and not self
.on_spawn_run
then
3027 if self
.on_spawn(self
) then
3028 self
.on_spawn_run
= true -- if true, set flag to run once only
3032 -- run after_activate
3033 if def
.after_activate
then
3034 def
.after_activate(self
, staticdata
, def
, dtime
)
3038 self
._cmi_components
= cmi
.activate_components(self
.serialized_cmi_components
)
3039 cmi
.notify_activate(self
.object
, dtime
)
3044 -- main mob function
3045 local mob_step
= function(self
, dtime
)
3048 cmi
.notify_step(self
.object
, dtime
)
3051 local pos
= self
.object
:get_pos()
3054 -- Despawning: when lifetimer expires, remove mob
3056 and self
.can_despawn
== true
3057 and ((not self
.nametag
) or (self
.nametag
== "")) then
3059 -- TODO: Finish up implementation of despawning rules
3061 self
.lifetimer
= self
.lifetimer
- dtime
3063 if self
.lifetimer
<= 0 then
3065 -- only despawn away from player
3066 local objs
= minetest
.get_objects_inside_radius(pos
, 32)
3070 if objs
[n
]:is_player() then
3078 minetest
.log("action", "Mob "..name
.." despawns in mob_step at "..minetest
.pos_to_string(pos
))
3079 self
.object
:remove()
3085 if self
.jump_sound_cooloff
> 0 then
3086 self
.jump_sound_cooloff
= self
.jump_sound_cooloff
- dtime
3088 if self
.opinion_sound_cooloff
> 0 then
3089 self
.opinion_sound_cooloff
= self
.opinion_sound_cooloff
- dtime
3091 if falling(self
, pos
) then
3092 -- Return if mob died after falling
3096 -- smooth rotation by ThomasMonroe314
3098 if self
.delay
and self
.delay
> 0 then
3100 local yaw
= self
.object
:get_yaw() or 0
3102 if self
.delay
== 1 then
3103 yaw
= self
.target_yaw
3105 local dif
= abs(yaw
- self
.target_yaw
)
3107 if yaw
> self
.target_yaw
then
3110 dif
= 2 * pi
- dif
-- need to add
3111 yaw
= yaw
+ dif
/ self
.delay
3113 yaw
= yaw
- dif
/ self
.delay
-- need to subtract
3116 elseif yaw
< self
.target_yaw
then
3120 yaw
= yaw
- dif
/ self
.delay
-- need to subtract
3122 yaw
= yaw
+ dif
/ self
.delay
-- need to add
3126 if yaw
> (pi
* 2) then yaw
= yaw
- (pi
* 2) end
3127 if yaw
< 0 then yaw
= yaw
+ (pi
* 2) end
3130 self
.delay
= self
.delay
- 1
3131 self
.object
:set_yaw(yaw
)
3137 if self
.pause_timer
> 0 then
3139 self
.pause_timer
= self
.pause_timer
- dtime
3144 -- run custom function (defined in mob lua file)
3145 if self
.do_custom
then
3147 -- when false skip going any further
3148 if self
.do_custom(self
, dtime
) == false then
3154 self
.timer
= self
.timer
+ dtime
3156 if self
.state
~= "attack" then
3158 if self
.timer
< 1 then
3165 -- never go over 100
3166 if self
.timer
> 100 then
3170 -- mob plays random sound at times
3171 if random(1, 100) == 1 then
3172 mob_sound(self
, "random", true)
3175 -- environmental damage timer (every 1 second)
3176 self
.env_damage_timer
= self
.env_damage_timer
+ dtime
3178 if (self
.state
== "attack" and self
.env_damage_timer
> 1)
3179 or self
.state
~= "attack" then
3181 self
.env_damage_timer
= 0
3183 -- check for environmental damage (water, fire, lava etc.)
3184 if do_env_damage(self
) then
3188 -- node replace check (cow eats grass etc.)
3192 monster_attack(self
)
3200 if do_states(self
, dtime
) then
3211 -- default function when mobs are blown up with TNT
3212 local do_tnt
= function(obj
, damage
)
3214 obj
.object
:punch(obj
.object
, 1.0, {
3215 full_punch_interval
= 1.0,
3216 damage_groups
= {fleshy
= damage
},
3219 return false, true, {}
3223 mobs
.spawning_mobs
= {}
3225 -- Code to execute before custom on_rightclick handling
3226 local on_rightclick_prefix
= function(self
, clicker
)
3227 local item
= clicker
:get_wielded_item()
3229 -- Name mob with nametag
3230 if not self
.ignores_nametag
and item
:get_name() == "mcl_mobs:nametag" then
3232 local tag = item
:get_meta():get_string("name")
3234 if string.len(tag) > MAX_MOB_NAME_LENGTH
then
3235 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH
)
3241 if not mobs
.is_creative(clicker
:get_player_name()) then
3243 clicker
:set_wielded_item(item
)
3252 local create_mob_on_rightclick
= function(on_rightclick
)
3253 return function(self
, clicker
)
3254 local stop
= on_rightclick_prefix(self
, clicker
)
3255 if (not stop
) and (on_rightclick
) then
3256 on_rightclick(self
, clicker
)
3261 -- register mob entity
3262 function mobs
:register_mob(name
, def
)
3264 mobs
.spawning_mobs
[name
] = true
3267 if def
.can_despawn
~= nil then
3268 can_despawn
= def
.can_despawn
3273 local function scale_difficulty(value
, default
, min, special
)
3274 if (not value
) or (value
== default
) or (value
== special
) then
3277 return max(min, value
* difficulty
)
3281 local collisionbox
= def
.collisionbox
or {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25}
3282 -- Workaround for <https://github.com/minetest/minetest/issues/5966>:
3283 -- Increase upper Y limit to avoid mobs glitching through solid nodes.
3284 -- FIXME: Remove workaround if it's no longer needed.
3285 if collisionbox
[5] < 0.79 then
3286 collisionbox
[5] = 0.79
3289 minetest
.register_entity(name
, {
3291 stepheight
= def
.stepheight
or 0.6,
3294 attack_type
= def
.attack_type
,
3296 fly_in
= def
.fly_in
or {"air", "__airlike"},
3297 owner
= def
.owner
or "",
3298 order
= def
.order
or "",
3299 on_die
= def
.on_die
,
3300 spawn_small_alternative
= def
.spawn_small_alternative
,
3301 do_custom
= def
.do_custom
,
3302 jump_height
= def
.jump_height
or 4, -- was 6
3303 rotate
= math
.rad(def
.rotate
or 0), -- 0=front, 90=side, 180=back, 270=side2
3304 lifetimer
= def
.lifetimer
or 57.73,
3305 hp_min
= scale_difficulty(def
.hp_min
, 5, 1),
3306 hp_max
= scale_difficulty(def
.hp_max
, 10, 1),
3307 breath_max
= def
.breath_max
or 15,
3308 breathes_in_water
= def
.breathes_in_water
or false,
3310 collisionbox
= collisionbox
,
3311 selectionbox
= def
.selectionbox
or def
.collisionbox
,
3312 visual
= def
.visual
,
3313 visual_size
= def
.visual_size
or {x
= 1, y
= 1},
3315 makes_footstep_sound
= def
.makes_footstep_sound
or false,
3316 view_range
= def
.view_range
or 16,
3317 walk_velocity
= def
.walk_velocity
or 1,
3318 run_velocity
= def
.run_velocity
or 2,
3319 damage
= scale_difficulty(def
.damage
, 0, 0),
3320 light_damage
= def
.light_damage
or 0,
3321 sunlight_damage
= def
.sunlight_damage
or 0,
3322 water_damage
= def
.water_damage
or 0,
3323 lava_damage
= def
.lava_damage
or 8,
3324 fire_damage
= def
.fire_damage
or 1,
3325 suffocation
= def
.suffocation
or true,
3326 fall_damage
= def
.fall_damage
or 1,
3327 fall_speed
= def
.fall_speed
or -10, -- must be lower than -2 (default: -10)
3328 drops
= def
.drops
or {},
3329 armor
= def
.armor
or 100,
3330 on_rightclick
= create_mob_on_rightclick(def
.on_rightclick
),
3332 shoot_interval
= def
.shoot_interval
,
3333 sounds
= def
.sounds
or {},
3334 animation
= def
.animation
,
3335 follow
= def
.follow
,
3336 jump
= def
.jump
~= false,
3337 walk_chance
= def
.walk_chance
or 50,
3338 attacks_monsters
= def
.attacks_monsters
or false,
3339 group_attack
= def
.group_attack
or false,
3340 passive
= def
.passive
or false,
3341 knock_back
= def
.knock_back
~= false,
3342 shoot_offset
= def
.shoot_offset
or 0,
3343 floats
= def
.floats
or 1, -- floats in water by default
3344 replace_rate
= def
.replace_rate
,
3345 replace_what
= def
.replace_what
,
3346 replace_with
= def
.replace_with
,
3347 replace_offset
= def
.replace_offset
or 0,
3348 on_replace
= def
.on_replace
,
3350 env_damage_timer
= 0, -- only used when state = "attack"
3357 reach
= def
.reach
or 3,
3359 texture_list
= def
.textures
,
3360 child_texture
= def
.child_texture
,
3361 docile_by_day
= def
.docile_by_day
or false,
3363 fear_height
= def
.fear_height
or 0,
3364 runaway
= def
.runaway
,
3366 pathfinding
= def
.pathfinding
,
3367 immune_to
= def
.immune_to
or {},
3368 explosion_radius
= def
.explosion_radius
,
3369 explosion_damage_radius
= def
.explosion_damage_radius
,
3370 explosion_timer
= def
.explosion_timer
or 3,
3371 allow_fuse_reset
= def
.allow_fuse_reset
~= false,
3372 stop_to_explode
= def
.stop_to_explode
~= false,
3373 custom_attack
= def
.custom_attack
,
3374 double_melee_attack
= def
.double_melee_attack
,
3375 dogshoot_switch
= def
.dogshoot_switch
,
3377 dogshoot_count_max
= def
.dogshoot_count_max
or 5,
3378 dogshoot_count2_max
= def
.dogshoot_count2_max
or (def
.dogshoot_count_max
or 5),
3379 attack_animals
= def
.attack_animals
or false,
3380 specific_attack
= def
.specific_attack
,
3381 runaway_from
= def
.runaway_from
,
3382 owner_loyal
= def
.owner_loyal
,
3383 facing_fence
= false,
3387 spawn_class
= def
.spawn_class
,
3388 ignores_nametag
= def
.ignores_nametag
or false,
3389 rain_damage
= def
.rain_damage
or 0,
3391 can_despawn
= can_despawn
,
3392 child
= def
.child
or false,
3394 shoot_arrow
= def
.shoot_arrow
,
3395 sounds_child
= def
.sounds_child
,
3396 -- End of MCL2 extensions
3398 on_spawn
= def
.on_spawn
,
3400 on_blast
= def
.on_blast
or do_tnt
,
3404 do_punch
= def
.do_punch
,
3406 on_punch
= mob_punch
,
3408 on_breed
= def
.on_breed
,
3410 on_grown
= def
.on_grown
,
3412 on_detach_child
= mob_detach_child
,
3414 on_activate
= function(self
, staticdata
, dtime
)
3415 return mob_activate(self
, staticdata
, def
, dtime
)
3418 get_staticdata
= function(self
)
3419 return mob_staticdata(self
)
3424 if minetest
.get_modpath("doc_identifier") ~= nil then
3425 doc
.sub
.identifier
.register_object(name
, "basics", "mobs")
3428 end -- END mobs:register_mob function
3431 -- count how many mobs of one type are inside an area
3432 local count_mobs
= function(pos
, mobtype
)
3435 local objs
= minetest
.get_objects_inside_radius(pos
, aoc_range
)
3439 local obj
= objs
[n
]:get_luaentity()
3441 if obj
and obj
.name
and obj
._cmi_is_mob
then
3443 -- count passive mobs only
3444 if mobtype
== "!passive" then
3445 if obj
.spawn_class
== "passive" then
3448 -- count hostile mobs only
3449 elseif mobtype
== "!hostile" then
3450 if obj
.spawn_class
== "hostile" then
3453 -- count ambient mobs only
3454 elseif mobtype
== "!ambient" then
3455 if obj
.spawn_class
== "ambient" then
3458 -- count water mobs only
3459 elseif mobtype
== "!water" then
3460 if obj
.spawn_class
== "water" then
3464 elseif mobtype
and obj
.name
== mobtype
then
3467 elseif not mobtype
then
3479 function mobs
:spawn_abm_check(pos
, node
, name
)
3480 -- global function to add additional spawn checks
3481 -- return true to stop spawning mob
3485 function mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
,
3486 interval
, chance
, aoc
, min_height
, max_height
, day_toggle
, on_spawn
)
3488 -- Do mobs spawn at all?
3489 if not mobs_spawn
then
3493 -- chance/spawn number override in minetest.conf for registered mob
3494 local numbers
= minetest
.settings
:get(name
)
3497 numbers
= numbers
:split(",")
3498 chance
= tonumber(numbers
[1]) or chance
3499 aoc
= tonumber(numbers
[2]) or aoc
3502 minetest
.log("warning", string.format("[mobs] %s has spawning disabled", name
))
3506 minetest
.log("action",
3507 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name
, chance
, aoc
))
3512 spawn_action
= function(pos
, node
, active_object_count
, active_object_count_wider
, name
)
3514 local orig_pos
= table.copy(pos
)
3515 -- is mob actually registered?
3516 if not mobs
.spawning_mobs
[name
]
3517 or not minetest
.registered_entities
[name
] then
3518 minetest
.log("warning", "Mob spawn of "..name
.." failed, unknown entity or mob is not registered for spawning!")
3522 -- additional custom checks for spawning mob
3523 if mobs
:spawn_abm_check(pos
, node
, name
) == true then
3524 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, ABM check rejected!")
3528 -- count nearby mobs in same spawn class
3529 local entdef
= minetest
.registered_entities
[name
]
3530 local spawn_class
= entdef
and entdef
.spawn_class
3531 if not spawn_class
then
3532 if entdef
.type == "monster" then
3533 spawn_class
= "hostile"
3535 spawn_class
= "passive"
3538 local in_class_cap
= count_mobs(pos
, "!"..spawn_class
) < MOB_CAP
[spawn_class
]
3539 -- do not spawn if too many of same mob in area
3540 if active_object_count_wider
>= max_per_block
-- large-range mob cap
3541 or (not in_class_cap
) -- spawn class mob cap
3542 or count_mobs(pos
, name
) >= aoc
then -- per-mob mob cap
3543 -- too many entities
3544 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, too crowded!")
3548 -- if toggle set to nil then ignore day/night check
3549 if day_toggle
~= nil then
3551 local tod
= (minetest
.get_timeofday() or 0) * 24000
3553 if tod
> 4500 and tod
< 19500 then
3554 -- daylight, but mob wants night
3555 if day_toggle
== false then
3557 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, mob needs light!")
3561 -- night time but mob wants day
3562 if day_toggle
== true then
3564 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, mob needs daylight!")
3573 -- only spawn away from player
3574 local objs
= minetest
.get_objects_inside_radius(pos
, 10)
3578 if objs
[n
]:is_player() then
3580 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, player too close!")
3585 -- mobs cannot spawn in protected areas when enabled
3586 if not spawn_protected
3587 and minetest
.is_protected(pos
, "") then
3588 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, position is protected!")
3592 -- are we spawning within height limits?
3593 if pos
.y
> max_height
3594 or pos
.y
< min_height
then
3595 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, out of height limit!")
3599 -- are light levels ok?
3600 local light
= minetest
.get_node_light(pos
)
3602 or light
> max_light
3603 or light
< min_light
then
3604 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, bad light!")
3608 -- do we have enough space to spawn mob?
3609 local ent
= minetest
.registered_entities
[name
]
3610 local width_x
= max(1, math
.ceil(ent
.collisionbox
[4] - ent
.collisionbox
[1]))
3612 if width_x
% 2 == 0 then
3613 max_x
= math
.floor(width_x
/2)
3616 max_x
= math
.floor(width_x
/2)
3620 local width_z
= max(1, math
.ceil(ent
.collisionbox
[6] - ent
.collisionbox
[3]))
3622 if width_z
% 2 == 0 then
3623 max_z
= math
.floor(width_z
/2)
3626 max_z
= math
.floor(width_z
/2)
3630 local max_y
= max(0, math
.ceil(ent
.collisionbox
[5] - ent
.collisionbox
[2]) - 1)
3633 for x
= min_x
, max_x
do
3634 for z
= min_z
, max_z
do
3635 local pos2
= {x
= pos
.x
+x
, y
= pos
.y
+y
, z
= pos
.z
+z
}
3636 if minetest
.registered_nodes
[node_ok(pos2
).name
].walkable
== true then
3638 minetest
.log("info", "Mob spawn of "..name
.." at "..minetest
.pos_to_string(pos
).." failed, too little space!")
3639 if ent
.spawn_small_alternative
~= nil and (not minetest
.registered_nodes
[node_ok(pos
).name
].walkable
) then
3640 minetest
.log("info", "Trying to spawn smaller alternative mob: "..ent
.spawn_small_alternative
)
3641 spawn_action(orig_pos
, node
, active_object_count
, active_object_count_wider
, ent
.spawn_small_alternative
)
3649 -- spawn mob 1/2 node above ground
3651 -- tweak X/Z spawn pos
3652 if width_x
% 2 == 0 then
3655 if width_z
% 2 == 0 then
3659 local mob
= minetest
.add_entity(pos
, name
)
3660 minetest
.log("action", "Mob spawned: "..name
.." at "..minetest
.pos_to_string(pos
))
3664 local ent
= mob
:get_luaentity()
3670 local function spawn_abm_action(pos
, node
, active_object_count
, active_object_count_wider
)
3671 spawn_action(pos
, node
, active_object_count
, active_object_count_wider
, name
)
3674 minetest
.register_abm({
3675 label
= name
.. " spawning",
3677 neighbors
= neighbors
,
3678 interval
= interval
,
3679 chance
= floor(max(1, chance
* mobs_spawn_chance
)),
3681 action
= spawn_abm_action
,
3686 -- compatibility with older mob registration
3687 function mobs
:register_spawn(name
, nodes
, max_light
, min_light
, chance
, active_object_count
, max_height
, day_toggle
)
3689 mobs
:spawn_specific(name
, nodes
, {"air"}, min_light
, max_light
, 30,
3690 chance
, active_object_count
, -31000, max_height
, day_toggle
)
3694 -- MarkBu's spawn function
3695 function mobs
:spawn(def
)
3697 local name
= def
.name
3698 local nodes
= def
.nodes
or {"group:soil", "group:stone"}
3699 local neighbors
= def
.neighbors
or {"air"}
3700 local min_light
= def
.min_light
or 0
3701 local max_light
= def
.max_light
or 15
3702 local interval
= def
.interval
or 30
3703 local chance
= def
.chance
or 5000
3704 local active_object_count
= def
.active_object_count
or 1
3705 local min_height
= def
.min_height
or -31000
3706 local max_height
= def
.max_height
or 31000
3707 local day_toggle
= def
.day_toggle
3708 local on_spawn
= def
.on_spawn
3710 mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
, interval
,
3711 chance
, active_object_count
, min_height
, max_height
, day_toggle
, on_spawn
)
3715 -- register arrow for shoot attack
3716 function mobs
:register_arrow(name
, def
)
3718 if not name
or not def
then return end -- errorcheck
3720 minetest
.register_entity(name
, {
3723 visual
= def
.visual
,
3724 visual_size
= def
.visual_size
,
3725 textures
= def
.textures
,
3726 velocity
= def
.velocity
,
3727 hit_player
= def
.hit_player
,
3728 hit_node
= def
.hit_node
,
3729 hit_mob
= def
.hit_mob
,
3730 hit_object
= def
.hit_object
,
3731 drop
= def
.drop
or false, -- drops arrow as registered item when true
3732 collisionbox
= {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3735 owner_id
= def
.owner_id
,
3736 rotate
= def
.rotate
,
3737 automatic_face_movement_dir
= def
.rotate
3738 and (def
.rotate
- (pi
/ 180)) or false,
3740 on_activate
= def
.on_activate
,
3742 on_step
= def
.on_step
or function(self
, dtime
)
3744 self
.timer
= self
.timer
+ 1
3746 local pos
= self
.object
:get_pos()
3750 or not within_limits(pos
, 0) then
3752 self
.object
:remove();
3757 -- does arrow have a tail (fireball)
3760 and def
.tail_texture
then
3762 minetest
.add_particle({
3764 velocity
= {x
= 0, y
= 0, z
= 0},
3765 acceleration
= {x
= 0, y
= 0, z
= 0},
3766 expirationtime
= def
.expire
or 0.25,
3767 collisiondetection
= false,
3768 texture
= def
.tail_texture
,
3769 size
= def
.tail_size
or 5,
3770 glow
= def
.glow
or 0,
3774 if self
.hit_node
then
3776 local node
= node_ok(pos
).name
3778 if minetest
.registered_nodes
[node
].walkable
then
3780 self
.hit_node(self
, pos
, node
)
3782 if self
.drop
== true then
3786 self
.lastpos
= (self
.lastpos
or pos
)
3788 minetest
.add_item(self
.lastpos
, self
.object
:get_luaentity().name
)
3791 self
.object
:remove();
3797 if self
.hit_player
or self
.hit_mob
or self
.hit_object
then
3799 for _
,player
in pairs(minetest
.get_objects_inside_radius(pos
, 1.0)) do
3802 and player
:is_player() then
3804 self
.hit_player(self
, player
)
3805 self
.object
:remove();
3809 local entity
= player
:get_luaentity()
3813 and entity
._cmi_is_mob
== true
3814 and tostring(player
) ~= self
.owner_id
3815 and entity
.name
~= self
.object
:get_luaentity().name
then
3816 self
.hit_mob(self
, player
)
3817 self
.object
:remove();
3823 and (not entity
._cmi_is_mob
)
3824 and tostring(player
) ~= self
.owner_id
3825 and entity
.name
~= self
.object
:get_luaentity().name
then
3826 self
.hit_object(self
, player
)
3827 self
.object
:remove();
3839 -- no damage to nodes explosion
3840 function mobs
:safe_boom(self
, pos
, radius
)
3842 minetest
.sound_play(self
.sounds
and self
.sounds
.explode
or "tnt_explode", {
3845 max_hear_distance
= self
.sounds
and self
.sounds
.distance
or 32
3848 entity_physics(pos
, radius
)
3849 effect(pos
, 32, "tnt_smoke.png", radius
* 3, radius
* 5, radius
, 1, 0)
3853 -- make explosion with protection and tnt mod check
3854 function mobs
:boom(self
, pos
, radius
)
3857 and mod_tnt
and tnt
and tnt
.boom
3858 and not minetest
.is_protected(pos
, "") then
3862 damage_radius
= radius
,
3863 sound
= self
.sounds
and self
.sounds
.explode
,
3864 explode_center
= true,
3868 mobs
:safe_boom(self
, pos
, radius
)
3873 -- Register spawn eggs
3875 -- Note: This also introduces the “spawn_egg” group:
3876 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3877 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3878 function mobs
:register_egg(mob
, desc
, background
, addegg
, no_creative
)
3880 local grp
= {spawn_egg
= 1}
3882 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3883 if creative
and no_creative
== true then
3884 grp
.not_in_creative_inventory
= 1
3887 local invimg
= background
3890 invimg
= "mobs_chicken_egg.png^(" .. invimg
..
3891 "^[mask:mobs_chicken_egg_overlay.png)"
3894 -- register old stackable mob egg
3895 minetest
.register_craftitem(mob
, {
3898 inventory_image
= invimg
,
3901 _doc_items_longdesc
= S("This allows you to place a single mob."),
3902 _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."),
3904 on_place
= function(itemstack
, placer
, pointed_thing
)
3906 local pos
= pointed_thing
.above
3908 -- am I clicking on something with existing on_rightclick function?
3909 local under
= minetest
.get_node(pointed_thing
.under
)
3910 local def
= minetest
.registered_nodes
[under
.name
]
3911 if def
and def
.on_rightclick
then
3912 return def
.on_rightclick(pointed_thing
.under
, under
, placer
, itemstack
)
3916 and within_limits(pos
, 0)
3917 and not minetest
.is_protected(pos
, placer
:get_player_name()) then
3919 local name
= placer
:get_player_name()
3920 local privs
= minetest
.get_player_privs(name
)
3921 if mod_mobspawners
and under
.name
== "mcl_mobspawners:spawner" then
3922 if minetest
.is_protected(pointed_thing
.under
, name
) then
3923 minetest
.record_protection_violation(pointed_thing
.under
, name
)
3926 if not privs
.maphack
then
3927 minetest
.chat_send_player(name
, S("You need the “maphack” privilege to change the mob spawner."))
3930 mcl_mobspawners
.setup_spawner(pointed_thing
.under
, itemstack
:get_name())
3931 if not minetest
.settings
:get_bool("creative_mode") then
3932 itemstack
:take_item()
3937 if not minetest
.registered_entities
[mob
] then
3941 if minetest
.settings
:get_bool("only_peaceful_mobs", false)
3942 and minetest
.registered_entities
[mob
].type == "monster" then
3943 minetest
.chat_send_player(name
, S("Only peaceful mobs allowed!"))
3949 local mob
= minetest
.add_entity(pos
, mob
)
3950 local ent
= mob
:get_luaentity()
3952 -- don't set owner if monster or sneak pressed
3953 if ent
.type ~= "monster"
3954 and not placer
:get_player_control().sneak
then
3955 ent
.owner
= placer
:get_player_name()
3960 local nametag
= itemstack
:get_meta():get_string("name")
3961 if nametag
~= "" then
3962 if string.len(nametag
) > MAX_MOB_NAME_LENGTH
then
3963 nametag
= string.sub(nametag
, 1, MAX_MOB_NAME_LENGTH
)
3965 ent
.nametag
= nametag
3969 -- if not in creative then take item
3970 if not mobs
.is_creative(placer
:get_player_name()) then
3971 itemstack
:take_item()
3982 -- No-op in MCL2 (capturing mobs is not possible).
3983 -- Provided for compability with Mobs Redo
3984 function mobs
:capture_mob(self
, clicker
, chance_hand
, chance_net
, chance_lasso
, force_take
, replacewith
)
3989 -- No-op in MCL2 (protecting mobs is not possible).
3990 function mobs
:protect(self
, clicker
)
3995 -- feeding, taming and breeding (thanks blert2112)
3996 function mobs
:feed_tame(self
, clicker
, feed_count
, breed
, tame
)
3997 if not self
.follow
then
4001 -- can eat/tame with item in hand
4002 if follow_holding(self
, clicker
) then
4004 -- if not in creative then take item
4005 if not mobs
.is_creative(clicker
:get_player_name()) then
4007 local item
= clicker
:get_wielded_item()
4011 clicker
:set_wielded_item(item
)
4015 self
.health
= self
.health
+ 4
4017 if self
.health
>= self
.hp_max
then
4019 self
.health
= self
.hp_max
4021 if self
.htimer
< 1 then
4026 self
.object
:set_hp(self
.health
)
4030 -- make children grow quicker
4031 if self
.child
== true then
4033 self
.hornytimer
= self
.hornytimer
+ 20
4039 self
.food
= (self
.food
or 0) + 1
4040 if self
.food
>= feed_count
then
4044 if breed
and self
.hornytimer
== 0 then
4052 if not self
.owner
or self
.owner
== "" then
4053 self
.owner
= clicker
:get_player_name()
4057 -- make sound when fed so many times
4058 mob_sound(self
, "random", true)
4068 function mobs
:spawn_child(pos
, mob_type
)
4069 local child
= minetest
.add_entity(pos
, mob_type
)
4074 local ent
= child
:get_luaentity()
4075 effect(pos
, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
4080 -- using specific child texture (if found)
4081 if ent
.child_texture
then
4082 textures
= ent
.child_texture
[1]
4085 -- and resize to half height
4086 child
:set_properties({
4087 textures
= textures
,
4089 x
= ent
.base_size
.x
* .5,
4090 y
= ent
.base_size
.y
* .5,
4093 ent
.base_colbox
[1] * .5,
4094 ent
.base_colbox
[2] * .5,
4095 ent
.base_colbox
[3] * .5,
4096 ent
.base_colbox
[4] * .5,
4097 ent
.base_colbox
[5] * .5,
4098 ent
.base_colbox
[6] * .5,
4101 ent
.base_selbox
[1] * .5,
4102 ent
.base_selbox
[2] * .5,
4103 ent
.base_selbox
[3] * .5,
4104 ent
.base_selbox
[4] * .5,
4105 ent
.base_selbox
[5] * .5,
4106 ent
.base_selbox
[6] * .5,
4114 -- compatibility function for old entities to new modpack entities
4115 function mobs
:alias_mob(old_name
, new_name
)
4118 minetest
.register_alias(old_name
, new_name
)
4121 minetest
.register_entity(":" .. old_name
, {
4125 on_step
= function(self
)
4127 if minetest
.registered_entities
[new_name
] then
4128 minetest
.add_entity(self
.object
:get_pos(), new_name
)
4131 self
.object
:remove()