Skip to content

Commit 67eb25a

Browse files
authored
Add support for background ambience (#353)
* add support for background ambience * add sample ambience config file * Send ambience BEFORE music when possible
1 parent 7e9227f commit 67eb25a

File tree

11 files changed

+116
-2
lines changed

11 files changed

+116
-2
lines changed

bin/config_sample/ambience.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[CourtGreatWitchVS]
2+
ambience = ambient/greatwitchtrial.opus

bin/config_sample/command_extensions.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,7 @@ aliases = toggleshouts
8686
aliases = kickother
8787

8888
[jukebox_skip]
89-
aliases = jukeboxskip
89+
aliases = jukeboxskip
90+
91+
[play_ambience]
92+
aliases = playambience playa

bin/config_sample/text/commandhelp.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,5 +872,12 @@
872872
],
873873
"usage":"/jukebox_skip",
874874
"text":"Skips the current song in the Jukebox and plays the next available one."
875+
},
876+
{
877+
"names": [
878+
"play_ambience"
879+
],
880+
"usage":"/play_ambience",
881+
"text":"Plays the requested track in the ambience channel."
875882
}
876883
]

core/include/aoclient.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,19 @@ class AOClient : public QObject
18431843
*/
18441844
void cmdPlay(int argc, QStringList argv);
18451845

1846+
/**
1847+
* @brief Plays ambience in the area.
1848+
*
1849+
* @details The arguments are **the song's filepath** originating from `base/sounds/music/`,
1850+
* or **the song's URL** if it's a stream.
1851+
*
1852+
* As described above, this command can be used to play ambience by URL (for clients at and above version 2.9),
1853+
* but it can also be used to play ambience locally available for the clients.
1854+
*
1855+
* @iscommand
1856+
*/
1857+
void cmdPlayAmbience(int argc, QStringList argv);
1858+
18461859
/**
18471860
* @brief DJ-blocks a client.
18481861
*

core/include/area_data.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,15 @@ class AreaData : public QObject
638638
*/
639639
QString currentMusic() const;
640640

641+
/**
642+
* @brief Returns the ambient audio currently being played in the area.
643+
*
644+
* @return See short description.
645+
*
646+
* @see #m_currentAmbience
647+
*/
648+
QString currentAmbience() const;
649+
641650
/**
642651
* @brief Sets the music currently being played in the area.
643652
*
@@ -673,6 +682,13 @@ class AreaData : public QObject
673682
*/
674683
void changeMusic(const QString &f_source_r, const QString &f_newSong_r);
675684

685+
/**
686+
* @brief Changes the ambience audio being played in the area.
687+
*
688+
* @param f_newSong_r The name of the new audio that is going to be played in the area.
689+
*/
690+
void changeAmbience(const QString &f_newSong_r);
691+
676692
/**
677693
* @brief Returns the evidence mod in the area.
678694
*
@@ -1097,6 +1113,14 @@ class AreaData : public QObject
10971113
*/
10981114
QString m_currentMusic;
10991115

1116+
/**
1117+
* @brief See m_currentMusic, but for ambience.
1118+
*
1119+
* @details Title is a path to the music file, with the starting point on
1120+
* `base/sounds/music/` clientside, with file extension.
1121+
*/
1122+
QString m_currentAmbience;
1123+
11001124
/**
11011125
* @brief The name of the client (or client's character) that started the currently playing music.
11021126
*/

core/include/config_manager.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ class ConfigManager
101101
*/
102102
static QSettings *areaData();
103103

104+
/**
105+
* @brief Returns a pointer to the QSettings object which contains the ambience configuration.
106+
*
107+
* @return See short description.
108+
*/
109+
static QSettings *ambience();
110+
104111
/**
105112
* @brief Returns a sanitized QStringList of the areas.
106113
*
@@ -552,6 +559,11 @@ class ConfigManager
552559
*/
553560
static QSettings *m_logtext;
554561

562+
/**
563+
* @brief Stores all adjustable logstrings.
564+
*/
565+
static QSettings *m_ambience;
566+
555567
/**
556568
* @brief Pointer to QElapsedTimer to track the uptime of the server.
557569
*/

core/src/aoclient.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ const QMap<QString, AOClient::CommandInfo> AOClient::COMMANDS{
147147
{"toggle_wtce", {{ACLRole::CM}, 0, &AOClient::cmdToggleWtce}},
148148
{"toggle_shouts", {{ACLRole::CM}, 0, &AOClient::cmdToggleShouts}},
149149
{"kick_other", {{ACLRole::NONE}, 0, &AOClient::cmdKickOther}},
150-
{"jukebox_skip", {{ACLRole::CM}, 0, &AOClient::cmdJukeboxSkip}}};
150+
{"jukebox_skip", {{ACLRole::CM}, 0, &AOClient::cmdJukeboxSkip}},
151+
{"play_ambience", {{ACLRole::NONE}, 1, &AOClient::cmdPlayAmbience}}};
151152

