Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 98a9997353f23b99eae5b801a5b3a09d582e809e 1 parent 7203072
Amit Patel amitp authored

Showing 2 changed files with 57 additions and 21 deletions. Show diff stats Hide diff stats

  1. +34 0 Watersheds.as
  2. +23 21 mapgen2.as
34 Watersheds.as
... ... @@ -0,0 +1,34 @@
  1 +// Define watersheds: if a drop of rain falls on any polygon, where
  2 +// does it exit the island? We follow the map corner downslope field.
  3 +// Author: amitp@cs.stanford.edu
  4 +// License: MIT
  5 +
  6 +package {
  7 + import graph.*;
  8 +
  9 + public class Watersheds {
  10 + public var lowestCorner:Array = []; // polygon index -> corner index
  11 + public var watersheds:Array = []; // polygon index -> corner index
  12 +
  13 + // We want to mark each polygon with the corner where water would
  14 + // exit the island.
  15 + public function createWatersheds(map:Map):void {
  16 + var p:Center, q:Corner, s:Corner;
  17 +
  18 + // Find the lowest corner of the polygon, and set that as the
  19 + // exit point for rain falling on this polygon
  20 + for each (p in map.centers) {
  21 + s = null;
  22 + for each (q in p.corners) {
  23 + if (s == null || q.elevation < s.elevation) {
  24 + s = q;
  25 + }
  26 + }
  27 + lowestCorner[p.index] = (s == null)? -1 : s.index;
  28 + watersheds[p.index] = (s == null)? -1 : (s.watershed == null)? -1 : s.watershed.index;
  29 + }
  30 + }
  31 +
  32 + }
  33 +}
  34 +
44 mapgen2.as
@@ -90,6 +90,7 @@ package {
90 90 public var map:Map;
91 91 public var roads:Roads;
92 92 public var lava:Lava;
  93 + public var watersheds:Watersheds;
93 94 public var noisyEdges:NoisyEdges;
94 95
95 96
@@ -171,6 +172,7 @@ package {
171 172
172 173 roads = new Roads();
173 174 lava = new Lava();
  175 + watersheds = new Watersheds();
174 176 noisyEdges = new NoisyEdges();
175 177
176 178 commandExecute("Shaping map...",
@@ -208,6 +210,7 @@ package {
208 210 function():void {
209 211 roads.createRoads(map);
210 212 lava.createLava(map, map.mapRandom.nextDouble);
  213 + watersheds.createWatersheds(map);
211 214 noisyEdges.buildNoisyEdges(map, lava, map.mapRandom);
212 215 drawMap(mapMode);
213 216 });
@@ -810,30 +813,29 @@ package {
810 813
811 814 // Render the paths from each polygon to the ocean, showing watersheds
812 815 public function renderWatersheds(graphics:Graphics):void {
813   - var q:Corner, r:Corner;
  816 + var edge:Edge, w0:int, w1:int;
814 817
815   - for each (q in map.corners) {
816   - if (!q.ocean) {
817   - r = q.downslope;
818   - graphics.lineStyle(1.2, q.watershed == r.watershed? 0x00ffff : 0xff00ff,
819   - 0.1*Math.sqrt(q.watershed.watershed_size || 1));
820   - graphics.moveTo(q.point.x, q.point.y);
821   - graphics.lineTo(r.point.x, r.point.y);
822   - graphics.lineStyle();
  818 + for each (edge in map.edges) {
  819 + if (edge.d0 && edge.d1 && edge.v0 && edge.v1
  820 + && !edge.d0.ocean && !edge.d1.ocean) {
  821 + w0 = watersheds.watersheds[edge.d0.index];
  822 + w1 = watersheds.watersheds[edge.d1.index];
  823 + if (w0 != w1) {
  824 + graphics.lineStyle(3.5, 0x000000, 0.1*Math.sqrt((map.corners[w0].watershed_size || 1) + (map.corners[w1].watershed.watershed_size || 1)));
  825 + graphics.moveTo(edge.v0.point.x, edge.v0.point.y);
  826 + graphics.lineTo(edge.v1.point.x, edge.v1.point.y);
  827 + graphics.lineStyle();
  828 + }
823 829 }
824 830 }
825   -
826   - for each (q in map.corners) {
827   - for each (r in q.adjacent) {
828   - if (!q.ocean && !r.ocean && q.watershed != r.watershed && !q.coast && !r.coast) {
829   - var edge:Edge = map.lookupEdgeFromCorner(q, r);
830   - graphics.lineStyle(2.5, 0x000000, 0.05*Math.sqrt((q.watershed.watershed_size || 1) + (r.watershed.watershed_size || 1)));
831   - graphics.moveTo(edge.d0.point.x, edge.d0.point.y);
832   - graphics.lineTo(edge.midpoint.x, edge.midpoint.y);
833   - graphics.lineTo(edge.d1.point.x, edge.d1.point.y);
834   - graphics.lineStyle();
835   - }
836   - }
  831 +
  832 + for each (edge in map.edges) {
  833 + if (edge.river) {
  834 + graphics.lineStyle(1.0, 0x6699ff);
  835 + graphics.moveTo(edge.v0.point.x, edge.v0.point.y);
  836 + graphics.lineTo(edge.v1.point.x, edge.v1.point.y);
  837 + graphics.lineStyle();
  838 + }
837 839 }
838 840 }
839 841

0 comments on commit 98a9997

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