private Flow CalculateFlowAt(int x, int y) { Flow result = new Flow(); for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { int xd = i + x - 1; int yd = j + y - 1; if (field.Validate (xd, yd)) { if (SurfaceLevel (x, y) > SurfaceLevel (xd, yd) + maxDelta) { int delta = SurfaceLevel (x, y) - SurfaceLevel (xd, yd); result.flowDirection [i, j] = delta; result.flowAmount = Math.Max (result.flowAmount, delta); } } } } return result; }
private bool PerformFlow() { bool result = false; Flow[,] flows = new Flow[xSize, ySize]; // calculating where the water will flow for (int x=0; x<xSize; x++) { for (int y=0; y<ySize; y++) { if (values [x, y] > 0) { flows [x, y] = CalculateFlowAt (x, y); } } } // performing the water flow for (int x=0; x<xSize; x++) { for (int y=0; y<ySize; y++) { if (values [x, y] > 0) { result = result || PerformFlowAt (x, y, flows [x, y]); } } } return result; }
private bool PerformFlowAt(int x, int y, Flow flow) { bool result = false; int [,] normalizedFlow = flow.NormalizedFlow(); for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { int xd = i + x - 1; int yd = j + y - 1; int flowThere = normalizedFlow [i, j]; if (flowThere > 0) { result = true; values [xd, yd] += flowThere; values [x, y] -= flowThere; } } } return result; }