api.lua 108 KB

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