1
0

Added BiomeToString() API function.

This commit is contained in:
madmaxoft 2014-04-29 15:36:05 +02:00
parent 709ae320e0
commit 015bf244b5
2 changed files with 106 additions and 81 deletions

View File

@ -7,30 +7,14 @@
#include "BiomeDef.h"
EMCSBiome StringToBiome(const AString & a_BiomeString)
{
// If it is a number, return it:
int res = atoi(a_BiomeString.c_str());
if ((res != 0) || (a_BiomeString.compare("0") == 0))
{
if ((res >= biFirstBiome) && (res < biNumBiomes))
{
return (EMCSBiome)res;
}
else if ((res >= biFirstVariantBiome) && (res < biNumVariantBiomes))
{
return (EMCSBiome)res;
}
// It was an invalid number
return biInvalidBiome;
}
// Convert using the built-in map:
static struct {
// The "map" used for biome <-> string conversions:
static struct {
EMCSBiome m_Biome;
const char * m_String;
} BiomeMap[] =
{
} g_BiomeMap[] =
{
{biOcean, "Ocean"} ,
{biPlains, "Plains"},
{biDesert, "Desert"},
@ -99,13 +83,35 @@ EMCSBiome StringToBiome(const AString & a_BiomeString)
{biMesaBryce, "MesaBryce"},
{biMesaPlateauFM, "MesaPlateauFM"},
{biMesaPlateauM, "MesaPlateauM"},
} ;
} ;
for (size_t i = 0; i < ARRAYCOUNT(BiomeMap); i++)
EMCSBiome StringToBiome(const AString & a_BiomeString)
{
// If it is a number, return it:
int res = atoi(a_BiomeString.c_str());
if ((res != 0) || (a_BiomeString.compare("0") == 0))
{
if (NoCaseCompare(BiomeMap[i].m_String, a_BiomeString) == 0)
if ((res >= biFirstBiome) && (res < biNumBiomes))
{
return BiomeMap[i].m_Biome;
return (EMCSBiome)res;
}
else if ((res >= biFirstVariantBiome) && (res < biNumVariantBiomes))
{
return (EMCSBiome)res;
}
// It was an invalid number
return biInvalidBiome;
}
for (size_t i = 0; i < ARRAYCOUNT(g_BiomeMap); i++)
{
if (NoCaseCompare(g_BiomeMap[i].m_String, a_BiomeString) == 0)
{
return g_BiomeMap[i].m_Biome;
}
} // for i - BiomeMap[]
return biInvalidBiome;
@ -115,6 +121,22 @@ EMCSBiome StringToBiome(const AString & a_BiomeString)
AString BiomeToString(int a_Biome)
{
for (size_t i = 0; i < ARRAYCOUNT(g_BiomeMap); i++)
{
if (g_BiomeMap[i].m_Biome == a_Biome)
{
return g_BiomeMap[i].m_String;
}
}
return AString();
}
bool IsBiomeNoDownfall(EMCSBiome a_Biome)
{
switch (a_Biome)

View File

@ -104,10 +104,13 @@ enum EMCSBiome
biMaxVariantBiome = biNumVariantBiomes - 1, // The maximum biome value
} ;
/// Translates a biome string to biome enum. Takes either a number or a biome alias (built-in). Returns biInvalidBiome on failure.
/** Translates a biome string to biome enum. Takes either a number or a biome alias (built-in). Returns biInvalidBiome on failure. */
extern EMCSBiome StringToBiome(const AString & a_BiomeString);
/// Returns true if the biome has no downfall - deserts and savannas
/** Translates biome enum into biome string. Returns empty string on failure (unknown biome). */
extern AString BiomeToString(int a_Biome);
/** Returns true if the biome has no downfall - deserts and savannas */
extern bool IsBiomeNoDownfall(EMCSBiome a_Biome);