Skip to content

Commit

Permalink
Remove floats related to map height.
Browse files Browse the repository at this point in the history
The MAPTILE.height and MAPTILE.waterLevel fields are now integers and are pre-multiplied by ELEVATION_SCALE (which was 2), and therefore now store the actual heights.

Preserved old behaviour including a couple of ELEVATION_SCALE-related bugs, which will be fixed in the next commit.
Might reduce desynchs, due to reduced floats.

For several recent commits:
Changelog: Removed lots of floats, which hopefully reduces the risk of desynchs.
  • Loading branch information
Cyp committed Nov 16, 2010
1 parent 902ec04 commit 275428c
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 86 deletions.
6 changes: 3 additions & 3 deletions src/display3d.c
Expand Up @@ -799,7 +799,7 @@ static void calcAverageTerrainHeight(iView *player)
/* Get a pointer to the tile at this location */
MAPTILE *psTile = mapTile(playerXTile + j, playerZTile + i);

averageCentreTerrainHeight += psTile->height * ELEVATION_SCALE;
averageCentreTerrainHeight += psTile->height;
numTilesAveraged++;
}
}
Expand All @@ -811,9 +811,9 @@ static void calcAverageTerrainHeight(iView *player)
MAPTILE *psTile = mapTile(playerXTile + visibleTiles.x / 2, playerZTile + visibleTiles.y / 2);

