From aa2ec3d1624bb4dd88b276d8ada6aae8073571fc Mon Sep 17 00:00:00 2001 From: jacob1 Date: Thu, 12 Sep 2013 14:35:10 -0400 Subject: [PATCH] fix SOAP crashes by making sure particle id's are between 0 and NPART. Also cut off .life to between 0 and 65535 when saving --- src/client/GameSave.cpp | 12 +++++++++--- src/simulation/elements/SOAP.cpp | 22 ++++++++++++---------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index 9f524409c..e4dc1f772 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -1879,12 +1879,17 @@ char * GameSave::serialiseOPS(int & dataLength) //Life (optional), 1 to 2 bytes if(particles[i].life) { + int life = particles[i].life; + if (life > 0xFFFF) + life = 0xFFFF; + else if (life < 0) + life = 0; fieldDesc |= 1 << 1; - partsData[partsDataLen++] = particles[i].life; + partsData[partsDataLen++] = life; if(particles[i].life & 0xFF00) { fieldDesc |= 1 << 2; - partsData[partsDataLen++] = particles[i].life >> 8; + partsData[partsDataLen++] = life >> 8; } } @@ -1962,6 +1967,7 @@ char * GameSave::serialiseOPS(int & dataLength) } } + //Pavg, 4 bytes //Don't save pavg for things that break under pressure, because then they will break when the save is loaded, since pressure isn't also loaded if ((particles[i].pavg[0] || particles[i].pavg[1]) && !(particles[i].type == PT_QRTZ || particles[i].type == PT_GLAS || particles[i].type == PT_TUNG)) { @@ -1972,7 +1978,7 @@ char * GameSave::serialiseOPS(int & dataLength) partsData[partsDataLen++] = ((int)particles[i].pavg[1])>>8; } - //Write the field descriptor; + //Write the field descriptor partsData[fieldDescLoc] = fieldDesc; partsData[fieldDescLoc+1] = fieldDesc>>8; diff --git a/src/simulation/elements/SOAP.cpp b/src/simulation/elements/SOAP.cpp index fb882872f..2e721d169 100644 --- a/src/simulation/elements/SOAP.cpp +++ b/src/simulation/elements/SOAP.cpp @@ -161,7 +161,6 @@ int Element_SOAP::update(UPDATE_FUNC_ARGS) } else if (parts[r>>8].ctype == 7 && parts[i].tmp != r>>8 && parts[i].tmp2 != r>>8) { - int buf; parts[parts[i].tmp].tmp2 = parts[r>>8].tmp2; parts[parts[r>>8].tmp2].tmp = parts[i].tmp; parts[r>>8].tmp2 = i; @@ -181,17 +180,20 @@ int Element_SOAP::update(UPDATE_FUNC_ARGS) parts[i].vx += dx*d; parts[i].vy += dy*d; if ((parts[parts[i].tmp].ctype&2) && (parts[parts[i].tmp].ctype&1) + && (parts[parts[i].tmp].tmp >= 0 && parts[parts[i].tmp].tmp < NPART) && (parts[parts[parts[i].tmp].tmp].ctype&2) && (parts[parts[parts[i].tmp].tmp].ctype&1)) { - int ii; - ii = parts[parts[parts[i].tmp].tmp].tmp; - dx = parts[ii].x - parts[parts[i].tmp].x; - dy = parts[ii].y - parts[parts[i].tmp].y; - d = 81/(pow(dx, 2)+pow(dy, 2)+81)-0.5; - parts[parts[i].tmp].vx -= dx*d*0.5f; - parts[parts[i].tmp].vy -= dy*d*0.5f; - parts[ii].vx += dx*d*0.5f; - parts[ii].vy += dy*d*0.5f; + int ii = parts[parts[parts[i].tmp].tmp].tmp; + if (ii >= 0 && ii < NPART) + { + dx = parts[ii].x - parts[parts[i].tmp].x; + dy = parts[ii].y - parts[parts[i].tmp].y; + d = 81/(pow(dx, 2)+pow(dy, 2)+81)-0.5; + parts[parts[i].tmp].vx -= dx*d*0.5f; + parts[parts[i].tmp].vy -= dy*d*0.5f; + parts[ii].vx += dx*d*0.5f; + parts[ii].vy += dy*d*0.5f; + } } } }