api.lua 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  1. -- Mobs Api (16th January 2016)
  2. mobs = {}
  3. mobs.mod = "smart"
  4. -- Load settings
  5. local damage_enabled = minetest.setting_getbool("enable_damage")
  6. local peaceful_only = minetest.setting_getbool("only_peaceful_mobs")
  7. local disable_blood = minetest.setting_getbool("mobs_disable_blood")
  8. local creative = minetest.setting_getbool("creative_mode")
  9. mobs.protected = tonumber(minetest.setting_get("mobs_spawn_protected")) or 1
  10. mobs.remove = minetest.setting_getbool("remove_far_mobs")
  11. -- internal functions
  12. local pi = math.pi
  13. local square = math.sqrt
  14. do_attack = function(self, player)
  15. if self.state ~= "attack" then
  16. if math.random(0,100) < 90
  17. and self.sounds.war_cry then
  18. minetest.sound_play(self.sounds.war_cry,{
  19. object = self.object,
  20. max_hear_distance = self.sounds.distance
  21. })
  22. end
  23. self.state = "attack"
  24. self.attack = player
  25. end
  26. end
  27. set_velocity = function(self, v)
  28. local x = 0
  29. local z = 0
  30. if v and v ~= 0 then
  31. local yaw = (self.object:getyaw() + self.rotate) or 0
  32. x = math.sin(yaw) * -v
  33. z = math.cos(yaw) * v
  34. end
  35. self.object:setvelocity({
  36. x = x,
  37. y = self.object:getvelocity().y,
  38. z = z
  39. })
  40. end
  41. get_velocity = function(self)
  42. local v = self.object:getvelocity()
  43. return (v.x * v.x + v.z * v.z) ^ 0.5
  44. end
  45. set_animation = function(self, type)
  46. if not self.animation then
  47. return
  48. end
  49. self.animation.current = self.animation.current or ""
  50. if type == "stand"
  51. and self.animation.current ~= "stand" then
  52. if self.animation.stand_start
  53. and self.animation.stand_end
  54. and self.animation.speed_normal then
  55. self.object:set_animation({
  56. x = self.animation.stand_start,
  57. y = self.animation.stand_end},
  58. self.animation.speed_normal, 0)
  59. self.animation.current = "stand"
  60. end
  61. elseif type == "walk"
  62. and self.animation.current ~= "walk" then
  63. if self.animation.walk_start
  64. and self.animation.walk_end
  65. and self.animation.speed_normal then
  66. self.object:set_animation({
  67. x = self.animation.walk_start,
  68. y = self.animation.walk_end},
  69. self.animation.speed_normal, 0)
  70. self.animation.current = "walk"
  71. end
  72. elseif type == "run"
  73. and self.animation.current ~= "run" then
  74. if self.animation.run_start
  75. and self.animation.run_end
  76. and self.animation.speed_run then
  77. self.object:set_animation({
  78. x = self.animation.run_start,
  79. y = self.animation.run_end},
  80. self.animation.speed_run, 0)
  81. self.animation.current = "run"
  82. end
  83. elseif type == "punch"
  84. and self.animation.current ~= "punch" then
  85. if self.animation.punch_start
  86. and self.animation.punch_end
  87. and self.animation.speed_normal then
  88. self.object:set_animation({
  89. x = self.animation.punch_start,
  90. y = self.animation.punch_end},
  91. self.animation.speed_normal, 0)
  92. self.animation.current = "punch"
  93. end
  94. end
  95. end
  96. -- particle effects
  97. function effect(pos, amount, texture, max_size)
  98. minetest.add_particlespawner({
  99. amount = amount,
  100. time = 0.25,
  101. minpos = pos,
  102. maxpos = pos,
  103. minvel = {x = -0, y = -2, z = -0},
  104. maxvel = {x = 2, y = 2, z = 2},
  105. minacc = {x = -4, y = -4, z = -4},
  106. maxacc = {x = 4, y = 4, z = 4},
  107. minexptime = 0.1,
  108. maxexptime = 1,
  109. minsize = 0.5,
  110. maxsize = (max_size or 1),
  111. texture = texture,
  112. })
  113. end
  114. -- update nametag and colour
  115. function update_tag(self)
  116. local col = "#00FF00"
  117. local qua = self.hp_max / 4
  118. if self.health <= math.floor(qua * 3) then
  119. col = "#FFFF00"
  120. end
  121. if self.health <= math.floor(qua * 2) then
  122. col = "#FF6600"
  123. end
  124. if self.health <= math.floor(qua) then
  125. col = "#FF0000"
  126. end
  127. self.object:set_properties({
  128. nametag = self.nametag,
  129. nametag_color = col
  130. })
  131. end
  132. -- check if mob is dead or only hurt
  133. function check_for_death(self)
  134. -- return if no change
  135. local hp = self.object:get_hp()
  136. if hp == self.health then
  137. return false
  138. end
  139. local pos = self.object:getpos()
  140. -- still got some health? play hurt sound
  141. if hp > 0 then
  142. self.health = hp
  143. if self.sounds.damage then
  144. minetest.sound_play(self.sounds.damage,{
  145. pos = pos,
  146. gain = 1.0,
  147. max_hear_distance = self.sounds.distance
  148. })
  149. end
  150. update_tag(self)
  151. return false
  152. end
  153. -- drop items when dead
  154. local obj
  155. for _,drop in pairs(self.drops) do
  156. if math.random(1, drop.chance) == 1 then
  157. obj = minetest.add_item(pos,
  158. ItemStack(drop.name .. " "
  159. .. math.random(drop.min, drop.max)))
  160. if obj then
  161. obj:setvelocity({
  162. x = math.random(-1, 1),
  163. y = 6,
  164. z = math.random(-1, 1)
  165. })
  166. end
  167. end
  168. end
  169. -- play death sound
  170. if self.sounds.death then
  171. minetest.sound_play(self.sounds.death,{
  172. pos = pos,
  173. gain = 1.0,
  174. max_hear_distance = self.sounds.distance
  175. })
  176. end
  177. -- execute custom death function
  178. if self.on_die then
  179. self.on_die(self, pos)
  180. end
  181. self.object:remove()
  182. return true
  183. end
  184. -- check if within map limits (-30911 to 30927)
  185. function within_limits(pos, radius)
  186. if (pos.x - radius) > -30913
  187. and (pos.x + radius) < 30928
  188. and (pos.y - radius) > -30913
  189. and (pos.y + radius) < 30928
  190. and (pos.z - radius) > -30913
  191. and (pos.z + radius) < 30928 then
  192. return true -- within limits
  193. end
  194. return false -- beyond limits
  195. end
  196. -- is mob facing a cliff
  197. local function is_at_cliff(self)
  198. if self.fear_height == 0 then -- if 0, no falling protection!
  199. return false
  200. end
  201. local yaw = self.object:getyaw()
  202. local dir_x = -math.sin(yaw) * (self.collisionbox[4] + 0.5)
  203. local dir_z = math.cos(yaw) * (self.collisionbox[4] + 0.5)
  204. local pos = self.object:getpos()
  205. local ypos = pos.y + self.collisionbox[2] -- just above floor
  206. if minetest.line_of_sight(
  207. {x = pos.x + dir_x, y = ypos, z = pos.z + dir_z},
  208. {x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z}
  209. , 1) then
  210. return true
  211. end
  212. return false
  213. end
  214. -- environmental damage (water, lava, fire, light)
  215. do_env_damage = function(self)
  216. -- feed/tame text timer (so mob 'full' messages dont spam chat)
  217. if self.htimer > 0 then
  218. self.htimer = self.htimer - 1
  219. end
  220. local pos = self.object:getpos()
  221. self.time_of_day = minetest.get_timeofday()
  222. -- remove mob if beyond map limits
  223. if not within_limits(pos, 0) then
  224. self.object:remove()
  225. return
  226. end
  227. -- daylight above ground
  228. if self.light_damage ~= 0
  229. and pos.y > 0
  230. and self.time_of_day > 0.2
  231. and self.time_of_day < 0.8
  232. and (minetest.get_node_light(pos) or 0) > 12 then
  233. self.object:set_hp(self.object:get_hp() - self.light_damage)
  234. effect(pos, 5, "tnt_smoke.png")
  235. end
  236. if self.water_damage ~= 0
  237. or self.lava_damage ~= 0 then
  238. pos.y = pos.y + self.collisionbox[2] + 0.1 -- foot level
  239. local nod = node_ok(pos, "air") ; --print ("standing in "..nod.name)
  240. local nodef = minetest.registered_nodes[nod.name]
  241. pos.y = pos.y + 1
  242. -- water
  243. if self.water_damage ~= 0
  244. and nodef.groups.water then
  245. self.object:set_hp(self.object:get_hp() - self.water_damage)
  246. effect(pos, 5, "bubble.png")
  247. end
  248. -- lava or fire
  249. if self.lava_damage ~= 0
  250. and (nodef.groups.lava
  251. or nod.name == "fire:basic_flame"
  252. or nod.name == "fire:permanent_flame") then
  253. self.object:set_hp(self.object:get_hp() - self.lava_damage)
  254. effect(pos, 5, "fire_basic_flame.png")
  255. end
  256. end
  257. check_for_death(self)
  258. end
  259. -- jump if facing a solid node (not fences)
  260. do_jump = function(self)
  261. if self.fly
  262. or self.child then
  263. return
  264. end
  265. local pos = self.object:getpos()
  266. -- what is mob standing on?
  267. pos.y = pos.y + self.collisionbox[2] - 0.2
  268. local nod = node_ok(pos)
  269. --print ("standing on:", nod.name, pos.y)
  270. if minetest.registered_nodes[nod.name].walkable == false then
  271. return
  272. end
  273. -- where is front
  274. local yaw = self.object:getyaw()
  275. local dir_x = -math.sin(yaw) * (self.collisionbox[4] + 0.5)
  276. local dir_z = math.cos(yaw) * (self.collisionbox[4] + 0.5)
  277. -- what is in front of mob?
  278. local nod = node_ok({
  279. x = pos.x + dir_x,
  280. y = pos.y + 0.5,
  281. z = pos.z + dir_z
  282. })
  283. -- thin blocks that do not need to be jumped
  284. if nod.name == "default:snow" then
  285. return
  286. end
  287. --print ("in front:", nod.name, pos.y + 0.5)
  288. if minetest.registered_items[nod.name].walkable
  289. and not nod.name:find("fence")
  290. or self.walk_chance == 0 then
  291. local v = self.object:getvelocity()
  292. v.y = self.jump_height + 1
  293. v.x = v.x * 2.2
  294. v.z = v.z * 2.2
  295. self.object:setvelocity(v)
  296. if self.sounds.jump then
  297. minetest.sound_play(self.sounds.jump, {
  298. pos = pos,
  299. gain = 1.0,
  300. max_hear_distance = self.sounds.distance
  301. })
  302. end
  303. else
  304. if self.state ~= "attack" then
  305. self.state = "stand"
  306. set_animation(self, "stand")
  307. end
  308. end
  309. end
  310. -- this is a faster way to calculate distance
  311. function get_distance(a, b)
  312. local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z
  313. return square(x * x + y * y + z * z)
  314. end
  315. -- blast damage to entities nearby (modified from TNT mod)
  316. function entity_physics(pos, radius)
  317. radius = radius * 2
  318. local objs = minetest.get_objects_inside_radius(pos, radius)
  319. local obj_pos, dist
  320. for _, obj in pairs(objs) do
  321. obj_pos = obj:getpos()
  322. dist = math.max(1, get_distance(pos, obj_pos))
  323. local damage = math.floor((4 / dist) * radius)
  324. obj:set_hp(obj:get_hp() - damage)
  325. end
  326. end
  327. -- get node but use fallback for nil or unknown
  328. function node_ok(pos, fallback)
  329. fallback = fallback or "default:dirt"
  330. local node = minetest.get_node_or_nil(pos)
  331. if not node then
  332. return minetest.registered_nodes[fallback]
  333. end
  334. local nodef = minetest.registered_nodes[node.name]
  335. if nodef then
  336. return node
  337. end
  338. return minetest.registered_nodes[fallback]
  339. end
  340. -- should mob follow what I'm holding ?
  341. function follow_holding(self, clicker)
  342. local item = clicker:get_wielded_item()
  343. local t = type(self.follow)
  344. -- single item
  345. if t == "string"
  346. and item:get_name() == self.follow then
  347. return true
  348. -- multiple items
  349. elseif t == "table" then
  350. for no = 1, #self.follow do
  351. if self.follow[no] == item:get_name() then
  352. return true
  353. end
  354. end
  355. end
  356. return false
  357. end
  358. local function breed(self)
  359. -- child take 240 seconds before growing into adult
  360. if self.child == true then
  361. self.hornytimer = self.hornytimer + 1
  362. if self.hornytimer > 240 then
  363. self.child = false
  364. self.hornytimer = 0
  365. self.object:set_properties({
  366. textures = self.base_texture,
  367. mesh = self.base_mesh,
  368. visual_size = self.base_size,
  369. collisionbox = self.base_colbox,
  370. })
  371. -- jump when fully grown so not to fall into ground
  372. self.object:setvelocity({
  373. x = 0,
  374. y = self.jump_height,
  375. z = 0
  376. })
  377. end
  378. return
  379. end
  380. -- horny animal can mate for 40 seconds,
  381. -- afterwards horny animal cannot mate again for 200 seconds
  382. if self.horny == true
  383. and self.hornytimer < 240 then
  384. self.hornytimer = self.hornytimer + 1
  385. if self.hornytimer >= 240 then
  386. self.hornytimer = 0
  387. self.horny = false
  388. end
  389. end
  390. -- find another same animal who is also horny and mate if close enough
  391. if self.horny == true
  392. and self.hornytimer <= 40 then
  393. local pos = self.object:getpos()
  394. effect({x = pos.x, y = pos.y + 1, z = pos.z}, 4, "heart.png")
  395. local ents = minetest.get_objects_inside_radius(pos, 3)
  396. local num = 0
  397. local ent = nil
  398. for i, obj in pairs(ents) do
  399. ent = obj:get_luaentity()
  400. -- check for same animal with different colour
  401. local canmate = false
  402. if ent then
  403. if ent.name == self.name then
  404. canmate = true
  405. else
  406. local entname = string.split(ent.name,":")
  407. local selfname = string.split(self.name,":")
  408. if entname[1] == selfname[1] then
  409. entname = string.split(entname[2],"_")
  410. selfname = string.split(selfname[2],"_")
  411. if entname[1] == selfname[1] then
  412. canmate = true
  413. end
  414. end
  415. end
  416. end
  417. if ent
  418. and canmate == true
  419. and ent.horny == true
  420. and ent.hornytimer <= 40 then
  421. num = num + 1
  422. end
  423. -- found your mate? then have a baby
  424. if num > 1 then
  425. self.hornytimer = 41
  426. ent.hornytimer = 41
  427. -- spawn baby
  428. minetest.after(5, function(dtime)
  429. local mob = minetest.add_entity(pos, self.name)
  430. local ent2 = mob:get_luaentity()
  431. local textures = self.base_texture
  432. if self.child_texture then
  433. textures = self.child_texture[1]
  434. end
  435. mob:set_properties({
  436. textures = textures,
  437. visual_size = {
  438. x = self.base_size.x / 2,
  439. y = self.base_size.y / 2
  440. },
  441. collisionbox = {
  442. self.base_colbox[1] / 2,
  443. self.base_colbox[2] / 2,
  444. self.base_colbox[3] / 2,
  445. self.base_colbox[4] / 2,
  446. self.base_colbox[5] / 2,
  447. self.base_colbox[6] / 2
  448. },
  449. })
  450. ent2.child = true
  451. ent2.tamed = true
  452. ent2.owner = self.owner
  453. end)
  454. num = 0
  455. break
  456. end
  457. end
  458. end
  459. end
  460. function replace(self, pos)
  461. if self.replace_rate
  462. and self.child == false
  463. and math.random(1, self.replace_rate) == 1 then
  464. local pos = self.object:getpos()
  465. pos.y = pos.y + self.replace_offset
  466. -- print ("replace node = ".. minetest.get_node(pos).name, pos.y)
  467. if self.replace_what
  468. and self.object:getvelocity().y == 0
  469. and #minetest.find_nodes_in_area(pos, pos, self.replace_what) > 0 then
  470. minetest.set_node(pos, {name = self.replace_with})
  471. -- when cow/sheep eats grass, replace wool and milk
  472. if self.gotten == true then
  473. self.gotten = false
  474. self.object:set_properties(self)
  475. end
  476. end
  477. end
  478. end
  479. -- check if daytime and also if mob is docile during daylight hours
  480. function day_docile(self)
  481. if self.docile_by_day == false then
  482. return false
  483. elseif self.docile_by_day == true
  484. and self.time_of_day > 0.2
  485. and self.time_of_day < 0.8 then
  486. return true
  487. end
  488. end
  489. -- register mob function
  490. function mobs:register_mob(name, def)
  491. minetest.register_entity(name, {
  492. stepheight = def.stepheight or 0.6,
  493. name = name,
  494. type = def.type,
  495. attack_type = def.attack_type,
  496. fly = def.fly,
  497. fly_in = def.fly_in or "air",
  498. owner = def.owner or "",
  499. order = def.order or "",
  500. on_die = def.on_die,
  501. do_custom = def.do_custom,
  502. jump_height = def.jump_height or 6,
  503. jump_chance = def.jump_chance or 0,
  504. drawtype = def.drawtype, -- DEPRECATED, use rotate instead
  505. rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
  506. lifetimer = def.lifetimer or 180, -- 3 minutes
  507. hp_min = def.hp_min or 5,
  508. hp_max = def.hp_max or 10,
  509. physical = true,
  510. collisionbox = def.collisionbox,
  511. visual = def.visual,
  512. visual_size = def.visual_size or {x = 1, y = 1},
  513. mesh = def.mesh,
  514. makes_footstep_sound = def.makes_footstep_sound or false,
  515. view_range = def.view_range or 5,
  516. walk_velocity = def.walk_velocity or 1,
  517. run_velocity = def.run_velocity or 2,
  518. damage = def.damage or 0,
  519. light_damage = def.light_damage or 0,
  520. water_damage = def.water_damage or 0,
  521. lava_damage = def.lava_damage or 0,
  522. fall_damage = def.fall_damage or 1,
  523. fall_speed = def.fall_speed or -10, -- must be lower than -2 (default: -10)
  524. drops = def.drops or {},
  525. armor = def.armor,
  526. on_rightclick = def.on_rightclick,
  527. arrow = def.arrow,
  528. shoot_interval = def.shoot_interval,
  529. sounds = def.sounds or {},
  530. animation = def.animation,
  531. follow = def.follow,
  532. jump = def.jump or true,
  533. walk_chance = def.walk_chance or 50,
  534. attacks_monsters = def.attacks_monsters or false,
  535. group_attack = def.group_attack or false,
  536. --fov = def.fov or 120,
  537. passive = def.passive or false,
  538. recovery_time = def.recovery_time or 0.5,
  539. knock_back = def.knock_back or 3,
  540. blood_amount = def.blood_amount or 5,
  541. blood_texture = def.blood_texture or "mobs_blood.png",
  542. shoot_offset = def.shoot_offset or 0,
  543. floats = def.floats or 1, -- floats in water by default
  544. replace_rate = def.replace_rate,
  545. replace_what = def.replace_what,
  546. replace_with = def.replace_with,
  547. replace_offset = def.replace_offset or 0,
  548. timer = 0,
  549. env_damage_timer = 0, -- only used when state = "attack"
  550. tamed = false,
  551. pause_timer = 0,
  552. horny = false,
  553. hornytimer = 0,
  554. child = false,
  555. gotten = false,
  556. health = 0,
  557. reach = def.reach or 3,
  558. htimer = 0,
  559. child_texture = def.child_texture,
  560. docile_by_day = def.docile_by_day or false,
  561. time_of_day = 0.5,
  562. fear_height = def.fear_height or 0,
  563. runaway = def.runaway,
  564. runaway_timer = 0,
  565. on_step = function(self, dtime)
  566. local pos = self.object:getpos()
  567. local yaw = self.object:getyaw() or 0
  568. -- when lifetimer expires remove mob (except npc and tamed)
  569. if self.type ~= "npc"
  570. and not self.tamed
  571. and self.state ~= "attack" then
  572. self.lifetimer = self.lifetimer - dtime
  573. if self.lifetimer <= 0 then
  574. minetest.log("action",
  575. "lifetimer expired, removed " .. self.name)
  576. effect(pos, 15, "tnt_smoke.png")
  577. self.object:remove()
  578. return
  579. end
  580. end
  581. if not self.fly then
  582. -- floating in water (or falling)
  583. local v = self.object:getvelocity()
  584. -- going up then apply gravity
  585. if v.y > 0.1 then
  586. self.object:setacceleration({
  587. x = 0,
  588. y = self.fall_speed,
  589. z = 0
  590. })
  591. end
  592. -- in water then float up
  593. if minetest.registered_nodes[node_ok(pos).name].groups.water then
  594. if self.floats == 1 then
  595. self.object:setacceleration({
  596. x = 0,
  597. y = -self.fall_speed / (math.max(1, v.y) ^ 2),
  598. z = 0
  599. })
  600. end
  601. else
  602. -- fall downwards
  603. self.object:setacceleration({
  604. x = 0,
  605. y = self.fall_speed,
  606. z = 0
  607. })
  608. -- fall damage
  609. if self.fall_damage == 1
  610. and self.object:getvelocity().y == 0 then
  611. local d = self.old_y - self.object:getpos().y
  612. if d > 5 then
  613. self.object:set_hp(self.object:get_hp() - math.floor(d - 5))
  614. effect(pos, 5, "tnt_smoke.png")
  615. if check_for_death(self) then
  616. return
  617. end
  618. end
  619. self.old_y = self.object:getpos().y
  620. end
  621. end
  622. end
  623. -- knockback timer
  624. if self.pause_timer > 0 then
  625. self.pause_timer = self.pause_timer - dtime
  626. if self.pause_timer < 1 then
  627. self.pause_timer = 0
  628. end
  629. return
  630. end
  631. -- attack timer
  632. self.timer = self.timer + dtime
  633. if self.state ~= "attack" then
  634. if self.timer < 1 then
  635. return
  636. end
  637. self.timer = 0
  638. end
  639. -- never go over 100
  640. if self.timer > 100 then
  641. self.timer = 1
  642. end
  643. -- node replace check (cow eats grass etc.)
  644. replace(self, pos)
  645. -- mob plays random sound at times
  646. if self.sounds.random
  647. and math.random(1, 100) == 1 then
  648. minetest.sound_play(self.sounds.random, {
  649. object = self.object,
  650. max_hear_distance = self.sounds.distance
  651. })
  652. end
  653. -- environmental damage timer (every 1 second)
  654. self.env_damage_timer = self.env_damage_timer + dtime
  655. if (self.state == "attack" and self.env_damage_timer > 1)
  656. or self.state ~= "attack" then
  657. self.env_damage_timer = 0
  658. do_env_damage(self)
  659. -- custom function (defined in mob lua file)
  660. if self.do_custom then
  661. self.do_custom(self)
  662. end
  663. end
  664. -- find someone to attack
  665. if self.type == "monster"
  666. and damage_enabled
  667. and self.state ~= "attack"
  668. and not day_docile(self) then
  669. local s = self.object:getpos()
  670. local p, sp, dist
  671. local player = nil
  672. local type = nil
  673. local obj = nil
  674. local min_dist = self.view_range + 1
  675. local min_player = nil
  676. for _,oir in pairs(minetest.get_objects_inside_radius(s, self.view_range)) do
  677. if oir:is_player() then
  678. player = oir
  679. type = "player"
  680. else
  681. obj = oir:get_luaentity()
  682. if obj then
  683. player = obj.object
  684. type = obj.type
  685. end
  686. end
  687. if type == "player"
  688. or type == "npc" then
  689. s = self.object:getpos()
  690. p = player:getpos()
  691. sp = s
  692. -- aim higher to make looking up hills more realistic
  693. p.y = p.y + 1
  694. sp.y = sp.y + 1
  695. dist = get_distance(p, s)
  696. if dist < self.view_range then
  697. -- field of view check goes here
  698. -- choose closest player to attack
  699. if minetest.line_of_sight(sp, p, 2) == true
  700. and dist < min_dist then
  701. min_dist = dist
  702. min_player = player
  703. end
  704. end
  705. end
  706. end
  707. -- attack player
  708. if min_player then
  709. do_attack(self, min_player)
  710. end
  711. end
  712. -- npc, find closest monster to attack
  713. local min_dist = self.view_range + 1
  714. local min_player = nil
  715. if self.type == "npc"
  716. and self.attacks_monsters
  717. and self.state ~= "attack" then
  718. local s = self.object:getpos()
  719. local obj = nil
  720. for _, oir in pairs(minetest.get_objects_inside_radius(s, self.view_range)) do
  721. obj = oir:get_luaentity()
  722. if obj
  723. and obj.type == "monster" then
  724. -- attack monster
  725. p = obj.object:getpos()
  726. dist = get_distance(p, s)
  727. if dist < min_dist then
  728. min_dist = dist
  729. min_player = obj.object
  730. end
  731. end
  732. end
  733. if min_player then
  734. do_attack(self, min_player)
  735. end
  736. end
  737. -- breed and grow children
  738. breed(self)
  739. -- find player to follow
  740. if (self.follow ~= ""
  741. or self.order == "follow")
  742. and not self.following
  743. and self.state ~= "attack"
  744. and self.state ~= "runaway" then
  745. local s, p, dist
  746. for _,player in pairs(minetest.get_connected_players()) do
  747. s = self.object:getpos()
  748. p = player:getpos()
  749. dist = get_distance(p, s)
  750. if dist < self.view_range then
  751. self.following = player
  752. break
  753. end
  754. end
  755. end
  756. if self.type == "npc"
  757. and self.order == "follow"
  758. and self.state ~= "attack"
  759. and self.owner ~= "" then
  760. -- npc stop following player if not owner
  761. if self.following
  762. and self.owner
  763. and self.owner ~= self.following:get_player_name() then
  764. self.following = nil
  765. end
  766. else
  767. -- stop following player if not holding specific item
  768. if self.following
  769. and self.following:is_player()
  770. and follow_holding(self, self.following) == false then
  771. self.following = nil
  772. end
  773. end
  774. -- follow that thing
  775. if self.following then
  776. local s = self.object:getpos()
  777. local p
  778. if self.following:is_player() then
  779. p = self.following:getpos()
  780. elseif self.following.object then
  781. p = self.following.object:getpos()
  782. end
  783. if p then
  784. local dist = get_distance(p, s)
  785. -- dont follow if out of range
  786. if dist > self.view_range then
  787. self.following = nil
  788. else
  789. local vec = {
  790. x = p.x - s.x,
  791. y = p.y - s.y,
  792. z = p.z - s.z
  793. }
  794. if vec.x ~= 0
  795. or vec.z ~= 0 then
  796. yaw = (math.atan(vec.z / vec.x) + pi / 2) - self.rotate
  797. if p.x > s.x then
  798. yaw = yaw + pi
  799. end
  800. self.object:setyaw(yaw)
  801. end
  802. -- anyone but standing npc's can move along
  803. if dist > self.reach
  804. and self.order ~= "stand" then
  805. if (self.jump
  806. and get_velocity(self) <= 0.5
  807. and self.object:getvelocity().y == 0)
  808. or (self.object:getvelocity().y == 0
  809. and self.jump_chance > 0) then
  810. do_jump(self)
  811. end
  812. set_velocity(self, self.walk_velocity)
  813. if self.walk_chance ~= 0 then
  814. set_animation(self, "walk")
  815. end
  816. else
  817. set_velocity(self, 0)
  818. set_animation(self, "stand")
  819. end
  820. return
  821. end
  822. end
  823. end
  824. if self.state == "stand" then
  825. if math.random(1, 4) == 1 then
  826. local lp = nil
  827. local s = self.object:getpos()
  828. if self.type == "npc" then
  829. local o = minetest.get_objects_inside_radius(self.object:getpos(), 3)
  830. for _,o in pairs(o) do
  831. if o:is_player() then
  832. lp = o:getpos()
  833. break
  834. end
  835. end
  836. end
  837. -- look at any players nearby, otherwise turn randomly
  838. if lp then
  839. local vec = {
  840. x = lp.x - s.x,
  841. y = lp.y - s.y,
  842. z = lp.z - s.z
  843. }
  844. if vec.x ~= 0
  845. or vec.z ~= 0 then
  846. yaw = (math.atan(vec.z / vec.x) + pi / 2) - self.rotate
  847. if lp.x > s.x then
  848. yaw = yaw + pi
  849. end
  850. end
  851. else
  852. yaw = self.object:getyaw() + ((math.random(0, 360) - 180) / 180 * pi)
  853. end
  854. self.object:setyaw(yaw)
  855. end
  856. set_velocity(self, 0)
  857. set_animation(self, "stand")
  858. -- npc's ordered to stand stay standing
  859. if self.type ~= "npc"
  860. or self.order ~= "stand" then
  861. if self.walk_chance ~= 0
  862. and math.random(1, 100) <= self.walk_chance
  863. and is_at_cliff(self) == false then
  864. set_velocity(self, self.walk_velocity)
  865. self.state = "walk"
  866. set_animation(self, "walk")
  867. end
  868. end
  869. elseif self.state == "walk" then
  870. local s = self.object:getpos()
  871. local lp = minetest.find_node_near(s, 1, {"group:water"})
  872. -- water swimmers cannot move out of water
  873. if self.fly
  874. and self.fly_in == "default:water_source"
  875. and not lp then
  876. --print ("out of water")
  877. set_velocity(self, 0)
  878. -- change to undefined state so nothing more happens
  879. self.state = "flop"
  880. set_animation(self, "stand")
  881. return
  882. end
  883. -- if water nearby then turn away
  884. if lp then
  885. local vec = {
  886. x = lp.x - s.x,
  887. y = lp.y - s.y,
  888. z = lp.z - s.z
  889. }
  890. if vec.x ~= 0
  891. or vec.z ~= 0 then
  892. yaw = math.atan(vec.z / vec.x) + 3 * pi / 2 - self.rotate
  893. if lp.x > s.x then
  894. yaw = yaw + pi
  895. end
  896. self.object:setyaw(yaw)
  897. end
  898. -- otherwise randomly turn
  899. elseif math.random(1, 100) <= 30 then
  900. yaw = self.object:getyaw() + ((math.random(0, 360) - 180) / 180 * pi)
  901. self.object:setyaw(yaw)
  902. end
  903. -- stand for great fall in front
  904. local temp_is_cliff = is_at_cliff(self)
  905. -- jump when walking comes to a halt
  906. if temp_is_cliff == false
  907. and self.jump
  908. and get_velocity(self) <= 0.5
  909. and self.object:getvelocity().y == 0 then
  910. do_jump(self)
  911. end
  912. if temp_is_cliff
  913. or math.random(1, 100) <= 30 then
  914. set_velocity(self, 0)
  915. self.state = "stand"
  916. set_animation(self, "stand")
  917. else
  918. set_velocity(self, self.walk_velocity)
  919. set_animation(self, "walk")
  920. end
  921. -- runaway when punched
  922. elseif self.state == "runaway" then
  923. self.runaway_timer = self.runaway_timer + 1
  924. -- stop after 3 seconds or when at cliff
  925. if self.runaway_timer > 3
  926. or is_at_cliff(self) then
  927. self.runaway_timer = 0
  928. set_velocity(self, 0)
  929. self.state = "stand"
  930. set_animation(self, "stand")
  931. else
  932. set_velocity(self, self.run_velocity)
  933. set_animation(self, "walk")
  934. end
  935. -- jump when walking comes to a halt
  936. if self.jump
  937. and get_velocity(self) <= 0.5
  938. and self.object:getvelocity().y == 0 then
  939. do_jump(self)
  940. end
  941. -- attack routines (explode, dogfight, shoot, dogshoot)
  942. elseif self.state == "attack" then
  943. -- calculate distance from mob and enemy
  944. local s = self.object:getpos()
  945. local p = self.attack:getpos() or s
  946. local dist = get_distance(p, s)
  947. -- stop attacking if player or out of range
  948. if dist > self.view_range
  949. or not self.attack
  950. or not self.attack:getpos()
  951. or self.attack:get_hp() <= 0 then
  952. --print(" ** stop attacking **", dist, self.view_range)
  953. self.state = "stand"
  954. set_velocity(self, 0)
  955. set_animation(self, "stand")
  956. self.attack = nil
  957. self.v_start = false
  958. self.timer = 0
  959. self.blinktimer = 0
  960. return
  961. end
  962. if self.attack_type == "explode" then
  963. local vec = {
  964. x = p.x - s.x,
  965. y = p.y - s.y,
  966. z = p.z - s.z
  967. }
  968. if vec.x ~= 0
  969. or vec.z ~= 0 then
  970. yaw = math.atan(vec.z / vec.x) + pi / 2 - self.rotate
  971. if p.x > s.x then
  972. yaw = yaw + pi
  973. end
  974. self.object:setyaw(yaw)
  975. end
  976. if dist > self.reach then
  977. if not self.v_start then
  978. self.v_start = true
  979. set_velocity(self, self.run_velocity)
  980. self.timer = 0
  981. self.blinktimer = 0
  982. else
  983. self.timer = 0
  984. self.blinktimer = 0
  985. if get_velocity(self) <= 0.5
  986. and self.object:getvelocity().y == 0 then
  987. local v = self.object:getvelocity()
  988. v.y = 5
  989. self.object:setvelocity(v)
  990. end
  991. set_velocity(self, self.run_velocity)
  992. end
  993. set_animation(self, "run")
  994. else
  995. set_velocity(self, 0)
  996. self.timer = self.timer + dtime
  997. self.blinktimer = (self.blinktimer or 0) + dtime
  998. if self.blinktimer > 0.2 then
  999. self.blinktimer = 0
  1000. if self.blinkstatus then
  1001. self.object:settexturemod("")
  1002. else
  1003. self.object:settexturemod("^[brighten")
  1004. end
  1005. self.blinkstatus = not self.blinkstatus
  1006. end
  1007. if self.timer > 3 then
  1008. local pos = self.object:getpos()
  1009. -- hurt player/mobs caught in blast area
  1010. entity_physics(pos, 3)
  1011. -- dont damage anything if area protected or next to water
  1012. if minetest.find_node_near(pos, 1, {"group:water"})
  1013. or minetest.is_protected(pos, "") then
  1014. if self.sounds.explode then
  1015. minetest.sound_play(self.sounds.explode, {
  1016. pos = pos,
  1017. gain = 1.0,
  1018. max_hear_distance = 16
  1019. })
  1020. end
  1021. self.object:remove()
  1022. effect(pos, 15, "tnt_smoke.png", 5)
  1023. return
  1024. end
  1025. pos.y = pos.y - 1
  1026. mobs:explosion(pos, 2, 0, 1, self.sounds.explode)
  1027. self.object:remove()
  1028. return
  1029. end
  1030. end
  1031. elseif self.attack_type == "dogfight"
  1032. or (self.attack_type == "dogshoot" and dist <= self.reach) then
  1033. if self.fly
  1034. and dist > self.reach then
  1035. local nod = node_ok(s)
  1036. local p1 = s
  1037. local me_y = math.floor(p1.y)
  1038. local p2 = p
  1039. local p_y = math.floor(p2.y + 1)
  1040. local v = self.object:getvelocity()
  1041. if nod.name == self.fly_in then
  1042. if me_y < p_y then
  1043. self.object:setvelocity({
  1044. x = v.x,
  1045. y = 1 * self.walk_velocity,
  1046. z = v.z
  1047. })
  1048. elseif me_y > p_y then
  1049. self.object:setvelocity({
  1050. x = v.x,
  1051. y = -1 * self.walk_velocity,
  1052. z = v.z
  1053. })
  1054. end
  1055. else
  1056. if me_y < p_y then
  1057. self.object:setvelocity({
  1058. x = v.x,
  1059. y = 0.01,
  1060. z = v.z
  1061. })
  1062. elseif me_y > p_y then
  1063. self.object:setvelocity({
  1064. x = v.x,
  1065. y = -0.01,
  1066. z = v.z
  1067. })
  1068. end
  1069. end
  1070. end
  1071. local vec = {
  1072. x = p.x - s.x,
  1073. y = p.y - s.y,
  1074. z = p.z - s.z
  1075. }
  1076. if vec.x ~= 0
  1077. or vec.z ~= 0 then
  1078. yaw = (math.atan(vec.z / vec.x) + pi / 2) - self.rotate
  1079. if p.x > s.x then
  1080. yaw = yaw + pi
  1081. end
  1082. self.object:setyaw(yaw)
  1083. end
  1084. -- move towards enemy if beyond mob reach
  1085. if dist > self.reach then
  1086. -- jump attack
  1087. if (self.jump
  1088. and get_velocity(self) <= 0.5
  1089. and self.object:getvelocity().y == 0)
  1090. or (self.object:getvelocity().y == 0
  1091. and self.jump_chance > 0) then
  1092. do_jump(self)
  1093. end
  1094. if is_at_cliff(self) then
  1095. set_velocity(self, 0)
  1096. set_animation(self, "stand")
  1097. else
  1098. set_velocity(self, self.run_velocity)
  1099. set_animation(self, "run")
  1100. end
  1101. else
  1102. set_velocity(self, 0)
  1103. set_animation(self, "punch")
  1104. if self.timer > 1 then
  1105. self.timer = 0
  1106. local p2 = p
  1107. local s2 = s
  1108. p2.y = p2.y + 1.5
  1109. s2.y = s2.y + 1.5
  1110. if minetest.line_of_sight(p2, s2) == true then
  1111. -- play attack sound
  1112. if self.sounds.attack then
  1113. minetest.sound_play(self.sounds.attack, {
  1114. object = self.object,
  1115. max_hear_distance = self.sounds.distance
  1116. })
  1117. end
  1118. -- punch player
  1119. self.attack:punch(self.object, 1.0, {
  1120. full_punch_interval = 1.0,
  1121. damage_groups = {fleshy = self.damage}
  1122. }, nil)
  1123. end
  1124. end
  1125. end
  1126. elseif self.attack_type == "shoot"
  1127. or (self.attack_type == "dogshoot" and dist > self.reach) then
  1128. p.y = p.y - .5
  1129. s.y = s.y + .5
  1130. local dist = get_distance(p, s)
  1131. local vec = {
  1132. x = p.x - s.x,
  1133. y = p.y - s.y,
  1134. z = p.z - s.z
  1135. }
  1136. if vec.x ~= 0
  1137. or vec.z ~= 0 then
  1138. yaw = (math.atan(vec.z / vec.x) + pi / 2) - self.rotate
  1139. if p.x > s.x then
  1140. yaw = yaw + pi
  1141. end
  1142. self.object:setyaw(yaw)
  1143. end
  1144. set_velocity(self, 0)
  1145. if self.shoot_interval
  1146. and self.timer > self.shoot_interval
  1147. and math.random(1, 100) <= 60 then
  1148. self.timer = 0
  1149. set_animation(self, "punch")
  1150. -- play shoot attack sound
  1151. if self.sounds.shoot_attack then
  1152. minetest.sound_play(self.sounds.shoot_attack, {
  1153. object = self.object,
  1154. max_hear_distance = self.sounds.distance
  1155. })
  1156. end
  1157. local p = self.object:getpos()
  1158. p.y = p.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
  1159. local obj = minetest.add_entity(p, self.arrow)
  1160. local ent = obj:get_luaentity()
  1161. local amount = (vec.x ^ 2 + vec.y ^ 2 + vec.z ^ 2) ^ 0.5
  1162. local v = ent.velocity
  1163. ent.switch = 1
  1164. -- offset makes shoot aim accurate
  1165. vec.y = vec.y + self.shoot_offset
  1166. vec.x = vec.x * v / amount
  1167. vec.y = vec.y * v / amount
  1168. vec.z = vec.z * v / amount
  1169. obj:setvelocity(vec)
  1170. end
  1171. end
  1172. end -- END if self.state == "attack"
  1173. end,
  1174. on_punch = function(self, hitter, tflp, tool_capabilities, dir)
  1175. -- weapon wear
  1176. local weapon = hitter:get_wielded_item()
  1177. local punch_interval = 1.4
  1178. if tool_capabilities then
  1179. punch_interval = tool_capabilities.full_punch_interval or 1.4
  1180. end
  1181. if weapon:get_definition()
  1182. and weapon:get_definition().tool_capabilities then
  1183. weapon:add_wear(math.floor((punch_interval / 75) * 9000))
  1184. hitter:set_wielded_item(weapon)
  1185. end
  1186. -- weapon sounds
  1187. if weapon:get_definition().sounds ~= nil then
  1188. local s = math.random(0, #weapon:get_definition().sounds)
  1189. minetest.sound_play(weapon:get_definition().sounds[s], {
  1190. object = hitter,
  1191. max_hear_distance = 8
  1192. })
  1193. else
  1194. minetest.sound_play("default_punch", {
  1195. object = hitter,
  1196. max_hear_distance = 5
  1197. })
  1198. end
  1199. -- exit here if dead
  1200. if check_for_death(self) then
  1201. return
  1202. end
  1203. -- blood_particles
  1204. if self.blood_amount > 0
  1205. and not disable_blood then
  1206. local pos = self.object:getpos()
  1207. pos.y = pos.y + (-self.collisionbox[2] + self.collisionbox[5]) / 2
  1208. effect(pos, self.blood_amount, self.blood_texture)
  1209. end
  1210. -- knock back effect
  1211. if self.knock_back > 0 then
  1212. local v = self.object:getvelocity()
  1213. local r = 1.4 - math.min(punch_interval, 1.4)
  1214. local kb = r * 5
  1215. self.object:setvelocity({
  1216. x = (dir.x or 0) * kb,
  1217. y = 2,
  1218. z = (dir.z or 0) * kb
  1219. })
  1220. self.pause_timer = r
  1221. end
  1222. -- if skittish then run away
  1223. if self.runaway == true then
  1224. local lp = hitter:getpos()
  1225. local s = self.object:getpos()
  1226. local vec = {
  1227. x = lp.x - s.x,
  1228. y = lp.y - s.y,
  1229. z = lp.z - s.z
  1230. }
  1231. if vec.x ~= 0
  1232. or vec.z ~= 0 then
  1233. local yaw = math.atan(vec.z / vec.x) + 3 * pi / 2 - self.rotate
  1234. if lp.x > s.x then
  1235. yaw = yaw + pi
  1236. end
  1237. self.object:setyaw(yaw)
  1238. end
  1239. self.state = "runaway"
  1240. self.runaway_timer = 0
  1241. self.following = nil
  1242. end
  1243. -- attack puncher and call other mobs for help
  1244. if self.passive == false
  1245. and self.child == false
  1246. and hitter:get_player_name() ~= self.owner then
  1247. if self.state ~= "attack" then
  1248. do_attack(self, hitter)
  1249. end
  1250. -- alert others to the attack
  1251. local obj = nil
  1252. for _, oir in pairs(minetest.get_objects_inside_radius(hitter:getpos(), 5)) do
  1253. obj = oir:get_luaentity()
  1254. if obj then
  1255. if obj.group_attack == true
  1256. and obj.state ~= "attack" then
  1257. do_attack(obj, hitter)
  1258. end
  1259. end
  1260. end
  1261. end
  1262. end,
  1263. on_activate = function(self, staticdata, dtime_s)
  1264. -- remove monsters if playing on peaceful
  1265. if self.type == "monster"
  1266. and peaceful_only then
  1267. self.object:remove()
  1268. return
  1269. end
  1270. -- load entity variables
  1271. if staticdata then
  1272. local tmp = minetest.deserialize(staticdata)
  1273. if tmp then
  1274. for _,stat in pairs(tmp) do
  1275. self[_] = stat
  1276. end
  1277. end
  1278. else
  1279. self.object:remove()
  1280. return
  1281. end
  1282. -- select random texture, set model and size
  1283. if not self.base_texture then
  1284. self.base_texture = def.textures[math.random(1, #def.textures)]
  1285. self.base_mesh = def.mesh
  1286. self.base_size = self.visual_size
  1287. self.base_colbox = self.collisionbox
  1288. end
  1289. -- set texture, model and size
  1290. local textures = self.base_texture
  1291. local mesh = self.base_mesh
  1292. local vis_size = self.base_size
  1293. local colbox = self.base_colbox
  1294. -- specific texture if gotten
  1295. if self.gotten == true
  1296. and def.gotten_texture then
  1297. textures = def.gotten_texture
  1298. end
  1299. -- specific mesh if gotten
  1300. if self.gotten == true
  1301. and def.gotten_mesh then
  1302. mesh = def.gotten_mesh
  1303. end
  1304. -- set child objects to half size
  1305. if self.child == true then
  1306. vis_size = {
  1307. x = self.base_size.x / 2,
  1308. y = self.base_size.y / 2
  1309. }
  1310. if def.child_texture then
  1311. textures = def.child_texture[1]
  1312. end
  1313. colbox = {
  1314. self.base_colbox[1] / 2,
  1315. self.base_colbox[2] / 2,
  1316. self.base_colbox[3] / 2,
  1317. self.base_colbox[4] / 2,
  1318. self.base_colbox[5] / 2,
  1319. self.base_colbox[6] / 2
  1320. }
  1321. end
  1322. if self.health == 0 then
  1323. self.health = math.random (self.hp_min, self.hp_max)
  1324. end
  1325. self.object:set_hp(self.health)
  1326. self.object:set_armor_groups({fleshy = self.armor})
  1327. self.old_y = self.object:getpos().y
  1328. self.object:setyaw(math.random(1, 360) / 180 * pi)
  1329. self.sounds.distance = (self.sounds.distance or 10)
  1330. self.textures = textures
  1331. self.mesh = mesh
  1332. self.collisionbox = colbox
  1333. self.visual_size = vis_size
  1334. -- set anything changed above
  1335. self.object:set_properties(self)
  1336. update_tag(self)
  1337. end,
  1338. get_staticdata = function(self)
  1339. -- remove mob when out of range unless tamed
  1340. if mobs.remove
  1341. and self.remove_ok
  1342. and not self.tamed then
  1343. --print ("REMOVED", self.remove_ok, self.name)
  1344. self.object:remove()
  1345. return nil
  1346. end
  1347. self.remove_ok = true
  1348. self.attack = nil
  1349. self.following = nil
  1350. self.state = "stand"
  1351. -- used to rotate older mobs
  1352. if self.drawtype
  1353. and self.drawtype == "side" then
  1354. self.rotate = math.rad(90)
  1355. end
  1356. local tmp = {}
  1357. for _,stat in pairs(self) do
  1358. local t = type(stat)
  1359. if t ~= 'function'
  1360. and t ~= 'nil'
  1361. and t ~= 'userdata' then
  1362. tmp[_] = self[_]
  1363. end
  1364. end
  1365. -- print('===== '..self.name..'\n'.. dump(tmp)..'\n=====\n')
  1366. return minetest.serialize(tmp)
  1367. end,
  1368. })
  1369. end -- END mobs:register_mob function
  1370. -- global functions
  1371. mobs.spawning_mobs = {}
  1372. function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light,
  1373. interval, chance, active_object_count, min_height, max_height)
  1374. mobs.spawning_mobs[name] = true
  1375. -- chance override in minetest.conf for registered mob
  1376. local new_chance = tonumber(minetest.setting_get(name .. "_chance"))
  1377. if new_chance ~= nil then
  1378. if chance == 0 then
  1379. print("[Mobs Redo] " .. name .. " has spawning disabled")
  1380. return
  1381. end
  1382. chance = new_chance
  1383. print ("[Mobs Redo] Chance setting for " .. name .. " is now " .. chance)
  1384. end
  1385. minetest.register_abm({
  1386. nodenames = nodes,
  1387. neighbors = neighbors,
  1388. interval = interval,
  1389. chance = chance,
  1390. action = function(pos, node, _, active_object_count_wider)
  1391. -- do not spawn if too many active entities in area
  1392. if active_object_count_wider > active_object_count
  1393. or not mobs.spawning_mobs[name] then
  1394. return
  1395. end
  1396. -- spawn above node
  1397. pos.y = pos.y + 1
  1398. -- mobs cannot spawn in protected areas when enabled
  1399. if mobs.protected == 1
  1400. and minetest.is_protected(pos, "") then
  1401. return
  1402. end
  1403. -- check if light and height levels are ok to spawn
  1404. local light = minetest.get_node_light(pos)
  1405. if not light
  1406. or light > max_light
  1407. or light < min_light
  1408. or pos.y > max_height
  1409. or pos.y < min_height then
  1410. return
  1411. end
  1412. -- are we spawning inside solid nodes?
  1413. if minetest.registered_nodes[node_ok(pos).name].walkable == true then
  1414. return
  1415. end
  1416. pos.y = pos.y + 1
  1417. if minetest.registered_nodes[node_ok(pos).name].walkable == true then
  1418. return
  1419. end
  1420. -- spawn mob half block higher than ground
  1421. pos.y = pos.y - 0.5
  1422. local mob = minetest.add_entity(pos, name)
  1423. if mob and mob:get_luaentity() then
  1424. -- print ("[mobs] Spawned " .. name .. " at "
  1425. -- .. minetest.pos_to_string(pos) .. " on "
  1426. -- .. node.name .. " near " .. neighbors[1])
  1427. else
  1428. print ("[mobs]" .. name .. " failed to spawn at "
  1429. .. minetest.pos_to_string(pos))
  1430. end
  1431. end
  1432. })
  1433. end
  1434. -- compatibility with older mob registration
  1435. function mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height)
  1436. mobs:spawn_specific(name, nodes, {"air"}, min_light, max_light, 30,
  1437. chance, active_object_count, -31000, max_height)
  1438. end
  1439. -- set content id's
  1440. local c_air = minetest.get_content_id("air")
  1441. local c_ignore = minetest.get_content_id("ignore")
  1442. local c_obsidian = minetest.get_content_id("default:obsidian")
  1443. local c_brick = minetest.get_content_id("default:obsidianbrick")
  1444. local c_chest = minetest.get_content_id("default:chest_locked")
  1445. -- explosion (cannot break protected or unbreakable nodes)
  1446. function mobs:explosion(pos, radius, fire, smoke, sound)
  1447. radius = radius or 0
  1448. fire = fire or 0
  1449. smoke = smoke or 0
  1450. -- if area protected or near map limits then no blast damage
  1451. if minetest.is_protected(pos, "")
  1452. or not within_limits(pos, radius) then
  1453. return
  1454. end
  1455. -- explosion sound
  1456. if sound
  1457. and sound ~= "" then
  1458. minetest.sound_play(sound, {
  1459. pos = pos,
  1460. gain = 1.0,
  1461. max_hear_distance = 16
  1462. })
  1463. end
  1464. pos = vector.round(pos) -- voxelmanip doesn't work properly unless pos is rounded ?!?!
  1465. local vm = VoxelManip()
  1466. local minp, maxp = vm:read_from_map(vector.subtract(pos, radius), vector.add(pos, radius))
  1467. local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
  1468. local data = vm:get_data()
  1469. local p = {}
  1470. for z = -radius, radius do
  1471. for y = -radius, radius do
  1472. local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
  1473. for x = -radius, radius do
  1474. p.x = pos.x + x
  1475. p.y = pos.y + y
  1476. p.z = pos.z + z
  1477. if data[vi] ~= c_air
  1478. and data[vi] ~= c_ignore
  1479. and data[vi] ~= c_obsidian
  1480. and data[vi] ~= c_brick
  1481. and data[vi] ~= c_chest then
  1482. local n = node_ok(p).name
  1483. if minetest.get_item_group(n, "unbreakable") ~= 1 then
  1484. -- if chest then drop items inside
  1485. if n == "default:chest"
  1486. or n == "3dchest:chest"
  1487. or n == "bones:bones" then
  1488. local meta = minetest.get_meta(p)
  1489. local inv = meta:get_inventory()
  1490. for i = 1, inv:get_size("main") do
  1491. local m_stack = inv:get_stack("main", i)
  1492. local obj = minetest.add_item(p, m_stack)
  1493. if obj then
  1494. obj:setvelocity({
  1495. x = math.random(-2, 2),
  1496. y = 7,
  1497. z = math.random(-2, 2)
  1498. })
  1499. end
  1500. end
  1501. end
  1502. -- after effects
  1503. if fire > 0
  1504. and (minetest.registered_nodes[n].groups.flammable
  1505. or math.random(1, 100) <= 30) then
  1506. minetest.set_node(p, {name = "fire:basic_flame"})
  1507. else
  1508. minetest.set_node(p, {name = "air"})
  1509. if smoke > 0 then
  1510. effect(p, 2, "tnt_smoke.png", 5)
  1511. end
  1512. end
  1513. end
  1514. end
  1515. vi = vi + 1
  1516. end
  1517. end
  1518. end
  1519. end
  1520. -- register arrow for shoot attack
  1521. function mobs:register_arrow(name, def)
  1522. if not name or not def then return end -- errorcheck
  1523. minetest.register_entity(name, {
  1524. physical = false,
  1525. visual = def.visual,
  1526. visual_size = def.visual_size,
  1527. textures = def.textures,
  1528. velocity = def.velocity,
  1529. hit_player = def.hit_player,
  1530. hit_node = def.hit_node,
  1531. hit_mob = def.hit_mob,
  1532. drop = def.drop or false,
  1533. collisionbox = {0, 0, 0, 0, 0, 0}, -- remove box around arrows
  1534. timer = 0,
  1535. switch = 0,
  1536. on_step = function(self, dtime)
  1537. self.timer = self.timer + 1
  1538. local pos = self.object:getpos()
  1539. if self.switch == 0
  1540. or self.timer > 150
  1541. or not within_limits(pos, 0) then
  1542. self.object:remove() ; -- print ("removed arrow")
  1543. return
  1544. end
  1545. if self.hit_node then
  1546. local node = node_ok(pos).name
  1547. if minetest.registered_nodes[node].walkable then
  1548. self.hit_node(self, pos, node)
  1549. if self.drop == true then
  1550. pos.y = pos.y + 1
  1551. self.lastpos = (self.lastpos or pos)
  1552. minetest.add_item(self.lastpos, self.object:get_luaentity().name)
  1553. end
  1554. self.object:remove() ; -- print ("hit node")
  1555. return
  1556. end
  1557. end
  1558. if (self.hit_player or self.hit_mob)
  1559. -- clear mob entity before arrow becomes active
  1560. and self.timer > (10 - (self.velocity / 2)) then
  1561. for _,player in pairs(minetest.get_objects_inside_radius(pos, 1.0)) do
  1562. if self.hit_player
  1563. and player:is_player() then
  1564. self.hit_player(self, player)
  1565. self.object:remove() ; -- print ("hit player")
  1566. return
  1567. end
  1568. if self.hit_mob
  1569. and player:get_luaentity()
  1570. and player:get_luaentity().name ~= self.object:get_luaentity().name
  1571. and player:get_luaentity().name ~= "__builtin:item"
  1572. and player:get_luaentity().name ~= "gauges:hp_bar"
  1573. and player:get_luaentity().name ~= "signs:text" then
  1574. self.hit_mob(self, player)
  1575. self.object:remove() ; -- print ("hit mob")
  1576. return
  1577. end
  1578. end
  1579. end
  1580. self.lastpos = pos
  1581. end
  1582. })
  1583. end
  1584. -- Spawn Egg
  1585. function mobs:register_egg(mob, desc, background, addegg)
  1586. local invimg = background
  1587. if addegg == 1 then
  1588. invimg = invimg .. "^mobs_chicken_egg.png"
  1589. end
  1590. minetest.register_craftitem(mob, {
  1591. description = desc,
  1592. inventory_image = invimg,
  1593. on_place = function(itemstack, placer, pointed_thing)
  1594. local pos = pointed_thing.above
  1595. if pos
  1596. and within_limits(pos, 0)
  1597. and not minetest.is_protected(pos, placer:get_player_name()) then
  1598. pos.y = pos.y + 1
  1599. local mob = minetest.add_entity(pos, mob)
  1600. local ent = mob:get_luaentity()
  1601. if ent.type ~= "monster" then
  1602. -- set owner and tame if not monster
  1603. ent.owner = placer:get_player_name()
  1604. ent.tamed = true
  1605. end
  1606. -- if not in creative then take item
  1607. if not creative then
  1608. itemstack:take_item()
  1609. end
  1610. end
  1611. return itemstack
  1612. end,
  1613. })
  1614. end
  1615. -- capture critter (thanks to blert2112 for idea)
  1616. function mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
  1617. if not self.child
  1618. and clicker:is_player()
  1619. and clicker:get_inventory() then
  1620. -- get name of clicked mob
  1621. local mobname = self.name
  1622. -- if not nil change what will be added to inventory
  1623. if replacewith then
  1624. mobname = replacewith
  1625. end
  1626. local name = clicker:get_player_name()
  1627. -- is mob tamed?
  1628. if self.tamed == false
  1629. and force_take == false then
  1630. minetest.chat_send_player(name, "Not tamed!")
  1631. return
  1632. end
  1633. -- cannot pick up if not owner
  1634. if self.owner ~= name
  1635. and force_take == false then
  1636. minetest.chat_send_player(name, self.owner.." is owner!")
  1637. return
  1638. end
  1639. if clicker:get_inventory():room_for_item("main", mobname) then
  1640. -- was mob clicked with hand, net, or lasso?
  1641. local tool = clicker:get_wielded_item()
  1642. local chance = 0
  1643. if tool:is_empty() then
  1644. chance = chance_hand
  1645. elseif tool:get_name() == "mobs:net" then
  1646. chance = chance_net
  1647. tool:add_wear(4000) -- 17 uses
  1648. clicker:set_wielded_item(tool)
  1649. elseif tool:get_name() == "mobs:magic_lasso" then
  1650. chance = chance_lasso
  1651. tool:add_wear(650) -- 100 uses
  1652. clicker:set_wielded_item(tool)
  1653. end
  1654. -- return if no chance
  1655. if chance == 0 then return end
  1656. -- calculate chance.. add to inventory if successful?
  1657. if math.random(1, 100) <= chance then
  1658. clicker:get_inventory():add_item("main", mobname)
  1659. self.object:remove()
  1660. else
  1661. minetest.chat_send_player(name, "Missed!")
  1662. end
  1663. end
  1664. end
  1665. end
  1666. local mob_obj = {}
  1667. local mob_sta = {}
  1668. -- feeding, taming and breeding (thanks blert2112)
  1669. function mobs:feed_tame(self, clicker, feed_count, breed, tame)
  1670. if not self.follow then
  1671. return false
  1672. end
  1673. -- can eat/tame with item in hand
  1674. if follow_holding(self, clicker) then
  1675. -- if not in creative then take item
  1676. if not creative then
  1677. local item = clicker:get_wielded_item()
  1678. item:take_item()
  1679. clicker:set_wielded_item(item)
  1680. end
  1681. -- increase health
  1682. self.health = self.health + 4
  1683. if self.health >= self.hp_max then
  1684. self.health = self.hp_max
  1685. if self.htimer < 1 then
  1686. minetest.chat_send_player(clicker:get_player_name(),
  1687. self.name:split(":")[2]
  1688. .. " at full health (" .. tostring(self.health) .. ")")
  1689. self.htimer = 5
  1690. end
  1691. end
  1692. self.object:set_hp(self.health)
  1693. update_tag(self)
  1694. -- make children grow quicker
  1695. if self.child == true then
  1696. self.hornytimer = self.hornytimer + 20
  1697. return true
  1698. end
  1699. -- feed and tame
  1700. self.food = (self.food or 0) + 1
  1701. if self.food >= feed_count then
  1702. self.food = 0
  1703. if breed and self.hornytimer == 0 then
  1704. self.horny = true
  1705. end
  1706. self.gotten = false
  1707. if tame then
  1708. if self.tamed == false then
  1709. minetest.chat_send_player(clicker:get_player_name(),
  1710. self.name:split(":")[2]
  1711. .. " has been tamed!")
  1712. end
  1713. self.tamed = true
  1714. if not self.owner or self.owner == "" then
  1715. self.owner = clicker:get_player_name()
  1716. end
  1717. end
  1718. -- make sound when fed so many times
  1719. if self.sounds.random then
  1720. minetest.sound_play(self.sounds.random, {
  1721. object = self.object,
  1722. max_hear_distance = self.sounds.distance
  1723. })
  1724. end
  1725. end
  1726. return true
  1727. end
  1728. local item = clicker:get_wielded_item()
  1729. -- if mob has been tamed you can name it with a nametag
  1730. if item:get_name() == "mobs:nametag"
  1731. and clicker:get_player_name() == self.owner then
  1732. local name = clicker:get_player_name()
  1733. -- store mob and nametag stack in external variables
  1734. mob_obj[name] = self
  1735. mob_sta[name] = item
  1736. local tag = self.nametag or ""
  1737. local formspec = "size[8,4]"
  1738. .. default.gui_bg
  1739. .. default.gui_bg_img
  1740. .. "field[0.5,1;7.5,0;name;Enter name and press button:;" .. tag .. "]"
  1741. .. "button_exit[2.5,3.5;3,1;mob_rename;Rename]"
  1742. minetest.show_formspec(name, "mobs_nametag", formspec)
  1743. end
  1744. return false
  1745. end
  1746. -- inspired by blockmen's nametag mod
  1747. minetest.register_on_player_receive_fields(function(player, formname, fields)
  1748. -- right-clicked with nametag, name entered and button pressed?
  1749. if formname == "mobs_nametag"
  1750. and fields.mob_rename
  1751. and fields.name ~= "" then
  1752. local name = player:get_player_name()
  1753. local ent = mob_obj[name]
  1754. if not ent
  1755. or not ent.object then
  1756. return
  1757. end
  1758. -- update nametag
  1759. ent.nametag = fields.name
  1760. update_tag(ent)
  1761. -- if not in creative then take item
  1762. if not creative then
  1763. local itemstack = mob_sta[name]
  1764. itemstack:take_item()
  1765. player:set_wielded_item(itemstack)
  1766. end
  1767. -- reset external variables
  1768. mob_obj[name] = nil
  1769. mob_sta[name] = nil
  1770. end
  1771. end)