api.lua 101 KB

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