yl_dice/internal.lua
2024-03-19 20:08:20 +01:00

170 lines
4.4 KiB
Lua

local COOLDOWN = 1.8
local PARTICLE_TIME = COOLDOWN - 0.2
-- can't be bothered to figure out math for this
-- map from param2 rotation to the face value
local V = {
1, 1, 1, 1,
2, 3, 5, 4,
5, 4, 2, 3,
4, 2, 3, 5,
3, 5, 4, 2,
6, 6, 6, 6,
}
local color_values = {
[0] = "#ffffff",
"#d94257",
"#405fdc",
"#9c40dc",
"#dc6540",
"#f7d96e",
"#86d942",
"#42d9b6",
}
local function roll_dice(pos, node)
if not node then
node = minetest.get_node(pos)
end
local timer = minetest.get_node_timer(pos)
if timer:is_started() then
return
end
timer:start(COOLDOWN)
local color = math.floor(node.param2 / 32)
local facedir = math.random(0, 23)
node.param2 = color * 32 + facedir
minetest.swap_node(pos, node)
local color_string = color_values[color]
local spawner_def = {
size = 0.6,
amount = 42,
time = 0.7,
glow = 10,
pos = {
min = vector.add(pos, vector.new(-0.5, -0.5, -0.5)),
max = vector.add(pos, vector.new(0.5, 0.5, 0.5)),
},
vel = {
min = { x = 2, y = 3, z = 2 },
max = { x = -2, y = 1, z = -2 },
},
acc = {
min = { x = 0, y = 10, z = 0 },
max = { x = 0, y = 10, z = 0 },
},
exptime = { min = 0.2, max = 0.5, },
vertical = false,
texture = "yl_dice_6sides.png^[multiply:" .. color_string,
animation = {
type = "sheet_2d",
frames_w = 2,
frames_h = 3,
frame_length = 0.1,
}
}
minetest.add_particlespawner(spawner_def)
local val = V[facedir + 1] or 0
local particle_def = {
expirationtime = PARTICLE_TIME,
pos = vector.add(pos, vector.new(0.0, 0.8, 0.0)),
velocity = { x = 0, y = 0.1, z = 0 },
glow = 14,
texture = ("yl_dice_%d.png^[multiply:" .. color_string):format(val),
size = 4,
}
minetest.add_particle(particle_def)
local spec = "default_dig_cracky" --"yl_dice_roll"
local parameters = {
pos = pos,
max_hear_distance = 32, -- default
pitch = 1.0 + 0.1*math.random(),
}
minetest.sound_play(spec, parameters, true)
return val
end
minetest.register_node("yl_dice:dice", {
description = "Dice",
drawtype = "mesh",
paramtype = "light",
paramtype2 = "colorfacedir",
mesh = "yl_dice6.obj",
tiles = {
"yl_dice_6sides.png",
},
drop = {
items = {
{ items = { "yl_dice:dice" }, inherit_color = true },
}
},
inventory_image = "yl_dice_3.png",
wield_image = "yl_dice_3.png",
groups = { dig_immediate = 2 },
palette = "yl_dice_6_palette.png",
is_ground_content = false,
on_construct = function(pos)
local val = roll_dice(pos)
minetest.log("action", ("[yl_dice] placed d6 has rolled %s at %s"):format(val, minetest.pos_to_string(pos)))
end,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local val = roll_dice(pos, node)
if val then
-- `val` is nil when cooldown has not ended yet
local name = clicker and clicker:is_player() and clicker:get_player_name() or "UNKNOWN"
minetest.log("action", ("[yl_dice] %s has rolled %s by using d6 at %s"):format(name, val, minetest.pos_to_string(pos)))
end
end,
})
local BASE_BLOCK
if minetest.registered_items["yl_nether:ivory_block"] then
BASE_BLOCK = "yl_nether:ivory_block"
end
if BASE_BLOCK then
minetest.register_craft({
type = "shaped",
output = minetest.itemstring_with_palette("yl_dice:dice", 0),
recipe = {
{ BASE_BLOCK, "dye:black", "" },
{ "", "", "" },
{ "", "", "" },
}
})
end
if minetest.registered_items["dye:red"] then
local colors = {
"red",
"blue",
"violet",
"orange",
"yellow",
"green",
"cyan",
}
for i, color in ipairs(colors) do
minetest.register_craft({
type = "shaped",
output = minetest.itemstring_with_palette("yl_dice:dice", 32 * i),
recipe = {
{ "yl_dice:dice", "dye:" .. color, "" },
{ "", "", "" },
{ "", "", "" },
}
})
end
end