1
0
mirror of https://codeberg.org/tenplus1/mobs_animal.git synced 2024-12-22 17:00:26 +01:00
mobs_animal/sheep.lua

528 lines
14 KiB
Lua
Raw Normal View History

2023-08-13 11:56:57 +02:00
2024-08-10 15:10:22 +02:00
-- translation and localize function
local S = minetest.get_translator("mobs_animal")
2022-10-11 12:09:19 +02:00
local random = math.random
2024-08-10 15:10:22 +02:00
-- sheep colour table
2016-04-15 15:59:43 +02:00
local all_colours = {
{"black", S("Black"), "#212121b0"}, -- referenced down in mobs:spawn
2016-06-11 12:14:08 +02:00
{"blue", S("Blue"), "#015dbb70"},
{"brown", S("Brown"), "#663300a0"}, -- referenced down in mobs:spawn
{"cyan", S("Cyan"), "#00b4bf70"},
2016-06-11 12:14:08 +02:00
{"dark_green", S("Dark Green"), "#005b0770"},
{"dark_grey", S("Dark Grey"), "#444444b0"}, -- referenced down in mobs:spawn
2016-06-11 12:14:08 +02:00
{"green", S("Green"), "#61ff0170"},
{"grey", S("Grey"), "#919191b0"}, -- referenced down in mobs:spawn
{"magenta", S("Magenta"), "#ed03ad70"},
2016-06-11 12:14:08 +02:00
{"orange", S("Orange"), "#ff840170"},
{"pink", S("Pink"), "#ff85c470"},
2016-06-11 12:14:08 +02:00
{"red", S("Red"), "#ff0000a0"},
{"violet", S("Violet"), "#5c00c970"},
{"white", S("White"), "#ffffffc0"}, -- referenced down in mobs:spawn
{"yellow", S("Yellow"), "#fff80070"}
2016-04-15 15:59:43 +02:00
}
-- Sheep by PilzAdam/K Pavel, texture converted to minetest by AMMOnym from Summerfield pack
2016-04-15 15:59:43 +02:00
for _, col in ipairs(all_colours) do
2016-04-15 15:59:43 +02:00
local drops_normal = {
{name = "mobs:mutton_raw", chance = 1, min = 1, max = 2},
{name = "wool:" .. col[1], chance = 1, min = 1, max = 1}
}
local drops_gotten = {
{name = "mobs:mutton_raw", chance = 1, min = 1, max = 2}
}
local function horn_texture_sel(horns, gotten, colr)
2023-07-21 09:48:37 +02:00
-- get override colours hex value from table
if colr then
for _2, col2 in ipairs(all_colours) do
if col2[1] == colr then
colr = col2[3]
break
end
end
end
local base_text = "mobs_sheep_base.png"
local wool_shave_text = "mobs_sheep_wool.png"
local shav_text = "mobs_sheep_shaved.png"
local horn_text = "mobs_sheep_horns.png"
local col_override = colr and colr or col[3]
local col_text = "^[multiply:" .. col_override
if gotten then
wool_shave_text = shav_text
col_text = ""
end
-- results in unneccesary brackets for shaved but these are ignored by engine
local textures = base_text .. "^(" .. wool_shave_text .. col_text .. ")"
if horns then
textures = base_text .. "^" .. horn_text .. "^(" .. wool_shave_text
.. col_text .. ")"
end
return textures
end
mobs:register_mob("mobs_animal:sheep_" .. col[1], {
2019-01-24 12:11:25 +01:00
stay_near = {"farming:straw", 10},
stepheight = 0.6,
2016-04-15 15:59:43 +02:00
type = "animal",
passive = true,
hp_min = 8,
hp_max = 10,
armor = 200,
collisionbox = {-0.5, -1, -0.5, 0.5, 0.3, 0.5},
visual = "mesh",
mesh = "mobs_sheep.b3d",
textures = {
{"mobs_sheep_base.png^(mobs_sheep_wool.png^[multiply:" .. col[3] .. ")"}
2016-04-15 15:59:43 +02:00
},
gotten_texture = {"mobs_sheep_base.png^mobs_sheep_shaved.png"},
2016-04-15 15:59:43 +02:00
makes_footstep_sound = true,
2024-08-10 15:10:22 +02:00
sounds = {random = "mobs_sheep"},
2016-04-15 15:59:43 +02:00
walk_velocity = 1,
run_velocity = 2,
runaway = true,
jump = true,
jump_height = 6,
2018-09-14 18:12:22 +02:00
pushable = true,
drops = drops_normal,
water_damage = 0.01,
2016-04-15 15:59:43 +02:00
lava_damage = 5,
light_damage = 0,
animation = {
2024-08-10 15:10:22 +02:00
speed_normal = 15, speed_run = 15,
stand_start = 0, stand_end = 80,
walk_start = 81, walk_end = 100,
-- no death animation so we'll re-use 2 standing frames at a speed of 1 fps
-- and have mob rotate while dying.
die_start = 1, die_end = 2, die_speed = 1,
die_loop = false, die_rotate = true
2016-04-15 15:59:43 +02:00
},
follow = {
"farming:wheat", "default:grass_1", "farming:barley",
"farming:oat", "farming:rye"
},
2016-04-15 15:59:43 +02:00
view_range = 8,
replace_rate = 10,
replace_what = {
{"group:grass", "air", -1},
{"default:dirt_with_grass", "default:dirt", -2}
},
2016-04-15 15:59:43 +02:00
fear_height = 3,
2022-09-27 10:15:59 +02:00
2017-07-09 15:09:34 +02:00
on_replace = function(self, pos, oldnode, newnode)
self.food = (self.food or 0) + 1
-- if sheep replaces 8x grass then it regrows wool
if self.food >= 8 then
self.food = 0
self.gotten = false
self.drops = drops_normal
local textures = horn_texture_sel(self.attribute_horns, self.gotten)
self.object:set_properties({textures = {textures}})
-- base_texture must be kept up to date for static_save so horns persist
-- visually on server/game restart
self.base_texture = {textures}
end
2017-07-09 15:09:34 +02:00
end,
2022-09-27 10:15:59 +02:00
on_breed = function(parent1, parent2)
-- simple truth table P1/P2 horns/no_horns
local breed_out = {
2023-05-28 13:19:49 +02:00
["P1_N"] = {["P2_N"] = 5 ,["P2_H"] = 50},
["P1_H"] = {["P2_N"] = 50,["P2_H"] = 95}
}
2023-05-28 13:19:49 +02:00
local p1 = parent1.attribute_horns and "P1_H" or "P1_N"
local p2 = parent2.attribute_horns and "P2_H" or "P2_N"
local horn_chance = breed_out[p1][p2]
local horns = random(100) <= horn_chance
local pos = parent1.object:get_pos()
-- can't see an easy way to pass horn attribute into
-- child entity def, handle all spawning here, unfortunate
-- code replication from mobs_redo api.lua line 1497+
-- replace "self" with "parent1" and few other minor tweaks
----------------------------------------------------
pos.y = pos.y + 0.5 -- spawn child a little higher
local mob = minetest.add_entity(pos, parent1.name)
local ent2 = mob:get_luaentity()
-- remove horns from parents' texture string, lambs dont have horns
local textures = string.gsub(parent1.base_texture[1],
"%^mobs_sheep_horns.png", "")
-- using specific child texture (if found)
if parent1.child_texture then
textures = parent1.child_texture[1]
end
-- and resize to half height
mob:set_properties({
textures = {textures},
visual_size = {
2024-08-10 15:10:22 +02:00
x = parent1.base_size.x * .5, y = parent1.base_size.y * .5
},
collisionbox = {
2024-08-10 15:10:22 +02:00
parent1.base_colbox[1] * .5, parent1.base_colbox[2] * .5,
parent1.base_colbox[3] * .5, parent1.base_colbox[4] * .5,
parent1.base_colbox[5] * .5, parent1.base_colbox[6] * .5
},
selectionbox = {
2024-08-10 15:10:22 +02:00
parent1.base_selbox[1] * .5, parent1.base_selbox[2] * .5,
parent1.base_selbox[3] * .5, parent1.base_selbox[4] * .5,
parent1.base_selbox[5] * .5, parent1.base_selbox[6] * .5
}
})
-- tamed and owned by parents' owner
ent2.child = true
ent2.tamed = true
ent2.owner = parent1.owner
ent2.attribute_horns = horns
-- stop mobs_redo api from spawning child
return false
end,
2024-03-16 08:28:44 +01:00
-- fix any issue with horns by re-checking
on_spawn = function(self)
if self.child then return end -- baby sheep dont have horns
2024-03-16 08:28:44 +01:00
local textures = horn_texture_sel(self.attribute_horns, self.gotten)
self.object:set_properties({textures = {textures}})
self.base_texture = {textures}
end,
on_grown = function(self)
-- add the horns if we have horns when fully grown
local textures = horn_texture_sel(self.attribute_horns, self.gotten)
self.object:set_properties({textures = {textures}})
self.base_texture = {textures}
2023-05-27 20:29:33 +02:00
local pos = self.object:get_pos()
2023-10-05 14:46:54 +02:00
local prop = self.object:get_properties()
2023-05-27 20:29:33 +02:00
2023-10-05 14:46:54 +02:00
pos.y = pos.y + (prop.collisionbox[2] * -1) - 0.4
2023-05-27 20:29:33 +02:00
self.object:set_pos(pos)
-- jump slightly when fully grown so as not to fall into ground
self.object:set_velocity({x = 0, y = 2, z = 0 })
end,
2016-04-15 15:59:43 +02:00
on_rightclick = function(self, clicker)
-- are we feeding?
2016-04-15 15:59:43 +02:00
if mobs:feed_tame(self, clicker, 8, true, true) then
if not self.child then
local textures = horn_texture_sel(self.attribute_horns, self.gotten)
self.object:set_properties({textures = {textures}})
self.base_texture = {textures}
end
2020-12-06 09:57:16 +01:00
--if fed 7 times then sheep regrows wool
if self.food and self.food > 6 then
self.gotten = false
self.drops = drops_normal
2016-04-15 15:59:43 +02:00
local textures = horn_texture_sel(self.attribute_horns, self.gotten)
self.object:set_properties({textures = {textures}})
self.base_texture = {textures}
2016-04-15 15:59:43 +02:00
end
return
end
local item = clicker:get_wielded_item()
local itemname = item:get_name()
2018-01-22 17:09:22 +01:00
local name = clicker:get_player_name()
2016-04-15 15:59:43 +02:00
-- are we giving a haircut>
2016-04-15 15:59:43 +02:00
if itemname == "mobs:shears" then
2024-08-10 15:10:22 +02:00
if self.gotten ~= false or self.child ~= false
or name ~= self.owner or not minetest.get_modpath("wool") then
2016-04-15 15:59:43 +02:00
return
end
self.gotten = true -- shaved
self.drops = drops_gotten
2016-04-15 15:59:43 +02:00
local obj = minetest.add_item(
2017-10-09 16:16:05 +02:00
self.object:get_pos(),
2022-10-11 12:09:19 +02:00
ItemStack("wool:" .. col[1] .. " " .. random(3))
2016-04-15 15:59:43 +02:00
)
if obj then
2022-10-11 12:09:19 +02:00
obj:set_velocity({
2024-08-10 15:10:22 +02:00
x = random(-1, 1), y = 5, z = random(-1, 1)})
2016-04-15 15:59:43 +02:00
end
item:add_wear(650) -- 100 uses
clicker:set_wielded_item(item)
local textures = horn_texture_sel(self.attribute_horns, self.gotten)
self.object:set_properties({textures = {textures}})
self.base_texture = {textures}
2016-04-15 15:59:43 +02:00
return
end
-- are we coloring?
2016-04-15 15:59:43 +02:00
if itemname:find("dye:") then
2024-08-10 15:10:22 +02:00
if self.gotten == false and self.child == false
and self.tamed == true and name == self.owner then
2016-04-15 15:59:43 +02:00
local colr = string.split(itemname, ":")[2]
for _,c in pairs(all_colours) do
-- only dye if colour option available and sheep not same colour
2024-08-10 15:10:22 +02:00
if c[1] == colr and self.name ~= "mobs_animal:sheep_" .. colr then
2016-04-15 15:59:43 +02:00
2017-10-09 16:16:05 +02:00
local pos = self.object:get_pos()
2016-04-15 15:59:43 +02:00
-- add new coloured sheep
2016-04-15 15:59:43 +02:00
local mob = minetest.add_entity(pos, "mobs_animal:sheep_" .. colr)
local ent = mob:get_luaentity()
if ent then
2024-07-22 15:57:55 +02:00
local prop = self.object:get_properties()
-- add old sheep attributes
ent.attribute_horns = self.attribute_horns
2024-07-22 15:57:55 +02:00
ent._nametag = prop.nametag
ent.owner = name
ent.tamed = true
ent.protected = self.protected
ent.fire_damage = self.fire_damage
-- set sheep texture with colour override since we're
-- calling function inside different sheep
local textures = horn_texture_sel(
self.attribute_horns, false, colr)
ent.base_texture = {textures}
ent.object:set_properties({
2024-07-22 15:57:55 +02:00
textures = {textures}
})
-- remove old sheep
self.object:remove()
-- take item
if not mobs.is_creative(clicker:get_player_name()) then
item:take_item()
clicker:set_wielded_item(item)
end
break
2016-04-15 15:59:43 +02:00
end
end
end
end
return
end
2016-11-08 17:11:49 +01:00
-- protect mod with mobs:protector item
2017-04-27 15:00:57 +02:00
if mobs:protect(self, clicker) then return end
2016-11-08 17:11:49 +01:00
2016-04-15 15:59:43 +02:00
--are we capturing?
2017-04-27 15:00:57 +02:00
if mobs:capture_mob(self, clicker, 0, 5, 60, false, nil) then return end
2016-04-15 15:59:43 +02:00
end
})
2022-09-27 10:15:59 +02:00
-- spawn egg
mobs:register_egg("mobs_animal:sheep_"..col[1], S("@1 Sheep", col[2]),
"wool_" .. col[1] .. ".png^mobs_sheep_inv.png")
2016-04-15 15:59:43 +02:00
-- compatibility
mobs:alias_mob("mobs:sheep_" .. col[1], "mobs_animal:sheep_" .. col[1])
2016-04-15 15:59:43 +02:00
end
2024-08-10 15:10:22 +02:00
-- where to spawn
if not mobs.custom_spawn_animal then
2022-09-27 10:15:59 +02:00
local max_ht = 400
local spawn_on = {"default:dirt_with_grass", "ethereal:green_dirt"}
local mod_ethereal = minetest.get_modpath("ethereal")
local spawn_chance = mod_ethereal and 12000 or 8000
2022-09-27 10:15:59 +02:00
mobs:spawn({
name = "mobs_animal:sheep_white",
nodes = spawn_on,
neighbors = {"group:grass"},
2022-09-27 10:15:59 +02:00
min_light = 14,
interval = 60,
chance = spawn_chance,
2022-09-27 10:15:59 +02:00
min_height = 0,
max_height = max_ht,
2022-10-11 12:09:19 +02:00
day_toggle = true,
active_object_count = 3,
-- custom function to spawn sheep herds around main mob
on_spawn = function(self, pos)
local nat_colors = {-- reference for all_colours table
2023-07-21 09:48:37 +02:00
["white"] = 14,
["grey"] = 8,
["dark_grey"] = 6,
2023-07-21 09:48:37 +02:00
["black"] = 1,
["brown"] = 3
}
local function random_sheep(pos, first)
local types = "white"
local color = all_colours[nat_colors["white"]][3]
-- Random colour selection
-- black(1/8), brown(1/16), dark_grey(1/32), grey(1/32), 6/8 white
local ran_col = random(32)
if ran_col <= 8 then
if ran_col == 1 then
types = "dark_grey"
elseif ran_col == 2 then
types = "grey"
elseif ran_col >= 3 and ran_col <= 4 then
types = "brown"
else
types = "black"
end
end
-- 1/4 chance of lamb
local lamb
if not first then
lamb = random(4) == 1
end
-- store returned entity data from mobs:add_mob, "nil" indicates the
-- rules in mobs:add_mob stopped mob from being spawned/created.
local entity = mobs:add_mob(pos,
{name = "mobs_animal:sheep_" .. types, child = lamb})
2024-08-10 15:10:22 +02:00
-- nil check
if not entity then return end
2023-09-29 16:31:04 +02:00
if not lamb then
-- Set horns attribute, lower height will be rarer.
-- This wont affect mobs spawned by egg those only spawn hornless sheep.
local horns = random(max_ht) <= pos.y
if horns then
local text = "mobs_sheep_base.png^mobs_sheep_horns.png"
.."^(mobs_sheep_wool.png^[multiply:"
.. all_colours[nat_colors[types]][3] .. ")"
entity.object:set_properties({textures = {text}})
entity.base_texture = {text}
entity.attribute_horns = horns
end
end
end
-- First Sheep, Randomize color/horns
self.object:remove()
random_sheep(pos, true)
-- Rest of herd
2022-10-11 12:09:19 +02:00
local nods = minetest.find_nodes_in_area_under_air(
{x = pos.x - 4, y = pos.y - 3, z = pos.z - 4},
{x = pos.x + 4, y = pos.y + 3, z = pos.z + 4}, spawn_on)
2022-10-11 12:09:19 +02:00
if nods and #nods > 0 then
-- min herd of 3
local iter = math.min(#nods, 3)
for n = 1, iter do
2022-10-11 13:05:56 +02:00
local pos2 = nods[random(#nods)]
2022-10-11 12:09:19 +02:00
2022-10-11 13:05:56 +02:00
pos2.y = pos2.y + 2
if minetest.get_node(pos2).name == "air" then
-- Add a sheep or lamb
random_sheep(pos2, false)
2022-10-11 13:05:56 +02:00
end
2022-10-11 12:09:19 +02:00
end
end
end
2022-09-27 10:15:59 +02:00
})
end
2016-04-15 15:59:43 +02:00
2024-08-10 15:10:22 +02:00
-- compatibility with older mobs mod
2024-08-10 15:10:22 +02:00
mobs:alias_mob("mobs:sheep", "mobs_animal:sheep_white")
-- raw mutton
2024-08-10 15:10:22 +02:00
minetest.register_craftitem(":mobs:mutton_raw", {
2024-07-26 18:10:17 +02:00
description = S("Raw Mutton"),
inventory_image = "mobs_mutton_raw.png",
on_use = minetest.item_eat(2),
2024-07-26 18:10:17 +02:00
groups = {food_meat_raw = 1, food_mutton_raw = 1}
})
2024-07-26 18:10:17 +02:00
mobs.add_eatable("mobs:mutton_raw", 2)
2024-08-10 15:10:22 +02:00
-- cooked mutton and recipe
minetest.register_craftitem(":mobs:mutton_cooked", {
2024-07-26 18:10:17 +02:00
description = S("Cooked Mutton"),
inventory_image = "mobs_mutton_cooked.png",
on_use = minetest.item_eat(6),
2024-07-26 18:10:17 +02:00
groups = {food_meat = 1, food_mutton = 1}
})
2024-07-26 18:10:17 +02:00
mobs.add_eatable("mobs:mutton_cooked", 6)
minetest.register_craft({
type = "cooking",
output = "mobs:mutton_cooked",
recipe = "mobs:mutton_raw",
2022-01-20 09:55:41 +01:00
cooktime = 5
})