6 mobs
.version
= "20180328"
8 local MAX_MOB_NAME_LENGTH
= 30
11 local MP
= minetest
.get_modpath(minetest
.get_current_modname())
12 local S
, NS
= dofile(MP
.. "/intllib.lua")
17 local use_cmi
= minetest
.global_exists("cmi")
20 -- Invisibility mod check
22 if minetest
.global_exists("invisibility") then
23 mobs
.invis
= invisibility
28 local creative_mode_cache
= minetest
.settings
:get_bool("creative_mode")
29 function mobs
.is_creative(name
)
30 return creative_mode_cache
or minetest
.check_player_privs(name
, {creative
= true})
34 -- localize math functions
36 local square
= math
.sqrt
42 local atann
= math
.atan
43 local random = math
.random
44 local floor = math
.floor
45 local atan = function(x
)
46 if not x
or x
~= x
then
47 --error("atan bassed NaN")
56 local damage_enabled
= minetest
.settings
:get_bool("enable_damage")
57 local mobs_spawn
= minetest
.settings
:get_bool("mobs_spawn") ~= false
58 local peaceful_only
= minetest
.settings
:get_bool("only_peaceful_mobs")
59 local disable_blood
= minetest
.settings
:get_bool("mobs_disable_blood")
60 local mobs_drop_items
= minetest
.settings
:get_bool("mobs_drop_items") ~= false
61 local mobs_griefing
= minetest
.settings
:get_bool("mobs_griefing") ~= false
62 local creative
= minetest
.settings
:get_bool("creative_mode")
63 local spawn_protected
= minetest
.settings
:get_bool("mobs_spawn_protected") ~= false
64 local remove_far
= minetest
.settings
:get_bool("remove_far_mobs")
65 local difficulty
= tonumber(minetest
.settings
:get("mob_difficulty")) or 1.0
66 local show_health
= false --minetest.settings:get_bool("mob_show_health") ~= false
67 local max_per_block
= tonumber(minetest
.settings
:get("max_objects_per_block") or 99)
68 local mob_chance_multiplier
= tonumber(minetest
.settings
:get("mob_chance_multiplier") or 1)
70 -- Peaceful mode message so players will know there are no monsters
72 minetest
.register_on_joinplayer(function(player
)
73 minetest
.chat_send_player(player
:get_player_name(),
74 S("** Peaceful Mode Active - No Monsters Will Spawn"))
78 -- calculate aoc range for mob count
79 local aosrb
= tonumber(minetest
.settings
:get("active_object_send_range_blocks"))
80 local abr
= tonumber(minetest
.settings
:get("active_block_range"))
81 local aoc_range
= max(aosrb
, abr
) * 16
83 -- pathfinding settings
84 local enable_pathfinding
= true
85 local stuck_timeout
= 3 -- how long before mob gets stuck in place and starts searching
86 local stuck_path_timeout
= 10 -- how long will mob follow path before giving up
89 local node_fire
= "mcl_fire:fire"
90 local node_permanent_flame
= "mcl_fire:eternal_fire"
91 local node_ice
= "mcl_core:ice"
92 local node_snowblock
= "mcl_core:snowblock"
93 local node_snow
= "mcl_core:snow"
94 mobs
.fallback_node
= minetest
.registered_aliases
["mapgen_dirt"] or "mcl_core:dirt"
98 local mob_sound
= function(self
, sound
)
101 minetest
.sound_play(sound
, {
102 object
= self
.object
,
104 max_hear_distance
= self
.sounds
.distance
111 local do_attack
= function(self
, player
)
113 if self
.state
== "attack" then
118 self
.state
= "attack"
120 if random(0, 100) < 90 then
121 mob_sound(self
, self
.sounds
.war_cry
)
126 -- move mob in facing direction
127 local set_velocity
= function(self
, v
)
129 local yaw
= (self
.object
:get_yaw() or 0) + self
.rotate
131 self
.object
:setvelocity({
133 y
= self
.object
:getvelocity().y
,
139 -- calculate mob velocity
140 local get_velocity
= function(self
)
142 local v
= self
.object
:getvelocity()
144 return (v
.x
* v
.x
+ v
.z
* v
.z
) ^
0.5
148 -- set and return valid yaw
149 local set_yaw
= function(self
, yaw
)
151 if not yaw
or yaw
~= yaw
then
161 -- set defined animation
162 local set_animation
= function(self
, anim
)
164 if not self
.animation
165 or not anim
then return end
167 self
.animation
.current
= self
.animation
.current
or ""
169 if anim
== self
.animation
.current
170 or not self
.animation
[anim
.. "_start"]
171 or not self
.animation
[anim
.. "_end"] then
175 self
.animation
.current
= anim
177 self
.object
:set_animation({
178 x
= self
.animation
[anim
.. "_start"],
179 y
= self
.animation
[anim
.. "_end"]},
180 self
.animation
[anim
.. "_speed"] or self
.animation
.speed_normal
or 15,
181 0, self
.animation
[anim
.. "_loop"] ~= false)
185 -- above function exported for mount.lua
186 function mobs
:set_animation(self
, anim
)
187 set_animation(self
, anim
)
191 -- calculate distance
192 local get_distance
= function(a
, b
)
194 local x
, y
, z
= a
.x
- b
.x
, a
.y
- b
.y
, a
.z
- b
.z
196 return square(x
* x
+ y
* y
+ z
* z
)
200 -- check line of sight (BrunoMine)
201 local line_of_sight
= function(self
, pos1
, pos2
, stepsize
)
203 stepsize
= stepsize
or 1
205 local s
, pos
= minetest
.line_of_sight(pos1
, pos2
, stepsize
)
207 -- normal walking and flying mobs can see you through air
212 -- New pos1 to be analyzed
213 local npos1
= {x
= pos1
.x
, y
= pos1
.y
, z
= pos1
.z
}
215 local r
, pos
= minetest
.line_of_sight(npos1
, pos2
, stepsize
)
218 if r
== true then return true end
221 local nn
= minetest
.get_node(pos
).name
223 -- Target Distance (td) to travel
224 local td
= get_distance(pos1
, pos2
)
226 -- Actual Distance (ad) traveled
229 -- It continues to advance in the line of sight in search of a real
230 -- obstruction which counts as 'normal' nodebox.
231 while minetest
.registered_nodes
[nn
]
232 and (minetest
.registered_nodes
[nn
].walkable
== false
233 or minetest
.registered_nodes
[nn
].drawtype
== "nodebox") do
235 -- Check if you can still move forward
236 if td
< ad
+ stepsize
then
237 return true -- Reached the target
240 -- Moves the analyzed pos
241 local d
= get_distance(pos1
, pos2
)
243 npos1
.x
= ((pos2
.x
- pos1
.x
) / d
* stepsize
) + pos1
.x
244 npos1
.y
= ((pos2
.y
- pos1
.y
) / d
* stepsize
) + pos1
.y
245 npos1
.z
= ((pos2
.z
- pos1
.z
) / d
* stepsize
) + pos1
.z
249 or npos1
.x
~= npos1
.x
250 or npos1
.y
~= npos1
.y
251 or npos1
.z
~= npos1
.z
then
258 r
, pos
= minetest
.line_of_sight(npos1
, pos2
, stepsize
)
260 if r
== true then return true end
262 -- New Nodename found
263 nn
= minetest
.get_node(pos
).name
271 -- are we flying in what we are suppose to? (taikedz)
272 local flight_check
= function(self
, pos_w
)
274 local nod
= self
.standing_in
275 local def
= minetest
.registered_nodes
[nod
]
277 if not def
then return false end -- nil check
279 if type(self
.fly_in
) == "string"
280 and nod
== self
.fly_in
then
284 elseif type(self
.fly_in
) == "table" then
286 for _
,fly_in
in pairs(self
.fly_in
) do
288 if nod
== fly_in
then
295 -- stops mobs getting stuck inside stairs and plantlike nodes
296 if def
.drawtype
~= "airlike"
297 and def
.drawtype
~= "liquid"
298 and def
.drawtype
~= "flowingliquid" then
306 -- custom particle effects
307 local effect
= function(pos
, amount
, texture
, min_size
, max_size
, radius
, gravity
, glow
)
310 min_size
= min_size
or 0.5
311 max_size
= max_size
or 1
312 gravity
= gravity
or -10
315 minetest
.add_particlespawner({
320 minvel
= {x
= -radius
, y
= -radius
, z
= -radius
},
321 maxvel
= {x
= radius
, y
= radius
, z
= radius
},
322 minacc
= {x
= 0, y
= gravity
, z
= 0},
323 maxacc
= {x
= 0, y
= gravity
, z
= 0},
334 local update_tag
= function(self
)
338 local col = "#00FF00"
339 local qua = self.hp_max / 4
341 if self.health <= floor(qua * 3) then
345 if self.health <= floor(qua * 2) then
349 if self.health <= floor(qua) then
354 self
.object
:set_properties({
355 nametag
= self
.nametag
,
356 -- No nametag coloring
363 local item_drop
= function(self
, cooked
)
365 -- no drops if disabled by setting
366 if not mobs_drop_items
then return end
368 -- no drops for child mobs
369 if self
.child
then return end
372 local pos
= self
.object
:get_pos()
374 self
.drops
= self
.drops
or {} -- nil check
376 for n
= 1, #self
.drops
do
378 if random(1, self
.drops
[n
].chance
) == 1 then
380 num
= random(self
.drops
[n
].min or 1, self
.drops
[n
].max or 1)
381 item
= self
.drops
[n
].name
383 -- cook items when true
386 local output
= minetest
.get_craft_result({
387 method
= "cooking", width
= 1, items
= {item
}})
389 if output
and output
.item
and not output
.item
:is_empty() then
390 item
= output
.item
:get_name()
394 -- add item if it exists
395 obj
= minetest
.add_item(pos
, ItemStack(item
.. " " .. num
))
397 if obj
and obj
:get_luaentity() then
400 x
= random(-10, 10) / 9,
402 z
= random(-10, 10) / 9,
405 obj
:remove() -- item does not exist
414 -- check if mob is dead or only hurt
415 local check_for_death
= function(self
, cause
, cmi_cause
)
417 -- has health actually changed?
418 if self
.health
== self
.old_health
and self
.health
> 0 then
422 self
.old_health
= self
.health
424 -- still got some health? play hurt sound
425 if self
.health
> 0 then
427 mob_sound(self
, self
.sounds
.damage
)
429 -- make sure health isn't higher than max
430 if self
.health
> self
.hp_max
then
431 self
.health
= self
.hp_max
434 -- backup nametag so we can show health stats
435 if not self
.nametag2
then
436 self
.nametag2
= self
.nametag
or ""
440 and (cmi_cause
and cmi_cause
.type == "punch") then
443 self
.nametag
= "♥ " .. self
.health
.. " / " .. self
.hp_max
451 -- dropped cooked item if mob died in lava
452 if cause
== "lava" then
453 item_drop(self
, true)
458 mob_sound(self
, self
.sounds
.death
)
460 local pos
= self
.object
:get_pos()
462 -- execute custom death function
465 self
.on_die(self
, pos
)
468 cmi
.notify_die(self
.object
, cmi_cause
)
476 -- default death function and die animation (if defined)
478 and self
.animation
.die_start
479 and self
.animation
.die_end
then
481 local frames
= self
.animation
.die_end
- self
.animation
.die_start
482 local speed
= self
.animation
.die_speed
or 15
483 local length
= max(frames
/ speed
, 0)
491 set_velocity(self
, 0)
492 set_animation(self
, "die")
494 minetest
.after(length
, function(self
)
497 cmi
.notify_die(self
.object
, cmi_cause
)
505 cmi
.notify_die(self
.object
, cmi_cause
)
511 effect(pos
, 20, "tnt_smoke.png")
517 -- check if within physical map limits (-30911 to 30927)
518 local within_limits
= function(pos
, radius
)
520 if (pos
.x
- radius
) > -30913
521 and (pos
.x
+ radius
) < 30928
522 and (pos
.y
- radius
) > -30913
523 and (pos
.y
+ radius
) < 30928
524 and (pos
.z
- radius
) > -30913
525 and (pos
.z
+ radius
) < 30928 then
526 return true -- within limits
529 return false -- beyond limits
533 -- is mob facing a cliff
534 local is_at_cliff
= function(self
)
536 if self
.fear_height
== 0 then -- 0 for no falling protection!
540 local yaw
= self
.object
:get_yaw()
541 local dir_x
= -sin(yaw
) * (self
.collisionbox
[4] + 0.5)
542 local dir_z
= cos(yaw
) * (self
.collisionbox
[4] + 0.5)
543 local pos
= self
.object
:get_pos()
544 local ypos
= pos
.y
+ self
.collisionbox
[2] -- just above floor
546 if minetest
.line_of_sight(
547 {x
= pos
.x
+ dir_x
, y
= ypos
, z
= pos
.z
+ dir_z
},
548 {x
= pos
.x
+ dir_x
, y
= ypos
- self
.fear_height
, z
= pos
.z
+ dir_z
}
558 -- get node but use fallback for nil or unknown
559 local node_ok
= function(pos
, fallback
)
561 fallback
= fallback
or mobs
.fallback_node
563 local node
= minetest
.get_node_or_nil(pos
)
565 if node
and minetest
.registered_nodes
[node
.name
] then
569 return minetest
.registered_nodes
[fallback
] -- {name = fallback}
573 -- environmental damage (water, lava, fire, light etc.)
574 local do_env_damage
= function(self
)
576 -- feed/tame text timer (so mob 'full' messages dont spam chat)
577 if self
.htimer
> 0 then
578 self
.htimer
= self
.htimer
- 1
581 -- reset nametag after showing health stats
582 if self
.htimer
< 1 and self
.nametag2
then
584 self
.nametag
= self
.nametag2
590 local pos
= self
.object
:get_pos()
592 self
.time_of_day
= minetest
.get_timeofday()
594 -- remove mob if beyond map limits
595 if not within_limits(pos
, 0) then
600 -- bright light harms mob
601 if self
.light_damage
~= 0
603 -- and self.time_of_day > 0.2
604 -- and self.time_of_day < 0.8
605 and (minetest
.get_node_light(pos
) or 0) > 12 then
607 self
.health
= self
.health
- self
.light_damage
609 effect(pos
, 5, "tnt_smoke.png")
611 if check_for_death(self
, "light", {type = "light"}) then return end
614 local y_level
= self
.collisionbox
[2]
617 y_level
= self
.collisionbox
[2] * 0.5
620 -- what is mob standing in?
621 pos
.y
= pos
.y
+ y_level
+ 0.25 -- foot level
622 self
.standing_in
= node_ok(pos
, "air").name
623 -- print ("standing in " .. self.standing_in)
625 -- don't fall when on ignore, just stand still
626 if self
.standing_in
== "ignore" then
627 self
.object
:setvelocity({x
= 0, y
= 0, z
= 0})
630 local nodef
= minetest
.registered_nodes
[self
.standing_in
]
632 pos
.y
= pos
.y
+ 1 -- for particle effect position
636 and nodef
.groups
.water
then
638 if self
.water_damage
~= 0 then
640 self
.health
= self
.health
- self
.water_damage
642 effect(pos
, 5, "bubble.png", nil, nil, 1, nil)
644 if check_for_death(self
, "water", {type = "environment",
645 pos
= pos
, node
= self
.standing_in
}) then return end
649 elseif self
.lava_damage
650 and (nodef
.groups
.lava
651 or self
.standing_in
== node_fire
652 or self
.standing_in
== node_permanent_flame
) then
654 if self
.lava_damage
~= 0 then
656 self
.health
= self
.health
- self
.lava_damage
658 effect(pos
, 5, "fire_basic_flame.png", nil, nil, 1, nil)
660 if check_for_death(self
, "lava", {type = "environment",
661 pos
= pos
, node
= self
.standing_in
}) then return end
664 -- damage_per_second node check
665 elseif nodef
.damage_per_second
~= 0 then
667 self
.health
= self
.health
- nodef
.damage_per_second
669 effect(pos
, 5, "tnt_smoke.png")
671 if check_for_death(self
, "dps", {type = "environment",
672 pos
= pos
, node
= self
.standing_in
}) then return end
675 --- suffocation inside solid node
676 if self.suffocation ~= 0
677 and nodef.walkable == true
678 and nodef.groups.disable_suffocation ~= 1
679 and nodef.drawtype == "normal" then
681 self.health = self.health - self.suffocation
683 if check_for_death(self, "suffocation", {type = "environment",
684 pos = pos, node = self.standing_in}) then return end
687 check_for_death(self
, "", {type = "unknown"})
691 -- jump if facing a solid node (not fences or gates)
692 local do_jump
= function(self
)
695 or self
.jump_height
== 0
701 self
.facing_fence
= false
703 -- something stopping us while moving?
704 if self
.state
~= "stand"
705 and get_velocity(self
) > 0.5
706 and self
.object
:getvelocity().y
~= 0 then
710 local pos
= self
.object
:get_pos()
711 local yaw
= self
.object
:get_yaw()
713 -- what is mob standing on?
714 pos
.y
= pos
.y
+ self
.collisionbox
[2] - 0.2
716 local nod
= node_ok(pos
)
718 --print ("standing on:", nod.name, pos.y)
720 if minetest
.registered_nodes
[nod
.name
].walkable
== false then
725 local dir_x
= -sin(yaw
) * (self
.collisionbox
[4] + 0.5)
726 local dir_z
= cos(yaw
) * (self
.collisionbox
[4] + 0.5)
728 -- what is in front of mob?
729 local nod
= node_ok({
735 -- thin blocks that do not need to be jumped
736 if nod
.name
== node_snow
then
740 --print ("in front:", nod.name, pos.y + 0.5)
742 if self
.walk_chance
== 0
743 or minetest
.registered_items
[nod
.name
].walkable
then
745 if not nod
.name
:find("fence")
746 and not nod
.name
:find("gate") then
748 local v
= self
.object
:getvelocity()
750 v
.y
= self
.jump_height
752 set_animation(self
, "jump") -- only when defined
754 self
.object
:setvelocity(v
)
756 if get_velocity(self
) > 0 then
757 mob_sound(self
, self
.sounds
.jump
)
760 self
.facing_fence
= true
770 -- blast damage to entities nearby (modified from TNT mod)
771 local entity_physics
= function(pos
, radius
)
775 local objs
= minetest
.get_objects_inside_radius(pos
, radius
)
780 obj_pos
= objs
[n
]:get_pos()
782 dist
= get_distance(pos
, obj_pos
)
783 if dist
< 1 then dist
= 1 end
785 local damage
= floor((4 / dist
) * radius
)
786 local ent
= objs
[n
]:get_luaentity()
788 -- punches work on entities AND players
789 objs
[n
]:punch(objs
[n
], 1.0, {
790 full_punch_interval
= 1.0,
791 damage_groups
= {fleshy
= damage
},
797 -- should mob follow what I'm holding ?
798 local follow_holding
= function(self
, clicker
)
800 if mobs
.invis
[clicker
:get_player_name()] then
804 local item
= clicker
:get_wielded_item()
805 local t
= type(self
.follow
)
809 and item
:get_name() == self
.follow
then
813 elseif t
== "table" then
815 for no
= 1, #self
.follow
do
817 if self
.follow
[no
] == item
:get_name() then
827 -- find two animals of same type and breed if nearby and horny
828 local breed
= function(self
)
830 -- child takes 240 seconds before growing into adult
831 if self
.child
== true then
833 self
.hornytimer
= self
.hornytimer
+ 1
835 if self
.hornytimer
> 240 then
840 self
.object
:set_properties({
841 textures
= self
.base_texture
,
842 mesh
= self
.base_mesh
,
843 visual_size
= self
.base_size
,
844 collisionbox
= self
.base_colbox
,
845 selectionbox
= self
.base_selbox
,
848 -- custom function when child grows up
849 if self
.on_grown
then
852 -- jump when fully grown so as not to fall into ground
853 self
.object
:setvelocity({
855 y
= self
.jump_height
,
864 -- horny animal can mate for 40 seconds,
865 -- afterwards horny animal cannot mate again for 200 seconds
866 if self
.horny
== true
867 and self
.hornytimer
< 240 then
869 self
.hornytimer
= self
.hornytimer
+ 1
871 if self
.hornytimer
>= 240 then
877 -- find another same animal who is also horny and mate if nearby
878 if self
.horny
== true
879 and self
.hornytimer
<= 40 then
881 local pos
= self
.object
:get_pos()
883 effect({x
= pos
.x
, y
= pos
.y
+ 1, z
= pos
.z
}, 8, "heart.png", 3, 4, 1, 0.1)
885 local objs
= minetest
.get_objects_inside_radius(pos
, 3)
891 ent
= objs
[n
]:get_luaentity()
893 -- check for same animal with different colour
894 local canmate
= false
898 if ent
.name
== self
.name
then
901 local entname
= string.split(ent
.name
,":")
902 local selfname
= string.split(self
.name
,":")
904 if entname
[1] == selfname
[1] then
905 entname
= string.split(entname
[2],"_")
906 selfname
= string.split(selfname
[2],"_")
908 if entname
[1] == selfname
[1] then
917 and ent
.horny
== true
918 and ent
.hornytimer
<= 40 then
922 -- found your mate? then have a baby
929 minetest
.after(5, function()
931 -- custom breed function
932 if self
.on_breed
then
934 -- when false skip going any further
935 if self
.on_breed(self
, ent
) == false then
939 effect(pos
, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
942 local mob
= minetest
.add_entity(pos
, self
.name
)
943 local ent2
= mob
:get_luaentity()
944 local textures
= self
.base_texture
946 -- using specific child texture (if found)
947 if self
.child_texture
then
948 textures
= self
.child_texture
[1]
951 -- and resize to half height
955 x
= self
.base_size
.x
* .5,
956 y
= self
.base_size
.y
* .5,
959 self
.base_colbox
[1] * .5,
960 self
.base_colbox
[2] * .5,
961 self
.base_colbox
[3] * .5,
962 self
.base_colbox
[4] * .5,
963 self
.base_colbox
[5] * .5,
964 self
.base_colbox
[6] * .5,
967 self
.base_selbox
[1] * .5,
968 self
.base_selbox
[2] * .5,
969 self
.base_selbox
[3] * .5,
970 self
.base_selbox
[4] * .5,
971 self
.base_selbox
[5] * .5,
972 self
.base_selbox
[6] * .5,
975 -- tamed and owned by parents' owner
978 ent2
.owner
= self
.owner
990 -- find and replace what mob is looking for (grass, wheat etc.)
991 local replace
= function(self
, pos
)
994 or not self
.replace_rate
995 or not self
.replace_what
996 or self
.child
== true
997 or self
.object
:getvelocity().y
~= 0
998 or random(1, self
.replace_rate
) > 1 then
1002 local what
, with
, y_offset
1004 if type(self
.replace_what
[1]) == "table" then
1006 local num
= random(#self
.replace_what
)
1008 what
= self
.replace_what
[num
][1] or ""
1009 with
= self
.replace_what
[num
][2] or ""
1010 y_offset
= self
.replace_what
[num
][3] or 0
1012 what
= self
.replace_what
1013 with
= self
.replace_with
or ""
1014 y_offset
= self
.replace_offset
or 0
1017 pos
.y
= pos
.y
+ y_offset
1019 if #minetest
.find_nodes_in_area(pos
, pos
, what
) > 0 then
1021 -- print ("replace node = ".. minetest.get_node(pos).name, pos.y)
1023 local oldnode
= {name
= what
}
1024 local newnode
= {name
= with
}
1025 local on_replace_return
1027 if self
.on_replace
then
1028 on_replace_return
= self
.on_replace(self
, pos
, oldnode
, newnode
)
1031 if on_replace_return
~= false then
1033 minetest
.set_node(pos
, {name
= with
})
1035 -- when cow/sheep eats grass, replace wool and milk
1036 if self
.gotten
== true then
1038 self
.object
:set_properties(self
)
1045 -- check if daytime and also if mob is docile during daylight hours
1046 local day_docile
= function(self
)
1048 if self
.docile_by_day
== false then
1052 elseif self
.docile_by_day
== true
1053 and self
.time_of_day
> 0.2
1054 and self
.time_of_day
< 0.8 then
1061 -- path finding and smart mob routine by rnd
1062 local smart_mobs
= function(self
, s
, p
, dist
, dtime
)
1064 local s1
= self
.path
.lastpos
1066 -- is it becoming stuck?
1067 if abs(s1
.x
- s
.x
) + abs(s1
.z
- s
.z
) < 1.5 then
1068 self
.path
.stuck_timer
= self
.path
.stuck_timer
+ dtime
1070 self
.path
.stuck_timer
= 0
1073 self
.path
.lastpos
= {x
= s
.x
, y
= s
.y
, z
= s
.z
}
1075 -- im stuck, search for path
1076 if (self
.path
.stuck_timer
> stuck_timeout
and not self
.path
.following
)
1077 or (self
.path
.stuck_timer
> stuck_path_timeout
and self
.path
.following
) then
1079 self
.path
.stuck_timer
= 0
1081 -- lets try find a path, first take care of positions
1082 -- since pathfinder is very sensitive
1083 local sheight
= self
.collisionbox
[5] - self
.collisionbox
[2]
1085 -- round position to center of node to avoid stuck in walls
1086 -- also adjust height for player models!
1087 s
.x
= floor(s
.x
+ 0.5)
1088 -- s.y = floor(s.y + 0.5) - sheight
1089 s
.z
= floor(s
.z
+ 0.5)
1091 local ssight
, sground
= minetest
.line_of_sight(s
, {
1092 x
= s
.x
, y
= s
.y
- 4, z
= s
.z
}, 1)
1094 -- determine node above ground
1099 local p1
= self
.attack
:get_pos()
1101 p1
.x
= floor(p1
.x
+ 0.5)
1102 p1
.y
= floor(p1
.y
+ 0.5)
1103 p1
.z
= floor(p1
.z
+ 0.5)
1105 local dropheight
= 6
1106 if self
.fear_height
~= 0 then dropheight
= self
.fear_height
end
1108 -- self.path.way = minetest.find_path(s, p1, 16, 2, 6, "Dijkstra")
1109 self
.path
.way
= minetest
.find_path(s
, p1
, 16, self
.stepheight
, dropheight
, "A*_noprefetch")
1111 -- attempt to unstick mob that is "daydreaming"
1112 --[[ BUT NOT IN MINECLONE2, SILLY!
1113 self.object:setpos({
1114 x = s.x + 0.1 * (random() * 2 - 1),
1116 z = s.z + 0.1 * (random() * 2 - 1)
1121 do_attack(self
, self
.attack
)
1123 -- no path found, try something else
1124 if not self
.path
.way
then
1126 self
.path
.following
= false
1128 -- lets make way by digging/building if not accessible
1129 if self
.pathfinding
== 2 and mobs_griefing
then
1131 -- is player higher than mob?
1135 if not minetest
.is_protected(s
, "") then
1137 local ndef1
= minetest
.registered_nodes
[self
.standing_in
]
1139 if ndef1
and (ndef1
.buildable_to
or ndef1
.groups
.liquid
) then
1141 minetest
.set_node(s
, {name
= mobs
.fallback_node
})
1145 local sheight
= math
.ceil(self
.collisionbox
[5]) + 1
1147 -- assume mob is 2 blocks high so it digs above its head
1150 -- remove one block above to make room to jump
1151 if not minetest
.is_protected(s
, "") then
1153 local node1
= node_ok(s
, "air").name
1154 local ndef1
= minetest
.registered_nodes
[node1
]
1157 and node1
~= "ignore"
1159 and not ndef1
.groups
.level
1160 and not ndef1
.groups
.unbreakable
1161 and not ndef1
.groups
.liquid
then
1163 minetest
.set_node(s
, {name
= "air"})
1164 minetest
.add_item(s
, ItemStack(node1
))
1170 self
.object
:setpos({x
= s
.x
, y
= s
.y
+ 2, z
= s
.z
})
1172 else -- dig 2 blocks to make door toward player direction
1174 local yaw1
= self
.object
:get_yaw() + pi
/ 2
1176 x
= s
.x
+ cos(yaw1
),
1181 if not minetest
.is_protected(p1
, "") then
1183 local node1
= node_ok(p1
, "air").name
1184 local ndef1
= minetest
.registered_nodes
[node1
]
1187 and node1
~= "ignore"
1189 and not ndef1
.groups
.level
1190 and not ndef1
.groups
.unbreakable
1191 and not ndef1
.groups
.liquid
then
1193 minetest
.add_item(p1
, ItemStack(node1
))
1194 minetest
.set_node(p1
, {name
= "air"})
1198 node1
= node_ok(p1
, "air").name
1199 ndef1
= minetest
.registered_nodes
[node1
]
1202 and node1
~= "ignore"
1204 and not ndef1
.groups
.level
1205 and not ndef1
.groups
.unbreakable
1206 and not ndef1
.groups
.liquid
then
1208 minetest
.add_item(p1
, ItemStack(node1
))
1209 minetest
.set_node(p1
, {name
= "air"})
1216 -- will try again in 2 second
1217 self
.path
.stuck_timer
= stuck_timeout
- 2
1219 -- frustration! cant find the damn path :(
1220 mob_sound(self
, self
.sounds
.random)
1223 mob_sound(self
, self
.sounds
.war_cry
)
1225 set_velocity(self
, self
.walk_velocity
)
1227 -- follow path now that it has it
1228 self
.path
.following
= true
1235 local specific_attack
= function(list
, what
)
1237 -- no list so attack default (player, animals etc.)
1242 -- found entity on list to attack?
1243 for no
= 1, #list
do
1245 if list
[no
] == what
then
1254 -- monster find someone to attack
1255 local monster_attack
= function(self
)
1257 if self
.type ~= "monster"
1258 or not damage_enabled
1260 or self
.state
== "attack"
1261 or day_docile(self
) then
1265 local s
= self
.object
:get_pos()
1267 local player
, obj
, min_player
1268 local type, name
= "", ""
1269 local min_dist
= self
.view_range
+ 1
1270 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1274 if objs
[n
]:is_player() then
1276 if mobs
.invis
[ objs
[n
]:get_player_name() ] then
1285 obj
= objs
[n
]:get_luaentity()
1290 name
= obj
.name
or ""
1294 -- find specific mob to attack, failing that attack player/npc/animal
1295 if specific_attack(self
.specific_attack
, name
)
1296 and (type == "player" or type == "npc"
1297 or (type == "animal" and self
.attack_animals
== true)) then
1299 s
= self
.object
:get_pos()
1300 p
= player
:get_pos()
1303 -- aim higher to make looking up hills more realistic
1307 dist
= get_distance(p
, s
)
1309 if dist
< self
.view_range
then
1310 -- field of view check goes here
1312 -- choose closest player to attack
1313 if line_of_sight(self
, sp
, p
, 2) == true
1314 and dist
< min_dist
then
1324 do_attack(self
, min_player
)
1329 -- npc, find closest monster to attack
1330 local npc_attack
= function(self
)
1332 if self
.type ~= "npc"
1333 or not self
.attacks_monsters
1334 or self
.state
== "attack" then
1338 local p
, sp
, obj
, min_player
1339 local s
= self
.object
:get_pos()
1340 local min_dist
= self
.view_range
+ 1
1341 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1345 obj
= objs
[n
]:get_luaentity()
1347 if obj
and obj
.type == "monster" then
1349 p
= obj
.object
:get_pos()
1351 dist
= get_distance(p
, s
)
1353 if dist
< min_dist
then
1355 min_player
= obj
.object
1361 do_attack(self
, min_player
)
1367 local specific_runaway
= function(list
, what
)
1369 -- no list so do not run
1374 -- found entity on list to attack?
1375 for no
= 1, #list
do
1377 if list
[no
] == what
or list
[no
] == "player" then
1386 -- find someone to runaway from
1387 local runaway_from
= function(self
)
1389 if not self
.runaway_from
then
1393 local s
= self
.object
:get_pos()
1395 local player
, obj
, min_player
1396 local type, name
= "", ""
1397 local min_dist
= self
.view_range
+ 1
1398 local objs
= minetest
.get_objects_inside_radius(s
, self
.view_range
)
1402 if objs
[n
]:is_player() then
1404 if mobs
.invis
[ objs
[n
]:get_player_name() ]
1405 or self
.owner
== objs
[n
]:get_player_name() then
1414 obj
= objs
[n
]:get_luaentity()
1419 name
= obj
.name
or ""
1423 -- find specific mob to runaway from
1424 if name
~= "" and name
~= self
.name
1425 and specific_runaway(self
.runaway_from
, name
) then
1427 s
= self
.object
:get_pos()
1428 p
= player
:get_pos()
1431 -- aim higher to make looking up hills more realistic
1435 dist
= get_distance(p
, s
)
1437 if dist
< self
.view_range
then
1438 -- field of view check goes here
1440 -- choose closest player/mpb to runaway from
1441 if line_of_sight(self
, sp
, p
, 2) == true
1442 and dist
< min_dist
then
1452 local lp
= player
:get_pos()
1459 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
1465 yaw
= set_yaw(self
.object
, yaw
)
1466 self
.state
= "runaway"
1467 self
.runaway_timer
= 3
1468 self
.following
= nil
1473 -- follow player if owner or holding item, if fish outta water then flop
1474 local follow_flop
= function(self
)
1476 -- find player to follow
1477 if (self
.follow
~= ""
1478 or self
.order
== "follow")
1479 and not self
.following
1480 and self
.state
~= "attack"
1481 and self
.state
~= "runaway" then
1483 local s
= self
.object
:get_pos()
1484 local players
= minetest
.get_connected_players()
1486 for n
= 1, #players
do
1488 if get_distance(players
[n
]:get_pos(), s
) < self
.view_range
1489 and not mobs
.invis
[ players
[n
]:get_player_name() ] then
1491 self
.following
= players
[n
]
1498 if self
.type == "npc"
1499 and self
.order
== "follow"
1500 and self
.state
~= "attack"
1501 and self
.owner
~= "" then
1503 -- npc stop following player if not owner
1506 and self
.owner
~= self
.following
:get_player_name() then
1507 self
.following
= nil
1510 -- stop following player if not holding specific item
1512 and self
.following
:is_player()
1513 and follow_holding(self
, self
.following
) == false then
1514 self
.following
= nil
1519 -- follow that thing
1520 if self
.following
then
1522 local s
= self
.object
:get_pos()
1525 if self
.following
:is_player() then
1527 p
= self
.following
:get_pos()
1529 elseif self
.following
.object
then
1531 p
= self
.following
.object
:get_pos()
1536 local dist
= get_distance(p
, s
)
1538 -- dont follow if out of range
1539 if dist
> self
.view_range
then
1540 self
.following
= nil
1547 local yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1549 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
1551 yaw
= set_yaw(self
.object
, yaw
)
1553 -- anyone but standing npc's can move along
1554 if dist
> self
.reach
1555 and self
.order
~= "stand" then
1557 set_velocity(self
, self
.walk_velocity
)
1559 if self
.walk_chance
~= 0 then
1560 set_animation(self
, "walk")
1563 set_velocity(self
, 0)
1564 set_animation(self
, "stand")
1572 -- swimmers flop when out of their element, and swim again when back in
1574 local s
= self
.object
:get_pos()
1575 if not flight_check(self
, s
) then
1578 self
.object
:setvelocity({x
= 0, y
= -5, z
= 0})
1580 set_animation(self
, "stand")
1583 elseif self
.state
== "flop" then
1584 self
.state
= "stand"
1590 -- dogshoot attack switch and counter function
1591 local dogswitch
= function(self
, dtime
)
1593 -- switch mode not activated
1594 if not self
.dogshoot_switch
1599 self
.dogshoot_count
= self
.dogshoot_count
+ dtime
1601 if (self
.dogshoot_switch
== 1
1602 and self
.dogshoot_count
> self
.dogshoot_count_max
)
1603 or (self
.dogshoot_switch
== 2
1604 and self
.dogshoot_count
> self
.dogshoot_count2_max
) then
1606 self
.dogshoot_count
= 0
1608 if self
.dogshoot_switch
== 1 then
1609 self
.dogshoot_switch
= 2
1611 self
.dogshoot_switch
= 1
1615 return self
.dogshoot_switch
1619 -- execute current state (stand, walk, run, attacks)
1620 local do_states
= function(self
, dtime
)
1622 local yaw
= self
.object
:get_yaw() or 0
1624 if self
.state
== "stand" then
1626 if random(1, 4) == 1 then
1629 local s
= self
.object
:get_pos()
1630 local objs
= minetest
.get_objects_inside_radius(s
, 3)
1634 if objs
[n
]:is_player() then
1635 lp
= objs
[n
]:get_pos()
1640 -- look at any players nearby, otherwise turn randomly
1648 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1650 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1652 yaw
= yaw
+ random(-0.5, 0.5)
1655 yaw
= set_yaw(self
.object
, yaw
)
1658 set_velocity(self
, 0)
1659 set_animation(self
, "stand")
1661 -- npc's ordered to stand stay standing
1662 if self
.type ~= "npc"
1663 or self
.order
~= "stand" then
1665 if self
.walk_chance
~= 0
1666 and self
.facing_fence
~= true
1667 and random(1, 100) <= self
.walk_chance
1668 and is_at_cliff(self
) == false then
1670 set_velocity(self
, self
.walk_velocity
)
1672 set_animation(self
, "walk")
1674 -- fly up/down randomly for flying mobs
1675 if self
.fly
and random(1, 100) <= self
.walk_chance
then
1677 local v
= self
.object
:getvelocity()
1678 local ud
= random(-1, 2) / 9
1680 self
.object
:setvelocity({x
= v
.x
, y
= ud
, z
= v
.z
})
1685 elseif self
.state
== "walk" then
1687 local s
= self
.object
:get_pos()
1690 -- is there something I need to avoid?
1691 if self
.water_damage
> 0
1692 and self
.lava_damage
> 0 then
1694 lp
= minetest
.find_node_near(s
, 1, {"group:water", "group:lava"})
1696 elseif self
.water_damage
> 0 then
1698 lp
= minetest
.find_node_near(s
, 1, {"group:water"})
1700 elseif self
.lava_damage
> 0 then
1702 lp
= minetest
.find_node_near(s
, 1, {"group:lava"})
1707 -- if mob in water or lava then look for land
1708 if (self
.lava_damage
1709 and minetest
.registered_nodes
[self
.standing_in
].groups
.lava
)
1710 or (self
.water_damage
1711 and minetest
.registered_nodes
[self
.standing_in
].groups
.water
) then
1713 lp
= minetest
.find_node_near(s
, 5, {"group:soil", "group:stone",
1714 "group:sand", node_ice
, node_snowblock
})
1716 -- did we find land?
1724 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1726 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1728 -- look towards land and jump/move in that direction
1729 yaw
= set_yaw(self
.object
, yaw
)
1731 set_velocity(self
, self
.walk_velocity
)
1733 yaw
= yaw
+ random(-0.5, 0.5)
1743 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1745 if lp
.x
> s
.x
then yaw
= yaw
+ pi
end
1748 yaw
= set_yaw(self
.object
, yaw
)
1750 -- otherwise randomly turn
1751 elseif random(1, 100) <= 30 then
1753 yaw
= yaw
+ random(-0.5, 0.5)
1755 yaw
= set_yaw(self
.object
, yaw
)
1758 -- stand for great fall in front
1759 local temp_is_cliff
= is_at_cliff(self
)
1761 if self
.facing_fence
== true
1763 or random(1, 100) <= 30 then
1765 set_velocity(self
, 0)
1766 self
.state
= "stand"
1767 set_animation(self
, "stand")
1769 set_velocity(self
, self
.walk_velocity
)
1771 if flight_check(self
)
1773 and self
.animation
.fly_start
1774 and self
.animation
.fly_end
then
1775 set_animation(self
, "fly")
1777 set_animation(self
, "walk")
1781 -- runaway when punched
1782 elseif self
.state
== "runaway" then
1784 self
.runaway_timer
= self
.runaway_timer
+ 1
1786 -- stop after 5 seconds or when at cliff
1787 if self
.runaway_timer
> 5
1788 or is_at_cliff(self
) then
1789 self
.runaway_timer
= 0
1790 set_velocity(self
, 0)
1791 self
.state
= "stand"
1792 set_animation(self
, "stand")
1794 set_velocity(self
, self
.run_velocity
)
1795 set_animation(self
, "walk")
1798 -- attack routines (explode, dogfight, shoot, dogshoot)
1799 elseif self
.state
== "attack" then
1801 -- calculate distance from mob and enemy
1802 local s
= self
.object
:get_pos()
1803 local p
= self
.attack
:get_pos() or s
1804 local dist
= get_distance(p
, s
)
1806 -- stop attacking if player invisible or out of range
1807 if dist
> self
.view_range
1809 or not self
.attack
:get_pos()
1810 or self
.attack
:get_hp() <= 0
1811 or (self
.attack
:is_player() and mobs
.invis
[ self
.attack
:get_player_name() ]) then
1813 -- print(" ** stop attacking **", dist, self.view_range)
1814 self
.state
= "stand"
1815 set_velocity(self
, 0)
1816 set_animation(self
, "stand")
1818 self
.v_start
= false
1825 if self
.attack_type
== "explode" then
1832 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
1834 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
1836 yaw
= set_yaw(self
.object
, yaw
)
1838 local node_break_radius
= self
.explosion_radius
or 1
1839 local entity_damage_radius
= self
.explosion_damage_radius
1840 or (node_break_radius
* 2)
1842 -- start timer when in reach and line of sight
1844 and dist
<= self
.reach
1845 and line_of_sight(self
, s
, p
, 2) then
1850 mob_sound(self
, self
.sounds
.fuse
)
1851 -- print ("=== explosion timer started", self.explosion_timer)
1853 -- stop timer if out of reach or direct line of sight
1854 elseif self
.allow_fuse_reset
1856 and (dist
> self
.reach
1857 or not line_of_sight(self
, s
, p
, 2)) then
1858 self
.v_start
= false
1861 self
.blinkstatus
= false
1862 self
.object
:settexturemod("")
1865 -- walk right up to player unless the timer is active
1866 if self
.v_start
and (self
.stop_to_explode
or dist
< 1.5) then
1867 set_velocity(self
, 0)
1869 set_velocity(self
, self
.run_velocity
)
1872 if self
.animation
and self
.animation
.run_start
then
1873 set_animation(self
, "run")
1875 set_animation(self
, "walk")
1878 if self
.v_start
then
1880 self
.timer
= self
.timer
+ dtime
1881 self
.blinktimer
= (self
.blinktimer
or 0) + dtime
1883 if self
.blinktimer
> 0.2 then
1887 if self
.blinkstatus
then
1888 self
.object
:settexturemod("")
1890 self
.object
:settexturemod("^[brighten")
1893 self
.blinkstatus
= not self
.blinkstatus
1896 -- print ("=== explosion timer", self.timer)
1898 if self
.timer
> self
.explosion_timer
then
1900 local pos
= self
.object
:get_pos()
1902 -- dont damage anything if area protected or next to water
1903 if minetest
.find_node_near(pos
, 1, {"group:water"})
1904 or minetest
.is_protected(pos
, "") then
1906 node_break_radius
= 0
1909 self
.object
:remove()
1911 if minetest
.get_modpath("mcl_tnt") and tnt
and tnt
.boom
1912 and not minetest
.is_protected(pos
, "") then
1915 radius
= node_break_radius
,
1916 damage_radius
= entity_damage_radius
,
1917 sound
= self
.sounds
.explode
,
1921 minetest
.sound_play(self
.sounds
.explode
, {
1924 max_hear_distance
= self
.sounds
.distance
or 32
1927 entity_physics(pos
, entity_damage_radius
)
1928 effect(pos
, 32, "tnt_smoke.png", nil, nil, node_break_radius
, 1, 0)
1935 elseif self
.attack_type
== "dogfight"
1936 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 2)
1937 or (self
.attack_type
== "dogshoot" and dist
<= self
.reach
and dogswitch(self
) == 0) then
1940 and dist
> self
.reach
then
1943 local me_y
= floor(p1
.y
)
1945 local p_y
= floor(p2
.y
+ 1)
1946 local v
= self
.object
:getvelocity()
1948 if flight_check(self
, s
) then
1952 self
.object
:setvelocity({
1954 y
= 1 * self
.walk_velocity
,
1958 elseif me_y
> p_y
then
1960 self
.object
:setvelocity({
1962 y
= -1 * self
.walk_velocity
,
1969 self
.object
:setvelocity({
1975 elseif me_y
> p_y
then
1977 self
.object
:setvelocity({
1987 -- rnd: new movement direction
1988 if self
.path
.following
1990 and self
.attack_type
~= "dogshoot" then
1992 -- no paths longer than 50
1993 if #self
.path
.way
> 50
1994 or dist
< self
.reach
then
1995 self
.path
.following
= false
1999 local p1
= self
.path
.way
[1]
2002 self
.path
.following
= false
2006 if abs(p1
.x
-s
.x
) + abs(p1
.z
- s
.z
) < 0.6 then
2007 -- reached waypoint, remove it from queue
2008 table.remove(self
.path
.way
, 1)
2011 -- set new temporary target
2012 p
= {x
= p1
.x
, y
= p1
.y
, z
= p1
.z
}
2020 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2022 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2024 yaw
= set_yaw(self
.object
, yaw
)
2026 -- move towards enemy if beyond mob reach
2027 if dist
> self
.reach
then
2029 -- path finding by rnd
2030 if self
.pathfinding
-- only if mob has pathfinding enabled
2031 and enable_pathfinding
then
2033 smart_mobs(self
, s
, p
, dist
, dtime
)
2036 if is_at_cliff(self
) then
2038 set_velocity(self
, 0)
2039 set_animation(self
, "stand")
2042 if self
.path
.stuck
then
2043 set_velocity(self
, self
.walk_velocity
)
2045 set_velocity(self
, self
.run_velocity
)
2048 if self
.animation
and self
.animation
.run_start
then
2049 set_animation(self
, "run")
2051 set_animation(self
, "walk")
2055 else -- rnd: if inside reach range
2057 self
.path
.stuck
= false
2058 self
.path
.stuck_timer
= 0
2059 self
.path
.following
= false -- not stuck anymore
2061 set_velocity(self
, 0)
2063 if not self
.custom_attack
then
2065 if self
.timer
> 1 then
2069 if self
.double_melee_attack
2070 and random(1, 2) == 1 then
2071 set_animation(self
, "punch2")
2073 set_animation(self
, "punch")
2082 if line_of_sight(self
, p2
, s2
) == true then
2084 -- play attack sound
2085 mob_sound(self
, self
.sounds
.attack
)
2087 -- punch player (or what player is attached to)
2088 local attached
= self
.attack
:get_attach()
2090 self
.attack
= attached
2092 self
.attack
:punch(self
.object
, 1.0, {
2093 full_punch_interval
= 1.0,
2094 damage_groups
= {fleshy
= self
.damage
}
2098 else -- call custom attack every second
2099 if self
.custom_attack
2100 and self
.timer
> 1 then
2104 self
.custom_attack(self
, p
)
2109 elseif self
.attack_type
== "shoot"
2110 or (self
.attack_type
== "dogshoot" and dogswitch(self
, dtime
) == 1)
2111 or (self
.attack_type
== "dogshoot" and dist
> self
.reach
and dogswitch(self
) == 0) then
2116 local dist
= get_distance(p
, s
)
2123 yaw
= (atan(vec
.z
/ vec
.x
) + pi
/ 2) - self
.rotate
2125 if p
.x
> s
.x
then yaw
= yaw
+ pi
end
2127 yaw
= set_yaw(self
.object
, yaw
)
2129 set_velocity(self
, 0)
2131 if self
.shoot_interval
2132 and self
.timer
> self
.shoot_interval
2133 and random(1, 100) <= 60 then
2136 set_animation(self
, "shoot")
2138 -- play shoot attack sound
2139 mob_sound(self
, self
.sounds
.shoot_attack
)
2141 local p
= self
.object
:get_pos()
2143 p
.y
= p
.y
+ (self
.collisionbox
[2] + self
.collisionbox
[5]) / 2
2145 if minetest
.registered_entities
[self
.arrow
] then
2147 local obj
= minetest
.add_entity(p
, self
.arrow
)
2148 local ent
= obj
:get_luaentity()
2149 local amount
= (vec
.x
* vec
.x
+ vec
.y
* vec
.y
+ vec
.z
* vec
.z
) ^
0.5
2150 local v
= ent
.velocity
or 1 -- or set to default
2153 ent
.owner_id
= tostring(self
.object
) -- add unique owner id to arrow
2155 -- offset makes shoot aim accurate
2156 vec
.y
= vec
.y
+ self
.shoot_offset
2157 vec
.x
= vec
.x
* (v
/ amount
)
2158 vec
.y
= vec
.y
* (v
/ amount
)
2159 vec
.z
= vec
.z
* (v
/ amount
)
2161 obj
:setvelocity(vec
)
2169 -- falling and fall damage
2170 local falling
= function(self
, pos
)
2176 -- floating in water (or falling)
2177 local v
= self
.object
:getvelocity()
2181 -- apply gravity when moving up
2182 self
.object
:setacceleration({
2188 elseif v
.y
<= 0 and v
.y
> self
.fall_speed
then
2190 -- fall downwards at set speed
2191 self
.object
:setacceleration({
2193 y
= self
.fall_speed
,
2197 -- stop accelerating once max fall speed hit
2198 self
.object
:setacceleration({x
= 0, y
= 0, z
= 0})
2201 -- in water then float up
2202 -- if minetest.registered_nodes[node_ok(pos).name].groups.liquid then
2203 if minetest
.registered_nodes
[node_ok(pos
).name
].groups
.water
then
2205 if self
.floats
== 1 then
2207 self
.object
:setacceleration({
2209 y
= -self
.fall_speed
/ (max(1, v
.y
) ^
2),
2215 -- fall damage onto solid ground
2216 if self
.fall_damage
== 1
2217 and self
.object
:getvelocity().y
== 0 then
2219 local d
= (self
.old_y
or 0) - self
.object
:get_pos().y
2223 self
.health
= self
.health
- floor(d
- 5)
2225 effect(pos
, 5, "tnt_smoke.png", 1, 2, 2, nil)
2227 if check_for_death(self
, "fall", {type = "fall"}) then
2232 self
.old_y
= self
.object
:get_pos().y
2238 -- deal damage and effects when mob punched
2239 local mob_punch
= function(self
, hitter
, tflp
, tool_capabilities
, dir
)
2241 -- custom punch function
2242 if self
.do_punch
then
2244 -- when false skip going any further
2245 if self
.do_punch(self
, hitter
, tflp
, tool_caps
, dir
) == false then
2251 -- if self.health <= 0 then
2255 -- error checking when mod profiling is enabled
2256 if not tool_capabilities
then
2257 minetest
.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
2261 -- is mob protected?
2262 if self
.protected
and hitter
:is_player()
2263 and minetest
.is_protected(self
.object
:get_pos(), hitter
:get_player_name()) then
2264 minetest
.chat_send_player(hitter
:get_player_name(), S("Mob has been protected!"))
2270 local weapon
= hitter
:get_wielded_item()
2271 local punch_interval
= 1.4
2273 -- calculate mob damage
2275 local armor
= self
.object
:get_armor_groups() or {}
2278 -- quick error check incase it ends up 0 (serialize.h check test)
2284 damage
= cmi
.calculate_damage(self
.object
, hitter
, tflp
, tool_capabilities
, dir
)
2287 for group
,_
in pairs( (tool_capabilities
.damage_groups
or {}) ) do
2289 tmp
= tflp
/ (tool_capabilities
.full_punch_interval
or 1.4)
2297 damage
= damage
+ (tool_capabilities
.damage_groups
[group
] or 0)
2298 * tmp
* ((armor
[group
] or 0) / 100.0)
2302 -- check for tool immunity or special damage
2303 for n
= 1, #self
.immune_to
do
2305 if self
.immune_to
[n
][1] == weapon
:get_name() then
2307 damage
= self
.immune_to
[n
][2] or 0
2313 if damage
<= -1 then
2314 self
.health
= self
.health
- floor(damage
)
2318 -- print ("Mob Damage is", damage)
2322 local cancel
= cmi
.notify_punch(self
.object
, hitter
, tflp
, tool_capabilities
, dir
, damage
)
2324 if cancel
then return end
2328 if tool_capabilities
then
2329 punch_interval
= tool_capabilities
.full_punch_interval
or 1.4
2332 if weapon
:get_definition()
2333 and weapon
:get_definition().tool_capabilities
then
2335 weapon
:add_wear(floor((punch_interval
/ 75) * 9000))
2336 hitter
:set_wielded_item(weapon
)
2339 -- only play hit sound and show blood effects if damage is 1 or over
2343 if weapon
:get_definition().sounds
~= nil then
2345 local s
= random(0, #weapon
:get_definition().sounds
)
2347 minetest
.sound_play(weapon
:get_definition().sounds
[s
], {
2348 object
= self
.object
, --hitter,
2349 max_hear_distance
= 8
2352 minetest
.sound_play("default_punch", {
2353 object
= self
.object
, --hitter,
2354 max_hear_distance
= 5
2359 if self
.blood_amount
> 0
2360 and not disable_blood
then
2362 local pos
= self
.object
:get_pos()
2364 pos
.y
= pos
.y
+ (-self
.collisionbox
[2] + self
.collisionbox
[5]) * .5
2366 -- do we have a single blood texture or multiple?
2367 if type(self
.blood_texture
) == "table" then
2369 local blood
= self
.blood_texture
[random(1, #self
.blood_texture
)]
2371 effect(pos
, self
.blood_amount
, blood
, nil, nil, 1, nil)
2373 effect(pos
, self
.blood_amount
, self
.blood_texture
, nil, nil, 1, nil)
2378 self
.health
= self
.health
- floor(damage
)
2380 -- exit here if dead, special item check
2381 if weapon
:get_name() == "mobs:pick_lava" then
2382 if check_for_death(self
, "lava", {type = "punch",
2383 puncher
= hitter
}) then
2387 if check_for_death(self
, "hit", {type = "punch",
2388 puncher
= hitter
}) then
2393 --[[ add healthy afterglow when hit (can cause hit lag with larger textures)
2394 core.after(0.1, function()
2395 self.object:settexturemod("^[colorize:#c9900070")
2397 core.after(0.3, function()
2398 self.object:settexturemod("")
2402 -- knock back effect (only on full punch)
2403 if self
.knock_back
> 0
2404 and tflp
>= punch_interval
then
2406 local v
= self
.object
:getvelocity()
2407 local r
= 1.4 - min(punch_interval
, 1.4)
2411 -- if already in air then dont go up anymore when hit
2417 -- direction error check
2418 dir
= dir
or {x
= 0, y
= 0, z
= 0}
2420 -- check if tool already has specific knockback value
2421 if tool_capabilities
.damage_groups
["knockback"] then
2422 kb
= tool_capabilities
.damage_groups
["knockback"]
2427 self
.object
:setvelocity({
2433 self
.pause_timer
= 0.25
2435 end -- END if damage
2437 -- if skittish then run away
2438 if self
.runaway
== true then
2440 local lp
= hitter
:get_pos()
2441 local s
= self
.object
:get_pos()
2448 local yaw
= (atan(vec
.z
/ vec
.x
) + 3 * pi
/ 2) - self
.rotate
2454 yaw
= set_yaw(self
.object
, yaw
)
2455 self
.state
= "runaway"
2456 self
.runaway_timer
= 0
2457 self
.following
= nil
2460 local name
= hitter
:get_player_name() or ""
2462 -- attack puncher and call other mobs for help
2463 if self
.passive
== false
2464 and self
.state
~= "flop"
2465 and self
.child
== false
2466 and hitter
:get_player_name() ~= self
.owner
2467 and not mobs
.invis
[ name
] then
2469 -- attack whoever punched mob
2471 do_attack(self
, hitter
)
2473 -- alert others to the attack
2474 local objs
= minetest
.get_objects_inside_radius(hitter
:get_pos(), self
.view_range
)
2479 obj
= objs
[n
]:get_luaentity()
2483 -- only alert members of same mob
2484 if obj
.group_attack
== true
2485 and obj
.state
~= "attack"
2486 and obj
.owner
~= name
2487 and obj
.name
== self
.name
then
2488 do_attack(obj
, hitter
)
2491 -- have owned mobs attack player threat
2492 if obj
.owner
== name
and obj
.owner_loyal
then
2493 do_attack(obj
, self
.object
)
2501 -- get entity staticdata
2502 local mob_staticdata
= function(self
)
2504 -- remove mob when out of range unless tamed
2507 and self
.type ~= "npc"
2508 and self
.state
~= "attack"
2510 and self
.lifetimer
< 20000 then
2512 --print ("REMOVED " .. self.name)
2514 self
.object
:remove()
2519 self
.remove_ok
= true
2521 self
.following
= nil
2522 self
.state
= "stand"
2524 -- used to rotate older mobs
2526 and self
.drawtype
== "side" then
2527 self
.rotate
= math
.rad(90)
2531 self
.serialized_cmi_components
= cmi
.serialize_components(self
._cmi_components
)
2536 for _
,stat
in pairs(self
) do
2538 local t
= type(stat
)
2543 and _
~= "_cmi_components" then
2548 --print('===== '..self.name..'\n'.. dump(tmp)..'\n=====\n')
2549 return minetest
.serialize(tmp
)
2553 -- activate mob and reload settings
2554 local mob_activate
= function(self
, staticdata
, def
, dtime
)
2556 -- remove monsters in peaceful mode
2557 if self
.type == "monster"
2558 and peaceful_only
then
2560 self
.object
:remove()
2565 -- load entity variables
2566 local tmp
= minetest
.deserialize(staticdata
)
2569 for _
,stat
in pairs(tmp
) do
2574 -- select random texture, set model and size
2575 if not self
.base_texture
then
2577 -- compatiblity with old simple mobs textures
2578 if type(def
.textures
[1]) == "string" then
2579 def
.textures
= {def
.textures
}
2582 self
.base_texture
= def
.textures
[random(1, #def
.textures
)]
2583 self
.base_mesh
= def
.mesh
2584 self
.base_size
= self
.visual_size
2585 self
.base_colbox
= self
.collisionbox
2586 self
.base_selbox
= self
.selectionbox
2589 -- for current mobs that dont have this set
2590 if not self
.base_selbox
then
2591 self
.base_selbox
= self
.selectionbox
or self
.base_colbox
2594 -- set texture, model and size
2595 local textures
= self
.base_texture
2596 local mesh
= self
.base_mesh
2597 local vis_size
= self
.base_size
2598 local colbox
= self
.base_colbox
2599 local selbox
= self
.base_selbox
2601 -- specific texture if gotten
2602 if self
.gotten
== true
2603 and def
.gotten_texture
then
2604 textures
= def
.gotten_texture
2607 -- specific mesh if gotten
2608 if self
.gotten
== true
2609 and def
.gotten_mesh
then
2610 mesh
= def
.gotten_mesh
2613 -- set child objects to half size
2614 if self
.child
== true then
2617 x
= self
.base_size
.x
* .5,
2618 y
= self
.base_size
.y
* .5,
2621 if def
.child_texture
then
2622 textures
= def
.child_texture
[1]
2626 self
.base_colbox
[1] * .5,
2627 self
.base_colbox
[2] * .5,
2628 self
.base_colbox
[3] * .5,
2629 self
.base_colbox
[4] * .5,
2630 self
.base_colbox
[5] * .5,
2631 self
.base_colbox
[6] * .5
2634 self
.base_selbox
[1] * .5,
2635 self
.base_selbox
[2] * .5,
2636 self
.base_selbox
[3] * .5,
2637 self
.base_selbox
[4] * .5,
2638 self
.base_selbox
[5] * .5,
2639 self
.base_selbox
[6] * .5
2643 if self
.health
== 0 then
2644 self
.health
= random (self
.hp_min
, self
.hp_max
)
2649 self
.path
.way
= {} -- path to follow, table of positions
2650 self
.path
.lastpos
= {x
= 0, y
= 0, z
= 0}
2651 self
.path
.stuck
= false
2652 self
.path
.following
= false -- currently following path?
2653 self
.path
.stuck_timer
= 0 -- if stuck for too long search for path
2656 self
.object
:set_armor_groups({immortal
= 1, fleshy
= self
.armor
})
2657 self
.old_y
= self
.object
:get_pos().y
2658 self
.old_health
= self
.health
2659 self
.sounds
.distance
= self
.sounds
.distance
or 10
2660 self
.textures
= textures
2662 self
.collisionbox
= colbox
2663 self
.selectionbox
= selbox
2664 self
.visual_size
= vis_size
2665 self
.standing_in
= ""
2667 -- check existing nametag
2668 if not self
.nametag
then
2669 self
.nametag
= def
.nametag
2672 -- set anything changed above
2673 self
.object
:set_properties(self
)
2674 set_yaw(self
.object
, (random(0, 360) - 180) / 180 * pi
)
2676 set_animation(self
, "stand")
2678 -- run on_spawn function if found
2679 if self
.on_spawn
and not self
.on_spawn_run
then
2680 if self
.on_spawn(self
) then
2681 self
.on_spawn_run
= true -- if true, set flag to run once only
2685 -- run after_activate
2686 if def
.after_activate
then
2687 def
.after_activate(self
, staticdata
, def
, dtime
)
2691 self
._cmi_components
= cmi
.activate_components(self
.serialized_cmi_components
)
2692 cmi
.notify_activate(self
.object
, dtime
)
2697 -- main mob function
2698 local mob_step
= function(self
, dtime
)
2701 cmi
.notify_step(self
.object
, dtime
)
2704 local pos
= self
.object
:get_pos()
2707 -- when lifetimer expires remove mob (except npc and tamed)
2708 if self
.type ~= "npc"
2710 and self
.state
~= "attack"
2711 and remove_far
~= true
2712 and self
.lifetimer
< 20000 then
2714 self
.lifetimer
= self
.lifetimer
- dtime
2716 if self
.lifetimer
<= 0 then
2718 -- only despawn away from player
2719 local objs
= minetest
.get_objects_inside_radius(pos
, 15)
2723 if objs
[n
]:is_player() then
2731 -- minetest.log("action",
2732 -- S("lifetimer expired, removed @1", self.name))
2734 effect(pos
, 15, "tnt_smoke.png", 2, 4, 2, 0)
2736 self
.object
:remove()
2745 if self
.pause_timer
> 0 then
2747 self
.pause_timer
= self
.pause_timer
- dtime
2752 -- run custom function (defined in mob lua file)
2753 if self
.do_custom
then
2755 -- when false skip going any further
2756 if self
.do_custom(self
, dtime
) == false then
2762 self
.timer
= self
.timer
+ dtime
2764 if self
.state
~= "attack" then
2766 if self
.timer
< 1 then
2773 -- never go over 100
2774 if self
.timer
> 100 then
2778 -- node replace check (cow eats grass etc.)
2781 -- mob plays random sound at times
2782 if random(1, 100) == 1 then
2783 mob_sound(self
, self
.sounds
.random)
2786 -- environmental damage timer (every 1 second)
2787 self
.env_damage_timer
= self
.env_damage_timer
+ dtime
2789 if (self
.state
== "attack" and self
.env_damage_timer
> 1)
2790 or self
.state
~= "attack" then
2792 self
.env_damage_timer
= 0
2797 monster_attack(self
)
2805 do_states(self
, dtime
)
2814 -- default function when mobs are blown up with TNT
2815 local do_tnt
= function(obj
, damage
)
2817 --print ("----- Damage", damage)
2819 obj
.object
:punch(obj
.object
, 1.0, {
2820 full_punch_interval
= 1.0,
2821 damage_groups
= {fleshy
= damage
},
2824 return false, true, {}
2828 mobs
.spawning_mobs
= {}
2830 -- register mob entity
2831 function mobs
:register_mob(name
, def
)
2833 mobs
.spawning_mobs
[name
] = true
2835 minetest
.register_entity(name
, {
2837 stepheight
= def
.stepheight
or 1.1, -- was 0.6
2840 attack_type
= def
.attack_type
,
2842 fly_in
= def
.fly_in
or "air",
2843 owner
= def
.owner
or "",
2844 order
= def
.order
or "",
2845 on_die
= def
.on_die
,
2846 do_custom
= def
.do_custom
,
2847 jump_height
= def
.jump_height
or 4, -- was 6
2848 drawtype
= def
.drawtype
, -- DEPRECATED, use rotate instead
2849 rotate
= math
.rad(def
.rotate
or 0), -- 0=front, 90=side, 180=back, 270=side2
2850 lifetimer
= def
.lifetimer
or 180, -- 3 minutes
2851 hp_min
= max(1, (def
.hp_min
or 5) * difficulty
),
2852 hp_max
= max(1, (def
.hp_max
or 10) * difficulty
),
2854 collisionbox
= def
.collisionbox
,
2855 selectionbox
= def
.selectionbox
or def
.collisionbox
,
2856 visual
= def
.visual
,
2857 visual_size
= def
.visual_size
or {x
= 1, y
= 1},
2859 makes_footstep_sound
= def
.makes_footstep_sound
or false,
2860 view_range
= def
.view_range
or 5,
2861 walk_velocity
= def
.walk_velocity
or 1,
2862 run_velocity
= def
.run_velocity
or 2,
2863 damage
= max(0, (def
.damage
or 0) * difficulty
),
2864 light_damage
= def
.light_damage
or 0,
2865 water_damage
= def
.water_damage
or 0,
2866 lava_damage
= def
.lava_damage
or 0,
2867 suffocation
= def
.suffocation
or 2,
2868 fall_damage
= def
.fall_damage
or 1,
2869 fall_speed
= def
.fall_speed
or -10, -- must be lower than -2 (default: -10)
2870 drops
= def
.drops
or {},
2871 armor
= def
.armor
or 100,
2872 on_rightclick
= def
.on_rightclick
,
2874 shoot_interval
= def
.shoot_interval
,
2875 sounds
= def
.sounds
or {},
2876 animation
= def
.animation
,
2877 follow
= def
.follow
,
2878 jump
= def
.jump
~= false,
2879 walk_chance
= def
.walk_chance
or 50,
2880 attacks_monsters
= def
.attacks_monsters
or false,
2881 group_attack
= def
.group_attack
or false,
2882 passive
= def
.passive
or false,
2883 knock_back
= def
.knock_back
or 3,
2884 blood_amount
= def
.blood_amount
or 5,
2885 blood_texture
= def
.blood_texture
or "mobs_blood.png",
2886 shoot_offset
= def
.shoot_offset
or 0,
2887 floats
= def
.floats
or 1, -- floats in water by default
2888 replace_rate
= def
.replace_rate
,
2889 replace_what
= def
.replace_what
,
2890 replace_with
= def
.replace_with
,
2891 replace_offset
= def
.replace_offset
or 0,
2892 on_replace
= def
.on_replace
,
2894 env_damage_timer
= 0, -- only used when state = "attack"
2902 reach
= def
.reach
or 3,
2904 texture_list
= def
.textures
,
2905 child_texture
= def
.child_texture
,
2906 docile_by_day
= def
.docile_by_day
or false,
2908 fear_height
= def
.fear_height
or 0,
2909 runaway
= def
.runaway
,
2911 pathfinding
= def
.pathfinding
,
2912 immune_to
= def
.immune_to
or {},
2913 explosion_radius
= def
.explosion_radius
,
2914 explosion_damage_radius
= def
.explosion_damage_radius
,
2915 explosion_timer
= def
.explosion_timer
or 3,
2916 allow_fuse_reset
= def
.allow_fuse_reset
~= false,
2917 stop_to_explode
= def
.stop_to_explode
~= false,
2918 custom_attack
= def
.custom_attack
,
2919 double_melee_attack
= def
.double_melee_attack
,
2920 dogshoot_switch
= def
.dogshoot_switch
,
2922 dogshoot_count_max
= def
.dogshoot_count_max
or 5,
2923 dogshoot_count2_max
= def
.dogshoot_count2_max
or (def
.dogshoot_count_max
or 5),
2924 attack_animals
= def
.attack_animals
or false,
2925 specific_attack
= def
.specific_attack
,
2926 runaway_from
= def
.runaway_from
,
2927 owner_loyal
= def
.owner_loyal
,
2928 facing_fence
= false,
2931 on_spawn
= def
.on_spawn
,
2933 on_blast
= def
.on_blast
or do_tnt
,
2937 do_punch
= def
.do_punch
,
2939 on_punch
= mob_punch
,
2941 on_breed
= def
.on_breed
,
2943 on_grown
= def
.on_grown
,
2945 on_activate
= function(self
, staticdata
, dtime
)
2946 return mob_activate(self
, staticdata
, def
, dtime
)
2949 get_staticdata
= function(self
)
2950 return mob_staticdata(self
)
2955 end -- END mobs:register_mob function
2958 -- count how many mobs of one type are inside an area
2959 local count_mobs
= function(pos
, type)
2963 local objs
= minetest
.get_objects_inside_radius(pos
, aoc_range
)
2967 if not objs
[n
]:is_player() then
2969 local obj
= objs
[n
]:get_luaentity()
2971 -- count mob type and add to total also
2972 if obj
and obj
.name
and obj
.name
== type then
2974 num_type
= num_type
+ 1
2975 num_total
= num_total
+ 1
2977 -- add to total mobs
2978 elseif obj
and obj
.name
and obj
.health
~= nil then
2980 num_total
= num_total
+ 1
2985 return num_type
, num_total
2991 function mobs
:spawn_abm_check(pos
, node
, name
)
2992 -- global function to add additional spawn checks
2993 -- return true to stop spawning mob
2997 function mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
,
2998 interval
, chance
, aoc
, min_height
, max_height
, day_toggle
, on_spawn
)
3000 -- Do mobs spawn at all?
3001 if not mobs_spawn
then
3005 -- chance/spawn number override in minetest.conf for registered mob
3006 local numbers
= minetest
.settings
:get(name
)
3009 numbers
= numbers
:split(",")
3010 chance
= tonumber(numbers
[1]) or chance
3011 aoc
= tonumber(numbers
[2]) or aoc
3014 minetest
.log("warning", string.format("[mobs] %s has spawning disabled", name
))
3018 minetest
.log("action",
3019 string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name
, chance
, aoc
))
3023 minetest
.register_abm({
3025 label
= name
.. " spawning",
3027 neighbors
= neighbors
,
3028 interval
= interval
,
3029 chance
= max(1, (chance
* mob_chance_multiplier
)),
3032 action
= function(pos
, node
, active_object_count
, active_object_count_wider
)
3034 -- is mob actually registered?
3035 if not mobs
.spawning_mobs
[name
]
3036 or not minetest
.registered_entities
[name
] then
3037 --print ("--- mob doesn't exist", name)
3041 -- additional custom checks for spawning mob
3042 if mobs
:spawn_abm_check(pos
, node
, name
) == true then
3046 -- do not spawn if too many of same mob in area
3047 if active_object_count_wider
>= max_per_block
3048 or count_mobs(pos
, name
) >= aoc
then
3049 --print ("--- too many entities", name, aoc, active_object_count_wider)
3053 -- if toggle set to nil then ignore day/night check
3054 if day_toggle
~= nil then
3056 local tod
= (minetest
.get_timeofday() or 0) * 24000
3058 if tod
> 4500 and tod
< 19500 then
3059 -- daylight, but mob wants night
3060 if day_toggle
== false then
3061 --print ("--- mob needs night", name)
3065 -- night time but mob wants day
3066 if day_toggle
== true then
3067 --print ("--- mob needs day", name)
3076 -- only spawn away from player
3077 local objs
= minetest
.get_objects_inside_radius(pos
, 10)
3081 if objs
[n
]:is_player() then
3082 --print ("--- player too close", name)
3087 -- mobs cannot spawn in protected areas when enabled
3088 if not spawn_protected
3089 and minetest
.is_protected(pos
, "") then
3090 --print ("--- inside protected area", name)
3094 -- are we spawning within height limits?
3095 if pos
.y
> max_height
3096 or pos
.y
< min_height
then
3097 --print ("--- height limits not met", name, pos.y)
3101 -- are light levels ok?
3102 local light
= minetest
.get_node_light(pos
)
3104 or light
> max_light
3105 or light
< min_light
then
3106 --print ("--- light limits not met", name, light)
3110 -- do we have enough height clearance to spawn mob?
3111 local ent
= minetest
.registered_entities
[name
]
3112 local height
= max(0, math
.ceil(ent
.collisionbox
[5] - ent
.collisionbox
[2]) - 1)
3114 for n
= 0, height
do
3116 local pos2
= {x
= pos
.x
, y
= pos
.y
+ n
, z
= pos
.z
}
3118 if minetest
.registered_nodes
[node_ok(pos2
).name
].walkable
== true then
3119 --print ("--- inside block", name, node_ok(pos2).name)
3124 -- spawn mob half block higher than ground
3127 local mob
= minetest
.add_entity(pos
, name
)
3129 print ("[mobs] Spawned " .. name .. " at "
3130 .. minetest.pos_to_string(pos) .. " on "
3131 .. node.name .. " near " .. neighbors[1])
3135 local ent
= mob
:get_luaentity()
3144 -- compatibility with older mob registration
3145 function mobs
:register_spawn(name
, nodes
, max_light
, min_light
, chance
, active_object_count
, max_height
, day_toggle
)
3147 mobs
:spawn_specific(name
, nodes
, {"air"}, min_light
, max_light
, 30,
3148 chance
, active_object_count
, -31000, max_height
, day_toggle
)
3152 -- MarkBu's spawn function
3153 function mobs
:spawn(def
)
3155 local name
= def
.name
3156 local nodes
= def
.nodes
or {"group:soil", "group:stone"}
3157 local neighbors
= def
.neighbors
or {"air"}
3158 local min_light
= def
.min_light
or 0
3159 local max_light
= def
.max_light
or 15
3160 local interval
= def
.interval
or 30
3161 local chance
= def
.chance
or 5000
3162 local active_object_count
= def
.active_object_count
or 1
3163 local min_height
= def
.min_height
or -31000
3164 local max_height
= def
.max_height
or 31000
3165 local day_toggle
= def
.day_toggle
3166 local on_spawn
= def
.on_spawn
3168 mobs
:spawn_specific(name
, nodes
, neighbors
, min_light
, max_light
, interval
,
3169 chance
, active_object_count
, min_height
, max_height
, day_toggle
, on_spawn
)
3173 -- register arrow for shoot attack
3174 function mobs
:register_arrow(name
, def
)
3176 if not name
or not def
then return end -- errorcheck
3178 minetest
.register_entity(name
, {
3181 visual
= def
.visual
,
3182 visual_size
= def
.visual_size
,
3183 textures
= def
.textures
,
3184 velocity
= def
.velocity
,
3185 hit_player
= def
.hit_player
,
3186 hit_node
= def
.hit_node
,
3187 hit_mob
= def
.hit_mob
,
3188 drop
= def
.drop
or false, -- drops arrow as registered item when true
3189 collisionbox
= {0, 0, 0, 0, 0, 0}, -- remove box around arrows
3192 owner_id
= def
.owner_id
,
3193 rotate
= def
.rotate
,
3194 automatic_face_movement_dir
= def
.rotate
3195 and (def
.rotate
- (pi
/ 180)) or false,
3197 on_activate
= def
.on_activate
,
3199 on_step
= def
.on_step
or function(self
, dtime
)
3201 self
.timer
= self
.timer
+ 1
3203 local pos
= self
.object
:get_pos()
3207 or not within_limits(pos
, 0) then
3209 self
.object
:remove() ; -- print ("removed arrow")
3214 -- does arrow have a tail (fireball)
3217 and def
.tail_texture
then
3219 minetest
.add_particle({
3221 velocity
= {x
= 0, y
= 0, z
= 0},
3222 acceleration
= {x
= 0, y
= 0, z
= 0},
3223 expirationtime
= def
.expire
or 0.25,
3224 collisiondetection
= false,
3225 texture
= def
.tail_texture
,
3226 size
= def
.tail_size
or 5,
3227 glow
= def
.glow
or 0,
3231 if self
.hit_node
then
3233 local node
= node_ok(pos
).name
3235 if minetest
.registered_nodes
[node
].walkable
then
3237 self
.hit_node(self
, pos
, node
)
3239 if self
.drop
== true then
3243 self
.lastpos
= (self
.lastpos
or pos
)
3245 minetest
.add_item(self
.lastpos
, self
.object
:get_luaentity().name
)
3248 self
.object
:remove() ; -- print ("hit node")
3254 if self
.hit_player
or self
.hit_mob
then
3256 for _
,player
in pairs(minetest
.get_objects_inside_radius(pos
, 1.0)) do
3259 and player
:is_player() then
3261 self
.hit_player(self
, player
)
3262 self
.object
:remove() ; -- print ("hit player")
3266 local entity
= player
:get_luaentity()
3270 and entity
._cmi_is_mob
== true
3271 and tostring(player
) ~= self
.owner_id
3272 and entity
.name
~= self
.object
:get_luaentity().name
then
3274 self
.hit_mob(self
, player
)
3276 self
.object
:remove() ; --print ("hit mob")
3289 -- compatibility function
3290 function mobs
:explosion(pos
, radius
)
3291 local self
= {sounds
= {}}
3292 self
.sounds
.explode
= "tnt_explode"
3293 mobs
:boom(self
, pos
, radius
)
3297 -- no damage to nodes explosion
3298 function mobs
:safe_boom(self
, pos
, radius
)
3300 minetest
.sound_play(self
.sounds
and self
.sounds
.explode
or "tnt_explode", {
3303 max_hear_distance
= self
.sounds
and self
.sounds
.distance
or 32
3306 entity_physics(pos
, radius
)
3307 effect(pos
, 32, "tnt_smoke.png", radius
* 3, radius
* 5, radius
, 1, 0)
3311 -- make explosion with protection and tnt mod check
3312 function mobs
:boom(self
, pos
, radius
)
3315 and minetest
.get_modpath("mcl_tnt") and tnt
and tnt
.boom
3316 and not minetest
.is_protected(pos
, "") then
3320 damage_radius
= radius
,
3321 sound
= self
.sounds
and self
.sounds
.explode
,
3322 explode_center
= true,
3325 mobs
:safe_boom(self
, pos
, radius
)
3330 -- Register spawn eggs
3332 -- Note: This also introduces the “spawn_egg” group:
3333 -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
3334 -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
3335 function mobs
:register_egg(mob
, desc
, background
, addegg
, no_creative
)
3337 local grp
= {spawn_egg
= 1}
3339 -- do NOT add this egg to creative inventory (e.g. dungeon master)
3340 if no_creative
== true then
3341 grp
.not_in_creative_inventory
= 1
3344 local invimg
= background
3347 invimg
= "mobs_chicken_egg.png^(" .. invimg
..
3348 "^[mask:mobs_chicken_egg_overlay.png)"
3351 -- register new spawn egg containing mob information
3352 --[=[ DISABLED IN MCL2
3353 minetest
.register_craftitem(mob
.. "_set", {
3355 description
= S("@1 (Tamed)", desc
),
3356 inventory_image
= invimg
,
3357 groups
= {spawn_egg
= 2, not_in_creative_inventory
= 1},
3360 on_place
= function(itemstack
, placer
, pointed_thing
)
3362 local pos
= pointed_thing
.above
3364 -- am I clicking on something with existing on_rightclick function?
3365 local under
= minetest
.get_node(pointed_thing
.under
)
3366 local def
= minetest
.registered_nodes
[under
.name
]
3367 if def
and def
.on_rightclick
then
3368 return def
.on_rightclick(pointed_thing
.under
, under
, placer
, itemstack
)
3372 and within_limits(pos
, 0)
3373 and not minetest
.is_protected(pos
, placer
:get_player_name()) then
3375 if not minetest
.registered_entities
[mob
] then
3381 local data
= itemstack
:get_metadata()
3382 local mob
= minetest
.add_entity(pos
, mob
, data
)
3383 local ent
= mob
:get_luaentity()
3385 -- set owner if not a monster
3386 if ent
.type ~= "monster" then
3387 ent
.owner
= placer
:get_player_name()
3391 -- since mob is unique we remove egg once spawned
3392 itemstack
:take_item()
3400 -- register old stackable mob egg
3401 minetest
.register_craftitem(mob
, {
3405 inventory_image
= invimg
,
3408 _doc_items_longdesc
= "This allows you to place a single mob.",
3409 _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.",
3411 on_place
= function(itemstack
, placer
, pointed_thing
)
3413 local pos
= pointed_thing
.above
3415 -- am I clicking on something with existing on_rightclick function?
3416 local under
= minetest
.get_node(pointed_thing
.under
)
3417 local def
= minetest
.registered_nodes
[under
.name
]
3418 if def
and def
.on_rightclick
then
3419 return def
.on_rightclick(pointed_thing
.under
, under
, placer
, itemstack
)
3423 and within_limits(pos
, 0)
3424 and not minetest
.is_protected(pos
, placer
:get_player_name()) then
3426 local name
= placer
:get_player_name()
3427 local privs
= minetest
.get_player_privs(name
)
3428 if not privs
.maphack
then
3429 minetest
.chat_send_player(name
, "You need the “maphack” privilege to change the mob spawner.")
3432 if minetest
.get_modpath("mcl_mobspawners") and under
.name
== "mcl_mobspawners:spawner" then
3433 mcl_mobspawners
.setup_spawner(pointed_thing
.under
, itemstack
:get_name())
3434 if not minetest
.settings
:get_bool("creative_mode") then
3435 itemstack
:take_item()
3440 if not minetest
.registered_entities
[mob
] then
3446 local mob
= minetest
.add_entity(pos
, mob
)
3447 local ent
= mob
:get_luaentity()
3449 -- don't set owner if monster or sneak pressed
3450 if ent
.type ~= "monster"
3451 and not placer
:get_player_control().sneak
then
3452 ent
.owner
= placer
:get_player_name()
3456 local nametag
= itemstack
:get_meta():get_string("name")
3457 if nametag
~= "" then
3458 if string.len(nametag
) > MAX_MOB_NAME_LENGTH
then
3459 nametag
= string.sub(nametag
, 1, MAX_MOB_NAME_LENGTH
)
3461 ent
.nametag
= nametag
3465 -- if not in creative then take item
3466 if not mobs
.is_creative(placer
:get_player_name()) then
3467 itemstack
:take_item()
3478 -- capture critter (thanks to blert2112 for idea)
3479 function mobs
:capture_mob(self
, clicker
, chance_hand
, chance_net
, chance_lasso
, force_take
, replacewith
)
3484 or not clicker
:is_player()
3485 or not clicker
:get_inventory() then
3489 -- get name of clicked mob
3490 local mobname
= self
.name
3492 -- if not nil change what will be added to inventory
3494 mobname
= replacewith
3497 local name
= clicker
:get_player_name()
3498 local tool
= clicker
:get_wielded_item()
3500 -- are we using hand, net or lasso to pick up mob?
3501 if tool
:get_name() ~= ""
3502 and tool
:get_name() ~= "mobs:net"
3503 and tool
:get_name() ~= "mobs:lasso" then
3508 if self
.tamed
== false
3509 and force_take
== false then
3511 minetest
.chat_send_player(name
, S("Not tamed!"))
3513 return true -- false
3516 -- cannot pick up if not owner
3517 if self
.owner
~= name
3518 and force_take
== false then
3520 minetest
.chat_send_player(name
, S("@1 is owner!", self
.owner
))
3522 return true -- false
3525 if clicker
:get_inventory():room_for_item("main", mobname
) then
3527 -- was mob clicked with hand, net, or lasso?
3530 if tool
:get_name() == "" then
3531 chance
= chance_hand
3533 elseif tool
:get_name() == "mobs:net" then
3537 tool
:add_wear(4000) -- 17 uses
3539 clicker
:set_wielded_item(tool
)
3541 elseif tool
:get_name() == "mobs:lasso" then
3543 chance
= chance_lasso
3545 tool
:add_wear(650) -- 100 uses
3547 clicker
:set_wielded_item(tool
)
3551 -- calculate chance.. add to inventory if successful?
3552 if chance
> 0 and random(1, 100) <= chance
then
3555 local new_stack
= ItemStack(mobname
)
3557 -- add special mob egg with all mob information
3558 -- unless 'replacewith' contains new item to use
3559 if not replacewith
then
3561 new_stack
= ItemStack(mobname
.. "_set")
3565 for _
,stat
in pairs(self
) do
3566 local t
= type(stat
)
3569 and t
~= "userdata" then
3574 local data_str
= minetest
.serialize(tmp
)
3576 new_stack
:set_metadata(data_str
)
3579 local inv
= clicker
:get_inventory()
3581 if inv
:room_for_item("main", new_stack
) then
3582 inv
:add_item("main", new_stack
)
3584 minetest
.add_item(clicker
:get_pos(), new_stack
)
3587 self
.object
:remove()
3589 mob_sound(self
, "default_place_node_hard")
3593 minetest
.chat_send_player(name
, S("Missed!"))
3595 mob_sound(self
, "mobs_swing")
3604 -- protect tamed mob with rune item
3605 function mobs
:protect(self
, clicker
)
3607 local name
= clicker
:get_player_name()
3608 local tool
= clicker
:get_wielded_item()
3610 if tool
:get_name() ~= "mobs:protector" then
3614 if self
.tamed
== false then
3615 minetest
.chat_send_player(name
, S("Not tamed!"))
3616 return true -- false
3619 if self
.protected
== true then
3620 minetest
.chat_send_player(name
, S("Already protected!"))
3621 return true -- false
3624 if not mobs
.is_creative(clicker
:get_player_name()) then
3625 tool
:take_item() -- take 1 protection rune
3626 clicker
:set_wielded_item(tool
)
3629 self
.protected
= true
3631 local pos
= self
.object
:get_pos()
3632 pos
.y
= pos
.y
+ self
.collisionbox
[2] + 0.5
3634 effect(self
.object
:get_pos(), 25, "mobs_protect_particle.png", 0.5, 4, 2, 15)
3636 mob_sound(self
, "mobs_spell")
3645 -- feeding, taming and breeding (thanks blert2112)
3646 function mobs
:feed_tame(self
, clicker
, feed_count
, breed
, tame
)
3648 if not self
.follow
then
3652 -- can eat/tame with item in hand
3653 if follow_holding(self
, clicker
) then
3655 -- if not in creative then take item
3656 if not mobs
.is_creative(clicker
:get_player_name()) then
3658 local item
= clicker
:get_wielded_item()
3662 clicker
:set_wielded_item(item
)
3666 self
.health
= self
.health
+ 4
3668 if self
.health
>= self
.hp_max
then
3670 self
.health
= self
.hp_max
3672 if self
.htimer
< 1 then
3676 minetest
.chat_send_player(clicker
:get_player_name(),
3677 S("@1 at full health (@2)",
3678 self
.name
:split(":")[2], tostring(self
.health
)))
3685 self
.object
:set_hp(self
.health
)
3689 -- make children grow quicker
3690 if self
.child
== true then
3692 self
.hornytimer
= self
.hornytimer
+ 20
3698 self
.food
= (self
.food
or 0) + 1
3699 if self
.food
>= feed_count
then
3703 if breed
and self
.hornytimer
== 0 then
3711 if self
.tamed
== false then
3712 minetest
.chat_send_player(clicker
:get_player_name(),
3713 S("@1 has been tamed!",
3714 self
.name
:split(":")[2]))
3719 if not self
.owner
or self
.owner
== "" then
3720 self
.owner
= clicker
:get_player_name()
3724 -- make sound when fed so many times
3725 mob_sound(self
, self
.sounds
.random)
3731 local item
= clicker
:get_wielded_item()
3733 -- Name mob with nametag
3734 if item
:get_name() == "mobs:nametag" then
3736 local tag = item
:get_meta():get_string("name")
3738 if string.len(tag) > MAX_MOB_NAME_LENGTH
then
3739 tag = string.sub(tag, 1, MAX_MOB_NAME_LENGTH
)
3745 if not mobs
.is_creative(name
) then
3747 player
:set_wielded_item(item
)
3759 -- inspired by blockmen's nametag mod
3760 minetest
.register_on_player_receive_fields(function(player
, formname
, fields
)
3762 -- right-clicked with nametag and name entered?
3763 if formname
== "mobs_nametag"
3765 and fields
.name
~= "" then
3767 local name
= player
:get_player_name()
3769 if not mob_obj
[name
]
3770 or not mob_obj
[name
].object
then
3774 -- make sure nametag is being used to name mob
3775 local item
= player
:get_wielded_item()
3777 if item
:get_name() ~= "mobs:nametag" then
3781 -- limit name entered to 64 characters long
3782 if string.len(fields
.name
) > 64 then
3783 fields
.name
= string.sub(fields
.name
, 1, 64)
3787 mob_obj
[name
].nametag
= fields
.name
3789 update_tag(mob_obj
[name
])
3791 -- if not in creative then take item
3792 if not mobs
.is_creative(name
) then
3794 mob_sta
[name
]:take_item()
3796 player
:set_wielded_item(mob_sta
[name
])
3799 -- reset external variables
3807 -- compatibility function for old entities to new modpack entities
3808 function mobs
:alias_mob(old_name
, new_name
)
3811 minetest
.register_alias(old_name
, new_name
)
3814 minetest
.register_entity(":" .. old_name
, {
3818 on_step
= function(self
)
3820 local pos
= self
.object
:get_pos()
3822 if minetest
.registered_entities
[new_name
] then
3823 minetest
.add_entity(pos
, new_name
)
3826 self
.object
:remove()