public void CanPeekValues() { var dict = new PriorityDictionary <Point, float>(); for (var x = 0; x < 10; x++) { for (var y = 0; y < 10; y++) { dict.Add(new Point(x, y), ((x - 2) * (x - 2.5f)) + ((y - 3) * (y - 3.2f))); } } var min = dict.PopMin(); Assert.AreEqual(0, min.Value, 0.01f); Assert.AreEqual(new Point(2, 3), min.Key); min = dict.PeekMin(); Assert.AreEqual(0.5, min.Value, 0.01f); Assert.AreEqual(new Point(3, 3), min.Key); var max = dict.PeekMax(); Assert.AreEqual(80.3f, max.Value, 0.01f); Assert.AreEqual(new Point(9, 9), max.Key); }
// Use this for initialization void Start() { if (seed < 0) { seed = (int)(new System.Random().NextDouble() * 1000000.0); } LODdistances = new float[] { 0.005f, 0.03f, 0.1f, 0.3f, 0.5f, 1f, 2f, 5f, 20f, 30f }; threads = new List <BaseThread>(); threadExceptions = new List <Exception>(); regionCalculationStack = new RegionStack(); regionRenderQueue = new PriorityDictionary <Region>(); regionPlannedDestroyQueue = new PriorityDictionary <Region>(); regionDestroyQueue = new PriorityDictionary <Region>(); celestials = new List <CelestialBody>(); UpdatePlayerPosition(); StartThreads(); Texture2D skybox = new ProceduralSkybox(seed, 256).GenerateTexture(); materialController.skyboxMaterial.SetTexture("_MainTex", skybox); systemData = new SystemData(this, seed); systemData.GenerateSystem(); text = GameObject.Find("Debug").GetComponent <Text>(); planetHud.GeneratePlanetHuds(); }
public void CanAddItems() { var dict = new PriorityDictionary <int, string>(); dict.Add(1, "Test"); Assert.AreEqual(dict[1], "Test"); }
public void CanUseCustomComparer(bool useComparer) { var dict = new PriorityDictionary <Point, TestContainer>((a, b) => a.Content.CompareTo(b.Content)); if (useComparer) { dict = new PriorityDictionary <Point, TestContainer>(new TestContainerComparer()); } for (var x = 0; x < 10; x++) { for (var y = 0; y < 10; y++) { dict.Add(new Point(x, y), new TestContainer(Math.Abs(x - 4) + Math.Abs(y - 6))); } } var min = dict.PopMin(); Assert.AreEqual(0, min.Value.Content); Assert.AreEqual(new Point(4, 6), min.Key); min = dict.PeekMin(); Assert.AreEqual(1, min.Value.Content); var max = dict.PeekMax(); Assert.AreEqual(11, max.Value.Content); }
private static void EnqueueIfPassable(ICollection <int> seen, int newCell, int cost, PriorityDictionary <int, int> queue) { if (Grid.IsValidCell(newCell) && Grid.Element[newCell]?.IsSolid == false && !seen.Contains(newCell)) { seen.Add(newCell); queue.Enqueue(cost, newCell); } }
public void ThrowsIfNotComparable() { var dict = new PriorityDictionary <Point, TestContainer>(); _ = Assert.ThrowsException <ArgumentException>(() => { dict.Add(new Point(), new TestContainer()); dict.Add(new Point(), new TestContainer()); }); }
public void CanRemoveMinValue() { var dict = new PriorityDictionary <int, string>(); dict.Add(1, "BBB"); dict.Add(2, "AAA"); var min = dict.PopMin(); Assert.AreEqual("AAA", min.Value); Assert.AreEqual(2, min.Key); }
/// <summary> /// Initializes the fish object /// </summary> protected override void Awake() { // Call parent LightSource Awake() first base.Awake(); // Initialize action priority dictionary this.actions = new PriorityDictionary(); this.Move(); // Cache the 'Steerable' component attached to the GameObject performing this action this.steerable = transform.GetComponent <Steerable>(); // Set the fish's initial swim direction steerable.WanderAngle = defaultWanderAngle; }
/// <summary> /// Adds nodes to queue by priority, processing the specified node neighbors /// </summary> /// <param name="openPathsQueue">Queue</param> /// <param name="nodesData">Nodes data dictionary</param> /// <param name="currentNode">Current node</param> /// <param name="end">End node</param> /// <param name="heuristicMethod">Heuristic metod</param> /// <param name="heuristicEstimateValue">Heuristic estimate value</param> private static void ProcessNeighbors(PriorityDictionary <GridNode, float> openPathsQueue, Dictionary <GridNode, AStarQueryData> nodesData, GridNode currentNode, GridNode end, HeuristicMethods heuristicMethod, int heuristicEstimateValue) { //Search every possible direction from the current node for (int i = 1; i < currentNode.Connections.Length; i++) { var nextNode = currentNode[i]; if (nextNode == null) { continue; } if (!nodesData.ContainsKey(nextNode)) { nodesData.Add(nextNode, new AStarQueryData()); } if (nextNode.State == GridNodeStates.Closed) { //Impassable node continue; } var nextNodeData = nodesData[nextNode]; if (nextNodeData.State == GridNodeStates.Closed) { //Closed node continue; } float newGone = currentNode.TotalCost + ((int)nextNodeData.State); if (nextNodeData.State == GridNodeStates.Clear && nextNode.TotalCost < newGone) { continue; } nextNodeData.NextNode = currentNode; nextNodeData.Cost = newGone; nextNodeData.State = GridNodeStates.Clear; //Calculate priority from next to end float heuristicValue = CalcHeuristic( nextNode.Center, end.Center, heuristicMethod); openPathsQueue.Enqueue(nextNode, newGone + (heuristicEstimateValue * heuristicValue)); } }
protected override void VisualizeCells(ICollection <VisCellData> newCells) { // Rotation is only used to rotate the offset, radius is the same in all directions int startCell = RotateOffsetCell(Grid.PosToCell(gameObject), offset); if (Grid.IsValidCell(startCell) && Grid.Element[startCell]?.IsSolid == false) { var queue = new PriorityDictionary <int, int>(radius * radius); // Initial cell is seen var seen = HashSetPool <int, ElementConsumerVisualizer> .Allocate(); try { queue.Enqueue(0, startCell); seen.Add(startCell); // Dijkstra's algorithm do { queue.Dequeue(out int cost, out int newCell); if (cost < radius - 1) { // Cardinal directions EnqueueIfPassable(seen, Grid.CellLeft(newCell), cost + 1, queue); EnqueueIfPassable(seen, Grid.CellRight(newCell), cost + 1, queue); EnqueueIfPassable(seen, Grid.CellAbove(newCell), cost + 1, queue); EnqueueIfPassable(seen, Grid.CellBelow(newCell), cost + 1, queue); } } while (queue.Count > 0); // Add all cells as normal color foreach (var cell in seen) { newCells.Add(new VisCellData(cell, color)); } } finally { seen.Recycle(); } } }
public XmlBinaryWriterSession() { _nextKey = 0; _maps = new PriorityDictionary <IXmlDictionary, IntArray>(); _strings = new PriorityDictionary <string, int>(); }
/// <summary> /// Initializes the fish object /// </summary> protected override void Awake() { // Call parent LightSource Awake() first base.Awake(); // Initialize action priority dictionary this.actions = new PriorityDictionary(); this.Move(); // Cache the 'Steerable' component attached to the GameObject performing this action this.steerable = transform.GetComponent<Steerable>(); // Set the fish's initial swim direction steerable.WanderAngle = defaultWanderAngle; }
public void CanBeConstructed() { var dict = new PriorityDictionary <int, string>(); Assert.IsTrue(dict is IDictionary <int, string>); }
/// <summary> /// Gets the path from start to end /// </summary> /// <param name="start">Start node</param> /// <param name="end">End node</param> /// <param name="heuristicMethod">Heuristic metod</param> /// <param name="heuristicEstimateValue">Heuristic estimate value</param> /// <returns>Returns the path from start to end</returns> private static Vector3[] CalcReturnPath(GridNode start, GridNode end, HeuristicMethods heuristicMethod, int heuristicEstimateValue) { //New queue PriorityDictionary <GridNode, float> openPathsQueue = new PriorityDictionary <GridNode, float>(); //Data dictionary Dictionary <GridNode, AStarQueryData> nodesData = new Dictionary <GridNode, AStarQueryData>(); //Add first node openPathsQueue.Enqueue(start, 1); nodesData.Add(start, new AStarQueryData()); bool nodeFound = false; while (openPathsQueue.Count > 0) { //Dequeue the node with lower priority var item = openPathsQueue.Dequeue(); var currentNode = item.Value; var currentNodeData = nodesData[currentNode]; //If the node is not closed to continue the process if (currentNodeData.State != GridNodeStates.Closed) { //Set the node status Closed currentNodeData.State = GridNodeStates.Closed; //If the current node is the destination node has found the way if (currentNode == end) { currentNodeData.State = GridNodeStates.Closed; nodeFound = true; break; } else { //Process neigbors ProcessNeighbors( openPathsQueue, nodesData, currentNode, end, heuristicMethod, heuristicEstimateValue); } } } if (nodeFound) { //We found a valid path List <Vector3> solvedList = new List <Vector3>(); var node = end; while (node != null) { solvedList.Insert(0, node.Center); node = nodesData[node].NextNode; } return(solvedList.ToArray()); } else { //If no result... return(new Vector3[] { }); } }
public XmlBinaryWriterSession() { this.nextKey = 0; this.maps = new PriorityDictionary <IXmlDictionary, IntArray>(); this.strings = new PriorityDictionary <string, int>(); }