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.

4648 lines
101KB

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