]> git.lizzy.rs Git - minetest.git/commitdiff
Implement tool use sounds
authorsfan5 <sfan5@live.de>
Sun, 11 Sep 2022 19:16:17 +0000 (21:16 +0200)
committersfan5 <sfan5@live.de>
Fri, 30 Sep 2022 12:02:14 +0000 (14:02 +0200)
doc/lua_api.txt
games/devtest/mods/soundstuff/init.lua
src/client/game.cpp
src/itemdef.cpp
src/itemdef.h
src/script/common/c_content.cpp

index 10c6240332d8f5ffeae1e1b3c08680f224cee99b..e253af30e3fdf089b9e5cb8953dc5ec61fd784a9 100644 (file)
@@ -8027,6 +8027,12 @@ Used by `minetest.register_node`, `minetest.register_craftitem`, and
 
             eat = <SimpleSoundSpec>,
             -- When item is eaten with `minetest.do_item_eat`
+
+            punch_use = <SimpleSoundSpec>,
+            -- When item is used with the 'punch/mine' key pointing at a node or entity
+
+            punch_use_air = <SimpleSoundSpec>,
+            -- When item is used with the 'punch/mine' key pointing at nothing (air)
         },
 
         on_place = function(itemstack, placer, pointed_thing),
index b263a3f35567d8478f6f3e53c996cf71a1349afd..2523bd3027c9c9f9c77c0ae4a5f61472f6b5122b 100644 (file)
@@ -136,6 +136,35 @@ minetest.register_tool("soundstuff:breaks", {
        },
 })
 
+
+minetest.register_tool("soundstuff:punch_use", {
+       description = "Punch Use Sound Tool\n"..
+               "Digs cracky=3 and more\n"..
+               "Makes a sound when used on node or entity",
+       inventory_image = "soundstuff_node_dig.png",
+       sound = {
+               punch_use = { name = "soundstuff_mono", gain = 1.0 },
+       },
+       tool_capabilities = {
+               max_drop_level=0,
+               groupcaps={
+                       cracky={times={[2]=2.00, [3]=1.20}, uses=0, maxlevel=0},
+                       choppy={times={[2]=2.00, [3]=1.20}, uses=0, maxlevel=0},
+                       snappy={times={[2]=2.00, [3]=1.20}, uses=0, maxlevel=0},
+                       crumbly={times={[2]=2.00, [3]=1.20}, uses=0, maxlevel=0},
+               },
+       },
+})
+
+minetest.register_tool("soundstuff:punch_use_air", {
+       description = "Punch Use (Air) Sound Tool\n"..
+               "Makes a sound when used pointing at nothing",
+       inventory_image = "soundstuff_node_dig.png",
+       sound = {
+               punch_use_air = { name = "soundstuff_mono", gain = 1.0 },
+       },
+})
+
 -- Plays sound repeatedly
 minetest.register_node("soundstuff:positional", {
        description = "Positional Sound Node",
index 6807c7eca461574e7df6d34f6cc83a87e636c169..91c93ef7f874ebad86bde99c00f8d0219919cf3e 100644 (file)
@@ -264,6 +264,8 @@ class SoundMaker
 
        SimpleSoundSpec m_player_step_sound;
        SimpleSoundSpec m_player_leftpunch_sound;
+       // Second sound made on left punch, currently used for item 'use' sound
+       SimpleSoundSpec m_player_leftpunch_sound2;
        SimpleSoundSpec m_player_rightpunch_sound;
 
        SoundMaker(ISoundManager *sound, const NodeDefManager *ndef):
@@ -314,6 +316,7 @@ class SoundMaker
        {
                SoundMaker *sm = (SoundMaker *)data;
                sm->m_sound->playSound(sm->m_player_leftpunch_sound);
+               sm->m_sound->playSound(sm->m_player_leftpunch_sound2);
        }
 
        static void cameraPunchRight(MtEvent *e, void *data)
@@ -3236,7 +3239,9 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud)
 
        runData.punching = false;
 
-       soundmaker->m_player_leftpunch_sound.name.clear();
+       soundmaker->m_player_leftpunch_sound = SimpleSoundSpec();
+       soundmaker->m_player_leftpunch_sound2 = pointed.type != POINTEDTHING_NOTHING ?
+               selected_def.sound_use : selected_def.sound_use_air;
 
        // Prepare for repeating, unless we're not supposed to
        if (isKeyDown(KeyType::PLACE) && !g_settings->getBool("safe_dig_and_place"))
index e8d9ea3075a17a372f0b20de9916fb244ffe35bd..4d695083515e640780f6a822f945a53c3da42d78 100644 (file)
@@ -78,6 +78,8 @@ ItemDefinition& ItemDefinition::operator=(const ItemDefinition &def)
        place_param2 = def.place_param2;
        sound_place = def.sound_place;
        sound_place_failed = def.sound_place_failed;
+       sound_use = def.sound_use;
+       sound_use_air = def.sound_use_air;
        range = def.range;
        palette_image = def.palette_image;
        color = def.color;
@@ -117,6 +119,8 @@ void ItemDefinition::reset()
        groups.clear();
        sound_place = SimpleSoundSpec();
        sound_place_failed = SimpleSoundSpec();
+       sound_use = SimpleSoundSpec();
+       sound_use_air = SimpleSoundSpec();
        range = -1;
        node_placement_prediction.clear();
        place_param2 = 0;
@@ -166,6 +170,9 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
        os << serializeString16(short_description);
 
        os << place_param2;
+
+       sound_use.serialize(os, protocol_version);
+       sound_use_air.serialize(os, protocol_version);
 }
 
 void ItemDefinition::deSerialize(std::istream &is, u16 protocol_version)
