]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Environment/Terrain.cpp
Update copyright year to 2017
[lugaru.git] / Source / Environment / Terrain.cpp
index d22ecdfc749cbc1c7ad05aaff15a7b65e4f5d8f9..4874e337bcd1fa5e532597a414afa67a48fc5283 100644 (file)
@@ -1,6 +1,6 @@
 /*
 Copyright (C) 2003, 2010 - Wolfire Games
-Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
+Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)
 
 This file is part of Lugaru.
 
@@ -22,8 +22,8 @@ along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "Game.hpp"
 #include "Objects/Object.hpp"
-#include "Utils/Folders.hpp"
 #include "Tutorial.hpp"
+#include "Utils/Folders.hpp"
 
 extern XYZ viewer;
 extern float viewdistance;
@@ -42,7 +42,7 @@ extern bool skyboxtexture;
 
 //Functions
 
-int Terrain::lineTerrain(XYZ p1, XYZ p2, XYZ *p)
+int Terrain::lineTerrain(XYZ p1, XYZ p2, XYZp)
 {
     static int i, j, k;
     static float distance;
@@ -79,28 +79,36 @@ int Terrain::lineTerrain(XYZ p1, XYZ p2, XYZ *p)
         starty = i;
     }
 
-    if (startx < 0)
+    if (startx < 0) {
         startx = 0;
-    if (starty < 0)
+    }
+    if (starty < 0) {
         starty = 0;
-    if (endx > size - 1)
+    }
+    if (endx > size - 1) {
         endx = size - 1;
-    if (endy > size - 1)
+    }
+    if (endy > size - 1) {
         endy = size - 1;
+    }
 
     for (i = startx; i <= endx; i++) {
         for (j = starty; j <= endy; j++) {
             highest = -1000;
             lowest = 1000;
             for (k = 0; k < 2; k++) {
-                if (heightmap[i + k][j] > highest)
+                if (heightmap[i + k][j] > highest) {
                     highest = heightmap[i + k][j];
-                if (heightmap[i + k][j] < lowest)
+                }
+                if (heightmap[i + k][j] < lowest) {
                     lowest = heightmap[i + k][j];
-                if (heightmap[i + k][j + 1] > highest)
+                }
+                if (heightmap[i + k][j + 1] > highest) {
                     highest = heightmap[i + k][j + 1];
-                if (heightmap[i + k][j + 1] < lowest)
+                }
+                if (heightmap[i + k][j + 1] < lowest) {
                     lowest = heightmap[i + k][j + 1];
+                }
             }
             if ((p1.y <= highest || p2.y <= highest) && (p1.y >= lowest || p2.y >= lowest)) {
                 triangles[0].x = i;
@@ -169,8 +177,9 @@ void Terrain::UpdateTransparency(int whichx, int whichy)
                 vertex.z = j * scale;
                 vertex.y = heightmap[i][j] * scale;
                 distance = distsq(&viewer, &vertex);
-                if (distance > viewdistsquared)
+                if (distance > viewdistsquared) {
                     distance = viewdistsquared;
+                }
                 colors[i][j][3] = (viewdistsquared - (distance - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
             }
         }
@@ -246,8 +255,9 @@ void Terrain::UpdateTransparencyotherother(int whichx, int whichy)
                 vertex.z = j * scale;
                 vertex.y = heightmap[i][j] * scale;
                 distance = distsq(&viewer, &vertex);
-                if (distance > viewdistsquared)
+                if (distance > viewdistsquared) {
                     distance = viewdistsquared;
+                }
                 colors[i][j][3] = (viewdistsquared - (distance - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
             }
         }
@@ -277,7 +287,6 @@ void Terrain::UpdateVertexArray(int whichx, int whichy)
 {
     static int i, j, a, b, c, patch_size, stepsize;
 
-
     numtris[whichx][whichy] = 0;
 
     patch_size = size / subdivision;
@@ -355,19 +364,22 @@ void Terrain::UpdateVertexArray(int whichx, int whichy)
     minypatch[whichx][whichy] = 10000;
     for (a = 0; a < size / subdivision; a++) {
         for (b = 0; b < size / subdivision; b++) {
-            if (heightmap[(size / subdivision)*whichx + a][(size / subdivision)*whichy + b]*scale > maxypatch[whichx][whichy])
+            if (heightmap[(size / subdivision) * whichx + a][(size / subdivision) * whichy + b] * scale > maxypatch[whichx][whichy]) {
                 maxypatch[whichx][whichy] = heightmap[(size / subdivision) * whichx + a][(size / subdivision) * whichy + b] * scale;
-            if (heightmap[(size / subdivision)*whichx + a][(size / subdivision)*whichy + b]*scale < minypatch[whichx][whichy])
+            }
+            if (heightmap[(size / subdivision) * whichx + a][(size / subdivision) * whichy + b] * scale < minypatch[whichx][whichy]) {
                 minypatch[whichx][whichy] = heightmap[(size / subdivision) * whichx + a][(size / subdivision) * whichy + b] * scale;
+            }
         }
     }
     heightypatch[whichx][whichy] = (maxypatch[whichx][whichy] - minypatch[whichx][whichy]);
-    if (heightypatch[whichx][whichy] < size / subdivision * scale)
+    if (heightypatch[whichx][whichy] < size / subdivision * scale) {
         heightypatch[whichx][whichy] = size / subdivision * scale;
+    }
     avgypatch[whichx][whichy] = (minypatch[whichx][whichy] + maxypatch[whichx][whichy]) / 2;
 
-    for (i = whichx * size / subdivision; i < (whichx + 1)*size / subdivision - 1; i++) {
-        for (j = whichy * size / subdivision; j < (whichy + 1)*size / subdivision - 1; j++) {
+    for (i = whichx * size / subdivision; i < (whichx + 1) * size / subdivision - 1; i++) {
+        for (j = whichy * size / subdivision; j < (whichy + 1) * size / subdivision - 1; j++) {
             triangles[(i * (size - 1) * 2) + (j * 2)][0].x = i * scale;
             triangles[(i * (size - 1) * 2) + (j * 2)][0].y = heightmap[i][j] * scale;
             triangles[(i * (size - 1) * 2) + (j * 2)][0].z = j * scale;
@@ -393,10 +405,8 @@ void Terrain::UpdateVertexArray(int whichx, int whichy)
             triangles[(i * (size - 1) * 2) + (j * 2) + 1][2].z = j * scale + 1 * scale;
         }
     }
-
 }
 
-
 bool Terrain::load(const std::string& fileName)
 {
     static long i, j;
@@ -448,7 +458,6 @@ bool Terrain::load(const std::string& fileName)
     }
     Game::LoadingScreen();
 
-
     for (i = 0; i < size; i++) {
         for (j = 0; j < size; j++) {
             heightmap[i][j] *= .5;
@@ -462,8 +471,9 @@ bool Terrain::load(const std::string& fileName)
                     slopeness = heightmap[i][j] - heightmap[i][j - 1];
                 }
                 opacityother[i][j] = slopeness * slopeness * 2;
-                if (opacityother[i][j] > 1)
+                if (opacityother[i][j] > 1) {
                     opacityother[i][j] = 1;
+                }
                 opacityother[i][j] -= (float)abs(Random() % 100) / 300;
             }
             if (environment == desertenvironment) {
@@ -471,8 +481,9 @@ bool Terrain::load(const std::string& fileName)
                     slopeness = heightmap[i][j] - heightmap[i][j - 1];
                 }
                 opacityother[i][j] = slopeness * slopeness * 2;
-                if (opacityother[i][j] > 1)
+                if (opacityother[i][j] > 1) {
                     opacityother[i][j] = 1;
+                }
                 opacityother[i][j] -= (float)abs(Random() % 100) / 300;
             }
             if (environment == grassyenvironment) {
@@ -489,8 +500,9 @@ bool Terrain::load(const std::string& fileName)
                     slopeness = heightmap[i][j] - heightmap[i][j + 1];
                 }
                 opacityother[i][j] = slopeness * slopeness * 10;
-                if (opacityother[i][j] > 1)
+                if (opacityother[i][j] > 1) {
                     opacityother[i][j] = 1;
+                }
                 opacityother[i][j] -= (float)abs(Random() % 100) / 100;
             }
         }
@@ -511,55 +523,72 @@ bool Terrain::load(const std::string& fileName)
 
     for (i = 0; i < size; i++) {
         for (j = 0; j < size; j++) {
-            if (opacityother[i][j] < .1)
+            if (opacityother[i][j] < .1) {
                 opacityother[i][j] = 0;
+            }
             if (textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == -1) {
-                if (!opacityother[i][j])
+                if (!opacityother[i][j]) {
                     textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = allfirst;
-                if (opacityother[i][j] == 1)
+                }
+                if (opacityother[i][j] == 1) {
                     textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = allsecond;
+                }
             }
-            if (opacityother[i][j] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+            if (opacityother[i][j] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst) {
                 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-            if (opacityother[i][j] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+            }
+            if (opacityother[i][j] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond) {
                 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+            }
 
             x = i;
             y = j;
             if (i > 0) {
                 i--;
-                if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+                if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst) {
                     textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+                }
+                if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond) {
                     textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+                }
+                if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst) {
                     textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
-                if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+                }
+                if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond) {
                     textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+                }
 
                 if (j > 0) {
                     j--;
-                    if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+                    if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst) {
                         textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                    if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+                    }
+                    if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond) {
                         textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                    if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+                    }
+                    if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst) {
                         textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
-                    if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+                    }
+                    if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond) {
                         textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+                    }
                     j++;
                 }
 
                 if (j < size - 1) {
                     j++;
-                    if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+                    if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst) {
                         textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                    if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+                    }
+                    if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond) {
                         textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                    if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+                    }
+                    if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst) {
                         textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
-                    if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+                    }
+                    if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond) {
                         textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+                    }
                     j--;
                 }
                 i++;
@@ -567,38 +596,50 @@ bool Terrain::load(const std::string& fileName)
 
             if (i < size - 1) {
                 i++;
-                if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+                if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst) {
                     textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+                }
+                if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond) {
                     textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+                }
+                if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst) {
                     textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
-                if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+                }
+                if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond) {
                     textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+                }
 
                 if (j > 0) {
                     j--;
-                    if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+                    if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst) {
                         textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                    if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+                    }
+                    if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond) {
                         textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                    if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+                    }
+                    if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst) {
                         textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
-                    if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+                    }
+                    if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond) {
                         textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+                    }
                     j++;
                 }
 
                 if (j < size - 1) {
                     j++;
-                    if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+                    if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst) {
                         textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                    if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+                    }
+                    if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond) {
                         textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                    if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+                    }
+                    if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst) {
                         textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
-                    if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+                    }
+                    if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond) {
                         textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+                    }
                     j--;
                 }
                 i--;
@@ -606,36 +647,43 @@ bool Terrain::load(const std::string& fileName)
 
             if (j > 0) {
                 j--;
-                if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+                if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst) {
                     textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+                }
+                if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond) {
                     textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+                }
+                if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst) {
                     textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
-                if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+                }
+                if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond) {
                     textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+                }
                 j++;
             }
 
             if (j < size - 1) {
                 j++;
-                if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+                if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst) {
                     textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+                }
+                if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond) {
                     textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
-                if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+                }
+                if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst) {
                     textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
-                if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+                }
+                if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond) {
                     textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+                }
                 j--;
-
             }
         }
     }
     Game::LoadingScreen();
 
     patch_size = size / subdivision;
-    patch_elements = (patch_size) * (patch_size) * 54;
+    patch_elements = patch_size * patch_size * 54;
     CalculateNormals();
 
     return true;
@@ -682,7 +730,6 @@ void Terrain::CalculateNormals()
             normals[i][j + 1] = normals[i][j + 1] + facenormal;
             normals[i + 1][j] = normals[i + 1][j] + facenormal;
 
-
             a.x = i + 1;
             a.y = heightmap[i + 1][j];
             a.z = j;
@@ -719,8 +766,9 @@ void Terrain::CalculateNormals()
 
 void Terrain::drawpatch(int whichx, int whichy, float opacity)
 {
-    if (opacity >= 1)
+    if (opacity >= 1) {
         glDisable(GL_BLEND);
+    }
     if (opacity < 1) {
         glEnable(GL_BLEND);
         UpdateTransparency(whichx, whichy);
@@ -800,7 +848,6 @@ void Terrain::drawpatchotherother(int whichx, int whichy)
     glMatrixMode(GL_MODELVIEW);
 }
 
-
 float Terrain::getHeight(float pointx, float pointz)
 {
     static int tilex, tiley;
@@ -809,8 +856,9 @@ float Terrain::getHeight(float pointx, float pointz)
     pointx /= scale;
     pointz /= scale;
 
-    if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
+    if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0) {
         return 0;
+    }
 
     startpoint.x = pointx;
     startpoint.y = -1000;
@@ -859,8 +907,9 @@ float Terrain::getOpacity(float pointx, float pointz)
     pointx /= scale;
     pointz /= scale;
 
-    if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
+    if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0) {
         return 0;
+    }
 
     tilex = pointx;
     tiley = pointz;
@@ -880,8 +929,9 @@ XYZ Terrain::getNormal(float pointx, float pointz)
     pointz /= scale;
 
     height1 = 0;
-    if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
+    if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0) {
         return height1;
+    }
     tilex = pointx;
     tiley = pointz;
 
@@ -901,8 +951,9 @@ XYZ Terrain::getLighting(float pointx, float pointz)
     pointz /= scale;
 
     height1 = 0;
-    if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
+    if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0) {
         return height1;
+    }
     tilex = pointx;
     tiley = pointz;
 
@@ -932,19 +983,23 @@ void Terrain::draw(int layer)
     viewdistsquared = viewdistance * viewdistance;
 
     //Only nearby blocks
-    beginx = (viewer.x - viewdistance) / (patch_size) - 1;
-    if (beginx < 0)
+    beginx = ((viewer.x - viewdistance) / patch_size) - 1;
+    if (beginx < 0) {
         beginx = 0;
-    beginz = (viewer.z - viewdistance) / (patch_size) - 1;
-    if (beginz < 0)
+    }
+    beginz = ((viewer.z - viewdistance) / patch_size) - 1;
+    if (beginz < 0) {
         beginz = 0;
+    }
 
-    endx = (viewer.x + viewdistance) / (patch_size) + 1;
-    if (endx > subdivision)
+    endx = ((viewer.x + viewdistance) / patch_size) + 1;
+    if (endx > subdivision) {
         endx = subdivision;
-    endz = (viewer.z + viewdistance) / (patch_size) + 1;
-    if (endz > subdivision)
+    }
+    endz = ((viewer.z + viewdistance) / patch_size) + 1;
+    if (endz > subdivision) {
         endz = subdivision;
+    }
 
     if (!layer) {
         for (i = beginx; i < endx; i++) {
@@ -960,37 +1015,49 @@ void Terrain::draw(int layer)
         for (j = beginz; j < endz; j++) {
             if (distance[i][j] < (viewdistance + patch_size) * (viewdistance + patch_size)) {
                 opacity = 1;
-                if (distance[i][j] > viewdistsquared * fadestart - viewdistsquared)
+                if (distance[i][j] > viewdistsquared * fadestart - viewdistsquared) {
                     opacity = 0;
-                if (opacity == 1 && i != subdivision)
-                    if (distance[i + 1][j] > viewdistsquared * fadestart - viewdistsquared)
+                }
+                if (opacity == 1 && i != subdivision) {
+                    if (distance[i + 1][j] > viewdistsquared * fadestart - viewdistsquared) {
                         opacity = 0;
-                if (opacity == 1 && j != subdivision)
-                    if (distance[i][j + 1] > viewdistsquared * fadestart - viewdistsquared)
+                    }
+                }
+                if (opacity == 1 && j != subdivision) {
+                    if (distance[i][j + 1] > viewdistsquared * fadestart - viewdistsquared) {
                         opacity = 0;
-                if (opacity == 1 && j != subdivision && i != subdivision)
-                    if (distance[i + 1][j + 1] > viewdistsquared * fadestart - viewdistsquared)
+                    }
+                }
+                if (opacity == 1 && j != subdivision && i != subdivision) {
+                    if (distance[i + 1][j + 1] > viewdistsquared * fadestart - viewdistsquared) {
                         opacity = 0;
+                    }
+                }
                 glMatrixMode(GL_MODELVIEW);
                 glPushMatrix();
                 if (frustum.CubeInFrustum(i * patch_size + patch_size * .5, avgypatch[i][j], j * patch_size + patch_size * .5, heightypatch[i][j] / 2)) {
-                    if (environment == desertenvironment && distance[i][j] > viewdistsquared / 4)
-                        glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, blurness);
-                    else if (environment == desertenvironment)
-                        glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
-                    if (!layer && textureness[i][j] != allsecond)
+                    if (environment == desertenvironment && distance[i][j] > viewdistsquared / 4) {
+                        glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, blurness);
+                    } else if (environment == desertenvironment) {
+                        glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0);
+                    }
+                    if (!layer && textureness[i][j] != allsecond) {
                         drawpatch(i, j, opacity);
-                    if (layer == 1 && textureness[i][j] != allfirst)
+                    }
+                    if (layer == 1 && textureness[i][j] != allfirst) {
                         drawpatchother(i, j, opacity);
-                    if (layer == 2 && textureness[i][j] != allfirst)
+                    }
+                    if (layer == 2 && textureness[i][j] != allfirst) {
                         drawpatchotherother(i, j);
+                    }
                 }
                 glPopMatrix();
             }
         }
     }
-    if (environment == desertenvironment)
-        glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
+    if (environment == desertenvironment) {
+        glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0);
+    }
 }
 
 void Terrain::drawdecals()
@@ -1012,83 +1079,94 @@ void Terrain::drawdecals()
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
         glDepthMask(0);
         for (unsigned int i = 0; i < decals.size(); i++) {
-            if (decals[i].type == blooddecalfast && decals[i].alivetime < 2)
+            if (decals[i].type == blooddecalfast && decals[i].alivetime < 2) {
                 decals[i].alivetime = 2;
-            if ((decals[i].type == shadowdecal || decals[i].type == shadowdecalpermanent) && decals[i].type != lasttype) {
-                shadowtexture.bind();
-                if (!blend) {
-                    blend = 1;
-                    glAlphaFunc(GL_GREATER, 0.0001);
-                    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-                }
             }
-            if (decals[i].type == footprintdecal && decals[i].type != lasttype) {
-                footprinttexture.bind();
-                if (!blend) {
-                    blend = 1;
-                    glAlphaFunc(GL_GREATER, 0.0001);
-                    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+            if (decals[i].type != lasttype) {
+                if (decals[i].type == shadowdecal || decals[i].type == shadowdecalpermanent) {
+                    shadowtexture.bind();
+                    if (!blend) {
+                        blend = 1;
+                        glAlphaFunc(GL_GREATER, 0.0001);
+                        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+                    }
                 }
-            }
-            if (decals[i].type == bodyprintdecal && decals[i].type != lasttype) {
-                bodyprinttexture.bind();
-                if (!blend) {
-                    blend = 1;
-                    glAlphaFunc(GL_GREATER, 0.0001);
-                    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+                if (decals[i].type == footprintdecal) {
+                    footprinttexture.bind();
+                    if (!blend) {
+                        blend = 1;
+                        glAlphaFunc(GL_GREATER, 0.0001);
+                        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+                    }
                 }
-            }
-            if ((decals[i].type == blooddecal || decals[i].type == blooddecalslow) && decals[i].type != lasttype) {
-                bloodtexture.bind();
-                if (blend) {
-                    blend = 0;
-                    glAlphaFunc(GL_GREATER, 0.15);
-                    glBlendFunc(GL_ONE, GL_ZERO);
+                if (decals[i].type == bodyprintdecal) {
+                    bodyprinttexture.bind();
+                    if (!blend) {
+                        blend = 1;
+                        glAlphaFunc(GL_GREATER, 0.0001);
+                        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+                    }
                 }
-            }
-            if ((decals[i].type == blooddecalfast) && decals[i].type != lasttype) {
-                bloodtexture2.bind();
-                if (blend) {
-                    blend = 0;
-                    glAlphaFunc(GL_GREATER, 0.15);
-                    glBlendFunc(GL_ONE, GL_ZERO);
+                if (decals[i].type == blooddecal || decals[i].type == blooddecalslow) {
+                    bloodtexture.bind();
+                    if (blend) {
+                        blend = 0;
+                        glAlphaFunc(GL_GREATER, 0.15);
+                        glBlendFunc(GL_ONE, GL_ZERO);
+                    }
+                }
+                if (decals[i].type == blooddecalfast) {
+                    bloodtexture2.bind();
+                    if (blend) {
+                        blend = 0;
+                        glAlphaFunc(GL_GREATER, 0.15);
+                        glBlendFunc(GL_ONE, GL_ZERO);
+                    }
                 }
             }
             if (decals[i].type == shadowdecal || decals[i].type == shadowdecalpermanent) {
                 distancemult = (viewdistsquared - (distsq(&viewer, &decals[i].position) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
-                if (distancemult >= 1)
+                if (distancemult >= 1) {
                     glColor4f(1, 1, 1, decals[i].opacity);
-                if (distancemult < 1)
-                    glColor4f(1, 1, 1, decals[i].opacity*distancemult);
+                }
+                if (distancemult < 1) {
+                    glColor4f(1, 1, 1, decals[i].opacity * distancemult);
+                }
             }
             if (decals[i].type == footprintdecal || decals[i].type == bodyprintdecal) {
                 distancemult = (viewdistsquared - (distsq(&viewer, &decals[i].position) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
                 if (distancemult >= 1) {
                     glColor4f(1, 1, 1, decals[i].opacity);
-                    if (decals[i].alivetime > 3)
+                    if (decals[i].alivetime > 3) {
                         glColor4f(1, 1, 1, decals[i].opacity * (5 - decals[i].alivetime) / 2);
+                    }
                 }
                 if (distancemult < 1) {
-                    glColor4f(1, 1, 1, decals[i].opacity*distancemult);
-                    if (decals[i].alivetime > 3)
+                    glColor4f(1, 1, 1, decals[i].opacity * distancemult);
+                    if (decals[i].alivetime > 3) {
                         glColor4f(1, 1, 1, decals[i].opacity * (5 - decals[i].alivetime) / 2 * distancemult);
+                    }
                 }
             }
             if ((decals[i].type == blooddecal || decals[i].type == blooddecalfast || decals[i].type == blooddecalslow)) {
                 distancemult = (viewdistsquared - (distsq(&viewer, &decals[i].position) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
                 if (distancemult >= 1) {
                     glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity);
-                    if (decals[i].alivetime < 4)
-                        glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity*decals[i].alivetime*.25);
-                    if (decals[i].alivetime > 58)
+                    if (decals[i].alivetime < 4) {
+                        glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity * decals[i].alivetime * .25);
+                    }
+                    if (decals[i].alivetime > 58) {
                         glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity * (60 - decals[i].alivetime) / 2);
+                    }
                 }
                 if (distancemult < 1) {
-                    glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity*distancemult);
-                    if (decals[i].alivetime < 4)
-                        glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity*decals[i].alivetime*distancemult * .25);
-                    if (decals[i].alivetime > 58)
+                    glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity * distancemult);
+                    if (decals[i].alivetime < 4) {
+                        glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity * decals[i].alivetime * distancemult * .25);
+                    }
+                    if (decals[i].alivetime > 58) {
                         glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity * (60 - decals[i].alivetime) / 2 * distancemult);
+                    }
                 }
             }
             lasttype = decals[i].type;
@@ -1107,18 +1185,24 @@ void Terrain::drawdecals()
         }
         for (int i = decals.size() - 1; i >= 0; i--) {
             decals[i].alivetime += multiplier;
-            if (decals[i].type == blooddecalslow)
+            if (decals[i].type == blooddecalslow) {
                 decals[i].alivetime -= multiplier * 2 / 3;
-            if (decals[i].type == blooddecalfast)
+            }
+            if (decals[i].type == blooddecalfast) {
                 decals[i].alivetime += multiplier * 4;
-            if (decals[i].type == shadowdecal)
+            }
+            if (decals[i].type == shadowdecal) {
                 DeleteDecal(i);
-            if (decals[i].type == footprintdecal && decals[i].alivetime >= 5)
+            }
+            if (decals[i].type == footprintdecal && decals[i].alivetime >= 5) {
                 DeleteDecal(i);
-            if (decals[i].type == bodyprintdecal && decals[i].alivetime >= 5)
+            }
+            if (decals[i].type == bodyprintdecal && decals[i].alivetime >= 5) {
                 DeleteDecal(i);
-            if ((decals[i].type == blooddecal || decals[i].type == blooddecalfast || decals[i].type == blooddecalslow) && decals[i].alivetime >= 60)
+            }
+            if ((decals[i].type == blooddecal || decals[i].type == blooddecalfast || decals[i].type == blooddecalslow) && decals[i].alivetime >= 60) {
                 DeleteDecal(i);
+            }
         }
         glAlphaFunc(GL_GREATER, 0.0001);
     }
@@ -1126,47 +1210,35 @@ void Terrain::drawdecals()
 
 void Terrain::deleteDeadDecals()
 {
-    for (unsigned int i = 0; i < decals.size(); ) {
+    for (int i = decals.size() - 1; i >= 0; i--) {
         if ((decals[i].type == blooddecal || decals[i].type == blooddecalslow) && decals[i].alivetime < 2) {
             DeleteDecal(i);
-        } else {
-            i++;
         }
     }
 }
 
 void Terrain::AddObject(XYZ where, float radius, int id)
 {
-    XYZ points[4];
-    if (id >= 0 && id < 10000)
+    XYZ points[2];
+    if (id >= 0 && id < 10000) {
         for (int i = 0; i < subdivision; i++) {
             for (int j = 0; j < subdivision; j++) {
-                if (patchobjectnum[i][j] < 300 - 1) {
-                    bool done = false;
+                if (patchobjects[i][j].size() < 300 - 1) {
                     points[0].x = (size / subdivision) * i;
                     points[0].z = (size / subdivision) * j;
                     points[0].y = heightmap[(int)points[0].x][(int)points[0].z];
                     points[1].x = (size / subdivision) * (i + 1);
-                    points[1].z = (size / subdivision) * j;
+                    points[1].z = (size / subdivision) * (j + 1);
                     points[1].y = heightmap[(int)points[1].x][(int)points[1].z];
-                    points[2].x = (size / subdivision) * (i + 1);
-                    points[2].z = (size / subdivision) * (j + 1);
-                    points[2].y = heightmap[(int)points[2].x][(int)points[2].z];
-                    points[3].x = (size / subdivision) * i;
-                    points[3].z = (size / subdivision) * (j + 1);
-                    points[3].y = heightmap[(int)points[3].x][(int)points[3].z];
                     points[0] *= scale;
                     points[1] *= scale;
-                    points[2] *= scale;
-                    points[3] *= scale;
-                    if (!done && where.x + radius > points[0].x && where.x - radius < points[2].x && where.z + radius > points[0].z && where.z - radius < points[2].z) {
-                        patchobjects[i][j][patchobjectnum[i][j]] = id;
-                        patchobjectnum[i][j]++;
-                        done = 1;
+                    if (where.x + radius > points[0].x && where.x - radius < points[1].x && where.z + radius > points[0].z && where.z - radius < points[1].z) {
+                        patchobjects[i][j].push_back(id);
                     }
                 }
             }
         }
+    }
 }
 
 void Terrain::DeleteDecal(int which)
@@ -1176,7 +1248,7 @@ void Terrain::DeleteDecal(int which)
     }
 }
 
-void Terrain::MakeDecal(int type, XYZ where, float size, float opacity, float rotation)
+void Terrain::MakeDecal(decal_type type, XYZ where, float size, float opacity, float rotation)
 {
     if (decalstoggle) {
         if (opacity > 0 && size > 0) {
@@ -1210,14 +1282,15 @@ void Terrain::MakeDecal(int type, XYZ where, float size, float opacity, float ro
     }
 }
 
-void Terrain::MakeDecalLock(int type, XYZ where, int whichx, int whichy, float size, float opacity, float rotation)
+void Terrain::MakeDecalLock(decal_type type, XYZ where, int whichx, int whichy, float size, float opacity, float rotation)
 {
     if (decalstoggle) {
         XYZ rot = getLighting(where.x, where.z);
         float decalbright = (rot.x + rot.y + rot.z) / 3;
 
-        if (decalbright < .4)
+        if (decalbright < .4) {
             decalbright = .4;
+        }
 
         if (environment == grassyenvironment) {
             decalbright *= .6;
@@ -1229,28 +1302,36 @@ void Terrain::MakeDecalLock(int type, XYZ where, int whichx, int whichy, float s
 
         Decal decal(where, type, opacity, rotation, decalbright, whichx, whichy, size, *this, true);
 
-        if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0))
-            if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0))
-                if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1))
-                    if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1))
-                        if (decals.size() < max_decals - 1)
+        if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0)) {
+            if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0)) {
+                if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1)) {
+                    if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) {
+                        if (decals.size() < max_decals - 1) {
                             decals.push_back(decal);
+                        }
+                    }
+                }
+            }
+        }
 
         Decal decal2(where, type, opacity, rotation, decalbright, whichx, whichy, size, *this, false);
 
-        if (!(decal2.texcoords[0][0] < 0 && decal2.texcoords[1][0] < 0 && decal2.texcoords[2][0] < 0))
-            if (!(decal2.texcoords[0][1] < 0 && decal2.texcoords[1][1] < 0 && decal2.texcoords[2][1] < 0))
-                if (!(decal2.texcoords[0][0] > 1 && decal2.texcoords[1][0] > 1 && decal2.texcoords[2][0] > 1))
-                    if (!(decal2.texcoords[0][1] > 1 && decal2.texcoords[1][1] > 1 && decal2.texcoords[2][1] > 1))
-                        if (decals.size() < max_decals - 1)
+        if (!(decal2.texcoords[0][0] < 0 && decal2.texcoords[1][0] < 0 && decal2.texcoords[2][0] < 0)) {
+            if (!(decal2.texcoords[0][1] < 0 && decal2.texcoords[1][1] < 0 && decal2.texcoords[2][1] < 0)) {
+                if (!(decal2.texcoords[0][0] > 1 && decal2.texcoords[1][0] > 1 && decal2.texcoords[2][0] > 1)) {
+                    if (!(decal2.texcoords[0][1] > 1 && decal2.texcoords[1][1] > 1 && decal2.texcoords[2][1] > 1)) {
+                        if (decals.size() < max_decals - 1) {
                             decals.push_back(decal2);
+                        }
+                    }
+                }
+            }
+        }
     }
 }
 
 void Terrain::DoShadows()
 {
-    static int i, j, k, l, todivide;
-    static float brightness, total;
     static XYZ testpoint, testpoint2, terrainpoint, lightloc, col;
     lightloc = light.location;
     if (!skyboxtexture) {
@@ -1265,18 +1346,18 @@ void Terrain::DoShadows()
     float shadowed;
     Normalise(&lightloc);
     //Calculate shadows
-    for (i = 0; i < size; i++) {
-        for (j = 0; j < size; j++) {
-            terrainpoint.x = (float)(i) * scale;
-            terrainpoint.z = (float)(j) * scale;
+    for (short int i = 0; i < size; i++) {
+        for (short int j = 0; j < size; j++) {
+            terrainpoint.x = (float)i * scale;
+            terrainpoint.z = (float)j * scale;
             terrainpoint.y = heightmap[i][j] * scale;
 
             shadowed = 0;
-            patchx = (float)(i) * subdivision / size;
-            patchz = (float)(j) * subdivision / size;
-            if (patchobjectnum[patchx][patchz]) {
-                for (k = 0; k < patchobjectnum[patchx][patchz]; k++) {
-                    l = patchobjects[patchx][patchz][k];
+            patchx = (float)i * subdivision / size;
+            patchz = (float)j * subdivision / size;
+            if (patchobjects[patchx][patchz].size()) {
+                for (unsigned int k = 0; k < patchobjects[patchx][patchz].size(); k++) {
+                    unsigned int l = patchobjects[patchx][patchz][k];
                     if (Object::objects[l]->type != treetrunktype) {
                         testpoint = terrainpoint;
                         testpoint2 = terrainpoint + lightloc * 50 * (1 - shadowed);
@@ -1287,36 +1368,51 @@ void Terrain::DoShadows()
                 }
                 Game::LoadingScreen();
             }
-            brightness = dotproduct(&lightloc, &normals[i][j]);
-            if (shadowed)
+            float brightness = dotproduct(&lightloc, &normals[i][j]);
+            if (shadowed) {
                 brightness *= 1 - shadowed;
+            }
 
-            if (brightness > 1)
+            if (brightness > 1) {
                 brightness = 1;
-            if (brightness < 0)
+            }
+            if (brightness < 0) {
                 brightness = 0;
+            }
 
             colors[i][j][0] = light.color[0] * brightness + light.ambient[0];
             colors[i][j][1] = light.color[1] * brightness + light.ambient[1];
             colors[i][j][2] = light.color[2] * brightness + light.ambient[2];
 
-            if (colors[i][j][0] > 1) colors[i][j][0] = 1;
-            if (colors[i][j][1] > 1) colors[i][j][1] = 1;
-            if (colors[i][j][2] > 1) colors[i][j][2] = 1;
-            if (colors[i][j][0] < 0) colors[i][j][0] = 0;
-            if (colors[i][j][1] < 0) colors[i][j][1] = 0;
-            if (colors[i][j][2] < 0) colors[i][j][2] = 0;
+            if (colors[i][j][0] > 1) {
+                colors[i][j][0] = 1;
+            }
+            if (colors[i][j][1] > 1) {
+                colors[i][j][1] = 1;
+            }
+            if (colors[i][j][2] > 1) {
+                colors[i][j][2] = 1;
+            }
+            if (colors[i][j][0] < 0) {
+                colors[i][j][0] = 0;
+            }
+            if (colors[i][j][1] < 0) {
+                colors[i][j][1] = 0;
+            }
+            if (colors[i][j][2] < 0) {
+                colors[i][j][2] = 0;
+            }
         }
     }
 
     Game::LoadingScreen();
 
     //Smooth shadows
-    for (i = 0; i < size; i++) {
-        for (j = 0; j < size; j++) {
-            for (k = 0; k < 3; k++) {
-                total = 0;
-                todivide = 0;
+    for (short int i = 0; i < size; i++) {
+        for (short int j = 0; j < size; j++) {
+            for (short int k = 0; k < 3; k++) {
+                float total = 0;
+                unsigned int todivide = 0;
                 if (i != 0) {
                     total += colors[j][i - 1][k];
                     todivide++;
@@ -1357,8 +1453,8 @@ void Terrain::DoShadows()
         }
     }
 
-    for (i = 0; i < subdivision; i++) {
-        for (j = 0; j < subdivision; j++) {
+    for (unsigned int i = 0; i < subdivision; i++) {
+        for (unsigned int j = 0; j < subdivision; j++) {
             UpdateVertexArray(i, j);
         }
     }
@@ -1368,9 +1464,6 @@ Terrain::Terrain()
 {
     size = 0;
 
-    memset(patchobjectnum, 0, sizeof(patchobjectnum));
-    memset(patchobjects, 0, sizeof(patchobjects));
-
     scale = 1.0f;
     type = 0;
     memset(heightmap, 0, sizeof(heightmap));
@@ -1393,6 +1486,4 @@ Terrain::Terrain()
     memset(heightypatch, 0, sizeof(heightypatch));
 
     patch_elements = 0;
-
-    decals.clear();
 }