api.lua 97 KB

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