api.lua 91 KB

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