Skip to content

Commit

Permalink
enum class MusicId
Browse files Browse the repository at this point in the history
  • Loading branch information
captainurist committed Nov 11, 2023
1 parent e55c565 commit 8acd0bc
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 20 deletions.
40 changes: 40 additions & 0 deletions src/Bin/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,45 @@ int runBountyHuntCodeGen(CodeGenOptions options, GameResourceManager *resourceMa
return 0;
}

int runMusicCodeGen(CodeGenOptions options, GameResourceManager *resourceManager) {
MapStats mapStats;
mapStats.Initialize(resourceManager->getEventsFile("MapStats.txt"));

std::map<MusicId, std::vector<std::string>> mapNamesByMusicId, mapEnumNamesByMusicId;
for (const MapInfo &info : mapStats.pInfos) {
mapNamesByMusicId[info.uRedbookTrackID].push_back(info.pName);
mapEnumNamesByMusicId[info.uRedbookTrackID].push_back(mapIdEnumName(info));
}

CodeGenMap map;
map.insert(MUSIC_INVALID, "INVALID", "");

for (const auto &[musicId, mapEnumNames] : mapEnumNamesByMusicId) {
if (mapEnumNames.size() <= 3) {
map.insert(musicId, fmt::format("{}", fmt::join(mapEnumNames, "_")), "");
} else if (musicId == MUSIC_DUNGEON) {
map.insert(musicId, "DUNGEON", "Most of the game dungeons.");
} else if (musicId == MUSIC_BARROWS) {
map.insert(musicId, "BARROWS", "Barrows I-XV & Zokarr's Tomb.");
} else {
std::string comment = fmt::format("{}.", fmt::join(mapNamesByMusicId[musicId], ", "));

if (musicId == MUSIC_CASTLE_HARMONDALE) {
map.insert(musicId, "CASTLE_HARMONDALE", comment);
} else if (musicId == MUSIC_TEMPLES) {
map.insert(musicId, "TEMPLES", comment);
} else if (musicId == MUSIC_ENDGAME_DUNGEON) {
map.insert(musicId, "ENDGAME_DUNGEON", comment);
} else {
assert(false);
}
}
}

map.dump(stdout, "MUSIC_");
return 0;
}

