diff --git a/includes/gravity.h b/includes/gravity.h index 9168b9df5..949dc71f6 100644 --- a/includes/gravity.h +++ b/includes/gravity.h @@ -7,20 +7,17 @@ extern int ngrav_enable; //Newtonian gravity extern int gravwl_timeout; extern int gravityMode; -extern float gravmap[YRES/CELL][XRES/CELL]; //Maps to be used by the main thread -extern float *gravpf; -extern float *gravxf; -extern float *gravyf; -extern unsigned *gravmaskf; +float *gravmap;//Maps to be used by the main thread +float *gravp; +float *gravy; +float *gravx; +unsigned *gravmask; -extern float th_ogravmap[YRES/CELL][XRES/CELL]; // Maps to be processed by the gravity thread -extern float th_gravmap[YRES/CELL][XRES/CELL]; -extern float th_gravx[YRES/CELL][XRES/CELL]; -extern float th_gravy[YRES/CELL][XRES/CELL]; -extern float *th_gravpf; -extern float *th_gravxf; -extern float *th_gravyf; -extern float th_gravp[YRES/CELL][XRES/CELL]; +float *th_ogravmap;// Maps to be processed by the gravity thread +float *th_gravmap; +float *th_gravx; +float *th_gravy; +float *th_gravp; void gravity_init(); void gravity_cleanup(); diff --git a/src/elements/gbmb.c b/src/elements/gbmb.c index d60631256..c31f3237f 100644 --- a/src/elements/gbmb.c +++ b/src/elements/gbmb.c @@ -19,8 +19,8 @@ int update_GBMB(UPDATE_FUNC_ARGS) { } } if(parts[i].life>20) - gravmap[y/CELL][x/CELL] = 20; + gravmap[(y/CELL)*(XRES/CELL)+(x/CELL)] = 20; if(parts[i].life<20 && parts[i].life>=1) - gravmap[y/CELL][x/CELL] = -80; + gravmap[(y/CELL)*(XRES/CELL)+(x/CELL)] = -80; return 0; } diff --git a/src/elements/gpmp.c b/src/elements/gpmp.c index 1766b3baa..1f2be1d93 100644 --- a/src/elements/gpmp.c +++ b/src/elements/gpmp.c @@ -11,9 +11,9 @@ int update_GPMP(UPDATE_FUNC_ARGS) { if (parts[i].temp<= -256.0+273.15) parts[i].temp = -256.0+273.15; - gravmap[y/CELL][x/CELL] = 0.2f*(parts[i].temp-273.15); + gravmap[(y/CELL)*(XRES/CELL)+(x/CELL)] = 0.2f*(parts[i].temp-273.15); if (y+CELL=0 && y+ry>0 && x+rx int update_NBHL(UPDATE_FUNC_ARGS) { - gravmap[y/CELL][x/CELL] += 0.1f; + gravmap[(y/CELL)*(XRES/CELL)+(x/CELL)] += 0.1f; return 0; } diff --git a/src/elements/nwhl.c b/src/elements/nwhl.c index 8515542df..674ede3ed 100644 --- a/src/elements/nwhl.c +++ b/src/elements/nwhl.c @@ -1,6 +1,6 @@ #include int update_NWHL(UPDATE_FUNC_ARGS) { - gravmap[y/CELL][x/CELL] -= 0.1f; + gravmap[(y/CELL)*(XRES/CELL)+(x/CELL)] -= 0.1f; return 0; } diff --git a/src/elements/stkm.c b/src/elements/stkm.c index 40e441697..08cb1baf2 100644 --- a/src/elements/stkm.c +++ b/src/elements/stkm.c @@ -80,8 +80,8 @@ int run_stickman(playerst* playerp, UPDATE_FUNC_ARGS) { } } - gvx += gravxf[XRES*(int)parts[i].y + (int)parts[i].x]; - gvy += gravyf[XRES*(int)parts[i].y + (int)parts[i].x]; + gvx += gravx[((int)parts[i].y/CELL)*(XRES/CELL)+((int)parts[i].x/CELL)]; + gvy += gravy[((int)parts[i].y/CELL)*(XRES/CELL)+((int)parts[i].x/CELL)]; parts[i].vx -= gvx*dt; //Head up! parts[i].vy -= gvy*dt; diff --git a/src/graphics.c b/src/graphics.c index 55dd0532a..3a2c85c0b 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -1427,7 +1427,7 @@ void draw_grav_zones(pixel * vid) { for (x=0; x 0 && rx < XRES && ry > 0 && ry < YRES && gx > 0 && gx < XRES && gy > 0 && gy < YRES && bx > 0 && bx < XRES && by > 0 && by < YRES) { t = dst[ny*(XRES+BARSIZE)+nx]; diff --git a/src/gravity.c b/src/gravity.c index 89771507b..4f8fdc12b 100644 --- a/src/gravity.c +++ b/src/gravity.c @@ -9,24 +9,22 @@ #endif -float gravmap[YRES/CELL][XRES/CELL]; //Maps to be used by the main thread -float *gravpf; -float *gravyf; -float *gravxf; -unsigned *gravmaskf; +float *gravmap;//Maps to be used by the main thread +float *gravp; +float *gravy; +float *gravx; +unsigned *gravmask; -float th_ogravmap[YRES/CELL][XRES/CELL]; // Maps to be processed by the gravity thread -float th_gravmap[YRES/CELL][XRES/CELL]; -float th_gravx[YRES/CELL][XRES/CELL]; -float th_gravy[YRES/CELL][XRES/CELL]; -float th_gravp[YRES/CELL][XRES/CELL]; -float *th_gravpf; -float *th_gravyf; -float *th_gravxf; +float *th_ogravmap;// Maps to be processed by the gravity thread +float *th_gravmap; +float *th_gravx; +float *th_gravy; +float *th_gravp; int gravwl_timeout = 0; int gravityMode = 0; // starts enabled in "vertical" mode... -int ngrav_enable = 0; //Newtonian gravity, will be set by save +int ngrav_enable = 0; //Newtonian gravity +int th_gravchanged = 0; pthread_t gravthread; pthread_mutex_t gravmutex; @@ -63,13 +61,16 @@ void bilinear_interpolation(float *src, float *dst, int sw, int sh, int rw, int void gravity_init() { //Allocate full size Gravmaps - th_gravyf = calloc(XRES*YRES, sizeof(float)); - th_gravxf = calloc(XRES*YRES, sizeof(float)); - th_gravpf = calloc(XRES*YRES, sizeof(float)); - gravyf = calloc(XRES*YRES, sizeof(float)); - gravxf = calloc(XRES*YRES, sizeof(float)); - gravpf = calloc(XRES*YRES, sizeof(float)); - gravmaskf = malloc(XRES*YRES*sizeof(unsigned)); + th_ogravmap = calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); + th_gravmap = calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); + th_gravy = calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); + th_gravx = calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); + th_gravp = calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); + gravmap = calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); + gravy = calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); + gravx = calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); + gravp = calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); + gravmask = calloc((XRES/CELL)*(YRES/CELL), sizeof(unsigned)); } void gravity_cleanup() @@ -88,22 +89,28 @@ void gravity_update_async() result = grav_ready; if(result) //Did the gravity thread finish? { - memcpy(th_gravmap, gravmap, sizeof(gravmap)); //Move our current gravmap to be processed other thread - if (!sys_pause||framerender){ //Only update if not paused //Switch the full size gravmaps, we don't really need the two above any more float *tmpf; - tmpf = gravyf; - gravyf = th_gravyf; - th_gravyf = tmpf; - - tmpf = gravxf; - gravxf = th_gravxf; - th_gravxf = tmpf; - - tmpf = gravpf; - gravpf = th_gravpf; - th_gravpf = tmpf; + + if(th_gravchanged) + { + tmpf = gravy; + gravy = th_gravy; + th_gravy = tmpf; + + tmpf = gravx; + gravx = th_gravx; + th_gravx = tmpf; + + tmpf = gravp; + gravp = th_gravp; + th_gravp = tmpf; + } + + tmpf = gravmap; + gravmap = th_gravmap; + th_gravmap = tmpf; grav_ready = 0; //Tell the other thread that we're ready for it to continue pthread_cond_signal(&gravcv); @@ -111,8 +118,8 @@ void gravity_update_async() } pthread_mutex_unlock(&gravmutex); //Apply the gravity mask - membwand(gravyf, gravmaskf, XRES*YRES*sizeof(float), XRES*YRES*sizeof(unsigned)); - membwand(gravxf, gravmaskf, XRES*YRES*sizeof(float), XRES*YRES*sizeof(unsigned)); + membwand(gravy, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); + membwand(gravx, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); } } @@ -120,13 +127,14 @@ void* update_grav_async(void* unused) { int done = 0; int thread_done = 0; - memset(th_ogravmap, 0, sizeof(th_ogravmap)); - memset(th_gravmap, 0, sizeof(th_gravmap)); - memset(th_gravy, 0, sizeof(th_gravy)); - memset(th_gravx, 0, sizeof(th_gravx)); - memset(th_gravyf, 0, XRES*YRES*sizeof(float)); - memset(th_gravxf, 0, XRES*YRES*sizeof(float)); - memset(th_gravpf, 0, XRES*YRES*sizeof(float)); + memset(th_ogravmap, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); + memset(th_gravmap, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); + memset(th_gravy, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); + memset(th_gravx, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); + memset(th_gravp, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); + //memset(th_gravy, 0, XRES*YRES*sizeof(float)); + //memset(th_gravx, 0, XRES*YRES*sizeof(float)); + //memset(th_gravp, 0, XRES*YRES*sizeof(float)); #ifdef GRAVFFT grav_fft_init(); #endif @@ -163,9 +171,9 @@ void start_grav_async() pthread_create(&gravthread, NULL, update_grav_async, NULL); //Start asynchronous gravity simulation ngrav_enable = 1; } - memset(gravyf, 0, XRES*YRES*sizeof(float)); - memset(gravxf, 0, XRES*YRES*sizeof(float)); - memset(gravpf, 0, XRES*YRES*sizeof(float)); + memset(gravy, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); + memset(gravx, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); + memset(gravp, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); } void stop_grav_async() @@ -180,9 +188,9 @@ void stop_grav_async() ngrav_enable = 0; } //Clear the grav velocities - memset(gravyf, 0, XRES*YRES*sizeof(float)); - memset(gravxf, 0, XRES*YRES*sizeof(float)); - memset(gravpf, 0, XRES*YRES*sizeof(float)); + memset(gravy, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); + memset(gravx, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); + memset(gravp, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); } #ifdef GRAVFFT @@ -269,13 +277,16 @@ void grav_fft_cleanup() void update_grav() { int x, y, changed = 0; + int xblock2 = XRES/CELL*2, yblock2 = YRES/CELL*2; + int i, fft_tsize = (xblock2/2+1)*yblock2; + float mr, mc, pr, pc, gr, gc; for (y=0; y 0.0001f || th_gravmap[i][j]<-0.0001f) //Only calculate with populated or changed cells. + if (th_gravmap[i*(XRES/CELL)+j] > 0.0001f || th_gravmap[i*(XRES/CELL)+j]<-0.0001f) //Only calculate with populated or changed cells. { #endif for (y = 0; y < YRES / CELL; y++) { @@ -376,24 +389,21 @@ void update_grav(void) continue; distance = sqrt(pow(j - x, 2) + pow(i - y, 2)); #ifdef GRAV_DIFF - val = th_gravmap[i][j] - th_ogravmap[i][j]; + val = th_gravmap[i*(XRES/CELL)+j] - th_ogravmap[i*(XRES/CELL)+j]; #else - val = th_gravmap[i][j]; + val = th_gravmap[i*(XRES/CELL)+j]; #endif - th_gravx[y][x] += M_GRAV * val * (j - x) / pow(distance, 3); - th_gravy[y][x] += M_GRAV * val * (i - y) / pow(distance, 3); - th_gravp[y][x] += M_GRAV * val / pow(distance, 2); + th_gravx[y*(XRES/CELL)+x] += M_GRAV * val * (j - x) / pow(distance, 3); + th_gravy[y*(XRES/CELL)+x] += M_GRAV * val * (i - y) / pow(distance, 3); + th_gravp[y*(XRES/CELL)+x] += M_GRAV * val / pow(distance, 2); } } } } } - bilinear_interpolation(th_gravy, th_gravyf, XRES/CELL, YRES/CELL, XRES, YRES); - bilinear_interpolation(th_gravx, th_gravxf, XRES/CELL, YRES/CELL, XRES, YRES); - bilinear_interpolation(th_gravp, th_gravpf, XRES/CELL, YRES/CELL, XRES, YRES); fin: - memcpy(th_ogravmap, th_gravmap, sizeof(th_gravmap)); - memset(th_gravmap, 0, sizeof(th_gravmap)); + memcpy(th_ogravmap, th_gravmap, (XRES/CELL)*(YRES/CELL)*sizeof(float)); + memset(th_gravmap, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); } #endif @@ -467,7 +477,7 @@ void gravity_mask() } } c_mask_el = t_mask_el; - memset(gravmaskf, 0, XRES*YRES*sizeof(unsigned)); + memset(gravmask, 0, (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); while(c_mask_el!=NULL) { char *cshape = c_mask_el->shape; @@ -480,13 +490,7 @@ void gravity_mask() maskvalue = 0xFFFFFFFF; else maskvalue = 0x00000000; - for (j=0; j XRES-1) - x1 = XRES-1; - if(y1 > YRES-1) - y1 = YRES-1; - if(x1+width > XRES-1) - width = XRES-x1; - if(y1+height > YRES-1) - height = YRES-y1; + width = abs(luaL_optint(l, 3, XRES/CELL)); + height = abs(luaL_optint(l, 4, YRES/CELL)); + if(x1 > (XRES/CELL)-1) + x1 = (XRES/CELL)-1; + if(y1 > (YRES/CELL)-1) + y1 = (YRES/CELL)-1; + if(x1+width > (XRES/CELL)-1) + width = (XRES/CELL)-x1; + if(y1+height > (YRES/CELL)-1) + height = (YRES/CELL)-y1; for (nx = x1; nxptransitions[t].phv&&ptransitions[t].pht>-1) { // particle type change due to high pressure if (ptransitions[t].pht!=PT_NUM) @@ -2449,8 +2449,8 @@ killed: pGravX = ptGrav * ((float)(nx - XCNTR) / pGravD); pGravY = ptGrav * ((float)(ny - YCNTR) / pGravD); } - pGravX += gravxf[(ny*XRES)+nx]; - pGravY += gravyf[(ny*XRES)+nx]; + pGravX += gravx[(ny/CELL)*(XRES/CELL)+(nx/CELL)]; + pGravY += gravy[(ny/CELL)*(XRES/CELL)+(nx/CELL)]; if (fabsf(pGravY)>fabsf(pGravX)) mv = fabsf(pGravY); else @@ -2508,8 +2508,8 @@ killed: pGravX = ptGrav * ((float)(nx - XCNTR) / pGravD); pGravY = ptGrav * ((float)(ny - YCNTR) / pGravD); } - pGravX += gravxf[(ny*XRES)+nx]; - pGravY += gravyf[(ny*XRES)+nx]; + pGravX += gravx[(ny/CELL)*(XRES/CELL)+(nx/CELL)]; + pGravY += gravy[(ny/CELL)*(XRES/CELL)+(nx/CELL)]; if (fabsf(pGravY)>fabsf(pGravX)) mv = fabsf(pGravY); else