api.lua 94 KB

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