summaryrefslogtreecommitdiff
path: root/src/simulation/Simulation.cpp
diff options
context:
space:
mode:
authorcracker64 <cracker642@gmail.com>2013-05-05 22:10:13 (GMT)
committer cracker64 <cracker642@gmail.com>2013-05-05 22:10:13 (GMT)
commit77102f4bbaff9d15617b83e674939b108442ae21 (patch)
tree0b3a5ae379dc834be2f3b457df96be10e368b849 /src/simulation/Simulation.cpp
parentb8b5f91e203c4ce2bd411f56694767546d503edd (diff)
downloadpowder-77102f4bbaff9d15617b83e674939b108442ae21.zip
powder-77102f4bbaff9d15617b83e674939b108442ae21.tar.gz
Optimize GoL a bit, haven't changed gol2 yet.
Diffstat (limited to 'src/simulation/Simulation.cpp')
-rw-r--r--src/simulation/Simulation.cpp78
1 files changed, 40 insertions, 38 deletions
diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp
index b110b72..577da93 100644
--- a/src/simulation/Simulation.cpp
+++ b/src/simulation/Simulation.cpp
@@ -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)
{
- 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;
int fin_x, fin_y, clear_x, clear_y, stagnant;
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!
if (elementCount[PT_LIFE]>0&&++CGOL>=GSPEED)//GSPEED is frames per generation
{
- int createdsomething = 0;
CGOL=0;
- ISGOL=0;
//TODO: maybe this should only loop through active particles
for (ny=CELL; ny<YRES-CELL; ny++)
{//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;
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) {
- kill_part(r>>8);
- continue;
- }
- if (parts[r>>8].tmp == grule[golnum][9]-1) {
- gol[ny][nx] = golnum;
- for ( nnx=-1; nnx<2; nnx++)
+ golnum = parts[r>>8].ctype+1;
+ if (golnum<=0 || golnum>NGOL) {
+ kill_part(r>>8);
+ continue;
+ }
+ gol[ny][nx] = golnum;
+ if (parts[r>>8].tmp == grule[golnum][9]-1) {
+ 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];
- if (!rt || (rt&0xFF)==PT_LIFE)
- {
- 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] ++;
- }
+ gol2[ady][adx][golnum] ++;
+ gol2[ady][adx][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++)
{
r = pmap[ny][nx];
- neighbors = gol2[ny][nx][0];
- if (neighbors==0 || !((r&0xFF)==PT_LIFE || !(r&0xFF)))
+ if (r && (r&0xFF)!=PT_LIFE)
continue;
- for (golnum = 1; golnum<=NGOL; golnum++)
+ neighbors = gol2[ny][nx][0];
+ if (neighbors)
{
- goldelete = neighbors;
- if (gol[ny][nx]==0&&grule[golnum][goldelete]>=2&&gol2[ny][nx][golnum]>=(goldelete%2)+goldelete/2)
+ golnum = gol[ny][nx];
+ if (!r)
{
- if (create_part(-1, nx, ny, PT_LIFE|((golnum-1)<<8)))
- createdsomething = 1;
+ //Find which type we can try and create
+ 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)
parts[r>>8].tmp --;
}
- if (r && parts[r>>8].tmp<=0)
- parts[r>>8].type = PT_NONE;//TODO: move delete into an update function, change to kill_part
+ for ( z = 0; z<=NGOL; z++)
+ 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++)
- gol2[ny][nx][z] = 0;//this improves performance A LOT compared to the memset, i was getting ~23 more fps with this.
+ //we still need to kill things with 0 neighbors (higher state life)
+ if (r && parts[r>>8].tmp<=0)
+ kill_part(r>>8);
}
}
//memset(gol2, 0, sizeof(gol2));
@@ -4076,7 +4079,6 @@ void Simulation::update_particles_i(int start, int inc)
if (t==PT_LIFE)
{
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)
{