From b901a7875e8eeeb6ba3aab7408b9eea39b51b1a5 Mon Sep 17 00:00:00 2001 From: PilzAdam Date: Thu, 20 Sep 2012 17:44:44 +0200 Subject: [PATCH] Merge register_animal and register_monster to register_mob --- README.txt | 17 ++--- api.lua | 188 ++++++----------------------------------------------- init.lua | 27 ++++++-- 3 files changed, 44 insertions(+), 188 deletions(-) diff --git a/README.txt b/README.txt index 18814af..8679063 100644 --- a/README.txt +++ b/README.txt @@ -31,10 +31,11 @@ There are also 2 friendly mobs: For developers: This mod add some functions that you can use in other mods: -1. mobs:register_monster(name, def) +1. mobs:register_mob(name, def) This adds a monster to Minetest that will attack the player "name" is the name of the monster ("[modname]:[monstername]") "def" is a table with the following values: + type: the type of the mob ("monster" or "animal") hp_max: same is in minetest.register_entity() physical: same is in minetest.register_entity() collisionbox: same is in minetest.register_entity() @@ -47,24 +48,14 @@ This mod add some functions that you can use in other mods: walk_velocity: the velocity when the monster is walking around run_velocity: the velocity when the monster is attacking a player damage: the damage per second - light_resistant: light wont cause damage on day drop: the drop item of the monster drop_count: the number of items that are dropped armor: the armor (integer)(3=lowest; 1=highest)(fleshy group is used) drawtype: "front" or "side" water_damage: the damage per second if the mob is in water lava_damage: the damage per second if the mob is in lava -2. mobs:register_animal(name, def) - This adds a animal to Minetest that will just walk arround - "name" is the name of the monster ("[modname]:[animalname]") - "def" is the same table as in register_monster but without these values: - -view_range - -run_velocity - -damage - -light_resistant - -armor - and it also has the field - -on_rightclick: its same as in minetest.register_entity() + light_damage: the damage per second if the mob is in light + on_rightclick: its same as in minetest.register_entity() 3. mobs:register_spawn(name, nodes, max_light, min_light, chance, mobs_per_30_block_radius) This function adds the spawning of an animal (without it the registered animals and monster won't spawn!) diff --git a/api.lua b/api.lua index e2f1d37..0a321e9 100644 --- a/api.lua +++ b/api.lua @@ -1,5 +1,5 @@ mobs = {} -function mobs:register_monster(name, def) +function mobs:register_mob(name, def) minetest.register_entity(name, { hp_max = def.hp_max, physical = true, @@ -12,13 +12,15 @@ function mobs:register_monster(name, def) walk_velocity = def.walk_velocity, run_velocity = def.run_velocity, damage = def.damage, - light_resistant = def.light_resistant, + light_damage = def.light_damage, water_damage = def.water_damage, lava_damage = def.lava_damage, drop = def.drop, drop_count = def.drop_count, armor = def.armor, drawtype = def.drawtype, + on_rightclick = def.on_rightclick, + type = def.type, timer = 0, attack = {player=nil, dist=nil}, @@ -82,11 +84,11 @@ function mobs:register_monster(name, def) end local do_env_damage = function(self) - if not self.light_resistant and minetest.env:get_timeofday() > 0.2 and minetest.env:get_timeofday() < 0.8 and minetest.env:get_node_light(self.object:getpos()) > 3 then + if self.light_damage and self.light_damage ~= 0 and minetest.env:get_timeofday() > 0.2 and minetest.env:get_timeofday() < 0.8 and minetest.env:get_node_light(self.object:getpos()) > 3 then self.object:punch(self.object, 1.0, { full_punch_interval=1.0, groupcaps={ - fleshy={times={[self.armor]=1/1}}, + fleshy={times={[self.armor]=1/self.light_damage}}, } }, nil) end @@ -116,21 +118,23 @@ function mobs:register_monster(name, def) do_env_damage(self) end - for _,player in pairs(minetest.get_connected_players()) do - local s = self.object:getpos() - local p = player:getpos() - local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5 - if dist < self.view_range then - if self.attack.dist then - if self.attack.dist < dist then + if self.type == "monster" then + for _,player in pairs(minetest.get_connected_players()) do + local s = self.object:getpos() + local p = player:getpos() + local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5 + if dist < self.view_range then + if self.attack.dist then + if self.attack.dist < dist then + self.state = "attack" + self.attack.player = player + self.attack.dist = dist + end + else self.state = "attack" self.attack.player = player self.attack.dist = dist end - else - self.state = "attack" - self.attack.player = player - self.attack.dist = dist end end end @@ -257,160 +261,6 @@ function mobs:register_monster(name, def) }) end -function mobs:register_animal(name, def) - minetest.register_entity(name, { - hp_max = def.hp_max, - physical = true, - collisionbox = def.collisionbox, - visual = def.visual, - visual_size = def.visual_size, - textures = def.textures, - makes_footstep_sound = def.makes_footstep_sound, - walk_velocity = def.walk_velocity, - drop = def.drop, - on_rightclick = def.on_rightclick, - drop_count = def.drop_count, - drawtype = def.drawtype, - water_damage = def.water_damage, - lava_damage = def.lava_damage, - - timer = 0, - state = "stand", - old_y = nil, - - - set_velocity = function(self, v) - local yaw = self.object:getyaw() - if self.drawtype == "side" then - yaw = yaw+(math.pi/2) - end - local x = math.sin(yaw) * -v - local z = math.cos(yaw) * v - self.object:setvelocity({x=x, y=self.object:getvelocity().y, z=z}) - end, - - get_velocity = function(self) - local v = self.object:getvelocity() - return (v.x^2 + v.z^2)^(0.5) - end, - - on_step = function(self, dtime) - if self.object:getvelocity().y > 0.1 then - local yaw = self.object:getyaw() - if self.drawtype == "side" then - yaw = yaw+(math.pi/2) - end - local x = math.sin(yaw) * -2 - local z = math.cos(yaw) * 2 - self.object:setacceleration({x=x, y=-10, z=z}) - else - self.object:setacceleration({x=0, y=-10, z=0}) - end - - if self.object:getvelocity().y == 0 then - if not self.old_y then - self.old_y = self.object:getpos().y - else - local d = self.old_y - self.object:getpos().y - if d > 5 then - local damage = d-5 - self.object:punch(self.object, 1.0, { - full_punch_interval=1.0, - groupcaps={ - fleshy={times={[3]=1/damage}}, - } - }, nil) - end - self.old_y = self.object:getpos().y - end - end - - self.timer = self.timer+dtime - if self.timer < 1 then - return - end - self.timer = 0 - - if self.water_damage and self.water_damage ~= 0 and string.find(minetest.env:get_node(self.object:getpos()).name, "default:water") then - self.object:punch(self.object, 1.0, { - full_punch_interval=1.0, - groupcaps={ - fleshy={times={[3]=1/self.water_damage}}, - } - }, nil) - end - - if self.lava_damage and self.lava_damage ~= 0 and string.find(minetest.env:get_node(self.object:getpos()).name, "default:lava") then - self.object:punch(self.object, 1.0, { - full_punch_interval=1.0, - groupcaps={ - fleshy={times={[3]=1/self.lava_damage}}, - } - }, nil) - end - - if self.state == "stand" then - if math.random(1, 2) == 1 then - self.object:setyaw(self.object:getyaw()+((math.random(0,360)-180)/180*math.pi)) - end - if math.random(1, 100) <= 50 then - self.set_velocity(self, self.walk_velocity) - self.state = "walk" - end - elseif self.state == "walk" then - if math.random(1, 100) <= 30 then - self.object:setyaw(self.object:getyaw()+((math.random(0,360)-180)/180*math.pi)) - self.set_velocity(self, self.get_velocity(self)) - end - if math.random(1, 100) <= 10 then - self.set_velocity(self, 0) - self.state = "stand" - end - if self.get_velocity(self) <= 0.5 and self.object:getvelocity().y == 0 then - local v = self.object:getvelocity() - v.y = 5 - self.object:setvelocity(v) - end - end - end, - - on_activate = function(self, staticdata) - self.object:set_armor_groups({fleshy=3}) - self.object:setacceleration({x=0, y=-10, z=0}) - self.state = "stand" - self.object:setvelocity({x=0, y=self.object:getvelocity().y, z=0}) - self.object:setyaw(math.random(1, 360)/180*math.pi) - end, - - on_punch = function(self, hitter) - if self.object:get_hp() <= 0 then - if hitter and hitter:is_player() and hitter:get_inventory() then - for i=1,math.random(0,2)-1+self.drop_count do - hitter:get_inventory():add_item("main", ItemStack(self.drop)) - end - else - for i=1,math.random(0,2)-1+self.drop_count do - local obj = minetest.env:add_item(self.object:getpos(), self.drop) - if obj then - obj:get_luaentity().collect = true - local x = math.random(1, 5) - if math.random(1,2) == 1 then - x = -x - end - local z = math.random(1, 5) - if math.random(1,2) == 1 then - z = -z - end - obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z}) - end - end - end - end - end, - - }) -end - function mobs:register_spawn(name, nodes, max_light, min_light, chance, mobs_per_30_block_radius) minetest.register_abm({ nodenames = nodes, diff --git a/init.lua b/init.lua index 5e70c69..fb01f80 100644 --- a/init.lua +++ b/init.lua @@ -1,6 +1,7 @@ dofile(minetest.get_modpath("mobs").."/api.lua") -mobs:register_monster("mobs:dirt_monster", { +mobs:register_mob("mobs:dirt_monster", { + type = "monster", hp_max = 5, collisionbox = {-0.4, -1, -0.4, 0.4, 1, 0.4}, visual = "upright_sprite", @@ -17,10 +18,13 @@ mobs:register_monster("mobs:dirt_monster", { drawtype = "front", water_damage = 1, lava_damage = 5, + light_damage = 2, + on_rightclick = nil, }) mobs:register_spawn("mobs:dirt_monster", {"default:dirt_with_grass"}, 3, -1, 5000, 5) -mobs:register_monster("mobs:stone_monster", { +mobs:register_mob("mobs:stone_monster", { + type = "monster", hp_max = 10, collisionbox = {-0.4, -1, -0.4, 0.4, 1, 0.4}, visual = "upright_sprite", @@ -38,11 +42,13 @@ mobs:register_monster("mobs:stone_monster", { drawtype = "front", water_damage = 0, lava_damage = 0, + light_damage = 0, }) mobs:register_spawn("mobs:stone_monster", {"default:stone"}, 3, -1, 5000, 5) -mobs:register_monster("mobs:sand_monster", { +mobs:register_mob("mobs:sand_monster", { + type = "monster", hp_max = 3, collisionbox = {-0.4, -1, -0.4, 0.4, 1, 0.4}, visual = "upright_sprite", @@ -60,10 +66,12 @@ mobs:register_monster("mobs:sand_monster", { drawtype = "front", water_damage = 3, lava_damage = 1, + light_damage = 0, }) mobs:register_spawn("mobs:sand_monster", {"default:desert_sand"}, 20, -1, 5000, 5) -mobs:register_animal("mobs:sheep", { +mobs:register_mob("mobs:sheep", { + type = "animal", hp_max = 5, collisionbox = {-0.6, -0.625, -0.6, 0.6, 0.625, 0.6}, visual = "upright_sprite", @@ -71,11 +79,13 @@ mobs:register_animal("mobs:sheep", { textures = {"mobs_sheep.png", "mobs_sheep.png"}, makes_footstep_sound = true, walk_velocity = 1, + armor = 3, drop = "mobs:meat_raw", drop_count = 2, drawtype = "side", water_damage = 1, lava_damage = 5, + light_damage = 0, on_rightclick = function(self, clicker) if self.naked then @@ -110,7 +120,8 @@ minetest.register_craft({ cooktime = 5, }) -mobs:register_animal("mobs:rat", { +mobs:register_mob("mobs:rat", { + type = "animal", hp_max = 1, collisionbox = {-0.25, -0.175, -0.25, 0.25, 0.1, 0.25}, visual = "upright_sprite", @@ -118,11 +129,13 @@ mobs:register_animal("mobs:rat", { textures = {"mobs_rat.png", "mobs_rat.png"}, makes_footstep_sound = false, walk_velocity = 1, + armor = 3, drop = "", drop_count = 0, drawtype = "side", water_damage = 0, lava_damage = 1, + light_damage = 0, on_rightclick = function(self, clicker) if clicker:is_player() and clicker:get_inventory() then @@ -160,7 +173,8 @@ minetest.register_craft({ cooktime = 5, }) -mobs:register_monster("mobs:oerkki", { +mobs:register_mob("mobs:oerkki", { + type = "monster", hp_max = 8, collisionbox = {-0.4, -1, -0.4, 0.4, 1, 0.4}, visual = "upright_sprite", @@ -178,5 +192,6 @@ mobs:register_monster("mobs:oerkki", { light_resistant = true, water_damage = 1, lava_damage = 1, + light_damage = 0, }) mobs:register_spawn("mobs:oerkki", {"default:stone"}, 2, -1, 5000, 5)