api.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. MOB API (13th July 2017)
  2. The mob api is a function that can be called on by other mods to add new animals or monsters into minetest.
  3. minetest.conf settings*
  4. 'enable_damage' if true monsters will attack players (default is true)
  5. 'only_peaceful_mobs' if true only animals will spawn in game (default is false)
  6. 'mobs_disable_blood' if false blood effects appear when mob is hit (default is false)
  7. 'mobs_spawn_protected' if set to false then mobs will not spawn in protected areas (default is true)
  8. 'remove_far_mobs' if true then mobs that are outside players visual range will be removed (default is false)
  9. 'mobname' can change specific mob chance rate (0 to disable) and spawn number e.g. mobs_animal:cow = 1000,5
  10. 'mob_difficulty' sets difficulty level (health and hit damage multiplied by this number), defaults to 1.0.
  11. 'mob_show_health' if false then punching mob will not show health status (true by default)
  12. mobs:register_mob(name, definition)
  13. This functions registers a new mob as a Minetest entity.
  14. 'name' is the name of the mob (e.g. "mobs:dirt_monster")
  15. definition is a table with the following fields
  16. 'type' the type of the mob ("monster", "animal" or "npc") where monsters attack players and npc's, animals and npc's tend to wander around and can attack when hit 1st.
  17. 'passive' will mob defend itself, set to false to attack
  18. 'docile_by_day' when true, mob will not attack during daylight hours unless provoked
  19. 'attacks_monsters' usually for npc's to attack monsters in area
  20. 'group_attack' true to defend same kind of mobs from attack in area
  21. 'owner_loyal' when true owned mobs will attack any monsters you punch
  22. 'attack_animals' true for monster to attack animals as well as player and npc's
  23. 'specific_attack' has a table of entity names that monsters can attack {"player", "mobs_animal:chicken"}
  24. 'hp_min' minimum health
  25. 'hp_max' maximum health (mob health is randomly selected between both)
  26. 'physical' same is in minetest.register_entity()
  27. 'collisionbox' same is in minetest.register_entity()
  28. 'visual' same is in minetest.register_entity()
  29. 'visual_size' same is in minetest.register_entity()
  30. 'textures' same is in minetest.register_entity()
  31. although you can add multiple lines for random textures {{"texture1.png"},{"texture2.png"}},
  32. 'gotten_texture' alt. texture for when self.gotten value is set to true (used for shearing sheep)
  33. 'child_texture' texture of mod for when self.child is set to true
  34. 'mesh' same is in minetest.register_entity()
  35. 'gotten_mesh' alternative mesh for when self.gotten is true (used for sheep)
  36. 'makes_footstep_sound' same is in minetest.register_entity()
  37. 'follow' item when held will cause mob to follow player, can be single string "default:apple" or table {"default:apple", "default:diamond"}. These are also items that are used to feed and tame mob.
  38. 'view_range' the range in which the mob will follow or attack player
  39. 'walk_chance' chance of mob walking around (0 to 100), set to 0 for jumping mob only
  40. 'walk_velocity' the velocity when the monster is walking around
  41. 'run_velocity' the velocity when the monster is attacking a player
  42. 'runaway' when true mob will turn and run away when punched
  43. 'stepheight' minimum node height mob can walk onto without jumping (default: 0.6)
  44. 'jump' can mob jump, true or false
  45. 'jump_height' height mob can jump, default is 6 (0 to disable jump)
  46. 'fly' can mob fly, true or false (used for swimming mobs also)
  47. 'fly_in' node name that mob flys inside, e.g "air", "default:water_source" for fish
  48. 'damage' the damage mobs inflict per melee attack.
  49. 'recovery_time' how much time from when mob is hit to recovery (default: 0.5 seconds)
  50. 'knock_back' strength of knock-back when mob hit (default: 3)
  51. 'immune_to' table holding special tool/item names and damage the incur e.g.
  52. {"default:sword_wood", 0}, {"default:gold_lump", -10} immune to sword, gold lump heals
  53. 'blood_amount' number of droplets that appear when hit
  54. 'blood_texture' texture of blood droplets (default: "mobs_blood.png")
  55. 'drops' is list of tables with the following fields:
  56. 'name' itemname e.g. default:stone
  57. 'chance' the inverted chance (same as in abm) to get the item
  58. 'min' the minimum number of items
  59. 'max' the maximum number of items
  60. 'armor' this integer holds armor strength with 100 being normal, with lower numbers hardening the armor and higher numbers making it weaker (weird I know but compatible with simple mobs)
  61. 'drawtype' "front" or "side" (DEPRECATED, replaced with below)
  62. 'rotate' set mob rotation, 0=front, 90=side, 180=back, 270=other side
  63. 'water_damage' the damage per second if the mob is in water
  64. 'lava_damage' the damage per second if the mob is in lava
  65. 'light_damage' the damage per second if the mob is in light
  66. 'suffocation' health value mob loses when inside a solid node
  67. 'fall_damage' will mob be hurt when falling from height
  68. 'fall_speed' maximum falling velocity of mob (default is -10 and must be below -2)
  69. 'fear_height' when mob walks near a drop then anything over this value makes it stop and turn back (default is 0 to disable)
  70. 'on_die' a function that is called when the mob is killed the parameters are (self, pos)
  71. 'floats' 1 to float in water, 0 to sink
  72. 'on_rightclick' its same as in minetest.register_entity()
  73. 'pathfinding' set to 1 for mobs to use pathfinder feature to locate player, set to 2 so they can build/break also (only works with dogfight attack)
  74. 'attack_type' the attack type of a monster
  75. 'dogfight' follows player in range and attacks when in reach
  76. 'shoot' shoots defined arrows when player is within range
  77. 'explode' follows player in range and will flash and explode when in reach
  78. 'dogshoot' shoots arrows when in range and one on one attack when in reach
  79. 'dogshoot_switch' allows switching between shoot and dogfight modes inside dogshoot using timer (1 = shoot, 2 = dogfight)
  80. 'dogshoot_count_max' number of seconds before switching to dogfight mode.
  81. 'dogshoot_count2_max' number of seconds before switching back to shoot mode.
  82. 'custom_attack' when set this function is called instead of the normal mob melee attack, parameters are (self, to_attack)
  83. 'double_melee_attack' if false then api will choose randomly between 'punch' and 'punch2' attack animations
  84. 'on_blast' is called when an explosion happens near mob when using TNT functions, parameters are (object, damage) and returns (do_damage, do_knockback, drops)
  85. 'explosion_radius' radius of explosion attack (defaults to 1)
  86. 'arrow' if the attack_type is "shoot" or "dogshoot" then the entity name of a pre-defined arrow is required, see below for arrow definition.
  87. 'shoot_interval' the minimum shoot interval
  88. 'shoot_offset' +/- value to position arrow/fireball when fired
  89. 'reach' how far a reach this mob has, default is 3
  90. 'sounds' this is a table with sounds of the mob
  91. 'random' random sounds during gameplay
  92. 'war_cry' sound when starting to attack player
  93. 'attack' sound when attacking player
  94. 'shoot_attack' sound when attacking player by shooting arrow/entity
  95. 'damage' sound when being hit
  96. 'death' sound when killed
  97. 'jump' sound when jumping
  98. 'explode' sound when exploding
  99. 'distance' maximum distance sounds are heard from (default is 10)
  100. Mobs can look for specific nodes as they walk and replace them to mimic eating.
  101. 'replace_what' group if items to replace e.g. {"farming:wheat_8", "farming:carrot_8"}
  102. 'replace_with' replace with what e.g. "air" or in chickens case "mobs:egg"
  103. 'replace_rate' how random should the replace rate be (typically 10)
  104. 'replace_offset' +/- value to check specific node to replace
  105. 'on_replace(self, pos, oldnode, newnode)' gets called when mob is about to replace a node
  106. self: ObjectRef of mob
  107. pos: Position of node to replace
  108. oldnode: Current node
  109. newnode: What the node will become after replacing
  110. If false is returned, the mob will not replace the node.
  111. By default, replacing sets self.gotten to true and resets the object properties.
  112. The 'replace_what' has been updated to use tables for what, with and y_offset e.g.
  113. replace_what = { {"group:grass", "air", 0}, {"default:dirt_with_grass", "default:dirt", -1} }
  114. Mob animation comes in three parts, start_frame, end_frame and frame_speed which
  115. can be added to the mob definition under pre-defined mob animation names like:
  116. 'animation' a table with the animation ranges and speed of the model
  117. 'stand_start', 'stand_end', 'stand_speed' when mob stands still
  118. 'walk_start', 'walk_end', 'walk_speed' when mob walks
  119. 'run_start', 'run_end', 'run_speed' when mob runs
  120. 'fly_start', 'fly_end', 'fly_speed' when mob flies
  121. 'punch_start', 'punch_end', 'punch_speed' when mob attacks
  122. 'punch2_start', 'punch2_end', 'punch2_speed' when mob attacks (alternative)
  123. 'die_start', 'die_end', 'die_speed' when mob dies
  124. '*_loop' bool value to determine if any set animation loops e.g (die_loop = false)
  125. defaults to true if not set
  126. also 'speed_normal' for compatibility with older mobs for animation speed (deprecated)
  127. The mob api also has some preset variables and functions that it will remember for each mob
  128. 'self.health' contains current health of mob (cannot exceed self.hp_max)
  129. 'self.texture_list' contains list of all mob textures
  130. 'self.child_texture' contains mob child texture when growing up
  131. 'self.base_texture' contains current skin texture which was randomly selected from textures list
  132. 'self.gotten' this is used for obtaining milk from cow and wool from sheep
  133. 'self.horny' when animal fed enough it is set to true and animal can breed with same animal
  134. 'self.hornytimer' background timer that controls breeding functions and mob childhood timings
  135. 'self.child' used for when breeding animals have child, will use child_texture and be half size
  136. 'self.owner' string used to set owner of npc mobs, typically used for dogs
  137. 'self.order' set to "follow" or "stand" so that npc will follow owner or stand it's ground
  138. 'self.nametag' contains the name of the mob which it can show above
  139. 'on_die' a function that is called when mob is killed
  140. 'do_custom' a custom function that is called every tick while mob is active and which has access to all of the self.* variables e.g. (self.health for health or self.standing_in for node status), return with 'false' to skip remainder of mob API.
  141. mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
  142. mobs:spawn_specfic(name, nodes, neighbors, min_light, max_light, interval, chance, active_object_count, min_height, max_height, day_toggle, on_spawn)
  143. These functions register a spawn algorithm for the mob. Without this function the call the mobs won't spawn.
  144. 'name' is the name of the animal/monster
  145. 'nodes' is a list of nodenames on that the animal/monster can spawn on top of
  146. 'neighbors' is a list of nodenames on that the animal/monster will spawn beside (default is {"air"} for mobs:register_spawn)
  147. 'max_light' is the maximum of light
  148. 'min_light' is the minimum of light
  149. 'interval' is same as in register_abm() (default is 30 for mobs:register_spawn)
  150. 'chance' is same as in register_abm()
  151. 'active_object_count' mob is only spawned if active_object_count_wider of ABM is <= this
  152. 'min_height' is the minimum height the mob can spawn
  153. 'max_height' is the maximum height the mob can spawn
  154. 'day_toggle' true for day spawning, false for night or nil for anytime
  155. 'on_spawn' is a custom function which runs after mob has spawned and gives self and pos values.
  156. ... also a simpler way to handle mob spawns has been added with the mobs:spawn(def) command which uses above names to make settings clearer:
  157. mobs:spawn({name = "mobs_monster:tree_monster",
  158. nodes = {"group:leaves"},
  159. max_light = 7,
  160. })
  161. Players can override the spawn chance for each mob registered by adding a line to their minetest.conf file with a new value, the lower the value the more each mob will spawn e.g.
  162. mobs_animal:sheep_chance 11000 or mobs_monster:sand_monster_chance 100
  163. For each mob that spawns with this function is a field in mobs.spawning_mobs. It tells if the mob should spawn or not. Default is true. So other mods can only use the API of this mod by disabling the spawning of the default mobs in this mod.
  164. mobs:register_arrow(name, definition)
  165. This function registers a arrow for mobs with the attack type shoot.
  166. 'name' is the name of the arrow
  167. -definition' is a table with the following values:
  168. 'visual' same is in minetest.register_entity()
  169. 'visual_size' same is in minetest.register_entity()
  170. 'textures' same is in minetest.register_entity()
  171. 'velocity' the velocity of the arrow
  172. 'drop' if set to true any arrows hitting a node will drop as item
  173. 'hit_player' a function that is called when the arrow hits a player; this function should hurt the player
  174. the parameters are (self, player)
  175. 'hit_mob' a function that is called when the arrow hits a mob; this function should hurt the mob
  176. the parameters are (self, player)
  177. 'hit_node' a function that is called when the arrow hits a node
  178. the parameters are (self, pos, node)
  179. 'tail' when set to 1 adds a trail or tail to mob arrows
  180. 'tail_texture' texture string used for above effect
  181. 'tail_size' has size for above texture (defaults to between 5 and 10)
  182. 'expire' contains float value for how long tail appears for (defaults to 0.25)
  183. 'glow' has value for how brightly tail glows 1 to 10 (default is 0, no glow)
  184. 'rotate' integer value in degrees to rotate arrow
  185. 'on_step' is a custom function when arrow is active, nil for default.
  186. mobs:register_egg(name, description, background, addegg, no_creative)
  187. This function registers a spawn egg which can be used by admin to properly spawn in a mob.
  188. 'name' this is the name of your new mob to spawn e.g. "mob:sheep"
  189. 'description' the name of the new egg you are creating e.g. "Spawn Sheep"
  190. 'background' the texture displayed for the egg in inventory
  191. 'addegg' would you like an egg image in front of your texture (1=yes, 0=no)
  192. 'no_creative' when set to true this stops spawn egg appearing in creative mode for destructive mobs like Dungeon Masters
  193. mobs:explosion(pos, radius) -- DEPRECATED!!! use mobs:boom() instead
  194. mobs:boom(self, pos, radius)
  195. This function generates an explosion which removes nodes in a specific radius and damages any entity caught inside the blast radius. Protection will limit node destruction but not entity damage.
  196. 'self' mob entity
  197. 'pos' centre position of explosion
  198. 'radius' radius of explosion (typically set to 3)
  199. mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith)
  200. This function is generally called inside the on_rightclick section of the mob api code, it provides a chance of capturing the mob by hand, using the net or magic lasso items, and can also have the player take the mob by force if tamed and replace with another item entirely.
  201. 'self' mob information
  202. 'clicker' player information
  203. 'chance_hand' chance of capturing mob by hand (1 to 100) 0 to disable
  204. 'chance_net' chance of capturing mob using net (1 to 100) 0 to disable
  205. 'chance_lasso' chance of capturing mob using magic lasso (1 to 100) 0 to disable
  206. 'force_take' take mob by force, even if tamed (true or false)
  207. 'replacewith' once captured replace mob with this item instead (overrides new mob eggs with saved information)
  208. mobs:feed_tame(self, clicker, feed_count, breed, tame)
  209. This function allows the mob to be fed the item inside self.follow be it apple, wheat or whatever a set number of times and be tamed or bred as a result. Will return true when mob is fed with item it likes.
  210. 'self' mob information
  211. 'clicker' player information
  212. 'feed_count' number of times mob must be fed to tame or breed
  213. 'breed' true or false stating if mob can be bred and a child created afterwards
  214. 'tame' true or false stating if mob can be tamed so player can pick them up
  215. mobs:protect(self, clicker)
  216. This function can be used to right-click any tamed mob with mobs:protector item, this will protect the mob from harm inside of a protected area from other players. Will return true when mob right-clicked with mobs:protector item.
  217. 'self' mob information
  218. 'clicker' player information
  219. Mobs can now be ridden by players and the following shows the functions and usage:
  220. mobs:attach(self, player)
  221. This function attaches a player to the mob so it can be ridden.
  222. 'self' mob information
  223. 'player' player information
  224. mobs:detach(player, offset)
  225. This function will detach the player currently riding a mob to an offset position.
  226. 'player' player information
  227. 'offset' position table containing offset values
  228. mobs:drive(self, move_animation, stand_animation, can_fly, dtime)
  229. This function allows an attached player to move the mob around and animate it at same time.
  230. 'self' mob information
  231. 'move_animation' string containing movement animation e.g. "walk"
  232. 'stand_animation' string containing standing animation e.g. "stand"
  233. 'can_fly' if true then jump and sneak controls will allow mob to fly up and down
  234. 'dtime' tick time used inside drive function
  235. mobs:fly(self, dtime, speed, can_shoot, arrow_entity, move_animation, stand_animation)
  236. This function allows an attached player to fly the mob around using directional controls.
  237. 'self' mob information
  238. 'dtime' tick time used inside fly function
  239. 'speed' speed of flight
  240. 'can_shoot' true if mob can fire arrow (sneak and left mouse button fires)
  241. 'arrow_entity' name of arrow entity used for firing
  242. 'move_animation' string containing name of pre-defined animation e.g. "walk" or "fly" etc.
  243. 'stand_animation' string containing name of pre-defined animation e.g. "stand" or "blink" etc.
  244. Note: animation names above are from the pre-defined animation lists inside mob registry without extensions.
  245. mobs:set_animation(self, name)
  246. This function sets the current animation for mob, defaulting to "stand" if not found.
  247. 'self' mob information
  248. 'name' name of animation
  249. Certain variables need to be set before using the above functions:
  250. 'self.v2' toggle switch used to define below values for the first time
  251. 'self.max_speed_forward' max speed mob can move forward
  252. 'self.max_speed_reverse' max speed mob can move backwards
  253. 'self.accel' acceleration speed
  254. 'self.terrain_type' integer containing terrain mob can walk on (1 = water, 2 or 3 = land)
  255. 'self.driver_attach_at' position offset for attaching player to mob
  256. 'self.driver_eye_offset' position offset for attached player view
  257. 'self.driver_scale' sets driver scale for mobs larger than {x=1, y=1}
  258. Here is an example mob to show how the above functions work:
  259. -- rideable horse
  260. mobs:register_mob("mob_horse:horse", {
  261. type = "animal",
  262. visual = "mesh",
  263. visual_size = {x = 1.20, y = 1.20},
  264. mesh = "mobs_horse.x",
  265. collisionbox = {-0.4, -0.01, -0.4, 0.4, 1.25, 0.4},
  266. animation = {
  267. speed_normal = 15,
  268. speed_run = 30,
  269. stand_start = 25,
  270. stand_end = 75,
  271. walk_start = 75,
  272. walk_end = 100,
  273. run_start = 75,
  274. run_end = 100,
  275. },
  276. textures = {
  277. {"mobs_horse.png"},
  278. {"mobs_horsepeg.png"},
  279. {"mobs_horseara.png"}
  280. },
  281. fear_height = 3,
  282. runaway = true,
  283. fly = false,
  284. walk_chance = 60,
  285. view_range = 5,
  286. follow = {"farming:wheat"},
  287. passive = true,
  288. hp_min = 12,
  289. hp_max = 16,
  290. armor = 200,
  291. lava_damage = 5,
  292. fall_damage = 5,
  293. water_damage = 1,
  294. makes_footstep_sound = true,
  295. drops = {
  296. {name = "mobs:meat_raw", chance = 1, min = 2, max = 3}
  297. },
  298. do_custom = function(self, dtime)
  299. -- set needed values if not already present
  300. if not self.v2 then
  301. self.v2 = 0
  302. self.max_speed_forward = 6
  303. self.max_speed_reverse = 2
  304. self.accel = 6
  305. self.terrain_type = 3
  306. self.driver_attach_at = {x = 0, y = 20, z = -2}
  307. self.driver_eye_offset = {x = 0, y = 3, z = 0}
  308. self.driver_scale = {x = 1, y = 1}
  309. end
  310. -- if driver present allow control of horse
  311. if self.driver then
  312. mobs.drive(self, "walk", "stand", false, dtime)
  313. return false -- skip rest of mob functions
  314. end
  315. return true
  316. end,
  317. on_die = function(self, pos)
  318. -- drop saddle when horse is killed while riding
  319. -- also detach from horse properly
  320. if self.driver then
  321. minetest.add_item(pos, "mobs:saddle")
  322. mobs.detach(self.driver, {x = 1, y = 0, z = 1})
  323. end
  324. end,
  325. on_rightclick = function(self, clicker)
  326. -- make sure player is clicking
  327. if not clicker or not clicker:is_player() then
  328. return
  329. end
  330. -- feed, tame or heal horse
  331. if mobs:feed_tame(self, clicker, 10, true, true) then
  332. return
  333. end
  334. -- make sure tamed horse is being clicked by owner only
  335. if self.tamed and self.owner == clicker:get_player_name() then
  336. local inv = clicker:get_inventory()
  337. -- detatch player already riding horse
  338. if self.driver and clicker == self.driver then
  339. mobs.detach(clicker, {x = 1, y = 0, z = 1})
  340. -- add saddle back to inventory
  341. if inv:room_for_item("main", "mobs:saddle") then
  342. inv:add_item("main", "mobs:saddle")
  343. else
  344. minetest.add_item(clicker.getpos(), "mobs:saddle")
  345. end
  346. -- attach player to horse
  347. elseif not self.driver
  348. and clicker:get_wielded_item():get_name() == "mobs:saddle" then
  349. self.object:set_properties({stepheight = 1.1})
  350. mobs.attach(self, clicker)
  351. -- take saddle from inventory
  352. inv:remove_item("main", "mobs:saddle")
  353. end
  354. end
  355. -- used to capture horse with magic lasso
  356. mobs:capture_mob(self, clicker, 0, 0, 80, false, nil)
  357. end
  358. })