api.lua 101 KB

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