Skip to content
Browse files

Add per-polygon watershed calculation, and render that instead of per…

…-corner watersheds. This still isn't quite what I want from watersheds but it seems closer.
  • Loading branch information...
1 parent 7203072 commit 98a9997353f23b99eae5b801a5b3a09d582e809e @amitp amitp committed Sep 22, 2010
Showing with 57 additions and 21 deletions.
  1. +34 −0 Watersheds.as
  2. +23 −21 mapgen2.as
View
34 Watersheds.as
@@ -0,0 +1,34 @@
+// Define watersheds: if a drop of rain falls on any polygon, where
+// does it exit the island? We follow the map corner downslope field.
+// Author: amitp@cs.stanford.edu
+// License: MIT
+
+package {
+ import graph.*;
+
+ public class Watersheds {
+ public var lowestCorner:Array = []; // polygon index -> corner index
+ public var watersheds:Array = []; // polygon index -> corner index
+
+ // We want to mark each polygon with the corner where water would
+ // exit the island.
+ public function createWatersheds(map:Map):void {
+ var p:Center, q:Corner, s:Corner;
+
+ // Find the lowest corner of the polygon, and set that as the
+ // exit point for rain falling on this polygon
+ for each (p in map.centers) {
+ s = null;
+ for each (q in p.corners) {
+ if (s == null || q.elevation < s.elevation) {
+ s = q;
+ }
+ }
+ lowestCorner[p.index] = (s == null)? -1 : s.index;
+ watersheds[p.index] = (s == null)? -1 : (s.watershed == null)? -1 : s.watershed.index;
+ }
+ }
+
+ }
+}
+
View
44 mapgen2.as
@@ -90,6 +90,7 @@ package {
public var map:Map;
public var roads:Roads;
public var lava:Lava;
+ public var watersheds:Watersheds;
public var noisyEdges:NoisyEdges;
@@ -171,6 +172,7 @@ package {
roads = new Roads();
lava = new Lava();
+ watersheds = new Watersheds();
noisyEdges = new NoisyEdges();
commandExecute("Shaping map...",
@@ -208,6 +210,7 @@ package {
function():void {
roads.createRoads(map);
lava.createLava(map, map.mapRandom.nextDouble);
+ watersheds.createWatersheds(map);
noisyEdges.buildNoisyEdges(map, lava, map.mapRandom);
drawMap(mapMode);
});
@@ -810,30 +813,29 @@ package {
// Render the paths from each polygon to the ocean, showing watersheds
public function renderWatersheds(graphics:Graphics):void {
- var q:Corner, r:Corner;
+ var edge:Edge, w0:int, w1:int;
- for each (q in map.corners) {
- if (!q.ocean) {
- r = q.downslope;
- graphics.lineStyle(1.2, q.watershed == r.watershed? 0x00ffff : 0xff00ff,
- 0.1*Math.sqrt(q.watershed.watershed_size || 1));
- graphics.moveTo(q.point.x, q.point.y);
- graphics.lineTo(r.point.x, r.point.y);
- graphics.lineStyle();
+ for each (edge in map.edges) {
+ if (edge.d0 && edge.d1 && edge.v0 && edge.v1
+ && !edge.d0.ocean && !edge.d1.ocean) {
+ w0 = watersheds.watersheds[edge.d0.index];
+ w1 = watersheds.watersheds[edge.d1.index];
+ if (w0 != w1) {
+ graphics.lineStyle(3.5, 0x000000, 0.1*Math.sqrt((map.corners[w0].watershed_size || 1) + (map.corners[w1].watershed.watershed_size || 1)));
+ graphics.moveTo(edge.v0.point.x, edge.v0.point.y);
+ graphics.lineTo(edge.v1.point.x, edge.v1.point.y);
+ graphics.lineStyle();
+ }
}
}
-
- for each (q in map.corners) {
- for each (r in q.adjacent) {
- if (!q.ocean && !r.ocean && q.watershed != r.watershed && !q.coast && !r.coast) {
- var edge:Edge = map.lookupEdgeFromCorner(q, r);
- graphics.lineStyle(2.5, 0x000000, 0.05*Math.sqrt((q.watershed.watershed_size || 1) + (r.watershed.watershed_size || 1)));
- graphics.moveTo(edge.d0.point.x, edge.d0.point.y);
- graphics.lineTo(edge.midpoint.x, edge.midpoint.y);
- graphics.lineTo(edge.d1.point.x, edge.d1.point.y);
- graphics.lineStyle();
- }
- }
+
+ for each (edge in map.edges) {
+ if (edge.river) {
+ graphics.lineStyle(1.0, 0x6699ff);
+ graphics.moveTo(edge.v0.point.x, edge.v0.point.y);
+ graphics.lineTo(edge.v1.point.x, edge.v1.point.y);
+ graphics.lineStyle();
+ }
}
}

0 comments on commit 98a9997

Please sign in to comment.
Something went wrong with that request. Please try again.