averageCentreTerrainHeight /= numTilesAveraged;
if (averageCentreTerrainHeight < psTile->height * ELEVATION_SCALE)
if (averageCentreTerrainHeight < psTile->height)
{
averageCentreTerrainHeight = psTile->height * ELEVATION_SCALE;
averageCentreTerrainHeight = psTile->height;
}
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/edit3d.c
Expand Up @@ -94,9 +94,9 @@ void lowerTile(int tile3dX, int tile3dY)
/* Ensures any adjustment to tile elevation is within allowed ranges */
void adjustTileHeight(MAPTILE *psTile, SDWORD adjust)
{
float newHeight = psTile->height + adjust;
int32_t newHeight = psTile->height + adjust*ELEVATION_SCALE;

if (newHeight>=MIN_TILE_HEIGHT && newHeight<=MAX_TILE_HEIGHT)
if (newHeight >= MIN_TILE_HEIGHT*ELEVATION_SCALE && newHeight <= MAX_TILE_HEIGHT*ELEVATION_SCALE)
{
psTile->height = newHeight;
}
Expand Down
2 changes: 1 addition & 1 deletion src/feature.c
Expand Up @@ -420,7 +420,7 @@ FEATURE * buildFeature(FEATURE_STATS *psStats, UDWORD x, UDWORD y,BOOL FromSave)

if( (!psStats->tileDraw) && (FromSave == false) )
{
psTile->height = (UBYTE)(height / ELEVATION_SCALE);
psTile->height = height;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/keybind.c
Expand Up @@ -676,7 +676,7 @@ void kf_TileInfo(void)
{
MAPTILE *psTile = mapTile(mouseTileX, mouseTileY);

debug(LOG_ERROR, "Tile position=(%d, %d) Terrain=%hhu Texture=%u Height=%.0g Illumination=%hhu",
debug(LOG_ERROR, "Tile position=(%d, %d) Terrain=%hhu Texture=%u Height=%d Illumination=%hhu",
mouseTileX, mouseTileY, terrainType(psTile), TileNumber_tile(psTile->texture), psTile->height,
psTile->illumination);
addConsoleMessage("Tile info dumped into log", DEFAULT_JUSTIFY, SYSTEM_MESSAGE);
Expand Down
48 changes: 24 additions & 24 deletions src/lighting.c
Expand Up @@ -147,17 +147,17 @@ static void normalsOnTile(unsigned int tileX, unsigned int tileY, unsigned int q
corner1 = {
world_coord(tileX + 1),
world_coord(tileY),
tileRight->height - rMod
tileRight->height*(1.f/ELEVATION_SCALE) - rMod
},
corner2 = {
world_coord(tileX + 1),
world_coord(tileY + 1),
tileDownRight->height - drMod
tileDownRight->height*(1.f/ELEVATION_SCALE) - drMod
},
corner3 = {
world_coord(tileX),
world_coord(tileY + 1),
tileDown->height - dMod
tileDown->height*(1.f/ELEVATION_SCALE) - dMod
};

normals[(*numNormals)++] = pie_SurfaceNormal3fv( corner1, corner2, corner3);
Expand All @@ -168,17 +168,17 @@ static void normalsOnTile(unsigned int tileX, unsigned int tileY, unsigned int q
corner1 = {
world_coord(tileX),
world_coord(tileY),
psTile->height - nMod
psTile->height*(1.f/ELEVATION_SCALE) - nMod
},
corner2 = {
world_coord(tileX + 1),
world_coord(tileY),
tileRight->height - rMod
tileRight->height*(1.f/ELEVATION_SCALE) - rMod
},
corner3 = {
world_coord(tileX),
world_coord(tileY + 1),
tileDown->height - dMod
tileDown->height*(1.f/ELEVATION_SCALE) - dMod
};

normals[(*numNormals)++] = pie_SurfaceNormal3fv( corner1, corner2, corner3);
Expand All @@ -192,17 +192,17 @@ static void normalsOnTile(unsigned int tileX, unsigned int tileY, unsigned int q
corner1 = {
world_coord(tileX),
world_coord(tileY),
psTile->height - nMod
psTile->height*(1.f/ELEVATION_SCALE) - nMod
},
corner2 = {
world_coord(tileX + 1),
world_coord(tileY),
tileRight->height - rMod
tileRight->height*(1.f/ELEVATION_SCALE) - rMod
},
corner3 = {
world_coord(tileX + 1),
world_coord(tileY + 1),
tileDownRight->height - drMod
tileDownRight->height*(1.f/ELEVATION_SCALE) - drMod
};

normals[(*numNormals)++] = pie_SurfaceNormal3fv( corner1, corner2, corner3);
Expand All @@ -213,17 +213,17 @@ static void normalsOnTile(unsigned int tileX, unsigned int tileY, unsigned int q
corner1 = {
world_coord(tileX),
world_coord(tileY),
psTile->height - nMod
psTile->height*(1.f/ELEVATION_SCALE) - nMod
},
corner2 = {
world_coord(tileX + 1),
world_coord(tileY + 1),
tileDownRight->height - drMod
tileDownRight->height*(1.f/ELEVATION_SCALE) - drMod
},
corner3 = {
world_coord(tileX),
world_coord(tileY + 1),
tileDown->height - dMod
tileDown->height*(1.f/ELEVATION_SCALE) - dMod
};

normals[(*numNormals)++] = pie_SurfaceNormal3fv( corner1, corner2, corner3);
Expand All @@ -240,17 +240,17 @@ static void normalsOnTile(unsigned int tileX, unsigned int tileY, unsigned int q
corner1 = {
world_coord(tileX),
world_coord(tileY),
psTile->height - nMod
psTile->height*(1.f/ELEVATION_SCALE) - nMod
},
corner2 = {
world_coord(tileX + 1),
world_coord(tileY),
tileRight->height - rMod
tileRight->height*(1.f/ELEVATION_SCALE) - rMod
},
corner3 = {
world_coord(tileX),
world_coord(tileY + 1),
tileDown->height - dMod
tileDown->height*(1.f/ELEVATION_SCALE) - dMod
};

normals[(*numNormals)++] = pie_SurfaceNormal3fv( corner1, corner2, corner3);
Expand All @@ -261,17 +261,17 @@ static void normalsOnTile(unsigned int tileX, unsigned int tileY, unsigned int q
corner1 = {
world_coord(tileX + 1),
world_coord(tileY),
tileRight->height - rMod
tileRight->height*(1.f/ELEVATION_SCALE) - rMod
},
corner2 = {
world_coord(tileX + 1),
world_coord(tileY + 1),
tileDownRight->height - drMod
tileDownRight->height*(1.f/ELEVATION_SCALE) - drMod
},
corner3 = {
world_coord(tileX),
world_coord(tileY + 1),
tileDown->height - dMod
tileDown->height*(1.f/ELEVATION_SCALE) - dMod
};

normals[(*numNormals)++] = pie_SurfaceNormal3fv( corner1, corner2, corner3);
Expand All @@ -285,17 +285,17 @@ static void normalsOnTile(unsigned int tileX, unsigned int tileY, unsigned int q
corner1 = {
world_coord(tileX),
world_coord(tileY),
psTile->height - nMod
psTile->height*(1.f/ELEVATION_SCALE) - nMod
},
corner2 = {
world_coord(tileX + 1),
world_coord(tileY + 1),
tileDownRight->height - drMod
tileDownRight->height*(1.f/ELEVATION_SCALE) - drMod
},
corner3 = {
world_coord(tileX),
world_coord(tileY + 1),
tileDown->height - dMod
tileDown->height*(1.f/ELEVATION_SCALE) - dMod
};

normals[(*numNormals)++] = pie_SurfaceNormal3fv( corner1, corner2, corner3);
Expand All @@ -306,17 +306,17 @@ static void normalsOnTile(unsigned int tileX, unsigned int tileY, unsigned int q
corner1 = {
world_coord(tileX),
world_coord(tileY),
psTile->height - nMod
psTile->height*(1.f/ELEVATION_SCALE) - nMod
},
corner2 = {
world_coord(tileX + 1),
world_coord(tileY),
tileRight->height - rMod
tileRight->height*(1.f/ELEVATION_SCALE) - rMod
},
corner3 = {
world_coord(tileX + 1),
world_coord(tileY + 1),
tileDownRight->height - drMod
tileDownRight->height*(1.f/ELEVATION_SCALE) - drMod
};

normals[(*numNormals)++] = pie_SurfaceNormal3fv( corner1, corner2, corner3);
Expand Down
60 changes: 30 additions & 30 deletions src/map.c
Expand Up @@ -188,7 +188,7 @@ BOOL mapNew(UDWORD width, UDWORD height)
psTile = psMapTiles;
for (i = 0; i < width * height; i++)
{
psTile->height = MAX_HEIGHT / 4;
psTile->height = MAX_HEIGHT*ELEVATION_SCALE / 4;
psTile->illumination = 255;
psTile->level = psTile->illumination;
memset(psTile->watchers, 0, sizeof(psTile->watchers));
Expand Down Expand Up @@ -834,7 +834,7 @@ BOOL mapLoad(char *filename, BOOL preview)
}

psMapTiles[i].texture = texture;
psMapTiles[i].height = height;
psMapTiles[i].height = height*ELEVATION_SCALE;

// Visibility stuff
memset(psMapTiles[i].watchers, 0, sizeof(psMapTiles[i].watchers));
Expand Down Expand Up @@ -884,11 +884,11 @@ BOOL mapLoad(char *filename, BOOL preview)
for (j = 0; j < mapHeight; j++)
{
// FIXME: magic number
mapTile(i, j)->waterLevel = mapTile(i, j)->height - world_coord(1) / 3.0f / (float)ELEVATION_SCALE;
mapTile(i, j)->waterLevel = mapTile(i, j)->height - world_coord(1) / 3;
// lower riverbed
if (mapTile(i, j)->ground == waterGroundType)
{
mapTile(i, j)->height -= (WATER_DEPTH - 2.0f * environGetData(i, j)) / (float)ELEVATION_SCALE;
mapTile(i, j)->height -= (WATER_DEPTH - 2 * environGetData(i, j));
}
}
}
Expand Down Expand Up @@ -996,11 +996,11 @@ BOOL mapSave(char **ppFileData, UDWORD *pFileSize)
psTileData->texture = psTile->texture;
if (psTile->ground == waterGroundType)
{
psTileData->height = MIN(255.0f, psTile->height + (WATER_DEPTH - 2.0f * environGetData(i % mapWidth, i / mapWidth)) / (float)ELEVATION_SCALE);
psTileData->height = MIN(255, (psTile->height + WATER_DEPTH - 2 * environGetData(i % mapWidth, i / mapWidth)) / ELEVATION_SCALE);
}
else
{
psTileData->height = psTile->height;
psTileData->height = psTile->height / ELEVATION_SCALE;
}

/* MAP_SAVETILE */
Expand Down Expand Up @@ -1087,30 +1087,30 @@ extern int32_t map_Height(int x, int y)
{
int tileX, tileY;
int i, j;
float height[2][2], center;
float onTileX, onTileY;
float left, right, middle;
float onBottom, result;
float towardsCenter, towardsRight;
int32_t height[2][2], center;
int32_t onTileX, onTileY;
int32_t left, right, middle;
int32_t onBottom, result;
int towardsCenter, towardsRight;

// Clamp x and y values to actual ones
// Give one tile worth of leeway before asserting, for units/transporters coming in from off-map.
ASSERT(x >= -TILE_UNITS, "map_Height: x value is too small (%d,%d) in %dx%d",map_coord(x),map_coord(y),mapWidth,mapHeight);
ASSERT(y >= -TILE_UNITS, "map_Height: y value is too small (%d,%d) in %dx%d",map_coord(x),map_coord(y),mapWidth,mapHeight);
x = (x < 0 ? 0 : x);
y = (y < 0 ? 0 : y);
x = MAX(x, 0);
y = MAX(y, 0);
ASSERT(x < world_coord(mapWidth)+TILE_UNITS, "map_Height: x value is too big (%d,%d) in %dx%d",map_coord(x),map_coord(y),mapWidth,mapHeight);
ASSERT(y < world_coord(mapHeight)+TILE_UNITS, "map_Height: y value is too big (%d,%d) in %dx%d",map_coord(x),map_coord(y),mapWidth,mapHeight);
x = (x >= world_coord(mapWidth) ? world_coord(mapWidth) - 1 : x);
y = (y >= world_coord(mapHeight) ? world_coord(mapHeight) - 1 : y);
x = MIN(x, world_coord(mapWidth) - 1);
y = MIN(y, world_coord(mapHeight) - 1);

// on which tile are these coords?
tileX = map_coord(x);
tileY = map_coord(y);

// where on the tile? (scale to (0,1))
onTileX = (x - world_coord(tileX))/(float)world_coord(1);
onTileY = (y - world_coord(tileY))/(float)world_coord(1);
onTileX = x - world_coord(tileX);
onTileY = y - world_coord(tileY);

// get the height for the corners and center
center = 0;
Expand All @@ -1135,31 +1135,31 @@ extern int32_t map_Height(int x, int y)
// get heights for left and right corners and the distances
if (onTileY > onTileX)
{
if (onTileY < 1 - onTileX)
if (onTileY < TILE_UNITS - onTileX)
{
// A
right = height[0][0];
left = height[0][1];
towardsCenter = onTileX;
towardsRight = 1 - onTileY;
towardsRight = TILE_UNITS - onTileY;
}
else
{
// B
right = height[0][1];
left = height[1][1];
towardsCenter = 1 - onTileY;
towardsRight = 1 - onTileX;
towardsCenter = TILE_UNITS - onTileY;
towardsRight = TILE_UNITS - onTileX;
}
}
else
{
if (onTileX > 1 - onTileY)
if (onTileX > TILE_UNITS - onTileY)
{
// C
right = height[1][1];
left = height[1][0];
towardsCenter = 1 - onTileX;
towardsCenter = TILE_UNITS - onTileX;
towardsRight = onTileY;
}
else
Expand All @@ -1171,17 +1171,17 @@ extern int32_t map_Height(int x, int y)
towardsRight = onTileX;
}
}
ASSERT(towardsCenter <= 0.5, "towardsCenter is too high");
ASSERT(towardsCenter <= TILE_UNITS/2, "towardsCenter is too high");

// now we have:
// center
// left m right

middle = (left + right)/2;
onBottom = left * (1 - towardsRight) + right * towardsRight;
onBottom = left * (TILE_UNITS - towardsRight) + right * towardsRight;
result = onBottom + (center - middle) * towardsCenter * 2;

return (SDWORD)(result+0.5f);
return (result + TILE_UNITS/2) / TILE_UNITS;
}

/* returns true if object is above ground */
Expand All @@ -1194,10 +1194,10 @@ extern BOOL mapObjIsAboveGround( BASE_OBJECT *psObj )
tileY = map_coord(psObj->pos.y),
tileYOffset1 = (tileY * mapWidth),
tileYOffset2 = ((tileY+1) * mapWidth),
h1 = psMapTiles[MIN(mapWidth * mapHeight - 1, tileYOffset1 + tileX) ].height,
h2 = psMapTiles[MIN(mapWidth * mapHeight - 1, tileYOffset1 + tileX + 1)].height,
h3 = psMapTiles[MIN(mapWidth * mapHeight - 1, tileYOffset2 + tileX) ].height,
h4 = psMapTiles[MIN(mapWidth * mapHeight - 1, tileYOffset2 + tileX + 1)].height;
h1 = psMapTiles[MIN(mapWidth * mapHeight - 1, tileYOffset1 + tileX) ].height / ELEVATION_SCALE,
h2 = psMapTiles[MIN(mapWidth * mapHeight - 1, tileYOffset1 + tileX + 1)].height / ELEVATION_SCALE,
h3 = psMapTiles[MIN(mapWidth * mapHeight - 1, tileYOffset2 + tileX) ].height / ELEVATION_SCALE,
h4 = psMapTiles[MIN(mapWidth * mapHeight - 1, tileYOffset2 + tileX + 1)].height / ELEVATION_SCALE;

/* trivial test above */
if ( (psObj->pos.z > h1) && (psObj->pos.z > h2) &&
Expand Down

0 comments on commit 275428c

Please sign in to comment.