- 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
{
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<uint32_t> Choices;
uint32_t Owner = 0;
};
struct FPlayerClassLookup
@ -350,9 +336,9 @@ static bool S_CheckSound(sfxinfo_t *startsfx, sfxinfo_t *sfx, TArray<sfxinfo_t *
if (me->bRandomHeader)
{
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());
@ -427,7 +413,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;
}
@ -465,10 +451,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;
@ -504,11 +490,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]]);
}
}
}
@ -685,10 +671,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;
@ -1069,7 +1053,7 @@ void S_AddLocalSndInfo(int lump)
static void S_AddSNDINFO (int lump)
{
bool skipToEndIf;
TArray<uint16_t> list;
TArray<uint32_t> list;
FScanner sc(lump);
skipToEndIf = false;
@ -1390,12 +1374,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;
@ -1404,17 +1388,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;
@ -2104,9 +2089,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");
}