diff --git a/src/save.c b/src/save.c index f6ad26ff7..90e22525a 100644 --- a/src/save.c +++ b/src/save.c @@ -440,9 +440,11 @@ fin: void *build_save_OPS(int *size, int orig_x0, int orig_y0, int orig_w, int orig_h, unsigned char bmap[YRES/CELL][XRES/CELL], float vx[YRES/CELL][XRES/CELL], float vy[YRES/CELL][XRES/CELL], float pv[YRES/CELL][XRES/CELL], float fvx[YRES/CELL][XRES/CELL], float fvy[YRES/CELL][XRES/CELL], sign signs[MAXSIGNS], void* o_partsptr) { particle *partsptr = o_partsptr; - unsigned char *partsData = NULL, *partsPosData = NULL, *fanData = NULL, *wallData = NULL, *finalData = NULL, *outputData = NULL; + unsigned char *partsData = NULL, *partsPosData = NULL, *fanData = NULL, *wallData = NULL, *finalData = NULL, *outputData = NULL, *soapLinkData = NULL; unsigned *partsPosLink = NULL, *partsPosFirstMap = NULL, *partsPosCount = NULL, *partsPosLastMap = NULL; - int partsDataLen, partsPosDataLen, fanDataLen, wallDataLen, finalDataLen, outputDataLen; + unsigned partsCount = 0, *partsSaveIndex = NULL; + unsigned *elementCount = calloc(PT_NUM, sizeof(unsigned)); + int partsDataLen, partsPosDataLen, fanDataLen, wallDataLen, finalDataLen, outputDataLen, soapLinkDataLen; int blockX, blockY, blockW, blockH, fullX, fullY, fullW, fullH; int x, y, i, wallDataFound = 0; int posCount, signsCount; @@ -557,6 +559,8 @@ void *build_save_OPS(int *size, int orig_x0, int orig_y0, int orig_w, int orig_h */ partsData = malloc(NPART * (sizeof(particle)+1)); partsDataLen = 0; + partsSaveIndex = calloc(NPART, sizeof(unsigned)); + partsCount = 0; for (y=0;y>8; + //Store saved particle index+1 for this partsptr index (0 means not saved) + partsSaveIndex[i] = (partsCount++) + 1; + //Type (required) partsData[partsDataLen++] = partsptr[i].type; + elementCount[partsptr[i].type]++; //Location of the field descriptor fieldDescLoc = partsDataLen++; @@ -679,6 +687,47 @@ void *build_save_OPS(int *size, int orig_x0, int orig_y0, int orig_w, int orig_h } } } + + soapLinkData = malloc(3*elementCount[PT_SOAP]); + soapLinkDataLen = 0; + //Iterate through particles in the same order that they were saved + for (y=0;y>8; + + if (partsptr[i].type==PT_SOAP) + { + //Only save forward link for each particle, back links can be deduced from other forward links + //linkedIndex is index within saved particles + 1, 0 means not saved or no link + unsigned linkedIndex = 0; + if ((partsptr[i].ctype&2) && partsptr[i].tmp>=0 && partsptr[i].tmp>16; + soapLinkData[soapLinkDataLen++] = (linkedIndex&0x00FF00)>>8; + soapLinkData[soapLinkDataLen++] = (linkedIndex&0x0000FF); + } + + //Get the pmap entry for the next particle in the same position + i = partsPosLink[i]; + } + } + } + if(!soapLinkDataLen) + { + free(soapLinkData); + soapLinkData = NULL; + } if(!partsDataLen) { free(partsData); @@ -704,6 +753,8 @@ void *build_save_OPS(int *size, int orig_x0, int orig_y0, int orig_w, int orig_h bson_append_binary(&b, "wallMap", BSON_BIN_USER, wallData, wallDataLen); if(fanData) bson_append_binary(&b, "fanMap", BSON_BIN_USER, fanData, fanDataLen); + if(soapLinkData) + bson_append_binary(&b, "soapLinks", BSON_BIN_USER, soapLinkData, soapLinkDataLen); signsCount = 0; for(i = 0; i < MAXSIGNS; i++) { @@ -770,6 +821,12 @@ fin: free(wallData); if(fanData) free(fanData); + if (elementCount) + free(elementCount); + if (partsSaveIndex) + free(partsSaveIndex); + if (soapLinkData) + free(soapLinkData); return outputData; } @@ -777,8 +834,9 @@ fin: int parse_save_OPS(void *save, int size, int replace, int x0, int y0, unsigned char bmap[YRES/CELL][XRES/CELL], float vx[YRES/CELL][XRES/CELL], float vy[YRES/CELL][XRES/CELL], float pv[YRES/CELL][XRES/CELL], float fvx[YRES/CELL][XRES/CELL], float fvy[YRES/CELL][XRES/CELL], sign signs[MAXSIGNS], void* o_partsptr, unsigned pmap[YRES][XRES]) { particle *partsptr = o_partsptr; - unsigned char * inputData = save, *bsonData = NULL, *partsData = NULL, *partsPosData = NULL, *fanData = NULL, *wallData = NULL; - int inputDataLen = size, bsonDataLen = 0, partsDataLen, partsPosDataLen, fanDataLen, wallDataLen; + unsigned char * inputData = save, *bsonData = NULL, *partsData = NULL, *partsPosData = NULL, *fanData = NULL, *wallData = NULL, *soapLinkData = NULL; + int inputDataLen = size, bsonDataLen = 0, partsDataLen, partsPosDataLen, fanDataLen, wallDataLen, soapLinkDataLen; + unsigned partsCount = 0, *partsSimIndex = NULL; int i, freeIndicesCount, x, y, returnCode = 0, j; int *freeIndices = NULL; int blockX, blockY, blockW, blockH, fullX, fullY, fullW, fullH; @@ -951,6 +1009,17 @@ int parse_save_OPS(void *save, int size, int replace, int x0, int y0, unsigned c fprintf(stderr, "Invalid datatype of fan data: %d[%d] %d[%d] %d[%d]\n", bson_iterator_type(&iter), bson_iterator_type(&iter)==BSON_BINDATA, (unsigned char)bson_iterator_bin_type(&iter), ((unsigned char)bson_iterator_bin_type(&iter))==BSON_BIN_USER, bson_iterator_bin_len(&iter), bson_iterator_bin_len(&iter)>0); } } + else if(strcmp(bson_iterator_key(&iter), "soapLinks")==0) + { + if(bson_iterator_type(&iter)==BSON_BINDATA && ((unsigned char)bson_iterator_bin_type(&iter))==BSON_BIN_USER && (soapLinkDataLen = bson_iterator_bin_len(&iter)) > 0) + { + soapLinkData = bson_iterator_bin_data(&iter); + } + else + { + fprintf(stderr, "Invalid datatype of soap data: %d[%d] %d[%d] %d[%d]\n", bson_iterator_type(&iter), bson_iterator_type(&iter)==BSON_BINDATA, (unsigned char)bson_iterator_bin_type(&iter), ((unsigned char)bson_iterator_bin_type(&iter))==BSON_BIN_USER, bson_iterator_bin_len(&iter), bson_iterator_bin_len(&iter)>0); + } + } else if(strcmp(bson_iterator_key(&iter), "legacyEnable")==0 && replace) { if(bson_iterator_type(&iter)==BSON_BOOL) @@ -1102,6 +1171,8 @@ int parse_save_OPS(void *save, int size, int replace, int x0, int y0, unsigned c parts_lastActiveIndex = NPART-1; freeIndicesCount = 0; freeIndices = calloc(sizeof(int), NPART); + partsSimIndex = calloc(NPART, sizeof(unsigned)); + partsCount = 0; for (i = 0; i= NPART) goto fail; - + + //Store partsptr index+1 for this saved particle index (0 means not loaded) + partsSimIndex[partsCount++] = newIndex+1; + //Clear the particle, ready for our new properties memset(&(partsptr[newIndex]), 0, sizeof(particle)); @@ -1286,13 +1360,41 @@ int parse_save_OPS(void *save, int size, int replace, int x0, int y0, unsigned c } else if (partsptr[newIndex].type == PT_SOAP) { - partsptr[newIndex].ctype = 0; + //Clear soap links, links will be added back in if soapLinkData is present + partsptr[newIndex].ctype &= ~6; } if (!ptypes[partsptr[newIndex].type].enabled) partsptr[newIndex].type = PT_NONE; } } } + if (soapLinkData) + { + int soapLinkDataPos = 0; + for (i=0; i soapLinkDataLen) break; + linkedIndex |= soapLinkData[soapLinkDataPos++]<<16; + linkedIndex |= soapLinkData[soapLinkDataPos++]<<8; + linkedIndex |= soapLinkData[soapLinkDataPos++]; + // All indexes in soapLinkData and partsSimIndex have 1 added to them (0 means not saved/loaded) + if (!linkedIndex || linkedIndex-1>=partsCount || !partsSimIndex[linkedIndex-1]) + continue; + linkedIndex = partsSimIndex[linkedIndex-1]-1; + newIndex = partsSimIndex[i]-1; + + //Attach the two particles + partsptr[newIndex].ctype |= 2; + partsptr[newIndex].tmp = linkedIndex; + partsptr[linkedIndex].ctype |= 4; + partsptr[linkedIndex].tmp2 = newIndex; + } + } + } } goto fin; fail: @@ -1302,6 +1404,8 @@ fin: bson_destroy(&b); if(freeIndices) free(freeIndices); + if(partsSimIndex) + free(partsSimIndex); return returnCode; }