style cleanup
[blender.git] / source / blender / blenkernel / intern / pointcache.c
index c5f948bb27fa28744099879f3dae22f159a2e8ba..9c3c1b0e508ce0a00136e798a9e4009b0db2b9b5 100644 (file)
 #include "BKE_scene.h"
 #include "BKE_smoke.h"
 #include "BKE_softbody.h"
-#include "BKE_utildefines.h"
 
 #include "BIK_api.h"
 
 /* both in intern */
+#ifdef WITH_SMOKE
 #include "smoke_API.h"
+#endif
 
 #ifdef WITH_LZO
 #include "minilzo.h"
 /* needed for directory lookup */
 /* untitled blend's need getpid for a unique name */
 #ifndef WIN32
-  #include <dirent.h>
-#include <unistd.h>
+#  include <dirent.h>
+#  include <unistd.h>
 #else
-#include <process.h>
-  #include "BLI_winstuff.h"
+#  include <process.h>
+#  include "BLI_winstuff.h"
 #endif
 
-#define PTCACHE_DATA_FROM(data, type, from)            if(data[type]) { memcpy(data[type], from, ptcache_data_size[type]); }
-#define PTCACHE_DATA_TO(data, type, index, to) if(data[type]) { memcpy(to, (char*)data[type] + (index ? index * ptcache_data_size[type] : 0), ptcache_data_size[type]); }
+#define PTCACHE_DATA_FROM(data, type, from)  \
+       if (data[type]) { \
+               memcpy(data[type], from, ptcache_data_size[type]); \
+       } (void)0
+
+#define PTCACHE_DATA_TO(data, type, index, to)  \
+       if (data[type]) { \
+               memcpy(to, (char *)(data)[type] + ((index) ? (index) * ptcache_data_size[type] : 0), ptcache_data_size[type]); \
+       } (void)0
 
 /* could be made into a pointcache option */
 #define DURIAN_POINTCACHE_LIB_OK 1
@@ -131,10 +139,10 @@ static int ptcache_basic_header_read(PTCacheFile *pf)
        int error=0;
 
        /* Custom functions should read these basic elements too! */
