diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 111 | ||||
-rw-r--r-- | src/nodedef.cpp | 3 | ||||
-rw-r--r-- | src/nodedef.h | 1 | ||||
-rw-r--r-- | src/scriptapi.cpp | 9 | ||||
-rw-r--r-- | src/sound.h | 2 | ||||
-rw-r--r-- | src/sound_openal.cpp | 4 |
6 files changed, 89 insertions, 41 deletions
diff --git a/src/game.cpp b/src/game.cpp index 3cad4c895..9715c6676 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -780,19 +780,34 @@ public: } }; -class SoundMaker +class NodeDugEvent: public MtEvent { public: - ISoundManager *m_sound; + v3s16 p; + MapNode n; + NodeDugEvent(v3s16 p, MapNode n): + p(p), + n(n) + {} + const char* getType() const + {return "NodeDug";} +}; + +class SoundMaker +{ + ISoundManager *m_sound; + INodeDefManager *m_ndef; +public: float m_player_step_timer; SimpleSoundSpec m_player_step_sound; SimpleSoundSpec m_player_leftpunch_sound; SimpleSoundSpec m_player_rightpunch_sound; - SoundMaker(ISoundManager *sound): + SoundMaker(ISoundManager *sound, INodeDefManager *ndef): m_sound(sound), + m_ndef(ndef), m_player_step_timer(0) { } @@ -805,20 +820,6 @@ public: } } - void playPlayerLeftPunch() - { - if(m_player_leftpunch_sound.exists()){ - m_sound->playSound(m_player_leftpunch_sound, false); - } - } - - void playPlayerRightPunch() - { - if(m_player_rightpunch_sound.exists()){ - m_sound->playSound(m_player_rightpunch_sound, false); - } - } - static void viewBobbingStep(MtEvent *e, void *data) { SoundMaker *sm = (SoundMaker*)data; @@ -839,13 +840,20 @@ public: static void cameraPunchLeft(MtEvent *e, void *data) { SoundMaker *sm = (SoundMaker*)data; - sm->playPlayerLeftPunch(); + sm->m_sound->playSound(sm->m_player_leftpunch_sound, false); } static void cameraPunchRight(MtEvent *e, void *data) { SoundMaker *sm = (SoundMaker*)data; - sm->playPlayerRightPunch(); + sm->m_sound->playSound(sm->m_player_rightpunch_sound, false); + } + + static void nodeDug(MtEvent *e, void *data) + { + SoundMaker *sm = (SoundMaker*)data; + NodeDugEvent *nde = (NodeDugEvent*)e; + sm->m_sound->playSound(sm->m_ndef->get(nde->n).sound_dug, false); } void registerReceiver(MtEventManager *mgr) @@ -855,6 +863,7 @@ public: mgr->reg("PlayerJump", SoundMaker::playerJump, this); mgr->reg("CameraPunchLeft", SoundMaker::cameraPunchLeft, this); mgr->reg("CameraPunchRight", SoundMaker::cameraPunchRight, this); + mgr->reg("NodeDug", SoundMaker::nodeDug, this); } void step(float dtime) @@ -863,6 +872,33 @@ public: } }; +// Locally stored sounds don't need to be preloaded because of this +class GameOnDemandSoundFetcher: public OnDemandSoundFetcher +{ + std::set<std::string> m_fetched; +public: + + void fetchSounds(const std::string &name, + std::set<std::string> &dst_paths, + std::set<std::vector<char> > &dst_datas) + { + if(m_fetched.count(name)) + return; + m_fetched.insert(name); + std::string base = porting::path_share + DIR_DELIM + "testsounds"; + dst_paths.insert(base + DIR_DELIM + name + ".ogg"); + dst_paths.insert(base + DIR_DELIM + name + "1.ogg"); + dst_paths.insert(base + DIR_DELIM + name + "2.ogg"); + dst_paths.insert(base + DIR_DELIM + name + "3.ogg"); + dst_paths.insert(base + DIR_DELIM + name + "4.ogg"); + dst_paths.insert(base + DIR_DELIM + name + "5.ogg"); + dst_paths.insert(base + DIR_DELIM + name + "6.ogg"); + dst_paths.insert(base + DIR_DELIM + name + "7.ogg"); + dst_paths.insert(base + DIR_DELIM + name + "8.ogg"); + dst_paths.insert(base + DIR_DELIM + name + "9.ogg"); + } +}; + void the_game( bool &kill, bool random_input, @@ -911,12 +947,15 @@ void the_game( // Create node definition manager IWritableNodeDefManager *nodedef = createNodeDefManager(); + // Sound fetcher (useful when testing) + GameOnDemandSoundFetcher soundfetcher; + // Sound manager ISoundManager *sound = NULL; bool sound_is_dummy = false; #if USE_AUDIO infostream<<"Attempting to use OpenAL audio"<<std::endl; - sound = createOpenALSoundManager(NULL); + sound = createOpenALSoundManager(&soundfetcher); if(!sound) infostream<<"Failed to initialize OpenAL audio"<<std::endl; #endif @@ -930,10 +969,11 @@ void the_game( EventManager eventmgr; // Sound maker - SoundMaker soundmaker(sound); + SoundMaker soundmaker(sound, nodedef); soundmaker.registerReceiver(&eventmgr); - // Test sounds + // Preload sounds +#if 0 sound->loadSound("default_grass_footstep", porting::path_share + DIR_DELIM + "sounds" + DIR_DELIM + "default_grass_walk1.ogg"); sound->loadSound("default_grass_footstep", porting::path_share + DIR_DELIM @@ -955,6 +995,7 @@ void the_game( + "sounds" + DIR_DELIM + "default_place_node2.ogg"); sound->loadSound("default_place_node", porting::path_share + DIR_DELIM + "sounds" + DIR_DELIM + "default_place_node3.ogg"); +#endif // Add chat log output for errors to be shown in chat LogOutputBuffer chat_log_error_buf(LMT_ERROR); @@ -2150,6 +2191,7 @@ void the_game( } bool left_punch = false; + soundmaker.m_player_leftpunch_sound.name = ""; if(playeritem_usable && input->getLeftState()) { @@ -2208,15 +2250,11 @@ void the_game( params = getDigParams(nodedef->get(n).groups, tp); } - soundmaker.m_player_leftpunch_sound.gain = 0.5; - if(params.main_group == "crumbly") - soundmaker.m_player_leftpunch_sound.name = - "default_dig_crumbly"; - else if(params.main_group == "cracky") + if(params.main_group != ""){ + soundmaker.m_player_leftpunch_sound.gain = 0.5; soundmaker.m_player_leftpunch_sound.name = - "default_dig_cracky"; - else - soundmaker.m_player_leftpunch_sound.name = ""; + std::string("default_dig_") + params.main_group; + } float dig_time_complete = 0.0; @@ -2256,6 +2294,7 @@ void the_game( infostream<<"Digging completed"<<std::endl; client.interact(2, pointed); client.setCrack(-1, v3s16(0,0,0)); + MapNode wasnode = map.getNode(nodepos); client.removeNode(nodepos); dig_time = 0; @@ -2266,17 +2305,17 @@ void the_game( // We don't want a corresponding delay to // very time consuming nodes - if(nodig_delay_timer > 0.5) - { - nodig_delay_timer = 0.5; - } + if(nodig_delay_timer > 0.3) + nodig_delay_timer = 0.3; // We want a slight delay to very little // time consuming nodes float mindelay = 0.15; if(nodig_delay_timer < mindelay) - { nodig_delay_timer = mindelay; - } + + // Send event to trigger sound + MtEvent *e = new NodeDugEvent(nodepos, wasnode); + gamedef->event()->put(e); } dig_time += dtime; diff --git a/src/nodedef.cpp b/src/nodedef.cpp index 2a21136bf..954467d48 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -156,6 +156,7 @@ void ContentFeatures::reset() legacy_facedir_simple = false; legacy_wallmounted = false; sound_footstep = SimpleSoundSpec(); + sound_dug = SimpleSoundSpec(); } void ContentFeatures::serialize(std::ostream &os) @@ -203,6 +204,7 @@ void ContentFeatures::serialize(std::ostream &os) writeU8(os, legacy_facedir_simple); writeU8(os, legacy_wallmounted); serializeSimpleSoundSpec(sound_footstep, os); + serializeSimpleSoundSpec(sound_dug, os); } void ContentFeatures::deSerialize(std::istream &is) @@ -256,6 +258,7 @@ void ContentFeatures::deSerialize(std::istream &is) legacy_wallmounted = readU8(is); try{ deSerializeSimpleSoundSpec(sound_footstep, is); + deSerializeSimpleSoundSpec(sound_dug, is); }catch(SerializationError &e) {}; } diff --git a/src/nodedef.h b/src/nodedef.h index 3a0d0675f..585755b9d 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -203,6 +203,7 @@ struct ContentFeatures // Sound properties SimpleSoundSpec sound_footstep; + SimpleSoundSpec sound_dug; /* Methods diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 79da15c49..fce760eb8 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -811,11 +811,10 @@ static void push_pointed_thing(lua_State *L, const PointedThing& pointed) SimpleSoundSpec */ -static SimpleSoundSpec read_soundspec(lua_State *L, int index) +static void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec) { if(index < 0) index = lua_gettop(L) + 1 + index; - SimpleSoundSpec spec; if(lua_isnil(L, index)){ } else if(lua_istable(L, index)){ getstringfield(L, index, "name", spec.name); @@ -823,7 +822,6 @@ static SimpleSoundSpec read_soundspec(lua_State *L, int index) } else if(lua_isstring(L, index)){ spec.name = lua_tostring(L, index); } - return spec; } /* @@ -1062,7 +1060,10 @@ static ContentFeatures read_content_features(lua_State *L, int index) lua_getfield(L, index, "sounds"); if(lua_istable(L, -1)){ lua_getfield(L, -1, "footstep"); - f.sound_footstep = read_soundspec(L, -1); + read_soundspec(L, -1, f.sound_footstep); + lua_pop(L, 1); + lua_getfield(L, -1, "dug"); + read_soundspec(L, -1, f.sound_dug); lua_pop(L, 1); } lua_pop(L, 1); diff --git a/src/sound.h b/src/sound.h index 6363d614e..7f6e4141e 100644 --- a/src/sound.h +++ b/src/sound.h @@ -61,7 +61,7 @@ public: virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0; // playSound functions return -1 on failure, otherwise a handle to the - // sound + // sound. If name=="", call should be ignored without error. virtual int playSound(const std::string &name, bool loop, float volume) = 0; virtual int playSoundAt(const std::string &name, bool loop, diff --git a/src/sound_openal.cpp b/src/sound_openal.cpp index 4f056888b..edcb9e8d4 100644 --- a/src/sound_openal.cpp +++ b/src/sound_openal.cpp @@ -454,6 +454,8 @@ public: int playSound(const std::string &name, bool loop, float volume) { maintain(); + if(name == "") + return 0; SoundBuffer *buf = getFetchBuffer(name); if(!buf){ infostream<<"OpenALSoundManager: \""<<name<<"\" not found." @@ -465,6 +467,8 @@ public: int playSoundAt(const std::string &name, bool loop, float volume, v3f pos) { maintain(); + if(name == "") + return 0; SoundBuffer *buf = getFetchBuffer(name); if(!buf){ infostream<<"OpenALSoundManager: \""<<name<<"\" not found." |