Fixed a floating point rounding error

This commit is contained in:
xen-42 2025-08-04 21:44:18 -04:00
parent 57d945d6ee
commit 831a94cdbc
2 changed files with 5 additions and 3 deletions

View File

@ -100,8 +100,6 @@ namespace NewHorizons.Builder.Props.Audio
var freq = CollectionUtilities.KeyByValue(_customFrequencyNames, str);
if (freq != default) return freq;
NHLogger.Log($"Registering new frequency name [{str}]");
if (NumberOfFrequencies == 31)
{
NHLogger.LogWarning($"Can't store any more frequencies, skipping [{str}]");
@ -111,6 +109,8 @@ namespace NewHorizons.Builder.Props.Audio
freq = EnumUtilities.Create<SignalFrequency>(str);
_customFrequencyNames.Add(freq, str);
NHLogger.Log($"Registered new frequency name [{str}] with value [{(int)freq}] and index [{AudioSignal.FrequencyToIndex(freq)}]");
NumberOfFrequencies = EnumUtils.GetValues<SignalFrequency>().Length;
return freq;

View File

@ -45,7 +45,9 @@ namespace NewHorizons.Patches.SignalPatches
SignalFrequency.HideAndSeek => 5,
SignalFrequency.Radio => 6,
SignalFrequency.Statue => 7,
_ => (int)(Mathf.Log((float)frequency) / Mathf.Log(2f)),// Frequencies are in powers of 2
// Can't cast to int because floating point error, it was doing 12.9999999 -> 12
// Frequencies are in powers of 2
_ => Mathf.RoundToInt(Mathf.Log((float)frequency) / Mathf.Log(2f)),
};
return false;
}