int platformMain(int argc, char **argv) {
try {
UnicodeCrt _(argc, argv);
Expand All @@ -408,6 +447,7 @@ int platformMain(int argc, char **argv) {
case CodeGenOptions::SUBCOMMAND_MONSTER_ID: return runMonsterIdCodeGen(std::move(options), &resourceManager);
case CodeGenOptions::SUBCOMMAND_MONSTER_TYPE: return runMonsterTypeCodeGen(std::move(options), &resourceManager);
case CodeGenOptions::SUBCOMMAND_BOUNTY_HUNT: return runBountyHuntCodeGen(std::move(options), &resourceManager);
case CodeGenOptions::SUBCOMMAND_MUSIC: return runMusicCodeGen(std::move(options), &resourceManager);
default:
assert(false);
return 1;
Expand Down
1 change: 1 addition & 0 deletions src/Bin/CodeGen/CodeGenOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ CodeGenOptions CodeGenOptions::parse(int argc, char **argv) {
app->add_subcommand("monsters", "Generate monster ids enum.", result.subcommand, SUBCOMMAND_MONSTER_ID)->fallthrough();
app->add_subcommand("monster_types", "Generate monster types enum.", result.subcommand, SUBCOMMAND_MONSTER_TYPE)->fallthrough();
app->add_subcommand("bounty_hunt", "Generate monster type / town hall table for bounty hunts.", result.subcommand, SUBCOMMAND_BOUNTY_HUNT)->fallthrough();
app->add_subcommand("music", "Generate music ids enum.", result.subcommand, SUBCOMMAND_MUSIC)->fallthrough();

app->parse(argc, argv, result.helpPrinted);
return result;
Expand Down
1 change: 1 addition & 0 deletions src/Bin/CodeGen/CodeGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct CodeGenOptions : GameStarterOptions {
SUBCOMMAND_MONSTER_ID,
SUBCOMMAND_MONSTER_TYPE,
SUBCOMMAND_BOUNTY_HUNT,
SUBCOMMAND_MUSIC
};
using enum Subcommand;

Expand Down
2 changes: 1 addition & 1 deletion src/Engine/MapInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void MapStats::Initialize(const Blob &mapStats) {
}
break;
case 28:
pInfos[i].uRedbookTrackID = atoi(test_string);
pInfos[i].uRedbookTrackID = static_cast<MusicId>(atoi(test_string));
break;
case 29: {
pInfos[i].uEAXEnv = 0xff;
Expand Down
4 changes: 3 additions & 1 deletion src/Engine/MapInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <array>
#include <string>

#include "Media/Audio/SoundEnums.h"

#include "Utility/IndexedArray.h"

#include "MapEnums.h"
Expand Down Expand Up @@ -42,7 +44,7 @@ struct MapInfo {
char field_3D;
char field_3E;
char field_3F;
uint8_t uRedbookTrackID;
MusicId uRedbookTrackID;
uint8_t uEAXEnv;
char field_42;
char field_43;
Expand Down
2 changes: 1 addition & 1 deletion src/GUI/UI/UICredits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void GUICredits::EventLoop() {
void GUICredits::ExecuteCredits() {
engine->_messageQueue->clear();

pAudioPlayer->MusicPlayTrack(MUSIC_Credits);
pAudioPlayer->MusicPlayTrack(MUSIC_CREDITS);

GUICredits *pWindow_Credits = new GUICredits();
current_screen_type = SCREEN_CREATORS;
Expand Down
2 changes: 1 addition & 1 deletion src/GUI/UI/UIMainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void GUIWindow_MainMenu::drawCopyrightAndInit(std::function<void()> initFunc) {

void GUIWindow_MainMenu::loop() {
pAudioPlayer->stopSounds();
pAudioPlayer->MusicPlayTrack(MUSIC_MainMenu);
pAudioPlayer->MusicPlayTrack(MUSIC_MAIN_MENU);

current_screen_type = SCREEN_GAME;

Expand Down
18 changes: 9 additions & 9 deletions src/Media/Audio/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extern OpenALSoundProvider *provider;

AudioPlayer::~AudioPlayer() = default;

void AudioPlayer::MusicPlayTrack(MusicID eTrack) {
void AudioPlayer::MusicPlayTrack(MusicId eTrack) {
if (currentMusicTrack == eTrack) {
return;
}
Expand All @@ -54,9 +54,9 @@ void AudioPlayer::MusicPlayTrack(MusicID eTrack) {
if (pCurrentMusicTrack) {
pCurrentMusicTrack->Stop();
}
currentMusicTrack = MUSIC_Invalid;
currentMusicTrack = MUSIC_INVALID;

std::string file_path = fmt::format("{}.mp3", eTrack);
std::string file_path = fmt::format("{}.mp3", std::to_underlying(eTrack));
file_path = makeDataPath("music", file_path);
if (!std::filesystem::exists(file_path)) {
logger->warning("AudioPlayer: {} not found", file_path);
Expand Down Expand Up @@ -85,7 +85,7 @@ void AudioPlayer::MusicStop() {

pCurrentMusicTrack->Stop();
pCurrentMusicTrack = nullptr;
currentMusicTrack = MUSIC_Invalid;
currentMusicTrack = MUSIC_INVALID;
}

void AudioPlayer::MusicPause() {
Expand All @@ -102,10 +102,10 @@ void AudioPlayer::MusicResume() {
}

if (!pCurrentMusicTrack->Resume()) {
int playedMusicTrack = currentMusicTrack;
if (currentMusicTrack != MUSIC_Invalid) {
MusicId playedMusicTrack = currentMusicTrack;
if (currentMusicTrack != MUSIC_INVALID) {
MusicStop();
MusicPlayTrack((MusicID)playedMusicTrack);
MusicPlayTrack((MusicId)playedMusicTrack);
}
}
}
Expand Down Expand Up @@ -400,7 +400,7 @@ bool AudioPlayer::isWalkingSoundPlays() {
}

void AudioPlayer::Initialize() {
currentMusicTrack = MUSIC_Invalid;
currentMusicTrack = MUSIC_INVALID;
uMasterVolume = 127;

UpdateVolumeFromConfig();
Expand All @@ -418,7 +418,7 @@ void AudioPlayer::UpdateVolumeFromConfig() {
void PlayLevelMusic() {
MapId map_id = pMapStats->GetMapInfo(pCurrentMapName);
if (map_id != MAP_INVALID) {
pAudioPlayer->MusicPlayTrack((MusicID)pMapStats->pInfos[map_id].uRedbookTrackID);
pAudioPlayer->MusicPlayTrack((MusicId)pMapStats->pInfos[map_id].uRedbookTrackID);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Media/Audio/AudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AudioPlayer {
void SetVoiceVolume(int level);
void SetMusicVolume(int level);

void MusicPlayTrack(MusicID eTrack);
void MusicPlayTrack(MusicId eTrack);
void MusicStart();
void MusicStop();
void MusicPause();
Expand Down Expand Up @@ -131,7 +131,7 @@ class AudioPlayer {

protected:
bool bPlayerReady = false;
MusicID currentMusicTrack = MUSIC_Invalid;
MusicId currentMusicTrack = MUSIC_INVALID;
float uMasterVolume = 0;
float uMusicVolume = 0;
float uVoiceVolume = 0;
Expand Down
33 changes: 28 additions & 5 deletions src/Media/Audio/SoundEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,35 @@ enum SoundID {
SOUND_quest = 20001,
};

// TODO(captainurist): #enum class
enum MusicID {
MUSIC_Invalid = 0,
MUSIC_MainMenu = 14,
MUSIC_Credits = 15
/**
* Enum value is used to load mp3 files, which are named `2.mp3`-`20.mp3`.
*/
enum class MusicId {
MUSIC_INVALID = 0,
MUSIC_DEYJA_BRACADA_DESERT = 2,
MUSIC_DUNGEON = 3, // Most of the game dungeons.
MUSIC_HARMONDALE_TATALIA_AVLEE = 4,
MUSIC_BARROWS = 5, // Barrows I-XV & Zokarr's Tomb.
MUSIC_EVENMORN_ISLAND = 6,
MUSIC_PIT_CASTLE_GLOAMING = 7,
MUSIC_LINCOLN_ARENA = 8,
MUSIC_SHOALS = 9,
MUSIC_CELESTE = 10,
MUSIC_MOUNT_NIGHON = 11,
MUSIC_BARROW_DOWNS_LAND_OF_THE_GIANTS = 12,
MUSIC_TEMPLES = 13, // The Temple of the Moon, Temple of the Light, Temple of the Dark, Grand Temple of the Moon, Grand Temple of the Sun, The Temple of Baa.
MUSIC_CASTLE_GRYPHONHEART_CASTLE_NAVAN = 14,
MUSIC_CASTLE_LAMBENT = 15,
MUSIC_ENDGAME_DUNGEON = 16, // The Dragon Caves, Thunderfist Mountain, The Titans' Stronghold, Tunnels to Eeofol.
MUSIC_ERATHIA = 17,
MUSIC_TULAREAN_FOREST = 18,
MUSIC_CASTLE_HARMONDALE = 19, // Lord Markham's Manor, The Bandit Caves, Castle Harmondale, Fort Riverstride, The School of Sorcery, Stone City, The Mercenary Guild, William Setag's Tower, The Strange Temple, The Small House.
MUSIC_EMERALD_ISLAND = 20,

MUSIC_MAIN_MENU = MUSIC_CASTLE_GRYPHONHEART_CASTLE_NAVAN,
MUSIC_CREDITS = MUSIC_CASTLE_LAMBENT,
};
using enum MusicId;

// TODO(captainurist): #enum class
enum SOUND_TYPE {
Expand Down

0 comments on commit 8acd0bc

Please sign in to comment.