api.lua 85 KB

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