api.lua_testspawn 85 KB

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