Files
claudeandClaude Opus 5.5 82433b2d65 Camera twice as far back with a matching narrower FOV
CAMERA_DIST is now the strata_camera_distance setting (default 44,
16..80) and CAMERA_FOV is derived from it so the plane always shows
VIEW_HEIGHT (21.5) nodes top to bottom: about 28 degrees at 44. The pan
needed to aim at the edge of reach drops from ~22 to ~11 degrees.
Sound hearing distance follows the distance (CAMERA_DIST + 18); the
shadow slab, POINT_RANGE, view area and test area were already derived.

Needs a hands-on check in the client for feel.

Closes #71

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 01:25:05 -05:00

156 lines
5.1 KiB
Lua

-- Strata feedback: node sounds, block-break particles and hit flashes.
--
-- * Node sounds come in sets (stone, dirt, wood, leaves) made by
-- tools/gen_sounds.py. strata.node_sounds(set) builds a node `sounds`
-- table. Nodes registered without one get a set from their dig group once
-- all mods have loaded, so new nodes sound right by default.
-- * The client plays dig and dug sounds and its own dust for the player who
-- digs. Everyone else gets the dug sound from the engine; the particles
-- below are spawned by the server so every player sees a block break, on
-- the camera side of the plane.
-- * strata.flash(obj) tints an entity for a moment, for hits on things that
-- the engine's damage_texture_modifier doesn't cover (the avatar).
local DIG_GAIN = 0.6
local DUG_GAIN = 0.8
local PLACE_GAIN = 0.7
local PLACE_PITCH = 1.25
local FOOTSTEP_GAIN = 0.25
local FOOTSTEP_PITCH = 0.8
local DUST_AMOUNT = 12
local DUST_TIME = 0.05
local DUST_FRONT = 0.55 -- particles start this far in front of the node (towards the camera)
local DUST_SPEED = 2.5 -- nodes/s, sideways and upwards
local DUST_GRAVITY = 12
local DUST_LIFE = {0.4, 0.8}
local DUST_SIZE = {0.8, 1.6}
-- max_hear_distance for sounds heard from the camera, CAMERA_DIST away
local DUST_HEAR = strata.CAMERA_DIST + 18
local FLASH_MOD = "^[colorize:#ff3030:140"
local FLASH_TIME = 0.2
-- Dig group -> sound set, in priority order
local GROUP_SETS = {
{"leaves", "leaves"},
{"cracky", "stone"},
{"choppy", "wood"},
{"crumbly", "dirt"},
{"snappy", "leaves"},
{"dig_immediate", "leaves"},
}
local SETS = {stone = true, dirt = true, wood = true, leaves = true}
strata.SOUND_HEAR_DISTANCE = DUST_HEAR
--- A node `sounds` table for a sound set: "stone", "dirt", "wood" or
-- "leaves". The footstep is the dig sound, quieter and lower.
function strata.node_sounds(set)
assert(SETS[set], "unknown sound set " .. tostring(set))
local dig = "strata_dig_" .. set
local dug = "strata_dug_" .. set
return {
dig = {name = dig, gain = DIG_GAIN},
dug = {name = dug, gain = DUG_GAIN},
place = {name = dug, gain = PLACE_GAIN, pitch = PLACE_PITCH},
footstep = {name = dig, gain = FOOTSTEP_GAIN, pitch = FOOTSTEP_PITCH},
}
end
--- The sound set a node with these groups should use, or nil.
function strata.sound_set_for_groups(groups)
for _, gs in ipairs(GROUP_SETS) do
if (groups[gs[1]] or 0) > 0 then
return gs[2]
end
end
if (groups.wall or 0) > 0 then
return "stone"
end
end
core.register_on_mods_loaded(function()
for name, def in pairs(core.registered_nodes) do
if def.sounds == nil or next(def.sounds) == nil then
local set = strata.sound_set_for_groups(def.groups or {})
if set then
core.override_item(name, {sounds = strata.node_sounds(set)})
end
end
end
end)
--------------------------------------------------------------------------
-- Block-break dust
--------------------------------------------------------------------------
--- Spawn break particles textured from `node` at pos, in front of it.
-- Returns the particle spawner id.
function strata.dig_particles(pos, node)
local def = core.registered_nodes[node.name]
if not def or def.drawtype == "airlike" then
return
end
local z = pos.z - DUST_FRONT
return core.add_particlespawner({
amount = DUST_AMOUNT,
time = DUST_TIME,
minpos = {x = pos.x - 0.4, y = pos.y - 0.4, z = z},
maxpos = {x = pos.x + 0.4, y = pos.y + 0.4, z = z},
minvel = {x = -DUST_SPEED, y = 0, z = 0},
maxvel = {x = DUST_SPEED, y = DUST_SPEED, z = 0},
minacc = {x = 0, y = -DUST_GRAVITY, z = 0},
maxacc = {x = 0, y = -DUST_GRAVITY, z = 0},
minexptime = DUST_LIFE[1],
maxexptime = DUST_LIFE[2],
minsize = DUST_SIZE[1],
maxsize = DUST_SIZE[2],
collisiondetection = false,
node = {name = node.name, param2 = node.param2 or 0},
})
end
core.register_on_dignode(function(pos, oldnode)
strata.dig_particles(pos, oldnode)
end)
--------------------------------------------------------------------------
-- Hit flash
--------------------------------------------------------------------------
local flash_tokens = setmetatable({}, {__mode = "k"})
--- Tint an entity red for FLASH_TIME. Repeated hits extend the flash.
function strata.flash(obj)
if not obj or not obj:get_pos() then
return
end
local token = (flash_tokens[obj] or 0) + 1
flash_tokens[obj] = token
obj:set_texture_mod(FLASH_MOD)
core.after(FLASH_TIME, function()
if flash_tokens[obj] == token and obj:get_pos() then
obj:set_texture_mod("")
end
end)
end
strata.FLASH_MOD = FLASH_MOD
strata.FLASH_TIME = FLASH_TIME
--------------------------------------------------------------------------
-- Place sounds for custom on_place handlers
--------------------------------------------------------------------------
--- Play a node's place sound at pos for everyone nearby. For on_place
-- handlers that set nodes themselves, where neither the client nor the
-- engine plays it.
function strata.play_place_sound(pos, nodename)
local def = core.registered_nodes[nodename]
local spec = def and def.sounds and def.sounds.place
if not spec then
return
end
return core.sound_play(spec, {pos = pos, max_hear_distance = DUST_HEAR}, true)
end