@@ -214,12 +221,15 @@ void ItemDefinition::deSerialize(std::istream &is, u16 protocol_version)
        inventory_overlay = deSerializeString16(is);
        wield_overlay = deSerializeString16(is);
 
-       // If you add anything here, insert it primarily inside the try-catch
+       // If you add anything here, insert it inside the try-catch
        // block to not need to increase the version.
        try {
                short_description = deSerializeString16(is);
 
                place_param2 = readU8(is); // 0 if missing
+
+               sound_use.deSerialize(is, protocol_version);
+               sound_use_air.deSerialize(is, protocol_version);
        } catch(SerializationError &e) {};
 }
 
index 035717379f927a058ca841e6325105551142c329..3bb27559ecedf3353d4e98d5762c7b7371186527 100644 (file)
@@ -80,6 +80,7 @@ struct ItemDefinition
        ItemGroupList groups;
        SimpleSoundSpec sound_place;
        SimpleSoundSpec sound_place_failed;
+       SimpleSoundSpec sound_use, sound_use_air;
        f32 range;
 
        // Client shall immediately place this node when player places the item.
index da54edb964bce678411fed6a487641152807ab1e..93202dcee7dcfb1263a890d8f31ed1b3c4d8c9ca 100644 (file)
@@ -112,6 +112,19 @@ void read_item_definition(lua_State* L, int index,
        }
        lua_pop(L, 1);
 
+       // No, this is not a mistake. Item sounds are in "sound", node sounds in "sounds".
+       lua_getfield(L, index, "sound");
+       if (!lua_isnil(L, -1)) {
+               luaL_checktype(L, -1, LUA_TTABLE);
+               lua_getfield(L, -1, "punch_use");
+               read_soundspec(L, -1, def.sound_use);
+               lua_pop(L, 1);
+               lua_getfield(L, -1, "punch_use_air");
+               read_soundspec(L, -1, def.sound_use_air);
+               lua_pop(L, 1);
+       }
+       lua_pop(L, 1);
+
        def.range = getfloatfield_default(L, index, "range", def.range);
 
        // Client shall immediately place this node when player places the item.