api.lua 88 KB

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