bool loadFunctionStats(const char *pFunctionData, UDWORD bufferSize) { //array of functions pointers for each load function static const LoadFunction pLoadFunction[NUMFUNCTIONS] = { loadProduction, loadProductionUpgradeFunction, loadResearchFunction, loadResearchUpgradeFunction, loadPowerGenFunction, loadResourceFunction, loadRepairDroidFunction, loadWeaponUpgradeFunction, loadWallFunction, loadStructureUpgradeFunction, loadWallDefenceUpgradeFunction, loadPowerUpgradeFunction, loadRepairUpgradeFunction, loadDroidRepairUpgradeFunction, loadDroidECMUpgradeFunction, loadDroidBodyUpgradeFunction, loadDroidSensorUpgradeFunction, loadDroidConstUpgradeFunction, loadReArmFunction, loadReArmUpgradeFunction, }; const unsigned int totalFunctions = numCR(pFunctionData, bufferSize); UDWORD i, type; char FunctionType[MAX_STR_LENGTH]; FUNCTION **pStartList; //allocate storage for the Function pointer array asFunctions = (FUNCTION **) malloc(totalFunctions * sizeof(FUNCTION *)); pStartList = asFunctions; //initialise the storage memset(asFunctions, 0, totalFunctions * sizeof(FUNCTION *)); numFunctions = 0; for (i = 0; i < totalFunctions; i++) { //read the data into the storage - the data is delimeted using comma's FunctionType[0] = '\0'; sscanf(pFunctionData, "%255[^,'\r\n]", FunctionType); type = functionType(FunctionType); pFunctionData += (strlen(FunctionType) + 1); if (!(pLoadFunction[type](pFunctionData))) { return false; } //increment the pointer to the start of the next record pFunctionData = strchr(pFunctionData, '\n') + 1; } //set the function list pointer to the start asFunctions = pStartList; return true; }
/* Load the feature stats */ BOOL loadFeatureStats(const char *pFeatureData, UDWORD bufferSize) { FEATURE_STATS *psFeature; unsigned int i; char featureName[MAX_STR_LENGTH], GfxFile[MAX_STR_LENGTH], type[MAX_STR_LENGTH]; numFeatureStats = numCR(pFeatureData, bufferSize); // Skip descriptive header if (strncmp(pFeatureData,"Feature ",8)==0) { pFeatureData = strchr(pFeatureData,'\n') + 1; numFeatureStats--; } asFeatureStats = (FEATURE_STATS*)malloc(sizeof(FEATURE_STATS) * numFeatureStats); if (asFeatureStats == NULL) { debug( LOG_FATAL, "Feature Stats - Out of memory" ); abort(); return false; } psFeature = asFeatureStats; for (i = 0; i < numFeatureStats; i++) { UDWORD Width, Breadth; int damageable = 0, tileDraw = 0, allowLOS = 0, visibleAtStart = 0; memset(psFeature, 0, sizeof(FEATURE_STATS)); featureName[0] = '\0'; GfxFile[0] = '\0'; type[0] = '\0'; //read the data into the storage - the data is delimeted using comma's sscanf(pFeatureData, "%[^','],%d,%d,%d,%d,%d,%[^','],%[^','],%d,%d,%d", featureName, &Width, &Breadth, &damageable, &psFeature->armourValue, &psFeature->body, GfxFile, type, &tileDraw, &allowLOS, &visibleAtStart); psFeature->damageable = damageable; psFeature->tileDraw = tileDraw; psFeature->allowLOS = allowLOS; psFeature->visibleAtStart = visibleAtStart; // These are now only 16 bits wide - so we need to copy them psFeature->baseWidth = Width; psFeature->baseBreadth = Breadth; psFeature->pName = allocateName(featureName); if (!psFeature->pName) { return false; } if (psFeature->damageable && psFeature->body == 0) { debug(LOG_ERROR, "The feature %s, ref %d, is damageable, but has no body points! The files need to be updated / fixed. " \ "Assigning 1 body point to feature.", psFeature->pName, psFeature->ref); psFeature->body = 1; } //determine the feature type featureType(psFeature, type); //and the oil resource - assumes only one! if (psFeature->subType == FEAT_OIL_RESOURCE) { oilResFeature = psFeature; } //get the IMD for the feature psFeature->psImd = (iIMDShape *) resGetData("IMD", GfxFile); if (psFeature->psImd == NULL) { debug( LOG_ERROR, "Cannot find the feature PIE for record %s", getName( psFeature->pName ) ); return false; } psFeature->ref = REF_FEATURE_START + i; //increment the pointer to the start of the next record pFeatureData = strchr(pFeatureData,'\n') + 1; //increment the list to the start of the next storage block psFeature++; } return true; }