Optimize GoL a bit, haven't changed gol2 yet.

This commit is contained in:
cracker64 2013-05-05 18:10:13 -04:00
parent b8b5f91e20
commit 77102f4bba

View File

@ -3326,7 +3326,7 @@ void Simulation::delete_part(int x, int y, int flags)//calls kill_part with the
void Simulation::update_particles_i(int start, int inc) void Simulation::update_particles_i(int start, int inc)
{ {
int i, j, x, y, t, nx, ny, r, surround_space, s, lt, rt, nt, nnx, nny, q, golnum, goldelete, z, neighbors, createdsomething; int i, j, x, y, t, nx, ny, r, surround_space, s, lt, rt, nt, nnx, nny, q, golnum, z, neighbors;
float mv, dx, dy, ix, iy, lx, ly, nrx, nry, dp, ctemph, ctempl, gravtot; float mv, dx, dy, ix, iy, lx, ly, nrx, nry, dp, ctemph, ctempl, gravtot;
int fin_x, fin_y, clear_x, clear_y, stagnant; int fin_x, fin_y, clear_x, clear_y, stagnant;
float fin_xf, fin_yf, clear_xf, clear_yf; float fin_xf, fin_yf, clear_xf, clear_yf;
@ -3530,9 +3530,7 @@ void Simulation::update_particles_i(int start, int inc)
//game of life! //game of life!
if (elementCount[PT_LIFE]>0&&++CGOL>=GSPEED)//GSPEED is frames per generation if (elementCount[PT_LIFE]>0&&++CGOL>=GSPEED)//GSPEED is frames per generation
{ {
int createdsomething = 0;
CGOL=0; CGOL=0;
ISGOL=0;
//TODO: maybe this should only loop through active particles //TODO: maybe this should only loop through active particles
for (ny=CELL; ny<YRES-CELL; ny++) for (ny=CELL; ny<YRES-CELL; ny++)
{//go through every particle and set neighbor map {//go through every particle and set neighbor map
@ -3544,34 +3542,31 @@ void Simulation::update_particles_i(int start, int inc)
gol[ny][nx] = 0; gol[ny][nx] = 0;
continue; continue;
} }
else if ((r&0xFF)==PT_LIFE)
{ {
if (parts[r>>8].type==PT_LIFE) golnum = parts[r>>8].ctype+1;
{ if (golnum<=0 || golnum>NGOL) {
golnum = parts[r>>8].ctype+1; kill_part(r>>8);
if (golnum<=0 || golnum>NGOL) { continue;
kill_part(r>>8); }
continue; gol[ny][nx] = golnum;
} if (parts[r>>8].tmp == grule[golnum][9]-1) {
if (parts[r>>8].tmp == grule[golnum][9]-1) { for ( nnx=-1; nnx<2; nnx++)
gol[ny][nx] = golnum; {
for ( nnx=-1; nnx<2; nnx++) for ( nny=-1; nny<2; nny++)//it will count itself as its own neighbor, which is needed, but will have 1 extra for delete check
{ {
for ( nny=-1; nny<2; nny++)//it will count itself as its own neighbor, which is needed, but will have 1 extra for delete check int adx = ((nx+nnx+XRES-3*CELL)%(XRES-2*CELL))+CELL;
int ady = ((ny+nny+YRES-3*CELL)%(YRES-2*CELL))+CELL;
rt = pmap[ady][adx];
if (!rt || (rt&0xFF)==PT_LIFE)
{ {
rt = pmap[((ny+nny+YRES-3*CELL)%(YRES-2*CELL))+CELL][((nx+nnx+XRES-3*CELL)%(XRES-2*CELL))+CELL]; gol2[ady][adx][golnum] ++;
if (!rt || (rt&0xFF)==PT_LIFE) gol2[ady][adx][0] ++;
{
gol2[((ny+nny+YRES-3*CELL)%(YRES-2*CELL))+CELL][((nx+nnx+XRES-3*CELL)%(XRES-2*CELL))+CELL][golnum] ++;
gol2[((ny+nny+YRES-3*CELL)%(YRES-2*CELL))+CELL][((nx+nnx+XRES-3*CELL)%(XRES-2*CELL))+CELL][0] ++;
}
} }
} }
} else {
parts[r>>8].tmp --;
if (parts[r>>8].tmp<=0)
parts[r>>8].type = PT_NONE;//TODO: move delete into an update function, change to kill_part
} }
} else {
parts[r>>8].tmp --;
} }
} }
} }
@ -3581,27 +3576,35 @@ void Simulation::update_particles_i(int start, int inc)
for (nx=CELL; nx<XRES-CELL; nx++) for (nx=CELL; nx<XRES-CELL; nx++)
{ {
r = pmap[ny][nx]; r = pmap[ny][nx];
neighbors = gol2[ny][nx][0]; if (r && (r&0xFF)!=PT_LIFE)
if (neighbors==0 || !((r&0xFF)==PT_LIFE || !(r&0xFF)))
continue; continue;
for (golnum = 1; golnum<=NGOL; golnum++) neighbors = gol2[ny][nx][0];
if (neighbors)
{ {
goldelete = neighbors; golnum = gol[ny][nx];
if (gol[ny][nx]==0&&grule[golnum][goldelete]>=2&&gol2[ny][nx][golnum]>=(goldelete%2)+goldelete/2) if (!r)
{ {
if (create_part(-1, nx, ny, PT_LIFE|((golnum-1)<<8))) //Find which type we can try and create
createdsomething = 1; for (golnum = 1; golnum<=NGOL; golnum++)
{
if (grule[golnum][neighbors]>=2 && gol2[ny][nx][golnum]>=(neighbors%2)+neighbors/2)
{
create_part(-1, nx, ny, PT_LIFE|((golnum-1)<<8));
break;
}
}
} }
else if (gol[ny][nx]==golnum&&(grule[golnum][goldelete-1]==0||grule[golnum][goldelete-1]==2))//subtract 1 because it counted itself else if (grule[golnum][neighbors-1]==0 || grule[golnum][neighbors-1]==2)//subtract 1 because it counted itself
{ {
if (parts[r>>8].tmp==grule[golnum][9]-1) if (parts[r>>8].tmp==grule[golnum][9]-1)
parts[r>>8].tmp --; parts[r>>8].tmp --;
} }
if (r && parts[r>>8].tmp<=0) for ( z = 0; z<=NGOL; z++)
parts[r>>8].type = PT_NONE;//TODO: move delete into an update function, change to kill_part gol2[ny][nx][z] = 0;//this improves performance A LOT compared to the memset, i was getting ~23 more fps with this.
} }
for ( z = 0; z<=NGOL; z++) //we still need to kill things with 0 neighbors (higher state life)
gol2[ny][nx][z] = 0;//this improves performance A LOT compared to the memset, i was getting ~23 more fps with this. if (r && parts[r>>8].tmp<=0)
kill_part(r>>8);
} }
} }
//memset(gol2, 0, sizeof(gol2)); //memset(gol2, 0, sizeof(gol2));
@ -4076,7 +4079,6 @@ void Simulation::update_particles_i(int start, int inc)
if (t==PT_LIFE) if (t==PT_LIFE)
{ {
parts[i].temp = restrict_flt(parts[i].temp-50.0f, MIN_TEMP, MAX_TEMP); parts[i].temp = restrict_flt(parts[i].temp-50.0f, MIN_TEMP, MAX_TEMP);
//ISGOL=1;//means there is a life particle on screen
} }
if (t==PT_WIRE) if (t==PT_WIRE)
{ {