* Rename dirtySqrt() to dirtyHypot() because its more similar to hypot() than to sqrt()

* Change dirtyHypot()'s interface to match that of hypot() (i.e. specify the coordinates relative to the origin instead of another coordinate)
 * Mark dirtyHpot() as deprecated in the Doxygen documentation and suggest to use hypot() or hypotf() instead (C99 functions for which I'll provide work arounds in math-help.h soon after this commit)


git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@6155 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2008-10-14 19:31:40 +00:00
parent 29d7c7abfd
commit 05cc7093b4
6 changed files with 49 additions and 39 deletions

View File

@ -147,7 +147,7 @@ SDWORD aiBestNearestTarget(DROID *psDroid, BASE_OBJECT **ppsObj, int weapon_slot
if(friendlyDroid->order != DORDER_ATTACK)
{
// make sure target is near enough
if (dirtySqrt(psDroid->pos.x, psDroid->pos.y, tempTarget->pos.x, tempTarget->pos.y)
if (dirtyHypot(psDroid->pos.x - tempTarget->pos.x, psDroid->pos.y - tempTarget->pos.y)
< droidSensorRange(psDroid))
{
targetInQuestion = tempTarget; //consider this target
@ -405,7 +405,7 @@ static SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker,
/* Now calculate the overall weight */
attackWeight = asWeaponModifier[weaponEffect][(asPropulsionStats + targetDroid->asBits[COMP_PROPULSION].nStat)->propulsionType] // Our weapon's effect against target
- WEIGHT_DIST_TILE_DROID * map_coord(dirtySqrt(psAttacker->pos.x, psAttacker->pos.y, targetDroid->pos.x, targetDroid->pos.y)) // farer droids are less attractive
- WEIGHT_DIST_TILE_DROID * map_coord(dirtyHypot(psAttacker->pos.x - targetDroid->pos.x, psAttacker->pos.y - targetDroid->pos.y)) // farer droids are less attractive
+ WEIGHT_HEALTH_DROID * damageRatio // we prefer damaged droids
+ targetTypeBonus; // some droid types have higher priority
@ -446,7 +446,7 @@ static SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker,
/* Now calculate the overall weight */
attackWeight = asStructStrengthModifier[weaponEffect][targetStructure->pStructureType->strength] // Our weapon's effect against target
- WEIGHT_DIST_TILE_STRUCT * map_coord(dirtySqrt(psAttacker->pos.x, psAttacker->pos.y, targetStructure->pos.x, targetStructure->pos.y)) // farer structs are less attractive
- WEIGHT_DIST_TILE_STRUCT * map_coord(dirtyHypot(psAttacker->pos.x - targetStructure->pos.x, psAttacker->pos.y - targetStructure->pos.y)) // farer structs are less attractive
+ WEIGHT_HEALTH_STRUCT * damageRatio // we prefer damaged structures
+ targetTypeBonus; // some structure types have higher priority

View File

@ -814,7 +814,7 @@ SDWORD GetBaseDefendLocIndex(SDWORD x, SDWORD y, SDWORD nPlayer)
if((baseDefendLocation[nPlayer][i][0] > 0) && (baseDefendLocation[nPlayer][i][1] > 0)) //if this one initialized
{
//check if very close to an already stored location
if(dirtySqrt(x,y,baseDefendLocation[nPlayer][i][0],baseDefendLocation[nPlayer][i][1]) < range)
if (dirtyHypot(x - baseDefendLocation[nPlayer][i][0], y - baseDefendLocation[nPlayer][i][1]) < range)
{
return i; //end here
}
@ -965,7 +965,7 @@ SDWORD GetOilDefendLocIndex(SDWORD x, SDWORD y, SDWORD nPlayer)
if((oilDefendLocation[nPlayer][i][0] > 0) && (oilDefendLocation[nPlayer][i][1] > 0)) //if this one initialized
{
//check if very close to an already stored location
if(dirtySqrt(x,y,oilDefendLocation[nPlayer][i][0],oilDefendLocation[nPlayer][i][1]) < range)
if (dirtyHypot(x - oilDefendLocation[nPlayer][i][0], y - oilDefendLocation[nPlayer][i][1]) < range)
{
return i; //end here
}

View File

@ -159,12 +159,22 @@ SDWORD sum;
return 0;
}
// Approximates a square root - never more than 11% out...
UDWORD dirtySqrt( SDWORD x1, SDWORD y1, SDWORD x2, SDWORD y2)
/**
* Approximates the euclidian distance function, never moret than 11% out.
*
* Mathematically equivalent to sqrt(deltaX * deltaX + deltaY * deltaY).
*
* @Deprecated All uses of this function should be replaced by calls to hypot()
* or hypotf(), the C99 functions. This because this integer
* optimisation is no longer required (due to hardware improvements
* since 1997).
*/
unsigned int WZ_DECL_CONST dirtyHypot(int deltaX, int deltaY)
{
const UDWORD xDif = abs(x1 - x2), yDif = abs(y1 - y2);
return MAX(xDif, yDif) + MIN(xDif, yDif) / 2;
deltaX = abs(deltaX);
deltaY = abs(deltaY);
return MAX(deltaX, deltaY) + MIN(deltaX, deltaY) / 2;
}
//-----------------------------------------------------------------------------------

View File

@ -36,7 +36,7 @@ extern int inQuad( const Vector2i *pt, const QUAD *quad );
extern DROID *getNearestDroid( UDWORD x, UDWORD y, BOOL bSelected );
extern BOOL droidOnScreen( DROID *psDroid, SDWORD tolerance );
extern UDWORD dirtySqrt( SDWORD x1, SDWORD y1, SDWORD x2, SDWORD y2 ) WZ_DECL_CONST;
extern unsigned int WZ_DECL_CONST dirtyHypot(int deltaX, int deltaY);
static inline STRUCTURE *getTileStructure(UDWORD x, UDWORD y)
{

View File

@ -1919,7 +1919,7 @@ static BOOL defenseLocation(BOOL variantB)
gX = (psGate->x1 + psGate->x2)/2;
gY = (psGate->y1 + psGate->y2)/2;
/* Estimate the distance to it */
dist = dirtySqrt(x,y,gX,gY);
dist = dirtyHypot(x - gX, y - gY);
/* Is it best we've found? */
if(dist<nearestSoFar && dist<30)
{

View File

@ -4965,7 +4965,7 @@ BOOL scrDistanceTwoPts( void )
}
/* Approximate the distance */
scrFunctionResult.v.ival = (SDWORD)dirtySqrt(x1,y1,x2,y2);
scrFunctionResult.v.ival = dirtyHypot(x1 - x2, y1 - y2);
if(!stackPushResult(VAL_INT, &scrFunctionResult))
{
ASSERT( false,"SCRIPT : Distance between two points - cannot return scrFunctionResult" );
@ -5189,7 +5189,7 @@ BOOL scrGetNearestGateway( void )
gY = (psGateway->y1 + psGateway->y2)/2;
/* Estimate the distance to it */
dist = dirtySqrt(x,y,gX,gY);
dist = dirtyHypot(x - gX, y - gY);
/* Is it best we've found? */
if(dist<nearestSoFar)
@ -7265,7 +7265,7 @@ BOOL ThreatInRange(SDWORD player, SDWORD range, SDWORD rangeX, SDWORD rangeY, BO
case REF_REARM_PAD:
if (range < 0
|| world_coord(dirtySqrt(tx, ty, map_coord(psStruct->pos.x), map_coord(psStruct->pos.y))) < range) //enemy in range
|| world_coord(dirtyHypot(tx - map_coord(psStruct->pos.x), ty - map_coord(psStruct->pos.y))) < range) //enemy in range
{
return true;
}
@ -7293,7 +7293,7 @@ BOOL ThreatInRange(SDWORD player, SDWORD range, SDWORD rangeX, SDWORD rangeY, BO
}
if (range < 0
|| world_coord(dirtySqrt(tx, ty , map_coord(psDroid->pos.x), map_coord(psDroid->pos.y))) < range) //enemy in range
|| world_coord(dirtyHypot(tx - map_coord(psDroid->pos.x), ty - map_coord(psDroid->pos.y))) < range) //enemy in range
{
return true;
}
@ -7349,10 +7349,10 @@ BOOL scrFogTileInRange(void)
{
//within base range
if (wRange <= 0
|| world_coord(dirtySqrt(tRangeX, tRangeY, i, j)) < wRange) //dist in world units between baseX/baseY and the tile
|| world_coord(dirtyHypot(tRangeX - i, tRangeY - j)) < wRange) //dist in world units between baseX/baseY and the tile
{
//calc dist between this tile and looker
wDist = world_coord(dirtySqrt(tx, ty, i, j));
wDist = world_coord(dirtyHypot(tx - i, ty - j));
//closer than last one?
if(wDist < wBestDist)
@ -7440,8 +7440,8 @@ BOOL scrMapRevealedInRange(void)
if(abs(tRangeX-i) < tRange && abs(tRangeY-j) < tRange)
{
//within range
if ((world_coord(dirtySqrt(tRangeX, tRangeY, i, j)) < wRange) && //dist in world units between x/y and the tile
TEST_TILE_VISIBLE( player,mapTile(i,j) )) //not visible
if (world_coord(dirtyHypot(tRangeX - i, tRangeY - j)) < wRange //dist in world units between x/y and the tile
&& TEST_TILE_VISIBLE(player, mapTile(i, j))) //not visible
{
scrFunctionResult.v.bval = true;
if (!stackPushResult(VAL_BOOL, &scrFunctionResult))
@ -7915,7 +7915,8 @@ static UDWORD costOrAmountInRange(SDWORD player, SDWORD lookingPlayer, SDWORD ra
continue;
}
if((range < 0) || (dirtySqrt(rangeX, rangeY , psDroid->pos.x, psDroid->pos.y) < range)) //enemy in range
if (range < 0
|| dirtyHypot(rangeX - psDroid->pos.x, rangeY - psDroid->pos.y) < range) //enemy in range
{
if (justCount)
{
@ -7966,7 +7967,7 @@ UDWORD numPlayerWeapStructsInRange(SDWORD player, SDWORD lookingPlayer, SDWORD r
if(!bFinished || psStruct->status == SS_BUILT)
{
if (range < 0
|| world_coord(dirtySqrt(tx, ty, map_coord(psStruct->pos.x), map_coord(psStruct->pos.y))) < range) //enemy in range
|| world_coord(dirtyHypot(tx - map_coord(psStruct->pos.x), ty - map_coord(psStruct->pos.y))) < range) //enemy in range
{
numStructs++;
}
@ -7992,17 +7993,16 @@ UDWORD playerWeapStructsCostInRange(SDWORD player, SDWORD lookingPlayer, SDWORD
//check structures
for(psStruct = apsStructLists[player]; psStruct; psStruct=psStruct->psNext)
{
if(psStruct->visible[lookingPlayer]) //if can see it
if (psStruct->visible[lookingPlayer] //if can see it
&& objHasWeapon((BASE_OBJECT *) psStruct))
{
if(objHasWeapon((BASE_OBJECT *) psStruct))
if (!bFinished
|| psStruct->status == SS_BUILT)
{
if(!bFinished || psStruct->status == SS_BUILT)
if (range < 0
|| world_coord(dirtyHypot(tx - map_coord(psStruct->pos.x), ty - map_coord(psStruct->pos.y))) < range) //enemy in range
{
if((range < 0) || ((dirtySqrt(tx, ty, psStruct->pos.x >> TILE_SHIFT, psStruct->pos.y >> TILE_SHIFT)
<< TILE_SHIFT) < range)) //enemy in range
{
structsCost += structPowerToBuild(psStruct);
}
structsCost += structPowerToBuild(psStruct);
}
}
}
@ -8297,7 +8297,7 @@ UDWORD numEnemyObjInRange(SDWORD player, SDWORD range, SDWORD rangeX, SDWORD ran
if(!bFinished || psStruct->status == SS_BUILT)
{
if (range < 0
|| world_coord(dirtySqrt(tx, ty, map_coord(psStruct->pos.x), map_coord(psStruct->pos.y))) < range) //enemy in range
|| world_coord(dirtyHypot(tx - map_coord(psStruct->pos.x), ty - map_coord(psStruct->pos.y))) < range) //enemy in range
{
numEnemies++;
}
@ -8317,7 +8317,7 @@ UDWORD numEnemyObjInRange(SDWORD player, SDWORD range, SDWORD rangeX, SDWORD ran
}
if (range < 0
|| world_coord(dirtySqrt(tx, ty , map_coord(psDroid->pos.x), map_coord(psDroid->pos.y))) < range) //enemy in range
|| world_coord(dirtyHypot(tx - map_coord(psDroid->pos.x), ty - map_coord(psDroid->pos.y))) < range) //enemy in range
{
numEnemies++;
}
@ -8832,7 +8832,7 @@ BOOL scrGetClosestEnemy(void)
continue;
}
dist = world_coord(dirtySqrt(tx, ty , map_coord(psDroid->pos.x), map_coord(psDroid->pos.y)));
dist = world_coord(dirtyHypot(tx - map_coord(psDroid->pos.x), ty - map_coord(psDroid->pos.y)));
if(dist < bestDist)
{
if((range < 0) || (dist < range)) //enemy in range
@ -8857,7 +8857,7 @@ BOOL scrGetClosestEnemy(void)
continue;
}
dist = world_coord(dirtySqrt(tx, ty, map_coord(psStruct->pos.x), map_coord(psStruct->pos.y)));
dist = world_coord(dirtyHypot(tx - map_coord(psStruct->pos.x), ty - map_coord(psStruct->pos.y)));
if(dist < bestDist)
{
if((range < 0) || (dist < range)) //in range
@ -9211,7 +9211,7 @@ BOOL scrGetClosestEnemyDroidByType(void)
continue;
}
dist = world_coord(dirtySqrt(tx, ty , map_coord(psDroid->pos.x), map_coord(psDroid->pos.y)));
dist = world_coord(dirtyHypot(tx - map_coord(psDroid->pos.x), ty - map_coord(psDroid->pos.y)));
if(dist < bestDist)
{
if(dist < range) //enemy in range
@ -9293,7 +9293,7 @@ BOOL scrGetClosestEnemyStructByType(void)
continue;
}
dist = world_coord(dirtySqrt(tx, ty, map_coord(psStruct->pos.x), map_coord(psStruct->pos.y)));
dist = world_coord(dirtyHypot(tx - map_coord(psStruct->pos.x), ty - map_coord(psStruct->pos.y)));
if(dist < bestDist)
{
if((range < 0) || (dist < range)) //in range or no range check
@ -9353,7 +9353,7 @@ BOOL scrCirclePerimPoint(void)
tempx = (float)(*grx - basex); //x len (signed!)
tempy = (float)(*gry - basey);
dist = dirtySqrt(basex,basey,*grx,*gry); //len
dist = dirtyHypot(basex - *grx, basey - *gry); //len
factor = (float)((float)dist / (float)radius); //by what factor is dist > radius?
@ -9474,7 +9474,7 @@ BOOL scrNumAAinRange(void)
(asWeaponStats[psStruct->asWeaps[0].nStat].surfaceToAir == SHOOT_IN_AIR) )
{
if (range < 0
|| world_coord(dirtySqrt(tx, ty, map_coord(psStruct->pos.x), map_coord(psStruct->pos.y))) < range) //enemy in range
|| world_coord(dirtyHypot(tx - map_coord(psStruct->pos.x), ty - map_coord(psStruct->pos.y))) < range) //enemy in range
{
numFound++;
}
@ -10358,7 +10358,7 @@ BOOL scrClosestDamagedGroupDroid(void)
{
if((psDroid->body * 100 / psDroid->originalBody) <= healthLeft) //in%
{
wDist = map_coord(dirtySqrt(psDroid->pos.x, psDroid->pos.y, x, y)); //in tiles
wDist = map_coord(dirtyHypot(psDroid->pos.x - x, psDroid->pos.y - y)); //in tiles
if(wDist < wBestDist)
{
if((maxRepairedBy < 0) || (getNumRepairedBy(psDroid, player) <= maxRepairedBy))