api.lua 92 KB

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