diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 61014fdaa..d4217c9f5 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -320,7 +320,7 @@ Snapshot * Simulation::CreateSnapshot() snap->AirVelocityX.insert(snap->AirVelocityX.begin(), &vx[0][0], &vx[0][0]+((XRES/CELL)*(YRES/CELL))); snap->AirVelocityY.insert(snap->AirVelocityY.begin(), &vy[0][0], &vy[0][0]+((XRES/CELL)*(YRES/CELL))); snap->AmbientHeat.insert(snap->AmbientHeat.begin(), &hv[0][0], &hv[0][0]+((XRES/CELL)*(YRES/CELL))); - snap->Particles.insert(snap->Particles.begin(), parts, parts+NPART); + snap->Particles.insert(snap->Particles.begin(), parts, parts+parts_lastActiveIndex+1); snap->PortalParticles.insert(snap->PortalParticles.begin(), &portalp[0][0][0], &portalp[CHANNELS-1][8-1][80-1]); snap->WirelessData.insert(snap->WirelessData.begin(), &wireless[0][0], &wireless[CHANNELS-1][2-1]); snap->GravVelocityX.insert(snap->GravVelocityX.begin(), gravx, gravx+((XRES/CELL)*(YRES/CELL))); @@ -348,7 +348,11 @@ void Simulation::Restore(const Snapshot & snap) std::copy(snap.AirVelocityX.begin(), snap.AirVelocityX.end(), &vx[0][0]); std::copy(snap.AirVelocityY.begin(), snap.AirVelocityY.end(), &vy[0][0]); std::copy(snap.AmbientHeat.begin(), snap.AmbientHeat.end(), &hv[0][0]); + for (int i = 0; i < NPART; i++) + parts[i].type = 0; std::copy(snap.Particles.begin(), snap.Particles.end(), parts); + parts_lastActiveIndex = NPART-1; + RecalcFreeParticles(); std::copy(snap.PortalParticles.begin(), snap.PortalParticles.end(), &portalp[0][0][0]); std::copy(snap.WirelessData.begin(), snap.WirelessData.end(), &wireless[0][0]); if (grav->ngrav_enable) @@ -4744,6 +4748,99 @@ void Simulation::SimulateGoL() //memset(gol2, 0, sizeof(gol2)); } +void Simulation::RecalcFreeParticles() +{ + int x, y, t; + int lastPartUsed = 0; + int lastPartUnused = -1; + + memset(pmap, 0, sizeof(pmap)); + memset(pmap_count, 0, sizeof(pmap_count)); + memset(photons, 0, sizeof(photons)); + + NUM_PARTS = 0; + //the particle loop that resets the pmap/photon maps every frame, to update them. + for (int i = 0; i <= parts_lastActiveIndex; i++) + { + if (parts[i].type) + { + t = parts[i].type; + x = (int)(parts[i].x+0.5f); + y = (int)(parts[i].y+0.5f); + if (x>=0 && y>=0 && x=PT_NUM || !elements[t].Enabled) + { + kill_part(i); + continue; + } + + if (elementRecount) + elementCount[t]++; + + unsigned int elem_properties = elements[t].Properties; + if (parts[i].life>0 && (elem_properties&PROP_LIFE_DEC)) + { + // automatically decrease life + parts[i].life--; + if (parts[i].life<=0 && (elem_properties&(PROP_LIFE_KILL_DEC|PROP_LIFE_KILL))) + { + // kill on change to no life + kill_part(i); + continue; + } + } + else if (parts[i].life<=0 && (elem_properties&PROP_LIFE_KILL)) + { + // kill if no life + kill_part(i); + continue; + } + } + } + else + { + if (lastPartUnused<0) pfree = i; + else parts[lastPartUnused].life = i; + lastPartUnused = i; + } + } + if (lastPartUnused == -1) + { + if (parts_lastActiveIndex>=NPART-1) + pfree = -1; + else + pfree = parts_lastActiveIndex+1; + } + else + { + if (parts_lastActiveIndex>=NPART-1) + parts[lastPartUnused].life = -1; + else + parts[lastPartUnused].life = parts_lastActiveIndex+1; + } + parts_lastActiveIndex = lastPartUsed; +} + void Simulation::CheckStacking() { bool excessive_stacking_found = false; @@ -4808,15 +4905,7 @@ void Simulation::CheckStacking() //updates pmap, gol, and some other simulation stuff (but not particles) void Simulation::BeforeSim() { - int i, x, y, t; - int lastPartUsed = 0; - int lastPartUnused = -1; -#ifdef MT - int pt = 0, pc = 0; - pthread_t *InterThreads; -#endif - - if(!sys_pause||framerender) + if (!sys_pause||framerender) { air->update_air(); @@ -4854,91 +4943,15 @@ void Simulation::BeforeSim() sandcolour = (int)(20.0f*sin((float)sandcolour_frame*(M_PI/180.0f))); sandcolour_frame = (sandcolour_frame+1)%360; - memset(pmap, 0, sizeof(pmap)); - memset(pmap_count, 0, sizeof(pmap_count)); - memset(photons, 0, sizeof(photons)); - NUM_PARTS = 0; - for (i=0; i<=parts_lastActiveIndex; i++)//the particle loop that resets the pmap/photon maps every frame, to update them. - { - if (parts[i].type) - { - t = parts[i].type; - x = (int)(parts[i].x+0.5f); - y = (int)(parts[i].y+0.5f); - if (x>=0 && y>=0 && x=PT_NUM || !elements[t].Enabled) - { - kill_part(i); - continue; - } - - if (elementRecount) - elementCount[t]++; - - unsigned int elem_properties = elements[t].Properties; - if (parts[i].life>0 && (elem_properties&PROP_LIFE_DEC)) - { - // automatically decrease life - parts[i].life--; - if (parts[i].life<=0 && (elem_properties&(PROP_LIFE_KILL_DEC|PROP_LIFE_KILL))) - { - // kill on change to no life - kill_part(i); - continue; - } - } - else if (parts[i].life<=0 && (elem_properties&PROP_LIFE_KILL)) - { - // kill if no life - kill_part(i); - continue; - } - } - } - else - { - if (lastPartUnused<0) pfree = i; - else parts[lastPartUnused].life = i; - lastPartUnused = i; - } - } - if (lastPartUnused==-1) - { - if (parts_lastActiveIndex>=NPART-1) pfree = -1; - else pfree = parts_lastActiveIndex+1; - } - else - { - if (parts_lastActiveIndex>=NPART-1) parts[lastPartUnused].life = -1; - else parts[lastPartUnused].life = parts_lastActiveIndex+1; - } - parts_lastActiveIndex = lastPartUsed; - if (!sys_pause || framerender) + if (!sys_pause || framerender) { // decrease wall conduction, make walls block air and ambient heat - for (y=0; y0) + if (ISWIRE > 0) { for (int q = 0; q < (int)(MAX_TEMP-73.15f)/100+2; q++) { diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h index f91b958e2..c11486402 100644 --- a/src/simulation/Simulation.h +++ b/src/simulation/Simulation.h @@ -160,6 +160,7 @@ public: void create_arc(int sx, int sy, int dx, int dy, int midpoints, int variance, int type, int flags); void UpdateParticles(int start, int end); void SimulateGoL(); + void RecalcFreeParticles(); void CheckStacking(); void BeforeSim(); void AfterSim(); diff --git a/src/simulation/Snapshot.h b/src/simulation/Snapshot.h index 2ef4d3019..468b21a9b 100644 --- a/src/simulation/Snapshot.h +++ b/src/simulation/Snapshot.h @@ -13,9 +13,6 @@ public: std::vector AmbientHeat; std::vector Particles; - std::vector PortalParticles; - - std::vector WirelessData; std::vector GravVelocityX; std::vector GravVelocityY; @@ -28,6 +25,9 @@ public: std::vector FanVelocityX; std::vector FanVelocityY; + + std::vector PortalParticles; + std::vector WirelessData; std::vector stickmen; std::vector signs; @@ -37,8 +37,6 @@ public: AirVelocityY(), AmbientHeat(), Particles(), - PortalParticles(), - WirelessData(), GravVelocityX(), GravVelocityY(), GravValue(), @@ -47,6 +45,8 @@ public: ElecMap(), FanVelocityX(), FanVelocityY(), + PortalParticles(), + WirelessData(), stickmen(), signs() {