api.lua 102 KB

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