From 5ccf99a069697b7c325dde4d5d7ffdda4559db87 Mon Sep 17 00:00:00 2001 From: Lesh Date: Wed, 4 Oct 2006 16:24:46 +0000 Subject: [PATCH] - added sector variation (see "TableData\Map\AltSectors.xml") git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@591 3b4a5df2-a311-0410-b5c6-a8a6f20db521 --- Init.cpp | 6 ++ Strategic/Campaign Init.cpp | 178 +++++++++++++++++++++++++++++++++++- Tactical/XML.h | 5 +- 3 files changed, 187 insertions(+), 2 deletions(-) diff --git a/Init.cpp b/Init.cpp index 3ae9d6be..2edef9ad 100644 --- a/Init.cpp +++ b/Init.cpp @@ -356,6 +356,12 @@ BOOLEAN LoadExternalGameplayData(STR directoryName) // function BOOLEAN InitStrategicMovementCosts(); // It is called several times from various places and acts after clearing SectorInfo array + // Lesh: load altsectors list + strcpy(fileName, directoryName); + strcat(fileName, ALTSECTORSFILENAME); + if ( !ReadInAltSectors(fileName) ) + return FALSE; + strcpy(fileName, directoryName); strcat(fileName, EXPLOSIONDATAFILENAME); if(!ReadInExplosionDataStats(fileName)) diff --git a/Strategic/Campaign Init.cpp b/Strategic/Campaign Init.cpp index a4f8026e..4fa7d90d 100644 --- a/Strategic/Campaign Init.cpp +++ b/Strategic/Campaign Init.cpp @@ -1,5 +1,6 @@ #ifdef PRECOMPILEDHEADERS #include "Strategic All.h" + #include "XML.h" #else #include "types.h" #include "Campaign Init.h" @@ -12,6 +13,7 @@ #include "GameSettings.h" #include "Creature Spreading.h" #include "Strategic AI.h" + #include "XML.h" #endif extern BOOLEAN InitStrategicMovementCosts(); @@ -45,6 +47,180 @@ BOOLEAN RandomSector[256] = /* O */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* P */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +typedef enum +{ + ALTSECTORS_ELEMENT_NONE = 0, + ALTSECTORS_ELEMENT_LIST, + ALTSECTORS_ELEMENT_ROW, +} ALTSECTORS_PARSE_STAGE; + +typedef struct +{ + ALTSECTORS_PARSE_STAGE curElement; + INT8 szCharData[MAX_CHAR_DATA_LENGTH+1]; + UINT32 uiRowNumber; + + UINT32 currentDepth; + UINT32 maxReadDepth; +} altSectorsParseData; + +static void XMLCALL +altSectorsStartElementHandle(void *userData, const char *name, const char **atts) +{ + altSectorsParseData * pData = (altSectorsParseData *) userData; + + if(pData->currentDepth <= pData->maxReadDepth) //are we reading this element? + { + if(strcmp(name, "ALT_SECTORS_LIST") == 0 && pData->curElement == ALTSECTORS_ELEMENT_NONE) + { + pData->curElement = ALTSECTORS_ELEMENT_LIST; + pData->maxReadDepth++; //we are not skipping this element + } + else if(strcmp(name, "ROW") == 0 && pData->curElement == ALTSECTORS_ELEMENT_LIST) + { + pData->curElement = ALTSECTORS_ELEMENT_ROW; + + if(strcmp(atts[0], "y") == 0) + { + if ( isalpha(atts[1][0]) ) + pData->uiRowNumber = (UINT32)(atts[1][0] - 0x40); + else + pData->uiRowNumber = atol(atts[1]); + } + pData->maxReadDepth++; //we are not skipping this element + } + pData->szCharData[0] = '\0'; + } + + pData->currentDepth++; +} + +static void XMLCALL +altSectorsCharacterDataHandle(void *userData, const char *str, int len) +{ + altSectorsParseData * pData = (altSectorsParseData *) userData; + + if(pData->currentDepth <= pData->maxReadDepth && strlen(pData->szCharData) < MAX_CHAR_DATA_LENGTH) + strncat(pData->szCharData,str,__min((unsigned int)len,MAX_CHAR_DATA_LENGTH-strlen(pData->szCharData))); +} + + +static void XMLCALL +altSectorsEndElementHandle(void *userData, const char *name) +{ + altSectorsParseData * pData = (altSectorsParseData *) userData; + + if(pData->currentDepth <= pData->maxReadDepth) //we're at the end of an element that we've been reading + { + if(strcmp(name, "ALT_SECTORS_LIST") == 0 && pData->curElement == ALTSECTORS_ELEMENT_LIST) + { + pData->curElement = ALTSECTORS_ELEMENT_NONE; + } + else if(strcmp(name, "ROW") == 0 && pData->curElement == ALTSECTORS_ELEMENT_ROW) + { + INT8 * curBuffer = pData->szCharData + strspn((const char *)pData->szCharData," \t\n\r"); + UINT32 curCellIndex = 0; + UINT32 curNumber; + + pData->curElement = ALTSECTORS_ELEMENT_LIST; + + while(curBuffer[0] != '\0') + { + sscanf( (const char *)curBuffer,"%d",&curNumber); + + RandomSector[curCellIndex+16*(pData->uiRowNumber - 1)] = curNumber; + + curCellIndex++; + curBuffer += strcspn(curBuffer," \t\n\r\0"); + curBuffer += strspn((const char *)curBuffer," \t\n\r"); + } + } + pData->maxReadDepth--; + } + + pData->currentDepth--; +} + + +BOOLEAN ReadInAltSectors(STR fileName) +{ + HWFILE hFile; + UINT32 uiBytesRead; + UINT32 uiFSize; + CHAR8 * lpcBuffer; + XML_Parser parser = XML_ParserCreate(NULL); + + altSectorsParseData pData; + + // Open xml file + hFile = FileOpen( fileName, FILE_ACCESS_READ, FALSE ); + if ( !hFile ) + return( FALSE ); + + uiFSize = FileGetSize(hFile); + lpcBuffer = (CHAR8 *) MemAlloc(uiFSize+1); + + //Read in block + if ( !FileRead( hFile, lpcBuffer, uiFSize, &uiBytesRead ) ) + { + MemFree(lpcBuffer); + return( FALSE ); + } + + lpcBuffer[uiFSize] = 0; //add a null terminator + + FileClose( hFile ); + + + XML_SetElementHandler(parser, altSectorsStartElementHandle, altSectorsEndElementHandle); + XML_SetCharacterDataHandler(parser, altSectorsCharacterDataHandle); + + + memset(&pData,0,sizeof(pData)); + XML_SetUserData(parser, &pData); + + + if(!XML_Parse(parser, lpcBuffer, uiFSize, TRUE)) + { + CHAR8 errorBuf[511]; + + sprintf(errorBuf, "XML Parser Error in AltSectors.xml: %s at line %d", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser)); + LiveMessage(errorBuf); + + MemFree(lpcBuffer); + return FALSE; + } + + MemFree(lpcBuffer); + + XML_ParserFree(parser); + + return( TRUE ); +} + +BOOLEAN WriteInAltSectors(STR fileName) +{ + // Lets output the current Strategic map format using the XML structure I've devised. + FILE *outfile = fopen(fileName, "wt"); + + UINT32 x, y; + + fprintf (outfile, "\n"); + for(y = 1;y <= 16;y++) + { + fprintf (outfile, "\t",(y+0x40)); + for(x = 1;x <= 16; x++) + { + fprintf (outfile, " %d",RandomSector[ ((y - 1) * 16) + (x - 1) ] ); + } + fprintf (outfile, " \n"); + } + fprintf (outfile, "\n"); + + fclose(outfile); + + return (TRUE); +} UNDERGROUND_SECTORINFO* NewUndergroundNode( UINT8 ubSectorX, UINT8 ubSectorY, UINT8 ubSectorZ ) { @@ -398,7 +574,7 @@ void InitNewCampaign() InitStrategicMovementCosts(); RemoveAllGroups(); - InitWorld(); + InitWorld(); // Lesh: generate different world each time using alternative maps InitMiningLocations(); InitKnowFacilitiesFlags( ); diff --git a/Tactical/XML.h b/Tactical/XML.h index 6b1e5593..66da43d7 100644 --- a/Tactical/XML.h +++ b/Tactical/XML.h @@ -67,6 +67,7 @@ typedef PARSE_STAGE; #define EXPLOSIONDATAFILENAME "ExplosionData.xml" #define CITYTABLEFILENAME "Map\\Cities.xml" #define MOVEMENTCOSTFILENAME "Map\\MovementCosts.xml" +#define ALTSECTORSFILENAME "Map\\AltSectors.xml" // WANNE: drops filename #define ENEMYWEAPONDROPSFILENAME "EnemyWeaponDrops.xml" @@ -151,8 +152,10 @@ extern BOOLEAN WriteBurstSoundArray(); extern BOOLEAN ReadInExplosionDataStats(STR fileName); extern BOOLEAN WriteExplosionDataStats(); -//Lesh: strategic movement costs +//Lesh: strategic movement costs and alternative sectors table extern BOOLEAN ReadInStrategicMovementCosts(STR fileName); extern BOOLEAN WriteInStrategicMovementCosts(STR fileName); +extern BOOLEAN ReadInAltSectors(STR fileName); +extern BOOLEAN WriteInAltSectors(STR fileName); #endif \ No newline at end of file