api.lua 109 KB

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