api.lua 112 KB

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