api.lua 87 KB

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