api.lua 92 KB

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