1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-22 11:44:17 +01:00

Codechange: Use vector for industry random sounds. (#12606)

Use a vector to store the list of random sounds played for an industry.

The removes manual memory allocation, flags to control memory management, a separate count member, and a try/catch block.
This commit is contained in:
Peter Nelson
2024-05-01 20:55:00 +01:00
committed by GitHub
parent 7147fe9e7a
commit f146680121
4 changed files with 52 additions and 75 deletions

View File

@@ -3705,23 +3705,15 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop,
break;
case 0x15: { // Random sound effects
indsp->number_of_sounds = buf->ReadByte();
uint8_t *sounds = MallocT<uint8_t>(indsp->number_of_sounds);
uint8_t num_sounds = buf->ReadByte();
try {
for (uint8_t j = 0; j < indsp->number_of_sounds; j++) {
sounds[j] = buf->ReadByte();
}
} catch (...) {
free(sounds);
throw;
std::vector<uint8_t> sounds;
sounds.reserve(num_sounds);
for (uint8_t j = 0; j < num_sounds; ++j) {
sounds.push_back(buf->ReadByte());
}
if (HasBit(indsp->cleanup_flag, CLEAN_RANDOMSOUNDS)) {
free(indsp->random_sounds);
}
indsp->random_sounds = sounds;
SetBit(indsp->cleanup_flag, CLEAN_RANDOMSOUNDS);
indsp->random_sounds = std::move(sounds);
break;
}