152153
void AOClient::clientDisconnected()
153154
{

core/src/area_data.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ void AreaData::clientJoinedArea(int f_charId, int f_userId)
108108
}
109109
m_joined_ids.append(f_userId);
110110
emit userJoinedArea(m_index, f_userId);
111+
// Send out ambience as well. Use channel 1 for that
112+
emit sendAreaPacketClient(PacketFactory::createPacket("MC", {m_currentAmbience, QString::number(-1), ConfigManager::serverName(), QString::number(1), QString::number(1)}), f_userId);
111113
// The name will never be shown as we are using a spectator ID. Still nice for people who network sniff.
112114
// We auto-loop this so you'll never sit in silence unless wanted.
113115
emit sendAreaPacketClient(PacketFactory::createPacket("MC", {m_currentMusic, QString::number(-1), ConfigManager::serverName(), QString::number(1)}), f_userId);
@@ -494,11 +496,21 @@ void AreaData::changeMusic(const QString &f_source_r, const QString &f_newSong_r
494496
m_musicPlayedBy = f_source_r;
495497
}
496498

499+
void AreaData::changeAmbience(const QString &f_newSong_r)
500+
{
501+
m_currentAmbience = f_newSong_r;
502+
}
503+
497504
QString AreaData::currentMusic() const
498505
{
499506
return m_currentMusic;
500507
}
501508

509+
QString AreaData::currentAmbience() const
510+
{
511+
return m_currentAmbience;
512+
}
513+
502514
void AreaData::setCurrentMusic(QString f_current_song)
503515
{
504516
m_currentMusic = f_current_song;
@@ -587,6 +599,14 @@ QString AreaData::background() const
587599
void AreaData::setBackground(const QString f_background)
588600
{
589601
m_background = f_background;
602+
QSettings *ambience_data = ConfigManager::ambience();
603+
QString new_ambience = ambience_data->value(f_background + "/ambience").toString();
604+
if (new_ambience != "") {
605+
changeAmbience(new_ambience);
606+
}
607+
else {
608+
changeAmbience(""); // DON'T use ~stop.mp3 it overrides some code we don't want overridden
609+
}
590610
}
591611

592612
bool AreaData::ignoreBgList()

core/src/commands/area.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ void AOClient::cmdSetBackground(int argc, QStringList argv)
299299
if (server->getBackgrounds().contains(f_background, Qt::CaseInsensitive) || area->ignoreBgList() == true) {
300300
area->setBackground(f_background);
301301
server->broadcast(PacketFactory::createPacket("BN", {f_background}), m_current_area);
302+
QString ambience_name = ConfigManager::ambience()->value(f_background + "/ambience").toString();
303+
if (ambience_name != "") {
304+
server->broadcast(PacketFactory::createPacket("MC", {ambience_name, "-1", m_showname, "1", "1"}), m_current_area);
305+
}
306+
else {
307+
server->broadcast(PacketFactory::createPacket("MC", {"~stop.mp3", "-1", m_showname, "1", "1"}), m_current_area);
308+
}
302309
sendServerMessageArea(m_current_char + " changed the background to " + f_background);
303310
}
304311
else {

core/src/commands/music.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ void AOClient::cmdPlay(int argc, QStringList argv)
4949
server->broadcast(music_change, m_current_area);
5050
}
5151

52+
void AOClient::cmdPlayAmbience(int argc, QStringList argv)
53+
{
54+
Q_UNUSED(argc);
55+
56+
if (m_is_dj_blocked) {
57+
sendServerMessage("You are blocked from changing the ambience.");
58+
return;
59+
}
60+
AreaData *l_area = server->getAreaById(m_current_area);
61+
if (!l_area->owners().contains(m_id) && !l_area->isPlayEnabled()) { // Make sure we have permission to play music
62+
sendServerMessage("Free ambience play is disabled in this area.");
63+
return;
64+
}
65+
QString l_song = argv.join(" ");
66+
l_area->changeAmbience(l_song);
67+
AOPacket *music_change = PacketFactory::createPacket("MC", {l_song, "-1", m_showname, "1", "1"});
68+
server->broadcast(music_change, m_current_area);
69+
}
70+
5271
void AOClient::cmdCurrentMusic(int argc, QStringList argv)
5372
{
5473
Q_UNUSED(argc);

core/src/config_manager.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ QSettings *ConfigManager::m_settings = new QSettings("config/config.ini", QSetti
2323
QSettings *ConfigManager::m_discord = new QSettings("config/discord.ini", QSettings::IniFormat);
2424
QSettings *ConfigManager::m_areas = new QSettings("config/areas.ini", QSettings::IniFormat);
2525
QSettings *ConfigManager::m_logtext = new QSettings("config/text/logtext.ini", QSettings::IniFormat);
26+
QSettings *ConfigManager::m_ambience = new QSettings("config/ambience.ini", QSettings::IniFormat);
2627
ConfigManager::CommandSettings *ConfigManager::m_commands = new CommandSettings();
2728
QElapsedTimer *ConfigManager::m_uptimeTimer = new QElapsedTimer;
2829
MusicList *ConfigManager::m_musicList = new MusicList;
@@ -231,6 +232,11 @@ QSettings *ConfigManager::areaData()
231232
return m_areas;
232233
}
233234

235+
QSettings *ConfigManager::ambience()
236+
{
237+
return m_ambience;
238+
}
239+
234240
QStringList ConfigManager::sanitizedAreaNames()
235241
{
236242
QStringList l_area_names = m_areas->childGroups(); // invisibly does a lexicographical sort, because Qt is great like that

0 commit comments

Comments
 (0)