api.lua 107 KB

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