api.lua 92 KB

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