api.lua 107 KB

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