-       if(!error && !fread(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
+       if (!error && !fread(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
                error = 1;
        
-       if(!error && !fread(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
+       if (!error && !fread(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
                error = 1;
 
        return !error;
@@ -142,10 +150,10 @@ static int ptcache_basic_header_read(PTCacheFile *pf)
 static int ptcache_basic_header_write(PTCacheFile *pf)
 {
        /* Custom functions should write these basic elements too! */
-       if(!fwrite(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
+       if (!fwrite(&pf->totpoint, sizeof(unsigned int), 1, pf->fp))
                return 0;
        
-       if(!fwrite(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
+       if (!fwrite(&pf->data_types, sizeof(unsigned int), 1, pf->fp))
                return 0;
 
        return 1;
@@ -166,7 +174,7 @@ static void ptcache_softbody_read(int index, void *soft_v, void **data, float UN
        SoftBody *soft= soft_v;
        BodyPoint *bp = soft->bpoint + index;
 
-       if(old_data) {
+       if (old_data) {
                memcpy(bp->pos, data, 3 * sizeof(float));
                memcpy(bp->vec, data + 3, 3 * sizeof(float));
        }
@@ -182,13 +190,13 @@ static void ptcache_softbody_interpolate(int index, void *soft_v, void **data, f
        ParticleKey keys[4];
        float dfra;
 
-       if(cfra1 == cfra2)
+       if (cfra1 == cfra2)
                return;
 
        copy_v3_v3(keys[1].co, bp->pos);
        copy_v3_v3(keys[1].vel, bp->vec);
 
-       if(old_data) {
+       if (old_data) {
                memcpy(keys[2].co, old_data, 3 * sizeof(float));
                memcpy(keys[2].vel, old_data + 3, 3 * sizeof(float));
        }
@@ -219,8 +227,8 @@ void BKE_ptcache_make_particle_key(ParticleKey *key, int index, void **data, flo
        PTCACHE_DATA_TO(data, BPHYS_DATA_VELOCITY, index, key->vel);
        
        /* no rotation info, so make something nice up */
-       if(data[BPHYS_DATA_ROTATION]==NULL) {
-               vec_to_quat( key->rot, key->vel, OB_NEGX, OB_POSZ);
+       if (data[BPHYS_DATA_ROTATION]==NULL) {
+               vec_to_quat(key->rot, key->vel, OB_NEGX, OB_POSZ);
        }
        else {
                PTCACHE_DATA_TO(data, BPHYS_DATA_ROTATION, index, key->rot);
@@ -238,12 +246,12 @@ static int  ptcache_particle_write(int index, void *psys_v, void **data, int cfr
        int step = psys->pointcache->step;
 
        /* No need to store unborn or died particles outside cache step bounds */
-       if(data[BPHYS_DATA_INDEX] && (cfra < pa->time - step || cfra > pa->dietime + step))
+       if (data[BPHYS_DATA_INDEX] && (cfra < pa->time - step || cfra > pa->dietime + step))
                return 0;
 
-       times[0]= pa->time;
-       times[1]= pa->dietime;
-       times[2]= pa->lifetime;
+       times[0] = pa->time;
+       times[1] = pa->dietime;
+       times[2] = pa->lifetime;
 
        PTCACHE_DATA_FROM(data, BPHYS_DATA_INDEX, &index);
        PTCACHE_DATA_FROM(data, BPHYS_DATA_LOCATION, pa->state.co);
@@ -253,7 +261,7 @@ static int  ptcache_particle_write(int index, void *psys_v, void **data, int cfr
        PTCACHE_DATA_FROM(data, BPHYS_DATA_SIZE, &pa->size);
        PTCACHE_DATA_FROM(data, BPHYS_DATA_TIMES, times);
 
-       if(boid)
+       if (boid)
                PTCACHE_DATA_FROM(data, BPHYS_DATA_BOIDS, &boid->data);
 
        /* return flag 1+1=2 for newly born particles to copy exact birth location to previously cached frame */
@@ -266,16 +274,16 @@ static void ptcache_particle_read(int index, void *psys_v, void **data, float cf
        BoidParticle *boid;
        float timestep = 0.04f*psys->part->timetweak;
 
-       if(index >= psys->totpart)
+       if (index >= psys->totpart)
                return;
 
        pa = psys->particles + index;
        boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : NULL;
 
-       if(cfra > pa->state.time)
+       if (cfra > pa->state.time)
                memcpy(&pa->prev_state, &pa->state, sizeof(ParticleKey));
 
-       if(old_data){
+       if (old_data) {
                /* old format cache */
                memcpy(&pa->state, old_data, sizeof(ParticleKey));
                return;
@@ -284,15 +292,15 @@ static void ptcache_particle_read(int index, void *psys_v, void **data, float cf
        BKE_ptcache_make_particle_key(&pa->state, 0, data, cfra);
 
        /* set frames cached before birth to birth time */
-       if(cfra < pa->time)
+       if (cfra < pa->time)
                pa->state.time = pa->time;
-       else if(cfra > pa->dietime)
+       else if (cfra > pa->dietime)
                pa->state.time = pa->dietime;
 
-       if(data[BPHYS_DATA_SIZE])
+       if (data[BPHYS_DATA_SIZE])
                PTCACHE_DATA_TO(data, BPHYS_DATA_SIZE, 0, &pa->size);
        
-       if(data[BPHYS_DATA_TIMES]) {
+       if (data[BPHYS_DATA_TIMES]) {
                float times[3];
                PTCACHE_DATA_TO(data, BPHYS_DATA_TIMES, 0, &times);
                pa->time = times[0];
@@ -300,12 +308,12 @@ static void ptcache_particle_read(int index, void *psys_v, void **data, float cf
                pa->lifetime = times[2];
        }
 
-       if(boid)
+       if (boid)
                PTCACHE_DATA_TO(data, BPHYS_DATA_BOIDS, 0, &boid->data);
 
        /* determine velocity from previous location */
-       if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
-               if(cfra > pa->prev_state.time) {
+       if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
+               if (cfra > pa->prev_state.time) {
                        sub_v3_v3v3(pa->state.vel, pa->state.co, pa->prev_state.co);
                        mul_v3_fl(pa->state.vel, (cfra - pa->prev_state.time) * timestep);
                }
@@ -315,9 +323,9 @@ static void ptcache_particle_read(int index, void *psys_v, void **data, float cf
                }
        }
 
-       /* determine rotation from velocity */
-       if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
-               vec_to_quat( pa->state.rot,pa->state.vel, OB_NEGX, OB_POSZ);
+       /* default to no rotation */
+       if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
+               unit_qt(pa->state.rot);
        }
 }
 static void ptcache_particle_interpolate(int index, void *psys_v, void **data, float cfra, float cfra1, float cfra2, float *old_data)
@@ -327,31 +335,31 @@ static void ptcache_particle_interpolate(int index, void *psys_v, void **data, f
        ParticleKey keys[4];
        float dfra, timestep = 0.04f*psys->part->timetweak;
 
-       if(index >= psys->totpart)
+       if (index >= psys->totpart)
                return;
 
        pa = psys->particles + index;
 
        /* particle wasn't read from first cache so can't interpolate */
-       if((int)cfra1 < pa->time - psys->pointcache->step || (int)cfra1 > pa->dietime + psys->pointcache->step)
+       if ((int)cfra1 < pa->time - psys->pointcache->step || (int)cfra1 > pa->dietime + psys->pointcache->step)
                return;
 
        cfra = MIN2(cfra, pa->dietime);
        cfra1 = MIN2(cfra1, pa->dietime);
        cfra2 = MIN2(cfra2, pa->dietime);
 
-       if(cfra1 == cfra2)
+       if (cfra1 == cfra2)
                return;
 
        memcpy(keys+1, &pa->state, sizeof(ParticleKey));
-       if(old_data)
+       if (old_data)
                memcpy(keys+2, old_data, sizeof(ParticleKey));
        else
                BKE_ptcache_make_particle_key(keys+2, 0, data, cfra2);
 
        /* determine velocity from previous location */
-       if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
-               if(keys[1].time > keys[2].time) {
+       if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_VELOCITY]) {
+               if (keys[1].time > keys[2].time) {
                        sub_v3_v3v3(keys[2].vel, keys[1].co, keys[2].co);
                        mul_v3_fl(keys[2].vel, (keys[1].time - keys[2].time) * timestep);
                }
@@ -362,11 +370,11 @@ static void ptcache_particle_interpolate(int index, void *psys_v, void **data, f
        }
 
        /* determine rotation from velocity */
-       if(data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
-               vec_to_quat( keys[2].rot,keys[2].vel, OB_NEGX, OB_POSZ);
+       if (data[BPHYS_DATA_LOCATION] && !data[BPHYS_DATA_ROTATION]) {
+               vec_to_quat(keys[2].rot, keys[2].vel, OB_NEGX, OB_POSZ);
        }
 
-       if(cfra > pa->time)
+       if (cfra > pa->time)
                cfra1 = MAX2(cfra1, pa->time);
 
        dfra = cfra2 - cfra1;
@@ -394,10 +402,10 @@ static int  ptcache_particle_totwrite(void *psys_v, int cfra)
        int p, step = psys->pointcache->step;
        int totwrite = 0;
 
-       if(cfra == 0)
+       if (cfra == 0)
                return psys->totpart;
 
-       for(p=0; p<psys->totpart; p++,pa++)
+       for (p=0; p<psys->totpart; p++, pa++)
                totwrite += (cfra >= pa->time - step && cfra <= pa->dietime + step);
 
        return totwrite;
@@ -408,7 +416,7 @@ static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int UNUSE
        ParticleSystem *psys = psys_v;
        PTCacheExtra *extra = NULL;
 
-       if(psys->part->phystype == PART_PHYS_FLUID &&
+       if (psys->part->phystype == PART_PHYS_FLUID &&
                psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS &&
                psys->tot_fluidsprings && psys->fluid_springs) {
 
@@ -429,11 +437,11 @@ static void ptcache_particle_extra_read(void *psys_v, PTCacheMem *pm, float UNUS
        ParticleSystem *psys = psys_v;
        PTCacheExtra *extra = pm->extradata.first;
 
-       for(; extra; extra=extra->next) {
-               switch(extra->type) {
+       for (; extra; extra=extra->next) {
+               switch (extra->type) {
                        case BPHYS_EXTRA_FLUID_SPRINGS:
                        {
-                               if(psys->fluid_springs)
+                               if (psys->fluid_springs)
                                        MEM_freeN(psys->fluid_springs);
 
                                psys->fluid_springs = MEM_dupallocN(extra->data);
@@ -463,7 +471,7 @@ static void ptcache_cloth_read(int index, void *cloth_v, void **data, float UNUS
        Cloth *cloth= clmd->clothObject;
        ClothVertex *vert = cloth->verts + index;
        
-       if(old_data) {
+       if (old_data) {
                memcpy(vert->x, data, 3 * sizeof(float));
                memcpy(vert->xconst, data + 3, 3 * sizeof(float));
                memcpy(vert->v, data + 6, 3 * sizeof(float));
@@ -482,13 +490,13 @@ static void ptcache_cloth_interpolate(int index, void *cloth_v, void **data, flo
        ParticleKey keys[4];
        float dfra;
 
-       if(cfra1 == cfra2)
+       if (cfra1 == cfra2)
                return;
 
        copy_v3_v3(keys[1].co, vert->x);
        copy_v3_v3(keys[1].vel, vert->v);
 
-       if(old_data) {
+       if (old_data) {
                memcpy(keys[2].co, old_data, 3 * sizeof(float));
                memcpy(keys[2].vel, old_data + 6, 3 * sizeof(float));
        }
@@ -523,21 +531,32 @@ static int  ptcache_smoke_totpoint(void *smoke_v, int UNUSED(cfra))
        SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
        SmokeDomainSettings *sds = smd->domain;
        
-       if(sds->fluid) {
-               return sds->res[0]*sds->res[1]*sds->res[2];
+       if (sds->fluid) {
+               return sds->base_res[0]*sds->base_res[1]*sds->base_res[2];
        }
        else
                return 0;
 }
+
+#define SMOKE_CACHE_VERSION "1.04"
+
 static int  ptcache_smoke_write(PTCacheFile *pf, void *smoke_v)
 {      
        SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
        SmokeDomainSettings *sds = smd->domain;
        int ret = 0;
+       int fluid_fields = smoke_get_data_flags(sds);
+
+       /* version header */
+       ptcache_file_write(pf, SMOKE_CACHE_VERSION, 4, sizeof(char));
+       ptcache_file_write(pf, &fluid_fields, 1, sizeof(int));
+       ptcache_file_write(pf, &sds->active_fields, 1, sizeof(int));
+       ptcache_file_write(pf, &sds->res, 3, sizeof(int));
+       ptcache_file_write(pf, &sds->dx, 1, sizeof(float));
        
-       if(sds->fluid) {
+       if (sds->fluid) {
                size_t res = sds->res[0]*sds->res[1]*sds->res[2];
-               float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
+               float dt, dx, *dens, *react, *fuel, *flame, *heat, *heatold, *vx, *vy, *vz, *r, *g, *b;
                unsigned char *obstacles;
                unsigned int in_len = sizeof(float)*(unsigned int)res;
                unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
@@ -545,33 +564,51 @@ static int  ptcache_smoke_write(PTCacheFile *pf, void *smoke_v)
                int mode=1;             // light
                if (sds->cache_comp == SM_CACHE_HEAVY) mode=2;  // heavy
 
-               smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
+               smoke_export(sds->fluid, &dt, &dx, &dens, &react, &flame, &fuel, &heat, &heatold, &vx, &vy, &vz, &r, &g, &b, &obstacles);
 
                ptcache_file_compressed_write(pf, (unsigned char *)sds->shadow, in_len, out, mode);
                ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len, out, mode);
-               ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len, out, mode); 
-               ptcache_file_compressed_write(pf, (unsigned char *)heat, in_len, out, mode);
-               ptcache_file_compressed_write(pf, (unsigned char *)heatold, in_len, out, mode);
+               if (fluid_fields & SM_ACTIVE_HEAT) {
+                       ptcache_file_compressed_write(pf, (unsigned char *)heat, in_len, out, mode);
+                       ptcache_file_compressed_write(pf, (unsigned char *)heatold, in_len, out, mode);
+               }
+               if (fluid_fields & SM_ACTIVE_FIRE) {
+                       ptcache_file_compressed_write(pf, (unsigned char *)flame, in_len, out, mode);
+                       ptcache_file_compressed_write(pf, (unsigned char *)fuel, in_len, out, mode);
+                       ptcache_file_compressed_write(pf, (unsigned char *)react, in_len, out, mode);
+               }
+               if (fluid_fields & SM_ACTIVE_COLORS) {
+                       ptcache_file_compressed_write(pf, (unsigned char *)r, in_len, out, mode);
+                       ptcache_file_compressed_write(pf, (unsigned char *)g, in_len, out, mode);
+                       ptcache_file_compressed_write(pf, (unsigned char *)b, in_len, out, mode);
+               }
                ptcache_file_compressed_write(pf, (unsigned char *)vx, in_len, out, mode);
                ptcache_file_compressed_write(pf, (unsigned char *)vy, in_len, out, mode);
                ptcache_file_compressed_write(pf, (unsigned char *)vz, in_len, out, mode);
-               ptcache_file_compressed_write(pf, (unsigned char *)vxold, in_len, out, mode);
-               ptcache_file_compressed_write(pf, (unsigned char *)vyold, in_len, out, mode);
-               ptcache_file_compressed_write(pf, (unsigned char *)vzold, in_len, out, mode);
                ptcache_file_compressed_write(pf, (unsigned char *)obstacles, (unsigned int)res, out, mode);
                ptcache_file_write(pf, &dt, 1, sizeof(float));
                ptcache_file_write(pf, &dx, 1, sizeof(float));
+               ptcache_file_write(pf, &sds->p0, 3, sizeof(float));
+               ptcache_file_write(pf, &sds->p1, 3, sizeof(float));
+               ptcache_file_write(pf, &sds->dp0, 3, sizeof(float));
+               ptcache_file_write(pf, &sds->shift, 3, sizeof(int));
+               ptcache_file_write(pf, &sds->obj_shift_f, 3, sizeof(float));
+               ptcache_file_write(pf, &sds->obmat, 16, sizeof(float));
+               ptcache_file_write(pf, &sds->base_res, 3, sizeof(int));
+               ptcache_file_write(pf, &sds->res_min, 3, sizeof(int));
+               ptcache_file_write(pf, &sds->res_max, 3, sizeof(int));
+               ptcache_file_write(pf, &sds->active_color, 3, sizeof(float));
 
                MEM_freeN(out);
                
                ret = 1;
        }
 
-       if(sds->wt) {
+       if (sds->wt) {
                int res_big_array[3];
                int res_big;
                int res = sds->res[0]*sds->res[1]*sds->res[2];
-               float *dens, *densold, *tcu, *tcv, *tcw;
+               float *dens, *react, *fuel, *flame, *tcu, *tcv, *tcw, *r, *g, *b;
                unsigned int in_len = sizeof(float)*(unsigned int)res;
                unsigned int in_len_big;
                unsigned char *out;
@@ -585,11 +622,20 @@ static int  ptcache_smoke_write(PTCacheFile *pf, void *smoke_v)
 
                in_len_big = sizeof(float) * (unsigned int)res_big;
 
-               smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
+               smoke_turbulence_export(sds->wt, &dens, &react, &flame, &fuel, &r, &g, &b, &tcu, &tcv, &tcw);
 
                out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len_big), "pointcache_lzo_buffer");
                ptcache_file_compressed_write(pf, (unsigned char *)dens, in_len_big, out, mode);
-               ptcache_file_compressed_write(pf, (unsigned char *)densold, in_len_big, out, mode);     
+               if (fluid_fields & SM_ACTIVE_FIRE) {
+                       ptcache_file_compressed_write(pf, (unsigned char *)flame, in_len_big, out, mode);
+                       ptcache_file_compressed_write(pf, (unsigned char *)fuel, in_len_big, out, mode);
+                       ptcache_file_compressed_write(pf, (unsigned char *)react, in_len_big, out, mode);
+               }
+               if (fluid_fields & SM_ACTIVE_COLORS) {
+                       ptcache_file_compressed_write(pf, (unsigned char *)r, in_len_big, out, mode);
+                       ptcache_file_compressed_write(pf, (unsigned char *)g, in_len_big, out, mode);
+                       ptcache_file_compressed_write(pf, (unsigned char *)b, in_len_big, out, mode);
+               }
                MEM_freeN(out);
 
                out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
@@ -607,34 +653,95 @@ static int ptcache_smoke_read(PTCacheFile *pf, void *smoke_v)
 {
        SmokeModifierData *smd= (SmokeModifierData *)smoke_v;
        SmokeDomainSettings *sds = smd->domain;
+       char version[4];
+       int ch_res[3];
+       float ch_dx;
+       int fluid_fields = smoke_get_data_flags(sds);
+       int cache_fields = 0;
+       int active_fields = 0;
+       int reallocate = 0;
+
+       /* version header */
+       ptcache_file_read(pf, version, 4, sizeof(char));
+       if (strncmp(version, SMOKE_CACHE_VERSION, 4)) return 0;
+       /* fluid info */
+       ptcache_file_read(pf, &cache_fields, 1, sizeof(int));
+       ptcache_file_read(pf, &active_fields, 1, sizeof(int));
+       ptcache_file_read(pf, &ch_res, 3, sizeof(int));
+       ptcache_file_read(pf, &ch_dx, 1, sizeof(float));
+
+       /* check if resolution has changed */
+       if (sds->res[0] != ch_res[0] ||
+               sds->res[1] != ch_res[1] ||
+               sds->res[2] != ch_res[2]) {
+               if (sds->flags & MOD_SMOKE_ADAPTIVE_DOMAIN)
+                       reallocate = 1;
+               else
+                       return 0;
+       }
+       /* check if active fields have changed */
+       if (fluid_fields != cache_fields ||
+               active_fields != sds->active_fields)
+               reallocate = 1;
+
+       /* reallocate fluid if needed*/
+       if (reallocate) {
+               sds->active_fields = active_fields | cache_fields;
+               smoke_reallocate_fluid(sds, ch_dx, ch_res, 1);
+               sds->dx = ch_dx;
+               VECCOPY(sds->res, ch_res);
+               sds->total_cells = ch_res[0]*ch_res[1]*ch_res[2];
+               if (sds->flags & MOD_SMOKE_HIGHRES) {
+                       smoke_reallocate_highres_fluid(sds, ch_dx, ch_res, 1);
+               }
+       }
        
-       if(sds->fluid) {
+       if (sds->fluid) {
                size_t res = sds->res[0]*sds->res[1]*sds->res[2];
-               float dt, dx, *dens, *densold, *heat, *heatold, *vx, *vy, *vz, *vxold, *vyold, *vzold;
+               float dt, dx, *dens, *react, *fuel, *flame, *heat, *heatold, *vx, *vy, *vz, *r, *g, *b;
                unsigned char *obstacles;
                unsigned int out_len = (unsigned int)res * sizeof(float);
                
-               smoke_export(sds->fluid, &dt, &dx, &dens, &densold, &heat, &heatold, &vx, &vy, &vz, &vxold, &vyold, &vzold, &obstacles);
+               smoke_export(sds->fluid, &dt, &dx, &dens, &react, &flame, &fuel, &heat, &heatold, &vx, &vy, &vz, &r, &g, &b, &obstacles);
 
                ptcache_file_compressed_read(pf, (unsigned char *)sds->shadow, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)heat, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)heatold, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)vx, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)vy, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)vz, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)vxold, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)vyold, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)vzold, out_len);
-               ptcache_file_compressed_read(pf, (unsigned char*)obstacles, (unsigned int)res);
+               ptcache_file_compressed_read(pf, (unsigned char *)dens, out_len);
+               if (cache_fields & SM_ACTIVE_HEAT) {
+                       ptcache_file_compressed_read(pf, (unsigned char *)heat, out_len);
+                       ptcache_file_compressed_read(pf, (unsigned char *)heatold, out_len);
+               }
+               if (cache_fields & SM_ACTIVE_FIRE) {
+                       ptcache_file_compressed_read(pf, (unsigned char *)flame, out_len);
+                       ptcache_file_compressed_read(pf, (unsigned char *)fuel, out_len);
+                       ptcache_file_compressed_read(pf, (unsigned char *)react, out_len);
+               }
+               if (cache_fields & SM_ACTIVE_COLORS) {
+                       ptcache_file_compressed_read(pf, (unsigned char *)r, out_len);
+                       ptcache_file_compressed_read(pf, (unsigned char *)g, out_len);
+                       ptcache_file_compressed_read(pf, (unsigned char *)b, out_len);
+               }
+               ptcache_file_compressed_read(pf, (unsigned char *)vx, out_len);
+               ptcache_file_compressed_read(pf, (unsigned char *)vy, out_len);
+               ptcache_file_compressed_read(pf, (unsigned char *)vz, out_len);
+               ptcache_file_compressed_read(pf, (unsigned char *)obstacles, (unsigned int)res);
                ptcache_file_read(pf, &dt, 1, sizeof(float));
                ptcache_file_read(pf, &dx, 1, sizeof(float));
-
-               if(pf->data_types & (1<<BPHYS_DATA_SMOKE_HIGH) && sds->wt) {
+               ptcache_file_read(pf, &sds->p0, 3, sizeof(float));
+               ptcache_file_read(pf, &sds->p1, 3, sizeof(float));
+               ptcache_file_read(pf, &sds->dp0, 3, sizeof(float));
+               ptcache_file_read(pf, &sds->shift, 3, sizeof(int));
+               ptcache_file_read(pf, &sds->obj_shift_f, 3, sizeof(float));
+               ptcache_file_read(pf, &sds->obmat, 16, sizeof(float));
+               ptcache_file_read(pf, &sds->base_res, 3, sizeof(int));
+               ptcache_file_read(pf, &sds->res_min, 3, sizeof(int));
+               ptcache_file_read(pf, &sds->res_max, 3, sizeof(int));
+               ptcache_file_read(pf, &sds->active_color, 3, sizeof(float));
+       }
+
+       if (pf->data_types & (1<<BPHYS_DATA_SMOKE_HIGH) && sds->wt) {
                        int res = sds->res[0]*sds->res[1]*sds->res[2];
                        int res_big, res_big_array[3];
-                       float *dens, *densold, *tcu, *tcv, *tcw;
+                       float *dens, *react, *fuel, *flame, *tcu, *tcv, *tcw, *r, *g, *b;
                        unsigned int out_len = sizeof(float)*(unsigned int)res;
                        unsigned int out_len_big;
 
@@ -642,16 +749,24 @@ static int ptcache_smoke_read(PTCacheFile *pf, void *smoke_v)
                        res_big = res_big_array[0]*res_big_array[1]*res_big_array[2];
                        out_len_big = sizeof(float) * (unsigned int)res_big;
 
-                       smoke_turbulence_export(sds->wt, &dens, &densold, &tcu, &tcv, &tcw);
+                       smoke_turbulence_export(sds->wt, &dens, &react, &flame, &fuel, &r, &g, &b, &tcu, &tcv, &tcw);
 
-                       ptcache_file_compressed_read(pf, (unsigned char*)dens, out_len_big);
-                       ptcache_file_compressed_read(pf, (unsigned char*)densold, out_len_big);
+                       ptcache_file_compressed_read(pf, (unsigned char *)dens, out_len_big);
+                       if (cache_fields & SM_ACTIVE_FIRE) {
+                               ptcache_file_compressed_read(pf, (unsigned char *)flame, out_len_big);
+                               ptcache_file_compressed_read(pf, (unsigned char *)fuel, out_len_big);
+                               ptcache_file_compressed_read(pf, (unsigned char *)react, out_len_big);
+                       }
+                       if (cache_fields & SM_ACTIVE_COLORS) {
+                               ptcache_file_compressed_read(pf, (unsigned char *)r, out_len_big);
+                               ptcache_file_compressed_read(pf, (unsigned char *)g, out_len_big);
+                               ptcache_file_compressed_read(pf, (unsigned char *)b, out_len_big);
+                       }
 
-                       ptcache_file_compressed_read(pf, (unsigned char*)tcu, out_len);
-                       ptcache_file_compressed_read(pf, (unsigned char*)tcv, out_len);
-                       ptcache_file_compressed_read(pf, (unsigned char*)tcw, out_len);
+                       ptcache_file_compressed_read(pf, (unsigned char *)tcu, out_len);
+                       ptcache_file_compressed_read(pf, (unsigned char *)tcv, out_len);
+                       ptcache_file_compressed_read(pf, (unsigned char *)tcw, out_len);
                }
-       }
 
        return 1;
 }
@@ -679,7 +794,7 @@ static int  ptcache_dynamicpaint_write(PTCacheFile *pf, void *dp_v)
        /* version header */
        ptcache_file_write(pf, DPAINT_CACHE_VERSION, 1, sizeof(char)*4);
 
-       if(surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
+       if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
                int total_points=surface->data->total_points;
                unsigned int in_len;
                unsigned char *out;
@@ -687,14 +802,20 @@ static int  ptcache_dynamicpaint_write(PTCacheFile *pf, void *dp_v)
                /* cache type */
                ptcache_file_write(pf, &surface->type, 1, sizeof(int));
 
-               if (surface->type == MOD_DPAINT_SURFACE_T_PAINT)
-                       in_len = sizeof(PaintPoint)*total_points;
+               if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
+                       in_len = sizeof(PaintPoint) * total_points;
+               }
                else if (surface->type == MOD_DPAINT_SURFACE_T_DISPLACE ||
-                                surface->type == MOD_DPAINT_SURFACE_T_WEIGHT)
-                       in_len = sizeof(float)*total_points;
-               else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE)
-                       in_len = sizeof(PaintWavePoint)*total_points;
-               else return 0;
+                        surface->type == MOD_DPAINT_SURFACE_T_WEIGHT)
+               {
+                       in_len = sizeof(float) * total_points;
+               }
+               else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
+                       in_len = sizeof(PaintWavePoint) * total_points;
+               }
+               else {
+                       return 0;
+               }
 
                out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len), "pointcache_lzo_buffer");
 
@@ -711,9 +832,9 @@ static int ptcache_dynamicpaint_read(PTCacheFile *pf, void *dp_v)
        
        /* version header */
        ptcache_file_read(pf, version, 1, sizeof(char)*4);
-       if (strncmp(version, DPAINT_CACHE_VERSION,4)) {printf("Dynamic Paint: Invalid cache version: %s!\n",version); return 0;}
+       if (strncmp(version, DPAINT_CACHE_VERSION, 4)) {printf("Dynamic Paint: Invalid cache version: %s!\n", version); return 0;}
 
-       if(surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
+       if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
                unsigned int data_len;
                int surface_type;
 
@@ -724,16 +845,22 @@ static int ptcache_dynamicpaint_read(PTCacheFile *pf, void *dp_v)
                        return 0;
 
                /* read surface data */
-               if (surface->type == MOD_DPAINT_SURFACE_T_PAINT)
+               if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
                        data_len = sizeof(PaintPoint);
+               }
                else if (surface->type == MOD_DPAINT_SURFACE_T_DISPLACE ||
-                                surface->type == MOD_DPAINT_SURFACE_T_WEIGHT)
+                        surface->type == MOD_DPAINT_SURFACE_T_WEIGHT)
+               {
                        data_len = sizeof(float);
-               else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE)
+               }
+               else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
                        data_len = sizeof(PaintWavePoint);
-               else return 0;
+               }
+               else {
+                       return 0;
+               }
 
-               ptcache_file_compressed_read(pf, (unsigned char*)surface->data->type_data, data_len*surface->data->total_points);
+               ptcache_file_compressed_read(pf, (unsigned char *)surface->data->type_data, data_len*surface->data->total_points);
 
        }
        return 1;
@@ -786,7 +913,7 @@ void BKE_ptcache_id_from_particles(PTCacheID *pid, Object *ob, ParticleSystem *p
        pid->cache_ptr= &psys->pointcache;
        pid->ptcaches= &psys->ptcaches;
 
-       if(psys->part->type != PART_HAIR)
+       if (psys->part->type != PART_HAIR)
                pid->flag |= PTCACHE_VEL_PER_SEC;
 
        pid->totpoint                           = ptcache_particle_totpoint;
@@ -808,20 +935,24 @@ void BKE_ptcache_id_from_particles(PTCacheID *pid, Object *ob, ParticleSystem *p
 
        pid->data_types = (1<<BPHYS_DATA_LOCATION) | (1<<BPHYS_DATA_VELOCITY) | (1<<BPHYS_DATA_INDEX);
 
-       if(psys->part->phystype == PART_PHYS_BOIDS)
+       if (psys->part->phystype == PART_PHYS_BOIDS)
                pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION) | (1<<BPHYS_DATA_BOIDS);
-       else if(psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS) {
+       else if (psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS) {
                pid->write_extra_data = ptcache_particle_extra_write;
                pid->read_extra_data = ptcache_particle_extra_read;
        }
 
-       if(psys->part->rotmode!=PART_ROT_VEL
-               || psys->part->avemode!=PART_AVE_SPIN || psys->part->avefac!=0.0f)
-               pid->data_types|= (1<<BPHYS_DATA_AVELOCITY) | (1<<BPHYS_DATA_ROTATION);
-
-       if(psys->part->flag & PART_ROT_DYN)
+       if (psys->part->flag & PART_ROTATIONS) {
                pid->data_types|= (1<<BPHYS_DATA_ROTATION);
 
+               if (psys->part->rotmode != PART_ROT_VEL  ||
+                   psys->part->avemode == PART_AVE_RAND ||
+                   psys->part->avefac != 0.0f)
+               {
+                       pid->data_types |= (1 << BPHYS_DATA_AVELOCITY);
+               }
+       }
+
        pid->info_types= (1<<BPHYS_DATA_TIMES);
 
        pid->default_step = 10;
@@ -895,9 +1026,9 @@ void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct SmokeMo
        pid->data_types= 0;
        pid->info_types= 0;
 
-       if(sds->fluid)
+       if (sds->fluid)
                pid->data_types |= (1<<BPHYS_DATA_SMOKE_LOW);
-       if(sds->wt)
+       if (sds->wt)
                pid->data_types |= (1<<BPHYS_DATA_SMOKE_HIGH);
 
        pid->default_step = 1;
@@ -948,25 +1079,25 @@ void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int dup
 
        lb->first= lb->last= NULL;
 
-       if(ob->soft) {
+       if (ob->soft) {
                pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
                BKE_ptcache_id_from_softbody(pid, ob, ob->soft);
                BLI_addtail(lb, pid);
        }
 
-       for(psys=ob->particlesystem.first; psys; psys=psys->next) {
-               if(psys->part==NULL)
+       for (psys=ob->particlesystem.first; psys; psys=psys->next) {
+               if (psys->part==NULL)
                        continue;
                
                /* check to make sure point cache is actually used by the particles */
-               if(ELEM(psys->part->phystype, PART_PHYS_NO, PART_PHYS_KEYED))
+               if (ELEM(psys->part->phystype, PART_PHYS_NO, PART_PHYS_KEYED))
                        continue;
 
                /* hair needs to be included in id-list for cache edit mode to work */
-               /* if(psys->part->type == PART_HAIR && (psys->flag & PSYS_HAIR_DYNAMICS)==0) */
+               /* if (psys->part->type == PART_HAIR && (psys->flag & PSYS_HAIR_DYNAMICS)==0) */
                /*      continue; */
                        
-               if(psys->part->type == PART_FLUID)
+               if (psys->part->type == PART_FLUID)
                        continue;
 
                pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
@@ -974,25 +1105,23 @@ void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int dup
                BLI_addtail(lb, pid);
        }
 
-       for(md=ob->modifiers.first; md; md=md->next) {
-               if(md->type == eModifierType_Cloth) {
+       for (md=ob->modifiers.first; md; md=md->next) {
+               if (md->type == eModifierType_Cloth) {
                        pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
                        BKE_ptcache_id_from_cloth(pid, ob, (ClothModifierData*)md);
                        BLI_addtail(lb, pid);
                }
-               else if(md->type == eModifierType_Smoke) {
+               else if (md->type == eModifierType_Smoke) {
                        SmokeModifierData *smd = (SmokeModifierData *)md;
-                       if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
-                       {
+                       if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
                                pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
                                BKE_ptcache_id_from_smoke(pid, ob, (SmokeModifierData*)md);
                                BLI_addtail(lb, pid);
                        }
                }
-               else if(md->type == eModifierType_DynamicPaint) {
+               else if (md->type == eModifierType_DynamicPaint) {
                        DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
-                       if(pmd->canvas)
-                       {
+                       if (pmd->canvas) {
                                DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
 
                                for (; surface; surface=surface->next) {
@@ -1004,17 +1133,18 @@ void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int dup
                }
        }
 
-       if(scene && (duplis-- > 0) && (ob->transflag & OB_DUPLI)) {
+       if (scene && (duplis-- > 0) && (ob->transflag & OB_DUPLI)) {
                ListBase *lb_dupli_ob;
 
-               if((lb_dupli_ob=object_duplilist(scene, ob))) {
+               /* don't update the dupli groups, we only wan't their pid's */
+               if ((lb_dupli_ob = object_duplilist_ex(scene, ob, FALSE, FALSE))) {
                        DupliObject *dob;
-                       for(dob= lb_dupli_ob->first; dob; dob= dob->next) {
-                               if(dob->ob != ob) { /* avoids recursive loops with dupliframes: bug 22988 */
+                       for (dob= lb_dupli_ob->first; dob; dob= dob->next) {
+                               if (dob->ob != ob) { /* avoids recursive loops with dupliframes: bug 22988 */
                                        ListBase lb_dupli_pid;
                                        BKE_ptcache_ids_from_object(&lb_dupli_pid, dob->ob, scene, duplis);
                                        BLI_movelisttolist(lb, &lb_dupli_pid);
-                                       if(lb_dupli_pid.first)
+                                       if (lb_dupli_pid.first)
                                                printf("Adding Dupli\n");
                                }
                        }
@@ -1026,11 +1156,11 @@ void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int dup
 
 /* File handling */
 
-/*     Takes an Object ID and returns a unique name
      - id: object id
      - cfra: frame for the cache, can be negative
      - stack_index: index in the modifier stack. we can have cache for more then one stack_index
-*/
+/* Takes an Object ID and returns a unique name
* - id: object id
* - cfra: frame for the cache, can be negative
* - stack_index: index in the modifier stack. we can have cache for more then one stack_index
+ */
 
 #define MAX_PTCACHE_PATH FILE_MAX
 #define MAX_PTCACHE_FILE ((FILE_MAX)*2)
@@ -1041,16 +1171,17 @@ static int ptcache_path(PTCacheID *pid, char *filename)
        const char *blendfilename= (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH)==0) ? lib->filepath: G.main->name;
        size_t i;
 
-       if(pid->cache->flag & PTCACHE_EXTERNAL) {
+       if (pid->cache->flag & PTCACHE_EXTERNAL) {
                strcpy(filename, pid->cache->path);
 
-               if(strncmp(filename, "//", 2)==0)
+               if (BLI_path_is_rel(filename)) {
                        BLI_path_abs(filename, blendfilename);
+               }
 
                return BLI_add_slash(filename); /* new strlen() */
        }
        else if (G.relbase_valid || lib) {
-               char file[MAX_PTCACHE_PATH]; /* we dont want the dir, only the file */
+               char file[MAX_PTCACHE_PATH]; /* we don't want the dir, only the file */
 
                BLI_split_file_part(blendfilename, file, sizeof(file));
                i = strlen(file);
@@ -1086,10 +1217,10 @@ static int ptcache_filename(PTCacheID *pid, char *filename, int cfra, short do_p
                len = ptcache_path(pid, filename);
                newname += len;
        }
-       if(pid->cache->name[0] == '\0' && (pid->cache->flag & PTCACHE_EXTERNAL)==0) {
+       if (pid->cache->name[0] == '\0' && (pid->cache->flag & PTCACHE_EXTERNAL)==0) {
                idname = (pid->ob->id.name+2);
                /* convert chars to hex so they are always a valid filename */
-               while('\0' != *idname) {
+               while ('\0' != *idname) {
                        BLI_snprintf(newname, MAX_PTCACHE_FILE, "%02X", (char)(*idname++));
                        newname+=2;
                        len += 2;
@@ -1104,11 +1235,11 @@ static int ptcache_filename(PTCacheID *pid, char *filename, int cfra, short do_p
 
        if (do_ext) {
 
-               if(pid->cache->index < 0)
-                       pid->cache->index =  pid->stack_index = object_insert_ptcache(pid->ob);
+               if (pid->cache->index < 0)
+                       pid->cache->index =  pid->stack_index = BKE_object_insert_ptcache(pid->ob);
 
-               if(pid->cache->flag & PTCACHE_EXTERNAL) {
-                       if(pid->cache->index >= 0)
+               if (pid->cache->flag & PTCACHE_EXTERNAL) {
+                       if (pid->cache->index >= 0)
                                BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02u"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
                        else
                                BLI_snprintf(newname, MAX_PTCACHE_FILE, "_%06d"PTCACHE_EXT, cfra); /* always 6 chars */
@@ -1131,7 +1262,7 @@ static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
 
 #ifndef DURIAN_POINTCACHE_LIB_OK
        /* don't allow writing for linked objects */
-       if(pid->ob->id.lib && mode == PTCACHE_FILE_WRITE)
+       if (pid->ob->id.lib && mode == PTCACHE_FILE_WRITE)
                return NULL;
 #endif
        if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL)==0) return NULL; /* save blend file before using disk pointcache */
@@ -1142,13 +1273,15 @@ static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
                if (!BLI_exists(filename)) {
                        return NULL;
                }
-               fp = fopen(filename, "rb");
-       } else if (mode==PTCACHE_FILE_WRITE) {
+               fp = BLI_fopen(filename, "rb");
+       }
+       else if (mode==PTCACHE_FILE_WRITE) {
                BLI_make_existing_file(filename); /* will create the dir if needs be, same as //textures is created */
-               fp = fopen(filename, "wb");
-       } else if (mode==PTCACHE_FILE_UPDATE) {
+               fp = BLI_fopen(filename, "wb");
+       }
+       else if (mode==PTCACHE_FILE_UPDATE) {
                BLI_make_existing_file(filename);
-               fp = fopen(filename, "rb+");
+               fp = BLI_fopen(filename, "rb+");
        }
 
        if (!fp)
@@ -1163,7 +1296,7 @@ static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
 }
 static void ptcache_file_close(PTCacheFile *pf)
 {
-       if(pf) {
+       if (pf) {
                fclose(pf->fp);
                MEM_freeN(pf);
        }
@@ -1181,25 +1314,24 @@ static int ptcache_file_compressed_read(PTCacheFile *pf, unsigned char *result,
        unsigned char *props = MEM_callocN(16*sizeof(char), "tmp");
 
        ptcache_file_read(pf, &compressed, 1, sizeof(unsigned char));
-       if(compressed) {
+       if (compressed) {
                unsigned int size;
                ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
                in_len = (size_t)size;
-               if(in_len==0) {
+               if (in_len==0) {
                        /* do nothing */
                }
                else {
                        in = (unsigned char *)MEM_callocN(sizeof(unsigned char)*in_len, "pointcache_compressed_buffer");
                        ptcache_file_read(pf, in, in_len, sizeof(unsigned char));
 #ifdef WITH_LZO
-                       if(compressed == 1)
+                       if (compressed == 1)
                                r = lzo1x_decompress_safe(in, (lzo_uint)in_len, result, (lzo_uint *)&out_len, NULL);
 #endif
 #ifdef WITH_LZMA
-                       if(compressed == 2)
-                       {
+                       if (compressed == 2) {
                                size_t sizeOfIt;
-                               size_t leni = in_len, leno = out_len;
+                               size_t leni = in_len, leno = len;
                                ptcache_file_read(pf, &size, 1, sizeof(unsigned int));
                                sizeOfIt = (size_t)size;
                                ptcache_file_read(pf, props, sizeOfIt, sizeof(unsigned char));
@@ -1229,10 +1361,10 @@ static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, uns
 
 #ifdef WITH_LZO
        out_len= LZO_OUT_LEN(in_len);
-       if(mode == 1) {
+       if (mode == 1) {
                LZO_HEAP_ALLOC(wrkmem, LZO1X_MEM_COMPRESS);
                
-               r = lzo1x_1_compress(in, (lzo_uint)in_len, out, (lzo_uint *)&out_len, wrkmem);  
+               r = lzo1x_1_compress(in, (lzo_uint)in_len, out, (lzo_uint *)&out_len, wrkmem);
                if (!(r == LZO_E_OK) || (out_len >= in_len))
                        compressed = 0;
                else
@@ -1240,12 +1372,12 @@ static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, uns
        }
 #endif
 #ifdef WITH_LZMA
-       if(mode == 2) {
+       if (mode == 2) {
                
-               r = LzmaCompress(out, &out_len, in, in_len,//assume sizeof(char)==1....
+               r = LzmaCompress(out, &out_len, in, in_len, //assume sizeof(char)==1....
                                                props, &sizeOfIt, 5, 1 << 24, 3, 0, 2, 32, 2);
 
-               if(!(r == SZ_OK) || (out_len >= in_len))
+               if (!(r == SZ_OK) || (out_len >= in_len))
                        compressed = 0;
                else
                        compressed = 2;
@@ -1253,7 +1385,7 @@ static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, uns
 #endif
        
        ptcache_file_write(pf, &compressed, 1, sizeof(unsigned char));
-       if(compressed) {
+       if (compressed) {
                unsigned int size = out_len;
                ptcache_file_write(pf, &size, 1, sizeof(unsigned int));
                ptcache_file_write(pf, out, out_len, sizeof(unsigned char));
@@ -1261,8 +1393,7 @@ static int ptcache_file_compressed_write(PTCacheFile *pf, unsigned char *in, uns
        else
                ptcache_file_write(pf, in, in_len, sizeof(unsigned char));
 
-       if(compressed == 2)
-       {
+       if (compressed == 2) {
                unsigned int size = sizeOfIt;
                ptcache_file_write(pf, &sizeOfIt, 1, sizeof(unsigned int));
                ptcache_file_write(pf, props, size, sizeof(unsigned char));
@@ -1284,8 +1415,8 @@ static int ptcache_file_data_read(PTCacheFile *pf)
 {
        int i;
 
-       for(i=0; i<BPHYS_TOT_DATA; i++) {
-               if((pf->data_types & (1<<i)) && !ptcache_file_read(pf, pf->cur[i], 1, ptcache_data_size[i]))
+       for (i=0; i<BPHYS_TOT_DATA; i++) {
+               if ((pf->data_types & (1<<i)) && !ptcache_file_read(pf, pf->cur[i], 1, ptcache_data_size[i]))
                        return 0;
        }
        
@@ -1295,8 +1426,8 @@ static int ptcache_file_data_write(PTCacheFile *pf)
 {              
        int i;
 
-       for(i=0; i<BPHYS_TOT_DATA; i++) {
-               if((pf->data_types & (1<<i)) && !ptcache_file_write(pf, pf->cur[i], 1, ptcache_data_size[i]))
+       for (i=0; i<BPHYS_TOT_DATA; i++) {
+               if ((pf->data_types & (1<<i)) && !ptcache_file_write(pf, pf->cur[i], 1, ptcache_data_size[i]))
                        return 0;
        }
        
@@ -1310,20 +1441,20 @@ static int ptcache_file_header_begin_read(PTCacheFile *pf)
        
        pf->data_types = 0;
        
-       if(fread(bphysics, sizeof(char), 8, pf->fp) != 8)
+       if (fread(bphysics, sizeof(char), 8, pf->fp) != 8)
                error = 1;
        
-       if(!error && strncmp(bphysics, "BPHYSICS", 8))
+       if (!error && strncmp(bphysics, "BPHYSICS", 8))
                error = 1;
 
-       if(!error && !fread(&typeflag, sizeof(unsigned int), 1, pf->fp))
+       if (!error && !fread(&typeflag, sizeof(unsigned int), 1, pf->fp))
                error = 1;
 
        pf->type = (typeflag & PTCACHE_TYPEFLAG_TYPEMASK);
        pf->flag = (typeflag & PTCACHE_TYPEFLAG_FLAGMASK);
        
        /* if there was an error set file as it was */
-       if(error)
+       if (error)
                fseek(pf->fp, 0, SEEK_SET);
 
        return !error;
@@ -1333,10 +1464,10 @@ static int ptcache_file_header_begin_write(PTCacheFile *pf)
        const char *bphysics = "BPHYSICS";
        unsigned int typeflag = pf->type + pf->flag;
        
-       if(fwrite(bphysics, sizeof(char), 8, pf->fp) != 8)
+       if (fwrite(bphysics, sizeof(char), 8, pf->fp) != 8)
                return 0;
 
-       if(!fwrite(&typeflag, sizeof(unsigned int), 1, pf->fp))
+       if (!fwrite(&typeflag, sizeof(unsigned int), 1, pf->fp))
                return 0;
        
        return 1;
@@ -1365,23 +1496,23 @@ static void ptcache_file_pointers_init(PTCacheFile *pf)
 /* Check to see if point number "index" is in pm, uses binary search for index data. */
 int BKE_ptcache_mem_index_find(PTCacheMem *pm, unsigned int index)
 {
-       if(pm->data[BPHYS_DATA_INDEX]) {
+       if (pm->data[BPHYS_DATA_INDEX]) {
                unsigned int *data = pm->data[BPHYS_DATA_INDEX];
                unsigned int mid, low = 0, high = pm->totpoint - 1;
 
-               if(index < *data || index > *(data+high))
+               if (index < *data || index > *(data+high))
                        return -1;
 
                /* check simple case for continuous indexes first */
-               if(index-*data < high && data[index-*data] == index)
+               if (index-*data < high && data[index-*data] == index)
                        return index-*data;
 
-               while(low <= high) {
+               while (low <= high) {
                        mid= (low + high)/2;
 
-                       if(data[mid] > index)
+                       if (data[mid] > index)
                                high = mid - 1;
-                       else if(data[mid] < index)
+                       else if (data[mid] < index)
                                low = mid + 1;
                        else
                                return mid;
@@ -1399,7 +1530,7 @@ void BKE_ptcache_mem_pointers_init(PTCacheMem *pm)
        int data_types = pm->data_types;
        int i;
 
-       for(i=0; i<BPHYS_TOT_DATA; i++)
+       for (i=0; i<BPHYS_TOT_DATA; i++)
                pm->cur[i] = ((data_types & (1<<i)) ? pm->data[i] : NULL);
 }
 
@@ -1407,9 +1538,9 @@ void BKE_ptcache_mem_pointers_incr(PTCacheMem *pm)
 {
        int i;
 
-       for(i=0; i<BPHYS_TOT_DATA; i++) {
-               if(pm->cur[i])
-                       pm->cur[i] = (char*)pm->cur[i] + ptcache_data_size[i];
+       for (i=0; i<BPHYS_TOT_DATA; i++) {
+               if (pm->cur[i])
+                       pm->cur[i] = (char *)pm->cur[i] + ptcache_data_size[i];
        }
 }
 int  BKE_ptcache_mem_pointers_seek(int point_index, PTCacheMem *pm)
@@ -1417,7 +1548,7 @@ int  BKE_ptcache_mem_pointers_seek(int point_index, PTCacheMem *pm)
        int data_types = pm->data_types;
        int i, index = BKE_ptcache_mem_index_find(pm, point_index);
 
-       if(index < 0) {
+       if (index < 0) {
                /* Can't give proper location without reallocation, so don't give any location.
                 * Some points will be cached improperly, but this only happens with simulation
                 * steps bigger than cache->step, so the cache has to be recalculated anyways
@@ -1426,8 +1557,8 @@ int  BKE_ptcache_mem_pointers_seek(int point_index, PTCacheMem *pm)
                return 0;
        }
 
-       for(i=0; i<BPHYS_TOT_DATA; i++)
-               pm->cur[i] = data_types & (1<<i) ? (char*)pm->data[i] + index * ptcache_data_size[i] : NULL;
+       for (i=0; i<BPHYS_TOT_DATA; i++)
+               pm->cur[i] = data_types & (1<<i) ? (char *)pm->data[i] + index * ptcache_data_size[i] : NULL;
 
        return 1;
 }
@@ -1437,8 +1568,8 @@ static void ptcache_data_alloc(PTCacheMem *pm)
        int totpoint = pm->totpoint;
        int i;
 
-       for(i=0; i<BPHYS_TOT_DATA; i++) {
-               if(data_types & (1<<i))
+       for (i=0; i<BPHYS_TOT_DATA; i++) {
+               if (data_types & (1<<i))
                        pm->data[i] = MEM_callocN(totpoint * ptcache_data_size[i], "PTCache Data");
        }
 }
@@ -1447,18 +1578,18 @@ static void ptcache_data_free(PTCacheMem *pm)
        void **data = pm->data;
        int i;
 
-       for(i=0; i<BPHYS_TOT_DATA; i++) {
-               if(data[i])
+       for (i=0; i<BPHYS_TOT_DATA; i++) {
+               if (data[i])
                        MEM_freeN(data[i]);
        }
 }
 static void ptcache_data_copy(void *from[], void *to[])
 {
        int i;
-       for(i=0; i<BPHYS_TOT_DATA; i++) {
+       for (i=0; i<BPHYS_TOT_DATA; i++) {
        /* note, durian file 03.4b_comp crashes if to[i] is not tested
         * its NULL, not sure if this should be fixed elsewhere but for now its needed */
-               if(from[i] && to[i])
+               if (from[i] && to[i])
                        memcpy(to[i], from[i], ptcache_data_size[i]);
        }
 }
@@ -1467,9 +1598,9 @@ static void ptcache_extra_free(PTCacheMem *pm)
 {
        PTCacheExtra *extra = pm->extradata.first;
 
-       if(extra) {
-               for(; extra; extra=extra->next) {
-                       if(extra->data)
+       if (extra) {
+               for (; extra; extra=extra->next) {
+                       if (extra->data)
                                MEM_freeN(extra->data);
                }
 
@@ -1478,11 +1609,11 @@ static void ptcache_extra_free(PTCacheMem *pm)
 }
 static int ptcache_old_elemsize(PTCacheID *pid)
 {
-       if(pid->type==PTCACHE_TYPE_SOFTBODY)
+       if (pid->type==PTCACHE_TYPE_SOFTBODY)
                return 6 * sizeof(float);
-       else if(pid->type==PTCACHE_TYPE_PARTICLES)
+       else if (pid->type==PTCACHE_TYPE_PARTICLES)
                return sizeof(ParticleKey);
-       else if(pid->type==PTCACHE_TYPE_CLOTH)
+       else if (pid->type==PTCACHE_TYPE_CLOTH)
                return 9 * sizeof(float);
 
        return 0;
@@ -1490,22 +1621,22 @@ static int ptcache_old_elemsize(PTCacheID *pid)
 
 static void ptcache_find_frames_around(PTCacheID *pid, unsigned int frame, int *fra1, int *fra2)
 {
-       if(pid->cache->flag & PTCACHE_DISK_CACHE) {
+       if (pid->cache->flag & PTCACHE_DISK_CACHE) {
                int cfra1=frame, cfra2=frame+1;
 
-               while(cfra1 >= pid->cache->startframe && !BKE_ptcache_id_exist(pid, cfra1))
+               while (cfra1 >= pid->cache->startframe && !BKE_ptcache_id_exist(pid, cfra1))
                        cfra1--;
 
-               if(cfra1 < pid->cache->startframe)
+               if (cfra1 < pid->cache->startframe)
                        cfra1 = 0;
 
-               while(cfra2 <= pid->cache->endframe && !BKE_ptcache_id_exist(pid, cfra2))
+               while (cfra2 <= pid->cache->endframe && !BKE_ptcache_id_exist(pid, cfra2))
                        cfra2++;
 
-               if(cfra2 > pid->cache->endframe)
+               if (cfra2 > pid->cache->endframe)
                        cfra2 = 0;
 
-               if(cfra1 && !cfra2) {
+               if (cfra1 && !cfra2) {
                        *fra1 = 0;
                        *fra2 = cfra1;
                }
@@ -1514,23 +1645,23 @@ static void ptcache_find_frames_around(PTCacheID *pid, unsigned int frame, int *
                        *fra2 = cfra2;
                }
        }
-       else if(pid->cache->mem_cache.first) {
+       else if (pid->cache->mem_cache.first) {
                PTCacheMem *pm = pid->cache->mem_cache.first;
                PTCacheMem *pm2 = pid->cache->mem_cache.last;
 
-               while(pm->next && pm->next->frame <= frame)
+               while (pm->next && pm->next->frame <= frame)
                        pm= pm->next;
 
-               if(pm2->frame < frame) {
+               if (pm2->frame < frame) {
                        pm2 = NULL;
                }
                else {
-                       while(pm2->prev && pm2->prev->frame > frame) {
+                       while (pm2->prev && pm2->prev->frame > frame) {
                                pm2= pm2->prev;
                        }
                }
 
-               if(!pm2) {
+               if (!pm2) {
                        *fra1 = 0;
                        *fra2 = pm->frame;
                }
@@ -1547,16 +1678,16 @@ static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
        PTCacheMem *pm = NULL;
        unsigned int i, error = 0;
 
-       if(pf == NULL)
+       if (pf == NULL)
                return NULL;
 
-       if(!ptcache_file_header_begin_read(pf))
+       if (!ptcache_file_header_begin_read(pf))
                error = 1;
 
-       if(!error && (pf->type != pid->type || !pid->read_header(pf)))
+       if (!error && (pf->type != pid->type || !pid->read_header(pf)))
                error = 1;
 
-       if(!error) {
+       if (!error) {
                pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
 
                pm->totpoint = pf->totpoint;
@@ -1565,19 +1696,19 @@ static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
 
                ptcache_data_alloc(pm);
 
-               if(pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
-                       for(i=0; i<BPHYS_TOT_DATA; i++) {
+               if (pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
+                       for (i=0; i<BPHYS_TOT_DATA; i++) {
                                unsigned int out_len = pm->totpoint*ptcache_data_size[i];
-                               if(pf->data_types & (1<<i))
-                                       ptcache_file_compressed_read(pf, (unsigned char*)(pm->data[i]), out_len);
+                               if (pf->data_types & (1<<i))
+                                       ptcache_file_compressed_read(pf, (unsigned char *)(pm->data[i]), out_len);
                        }
                }
                else {
                        BKE_ptcache_mem_pointers_init(pm);
                        ptcache_file_pointers_init(pf);
 
-                       for(i=0; i<pm->totpoint; i++) {
-                               if(!ptcache_file_data_read(pf)) {
+                       for (i=0; i<pm->totpoint; i++) {
+                               if (!ptcache_file_data_read(pf)) {
                                        error = 1;
                                        break;
                                }
@@ -1587,10 +1718,10 @@ static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
                }
        }
 
-       if(!error && pf->flag & PTCACHE_TYPEFLAG_EXTRADATA) {
+       if (!error && pf->flag & PTCACHE_TYPEFLAG_EXTRADATA) {
                unsigned int extratype = 0;
 
-               while(ptcache_file_read(pf, &extratype, 1, sizeof(unsigned int))) {
+               while (ptcache_file_read(pf, &extratype, 1, sizeof(unsigned int))) {
                        PTCacheExtra *extra = MEM_callocN(sizeof(PTCacheExtra), "Pointcache extradata");
 
                        extra->type = extratype;
@@ -1599,8 +1730,8 @@ static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
 
                        extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], "Pointcache extradata->data");
 
-                       if(pf->flag & PTCACHE_TYPEFLAG_COMPRESS)
-                               ptcache_file_compressed_read(pf, (unsigned char*)(extra->data), extra->totdata*ptcache_extra_datasize[extra->type]);
+                       if (pf->flag & PTCACHE_TYPEFLAG_COMPRESS)
+                               ptcache_file_compressed_read(pf, (unsigned char *)(extra->data), extra->totdata*ptcache_extra_datasize[extra->type]);
                        else
                                ptcache_file_read(pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
 
@@ -1608,7 +1739,7 @@ static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
                }
        }
 
-       if(error && pm) {
+       if (error && pm) {
                ptcache_data_free(pm);
                ptcache_extra_free(pm);
                MEM_freeN(pm);
@@ -1617,7 +1748,7 @@ static PTCacheMem *ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
 
        ptcache_file_close(pf);
 
-       if (error && G.f & G_DEBUG) 
+       if (error && G.debug & G_DEBUG)
                printf("Error reading from disk cache\n");
        
        return pm;
@@ -1631,8 +1762,8 @@ static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
 
        pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, pm->frame);
 
-       if(pf==NULL) {
-               if (G.f & G_DEBUG) 
+       if (pf==NULL) {
+               if (G.debug & G_DEBUG)
                        printf("Error opening disk cache file for writing\n");
                return 0;
        }
@@ -1642,22 +1773,22 @@ static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
        pf->type = pid->type;
        pf->flag = 0;
        
-       if(pm->extradata.first)
+       if (pm->extradata.first)
                pf->flag |= PTCACHE_TYPEFLAG_EXTRADATA;
        
-       if(pid->cache->compression)
+       if (pid->cache->compression)
                pf->flag |= PTCACHE_TYPEFLAG_COMPRESS;
 
-       if(!ptcache_file_header_begin_write(pf) || !pid->write_header(pf))
+       if (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf))
                error = 1;
 
-       if(!error) {
-               if(pid->cache->compression) {
-                       for(i=0; i<BPHYS_TOT_DATA; i++) {
-                               if(pm->data[i]) {
+       if (!error) {
+               if (pid->cache->compression) {
+                       for (i=0; i<BPHYS_TOT_DATA; i++) {
+                               if (pm->data[i]) {
                                        unsigned int in_len = pm->totpoint*ptcache_data_size[i];
                                        unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
-                                       ptcache_file_compressed_write(pf, (unsigned char*)(pm->data[i]), in_len, out, pid->cache->compression);
+                                       ptcache_file_compressed_write(pf, (unsigned char *)(pm->data[i]), in_len, out, pid->cache->compression);
                                        MEM_freeN(out);
                                }
                        }
@@ -1666,9 +1797,9 @@ static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
                        BKE_ptcache_mem_pointers_init(pm);
                        ptcache_file_pointers_init(pf);
 
-                       for(i=0; i<pm->totpoint; i++) {
+                       for (i=0; i<pm->totpoint; i++) {
                                ptcache_data_copy(pm->cur, pf->cur);
-                               if(!ptcache_file_data_write(pf)) {
+                               if (!ptcache_file_data_write(pf)) {
                                        error = 1;
                                        break;
                                }
@@ -1677,20 +1808,20 @@ static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
                }
        }
 
-       if(!error && pm->extradata.first) {
+       if (!error && pm->extradata.first) {
                PTCacheExtra *extra = pm->extradata.first;
 
-               for(; extra; extra=extra->next) {
-                       if(extra->data == NULL || extra->totdata == 0)
+               for (; extra; extra=extra->next) {
+                       if (extra->data == NULL || extra->totdata == 0)
                                continue;
 
                        ptcache_file_write(pf, &extra->type, 1, sizeof(unsigned int));
                        ptcache_file_write(pf, &extra->totdata, 1, sizeof(unsigned int));
 
-                       if(pid->cache->compression) {
+                       if (pid->cache->compression) {
                                unsigned int in_len = extra->totdata * ptcache_extra_datasize[extra->type];
                                unsigned char *out = (unsigned char *)MEM_callocN(LZO_OUT_LEN(in_len)*4, "pointcache_lzo_buffer");
-                               ptcache_file_compressed_write(pf, (unsigned char*)(extra->data), in_len, out, pid->cache->compression);
+                               ptcache_file_compressed_write(pf, (unsigned char *)(extra->data), in_len, out, pid->cache->compression);
                                MEM_freeN(out);
                        }
                        else {
@@ -1701,7 +1832,7 @@ static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
 
        ptcache_file_close(pf);
        
-       if (error && G.f & G_DEBUG) 
+       if (error && G.debug & G_DEBUG)
                printf("Error writing to disk cache\n");
 
        return error==0;
@@ -1712,25 +1843,25 @@ static int ptcache_read_stream(PTCacheID *pid, int cfra)
        PTCacheFile *pf = ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
        int error = 0;
 
-       if(pid->read_stream == NULL)
+       if (pid->read_stream == NULL)
                return 0;
 
-       if(pf == NULL) {
-               if (G.f & G_DEBUG) 
+       if (pf == NULL) {
+               if (G.debug & G_DEBUG)
                        printf("Error opening disk cache file for reading\n");
                return 0;
        }
 
-       if(!ptcache_file_header_begin_read(pf))
+       if (!ptcache_file_header_begin_read(pf))
                error = 1;
 
-       if(!error && (pf->type != pid->type || !pid->read_header(pf)))
+       if (!error && (pf->type != pid->type || !pid->read_header(pf)))
                error = 1;
 
-       if(!error && pf->totpoint != pid->totpoint(pid->calldata, cfra))
+       if (!error && pf->totpoint != pid->totpoint(pid->calldata, cfra))
                error = 1;
 
-       if(!error) {
+       if (!error) {
                ptcache_file_pointers_init(pf);
 
                // we have stream reading here
@@ -1749,27 +1880,27 @@ static int ptcache_read(PTCacheID *pid, int cfra)
        int *index = &i;
 
        /* get a memory cache to read from */
-       if(pid->cache->flag & PTCACHE_DISK_CACHE) {
+       if (pid->cache->flag & PTCACHE_DISK_CACHE) {
                pm = ptcache_disk_frame_to_mem(pid, cfra);
        }
        else {
                pm = pid->cache->mem_cache.first;
                
-               while(pm && pm->frame != cfra)
+               while (pm && pm->frame != cfra)
                        pm = pm->next;
        }
 
        /* read the cache */
-       if(pm) {
+       if (pm) {
                int totpoint = pm->totpoint;
 
-               if((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
+               if ((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
                        totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, cfra));
 
                BKE_ptcache_mem_pointers_init(pm);
 
-               for(i=0; i<totpoint; i++) {
-                       if(pm->data_types & (1<<BPHYS_DATA_INDEX))
+               for (i=0; i<totpoint; i++) {
+                       if (pm->data_types & (1<<BPHYS_DATA_INDEX))
                                index = pm->cur[BPHYS_DATA_INDEX];
 
                        pid->read_point(*index, pid->calldata, pm->cur, (float)pm->frame, NULL);
@@ -1777,11 +1908,11 @@ static int ptcache_read(PTCacheID *pid, int cfra)
                        BKE_ptcache_mem_pointers_incr(pm);
                }
 
-               if(pid->read_extra_data && pm->extradata.first)
+               if (pid->read_extra_data && pm->extradata.first)
                        pid->read_extra_data(pid->calldata, pm, (float)pm->frame);
 
                /* clean up temporary memory cache */
-               if(pid->cache->flag & PTCACHE_DISK_CACHE) {
+               if (pid->cache->flag & PTCACHE_DISK_CACHE) {
                        ptcache_data_free(pm);
                        ptcache_extra_free(pm);
                        MEM_freeN(pm);
@@ -1797,38 +1928,38 @@ static int ptcache_interpolate(PTCacheID *pid, float cfra, int cfra1, int cfra2)
        int *index = &i;
 
        /* get a memory cache to read from */
-       if(pid->cache->flag & PTCACHE_DISK_CACHE) {
+       if (pid->cache->flag & PTCACHE_DISK_CACHE) {
                pm = ptcache_disk_frame_to_mem(pid, cfra2);
        }
        else {
                pm = pid->cache->mem_cache.first;
                
-               while(pm && pm->frame != cfra2)
+               while (pm && pm->frame != cfra2)
                        pm = pm->next;
        }
 
        /* read the cache */
-       if(pm) {
+       if (pm) {
                int totpoint = pm->totpoint;
 
-               if((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
+               if ((pid->data_types & (1<<BPHYS_DATA_INDEX)) == 0)
                        totpoint = MIN2(totpoint, pid->totpoint(pid->calldata, (int)cfra));
 
                BKE_ptcache_mem_pointers_init(pm);
 
-               for(i=0; i<totpoint; i++) {
-                       if(pm->data_types & (1<<BPHYS_DATA_INDEX))
+               for (i=0; i<totpoint; i++) {
+                       if (pm->data_types & (1<<BPHYS_DATA_INDEX))
                                index = pm->cur[BPHYS_DATA_INDEX];
 
                        pid->interpolate_point(*index, pid->calldata, pm->cur, cfra, (float)cfra1, (float)cfra2, NULL);
                        BKE_ptcache_mem_pointers_incr(pm);
                }
 
-               if(pid->interpolate_extra_data && pm->extradata.first)
+               if (pid->interpolate_extra_data && pm->extradata.first)
                        pid->interpolate_extra_data(pid->calldata, pm, cfra, (float)cfra1, (float)cfra2);
 
                /* clean up temporary memory cache */
-               if(pid->cache->flag & PTCACHE_DISK_CACHE) {
+               if (pid->cache->flag & PTCACHE_DISK_CACHE) {
                        ptcache_data_free(pm);
                        ptcache_extra_free(pm);
                        MEM_freeN(pm);
@@ -1845,74 +1976,72 @@ int BKE_ptcache_read(PTCacheID *pid, float cfra)
        int ret = 0;
 
        /* nothing to read to */
-       if(pid->totpoint(pid->calldata, cfrai) == 0)
+       if (pid->totpoint(pid->calldata, cfrai) == 0)
                return 0;
 
-       if(pid->cache->flag & PTCACHE_READ_INFO) {
+       if (pid->cache->flag & PTCACHE_READ_INFO) {
                pid->cache->flag &= ~PTCACHE_READ_INFO;
                ptcache_read(pid, 0);
        }
 
        /* first check if we have the actual frame cached */
-       if(cfra == (float)cfrai && BKE_ptcache_id_exist(pid, cfrai))
+       if (cfra == (float)cfrai && BKE_ptcache_id_exist(pid, cfrai))
                cfra1 = cfrai;
 
        /* no exact cache frame found so try to find cached frames around cfra */
-       if(cfra1 == 0)
+       if (cfra1 == 0)
                ptcache_find_frames_around(pid, cfrai, &cfra1, &cfra2);
 
-       if(cfra1 == 0 && cfra2 == 0)
+       if (cfra1 == 0 && cfra2 == 0)
                return 0;
 
        /* don't read old cache if already simulated past cached frame */
-       if(cfra1 == 0 && cfra2 && cfra2 <= pid->cache->simframe)
+       if (cfra1 == 0 && cfra2 && cfra2 <= pid->cache->simframe)
                return 0;
-       if(cfra1 && cfra1 == cfra2)
+       if (cfra1 && cfra1 == cfra2)
                return 0;
 
-       if(cfra1) {
+       if (cfra1) {
                
-               if(pid->read_stream) {
+               if (pid->read_stream) {
                        if (!ptcache_read_stream(pid, cfra1))
                                return 0;
                }
-               else if(pid->read_point)
+               else if (pid->read_point)
                        ptcache_read(pid, cfra1);
        }
 
-       if(cfra2) {
+       if (cfra2) {
                
-               if(pid->read_stream) {
+               if (pid->read_stream) {
                        if (!ptcache_read_stream(pid, cfra2))
                                return 0;
                }
-               else if(pid->read_point) {
-                       if(cfra1 && cfra2 && pid->interpolate_point)
+               else if (pid->read_point) {
+                       if (cfra1 && cfra2 && pid->interpolate_point)
                                ptcache_interpolate(pid, cfra, cfra1, cfra2);
                        else
                                ptcache_read(pid, cfra2);
                }
        }
 
-       if(cfra1)
+       if (cfra1)
                ret = (cfra2 ? PTCACHE_READ_INTERPOLATED : PTCACHE_READ_EXACT);
-       else if(cfra2) {
+       else if (cfra2) {
                ret = PTCACHE_READ_OLD;
                pid->cache->simframe = cfra2;
        }
 
-       if((pid->cache->flag & PTCACHE_QUICK_CACHE)==0) {
-               cfrai = (int)cfra;
-               /* clear invalid cache frames so that better stuff can be simulated */
-               if(pid->cache->flag & PTCACHE_OUTDATED) {
-                       BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, cfrai);
-               }
-               else if(pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
-                       if(cfra <= pid->cache->last_exact)
-                               pid->cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
+       cfrai = (int)cfra;
+       /* clear invalid cache frames so that better stuff can be simulated */
+       if (pid->cache->flag & PTCACHE_OUTDATED) {
+               BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, cfrai);
+       }
+       else if (pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
+               if (cfra <= pid->cache->last_exact)
+                       pid->cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
 
-                       BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, MAX2(cfrai, pid->cache->last_exact));
-               }
+               BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, MAX2(cfrai, pid->cache->last_exact));
        }
 
        return ret;
@@ -1926,8 +2055,8 @@ static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
 
        pf = ptcache_file_open(pid, PTCACHE_FILE_WRITE, cfra);
 
-       if(pf==NULL) {
-               if (G.f & G_DEBUG) 
+       if (pf==NULL) {
+               if (G.debug & G_DEBUG)
                        printf("Error opening disk cache file for writing\n");
                return 0;
        }
@@ -1937,15 +2066,15 @@ static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
        pf->type = pid->type;
        pf->flag = 0;
 
-       if(!error && (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf)))
+       if (!error && (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf)))
                error = 1;
 
-       if(!error && pid->write_stream)
+       if (!error && pid->write_stream)
                pid->write_stream(pf, pid->calldata);
 
        ptcache_file_close(pf);
 
-       if (error && G.f & G_DEBUG) 
+       if (error && G.debug & G_DEBUG)
                printf("Error writing to disk cache\n");
 
        return error == 0;
@@ -1965,11 +2094,11 @@ static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
        ptcache_data_alloc(pm);
        BKE_ptcache_mem_pointers_init(pm);
 
-       if(overwrite) {
-               if(cache->flag & PTCACHE_DISK_CACHE) {
+       if (overwrite) {
+               if (cache->flag & PTCACHE_DISK_CACHE) {
                        int fra = cfra-1;
 
-                       while(fra >= cache->startframe && !BKE_ptcache_id_exist(pid, fra))
+                       while (fra >= cache->startframe && !BKE_ptcache_id_exist(pid, fra))
                                fra--;
                        
                        pm2 = ptcache_disk_frame_to_mem(pid, fra);
@@ -1978,35 +2107,35 @@ static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
                        pm2 = cache->mem_cache.last;
        }
 
-       if(pid->write_point) {
-               for(i=0; i<totpoint; i++) {
+       if (pid->write_point) {
+               for (i=0; i<totpoint; i++) {
                        int write = pid->write_point(i, pid->calldata, pm->cur, cfra);
-                       if(write) {
+                       if (write) {
                                BKE_ptcache_mem_pointers_incr(pm);
 
                                /* newly born particles have to be copied to previous cached frame */
-                               if(overwrite && write == 2 && pm2 && BKE_ptcache_mem_pointers_seek(i, pm2))
+                               if (overwrite && write == 2 && pm2 && BKE_ptcache_mem_pointers_seek(i, pm2))
                                        pid->write_point(i, pid->calldata, pm2->cur, cfra);
                        }
                }
        }
 
-       if(pid->write_extra_data)
+       if (pid->write_extra_data)
                pid->write_extra_data(pid->calldata, pm, cfra);
 
        pm->frame = cfra;
 
-       if(cache->flag & PTCACHE_DISK_CACHE) {
+       if (cache->flag & PTCACHE_DISK_CACHE) {
                error += !ptcache_mem_frame_to_disk(pid, pm);
 
-               // if(pm) /* pm is always set */
+               // if (pm) /* pm is always set */
                {
                        ptcache_data_free(pm);
                        ptcache_extra_free(pm);
                        MEM_freeN(pm);
                }
 
-               if(pm2) {
+               if (pm2) {
                        error += !ptcache_mem_frame_to_disk(pid, pm2);
                        ptcache_data_free(pm2);
                        ptcache_extra_free(pm2);
@@ -2025,40 +2154,40 @@ static int ptcache_write_needed(PTCacheID *pid, int cfra, int *overwrite)
        int ofra = 0, efra = cache->endframe;
 
        /* allways start from scratch on the first frame */
-       if(cfra && cfra == cache->startframe) {
+       if (cfra && cfra == cache->startframe) {
                BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, cfra);
                cache->flag &= ~PTCACHE_REDO_NEEDED;
                return 1;
        }
 
-       if(pid->cache->flag & PTCACHE_DISK_CACHE) {
-               if(cfra==0 && cache->startframe > 0)
+       if (pid->cache->flag & PTCACHE_DISK_CACHE) {
+               if (cfra==0 && cache->startframe > 0)
                        return 1;
 
                                /* find last cached frame */
-               while(efra > cache->startframe && !BKE_ptcache_id_exist(pid, efra))
+               while (efra > cache->startframe && !BKE_ptcache_id_exist(pid, efra))
                        efra--;
 
                /* find second last cached frame */
                ofra = efra-1;
-               while(ofra > cache->startframe && !BKE_ptcache_id_exist(pid, ofra))
+               while (ofra > cache->startframe && !BKE_ptcache_id_exist(pid, ofra))
                        ofra--;
        }
        else {
                PTCacheMem *pm = cache->mem_cache.last;
                /* don't write info file in memory */
-               if(cfra == 0)
+               if (cfra == 0)
                        return 0;
 
-               if(pm == NULL)
+               if (pm == NULL)
                        return 1;
 
                efra = pm->frame;
                ofra = (pm->prev ? pm->prev->frame : efra - cache->step);
        }
 
-       if(efra >= cache->startframe && cfra > efra) {
-               if(ofra >= cache->startframe && efra - ofra < cache->step) {
+       if (efra >= cache->startframe && cfra > efra) {
+               if (ofra >= cache->startframe && efra - ofra < cache->step) {
                        /* overwrite previous frame */
                        BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_FRAME, efra);
                        *overwrite = 1;
@@ -2075,30 +2204,30 @@ int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
        int totpoint = pid->totpoint(pid->calldata, cfra);
        int overwrite = 0, error = 0;
 
-       if(totpoint == 0 || (cfra ? pid->data_types == 0 : pid->info_types == 0))
+       if (totpoint == 0 || (cfra ? pid->data_types == 0 : pid->info_types == 0))
                return 0;
 
-       if(ptcache_write_needed(pid, cfra, &overwrite)==0)
+       if (ptcache_write_needed(pid, cfra, &overwrite)==0)
                return 0;
 
-       if(pid->write_stream) {
+       if (pid->write_stream) {
                ptcache_write_stream(pid, cfra, totpoint);
        }
-       else if(pid->write_point) {
+       else if (pid->write_point) {
                error += ptcache_write(pid, cfra, overwrite);
        }
 
        /* Mark frames skipped if more than 1 frame forwards since last non-skipped frame. */
-       if(cfra - cache->last_exact == 1 || cfra == cache->startframe) {
+       if (cfra - cache->last_exact == 1 || cfra == cache->startframe) {
                cache->last_exact = cfra;
                cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
        }
        /* Don't mark skipped when writing info file (frame 0) */
-       else if(cfra)
+       else if (cfra)
                cache->flag |= PTCACHE_FRAMES_SKIPPED;
 
        /* Update timeline cache display */
-       if(cfra && cache->cached_frames)
+       if (cfra && cache->cached_frames)
                cache->cached_frames[cfra-cache->startframe] = 1;
 
        BKE_ptcache_update_info(pid);
@@ -2107,8 +2236,8 @@ int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
 }
 /* youll need to close yourself after!
  * mode - PTCACHE_CLEAR_ALL, 
+ */
 
-*/
 /* Clears & resets */
 void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
 {
@@ -2123,7 +2252,7 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
        char path_full[MAX_PTCACHE_FILE];
        char ext[MAX_PTCACHE_PATH];
 
-       if(!pid || !pid->cache || pid->cache->flag & PTCACHE_BAKED)
+       if (!pid || !pid->cache || pid->cache->flag & PTCACHE_BAKED)
                return;
 
        if (pid->cache->flag & PTCACHE_IGNORE_CLEAR)
@@ -2134,7 +2263,7 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
 
 #ifndef DURIAN_POINTCACHE_LIB_OK
        /* don't allow clearing for linked objects */
-       if(pid->ob->id.lib)
+       if (pid->ob->id.lib)
                return;
 #endif
 
@@ -2143,9 +2272,9 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
        /* clear all files in the temp dir with the prefix of the ID and the ".bphys" suffix */
        switch (mode) {
        case PTCACHE_CLEAR_ALL:
-       case PTCACHE_CLEAR_BEFORE:      
+       case PTCACHE_CLEAR_BEFORE:
        case PTCACHE_CLEAR_AFTER:
-               if(pid->cache->flag & PTCACHE_DISK_CACHE) {
+               if (pid->cache->flag & PTCACHE_DISK_CACHE) {
                        ptcache_path(pid, path);
                        
                        len = ptcache_filename(pid, filename, cfra, 0, 0); /* no path */
@@ -2163,7 +2292,8 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
                                                        pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
                                                        BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
                                                        BLI_delete(path_full, 0, 0);
-                                               } else {
+                                               }
+                                               else {
                                                        /* read the number of the file */
                                                        unsigned int frame, len2 = (int)strlen(de->d_name);
                                                        char num[7];
@@ -2172,12 +2302,13 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
                                                                BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
                                                                frame = atoi(num);
                                                                
-                                                               if((mode==PTCACHE_CLEAR_BEFORE && frame < cfra) || 
-                                                               (mode==PTCACHE_CLEAR_AFTER && frame > cfra)     ) {
+                                                               if ((mode == PTCACHE_CLEAR_BEFORE && frame < cfra) ||
+                                                                   (mode == PTCACHE_CLEAR_AFTER && frame > cfra))
+                                                               {
                                                                        
                                                                        BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
                                                                        BLI_delete(path_full, 0, 0);
-                                                                       if(pid->cache->cached_frames && frame >=sta && frame <= end)
+                                                                       if (pid->cache->cached_frames && frame >=sta && frame <= end)
                                                                                pid->cache->cached_frames[frame-sta] = 0;
                                                                }
                                                        }
@@ -2187,30 +2318,32 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
                        }
                        closedir(dir);
 
-                       if(mode == PTCACHE_CLEAR_ALL && pid->cache->cached_frames)
+                       if (mode == PTCACHE_CLEAR_ALL && pid->cache->cached_frames)
                                memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
                }
                else {
                        PTCacheMem *pm= pid->cache->mem_cache.first;
                        PTCacheMem *link= NULL;
 
-                       if(mode == PTCACHE_CLEAR_ALL) {
+                       if (mode == PTCACHE_CLEAR_ALL) {
                                /*we want startframe if the cache starts before zero*/
                                pid->cache->last_exact = MIN2(pid->cache->startframe, 0);
-                               for(; pm; pm=pm->next) {
+                               for (; pm; pm=pm->next) {
                                        ptcache_data_free(pm);
                                        ptcache_extra_free(pm);
                                }
                                BLI_freelistN(&pid->cache->mem_cache);
 
-                               if(pid->cache->cached_frames) 
+                               if (pid->cache->cached_frames) 
                                        memset(pid->cache->cached_frames, 0, MEM_allocN_len(pid->cache->cached_frames));
-                       } else {
-                               while(pm) {
-                                       if((mode==PTCACHE_CLEAR_BEFORE && pm->frame < cfra)     || 
-                                       (mode==PTCACHE_CLEAR_AFTER && pm->frame > cfra) ) {
+                       }
+                       else {
+                               while (pm) {
+                                       if ((mode == PTCACHE_CLEAR_BEFORE && pm->frame < cfra) ||
+                                           (mode == PTCACHE_CLEAR_AFTER && pm->frame > cfra))
+                                       {
                                                link = pm;
-                                               if(pid->cache->cached_frames && pm->frame >=sta && pm->frame <= end)
+                                               if (pid->cache->cached_frames && pm->frame >=sta && pm->frame <= end)
                                                        pid->cache->cached_frames[pm->frame-sta] = 0;
                                                ptcache_data_free(pm);
                                                ptcache_extra_free(pm);
@@ -2225,8 +2358,8 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
                break;
                
        case PTCACHE_CLEAR_FRAME:
-               if(pid->cache->flag & PTCACHE_DISK_CACHE) {
-                       if(BKE_ptcache_id_exist(pid, cfra)) {
+               if (pid->cache->flag & PTCACHE_DISK_CACHE) {
+                       if (BKE_ptcache_id_exist(pid, cfra)) {
                                ptcache_filename(pid, filename, cfra, 1, 1); /* no path */
                                BLI_delete(filename, 0, 0);
                        }
@@ -2234,8 +2367,8 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
                else {
                        PTCacheMem *pm = pid->cache->mem_cache.first;
 
-                       for(; pm; pm=pm->next) {
-                               if(pm->frame == cfra) {
+                       for (; pm; pm=pm->next) {
+                               if (pm->frame == cfra) {
                                        ptcache_data_free(pm);
                                        ptcache_extra_free(pm);
                                        BLI_freelinkN(&pid->cache->mem_cache, pm);
@@ -2243,7 +2376,7 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
                                }
                        }
                }
-               if(pid->cache->cached_frames && cfra>=sta && cfra<=end)
+               if (pid->cache->cached_frames && cfra >= sta && cfra <= end)
                        pid->cache->cached_frames[cfra-sta] = 0;
                break;
        }
@@ -2252,16 +2385,16 @@ void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
 }
 int  BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
 {
-       if(!pid->cache)
+       if (!pid->cache)
                return 0;
 
-       if(cfra<pid->cache->startframe || cfra > pid->cache->endframe)
+       if (cfra<pid->cache->startframe || cfra > pid->cache->endframe)
                return 0;
 
-       if(pid->cache->cached_frames && pid->cache->cached_frames[cfra-pid->cache->startframe]==0)
+       if (pid->cache->cached_frames &&        pid->cache->cached_frames[cfra-pid->cache->startframe]==0)
                return 0;
        
-       if(pid->cache->flag & PTCACHE_DISK_CACHE) {
+       if (pid->cache->flag & PTCACHE_DISK_CACHE) {
                char filename[MAX_PTCACHE_FILE];
                
                ptcache_filename(pid, filename, cfra, 1, 1);
@@ -2271,8 +2404,8 @@ int  BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
        else {
                PTCacheMem *pm = pid->cache->mem_cache.first;
 
-               for(; pm; pm=pm->next) {
-                       if(pm->frame==cfra)
+               for (; pm; pm=pm->next) {
+                       if (pm->frame==cfra)
                                return 1;
                }
                return 0;
@@ -2285,14 +2418,14 @@ void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startfra
        /* float offset; unused for now */
        float time, nexttime;
 
-       /* TODO: this has to be sorter out once bsystem_time gets redone, */
+       /* TODO: this has to be sorted out once bsystem_time gets redone, */
        /*       now caches can handle interpolating etc. too - jahka */
 
        /* time handling for point cache:
         * - simulation time is scaled by result of bsystem_time
         * - for offsetting time only time offset is taken into account, since
         *   that's always the same and can't be animated. a timeoffset which
-        *   varies over time is not simpe to support.
+        *   varies over time is not simple to support.
         * - field and motion blur offsets are currently ignored, proper solution
         *   is probably to interpolate results from two frames for that ..
         */
@@ -2300,14 +2433,14 @@ void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startfra
        /* ob= pid->ob; */ /* UNUSED */
        cache= pid->cache;
 
-       if(timescale) {
-               time= BKE_curframe(scene);
-               nexttime= BKE_frame_to_ctime(scene, CFRA + 1.0f);
+       if (timescale) {
+               time= BKE_scene_frame_get(scene);
+               nexttime = BKE_scene_frame_get_from_ctime(scene, CFRA + 1.0f);
                
                *timescale= MAX2(nexttime - time, 0.0f);
        }
 
-       if(startframe && endframe) {
+       if (startframe && endframe) {
                *startframe= cache->startframe;
                *endframe= cache->endframe;
 
@@ -2326,20 +2459,20 @@ void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startfra
        }
 
        /* verify cached_frames array is up to date */
-       if(cache->cached_frames) {
-               if(MEM_allocN_len(cache->cached_frames) != sizeof(char) * (cache->endframe-cache->startframe+1)) {
+       if (cache->cached_frames) {
+               if (MEM_allocN_len(cache->cached_frames) != sizeof(char) * (cache->endframe-cache->startframe+1)) {
                        MEM_freeN(cache->cached_frames);
                        cache->cached_frames = NULL;
-               }       
+               }
        }
 
-       if(cache->cached_frames==NULL && cache->endframe > cache->startframe) {
+       if (cache->cached_frames==NULL && cache->endframe > cache->startframe) {
                unsigned int sta=cache->startframe;
                unsigned int end=cache->endframe;
 
                cache->cached_frames = MEM_callocN(sizeof(char) * (cache->endframe-cache->startframe+1), "cached frames array");
 
-               if(pid->cache->flag & PTCACHE_DISK_CACHE) {
+               if (pid->cache->flag & PTCACHE_DISK_CACHE) {
                        /* mode is same as fopen's modes */
                        DIR *dir; 
                        struct dirent *de;
@@ -2369,7 +2502,7 @@ void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startfra
                                                        BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
                                                        frame = atoi(num);
                                                        
-                                                       if(frame >= sta && frame <= end)
+                                                       if (frame >= sta && frame <= end)
                                                                cache->cached_frames[frame-sta] = 1;
                                                }
                                        }
@@ -2380,8 +2513,8 @@ void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startfra
                else {
                        PTCacheMem *pm= pid->cache->mem_cache.first;
 
-                       while(pm) {
-                               if(pm->frame >= sta && pm->frame <= end)
+                       while (pm) {
+                               if (pm->frame >= sta && pm->frame <= end)
                                        cache->cached_frames[pm->frame-sta] = 1;
                                pm = pm->next;
                        }
@@ -2393,7 +2526,7 @@ int  BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode)
        PointCache *cache;
        int reset, clear, after;
 
-       if(!pid->cache)
+       if (!pid->cache)
                return 0;
 
        cache= pid->cache;
@@ -2401,53 +2534,53 @@ int  BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode)
        clear= 0;
        after= 0;
 
-       if(mode == PTCACHE_RESET_DEPSGRAPH) {
-               if(!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) {
-                       if(cache->flag & PTCACHE_QUICK_CACHE)
-                               clear= 1;
+       if (mode == PTCACHE_RESET_DEPSGRAPH) {
+               if (!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) {
 
                        after= 1;
                }
 
                cache->flag |= PTCACHE_OUTDATED;
        }
-       else if(mode == PTCACHE_RESET_BAKED) {
-               if(!BKE_ptcache_get_continue_physics()) {
+       else if (mode == PTCACHE_RESET_BAKED) {
+               if (!BKE_ptcache_get_continue_physics()) {
                        reset= 1;
                        clear= 1;
                }
                else
                        cache->flag |= PTCACHE_OUTDATED;
        }
-       else if(mode == PTCACHE_RESET_OUTDATED) {
+       else if (mode == PTCACHE_RESET_OUTDATED) {
                reset = 1;
 
-               if(cache->flag & PTCACHE_OUTDATED && !(cache->flag & PTCACHE_BAKED)) {
+               if (cache->flag & PTCACHE_OUTDATED && !(cache->flag & PTCACHE_BAKED)) {
                        clear= 1;
                        cache->flag &= ~PTCACHE_OUTDATED;
                }
        }
 
-       if(reset) {
+       if (reset) {
                BKE_ptcache_invalidate(cache);
                cache->flag &= ~PTCACHE_REDO_NEEDED;
 
-               if(pid->type == PTCACHE_TYPE_CLOTH)
+               if (pid->type == PTCACHE_TYPE_CLOTH)
                        cloth_free_modifier(pid->calldata);
-               else if(pid->type == PTCACHE_TYPE_SOFTBODY)
+               else if (pid->type == PTCACHE_TYPE_SOFTBODY)
                        sbFreeSimulation(pid->calldata);
-               else if(pid->type == PTCACHE_TYPE_PARTICLES)
+               else if (pid->type == PTCACHE_TYPE_PARTICLES)
                        psys_reset(pid->calldata, PSYS_RESET_DEPSGRAPH);
-               else if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
+#if 0
+               else if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
                        smokeModifier_reset(pid->calldata);
-               else if(pid->type == PTCACHE_TYPE_SMOKE_HIGHRES)
+               else if (pid->type == PTCACHE_TYPE_SMOKE_HIGHRES)
                        smokeModifier_reset_turbulence(pid->calldata);
-               else if(pid->type == PTCACHE_TYPE_DYNAMICPAINT)
+#endif
+               else if (pid->type == PTCACHE_TYPE_DYNAMICPAINT)
                        dynamicPaint_clearSurface((DynamicPaintSurface*)pid->calldata);
        }
-       if(clear)
+       if (clear)
                BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
-       else if(after)
+       else if (after)
                BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, CFRA);
 
        return (reset || clear || after);
@@ -2462,48 +2595,46 @@ int  BKE_ptcache_object_reset(Scene *scene, Object *ob, int mode)
        reset= 0;
        skip= 0;
 
-       if(ob->soft) {
+       if (ob->soft) {
                BKE_ptcache_id_from_softbody(&pid, ob, ob->soft);
                reset |= BKE_ptcache_id_reset(scene, &pid, mode);
        }
 
-       for(psys=ob->particlesystem.first; psys; psys=psys->next) {
-               /* children or just redo can be calculated without reseting anything */
-               if(psys->recalc & PSYS_RECALC_REDO || psys->recalc & PSYS_RECALC_CHILD)
+       for (psys=ob->particlesystem.first; psys; psys=psys->next) {
+               /* children or just redo can be calculated without resetting anything */
+               if (psys->recalc & PSYS_RECALC_REDO || psys->recalc & PSYS_RECALC_CHILD)
                        skip = 1;
                /* Baked cloth hair has to be checked too, because we don't want to reset */
                /* particles or cloth in that case -jahka */
-               else if(psys->clmd) {
+               else if (psys->clmd) {
                        BKE_ptcache_id_from_cloth(&pid, ob, psys->clmd);
-                       if(mode == PSYS_RESET_ALL || !(psys->part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED))) 
+                       if (mode == PSYS_RESET_ALL || !(psys->part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED))) 
                                reset |= BKE_ptcache_id_reset(scene, &pid, mode);
                        else
                                skip = 1;
                }
 
-               if(skip == 0 && psys->part) {
+               if (skip == 0 && psys->part) {
                        BKE_ptcache_id_from_particles(&pid, ob, psys);
                        reset |= BKE_ptcache_id_reset(scene, &pid, mode);
                }
        }
 
-       for(md=ob->modifiers.first; md; md=md->next) {
-               if(md->type == eModifierType_Cloth) {
+       for (md=ob->modifiers.first; md; md=md->next) {
+               if (md->type == eModifierType_Cloth) {
                        BKE_ptcache_id_from_cloth(&pid, ob, (ClothModifierData*)md);
                        reset |= BKE_ptcache_id_reset(scene, &pid, mode);
                }
-               if(md->type == eModifierType_Smoke) {
+               if (md->type == eModifierType_Smoke) {
                        SmokeModifierData *smd = (SmokeModifierData *)md;
-                       if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
-                       {
+                       if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
                                BKE_ptcache_id_from_smoke(&pid, ob, (SmokeModifierData*)md);
                                reset |= BKE_ptcache_id_reset(scene, &pid, mode);
                        }
                }
-               if(md->type == eModifierType_DynamicPaint) {
+               if (md->type == eModifierType_DynamicPaint) {
                        DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
-                       if(pmd->canvas)
-                       {
+                       if (pmd->canvas) {
                                DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
 
                                for (; surface; surface=surface->next) {
@@ -2540,18 +2671,21 @@ void BKE_ptcache_remove(void)
                        return;
                
                while ((de = readdir(dir)) != NULL) {
-                       if( strcmp(de->d_name, ".")==0 || strcmp(de->d_name, "..")==0) {
+                       if ( strcmp(de->d_name, ".")==0 || strcmp(de->d_name, "..")==0) {
                                /* do nothing */
-                       } else if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
+                       }
+                       else if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
                                BLI_join_dirfile(path_full, sizeof(path_full), path, de->d_name);
                                BLI_delete(path_full, 0, 0);
-                       } else {
-                               rmdir = 0; /* unknown file, dont remove the dir */
+                       }
+                       else {
+                               rmdir = 0; /* unknown file, don't remove the dir */
                        }
                }
 
                closedir(dir);
-       } else { 
+       }
+       else {
                rmdir = 0; /* path dosnt exist  */
        }
        
@@ -2568,12 +2702,12 @@ void BKE_ptcache_set_continue_physics(Main *bmain, Scene *scene, int enable)
 {
        Object *ob;
 
-       if(CONTINUE_PHYSICS != enable) {
+       if (CONTINUE_PHYSICS != enable) {
                CONTINUE_PHYSICS = enable;
 
-               if(CONTINUE_PHYSICS == 0) {
-                       for(ob=bmain->object.first; ob; ob=ob->id.next)
-                               if(BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_OUTDATED))
+               if (CONTINUE_PHYSICS == 0) {
+                       for (ob=bmain->object.first; ob; ob=ob->id.next)
+                               if (BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_OUTDATED))
                                        DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
                }
        }
@@ -2605,8 +2739,8 @@ void BKE_ptcache_free_mem(ListBase *mem_cache)
 {
        PTCacheMem *pm = mem_cache->first;
 
-       if(pm) {
-               for(; pm; pm=pm->next) {
+       if (pm) {
+               for (; pm; pm=pm->next) {
                        ptcache_data_free(pm);
                        ptcache_extra_free(pm);
                }
@@ -2617,9 +2751,9 @@ void BKE_ptcache_free_mem(ListBase *mem_cache)
 void BKE_ptcache_free(PointCache *cache)
 {
        BKE_ptcache_free_mem(&cache->mem_cache);
-       if(cache->edit && cache->free_edit)
+       if (cache->edit && cache->free_edit)
                cache->free_edit(cache->edit);
-       if(cache->cached_frames)
+       if (cache->cached_frames)
                MEM_freeN(cache->cached_frames);
        MEM_freeN(cache);
 }
@@ -2627,43 +2761,73 @@ void BKE_ptcache_free_list(ListBase *ptcaches)
 {
        PointCache *cache = ptcaches->first;
 
-       while(cache) {
+       while (cache) {
                BLI_remlink(ptcaches, cache);
                BKE_ptcache_free(cache);
                cache = ptcaches->first;
        }
 }
 
-static PointCache *ptcache_copy(PointCache *cache)
+static PointCache *ptcache_copy(PointCache *cache, int copy_data)
 {
        PointCache *ncache;
 
        ncache= MEM_dupallocN(cache);
 
-       /* hmm, should these be copied over instead? */
        ncache->mem_cache.first = NULL;
        ncache->mem_cache.last = NULL;
-       ncache->cached_frames = NULL;
-       ncache->edit = NULL;
 
-       ncache->flag= 0;
-       ncache->simframe= 0;
+       if (copy_data == FALSE) {
+               ncache->mem_cache.first = NULL;
+               ncache->mem_cache.last = NULL;
+               ncache->cached_frames = NULL;
+
+               /* flag is a mix of user settings and simulator/baking state */
+               ncache->flag= ncache->flag & (PTCACHE_DISK_CACHE|PTCACHE_EXTERNAL|PTCACHE_IGNORE_LIBPATH);
+               ncache->simframe= 0;
+       }
+       else {
+               PTCacheMem *pm;
+
+               for (pm = cache->mem_cache.first; pm; pm = pm->next) {
+                       PTCacheMem *pmn = MEM_dupallocN(pm);
+                       int i;
+
+                       for (i = 0; i < BPHYS_TOT_DATA; i++) {
+                               if (pmn->data[i])
+                                       pmn->data[i] = MEM_dupallocN(pm->data[i]);
+                       }
+
+                       BKE_ptcache_mem_pointers_init(pm);
+
+                       BLI_addtail(&ncache->mem_cache, pmn);
+               }
+
+               if (ncache->cached_frames)
+                       ncache->cached_frames = MEM_dupallocN(cache->cached_frames);
+       }
+
+       /* hmm, should these be copied over instead? */
+       ncache->edit = NULL;
 
        return ncache;
 }
+
 /* returns first point cache */
-PointCache *BKE_ptcache_copy_list(ListBase *ptcaches_new, ListBase *ptcaches_old)
+PointCache *BKE_ptcache_copy_list(ListBase *ptcaches_new, ListBase *ptcaches_old, int copy_data)
 {
        PointCache *cache = ptcaches_old->first;
 
        ptcaches_new->first = ptcaches_new->last = NULL;
 
-       for(; cache; cache=cache->next)
-               BLI_addtail(ptcaches_new, ptcache_copy(cache));
+       for (; cache; cache=cache->next)
+               BLI_addtail(ptcaches_new, ptcache_copy(cache, copy_data));
 
        return ptcaches_new->first;
 }
 
+/* Disabled this code; this is being called on scene_update_tagged, and that in turn gets called on 
+ * every user action changing stuff, and then it runs a complete bake??? (ton) */
 
 /* Baking */
 void BKE_ptcache_quick_cache_all(Main *bmain, Scene *scene)
@@ -2687,7 +2851,7 @@ void BKE_ptcache_quick_cache_all(Main *bmain, Scene *scene)
 }
 
 /* Simulation thread, no need for interlocks as data written in both threads
- are only unitary integers (I/O assumed to be atomic for them) */
are only unitary integers (I/O assumed to be atomic for them) */
 typedef struct {
        int break_operation;
        int thread_ended;
@@ -2700,8 +2864,8 @@ typedef struct {
 
 static void ptcache_dt_to_str(char *str, double dtime)
 {
-       if(dtime > 60.0) {
-               if(dtime > 3600.0)
+       if (dtime > 60.0) {
+               if (dtime > 3600.0)
                        sprintf(str, "%ih %im %is", (int)(dtime/3600), ((int)(dtime/60))%60, ((int)dtime) % 60);
                else
                        sprintf(str, "%im %is", ((int)(dtime/60))%60, ((int)dtime) % 60);
@@ -2712,7 +2876,7 @@ static void ptcache_dt_to_str(char *str, double dtime)
 
 static void *ptcache_bake_thread(void *ptr)
 {
-       int usetimer = 0, sfra, efra;
+       int use_timer = FALSE, sfra, efra;
        double stime, ptime, ctime, fetd;
        char run[32], cur[32], etd[32];
 
@@ -2722,9 +2886,9 @@ static void *ptcache_bake_thread(void *ptr)
        sfra = *data->cfra_ptr;
        efra = data->endframe;
 
-       for(; (*data->cfra_ptr <= data->endframe) && !data->break_operation; *data->cfra_ptr+=data->step) {
-               scene_update_for_newframe(data->main, data->scene, data->scene->lay);
-               if(G.background) {
+       for (; (*data->cfra_ptr <= data->endframe) && !data->break_operation; *data->cfra_ptr+=data->step) {
+               BKE_scene_update_for_newframe(data->main, data->scene, data->scene->lay);
+               if (G.background) {
                        printf("bake: frame %d :: %d\n", (int)*data->cfra_ptr, data->endframe);
                }
                else {
@@ -2732,22 +2896,22 @@ static void *ptcache_bake_thread(void *ptr)
 
                        fetd = (ctime-ptime)*(efra-*data->cfra_ptr)/data->step;
 
-                       if(usetimer || fetd > 60.0) {
-                               usetimer = 1;
+                       if (use_timer || fetd > 60.0) {
+                               use_timer = TRUE;
 
                                ptcache_dt_to_str(cur, ctime-ptime);
                                ptcache_dt_to_str(run, ctime-stime);
                                ptcache_dt_to_str(etd, fetd);
 
-                               printf("Baked for %s, current frame: %i/%i (%.3fs), ETC: %s          \r", run, *data->cfra_ptr-sfra+1, efra-sfra+1, ctime-ptime, etd);
+                               printf("Baked for %s, current frame: %i/%i (%.3fs), ETC: %s\r", run, *data->cfra_ptr-sfra+1, efra-sfra+1, ctime-ptime, etd);
                        }
                        ptime = ctime;
                }
        }
 
-       if(usetimer) {
+       if (use_timer) {
                ptcache_dt_to_str(run, PIL_check_seconds_timer()-stime);
-               printf("Bake %s %s (%i frames simulated).                       \n", (data->break_operation ? "canceled after" : "finished in"), run, *data->cfra_ptr-sfra);
+               printf("Bake %s %s (%i frames simulated).\n", (data->break_operation ? "canceled after" : "finished in"), run, *data->cfra_ptr-sfra);
        }
 
        data->thread_ended = TRUE;
@@ -2779,32 +2943,31 @@ void BKE_ptcache_bake(PTCacheBaker* baker)
        thread_data.scene = baker->scene;
        thread_data.main = baker->main;
 
-       G.afbreek = 0;
+       G.is_break = FALSE;
 
        /* set caches to baking mode and figure out start frame */
-       if(pid) {
+       if (pid) {
                /* cache/bake a single object */
                cache = pid->cache;
-               if((cache->flag & PTCACHE_BAKED)==0) {
-                       if(pid->type==PTCACHE_TYPE_PARTICLES) {
+               if ((cache->flag & PTCACHE_BAKED)==0) {
+                       if (pid->type==PTCACHE_TYPE_PARTICLES) {
                                ParticleSystem *psys= pid->calldata;
 
                                /* a bit confusing, could make this work better in the UI */
-                               if(psys->part->type == PART_EMITTER)
+                               if (psys->part->type == PART_EMITTER)
                                        psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
                        }
-                       else if(pid->type == PTCACHE_TYPE_SMOKE_HIGHRES) {
+                       else if (pid->type == PTCACHE_TYPE_SMOKE_HIGHRES) {
                                /* get all pids from the object and search for smoke low res */
                                ListBase pidlist2;
                                PTCacheID *pid2;
                                BKE_ptcache_ids_from_object(&pidlist2, pid->ob, scene, MAX_DUPLI_RECUR);
-                               for(pid2=pidlist2.first; pid2; pid2=pid2->next) {
-                                       if(pid2->type == PTCACHE_TYPE_SMOKE_DOMAIN) 
-                                       {
-                                               if(pid2->cache && !(pid2->cache->flag & PTCACHE_BAKED)) {
-                                                       if(bake || pid2->cache->flag & PTCACHE_REDO_NEEDED)
+                               for (pid2=pidlist2.first; pid2; pid2=pid2->next) {
+                                       if (pid2->type == PTCACHE_TYPE_SMOKE_DOMAIN) {
+                                               if (pid2->cache && !(pid2->cache->flag & PTCACHE_BAKED)) {
+                                                       if (bake || pid2->cache->flag & PTCACHE_REDO_NEEDED)
                                                                BKE_ptcache_id_clear(pid2, PTCACHE_CLEAR_ALL, 0);
-                                                       if(bake) {
+                                                       if (bake) {
                                                                pid2->cache->flag |= PTCACHE_BAKING;
                                                                pid2->cache->flag &= ~PTCACHE_BAKED;
                                                        }
@@ -2814,12 +2977,12 @@ void BKE_ptcache_bake(PTCacheBaker* baker)
                                BLI_freelistN(&pidlist2);
                        }
 
-                       if(bake || cache->flag & PTCACHE_REDO_NEEDED)
+                       if (bake || cache->flag & PTCACHE_REDO_NEEDED)
                                BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
 
                        startframe = MAX2(cache->last_exact, cache->startframe);
 
-                       if(bake) {
+                       if (bake) {
                                thread_data.endframe = cache->endframe;
                                cache->flag |= PTCACHE_BAKING;
                        }
@@ -2830,40 +2993,44 @@ void BKE_ptcache_bake(PTCacheBaker* baker)
                        cache->flag &= ~PTCACHE_BAKED;
                }
        }
-       else for(SETLOOPER(scene, sce_iter, base)) {
-               /* cache/bake everything in the scene */
-               BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
+       else {
+               for (SETLOOPER(scene, sce_iter, base)) {
+                       /* cache/bake everything in the scene */
+                       BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
+
+                       for (pid=pidlist.first; pid; pid=pid->next) {
+                               cache = pid->cache;
+                               if ((cache->flag & PTCACHE_BAKED)==0) {
+                                       if (pid->type==PTCACHE_TYPE_PARTICLES) {
+                                               ParticleSystem *psys = (ParticleSystem*)pid->calldata;
+                                               /* skip hair & keyed particles */
+                                               if (psys->part->type == PART_HAIR || psys->part->phystype == PART_PHYS_KEYED)
+                                                       continue;
+
+                                               psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
+                                       }
 
-               for(pid=pidlist.first; pid; pid=pid->next) {
-                       cache = pid->cache;
-                       if((cache->flag & PTCACHE_BAKED)==0) {
-                               if(pid->type==PTCACHE_TYPE_PARTICLES) {
-                                       ParticleSystem *psys = (ParticleSystem*)pid->calldata;
-                                       /* skip hair & keyed particles */
-                                       if(psys->part->type == PART_HAIR || psys->part->phystype == PART_PHYS_KEYED)
-                                               continue;
+                                       if ((cache->flag & PTCACHE_REDO_NEEDED || (cache->flag & PTCACHE_SIMULATION_VALID)==0) &&
+                                           (render || bake))
+                                       {
+                                               BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
+                                       }
 
-                                       psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe);
-                               }
+                                       startframe = MIN2(startframe, cache->startframe);
 
-                               if((cache->flag & PTCACHE_REDO_NEEDED || (cache->flag & PTCACHE_SIMULATION_VALID)==0)
-                                       && ((cache->flag & PTCACHE_QUICK_CACHE)==0 || render || bake))
-                                       BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
+                                       if (bake || render) {
+                                               cache->flag |= PTCACHE_BAKING;
 
-                               startframe = MIN2(startframe, cache->startframe);
+                                               if (bake)
+                                                       thread_data.endframe = MAX2(thread_data.endframe, cache->endframe);
+                                       }
 
-                               if(bake || render) {
-                                       cache->flag |= PTCACHE_BAKING;
+                                       cache->flag &= ~PTCACHE_BAKED;
 
-                                       if(bake)
-                                               thread_data.endframe = MAX2(thread_data.endframe, cache->endframe);
                                }
-
-                               cache->flag &= ~PTCACHE_BAKED;
-
                        }
+                       BLI_freelistN(&pidlist);
                }
-               BLI_freelistN(&pidlist);
        }
 
        CFRA = startframe;
@@ -2874,7 +3041,7 @@ void BKE_ptcache_bake(PTCacheBaker* baker)
 
        WM_cursor_wait(1);
        
-       if(G.background) {
+       if (G.background) {
                ptcache_bake_thread((void*)&thread_data);
        }
        else {
@@ -2883,7 +3050,7 @@ void BKE_ptcache_bake(PTCacheBaker* baker)
 
                while (thread_data.thread_ended == FALSE) {
 
-                       if(bake)
+                       if (bake)
                                progress = (int)(100.0f * (float)(CFRA - startframe)/(float)(thread_data.endframe-startframe));
                        else
                                progress = CFRA;
@@ -2898,7 +3065,7 @@ void BKE_ptcache_bake(PTCacheBaker* baker)
                        PIL_sleep_ms(200);
 
                        /* NOTE: breaking baking should leave calculated frames in cache, not clear it */
-                       if(blender_test_break() && !thread_data.break_operation) {
+                       if (blender_test_break() && !thread_data.break_operation) {
                                thread_data.break_operation = TRUE;
                                if (baker->progressend)
                                        baker->progressend(baker->progresscontext);
@@ -2909,47 +3076,49 @@ void BKE_ptcache_bake(PTCacheBaker* baker)
        BLI_end_threads(&threads);
        }
        /* clear baking flag */
-       if(pid) {
+       if (pid) {
                cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
                cache->flag |= PTCACHE_SIMULATION_VALID;
-               if(bake) {
+               if (bake) {
                        cache->flag |= PTCACHE_BAKED;
                        /* write info file */
-                       if(cache->flag & PTCACHE_DISK_CACHE)
+                       if (cache->flag & PTCACHE_DISK_CACHE)
                                BKE_ptcache_write(pid, 0);
                }
        }
-       else for(SETLOOPER(scene, sce_iter, base)) {
-               BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
+       else {
+               for (SETLOOPER(scene, sce_iter, base)) {
+                       BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
 
-               for(pid=pidlist.first; pid; pid=pid->next) {
-                       /* skip hair particles */
-                       if(pid->type==PTCACHE_TYPE_PARTICLES && ((ParticleSystem*)pid->calldata)->part->type == PART_HAIR)
-                               continue;
-               
-                       cache = pid->cache;
+                       for (pid=pidlist.first; pid; pid=pid->next) {
+                               /* skip hair particles */
+                               if (pid->type==PTCACHE_TYPE_PARTICLES && ((ParticleSystem*)pid->calldata)->part->type == PART_HAIR)
+                                       continue;
 
-                       if(thread_data.step > 1)
-                               cache->flag &= ~(PTCACHE_BAKING|PTCACHE_OUTDATED);
-                       else
-                               cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
+                               cache = pid->cache;
+
+                               if (thread_data.step > 1)
+                                       cache->flag &= ~(PTCACHE_BAKING|PTCACHE_OUTDATED);
+                               else
+                                       cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
 
-                       cache->flag |= PTCACHE_SIMULATION_VALID;
+                               cache->flag |= PTCACHE_SIMULATION_VALID;
 
-                       if(bake) {
-                               cache->flag |= PTCACHE_BAKED;
-                               if(cache->flag & PTCACHE_DISK_CACHE)
-                                       BKE_ptcache_write(pid, 0);
+                               if (bake) {
+                                       cache->flag |= PTCACHE_BAKED;
+                                       if (cache->flag & PTCACHE_DISK_CACHE)
+                                               BKE_ptcache_write(pid, 0);
+                               }
                        }
+                       BLI_freelistN(&pidlist);
                }
-               BLI_freelistN(&pidlist);
        }
 
        scene->r.framelen = frameleno;
        CFRA = cfrao;
        
-       if(bake) /* already on cfra unless baking */
-               scene_update_for_newframe(bmain, scene, scene->lay);
+       if (bake) /* already on cfra unless baking */
+               BKE_scene_update_for_newframe(bmain, scene, scene->lay);
 
        if (thread_data.break_operation)
                WM_cursor_wait(0);
@@ -2977,10 +3146,10 @@ void BKE_ptcache_disk_to_mem(PTCacheID *pid)
        /* restore possible bake flag */
        cache->flag |= baked;
 
-       for(cfra=sfra; cfra <= efra; cfra++) {
+       for (cfra=sfra; cfra <= efra; cfra++) {
                pm = ptcache_disk_frame_to_mem(pid, cfra);
 
-               if(pm)
+               if (pm)
                        BLI_addtail(&pid->cache->mem_cache, pm);
        }
 }
@@ -2999,15 +3168,15 @@ void BKE_ptcache_mem_to_disk(PTCacheID *pid)
        /* restore possible bake flag */
        cache->flag |= baked;
 
-       for(; pm; pm=pm->next) {
-               if(ptcache_mem_frame_to_disk(pid, pm)==0) {
+       for (; pm; pm=pm->next) {
+               if (ptcache_mem_frame_to_disk(pid, pm)==0) {
                        cache->flag &= ~PTCACHE_DISK_CACHE;
                        break;
                }
        }
 
        /* write info file */
-       if(cache->flag & PTCACHE_BAKED)
+       if (cache->flag & PTCACHE_BAKED)
                BKE_ptcache_write(pid, 0);
 }
 void BKE_ptcache_toggle_disk_cache(PTCacheID *pid)
@@ -3015,19 +3184,19 @@ void BKE_ptcache_toggle_disk_cache(PTCacheID *pid)
        PointCache *cache = pid->cache;
        int last_exact = cache->last_exact;
 
-       if (!G.relbase_valid){
+       if (!G.relbase_valid) {
                cache->flag &= ~PTCACHE_DISK_CACHE;
-               if (G.f & G_DEBUG) 
+               if (G.debug & G_DEBUG)
                        printf("File must be saved before using disk cache!\n");
                return;
        }
 
-       if(cache->cached_frames) {
+       if (cache->cached_frames) {
                MEM_freeN(cache->cached_frames);
                cache->cached_frames=NULL;
        }
 
-       if(cache->flag & PTCACHE_DISK_CACHE)
+       if (cache->flag & PTCACHE_DISK_CACHE)
                BKE_ptcache_mem_to_disk(pid);
        else
                BKE_ptcache_disk_to_mem(pid);
@@ -3066,7 +3235,7 @@ void BKE_ptcache_disk_cache_rename(PTCacheID *pid, const char *name_src, const c
 
        ptcache_path(pid, path);
        dir = opendir(path);
-       if(dir==NULL) {
+       if (dir==NULL) {
                BLI_strncpy(pid->cache->name, old_name, sizeof(pid->cache->name));
                return;
        }
@@ -3115,7 +3284,7 @@ void BKE_ptcache_load_external(PTCacheID *pid)
        char filename[MAX_PTCACHE_FILE];
        char ext[MAX_PTCACHE_PATH];
 
-       if(!cache)
+       if (!cache)
                return;
 
        ptcache_path(pid, path);
@@ -3126,7 +3295,7 @@ void BKE_ptcache_load_external(PTCacheID *pid)
        if (dir==NULL)
                return;
 
-       if(cache->index >= 0)
+       if (cache->index >= 0)
                BLI_snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, cache->index);
        else
                BLI_strncpy(ext, PTCACHE_EXT, sizeof(ext));
@@ -3142,7 +3311,7 @@ void BKE_ptcache_load_external(PTCacheID *pid)
                                        BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
                                        frame = atoi(num);
 
-                                       if(frame) {
+                                       if (frame) {
                                                start = MIN2(start, frame);
                                                end = MAX2(end, frame);
                                        }
@@ -3154,22 +3323,23 @@ void BKE_ptcache_load_external(PTCacheID *pid)
        }
        closedir(dir);
 
-       if(start != MAXFRAME) {
+       if (start != MAXFRAME) {
                PTCacheFile *pf;
 
                cache->startframe = start;
                cache->endframe = end;
                cache->totpoint = 0;
 
-               if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
-                       ; /*necessary info in every file*/
+               if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN) {
+                       /* necessary info in every file */
+               }
                /* read totpoint from info file (frame 0) */
-               else if(info) {
+               else if (info) {
                        pf= ptcache_file_open(pid, PTCACHE_FILE_READ, 0);
 
-                       if(pf) {
-                               if(ptcache_file_header_begin_read(pf)) {
-                                       if(pf->type == pid->type && pid->read_header(pf)) {
+                       if (pf) {
+                               if (ptcache_file_header_begin_read(pf)) {
+                                       if (pf->type == pid->type && pid->read_header(pf)) {
                                                cache->totpoint = pf->totpoint;
                                                cache->flag |= PTCACHE_READ_INFO;
                                        }
@@ -3186,8 +3356,8 @@ void BKE_ptcache_load_external(PTCacheID *pid)
                        int elemsize = ptcache_old_elemsize(pid);
                        pf= ptcache_file_open(pid, PTCACHE_FILE_READ, cache->startframe);
 
-                       if(pf) {
-                               while(ptcache_file_read(pf, old_data, 1, elemsize))
+                       if (pf) {
+                               while (ptcache_file_read(pf, old_data, 1, elemsize))
                                        cache->totpoint++;
                                
                                ptcache_file_close(pf);
@@ -3207,30 +3377,29 @@ void BKE_ptcache_update_info(PTCacheID *pid)
        int totframes = 0;
        char mem_info[64];
 
-       if(cache->flag & PTCACHE_EXTERNAL) {
+       if (cache->flag & PTCACHE_EXTERNAL) {
                int cfra = cache->startframe;
 
-               for(; cfra<=cache->endframe; cfra++) {
-                       if(BKE_ptcache_id_exist(pid, cfra))
+               for (; cfra <= cache->endframe; cfra++) {
+                       if (BKE_ptcache_id_exist(pid, cfra))
                                totframes++;
                }
 
                /* smoke doesn't use frame 0 as info frame so can't check based on totpoint */
-               if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN && totframes)
+               if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN && totframes)
                        BLI_snprintf(cache->info, sizeof(cache->info), "%i frames found!", totframes);
-               else if(totframes && cache->totpoint)
+               else if (totframes && cache->totpoint)
                        BLI_snprintf(cache->info, sizeof(cache->info), "%i points found!", cache->totpoint);
                else
                        BLI_snprintf(cache->info, sizeof(cache->info), "No valid data to read!");
                return;
        }
 
-       if(cache->flag & PTCACHE_DISK_CACHE) {
-               if(pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
-               {
+       if (cache->flag & PTCACHE_DISK_CACHE) {
+               if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN) {
                        int totpoint = pid->totpoint(pid->calldata, 0);
 
-                       if(cache->totpoint > totpoint)
+                       if (cache->totpoint > totpoint)
                                BLI_snprintf(mem_info, sizeof(mem_info), "%i cells + High Resolution cached", totpoint);
                        else
                                BLI_snprintf(mem_info, sizeof(mem_info), "%i cells cached", totpoint);
@@ -3238,8 +3407,8 @@ void BKE_ptcache_update_info(PTCacheID *pid)
                else {
                        int cfra = cache->startframe;
 
-                       for(; cfra<=cache->endframe; cfra++) {
-                               if(BKE_ptcache_id_exist(pid, cfra))
+                       for (; cfra <= cache->endframe; cfra++) {
+                               if (BKE_ptcache_id_exist(pid, cfra))
                                        totframes++;
                        }
 
@@ -3247,15 +3416,15 @@ void BKE_ptcache_update_info(PTCacheID *pid)
                }
        }
        else {
-               PTCacheMem *pm = cache->mem_cache.first;                
+               PTCacheMem *pm = cache->mem_cache.first;
                float bytes = 0.0f;
                int i, mb;
                
-               for(; pm; pm=pm->next) {
-                       for(i=0; i<BPHYS_TOT_DATA; i++)
+               for (; pm; pm=pm->next) {
+                       for (i=0; i<BPHYS_TOT_DATA; i++)
                                bytes += MEM_allocN_len(pm->data[i]);
 
-                       for(extra=pm->extradata.first; extra; extra=extra->next) {
+                       for (extra=pm->extradata.first; extra; extra=extra->next) {
                                bytes += MEM_allocN_len(extra->data);
                                bytes += sizeof(PTCacheExtra);
                        }
@@ -3268,15 +3437,15 @@ void BKE_ptcache_update_info(PTCacheID *pid)
                mb = (bytes > 1024.0f * 1024.0f);
 
                BLI_snprintf(mem_info, sizeof(mem_info), "%i frames in memory (%.1f %s)",
-                       totframes,
-                       bytes / (mb ? 1024.0f * 1024.0f : 1024.0f),
-                       mb ? "Mb" : "kb");
+                            totframes,
+                            bytes / (mb ? 1024.0f * 1024.0f : 1024.0f),
+                            mb ? "Mb" : "kb");
        }
 
-       if(cache->flag & PTCACHE_OUTDATED) {
+       if (cache->flag & PTCACHE_OUTDATED) {
                BLI_snprintf(cache->info, sizeof(cache->info), "%s, cache is outdated!", mem_info);
        }
-       else if(cache->flag & PTCACHE_FRAMES_SKIPPED) {
+       else if (cache->flag & PTCACHE_FRAMES_SKIPPED) {
                BLI_snprintf(cache->info, sizeof(cache->info), "%s, not exact since frame %i.", mem_info, cache->last_exact);
        }
        else {
@@ -3286,14 +3455,14 @@ void BKE_ptcache_update_info(PTCacheID *pid)
 
 void BKE_ptcache_validate(PointCache *cache, int framenr)
 {
-       if(cache) {
+       if (cache) {
                cache->flag |= PTCACHE_SIMULATION_VALID;
                cache->simframe = framenr;
        }
 }
 void BKE_ptcache_invalidate(PointCache *cache)
 {
-       if(cache) {
+       if (cache) {
                cache->flag &= ~PTCACHE_SIMULATION_VALID;
                cache->simframe = 0;
                cache->last_exact = MIN2(cache->startframe, 0);