Added Terrain::generateTerrain, basicly a straight copy from the old one.
authorsaiam
Sun, 30 Nov 2008 13:31:32 +0000
changeset 143 45565385d972
parent 142 00672d0682ac
child 144 d02f642625ec
Added Terrain::generateTerrain, basicly a straight copy from the old one.
src/proto2/Terrain.cc
src/proto2/Terrain.hh
--- a/src/proto2/Terrain.cc	Sun Nov 30 13:17:37 2008 +0000
+++ b/src/proto2/Terrain.cc	Sun Nov 30 13:31:32 2008 +0000
@@ -13,6 +13,49 @@
     this->generatePixelBuffer();
 }
 
+void Terrain::generatePixelBuffer() {
+    // These could be somewhere else
+    const CL_Color colorEmpty(86, 41, 0);
+    const CL_Color colorDirt(144, 82, 23);
+    const CL_Color colorRock(132, 136, 135);
+
+    this->pixbuf = CL_PixelBuffer(MAP_WIDTH, MAP_HEIGHT, 4*MAP_WIDTH, 
+                                  CL_PixelFormat::rgba8888);
+
+    CL_Color color;
+    for (uint16_t i = 0; i < MAP_WIDTH; i++) {
+        for (uint16_t j = 0; j < MAP_HEIGHT; j++) {
+            switch(terrain[i][j]) {
+            case EMPTY:
+                color = colorEmpty;
+                break;
+            case DIRT:
+                color = colorDirt;
+                break;
+            case ROCK:
+                color = colorRock;
+                break;
+            default: // TODO: Shouldn't be here.
+                break; 
+            }
+            this->pixbuf.draw_pixel(i, j, color);
+        }
+    }
+}
+
+Vector Terrain::getPixelLocation(Vector point) {
+    Vector scal(scale.x/MAP_WIDTH, scale.y/MAP_HEIGHT);
+    return Vector((int)round(point.x/scal.x), 
+                  (int)round(point.y/scal.y));
+}
+
+TerrainType Terrain::getType(uint16_t x, uint16_t y) {
+    if ((x < 0) || (y < 0) ||(x >= MAP_WIDTH) || (y >= MAP_HEIGHT)) {
+        return ROCK;
+    }
+    return terrain[x][y];
+}
+
 bool Terrain::collides(const Vector &point) const {
     Vector coor = getPixelLocation(point);
     return (getType(coor.x, coor.y) != EMPTY);
@@ -69,47 +112,52 @@
     // Remember also to redraw the pixelbuffer
 }
 
-Vector Terrain::getPixelLocation(Vector point) {
-    Vector scal(scale.x/MAP_WIDTH, scale.y/MAP_HEIGHT);
-    return Vector((int)round(point.x/scal.x), 
-                  (int)round(point.y/scal.y));
-}
-
-TerrainType Terrain::getType(uint16_t x, uint16_t y) {
-    if ((x < 0) || (y < 0) ||(x >= MAP_WIDTH) || (y >= MAP_HEIGHT)) {
-        return ROCK;
-    }
-    return terrain[x][y];
-}
-
-void Terrain::generatePixelBuffer() {
-    // These could be somewhere else
-    const CL_Color colorEmpty(86, 41, 0);
-    const CL_Color colorDirt(144, 82, 23);
-    const CL_Color colorRock(132, 136, 135);
 
-    this->pixbuf = CL_PixelBuffer(MAP_WIDTH, MAP_HEIGHT, 4*MAP_WIDTH, 
-                                  CL_PixelFormat::rgba8888);
+// TODO: This could better :)
+// TODO: And this need some cleaning :)
+void Terrain::generateTerrain(int seed) {
+    srand(seed); // Set random number generator seed.
 
-    CL_Color color;
-    for (uint16_t i = 0; i < MAP_WIDTH; i++) {
-        for (uint16_t j = 0; j < MAP_HEIGHT; j++) {
-            switch(terrain[i][j]) {
-            case EMPTY:
-                color = colorEmpty;
-                break;
-            case DIRT:
-                color = colorDirt;
-                break;
-            case ROCK:
-                color = colorRock;
-                break;
-            default: // TODO: Shouldn't be here.
-                break; 
-            }
-            this->pixbuf.draw_pixel(i, j, color);
+    // Some constants to control random generation
+    const int min_range = 25;
+    const int max_range = 80;
+    const int num = 50;
+    const int rock_rarity = 4;
+
+    // Generate circles (or whatever)
+    for (int i = 0; i < num; i++) {
+        // Random generate circle attributes
+        int midx = rand()%MAP_WIDTH;
+        int midy = rand()%MAP_HEIGHT;
+        int range = rand()%(max_range-min_range)+min_range;
+
+        // Make sure that there's a circle in the midle of the cave
+        if (i == 0) {
+            midx = MAP_WIDTH/2;
+            midy = MAP_WIDTH/2;
+            range = 150;
         }
+
+        TerrainType type = EMPTY;
+        if (rand()%rock_rarity == 0) {
+            type = ROCK;
+        }
+
+        // Loops for every pixel of the cirlcle (or square as it seems
+        // now)
+        for (int x = std::max(0, midx-range); 
+             x < std::min(MAP_WIDTH, midx+range); 
+             x++) {
+            for (int y = std::max(0, midy-range);
+                 y < std::min((int)MAP_HEIGHT, midy+range);
+                 y++) {
+                terrain[x][y] = type;
+            } 
+        } 
+
     }
+
+    this->generatePixelBuffer();
 }
 
 void Terrain::draw(CL_GraphicContext *gc) {
--- a/src/proto2/Terrain.hh	Sun Nov 30 13:17:37 2008 +0000
+++ b/src/proto2/Terrain.hh	Sun Nov 30 13:31:32 2008 +0000
@@ -42,7 +42,8 @@
 
 
     /**
-     * Return the type of terrain at given position.
+     * Return the type of terrain at given position. Returns ROCK if
+     * given point is not inside terrain area.
      *
      * @param x X coordinate
      * @param y Y coordinate