api_old.lua 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837
  1. -- Mobs Api (15th April 2016)
  2. mobs = {}
  3. mobs.mod = "redo"
  4. -- Load settings
  5. local damage_enabled = minetest.setting_getbool("enable_damage")
  6. local peaceful_only = minetest.setting_getbool("only_peaceful_mobs")
  7. local disable_blood = minetest.setting_getbool("mobs_disable_blood")
  8. local creative = minetest.setting_getbool("creative_mode")
  9. local spawn_protected = tonumber(minetest.setting_get("mobs_spawn_protected")) or 1
  10. local remove_far = minetest.setting_getbool("remove_far_mobs")
  11. -- pathfinding settings
  12. local enable_pathfinding = true
  13. local enable_pathfind_digging = false
  14. local stuck_timeout = 3 -- how long before mob gets stuck in place and starts searching
  15. local stuck_path_timeout = 10 -- how long will mob follow path before giving up
  16. -- internal functions
  17. local pi = math.pi
  18. local square = math.sqrt
  19. local atan = function(x)
  20. if x ~= x then
  21. --error("atan bassed NaN")
  22. print ("atan based NaN")
  23. return 0
  24. else
  25. return math.atan(x)
  26. end
  27. end
  28. do_attack = function(self, player)
  29. if self.state ~= "attack" then
  30. if math.random(0,100) < 90
  31. and self.sounds.war_cry then
  32. minetest.sound_play(self.sounds.war_cry,{
  33. object = self.object,
  34. max_hear_distance = self.sounds.distance
  35. })
  36. end
  37. self.state = "attack"
  38. self.attack = player
  39. end
  40. end
  41. set_velocity = function(self, v)
  42. v = v or 0
  43. local yaw = (self.object:getyaw() + self.rotate) or 0
  44. self.object:setvelocity({
  45. x = math.sin(yaw) * -v,
  46. y = self.object:getvelocity().y,
  47. z = math.cos(yaw) * v
  48. })
  49. end
  50. get_velocity = function(self)
  51. local v = self.object:getvelocity()
  52. return (v.x * v.x + v.z * v.z) ^ 0.5
  53. end
  54. set_animation = function(self, type)
  55. if not self.animation then
  56. return
  57. end
  58. self.animation.current = self.animation.current or ""
  59. self.animation.speed_normal = self.animation.speed_normal or 15
  60. if type == "stand"
  61. and self.animation.current ~= "stand" then
  62. if self.animation.stand_start
  63. and self.animation.stand_end
  64. and self.animation.speed_normal then
  65. self.object:set_animation({
  66. x = self.animation.stand_start,
  67. y = self.animation.stand_end},
  68. self.animation.speed_normal, 0)
  69. self.animation.current = "stand"
  70. end
  71. elseif type == "walk"
  72. and self.animation.current ~= "walk" then
  73. if self.animation.walk_start
  74. and self.animation.walk_end
  75. and self.animation.speed_normal then
  76. self.object:set_animation({
  77. x = self.animation.walk_start,
  78. y = self.animation.walk_end},
  79. self.animation.speed_normal, 0)
  80. self.animation.current = "walk"
  81. end
  82. elseif type == "run"
  83. and self.animation.current ~= "run" then
  84. if self.animation.run_start
  85. and self.animation.run_end
  86. and self.animation.speed_run then
  87. self.object:set_animation({
  88. x = self.animation.run_start,
  89. y = self.animation.run_end},
  90. (self.animation.speed_run or self.animation.speed_normal), 0)
  91. self.animation.current = "run"
  92. end
  93. elseif type == "punch"
  94. and self.animation.current ~= "punch" then
  95. if self.animation.punch_start
  96. and self.animation.punch_end
  97. and self.animation.speed_normal then
  98. self.object:set_animation({
  99. x = self.animation.punch_start,
  100. y = self.animation.punch_end},
  101. (self.animation.speed_punch or self.animation.speed_normal), 0)
  102. self.animation.current = "punch"
  103. end
  104. end
  105. end
  106. -- check line of sight for walkers and swimmers alike
  107. function line_of_sight_water(self, pos1, pos2, stepsize)
  108. local s, pos_w = minetest.line_of_sight(pos1, pos2, stepsize)
  109. -- normal walking and flying mobs can see you through air
  110. if s == true then
  111. return true
  112. end
  113. -- swimming mobs can see you through water
  114. if s == false
  115. and self.fly
  116. and self.fly_in == "default:water_source" then
  117. local nod = minetest.get_node(pos_w).name
  118. if nod == "default:water_source"
  119. or nod == "default:water_flowing" then
  120. return true
  121. end
  122. end
  123. return false
  124. end
  125. -- particle effects
  126. function effect(pos, amount, texture, max_size, radius)
  127. radius = radius or 2
  128. minetest.add_particlespawner({
  129. amount = amount,
  130. time = 0.25,
  131. minpos = pos,
  132. maxpos = pos,
  133. minvel = {x = -radius, y = -radius, z = -radius},
  134. maxvel = {x = radius, y = radius, z = radius},
  135. minacc = {x = -radius, y = -radius, z = -radius},
  136. maxacc = {x = radius, y = radius, z = radius},
  137. minexptime = 0.1,
  138. maxexptime = 1,
  139. minsize = 0.5,
  140. maxsize = (max_size or 1),
  141. texture = texture,
  142. })
  143. end
  144. -- update nametag colour
  145. function update_tag(self)
  146. local col = "#00FF00"
  147. local qua = self.hp_max / 4
  148. if self.health <= math.floor(qua * 3) then
  149. col = "#FFFF00"
  150. end
  151. if self.health <= math.floor(qua * 2) then
  152. col = "#FF6600"
  153. end
  154. if self.health <= math.floor(qua) then
  155. col = "#FF0000"
  156. end
  157. self.object:set_properties({
  158. nametag = self.nametag,
  159. nametag_color = col
  160. })
  161. end
  162. -- check if mob is dead or only hurt
  163. function check_for_death(self)
  164. -- has health actually changed?
  165. if self.health == self.old_health then
  166. return
  167. end
  168. self.old_health = self.health
  169. -- still got some health? play hurt sound
  170. if self.health > 0 then
  171. if self.sounds.damage then
  172. minetest.sound_play(self.sounds.damage,{
  173. object = self.object,
  174. gain = 1.0,
  175. max_hear_distance = self.sounds.distance
  176. })
  177. end
  178. -- make sure health isn't higher than max
  179. if self.health > self.hp_max then
  180. self.health = self.hp_max
  181. end
  182. update_tag(self)
  183. return false
  184. end
  185. -- drop items when dead
  186. local obj
  187. local pos = self.object:getpos()
  188. for _,drop in pairs(self.drops) do
  189. if math.random(1, drop.chance) == 1 then
  190. obj = minetest.add_item(pos,
  191. ItemStack(drop.name .. " "
  192. .. math.random(drop.min, drop.max)))
  193. if obj then
  194. obj:setvelocity({
  195. x = math.random(-1, 1),
  196. y = 6,
  197. z = math.random(-1, 1)
  198. })
  199. end
  200. end
  201. end
  202. -- play death sound
  203. if self.sounds.death then
  204. minetest.sound_play(self.sounds.death,{
  205. object = self.object,
  206. gain = 1.0,
  207. max_hear_distance = self.sounds.distance
  208. })
  209. end
  210. -- execute custom death function
  211. if self.on_die then
  212. self.on_die(self, pos)
  213. end
  214. self.object:remove()
  215. return true
  216. end
  217. -- check if within map limits (-30911 to 30927)
  218. function within_limits(pos, radius)
  219. if (pos.x - radius) > -30913
  220. and (pos.x + radius) < 30928
  221. and (pos.y - radius) > -30913
  222. and (pos.y + radius) < 30928
  223. and (pos.z - radius) > -30913
  224. and (pos.z + radius) < 30928 then
  225. return true -- within limits
  226. end
  227. return false -- beyond limits
  228. end
  229. -- is mob facing a cliff
  230. local function is_at_cliff(self)
  231. if self.fear_height == 0 then -- if 0, no falling protection!
  232. return false
  233. end
  234. local yaw = self.object:getyaw()
  235. local dir_x = -math.sin(yaw) * (self.collisionbox[4] + 0.5)
  236. local dir_z = math.cos(yaw) * (self.collisionbox[4] + 0.5)
  237. local pos = self.object:getpos()
  238. local ypos = pos.y + self.collisionbox[2] -- just above floor
  239. if minetest.line_of_sight(
  240. {x = pos.x + dir_x, y = ypos, z = pos.z + dir_z},
  241. {x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z}
  242. , 1) then
  243. return true
  244. end
  245. return false
  246. end
  247. -- get node but use fallback for nil or unknown
  248. local function node_ok(pos, fallback)
  249. fallback = fallback or "default:dirt"
  250. local node = minetest.get_node_or_nil(pos)
  251. if not node then
  252. return minetest.registered_nodes[fallback]
  253. end
  254. if minetest.registered_nodes[node.name] then
  255. return node
  256. end
  257. return minetest.registered_nodes[fallback]
  258. end
  259. -- environmental damage (water, lava, fire, light)
  260. do_env_damage = function(self)
  261. -- feed/tame text timer (so mob 'full' messages dont spam chat)
  262. if self.htimer > 0 then
  263. self.htimer = self.htimer - 1
  264. end
  265. local pos = self.object:getpos()
  266. self.time_of_day = minetest.get_timeofday()
  267. -- remove mob if beyond map limits
  268. if not within_limits(pos, 0) then
  269. self.object:remove()
  270. return
  271. end
  272. -- daylight above ground
  273. if self.light_damage ~= 0
  274. and pos.y > 0
  275. and self.time_of_day > 0.2
  276. and self.time_of_day < 0.8
  277. and (minetest.get_node_light(pos) or 0) > 12 then
  278. self.health = self.health - self.light_damage
  279. effect(pos, 5, "tnt_smoke.png")
  280. end
  281. -- what is mob standing in?
  282. pos.y = pos.y + self.collisionbox[2] + 0.1 -- foot level
  283. self.standing_in = node_ok(pos, "air").name
  284. --print ("standing in " .. self.standing_in)
  285. if self.water_damage ~= 0
  286. or self.lava_damage ~= 0 then
  287. local nodef = minetest.registered_nodes[self.standing_in]
  288. pos.y = pos.y + 1
  289. -- water
  290. if self.water_damage ~= 0
  291. and nodef.groups.water then
  292. self.health = self.health - self.water_damage
  293. effect(pos, 5, "bubble.png")
  294. end
  295. -- lava or fire
  296. if self.lava_damage ~= 0
  297. and (nodef.groups.lava
  298. or self.standing_in == "fire:basic_flame"
  299. or self.standing_in == "fire:permanent_flame") then
  300. self.health = self.health - self.lava_damage
  301. effect(pos, 5, "fire_basic_flame.png")
  302. end
  303. end
  304. check_for_death(self)
  305. end
  306. -- jump if facing a solid node (not fences)
  307. do_jump = function(self)
  308. if self.fly
  309. or self.child then
  310. return
  311. end
  312. local pos = self.object:getpos()
  313. -- what is mob standing on?
  314. pos.y = pos.y + self.collisionbox[2] - 0.2
  315. local nod = node_ok(pos)
  316. --print ("standing on:", nod.name, pos.y)
  317. if minetest.registered_nodes[nod.name].walkable == false then
  318. return
  319. end
  320. -- where is front
  321. local yaw = self.object:getyaw()
  322. local dir_x = -math.sin(yaw) * (self.collisionbox[4] + 0.5)
  323. local dir_z = math.cos(yaw) * (self.collisionbox[4] + 0.5)
  324. -- what is in front of mob?
  325. local nod = node_ok({
  326. x = pos.x + dir_x,
  327. y = pos.y + 0.5,
  328. z = pos.z + dir_z
  329. })
  330. -- thin blocks that do not need to be jumped
  331. if nod.name == "default:snow" then
  332. return
  333. end
  334. --print ("in front:", nod.name, pos.y + 0.5)
  335. if (minetest.registered_items[nod.name].walkable
  336. and not nod.name:find("fence")
  337. and not nod.name:find("gate"))
  338. or self.walk_chance == 0 then
  339. local v = self.object:getvelocity()
  340. v.y = self.jump_height + 1
  341. v.x = v.x * 2.2
  342. v.z = v.z * 2.2
  343. self.object:setvelocity(v)
  344. if self.sounds.jump then
  345. minetest.sound_play(self.sounds.jump, {
  346. object = self.object,
  347. gain = 1.0,
  348. max_hear_distance = self.sounds.distance
  349. })
  350. end
  351. else
  352. if self.state ~= "attack" then
  353. self.state = "stand"
  354. set_animation(self, "stand")
  355. end
  356. end
  357. end
  358. -- this is a faster way to calculate distance
  359. local get_distance = function(a, b)
  360. local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z
  361. return square(x * x + y * y + z * z)
  362. end
  363. -- blast damage to entities nearby (modified from TNT mod)
  364. function entity_physics(pos, radius)
  365. radius = radius * 2
  366. local objs = minetest.get_objects_inside_radius(pos, radius)
  367. local obj_pos, dist
  368. for _, obj in pairs(objs) do
  369. obj_pos = obj:getpos()
  370. dist = math.max(1, get_distance(pos, obj_pos))
  371. local damage = math.floor((4 / dist) * radius)
  372. local ent = obj:get_luaentity()
  373. if obj:is_player() then
  374. obj:set_hp(obj:get_hp() - damage)
  375. else --if ent.health then
  376. obj:punch(obj, 1.0, {
  377. full_punch_interval = 1.0,
  378. damage_groups = {fleshy = damage},
  379. }, nil)
  380. end
  381. end
  382. end
  383. -- should mob follow what I'm holding ?
  384. function follow_holding(self, clicker)
  385. local item = clicker:get_wielded_item()
  386. local t = type(self.follow)
  387. -- single item
  388. if t == "string"
  389. and item:get_name() == self.follow then
  390. return true
  391. -- multiple items
  392. elseif t == "table" then
  393. for no = 1, #self.follow do
  394. if self.follow[no] == item:get_name() then
  395. return true
  396. end
  397. end
  398. end
  399. return false
  400. end
  401. local function breed(self)
  402. -- child take 240 seconds before growing into adult
  403. if self.child == true then
  404. self.hornytimer = self.hornytimer + 1
  405. if self.hornytimer > 240 then
  406. self.child = false
  407. self.hornytimer = 0
  408. self.object:set_properties({
  409. textures = self.base_texture,
  410. mesh = self.base_mesh,
  411. visual_size = self.base_size,
  412. collisionbox = self.base_colbox,
  413. })
  414. -- jump when fully grown so not to fall into ground
  415. self.object:setvelocity({
  416. x = 0,
  417. y = self.jump_height,
  418. z = 0
  419. })
  420. end
  421. return
  422. end
  423. -- horny animal can mate for 40 seconds,
  424. -- afterwards horny animal cannot mate again for 200 seconds
  425. if self.horny == true
  426. and self.hornytimer < 240 then
  427. self.hornytimer = self.hornytimer + 1
  428. if self.hornytimer >= 240 then
  429. self.hornytimer = 0
  430. self.horny = false
  431. end
  432. end
  433. -- find another same animal who is also horny and mate if close enough
  434. if self.horny == true
  435. and self.hornytimer <= 40 then
  436. local pos = self.object:getpos()
  437. effect({x = pos.x, y = pos.y + 1, z = pos.z}, 4, "heart.png")
  438. local ents = minetest.get_objects_inside_radius(pos, 3)
  439. local num = 0
  440. local ent = nil
  441. for i, obj in pairs(ents) do
  442. ent = obj:get_luaentity()
  443. -- check for same animal with different colour
  444. local canmate = false
  445. if ent then
  446. if ent.name == self.name then
  447. canmate = true
  448. else
  449. local entname = string.split(ent.name,":")
  450. local selfname = string.split(self.name,":")
  451. if entname[1] == selfname[1] then
  452. entname = string.split(entname[2],"_")
  453. selfname = string.split(selfname[2],"_")
  454. if entname[1] == selfname[1] then
  455. canmate = true
  456. end
  457. end
  458. end
  459. end
  460. if ent
  461. and canmate == true
  462. and ent.horny == true
  463. and ent.hornytimer <= 40 then
  464. num = num + 1
  465. end
  466. -- found your mate? then have a baby
  467. if num > 1 then
  468. self.hornytimer = 41
  469. ent.hornytimer = 41
  470. -- spawn baby
  471. minetest.after(5, function(dtime)
  472. local mob = minetest.add_entity(pos, self.name)
  473. local ent2 = mob:get_luaentity()
  474. local textures = self.base_texture
  475. if self.child_texture then
  476. textures = self.child_texture[1]
  477. end
  478. mob:set_properties({
  479. textures = textures,
  480. visual_size = {
  481. x = self.base_size.x / 2,
  482. y = self.base_size.y / 2
  483. },
  484. collisionbox = {
  485. self.base_colbox[1] / 2,
  486. self.base_colbox[2] / 2,
  487. self.base_colbox[3] / 2,
  488. self.base_colbox[4] / 2,
  489. self.base_colbox[5] / 2,
  490. self.base_colbox[6] / 2
  491. },
  492. })
  493. ent2.child = true
  494. ent2.tamed = true
  495. ent2.owner = self.owner
  496. end)
  497. num = 0
  498. break
  499. end
  500. end
  501. end
  502. end
  503. function replace(self, pos)
  504. if self.replace_rate
  505. and self.child == false
  506. and math.random(1, self.replace_rate) == 1 then
  507. local pos = self.object:getpos()
  508. pos.y = pos.y + self.replace_offset
  509. -- print ("replace node = ".. minetest.get_node(pos).name, pos.y)
  510. if self.replace_what
  511. and self.replace_with
  512. and self.object:getvelocity().y == 0
  513. and #minetest.find_nodes_in_area(pos, pos, self.replace_what) > 0 then
  514. minetest.set_node(pos, {name = self.replace_with})
  515. -- when cow/sheep eats grass, replace wool and milk
  516. if self.gotten == true then
  517. self.gotten = false
  518. self.object:set_properties(self)
  519. end
  520. end
  521. end
  522. end
  523. -- check if daytime and also if mob is docile during daylight hours
  524. function day_docile(self)
  525. if self.docile_by_day == false then
  526. return false
  527. elseif self.docile_by_day == true
  528. and self.time_of_day > 0.2
  529. and self.time_of_day < 0.8 then
  530. return true
  531. end
  532. end
  533. -- path finding and smart mob routine by rnd
  534. function smart_mobs(self, s, p, dist, dtime)
  535. local s1 = self.path.lastpos
  536. -- is it becoming stuck?
  537. if math.abs(s1.x - s.x) + math.abs(s1.z - s.z) < 1.5 then
  538. self.path.stuck_timer = self.path.stuck_timer + dtime
  539. else
  540. self.path.stuck_timer = 0
  541. end
  542. self.path.lastpos = {x = s.x, y = s.y, z = s.z}
  543. -- im stuck, search for path
  544. if (self.path.stuck_timer > stuck_timeout and not self.path.following)
  545. or (self.path.stuck_timer > stuck_path_timeout
  546. and self.path.following) then
  547. self.path.stuck_timer = 0
  548. -- lets try find a path, first take care of positions
  549. -- since pathfinder is very sensitive
  550. local sheight = self.collisionbox[5] - self.collisionbox[2]
  551. -- round position to center of node to avoid stuck in walls
  552. -- also adjust height for player models!
  553. s.x = math.floor(s.x + 0.5)
  554. s.y = math.floor(s.y + 0.5) - sheight
  555. s.z = math.floor(s.z + 0.5)
  556. local ssight, sground
  557. ssight, sground = minetest.line_of_sight(s, {
  558. x = s.x, y = s.y - 4, z = s.z}, 1)
  559. -- determine node above ground
  560. if not ssight then
  561. s.y = sground.y + 1
  562. end
  563. local p1 = self.attack:getpos()
  564. p1.x = math.floor(p1.x + 0.5)
  565. p1.y = math.floor(p1.y + 0.5)
  566. p1.z = math.floor(p1.z + 0.5)
  567. self.path.way = minetest.find_path(s, p1, 16, 2, 6, "Dijkstra") --"A*_noprefetch")
  568. -- attempt to unstick mob that is "daydreaming"
  569. self.object:setpos({
  570. x = s.x + 0.1 * (math.random() * 2 - 1),
  571. y = s.y + 1,
  572. z = s.z + 0.1 * (math.random() * 2 - 1)
  573. })
  574. self.state = ""
  575. do_attack(self, self.attack)
  576. -- no path found, try something else
  577. if not self.path.way then
  578. self.path.following = false
  579. -- self.path.stuck = true
  580. -- lets make way by digging/building if not accessible
  581. if enable_pathfind_digging then
  582. -- add block and remove one block above so
  583. -- there is room to jump if needed
  584. if s.y < p1.y then
  585. if not minetest.is_protected(s, "") then
  586. minetest.set_node(s, {name = "default:dirt"})
  587. end
  588. local sheight = math.ceil(self.collisionbox[5]) + 1
  589. -- assume mob is 2 blocks high so it digs above its head
  590. s.y = s.y + sheight
  591. if not minetest.is_protected(s, "") then
  592. local node1 = minetest.get_node(s).name
  593. if node1 ~= "air"
  594. and node1 ~= "ignore" then
  595. minetest.set_node(s, {name = "air"})
  596. minetest.add_item(s, ItemStack(node1))
  597. end
  598. end
  599. s.y = s.y - sheight
  600. self.object:setpos({x = s.x, y = s.y + 2, z = s.z})
  601. else -- dig 2 blocks to make door toward player direction
  602. local yaw1 = self.object:getyaw() + pi / 2
  603. local p1 = {
  604. x = s.x + math.cos(yaw1),
  605. y = s.y,
  606. z = s.z + math.sin(yaw1)
  607. }
  608. if not minetest.is_protected(p1, "") then
  609. local node1 = minetest.get_node(p1).name
  610. if node1 ~= "air"
  611. and node1 ~= "ignore" then
  612. minetest.add_item(p1, ItemStack(node1))
  613. minetest.set_node(p1, {name = "air"})
  614. end
  615. p1.y = p1.y + 1
  616. node1 = minetest.get_node(p1).name
  617. if node1 ~= "air"
  618. and node1 ~= "ignore" then
  619. minetest.add_item(p1, ItemStack(node1))
  620. minetest.set_node(p1, {name = "air"})
  621. end
  622. end
  623. end
  624. end
  625. -- will try again in 2 second
  626. self.path.stuck_timer = stuck_timeout - 2
  627. -- frustration! cant find the damn path :(
  628. if self.sounds.random then
  629. minetest.sound_play(self.sounds.random, {
  630. object = self.object,
  631. max_hear_distance = self.sounds.distance
  632. })
  633. end
  634. else
  635. -- yay i found path
  636. if self.sounds.attack then
  637. set_velocity(self, self.walk_velocity)
  638. minetest.sound_play(self.sounds.attack, {
  639. object = self.object,
  640. max_hear_distance = self.sounds.distance
  641. })
  642. end
  643. -- follow path now that it has it
  644. self.path.following = true
  645. end
  646. end
  647. end
  648. mobs.spawning_mobs = {}
  649. -- register mob function
  650. function mobs:register_mob(name, def)
  651. mobs.spawning_mobs[name] = true
  652. minetest.register_entity(name, {
  653. stepheight = def.stepheight or 0.6,
  654. name = name,
  655. type = def.type,
  656. attack_type = def.attack_type,
  657. fly = def.fly,
  658. fly_in = def.fly_in or "air",
  659. owner = def.owner or "",
  660. order = def.order or "",
  661. on_die = def.on_die,
  662. do_custom = def.do_custom,
  663. jump_height = def.jump_height or 6,
  664. jump_chance = def.jump_chance or 0,
  665. drawtype = def.drawtype, -- DEPRECATED, use rotate instead
  666. rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
  667. lifetimer = def.lifetimer or 180, -- 3 minutes
  668. hp_min = def.hp_min or 5,
  669. hp_max = def.hp_max or 10,
  670. physical = true,
  671. collisionbox = def.collisionbox,
  672. visual = def.visual,
  673. visual_size = def.visual_size or {x = 1, y = 1},
  674. mesh = def.mesh,
  675. makes_footstep_sound = def.makes_footstep_sound or false,
  676. view_range = def.view_range or 5,
  677. walk_velocity = def.walk_velocity or 1,
  678. run_velocity = def.run_velocity or 2,
  679. damage = def.damage or 0,
  680. light_damage = def.light_damage or 0,
  681. water_damage = def.water_damage or 0,
  682. lava_damage = def.lava_damage or 0,
  683. fall_damage = def.fall_damage or 1,
  684. fall_speed = def.fall_speed or -10, -- must be lower than -2 (default: -10)
  685. drops = def.drops or {},
  686. armor = def.armor,
  687. on_rightclick = def.on_rightclick,
  688. arrow = def.arrow,
  689. shoot_interval = def.shoot_interval,
  690. sounds = def.sounds or {},
  691. animation = def.animation,
  692. follow = def.follow,
  693. jump = def.jump or true,
  694. walk_chance = def.walk_chance or 50,
  695. attacks_monsters = def.attacks_monsters or false,
  696. group_attack = def.group_attack or false,
  697. --fov = def.fov or 120,
  698. passive = def.passive or false,
  699. recovery_time = def.recovery_time or 0.5,
  700. knock_back = def.knock_back or 3,
  701. blood_amount = def.blood_amount or 5,
  702. blood_texture = def.blood_texture or "mobs_blood.png",
  703. shoot_offset = def.shoot_offset or 0,
  704. floats = def.floats or 1, -- floats in water by default
  705. replace_rate = def.replace_rate,
  706. replace_what = def.replace_what,
  707. replace_with = def.replace_with,
  708. replace_offset = def.replace_offset or 0,
  709. timer = 0,
  710. env_damage_timer = 0, -- only used when state = "attack"
  711. tamed = false,
  712. pause_timer = 0,
  713. horny = false,
  714. hornytimer = 0,
  715. child = false,
  716. gotten = false,
  717. health = 0,
  718. reach = def.reach or 3,
  719. htimer = 0,
  720. child_texture = def.child_texture,
  721. docile_by_day = def.docile_by_day or false,
  722. time_of_day = 0.5,
  723. fear_height = def.fear_height or 0,
  724. runaway = def.runaway,
  725. runaway_timer = 0,
  726. pathfinding = def.pathfinding,
  727. immune_to = def.immune_to or {},
  728. explosion_radius = def.explosion_radius,
  729. on_step = function(self, dtime)
  730. local pos = self.object:getpos()
  731. local yaw = self.object:getyaw() or 0
  732. -- when lifetimer expires remove mob (except npc and tamed)
  733. if self.type ~= "npc"
  734. and not self.tamed
  735. and self.state ~= "attack" then
  736. self.lifetimer = self.lifetimer - dtime
  737. if self.lifetimer <= 0 then
  738. -- only despawn away from player
  739. local objs = minetest.get_objects_inside_radius(pos, 10)
  740. for _,oir in pairs(objs) do
  741. if oir:is_player() then
  742. self.lifetimer = 20
  743. return
  744. end
  745. end
  746. minetest.log("action",
  747. "lifetimer expired, removed " .. self.name)
  748. effect(pos, 15, "tnt_smoke.png")
  749. self.object:remove()
  750. return
  751. end
  752. end
  753. if not self.fly then
  754. -- floating in water (or falling)
  755. local v = self.object:getvelocity()
  756. -- going up then apply gravity
  757. if v.y > 0.1 then
  758. self.object:setacceleration({
  759. x = 0,
  760. y = self.fall_speed,
  761. z = 0
  762. })
  763. end
  764. -- in water then float up
  765. if minetest.registered_nodes[node_ok(pos).name].groups.liquid then -- water then
  766. if self.floats == 1 then
  767. self.object:setacceleration({
  768. x = 0,
  769. y = -self.fall_speed / (math.max(1, v.y) ^ 2),
  770. z = 0
  771. })
  772. end
  773. else
  774. -- fall downwards
  775. self.object:setacceleration({
  776. x = 0,
  777. y = self.fall_speed,
  778. z = 0
  779. })
  780. -- fall damage
  781. if self.fall_damage == 1
  782. and self.object:getvelocity().y == 0 then
  783. local d = self.old_y - self.object:getpos().y
  784. if d > 5 then
  785. --self.object:set_hp(self.object:get_hp() - math.floor(d - 5))
  786. self.health = self.health - math.floor(d - 5)
  787. effect(pos, 5, "tnt_smoke.png")
  788. if check_for_death(self) then
  789. return
  790. end
  791. end
  792. self.old_y = self.object:getpos().y
  793. end
  794. end
  795. end
  796. -- knockback timer
  797. if self.pause_timer > 0 then
  798. self.pause_timer = self.pause_timer - dtime
  799. if self.pause_timer < 1 then
  800. self.pause_timer = 0
  801. end
  802. return
  803. end
  804. -- attack timer
  805. self.timer = self.timer + dtime
  806. if self.state ~= "attack" then
  807. if self.timer < 1 then
  808. return
  809. end
  810. self.timer = 0
  811. end
  812. -- never go over 100
  813. if self.timer > 100 then
  814. self.timer = 1
  815. end
  816. -- node replace check (cow eats grass etc.)
  817. replace(self, pos)
  818. -- mob plays random sound at times
  819. if self.sounds.random
  820. and math.random(1, 100) == 1 then
  821. minetest.sound_play(self.sounds.random, {
  822. object = self.object,
  823. max_hear_distance = self.sounds.distance
  824. })
  825. end
  826. -- environmental damage timer (every 1 second)
  827. self.env_damage_timer = self.env_damage_timer + dtime
  828. if (self.state == "attack" and self.env_damage_timer > 1)
  829. or self.state ~= "attack" then
  830. self.env_damage_timer = 0
  831. do_env_damage(self)
  832. -- custom function (defined in mob lua file)
  833. if self.do_custom then
  834. self.do_custom(self)
  835. end
  836. end
  837. -- find someone to attack
  838. if self.type == "monster"
  839. and damage_enabled
  840. and self.state ~= "attack"
  841. and not day_docile(self) then
  842. local s = self.object:getpos()
  843. local p, sp, dist
  844. local player = nil
  845. local type = nil
  846. local obj = nil
  847. local min_dist = self.view_range + 1
  848. local min_player = nil
  849. for _,oir in pairs(minetest.get_objects_inside_radius(s, self.view_range)) do
  850. if oir:is_player() then
  851. player = oir
  852. type = "player"
  853. else
  854. obj = oir:get_luaentity()
  855. if obj then
  856. player = obj.object
  857. type = obj.type
  858. end
  859. end
  860. if type == "player"
  861. or type == "npc" then
  862. s = self.object:getpos()
  863. p = player:getpos()
  864. sp = s
  865. -- aim higher to make looking up hills more realistic
  866. p.y = p.y + 1
  867. sp.y = sp.y + 1
  868. dist = get_distance(p, s)
  869. if dist < self.view_range then
  870. -- field of view check goes here
  871. -- choose closest player to attack
  872. --if minetest.line_of_sight(sp, p, 2) == true
  873. if line_of_sight_water(self, sp, p, 2) == true
  874. and dist < min_dist then
  875. min_dist = dist
  876. min_player = player
  877. end
  878. end
  879. end
  880. end
  881. -- attack player
  882. if min_player then
  883. do_attack(self, min_player)
  884. end
  885. end
  886. -- npc, find closest monster to attack
  887. local min_dist = self.view_range + 1
  888. local min_player = nil
  889. if self.type == "npc"
  890. and self.attacks_monsters
  891. and self.state ~= "attack" then
  892. local s = self.object:getpos()
  893. local obj = nil
  894. for _, oir in pairs(minetest.get_objects_inside_radius(s, self.view_range)) do
  895. obj = oir:get_luaentity()
  896. if obj
  897. and obj.type == "monster" then
  898. -- attack monster
  899. p = obj.object:getpos()
  900. dist = get_distance(p, s)
  901. if dist < min_dist then
  902. min_dist = dist
  903. min_player = obj.object
  904. end
  905. end
  906. end
  907. if min_player then
  908. do_attack(self, min_player)
  909. end
  910. end
  911. -- breed and grow children
  912. breed(self)
  913. -- find player to follow
  914. if (self.follow ~= ""
  915. or self.order == "follow")
  916. and not self.following
  917. and self.state ~= "attack"
  918. and self.state ~= "runaway" then
  919. local s, p, dist
  920. for _,player in pairs(minetest.get_connected_players()) do
  921. s = self.object:getpos()
  922. p = player:getpos()
  923. dist = get_distance(p, s)
  924. if dist < self.view_range then
  925. self.following = player
  926. break
  927. end
  928. end
  929. end
  930. if self.type == "npc"
  931. and self.order == "follow"
  932. and self.state ~= "attack"
  933. and self.owner ~= "" then
  934. -- npc stop following player if not owner
  935. if self.following
  936. and self.owner
  937. and self.owner ~= self.following:get_player_name() then
  938. self.following = nil
  939. end
  940. else
  941. -- stop following player if not holding specific item
  942. if self.following
  943. and self.following:is_player()
  944. and follow_holding(self, self.following) == false then
  945. self.following = nil
  946. end
  947. end
  948. -- follow that thing
  949. if self.following then
  950. local s = self.object:getpos()
  951. local p
  952. if self.following:is_player() then
  953. p = self.following:getpos()
  954. elseif self.following.object then
  955. p = self.following.object:getpos()
  956. end
  957. if p then
  958. local dist = get_distance(p, s)
  959. -- dont follow if out of range
  960. if dist > self.view_range then
  961. self.following = nil
  962. else
  963. local vec = {
  964. x = p.x - s.x,
  965. y = p.y - s.y,
  966. z = p.z - s.z
  967. }
  968. if vec.x ~= 0
  969. and vec.z ~= 0 then
  970. yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
  971. if p.x > s.x then
  972. yaw = yaw + pi
  973. end
  974. self.object:setyaw(yaw)
  975. end
  976. -- anyone but standing npc's can move along
  977. if dist > self.reach
  978. and self.order ~= "stand" then
  979. if (self.jump
  980. and get_velocity(self) <= 0.5
  981. and self.object:getvelocity().y == 0)
  982. or (self.object:getvelocity().y == 0
  983. and self.jump_chance > 0) then
  984. do_jump(self)
  985. end
  986. set_velocity(self, self.walk_velocity)
  987. if self.walk_chance ~= 0 then
  988. set_animation(self, "walk")
  989. end
  990. else
  991. set_velocity(self, 0)
  992. set_animation(self, "stand")
  993. end
  994. return
  995. end
  996. end
  997. end
  998. -- water swimmers flop when on land
  999. if self.fly
  1000. and self.fly_in == "default:water_source"
  1001. and self.standing_in ~= self.fly_in then
  1002. self.state = "flop"
  1003. self.object:setvelocity({x = 0, y = -5, z = 0})
  1004. set_animation(self, "stand")
  1005. return
  1006. end
  1007. if self.state == "stand" then
  1008. if math.random(1, 4) == 1 then
  1009. local lp = nil
  1010. local s = self.object:getpos()
  1011. if self.type == "npc" then
  1012. local o = minetest.get_objects_inside_radius(self.object:getpos(), 3)
  1013. for _,o in pairs(o) do
  1014. if o:is_player() then
  1015. lp = o:getpos()
  1016. break
  1017. end
  1018. end
  1019. end
  1020. -- look at any players nearby, otherwise turn randomly
  1021. if lp then
  1022. local vec = {
  1023. x = lp.x - s.x,
  1024. y = lp.y - s.y,
  1025. z = lp.z - s.z
  1026. }
  1027. if vec.x ~= 0
  1028. and vec.z ~= 0 then
  1029. yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
  1030. if lp.x > s.x then
  1031. yaw = yaw + pi
  1032. end
  1033. end
  1034. else
  1035. yaw = (math.random(0, 360) - 180) / 180 * pi
  1036. end
  1037. self.object:setyaw(yaw)
  1038. end
  1039. set_velocity(self, 0)
  1040. set_animation(self, "stand")
  1041. -- npc's ordered to stand stay standing
  1042. if self.type ~= "npc"
  1043. or self.order ~= "stand" then
  1044. if self.walk_chance ~= 0
  1045. and math.random(1, 100) <= self.walk_chance
  1046. and is_at_cliff(self) == false then
  1047. set_velocity(self, self.walk_velocity)
  1048. self.state = "walk"
  1049. set_animation(self, "walk")
  1050. end
  1051. end
  1052. elseif self.state == "walk" then
  1053. local s = self.object:getpos()
  1054. local lp = minetest.find_node_near(s, 1, {"group:water"})
  1055. -- if water nearby then turn away
  1056. if lp then
  1057. local vec = {
  1058. x = lp.x - s.x,
  1059. y = lp.y - s.y,
  1060. z = lp.z - s.z
  1061. }
  1062. if vec.x ~= 0
  1063. and vec.z ~= 0 then
  1064. yaw = atan(vec.z / vec.x) + 3 * pi / 2 - self.rotate
  1065. if lp.x > s.x then
  1066. yaw = yaw + pi
  1067. end
  1068. self.object:setyaw(yaw)
  1069. end
  1070. -- otherwise randomly turn
  1071. elseif math.random(1, 100) <= 30 then
  1072. yaw = (math.random(0, 360) - 180) / 180 * pi
  1073. self.object:setyaw(yaw)
  1074. end
  1075. -- stand for great fall in front
  1076. local temp_is_cliff = is_at_cliff(self)
  1077. -- jump when walking comes to a halt
  1078. if temp_is_cliff == false
  1079. and self.jump
  1080. and get_velocity(self) <= 0.5
  1081. and self.object:getvelocity().y == 0 then
  1082. do_jump(self)
  1083. end
  1084. if temp_is_cliff
  1085. or math.random(1, 100) <= 30 then
  1086. set_velocity(self, 0)
  1087. self.state = "stand"
  1088. set_animation(self, "stand")
  1089. else
  1090. set_velocity(self, self.walk_velocity)
  1091. set_animation(self, "walk")
  1092. end
  1093. -- runaway when punched
  1094. elseif self.state == "runaway" then
  1095. self.runaway_timer = self.runaway_timer + 1
  1096. -- stop after 3 seconds or when at cliff
  1097. if self.runaway_timer > 3
  1098. or is_at_cliff(self) then
  1099. self.runaway_timer = 0
  1100. set_velocity(self, 0)
  1101. self.state = "stand"
  1102. set_animation(self, "stand")
  1103. else
  1104. set_velocity(self, self.run_velocity)
  1105. set_animation(self, "walk")
  1106. end
  1107. -- jump when walking comes to a halt
  1108. if self.jump
  1109. and get_velocity(self) <= 0.5
  1110. and self.object:getvelocity().y == 0 then
  1111. do_jump(self)
  1112. end
  1113. -- attack routines (explode, dogfight, shoot, dogshoot)
  1114. elseif self.state == "attack" then
  1115. -- calculate distance from mob and enemy
  1116. local s = self.object:getpos()
  1117. local p = self.attack:getpos() or s
  1118. local dist = get_distance(p, s)
  1119. -- stop attacking if player or out of range
  1120. if dist > self.view_range
  1121. or not self.attack
  1122. or not self.attack:getpos()
  1123. or self.attack:get_hp() <= 0 then
  1124. --print(" ** stop attacking **", dist, self.view_range)
  1125. self.state = "stand"
  1126. set_velocity(self, 0)
  1127. set_animation(self, "stand")
  1128. self.attack = nil
  1129. self.v_start = false
  1130. self.timer = 0
  1131. self.blinktimer = 0
  1132. return
  1133. end
  1134. if self.attack_type == "explode" then
  1135. local vec = {
  1136. x = p.x - s.x,
  1137. y = p.y - s.y,
  1138. z = p.z - s.z
  1139. }
  1140. if vec.x ~= 0
  1141. and vec.z ~= 0 then
  1142. yaw = atan(vec.z / vec.x) + pi / 2 - self.rotate
  1143. if p.x > s.x then
  1144. yaw = yaw + pi
  1145. end
  1146. self.object:setyaw(yaw)
  1147. end
  1148. if dist > self.reach then
  1149. if not self.v_start then
  1150. self.v_start = true
  1151. set_velocity(self, self.run_velocity)
  1152. self.timer = 0
  1153. self.blinktimer = 0
  1154. else
  1155. self.timer = 0
  1156. self.blinktimer = 0
  1157. if get_velocity(self) <= 0.5
  1158. and self.object:getvelocity().y == 0 then
  1159. local v = self.object:getvelocity()
  1160. v.y = 5
  1161. self.object:setvelocity(v)
  1162. end
  1163. set_velocity(self, self.run_velocity)
  1164. end
  1165. set_animation(self, "run")
  1166. else
  1167. set_velocity(self, 0)
  1168. self.timer = self.timer + dtime
  1169. self.blinktimer = (self.blinktimer or 0) + dtime
  1170. if self.blinktimer > 0.2 then
  1171. self.blinktimer = 0
  1172. if self.blinkstatus then
  1173. self.object:settexturemod("")
  1174. else
  1175. self.object:settexturemod("^[brighten")
  1176. end
  1177. self.blinkstatus = not self.blinkstatus
  1178. end
  1179. if self.timer > 3 then
  1180. local pos = self.object:getpos()
  1181. local radius = self.explosion_radius or 1
  1182. -- hurt player/mobs caught in blast area
  1183. entity_physics(pos, radius)
  1184. -- dont damage anything if area protected or next to water
  1185. if minetest.find_node_near(pos, 1, {"group:water"})
  1186. or minetest.is_protected(pos, "") then
  1187. if self.sounds.explode then
  1188. minetest.sound_play(self.sounds.explode, {
  1189. object = self.object,
  1190. gain = 1.0,
  1191. max_hear_distance = 16
  1192. })
  1193. end
  1194. self.object:remove()
  1195. effect(pos, 15, "tnt_smoke.png", 5)
  1196. return
  1197. end
  1198. pos.y = pos.y - 1
  1199. mobs:explosion(pos, radius, 0, 1, self.sounds.explode)
  1200. self.object:remove()
  1201. return
  1202. end
  1203. end
  1204. elseif self.attack_type == "dogfight"
  1205. or (self.attack_type == "dogshoot" and dist <= self.reach) then
  1206. if self.fly
  1207. and dist > self.reach then
  1208. local nod = node_ok(s)
  1209. local p1 = s
  1210. local me_y = math.floor(p1.y)
  1211. local p2 = p
  1212. local p_y = math.floor(p2.y + 1)
  1213. local v = self.object:getvelocity()
  1214. if nod.name == self.fly_in then
  1215. if me_y < p_y then
  1216. self.object:setvelocity({
  1217. x = v.x,
  1218. y = 1 * self.walk_velocity,
  1219. z = v.z
  1220. })
  1221. elseif me_y > p_y then
  1222. self.object:setvelocity({
  1223. x = v.x,
  1224. y = -1 * self.walk_velocity,
  1225. z = v.z
  1226. })
  1227. end
  1228. else
  1229. if me_y < p_y then
  1230. self.object:setvelocity({
  1231. x = v.x,
  1232. y = 0.01,
  1233. z = v.z
  1234. })
  1235. elseif me_y > p_y then
  1236. self.object:setvelocity({
  1237. x = v.x,
  1238. y = -0.01,
  1239. z = v.z
  1240. })
  1241. end
  1242. end
  1243. end
  1244. -- rnd: new movement direction
  1245. if self.path.following
  1246. and self.path.way
  1247. and self.attack_type ~= "dogshoot" then
  1248. -- no paths longer than 50
  1249. if #self.path.way > 50
  1250. or dist < self.reach then
  1251. self.path.following = false
  1252. return
  1253. end
  1254. local p1 = self.path.way[1]
  1255. if not p1 then
  1256. self.path.following = false
  1257. return
  1258. end
  1259. if math.abs(p1.x-s.x) + math.abs(p1.z - s.z) < 0.6 then
  1260. -- reached waypoint, remove it from queue
  1261. table.remove(self.path.way, 1)
  1262. end
  1263. -- set new temporary target
  1264. p = {x = p1.x, y = p1.y, z = p1.z}
  1265. end
  1266. local vec = {
  1267. x = p.x - s.x,
  1268. y = p.y - s.y,
  1269. z = p.z - s.z
  1270. }
  1271. if vec.x ~= 0
  1272. and vec.z ~= 0 then
  1273. yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
  1274. if p.x > s.x then
  1275. yaw = yaw + pi
  1276. end
  1277. self.object:setyaw(yaw)
  1278. end
  1279. -- move towards enemy if beyond mob reach
  1280. if dist > self.reach then
  1281. -- path finding by rnd
  1282. if self.pathfinding -- only if mob has pathfinding enabled
  1283. and enable_pathfinding then
  1284. smart_mobs(self, s, p, dist, dtime)
  1285. end
  1286. -- jump attack
  1287. if (self.jump
  1288. and get_velocity(self) <= 0.5
  1289. and self.object:getvelocity().y == 0)
  1290. or (self.object:getvelocity().y == 0
  1291. and self.jump_chance > 0) then
  1292. do_jump(self)
  1293. end
  1294. if is_at_cliff(self) then
  1295. set_velocity(self, 0)
  1296. set_animation(self, "stand")
  1297. else
  1298. if self.path.stuck then
  1299. set_velocity(self, self.walk_velocity)
  1300. else
  1301. set_velocity(self, self.run_velocity)
  1302. end
  1303. set_animation(self, "run")
  1304. end
  1305. else -- rnd: if inside reach range
  1306. self.path.stuck = false
  1307. self.path.stuck_timer = 0
  1308. self.path.following = false -- not stuck anymore
  1309. set_velocity(self, 0)
  1310. set_animation(self, "punch")
  1311. if self.timer > 1 then
  1312. self.timer = 0
  1313. local p2 = p
  1314. local s2 = s
  1315. p2.y = p2.y + 1.5
  1316. s2.y = s2.y + 1.5
  1317. --if minetest.line_of_sight(p2, s2) == true then
  1318. if line_of_sight_water(self, p2, s2) == true then
  1319. -- play attack sound
  1320. if self.sounds.attack then
  1321. minetest.sound_play(self.sounds.attack, {
  1322. object = self.object,
  1323. max_hear_distance = self.sounds.distance
  1324. })
  1325. end
  1326. -- punch player
  1327. self.attack:punch(self.object, 1.0, {
  1328. full_punch_interval = 1.0,
  1329. damage_groups = {fleshy = self.damage}
  1330. }, nil)
  1331. end
  1332. end
  1333. end
  1334. elseif self.attack_type == "shoot"
  1335. or (self.attack_type == "dogshoot" and dist > self.reach) then
  1336. p.y = p.y - .5
  1337. s.y = s.y + .5
  1338. local dist = get_distance(p, s)
  1339. local vec = {
  1340. x = p.x - s.x,
  1341. y = p.y - s.y,
  1342. z = p.z - s.z
  1343. }
  1344. if vec.x ~= 0
  1345. and vec.z ~= 0 then
  1346. yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
  1347. if p.x > s.x then
  1348. yaw = yaw + pi
  1349. end
  1350. self.object:setyaw(yaw)
  1351. end
  1352. set_velocity(self, 0)
  1353. if self.shoot_interval
  1354. and self.timer > self.shoot_interval
  1355. and math.random(1, 100) <= 60 then
  1356. self.timer = 0
  1357. set_animation(self, "punch")
  1358. -- play shoot attack sound
  1359. if self.sounds.shoot_attack then
  1360. minetest.sound_play(self.sounds.shoot_attack, {
  1361. object = self.object,
  1362. max_hear_distance = self.sounds.distance
  1363. })
  1364. end
  1365. local p = self.object:getpos()
  1366. p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
  1367. local obj = minetest.add_entity(p, self.arrow)
  1368. local ent = obj:get_luaentity()
  1369. local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5
  1370. local v = ent.velocity or 1 -- or set to default
  1371. ent.switch = 1
  1372. -- offset makes shoot aim accurate
  1373. vec.y = vec.y + self.shoot_offset
  1374. vec.x = vec.x * (v / amount)
  1375. vec.y = vec.y * (v / amount)
  1376. vec.z = vec.z * (v / amount)
  1377. obj:setvelocity(vec)
  1378. end
  1379. end
  1380. end -- END if self.state == "attack"
  1381. end,
  1382. on_punch = function(self, hitter, tflp, tool_capabilities, dir)
  1383. -- direction error check
  1384. dir = dir or {x = 0, y = 0, z = 0}
  1385. -- weapon wear
  1386. local weapon = hitter:get_wielded_item()
  1387. local punch_interval = 1.4
  1388. -- calculate mob damage
  1389. local damage = 0
  1390. local armor = self.object:get_armor_groups() or {}
  1391. local tmp
  1392. -- quick error check incase it ends up 0 (serialize.h check test)
  1393. if tflp == 0 then
  1394. tflp = 0.2
  1395. end
  1396. for group,_ in pairs(tool_capabilities.damage_groups) do
  1397. tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)
  1398. if tmp < 0 then
  1399. tmp = 0.0
  1400. elseif tmp > 1 then
  1401. tmp = 1.0
  1402. end
  1403. damage = damage + (tool_capabilities.damage_groups[group] or 0)
  1404. * tmp * ((armor[group] or 0) / 100.0)
  1405. end
  1406. -- check for tool immunity or special damage
  1407. for _, no in pairs(self.immune_to) do
  1408. if no[1] == weapon:get_name() then
  1409. damage = no[2] or 0
  1410. break
  1411. end
  1412. end
  1413. -- print ("Mob Damage is", damage)
  1414. -- add weapon wear
  1415. if tool_capabilities then
  1416. punch_interval = tool_capabilities.full_punch_interval or 1.4
  1417. end
  1418. if weapon:get_definition()
  1419. and weapon:get_definition().tool_capabilities then
  1420. weapon:add_wear(math.floor((punch_interval / 75) * 9000))
  1421. hitter:set_wielded_item(weapon)
  1422. end
  1423. -- weapon sounds
  1424. if weapon:get_definition().sounds ~= nil then
  1425. local s = math.random(0, #weapon:get_definition().sounds)
  1426. minetest.sound_play(weapon:get_definition().sounds[s], {
  1427. object = hitter,
  1428. max_hear_distance = 8
  1429. })
  1430. else
  1431. minetest.sound_play("default_punch", {
  1432. object = hitter,
  1433. max_hear_distance = 5
  1434. })
  1435. end
  1436. -- do damage
  1437. self.health = self.health - math.floor(damage)
  1438. -- exit here if dead
  1439. if check_for_death(self) then
  1440. return
  1441. end
  1442. -- add healthy afterglow when hit
  1443. core.after(0.1, function()
  1444. self.object:settexturemod("^[colorize:#c9900070")
  1445. core.after(0.3, function()
  1446. self.object:settexturemod("")
  1447. end)
  1448. end)
  1449. -- blood_particles
  1450. if self.blood_amount > 0
  1451. and not disable_blood then
  1452. local pos = self.object:getpos()
  1453. pos.y = pos.y + (-self.collisionbox[2] + self.collisionbox[5]) / 2
  1454. effect(pos, self.blood_amount, self.blood_texture)
  1455. end
  1456. -- knock back effect (only on full punch)
  1457. if self.knock_back > 0
  1458. and tflp > punch_interval then
  1459. local v = self.object:getvelocity()
  1460. local r = 1.4 - math.min(punch_interval, 1.4)
  1461. local kb = r * 5
  1462. local up = 2
  1463. -- if already in air then dont go up anymore when hit
  1464. if v.y > 0
  1465. or self.fly then
  1466. up = 0
  1467. end
  1468. self.object:setvelocity({
  1469. x = dir.x * kb,
  1470. y = up,
  1471. z = dir.z * kb
  1472. })
  1473. self.pause_timer = r
  1474. end
  1475. -- if skittish then run away
  1476. if self.runaway == true then
  1477. local lp = hitter:getpos()
  1478. local s = self.object:getpos()
  1479. local vec = {
  1480. x = lp.x - s.x,
  1481. y = lp.y - s.y,
  1482. z = lp.z - s.z
  1483. }
  1484. if vec.x ~= 0
  1485. and vec.z ~= 0 then
  1486. local yaw = atan(vec.z / vec.x) + 3 * pi / 2 - self.rotate
  1487. if lp.x > s.x then
  1488. yaw = yaw + pi
  1489. end
  1490. self.object:setyaw(yaw)
  1491. end
  1492. self.state = "runaway"
  1493. self.runaway_timer = 0
  1494. self.following = nil
  1495. end
  1496. -- attack puncher and call other mobs for help
  1497. if self.passive == false
  1498. and self.state ~= "flop"
  1499. and self.child == false
  1500. and hitter:get_player_name() ~= self.owner then
  1501. -- attack whoever punched mob
  1502. self.state = ""
  1503. do_attack(self, hitter)
  1504. -- alert others to the attack
  1505. local obj = nil
  1506. for _, oir in pairs(minetest.get_objects_inside_radius(hitter:getpos(), 5)) do
  1507. obj = oir:get_luaentity()
  1508. if obj then
  1509. if obj.group_attack == true
  1510. and obj.state ~= "attack" then
  1511. do_attack(obj, hitter)
  1512. end
  1513. end
  1514. end
  1515. end
  1516. end,
  1517. on_activate = function(self, staticdata, dtime_s)
  1518. -- remove monsters in peaceful mode, or when no data
  1519. if (self.type == "monster" and peaceful_only)
  1520. or not staticdata then
  1521. self.object:remove()
  1522. return
  1523. end
  1524. -- load entity variables
  1525. local tmp = minetest.deserialize(staticdata)
  1526. if tmp then
  1527. for _,stat in pairs(tmp) do
  1528. self[_] = stat
  1529. end
  1530. end
  1531. -- select random texture, set model and size
  1532. if not self.base_texture then
  1533. self.base_texture = def.textures[math.random(1, #def.textures)]
  1534. self.base_mesh = def.mesh
  1535. self.base_size = self.visual_size
  1536. self.base_colbox = self.collisionbox
  1537. end
  1538. -- set texture, model and size
  1539. local textures = self.base_texture
  1540. local mesh = self.base_mesh
  1541. local vis_size = self.base_size
  1542. local colbox = self.base_colbox
  1543. -- specific texture if gotten
  1544. if self.gotten == true
  1545. and def.gotten_texture then
  1546. textures = def.gotten_texture
  1547. end
  1548. -- specific mesh if gotten
  1549. if self.gotten == true
  1550. and def.gotten_mesh then
  1551. mesh = def.gotten_mesh
  1552. end
  1553. -- set child objects to half size
  1554. if self.child == true then
  1555. vis_size = {
  1556. x = self.base_size.x / 2,
  1557. y = self.base_size.y / 2
  1558. }
  1559. if def.child_texture then
  1560. textures = def.child_texture[1]
  1561. end
  1562. colbox = {
  1563. self.base_colbox[1] / 2,
  1564. self.base_colbox[2] / 2,
  1565. self.base_colbox[3] / 2,
  1566. self.base_colbox[4] / 2,
  1567. self.base_colbox[5] / 2,
  1568. self.base_colbox[6] / 2
  1569. }
  1570. end
  1571. if self.health == 0 then
  1572. self.health = math.random (self.hp_min, self.hp_max)
  1573. end
  1574. -- rnd: pathfinding init
  1575. self.path = {}
  1576. self.path.way = {} -- path to follow, table of positions
  1577. self.path.lastpos = {x = 0, y = 0, z = 0}
  1578. self.path.stuck = false
  1579. self.path.following = false -- currently following path?
  1580. self.path.stuck_timer = 0 -- if stuck for too long search for path
  1581. -- end init
  1582. self.object:set_armor_groups({immortal = 1, fleshy = self.armor})
  1583. self.old_y = self.object:getpos().y
  1584. self.old_health = self.health
  1585. self.object:setyaw((math.random(0, 360) - 180) / 180 * pi)
  1586. self.sounds.distance = self.sounds.distance or 10
  1587. self.textures = textures
  1588. self.mesh = mesh
  1589. self.collisionbox = colbox
  1590. self.visual_size = vis_size
  1591. self.standing_in = ""
  1592. -- set anything changed above
  1593. self.object:set_properties(self)
  1594. update_tag(self)
  1595. end,
  1596. get_staticdata = function(self)
  1597. -- remove mob when out of range unless tamed
  1598. if remove_far
  1599. and self.remove_ok
  1600. and not self.tamed then
  1601. --print ("REMOVED " .. self.name)
  1602. self.object:remove()
  1603. return nil
  1604. end
  1605. self.remove_ok = true
  1606. self.attack = nil
  1607. self.following = nil
  1608. self.state = "stand"
  1609. -- used to rotate older mobs
  1610. if self.drawtype
  1611. and self.drawtype == "side" then
  1612. self.rotate = math.rad(90)
  1613. end
  1614. local tmp = {}
  1615. for _,stat in pairs(self) do
  1616. local t = type(stat)
  1617. if t ~= 'function'
  1618. and t ~= 'nil'
  1619. and t ~= 'userdata' then
  1620. tmp[_] = self[_]
  1621. end
  1622. end
  1623. -- print('===== '..self.name..'\n'.. dump(tmp)..'\n=====\n')
  1624. return minetest.serialize(tmp)
  1625. end,
  1626. })
  1627. end -- END mobs:register_mob function
  1628. -- global functions
  1629. function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
  1630. interval, chance, active_object_count, min_height, max_height, day_toggle)
  1631. -- chance override in minetest.conf for registered mob
  1632. local new_chance = tonumber(minetest.setting_get(name .. "_chance"))
  1633. if new_chance ~= nil then
  1634. if new_chance == 0 then
  1635. print("[Mobs Redo] " .. name .. " has spawning disabled")
  1636. return
  1637. end
  1638. chance = new_chance
  1639. print ("[Mobs Redo] Chance setting for " .. name .. " is now " .. chance)
  1640. end
  1641. minetest.register_abm({
  1642. nodenames = nodes,
  1643. neighbors = neighbors,
  1644. interval = interval,
  1645. chance = chance,
  1646. action = function(pos, node, aoc, active_object_count_wider)
  1647. -- do not spawn if too many active entities in area
  1648. if active_object_count_wider > active_object_count
  1649. or not mobs.spawning_mobs[name] then
  1650. return
  1651. end
  1652. -- if toggle set to nil then ignore day/night check
  1653. if day_toggle ~= nil then
  1654. local tod = (minetest.get_timeofday() or 0) * 24000
  1655. if tod > 4500 and tod < 19500 then
  1656. -- daylight, but mob wants night
  1657. if day_toggle == false then
  1658. return
  1659. end
  1660. else
  1661. -- night time but mob wants day
  1662. if day_toggle == true then
  1663. return
  1664. end
  1665. end
  1666. end
  1667. -- spawn above node
  1668. pos.y = pos.y + 1
  1669. -- only spawn away from player
  1670. local objs = minetest.get_objects_inside_radius(pos, 10)
  1671. for _,oir in pairs(objs) do
  1672. if oir:is_player() then
  1673. return
  1674. end
  1675. end
  1676. -- mobs cannot spawn in protected areas when enabled
  1677. if spawn_protected == 1
  1678. and minetest.is_protected(pos, "") then
  1679. return
  1680. end
  1681. -- check if light and height levels are ok to spawn
  1682. local light = minetest.get_node_light(pos)
  1683. if not light
  1684. or light > max_light
  1685. or light < min_light
  1686. or pos.y > max_height
  1687. or pos.y < min_height then
  1688. return
  1689. end
  1690. -- are we spawning inside solid nodes?
  1691. if minetest.registered_nodes[node_ok(pos).name].walkable == true then
  1692. return
  1693. end
  1694. pos.y = pos.y + 1
  1695. if minetest.registered_nodes[node_ok(pos).name].walkable == true then
  1696. return
  1697. end
  1698. -- spawn mob half block higher than ground
  1699. pos.y = pos.y - 0.5
  1700. local mob = minetest.add_entity(pos, name)
  1701. if mob and mob:get_luaentity() then
  1702. -- print ("[mobs] Spawned " .. name .. " at "
  1703. -- .. minetest.pos_to_string(pos) .. " on "
  1704. -- .. node.name .. " near " .. neighbors[1])
  1705. else
  1706. print ("[mobs]" .. name .. " failed to spawn at "
  1707. .. minetest.pos_to_string(pos))
  1708. end
  1709. end
  1710. })
  1711. end
  1712. -- compatibility with older mob registration
  1713. function mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
  1714. mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
  1715. chance, active_object_count, -31000, max_height, day_toggle)
  1716. end
  1717. -- set content id's
  1718. local c_air = minetest.get_content_id("air")
  1719. local c_ignore = minetest.get_content_id("ignore")
  1720. local c_obsidian = minetest.get_content_id("default:obsidian")
  1721. local c_brick = minetest.get_content_id("default:obsidianbrick")
  1722. local c_chest = minetest.get_content_id("default:chest_locked")
  1723. -- explosion (cannot break protected or unbreakable nodes)
  1724. function mobs:explosion(pos, radius, fire, smoke, sound)
  1725. radius = radius or 0
  1726. fire = fire or 0
  1727. smoke = smoke or 0
  1728. -- if area protected or near map limits then no blast damage
  1729. if minetest.is_protected(pos, "")
  1730. or not within_limits(pos, radius) then
  1731. return
  1732. end
  1733. -- explosion sound
  1734. if sound
  1735. and sound ~= "" then
  1736. minetest.sound_play(sound, {
  1737. pos = pos,
  1738. gain = 1.0,
  1739. max_hear_distance = 16
  1740. })
  1741. end
  1742. pos = vector.round(pos) -- voxelmanip doesn't work properly unless pos is rounded ?!?!
  1743. local vm = VoxelManip()
  1744. local minp, maxp = vm:read_from_map(vector.subtract(pos, radius), vector.add(pos, radius))
  1745. local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
  1746. local data = vm:get_data()
  1747. local p = {}
  1748. local pr = PseudoRandom(os.time())
  1749. for z = -radius, radius do
  1750. for y = -radius, radius do
  1751. local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
  1752. for x = -radius, radius do
  1753. p.x = pos.x + x
  1754. p.y = pos.y + y
  1755. p.z = pos.z + z
  1756. if (x * x) + (y * y) + (z * z) <= (radius * radius) + pr:next(-radius, radius)
  1757. and data[vi] ~= c_air
  1758. and data[vi] ~= c_ignore
  1759. and data[vi] ~= c_obsidian
  1760. and data[vi] ~= c_brick
  1761. and data[vi] ~= c_chest then
  1762. local n = node_ok(p).name
  1763. local on_blast = minetest.registered_nodes[n].on_blast
  1764. if on_blast then
  1765. return on_blast(p)
  1766. else
  1767. -- after effects
  1768. if fire > 0
  1769. and (minetest.registered_nodes[n].groups.flammable
  1770. or math.random(1, 100) <= 30) then
  1771. minetest.set_node(p, {name = "fire:basic_flame"})
  1772. else
  1773. minetest.set_node(p, {name = "air"})
  1774. if smoke > 0 then
  1775. effect(p, 2, "tnt_smoke.png", 5)
  1776. end
  1777. end
  1778. end
  1779. end
  1780. vi = vi + 1
  1781. end
  1782. end
  1783. end
  1784. end
  1785. -- register arrow for shoot attack
  1786. function mobs:register_arrow(name, def)
  1787. if not name or not def then return end -- errorcheck
  1788. minetest.register_entity(name, {
  1789. physical = false,
  1790. visual = def.visual,
  1791. visual_size = def.visual_size,
  1792. textures = def.textures,
  1793. velocity = def.velocity,
  1794. hit_player = def.hit_player,
  1795. hit_node = def.hit_node,
  1796. hit_mob = def.hit_mob,
  1797. drop = def.drop or false,
  1798. collisionbox = {0, 0, 0, 0, 0, 0}, -- remove box around arrows
  1799. timer = 0,
  1800. switch = 0,
  1801. on_step = function(self, dtime)
  1802. self.timer = self.timer + 1
  1803. local pos = self.object:getpos()
  1804. if self.switch == 0
  1805. or self.timer > 150
  1806. or not within_limits(pos, 0) then
  1807. self.object:remove() ; -- print ("removed arrow")
  1808. return
  1809. end
  1810. -- does arrow have a tail (fireball)
  1811. if def.tail
  1812. and def.tail == 1
  1813. and def.tail_texture then
  1814. effect(pos, 1, def.tail_texture, 10, 0)
  1815. end
  1816. if self.hit_node then
  1817. local node = node_ok(pos).name
  1818. --if minetest.registered_nodes[node].walkable then
  1819. if node ~= "air" then
  1820. self.hit_node(self, pos, node)
  1821. if self.drop == true then
  1822. pos.y = pos.y + 1
  1823. self.lastpos = (self.lastpos or pos)
  1824. minetest.add_item(self.lastpos, self.object:get_luaentity().name)
  1825. end
  1826. self.object:remove() ; -- print ("hit node")
  1827. return
  1828. end
  1829. end
  1830. if (self.hit_player or self.hit_mob)
  1831. -- clear mob entity before arrow becomes active
  1832. and self.timer > (10 - (self.velocity / 2)) then
  1833. for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do
  1834. if self.hit_player
  1835. and player:is_player() then
  1836. self.hit_player(self, player)
  1837. self.object:remove() ; -- print ("hit player")
  1838. return
  1839. end
  1840. if self.hit_mob
  1841. and player:get_luaentity()
  1842. and player:get_luaentity().name ~= self.object:get_luaentity().name
  1843. and player:get_luaentity().name ~= "__builtin:item"
  1844. and player:get_luaentity().name ~= "gauges:hp_bar"
  1845. and player:get_luaentity().name ~= "signs:text"
  1846. and player:get_luaentity().name ~= "itemframes:item" then
  1847. self.hit_mob(self, player)
  1848. self.object:remove() ; -- print ("hit mob")
  1849. return
  1850. end
  1851. end
  1852. end
  1853. self.lastpos = pos
  1854. end
  1855. })
  1856. end
  1857. -- Spawn Egg
  1858. function mobs:register_egg(mob, desc, background, addegg, no_creative)
  1859. local grp = {}
  1860. -- do NOT add this egg to creative inventory (e.g. dungeon master)
  1861. if creative and no_creative == true then
  1862. grp = {not_in_creative_inventory = 1}
  1863. end
  1864. local invimg = background
  1865. if addegg == 1 then
  1866. invimg = "mobs_chicken_egg.png^(" .. invimg ..
  1867. "^[mask:mobs_chicken_egg_overlay.png)"
  1868. end
  1869. minetest.register_craftitem(mob, {
  1870. description = desc,
  1871. inventory_image = invimg,
  1872. groups = grp,
  1873. on_place = function(itemstack, placer, pointed_thing)
  1874. local pos = pointed_thing.above
  1875. if pos
  1876. and within_limits(pos, 0)
  1877. and not minetest.is_protected(pos, placer:get_player_name()) then
  1878. pos.y = pos.y + 1
  1879. local mob = minetest.add_entity(pos, mob)
  1880. local ent = mob:get_luaentity()
  1881. if not ent then
  1882. mob:remove()
  1883. return
  1884. end
  1885. if ent.type ~= "monster" then
  1886. -- set owner and tame if not monster
  1887. ent.owner = placer:get_player_name()
  1888. ent.tamed = true
  1889. end
  1890. -- if not in creative then take item
  1891. if not creative then
  1892. itemstack:take_item()
  1893. end
  1894. end
  1895. return itemstack
  1896. end,
  1897. })
  1898. end
  1899. -- capture critter (thanks to blert2112 for idea)
  1900. function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
  1901. if not self.child
  1902. and clicker:is_player()
  1903. and clicker:get_inventory() then
  1904. -- get name of clicked mob
  1905. local mobname = self.name
  1906. -- if not nil change what will be added to inventory
  1907. if replacewith then
  1908. mobname = replacewith
  1909. end
  1910. local name = clicker:get_player_name()
  1911. -- is mob tamed?
  1912. if self.tamed == false
  1913. and force_take == false then
  1914. minetest.chat_send_player(name, "Not tamed!")
  1915. return
  1916. end
  1917. -- cannot pick up if not owner
  1918. if self.owner ~= name
  1919. and force_take == false then
  1920. minetest.chat_send_player(name, self.owner.." is owner!")
  1921. return
  1922. end
  1923. if clicker:get_inventory():room_for_item("main", mobname) then
  1924. -- was mob clicked with hand, net, or lasso?
  1925. local tool = clicker:get_wielded_item()
  1926. local chance = 0
  1927. if tool:is_empty() then
  1928. chance = chance_hand
  1929. elseif tool:get_name() == "mobs:net" then
  1930. chance = chance_net
  1931. tool:add_wear(4000) -- 17 uses
  1932. clicker:set_wielded_item(tool)
  1933. elseif tool:get_name() == "mobs:magic_lasso" then
  1934. chance = chance_lasso
  1935. tool:add_wear(650) -- 100 uses
  1936. clicker:set_wielded_item(tool)
  1937. end
  1938. -- return if no chance
  1939. if chance == 0 then return end
  1940. -- calculate chance.. add to inventory if successful?
  1941. if math.random(1, 100) <= chance then
  1942. clicker:get_inventory():add_item("main", mobname)
  1943. self.object:remove()
  1944. else
  1945. minetest.chat_send_player(name, "Missed!")
  1946. end
  1947. end
  1948. end
  1949. end
  1950. local mob_obj = {}
  1951. local mob_sta = {}
  1952. -- feeding, taming and breeding (thanks blert2112)
  1953. function mobs:feed_tame(self, clicker, feed_count, breed, tame)
  1954. if not self.follow then
  1955. return false
  1956. end
  1957. -- can eat/tame with item in hand
  1958. if follow_holding(self, clicker) then
  1959. -- if not in creative then take item
  1960. if not creative then
  1961. local item = clicker:get_wielded_item()
  1962. item:take_item()
  1963. clicker:set_wielded_item(item)
  1964. end
  1965. -- increase health
  1966. self.health = self.health + 4
  1967. if self.health >= self.hp_max then
  1968. self.health = self.hp_max
  1969. if self.htimer < 1 then
  1970. minetest.chat_send_player(clicker:get_player_name(),
  1971. self.name:split(":")[2]
  1972. .. " at full health (" .. tostring(self.health) .. ")")
  1973. self.htimer = 5
  1974. end
  1975. end
  1976. self.object:set_hp(self.health)
  1977. update_tag(self)
  1978. -- make children grow quicker
  1979. if self.child == true then
  1980. self.hornytimer = self.hornytimer + 20
  1981. return true
  1982. end
  1983. -- feed and tame
  1984. self.food = (self.food or 0) + 1
  1985. if self.food >= feed_count then
  1986. self.food = 0
  1987. if breed and self.hornytimer == 0 then
  1988. self.horny = true
  1989. end
  1990. self.gotten = false
  1991. if tame then
  1992. if self.tamed == false then
  1993. minetest.chat_send_player(clicker:get_player_name(),
  1994. self.name:split(":")[2]
  1995. .. " has been tamed!")
  1996. end
  1997. self.tamed = true
  1998. if not self.owner or self.owner == "" then
  1999. self.owner = clicker:get_player_name()
  2000. end
  2001. end
  2002. -- make sound when fed so many times
  2003. if self.sounds.random then
  2004. minetest.sound_play(self.sounds.random, {
  2005. object = self.object,
  2006. max_hear_distance = self.sounds.distance
  2007. })
  2008. end
  2009. end
  2010. return true
  2011. end
  2012. local item = clicker:get_wielded_item()
  2013. -- if mob has been tamed you can name it with a nametag
  2014. if item:get_name() == "mobs:nametag"
  2015. and clicker:get_player_name() == self.owner then
  2016. local name = clicker:get_player_name()
  2017. -- store mob and nametag stack in external variables
  2018. mob_obj[name] = self
  2019. mob_sta[name] = item
  2020. local tag = self.nametag or ""
  2021. local formspec = "size[8,4]"
  2022. .. default.gui_bg
  2023. .. default.gui_bg_img
  2024. .. "field[0.5,1;7.5,0;name;Enter name:;" .. tag .. "]"
  2025. .. "button_exit[2.5,3.5;3,1;mob_rename;Rename]"
  2026. minetest.show_formspec(name, "mobs_nametag", formspec)
  2027. end
  2028. return false
  2029. end
  2030. -- inspired by blockmen's nametag mod
  2031. minetest.register_on_player_receive_fields(function(player, formname, fields)
  2032. -- right-clicked with nametag and name entered?
  2033. if formname == "mobs_nametag"
  2034. and fields.name
  2035. and fields.name ~= "" then
  2036. local name = player:get_player_name()
  2037. if not mob_obj[name]
  2038. or not mob_obj[name].object then
  2039. return
  2040. end
  2041. -- update nametag
  2042. mob_obj[name].nametag = fields.name
  2043. update_tag(mob_obj[name])
  2044. -- if not in creative then take item
  2045. if not creative then
  2046. mob_sta[name]:take_item()
  2047. player:set_wielded_item(mob_sta[name])
  2048. end
  2049. -- reset external variables
  2050. mob_obj[name] = nil
  2051. mob_sta[name] = nil
  2052. end
  2053. end)
  2054. -- used to drop items inside a chest or container
  2055. local drop_items = function(pos, invstring)
  2056. local meta = minetest.get_meta(pos)
  2057. local inv = meta:get_inventory()
  2058. for i = 1, inv:get_size(invstring) do
  2059. local m_stack = inv:get_stack(invstring, i)
  2060. local obj = minetest.add_item(pos, m_stack)
  2061. if obj then
  2062. obj:setvelocity({
  2063. x = math.random(-10, 10) / 9,
  2064. y = 3,
  2065. z = math.random(-10, 10) / 9
  2066. })
  2067. end
  2068. end
  2069. end
  2070. -- override chest node so it drops items on explode
  2071. minetest.override_item("default:chest", {
  2072. on_blast = function(p)
  2073. minetest.after(0, function()
  2074. drop_items(p, "main")
  2075. minetest.remove_node(p)
  2076. end)
  2077. end,
  2078. })
  2079. -- compatibility function for old entities to new modpack entities
  2080. function mobs:alias_mob(old_name, new_name)
  2081. -- spawn egg
  2082. minetest.register_alias(old_name, new_name)
  2083. -- entity
  2084. minetest.register_entity(":" .. old_name, {
  2085. physical = false,
  2086. on_step = function(self)
  2087. local pos = self.object:getpos()
  2088. minetest.add_entity(pos, new_name)
  2089. self.object:remove()
  2090. end
  2091. })
  2092. end