api.lua 104 KB

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