Remove floats related to map height.

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.
master
Cyp 2010-11-16 20:02:22 +01:00
parent 902ec04272
commit 275428c161
12 changed files with 85 additions and 86 deletions

View File

@ -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++;
}
}
@ -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

View File

@ -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;
}

View File

@ -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;
}
}
}

View File

@ -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);

View File

@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);

View File

@ -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));
@ -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));
@ -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));
}
}
}
@ -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 */
@ -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;
@ -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
@ -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 */
@ -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) &&

View File

@ -113,15 +113,15 @@ typedef struct _maptile
uint8_t illumination; // How bright is this tile?
uint8_t watchers[MAX_PLAYERS]; // player sees through fog of war here with this many objects
uint16_t texture; // Which graphics texture is on this tile
float height; // The height at the top left of the tile
float level;
int32_t height; ///< The height at the top left of the tile
float level; ///< The visibility level of the top left of the tile, for this client.
BASE_OBJECT *psObject; // Any object sitting on the location (e.g. building)
PIELIGHT colour;
uint16_t limitedContinent; ///< For land or sea limited propulsion types
uint16_t hoverContinent; ///< For hover type propulsions
uint8_t ground; ///< The ground type used for the terrain renderer
uint16_t fireEndTime; ///< The (uint16_t)(gameTime / GAME_TICKS_PER_UPDATE) that BITS_ON_FIRE should be cleared.
float waterLevel; ///< At what height is the water for this tile
int32_t waterLevel; ///< At what height is the water for this tile
} MAPTILE;
@ -216,7 +216,6 @@ static inline unsigned char terrainType(const MAPTILE * tile)
/* The size and contents of the map */
extern SDWORD mapWidth, mapHeight;
extern MAPTILE *psMapTiles;
extern float waterLevel;
extern GROUND_TYPE *psGroundTypes;
extern int numGroundTypes;
@ -301,43 +300,43 @@ static inline WZ_DECL_PURE MAPTILE *mapTile(int32_t x, int32_t y)
#define worldTile(_x, _y) mapTile(map_coord(_x), map_coord(_y))
/// Return ground height of top-left corner of tile at x,y
static inline WZ_DECL_PURE float map_TileHeight(SDWORD x, SDWORD y)
static inline WZ_DECL_PURE int32_t map_TileHeight(int32_t x, int32_t y)
{
if ( x >= mapWidth || y >= mapHeight )
if ( x >= mapWidth || y >= mapHeight || x < 0 || y < 0 )
{
return 0;
}
return psMapTiles[x + (y * mapWidth)].height * ELEVATION_SCALE;
return psMapTiles[x + (y * mapWidth)].height;
}
/// Return water height of top-left corner of tile at x,y
static inline WZ_DECL_PURE float map_WaterHeight(SDWORD x, SDWORD y)
static inline WZ_DECL_PURE int32_t map_WaterHeight(int32_t x, int32_t y)
{
if ( x >= mapWidth || y >= mapHeight )
if ( x >= mapWidth || y >= mapHeight || x < 0 || y < 0 )
{
return 0;
}
return psMapTiles[x + (y * mapWidth)].waterLevel * ELEVATION_SCALE;
return psMapTiles[x + (y * mapWidth)].waterLevel;
}
/// Return max(ground, water) height of top-left corner of tile at x,y
static inline WZ_DECL_PURE float map_TileHeightSurface(SDWORD x, SDWORD y)
static inline WZ_DECL_PURE int32_t map_TileHeightSurface(int32_t x, int32_t y)
{
if ( x >= mapWidth || y >= mapHeight )
if ( x >= mapWidth || y >= mapHeight || x < 0 || y < 0 )
{
return 0;
}
return MAX(psMapTiles[x + (y * mapWidth)].height, psMapTiles[x + (y * mapWidth)].waterLevel) * ELEVATION_SCALE;
return MAX(psMapTiles[x + (y * mapWidth)].height, psMapTiles[x + (y * mapWidth)].waterLevel);
}
/*sets the tile height */
static inline void setTileHeight(SDWORD x, SDWORD y, float height)
static inline void setTileHeight(int32_t x, int32_t y, int32_t height)
{
ASSERT_OR_RETURN( , x < mapWidth, "x coordinate %u bigger than map width %u", x, mapWidth);
ASSERT_OR_RETURN( , y < mapHeight, "y coordinate %u bigger than map height %u", y, mapHeight);
ASSERT_OR_RETURN( , x < mapWidth && x >= 0, "x coordinate %d bigger than map width %u", x, mapWidth);
ASSERT_OR_RETURN( , y < mapHeight && x >= 0, "y coordinate %d bigger than map height %u", y, mapHeight);
psMapTiles[x + (y * mapWidth)].height = height / (float)ELEVATION_SCALE;
psMapTiles[x + (y * mapWidth)].height = height;
markTileDirty(x, y);
}

