Originally Posted by
ulillillia
Code:
if (TerrainData[ArrayIndex] == 135) { CurrentHeight -= 340; } // steepest downward slope
else if (TerrainData[ArrayIndex] == 151) { CurrentHeight -= 224; }
else if (TerrainData[ArrayIndex] == 167) { CurrentHeight -= 165; }
else if (TerrainData[ArrayIndex] == 183) { CurrentHeight -= 129; }
... etc ...
This piece of code could be made far more readable and maintainable by placing the data in an array or other data structure. I'd suggest something like this:
Code:
struct adjust_rec
{
int terrain;
int adjust;
};
struct adjust_rec terrain_adjust_table[] =
{
{ 135, -340 },
{ 151, -224 },
{ 167, -165 },
{ 183, -129 },
... etc ...
{ -1, -1 } /* Marks the end */
};
int get_terrain_adjust(int terrain, int *value)
{
struct adjust_rec *adj;
/* Loop until we reach the end marker */
for(adj = terrain_adjust_table; adj->terrain != -1; adj++)
{
if(adj->terrain == terrain)
{
*value = adj->adjust;
return 1;
}
}
return 0;
}
Now, the string of if-else statements can be replaced with this:
Code:
int delta_height;
if(!get_terrain_adjust(TerrainData[ArrayIndex], &delta_height))
{
Errors++;
printf("Invalid color at location %d\n", ArrayIndex);
}
else
{
CurrentHeight += delta_height;
}
Also, the selection of color indexes seems pretty strange. If you had chosen them to all be consecutive, you could have done this with a simple array of ints, used the terrain color to index into this array (after appropriate bounds checking).