diff --git a/includes/powder.h b/includes/powder.h index 958f3a1d3..aded05f03 100644 --- a/includes/powder.h +++ b/includes/powder.h @@ -848,6 +848,7 @@ unsigned cb_pmap[YRES][XRES]; unsigned photons[YRES][XRES]; +int do_move(int i, int x, int y, float nxf, float nyf); int try_move(int i, int x, int y, int nx, int ny); int eval_move(int pt, int nx, int ny, unsigned *rr); diff --git a/src/powder.c b/src/powder.c index af819bd8e..a50686dc9 100644 --- a/src/powder.c +++ b/src/powder.c @@ -306,6 +306,35 @@ int try_move(int i, int x, int y, int nx, int ny) return 1; } +// try to move particle, and if successful update pmap and parts[i].x,y +int do_move(int i, int x, int y, float nxf, float nyf) +{ + int nx = (int)(nxf+0.5f), ny = (int)(nyf+0.5f); + int result = try_move(i, x, y, nx, ny); + if (result) + { + int t = parts[i].type; + parts[i].x = nxf; + parts[i].y = nyf; + if (ny!=y || nx!=x) + { + if ((pmap[y][x]>>8)==i) pmap[y][x] = 0; + else if ((photons[y][x]>>8)==i) photons[y][x] = 0; + if (nx=XRES-CELL || ny=YRES-CELL)//kill_part if particle is out of bounds + { + kill_part(i); + return -1; + } + if (t==PT_PHOT||t==PT_NEUT) + photons[ny][nx] = t|(i<<8); + else + pmap[ny][nx] = t|(i<<8); + } + } + return result; +} + + static unsigned direction_to_map(float dx, float dy, int t) { // TODO: @@ -1845,10 +1874,8 @@ killed: } } } - if (try_move(i, x, y, fin_x, fin_y)) { - parts[i].x = fin_xf; - parts[i].y = fin_yf; - } else { + if (!do_move(i, x, y, fin_xf, fin_yf)) + { // reflection parts[i].flags |= FLAG_STAGNANT; if (t==PT_NEUT && 100>(rand()%1000)) @@ -1882,7 +1909,6 @@ killed: parts[i].vy -= 2.0f*dp*nry; fin_x = (int)(parts[i].x + parts[i].vx + 0.5f); fin_y = (int)(parts[i].y + parts[i].vy + 0.5f); - // cast as int then back to float for compatibility with existing saves if (t==PT_PHOT) { s = eval_move(PT_PHOT, fin_x, fin_y, &r); if ((((r&0xFF)==PT_GLAS && lt!=PT_GLAS) || ((r&0xFF)!=PT_GLAS && lt==PT_GLAS)) && s) { @@ -1890,10 +1916,8 @@ killed: goto movedone; } } - if (try_move(i, x, y, fin_x, fin_y)) { - parts[i].x = (float)fin_x; - parts[i].y = (float)fin_y; - } else { + // cast coords as int then back to float for compatibility with existing saves + if (!do_move(i, x, y, (float)fin_x, (float)fin_y)) { kill_part(i); continue; } @@ -1911,25 +1935,20 @@ killed: else if (ptypes[t].falldown==0) { // gasses and solids (but not powders) - if (try_move(i, x, y, fin_x, fin_y)) { - parts[i].x = fin_xf; - parts[i].y = fin_yf; - } else { + if (!do_move(i, x, y, fin_xf, fin_yf)) + { + // can't move there, so bounce off // TODO if (fin_x>x+ISTP) fin_x=x+ISTP; if (fin_xy+ISTP) fin_y=y+ISTP; if (fin_y=0 && j>=clear_x-rt && j=0 && j=ny-rt && j>8)==i) pmap[y][x] = 0; - else if ((photons[y][x]>>8)==i) photons[y][x] = 0; - if (nx=XRES-CELL || ny=YRES-CELL)//kill_part if particle is out of bounds - { - kill_part(i); - continue; - } - if (t==PT_PHOT||t==PT_NEUT) - photons[ny][nx] = t|(i<<8); - else - pmap[ny][nx] = t|(i<<8); - } + continue; } if (framerender) { framerender = 0;