api.lua 102 KB

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