View File

@ -335,7 +335,7 @@ void loadMapPreview(bool hideInterface)
for (x = 0; x < mapWidth; x++)
{
char * const p = imageData + (3 * (y * BACKDROP_HACK_WIDTH + x));
height = WTile->height;
height = WTile->height / ELEVATION_SCALE;
col = height;
switch (terrainType(WTile))

View File

@ -358,9 +358,9 @@ static PIELIGHT appliedRadarColour(RADAR_DRAW_MODE radarDrawMode, MAPTILE *WTile
// draw radar terrain on/off feature
PIELIGHT col = tileColours[TileNumber_tile(WTile->texture)];
col.byte.r = sqrtf(col.byte.r * (WTile->illumination + WTile->height) / 2);
col.byte.b = sqrtf(col.byte.b * (WTile->illumination + WTile->height) / 2);
col.byte.g = sqrtf(col.byte.g * (WTile->illumination + WTile->height) / 2);
col.byte.r = sqrtf(col.byte.r * (WTile->illumination + WTile->height / ELEVATION_SCALE) / 2);
col.byte.b = sqrtf(col.byte.b * (WTile->illumination + WTile->height / ELEVATION_SCALE) / 2);
col.byte.g = sqrtf(col.byte.g * (WTile->illumination + WTile->height / ELEVATION_SCALE) / 2);
if (terrainType(WTile) == TER_CLIFFFACE)
{
col.byte.r /= 2;
@ -378,7 +378,7 @@ static PIELIGHT appliedRadarColour(RADAR_DRAW_MODE radarDrawMode, MAPTILE *WTile
break;
case RADAR_MODE_HEIGHT_MAP:
{
WScr.byte.r = WScr.byte.g = WScr.byte.b = WTile->height;
WScr.byte.r = WScr.byte.g = WScr.byte.b = WTile->height / ELEVATION_SCALE;
}
break;
case RADAR_MODE_NO_TERRAIN:

View File

@ -11210,7 +11210,7 @@ BOOL scrSetTileHeight(void)
psTile = mapTile(tileX,tileY);
psTile->height = (UBYTE)newHeight;
psTile->height = (UBYTE)newHeight * ELEVATION_SCALE;
return true;
}

View File

@ -5246,7 +5246,7 @@ SWORD buildFoundation(STRUCTURE *psStruct, UDWORD x, UDWORD y)
{
if(TileHasStructure(mapTile(startX+width,startY+breadth)))
{
return((SWORD)map_TileHeight(startX+width,startY+breadth));
return map_TileHeight(startX+width, startY+breadth);
}
}
}

View File

@ -186,7 +186,7 @@ static void doWaveTerrain(int sx, int sy, int sz, unsigned radius, int rayPlayer
}
psTile = mapTile(mapX, mapY);
tileHeight = psTile->height * ELEVATION_SCALE;
tileHeight = psTile->height;
perspectiveHeight = (tileHeight - sz) * tiles[i].invRadius;
if (tiles[i].angBegin < lastAngle)