* Remove a futile check for error condition (psBuilding->psStructureType->numFuncs == 0) which can never occur unless errors occur while loading a stats file (and if they do, they should be detected by the code loading that file)

* Use calloc instead of malloc followed by memset(0) (calloc both allocates memory; and then sets all of it to zero)
 * Move a lot of duplicated malloc (calloc now) calls out of the switch statement; and in front of it
 * Use map_coord and world_coord instead of bitshifting with TILE_SHIFT
 * Declare and use variables in a more local scope
 * Take advantage of the fact that FUNCTIONALITY now is a struct by not casting anymore
Parts from patch by Freddie Witherden <evilguru> plus changes by me

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@2044 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2007-07-08 20:45:01 +00:00
parent b091d350e0
commit e531fb31fc
1 changed files with 79 additions and 185 deletions

View File

@ -1363,7 +1363,7 @@ SDWORD structureDamage(STRUCTURE *psStructure, UDWORD damage, UDWORD weaponClass
debug( LOG_ATTACK, " penetrated: %d\n", actualDamage); debug( LOG_ATTACK, " penetrated: %d\n", actualDamage);
} }
// If the shell did sufficient damage to destroy the structure // If the shell did sufficient damage to destroy the structure
if (actualDamage >= psStructure->body) if (actualDamage >= psStructure->body)
{ {
debug( LOG_ATTACK, " DESTROYED\n"); debug( LOG_ATTACK, " DESTROYED\n");
@ -2236,69 +2236,67 @@ STRUCTURE* buildStructure(STRUCTURE_STATS* pStructureType, UDWORD x, UDWORD y, U
BOOL setFunctionality(STRUCTURE *psBuilding, UDWORD functionType) BOOL setFunctionality(STRUCTURE *psBuilding, UDWORD functionType)
{ {
//SDWORD upgrade;
FACTORY *psFactory;
RESEARCH_FACILITY *psResFac;
POWER_GEN *psPowerGen;
REPAIR_FACILITY *psRepairFac;
REPAIR_DROID_FUNCTION *pFuncRepair;
REARM_PAD *psReArmPad;
UDWORD x, y;
CHECK_STRUCTURE(psBuilding); CHECK_STRUCTURE(psBuilding);
psBuilding->pFunctionality = NULL; switch (functionType)
switch(functionType)
{ {
case REF_FACTORY: case REF_FACTORY:
case REF_CYBORG_FACTORY: case REF_CYBORG_FACTORY:
case REF_VTOL_FACTORY: case REF_VTOL_FACTORY:
{ case REF_RESEARCH:
//this structure must have a function assigned to the stat case REF_POWER_GEN:
if (psBuilding->pStructureType->numFuncs == 0) case REF_RESOURCE_EXTRACTOR:
{ case REF_REPAIR_FACILITY:
debug( LOG_ERROR, "There must be a function assigned to this building - %s", getName( psBuilding->pStructureType->pName ) ); case REF_REARM_PAD:
abort(); // Allocate space for the buildings functionality
return FALSE; psBuilding->pFunctionality = calloc(1, sizeof(FUNCTIONALITY));
}
//allocate the necessary space
psBuilding->pFunctionality = malloc(sizeof(FUNCTIONALITY));
if (psBuilding->pFunctionality == NULL) if (psBuilding->pFunctionality == NULL)
{ {
debug(LOG_ERROR, "setFunctionality: Out of memory"); debug(LOG_ERROR, "setFunctionality: Out of memory");
abort(); abort();
return FALSE; return FALSE;
} }
break;
//initialise the memory default:
memset(psBuilding->pFunctionality, 0, sizeof(FUNCTIONALITY)); psBuilding->pFunctionality = NULL;
break;
}
psFactory = (FACTORY *)psBuilding->pFunctionality; switch (functionType)
psFactory->capacity = (UBYTE)((PRODUCTION_FUNCTION*)psBuilding-> {
pStructureType->asFuncList[0])->capacity; case REF_FACTORY:
psFactory->productionOutput = (UBYTE)((PRODUCTION_FUNCTION*)psBuilding-> case REF_CYBORG_FACTORY:
pStructureType->asFuncList[0])->productionOutput; case REF_VTOL_FACTORY:
{
FACTORY* psFactory = &psBuilding->pFunctionality->factory;
unsigned int x, y;
psFactory->capacity = (UBYTE) ((PRODUCTION_FUNCTION*)psBuilding->pStructureType->asFuncList[0])->capacity;
psFactory->productionOutput = (UBYTE) ((PRODUCTION_FUNCTION*)psBuilding->pStructureType->asFuncList[0])->productionOutput;
psFactory->psSubject = NULL; psFactory->psSubject = NULL;
//default the secondary order - AB 22/04/99
psFactory->secondaryOrder = DSS_ARANGE_DEFAULT | DSS_REPLEV_NEVER |
DSS_ALEV_ALWAYS | DSS_HALT_GUARD;
//add the assembly point to the factory // Default the secondary order - AB 22/04/99
psFactory->secondaryOrder = DSS_ARANGE_DEFAULT | DSS_REPLEV_NEVER
| DSS_ALEV_ALWAYS | DSS_HALT_GUARD;
// Create the assembly point for the factory
if (!createFlagPosition(&psFactory->psAssemblyPoint, psBuilding->player)) if (!createFlagPosition(&psFactory->psAssemblyPoint, psBuilding->player))
{ {
return FALSE; return FALSE;
} }
//initialise the assembly point position // initialise the assembly point position
x = (psBuilding->x+256) >> TILE_SHIFT; x = map_coord(psBuilding->x + 256);
y = (psBuilding->y+256) >> TILE_SHIFT; y = map_coord(psBuilding->y + 256);
// Belt and braces - shouldn't be able to build too near edge
setAssemblyPoint( psFactory->psAssemblyPoint, x << TILE_SHIFT, // Set the assembly point
y << TILE_SHIFT, psBuilding->player, TRUE); setAssemblyPoint(psFactory->psAssemblyPoint, world_coord(x), world_coord(y), psBuilding->player, TRUE);
// add it to the list
// Add the flag to the list
addFlagPosition(psFactory->psAssemblyPoint); addFlagPosition(psFactory->psAssemblyPoint);
switch(functionType) switch (functionType)
{ {
case REF_FACTORY: case REF_FACTORY:
setFlagPositionInc(psFactory, psBuilding->player, FACTORY_FLAG); setFlagPositionInc(psFactory, psBuilding->player, FACTORY_FLAG);
@ -2314,207 +2312,103 @@ BOOL setFunctionality(STRUCTURE *psBuilding, UDWORD functionType)
} }
psFactory->psFormation = NULL; psFactory->psFormation = NULL;
// Take advantage of upgrades
structureProductionUpgrade(psBuilding); structureProductionUpgrade(psBuilding);
break; break;
} }
case REF_RESEARCH: case REF_RESEARCH:
{ {
//this structure must have a function assigned to the stat RESEARCH_FACILITY* psResFac = &psBuilding->pFunctionality->researchFacility;
if (psBuilding->pStructureType->numFuncs == 0)
{
debug( LOG_ERROR, "There must be a function assigned to this building - %s", getName( psBuilding->pStructureType->pName ) );
abort();
return FALSE;
}
//try and create the Structure
psBuilding->pFunctionality = malloc(sizeof(FUNCTIONALITY));
if (psBuilding->pFunctionality == NULL)
{
debug(LOG_ERROR, "setFunctionality: Out of memory");
abort();
return FALSE;
}
//initialise the memory
memset(psBuilding->pFunctionality, 0, sizeof(FUNCTIONALITY));
psResFac = (RESEARCH_FACILITY *)psBuilding->pFunctionality; psResFac->researchPoints = ((RESEARCH_FUNCTION *) psBuilding->pStructureType->asFuncList[0])->researchPoints;
psResFac->researchPoints = ((RESEARCH_FUNCTION*)psBuilding->
pStructureType->asFuncList[0])->researchPoints; // Take advantage of upgrades
//check for upgrades - work backwards since want the most recent
structureResearchUpgrade(psBuilding); structureResearchUpgrade(psBuilding);
break; break;
} }
case REF_POWER_GEN: case REF_POWER_GEN:
{ {
//this structure must have a function assigned to the stat POWER_GEN* psPowerGen = &psBuilding->pFunctionality->powerGenerator;
if (psBuilding->pStructureType->numFuncs == 0)
{
debug( LOG_ERROR, "There must be a function assigned to this building - %s", getName( psBuilding->pStructureType->pName ) );
abort();
return FALSE;
}
//try and create the Structure psPowerGen->power = ((POWER_GEN_FUNCTION *) psBuilding->pStructureType->asFuncList[0])->powerOutput;
psBuilding->pFunctionality = malloc(sizeof(FUNCTIONALITY)); psPowerGen->multiplier = ((POWER_GEN_FUNCTION *) psBuilding->pStructureType->asFuncList[0])->powerMultiplier;
if (psBuilding->pFunctionality == NULL)
{
debug(LOG_ERROR, "setFunctionality: Out of memory");
abort();
return FALSE;
}
//initialise the memory
memset(psBuilding->pFunctionality, 0, sizeof(FUNCTIONALITY));
psPowerGen = (POWER_GEN *)psBuilding->pFunctionality;
psPowerGen->power = ((POWER_GEN_FUNCTION*)psBuilding->
pStructureType->asFuncList[0])->powerOutput;
psPowerGen->multiplier = ((POWER_GEN_FUNCTION*)psBuilding->
pStructureType->asFuncList[0])->powerMultiplier;
psPowerGen->capacity = 0; psPowerGen->capacity = 0;
//check for upgrades
// Take advantage of upgrades
structurePowerUpgrade(psBuilding); structurePowerUpgrade(psBuilding);
break; break;
} }
case REF_RESOURCE_EXTRACTOR: case REF_RESOURCE_EXTRACTOR:
{ {
//this structure must have a function assigned to the stat RES_EXTRACTOR* psResExtracter = &psBuilding->pFunctionality->resourceExtractor;
if (psBuilding->pStructureType->numFuncs == 0)
{
debug( LOG_ERROR, "There must be a function assigned to this building - %s", getName( psBuilding->pStructureType->pName ) );
abort();
return FALSE;
}
//try and create the Structure psResExtracter->power = ((RESOURCE_FUNCTION*)psBuilding->pStructureType->asFuncList[0])->maxPower;
psBuilding->pFunctionality = malloc(sizeof(FUNCTIONALITY));
if (psBuilding->pFunctionality == NULL)
{
debug(LOG_ERROR, "setFunctionality: Out of memory");
abort();
return FALSE;
}
//initialise the memory
memset(psBuilding->pFunctionality, 0, sizeof(FUNCTIONALITY));
((RES_EXTRACTOR*)psBuilding->pFunctionality)->power = (( // Make the structure inactive
//POWER_REG_FUNCTION*)psBuilding->pStructureType->asFuncList[0])-> psResExtracter->active = FALSE;
RESOURCE_FUNCTION*)psBuilding->pStructureType->asFuncList[0])-> psResExtracter->psPowerGen = NULL;
maxPower;
//set the structure to inactive
((RES_EXTRACTOR*)psBuilding->pFunctionality)->active = FALSE;
((RES_EXTRACTOR*)psBuilding->pFunctionality)->psPowerGen = NULL;
break; break;
} }
//this just checks that a function has been assigned
case REF_HQ: case REF_HQ:
{ {
// If an HQ has just been built make sure the radar is displayed!
radarOnScreen = TRUE; radarOnScreen = TRUE;
//this structure must have a function assigned to the stat
if (psBuilding->pStructureType->numFuncs == 0)
{
debug( LOG_ERROR, "There must be a function assigned to this building - %s", getName( psBuilding->pStructureType->pName ) );
abort();
return FALSE;
}
//this function is called once the structure has been built
break;
}
case REF_WALL:
{
//this structure must have a function assigned to the stat - this is just a check!
if (psBuilding->pStructureType->numFuncs == 0)
{
debug( LOG_ERROR, "There must be a function assigned to this building - %s", getName( psBuilding->pStructureType->pName ) );
abort();
return FALSE;
}
break; break;
} }
case REF_REPAIR_FACILITY: case REF_REPAIR_FACILITY:
{ {
//this structure must have a function assigned to the stat REPAIR_FACILITY* psRepairFac = &psBuilding->pFunctionality->repairFacility;
if (psBuilding->pStructureType->numFuncs == 0) REPAIR_DROID_FUNCTION* pFuncRepair = (REPAIR_DROID_FUNCTION*)psBuilding->pStructureType->asFuncList[0];
{ unsigned int x, y;
debug( LOG_ERROR, "There must be a function assigned to this building - %s", getName( psBuilding->pStructureType->pName ) );
abort();
return FALSE;
}
//try and create the Structure
psBuilding->pFunctionality = malloc(sizeof(FUNCTIONALITY));
if (psBuilding->pFunctionality == NULL)
{
debug(LOG_ERROR, "setFunctionality: Out of memory");
abort();
return FALSE;
}
//initialise the memory
memset(psBuilding->pFunctionality, 0, sizeof(FUNCTIONALITY));
psRepairFac = (REPAIR_FACILITY *) psBuilding->pFunctionality;
pFuncRepair = (REPAIR_DROID_FUNCTION *) psBuilding->pStructureType->asFuncList[0];
psRepairFac->power = pFuncRepair->repairPoints; psRepairFac->power = pFuncRepair->repairPoints;
psRepairFac->psObj = NULL; psRepairFac->psObj = NULL;
if ( !grpCreate(&((REPAIR_FACILITY *) psBuilding->pFunctionality)->psGroup) ) if (!grpCreate(&((REPAIR_FACILITY*)psBuilding->pFunctionality)->psGroup))
{ {
debug( LOG_NEVER, "setFunctionality: couldn't create repair facility group" ); debug( LOG_NEVER, "setFunctionality: couldn't create repair facility group" );
} }
else else
{ {
/* add null droid */ // Add NULL droid to the group
grpJoin( psRepairFac->psGroup, NULL ); grpJoin(psRepairFac->psGroup, NULL);
} }
//check for upgrades
// Take advantage of upgrades
structureRepairUpgrade(psBuilding); structureRepairUpgrade(psBuilding);
// add an assembly point // Create an assembly point for repaired droids
if (!createFlagPosition(&psRepairFac->psDeliveryPoint, psBuilding->player)) if (!createFlagPosition(&psRepairFac->psDeliveryPoint, psBuilding->player))
{ {
return FALSE; return FALSE;
} }
//initialise the assembly point position // Initialise the assembly point
x = (psBuilding->x+256) >> TILE_SHIFT; x = map_coord(psBuilding->x+256);
y = (psBuilding->y+256) >> TILE_SHIFT; y = map_coord(psBuilding->y+256);
// Belt and braces - shouldn't be able to build too near edge
setAssemblyPoint( psRepairFac->psDeliveryPoint, x << TILE_SHIFT,
y << TILE_SHIFT, psBuilding->player, TRUE);
// Set the assembly point
setAssemblyPoint(psRepairFac->psDeliveryPoint, world_coord(x),
world_coord(y), psBuilding->player, TRUE);
// Add the flag (triangular marker on the ground) at the delivery point
addFlagPosition(psRepairFac->psDeliveryPoint); addFlagPosition(psRepairFac->psDeliveryPoint);
setFlagPositionInc(psRepairFac, psBuilding->player, REPAIR_FLAG); setFlagPositionInc(psRepairFac, psBuilding->player, REPAIR_FLAG);
break; break;
} }
case REF_REARM_PAD: case REF_REARM_PAD:
{ {
//this structure must have a function assigned to the stat REARM_PAD* psReArmPad = &psBuilding->pFunctionality->rearmPad;
if (psBuilding->pStructureType->numFuncs == 0)
{
debug( LOG_ERROR, "There must be a function assigned to this building - %s", getName( psBuilding->pStructureType->pName ) );
abort();
return FALSE;
}
//try and create the Structure
psBuilding->pFunctionality = malloc(sizeof(FUNCTIONALITY));
if (psBuilding->pFunctionality == NULL)
{
debug(LOG_ERROR, "setFunctionality: Out of memory");
abort();
return FALSE;
}
//initialise the memory
memset(psBuilding->pFunctionality, 0, sizeof(FUNCTIONALITY));
psReArmPad = (REARM_PAD *)psBuilding->pFunctionality; psReArmPad->reArmPoints = ((REARM_PAD *)psBuilding->pStructureType->asFuncList[0])->reArmPoints;
psReArmPad->reArmPoints = ((REARM_PAD *)psBuilding->
pStructureType->asFuncList[0])->reArmPoints; // Take advantage of upgrades
//check for upgrades
structureReArmUpgrade(psBuilding); structureReArmUpgrade(psBuilding);
break; break;
} }
}//end of switch }
return TRUE; return TRUE;
} }