api.lua 107 KB

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