From 89a17d4257332403a9da721f5e63bcafc41141a3 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 21 Dec 2018 11:03:10 +0100 Subject: [PATCH] - fixed: The random sound handler was using 16 bit storage throughout Changed to use 32 bit and also fixed the random number call which was using the byte value variant of the access operator, effectively limiting the number of choices to 256. --- src/s_advsound.cpp | 69 ++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/src/s_advsound.cpp b/src/s_advsound.cpp index cb21697db..381e27384 100644 --- a/src/s_advsound.cpp +++ b/src/s_advsound.cpp @@ -67,22 +67,8 @@ struct FRandomSoundList { - FRandomSoundList() - : Sounds(0), SfxHead(0), NumSounds(0) - { - } - ~FRandomSoundList() - { - if (Sounds != NULL) - { - delete[] Sounds; - Sounds = NULL; - } - } - - uint16_t *Sounds; // A list of sounds that can result for the following id - uint16_t SfxHead; // The sound id used to reference this list - uint16_t NumSounds; + TArray Choices; + uint32_t Owner = 0; }; struct FPlayerClassLookup @@ -358,9 +344,9 @@ static bool S_CheckSound(sfxinfo_t *startsfx, sfxinfo_t *sfx, TArraybRandomHeader) { const FRandomSoundList *list = &S_rnd[me->link]; - for (int i = 0; i < list->NumSounds; ++i) + for (unsigned i = 0; i < list->Choices.Size(); ++i) { - auto rsfx = &S_sfx[list->Sounds[i]]; + auto rsfx = &S_sfx[list->Choices[i]]; if (rsfx == startsfx) { Printf(TEXTCOLOR_RED "recursive sound $random found for %s:\n", startsfx->name.GetChars()); @@ -435,7 +421,7 @@ int S_PickReplacement(int refid) while (S_sfx[refid].bRandomHeader) { const FRandomSoundList *list = &S_rnd[S_sfx[refid].link]; - refid = list->Sounds[pr_randsound() % list->NumSounds]; + refid = list->Choices[pr_randsound(list->Choices.Size())]; } return refid; } @@ -473,10 +459,10 @@ unsigned int S_GetMSLength(FSoundID sound) int length = 0; const FRandomSoundList *list = &S_rnd[sfx->link]; - for (int i=0; i < list->NumSounds; i++) + for (auto &me : list->Choices) { // unfortunately we must load all sounds to find the longest one... :( - int thislen = S_GetMSLength(list->Sounds[i]); + int thislen = S_GetMSLength(me); if (thislen > length) length = thislen; } return length; @@ -512,11 +498,11 @@ void S_CacheRandomSound (sfxinfo_t *sfx) if (sfx->bRandomHeader) { const FRandomSoundList *list = &S_rnd[sfx->link]; - for (int i = 0; i < list->NumSounds; ++i) + for (unsigned i = 0; i < list->Choices.Size(); ++i) { - sfx = &S_sfx[list->Sounds[i]]; + sfx = &S_sfx[list->Choices[i]]; sfx->bUsed = true; - S_CacheSound (&S_sfx[list->Sounds[i]]); + S_CacheSound (&S_sfx[list->Choices[i]]); } } } @@ -693,10 +679,8 @@ static int S_AddSound (const char *logicalname, int lumpnum, FScanner *sc) if (sfx->bRandomHeader) { FRandomSoundList *rnd = &S_rnd[sfx->link]; - delete[] rnd->Sounds; - rnd->Sounds = NULL; - rnd->NumSounds = 0; - rnd->SfxHead = 0; + rnd->Choices.Reset(); + rnd->Owner = 0; } sfx->lumpnum = lumpnum; sfx->bRandomHeader = false; @@ -1077,7 +1061,7 @@ void S_AddLocalSndInfo(int lump) static void S_AddSNDINFO (int lump) { bool skipToEndIf; - TArray list; + TArray list; FScanner sc(lump); skipToEndIf = false; @@ -1398,12 +1382,12 @@ static void S_AddSNDINFO (int lump) list.Clear (); sc.MustGetString (); - random.SfxHead = S_AddSound (sc.String, -1, &sc); + uint32_t Owner = S_AddSound (sc.String, -1, &sc); sc.MustGetStringName ("{"); while (sc.GetString () && !sc.Compare ("}")) { - uint16_t sfxto = S_FindSoundTentative (sc.String); - if (sfxto == random.SfxHead) + uint32_t sfxto = S_FindSoundTentative (sc.String); + if (sfxto == random.Owner) { Printf("Definition of random sound '%s' refers to itself recursively.\n", sc.String); continue; @@ -1412,17 +1396,18 @@ static void S_AddSNDINFO (int lump) } if (list.Size() == 1) { // Only one sound: treat as $alias - S_sfx[random.SfxHead].link = list[0]; - S_sfx[random.SfxHead].NearLimit = -1; + S_sfx[Owner].link = list[0]; + S_sfx[Owner].NearLimit = -1; } else if (list.Size() > 1) { // Only add non-empty random lists - random.NumSounds = (uint16_t)list.Size(); - S_sfx[random.SfxHead].link = (uint16_t)S_rnd.Push (random); - S_sfx[random.SfxHead].bRandomHeader = true; - S_rnd[S_sfx[random.SfxHead].link].Sounds = new uint16_t[random.NumSounds]; - memcpy (S_rnd[S_sfx[random.SfxHead].link].Sounds, &list[0], sizeof(uint16_t)*random.NumSounds); - S_sfx[random.SfxHead].NearLimit = -1; + auto index = S_rnd.Reserve(1); + auto &random = S_rnd.Last(); + random.Choices = std::move(list); + random.Owner = Owner; + S_sfx[Owner].link = index; + S_sfx[Owner].bRandomHeader = true; + S_sfx[Owner].NearLimit = -1; } } break; @@ -2112,9 +2097,9 @@ CCMD (soundlist) { Printf ("%3d. %s -> #%d {", i, sfx->name.GetChars(), sfx->link); const FRandomSoundList *list = &S_rnd[sfx->link]; - for (size_t j = 0; j < list->NumSounds; ++j) + for (auto &me : list->Choices) { - Printf (" %s ", S_sfx[list->Sounds[j]].name.GetChars()); + Printf (" %s ", S_sfx[me].name.GetChars()); } Printf ("}\n"); }