api.lua 106 KB

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