Guest User

Untitled

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