api.lua_ 74 KB

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