1 /*
2  *  The ManaPlus Client
3  *  Copyright (C) 2013-2019  The ManaPlus Developers
4  *  Copyright (C) 2019-2021  Andrei Karas
5  *
6  *  This file is part of The ManaPlus Client.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "itemsoundmanager.h"
23 
24 #include "soundmanager.h"
25 
26 #include "being/flooritem.h"
27 #include "being/localplayer.h"
28 
29 #include "resources/iteminfo.h"
30 
31 #include "resources/item/item.h"
32 
33 #include "debug.h"
34 
playSfx(const Item * const item,const ItemSoundEvent::Type sound)35 void ItemSoundManager::playSfx(const Item *const item,
36                                const ItemSoundEvent::Type sound)
37 {
38     if (item == nullptr)
39         return;
40     playSfx(item->getId(), sound);
41 }
42 
playSfx(const int itemId,const ItemSoundEvent::Type sound)43 void ItemSoundManager::playSfx(const int itemId,
44                                const ItemSoundEvent::Type sound)
45 {
46     playSfx(ItemDB::get(itemId), sound);
47 }
48 
playSfx(const FloorItem * const item,const ItemSoundEvent::Type sound)49 void ItemSoundManager::playSfx(const FloorItem *const item,
50                                const ItemSoundEvent::Type sound)
51 {
52     if (item == nullptr)
53         return;
54     playSfx(ItemDB::get(item->getItemId()), sound);
55 }
56 
getSoundEffect(const Being * const being,const ItemInfo & info,const ItemSoundEvent::Type sound)57 std::string ItemSoundManager::getSoundEffect(const Being *const being,
58                                              const ItemInfo &info,
59                                              const ItemSoundEvent::Type sound)
60 {
61     std::string sfx = info.getSound(sound).sound;
62     if (sfx.empty())
63     {
64         if (being == nullptr)
65             return std::string();
66 
67         // fallback to player race sound if no item sound.
68         const int id = -100 - toInt(being->getSubType(), int);
69         const ItemInfo &info2 = ItemDB::get(id);
70         sfx = info2.getSound(sound).sound;
71     }
72     return sfx;
73 }
74 
playSfx(const ItemInfo & info,const ItemSoundEvent::Type sound)75 void ItemSoundManager::playSfx(const ItemInfo &info,
76                                const ItemSoundEvent::Type sound)
77 {
78     soundManager.playSfx(getSoundEffect(localPlayer, info, sound),
79         0,
80         0);
81 }
82 
playSfx(const Being * const being,const int itemId,const ItemSoundEvent::Type sound)83 void ItemSoundManager::playSfx(const Being *const being,
84                                const int itemId,
85                                const ItemSoundEvent::Type sound)
86 {
87     if (being == nullptr)
88         return;
89 
90     soundManager.playSfx(getSoundEffect(being, ItemDB::get(itemId), sound),
91         being->getTileX(),
92         being->getTileY());
93 }
94