Mob API for mods to add monsters, animals and more. https://content.minetest.net/packages/TenPlus1/mobs/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4545 lines
98KB

  1. -- Intllib and CMI support check
  2. local MP = minetest.get_modpath(minetest.get_current_modname())
  3. local S, NS = dofile(MP .. "/intllib.lua")
  4. local use_cmi = minetest.global_exists("cmi")
  5. mobs = {
  6. mod = "redo",
  7. version = "20200528",
  8. intllib = S,
  9. invis = minetest.global_exists("invisibility") and invisibility or {}
  10. }
  11. -- creative check
  12. local creative_cache = minetest.settings:get_bool("creative_mode")
  13. function mobs.is_creative(name)
  14. return creative_cache or minetest.check_player_privs(name,
  15. {creative = true})
  16. end
  17. -- localize math functions
  18. local pi = math.pi
  19. local square = math.sqrt
  20. local sin = math.sin
  21. local cos = math.cos
  22. local abs = math.abs
  23. local min = math.min
  24. local max = math.max
  25. local random = math.random
  26. local floor = math.floor
  27. local ceil = math.ceil
  28. local rad = math.rad
  29. local atann = math.atan
  30. local atan = function(x)
  31. if not x or x ~= x then
  32. --error("atan bassed NaN")
  33. return 0
  34. else
  35. return atann(x)
  36. end
  37. end
  38. -- Load settings
  39. local damage_enabled = minetest.settings:get_bool("enable_damage")
  40. local mobs_spawn = minetest.settings:get_bool("mobs_spawn") ~= false
  41. local peaceful_only = minetest.settings:get_bool("only_peaceful_mobs")
  42. local disable_blood = minetest.settings:get_bool("mobs_disable_blood")
  43. local mobs_drop_items = minetest.settings:get_bool("mobs_drop_items") ~= false
  44. local mobs_griefing = minetest.settings:get_bool("mobs_griefing") ~= false
  45. local spawn_protected = minetest.settings:get_bool("mobs_spawn_protected") ~= false
  46. local remove_far = minetest.settings:get_bool("remove_far_mobs") ~= false
  47. local difficulty = tonumber(minetest.settings:get("mob_difficulty")) or 1.0
  48. local show_health = minetest.settings:get_bool("mob_show_health") ~= false
  49. local max_per_block = tonumber(minetest.settings:get("max_objects_per_block") or 99)
  50. local mob_nospawn_range = tonumber(minetest.settings:get("mob_nospawn_range") or 12)
  51. local active_limit = tonumber(minetest.settings:get("mob_active_limit") or 0)
  52. local mob_chance_multiplier =
  53. tonumber(minetest.settings:get("mob_chance_multiplier") or 1)
  54. local active_mobs = 0
  55. -- Peaceful mode message so players will know there are no monsters
  56. if peaceful_only then
  57. minetest.register_on_joinplayer(function(player)
  58. minetest.chat_send_player(player:get_player_name(),
  59. S("** Peaceful Mode Active - No Monsters Will Spawn"))
  60. end)
  61. end
  62. -- calculate aoc range for mob count
  63. local aoc_range = tonumber(minetest.settings:get("active_block_range")) * 16
  64. -- pathfinding settings
  65. local enable_pathfinding = true
  66. local stuck_timeout = 3 -- how long before stuck mod starts searching
  67. local stuck_path_timeout = 10 -- how long will mob follow path before giving up
  68. -- default nodes
  69. local node_fire = "fire:basic_flame"
  70. local node_permanent_flame = "fire:permanent_flame"
  71. local node_ice = "default:ice"
  72. local node_snowblock = "default:snowblock"
  73. local node_snow = "default:snow"
  74. mobs.fallback_node = minetest.registered_aliases["mapgen_dirt"] or "default:dirt"
  75. local mob_class = {
  76. stepheight = 1.1,
  77. fly_in = "air",
  78. owner = "",
  79. order = "",
  80. jump_height = 4,
  81. lifetimer = 180, -- 3 minutes
  82. physical = true,
  83. collisionbox = {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25},
  84. visual_size = {x = 1, y = 1},
  85. texture_mods = "",
  86. makes_footstep_sound = false,
  87. view_range = 5,
  88. walk_velocity = 1,
  89. run_velocity = 2,
  90. light_damage = 0,
  91. light_damage_min = 14,
  92. light_damage_max = 15,
  93. water_damage = 0,
  94. lava_damage = 0,
  95. suffocation = 2,
  96. fall_damage = 1,
  97. fall_speed = -10, -- must be lower than -2 (default: -10)
  98. drops = {},
  99. armor = 100,
  100. sounds = {},
  101. jump = true,
  102. knock_back = true,
  103. walk_chance = 50,
  104. stand_chance = 30,
  105. attack_chance = 5,
  106. passive = false,
  107. blood_amount = 5,
  108. blood_texture = "mobs_blood.png",
  109. shoot_offset = 0,
  110. floats = 1, -- floats in water by default
  111. replace_offset = 0,
  112. timer = 0,
  113. env_damage_timer = 0, -- only used when state = "attack"
  114. tamed = false,
  115. pause_timer = 0,
  116. horny = false,
  117. hornytimer = 0,
  118. child = false,
  119. gotten = false,
  120. health = 0,
  121. reach = 3,
  122. htimer = 0,
  123. docile_by_day = false,
  124. time_of_day = 0.5,
  125. fear_height = 0,
  126. runaway_timer = 0,
  127. immune_to = {},
  128. explosion_timer = 3,
  129. allow_fuse_reset = true,
  130. stop_to_explode = true,
  131. dogshoot_count = 0,
  132. dogshoot_count_max = 5,
  133. dogshoot_count2_max = 5,
  134. group_attack = false,
  135. attack_monsters = false,
  136. attack_animals = false,
  137. attack_players = true,
  138. attack_npcs = true,
  139. facing_fence = false,
  140. _cmi_is_mob = true
  141. }
  142. local mob_class_meta = {__index = mob_class}
  143. -- play sound
  144. function mob_class:mob_sound(sound)
  145. local pitch = 1.0
  146. -- higher pitch for a child
  147. if self.child then pitch = pitch * 1.5 end
  148. -- a little random pitch to be different
  149. pitch = pitch + random(-10, 10) * 0.005
  150. if sound then
  151. minetest.sound_play(sound, {
  152. object = self.object,
  153. gain = 1.0,
  154. max_hear_distance = self.sounds.distance,
  155. pitch = pitch
  156. }, true)
  157. end
  158. end
  159. -- attack player/mob
  160. function mob_class:do_attack(player)
  161. if self.state == "attack" then
  162. return
  163. end
  164. self.attack = player
  165. self.state = "attack"
  166. if random(0, 100) < 90 then
  167. self:mob_sound(self.sounds.war_cry)
  168. end
  169. end
  170. -- calculate distance
  171. local get_distance = function(a, b)
  172. local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z
  173. return square(x * x + y * y + z * z)
  174. end
  175. -- collision function based on jordan4ibanez' open_ai mod
  176. function mob_class:collision()
  177. local pos = self.object:get_pos()
  178. local vel = self.object:get_velocity()
  179. local x, z = 0, 0
  180. local width = -self.collisionbox[1] + self.collisionbox[4] + 0.5
  181. for _,object in ipairs(minetest.get_objects_inside_radius(pos, width)) do
  182. if object:is_player()
  183. or (object:get_luaentity()
  184. and object:get_luaentity()._cmi_is_mob == true
  185. and object ~= self.object) then
  186. local pos2 = object:get_pos()
  187. local vec = {x = pos.x - pos2.x, z = pos.z - pos2.z}
  188. x = x + vec.x
  189. z = z + vec.z
  190. end
  191. end
  192. return({x, z})
  193. end
  194. -- move mob in facing direction
  195. function mob_class:set_velocity(v)
  196. -- halt mob if it has been ordered to stay
  197. if self.order == "stand" then
  198. self.object:set_velocity({x = 0, y = 0, z = 0})
  199. return
  200. end
  201. local c_x, c_y = 0, 0
  202. -- can mob be pushed, if so calculate direction
  203. if self.pushable then
  204. c_x, c_y = unpack(self:collision())
  205. end
  206. local yaw = (self.object:get_yaw() or 0) + self.rotate
  207. -- nil check for velocity
  208. v = v or 0
  209. -- set velocity with hard limit of 10
  210. local vel = self.object:get_velocity()
  211. self.object:set_velocity({
  212. x = max(-10, min((sin(yaw) * -v) + c_x, 10)),
  213. y = max(-10, min((vel and vel.y or 0), 10)),
  214. z = max(-10, min((cos(yaw) * v) + c_y, 10))
  215. })
  216. end
  217. -- global version of above function
  218. function mobs:set_velocity(entity, v)
  219. mob_class.set_velocity(entity, v)
  220. end
  221. -- calculate mob velocity
  222. function mob_class:get_velocity()
  223. local v = self.object:get_velocity()
  224. if not v then return 0 end
  225. return (v.x * v.x + v.z * v.z) ^ 0.5
  226. end
  227. -- set and return valid yaw
  228. function mob_class:set_yaw(yaw, delay)
  229. if not yaw or yaw ~= yaw then
  230. yaw = 0
  231. end
  232. delay = delay or 0
  233. if delay == 0 then
  234. self.object:set_yaw(yaw)
  235. return yaw
  236. end
  237. self.target_yaw = yaw
  238. self.delay = delay
  239. return self.target_yaw
  240. end
  241. -- global function to set mob yaw
  242. function mobs:yaw(entity, yaw, delay)
  243. mob_class.set_yaw(entity, yaw, delay)
  244. end
  245. -- set defined animation
  246. function mob_class:set_animation(anim, force)
  247. if not self.animation or not anim then return end
  248. self.animation.current = self.animation.current or ""
  249. -- only use different animation for attacks when using same set
  250. if force ~= true and anim ~= "punch" and anim ~= "shoot"
  251. and string.find(self.animation.current, anim) then
  252. return
  253. end
  254. -- check for more than one animation
  255. local num = 0
  256. for n = 1, 4 do
  257. if self.animation[anim .. n .. "_start"]
  258. and self.animation[anim .. n .. "_end"] then
  259. num = n
  260. end
  261. end
  262. -- choose random animation from set
  263. if num > 0 then
  264. num = random(0, num)
  265. anim = anim .. (num ~= 0 and num or "")
  266. end
  267. if anim == self.animation.current
  268. or not self.animation[anim .. "_start"]
  269. or not self.animation[anim .. "_end"] then
  270. return
  271. end
  272. self.animation.current = anim
  273. self.object:set_animation({
  274. x = self.animation[anim .. "_start"],
  275. y = self.animation[anim .. "_end"]},
  276. self.animation[anim .. "_speed"] or
  277. self.animation.speed_normal or 15,
  278. 0, self.animation[anim .. "_loop"] ~= false)
  279. end
  280. -- above function exported for mount.lua
  281. function mobs:set_animation(entity, anim)
  282. mob_class.set_animation(entity, anim)
  283. end
  284. -- check line of sight (BrunoMine)
  285. local line_of_sight = function(self, pos1, pos2, stepsize)
  286. stepsize = stepsize or 1
  287. local s, pos = minetest.line_of_sight(pos1, pos2, stepsize)
  288. -- normal walking and flying mobs can see you through air
  289. if s == true then
  290. return true
  291. end
  292. -- New pos1 to be analyzed
  293. local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
  294. local r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
  295. -- Checks the return
  296. if r == true then return true end
  297. -- Nodename found
  298. local nn = minetest.get_node(pos).name
  299. -- Target Distance (td) to travel
  300. local td = get_distance(pos1, pos2)
  301. -- Actual Distance (ad) traveled
  302. local ad = 0
  303. -- It continues to advance in the line of sight in search of a real
  304. -- obstruction which counts as 'walkable' nodebox.
  305. while minetest.registered_nodes[nn]
  306. and (minetest.registered_nodes[nn].walkable == false) do
  307. -- Check if you can still move forward
  308. if td < ad + stepsize then
  309. return true -- Reached the target
  310. end
  311. -- Moves the analyzed pos
  312. local d = get_distance(pos1, pos2)
  313. npos1.x = ((pos2.x - pos1.x) / d * stepsize) + pos1.x
  314. npos1.y = ((pos2.y - pos1.y) / d * stepsize) + pos1.y
  315. npos1.z = ((pos2.z - pos1.z) / d * stepsize) + pos1.z
  316. -- NaN checks
  317. if d == 0
  318. or npos1.x ~= npos1.x
  319. or npos1.y ~= npos1.y
  320. or npos1.z ~= npos1.z then
  321. return false
  322. end
  323. ad = ad + stepsize
  324. -- scan again
  325. r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
  326. if r == true then return true end
  327. -- New Nodename found
  328. nn = minetest.get_node(pos).name
  329. end
  330. return false
  331. end
  332. -- check line of sight (by BrunoMine, tweaked by Astrobe)
  333. local new_line_of_sight = function(self, pos1, pos2, stepsize)
  334. if not pos1 or not pos2 then return end
  335. stepsize = stepsize or 1
  336. local stepv = vector.multiply(vector.direction(pos1, pos2), stepsize)
  337. local s, pos = minetest.line_of_sight(pos1, pos2, stepsize)
  338. -- normal walking and flying mobs can see you through air
  339. if s == true then return true end
  340. -- New pos1 to be analyzed
  341. local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
  342. local r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
  343. -- Checks the return
  344. if r == true then return true end
  345. -- Nodename found
  346. local nn = minetest.get_node(pos).name
  347. -- It continues to advance in the line of sight in search of a real
  348. -- obstruction which counts as 'walkable' nodebox.
  349. while minetest.registered_nodes[nn]
  350. and (minetest.registered_nodes[nn].walkable == false) do
  351. npos1 = vector.add(npos1, stepv)
  352. if get_distance(npos1, pos2) < stepsize then return true end
  353. -- scan again
  354. r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
  355. if r == true then return true end
  356. -- New Nodename found
  357. nn = minetest.get_node(pos).name
  358. end
  359. return false
  360. end
  361. -- check line of sight using raycasting (thanks Astrobe)
  362. local ray_line_of_sight = function(self, pos1, pos2)
  363. local ray = minetest.raycast(pos1, pos2, true, false)
  364. local thing = ray:next()
  365. while thing do -- thing.type, thing.ref
  366. if thing.type == "node" then
  367. local name = minetest.get_node(thing.under).name
  368. if minetest.registered_items[name]
  369. and minetest.registered_items[name].walkable then
  370. return false
  371. end
  372. end
  373. thing = ray:next()
  374. end
  375. return true
  376. end
  377. -- detect if using minetest 5.0 by searching for permafrost node
  378. local is_50 = minetest.registered_nodes["default:permafrost"]
  379. function mob_class:line_of_sight(pos1, pos2, stepsize)
  380. if is_50 then -- only use if minetest 5.0 is detected
  381. return ray_line_of_sight(self, pos1, pos2)
  382. end
  383. return line_of_sight(self, pos1, pos2, stepsize)
  384. end
  385. -- global function
  386. function mobs:line_of_sight(entity, pos1, pos2, stepsize)
  387. return entity:line_of_sight(pos1, pos2, stepsize)
  388. end
  389. function mob_class:attempt_flight_correction(override)
  390. if self:flight_check() and override ~= true then return true end
  391. -- We are not flying in what we are supposed to.
  392. -- See if we can find intended flight medium and return to it
  393. local pos = self.object:get_pos()
  394. local searchnodes = self.fly_in
  395. if type(searchnodes) == "string" then
  396. searchnodes = {self.fly_in}
  397. end
  398. local flyable_nodes = minetest.find_nodes_in_area(
  399. {x = pos.x - 1, y = pos.y - 1, z = pos.z - 1},
  400. {x = pos.x + 1, y = pos.y + 1, z = pos.z + 1}, searchnodes)
  401. if #flyable_nodes < 1 then
  402. return false
  403. end
  404. local escape_target = flyable_nodes[random(#flyable_nodes)]
  405. local escape_direction = vector.direction(pos, escape_target)
  406. self.object:set_velocity(
  407. vector.multiply(escape_direction, 1)) --self.run_velocity))
  408. return true
  409. end
  410. -- are we flying in what we are suppose to? (taikedz)
  411. function mob_class:flight_check()
  412. local def = minetest.registered_nodes[self.standing_in]
  413. if not def then return false end
  414. if type(self.fly_in) == "string"
  415. and self.standing_in == self.fly_in then
  416. return true
  417. elseif type(self.fly_in) == "table" then
  418. for _,fly_in in pairs(self.fly_in) do
  419. if self.standing_in == fly_in then
  420. return true
  421. end
  422. end
  423. end
  424. -- stops mobs getting stuck inside stairs and plantlike nodes
  425. if def.drawtype ~= "airlike"
  426. and def.drawtype ~= "liquid"
  427. and def.drawtype ~= "flowingliquid" then
  428. return true
  429. end
  430. return false
  431. end
  432. -- turn mob to face position
  433. local yaw_to_pos = function(self, target, rot)
  434. rot = rot or 0
  435. local pos = self.object:get_pos()
  436. local vec = {x = target.x - pos.x, z = target.z - pos.z}
  437. local yaw = (atan(vec.z / vec.x) + rot + pi / 2) - self.rotate
  438. if target.x > pos.x then
  439. yaw = yaw + pi
  440. end
  441. yaw = self:set_yaw(yaw, 6)
  442. return yaw
  443. end
  444. function mobs:yaw_to_pos(self, target, rot)
  445. return yaw_to_pos(self, target, rot)
  446. end
  447. -- if stay near set then check periodically for nodes and turn towards them
  448. function mob_class:do_stay_near()
  449. if not self.stay_near then return false end
  450. local pos = self.object:get_pos()
  451. local searchnodes = self.stay_near[1]
  452. local chance = self.stay_near[2] or 10
  453. if random(chance) > 1 then
  454. return false
  455. end
  456. if type(searchnodes) == "string" then
  457. searchnodes = {self.stay_near[1]}
  458. end
  459. local r = self.view_range
  460. local nearby_nodes = minetest.find_nodes_in_area(
  461. {x = pos.x - r, y = pos.y - 1, z = pos.z - r},
  462. {x = pos.x + r, y = pos.y + 1, z = pos.z + r}, searchnodes)
  463. if #nearby_nodes < 1 then
  464. return false
  465. end
  466. yaw_to_pos(self, nearby_nodes[random(#nearby_nodes)])
  467. self:set_animation("walk")
  468. self:set_velocity(self.walk_velocity)
  469. return true
  470. end
  471. -- custom particle effects
  472. local effect = function(pos, amount, texture, min_size, max_size,
  473. radius, gravity, glow, fall)
  474. radius = radius or 2
  475. min_size = min_size or 0.5
  476. max_size = max_size or 1
  477. gravity = gravity or -10
  478. glow = glow or 0
  479. fall = fall and 0 or -radius
  480. minetest.add_particlespawner({
  481. amount = amount,
  482. time = 0.25,
  483. minpos = pos,
  484. maxpos = pos,
  485. minvel = {x = -radius, y = fall, z = -radius},
  486. maxvel = {x = radius, y = radius, z = radius},
  487. minacc = {x = 0, y = gravity, z = 0},
  488. maxacc = {x = 0, y = gravity, z = 0},
  489. minexptime = 0.1,
  490. maxexptime = 1,
  491. minsize = min_size,
  492. maxsize = max_size,
  493. texture = texture,
  494. glow = glow
  495. })
  496. end
  497. -- update nametag colour
  498. function mob_class:update_tag()
  499. local col = "#00FF00"
  500. local qua = self.hp_max / 4
  501. if self.health <= floor(qua * 3) then
  502. col = "#FFFF00"
  503. end
  504. if self.health <= floor(qua * 2) then
  505. col = "#FF6600"
  506. end
  507. if self.health <= floor(qua) then
  508. col = "#FF0000"
  509. end
  510. self.object:set_properties({
  511. nametag = self.nametag,
  512. nametag_color = col
  513. })
  514. end
  515. -- drop items
  516. function mob_class:item_drop()
  517. -- no drops if disabled by setting or mob is child
  518. if not mobs_drop_items or self.child then return end
  519. local pos = self.object:get_pos()
  520. -- check for drops function
  521. self.drops = type(self.drops) == "function"
  522. and self.drops(pos) or self.drops
  523. -- check for nil or no drops
  524. if not self.drops or #self.drops == 0 then
  525. return
  526. end
  527. -- was mob killed by player?
  528. local death_by_player = self.cause_of_death
  529. and self.cause_of_death.puncher
  530. and self.cause_of_death.puncher:is_player() or nil
  531. local obj, item, num
  532. for n = 1, #self.drops do
  533. if random(self.drops[n].chance) == 1 then
  534. num = random(self.drops[n].min or 0, self.drops[n].max or 1)
  535. item = self.drops[n].name
  536. -- cook items on a hot death
  537. if self.cause_of_death.hot then
  538. local output = minetest.get_craft_result({
  539. method = "cooking", width = 1, items = {item}})
  540. if output and output.item and not output.item:is_empty() then
  541. item = output.item:get_name()
  542. end
  543. end
  544. -- only drop rare items (drops.min = 0) if killed by player
  545. if death_by_player then
  546. obj = minetest.add_item(pos, ItemStack(item .. " " .. num))
  547. elseif self.drops[n].min ~= 0 then
  548. obj = minetest.add_item(pos, ItemStack(item .. " " .. num))
  549. end
  550. if obj and obj:get_luaentity() then
  551. obj:set_velocity({
  552. x = random(-10, 10) / 9,
  553. y = 6,
  554. z = random(-10, 10) / 9
  555. })
  556. elseif obj then
  557. obj:remove() -- item does not exist
  558. end
  559. end
  560. end
  561. self.drops = {}
  562. end
  563. -- remove mob and descrease counter
  564. local remove_mob = function(self, decrease)
  565. self.object:remove()
  566. if decrease and active_limit > 0 then
  567. active_mobs = active_mobs - 1
  568. if active_mobs < 0 then
  569. active_mobs = 0
  570. end
  571. end
  572. --print("-- active mobs: " .. active_mobs .. " / " .. active_limit)
  573. end
  574. -- check if mob is dead or only hurt
  575. function mob_class:check_for_death(cmi_cause)
  576. -- has health actually changed?
  577. if self.health == self.old_health and self.health > 0 then
  578. return false
  579. end
  580. local damaged = self.health < self.old_health
  581. self.old_health = self.health
  582. -- still got some health? play hurt sound
  583. if self.health > 0 then
  584. -- only play hurt sound if damaged
  585. if damaged then
  586. self:mob_sound(self.sounds.damage)
  587. end
  588. -- make sure health isn't higher than max
  589. if self.health > self.hp_max then
  590. self.health = self.hp_max
  591. end
  592. -- backup nametag so we can show health stats
  593. if not self.nametag2 then
  594. self.nametag2 = self.nametag or ""
  595. end
  596. if show_health
  597. and (cmi_cause and cmi_cause.type == "punch") then
  598. self.htimer = 2
  599. self.nametag = "♥ " .. self.health .. " / " .. self.hp_max
  600. self:update_tag()
  601. end
  602. return false
  603. end
  604. self.cause_of_death = cmi_cause
  605. -- drop items
  606. self:item_drop()
  607. self:mob_sound(self.sounds.death)
  608. local pos = self.object:get_pos()
  609. -- execute custom death function
  610. if self.on_die then
  611. self:on_die(pos)
  612. if use_cmi then
  613. cmi.notify_die(self.object, cmi_cause)
  614. end
  615. remove_mob(self, true)
  616. return true
  617. end
  618. -- check for custom death function and die animation
  619. if self.animation
  620. and self.animation.die_start
  621. and self.animation.die_end then
  622. local frames = self.animation.die_end - self.animation.die_start
  623. local speed = self.animation.die_speed or 15
  624. local length = max(frames / speed, 0)
  625. self.attack = nil
  626. self.v_start = false
  627. self.timer = 0
  628. self.blinktimer = 0
  629. self.passive = true
  630. self.state = "die"
  631. self:set_velocity(0)
  632. self:set_animation("die")
  633. minetest.after(length, function(self)
  634. if use_cmi and self.object:get_luaentity() then
  635. cmi.notify_die(self.object, cmi_cause)
  636. end
  637. remove_mob(self, true)
  638. end, self)
  639. else
  640. if use_cmi then
  641. cmi.notify_die(self.object, cmi_cause)
  642. end
  643. remove_mob(self, true)
  644. end
  645. effect(pos, 20, "tnt_smoke.png")
  646. return true
  647. end
  648. -- get node but use fallback for nil or unknown
  649. local node_ok = function(pos, fallback)
  650. fallback = fallback or mobs.fallback_node
  651. local node = minetest.get_node_or_nil(pos)
  652. if node and minetest.registered_nodes[node.name] then
  653. return node
  654. end
  655. return minetest.registered_nodes[fallback]
  656. end
  657. -- Returns true is node can deal damage to self
  658. local is_node_dangerous = function(self, nodename)
  659. if self.water_damage > 0
  660. and minetest.get_item_group(nodename, "water") ~= 0 then
  661. return true
  662. end
  663. if self.lava_damage > 0
  664. and minetest.get_item_group(nodename, "igniter") ~= 0 then
  665. return true
  666. end
  667. if minetest.registered_nodes[nodename].damage_per_second > 0 then
  668. return true
  669. end
  670. return false
  671. end
  672. -- is mob facing a cliff
  673. function mob_class:is_at_cliff()
  674. if self.fear_height == 0 then -- 0 for no falling protection!
  675. return false
  676. end
  677. -- if object no longer exists then return
  678. if not self.object:get_luaentity() then
  679. return false
  680. end
  681. local yaw = self.object:get_yaw()
  682. local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
  683. local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
  684. local pos = self.object:get_pos()
  685. local ypos = pos.y + self.collisionbox[2] -- just above floor
  686. local free_fall, blocker = minetest.line_of_sight(
  687. {x = pos.x + dir_x, y = ypos, z = pos.z + dir_z},
  688. {x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z})
  689. -- check for straight drop
  690. if free_fall then
  691. return true
  692. end
  693. local bnode = node_ok(blocker)
  694. -- will we drop onto dangerous node?
  695. if is_node_dangerous(self, bnode.name) then
  696. return true
  697. end
  698. local def = minetest.registered_nodes[bnode.name]
  699. return (not def and def.walkable)
  700. end
  701. -- environmental damage (water, lava, fire, light etc.)
  702. function mob_class:do_env_damage()
  703. -- feed/tame text timer (so mob 'full' messages dont spam chat)
  704. if self.htimer > 0 then
  705. self.htimer = self.htimer - 1
  706. end
  707. -- reset nametag after showing health stats
  708. if self.htimer < 1 and self.nametag2 then
  709. self.nametag = self.nametag2
  710. self.nametag2 = nil
  711. self:update_tag()
  712. end
  713. local pos = self.object:get_pos()
  714. self.time_of_day = minetest.get_timeofday()
  715. -- halt mob if standing inside ignore node
  716. if self.standing_in == "ignore" then
  717. self.object:set_velocity({x = 0, y = 0, z = 0})
  718. return true
  719. end
  720. -- particle appears at random mob height
  721. pos.y = pos.y + random(self.collisionbox[2], self.collisionbox[5])
  722. -- is mob light sensative, or scared of the dark :P
  723. if self.light_damage ~= 0 then
  724. local light = minetest.get_node_light(pos) or 0
  725. if light >= self.light_damage_min
  726. and light <= self.light_damage_max then
  727. self.health = self.health - self.light_damage
  728. effect(pos, 5, "tnt_smoke.png")
  729. if self:check_for_death({type = "light"}) then
  730. return true
  731. end
  732. end
  733. end
  734. local nodef = minetest.registered_nodes[self.standing_in]
  735. -- water
  736. if self.water_damage and nodef.groups.water then
  737. if self.water_damage ~= 0 then
  738. self.health = self.health - self.water_damage
  739. effect(pos, 5, "bubble.png", nil, nil, 1, nil)
  740. if self:check_for_death({type = "environment",
  741. pos = pos, node = self.standing_in}) then
  742. return true
  743. end
  744. end
  745. -- ignition source (fire or lava)
  746. elseif self.lava_damage and nodef.groups.igniter then
  747. if self.lava_damage ~= 0 then
  748. self.health = self.health - self.lava_damage
  749. effect(pos, 15, "fire_basic_flame.png", 1, 5, 1, 0.2, 15, true)
  750. if self:check_for_death({type = "environment", pos = pos,
  751. node = self.standing_in, hot = true}) then
  752. return true
  753. end
  754. end
  755. -- damage_per_second node check
  756. elseif nodef.damage_per_second ~= 0 then
  757. self.health = self.health - nodef.damage_per_second
  758. effect(pos, 5, "tnt_smoke.png")
  759. if self:check_for_death({type = "environment",
  760. pos = pos, node = self.standing_in}) then
  761. return true
  762. end
  763. end
  764. --- suffocation inside solid node
  765. if (self.suffocation and self.suffocation ~= 0)
  766. and (nodef.walkable == nil or nodef.walkable == true)
  767. and (nodef.collision_box == nil or nodef.collision_box.type == "regular")
  768. and (nodef.node_box == nil or nodef.node_box.type == "regular")
  769. and (nodef.groups.disable_suffocation ~= 1) then
  770. local damage
  771. if self.suffocation == true then
  772. damage = 2
  773. else
  774. damage = (self.suffocation or 2)
  775. end
  776. self.health = self.health - damage
  777. if self:check_for_death({type = "suffocation",
  778. pos = pos, node = self.standing_in}) then
  779. return true
  780. end
  781. end
  782. return self:check_for_death({type = "unknown"})
  783. end
  784. -- jump if facing a solid node (not fences or gates)
  785. function mob_class:do_jump()
  786. if not self.jump
  787. or self.jump_height == 0
  788. or self.fly
  789. or self.child
  790. or self.order == "stand" then
  791. return false
  792. end
  793. self.facing_fence = false
  794. -- something stopping us while moving?
  795. if self.state ~= "stand"
  796. and self:get_velocity() > 0.5
  797. and self.object:get_velocity().y ~= 0 then
  798. return false
  799. end
  800. local pos = self.object:get_pos()
  801. local yaw = self.object:get_yaw()
  802. -- sanity check
  803. if not yaw then return false end
  804. -- what is mob standing on?
  805. pos.y = pos.y + self.collisionbox[2] - 0.2
  806. local nod = node_ok(pos)
  807. --print("standing on:", nod.name, pos.y)
  808. if minetest.registered_nodes[nod.name].walkable == false then
  809. return false
  810. end
  811. -- where is front
  812. local dir_x = -sin(yaw) * (self.collisionbox[4] + 0.5)
  813. local dir_z = cos(yaw) * (self.collisionbox[4] + 0.5)
  814. -- what is in front of mob?
  815. local nod = node_ok({
  816. x = pos.x + dir_x,
  817. y = pos.y + 0.5,
  818. z = pos.z + dir_z
  819. })
  820. -- what is above and in front?
  821. local nodt = node_ok({
  822. x = pos.x + dir_x,
  823. y = pos.y + 1.5,
  824. z = pos.z + dir_z
  825. })
  826. local blocked = minetest.registered_nodes[nodt.name].walkable
  827. --print("in front:", nod.name, pos.y + 0.5)
  828. --print("in front above:", nodt.name, pos.y + 1.5)
  829. -- jump if standing on solid node (not snow) and not blocked above
  830. if (self.walk_chance == 0
  831. or minetest.registered_items[nod.name].walkable)
  832. and not blocked
  833. and nod.name ~= node_snow then
  834. if not nod.name:find("fence")
  835. and not nod.name:find("gate")
  836. and not nod.name:find("wall") then
  837. local v = self.object:get_velocity()
  838. v.y = self.jump_height
  839. self:set_animation("jump") -- only when defined
  840. self.object:set_velocity(v)
  841. -- when in air move forward
  842. minetest.after(0.3, function(self, v)
  843. if self.object:get_luaentity() then
  844. self.object:set_acceleration({
  845. x = v.x * 2,
  846. y = 0,
  847. z = v.z * 2
  848. })
  849. end
  850. end, self, v)
  851. if self:get_velocity() > 0 then
  852. self:mob_sound(self.sounds.jump)
  853. end
  854. return true
  855. else
  856. self.facing_fence = true
  857. end
  858. end
  859. -- if blocked against a block/wall for 5 counts then turn
  860. if not self.following
  861. and (self.facing_fence or blocked) then
  862. self.jump_count = (self.jump_count or 0) + 1
  863. if self.jump_count > 4 then
  864. local yaw = self.object:get_yaw() or 0
  865. local turn = random(0, 2) + 1.35
  866. yaw = self:set_yaw(yaw + turn, 12)
  867. self.jump_count = 0
  868. end
  869. end
  870. return false
  871. end
  872. -- blast damage to entities nearby (modified from TNT mod)
  873. local entity_physics = function(pos, radius)
  874. radius = radius * 2
  875. local objs = minetest.get_objects_inside_radius(pos, radius)
  876. local obj_pos, dist
  877. for n = 1, #objs do
  878. obj_pos = objs[n]:get_pos()
  879. dist = get_distance(pos, obj_pos)
  880. if dist < 1 then dist = 1 end
  881. local damage = floor((4 / dist) * radius)
  882. local ent = objs[n]:get_luaentity()
  883. -- punches work on entities AND players
  884. objs[n]:punch(objs[n], 1.0, {
  885. -- objs[n]:punch(nil, 1.0, {
  886. full_punch_interval = 1.0,
  887. damage_groups = {fleshy = damage},
  888. }, pos)
  889. end
  890. end
  891. -- should mob follow what I'm holding ?
  892. function mob_class:follow_holding(clicker)
  893. if mobs.invis[clicker:get_player_name()] then
  894. return false
  895. end
  896. local item = clicker:get_wielded_item()
  897. local t = type(self.follow)
  898. -- single item
  899. if t == "string"
  900. and item:get_name() == self.follow then
  901. return true
  902. -- multiple items
  903. elseif t == "table" then
  904. for no = 1, #self.follow do
  905. if self.follow[no] == item:get_name() then
  906. return true
  907. end
  908. end
  909. end
  910. return false
  911. end
  912. -- find two animals of same type and breed if nearby and horny
  913. function mob_class:breed()
  914. -- child takes 240 seconds before growing into adult
  915. if self.child == true then
  916. self.hornytimer = self.hornytimer + 1
  917. if self.hornytimer > 240 then
  918. self.child = false
  919. self.hornytimer = 0
  920. self.object:set_properties({
  921. textures = self.base_texture,
  922. mesh = self.base_mesh,
  923. visual_size = self.base_size,
  924. collisionbox = self.base_colbox,
  925. selectionbox = self.base_selbox
  926. })
  927. -- custom function when child grows up
  928. if self.on_grown then
  929. self.on_grown(self)
  930. else
  931. -- jump when fully grown so as not to fall into ground
  932. self.object:set_velocity({
  933. x = 0,
  934. y = self.jump_height,
  935. z = 0
  936. })
  937. end
  938. end
  939. return
  940. end
  941. -- horny animal can mate for 40 seconds,
  942. -- afterwards horny animal cannot mate again for 200 seconds
  943. if self.horny == true
  944. and self.hornytimer < 240 then
  945. self.hornytimer = self.hornytimer + 1
  946. if self.hornytimer >= 240 then
  947. self.hornytimer = 0
  948. self.horny = false
  949. end
  950. end
  951. -- find another same animal who is also horny and mate if nearby
  952. if self.horny == true
  953. and self.hornytimer <= 40 then
  954. local pos = self.object:get_pos()
  955. effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8,
  956. "heart.png", 3, 4, 1, 0.1)
  957. local objs = minetest.get_objects_inside_radius(pos, 3)
  958. local num = 0
  959. local ent
  960. for n = 1, #objs do
  961. ent = objs[n]:get_luaentity()
  962. -- check for same animal with different colour
  963. local canmate = false
  964. if ent then
  965. if ent.name == self.name then
  966. canmate = true
  967. else
  968. local entname = ent.name:split(":")
  969. local selfname = self.name:split(":")
  970. if entname[1] == selfname[1] then
  971. entname = entname[2]:split("_")
  972. selfname = selfname[2]:split("_")
  973. if entname[1] == selfname[1] then
  974. canmate = true
  975. end
  976. end
  977. end
  978. end
  979. if ent
  980. and canmate == true
  981. and ent.horny == true
  982. and ent.hornytimer <= 40 then
  983. num = num + 1
  984. end
  985. -- found your mate? then have a baby
  986. if num > 1 then
  987. self.hornytimer = 41
  988. ent.hornytimer = 41
  989. -- have we reached active mob limit
  990. if active_limit > 0 and active_mobs >= active_limit then
  991. minetest.chat_send_player(self.owner,
  992. S("Active Mob Limit Reached!")
  993. .. " (" .. active_mobs
  994. .. " / " .. active_limit .. ")")
  995. return
  996. end
  997. -- spawn baby
  998. minetest.after(5, function(self, ent)
  999. if not self.object:get_luaentity() then
  1000. return
  1001. end
  1002. -- custom breed function
  1003. if self.on_breed then
  1004. -- when false skip going any further
  1005. if self:on_breed(ent) == false then
  1006. return
  1007. end
  1008. else
  1009. effect(pos, 15, "tnt_smoke.png", 1, 2, 2, 15, 5)
  1010. end
  1011. local mob = minetest.add_entity(pos, self.name)
  1012. local ent2 = mob:get_luaentity()
  1013. local textures = self.base_texture
  1014. -- using specific child texture (if found)
  1015. if self.child_texture then
  1016. textures = self.child_texture[1]
  1017. end
  1018. -- and resize to half height
  1019. mob:set_properties({
  1020. textures = textures,
  1021. visual_size = {
  1022. x = self.base_size.x * .5,
  1023. y = self.base_size.y * .5
  1024. },
  1025. collisionbox = {
  1026. self.base_colbox[1] * .5,
  1027. self.base_colbox[2] * .5,
  1028. self.base_colbox[3] * .5,
  1029. self.base_colbox[4] * .5,
  1030. self.base_colbox[5] * .5,
  1031. self.base_colbox[6] * .5
  1032. },
  1033. selectionbox = {
  1034. self.base_selbox[1] * .5,
  1035. self.base_selbox[2] * .5,
  1036. self.base_selbox[3] * .5,
  1037. self.base_selbox[4] * .5,
  1038. self.base_selbox[5] * .5,
  1039. self.base_selbox[6] * .5
  1040. },
  1041. })
  1042. -- tamed and owned by parents' owner
  1043. ent2.child = true
  1044. ent2.tamed = true
  1045. ent2.owner = self.owner
  1046. end, self, ent)
  1047. num = 0
  1048. break
  1049. end
  1050. end
  1051. end
  1052. end
  1053. -- find and replace what mob is looking for (grass, wheat etc.)
  1054. function mob_class:replace(pos)
  1055. local vel = self.object:get_velocity()
  1056. if not vel then return end
  1057. if not mobs_griefing
  1058. or not self.replace_rate
  1059. or not self.replace_what
  1060. or self.child == true
  1061. or vel.y ~= 0
  1062. or random(self.replace_rate) > 1 then
  1063. return
  1064. end
  1065. local what, with, y_offset
  1066. if type(self.replace_what[1]) == "table" then
  1067. local num = random(#self.replace_what)
  1068. what = self.replace_what[num][1] or ""
  1069. with = self.replace_what[num][2] or ""
  1070. y_offset = self.replace_what[num][3] or 0
  1071. else
  1072. what = self.replace_what
  1073. with = self.replace_with or ""
  1074. y_offset = self.replace_offset or 0
  1075. end
  1076. pos.y = pos.y + y_offset
  1077. if #minetest.find_nodes_in_area(pos, pos, what) > 0 then
  1078. -- print("replace node = ".. minetest.get_node(pos).name, pos.y)
  1079. if self.on_replace then
  1080. local oldnode = what or ""
  1081. local newnode = with
  1082. -- pass actual node name when using table or groups
  1083. if type(oldnode) == "table"
  1084. or oldnode:find("group:") then
  1085. oldnode = minetest.get_node(pos).name
  1086. end
  1087. if self:on_replace(pos, oldnode, newnode) == false then
  1088. return
  1089. end
  1090. end
  1091. minetest.set_node(pos, {name = with})
  1092. end
  1093. end
  1094. -- check if daytime and also if mob is docile during daylight hours
  1095. function mob_class:day_docile()
  1096. if self.docile_by_day == false then
  1097. return false
  1098. elseif self.docile_by_day == true
  1099. and self.time_of_day > 0.2
  1100. and self.time_of_day < 0.8 then
  1101. return true
  1102. end
  1103. end
  1104. local los_switcher = false
  1105. local height_switcher = false
  1106. -- path finding and smart mob routine by rnd,
  1107. -- line_of_sight and other edits by Elkien3
  1108. function mob_class:smart_mobs(s, p, dist, dtime)
  1109. local s1 = self.path.lastpos
  1110. local target_pos = self.attack:get_pos()
  1111. -- is it becoming stuck?
  1112. if abs(s1.x - s.x) + abs(s1.z - s.z) < .5 then
  1113. self.path.stuck_timer = self.path.stuck_timer + dtime
  1114. else
  1115. self.path.stuck_timer = 0
  1116. end
  1117. self.path.lastpos = {x = s.x, y = s.y, z = s.z}
  1118. local use_pathfind = false
  1119. local has_lineofsight = minetest.line_of_sight(
  1120. {x = s.x, y = (s.y) + .5, z = s.z},
  1121. {x = target_pos.x, y = (target_pos.y) + 1.5, z = target_pos.z}, .2)
  1122. -- im stuck, search for path
  1123. if not has_lineofsight then
  1124. if los_switcher == true then
  1125. use_pathfind = true
  1126. los_switcher = false
  1127. end -- cannot see target!
  1128. else
  1129. if los_switcher == false then
  1130. los_switcher = true
  1131. use_pathfind = false
  1132. minetest.after(1, function(self)
  1133. if self.object:get_luaentity() then
  1134. if has_lineofsight then
  1135. self.path.following = false
  1136. end
  1137. end
  1138. end, self)
  1139. end -- can see target!
  1140. end
  1141. if (self.path.stuck_timer > stuck_timeout and not self.path.following) then
  1142. use_pathfind = true
  1143. self.path.stuck_timer = 0
  1144. minetest.after(1, function(self)
  1145. if self.object:get_luaentity() then
  1146. if has_lineofsight then
  1147. self.path.following = false
  1148. end
  1149. end
  1150. end, self)
  1151. end
  1152. if (self.path.stuck_timer > stuck_path_timeout and self.path.following) then
  1153. use_pathfind = true
  1154. self.path.stuck_timer = 0
  1155. minetest.after(1, function(self)
  1156. if not self.object:get_luaentity() then
  1157. return
  1158. end
  1159. if self.object:get_luaentity() then
  1160. if has_lineofsight then
  1161. self.path.following = false
  1162. end
  1163. end
  1164. end, self)
  1165. end
  1166. if abs(vector.subtract(s,target_pos).y) > self.stepheight then
  1167. if height_switcher then
  1168. use_pathfind = true
  1169. height_switcher = false
  1170. end
  1171. else
  1172. if not height_switcher then
  1173. use_pathfind = false
  1174. height_switcher = true
  1175. end
  1176. end
  1177. -- lets try find a path, first take care of positions
  1178. -- since pathfinder is very sensitive
  1179. if use_pathfind then
  1180. -- round position to center of node to avoid stuck in walls
  1181. -- also adjust height for player models!
  1182. s.x = floor(s.x + 0.5)
  1183. s.z = floor(s.z + 0.5)
  1184. local ssight, sground = minetest.line_of_sight(s, {
  1185. x = s.x, y = s.y - 4, z = s.z}, 1)
  1186. -- determine node above ground
  1187. if not ssight then
  1188. s.y = sground.y + 1
  1189. end
  1190. local p1 = self.attack:get_pos()
  1191. p1.x = floor(p1.x + 0.5)
  1192. p1.y = floor(p1.y + 0.5)
  1193. p1.z = floor(p1.z + 0.5)
  1194. local dropheight = 6
  1195. if self.fear_height ~= 0 then dropheight = self.fear_height end
  1196. local jumpheight = 0
  1197. if self.jump and self.jump_height >= 4 then
  1198. jumpheight = min(ceil(self.jump_height / 4), 4)
  1199. elseif self.stepheight > 0.5 then
  1200. jumpheight = 1
  1201. end
  1202. self.path.way = minetest.find_path(s, p1, 16, jumpheight,
  1203. dropheight, "Dijkstra")
  1204. --[[
  1205. -- show path using particles
  1206. if self.path.way and #self.path.way > 0 then
  1207. print("-- path length:" .. tonumber(#self.path.way))
  1208. for _,pos in pairs(self.path.way) do
  1209. minetest.add_particle({
  1210. pos = pos,
  1211. velocity = {x=0, y=0, z=0},
  1212. acceleration = {x=0, y=0, z=0},
  1213. expirationtime = 1,
  1214. size = 4,
  1215. collisiondetection = false,
  1216. vertical = false,
  1217. texture = "heart.png",
  1218. })
  1219. end
  1220. end
  1221. ]]
  1222. self.state = ""
  1223. self:do_attack(self.attack)
  1224. -- no path found, try something else
  1225. if not self.path.way then
  1226. self.path.following = false
  1227. -- lets make way by digging/building if not accessible
  1228. if self.pathfinding == 2 and mobs_griefing then
  1229. -- is player higher than mob?
  1230. if s.y < p1.y then
  1231. -- build upwards
  1232. if not minetest.is_protected(s, "") then
  1233. local ndef1 = minetest.registered_nodes[self.standing_in]
  1234. if ndef1 and (ndef1.buildable_to or ndef1.groups.liquid) then
  1235. minetest.set_node(s, {name = mobs.fallback_node})
  1236. end
  1237. end
  1238. local sheight = ceil(self.collisionbox[5]) + 1
  1239. -- assume mob is 2 blocks high so it digs above its head
  1240. s.y = s.y + sheight
  1241. -- remove one block above to make room to jump
  1242. if not minetest.is_protected(s, "") then
  1243. local node1 = node_ok(s, "air").name
  1244. local ndef1 = minetest.registered_nodes[node1]
  1245. if node1 ~= "air"
  1246. and node1 ~= "ignore"
  1247. and ndef1
  1248. and not ndef1.groups.level
  1249. and not ndef1.groups.unbreakable
  1250. and not ndef1.groups.liquid then
  1251. minetest.set_node(s, {name = "air"})
  1252. minetest.add_item(s, ItemStack(node1))
  1253. end
  1254. end
  1255. s.y = s.y - sheight
  1256. self.object:set_pos({x = s.x, y = s.y + 2, z = s.z})
  1257. else -- dig 2 blocks to make door toward player direction
  1258. local yaw1 = self.object:get_yaw() + pi / 2
  1259. local p1 = {
  1260. x = s.x + cos(yaw1),
  1261. y = s.y,
  1262. z = s.z + sin(yaw1)
  1263. }
  1264. if not minetest.is_protected(p1, "") then
  1265. local node1 = node_ok(p1, "air").name
  1266. local ndef1 = minetest.registered_nodes[node1]
  1267. if node1 ~= "air"
  1268. and node1 ~= "ignore"
  1269. and ndef1
  1270. and not ndef1.groups.level
  1271. and not ndef1.groups.unbreakable
  1272. and not ndef1.groups.liquid then
  1273. minetest.add_item(p1, ItemStack(node1))
  1274. minetest.set_node(p1, {name = "air"})
  1275. end
  1276. p1.y = p1.y + 1
  1277. node1 = node_ok(p1, "air").name
  1278. ndef1 = minetest.registered_nodes[node1]
  1279. if node1 ~= "air"
  1280. and node1 ~= "ignore"
  1281. and ndef1
  1282. and not ndef1.groups.level
  1283. and not ndef1.groups.unbreakable
  1284. and not ndef1.groups.liquid then
  1285. minetest.add_item(p1, ItemStack(node1))
  1286. minetest.set_node(p1, {name = "air"})
  1287. end
  1288. end
  1289. end
  1290. end
  1291. -- will try again in 2 second
  1292. self.path.stuck_timer = stuck_timeout - 2
  1293. elseif s.y < p1.y and (not self.fly) then
  1294. self:do_jump() --add jump to pathfinding
  1295. self.path.following = true
  1296. else
  1297. -- yay i found path
  1298. self:mob_sound(self.sounds.war_cry)
  1299. self:set_velocity(self.walk_velocity)
  1300. -- follow path now that it has it
  1301. self.path.following = true
  1302. end
  1303. end
  1304. end
  1305. -- specific attacks
  1306. local specific_attack = function(list, what)
  1307. -- no list so attack default (player, animals etc.)
  1308. if list == nil then
  1309. return true
  1310. end
  1311. -- found entity on list to attack?
  1312. for no = 1, #list do
  1313. if list[no] == what then
  1314. return true
  1315. end
  1316. end
  1317. return false
  1318. end
  1319. -- general attack function for all mobs
  1320. function mob_class:general_attack()
  1321. -- return if already attacking, passive or docile during day
  1322. if self.passive
  1323. or self.state == "runaway"
  1324. or self.state == "attack"
  1325. or self:day_docile() then
  1326. return
  1327. end
  1328. local s = self.object:get_pos()
  1329. local objs = minetest.get_objects_inside_radius(s, self.view_range)
  1330. -- remove entities we aren't interested in
  1331. for n = 1, #objs do
  1332. local ent = objs[n]:get_luaentity()
  1333. -- are we a player?
  1334. if objs[n]:is_player() then
  1335. -- if player invisible or mob cannot attack then remove from list
  1336. if not damage_enabled
  1337. or self.attack_players == false
  1338. or (self.owner and self.type ~= "monster")
  1339. or mobs.invis[objs[n]:get_player_name()]
  1340. or not specific_attack(self.specific_attack, "player") then
  1341. objs[n] = nil
  1342. --print("- pla", n)
  1343. end
  1344. -- or are we a mob?
  1345. elseif ent and ent._cmi_is_mob then
  1346. -- remove mobs not to attack
  1347. if self.name == ent.name
  1348. or (not self.attack_animals and ent.type == "animal")
  1349. or (not self.attack_monsters and ent.type == "monster")
  1350. or (not self.attack_npcs and ent.type == "npc")
  1351. or not specific_attack(self.specific_attack, ent.name) then
  1352. objs[n] = nil
  1353. --print("- mob", n, self.name, ent.name)
  1354. end
  1355. -- remove all other entities
  1356. else
  1357. --print(" -obj", n)
  1358. objs[n] = nil
  1359. end
  1360. end
  1361. local p, sp, dist, min_player
  1362. local min_dist = self.view_range + 1
  1363. -- go through remaining entities and select closest
  1364. for _,player in pairs(objs) do
  1365. p = player:get_pos()
  1366. sp = s
  1367. dist = get_distance(p, s)
  1368. -- aim higher to make looking up hills more realistic
  1369. p.y = p.y + 1
  1370. sp.y = sp.y + 1
  1371. -- choose closest player to attack that isnt self
  1372. if dist ~= 0
  1373. and dist < min_dist
  1374. and self:line_of_sight(sp, p, 2) == true then
  1375. min_dist = dist
  1376. min_player = player
  1377. end
  1378. end
  1379. -- attack closest player or mob
  1380. if min_player and random(100) > self.attack_chance then
  1381. self:do_attack(min_player)
  1382. end
  1383. end
  1384. -- specific runaway
  1385. local specific_runaway = function(list, what)
  1386. -- no list so do not run
  1387. if list == nil then
  1388. return false
  1389. end
  1390. -- found entity on list to attack?
  1391. for no = 1, #list do
  1392. if list[no] == what then
  1393. return true
  1394. end
  1395. end
  1396. return false
  1397. end
  1398. -- find someone to runaway from
  1399. function mob_class:do_runaway_from()
  1400. if not self.runaway_from then
  1401. return
  1402. end
  1403. local s = self.object:get_pos()
  1404. local p, sp, dist, pname
  1405. local player, obj, min_player, name
  1406. local min_dist = self.view_range + 1
  1407. local objs = minetest.get_objects_inside_radius(s, self.view_range)
  1408. for n = 1, #objs do
  1409. if objs[n]:is_player() then
  1410. pname = objs[n]:get_player_name()
  1411. if mobs.invis[pname]
  1412. or self.owner == pname then
  1413. name = ""
  1414. else
  1415. player = objs[n]
  1416. name = "player"
  1417. end
  1418. else
  1419. obj = objs[n]:get_luaentity()
  1420. if obj then
  1421. player = obj.object
  1422. name = obj.name or ""
  1423. end
  1424. end
  1425. -- find specific mob to runaway from
  1426. if name ~= "" and name ~= self.name
  1427. and specific_runaway(self.runaway_from, name) then
  1428. p = player:get_pos()
  1429. sp = s
  1430. -- aim higher to make looking up hills more realistic
  1431. p.y = p.y + 1
  1432. sp.y = sp.y + 1
  1433. dist = get_distance(p, s)
  1434. -- choose closest player/mob to runaway from
  1435. if dist < min_dist
  1436. and self:line_of_sight(sp, p, 2) == true then
  1437. min_dist = dist
  1438. min_player = player
  1439. end
  1440. end
  1441. end
  1442. if min_player then
  1443. yaw_to_pos(self, min_player:get_pos(), 3)
  1444. self.state = "runaway"
  1445. self.runaway_timer = 3
  1446. self.following = nil
  1447. end
  1448. end
  1449. -- follow player if owner or holding item, if fish outta water then flop
  1450. function mob_class:follow_flop()
  1451. -- find player to follow
  1452. if (self.follow ~= "" or self.order == "follow")
  1453. and not self.following
  1454. and self.state ~= "attack"
  1455. and self.state ~= "runaway" then
  1456. local s = self.object:get_pos()
  1457. local players = minetest.get_connected_players()
  1458. for n = 1, #players do
  1459. if get_distance(players[n]:get_pos(), s) < self.view_range
  1460. and not mobs.invis[ players[n]:get_player_name() ] then
  1461. self.following = players[n]
  1462. break
  1463. end
  1464. end
  1465. end
  1466. if self.type == "npc"
  1467. and self.order == "follow"
  1468. and self.state ~= "attack"
  1469. and self.owner ~= "" then
  1470. -- npc stop following player if not owner
  1471. if self.following
  1472. and self.owner
  1473. and self.owner ~= self.following:get_player_name() then
  1474. self.following = nil
  1475. end
  1476. else
  1477. -- stop following player if not holding specific item
  1478. if self.following
  1479. and self.following:is_player()
  1480. and self:follow_holding(self.following) == false then
  1481. self.following = nil
  1482. end
  1483. end
  1484. -- follow that thing
  1485. if self.following then
  1486. local s = self.object:get_pos()
  1487. local p
  1488. if self.following:is_player() then
  1489. p = self.following:get_pos()
  1490. elseif self.following.object then
  1491. p = self.following.object:get_pos()
  1492. end
  1493. if p then
  1494. local dist = get_distance(p, s)
  1495. -- dont follow if out of range
  1496. if dist > self.view_range then
  1497. self.following = nil
  1498. else
  1499. yaw_to_pos(self, p)
  1500. -- anyone but standing npc's can move along
  1501. if dist > self.reach
  1502. and self.order ~= "stand" then
  1503. self:set_velocity(self.walk_velocity)
  1504. if self.walk_chance ~= 0 then
  1505. self:set_animation("walk")
  1506. end
  1507. else
  1508. self:set_velocity(0)
  1509. self:set_animation("stand")
  1510. end
  1511. return
  1512. end
  1513. end
  1514. end
  1515. -- swimmers flop when out of their element, and swim again when back in
  1516. if self.fly then
  1517. if not self:attempt_flight_correction() then
  1518. self.state = "flop"
  1519. self.object:set_velocity({x = 0, y = -5, z = 0})
  1520. self:set_animation("stand")
  1521. return
  1522. elseif self.state == "flop" then
  1523. self.state = "stand"
  1524. end
  1525. end
  1526. end
  1527. -- dogshoot attack switch and counter function
  1528. function mob_class:dogswitch(dtime)
  1529. -- switch mode not activated
  1530. if not self.dogshoot_switch
  1531. or not dtime then
  1532. return 0
  1533. end
  1534. self.dogshoot_count = self.dogshoot_count + dtime
  1535. if (self.dogshoot_switch == 1
  1536. and self.dogshoot_count > self.dogshoot_count_max)
  1537. or (self.dogshoot_switch == 2
  1538. and self.dogshoot_count > self.dogshoot_count2_max) then
  1539. self.dogshoot_count = 0
  1540. if self.dogshoot_switch == 1 then
  1541. self.dogshoot_switch = 2
  1542. else
  1543. self.dogshoot_switch = 1
  1544. end
  1545. end
  1546. return self.dogshoot_switch
  1547. end
  1548. -- execute current state (stand, walk, run, attacks)
  1549. function mob_class:do_states(dtime)
  1550. local yaw = self.object:get_yaw() or 0
  1551. if self.state == "stand" then
  1552. if random(4) == 1 then
  1553. local lp
  1554. local s = self.object:get_pos()
  1555. local objs = minetest.get_objects_inside_radius(s, 3)
  1556. for n = 1, #objs do
  1557. if objs[n]:is_player() then
  1558. lp = objs[n]:get_pos()
  1559. break
  1560. end
  1561. end
  1562. -- look at any players nearby, otherwise turn randomly
  1563. if lp then
  1564. yaw = yaw_to_pos(self, lp)
  1565. else
  1566. yaw = yaw + random(-0.5, 0.5)
  1567. end
  1568. yaw = self:set_yaw(yaw, 8)
  1569. end
  1570. self:set_velocity(0)
  1571. self:set_animation("stand")
  1572. -- mobs ordered to stand stay standing
  1573. if self.order ~= "stand"
  1574. and self.walk_chance ~= 0
  1575. and self.facing_fence ~= true
  1576. and random(100) <= self.walk_chance
  1577. and self.at_cliff == false then
  1578. self:set_velocity(self.walk_velocity)
  1579. self.state = "walk"
  1580. self:set_animation("walk")
  1581. end
  1582. elseif self.state == "walk" then
  1583. local s = self.object:get_pos()
  1584. local lp
  1585. -- is there something I need to avoid?
  1586. if self.water_damage > 0
  1587. and self.lava_damage > 0 then
  1588. lp = minetest.find_node_near(s, 1, {"group:water", "group:igniter"})
  1589. elseif self.water_damage > 0 then
  1590. lp = minetest.find_node_near(s, 1, {"group:water"})
  1591. elseif self.lava_damage > 0 then
  1592. lp = minetest.find_node_near(s, 1, {"group:igniter"})
  1593. end
  1594. if lp then
  1595. -- if mob in dangerous node then look for land
  1596. if not is_node_dangerous(self, self.standing_in) then
  1597. lp = minetest.find_nodes_in_area_under_air(
  1598. {s.x - 5, s.y - 1, s.z - 5},
  1599. {s.x + 5, s.y + 2, s.z + 5},
  1600. {"group:soil", "group:stone", "group:sand",
  1601. node_ice, node_snowblock})
  1602. -- select position of random block to climb onto
  1603. lp = #lp > 0 and lp[random(#lp)]
  1604. -- did we find land?
  1605. if lp then
  1606. yaw = yaw_to_pos(self, lp)
  1607. self:do_jump()
  1608. self:set_velocity(self.walk_velocity)
  1609. else
  1610. yaw = yaw + random(-0.5, 0.5)
  1611. end
  1612. end
  1613. yaw = self:set_yaw(yaw, 8)
  1614. -- otherwise randomly turn
  1615. elseif random(100) <= 30 then
  1616. yaw = yaw + random(-0.5, 0.5)
  1617. yaw = self:set_yaw(yaw, 8)
  1618. -- for flying/swimming mobs randomly move up and down also
  1619. if self.fly_in
  1620. and not self.following then
  1621. self:attempt_flight_correction(true)
  1622. end
  1623. end
  1624. -- stand for great fall in front
  1625. if self.facing_fence == true
  1626. or self.at_cliff
  1627. or random(100) <= self.stand_chance then
  1628. -- don't stand if mob flies and keep_flying set
  1629. if (self.fly and not self.keep_flying)
  1630. or not self.fly then
  1631. self:set_velocity(0)
  1632. self.state = "stand"
  1633. self:set_animation("stand", true)
  1634. end
  1635. else
  1636. self:set_velocity(self.walk_velocity)
  1637. if self:flight_check()
  1638. and self.animation
  1639. and self.animation.fly_start
  1640. and self.animation.fly_end then
  1641. self:set_animation("fly")
  1642. else
  1643. self:set_animation("walk")
  1644. end
  1645. end
  1646. -- runaway when punched
  1647. elseif self.state == "runaway" then
  1648. self.runaway_timer = self.runaway_timer + 1
  1649. -- stop after 5 seconds or when at cliff
  1650. if self.runaway_timer > 5
  1651. or self.at_cliff
  1652. or self.order == "stand" then
  1653. self.runaway_timer = 0
  1654. self:set_velocity(0)
  1655. self.state = "stand"
  1656. self:set_animation("stand")
  1657. else
  1658. self:set_velocity(self.run_velocity)
  1659. self:set_animation("walk")
  1660. end
  1661. -- attack routines (explode, dogfight, shoot, dogshoot)
  1662. elseif self.state == "attack" then
  1663. -- calculate distance from mob and enemy
  1664. local s = self.object:get_pos()
  1665. local p = self.attack:get_pos() or s
  1666. local dist = get_distance(p, s)
  1667. -- stop attacking if player invisible or out of range
  1668. if dist > self.view_range
  1669. or not self.attack
  1670. or not self.attack:get_pos()
  1671. or self.attack:get_hp() <= 0
  1672. or (self.attack:is_player()
  1673. and mobs.invis[ self.attack:get_player_name() ]) then
  1674. --print(" ** stop attacking **", dist, self.view_range)
  1675. self.state = "stand"
  1676. self:set_velocity(0)
  1677. self:set_animation("stand")
  1678. self.attack = nil
  1679. self.v_start = false
  1680. self.timer = 0
  1681. self.blinktimer = 0
  1682. self.path.way = nil
  1683. return
  1684. end
  1685. if self.attack_type == "explode" then
  1686. yaw = yaw_to_pos(self, p)
  1687. local node_break_radius = self.explosion_radius or 1
  1688. local entity_damage_radius = self.explosion_damage_radius
  1689. or (node_break_radius * 2)
  1690. -- look a little higher to fix raycast
  1691. s.y = s.y + 0.5 ; p.y = p.y + 0.5
  1692. -- start timer when in reach and line of sight
  1693. if not self.v_start
  1694. and dist <= self.reach
  1695. and self:line_of_sight(s, p, 2) then
  1696. self.v_start = true
  1697. self.timer = 0
  1698. self.blinktimer = 0
  1699. self:mob_sound(self.sounds.fuse)
  1700. --print("=== explosion timer started", self.explosion_timer)
  1701. -- stop timer if out of reach or direct line of sight
  1702. elseif self.allow_fuse_reset
  1703. and self.v_start
  1704. and (dist > self.reach or not self:line_of_sight(s, p, 2)) then
  1705. --print("=== explosion timer stopped")
  1706. self.v_start = false
  1707. self.timer = 0
  1708. self.blinktimer = 0
  1709. self.blinkstatus = false
  1710. self.object:set_texture_mod("")
  1711. end
  1712. -- walk right up to player unless the timer is active
  1713. if self.v_start and (self.stop_to_explode or dist < 1.5) then
  1714. self:set_velocity(0)
  1715. else
  1716. self:set_velocity(self.run_velocity)
  1717. end
  1718. if self.animation and self.animation.run_start then
  1719. self:set_animation("run")
  1720. else
  1721. self:set_animation("walk")
  1722. end
  1723. if self.v_start then
  1724. self.timer = self.timer + dtime
  1725. self.blinktimer = (self.blinktimer or 0) + dtime
  1726. if self.blinktimer > 0.2 then
  1727. self.blinktimer = 0
  1728. if self.blinkstatus then
  1729. -- self.object:set_texture_mod("")
  1730. self.object:set_texture_mod(self.texture_mods)
  1731. else
  1732. -- self.object:set_texture_mod("^[brighten")
  1733. self.object:set_texture_mod(self.texture_mods
  1734. .. "^[brighten")
  1735. end
  1736. self.blinkstatus = not self.blinkstatus
  1737. end
  1738. --print("=== explosion timer", self.timer)
  1739. if self.timer > self.explosion_timer then
  1740. local pos = self.object:get_pos()
  1741. -- dont damage anything if area protected or next to water
  1742. if minetest.find_node_near(pos, 1, {"group:water"})
  1743. or minetest.is_protected(pos, "") then
  1744. node_break_radius = 1
  1745. end
  1746. remove_mob(self, true)
  1747. if minetest.get_modpath("tnt") and tnt and tnt.boom
  1748. and not minetest.is_protected(pos, "") then
  1749. tnt.boom(pos, {
  1750. radius = node_break_radius,
  1751. damage_radius = entity_damage_radius,
  1752. sound = self.sounds.explode
  1753. })
  1754. else
  1755. minetest.sound_play(self.sounds.explode, {
  1756. pos = pos,
  1757. gain = 1.0,
  1758. max_hear_distance = self.sounds.distance or 32
  1759. })
  1760. entity_physics(pos, entity_damage_radius)
  1761. effect(pos, 32, "tnt_smoke.png", nil, nil,
  1762. node_break_radius, 1, 0)
  1763. end
  1764. return true
  1765. end
  1766. end
  1767. elseif self.attack_type == "dogfight"
  1768. or (self.attack_type == "dogshoot" and self:dogswitch(dtime) == 2)
  1769. or (self.attack_type == "dogshoot" and dist <= self.reach and self:dogswitch() == 0) then
  1770. if self.fly
  1771. and dist > self.reach then
  1772. local p1 = s
  1773. local me_y = floor(p1.y)
  1774. local p2 = p
  1775. local p_y = floor(p2.y + 1)
  1776. local v = self.object:get_velocity()
  1777. if self:flight_check() then
  1778. if me_y < p_y then
  1779. self.object:set_velocity({
  1780. x = v.x,
  1781. y = 1 * self.walk_velocity,
  1782. z = v.z
  1783. })
  1784. elseif me_y > p_y then
  1785. self.object:set_velocity({
  1786. x = v.x,
  1787. y = -1 * self.walk_velocity,
  1788. z = v.z
  1789. })
  1790. end
  1791. else
  1792. if me_y < p_y then
  1793. self.object:set_velocity({
  1794. x = v.x,
  1795. y = 0.01,
  1796. z = v.z
  1797. })
  1798. elseif me_y > p_y then
  1799. self.object:set_velocity({
  1800. x = v.x,
  1801. y = -0.01,
  1802. z = v.z
  1803. })
  1804. end
  1805. end
  1806. end
  1807. -- rnd: new movement direction
  1808. if self.path.following
  1809. and self.path.way
  1810. and self.attack_type ~= "dogshoot" then
  1811. -- no paths longer than 50
  1812. if #self.path.way > 50
  1813. or dist < self.reach then
  1814. self.path.following = false
  1815. return
  1816. end
  1817. local p1 = self.path.way[1]
  1818. if not p1 then
  1819. self.path.following = false
  1820. return
  1821. end
  1822. if abs(p1.x-s.x) + abs(p1.z - s.z) < 0.6 then
  1823. -- reached waypoint, remove it from queue
  1824. table.remove(self.path.way, 1)
  1825. end
  1826. -- set new temporary target
  1827. p = {x = p1.x, y = p1.y, z = p1.z}
  1828. end
  1829. yaw = yaw_to_pos(self, p)
  1830. -- move towards enemy if beyond mob reach
  1831. if dist > self.reach then
  1832. -- path finding by rnd
  1833. if self.pathfinding -- only if mob has pathfinding enabled
  1834. and enable_pathfinding then
  1835. self:smart_mobs(s, p, dist, dtime)
  1836. end
  1837. if self.at_cliff then
  1838. self:set_velocity(0)
  1839. self:set_animation("stand")
  1840. else
  1841. if self.path.stuck then
  1842. self:set_velocity(self.walk_velocity)
  1843. else
  1844. self:set_velocity(self.run_velocity)
  1845. end
  1846. if self.animation and self.animation.run_start then
  1847. self:set_animation("run")
  1848. else
  1849. self:set_animation("walk")
  1850. end
  1851. end
  1852. else -- rnd: if inside reach range
  1853. self.path.stuck = false
  1854. self.path.stuck_timer = 0
  1855. self.path.following = false -- not stuck anymore
  1856. self:set_velocity(0)
  1857. if not self.custom_attack then
  1858. if self.timer > 1 then
  1859. self.timer = 0
  1860. self:set_animation("punch")
  1861. local p2 = p
  1862. local s2 = s
  1863. p2.y = p2.y + .5
  1864. s2.y = s2.y + .5
  1865. if self:line_of_sight(p2, s2) == true then
  1866. -- play attack sound
  1867. self:mob_sound(self.sounds.attack)
  1868. -- punch player (or what player is attached to)
  1869. local attached = self.attack:get_attach()
  1870. if attached then
  1871. self.attack = attached
  1872. end
  1873. self.attack:punch(self.object, 1.0, {
  1874. full_punch_interval = 1.0,
  1875. damage_groups = {fleshy = self.damage}
  1876. }, nil)
  1877. end
  1878. end
  1879. else -- call custom attack every second
  1880. if self.custom_attack
  1881. and self.timer > 1 then
  1882. self.timer = 0
  1883. self:custom_attack(p)
  1884. end
  1885. end
  1886. end
  1887. elseif self.attack_type == "shoot"
  1888. or (self.attack_type == "dogshoot" and self:dogswitch(dtime) == 1)
  1889. or (self.attack_type == "dogshoot" and dist > self.reach and
  1890. self:dogswitch() == 0) then
  1891. p.y = p.y - .5
  1892. s.y = s.y + .5
  1893. local vec = {
  1894. x = p.x - s.x,
  1895. y = p.y - s.y,
  1896. z = p.z - s.z
  1897. }
  1898. yaw = yaw_to_pos(self, p)
  1899. self:set_velocity(0)
  1900. if self.shoot_interval
  1901. and self.timer > self.shoot_interval
  1902. and random(100) <= 60 then
  1903. self.timer = 0
  1904. self:set_animation("shoot")
  1905. -- play shoot attack sound
  1906. self:mob_sound(self.sounds.shoot_attack)
  1907. local p = self.object:get_pos()
  1908. p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
  1909. if minetest.registered_entities[self.arrow] then
  1910. local obj = minetest.add_entity(p, self.arrow)
  1911. local ent = obj:get_luaentity()
  1912. local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5
  1913. local v = ent.velocity or 1 -- or set to default
  1914. ent.switch = 1
  1915. ent.owner_id = tostring(self.object) -- add unique owner id to arrow
  1916. -- offset makes shoot aim accurate
  1917. vec.y = vec.y + self.shoot_offset
  1918. vec.x = vec.x * (v / amount)
  1919. vec.y = vec.y * (v / amount)
  1920. vec.z = vec.z * (v / amount)
  1921. obj:set_velocity(vec)
  1922. end
  1923. end
  1924. end
  1925. end
  1926. end
  1927. -- falling and fall damage
  1928. function mob_class:falling(pos)
  1929. if self.fly or self.disable_falling then
  1930. return
  1931. end
  1932. -- floating in water (or falling)
  1933. local v = self.object:get_velocity()
  1934. -- sanity check
  1935. if not v then return end
  1936. if v.y > 0 then
  1937. -- apply gravity when moving up
  1938. self.object:set_acceleration({
  1939. x = 0,
  1940. y = -10,
  1941. z = 0
  1942. })
  1943. elseif v.y <= 0 and v.y > self.fall_speed then
  1944. -- fall downwards at set speed
  1945. self.object:set_acceleration({
  1946. x = 0,
  1947. y = self.fall_speed,
  1948. z = 0
  1949. })
  1950. else
  1951. -- stop accelerating once max fall speed hit
  1952. self.object:set_acceleration({x = 0, y = 0, z = 0})
  1953. end
  1954. -- in water then float up
  1955. if self.standing_in
  1956. and minetest.registered_nodes[self.standing_in].groups.water then
  1957. if self.floats == 1 then
  1958. self.object:set_acceleration({
  1959. x = 0,
  1960. y = -self.fall_speed / (max(1, v.y) ^ 8), -- 8 was 2
  1961. z = 0
  1962. })
  1963. end
  1964. else
  1965. -- fall damage onto solid ground
  1966. if self.fall_damage == 1
  1967. and self.object:get_velocity().y == 0 then
  1968. local d = (self.old_y or 0) - self.object:get_pos().y
  1969. if d > 5 then
  1970. self.health = self.health - floor(d - 5)
  1971. effect(pos, 5, "tnt_smoke.png", 1, 2, 2, nil)
  1972. if self:check_for_death({type = "fall"}) then
  1973. return true
  1974. end
  1975. end
  1976. self.old_y = self.object:get_pos().y
  1977. end
  1978. end
  1979. end
  1980. -- is Took Ranks mod active?
  1981. local tr = minetest.get_modpath("toolranks")
  1982. -- deal damage and effects when mob punched
  1983. function mob_class:on_punch(hitter, tflp, tool_capabilities, dir, damage)
  1984. -- mob health check
  1985. if self.health <= 0 then
  1986. return
  1987. end
  1988. -- custom punch function
  1989. if self.do_punch
  1990. and self:do_punch(hitter, tflp, tool_capabilities, dir) == false then
  1991. return
  1992. end
  1993. -- error checking when mod profiling is enabled
  1994. if not tool_capabilities then
  1995. minetest.log("warning",
  1996. "[mobs] Mod profiling enabled, damage not enabled")
  1997. return
  1998. end
  1999. -- is mob protected?
  2000. if self.protected and hitter:is_player()
  2001. and minetest.is_protected(self.object:get_pos(),
  2002. hitter:get_player_name()) then
  2003. minetest.chat_send_player(hitter:get_player_name(),
  2004. S("Mob has been protected!"))
  2005. return
  2006. end
  2007. local weapon = hitter:get_wielded_item()
  2008. local weapon_def = weapon:get_definition() or {}
  2009. local punch_interval = 1.4
  2010. -- calculate mob damage
  2011. local damage = 0
  2012. local armor = self.object:get_armor_groups() or {}
  2013. local tmp
  2014. -- quick error check incase it ends up 0 (serialize.h check test)
  2015. if tflp == 0 then
  2016. tflp = 0.2
  2017. end
  2018. if use_cmi then
  2019. damage = cmi.calculate_damage(self.object, hitter, tflp, tool_capabilities, dir)
  2020. else
  2021. for group,_ in pairs( (tool_capabilities.damage_groups or {}) ) do
  2022. tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)
  2023. if tmp < 0 then
  2024. tmp = 0.0
  2025. elseif tmp > 1 then
  2026. tmp = 1.0
  2027. end
  2028. damage = damage + (tool_capabilities.damage_groups[group] or 0)
  2029. * tmp * ((armor[group] or 0) / 100.0)
  2030. end
  2031. end
  2032. -- check for tool immunity or special damage
  2033. for n = 1, #self.immune_to do
  2034. if self.immune_to[n][1] == weapon_def.name then
  2035. damage = self.immune_to[n][2] or 0
  2036. break
  2037. -- if "all" then no tools deal damage unless it's specified in list
  2038. elseif self.immune_to[n][1] == "all" then
  2039. damage = self.immune_to[n][2] or 0
  2040. end
  2041. end
  2042. --print("Mob Damage is", damage)
  2043. -- healing
  2044. if damage <= -1 then
  2045. self.health = self.health - floor(damage)
  2046. return true
  2047. end
  2048. if use_cmi
  2049. and cmi.notify_punch(
  2050. self.object, hitter, tflp, tool_capabilities, dir, damage) then
  2051. return
  2052. end
  2053. -- add weapon wear
  2054. punch_interval = tool_capabilities.full_punch_interval or 1.4
  2055. -- toolrank support
  2056. local wear = floor((punch_interval / 75) * 9000)
  2057. if mobs.is_creative(hitter:get_player_name()) then
  2058. if tr then
  2059. wear = 1
  2060. else
  2061. wear = 0
  2062. end
  2063. end
  2064. if tr then
  2065. if weapon_def.original_description then
  2066. toolranks.new_afteruse(weapon, hitter, nil, {wear = wear})
  2067. end
  2068. else
  2069. weapon:add_wear(wear)
  2070. end
  2071. hitter:set_wielded_item(weapon)
  2072. -- only play hit sound and show blood effects if damage is 1 or over
  2073. if damage >= 1 then
  2074. -- weapon sounds
  2075. if weapon_def.sounds then
  2076. local s = random(0, #weapon_def.sounds)
  2077. minetest.sound_play(weapon_def.sounds[s], {
  2078. object = self.object,
  2079. max_hear_distance = 8
  2080. }, true)
  2081. else
  2082. minetest.sound_play("default_punch", {
  2083. object = self.object,
  2084. max_hear_distance = 5
  2085. }, true)
  2086. end
  2087. -- blood_particles
  2088. if not disable_blood and self.blood_amount > 0 then
  2089. local pos = self.object:get_pos()
  2090. local blood = self.blood_texture
  2091. local amount = self.blood_amount
  2092. pos.y = pos.y + (-self.collisionbox[2]
  2093. + self.collisionbox[5]) * .5
  2094. -- lots of damage = more blood :)
  2095. if damage > 10 then
  2096. amount = self.blood_amount * 2
  2097. end
  2098. -- do we have a single blood texture or multiple?
  2099. if type(self.blood_texture) == "table" then
  2100. blood = self.blood_texture[random(#self.blood_texture)]
  2101. end
  2102. effect(pos, amount, blood, 1, 2, 1.75, nil, nil, true)
  2103. end
  2104. -- do damage
  2105. self.health = self.health - floor(damage)
  2106. -- exit here if dead, check for tools with fire damage
  2107. local hot = tool_capabilities and tool_capabilities.damage_groups
  2108. and tool_capabilities.damage_groups.fire
  2109. if self:check_for_death({type = "punch",
  2110. puncher = hitter, hot = hot}) then
  2111. return true
  2112. end
  2113. --[[ add healthy afterglow when hit (causes lag with large textures)
  2114. minetest.after(0.1, function()
  2115. if not self.object:get_luaentity() then return end
  2116. self.object:set_texture_mod("^[colorize:#c9900070")
  2117. minetest.after(0.3, function()
  2118. if not self.object:get_luaentity() then return end
  2119. self.object:set_texture_mod(self.texture_mods)
  2120. end)
  2121. end) ]]
  2122. end -- END if damage
  2123. -- knock back effect (only on full punch)
  2124. if self.knock_back
  2125. and tflp >= punch_interval then
  2126. local v = self.object:get_velocity()
  2127. -- sanity check
  2128. if not v then return end
  2129. local kb = damage or 1
  2130. local up = 2
  2131. -- if already in air then dont go up anymore when hit
  2132. if v.y > 0
  2133. or self.fly then
  2134. up = 0
  2135. end
  2136. -- direction error check
  2137. dir = dir or {x = 0, y = 0, z = 0}
  2138. -- use tool knockback value or default
  2139. kb = tool_capabilities.damage_groups["knockback"] or kb -- (kb * 1.5)
  2140. self.object:set_velocity({
  2141. x = dir.x * kb,
  2142. y = up,
  2143. z = dir.z * kb
  2144. })
  2145. self.pause_timer = 0.25
  2146. end
  2147. -- if skittish then run away
  2148. if self.runaway == true
  2149. and self.order ~= "stand" then
  2150. local lp = hitter:get_pos()
  2151. local yaw = yaw_to_pos(self, lp, 3)
  2152. self.state = "runaway"
  2153. self.runaway_timer = 0
  2154. self.following = nil
  2155. end
  2156. local name = hitter:get_player_name() or ""
  2157. -- attack puncher and call other mobs for help
  2158. if self.passive == false
  2159. and self.state ~= "flop"
  2160. and self.child == false
  2161. and self.attack_players == true
  2162. and hitter:get_player_name() ~= self.owner
  2163. and not mobs.invis[ name ] then
  2164. -- attack whoever punched mob
  2165. self.state = ""
  2166. self:do_attack(hitter)
  2167. -- alert others to the attack
  2168. local objs = minetest.get_objects_inside_radius(
  2169. hitter:get_pos(), self.view_range)
  2170. local obj
  2171. for n = 1, #objs do
  2172. obj = objs[n]:get_luaentity()
  2173. if obj and obj._cmi_is_mob then
  2174. -- only alert members of same mob and assigned helper
  2175. if obj.group_attack == true
  2176. and obj.state ~= "attack"
  2177. and obj.owner ~= name
  2178. and (obj.name == self.name
  2179. or obj.name == self.group_helper) then
  2180. obj:do_attack(hitter)
  2181. end
  2182. -- have owned mobs attack player threat
  2183. if obj.owner == name and obj.owner_loyal then
  2184. obj:do_attack(self.object)
  2185. end
  2186. end
  2187. end
  2188. end
  2189. end
  2190. -- get entity staticdata
  2191. function mob_class:mob_staticdata()
  2192. -- this handles mob count for mobs activated, unloaded, reloaded
  2193. if active_limit > 0 and self.active_toggle then
  2194. active_mobs = active_mobs + self.active_toggle
  2195. self.active_toggle = -self.active_toggle
  2196. --print("-- staticdata", active_mobs, active_limit, self.active_toggle)
  2197. end
  2198. -- remove mob when out of range unless tamed
  2199. if remove_far
  2200. and self.remove_ok
  2201. and self.type ~= "npc"
  2202. and self.state ~= "attack"
  2203. and not self.tamed
  2204. and self.lifetimer < 20000 then
  2205. --print("REMOVED " .. self.name)
  2206. remove_mob(self, true)
  2207. return minetest.serialize({remove_ok = true, static_save = true})
  2208. end
  2209. self.remove_ok = true
  2210. self.attack = nil
  2211. self.following = nil
  2212. self.state = "stand"
  2213. -- used to rotate older mobs
  2214. if self.drawtype and self.drawtype == "side" then
  2215. self.rotate = rad(90)
  2216. end
  2217. if use_cmi then
  2218. self.serialized_cmi_components = cmi.serialize_components(
  2219. self._cmi_components)
  2220. end
  2221. local tmp = {}
  2222. for _,stat in pairs(self) do
  2223. local t = type(stat)
  2224. if t ~= "function" and t ~= "nil" and t ~= "userdata"
  2225. and _ ~= "_cmi_components" then
  2226. tmp[_] = self[_]
  2227. end
  2228. end
  2229. --print('===== '..self.name..'\n'.. dump(tmp)..'\n=====\n')
  2230. return minetest.serialize(tmp)
  2231. end
  2232. -- activate mob and reload settings
  2233. function mob_class:mob_activate(staticdata, def, dtime)
  2234. -- if dtime == 0 then entity has just been created
  2235. -- anything higher means it is respawning (thanks SorceryKid)
  2236. if dtime == 0 and active_limit > 0 then
  2237. self.active_toggle = 1
  2238. end
  2239. -- remove mob if not tamed and mob total reached
  2240. if active_limit > 0 and active_mobs >= active_limit and not self.tamed then
  2241. remove_mob(self)
  2242. --print("-- mob limit reached, removing " .. self.name)
  2243. return
  2244. end
  2245. -- remove monsters in peaceful mode
  2246. if self.type == "monster" and peaceful_only then
  2247. remove_mob(self, true)
  2248. return
  2249. end
  2250. -- load entity variables
  2251. local tmp = minetest.deserialize(staticdata)
  2252. if tmp then
  2253. for _,stat in pairs(tmp) do
  2254. self[_] = stat
  2255. end
  2256. end
  2257. -- force current model into mob
  2258. self.mesh = def.mesh
  2259. self.base_mesh = def.mesh
  2260. self.collisionbox = def.collisionbox
  2261. self.selectionbox = def.selectionbox
  2262. -- select random texture, set model and size
  2263. if not self.base_texture then
  2264. -- compatiblity with old simple mobs textures
  2265. if def.textures and type(def.textures[1]) == "string" then
  2266. def.textures = {def.textures}
  2267. end
  2268. self.base_texture = def.textures and
  2269. def.textures[random(#def.textures)]
  2270. self.base_mesh = def.mesh
  2271. self.base_size = self.visual_size
  2272. self.base_colbox = self.collisionbox
  2273. self.base_selbox = self.selectionbox
  2274. end
  2275. -- for current mobs that dont have this set
  2276. if not self.base_selbox then
  2277. self.base_selbox = self.selectionbox or self.base_colbox
  2278. end
  2279. -- set texture, model and size
  2280. local textures = self.base_texture
  2281. local mesh = self.base_mesh
  2282. local vis_size = self.base_size
  2283. local colbox = self.base_colbox
  2284. local selbox = self.base_selbox
  2285. -- specific texture if gotten
  2286. if self.gotten == true and def.gotten_texture then
  2287. textures = def.gotten_texture
  2288. end
  2289. -- specific mesh if gotten
  2290. if self.gotten == true and def.gotten_mesh then
  2291. mesh = def.gotten_mesh
  2292. end
  2293. -- set child objects to half size
  2294. if self.child == true then
  2295. vis_size = {x = self.base_size.x * .5, y = self.base_size.y * .5}
  2296. if def.child_texture then
  2297. textures = def.child_texture[1]
  2298. end
  2299. colbox = {
  2300. self.base_colbox[1] * .5, self.base_colbox[2] * .5,
  2301. self.base_colbox[3] * .5, self.base_colbox[4] * .5,
  2302. self.base_colbox[5] * .5, self.base_colbox[6] * .5}
  2303. selbox = {
  2304. self.base_selbox[1] * .5, self.base_selbox[2] * .5,
  2305. self.base_selbox[3] * .5, self.base_selbox[4] * .5,
  2306. self.base_selbox[5] * .5, self.base_selbox[6] * .5}
  2307. end
  2308. if self.health == 0 then
  2309. self.health = random(self.hp_min, self.hp_max)
  2310. end
  2311. -- pathfinding init
  2312. self.path = {}
  2313. self.path.way = {} -- path to follow, table of positions
  2314. self.path.lastpos = {x = 0, y = 0, z = 0}
  2315. self.path.stuck = false
  2316. self.path.following = false -- currently following path?
  2317. self.path.stuck_timer = 0 -- if stuck for too long search for path
  2318. -- Armor groups (immortal = 1 for custom damage handling)
  2319. local armor
  2320. if type(self.armor) == "table" then
  2321. armor = table.copy(self.armor)
  2322. -- armor.immortal = 1
  2323. else
  2324. -- armor = {immortal = 1, fleshy = self.armor}
  2325. armor = {fleshy = self.armor}
  2326. end
  2327. self.object:set_armor_groups(armor)
  2328. -- mob defaults
  2329. self.old_y = self.object:get_pos().y
  2330. self.old_health = self.health
  2331. self.sounds.distance = self.sounds.distance or 10
  2332. self.textures = textures
  2333. self.mesh = mesh
  2334. self.collisionbox = colbox
  2335. self.selectionbox = selbox
  2336. self.visual_size = vis_size
  2337. self.standing_in = "air"
  2338. -- check existing nametag
  2339. if not self.nametag then
  2340. self.nametag = def.nametag
  2341. end
  2342. -- set anything changed above
  2343. self.object:set_properties(self)
  2344. self:set_yaw((random(0, 360) - 180) / 180 * pi, 6)
  2345. self:update_tag()
  2346. self:set_animation("stand")
  2347. -- apply any texture mods
  2348. self.object:set_texture_mod(self.texture_mods)
  2349. -- set 5.x flag to remove monsters when map area unloaded
  2350. if remove_far and self.type == "monster" then
  2351. self.static_save = false
  2352. end
  2353. -- run on_spawn function if found
  2354. if self.on_spawn and not self.on_spawn_run then
  2355. if self.on_spawn(self) then
  2356. self.on_spawn_run = true -- if true, set flag to run once only
  2357. end
  2358. end
  2359. -- run after_activate
  2360. if def.after_activate then
  2361. def.after_activate(self, staticdata, def, dtime)
  2362. end
  2363. if use_cmi then
  2364. self._cmi_components = cmi.activate_components(
  2365. self.serialized_cmi_components)
  2366. cmi.notify_activate(self.object, dtime)
  2367. end
  2368. end
  2369. -- handle mob lifetimer and expiration
  2370. function mob_class:mob_expire(pos, dtime)
  2371. -- when lifetimer expires remove mob (except npc and tamed)
  2372. if self.type ~= "npc"
  2373. and not self.tamed
  2374. and self.state ~= "attack"
  2375. and remove_far ~= true
  2376. and self.lifetimer < 20000 then
  2377. self.lifetimer = self.lifetimer - dtime
  2378. if self.lifetimer <= 0 then
  2379. -- only despawn away from player
  2380. local objs = minetest.get_objects_inside_radius(pos, 15)
  2381. for n = 1, #objs do
  2382. if objs[n]:is_player() then
  2383. self.lifetimer = 20
  2384. return
  2385. end
  2386. end
  2387. -- minetest.log("action",
  2388. -- S("lifetimer expired, removed @1", self.name))
  2389. effect(pos, 15, "tnt_smoke.png", 2, 4, 2, 0)
  2390. remove_mob(self, true)
  2391. return
  2392. end
  2393. end
  2394. end
  2395. -- main mob function
  2396. function mob_class:on_step(dtime, moveresult)
  2397. --[[ moveresult contains this for physical mobs
  2398. {
  2399. touching_ground = boolean,
  2400. collides = boolean,
  2401. standing_on_object = boolean,
  2402. collisions = {
  2403. {
  2404. type = string, -- "node" or "object",
  2405. axis = string, -- "x", "y" or "z"
  2406. node_pos = vector, -- if type is "node"
  2407. object = ObjectRef, -- if type is "object"
  2408. old_velocity = vector,
  2409. new_velocity = vector,
  2410. }}
  2411. }]]
  2412. if use_cmi then
  2413. cmi.notify_step(self.object, dtime)
  2414. end
  2415. local pos = self.object:get_pos()
  2416. local yaw = self.object:get_yaw()
  2417. -- early warning check, if no yaw then no entity, skip rest of function
  2418. if not yaw then return end
  2419. -- get node at foot level every quarter second
  2420. self.node_timer = (self.node_timer or 0) + dtime
  2421. if self.node_timer > 0.25 then
  2422. self.node_timer = 0
  2423. local y_level = self.collisionbox[2]
  2424. if self.child then
  2425. y_level = self.collisionbox[2] * 0.5
  2426. end
  2427. -- what is mob standing in?
  2428. self.standing_in = node_ok({
  2429. x = pos.x, y = pos.y + y_level + 0.25, z = pos.z}, "air").name
  2430. --print("standing in " .. self.standing_in)
  2431. -- if standing inside solid block then jump to escape
  2432. if minetest.registered_nodes[self.standing_in].walkable
  2433. and minetest.registered_nodes[self.standing_in].drawtype
  2434. == "normal" then
  2435. self.object:set_velocity({
  2436. x = 0,
  2437. y = self.jump_height,
  2438. z = 0
  2439. })
  2440. end
  2441. -- check and stop if standing at cliff and fear of heights
  2442. self.at_cliff = self:is_at_cliff()
  2443. if self.at_cliff then
  2444. self:set_velocity(0)
  2445. end
  2446. -- has mob expired (0.25 instead of dtime since were in a timer)
  2447. self:mob_expire(pos, 0.25)
  2448. end
  2449. -- check if falling, flying, floating and return if player died
  2450. if self:falling(pos) then
  2451. return
  2452. end
  2453. -- smooth rotation by ThomasMonroe314
  2454. if self.delay and self.delay > 0 then
  2455. if self.delay == 1 then
  2456. yaw = self.target_yaw
  2457. else
  2458. local dif = abs(yaw - self.target_yaw)
  2459. if yaw > self.target_yaw then
  2460. if dif > pi then
  2461. dif = 2 * pi - dif -- need to add
  2462. yaw = yaw + dif / self.delay
  2463. else
  2464. yaw = yaw - dif / self.delay -- need to subtract
  2465. end
  2466. elseif yaw < self.target_yaw then
  2467. if dif > pi then
  2468. dif = 2 * pi - dif
  2469. yaw = yaw - dif / self.delay -- need to subtract
  2470. else
  2471. yaw = yaw + dif / self.delay -- need to add
  2472. end
  2473. end
  2474. if yaw > (pi * 2) then yaw = yaw - (pi * 2) end
  2475. if yaw < 0 then yaw = yaw + (pi * 2) end
  2476. end
  2477. self.delay = self.delay - 1
  2478. self.object:set_yaw(yaw)
  2479. end
  2480. -- knockback timer
  2481. if self.pause_timer > 0 then
  2482. self.pause_timer = self.pause_timer - dtime
  2483. return
  2484. end
  2485. -- run custom function (defined in mob lua file)
  2486. if self.do_custom then
  2487. -- when false skip going any further
  2488. if self:do_custom(dtime) == false then
  2489. return
  2490. end
  2491. end
  2492. -- attack timer
  2493. self.timer = self.timer + dtime
  2494. if self.state ~= "attack" then
  2495. if self.timer < 1 then
  2496. return
  2497. end
  2498. self.timer = 0
  2499. end
  2500. -- never go over 100
  2501. if self.timer > 100 then
  2502. self.timer = 1
  2503. end
  2504. -- mob plays random sound at times
  2505. if random(100) == 1 then
  2506. self:mob_sound(self.sounds.random)
  2507. end
  2508. -- environmental damage timer (every 1 second)
  2509. self.env_damage_timer = self.env_damage_timer + dtime
  2510. if (self.state == "attack" and self.env_damage_timer > 1)
  2511. or self.state ~= "attack" then
  2512. self.env_damage_timer = 0
  2513. -- check for environmental damage (water, fire, lava etc.)
  2514. if self:do_env_damage() then return end
  2515. -- node replace check (cow eats grass etc.)
  2516. self:replace(pos)
  2517. end
  2518. self:general_attack()
  2519. self:breed()
  2520. self:follow_flop()
  2521. if self:do_states(dtime) then return end
  2522. self:do_jump()
  2523. self:do_runaway_from(self)
  2524. self:do_stay_near()
  2525. end
  2526. -- default function when mobs are blown up with TNT
  2527. function mob_class:on_blast(damage)
  2528. --print("-- blast damage", damage)
  2529. self.object:punch(self.object, 1.0, {
  2530. full_punch_interval = 1.0,
  2531. damage_groups = {fleshy = damage},
  2532. }, nil)
  2533. -- return no damage, no knockback, no item drops, mob api handles all
  2534. return false, false, {}
  2535. end
  2536. mobs.spawning_mobs = {}
  2537. -- register mob entity
  2538. function mobs:register_mob(name, def)
  2539. mobs.spawning_mobs[name] = true
  2540. minetest.register_entity(name, setmetatable({
  2541. stepheight = def.stepheight,
  2542. name = name,
  2543. type = def.type,
  2544. attack_type = def.attack_type,
  2545. fly = def.fly,
  2546. fly_in = def.fly_in,
  2547. keep_flying = def.keep_flying,
  2548. owner = def.owner,
  2549. order = def.order,
  2550. on_die = def.on_die,
  2551. do_custom = def.do_custom,
  2552. jump_height = def.jump_height,
  2553. drawtype = def.drawtype, -- DEPRECATED, use rotate instead
  2554. rotate = rad(def.rotate or 0), -- 0=front 90=side 180=back 270=side2
  2555. glow = def.glow,
  2556. lifetimer = def.lifetimer,
  2557. hp_min = max(1, (def.hp_min or 5) * difficulty),
  2558. hp_max = max(1, (def.hp_max or 10) * difficulty),
  2559. collisionbox = def.collisionbox,
  2560. selectionbox = def.selectionbox or def.collisionbox,
  2561. visual = def.visual,
  2562. visual_size = def.visual_size,
  2563. mesh = def.mesh,
  2564. makes_footstep_sound = def.makes_footstep_sound,
  2565. view_range = def.view_range,
  2566. walk_velocity = def.walk_velocity,
  2567. run_velocity = def.run_velocity,
  2568. damage = max(0, (def.damage or 0) * difficulty),
  2569. light_damage = def.light_damage,
  2570. light_damage_min = def.light_damage_min,
  2571. light_damage_max = def.light_damage_max,
  2572. water_damage = def.water_damage,
  2573. lava_damage = def.lava_damage,
  2574. suffocation = def.suffocation,
  2575. fall_damage = def.fall_damage,
  2576. fall_speed = def.fall_speed,
  2577. drops = def.drops,
  2578. armor = def.armor,
  2579. on_rightclick = def.on_rightclick,
  2580. arrow = def.arrow,
  2581. shoot_interval = def.shoot_interval,
  2582. sounds = def.sounds,
  2583. animation = def.animation,
  2584. follow = def.follow,
  2585. jump = def.jump,
  2586. walk_chance = def.walk_chance,
  2587. stand_chance = def.stand_chance,
  2588. attack_chance = def.attack_chance,
  2589. passive = def.passive,
  2590. knock_back = def.knock_back,
  2591. blood_amount = def.blood_amount,
  2592. blood_texture = def.blood_texture,
  2593. shoot_offset = def.shoot_offset,
  2594. floats = def.floats,
  2595. replace_rate = def.replace_rate,
  2596. replace_what = def.replace_what,
  2597. replace_with = def.replace_with,
  2598. replace_offset = def.replace_offset,
  2599. on_replace = def.on_replace,
  2600. reach = def.reach,
  2601. texture_list = def.textures,
  2602. texture_mods = def.texture_mods or "",
  2603. child_texture = def.child_texture,
  2604. docile_by_day = def.docile_by_day,
  2605. fear_height = def.fear_height,
  2606. runaway = def.runaway,
  2607. pathfinding = def.pathfinding,
  2608. immune_to = def.immune_to,
  2609. explosion_radius = def.explosion_radius,
  2610. explosion_damage_radius = def.explosion_damage_radius,
  2611. explosion_timer = def.explosion_timer,
  2612. allow_fuse_reset = def.allow_fuse_reset,
  2613. stop_to_explode = def.stop_to_explode,
  2614. custom_attack = def.custom_attack,
  2615. double_melee_attack = def.double_melee_attack,
  2616. dogshoot_switch = def.dogshoot_switch,
  2617. dogshoot_count_max = def.dogshoot_count_max,
  2618. dogshoot_count2_max = def.dogshoot_count2_max or def.dogshoot_count_max,
  2619. group_attack = def.group_attack,
  2620. group_helper = def.group_helper,
  2621. attack_monsters = def.attacks_monsters or def.attack_monsters,
  2622. attack_animals = def.attack_animals,
  2623. attack_players = def.attack_players,
  2624. attack_npcs = def.attack_npcs,
  2625. specific_attack = def.specific_attack,
  2626. runaway_from = def.runaway_from,
  2627. owner_loyal = def.owner_loyal,
  2628. pushable = def.pushable,
  2629. stay_near = def.stay_near,
  2630. on_spawn = def.on_spawn,
  2631. on_blast = def.on_blast, -- class redifinition
  2632. do_punch = def.do_punch,
  2633. on_breed = def.on_breed,
  2634. on_grown = def.on_grown,
  2635. on_activate = function(self, staticdata, dtime)
  2636. return self:mob_activate(staticdata, def, dtime)
  2637. end,
  2638. get_staticdata = function(self)
  2639. return self:mob_staticdata(self)
  2640. end,
  2641. }, mob_class_meta))
  2642. end -- END mobs:register_mob function
  2643. -- count how many mobs of one type are inside an area
  2644. -- will also return true for second value if player is inside area
  2645. local count_mobs = function(pos, type)
  2646. local total = 0
  2647. local objs = minetest.get_objects_inside_radius(pos, aoc_range * 2)
  2648. local ent
  2649. local players
  2650. for n = 1, #objs do
  2651. if not objs[n]:is_player() then
  2652. ent = objs[n]:get_luaentity()
  2653. -- count mob type and add to total also
  2654. if ent and ent.name and ent.name == type then
  2655. total = total + 1
  2656. end
  2657. else
  2658. players = true
  2659. end
  2660. end
  2661. return total, players
  2662. end
  2663. -- global functions
  2664. function mobs:spawn_abm_check(pos, node, name)
  2665. -- global function to add additional spawn checks
  2666. -- return true to stop spawning mob
  2667. end
  2668. function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
  2669. interval, chance, aoc, min_height, max_height, day_toggle, on_spawn)
  2670. -- Do mobs spawn at all?
  2671. if not mobs_spawn then
  2672. return
  2673. end
  2674. -- chance/spawn number override in minetest.conf for registered mob
  2675. local numbers = minetest.settings:get(name)
  2676. if numbers then
  2677. numbers = numbers:split(",")
  2678. chance = tonumber(numbers[1]) or chance
  2679. aoc = tonumber(numbers[2]) or aoc
  2680. if chance == 0 then
  2681. minetest.log("warning",
  2682. string.format("[mobs] %s has spawning disabled", name))
  2683. return
  2684. end
  2685. minetest.log("action", string.format(
  2686. "[mobs] Chance setting for %s changed to %s (total: %s)",
  2687. name, chance, aoc))
  2688. end
  2689. minetest.register_abm({
  2690. label = name .. " spawning",
  2691. nodenames = nodes,
  2692. neighbors = neighbors,
  2693. interval = interval,
  2694. chance = max(1, (chance * mob_chance_multiplier)),
  2695. catch_up = false,
  2696. action = function(pos, node, active_object_count,
  2697. active_object_count_wider)
  2698. -- is mob actually registered?
  2699. if not mobs.spawning_mobs[name]
  2700. or not minetest.registered_entities[name] then
  2701. --print("--- mob doesn't exist", name)
  2702. return
  2703. end
  2704. -- are we over active mob limit
  2705. if active_limit > 0 and active_mobs >= active_limit then
  2706. --print("--- active mob limit reached", active_mobs, active_limit)
  2707. return
  2708. end
  2709. -- additional custom checks for spawning mob
  2710. if mobs:spawn_abm_check(pos, node, name) == true then
  2711. return
  2712. end
  2713. -- do not spawn if too many entities in area
  2714. if active_object_count_wider >= max_per_block then
  2715. --print("--- too many entities in area", active_object_count_wider)
  2716. return
  2717. end
  2718. -- get total number of this mob in area
  2719. local num_mob, is_pla = count_mobs(pos, name)
  2720. if not is_pla then
  2721. --print("--- no players within active area, will not spawn " .. name)
  2722. return
  2723. end
  2724. if num_mob >= aoc then
  2725. --print("--- too many " .. name .. " in area", num_mob .. "/" .. aoc)
  2726. return
  2727. end
  2728. -- if toggle set to nil then ignore day/night check
  2729. if day_toggle ~= nil then
  2730. local tod = (minetest.get_timeofday() or 0) * 24000
  2731. if tod > 4500 and tod < 19500 then
  2732. -- daylight, but mob wants night
  2733. if day_toggle == false then
  2734. --print("--- mob needs night", name)
  2735. return
  2736. end
  2737. else
  2738. -- night time but mob wants day
  2739. if day_toggle == true then
  2740. --print("--- mob needs day", name)
  2741. return
  2742. end
  2743. end
  2744. end
  2745. -- spawn above node
  2746. pos.y = pos.y + 1
  2747. -- are we spawning within height limits?
  2748. if pos.y > max_height
  2749. or pos.y < min_height then
  2750. --print("--- height limits not met", name, pos.y)
  2751. return
  2752. end
  2753. -- are light levels ok?
  2754. local light = minetest.get_node_light(pos)
  2755. if not light
  2756. or light > max_light
  2757. or light < min_light then
  2758. --print("--- light limits not met", name, light)
  2759. return
  2760. end
  2761. -- mobs cannot spawn in protected areas when enabled
  2762. if not spawn_protected
  2763. and minetest.is_protected(pos, "") then
  2764. --print("--- inside protected area", name)
  2765. return
  2766. end
  2767. -- only spawn a set distance away from player
  2768. local objs = minetest.get_objects_inside_radius(
  2769. pos, mob_nospawn_range)
  2770. for n = 1, #objs do
  2771. if objs[n]:is_player() then
  2772. --print("--- player too close", name)
  2773. return
  2774. end
  2775. end
  2776. -- do we have enough space to spawn mob? (thanks wuzzy)
  2777. local ent = minetest.registered_entities[name]
  2778. local width_x = max(1,
  2779. ceil(ent.collisionbox[4] - ent.collisionbox[1]))
  2780. local min_x, max_x
  2781. if width_x % 2 == 0 then
  2782. max_x = floor(width_x / 2)
  2783. min_x = -(max_x - 1)
  2784. else
  2785. max_x = floor(width_x / 2)
  2786. min_x = -max_x
  2787. end
  2788. local width_z = max(1,
  2789. ceil(ent.collisionbox[6] - ent.collisionbox[3]))
  2790. local min_z, max_z
  2791. if width_z % 2 == 0 then
  2792. max_z = floor(width_z / 2)
  2793. min_z = -(max_z - 1)
  2794. else
  2795. max_z = floor(width_z / 2)
  2796. min_z = -max_z
  2797. end
  2798. local max_y = max(0,
  2799. ceil(ent.collisionbox[5] - ent.collisionbox[2]) - 1)
  2800. for y = 0, max_y do
  2801. for x = min_x, max_x do
  2802. for z = min_z, max_z do
  2803. local pos2 = {
  2804. x = pos.x + x,
  2805. y = pos.y + y,
  2806. z = pos.z + z}
  2807. if minetest.registered_nodes[
  2808. node_ok(pos2).name].walkable == true then
  2809. --print("--- not enough space to spawn", name)
  2810. return
  2811. end
  2812. end
  2813. end
  2814. end
  2815. -- spawn mob 1/2 node above ground
  2816. pos.y = pos.y + 0.5
  2817. -- tweak X/Z spawn pos
  2818. if width_x % 2 == 0 then
  2819. pos.x = pos.x + 0.5
  2820. end
  2821. if width_z % 2 == 0 then
  2822. pos.z = pos.z + 0.5
  2823. end
  2824. local mob = minetest.add_entity(pos, name)
  2825. -- print("[mobs] Spawned " .. name .. " at "
  2826. -- .. minetest.pos_to_string(pos) .. " on "
  2827. -- .. node.name .. " near " .. neighbors[1])
  2828. if on_spawn then
  2829. local ent = mob:get_luaentity()
  2830. on_spawn(ent, pos)
  2831. end
  2832. end
  2833. })
  2834. end
  2835. -- compatibility with older mob registration
  2836. function mobs:register_spawn(name, nodes, max_light, min_light, chance,
  2837. active_object_count, max_height, day_toggle)
  2838. mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
  2839. chance, active_object_count, -31000, max_height, day_toggle)
  2840. end
  2841. -- MarkBu's spawn function
  2842. function mobs:spawn(def)
  2843. mobs:spawn_specific(
  2844. def.name,
  2845. def.nodes or {"group:soil", "group:stone"},
  2846. def.neighbors or {"air"},
  2847. def.min_light or 0,
  2848. def.max_light or 15,
  2849. def.interval or 30,
  2850. def.chance or 5000,
  2851. def.active_object_count or 1,
  2852. def.min_height or -31000,
  2853. def.max_height or 31000,
  2854. def.day_toggle,
  2855. def.on_spawn)
  2856. end
  2857. -- register arrow for shoot attack
  2858. function mobs:register_arrow(name, def)
  2859. if not name or not def then return end -- errorcheck
  2860. minetest.register_entity(name, {
  2861. physical = false,
  2862. visual = def.visual,
  2863. visual_size = def.visual_size,
  2864. textures = def.textures,
  2865. velocity = def.velocity,
  2866. hit_player = def.hit_player,
  2867. hit_node = def.hit_node,
  2868. hit_mob = def.hit_mob,
  2869. hit_object = def.hit_object,
  2870. drop = def.drop or false, -- drops arrow as registered item when true
  2871. collisionbox = def.collisionbox or {-.1, -.1, -.1, .1, .1, .1},
  2872. timer = 0,
  2873. switch = 0,
  2874. owner_id = def.owner_id,
  2875. rotate = def.rotate,
  2876. automatic_face_movement_dir = def.rotate
  2877. and (def.rotate - (pi / 180)) or false,
  2878. on_activate = def.on_activate,
  2879. on_punch = def.on_punch or function(
  2880. self, hitter, tflp, tool_capabilities, dir)
  2881. end,
  2882. on_step = def.on_step or function(self, dtime)
  2883. self.timer = self.timer + 1
  2884. local pos = self.object:get_pos()
  2885. if self.switch == 0 or self.timer > 150 then
  2886. self.object:remove() ; -- print("removed arrow")
  2887. return
  2888. end
  2889. -- does arrow have a tail (fireball)
  2890. if def.tail and def.tail == 1 and def.tail_texture then
  2891. minetest.add_particle({
  2892. pos = pos,
  2893. velocity = {x = 0, y = 0, z = 0},
  2894. acceleration = {x = 0, y = 0, z = 0},
  2895. expirationtime = def.expire or 0.25,
  2896. collisiondetection = false,
  2897. texture = def.tail_texture,
  2898. size = def.tail_size or 5,
  2899. glow = def.glow or 0
  2900. })
  2901. end
  2902. if self.hit_node then
  2903. local node = node_ok(pos).name
  2904. if minetest.registered_nodes[node].walkable then
  2905. self:hit_node(pos, node)
  2906. if self.drop == true then
  2907. pos.y = pos.y + 1
  2908. self.lastpos = (self.lastpos or pos)
  2909. minetest.add_item(self.lastpos,
  2910. self.object:get_luaentity().name)
  2911. end
  2912. self.object:remove() ; -- print("hit node")
  2913. return
  2914. end
  2915. end
  2916. if self.hit_player or self.hit_mob or self.hit_object then
  2917. for _,player in pairs(
  2918. minetest.get_objects_inside_radius(pos, 1.0)) do
  2919. if self.hit_player and player:is_player() then
  2920. self:hit_player(player)
  2921. self.object:remove() ; -- print("hit player")
  2922. return
  2923. end
  2924. local entity = player:get_luaentity()
  2925. if entity
  2926. and self.hit_mob
  2927. and entity._cmi_is_mob == true
  2928. and tostring(player) ~= self.owner_id
  2929. and entity.name ~= self.object:get_luaentity().name then
  2930. self:hit_mob(player)
  2931. self.object:remove() ; --print("hit mob")
  2932. return
  2933. end
  2934. if entity
  2935. and self.hit_object
  2936. and (not entity._cmi_is_mob)
  2937. and tostring(player) ~= self.owner_id
  2938. and entity.name ~= self.object:get_luaentity().name then
  2939. self:hit_object(player)
  2940. self.object:remove(); -- print("hit object")
  2941. return
  2942. end
  2943. end
  2944. end
  2945. self.lastpos = pos
  2946. end
  2947. })
  2948. end
  2949. -- compatibility function
  2950. function mobs:explosion(pos, radius)
  2951. mobs:boom({sounds = {explode = "tnt_explode"}}, pos, radius)
  2952. end
  2953. -- no damage to nodes explosion
  2954. function mobs:safe_boom(self, pos, radius)
  2955. minetest.sound_play(self.sounds and self.sounds.explode or "tnt_explode", {
  2956. pos = pos,
  2957. gain = 1.0,
  2958. max_hear_distance = self.sounds and self.sounds.distance or 32
  2959. }, true)
  2960. entity_physics(pos, radius)
  2961. effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
  2962. end
  2963. -- make explosion with protection and tnt mod check
  2964. function mobs:boom(self, pos, radius)
  2965. if mobs_griefing
  2966. and minetest.get_modpath("tnt") and tnt and tnt.boom
  2967. and not minetest.is_protected(pos, "") then
  2968. tnt.boom(pos, {
  2969. radius = radius,
  2970. damage_radius = radius,
  2971. sound = self.sounds and self.sounds.explode,
  2972. explode_center = true,
  2973. })
  2974. else
  2975. mobs:safe_boom(self, pos, radius)
  2976. end
  2977. end
  2978. -- Register spawn eggs
  2979. -- Note: This also introduces the “spawn_egg” group:
  2980. -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
  2981. -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
  2982. function mobs:register_egg(mob, desc, background, addegg, no_creative)
  2983. local grp = {spawn_egg = 1}
  2984. -- do NOT add this egg to creative inventory (e.g. dungeon master)
  2985. if no_creative == true then
  2986. grp.not_in_creative_inventory = 1
  2987. end
  2988. local invimg = background
  2989. if addegg == 1 then
  2990. invimg = "mobs_chicken_egg.png^(" .. invimg ..
  2991. "^[mask:mobs_chicken_egg_overlay.png)"
  2992. end
  2993. -- register new spawn egg containing mob information
  2994. minetest.register_craftitem(mob .. "_set", {
  2995. description = S("@1 (Tamed)", desc),
  2996. inventory_image = invimg,
  2997. groups = {spawn_egg = 2, not_in_creative_inventory = 1},
  2998. stack_max = 1,
  2999. on_place = function(itemstack, placer, pointed_thing)
  3000. local pos = pointed_thing.above
  3001. -- does existing on_rightclick function exist?
  3002. local under = minetest.get_node(pointed_thing.under)
  3003. local def = minetest.registered_nodes[under.name]
  3004. if def and def.on_rightclick then
  3005. return def.on_rightclick(
  3006. pointed_thing.under, under, placer, itemstack)
  3007. end
  3008. if pos
  3009. and not minetest.is_protected(pos, placer:get_player_name()) then
  3010. if not minetest.registered_entities[mob] then
  3011. return
  3012. end
  3013. pos.y = pos.y + 1
  3014. local data = itemstack:get_metadata()
  3015. local smob = minetest.add_entity(pos, mob, data)
  3016. local ent = smob and smob:get_luaentity()
  3017. if not ent then return end -- sanity check
  3018. -- set owner if not a monster
  3019. if ent.type ~= "monster" then
  3020. ent.owner = placer:get_player_name()
  3021. ent.tamed = true
  3022. end
  3023. -- since mob is unique we remove egg once spawned
  3024. itemstack:take_item()
  3025. end
  3026. return itemstack
  3027. end,
  3028. })
  3029. -- register old stackable mob egg
  3030. minetest.register_craftitem(mob, {
  3031. description = desc,
  3032. inventory_image = invimg,
  3033. groups = grp,
  3034. on_place = function(itemstack, placer, pointed_thing)
  3035. local pos = pointed_thing.above
  3036. -- does existing on_rightclick function exist?
  3037. local under = minetest.get_node(pointed_thing.under)
  3038. local def = minetest.registered_nodes[under.name]
  3039. if def and def.on_rightclick then
  3040. return def.on_rightclick(
  3041. pointed_thing.under, under, placer, itemstack)
  3042. end
  3043. if pos
  3044. and not minetest.is_protected(pos, placer:get_player_name()) then
  3045. if not minetest.registered_entities[mob] then
  3046. return
  3047. end
  3048. -- have we reached active mob limit
  3049. if active_limit > 0 and active_mobs >= active_limit then
  3050. minetest.chat_send_player(placer:get_player_name(),
  3051. S("Active Mob Limit Reached!")
  3052. .. " (" .. active_mobs
  3053. .. " / " .. active_limit .. ")")
  3054. return
  3055. end
  3056. pos.y = pos.y + 1
  3057. local smob = minetest.add_entity(pos, mob)
  3058. local ent = smob and smob:get_luaentity()
  3059. if not ent then return end -- sanity check
  3060. -- don't set owner if monster or sneak pressed
  3061. if ent.type ~= "monster"
  3062. and not placer:get_player_control().sneak then
  3063. ent.owner = placer:get_player_name()
  3064. ent.tamed = true
  3065. end
  3066. -- if not in creative then take item
  3067. if not mobs.is_creative(placer:get_player_name()) then
  3068. itemstack:take_item()
  3069. end
  3070. end
  3071. return itemstack
  3072. end,
  3073. })
  3074. end
  3075. -- force capture a mob if space available in inventory, or drop as spawn egg
  3076. function mobs:force_capture(self, clicker)
  3077. -- add special mob egg with all mob information
  3078. local new_stack = ItemStack(self.name .. "_set")
  3079. local tmp = {}
  3080. for _,stat in pairs(self) do
  3081. local t = type(stat)
  3082. if t ~= "function" and t ~= "nil" and t ~= "userdata" then
  3083. tmp[_] = self[_]
  3084. end
  3085. end
  3086. local data_str = minetest.serialize(tmp)
  3087. new_stack:set_metadata(data_str)
  3088. local inv = clicker:get_inventory()
  3089. if inv:room_for_item("main", new_stack) then
  3090. inv:add_item("main", new_stack)
  3091. else
  3092. minetest.add_item(clicker:get_pos(), new_stack)
  3093. end
  3094. self:mob_sound("default_place_node_hard")
  3095. remove_mob(self, true)
  3096. end
  3097. -- capture critter (thanks to blert2112 for idea)
  3098. function mobs:capture_mob(self, clicker, chance_hand, chance_net,
  3099. chance_lasso, force_take, replacewith)
  3100. if self.child
  3101. or not clicker:is_player()
  3102. or not clicker:get_inventory() then
  3103. return false
  3104. end
  3105. -- get name of clicked mob
  3106. local mobname = self.name
  3107. -- if not nil change what will be added to inventory
  3108. if replacewith then
  3109. mobname = replacewith
  3110. end
  3111. local name = clicker:get_player_name()
  3112. local tool = clicker:get_wielded_item()
  3113. -- are we using hand, net or lasso to pick up mob?
  3114. if tool:get_name() ~= ""
  3115. and tool:get_name() ~= "mobs:net"
  3116. and tool:get_name() ~= "mobs:lasso" then
  3117. return false
  3118. end
  3119. -- is mob tamed?
  3120. if self.tamed == false and force_take == false then
  3121. minetest.chat_send_player(name, S("Not tamed!"))
  3122. return false
  3123. end
  3124. -- cannot pick up if not owner
  3125. if self.owner ~= name and force_take == false then
  3126. minetest.chat_send_player(name, S("@1 is owner!", self.owner))
  3127. return false
  3128. end
  3129. if clicker:get_inventory():room_for_item("main", mobname) then
  3130. -- was mob clicked with hand, net, or lasso?
  3131. local chance = 0
  3132. if tool:get_name() == "" then
  3133. chance = chance_hand
  3134. elseif tool:get_name() == "mobs:net" then
  3135. chance = chance_net
  3136. tool:add_wear(4000) -- 17 uses
  3137. clicker:set_wielded_item(tool)
  3138. elseif tool:get_name() == "mobs:lasso" then
  3139. chance = chance_lasso
  3140. tool:add_wear(650) -- 100 uses
  3141. clicker:set_wielded_item(tool)
  3142. end
  3143. -- calculate chance.. add to inventory if successful?
  3144. if chance and chance > 0 and random(100) <= chance then
  3145. -- default mob egg
  3146. local new_stack = ItemStack(mobname)
  3147. -- add special mob egg with all mob information
  3148. -- unless 'replacewith' contains new item to use
  3149. if not replacewith then
  3150. new_stack = ItemStack(mobname .. "_set")
  3151. local tmp = {}
  3152. for _,stat in pairs(self) do
  3153. local t = type(stat)
  3154. if t ~= "function"
  3155. and t ~= "nil"
  3156. and t ~= "userdata" then
  3157. tmp[_] = self[_]
  3158. end
  3159. end
  3160. local data_str = minetest.serialize(tmp)
  3161. new_stack:set_metadata(data_str)
  3162. end
  3163. local inv = clicker:get_inventory()
  3164. if inv:room_for_item("main", new_stack) then
  3165. inv:add_item("main", new_stack)
  3166. else
  3167. minetest.add_item(clicker:get_pos(), new_stack)
  3168. end
  3169. remove_mob(self, true)
  3170. self:mob_sound("default_place_node_hard")
  3171. return new_stack
  3172. -- when chance above fails or set to 0, miss!
  3173. elseif chance and chance ~= 0 then
  3174. minetest.chat_send_player(name, S("Missed!"))
  3175. self:mob_sound("mobs_swing")
  3176. return false
  3177. -- when chance is nil always return a miss (used for npc walk/follow)
  3178. elseif not chance then
  3179. return false
  3180. end
  3181. end
  3182. return true
  3183. end
  3184. -- protect tamed mob with rune item
  3185. function mobs:protect(self, clicker)
  3186. local name = clicker:get_player_name()
  3187. local tool = clicker:get_wielded_item()
  3188. if tool:get_name() ~= "mobs:protector" then
  3189. return false
  3190. end
  3191. if self.tamed == false then
  3192. minetest.chat_send_player(name, S("Not tamed!"))
  3193. return true -- false
  3194. end
  3195. if self.protected == true then
  3196. minetest.chat_send_player(name, S("Already protected!"))
  3197. return true -- false
  3198. end
  3199. if not mobs.is_creative(clicker:get_player_name()) then
  3200. tool:take_item() -- take 1 protection rune
  3201. clicker:set_wielded_item(tool)
  3202. end
  3203. self.protected = true
  3204. local pos = self.object:get_pos()
  3205. pos.y = pos.y + self.collisionbox[2] + 0.5
  3206. effect(self.object:get_pos(), 25, "mobs_protect_particle.png",
  3207. 0.5, 4, 2, 15)
  3208. self:mob_sound("mobs_spell")
  3209. return true
  3210. end
  3211. local mob_obj = {}
  3212. local mob_sta = {}
  3213. -- feeding, taming and breeding (thanks blert2112)
  3214. function mobs:feed_tame(self, clicker, feed_count, breed, tame)
  3215. -- can eat/tame with item in hand
  3216. if self.follow
  3217. and self:follow_holding(clicker) then
  3218. -- if not in creative then take item
  3219. if not mobs.is_creative(clicker:get_player_name()) then
  3220. local item = clicker:get_wielded_item()
  3221. item:take_item()
  3222. clicker:set_wielded_item(item)
  3223. end
  3224. -- increase health
  3225. self.health = self.health + 4
  3226. if self.health >= self.hp_max then
  3227. self.health = self.hp_max
  3228. if self.htimer < 1 then
  3229. minetest.chat_send_player(clicker:get_player_name(),
  3230. S("@1 at full health (@2)",
  3231. self.name:split(":")[2], tostring(self.health)))
  3232. self.htimer = 5
  3233. end
  3234. end
  3235. self.object:set_hp(self.health)
  3236. self:update_tag()
  3237. -- make children grow quicker
  3238. if self.child == true then
  3239. self.hornytimer = self.hornytimer + 20
  3240. return true
  3241. end
  3242. -- feed and tame
  3243. self.food = (self.food or 0) + 1
  3244. if self.food >= feed_count then
  3245. self.food = 0
  3246. if breed and self.hornytimer == 0 then
  3247. self.horny = true
  3248. end
  3249. if tame then
  3250. if self.tamed == false then
  3251. minetest.chat_send_player(clicker:get_player_name(),
  3252. S("@1 has been tamed!",
  3253. self.name:split(":")[2]))
  3254. end
  3255. self.tamed = true
  3256. if not self.owner or self.owner == "" then
  3257. self.owner = clicker:get_player_name()
  3258. end
  3259. end
  3260. -- make sound when fed so many times
  3261. self:mob_sound(self.sounds.random)
  3262. end
  3263. return true
  3264. end
  3265. local item = clicker:get_wielded_item()
  3266. -- if mob has been tamed you can name it with a nametag
  3267. if item:get_name() == "mobs:nametag"
  3268. and clicker:get_player_name() == self.owner then
  3269. local name = clicker:get_player_name()
  3270. -- store mob and nametag stack in external variables
  3271. mob_obj[name] = self
  3272. mob_sta[name] = item
  3273. local tag = self.nametag or ""
  3274. minetest.show_formspec(name, "mobs_nametag",
  3275. "size[8,4]"
  3276. .. "field[0.5,1;7.5,0;name;"
  3277. .. minetest.formspec_escape(S("Enter name:"))
  3278. .. ";" .. tag .. "]"
  3279. .. "button_exit[2.5,3.5;3,1;mob_rename;"
  3280. .. minetest.formspec_escape(S("Rename")) .. "]")
  3281. end
  3282. return false
  3283. end
  3284. -- inspired by blockmen's nametag mod
  3285. minetest.register_on_player_receive_fields(function(player, formname, fields)
  3286. -- right-clicked with nametag and name entered?
  3287. if formname == "mobs_nametag"
  3288. and fields.name
  3289. and fields.name ~= "" then
  3290. local name = player:get_player_name()
  3291. if not mob_obj[name]
  3292. or not mob_obj[name].object then
  3293. return
  3294. end
  3295. -- make sure nametag is being used to name mob
  3296. local item = player:get_wielded_item()
  3297. if item:get_name() ~= "mobs:nametag" then
  3298. return
  3299. end
  3300. -- limit name entered to 64 characters long
  3301. if fields.name:len() > 64 then
  3302. fields.name = fields.name:sub(1, 64)
  3303. end
  3304. -- update nametag
  3305. mob_obj[name].nametag = fields.name
  3306. mob_obj[name]:update_tag()
  3307. -- if not in creative then take item
  3308. if not mobs.is_creative(name) then
  3309. mob_sta[name]:take_item()
  3310. player:set_wielded_item(mob_sta[name])
  3311. end
  3312. -- reset external variables
  3313. mob_obj[name] = nil
  3314. mob_sta[name] = nil
  3315. end
  3316. end)
  3317. -- compatibility function for old entities to new modpack entities
  3318. function mobs:alias_mob(old_name, new_name)
  3319. -- check old_name entity doesnt already exist
  3320. if minetest.registered_entities[old_name] then
  3321. return
  3322. end
  3323. -- spawn egg
  3324. minetest.register_alias(old_name, new_name)
  3325. -- entity
  3326. minetest.register_entity(":" .. old_name, {
  3327. physical = false,
  3328. on_activate = function(self, staticdata)
  3329. if minetest.registered_entities[new_name] then
  3330. minetest.add_entity(self.object:get_pos(),
  3331. new_name, staticdata)
  3332. end
  3333. remove_mob(self)
  3334. end,
  3335. get_staticdata = function(self)
  3336. return self
  3337. end
  3338. })
  3339. end