Advertisement
timeshifter

flow

Jun 23rd, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1.         private void btnStep_Click(object sender, EventArgs e) {
  2.             btnStep.Text = "Working...";
  3.             Application.DoEvents();
  4.             ovWater = Overlays[Overlays.Count - 1];
  5.             Overlay ovHeight = Overlays[0];
  6.  
  7.             for (int idx = 0; idx < 100; idx++) {
  8.  
  9.  
  10.                 for (int x = 0; x < worldWidth; x++) {
  11.                 //Parallel.For(0, worldWidth, x => {
  12.                     for (int y = 0; y < worldHeight; y++) {
  13.                         slopeMap[x, y] = new FlowPoint();
  14.  
  15.                         if (x > 0) {
  16.                             slopeMap[x, y].West = (ovHeight.Heightmap[x, y] + ovWater.Heightmap[x, y]) - (ovHeight.Heightmap[x - 1, y] + ovWater.Heightmap[x - 1, y]);
  17.                         }
  18.                         if (x < worldWidth - 1) {
  19.                             slopeMap[x, y].East = (ovHeight.Heightmap[x, y] + ovWater.Heightmap[x, y]) - (ovHeight.Heightmap[x + 1, y] + ovWater.Heightmap[x + 1, y]);
  20.                         }
  21.                         if (y > 0) {
  22.                             slopeMap[x, y].North = (ovHeight.Heightmap[x, y] + ovWater.Heightmap[x, y]) - (ovHeight.Heightmap[x, y - 1] + ovWater.Heightmap[x, y - 1]);
  23.                         }
  24.                         if (y < worldHeight - 1) {
  25.                             slopeMap[x, y].South = (ovHeight.Heightmap[x, y] + ovWater.Heightmap[x, y]) - (ovHeight.Heightmap[x, y + 1] + ovWater.Heightmap[x, y + 1]);
  26.                         }
  27.  
  28.                     }
  29.                 }//);
  30.  
  31.                 double[,] tmp = new double[worldWidth, worldHeight];
  32.                
  33.  
  34.                 for (int x = 0; x < worldWidth; x++) {
  35.                 //Parallel.For(0, worldWidth, x => {
  36.                     for (int y = 0; y < worldHeight; y++) {
  37.  
  38.                         bool wvalid = x > 0 && slopeMap[x, y].West > 0,
  39.                              evalid = x < (worldWidth - 1) && slopeMap[x, y].East > 0,
  40.                              nvalid = y > 0 && slopeMap[x, y].North > 0,
  41.                              svalid = y < (worldHeight - 1) && slopeMap[x, y].South > 0;
  42.  
  43.                         int ct = (wvalid ? 1 : 0) +
  44.                             (evalid ? 1 : 0) +
  45.                             (nvalid ? 1 : 0) +
  46.                             (svalid ? 1 : 0);
  47.  
  48.                         double flowCt =
  49.                             (nvalid ? slopeMap[x, y].North : 0) +
  50.                             (svalid ? slopeMap[x, y].South : 0) +
  51.                             (evalid ? slopeMap[x, y].East : 0) +
  52.                             (wvalid ? slopeMap[x, y].West : 0);
  53.  
  54.                         double flowAmt = ovWater.Heightmap[x, y];
  55.  
  56.  
  57.                         //if (flowAmt > 0.01)
  58.                         //  flowAmt = 0.01;
  59.  
  60.                         if (ct > 0) {
  61.                             double flowTot = 0.0, amt = 0;
  62.  
  63.                             if (nvalid) {
  64.                                 amt = (flowAmt * (slopeMap[x, y].North / flowCt));
  65.                                 tmp[x, y - 1] += amt;
  66.                                 flowTot += amt;
  67.                             }
  68.                             if (svalid) {
  69.                                 amt = (flowAmt * (slopeMap[x, y].South / flowCt));
  70.                                 tmp[x, y + 1] += amt;
  71.                                 flowTot += amt;
  72.                             }
  73.                             if (evalid) {
  74.                                 amt = (flowAmt * (slopeMap[x, y].East / flowCt));
  75.                                 tmp[x + 1, y] += amt;
  76.                                 flowTot += amt;
  77.                             }
  78.  
  79.                             if (wvalid) {
  80.                                 amt = (flowAmt * (slopeMap[x, y].West / flowCt));
  81.                                 tmp[x - 1, y] += amt;
  82.                                 flowTot += amt;
  83.                             }
  84.  
  85.                             tmp[x, y] += (ovWater.Heightmap[x, y] - flowTot);
  86.                         }
  87.                         else {
  88.                             tmp[x, y] = ovWater.Heightmap[x, y];
  89.                         }
  90.                     }
  91.                 }//);
  92.  
  93.                 ovWater.Heightmap = tmp;
  94.                 Overlays[Overlays.Count - 1] = ovWater;
  95.  
  96.                 if (idx % 10 == 0) {
  97.                     FillDefaultOverlays();
  98.                     Draw();
  99.                 }
  100.  
  101.                 lblWater.Text =
  102.                     idx.ToString() + "\n" +
  103.                     (from double d in ovWater.Heightmap select d).Sum().ToString() + "\n" +
  104.                     (from double d in ovWater.Heightmap select d).Min().ToString() + "\n" +
  105.                     (from double d in ovWater.Heightmap select d).Max().ToString();
  106.                 Application.DoEvents();
  107.  
  108.             }
  109.  
  110.             FillDefaultOverlays();
  111.             Draw();
  112.             btnStep.Text = "Step water";
  113.             Application.DoEvents();
  114.  
  115.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement