diff --git a/src/simulation/elements/DRAY.cpp b/src/simulation/elements/DRAY.cpp index c543919e7..16607c4b6 100644 --- a/src/simulation/elements/DRAY.cpp +++ b/src/simulation/elements/DRAY.cpp @@ -72,7 +72,7 @@ static int update(UPDATE_FUNC_ARGS) if (TYP(r) == PT_SPRK && parts[ID(r)].life == 3) //spark found, start creating { bool overwrite = parts[ID(r)].ctype == PT_PSCN; - int partsRemaining = copyLength, xCopyTo, yCopyTo; //positions where the line will start being copied at + int partsRemaining = copyLength, xCopyTo = -1, yCopyTo = -1; //positions where the line will start being copied at int localCopyLength = copyLength; if (parts[ID(r)].ctype == PT_INWR && rx && ry) // INWR doesn't spark from diagonals @@ -83,6 +83,9 @@ static int update(UPDATE_FUNC_ARGS) bool isEnergy = false; for (int xStep = rx*-1, yStep = ry*-1, xCurrent = x+xStep, yCurrent = y+yStep; ; xCurrent+=xStep, yCurrent+=yStep) { + // Out of bounds, stop looking and don't copy anything + if (!sim->InBounds(xCurrent, yCurrent)) + break; int rr; // haven't found a particle yet, keep looking for one // the first particle it sees decides whether it will copy energy particles or not @@ -107,9 +110,8 @@ static int update(UPDATE_FUNC_ARGS) // Checks for when to stop: // 1: if .tmp isn't set, and the element in this spot is the ctype, then stop // 2: if .tmp is set, stop when the length limit reaches 0 - // 3. Stop when we are out of bounds if ((!localCopyLength && TYP(rr) == ctype && (ctype != PT_LIFE || parts[ID(rr)].ctype == ctypeExtra)) - || !(--partsRemaining && InBounds(xCurrent+xStep, yCurrent+yStep))) + || !--partsRemaining) { localCopyLength -= partsRemaining; xCopyTo = xCurrent + xStep*copySpaces;