api.lua 92 KB

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