api.lua 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483
  1. -- Mobs Api (26th July 2017)
  2. mobs = {}
  3. mobs.mod = "redo"
  4. mobs.version = "20170726"
  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(self, 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. if self.animation and self.animation.run_start then
  1196. set_animation(self, "run")
  1197. else
  1198. set_animation(self, "walk")
  1199. end
  1200. else
  1201. set_velocity(self, 0)
  1202. set_animation(self, "punch")
  1203. self.timer = self.timer + dtime
  1204. self.blinktimer = (self.blinktimer or 0) + dtime
  1205. if self.blinktimer > 0.2 then
  1206. self.blinktimer = 0
  1207. if self.blinkstatus then
  1208. self.object:settexturemod("")
  1209. else
  1210. self.object:settexturemod("^[brighten")
  1211. end
  1212. self.blinkstatus = not self.blinkstatus
  1213. end
  1214. if self.timer > 3 then
  1215. local pos = self.object:getpos()
  1216. local radius = self.explosion_radius or 1
  1217. local damage_radius = radius
  1218. -- dont damage anything if area protected or next to water
  1219. if minetest.find_node_near(pos, 1, {"group:water"})
  1220. or minetest.is_protected(pos, "") then
  1221. damage_radius = 0
  1222. end
  1223. self.object:remove()
  1224. if minetest.get_modpath("tnt") and tnt and tnt.boom
  1225. and not minetest.is_protected(pos, "") then
  1226. tnt.boom(pos, {
  1227. radius = radius,
  1228. damage_radius = damage_radius,
  1229. sound = self.sounds.explode,
  1230. })
  1231. else
  1232. minetest.sound_play(self.sounds.explode, {
  1233. pos = pos,
  1234. gain = 1.0,
  1235. max_hear_distance = self.sounds.distance or 32
  1236. })
  1237. entity_physics(pos, damage_radius)
  1238. effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
  1239. end
  1240. return
  1241. end
  1242. end
  1243. elseif self.attack_type == "dogfight"
  1244. or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 2)
  1245. or (self.attack_type == "dogshoot" and dist <= self.reach and dogswitch(self) == 0) then
  1246. if self.fly
  1247. and dist > self.reach then
  1248. local p1 = s
  1249. local me_y = floor(p1.y)
  1250. local p2 = p
  1251. local p_y = floor(p2.y + 1)
  1252. local v = self.object:getvelocity()
  1253. if flight_check(self, s) then
  1254. if me_y < p_y then
  1255. self.object:setvelocity({
  1256. x = v.x,
  1257. y = 1 * self.walk_velocity,
  1258. z = v.z
  1259. })
  1260. elseif me_y > p_y then
  1261. self.object:setvelocity({
  1262. x = v.x,
  1263. y = -1 * self.walk_velocity,
  1264. z = v.z
  1265. })
  1266. end
  1267. else
  1268. if me_y < p_y then
  1269. self.object:setvelocity({
  1270. x = v.x,
  1271. y = 0.01,
  1272. z = v.z
  1273. })
  1274. elseif me_y > p_y then
  1275. self.object:setvelocity({
  1276. x = v.x,
  1277. y = -0.01,
  1278. z = v.z
  1279. })
  1280. end
  1281. end
  1282. end
  1283. -- rnd: new movement direction
  1284. if self.path.following
  1285. and self.path.way
  1286. and self.attack_type ~= "dogshoot" then
  1287. -- no paths longer than 50
  1288. if #self.path.way > 50
  1289. or dist < self.reach then
  1290. self.path.following = false
  1291. return
  1292. end
  1293. local p1 = self.path.way[1]
  1294. if not p1 then
  1295. self.path.following = false
  1296. return
  1297. end
  1298. if abs(p1.x-s.x) + abs(p1.z - s.z) < 0.6 then
  1299. -- reached waypoint, remove it from queue
  1300. table.remove(self.path.way, 1)
  1301. end
  1302. -- set new temporary target
  1303. p = {x = p1.x, y = p1.y, z = p1.z}
  1304. end
  1305. local vec = {
  1306. x = p.x - s.x,
  1307. z = p.z - s.z
  1308. }
  1309. yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
  1310. if p.x > s.x then yaw = yaw + pi end
  1311. yaw = set_yaw(self.object, yaw)
  1312. -- move towards enemy if beyond mob reach
  1313. if dist > self.reach then
  1314. -- path finding by rnd
  1315. if self.pathfinding -- only if mob has pathfinding enabled
  1316. and enable_pathfinding then
  1317. smart_mobs(self, s, p, dist, dtime)
  1318. end
  1319. if is_at_cliff(self) then
  1320. set_velocity(self, 0)
  1321. set_animation(self, "stand")
  1322. else
  1323. if self.path.stuck then
  1324. set_velocity(self, self.walk_velocity)
  1325. else
  1326. set_velocity(self, self.run_velocity)
  1327. end
  1328. if self.animation and self.animation.run_start then
  1329. set_animation(self, "run")
  1330. else
  1331. set_animation(self, "walk")
  1332. end
  1333. end
  1334. else -- rnd: if inside reach range
  1335. self.path.stuck = false
  1336. self.path.stuck_timer = 0
  1337. self.path.following = false -- not stuck anymore
  1338. set_velocity(self, 0)
  1339. if not self.custom_attack then
  1340. if self.timer > 1 then
  1341. self.timer = 0
  1342. if self.double_melee_attack
  1343. and random(1, 2) == 1 then
  1344. set_animation(self, "punch2")
  1345. else
  1346. set_animation(self, "punch")
  1347. end
  1348. local p2 = p
  1349. local s2 = s
  1350. p2.y = p2.y + .5
  1351. s2.y = s2.y + .5
  1352. if line_of_sight(self, p2, s2) == true then
  1353. -- play attack sound
  1354. mob_sound(self, self.sounds.attack)
  1355. -- punch player (or what player is attached to)
  1356. local attached = self.attack:get_attach()
  1357. if attached then
  1358. self.attack = attached
  1359. end
  1360. self.attack:punch(self.object, 1.0, {
  1361. full_punch_interval = 1.0,
  1362. damage_groups = {fleshy = self.damage}
  1363. }, nil)
  1364. end
  1365. end
  1366. else -- call custom attack every second
  1367. if self.custom_attack
  1368. and self.timer > 1 then
  1369. self.timer = 0
  1370. self.custom_attack(self, p)
  1371. end
  1372. end
  1373. end
  1374. elseif self.attack_type == "shoot"
  1375. or (self.attack_type == "dogshoot" and dogswitch(self, dtime) == 1)
  1376. or (self.attack_type == "dogshoot" and dist > self.reach and dogswitch(self) == 0) then
  1377. p.y = p.y - .5
  1378. s.y = s.y + .5
  1379. local dist = get_distance(p, s)
  1380. local vec = {
  1381. x = p.x - s.x,
  1382. y = p.y - s.y,
  1383. z = p.z - s.z
  1384. }
  1385. yaw = (atan(vec.z / vec.x) + pi / 2) - self.rotate
  1386. if p.x > s.x then yaw = yaw + pi end
  1387. yaw = set_yaw(self.object, yaw)
  1388. set_velocity(self, 0)
  1389. if self.shoot_interval
  1390. and self.timer > self.shoot_interval
  1391. and random(1, 100) <= 60 then
  1392. self.timer = 0
  1393. set_animation(self, "shoot")
  1394. -- play shoot attack sound
  1395. mob_sound(self, self.sounds.shoot_attack)
  1396. local p = self.object:getpos()
  1397. p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
  1398. local obj = minetest.add_entity(p, self.arrow)
  1399. local ent = obj:get_luaentity()
  1400. if ent then
  1401. local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5
  1402. local v = ent.velocity or 1 -- or set to default
  1403. ent.switch = 1
  1404. ent.owner_id = tostring(self.object) -- add unique owner id to arrow
  1405. -- offset makes shoot aim accurate
  1406. vec.y = vec.y + self.shoot_offset
  1407. vec.x = vec.x * (v / amount)
  1408. vec.y = vec.y * (v / amount)
  1409. vec.z = vec.z * (v / amount)
  1410. obj:setvelocity(vec)
  1411. else
  1412. obj:remove() -- arrow entity does not exist
  1413. end
  1414. end
  1415. end
  1416. end
  1417. end
  1418. -- falling and fall damage
  1419. local falling = function(self, pos)
  1420. if self.fly then
  1421. return
  1422. end
  1423. -- floating in water (or falling)
  1424. local v = self.object:getvelocity()
  1425. if v.y > 0 then
  1426. -- apply gravity when moving up
  1427. self.object:setacceleration({
  1428. x = 0,
  1429. y = -10,
  1430. z = 0
  1431. })
  1432. elseif v.y <= 0 and v.y > self.fall_speed then
  1433. -- fall downwards at set speed
  1434. self.object:setacceleration({
  1435. x = 0,
  1436. y = self.fall_speed,
  1437. z = 0
  1438. })
  1439. else
  1440. -- stop accelerating once max fall speed hit
  1441. self.object:setacceleration({x = 0, y = 0, z = 0})
  1442. end
  1443. -- in water then float up
  1444. -- if minetest.registered_nodes[node_ok(pos).name].groups.liquid then
  1445. if minetest.registered_nodes[node_ok(pos).name].groups.water then
  1446. if self.floats == 1 then
  1447. self.object:setacceleration({
  1448. x = 0,
  1449. y = -self.fall_speed / (max(1, v.y) ^ 2),
  1450. z = 0
  1451. })
  1452. end
  1453. else
  1454. -- fall damage onto solid ground
  1455. if self.fall_damage == 1
  1456. and self.object:getvelocity().y == 0 then
  1457. local d = (self.old_y or 0) - self.object:getpos().y
  1458. if d > 5 then
  1459. self.health = self.health - floor(d - 5)
  1460. effect(pos, 5, "tnt_smoke.png", 1, 2, 2, nil)
  1461. if check_for_death(self, "fall", {type = "fall"}) then
  1462. return
  1463. end
  1464. end
  1465. self.old_y = self.object:getpos().y
  1466. end
  1467. end
  1468. end
  1469. -- deal damage and effects when mob punched
  1470. local mob_punch = function(self, hitter, tflp, tool_capabilities, dir)
  1471. -- mob health check
  1472. if self.health <= 0 then
  1473. return
  1474. end
  1475. -- error checking when mod profiling is enabled
  1476. if not tool_capabilities then
  1477. minetest.log("warning", "[mobs] Mod profiling enabled, damage not enabled")
  1478. return
  1479. end
  1480. -- is mob protected?
  1481. if self.protected and hitter:is_player()
  1482. and minetest.is_protected(self.object:getpos(), hitter:get_player_name()) then
  1483. minetest.chat_send_player(hitter:get_player_name(), S("Mob has been protected!"))
  1484. return
  1485. end
  1486. -- weapon wear
  1487. local weapon = hitter:get_wielded_item()
  1488. local punch_interval = 1.4
  1489. -- calculate mob damage
  1490. local damage = 0
  1491. local armor = self.object:get_armor_groups() or {}
  1492. local tmp
  1493. -- quick error check incase it ends up 0 (serialize.h check test)
  1494. if tflp == 0 then
  1495. tflp = 0.2
  1496. end
  1497. if use_cmi then
  1498. damage = cmi.calculate_damage(self.object, hitter, tflp, tool_capabilities, dir)
  1499. else
  1500. for group,_ in pairs( (tool_capabilities.damage_groups or {}) ) do
  1501. tmp = tflp / (tool_capabilities.full_punch_interval or 1.4)
  1502. if tmp < 0 then
  1503. tmp = 0.0
  1504. elseif tmp > 1 then
  1505. tmp = 1.0
  1506. end
  1507. damage = damage + (tool_capabilities.damage_groups[group] or 0)
  1508. * tmp * ((armor[group] or 0) / 100.0)
  1509. end
  1510. end
  1511. -- check for tool immunity or special damage
  1512. for n = 1, #self.immune_to do
  1513. if self.immune_to[n][1] == weapon:get_name() then
  1514. damage = self.immune_to[n][2] or 0
  1515. break
  1516. end
  1517. end
  1518. -- healing
  1519. if damage <= -1 then
  1520. self.health = self.health - floor(damage)
  1521. return
  1522. end
  1523. -- print ("Mob Damage is", damage)
  1524. if use_cmi then
  1525. local cancel = cmi.notify_punch(self.object, hitter, tflp, tool_capabilities, dir, damage)
  1526. if cancel then return end
  1527. end
  1528. -- add weapon wear
  1529. if tool_capabilities then
  1530. punch_interval = tool_capabilities.full_punch_interval or 1.4
  1531. end
  1532. if weapon:get_definition()
  1533. and weapon:get_definition().tool_capabilities then
  1534. weapon:add_wear(floor((punch_interval / 75) * 9000))
  1535. hitter:set_wielded_item(weapon)
  1536. end
  1537. -- only play hit sound and show blood effects if damage is 1 or over
  1538. if damage >= 1 then
  1539. -- weapon sounds
  1540. if weapon:get_definition().sounds ~= nil then
  1541. local s = random(0, #weapon:get_definition().sounds)
  1542. minetest.sound_play(weapon:get_definition().sounds[s], {
  1543. object = hitter,
  1544. max_hear_distance = 8
  1545. })
  1546. else
  1547. minetest.sound_play("default_punch", {
  1548. object = hitter,
  1549. max_hear_distance = 5
  1550. })
  1551. end
  1552. -- blood_particles
  1553. if self.blood_amount > 0
  1554. and not disable_blood then
  1555. local pos = self.object:getpos()
  1556. pos.y = pos.y + (-self.collisionbox[2] + self.collisionbox[5]) * .5
  1557. effect(pos, self.blood_amount, self.blood_texture, nil, nil, 1, nil)
  1558. end
  1559. -- do damage
  1560. self.health = self.health - floor(damage)
  1561. -- exit here if dead, special item check
  1562. if weapon:get_name() == "mobs:pick_lava" then
  1563. if check_for_death(self, "lava", {type = "punch",
  1564. puncher = hitter}) then
  1565. return
  1566. end
  1567. else
  1568. if check_for_death(self, "hit", {type = "punch",
  1569. puncher = hitter}) then
  1570. return
  1571. end
  1572. end
  1573. --[[ add healthy afterglow when hit (can cause hit lag with larger textures)
  1574. core.after(0.1, function()
  1575. self.object:settexturemod("^[colorize:#c9900070")
  1576. core.after(0.3, function()
  1577. self.object:settexturemod("")
  1578. end)
  1579. end) ]]
  1580. -- knock back effect (only on full punch)
  1581. if self.knock_back > 0
  1582. and tflp >= punch_interval then
  1583. local v = self.object:getvelocity()
  1584. local r = 1.4 - min(punch_interval, 1.4)
  1585. local kb = r * 5
  1586. local up = 2
  1587. -- if already in air then dont go up anymore when hit
  1588. if v.y > 0
  1589. or self.fly then
  1590. up = 0
  1591. end
  1592. -- direction error check
  1593. dir = dir or {x = 0, y = 0, z = 0}
  1594. self.object:setvelocity({
  1595. x = dir.x * kb,
  1596. y = up,
  1597. z = dir.z * kb
  1598. })
  1599. self.pause_timer = r
  1600. end
  1601. end -- END if damage
  1602. -- if skittish then run away
  1603. if self.runaway == true then
  1604. local lp = hitter:getpos()
  1605. local s = self.object:getpos()
  1606. local vec = {
  1607. x = lp.x - s.x,
  1608. y = lp.y - s.y,
  1609. z = lp.z - s.z
  1610. }
  1611. local yaw = (atan(vec.z / vec.x) + 3 * pi / 2) - self.rotate
  1612. if lp.x > s.x then
  1613. yaw = yaw + pi
  1614. end
  1615. yaw = set_yaw(self.object, yaw)
  1616. self.state = "runaway"
  1617. self.runaway_timer = 0
  1618. self.following = nil
  1619. end
  1620. local name = hitter:get_player_name() or ""
  1621. -- attack puncher and call other mobs for help
  1622. if self.passive == false
  1623. and self.state ~= "flop"
  1624. and self.child == false
  1625. and hitter:get_player_name() ~= self.owner
  1626. and not mobs.invis[ name ] then
  1627. -- attack whoever punched mob
  1628. self.state = ""
  1629. do_attack(self, hitter)
  1630. -- alert others to the attack
  1631. local objs = minetest.get_objects_inside_radius(hitter:getpos(), self.view_range)
  1632. local obj = nil
  1633. for n = 1, #objs do
  1634. obj = objs[n]:get_luaentity()
  1635. if obj then
  1636. -- only alert members of same mob
  1637. if obj.group_attack == true
  1638. and obj.state ~= "attack"
  1639. and obj.owner ~= name
  1640. and obj.name == self.name then
  1641. do_attack(obj, hitter)
  1642. end
  1643. -- have owned mobs attack player threat
  1644. if obj.owner == name and obj.owner_loyal then
  1645. do_attack(obj, self.object)
  1646. end
  1647. end
  1648. end
  1649. end
  1650. end
  1651. -- get entity staticdata
  1652. local mob_staticdata = function(self)
  1653. -- remove mob when out of range unless tamed
  1654. if remove_far
  1655. and self.remove_ok
  1656. and not self.tamed
  1657. and self.lifetimer < 20000 then
  1658. --print ("REMOVED " .. self.name)
  1659. self.object:remove()
  1660. return ""-- nil
  1661. end
  1662. self.remove_ok = true
  1663. self.attack = nil
  1664. self.following = nil
  1665. self.state = "stand"
  1666. -- used to rotate older mobs
  1667. if self.drawtype
  1668. and self.drawtype == "side" then
  1669. self.rotate = math.rad(90)
  1670. end
  1671. if use_cmi then
  1672. self.serialized_cmi_components = cmi.serialize_components(self._cmi_components)
  1673. end
  1674. local tmp = {}
  1675. for _,stat in pairs(self) do
  1676. local t = type(stat)
  1677. if t ~= "function"
  1678. and t ~= "nil"
  1679. and t ~= "userdata"
  1680. and _ ~= "_cmi_components" then
  1681. tmp[_] = self[_]
  1682. end
  1683. end
  1684. --print('===== '..self.name..'\n'.. dump(tmp)..'\n=====\n')
  1685. return minetest.serialize(tmp)
  1686. end
  1687. -- activate mob and reload settings
  1688. local mob_activate = function(self, staticdata, def, dtime)
  1689. -- remove monsters in peaceful mode, or when no data
  1690. if (self.type == "monster" and peaceful_only) then
  1691. self.object:remove()
  1692. return
  1693. end
  1694. -- load entity variables
  1695. local tmp = minetest.deserialize(staticdata)
  1696. if tmp then
  1697. for _,stat in pairs(tmp) do
  1698. self[_] = stat
  1699. end
  1700. end
  1701. -- select random texture, set model and size
  1702. if not self.base_texture then
  1703. -- compatiblity with old simple mobs textures
  1704. if type(def.textures[1]) == "string" then
  1705. def.textures = {def.textures}
  1706. end
  1707. self.base_texture = def.textures[random(1, #def.textures)]
  1708. self.base_mesh = def.mesh
  1709. self.base_size = self.visual_size
  1710. self.base_colbox = self.collisionbox
  1711. end
  1712. -- set texture, model and size
  1713. local textures = self.base_texture
  1714. local mesh = self.base_mesh
  1715. local vis_size = self.base_size
  1716. local colbox = self.base_colbox
  1717. -- specific texture if gotten
  1718. if self.gotten == true
  1719. and def.gotten_texture then
  1720. textures = def.gotten_texture
  1721. end
  1722. -- specific mesh if gotten
  1723. if self.gotten == true
  1724. and def.gotten_mesh then
  1725. mesh = def.gotten_mesh
  1726. end
  1727. -- set child objects to half size
  1728. if self.child == true then
  1729. vis_size = {
  1730. x = self.base_size.x * .5,
  1731. y = self.base_size.y * .5,
  1732. }
  1733. if def.child_texture then
  1734. textures = def.child_texture[1]
  1735. end
  1736. colbox = {
  1737. self.base_colbox[1] * .5,
  1738. self.base_colbox[2] * .5,
  1739. self.base_colbox[3] * .5,
  1740. self.base_colbox[4] * .5,
  1741. self.base_colbox[5] * .5,
  1742. self.base_colbox[6] * .5
  1743. }
  1744. end
  1745. if self.health == 0 then
  1746. self.health = random (self.hp_min, self.hp_max)
  1747. end
  1748. -- rnd: pathfinding init
  1749. self.path = {}
  1750. self.path.way = {} -- path to follow, table of positions
  1751. self.path.lastpos = {x = 0, y = 0, z = 0}
  1752. self.path.stuck = false
  1753. self.path.following = false -- currently following path?
  1754. self.path.stuck_timer = 0 -- if stuck for too long search for path
  1755. -- end init
  1756. self.object:set_armor_groups({immortal = 1, fleshy = self.armor})
  1757. self.old_y = self.object:getpos().y
  1758. self.old_health = self.health
  1759. self.sounds.distance = self.sounds.distance or 10
  1760. self.textures = textures
  1761. self.mesh = mesh
  1762. self.collisionbox = colbox
  1763. self.visual_size = vis_size
  1764. self.standing_in = ""
  1765. -- set anything changed above
  1766. self.object:set_properties(self)
  1767. set_yaw(self.object, (random(0, 360) - 180) / 180 * pi)
  1768. update_tag(self)
  1769. set_animation(self, "stand")
  1770. if use_cmi then
  1771. self._cmi_components = cmi.activate_components(self.serialized_cmi_components)
  1772. cmi.notify_activate(self.object, dtime)
  1773. end
  1774. end
  1775. -- main mob function
  1776. local mob_step = function(self, dtime)
  1777. if use_cmi then
  1778. cmi.notify_step(self.object, dtime)
  1779. end
  1780. local pos = self.object:getpos()
  1781. local yaw = 0
  1782. -- when lifetimer expires remove mob (except npc and tamed)
  1783. if self.type ~= "npc"
  1784. and not self.tamed
  1785. and self.state ~= "attack"
  1786. and remove_far ~= true
  1787. and self.lifetimer < 20000 then
  1788. self.lifetimer = self.lifetimer - dtime
  1789. if self.lifetimer <= 0 then
  1790. -- only despawn away from player
  1791. local objs = minetest.get_objects_inside_radius(pos, 15)
  1792. for n = 1, #objs do
  1793. if objs[n]:is_player() then
  1794. self.lifetimer = 20
  1795. return
  1796. end
  1797. end
  1798. -- minetest.log("action",
  1799. -- S("lifetimer expired, removed @1", self.name))
  1800. effect(pos, 15, "tnt_smoke.png", 2, 4, 2, 0)
  1801. self.object:remove()
  1802. return
  1803. end
  1804. end
  1805. falling(self, pos)
  1806. -- knockback timer
  1807. if self.pause_timer > 0 then
  1808. self.pause_timer = self.pause_timer - dtime
  1809. if self.pause_timer < 1 then
  1810. self.pause_timer = 0
  1811. end
  1812. return
  1813. end
  1814. -- run custom function (defined in mob lua file)
  1815. if self.do_custom then
  1816. -- when false skip going any further
  1817. if self.do_custom(self, dtime) == false then
  1818. return
  1819. end
  1820. end
  1821. -- attack timer
  1822. self.timer = self.timer + dtime
  1823. if self.state ~= "attack" then
  1824. if self.timer < 1 then
  1825. return
  1826. end
  1827. self.timer = 0
  1828. end
  1829. -- never go over 100
  1830. if self.timer > 100 then
  1831. self.timer = 1
  1832. end
  1833. -- node replace check (cow eats grass etc.)
  1834. replace(self, pos)
  1835. -- mob plays random sound at times
  1836. if random(1, 50) == 1 then
  1837. mob_sound(self, self.sounds.random)
  1838. end
  1839. -- environmental damage timer (every 1 second)
  1840. self.env_damage_timer = self.env_damage_timer + dtime
  1841. if (self.state == "attack" and self.env_damage_timer > 1)
  1842. or self.state ~= "attack" then
  1843. self.env_damage_timer = 0
  1844. do_env_damage(self)
  1845. end
  1846. monster_attack(self)
  1847. npc_attack(self)
  1848. breed(self)
  1849. follow_flop(self)
  1850. do_states(self, dtime)
  1851. do_jump(self)
  1852. end
  1853. -- default function when mobs are blown up with TNT
  1854. local do_tnt = function(obj, damage)
  1855. --print ("----- Damage", damage)
  1856. obj.object:punch(obj.object, 1.0, {
  1857. full_punch_interval = 1.0,
  1858. damage_groups = {fleshy = damage},
  1859. }, nil)
  1860. return false, true, {}
  1861. end
  1862. mobs.spawning_mobs = {}
  1863. -- register mob entity
  1864. function mobs:register_mob(name, def)
  1865. mobs.spawning_mobs[name] = true
  1866. minetest.register_entity(name, {
  1867. stepheight = def.stepheight or 1.1, -- was 0.6
  1868. name = name,
  1869. type = def.type,
  1870. attack_type = def.attack_type,
  1871. fly = def.fly,
  1872. fly_in = def.fly_in or "air",
  1873. owner = def.owner or "",
  1874. order = def.order or "",
  1875. on_die = def.on_die,
  1876. do_custom = def.do_custom,
  1877. jump_height = def.jump_height or 4, -- was 6
  1878. drawtype = def.drawtype, -- DEPRECATED, use rotate instead
  1879. rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
  1880. lifetimer = def.lifetimer or 180, -- 3 minutes
  1881. hp_min = max(1, (def.hp_min or 5) * difficulty),
  1882. hp_max = max(1, (def.hp_max or 10) * difficulty),
  1883. physical = true,
  1884. collisionbox = def.collisionbox,
  1885. visual = def.visual,
  1886. visual_size = def.visual_size or {x = 1, y = 1},
  1887. mesh = def.mesh,
  1888. makes_footstep_sound = def.makes_footstep_sound or false,
  1889. view_range = def.view_range or 5,
  1890. walk_velocity = def.walk_velocity or 1,
  1891. run_velocity = def.run_velocity or 2,
  1892. damage = max(0, (def.damage or 0) * difficulty),
  1893. light_damage = def.light_damage or 0,
  1894. water_damage = def.water_damage or 0,
  1895. lava_damage = def.lava_damage or 0,
  1896. suffocation = def.suffocation or 2,
  1897. fall_damage = def.fall_damage or 1,
  1898. fall_speed = def.fall_speed or -10, -- must be lower than -2 (default: -10)
  1899. drops = def.drops or {},
  1900. armor = def.armor or 100,
  1901. on_rightclick = def.on_rightclick,
  1902. arrow = def.arrow,
  1903. shoot_interval = def.shoot_interval,
  1904. sounds = def.sounds or {},
  1905. animation = def.animation,
  1906. follow = def.follow,
  1907. jump = def.jump ~= false,
  1908. walk_chance = def.walk_chance or 50,
  1909. attacks_monsters = def.attacks_monsters or false,
  1910. group_attack = def.group_attack or false,
  1911. passive = def.passive or false,
  1912. recovery_time = def.recovery_time or 0.5,
  1913. knock_back = def.knock_back or 3,
  1914. blood_amount = def.blood_amount or 5,
  1915. blood_texture = def.blood_texture or "mobs_blood.png",
  1916. shoot_offset = def.shoot_offset or 0,
  1917. floats = def.floats or 1, -- floats in water by default
  1918. replace_rate = def.replace_rate,
  1919. replace_what = def.replace_what,
  1920. replace_with = def.replace_with,
  1921. replace_offset = def.replace_offset or 0,
  1922. on_replace = def.on_replace,
  1923. timer = 0,
  1924. env_damage_timer = 0, -- only used when state = "attack"
  1925. tamed = false,
  1926. pause_timer = 0,
  1927. horny = false,
  1928. hornytimer = 0,
  1929. child = false,
  1930. gotten = false,
  1931. health = 0,
  1932. reach = def.reach or 3,
  1933. htimer = 0,
  1934. texture_list = def.textures,
  1935. child_texture = def.child_texture,
  1936. docile_by_day = def.docile_by_day or false,
  1937. time_of_day = 0.5,
  1938. fear_height = def.fear_height or 0,
  1939. runaway = def.runaway,
  1940. runaway_timer = 0,
  1941. pathfinding = def.pathfinding,
  1942. immune_to = def.immune_to or {},
  1943. explosion_radius = def.explosion_radius,
  1944. custom_attack = def.custom_attack,
  1945. double_melee_attack = def.double_melee_attack,
  1946. dogshoot_switch = def.dogshoot_switch,
  1947. dogshoot_count = 0,
  1948. dogshoot_count_max = def.dogshoot_count_max or 5,
  1949. dogshoot_count2_max = def.dogshoot_count2_max or (def.dogshoot_count_max or 5),
  1950. attack_animals = def.attack_animals or false,
  1951. specific_attack = def.specific_attack,
  1952. owner_loyal = def.owner_loyal,
  1953. _cmi_is_mob = true,
  1954. on_blast = def.on_blast or do_tnt,
  1955. on_step = mob_step,
  1956. on_punch = mob_punch,
  1957. on_activate = function(self, staticdata, dtime)
  1958. return mob_activate(self, staticdata, def, dtime)
  1959. end,
  1960. get_staticdata = function(self)
  1961. return mob_staticdata(self)
  1962. end,
  1963. })
  1964. end -- END mobs:register_mob function
  1965. -- count how many mobs of one type are inside an area
  1966. local count_mobs = function(pos, type)
  1967. local num_type = 0
  1968. local num_total = 0
  1969. local objs = minetest.get_objects_inside_radius(pos, aoc_range)
  1970. for n = 1, #objs do
  1971. if not objs[n]:is_player() then
  1972. local obj = objs[n]:get_luaentity()
  1973. -- count mob type and add to total also
  1974. if obj and obj.name and obj.name == type then
  1975. num_type = num_type + 1
  1976. num_total = num_total + 1
  1977. -- add to total mobs
  1978. elseif obj and obj.name and obj.health ~= nil then
  1979. num_total = num_total + 1
  1980. end
  1981. end
  1982. end
  1983. return num_type, num_total
  1984. end
  1985. -- global functions
  1986. function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
  1987. interval, chance, aoc, min_height, max_height, day_toggle, on_spawn)
  1988. -- chance/spawn number override in minetest.conf for registered mob
  1989. local numbers = minetest.setting_get(name)
  1990. if numbers then
  1991. numbers = numbers:split(",")
  1992. chance = tonumber(numbers[1]) or chance
  1993. aoc = tonumber(numbers[2]) or aoc
  1994. if chance == 0 then
  1995. minetest.log("warning", string.format("[mobs] %s has spawning disabled", name))
  1996. return
  1997. end
  1998. minetest.log("action",
  1999. string.format("[mobs] Chance setting for %s changed to %s (total: %s)", name, chance, aoc))
  2000. end
  2001. minetest.register_abm({
  2002. label = name .. " spawning",
  2003. nodenames = nodes,
  2004. neighbors = neighbors,
  2005. interval = interval,
  2006. chance = chance,
  2007. catch_up = false,
  2008. action = function(pos, node, active_object_count, active_object_count_wider)
  2009. -- is mob actually registered?
  2010. if not mobs.spawning_mobs[name] then
  2011. --print ("--- mob doesn't exist", name)
  2012. return
  2013. end
  2014. -- do not spawn if too many of same mob in area
  2015. if active_object_count_wider >= max_per_block
  2016. or count_mobs(pos, name) >= aoc then
  2017. --print ("--- too many entities", name, aoc, active_object_count_wider)
  2018. return
  2019. end
  2020. -- if toggle set to nil then ignore day/night check
  2021. if day_toggle ~= nil then
  2022. local tod = (minetest.get_timeofday() or 0) * 24000
  2023. if tod > 4500 and tod < 19500 then
  2024. -- daylight, but mob wants night
  2025. if day_toggle == false then
  2026. --print ("--- mob needs night", name)
  2027. return
  2028. end
  2029. else
  2030. -- night time but mob wants day
  2031. if day_toggle == true then
  2032. --print ("--- mob needs day", name)
  2033. return
  2034. end
  2035. end
  2036. end
  2037. -- spawn above node
  2038. pos.y = pos.y + 1
  2039. -- only spawn away from player
  2040. local objs = minetest.get_objects_inside_radius(pos, 10)
  2041. for n = 1, #objs do
  2042. if objs[n]:is_player() then
  2043. --print ("--- player too close", name)
  2044. return
  2045. end
  2046. end
  2047. -- mobs cannot spawn in protected areas when enabled
  2048. if not spawn_protected
  2049. and minetest.is_protected(pos, "") then
  2050. --print ("--- inside protected area", name)
  2051. return
  2052. end
  2053. -- are we spawning within height limits?
  2054. if pos.y > max_height
  2055. or pos.y < min_height then
  2056. --print ("--- height limits not met", name, pos.y)
  2057. return
  2058. end
  2059. -- are light levels ok?
  2060. local light = minetest.get_node_light(pos)
  2061. if not light
  2062. or light > max_light
  2063. or light < min_light then
  2064. --print ("--- light limits not met", name, light)
  2065. return
  2066. end
  2067. -- are we spawning inside solid nodes?
  2068. if minetest.registered_nodes[node_ok(pos).name].walkable == true then
  2069. --print ("--- feet in block", name, node_ok(pos).name)
  2070. return
  2071. end
  2072. pos.y = pos.y + 1
  2073. if minetest.registered_nodes[node_ok(pos).name].walkable == true then
  2074. --print ("--- head in block", name, node_ok(pos).name)
  2075. return
  2076. end
  2077. -- spawn mob half block higher than ground
  2078. pos.y = pos.y - 0.5
  2079. local mob = minetest.add_entity(pos, name)
  2080. if mob and mob:get_luaentity() then
  2081. -- print ("[mobs] Spawned " .. name .. " at "
  2082. -- .. minetest.pos_to_string(pos) .. " on "
  2083. -- .. node.name .. " near " .. neighbors[1])
  2084. if on_spawn and not on_spawn(mob, pos) then
  2085. return
  2086. end
  2087. else
  2088. minetest.log("warning", string.format("[mobs] %s failed to spawn at %s",
  2089. name, minetest.pos_to_string(pos)))
  2090. end
  2091. end
  2092. })
  2093. end
  2094. -- compatibility with older mob registration
  2095. function mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
  2096. mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
  2097. chance, active_object_count, -31000, max_height, day_toggle)
  2098. end
  2099. -- MarkBu's spawn function
  2100. function mobs:spawn(def)
  2101. local name = def.name
  2102. local nodes = def.nodes or {"group:soil", "group:stone"}
  2103. local neighbors = def.neighbors or {"air"}
  2104. local min_light = def.min_light or 0
  2105. local max_light = def.max_light or 15
  2106. local interval = def.interval or 30
  2107. local chance = def.chance or 5000
  2108. local active_object_count = def.active_object_count or 1
  2109. local min_height = def.min_height or -31000
  2110. local max_height = def.max_height or 31000
  2111. local day_toggle = def.day_toggle
  2112. local on_spawn = def.on_spawn
  2113. mobs:spawn_specific(name, nodes, neighbors, min_light, max_light, interval,
  2114. chance, active_object_count, min_height, max_height, day_toggle, on_spawn)
  2115. end
  2116. -- register arrow for shoot attack
  2117. function mobs:register_arrow(name, def)
  2118. if not name or not def then return end -- errorcheck
  2119. minetest.register_entity(name, {
  2120. physical = false,
  2121. visual = def.visual,
  2122. visual_size = def.visual_size,
  2123. textures = def.textures,
  2124. velocity = def.velocity,
  2125. hit_player = def.hit_player,
  2126. hit_node = def.hit_node,
  2127. hit_mob = def.hit_mob,
  2128. drop = def.drop or false, -- drops arrow as registered item when true
  2129. collisionbox = {0, 0, 0, 0, 0, 0}, -- remove box around arrows
  2130. timer = 0,
  2131. switch = 0,
  2132. owner_id = def.owner_id,
  2133. rotate = def.rotate,
  2134. automatic_face_movement_dir = def.rotate
  2135. and (def.rotate - (pi / 180)) or false,
  2136. on_activate = def.on_activate or nil,
  2137. on_step = def.on_step or function(self, dtime)
  2138. self.timer = self.timer + 1
  2139. local pos = self.object:getpos()
  2140. if self.switch == 0
  2141. or self.timer > 150
  2142. or not within_limits(pos, 0) then
  2143. self.object:remove() ; -- print ("removed arrow")
  2144. return
  2145. end
  2146. -- does arrow have a tail (fireball)
  2147. if def.tail
  2148. and def.tail == 1
  2149. and def.tail_texture then
  2150. minetest.add_particle({
  2151. pos = pos,
  2152. velocity = {x = 0, y = 0, z = 0},
  2153. acceleration = {x = 0, y = 0, z = 0},
  2154. expirationtime = def.expire or 0.25,
  2155. collisiondetection = false,
  2156. texture = def.tail_texture,
  2157. size = def.tail_size or 5,
  2158. glow = def.glow or 0,
  2159. })
  2160. end
  2161. if self.hit_node then
  2162. local node = node_ok(pos).name
  2163. if minetest.registered_nodes[node].walkable then
  2164. self.hit_node(self, pos, node)
  2165. if self.drop == true then
  2166. pos.y = pos.y + 1
  2167. self.lastpos = (self.lastpos or pos)
  2168. minetest.add_item(self.lastpos, self.object:get_luaentity().name)
  2169. end
  2170. self.object:remove() ; -- print ("hit node")
  2171. return
  2172. end
  2173. end
  2174. if self.hit_player or self.hit_mob then
  2175. for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do
  2176. if self.hit_player
  2177. and player:is_player() then
  2178. self.hit_player(self, player)
  2179. self.object:remove() ; -- print ("hit player")
  2180. return
  2181. end
  2182. local entity = player:get_luaentity()
  2183. if entity
  2184. and self.hit_mob
  2185. and entity._cmi_is_mob == true
  2186. and tostring(player) ~= self.owner_id
  2187. and entity.name ~= self.object:get_luaentity().name then
  2188. self.hit_mob(self, player)
  2189. self.object:remove() ; --print ("hit mob")
  2190. return
  2191. end
  2192. end
  2193. end
  2194. self.lastpos = pos
  2195. end
  2196. })
  2197. end
  2198. -- compatibility function
  2199. function mobs:explosion(pos, radius)
  2200. local self = {sounds = {}}
  2201. self.sounds.explode = "tnt_explode"
  2202. mobs:boom(self, pos, radius)
  2203. end
  2204. -- make explosion with protection and tnt mod check
  2205. function mobs:boom(self, pos, radius)
  2206. if minetest.get_modpath("tnt") and tnt and tnt.boom
  2207. and not minetest.is_protected(pos, "") then
  2208. tnt.boom(pos, {
  2209. radius = radius,
  2210. damage_radius = radius,
  2211. sound = self.sounds.explode,
  2212. })
  2213. else
  2214. minetest.sound_play(self.sounds.explode, {
  2215. pos = pos,
  2216. gain = 1.0,
  2217. max_hear_distance = self.sounds.distance or 32
  2218. })
  2219. entity_physics(pos, radius)
  2220. effect(pos, 32, "tnt_smoke.png", radius * 3, radius * 5, radius, 1, 0)
  2221. end
  2222. end
  2223. -- Register spawn eggs
  2224. -- Note: This also introduces the “spawn_egg” group:
  2225. -- * spawn_egg=1: Spawn egg (generic mob, no metadata)
  2226. -- * spawn_egg=2: Spawn egg (captured/tamed mob, metadata)
  2227. function mobs:register_egg(mob, desc, background, addegg, no_creative)
  2228. local grp = {spawn_egg = 1}
  2229. -- do NOT add this egg to creative inventory (e.g. dungeon master)
  2230. if creative and no_creative == true then
  2231. grp.not_in_creative_inventory = 1
  2232. end
  2233. local invimg = background
  2234. if addegg == 1 then
  2235. invimg = "mobs_chicken_egg.png^(" .. invimg ..
  2236. "^[mask:mobs_chicken_egg_overlay.png)"
  2237. end
  2238. -- register new spawn egg containing mob information
  2239. minetest.register_craftitem(mob .. "_set", {
  2240. description = S("@1 (Tamed)", desc),
  2241. inventory_image = invimg,
  2242. groups = {spawn_egg = 2, not_in_creative_inventory = 1},
  2243. stack_max = 1,
  2244. on_place = function(itemstack, placer, pointed_thing)
  2245. local pos = pointed_thing.above
  2246. -- am I clicking on something with existing on_rightclick function?
  2247. local under = minetest.get_node(pointed_thing.under)
  2248. local def = minetest.registered_nodes[under.name]
  2249. if def and def.on_rightclick then
  2250. return def.on_rightclick(pointed_thing.under, under, placer, itemstack)
  2251. end
  2252. if pos
  2253. and within_limits(pos, 0)
  2254. and not minetest.is_protected(pos, placer:get_player_name()) then
  2255. pos.y = pos.y + 1
  2256. local data = itemstack:get_metadata()
  2257. local mob = minetest.add_entity(pos, mob, data)
  2258. local ent = mob:get_luaentity()
  2259. if not ent then
  2260. mob:remove()
  2261. return
  2262. end
  2263. if ent.type ~= "monster" then
  2264. -- set owner and tame if not monster
  2265. ent.owner = placer:get_player_name()
  2266. ent.tamed = true
  2267. end
  2268. -- since mob is unique we remove egg once spawned
  2269. itemstack:take_item()
  2270. end
  2271. return itemstack
  2272. end,
  2273. })
  2274. -- register old stackable mob egg
  2275. minetest.register_craftitem(mob, {
  2276. description = desc,
  2277. inventory_image = invimg,
  2278. groups = grp,
  2279. on_place = function(itemstack, placer, pointed_thing)
  2280. local pos = pointed_thing.above
  2281. -- am I clicking on something with existing on_rightclick function?
  2282. local under = minetest.get_node(pointed_thing.under)
  2283. local def = minetest.registered_nodes[under.name]
  2284. if def and def.on_rightclick then
  2285. return def.on_rightclick(pointed_thing.under, under, placer, itemstack)
  2286. end
  2287. if pos
  2288. and within_limits(pos, 0)
  2289. and not minetest.is_protected(pos, placer:get_player_name()) then
  2290. pos.y = pos.y + 1
  2291. local mob = minetest.add_entity(pos, mob)
  2292. local ent = mob:get_luaentity()
  2293. if not ent then
  2294. mob:remove()
  2295. return
  2296. end
  2297. if ent.type ~= "monster"
  2298. and not placer:get_player_control().sneak then
  2299. -- set owner and tame if not monster
  2300. ent.owner = placer:get_player_name()
  2301. ent.tamed = true
  2302. end
  2303. -- if not in creative then take item
  2304. if not creative then
  2305. itemstack:take_item()
  2306. end
  2307. end
  2308. return itemstack
  2309. end,
  2310. })
  2311. end
  2312. -- capture critter (thanks to blert2112 for idea)
  2313. function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
  2314. if self.child
  2315. or not clicker:is_player()
  2316. or not clicker:get_inventory() then
  2317. return false
  2318. end
  2319. -- get name of clicked mob
  2320. local mobname = self.name
  2321. -- if not nil change what will be added to inventory
  2322. if replacewith then
  2323. mobname = replacewith
  2324. end
  2325. local name = clicker:get_player_name()
  2326. local tool = clicker:get_wielded_item()
  2327. -- are we using hand, net or lasso to pick up mob?
  2328. if tool:get_name() ~= ""
  2329. and tool:get_name() ~= "mobs:net"
  2330. and tool:get_name() ~= "mobs:lasso" then
  2331. return false
  2332. end
  2333. -- is mob tamed?
  2334. if self.tamed == false
  2335. and force_take == false then
  2336. minetest.chat_send_player(name, S("Not tamed!"))
  2337. return true -- false
  2338. end
  2339. -- cannot pick up if not owner
  2340. if self.owner ~= name
  2341. and force_take == false then
  2342. minetest.chat_send_player(name, S("@1 is owner!", self.owner))
  2343. return true -- false
  2344. end
  2345. if clicker:get_inventory():room_for_item("main", mobname) then
  2346. -- was mob clicked with hand, net, or lasso?
  2347. local chance = 0
  2348. if tool:get_name() == "" then
  2349. chance = chance_hand
  2350. elseif tool:get_name() == "mobs:net" then
  2351. chance = chance_net
  2352. tool:add_wear(4000) -- 17 uses
  2353. clicker:set_wielded_item(tool)
  2354. elseif tool:get_name() == "mobs:lasso" then
  2355. chance = chance_lasso
  2356. tool:add_wear(650) -- 100 uses
  2357. clicker:set_wielded_item(tool)
  2358. end
  2359. -- calculate chance.. add to inventory if successful?
  2360. if chance > 0 and random(1, 100) <= chance then
  2361. -- default mob egg
  2362. local new_stack = ItemStack(mobname)
  2363. -- add special mob egg with all mob information
  2364. -- unless 'replacewith' contains new item to use
  2365. if not replacewith then
  2366. new_stack = ItemStack(mobname .. "_set")
  2367. local tmp = {}
  2368. for _,stat in pairs(self) do
  2369. local t = type(stat)
  2370. if t ~= "function"
  2371. and t ~= "nil"
  2372. and t ~= "userdata" then
  2373. tmp[_] = self[_]
  2374. end
  2375. end
  2376. local data_str = minetest.serialize(tmp)
  2377. new_stack:set_metadata(data_str)
  2378. end
  2379. local inv = clicker:get_inventory()
  2380. if inv:room_for_item("main", new_stack) then
  2381. inv:add_item("main", new_stack)
  2382. else
  2383. minetest.add_item(clicker:getpos(), new_stack)
  2384. end
  2385. self.object:remove()
  2386. mob_sound(self, "default_place_node_hard")
  2387. else
  2388. minetest.chat_send_player(name, S("Missed!"))
  2389. mob_sound(self, "mobs_swing")
  2390. end
  2391. end
  2392. return true
  2393. end
  2394. -- protect tamed mob with rune item
  2395. function mobs:protect(self, clicker)
  2396. local name = clicker:get_player_name()
  2397. local tool = clicker:get_wielded_item()
  2398. if tool:get_name() ~= "mobs:protector" then
  2399. return false
  2400. end
  2401. if self.tamed == false then
  2402. minetest.chat_send_player(name, S("Not tamed!"))
  2403. return true -- false
  2404. end
  2405. if self.protected == true then
  2406. minetest.chat_send_player(name, S("Already protected!"))
  2407. return true -- false
  2408. end
  2409. if not creative then
  2410. tool:take_item() -- take 1 protection rune
  2411. clicker:set_wielded_item(tool)
  2412. end
  2413. self.protected = true
  2414. -- minetest.chat_send_player(name, S("Protected!"))
  2415. local pos = self.object:getpos()
  2416. pos.y = pos.y + self.collisionbox[2] + 0.5
  2417. effect(self.object:getpos(), 25, "mobs_protect_particle.png", 0.5, 4, 2, 15)
  2418. mob_sound(self, "mobs_spell")
  2419. return true
  2420. end
  2421. local mob_obj = {}
  2422. local mob_sta = {}
  2423. -- feeding, taming and breeding (thanks blert2112)
  2424. function mobs:feed_tame(self, clicker, feed_count, breed, tame)
  2425. if not self.follow then
  2426. return false
  2427. end
  2428. -- can eat/tame with item in hand
  2429. if follow_holding(self, clicker) then
  2430. -- if not in creative then take item
  2431. if not creative then
  2432. local item = clicker:get_wielded_item()
  2433. item:take_item()
  2434. clicker:set_wielded_item(item)
  2435. end
  2436. -- increase health
  2437. self.health = self.health + 4
  2438. if self.health >= self.hp_max then
  2439. self.health = self.hp_max
  2440. if self.htimer < 1 then
  2441. minetest.chat_send_player(clicker:get_player_name(),
  2442. S("@1 at full health (@2)",
  2443. self.name:split(":")[2], tostring(self.health)))
  2444. self.htimer = 5
  2445. end
  2446. end
  2447. self.object:set_hp(self.health)
  2448. update_tag(self)
  2449. -- make children grow quicker
  2450. if self.child == true then
  2451. self.hornytimer = self.hornytimer + 20
  2452. return true
  2453. end
  2454. -- feed and tame
  2455. self.food = (self.food or 0) + 1
  2456. if self.food >= feed_count then
  2457. self.food = 0
  2458. if breed and self.hornytimer == 0 then
  2459. self.horny = true
  2460. end
  2461. self.gotten = false
  2462. if tame then
  2463. if self.tamed == false then
  2464. minetest.chat_send_player(clicker:get_player_name(),
  2465. S("@1 has been tamed!",
  2466. self.name:split(":")[2]))
  2467. end
  2468. self.tamed = true
  2469. if not self.owner or self.owner == "" then
  2470. self.owner = clicker:get_player_name()
  2471. end
  2472. end
  2473. -- make sound when fed so many times
  2474. mob_sound(self, self.sounds.random)
  2475. end
  2476. return true
  2477. end
  2478. local item = clicker:get_wielded_item()
  2479. -- if mob has been tamed you can name it with a nametag
  2480. if item:get_name() == "mobs:nametag"
  2481. and clicker:get_player_name() == self.owner then
  2482. local name = clicker:get_player_name()
  2483. -- store mob and nametag stack in external variables
  2484. mob_obj[name] = self
  2485. mob_sta[name] = item
  2486. local tag = self.nametag or ""
  2487. minetest.show_formspec(name, "mobs_nametag", "size[8,4]"
  2488. .. default.gui_bg
  2489. .. default.gui_bg_img
  2490. .. "field[0.5,1;7.5,0;name;" .. minetest.formspec_escape(S("Enter name:")) .. ";" .. tag .. "]"
  2491. .. "button_exit[2.5,3.5;3,1;mob_rename;" .. minetest.formspec_escape(S("Rename")) .. "]")
  2492. end
  2493. return false
  2494. end
  2495. -- inspired by blockmen's nametag mod
  2496. minetest.register_on_player_receive_fields(function(player, formname, fields)
  2497. -- right-clicked with nametag and name entered?
  2498. if formname == "mobs_nametag"
  2499. and fields.name
  2500. and fields.name ~= "" then
  2501. local name = player:get_player_name()
  2502. if not mob_obj[name]
  2503. or not mob_obj[name].object then
  2504. return
  2505. end
  2506. -- limit name entered to 64 characters long
  2507. if string.len(fields.name) > 64 then
  2508. fields.name = string.sub(fields.name, 1, 64)
  2509. end
  2510. -- update nametag
  2511. mob_obj[name].nametag = fields.name
  2512. update_tag(mob_obj[name])
  2513. -- if not in creative then take item
  2514. if not creative then
  2515. mob_sta[name]:take_item()
  2516. player:set_wielded_item(mob_sta[name])
  2517. end
  2518. -- reset external variables
  2519. mob_obj[name] = nil
  2520. mob_sta[name] = nil
  2521. end
  2522. end)
  2523. -- compatibility function for old entities to new modpack entities
  2524. function mobs:alias_mob(old_name, new_name)
  2525. -- spawn egg
  2526. minetest.register_alias(old_name, new_name)
  2527. -- entity
  2528. minetest.register_entity(":" .. old_name, {
  2529. physical = false,
  2530. on_step = function(self)
  2531. local pos = self.object:getpos()
  2532. minetest.add_entity(pos, new_name)
  2533. self.object:remove()
  2534. end
  2535. })
  2536. end