Remove some code duplication from the getProduction* functions and a lot of code duplication from the pickATile* functions.

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@6435 4a71c877-e1ca-e34f-864e-861f7616d084
master
Gerard Krol 2008-12-04 22:21:10 +00:00
parent 94b7867399
commit abbcda662f
2 changed files with 34 additions and 178 deletions

View File

@ -3857,67 +3857,22 @@ static BOOL sensiblePlace(SDWORD x, SDWORD y, PROPULSION_TYPE propulsion)
// Should stop things being placed in inaccessible areas? Assume wheeled propulsion.
BOOL zonedPAT(UDWORD x, UDWORD y)
{
if (sensiblePlace(x, y, PROPULSION_TYPE_WHEELED) && noDroid(x,y))
{
return(true);
}
else
{
return(false);
}
return sensiblePlace(x, y, PROPULSION_TYPE_WHEELED) && noDroid(x,y);
}
// ------------------------------------------------------------------------------------
static BOOL canFitDroid(UDWORD x, UDWORD y)
{
return sensiblePlace(x, y, PROPULSION_TYPE_WHEELED) && (noDroid(x,y) || oneDroid(x, y));
}
/// find a tile for which the function will return true
BOOL pickATileGen(UDWORD *x, UDWORD *y, UBYTE numIterations,
BOOL (*function)(UDWORD x, UDWORD y))
{
SDWORD i,j;
SDWORD startX,endX,startY,endY;
UDWORD passes;
ASSERT( *x<mapWidth,"x coordinate is off-map for pickATileGen" );
ASSERT( *y<mapHeight,"y coordinate is off-map for pickATileGen" );
/* Exit if they're fine! */
if (sensiblePlace(*x, *y, PROPULSION_TYPE_WHEELED) && noDroid(*x,*y))
{
return(true);
}
/* Initial box dimensions and set iteration count to zero */
startX = endX = *x; startY = endY = *y; passes = 0;
/* Keep going until we get a tile or we exceed distance */
while(passes<numIterations)
{
/* Process whole box */
for(i = startX; i <= endX; i++)
{
for(j = startY; j<= endY; j++)
{
/* Test only perimeter as internal tested previous iteration */
if(i==startX || i==endX || j==startY || j==endY)
{
/* Good enough? */
if(function(i,j))
{
/* Set exit conditions and get out NOW */
*x = i; *y = j;
return(true);
}
}
}
}
/* Expand the box out in all directions - off map handled by tileAcceptable */
startX--; startY--; endX++; endY++; passes++;
}
/* If we got this far, then we failed - passed in values will be unchanged */
return(false);
return pickATileGenThreat(x, y, numIterations, -1, -1, function);
}
//same as orig, but with threat check
/// find a tile for which the passed function will return true without any threat in the specified range
BOOL pickATileGenThreat(UDWORD *x, UDWORD *y, UBYTE numIterations, SDWORD threatRange,
SDWORD player, BOOL (*function)(UDWORD x, UDWORD y))
{
@ -3966,108 +3921,16 @@ UDWORD passes;
}
// ------------------------------------------------------------------------------------
/* Improved pickATile - Replaces truly scary existing one. */
/* AM 22 - 10 - 98 */
/// find an empty tile accessible to a wheeled droid
BOOL pickATile(UDWORD *x, UDWORD *y, UBYTE numIterations)
{
SDWORD i,j;
SDWORD startX,endX,startY,endY;
UDWORD passes;
ASSERT( *x<mapWidth,"x coordinate is off-map for pickATile" );
ASSERT( *y<mapHeight,"y coordinate is off-map for pickATile" );
/* Exit if they're fine! */
if (sensiblePlace(*x, *y, PROPULSION_TYPE_WHEELED) && noDroid(*x,*y))
{
return(true);
}
/* Initial box dimensions and set iteration count to zero */
startX = endX = *x; startY = endY = *y; passes = 0;
/* Keep going until we get a tile or we exceed distance */
while(passes<numIterations)
{
/* Process whole box */
for(i = startX; i <= endX; i++)
{
for(j = startY; j<= endY; j++)
{
/* Test only perimeter as internal tested previous iteration */
if(i==startX || i==endX || j==startY || j==endY)
{
/* Good enough? */
if (sensiblePlace(i, j, PROPULSION_TYPE_WHEELED) && noDroid(i,j))
{
/* Set exit conditions and get out NOW */
*x = i; *y = j;
return(true);
}
}
}
}
/* Expand the box out in all directions - off map handled by tileAcceptable */
startX--; startY--; endX++; endY++; passes++;
}
/* If we got this far, then we failed - passed in values will be unchanged */
return(false);
return pickATileGen(x, y, numIterations, zonedPAT);
}
// pickHalfATile just like improved pickATile but with Double Density Droid Placement
/// find a tile for a wheeled droid with only one other droid present
PICKTILE pickHalfATile(UDWORD *x, UDWORD *y, UBYTE numIterations)
{
SDWORD i,j;
SDWORD startX,endX,startY,endY;
UDWORD passes;
/*
Why was this written - I wrote pickATileGen to take a function
pointer for what qualified as a valid tile - could use that.
I'm not going to change it in case I'm missing the point */
if (pickATileGen(x, y, numIterations,zonedPAT))
{
return FREE_TILE;
}
/* Exit if they're fine! */
if (sensiblePlace(*x, *y, PROPULSION_TYPE_WHEELED) && oneDroid(*x, *y))
{
return HALF_FREE_TILE;
}
/* Initial box dimensions and set iteration count to zero */
startX = endX = *x; startY = endY = *y; passes = 0;
/* Keep going until we get a tile or we exceed distance */
while(passes<numIterations)
{
/* Process whole box */
for(i = startX; i <= endX; i++)
{
for(j = startY; j<= endY; j++)
{
/* Test only perimeter as internal tested previous iteration */
if(i==startX || i==endX || j==startY || j==endY)
{
/* Good enough? */
if (sensiblePlace(i, j, PROPULSION_TYPE_WHEELED) && oneDroid(i,j))
{
/* Set exit conditions and get out NOW */
*x = i; *y = j;
return HALF_FREE_TILE;
}
}
}
}
/* Expand the box out in all directions - off map handled by tileAcceptable */
startX--; startY--; endX++; endY++; passes++;
}
/* If we got this far, then we failed - passed in values will be unchanged */
return NO_FREE_TILE;
return pickATileGen(x, y, numIterations, canFitDroid);
}
/* Looks through the players list of droids to see if any of them are

View File

@ -6857,9 +6857,11 @@ void factoryProdAdjust(STRUCTURE *psStructure, DROID_TEMPLATE *psTemplate, BOOL
}
}
//returns the quantity of a specific template in the production list
UDWORD getProductionQuantity(STRUCTURE *psStructure, DROID_TEMPLATE *psTemplate)
/** checks the status of the production of a template
* if totalquantity is true, it will return the total ordered amount
* it it is false, it will return the amount that has already been built
*/
static int getProduction(STRUCTURE *psStructure, DROID_TEMPLATE *psTemplate, BOOL totalQuantity)
{
UDWORD inc, factoryType, factoryInc;
FACTORY *psFactory;
@ -6876,7 +6878,14 @@ UDWORD getProductionQuantity(STRUCTURE *psStructure, DROID_TEMPLATE *psTemplate)
{
if (asProductionRun[factoryType][factoryInc][inc].psTemplate == psTemplate)
{
return asProductionRun[factoryType][factoryInc][inc].quantity;
if (totalQuantity)
{
return asProductionRun[factoryType][factoryInc][inc].quantity;
}
else
{
return asProductionRun[factoryType][factoryInc][inc].built;
}
}
}
}
@ -6885,33 +6894,17 @@ UDWORD getProductionQuantity(STRUCTURE *psStructure, DROID_TEMPLATE *psTemplate)
return 0;
}
/// the total amount ordered of a specific template in the production list
UDWORD getProductionQuantity(STRUCTURE *psStructure, DROID_TEMPLATE *psTemplate)
{
return getProduction(psStructure, psTemplate, true);
}
/*returns the quantity of a specific template in the production list that
have already been built*/
/// the number of times a specific template in the production list has been built
UDWORD getProductionBuilt(STRUCTURE *psStructure, DROID_TEMPLATE *psTemplate)
{
UDWORD inc, factoryType, factoryInc;
FACTORY *psFactory;
if (psStructure == NULL) return 0;
if (psStructure->player == productionPlayer)
{
psFactory = &psStructure->pFunctionality->factory;
factoryType = psFactory->psAssemblyPoint->factoryType;
factoryInc = psFactory->psAssemblyPoint->factoryInc;
//see if the template is in the list
for (inc=0; inc < MAX_PROD_RUN; inc++)
{
if (asProductionRun[factoryType][factoryInc][inc].psTemplate == psTemplate)
{
return asProductionRun[factoryType][factoryInc][inc].built;
}
}
}
//not in the list so none being produced
return 0;
return getProduction(psStructure, psTemplate, false);
}