api.lua 92 KB

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