api.lua 106 KB

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