public bool AreCornerFlowsValid(RiverFlow centerRight, RiverFlow?centerLeft, RiverFlow?leftRight) { if (centerLeft != null) { if (leftRight != null) { switch (centerRight) { case RiverFlow.Clockwise: return(!(centerLeft == RiverFlow.Counterclockwise && leftRight == RiverFlow.Counterclockwise)); case RiverFlow.Counterclockwise: return(!(centerLeft == RiverFlow.Clockwise && leftRight == RiverFlow.Clockwise)); default: throw new NotImplementedException(); } } else { return(centerLeft != centerRight.Opposite()); } } else { return(leftRight != centerRight.Opposite()); } }
public RiverPath( IHexCell cell, HexDirection pathStart, HexDirection pathEnd, RiverFlow flow, IRiverCanon riverCanon, IHexGrid grid ) { Cell = cell; Path = new List <HexDirection>(); Flow = flow; if (flow == RiverFlow.Clockwise) { for (HexDirection i = pathStart; i != pathEnd; i = i.Next()) { Path.Add(i); } } else { for (HexDirection i = pathStart; i != pathEnd; i = i.Previous()) { Path.Add(i); } } Path.Add(pathEnd); RiverCanon = riverCanon; Grid = grid; }
public bool CanAddRiverToCell(IHexCell cell, HexDirection edge, RiverFlow flow) { if (HasRiverAlongEdge(cell, edge)) { return(false); } return(RiverMeetsPlacementConditions(cell, edge, flow)); }
//The fact that we're building sections only from the NE, E, and SE //cell edges is very important. A lot of the code here is structured //as it is to deal with this reality. public void RefreshRiverSections() { sections.Clear(); SectionBetweenCells.Clear(); CellEdgeContourCanon.Clear(); foreach (var cell in Grid.Cells) { for (HexDirection direction = HexDirection.NE; direction <= HexDirection.SE; direction++) { if (RiverCanon.HasRiverAlongEdge(cell, direction)) { var neighbor = Grid.GetNeighbor(cell, direction); Vector3 start = cell.AbsolutePosition + RenderConfig.GetFirstCorner(direction); Vector3 end = cell.AbsolutePosition + RenderConfig.GetSecondCorner(direction); RiverFlow flow = RiverCanon.GetFlowOfRiverAtEdge(cell, direction); //Our control points need to operate differently if we're at endpoints, //and both ControlOne and ControlTwo might have alternate behavior. //We need to check both ends of the section for endpoints bool previousCellRiver = RiverCanon.HasRiverAlongEdge(cell, direction.Previous()); bool previousNeighborRiver = RiverCanon.HasRiverAlongEdge(neighbor, direction.Previous2()); bool hasPreviousEndpoint = !previousCellRiver && !previousNeighborRiver; bool hasNextEndpoint = !RiverCanon.HasRiverAlongEdge(cell, direction.Next()) && !RiverCanon.HasRiverAlongEdge(neighbor, direction.Next2()); var newRiverSection = new RiverSection() { AdjacentCellOne = cell, AdjacentCellTwo = neighbor, DirectionFromOne = direction, Start = start, End = end, FlowFromOne = flow, HasPreviousEndpoint = hasPreviousEndpoint, HasNextEndpoint = hasNextEndpoint }; SectionBetweenCells[new Tuple <IHexCell, IHexCell>(cell, neighbor)] = newRiverSection; sections.Add(newRiverSection); } } } }
/// <summary> /// Read input and print out river flow. /// </summary> static void getRiverFlow() { int section = getSection(); double[] width = getItemArray("What is the width of each section?", section); double[] depth = getItemArray("What is the depth of each section?", section); double[] velocity = getItemArray("What is the velocity of each section?", section); RiverFlow rf = new RiverFlow(section, width, depth, velocity); double riverFlow = rf.CalculateRiverFlow(); Console.WriteLine("River Flow is : {0}", riverFlow); }
private void AddRiverToCell( IHexCell cell, HexDirection direction, RiverFlow flow, HashSet <IHexCell> cellsAdjacentToRiver ) { RiverCanon.AddRiverToCell(cell, direction, flow); cellsAdjacentToRiver.Add(cell); if (Grid.HasNeighbor(cell, direction)) { cellsAdjacentToRiver.Add(Grid.GetNeighbor(cell, direction)); } }
public void OverrideRiverOnCell(IHexCell cell, HexDirection edge, RiverFlow flow) { GetPresenceArray(cell)[(int)edge] = true; GetDirectionArray(cell)[(int)edge] = flow; var neighborAtEdge = Grid.GetNeighbor(cell, edge); if (neighborAtEdge != null) { GetPresenceArray(neighborAtEdge)[(int)(edge.Opposite())] = true; GetDirectionArray(neighborAtEdge)[(int)(edge.Opposite())] = flow.Opposite(); } }
public RiverPath( IHexCell cell, HexDirection path, RiverFlow flow, IRiverCanon riverCanon, IHexGrid grid ) { Cell = cell; Path = new List <HexDirection>() { path }; Flow = flow; RiverCanon = riverCanon; Grid = grid; }
private RiverFlow[] GetDirectionArray(IHexCell cell) { RiverFlow[] retval; if (!RiverDirectionDict.ContainsKey(cell)) { retval = new RiverFlow[6]; RiverDirectionDict[cell] = retval; } else { retval = RiverDirectionDict[cell]; } return(retval); }
private bool RiverMeetsPlacementConditions(IHexCell cell, HexDirection edge, RiverFlow flow) { var right = Grid.GetNeighbor(cell, edge); if (right == null) { return(false); } if (cell.Terrain.IsWater() || right.Terrain.IsWater()) { return(false); } IHexCell left = Grid.GetNeighbor(cell, edge.Previous()); IHexCell nextRight = Grid.GetNeighbor(cell, edge.Next()); return(PreviousCornerIsValid(cell, left, right, edge, flow) && NextCornerIsValid(cell, right, nextRight, edge, flow)); }
public void AddRiverToCell(IHexCell cell, HexDirection edge, RiverFlow flow) { if (!CanAddRiverToCell(cell, edge, flow)) { throw new InvalidOperationException("CanAddRiverToCell must return true on the given arguments"); } GetPresenceArray(cell)[(int)edge] = true; GetDirectionArray(cell)[(int)edge] = flow; var neighborAtEdge = Grid.GetNeighbor(cell, edge); if (neighborAtEdge != null) { GetPresenceArray(neighborAtEdge)[(int)(edge.Opposite())] = true; GetDirectionArray(neighborAtEdge)[(int)(edge.Opposite())] = flow.Opposite(); CellSignals.GainedRiveredEdge.OnNext(neighborAtEdge); } CellSignals.GainedRiveredEdge.OnNext(cell); }
public void testWithSingleData() { Console.WriteLine("Single data test:"); try { int section = testSingleData.getSection(); double[] width = testSingleData.getWidth(); double[] depth = testSingleData.getDepth(); double[] velocity = testSingleData.getVelocity(); double expected = testSingleData.getExpected(); RiverFlow rf = new RiverFlow(section, width, depth, velocity); double riverFlow = rf.CalculateRiverFlow(); printTestInfo(section, width, depth, velocity, expected, riverFlow); if (riverFlow != expected) { throw new Exception("Not expected: testWithSingleData"); } } catch (Exception) { return; } Console.WriteLine("Single data test is done...\n"); }
private bool NextCornerIsValid( IHexCell cell, IHexCell right, IHexCell nextRight, HexDirection edge, RiverFlow flow ) { if (nextRight == null) { return(true); } RiverFlow?centerNextRightFlow = null; RiverFlow?nextRightRightFlow = null; if (HasRiverAlongEdge(cell, edge.Next())) { centerNextRightFlow = GetFlowOfRiverAtEdge(cell, edge.Next()); } if (HasRiverAlongEdge(nextRight, edge.Previous())) { nextRightRightFlow = GetFlowOfRiverAtEdge(nextRight, edge.Previous()); } return(RiverCornerValidityLogic.AreCornerFlowsValid(flow, centerNextRightFlow, nextRightRightFlow)); }
private bool HasRiverInDirectionWithFlow(IHexCell cell, HexDirection direction, RiverFlow flow) { return( RiverCanon.HasRiverAlongEdge(Cell, direction) && RiverCanon.GetFlowOfRiverAtEdge(Cell, direction) == Flow ); }
public static RiverFlow Opposite(this RiverFlow direction) { return((RiverFlow)(((int)direction + 1) % 2)); }