Readability of code and usage examples for TILE_UNITS/TILE_SHIFT

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@1232 4a71c877-e1ca-e34f-864e-861f7616d084
master
Dennis Schridde 2007-03-14 14:41:46 +00:00
parent 7e12df4e35
commit 5f98bde6d6
3 changed files with 24 additions and 15 deletions

View File

@ -277,7 +277,7 @@ SDWORD aiBestNearestTarget(DROID *psDroid, BASE_OBJECT **ppsObj, int weapon_slot
/* Calculates attack priority for a certain target */
SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker, SDWORD weapon_slot)
{
SDWORD targetTypeBonus,damage,attackWeight=0,noTarget=-1;
SDWORD targetTypeBonus=0, damageRatio=0, attackWeight=0, noTarget=-1;
DROID *targetDroid;
STRUCTURE *targetStructure;
WEAPON_EFFECT weaponEffect;
@ -306,7 +306,7 @@ SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker, SDWORD
targetDroid = (DROID *)psTarget;
/* Calculate damage this target suffered */
damage = targetDroid->originalBody - targetDroid->body;
damageRatio = 1 - targetDroid->body / targetDroid->originalBody;
/* See if this type of a droid should be prioritized */
switch (targetDroid->droidType)
@ -335,17 +335,17 @@ SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker, SDWORD
}
/* Now calculate the overall weight */
attackWeight = asWeaponModifier[weaponEffect][(asPropulsionStats + targetDroid->asBits[COMP_PROPULSION].nStat)->propulsionType]//Weapon effect
- ( WEIGHT_DIST_TILE_DROID * ( dirtySqrt(psAttacker->x, psAttacker->y, targetDroid->x, targetDroid->y) >> TILE_SHIFT ) )//substract WEIGHT_DIST_TILE_DROID per tile, 128 world units in a tile
+ ( damage * 10 / targetDroid->originalBody ) * WEIGHT_HEALTH_DROID//we prefer damaged droids
+ targetTypeBonus;//some droid types have higher priority
attackWeight = asWeaponModifier[weaponEffect][(asPropulsionStats + targetDroid->asBits[COMP_PROPULSION].nStat)->propulsionType] // Our weapon's effect against target
- WEIGHT_DIST_TILE_DROID * ( dirtySqrt(psAttacker->x, psAttacker->y, targetDroid->x, targetDroid->y) >> TILE_SHIFT ) // farer droids are less attractive
+ WEIGHT_HEALTH_DROID * damageRatio // we prefer damaged droids
+ targetTypeBonus; // some droid types have higher priority
}
else if(psTarget->type == OBJ_STRUCTURE)
{
targetStructure = (STRUCTURE *)psTarget;
/* Calculate damage this target suffered */
damage = structureBody(targetStructure) - targetStructure->body;
damageRatio = 1 - targetStructure->body / structureBody(targetStructure);
/* See if this type of a structure should be prioritized */
switch(targetStructure->pStructureType->type)
@ -366,10 +366,10 @@ SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker, SDWORD
}
/* Now calculate the overall weight */
attackWeight = asStructStrengthModifier[weaponEffect][targetStructure->pStructureType->strength] //Weapon effect
- (WEIGHT_DIST_TILE_STRUCT * (dirtySqrt(psAttacker->x, psAttacker->y,targetStructure->x,targetStructure->y) >> TILE_SHIFT) ) //substract WEIGHT_DIST_TILE_STRUCT per tile, 128 world units in a tile
+ (damage * 10 / structureBody(targetStructure) ) * WEIGHT_HEALTH_STRUCT //we prefer damaged structures
+ targetTypeBonus; //some structure types have higher priority
attackWeight = asStructStrengthModifier[weaponEffect][targetStructure->pStructureType->strength] // Our weapon's effect against target
- WEIGHT_DIST_TILE_STRUCT * ( dirtySqrt(psAttacker->x, psAttacker->y, targetStructure->x, targetStructure->y) >> TILE_SHIFT ) // farer structs are less attractive
+ WEIGHT_HEALTH_STRUCT * damageRatio // we prefer damaged structures
+ targetTypeBonus; // some structure types have higher priority
/* Go for unfinished structures only if nothing else found (same for non-visible structures) */
if(targetStructure->status != SS_BUILT) //a decoy?

View File

@ -46,9 +46,9 @@
#define WEIGHT_DIST_TILE 11 //In points used in weaponmodifier.txt and structuremodifier.txt
#define WEIGHT_DIST_TILE_DROID WEIGHT_DIST_TILE //How much weight a distance of 1 tile (128 world units) has when looking for the best nearest target
#define WEIGHT_DIST_TILE_STRUCT WEIGHT_DIST_TILE
#define WEIGHT_HEALTH_DROID WEIGHT_DIST_TILE //How much weight unit damage has (per 10% of damage, ie for each 10% of damage we add WEIGHT_HEALTH_DROID)
//~100% damage should be ~8 tiles (max sensor range)
#define WEIGHT_HEALTH_STRUCT WEIGHT_DIST_TILE
#define WEIGHT_HEALTH_DROID (WEIGHT_DIST_TILE * 10) //How much weight unit damage has (100% of damage is equaly weighted as 10 tiles distance)
//~100% damage should be ~8 tiles (max sensor range)
#define WEIGHT_HEALTH_STRUCT (WEIGHT_DIST_TILE * 10)
#define WEIGHT_NOT_VISIBLE_F 10 //We really don't like objects we can't see

View File

@ -189,10 +189,19 @@ extern MAPTILE *psMapTiles;
/* The shift on the y coord when calculating into the map */
extern UDWORD mapShift;
/*
* Note:
* TILE_UNITS = (1 << TILE_SHIFT)
*
* Usage-Example:
* tile_coordinate = (world_coordinate / TILE_UNITS) = (world_coordinate >> TILE_SHIFT)
* world_coordinate = (tile_coordinate * TILE_UNITS) = (tile_coordinate << TILE_SHIFT)
*/
/* The number of units accross a tile */
#define TILE_UNITS 128
/* The shift on a coordinate to get the tile coordinate */
/* The shift on a world coordinate to get the tile coordinate */
#define TILE_SHIFT 7
/* The mask to get internal tile coords from a full coordinate */