2 -- API for Mobs Redo: MineClone 2 Edition (MRM)
6 mobs
.version
= "20180531" -- don't rely too much on this, rarely updated, if ever
8 local MAX_MOB_NAME_LENGTH
= 30
11 local MP
= minetest
.get_modpath(minetest
.get_current_modname())
12 local S
, NS
= dofile(MP
.. "/intllib.lua")
17 local use_cmi
= minetest
.global_exists("cmi")
20 -- Invisibility mod check
22 if minetest
.global_exists("invisibility") then
23 mobs
.invis
= invisibility
28 local creative_mode_cache
= minetest
.settings
:get_bool("creative_mode")
29 function mobs
.is_creative(name
)
30 return creative_mode_cache
or minetest
.check_player_privs(name
, {creative
= true})
34 -- localize math functions
36 local square
= math
.sqrt
42 local atann
= math
.atan
43 local random = math
.random
44 local floor = math
.floor
45 local atan = function(x
)
46 if not x
or x
~= x
then
55 local damage_enabled
= minetest
.settings
:get_bool("enable_damage")
56 local mobs_spawn
= minetest
.settings
:get_bool("mobs_spawn") ~= false
57 local peaceful_only
= minetest
.settings
:get_bool("only_peaceful_mobs")
58 local disable_blood
= minetest
.settings
:get_bool("mobs_disable_blood")
59 local mobs_drop_items
= minetest
.settings
:get_bool("mobs_drop_items") ~= false
60 local mobs_griefing
= minetest
.settings
:get_bool("mobs_griefing") ~= false
61 local creative
= minetest
.settings
:get_bool("creative_mode")
62 local spawn_protected
= minetest
.settings
:get_bool("mobs_spawn_protected") ~= false
64 local remove_far
= false
65 local difficulty
= tonumber(minetest
.settings
:get("mob_difficulty")) or 1.0
66 local show_health
= false
67 local max_per_block
= tonumber(minetest
.settings
:get("max_objects_per_block") or 99)
68 local mob_chance_multiplier
= tonumber(minetest
.settings
:get("mob_chance_multiplier") or 1)
70 -- Peaceful mode message so players will know there are no monsters
72 minetest
.register_on_joinplayer(function(player
)
73 minetest
.chat_send_player(player
:get_player_name(),
74 S("** Peaceful Mode Active - No Monsters Will Spawn"))
78 -- calculate aoc range for mob count
79 local aosrb
= tonumber(minetest
.settings
:get("active_object_send_range_blocks"))
80 local abr
= tonumber(minetest
.settings
:get("active_block_range"))
81 local aoc_range
= max(aosrb
, abr
) * 16
83 -- pathfinding settings
84 local enable_pathfinding
= true
85 local stuck_timeout
= 3 -- how long before mob gets stuck in place and starts searching
86 local stuck_path_timeout
= 10 -- how long will mob follow path before giving up
89 local node_fire
= "mcl_fire:fire"
90 local node_permanent_flame
= "mcl_fire:eternal_fire"
91 local node_ice
= "mcl_core:ice"
92 local node_snowblock
= "mcl_core:snowblock"
93 local node_snow
= "mcl_core:snow"
94 mobs
.fallback_node
= minetest
.registered_aliases
["mapgen_dirt"] or "mcl_core:dirt"
96 local mod_weather
= minetest
.get_modpath("mcl_weather") ~= nil
97 local mod_tnt
= minetest
.get_modpath("mcl_tnt") ~= nil
98 local mod_mobspawners
= minetest
.get_modpath("mcl_mobspawners") ~= nil
101 local mob_sound
= function(self
, sound
)
104 minetest
.sound_play(sound
, {
105 object
= self
.object
,
107 max_hear_distance
= self
.sounds
.distance
114 local do_attack
= function(self
, player
)
116 if self
.state
== "attack" then
121 self
.state
= "attack"
123 if random(0, 100) < 90 then
124 mob_sound(self
, self
.sounds
.war_cry
)
129 -- move mob in facing direction
130 local set_velocity
= function(self
, v
)
132 -- do not move if mob has been ordered to stay
133 if self
.order
== "stand" then
134 self
.object
:setvelocity({x
= 0, y
= 0, z
= 0})
138 local yaw
= (self
.object
:get_yaw() or 0) + self
.rotate
140 self
.object
:setvelocity({
142 y
= self
.object
:getvelocity().y
,
148 -- calculate mob velocity
149 local get_velocity
= function(self
)
151 local v
= self
.object
:getvelocity()
153 return (v
.x
* v
.x
+ v
.z
* v
.z
) ^
0.5
157 -- set and return valid yaw
158 local set_yaw
= function(self
, yaw
, delay
)
160 if not yaw
or yaw
~= yaw
then
167 self
.object
:set_yaw(yaw
)
171 self
.target_yaw
= yaw
174 return self
.target_yaw
177 -- global function to set mob yaw
178 function mobs
:yaw(self
, yaw
, delay
)
179 set_yaw(self
, yaw
, delay
)
183 -- set defined animation
184 local set_animation
= function(self
, anim
)
186 if not self
.animation
187 or not anim
then return end
189 self
.animation
.current
= self
.animation
.current
or ""
191 if anim
== self
.animation
.current
192 or not self
.animation
[anim
.. "_start"]
193 or not self
.animation
[anim
.. "_end"] then
197 self
.animation
.current
= anim
199 self
.object
:set_animation({
200 x
= self
.animation
[anim
.. "_start"],
201 y
= self
.animation
[anim
.. "_end"]},
202 self
.animation
[anim
.. "_speed"] or self
.animation
.speed_normal
or 15,
203 0, self
.animation
[anim
.. "_loop"] ~= false)
207 -- above function exported for mount.lua
208 function mobs
:set_animation(self
, anim
)
209 set_animation(self
, anim
)
213 -- calculate distance
214 local get_distance
= function(a
, b
)
216 local x
, y
, z
= a
.x
- b
.x
, a
.y
- b
.y
, a
.z
- b
.z
218 return square(x
* x
+ y
* y
+ z
* z
)
222 -- check line of sight (BrunoMine)
223 local line_of_sight
= function(self
, pos1
, pos2
, stepsize
)
225 stepsize
= stepsize
or 1
227 local s
, pos
= minetest
.line_of_sight(pos1
, pos2
, stepsize
)
229 -- normal walking and flying mobs can see you through air
234 -- New pos1 to be analyzed
235 local npos1
= {x
= pos1
.x
, y
= pos1
.y
, z
= pos1
.z
}
237 local r
, pos
= minetest
.line_of_sight(npos1
, pos2
, stepsize
)
240 if r
== true then return true end
243 local nn
= minetest
.get_node(pos
).name
245 -- Target Distance (td) to travel
246 local td
= get_distance(pos1
, pos2
)
248 -- Actual Distance (ad) traveled
251 -- It continues to advance in the line of sight in search of a real
252 -- obstruction which counts as 'normal' nodebox.
253 while minetest
.registered_nodes
[nn
]
254 and (minetest
.registered_nodes
[nn
].walkable
== false
255 or minetest
.registered_nodes
[nn
].drawtype
== "nodebox") do
257 -- Check if you can still move forward
258 if td
< ad
+ stepsize
then
259 return true -- Reached the target
262 -- Moves the analyzed pos
263 local d
= get_distance(pos1
, pos2
)
265 npos1
.x
= ((pos2
.x
- pos1
.x
) / d
* stepsize
) + pos1
.x
266 npos1
.y
= ((pos2
.y
- pos1
.y
) / d
* stepsize
) + pos1
.y
267 npos1
.z
= ((pos2
.z
- pos1
.z
) / d
* stepsize
) + pos1
.z
271 or npos1
.x
~= npos1
.x
272 or npos1
.y
~= npos1
.y
273 or npos1
.z
~= npos1
.z
then
280 r
, pos
= minetest
.line_of_sight(npos1
, pos2
, stepsize
)
282 if r
== true then return true end
284 -- New Nodename found
285 nn
= minetest
.get_node(pos
).name
293 -- are we flying in what we are suppose to? (taikedz)
294 local flight_check
= function(self
, pos_w
)
296 local nod
= self
.standing_in
297 local def
= minetest
.registered_nodes
[nod
]
299 if not def
then return false end -- nil check
301 if type(self
.fly_in
) == "string"
302 and nod
== self
.fly_in
then
306 elseif type(self
.fly_in
) == "table" then
308 for _
,fly_in
in pairs(self
.fly_in
) do
310 if nod
== fly_in
then
317 -- stops mobs getting stuck inside stairs and plantlike nodes
318 if def
.drawtype
~= "airlike"
319 and def
.drawtype
~= "liquid"
320 and def
.drawtype
~= "flowingliquid" then
328 -- custom particle effects
329 local effect
= function(pos
, amount
, texture
, min_size
, max_size
, radius
, gravity
, glow
)
332 min_size
= min_size
or 0.5
333 max_size
= max_size
or 1
334 gravity
= gravity
or -10
337 minetest
.add_particlespawner({
342 minvel
= {x
= -radius
, y
= -radius
, z
= -radius
},
343 maxvel
= {x
= radius
, y
= radius
, z
= radius
},
344 minacc
= {x
= 0, y
= gravity
, z
= 0},
345 maxacc
= {x
= 0, y
= gravity
, z
= 0},
356 local update_tag
= function(self
)
357 self
.object
:set_properties({
358 nametag
= self
.nametag
,
365 local item_drop
= function(self
, cooked
)
367 -- no drops if disabled by setting
368 if not mobs_drop_items
then return end
370 -- no drops for child mobs
371 if self
.child
then return end
374 local pos
= self
.object
:get_pos()
376 self
.drops
= self
.drops
or {} -- nil check
378 for n
= 1, #self
.drops
do
380 if random(1, self
.drops
[n
].chance
) == 1 then
382 num
= random(self
.drops
[n
].min or 1, self
.drops
[n
].max or 1)
383 item
= self
.drops
[n
].name
385 -- cook items when true
388 local output
= minetest
.get_craft_result({
389 method
= "cooking", width
= 1, items
= {item
}})
391 if output
and output
.item
and not output
.item
:is_empty() then
392 item
= output
.item
:get_name()
396 -- add item if it exists
397 obj
= minetest
.add_item(pos
, ItemStack(item
.. " " .. num
))
399 if obj
and obj
:get_luaentity() then
402 x
= random(-10, 10) / 9,
404 z
= random(-10, 10) / 9,
407 obj
:remove() -- item does not exist
416 -- check if mob is dead or only hurt
417 local check_for_death
= function(self
, cause
, cmi_cause
)
419 -- has health actually changed?
420 if self
.health
== self
.old_health
and self
.health
> 0 then
424 self
.old_health
= self
.health
426 -- still got some health? play hurt sound
427 if self
.health
> 0 then
429 mob_sound(self
, self
.sounds
.damage
)
431 -- make sure health isn't higher than max
432 if self
.health
> self
.hp_max
then
433 self
.health
= self
.hp_max
436 -- backup nametag so we can show health stats
437 if not self
.nametag2
then
438 self
.nametag2
= self
.nametag
or ""
442 and (cmi_cause
and cmi_cause
.type == "punch") then
445 self
.nametag
= "♥ " .. self
.health
.. " / " .. self
.hp_max
453 -- dropped cooked item if mob died in lava
454 if cause
== "lava" then
455 item_drop(self
, true)
460 mob_sound(self
, self
.sounds
.death
)
462 local pos
= self
.object
:get_pos()
464 -- execute custom death function
467 self
.on_die(self
, pos
)
470 cmi
.notify_die(self
.object
, cmi_cause
)
478 -- default death function and die animation (if defined)
480 and self
.animation
.die_start
481 and self
.animation
.die_end
then
483 local frames
= self
.animation
.die_end
- self
.animation
.die_start
484 local speed
= self
.animation
.die_speed
or 15
485 local length
= max(frames
/ speed
, 0)
493 set_velocity(self
, 0)
494 set_animation(self
, "die")
496 minetest
.after(length
, function(self
)
497 if not self
.object
:get_luaentity() then
501 cmi
.notify_die(self
.object
, cmi_cause
)
509 cmi
.notify_die(self
.object
, cmi_cause
)
515 effect(pos
, 20, "tnt_smoke.png")
521 -- check if within physical map limits (-30911 to 30927)
522 local within_limits
= function(pos
, radius
)
524 if (pos
.x
- radius
) > -30913
525 and (pos
.x
+ radius
) < 30928
526 and (pos
.y
- radius
) > -30913
527 and (pos
.y
+ radius
) < 30928
528 and (pos
.z
- radius
) > -30913
529 and (pos
.z
+ radius
) < 30928 then
530 return true -- within limits
533 return false -- beyond limits
537 -- is mob facing a cliff
538 local is_at_cliff
= function(self
)
540 if self
.fear_height
== 0 then -- 0 for no falling protection!
544 local yaw
= self
.object
:get_yaw()
545 local dir_x
= -sin(yaw
) * (self
.collisionbox
[4] + 0.5)
546 local dir_z
= cos(yaw
) * (self
.collisionbox
[4] + 0.5)
547 local pos
= self
.object
:get_pos()
548 local ypos
= pos
.y
+ self
.collisionbox
[2] -- just above floor
550 if minetest
.line_of_sight(
551 {x
= pos
.x
+ dir_x
, y
= ypos
, z
= pos
.z
+ dir_z
},
552 {x
= pos
.x
+ dir_x
, y
= ypos
- self
.fear_height
, z
= pos
.z
+ dir_z
}
562 -- get node but use fallback for nil or unknown
563 local node_ok
= function(pos
, fallback
)
565 fallback
= fallback
or mobs
.fallback_node
567 local node
= minetest
.get_node_or_nil(pos
)
569 if node
and minetest
.registered_nodes
[node
.name
] then
573 return minetest
.registered_nodes
[fallback
]
577 -- environmental damage (water, lava, fire, light etc.)
578 local do_env_damage
= function(self
)
580 -- feed/tame text timer (so mob 'full' messages dont spam chat)
581 if self
.htimer
> 0 then
582 self
.htimer
= self
.htimer
- 1
585 -- reset nametag after showing health stats
586 if self
.htimer
< 1 and self
.nametag2
then
588 self
.nametag
= self
.nametag2
594 local pos
= self
.object
:get_pos()
596 self
.time_of_day
= minetest
.get_timeofday()
598 -- remove mob if beyond map limits
599 if not within_limits(pos
, 0) then
605 local deal_light_damage
= function(self
, pos
, damage
)
606 if not (mod_weather
and (mcl_weather
.rain
.raining
or mcl_weather
.state
== "snow") and mcl_weather
.is_outdoor(pos
)) then
607 self
.health
= self
.health
- damage
609 effect(pos
, 5, "tnt_smoke.png")
611 if check_for_death(self
, "light", {type = "light"}) then return end
615 -- bright light harms mob
616 if self
.light_damage
~= 0 and (minetest
.get_node_light(pos
) or 0) > 12 then
617 deal_light_damage(self
, pos
, self
.light_damage
)
619 local _
, dim
= mcl_worlds
.y_to_layer(pos
.y
)
620 if self
.sunlight_damage
~= 0 and (minetest
.get_node_light(pos
) or 0) >= minetest
.LIGHT_MAX
and dim
== "overworld" then
621 deal_light_damage(self
, pos
, self
.sunlight_damage
)
624 local y_level
= self
.collisionbox
[2]
627 y_level
= self
.collisionbox
[2] * 0.5
630 -- what is mob standing in?
631 pos
.y
= pos
.y
+ y_level
+ 0.25 -- foot level
632 self
.standing_in
= node_ok(pos
, "air").name
634 -- don't fall when on ignore, just stand still
635 if self
.standing_in
== "ignore" then
636 self
.object
:setvelocity({x
= 0, y
= 0, z
= 0})
639 local nodef
= minetest
.registered_nodes
[self
.standing_in
]
642 if self
.rain_damage
and mod_weather
then
643 if mcl_weather
.rain
.raining
and mcl_weather
.is_outdoor(pos
) then
645 self
.health
= self
.health
- self
.rain_damage
647 if check_for_death(self
, "rain", {type = "environment",
648 pos
= pos
, node
= self
.standing_in
}) then return end
652 pos
.y
= pos
.y
+ 1 -- for particle effect position
656 and nodef
.groups
.water
then
658 if self
.water_damage
~= 0 then
660 self
.health
= self
.health
- self
.water_damage
662 effect(pos
, 5, "bubble.png", nil, nil, 1, nil)
664 if check_for_death(self
, "water", {type = "environment",
665 pos
= pos
, node
= self
.standing_in
}) then return end
669 elseif self
.lava_damage
670 and (nodef
.groups
.lava
671 or self
.standing_in
== node_fire
672 or self
.standing_in
== node_permanent_flame
) then
674 if self
.lava_damage
~= 0 then
676 self
.health
= self
.health
- self
.lava_damage
678 effect(pos
, 5, "fire_basic_flame.png", nil, nil, 1, nil)
680 if check_for_death(self
, "lava", {type = "environment",
681 pos
= pos
, node
= self
.standing_in
}) then return end
684 -- damage_per_second node check
685 elseif nodef
.damage_per_second
~= 0 then
687 self
.health
= self
.health
- nodef
.damage_per_second
689 effect(pos
, 5, "tnt_smoke.png")
691 if check_for_death(self
, "dps", {type = "environment",
692 pos
= pos
, node
= self
.standing_in
}) then return end
695 --- suffocation inside solid node
696 if self.suffocation ~= 0
697 and nodef.walkable == true
698 and nodef.groups.disable_suffocation ~= 1
699 and nodef.drawtype == "normal" then
701 self.health = self.health - self.suffocation
703 if check_for_death(self, "suffocation", {type = "environment",
704 pos = pos, node = self.standing_in}) then return end
707 check_for_death(self
, "", {type = "unknown"})
711 -- jump if facing a solid node (not fences or gates)
712 local do_jump
= function(self
)
715 or self
.jump_height
== 0
718 or self
.order
== "stand" then
722 self
.facing_fence
= false
724 -- something stopping us while moving?
725 if self
.state
~= "stand"
726 and get_velocity(self
) > 0.5
727 and self
.object
:getvelocity().y
~= 0 then
731 local pos
= self
.object
:get_pos()
732 local yaw
= self
.object
:get_yaw()
734 -- what is mob standing on?
735 pos
.y
= pos
.y
+ self
.collisionbox
[2] - 0.2
737 local nod
= node_ok(pos
)
739 if minetest
.registered_nodes
[nod
.name
].walkable
== false then
744 local dir_x
= -sin(yaw
) * (self
.collisionbox
[4] + 0.5)
745 local dir_z
= cos(yaw
) * (self
.collisionbox
[4] + 0.5)
747 -- what is in front of mob?
748 local nod
= node_ok({
754 -- thin blocks that do not need to be jumped
755 if nod
.name
== node_snow
then
759 if self
.walk_chance
== 0
760 or minetest
.registered_items
[nod
.name
].walkable
then
762 if not nod
.name
:find("fence")
763 and not nod
.name
:find("gate") then
765 local v
= self
.object
:getvelocity()
767 v
.y
= self
.jump_height
769 set_animation(self
, "jump") -- only when defined
771 self
.object
:setvelocity(v
)
773 -- when in air move forward
774 minetest
.after(0.3, function(self
, v
)
775 if not self
.object
:get_luaentity() then
778 self
.object
:set_acceleration({
785 if get_velocity(self
) > 0 then
786 mob_sound(self
, self
.sounds
.jump
)
789 self
.facing_fence
= true
799 -- blast damage to entities nearby (modified from TNT mod)
800 local entity_physics
= function(pos
, radius
)
804 local objs
= minetest
.get_objects_inside_radius(pos
, radius
)
809 obj_pos
= objs
[n
]:get_pos()
811 dist
= get_distance(pos
, obj_pos
)
812 if dist
< 1 then dist
= 1 end
814 local damage
= floor((4 / dist
) * radius
)
815 local ent
= objs
[n
]:get_luaentity()
817 -- punches work on entities AND players
818 objs
[n
]:punch(objs
[n
], 1.0, {
819 full_punch_interval
= 1.0,
820 damage_groups
= {fleshy
= damage
},
826 -- should mob follow what I'm holding ?
827 local follow_holding
= function(self
, clicker
)
829 if mobs
.invis
[clicker
:get_player_name()] then
833 local item
= clicker
:get_wielded_item()
834 local t
= type(self
.follow
)
838 and item
:get_name() == self
.follow
then
842 elseif t
== "table" then
844 for no
= 1, #self
.follow
do
846 if self
.follow
[no
] == item
:get_name() then
856 -- find two animals of same type and breed if nearby and horny
857 local breed
= function(self
)
859 -- child takes 240 seconds before growing into adult
860 if self
.child
== true then
862 self
.hornytimer
= self
.hornytimer
+ 1
864 if self
.hornytimer
> 240 then
869 self
.object
:set_properties({
870 textures
= self
.base_texture
,
871 mesh
= self
.base_mesh
,
872 visual_size
= self
.base_size
,
873 collisionbox
= self
.base_colbox
,
874 selectionbox
= self
.base_selbox
,
877 -- custom function when child grows up
878 if self
.on_grown
then
881 -- jump when fully grown so as not to fall into ground
882 self
.object
:setvelocity({
884 y
= self
.jump_height
,
893 -- horny animal can mate for 40 seconds,
894 -- afterwards horny animal cannot mate again for 200 seconds
895 if self
.horny
== true
896 and self
.hornytimer
< 240 then
898 self
.hornytimer
= self
.hornytimer
+ 1
900 if self
.hornytimer
>= 240 then
906 -- find another same animal who is also horny and mate if nearby
907 if self
.horny
== true
908 and self
.hornytimer
<= 40 then
910 local pos
= self
.object
:get_pos()
912 effect({x
= pos
.x
, y
= pos
.y
+ 1, z
= pos
.z
}, 8, "heart.png", 3, 4, 1, 0.1)
914 local objs
= minetest
.get_objects_inside_radius(pos
, 3)
920 ent
= objs
[n
]:get_luaentity()
922 -- check for same animal with different colour
923 local canmate
= false
927 if ent
.name
== self
.name
then
930 local entname
= string.split(ent
.name
,":")
931 local selfname
= string.split(self
.name
,":")
933 if entname
[1] == selfname
[1] then
934 entname
= string.split(entname
[2],"_")
935 selfname
= string.split(selfname
[2],"_")
937 if entname
[1] == selfname
[1] then
946 and ent
.horny
== true
947 and ent
.hornytimer
<= 40 then
951 -- found your mate? then have a baby
958 minetest
.after(5, function(parent1
, parent2
, pos
)
959 if not parent1
.object
:get_luaentity() then
962 if not parent2
.object
:get_luaentity() then
966 -- custom breed function
967 if parent1
.on_breed
then
968 -- when false, skip going any further
969 if parent1
.on_breed(parent1
, parent2
) == false then
974 local child
= mobs
:spawn_child(pos
, parent1
.name
)
976 local ent_c
= child
:get_luaentity()
979 -- Use texture of one of the parents
980 local p
= math
.random(1, 2)
982 ent_c
.base_texture
= parent1
.base_texture
984 ent_c
.base_texture
= parent2
.base_texture
986 child
:set_properties({
987 textures
= ent_c
.base_texture
990 -- tamed and owned by parents' owner
992 ent_c
.owner
= parent1
.owner
1004 -- find and replace what mob is looking for (grass, wheat etc.)
1005 local replace
= function(self
, pos
)
1007 if not mobs_griefing
1008 or not self
.replace_rate
1009 or not self
.replace_what
1010 or self
.child
== true
1011 or self
.object
:getvelocity().y
~= 0
1012 or random(1, self
.replace_rate
) > 1 then
1016 local what
, with
, y_offset
1018 if type(self
.replace_what
[1]) == "table" then
1020 local num
= random(#self
.replace_what
)
1022 what
= self
.replace_what
[num
][1] or ""
1023 with
= self
.replace_what
[num
][2] or ""
1024 y_offset
= self
.replace_what
[num
][3] or 0
1026 what
= self
.replace_what
1027 with
= self
.replace_with
or ""
1028 y_offset
= self
.replace_offset
or 0
1031 pos
.y
= pos
.y
+ y_offset
1033 if #minetest
.find_nodes_in_area(pos
, pos
, what
) > 0 then
1035 local oldnode
= {name
= what
}
1036 local newnode
= {name
= with
}
1037 local on_replace_return
1039 if self
.on_replace
then
1040 on_replace_return
= self
.on_replace(self
, pos
, oldnode
, newnode
)
1043 if on_replace_return
~= false then
1045 minetest
.set_node(pos
, {name
= with
})
1047 -- when cow/sheep eats grass, replace wool and milk
1048 if self
.gotten
== true then
1050 self
.object
:set_properties(self
)
1057 -- check if daytime and also if mob is docile during daylight hours
1058 local day_docile
= function(self
)
1060 if self
.docile_by_day
== false then
1064 elseif self
.docile_by_day
== true
1065 and self
.time_of_day
> 0.2
1066 and self
.time_of_day
< 0.8 then
1073 local los_switcher
= false
1074 local height_switcher
= false
1076 -- path finding and smart mob routine by rnd, line_of_sight and other edits by Elkien3
1077 local smart_mobs
= function(self
, s
, p
, dist
, dtime
)
1079 local s1
= self
.path
.lastpos
1081 local target_pos
= self
.attack
:get_pos()
1083 -- is it becoming stuck?
1084 if abs(s1
.x
- s
.x
) + abs(s1
.z
- s
.z
) < .5 then
1085 self
.path
.stuck_timer
= self
.path
.stuck_timer
+ dtime
1087 self
.path
.stuck_timer
= 0
1090 self
.path
.lastpos
= {x
= s
.x
, y
= s
.y
, z
= s
.z
}
1092 local use_pathfind
= false
1093 local has_lineofsight
= minetest
.line_of_sight(
1094 {x
= s
.x
, y
= (s
.y
) + .5, z
= s
.z
},
1095 {x
= target_pos
.x
, y
= (target_pos
.y
) + 1.5, z
= target_pos
.z
}, .2)
1097 -- im stuck, search for path
1098 if not has_lineofsight
then
1100 if los_switcher
== true then
1102 los_switcher
= false
1103 end -- cannot see target!
1105 if los_switcher
== false then
1108 use_pathfind
= false
1110 minetest
.after(1, function(self
)
1111 if not self
.object
:get_luaentity() then
1114 if has_lineofsight
then self
.path
.following
= false end
1116 end -- can see target!
1119 if (self
.path
.stuck_timer
> stuck_timeout
and not self
.path
.following
) then
1122 self
.path
.stuck_timer
= 0
1124 minetest
.after(1, function(self
)
1125 if not self
.object
:get_luaentity() then
1128 if has_lineofsight
then self
.path
.following
= false end
1132 if (self
.path
.stuck_timer
> stuck_path_timeout
and self
.path
.following
) then
1135 self
.path
.stuck_timer
= 0
1137 minetest
.after(1, function(self
)
1138 if not self
.object
:get_luaentity() then
1141 if has_lineofsight
then self
.path
.following
= false end
1145 if math
.abs(vector
.subtract(s
,target_pos
).y
) > self
.stepheight
then
1147 if height_switcher
then
1149 height_switcher
= false
1152 if not height_switcher
then
1153 use_pathfind
= false
1154 height_switcher
= true
1158 if use_pathfind
then
1159 -- lets try find a path, first take care of positions
1160 -- since pathfinder is very sensitive
1161 local sheight
= self
.collisionbox
[5] - self
.collisionbox
[2]
1163 -- round position to center of node to avoid stuck in walls
1164 -- also adjust height for player models!
1165 s
.x
= floor(s
.x
+ 0.5)
1166 s
.z
= floor(s
.z
+ 0.5)
1168 local ssight
, sground
= minetest
.line_of_sight(s
, {
1169 x
= s
.x
, y
= s
.y
- 4, z
= s
.z
}, 1)
1171 -- determine node above ground
1176 local p1
= self
.attack
:get_pos()
1178 p1
.x
= floor(p1
.x
+ 0.5)
1179 p1
.y
= floor(p1
.y
+ 0.5)
1180 p1
.z
= floor(p1
.z
+ 0.5)
1182 local dropheight
= 6
1183 if self
.fear_height
~= 0 then dropheight
= self
.fear_height
end
1185 self
.path
.way
= minetest
.find_path(s
, p1
, 16, self
.stepheight
, dropheight
, "A*_noprefetch")
1188 do_attack(self
, self
.attack
)
1190 -- no path found, try something else
1191 if not self
.path
.way
then
1193 self
.path
.following
= false
1195 -- lets make way by digging/building if not accessible
1196 if self
.pathfinding
== 2 and mobs_griefing
then
1198 -- is player higher than mob?
1202 if not minetest
.is_protected(s
, "") then
1204 local ndef1
= minetest
.registered_nodes
[self
.standing_in
]
1206 if ndef1
and (ndef1
.buildable_to
or ndef1
.groups
.liquid
) then
1208 minetest
.set_node(s
, {name
= mobs
.fallback_node
})
1212 local sheight
= math
.ceil(self
.collisionbox
[5]) + 1
1214 -- assume mob is 2 blocks high so it digs above its head
1217 -- remove one block above to make room to jump
1218 if not minetest
.is_protected(s
, "") then
1220 local node1
= node_ok(s
, "air").name
1221 local ndef1
= minetest
.registered_nodes
[node1
]
1224 and node1
~= "ignore"
1226 and not ndef1
.groups
.level
1227 and not ndef1
.groups
.unbreakable
1228 and not ndef1
.groups
.liquid
then
1230 minetest
.set_node(s
, {name
= "air"})
1231 minetest
.add_item(s
, ItemStack(node1
))
1237 self
.object
:setpos({x
= s
.x
, y
= s
.y
+ 2, z
= s
.z
})
1239 else -- dig 2 blocks to make door toward player direction
1241 local yaw1
= self
.object
:get_yaw() + pi
/ 2
1243 x
= s
.x
+ cos(yaw1
),
1248 if not minetest
.is_protected(p1
, "") then
1250 local node1
= node_ok(p1
, "air").name
1251 local ndef1
= minetest
.registered_nodes
[node1
]
1254 and node1
~= "ignore"
1256 and not ndef1
.groups
.level
1257 and not ndef1
.groups
.unbreakable
1258 and not ndef1
.groups
.liquid
then
1260 minetest
.add_item(p1
, ItemStack(node1
))
1261 minetest
.set_node(p1
, {name
= "air"})
1265 node1
= node_ok(p1
, "air").name
1266 ndef1
= minetest
.registered_nodes
[node1
]
1269 and node1
~= "ignore"
1271 and not ndef1
.groups
.level
1272 and not ndef1
.groups
.unbreakable
1273 and not ndef1
.groups
.liquid
then
1275 minetest
.add_item(p1
, ItemStack(node1
))
1276 minetest
.set_node(p1
, {name
= "air"})
1283 -- will try again in 2 second
1284 self
.path
.stuck_timer
= stuck_timeout
- 2
1286 -- frustration! cant find the damn path :(
1287 mob_sound(self
, self
.sounds
.random)
1290 mob_sound(self
, self
.sounds
.war_cry
)
1291 set_velocity(self
, self
.walk_velocity
)
1293 -- follow path now that it has it
1294 self
.path
.following
= true
1301 local specific_attack
= function(list
, what
)
1303 -- no list so attack default (player, animals etc.)
1308 -- found entity on list to attack?
1309 for no
= 1, #list
do
1311 if list
[no
] == what
then
1320 -- monster find someone to attack
1321 local monster_attack
= function(self
)
1323 if self
.type ~= "monster"
1324 or not damage_enabled
1326 or self
.state
== "attack"
1327 or day_docile(self
) then
1331 local s
= self
.object
:get_pos()
1333 local player
, obj
, min_player
1334 local type, name
= "", ""
1335 local min_dist
= self
.view_range
+ 1
1336 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1340 if objs
[n
]:is_player() then
1342 if mobs
.invis
[ objs
[n
]:get_player_name() ] then
1351 obj
= objs
[n
]:get_luaentity()
1356 name
= obj
.name
or ""
1360 -- find specific mob to attack, failing that attack player/npc/animal
1361 if specific_attack(self
.specific_attack
, name
)
1362 and (type == "player" or type == "npc"
1363 or (type == "animal" and self
.attack_animals
== true)) then
1365 p
= player
:get_pos()
1368 dist
= get_distance(p
, s
)
1370 -- aim higher to make looking up hills more realistic
1375 -- choose closest player to attack
1377 and line_of_sight(self
, sp
, p
, 2) == true then
1386 do_attack(self
, min_player
)
1391 -- npc, find closest monster to attack
1392 local npc_attack
= function(self
)
1394 if self
.type ~= "npc"
1395 or not self
.attacks_monsters
1396 or self
.state
== "attack" then
1400 local p
, sp
, obj
, min_player
1401 local s
= self
.object
:get_pos()
1402 local min_dist
= self
.view_range
+ 1
1403 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1407 obj
= objs
[n
]:get_luaentity()
1409 if obj
and obj
.type == "monster" then
1411 p
= obj
.object
:get_pos()
1414 local dist
= get_distance(p
, s
)
1416 -- aim higher to make looking up hills more realistic
1421 and line_of_sight(self
, sp
, p
, 2) == true then
1423 min_player
= obj
.object
1429 do_attack(self
, min_player
)
1435 local specific_runaway
= function(list
, what
)
1437 -- no list so do not run
1442 -- found entity on list to attack?
1443 for no
= 1, #list
do
1445 if list
[no
] == what
then
1454 -- find someone to runaway from
1455 local runaway_from
= function(self
)
1457 if not self
.runaway_from
then
1461 local s
= self
.object
:get_pos()
1463 local player
, obj
, min_player
1464 local type, name
= "", ""
1465 local min_dist
= self
.view_range
+ 1
1466 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1470 if objs
[n
]:is_player() then
1472 if mobs
.invis
[ objs
[n
]:get_player_name() ]
1473 or self
.owner
== objs
[n
]:get_player_name() then
1482 obj
= objs
[n
]:get_luaentity()
1487 name
= obj
.name
or ""
1491 -- find specific mob to runaway from
1492 if name
~= "" and name
~= self
.name
1493 and specific_runaway(self
.runaway_from
, name
) then
1495 p
= player
:get_pos()
1498 -- aim higher to make looking up hills more realistic
1502 dist
= get_distance(p
, s
)
1505 -- choose closest player/mpb to runaway from
1507 and line_of_sight(self
, sp
, p
, 2) == true then
1516 local lp
= player
:get_pos()
1523 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
1529 yaw
= set_yaw(self
, yaw
, 4)
1530 self
.state
= "runaway"
1531 self
.runaway_timer
= 3
1532 self
.following
= nil
1537 -- follow player if owner or holding item, if fish outta water then flop
1538 local follow_flop
= function(self
)
1540 -- find player to follow
1541 if (self
.follow
~= ""
1542 or self
.order
== "follow")
1543 and not self
.following
1544 and self
.state
~= "attack"
1545 and self
.state
~= "runaway" then
1547 local s
= self
.object
:get_pos()
1548 local players
= minetest
.get_connected_players()
1550 for n
= 1, #players
do
1552 if get_distance(players
[n
]:get_pos(), s
) < self
.view_range
1553 and not mobs
.invis
[ players
[n
]:get_player_name() ] then
1555 self
.following
= players
[n
]
1562 if self
.type == "npc"
1563 and self
.order
== "follow"
1564 and self
.state
~= "attack"
1565 and self
.owner
~= "" then
1567 -- npc stop following player if not owner
1570 and self
.owner
~= self
.following
:get_player_name() then
1571 self
.following
= nil
1574 -- stop following player if not holding specific item
1576 and self
.following
:is_player()
1577 and follow_holding(self
, self
.following
) == false then
1578 self
.following
= nil
1583 -- follow that thing
1584 if self
.following
then
1586 local s
= self
.object
:get_pos()
1589 if self
.following
:is_player() then
1591 p
= self
.following
:get_pos()
1593 elseif self
.following
.object
then
1595 p
= self
.following
.object
:get_pos()
1600 local dist
= get_distance(p
, s
)
1602 -- dont follow if out of range
1603 if dist
> self
.view_range
then
1604 self
.following
= nil
1611 local yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1613 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
1615 yaw
= set_yaw(self
, yaw
, 6)
1617 -- anyone but standing npc's can move along
1618 if dist
> self
.reach
1619 and self
.order
~= "stand" then
1621 set_velocity(self
, self
.walk_velocity
)
1623 if self
.walk_chance
~= 0 then
1624 set_animation(self
, "walk")
1627 set_velocity(self
, 0)
1628 set_animation(self
, "stand")
1636 -- swimmers flop when out of their element, and swim again when back in
1638 local s
= self
.object
:get_pos()
1639 if not flight_check(self
, s
) then
1642 self
.object
:setvelocity({x
= 0, y
= -5, z
= 0})
1644 set_animation(self
, "stand")
1647 elseif self
.state
== "flop" then
1648 self
.state
= "stand"
1654 -- dogshoot attack switch and counter function
1655 local dogswitch
= function(self
, dtime
)
1657 -- switch mode not activated
1658 if not self
.dogshoot_switch
1663 self
.dogshoot_count
= self
.dogshoot_count
+ dtime
1665 if (self
.dogshoot_switch
== 1
1666 and self
.dogshoot_count
> self
.dogshoot_count_max
)
1667 or (self
.dogshoot_switch
== 2
1668 and self
.dogshoot_count
> self
.dogshoot_count2_max
) then
1670 self
.dogshoot_count
= 0
1672 if self
.dogshoot_switch
== 1 then
1673 self
.dogshoot_switch
= 2
1675 self
.dogshoot_switch
= 1
1679 return self
.dogshoot_switch
1683 -- execute current state (stand, walk, run, attacks)
1684 local do_states
= function(self
, dtime
)
1686 local yaw
= self
.object
:get_yaw() or 0
1688 if self
.state
== "stand" then
1690 if random(1, 4) == 1 then
1693 local s
= self
.object
:get_pos()
1694 local objs
= minetest
.get_objects_inside_radius(s
, 3)
1698 if objs
[n
]:is_player() then
1699 lp
= objs
[n
]:get_pos()
1704 -- look at any players nearby, otherwise turn randomly
1712 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1714 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1716 yaw
= yaw
+ random(-0.5, 0.5)
1719 yaw
= set_yaw(self
, yaw
, 8)
1722 set_velocity(self
, 0)
1723 set_animation(self
, "stand")
1725 -- npc's ordered to stand stay standing
1726 if self
.type ~= "npc"
1727 or self
.order
~= "stand" then
1729 if self
.walk_chance
~= 0
1730 and self
.facing_fence
~= true
1731 and random(1, 100) <= self
.walk_chance
1732 and is_at_cliff(self
) == false then
1734 set_velocity(self
, self
.walk_velocity
)
1736 set_animation(self
, "walk")
1740 elseif self
.state
== "walk" then
1742 local s
= self
.object
:get_pos()
1745 -- is there something I need to avoid?
1746 if self
.water_damage
> 0
1747 and self
.lava_damage
> 0 then
1749 lp
= minetest
.find_node_near(s
, 1, {"group:water", "group:lava"})
1751 elseif self
.water_damage
> 0 then
1753 lp
= minetest
.find_node_near(s
, 1, {"group:water"})
1755 elseif self
.lava_damage
> 0 then
1757 lp
= minetest
.find_node_near(s
, 1, {"group:lava"})
1762 -- if mob in water or lava then look for land
1763 if (self
.lava_damage
1764 and minetest
.registered_nodes
[self
.standing_in
].groups
.lava
)
1765 or (self
.water_damage
1766 and minetest
.registered_nodes
[self
.standing_in
].groups
.water
) then
1768 lp
= minetest
.find_node_near(s
, 5, {"group:soil", "group:stone",
1769 "group:sand", node_ice
, node_snowblock
})
1771 -- did we find land?
1779 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1781 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1783 -- look towards land and jump/move in that direction
1784 yaw
= set_yaw(self
, yaw
, 6)
1786 set_velocity(self
, self
.walk_velocity
)
1788 yaw
= yaw
+ random(-0.5, 0.5)
1798 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1800 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1803 yaw
= set_yaw(self
, yaw
, 8)
1805 -- otherwise randomly turn
1806 elseif random(1, 100) <= 30 then
1808 yaw
= yaw
+ random(-0.5, 0.5)
1810 yaw
= set_yaw(self
, yaw
, 8)
1813 -- stand for great fall in front
1814 local temp_is_cliff
= is_at_cliff(self
)
1816 if self
.facing_fence
== true
1818 or random(1, 100) <= 30 then
1820 set_velocity(self
, 0)
1821 self
.state
= "stand"
1822 set_animation(self
, "stand")
1824 set_velocity(self
, self
.walk_velocity
)
1826 if flight_check(self
)
1828 and self
.animation
.fly_start
1829 and self
.animation
.fly_end
then
1830 set_animation(self
, "fly")
1832 set_animation(self
, "walk")
1836 -- runaway when punched
1837 elseif self
.state
== "runaway" then
1839 self
.runaway_timer
= self
.runaway_timer
+ 1
1841 -- stop after 5 seconds or when at cliff
1842 if self
.runaway_timer
> 5
1843 or is_at_cliff(self
) then
1844 self
.runaway_timer
= 0
1845 set_velocity(self
, 0)
1846 self
.state
= "stand"
1847 set_animation(self
, "stand")
1849 set_velocity(self
, self
.run_velocity
)
1850 set_animation(self
, "walk")
1853 -- attack routines (explode, dogfight, shoot, dogshoot)
1854 elseif self
.state
== "attack" then
1856 -- calculate distance from mob and enemy
1857 local s
= self
.object
:get_pos()
1858 local p
= self
.attack
:get_pos() or s
1859 local dist
= get_distance(p
, s
)
1861 -- stop attacking if player invisible or out of range
1862 if dist
> self
.view_range
1864 or not self
.attack
:get_pos()
1865 or self
.attack
:get_hp() <= 0
1866 or (self
.attack
:is_player() and mobs
.invis
[ self
.attack
:get_player_name() ]) then
1868 self
.state
= "stand"
1869 set_velocity(self
, 0)
1870 set_animation(self
, "stand")
1872 self
.v_start
= false
1880 if self
.attack_type
== "explode" then
1887 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1889 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
1891 yaw
= set_yaw(self
, yaw
)
1893 local node_break_radius
= self
.explosion_radius
or 1
1894 local entity_damage_radius
= self
.explosion_damage_radius
1895 or (node_break_radius
* 2)
1897 -- start timer when in reach and line of sight
1899 and dist
<= self
.reach
1900 and line_of_sight(self
, s
, p
, 2) then
1905 mob_sound(self
, self
.sounds
.fuse
)
1907 -- stop timer if out of reach or direct line of sight
1908 elseif self
.allow_fuse_reset
1910 and (dist
> self
.reach
1911 or not line_of_sight(self
, s
, p
, 2)) then
1912 self
.v_start
= false
1915 self
.blinkstatus
= false
1916 self
.object
:settexturemod("")
1919 -- walk right up to player unless the timer is active
1920 if self
.v_start
and (self
.stop_to_explode
or dist
< 1.5) then
1921 set_velocity(self
, 0)
1923 set_velocity(self
, self
.run_velocity
)
1926 if self
.animation
and self
.animation
.run_start
then
1927 set_animation(self
, "run")
1929 set_animation(self
, "walk")
1932 if self
.v_start
then
1934 self
.timer
= self
.timer
+ dtime
1935 self
.blinktimer
= (self
.blinktimer
or 0) + dtime
1937 if self
.blinktimer
> 0.2 then
1941 if self
.blinkstatus
then
1942 self
.object
:settexturemod("")
1944 self
.object
:settexturemod("^[brighten")
1947 self
.blinkstatus
= not self
.blinkstatus
1950 if self
.timer
> self
.explosion_timer
then
1952 local pos
= self
.object
:get_pos()
1954 -- dont damage anything if area protected or next to water
1955 if minetest
.find_node_near(pos
, 1, {"group:water"})
1956 or minetest
.is_protected(pos
, "") then
1958 node_break_radius
= 1
1961 self
.object
:remove()
1963 if mobs_griefing
and mod_tnt
and tnt
and tnt
.boom
1964 and not minetest
.is_protected(pos
, "") then
1967 radius
= node_break_radius
,
1968 damage_radius
= entity_damage_radius
,
1969 sound
= self
.sounds
.explode
,
1973 minetest
.sound_play(self
.sounds
.explode
, {
1976 max_hear_distance
= self
.sounds
.distance
or 32
1979 entity_physics(pos
, entity_damage_radius
)
1980 effect(pos
, 32, "tnt_smoke.png", nil, nil, node_break_radius
, 1, 0)
1987 elseif self
.attack_type
== "dogfight"
1988 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 2)
1989 or (self
.attack_type
== "dogshoot" and dist
<= self
.reach
and dogswitch(self
) == 0) then
1992 and dist
> self
.reach
then
1995 local me_y
= floor(p1
.y
)
1997 local p_y
= floor(p2
.y
+ 1)
1998 local v
= self
.object
:getvelocity()
2000 if flight_check(self
, s
) then
2004 self
.object
:setvelocity({
2006 y
= 1 * self
.walk_velocity
,
2010 elseif me_y
> p_y
then
2012 self
.object
:setvelocity({
2014 y
= -1 * self
.walk_velocity
,
2021 self
.object
:setvelocity({
2027 elseif me_y
> p_y
then
2029 self
.object
:setvelocity({
2039 -- rnd: new movement direction
2040 if self
.path
.following
2042 and self
.attack_type
~= "dogshoot" then
2044 -- no paths longer than 50
2045 if #self
.path
.way
> 50
2046 or dist
< self
.reach
then
2047 self
.path
.following
= false
2051 local p1
= self
.path
.way
[1]
2054 self
.path
.following
= false
2058 if abs(p1
.x
-s
.x
) + abs(p1
.z
- s
.z
) < 0.6 then
2059 -- reached waypoint, remove it from queue
2060 table.remove(self
.path
.way
, 1)
2063 -- set new temporary target
2064 p
= {x
= p1
.x
, y
= p1
.y
, z
= p1
.z
}
2072 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2074 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2076 yaw
= set_yaw(self
, yaw
)
2078 -- move towards enemy if beyond mob reach
2079 if dist
> self
.reach
then
2081 -- path finding by rnd
2082 if self
.pathfinding
-- only if mob has pathfinding enabled
2083 and enable_pathfinding
then
2085 smart_mobs(self
, s
, p
, dist
, dtime
)
2088 if is_at_cliff(self
) then
2090 set_velocity(self
, 0)
2091 set_animation(self
, "stand")
2094 if self
.path
.stuck
then
2095 set_velocity(self
, self
.walk_velocity
)
2097 set_velocity(self
, self
.run_velocity
)
2100 if self
.animation
and self
.animation
.run_start
then
2101 set_animation(self
, "run")
2103 set_animation(self
, "walk")
2107 else -- rnd: if inside reach range
2109 self
.path
.stuck
= false
2110 self
.path
.stuck_timer
= 0
2111 self
.path
.following
= false -- not stuck anymore
2113 set_velocity(self
, 0)
2115 if not self
.custom_attack
then
2117 if self
.timer
> 1 then
2121 if self
.double_melee_attack
2122 and random(1, 2) == 1 then
2123 set_animation(self
, "punch2")
2125 set_animation(self
, "punch")
2134 if line_of_sight(self
, p2
, s2
) == true then
2136 -- play attack sound
2137 mob_sound(self
, self
.sounds
.attack
)
2139 -- punch player (or what player is attached to)
2140 local attached
= self
.attack
:get_attach()
2142 self
.attack
= attached
2144 self
.attack
:punch(self
.object
, 1.0, {
2145 full_punch_interval
= 1.0,
2146 damage_groups
= {fleshy
= self
.damage
}
2150 else -- call custom attack every second
2151 if self
.custom_attack
2152 and self
.timer
> 1 then
2156 self
.custom_attack(self
, p
)
2161 elseif self
.attack_type
== "shoot"
2162 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 1)
2163 or (self
.attack_type
== "dogshoot" and dist
> self
.reach
and dogswitch(self
) == 0) then
2168 local dist
= get_distance(p
, s
)
2175 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2177 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2179 yaw
= set_yaw(self
, yaw
)
2181 set_velocity(self
, 0)
2183 if self
.shoot_interval
2184 and self
.timer
> self
.shoot_interval
2185 and random(1, 100) <= 60 then
2188 set_animation(self
, "shoot")
2190 -- play shoot attack sound
2191 mob_sound(self
, self
.sounds
.shoot_attack
)
2193 local p
= self
.object
:get_pos()
2195 p
.y
= p
.y
+ (self
.collisionbox
[2] + self
.collisionbox
[5]) / 2
2197 if minetest
.registered_entities
[self
.arrow
] then
2199 local obj
= minetest
.add_entity(p
, self
.arrow
)
2200 local ent
= obj
:get_luaentity()
2201 local amount
= (vec
.x
* vec
.x
+ vec
.y
* vec
.y
+ vec
.z
* vec
.z
) ^
0.5
2202 local v
= ent
.velocity
or 1 -- or set to default
2205 ent
.owner_id
= tostring(self
.object
) -- add unique owner id to arrow
2207 -- offset makes shoot aim accurate
2208 vec
.y
= vec
.y
+ self
.shoot_offset
2209 vec
.x
= vec
.x
* (v
/ amount
)
2210 vec
.y
= vec
.y
* (v
/ amount
)
2211 vec
.z
= vec
.z
* (v
/ amount
)
2213 obj
:setvelocity(vec
)
2221 -- falling and fall damage
2222 local falling
= function(self
, pos
)
2228 -- floating in water (or falling)
2229 local v
= self
.object
:getvelocity()
2233 -- apply gravity when moving up
2234 self
.object
:setacceleration({
2240 elseif v
.y
<= 0 and v
.y
> self
.fall_speed
then
2242 -- fall downwards at set speed
2243 self
.object
:setacceleration({
2245 y
= self
.fall_speed
,
2249 -- stop accelerating once max fall speed hit
2250 self
.object
:setacceleration({x
= 0, y
= 0, z
= 0})
2253 -- in water then float up
2254 if minetest
.registered_nodes
[node_ok(pos
).name
].groups
.water
then
2256 if self
.floats
== 1 then
2258 self
.object
:setacceleration({
2260 y
= -self
.fall_speed
/ (max(1, v
.y
) ^
2),
2266 -- fall damage onto solid ground
2267 if self
.fall_damage
== 1
2268 and self
.object
:getvelocity().y
== 0 then
2270 local d
= (self
.old_y
or 0) - self
.object
:get_pos().y
2274 self
.health
= self
.health
- floor(d
- 5)
2276 effect(pos
, 5, "tnt_smoke.png", 1, 2, 2, nil)
2278 if check_for_death(self
, "fall", {type = "fall"}) then
2283 self
.old_y
= self
.object
:get_pos().y
2289 -- deal damage and effects when mob punched
2290 local mob_punch
= function(self
, hitter
, tflp
, tool_capabilities
, dir
)
2292 -- custom punch function
2293 if self
.do_punch
then
2295 -- when false skip going any further
2296 if self
.do_punch(self
, hitter
, tflp
, tool_caps
, dir
) == false then
2301 -- error checking when mod profiling is enabled
2302 if not tool_capabilities
then
2303 minetest
.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2307 -- is mob protected?
2308 if self
.protected
and hitter
:is_player()
2309 and minetest
.is_protected(self
.object
:get_pos(), hitter
:get_player_name()) then
2310 minetest
.chat_send_player(hitter
:get_player_name(), S("Mob has been protected!"))
2316 local weapon
= hitter
:get_wielded_item()
2317 local punch_interval
= 1.4
2319 -- calculate mob damage
2321 local armor
= self
.object
:get_armor_groups() or {}
2324 -- quick error check incase it ends up 0 (serialize.h check test)
2330 damage
= cmi
.calculate_damage(self
.object
, hitter
, tflp
, tool_capabilities
, dir
)
2333 for group
,_
in pairs( (tool_capabilities
.damage_groups
or {}) ) do
2335 tmp
= tflp
/ (tool_capabilities
.full_punch_interval
or 1.4)
2343 damage
= damage
+ (tool_capabilities
.damage_groups
[group
] or 0)
2344 * tmp
* ((armor
[group
] or 0) / 100.0)
2348 -- check for tool immunity or special damage
2349 for n
= 1, #self
.immune_to
do
2351 if self
.immune_to
[n
][1] == weapon
:get_name() then
2353 damage
= self
.immune_to
[n
][2] or 0
2359 if damage
<= -1 then
2360 self
.health
= self
.health
- floor(damage
)
2366 local cancel
= cmi
.notify_punch(self
.object
, hitter
, tflp
, tool_capabilities
, dir
, damage
)
2368 if cancel
then return end
2372 if tool_capabilities
then
2373 punch_interval
= tool_capabilities
.full_punch_interval
or 1.4
2376 if weapon
:get_definition()
2377 and weapon
:get_definition().tool_capabilities
then
2379 weapon
:add_wear(floor((punch_interval
/ 75) * 9000))
2380 hitter
:set_wielded_item(weapon
)
2383 -- only play hit sound and show blood effects if damage is 1 or over
2387 if weapon
:get_definition().sounds
~= nil then
2389 local s
= random(0, #weapon
:get_definition().sounds
)
2391 minetest
.sound_play(weapon
:get_definition().sounds
[s
], {
2392 object
= self
.object
, --hitter,
2393 max_hear_distance
= 8
2396 minetest
.sound_play("default_punch", {
2397 object
= self
.object
, --hitter,
2398 max_hear_distance
= 5
2403 if self
.blood_amount
> 0
2404 and not disable_blood
then
2406 local pos
= self
.object
:get_pos()
2408 pos
.y
= pos
.y
+ (-self
.collisionbox
[2] + self
.collisionbox
[5]) * .5
2410 -- do we have a single blood texture or multiple?
2411 if type(self
.blood_texture
) == "table" then
2413 local blood
= self
.blood_texture
[random(1, #self
.blood_texture
)]
2415 effect(pos
, self
.blood_amount
, blood
, nil, nil, 1, nil)
2417 effect(pos
, self
.blood_amount
, self
.blood_texture
, nil, nil, 1, nil)
2422 self
.health
= self
.health
- floor(damage
)
2424 -- exit here if dead, special item check
2425 if weapon
:get_name() == "mobs:pick_lava" then
2426 if check_for_death(self
, "lava", {type = "punch",
2427 puncher
= hitter
}) then
2431 if check_for_death(self
, "hit", {type = "punch",
2432 puncher
= hitter
}) then
2437 -- knock back effect (only on full punch)
2439 and tflp
>= punch_interval
then
2441 local v
= self
.object
:getvelocity()
2442 local r
= 1.4 - min(punch_interval
, 1.4)
2446 -- if already in air then dont go up anymore when hit
2452 -- direction error check
2453 dir
= dir
or {x
= 0, y
= 0, z
= 0}
2455 -- check if tool already has specific knockback value
2456 if tool_capabilities
.damage_groups
["knockback"] then
2457 kb
= tool_capabilities
.damage_groups
["knockback"]
2462 self
.object
:setvelocity({
2468 self
.pause_timer
= 0.25
2470 end -- END if damage
2472 -- if skittish then run away
2473 if self
.runaway
== true then
2475 local lp
= hitter
:get_pos()
2476 local s
= self
.object
:get_pos()
2483 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
2489 yaw
= set_yaw(self
, yaw
, 6)
2490 self
.state
= "runaway"
2491 self
.runaway_timer
= 0
2492 self
.following
= nil
2495 local name
= hitter
:get_player_name() or ""
2497 -- attack puncher and call other mobs for help
2498 if self
.passive
== false
2499 and self
.state
~= "flop"
2500 and self
.child
== false
2501 and hitter
:get_player_name() ~= self
.owner
2502 and not mobs
.invis
[ name
] then
2504 -- attack whoever punched mob
2506 do_attack(self
, hitter
)
2508 -- alert others to the attack
2509 local objs
= minetest
.get_objects_inside_radius(hitter
:get_pos(), self
.view_range
)
2514 obj
= objs
[n
]:get_luaentity()
2518 -- only alert members of same mob
2519 if obj
.group_attack
== true
2520 and obj
.state
~= "attack"
2521 and obj
.owner
~= name
2522 and obj
.name
== self
.name
then
2523 do_attack(obj
, hitter
)
2526 -- have owned mobs attack player threat
2527 if obj
.owner
== name
and obj
.owner_loyal
then
2528 do_attack(obj
, self
.object
)
2536 -- get entity staticdata
2537 local mob_staticdata
= function(self
)
2539 -- remove mob when out of range unless tamed
2541 and self
.can_despawn
2543 and ((not self
.nametag
) or (self
.nametag
== ""))
2544 and self
.lifetimer
<= 20 then
2546 self
.object
:remove()
2551 self
.remove_ok
= true
2553 self
.following
= nil
2554 self
.state
= "stand"
2556 -- used to rotate older mobs
2558 and self
.drawtype
== "side" then
2559 self
.rotate
= math
.rad(90)
2563 self
.serialized_cmi_components
= cmi
.serialize_components(self
._cmi_components
)
2568 for _
,stat
in pairs(self
) do
2570 local t
= type(stat
)
2575 and _
~= "_cmi_components" then
2580 return minetest
.serialize(tmp
)
2584 -- activate mob and reload settings
2585 local mob_activate
= function(self
, staticdata
, def
, dtime
)
2587 -- remove monsters in peaceful mode
2588 if self
.type == "monster"
2589 and peaceful_only
then
2591 self
.object
:remove()
2596 -- load entity variables
2597 local tmp
= minetest
.deserialize(staticdata
)
2600 for _
,stat
in pairs(tmp
) do
2605 -- select random texture, set model and size
2606 if not self
.base_texture
then
2608 -- compatiblity with old simple mobs textures
2609 if type(def
.textures
[1]) == "string" then
2610 def
.textures
= {def
.textures
}
2613 self
.base_texture
= def
.textures
[random(1, #def
.textures
)]
2614 self
.base_mesh
= def
.mesh
2615 self
.base_size
= self
.visual_size
2616 self
.base_colbox
= self
.collisionbox
2617 self
.base_selbox
= self
.selectionbox
2620 -- for current mobs that dont have this set
2621 if not self
.base_selbox
then
2622 self
.base_selbox
= self
.selectionbox
or self
.base_colbox
2625 -- set texture, model and size
2626 local textures
= self
.base_texture
2627 local mesh
= self
.base_mesh
2628 local vis_size
= self
.base_size
2629 local colbox
= self
.base_colbox
2630 local selbox
= self
.base_selbox
2632 -- specific texture if gotten
2633 if self
.gotten
== true
2634 and def
.gotten_texture
then
2635 textures
= def
.gotten_texture
2638 -- specific mesh if gotten
2639 if self
.gotten
== true
2640 and def
.gotten_mesh
then
2641 mesh
= def
.gotten_mesh
2644 -- set child objects to half size
2645 if self
.child
== true then
2648 x
= self
.base_size
.x
* .5,
2649 y
= self
.base_size
.y
* .5,
2652 if def
.child_texture
then
2653 textures
= def
.child_texture
[1]
2657 self
.base_colbox
[1] * .5,
2658 self
.base_colbox
[2] * .5,
2659 self
.base_colbox
[3] * .5,
2660 self
.base_colbox
[4] * .5,
2661 self
.base_colbox
[5] * .5,
2662 self
.base_colbox
[6] * .5
2665 self
.base_selbox
[1] * .5,
2666 self
.base_selbox
[2] * .5,
2667 self
.base_selbox
[3] * .5,
2668 self
.base_selbox
[4] * .5,
2669 self
.base_selbox
[5] * .5,
2670 self
.base_selbox
[6] * .5
2674 if self
.health
== 0 then
2675 self
.health
= random (self
.hp_min
, self
.hp_max
)
2680 self
.path
.way
= {} -- path to follow, table of positions
2681 self
.path
.lastpos
= {x
= 0, y
= 0, z
= 0}
2682 self
.path
.stuck
= false
2683 self
.path
.following
= false -- currently following path?
2684 self
.path
.stuck_timer
= 0 -- if stuck for too long search for path
2687 self
.object
:set_armor_groups({immortal
= 1, fleshy
= self
.armor
})
2688 self
.old_y
= self
.object
:get_pos().y
2689 self
.old_health
= self
.health
2690 self
.sounds
.distance
= self
.sounds
.distance
or 10
2691 self
.textures
= textures
2693 self
.collisionbox
= colbox
2694 self
.selectionbox
= selbox
2695 self
.visual_size
= vis_size
2696 self
.standing_in
= ""
2698 -- check existing nametag
2699 if not self
.nametag
then
2700 self
.nametag
= def
.nametag
2703 -- set anything changed above
2704 self
.object
:set_properties(self
)
2705 set_yaw(self
, (random(0, 360) - 180) / 180 * pi
, 6)
2707 set_animation(self
, "stand")
2709 -- run on_spawn function if found
2710 if self
.on_spawn
and not self
.on_spawn_run
then
2711 if self
.on_spawn(self
) then
2712 self
.on_spawn_run
= true -- if true, set flag to run once only
2716 -- run after_activate
2717 if def
.after_activate
then
2718 def
.after_activate(self
, staticdata
, def
, dtime
)
2722 self
._cmi_components
= cmi
.activate_components(self
.serialized_cmi_components
)
2723 cmi
.notify_activate(self
.object
, dtime
)
2728 -- main mob function
2729 local mob_step
= function(self
, dtime
)
2732 cmi
.notify_step(self
.object
, dtime
)
2735 local pos
= self
.object
:get_pos()
2738 -- Despawning: when lifetimer expires, remove mob
2740 and self
.can_despawn
== true
2741 and ((not self
.nametag
) or (self
.nametag
== "")) then
2743 -- TODO: Finish up implementation of despawning rules
2745 self
.lifetimer
= self
.lifetimer
- dtime
2747 if self
.lifetimer
<= 0 then
2749 -- only despawn away from player
2750 local objs
= minetest
.get_objects_inside_radius(pos
, 32)
2754 if objs
[n
]:is_player() then
2762 self
.object
:remove()
2770 -- smooth rotation by ThomasMonroe314
2772 if self
.delay
and self
.delay
> 0 then
2774 local yaw
= self
.object
:get_yaw()
2776 if self
.delay
== 1 then
2777 yaw
= self
.target_yaw
2779 local dif
= abs(yaw
- self
.target_yaw
)
2781 if yaw
> self
.target_yaw
then
2784 dif
= 2 * pi
- dif
-- need to add
2785 yaw
= yaw
+ dif
/ self
.delay
2787 yaw
= yaw
- dif
/ self
.delay
-- need to subtract
2790 elseif yaw
< self
.target_yaw
then
2794 yaw
= yaw
- dif
/ self
.delay
-- need to subtract
2796 yaw
= yaw
+ dif
/ self
.delay
-- need to add
2800 if yaw
> (pi
* 2) then yaw
= yaw
- (pi
* 2) end
2801 if yaw
< 0 then yaw
= yaw
+ (pi
* 2) end
2804 self
.delay
= self
.delay
- 1
2805 self
.object
:set_yaw(yaw
)
2811 if self
.pause_timer
> 0 then
2813 self
.pause_timer
= self
.pause_timer
- dtime
2818 -- run custom function (defined in mob lua file)
2819 if self
.do_custom
then
2821 -- when false skip going any further
2822 if self
.do_custom(self
, dtime
) == false then
2828 self
.timer
= self
.timer
+ dtime
2830 if self
.state
~= "attack" then
2832 if self
.timer
< 1 then
2839 -- never go over 100
2840 if self
.timer
> 100 then
2844 -- mob plays random sound at times
2845 if random(1, 100) == 1 then
2846 mob_sound(self
, self
.sounds
.random)
2849 -- environmental damage timer (every 1 second)
2850 self
.env_damage_timer
= self
.env_damage_timer
+ dtime
2852 if (self
.state
== "attack" and self
.env_damage_timer
> 1)
2853 or self
.state
~= "attack" then
2855 self
.env_damage_timer
= 0
2857 -- check for environmental damage (water, fire, lava etc.)
2860 -- node replace check (cow eats grass etc.)
2864 monster_attack(self
)
2872 do_states(self
, dtime
)
2881 -- default function when mobs are blown up with TNT
2882 local do_tnt
= function(obj
, damage
)
2884 obj
.object
:punch(obj
.object
, 1.0, {
2885 full_punch_interval
= 1.0,
2886 damage_groups
= {fleshy
= damage
},
2889 return false, true, {}
2893 mobs
.spawning_mobs
= {}
2895 -- Code to execute before custom on_rightclick handling
2896 local on_rightclick_prefix
= function(self
, clicker
)
2897 local item
= clicker
:get_wielded_item()
2899 -- Name mob with nametag
2900 if not self
.ignores_nametag
and item
:get_name() == "mcl_mobs:nametag" then
2902 local tag = item
:get_meta():get_string("name")
2904 if string.len(tag) > MAX_MOB_NAME_LENGTH
then
2905 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH
)
2911 if not mobs
.is_creative(clicker
:get_player_name()) then
2913 clicker
:set_wielded_item(item
)
2922 local create_mob_on_rightclick
= function(on_rightclick
)
2923 return function(self
, clicker
)
2924 local stop
= on_rightclick_prefix(self
, clicker
)
2925 if (not stop
) and (on_rightclick
) then
2926 on_rightclick(self
, clicker
)
2931 -- register mob entity
2932 function mobs
:register_mob(name
, def
)
2934 mobs
.spawning_mobs
[name
] = true
2937 if def
.can_despawn
~= nil then
2938 can_despawn
= def
.can_despawn
2940 if def
.type == "monster" then
2946 minetest
.register_entity(name
, {
2948 stepheight
= def
.stepheight
or 1.1, -- was 0.6
2951 attack_type
= def
.attack_type
,
2953 fly_in
= def
.fly_in
or "air",
2954 owner
= def
.owner
or "",
2955 order
= def
.order
or "",
2956 on_die
= def
.on_die
,
2957 do_custom
= def
.do_custom
,
2958 jump_height
= def
.jump_height
or 4, -- was 6
2959 drawtype
= def
.drawtype
, -- DEPRECATED, use rotate instead
2960 rotate
= math
.rad(def
.rotate
or 0), -- 0=front, 90=side, 180=back, 270=side2
2961 lifetimer
= def
.lifetimer
or 57.73,
2962 hp_min
= max(1, (def
.hp_min
or 5) * difficulty
),
2963 hp_max
= max(1, (def
.hp_max
or 10) * difficulty
),
2965 collisionbox
= def
.collisionbox
or {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25},
2966 selectionbox
= def
.selectionbox
or def
.collisionbox
,
2967 visual
= def
.visual
,
2968 visual_size
= def
.visual_size
or {x
= 1, y
= 1},
2970 makes_footstep_sound
= def
.makes_footstep_sound
or false,
2971 view_range
= def
.view_range
or 5,
2972 walk_velocity
= def
.walk_velocity
or 1,
2973 run_velocity
= def
.run_velocity
or 2,
2974 damage
= max(0, (def
.damage
or 0) * difficulty
),
2975 light_damage
= def
.light_damage
or 0,
2976 sunlight_damage
= def
.sunlight_damage
or 0,
2977 water_damage
= def
.water_damage
or 0,
2978 lava_damage
= def
.lava_damage
or 0,
2979 suffocation
= def
.suffocation
or 2,
2980 fall_damage
= def
.fall_damage
or 1,
2981 fall_speed
= def
.fall_speed
or -10, -- must be lower than -2 (default: -10)
2982 drops
= def
.drops
or {},
2983 armor
= def
.armor
or 100,
2984 on_rightclick
= create_mob_on_rightclick(def
.on_rightclick
),
2986 shoot_interval
= def
.shoot_interval
,
2987 sounds
= def
.sounds
or {},
2988 animation
= def
.animation
,
2989 follow
= def
.follow
,
2990 jump
= def
.jump
~= false,
2991 walk_chance
= def
.walk_chance
or 50,
2992 attacks_monsters
= def
.attacks_monsters
or false,
2993 group_attack
= def
.group_attack
or false,
2994 passive
= def
.passive
or false,
2995 knock_back
= def
.knock_back
~= false,
2996 blood_amount
= def
.blood_amount
or 5,
2997 blood_texture
= def
.blood_texture
or "mobs_blood.png",
2998 shoot_offset
= def
.shoot_offset
or 0,
2999 floats
= def
.floats
or 1, -- floats in water by default
3000 replace_rate
= def
.replace_rate
,
3001 replace_what
= def
.replace_what
,
3002 replace_with
= def
.replace_with
,
3003 replace_offset
= def
.replace_offset
or 0,
3004 on_replace
= def
.on_replace
,
3006 env_damage_timer
= 0, -- only used when state = "attack"
3014 reach
= def
.reach
or 3,
3016 texture_list
= def
.textures
,
3017 child_texture
= def
.child_texture
,
3018 docile_by_day
= def
.docile_by_day
or false,
3020 fear_height
= def
.fear_height
or 0,
3021 runaway
= def
.runaway
,
3023 pathfinding
= def
.pathfinding
,
3024 immune_to
= def
.immune_to
or {},
3025 explosion_radius
= def
.explosion_radius
,
3026 explosion_damage_radius
= def
.explosion_damage_radius
,
3027 explosion_timer
= def
.explosion_timer
or 3,
3028 allow_fuse_reset
= def
.allow_fuse_reset
~= false,
3029 stop_to_explode
= def
.stop_to_explode
~= false,
3030 custom_attack
= def
.custom_attack
,
3031 double_melee_attack
= def
.double_melee_attack
,
3032 dogshoot_switch
= def
.dogshoot_switch
,
3034 dogshoot_count_max
= def
.dogshoot_count_max
or 5,
3035 dogshoot_count2_max
= def
.dogshoot_count2_max
or (def
.dogshoot_count_max
or 5),
3036 attack_animals
= def
.attack_animals
or false,
3037 specific_attack
= def
.specific_attack
,
3038 runaway_from
= def
.runaway_from
,
3039 owner_loyal
= def
.owner_loyal
,
3040 facing_fence
= false,
3044 ignores_nametag
= def
.ignores_nametag
or false,
3045 rain_damage
= def
.rain_damage
or 0,
3046 can_despawn
= can_despawn
,
3048 on_spawn
= def
.on_spawn
,
3050 on_blast
= def
.on_blast
or do_tnt
,
3054 do_punch
= def
.do_punch
,
3056 on_punch
= mob_punch
,
3058 on_breed
= def
.on_breed
,
3060 on_grown
= def
.on_grown
,
3062 on_activate
= function(self
, staticdata
, dtime
)
3063 return mob_activate(self
, staticdata
, def
, dtime
)
3066 get_staticdata
= function(self
)
3067 return mob_staticdata(self
)
3072 end -- END mobs:register_mob function
3075 -- count how many mobs of one type are inside an area
3076 local count_mobs
= function(pos
, type)
3080 local objs
= minetest
.get_objects_inside_radius(pos
, aoc_range
)
3084 if not objs
[n
]:is_player() then
3086 local obj
= objs
[n
]:get_luaentity()
3088 -- count mob type and add to total also
3089 if obj
and obj
.name
and obj
.name
== type then
3091 num_type
= num_type
+ 1
3092 num_total
= num_total
+ 1
3094 -- add to total mobs
3095 elseif obj
and obj
.name
and obj
.health
~= nil then
3097 num_total
= num_total
+ 1
3102 return num_type
, num_total
3108 function mobs
:spawn_abm_check(pos
, node
, name
)
3109 -- global function to add additional spawn checks
3110 -- return true to stop spawning mob
3114 function mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
,
3115 interval
, chance
, aoc
, min_height
, max_height
, day_toggle
, on_spawn
)
3117 -- Do mobs spawn at all?
3118 if not mobs_spawn
then
3122 -- chance/spawn number override in minetest.conf for registered mob
3123 local numbers
= minetest
.settings
:get(name
)
3126 numbers
= numbers
:split(",")
3127 chance
= tonumber(numbers
[1]) or chance
3128 aoc
= tonumber(numbers
[2]) or aoc
3131 minetest
.log("warning", string.format("[mobs] %s has spawning disabled", name
))
3135 minetest
.log("action",
3136 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name
, chance
, aoc
))
3140 minetest
.register_abm({
3142 label
= name
.. " spawning",
3144 neighbors
= neighbors
,
3145 interval
= interval
,
3146 chance
= max(1, (chance
* mob_chance_multiplier
)),
3149 action
= function(pos
, node
, active_object_count
, active_object_count_wider
)
3151 -- is mob actually registered?
3152 if not mobs
.spawning_mobs
[name
]
3153 or not minetest
.registered_entities
[name
] then
3157 -- additional custom checks for spawning mob
3158 if mobs
:spawn_abm_check(pos
, node
, name
) == true then
3162 -- do not spawn if too many of same mob in area
3163 if active_object_count_wider
>= max_per_block
3164 or count_mobs(pos
, name
) >= aoc
then
3165 -- too many entities
3169 -- if toggle set to nil then ignore day/night check
3170 if day_toggle
~= nil then
3172 local tod
= (minetest
.get_timeofday() or 0) * 24000
3174 if tod
> 4500 and tod
< 19500 then
3175 -- daylight, but mob wants night
3176 if day_toggle
== false then
3181 -- night time but mob wants day
3182 if day_toggle
== true then
3192 -- only spawn away from player
3193 local objs
= minetest
.get_objects_inside_radius(pos
, 10)
3197 if objs
[n
]:is_player() then
3203 -- mobs cannot spawn in protected areas when enabled
3204 if not spawn_protected
3205 and minetest
.is_protected(pos
, "") then
3209 -- are we spawning within height limits?
3210 if pos
.y
> max_height
3211 or pos
.y
< min_height
then
3215 -- are light levels ok?
3216 local light
= minetest
.get_node_light(pos
)
3218 or light
> max_light
3219 or light
< min_light
then
3223 -- do we have enough height clearance to spawn mob?
3224 local ent
= minetest
.registered_entities
[name
]
3225 local height
= max(0, math
.ceil(ent
.collisionbox
[5] - ent
.collisionbox
[2]) - 1)
3227 for n
= 0, height
do
3229 local pos2
= {x
= pos
.x
, y
= pos
.y
+ n
, z
= pos
.z
}
3231 if minetest
.registered_nodes
[node_ok(pos2
).name
].walkable
== true then
3237 -- spawn mob half block higher than ground
3240 local mob
= minetest
.add_entity(pos
, name
)
3244 local ent
= mob
:get_luaentity()
3253 -- compatibility with older mob registration
3254 function mobs
:register_spawn(name
, nodes
, max_light
, min_light
, chance
, active_object_count
, max_height
, day_toggle
)
3256 mobs
:spawn_specific(name
, nodes
, {"air"}, min_light
, max_light
, 30,
3257 chance
, active_object_count
, -31000, max_height
, day_toggle
)
3261 -- MarkBu's spawn function
3262 function mobs
:spawn(def
)
3264 local name
= def
.name
3265 local nodes
= def
.nodes
or {"group:soil", "group:stone"}
3266 local neighbors
= def
.neighbors
or {"air"}
3267 local min_light
= def
.min_light
or 0
3268 local max_light
= def
.max_light
or 15
3269 local interval
= def
.interval
or 30
3270 local chance
= def
.chance
or 5000
3271 local active_object_count
= def
.active_object_count
or 1
3272 local min_height
= def
.min_height
or -31000
3273 local max_height
= def
.max_height
or 31000
3274 local day_toggle
= def
.day_toggle
3275 local on_spawn
= def
.on_spawn
3277 mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
, interval
,
3278 chance
, active_object_count
, min_height
, max_height
, day_toggle
, on_spawn
)
3282 -- register arrow for shoot attack
3283 function mobs
:register_arrow(name
, def
)
3285 if not name
or not def
then return end -- errorcheck
3287 minetest
.register_entity(name
, {
3290 visual
= def
.visual
,
3291 visual_size
= def
.visual_size
,
3292 textures
= def
.textures
,
3293 velocity
= def
.velocity
,
3294 hit_player
= def
.hit_player
,
3295 hit_node
= def
.hit_node
,
3296 hit_mob
= def
.hit_mob
,
3297 drop
= def
.drop
or false, -- drops arrow as registered item when true
3298 collisionbox
= {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3301 owner_id
= def
.owner_id
,
3302 rotate
= def
.rotate
,
3303 automatic_face_movement_dir
= def
.rotate
3304 and (def
.rotate
- (pi
/ 180)) or false,
3306 on_activate
= def
.on_activate
,
3308 on_step
= def
.on_step
or function(self
, dtime
)
3310 self
.timer
= self
.timer
+ 1
3312 local pos
= self
.object
:get_pos()
3316 or not within_limits(pos
, 0) then
3318 self
.object
:remove();
3323 -- does arrow have a tail (fireball)
3326 and def
.tail_texture
then
3328 minetest
.add_particle({
3330 velocity
= {x
= 0, y
= 0, z
= 0},
3331 acceleration
= {x
= 0, y
= 0, z
= 0},
3332 expirationtime
= def
.expire
or 0.25,
3333 collisiondetection
= false,
3334 texture
= def
.tail_texture
,
3335 size
= def
.tail_size
or 5,
3336 glow
= def
.glow
or 0,
3340 if self
.hit_node
then
3342 local node
= node_ok(pos
).name
3344 if minetest
.registered_nodes
[node
].walkable
then
3346 self
.hit_node(self
, pos
, node
)
3348 if self
.drop
== true then
3352 self
.lastpos
= (self
.lastpos
or pos
)
3354 minetest
.add_item(self
.lastpos
, self
.object
:get_luaentity().name
)
3357 self
.object
:remove();
3363 if self
.hit_player
or self
.hit_mob
then
3365 for _
,player
in pairs(minetest
.get_objects_inside_radius(pos
, 1.0)) do
3368 and player
:is_player() then
3370 self
.hit_player(self
, player
)
3371 self
.object
:remove();
3375 local entity
= player
:get_luaentity()
3379 and entity
._cmi_is_mob
== true
3380 and tostring(player
) ~= self
.owner_id
3381 and entity
.name
~= self
.object
:get_luaentity().name
then
3383 self
.hit_mob(self
, player
)
3385 self
.object
:remove();
3398 -- compatibility function
3399 function mobs
:explosion(pos
, radius
)
3400 local self
= {sounds
= {}}
3401 self
.sounds
.explode
= "tnt_explode"
3402 mobs
:boom(self
, pos
, radius
)
3406 -- no damage to nodes explosion
3407 function mobs
:safe_boom(self
, pos
, radius
)
3409 minetest
.sound_play(self
.sounds
and self
.sounds
.explode
or "tnt_explode", {
3412 max_hear_distance
= self
.sounds
and self
.sounds
.distance
or 32
3415 entity_physics(pos
, radius
)
3416 effect(pos
, 32, "tnt_smoke.png", radius
* 3, radius
* 5, radius
, 1, 0)
3420 -- make explosion with protection and tnt mod check
3421 function mobs
:boom(self
, pos
, radius
)
3424 and mod_tnt
and tnt
and tnt
.boom
3425 and not minetest
.is_protected(pos
, "") then
3429 damage_radius
= radius
,
3430 sound
= self
.sounds
and self
.sounds
.explode
,
3431 explode_center
= true,
3434 mobs
:safe_boom(self
, pos
, radius
)
3439 -- Register spawn eggs
3441 -- Note: This also introduces the “spawn_egg” group:
3442 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3443 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3444 function mobs
:register_egg(mob
, desc
, background
, addegg
, no_creative
)
3446 local grp
= {spawn_egg
= 1}
3448 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3449 if creative
and no_creative
== true then
3450 grp
.not_in_creative_inventory
= 1
3453 local invimg
= background
3456 invimg
= "mobs_chicken_egg.png^(" .. invimg
..
3457 "^[mask:mobs_chicken_egg_overlay.png)"
3460 -- register old stackable mob egg
3461 minetest
.register_craftitem(mob
, {
3464 inventory_image
= invimg
,
3467 _doc_items_longdesc
= "This allows you to place a single mob.",
3468 _doc_items_usagehelp
= "Just place it where you want the mob to appear. Animals will spawn tamed, unless you hold down the sneak key while placing. If you place this on a mob spawner, you change the mob it spawns.",
3470 on_place
= function(itemstack
, placer
, pointed_thing
)
3472 local pos
= pointed_thing
.above
3474 -- am I clicking on something with existing on_rightclick function?
3475 local under
= minetest
.get_node(pointed_thing
.under
)
3476 local def
= minetest
.registered_nodes
[under
.name
]
3477 if def
and def
.on_rightclick
then
3478 return def
.on_rightclick(pointed_thing
.under
, under
, placer
, itemstack
)
3482 and within_limits(pos
, 0)
3483 and not minetest
.is_protected(pos
, placer
:get_player_name()) then
3485 local name
= placer
:get_player_name()
3486 local privs
= minetest
.get_player_privs(name
)
3487 if not privs
.maphack
then
3488 minetest
.chat_send_player(name
, "You need the “maphack” privilege to change the mob spawner.")
3491 if mod_mobspawners
and under
.name
== "mcl_mobspawners:spawner" then
3492 mcl_mobspawners
.setup_spawner(pointed_thing
.under
, itemstack
:get_name())
3493 if not minetest
.settings
:get_bool("creative_mode") then
3494 itemstack
:take_item()
3499 if not minetest
.registered_entities
[mob
] then
3505 local mob
= minetest
.add_entity(pos
, mob
)
3506 local ent
= mob
:get_luaentity()
3508 -- don't set owner if monster or sneak pressed
3509 if ent
.type ~= "monster"
3510 and not placer
:get_player_control().sneak
then
3511 ent
.owner
= placer
:get_player_name()
3516 local nametag
= itemstack
:get_meta():get_string("name")
3517 if nametag
~= "" then
3518 if string.len(nametag
) > MAX_MOB_NAME_LENGTH
then
3519 nametag
= string.sub(nametag
, 1, MAX_MOB_NAME_LENGTH
)
3521 ent
.nametag
= nametag
3525 -- if not in creative then take item
3526 if not mobs
.is_creative(placer
:get_player_name()) then
3527 itemstack
:take_item()
3538 -- No-op in MCL2 (capturing mobs is not possible).
3539 -- Provided for compability with Mobs Redo
3540 function mobs
:capture_mob(self
, clicker
, chance_hand
, chance_net
, chance_lasso
, force_take
, replacewith
)
3545 -- protect tamed mob with rune item
3546 function mobs
:protect(self
, clicker
)
3547 local name
= clicker
:get_player_name()
3548 local tool
= clicker
:get_wielded_item()
3550 if tool
:get_name() ~= "mobs:protector" then
3554 if self
.tamed
== false then
3555 minetest
.chat_send_player(name
, S("Not tamed!"))
3556 return true -- false
3559 if self
.protected
== true then
3560 minetest
.chat_send_player(name
, S("Already protected!"))
3561 return true -- false
3564 if not mobs
.is_creative(clicker
:get_player_name()) then
3565 tool
:take_item() -- take 1 protection rune
3566 clicker
:set_wielded_item(tool
)
3569 self
.protected
= true
3571 local pos
= self
.object
:get_pos()
3572 pos
.y
= pos
.y
+ self
.collisionbox
[2] + 0.5
3574 effect(self
.object
:get_pos(), 25, "mobs_protect_particle.png", 0.5, 4, 2, 15)
3576 mob_sound(self
, "mobs_spell")
3585 -- feeding, taming and breeding (thanks blert2112)
3586 function mobs
:feed_tame(self
, clicker
, feed_count
, breed
, tame
)
3587 if not self
.follow
then
3591 -- can eat/tame with item in hand
3592 if follow_holding(self
, clicker
) then
3594 -- if not in creative then take item
3595 if not mobs
.is_creative(clicker
:get_player_name()) then
3597 local item
= clicker
:get_wielded_item()
3601 clicker
:set_wielded_item(item
)
3605 self
.health
= self
.health
+ 4
3607 if self
.health
>= self
.hp_max
then
3609 self
.health
= self
.hp_max
3611 if self
.htimer
< 1 then
3616 self
.object
:set_hp(self
.health
)
3620 -- make children grow quicker
3621 if self
.child
== true then
3623 self
.hornytimer
= self
.hornytimer
+ 20
3629 self
.food
= (self
.food
or 0) + 1
3630 if self
.food
>= feed_count
then
3634 if breed
and self
.hornytimer
== 0 then
3644 if not self
.owner
or self
.owner
== "" then
3645 self
.owner
= clicker
:get_player_name()
3649 -- make sound when fed so many times
3650 mob_sound(self
, self
.sounds
.random)
3660 function mobs
:spawn_child(pos
, mob_type
)
3661 local child
= minetest
.add_entity(pos
, mob_type
)
3666 local ent
= child
:get_luaentity()
3667 effect(pos
, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
3672 -- using specific child texture (if found)
3673 if ent
.child_texture
then
3674 textures
= ent
.child_texture
[1]
3677 -- and resize to half height
3678 child
:set_properties({
3679 textures
= textures
,
3681 x
= ent
.base_size
.x
* .5,
3682 y
= ent
.base_size
.y
* .5,
3685 ent
.base_colbox
[1] * .5,
3686 ent
.base_colbox
[2] * .5,
3687 ent
.base_colbox
[3] * .5,
3688 ent
.base_colbox
[4] * .5,
3689 ent
.base_colbox
[5] * .5,
3690 ent
.base_colbox
[6] * .5,
3693 ent
.base_selbox
[1] * .5,
3694 ent
.base_selbox
[2] * .5,
3695 ent
.base_selbox
[3] * .5,
3696 ent
.base_selbox
[4] * .5,
3697 ent
.base_selbox
[5] * .5,
3698 ent
.base_selbox
[6] * .5,
3706 -- compatibility function for old entities to new modpack entities
3707 function mobs
:alias_mob(old_name
, new_name
)
3710 minetest
.register_alias(old_name
, new_name
)
3713 minetest
.register_entity(":" .. old_name
, {
3717 on_step
= function(self
)
3719 if minetest
.registered_entities
[new_name
] then
3720 minetest
.add_entity(self
.object
:get_pos(), new_name
)
3723 self
.object
:remove()