- 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.
This commit is contained in:
Christoph Oelckers 2018-12-21 11:03:10 +01:00
parent 1deedd5671
commit 2c3c91ff42

View file

@ -59,22 +59,8 @@
struct FRandomSoundList struct FRandomSoundList
{ {
FRandomSoundList() TArray<uint32_t> Choices;
: Sounds(0), SfxHead(0), NumSounds(0) uint32_t Owner = 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;
}; };
struct FPlayerClassLookup struct FPlayerClassLookup
@ -350,9 +336,9 @@ static bool S_CheckSound(sfxinfo_t *startsfx, sfxinfo_t *sfx, TArray<sfxinfo_t *
if (me->bRandomHeader) if (me->bRandomHeader)
{ {
const FRandomSoundList *list = &S_rnd[me->link]; 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) if (rsfx == startsfx)
{ {
Printf(TEXTCOLOR_RED "recursive sound $random found for %s:\n", startsfx->name.GetChars()); Printf(TEXTCOLOR_RED "recursive sound $random found for %s:\n", startsfx->name.GetChars());
@ -427,7 +413,7 @@ int S_PickReplacement(int refid)
while (S_sfx[refid].bRandomHeader) while (S_sfx[refid].bRandomHeader)
{ {
const FRandomSoundList *list = &S_rnd[S_sfx[refid].link]; 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; return refid;
} }
@ -465,10 +451,10 @@ unsigned int S_GetMSLength(FSoundID sound)
int length = 0; int length = 0;
const FRandomSoundList *list = &S_rnd[sfx->link]; 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... :( // 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; if (thislen > length) length = thislen;
} }
return length; return length;
@ -504,11 +490,11 @@ void S_CacheRandomSound (sfxinfo_t *sfx)
if (sfx->bRandomHeader) if (sfx->bRandomHeader)
{ {
const FRandomSoundList *list = &S_rnd[sfx->link]; 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; sfx->bUsed = true;
S_CacheSound (&S_sfx[list->Sounds[i]]); S_CacheSound (&S_sfx[list->Choices[i]]);
} }
} }
} }
@ -685,10 +671,8 @@ static int S_AddSound (const char *logicalname, int lumpnum, FScanner *sc)
if (sfx->bRandomHeader) if (sfx->bRandomHeader)
{ {
FRandomSoundList *rnd = &S_rnd[sfx->link]; FRandomSoundList *rnd = &S_rnd[sfx->link];
delete[] rnd->Sounds; rnd->Choices.Reset();
rnd->Sounds = NULL; rnd->Owner = 0;
rnd->NumSounds = 0;
rnd->SfxHead = 0;
} }
sfx->lumpnum = lumpnum; sfx->lumpnum = lumpnum;
sfx->bRandomHeader = false; sfx->bRandomHeader = false;
@ -1069,7 +1053,7 @@ void S_AddLocalSndInfo(int lump)
static void S_AddSNDINFO (int lump) static void S_AddSNDINFO (int lump)
{ {
bool skipToEndIf; bool skipToEndIf;
TArray<uint16_t> list; TArray<uint32_t> list;
FScanner sc(lump); FScanner sc(lump);
skipToEndIf = false; skipToEndIf = false;
@ -1390,12 +1374,12 @@ static void S_AddSNDINFO (int lump)
list.Clear (); list.Clear ();
sc.MustGetString (); sc.MustGetString ();
random.SfxHead = S_AddSound (sc.String, -1, &sc); uint32_t Owner = S_AddSound (sc.String, -1, &sc);
sc.MustGetStringName ("{"); sc.MustGetStringName ("{");
while (sc.GetString () && !sc.Compare ("}")) while (sc.GetString () && !sc.Compare ("}"))
{ {
uint16_t sfxto = S_FindSoundTentative (sc.String); uint32_t sfxto = S_FindSoundTentative (sc.String);
if (sfxto == random.SfxHead) if (sfxto == random.Owner)
{ {
Printf("Definition of random sound '%s' refers to itself recursively.\n", sc.String); Printf("Definition of random sound '%s' refers to itself recursively.\n", sc.String);
continue; continue;
@ -1404,17 +1388,18 @@ static void S_AddSNDINFO (int lump)
} }
if (list.Size() == 1) if (list.Size() == 1)
{ // Only one sound: treat as $alias { // Only one sound: treat as $alias
S_sfx[random.SfxHead].link = list[0]; S_sfx[Owner].link = list[0];
S_sfx[random.SfxHead].NearLimit = -1; S_sfx[Owner].NearLimit = -1;
} }
else if (list.Size() > 1) else if (list.Size() > 1)
{ // Only add non-empty random lists { // Only add non-empty random lists
random.NumSounds = (uint16_t)list.Size(); auto index = S_rnd.Reserve(1);
S_sfx[random.SfxHead].link = (uint16_t)S_rnd.Push (random); auto &random = S_rnd.Last();
S_sfx[random.SfxHead].bRandomHeader = true; random.Choices = std::move(list);
S_rnd[S_sfx[random.SfxHead].link].Sounds = new uint16_t[random.NumSounds]; random.Owner = Owner;
memcpy (S_rnd[S_sfx[random.SfxHead].link].Sounds, &list[0], sizeof(uint16_t)*random.NumSounds); S_sfx[Owner].link = index;
S_sfx[random.SfxHead].NearLimit = -1; S_sfx[Owner].bRandomHeader = true;
S_sfx[Owner].NearLimit = -1;
} }
} }
break; break;
@ -2104,9 +2089,9 @@ CCMD (soundlist)
{ {
Printf ("%3d. %s -> #%d {", i, sfx->name.GetChars(), sfx->link); Printf ("%3d. %s -> #%d {", i, sfx->name.GetChars(), sfx->link);
const FRandomSoundList *list = &S_rnd[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"); Printf ("}\n");
} }