Anonymous avatar Anonymous committed 1b61697

loop cleanup in levelgen

Comments (0)

Files changed (1)

src/main/scala/com/mojang/ld22/level/levelgen/LevelGen.scala

 
 object LevelGen {
 
+  import util.Implicits._
+
   def createAndValidateTopMap(w: Int, h: Int): Array[Array[Byte]] = {
     val result = createTopMap(w, h)
     val count = new Array[Int](256)
         map(i) = Tile.grass.id
     }
 
-    {
-      var i: Int = 0
-      while (i < w * h / 2800) {
-        {
-          val xs: Int = random.nextInt(w)
-          val ys: Int = random.nextInt(h)
+    (w * h / 2800) times {
+      val xs: Int = random.nextInt(w)
+      val ys: Int = random.nextInt(h)
 
-          {
-            var k: Int = 0
-            while (k < 10) {
-              {
-                val x: Int = xs + random.nextInt(21) - 10
-                val y: Int = ys + random.nextInt(21) - 10
+      10 times {
+        val x: Int = xs + random.nextInt(21) - 10
+        val y: Int = ys + random.nextInt(21) - 10
 
-                {
-                  var j: Int = 0
-                  while (j < 100) {
-                    {
-                      val xo: Int = x + random.nextInt(5) - random.nextInt(5)
-                      val yo: Int = y + random.nextInt(5) - random.nextInt(5)
 
-                      {
-                        var yy: Int = yo - 1
-                        while (yy <= yo + 1) {
-                          {
-                            var xx: Int = xo - 1
-                            while (xx <= xo + 1) {
-                              if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
-                                if (map(xx + yy * w) == Tile.grass.id) {
-                                  map(xx + yy * w) = Tile.sand.id
-                                }
-                              }
-                              xx += 1
-                            }
-                          }
-                          yy += 1
-                        }
-                      }
-                    }
-                    j += 1
-                  }
-                }
+        100 times {
+          val xo: Int = x + random.nextInt(5) - random.nextInt(5)
+          val yo: Int = y + random.nextInt(5) - random.nextInt(5)
+
+          for (yy <- yo - 1 to yo + 1; xx <- xo - 1 to xo + 1) {
+            if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
+              if (map(xx + yy * w) == Tile.grass.id) {
+                map(xx + yy * w) = Tile.sand.id
               }
-              k += 1
             }
           }
         }
-        i += 1
       }
     }
-    {
-      var i: Int = 0
-      while (i < w * h / 400) {
-        {
-          val x: Int = random.nextInt(w)
-          val y: Int = random.nextInt(h)
 
-          {
-            var j: Int = 0
-            while (j < 200) {
-              {
-                val xx: Int = x + random.nextInt(15) - random.nextInt(15)
-                val yy: Int = y + random.nextInt(15) - random.nextInt(15)
-                if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
-                  if (map(xx + yy * w) == Tile.grass.id) {
-                    map(xx + yy * w) = Tile.tree.id
-                  }
-                }
-              }
-              j += 1
-            }
+    (w * h / 400) times {
+      val x: Int = random.nextInt(w)
+      val y: Int = random.nextInt(h)
+
+      200 times {
+        val xx: Int = x + random.nextInt(15) - random.nextInt(15)
+        val yy: Int = y + random.nextInt(15) - random.nextInt(15)
+        if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
+          if (map(xx + yy * w) == Tile.grass.id) {
+            map(xx + yy * w) = Tile.tree.id
           }
         }
-        i += 1
       }
     }
-    {
-      var i: Int = 0
-      while (i < w * h / 400) {
-        {
-          val x: Int = random.nextInt(w)
-          val y: Int = random.nextInt(h)
-          val col: Int = random.nextInt(4)
 
-          {
-            var j: Int = 0
-            while (j < 30) {
-              {
-                val xx: Int = x + random.nextInt(5) - random.nextInt(5)
-                val yy: Int = y + random.nextInt(5) - random.nextInt(5)
-                if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
-                  if (map(xx + yy * w) == Tile.grass.id) {
-                    map(xx + yy * w) = Tile.flower.id
-                    data(xx + yy * w) = (col + random.nextInt(4) * 16).asInstanceOf[Byte]
-                  }
-                }
-              }
-              j += 1
-            }
+    (w * h / 400) times {
+      val x: Int = random.nextInt(w)
+      val y: Int = random.nextInt(h)
+      val col: Int = random.nextInt(4)
+
+      30 times {
+        val xx: Int = x + random.nextInt(5) - random.nextInt(5)
+        val yy: Int = y + random.nextInt(5) - random.nextInt(5)
+        if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
+          if (map(xx + yy * w) == Tile.grass.id) {
+            map(xx + yy * w) = Tile.flower.id
+            data(xx + yy * w) = (col + random.nextInt(4) * 16).asInstanceOf[Byte]
           }
         }
-        i += 1
       }
     }
-    {
-      var i: Int = 0
-      while (i < w * h / 100) {
-        {
-          val xx: Int = random.nextInt(w)
-          val yy: Int = random.nextInt(h)
-          if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
-            if (map(xx + yy * w) == Tile.sand.id) {
-              map(xx + yy * w) = Tile.cactus.id
-            }
-          }
+
+    (w * h / 100) times {
+      val xx: Int = random.nextInt(w)
+      val yy: Int = random.nextInt(h)
+      if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
+        if (map(xx + yy * w) == Tile.sand.id) {
+          map(xx + yy * w) = Tile.cactus.id
         }
-        i += 1
       }
     }
+
     var stairsMade: Int = 0
     var attempts: Int = 0
     while ((attempts < w * h / 100) && (stairsMade < 4)) {
       val y: Int = random.nextInt(h - 2) + 1
       var eligible: Boolean = true
 
-      {
-        var yy: Int = y - 1
-        while (yy <= y + 1) {
-          {
-            var xx: Int = x - 1
-            while (xx <= x + 1) {
-              {
-                if (map(xx + yy * w) != Tile.rock.id) eligible = false
-              }
-              xx += 1
-            }
-          }
-          yy += 1
-        }
-      }
+      for (yy <- y - 1 to y + 1; xx <- x - 1 to x + 1)
+        if (map(xx + yy * w) != Tile.rock.id) eligible = false
+
       if (eligible) {
         map(x + y * w) = Tile.stairsDown.id
         stairsMade += 1
         val y: Int = random.nextInt(h - 20) + 10
         var eligible: Boolean = true
 
-        {
-          var yy: Int = y - 1
-          while (yy <= y + 1) {
-            {
-              var xx: Int = x - 1
-              while (xx <= x + 1) {
-                {
-                  if (map(xx + yy * w) != Tile.rock.id) eligible = false
-                }
-                xx += 1
-              }
-            }
-            yy += 1
-          }
-        }
+        for (yy <- y - 1 to y + 1; xx <- x - 1 to x + 1)
+          if (map(xx + yy * w) != Tile.rock.id) eligible = false
+
         if (eligible) {
           map(x + y * w) = Tile.stairsDown.id
           stairsMade += 1
     val map: Array[Byte] = new Array[Byte](w * h)
     val data: Array[Byte] = new Array[Byte](w * h)
 
-    {
-      var y: Int = 0
-      while (y < h) {
-        {
-          {
-            var x: Int = 0
-            while (x < w) {
-              {
-                val i: Int = x + y * w
-                var value: Double = math.abs(noise1(x, y) - noise2(x, y)) * 3 - 2
-                var xd: Double = x / (w - 1.0) * 2 - 1
-                var yd: Double = y / (h - 1.0) * 2 - 1
-                if (xd < 0) xd = -xd
-                if (yd < 0) yd = -yd
-                var dist: Double = if (xd >= yd) xd else yd
-                dist = dist * dist * dist * dist
-                dist = dist * dist * dist * dist
-                value = -value * 1 - 2.2
-                value = value + 1 - dist * 20
-                if (value < -0.25) {
-                  map(i) = Tile.infiniteFall.id
-                }
-                else {
-                  map(i) = Tile.cloud.id
-                }
-              }
-              x += 1
-            }
-          }
-        }
-        y += 1
+    for (y <- 0 until h; x <- 0 until w) {
+      val i: Int = x + y * w
+      var value: Double = math.abs(noise1(x, y) - noise2(x, y)) * 3 - 2
+      var xd: Double = x / (w - 1.0) * 2 - 1
+      var yd: Double = y / (h - 1.0) * 2 - 1
+      if (xd < 0) xd = -xd
+      if (yd < 0) yd = -yd
+      var dist: Double = if (xd >= yd) xd else yd
+      dist = dist * dist * dist * dist
+      dist = dist * dist * dist * dist
+      value = -value * 1 - 2.2
+      value = value + 1 - dist * 20
+      if (value < -0.25) {
+        map(i) = Tile.infiniteFall.id
+      }
+      else {
+        map(i) = Tile.cloud.id
       }
     }
+
+
     var attempts: Int = 0
     while ((attempts < w * h / 50)) {
       attempts += 1
       val y: Int = random.nextInt(h - 2) + 1
       var eligible: Boolean = true
 
-      {
-        var yy: Int = y - 1
-        while (yy <= y + 1) {
-          {
-            var xx: Int = x - 1
-            while (xx <= x + 1) {
-              {
-                if (map(xx + yy * w) != Tile.cloud.id) eligible = false
-              }
-              xx += 1
-            }
-          }
-          yy += 1
-        }
-      }
+      for (yy <- y - 1 to y + 1; xx <- x - 1 to x + 1)
+        if (map(xx + yy * w) != Tile.cloud.id) eligible = false
+
       if (eligible) {
         map(x + y * w) = Tile.cloudCactus.id
       }
       val y: Int = random.nextInt(h - 2) + 1
       var eligible: Boolean = true
 
-      {
-        var yy: Int = y - 1
-        while (yy <= y + 1) {
-          {
-            var xx: Int = x - 1
-            while (xx <= x + 1) {
-              {
-                if (map(xx + yy * w) != Tile.cloud.id) eligible = false
-              }
-              xx += 1
-            }
-          }
-          yy += 1
-        }
-      }
+      for (yy <- y - 1 to y + 1; xx <- x - 1 to x + 1)
+        if (map(xx + yy * w) != Tile.cloud.id) eligible = false
+
       if (eligible) {
         map(x + y * w) = Tile.stairsDown.id
         stairsMade += 1
       val img: BufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB)
       val pixels: Array[Int] = new Array[Int](w * h)
 
-      {
-        var y: Int = 0
-        while (y < h) {
-          {
-            {
-              var x: Int = 0
-              while (x < w) {
-                {
-                  val i: Int = x + y * w
-                  pixels(i) = 0x000000
-                  if (map(i) == Tile.water.id) pixels(i) = 0x000080
-                  if (map(i) == Tile.grass.id) pixels(i) = 0x208020
-                  if (map(i) == Tile.rock.id) pixels(i) = 0xa0a0a0
-                  if (map(i) == Tile.dirt.id) pixels(i) = 0x604040
-                  if (map(i) == Tile.sand.id) pixels(i) = 0xa0a040
-                  if (map(i) == Tile.tree.id) pixels(i) = 0x003000
-                  if (map(i) == Tile.lava.id) pixels(i) = 0xff2020
-                  if (map(i) == Tile.cloud.id) pixels(i) = 0xa0a0a0
-                  if (map(i) == Tile.stairsDown.id) pixels(i) = 0xffffff
-                  if (map(i) == Tile.stairsUp.id) pixels(i) = 0xffffff
-                  if (map(i) == Tile.cloudCactus.id) pixels(i) = 0xff00ff
-                  if (map(i) == Tile.flower.id) pixels(i) = 0xaaaa66
-                  if (map(i) == Tile.cactus.id) pixels(i) = 0x66cc66
-                }
-                x += 1
-              }
-            }
-          }
-          y += 1
-        }
+      for (y <- 0 until h; x <- 0 until w) {
+        val i: Int = x + y * w
+        pixels(i) = 0x000000
+        if (map(i) == Tile.water.id) pixels(i) = 0x000080
+        if (map(i) == Tile.grass.id) pixels(i) = 0x208020
+        if (map(i) == Tile.rock.id) pixels(i) = 0xa0a0a0
+        if (map(i) == Tile.dirt.id) pixels(i) = 0x604040
+        if (map(i) == Tile.sand.id) pixels(i) = 0xa0a040
+        if (map(i) == Tile.tree.id) pixels(i) = 0x003000
+        if (map(i) == Tile.lava.id) pixels(i) = 0xff2020
+        if (map(i) == Tile.cloud.id) pixels(i) = 0xa0a0a0
+        if (map(i) == Tile.stairsDown.id) pixels(i) = 0xffffff
+        if (map(i) == Tile.stairsUp.id) pixels(i) = 0xffffff
+        if (map(i) == Tile.cloudCactus.id) pixels(i) = 0xff00ff
+        if (map(i) == Tile.flower.id) pixels(i) = 0xaaaa66
+        if (map(i) == Tile.cactus.id) pixels(i) = 0x66cc66
       }
       img.setRGB(0, 0, w, h, pixels, 0, w)
       val ico: ImageIcon = new ImageIcon(img.getScaledInstance(w * 4, h * 4, Image.SCALE_AREA_AVERAGING))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.