api.lua 101 KB

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