2022-01-15 20:14:31 -05:00
|
|
|
-- LUALOCALS < ---------------------------------------------------------
|
2022-01-16 15:45:46 -05:00
|
|
|
local math, minetest, nodecore, pairs, string, vector
|
|
|
|
= math, minetest, nodecore, pairs, string, vector
|
|
|
|
local math_abs, math_random, string_format
|
|
|
|
= math.abs, math.random, string.format
|
2022-01-15 20:14:31 -05:00
|
|
|
-- LUALOCALS > ---------------------------------------------------------
|
|
|
|
|
|
|
|
local modname = minetest.get_current_modname()
|
|
|
|
|
2022-01-16 15:39:03 -05:00
|
|
|
local metakey = "catbreed"
|
|
|
|
|
|
|
|
minetest.register_abm({
|
|
|
|
label = "cat breeding",
|
|
|
|
nodenames = {"group:" .. modname .. "_cat"},
|
|
|
|
interval = 5,
|
|
|
|
chance = 1,
|
|
|
|
action = function(pos)
|
|
|
|
local found = #nodecore.find_nodes_around(pos,
|
|
|
|
"group:" .. modname .. "_cat", 3)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local qty = meta:get_float(metakey) or 0
|
|
|
|
qty = qty + 2 ^ -math_abs(2 - found)
|
|
|
|
meta:set_float(metakey, qty)
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2022-01-15 20:14:31 -05:00
|
|
|
local purrtimes = {
|
|
|
|
1.676,
|
|
|
|
1.617,
|
|
|
|
1.642,
|
|
|
|
1.655
|
|
|
|
}
|
|
|
|
|
|
|
|
local hashpos = minetest.hash_node_position
|
|
|
|
local purring = {}
|
|
|
|
|
|
|
|
local purrgain = 0.15
|
|
|
|
|
|
|
|
local function purr(pos)
|
2022-01-18 20:58:48 -05:00
|
|
|
purring[hashpos(pos)] = nodecore.gametime
|
2022-01-16 15:39:03 -05:00
|
|
|
|
2022-01-15 20:14:31 -05:00
|
|
|
local fade = math_random(1, 10) == 1
|
|
|
|
if not fade then
|
|
|
|
local nearby
|
|
|
|
for _, player in pairs(minetest.get_connected_players()) do
|
|
|
|
if not nearby then
|
|
|
|
local ppos = player:get_pos()
|
2022-01-15 23:42:20 -05:00
|
|
|
ppos.y = ppos.y + player:get_properties().eye_height
|
2022-01-15 20:14:31 -05:00
|
|
|
local diff = vector.subtract(pos, ppos)
|
|
|
|
local dsqr = vector.dot(diff, diff)
|
|
|
|
nearby = nearby or dsqr < 25
|
|
|
|
end
|
|
|
|
end
|
|
|
|
fade = not nearby
|
|
|
|
end
|
2022-01-16 15:39:03 -05:00
|
|
|
|
|
|
|
local id = math_random(1, #purrtimes)
|
2022-01-15 20:14:31 -05:00
|
|
|
local sh = nodecore.sound_play("nc_cats_purr_" .. id, {
|
|
|
|
pos = pos,
|
|
|
|
gain = purrgain,
|
|
|
|
not_ephemeral = fade
|
|
|
|
})
|
|
|
|
if fade then
|
|
|
|
minetest.sound_fade(sh, -purrgain / purrtimes[id], 0)
|
|
|
|
return minetest.after(purrtimes[id], function()
|
|
|
|
purring[hashpos(pos)] = nil
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
nodecore.dnt_set(pos, modname .. ":purr", purrtimes[id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
nodecore.register_dnt({
|
|
|
|
name = modname .. ":purr",
|
|
|
|
nodenames = {"group:" .. modname .. "_cat"},
|
|
|
|
action = purr
|
|
|
|
})
|
|
|
|
|
2022-01-16 15:39:03 -05:00
|
|
|
local function purrstart(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local qty = meta:get_float(metakey) or 0
|
2022-01-16 15:45:46 -05:00
|
|
|
local roll = math_random(20, 2000)
|
|
|
|
nodecore.log("action", string_format("cat at %s breed %0.2f, roll %d, %s",
|
|
|
|
minetest.pos_to_string(pos), qty, roll,
|
|
|
|
qty > roll and "success" or "failed"))
|
|
|
|
if qty > roll then
|
2022-01-16 15:39:03 -05:00
|
|
|
nodecore.item_eject(pos, modname .. ":prill", 5)
|
|
|
|
nodecore.witness(pos, "cat eject prill")
|
|
|
|
end
|
|
|
|
meta:set_float(metakey, 0)
|
|
|
|
|
|
|
|
return purr(pos)
|
|
|
|
end
|
|
|
|
|
2022-01-18 20:58:48 -05:00
|
|
|
local function notpurring(pos)
|
|
|
|
local purrtime = purring[hashpos(pos)]
|
|
|
|
if purrtime and purrtime < nodecore.gametime - 2 then
|
|
|
|
purrtime = nil
|
|
|
|
purring[hashpos(pos)] = nil
|
|
|
|
end
|
|
|
|
return not purrtime and {}
|
|
|
|
end
|
2022-01-15 23:50:55 -05:00
|
|
|
|
2022-01-15 20:14:31 -05:00
|
|
|
nodecore.register_craft({
|
|
|
|
label = "pet cat",
|
|
|
|
action = "pummel",
|
|
|
|
toolgroups = {thumpy = 1},
|
|
|
|
indexkeys = {"group:" .. modname .. "_cat"},
|
2022-01-15 23:50:55 -05:00
|
|
|
check = notpurring,
|
2022-01-15 20:14:31 -05:00
|
|
|
nodes = {
|
2022-01-15 23:56:19 -05:00
|
|
|
{match = {
|
|
|
|
groups = {[modname .. "_cat"] = true},
|
|
|
|
stacked = false
|
|
|
|
}}
|
2022-01-15 20:14:31 -05:00
|
|
|
},
|
2022-01-16 15:39:03 -05:00
|
|
|
after = purrstart
|
2022-01-15 20:14:31 -05:00
|
|
|
})
|
2022-01-15 23:50:55 -05:00
|
|
|
|
|
|
|
nodecore.register_ambiance({
|
|
|
|
label = modname .. " ambiance",
|
|
|
|
nodenames = {"group:" .. modname .. "_cat"},
|
|
|
|
interval = 5,
|
|
|
|
chance = 5,
|
|
|
|
check = notpurring,
|
|
|
|
sound_name = modname .. "_mew",
|
|
|
|
sound_gain = 0.5
|
|
|
|
})
|