]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Environment/Terrain.cpp
Fixed a few parentheses
[lugaru.git] / Source / Environment / Terrain.cpp
index d22ecdfc749cbc1c7ad05aaff15a7b65e4f5d8f9..6d195e204eaf9f5c39262b07c01b8a2d3f006af2 100644 (file)
@@ -635,7 +635,7 @@ bool Terrain::load(const std::string& fileName)
     Game::LoadingScreen();
 
     patch_size = size / subdivision;
-    patch_elements = (patch_size) * (patch_size) * 54;
+    patch_elements = patch_size * patch_size * 54;
     CalculateNormals();
 
     return true;
@@ -932,17 +932,17 @@ void Terrain::draw(int layer)
     viewdistsquared = viewdistance * viewdistance;
 
     //Only nearby blocks
-    beginx = (viewer.x - viewdistance) / (patch_size) - 1;
+    beginx = ((viewer.x - viewdistance) / patch_size) - 1;
     if (beginx < 0)
         beginx = 0;
-    beginz = (viewer.z - viewdistance) / (patch_size) - 1;
+    beginz = ((viewer.z - viewdistance) / patch_size) - 1;
     if (beginz < 0)
         beginz = 0;
 
-    endx = (viewer.x + viewdistance) / (patch_size) + 1;
+    endx = ((viewer.x + viewdistance) / patch_size) + 1;
     if (endx > subdivision)
         endx = subdivision;
-    endz = (viewer.z + viewdistance) / (patch_size) + 1;
+    endz = ((viewer.z + viewdistance) / patch_size) + 1;
     if (endz > subdivision)
         endz = subdivision;
 
@@ -1014,44 +1014,46 @@ void Terrain::drawdecals()
         for (unsigned int i = 0; i < decals.size(); i++) {
             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 != 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 == 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 == 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 == 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 == 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 == 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 == 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) && decals[i].type != lasttype) {
-                bloodtexture2.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) {
@@ -1126,47 +1128,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 +1166,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,7 +1200,7 @@ 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);
@@ -1249,8 +1239,6 @@ void Terrain::MakeDecalLock(int type, XYZ where, int whichx, int whichy, float s
 
 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 +1253,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,7 +1275,7 @@ void Terrain::DoShadows()
                 }
                 Game::LoadingScreen();
             }
-            brightness = dotproduct(&lightloc, &normals[i][j]);
+            float brightness = dotproduct(&lightloc, &normals[i][j]);
             if (shadowed)
                 brightness *= 1 - shadowed;
 
@@ -1312,11 +1300,11 @@ void Terrain::DoShadows()
     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 +1345,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 +1356,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 +1378,4 @@ Terrain::Terrain()
     memset(heightypatch, 0, sizeof(heightypatch));
 
     patch_elements = 0;
-
-    decals.clear();
 }