api.lua 89 KB

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