private bool HasDestinationToMove(IChainLink <IMainFieldSquare, Direction> item, out int destination) { destination = -1; if (!item.Value.State.IsActive || item.Value.State.Direction.IsNone()) { return(false); } var direction = item.Value.State.Direction; // look for an active square; var nextItem = item.Next(direction.Value); while (nextItem != null && !nextItem.Value.State.IsActive) { nextItem = nextItem.Next(direction.Value); } if (nextItem != null && nextItem != item.Next(direction.Value)) { // destination is last inactive square (before first active square!) on the way. var prevItem = nextItem.Next(direction.Opposite).Value; destination = direction.IsHorzOrient() ? (int)prevItem.X : (int)prevItem.Y; } else if (nextItem == null || CanIgnoreObstacles(item)) { // if there aren't any obstacle or they can be ignored, // so destination is the last square on the way destination = (int)GetLastIndexInDirection(direction.Value); } return(destination != -1); }
private void CountCombinationSize(IChainLink <IMainFieldSquare, Direction> item, SquareCombinationCounter counter = null) { if (item == null) { return; } var value = item.Value; // skip if item is not active or it was already counted as part of combination if (!value.State.IsActive || _matrixCounterRefs[value.X, value.Y] != null) { return; } if (counter == null) { counter = new SquareCombinationCounter(value.Color); _counters.Add(counter); } if (counter.Color != value.Color) { return; } counter.Squares.Add(value); _matrixCounterRefs[value.X, value.Y] = counter; CountCombinationSize(item.Next(Direction.Left), counter); CountCombinationSize(item.Next(Direction.Down), counter); CountCombinationSize(item.Next(Direction.Right), counter); CountCombinationSize(item.Next(Direction.Up), counter); }
/// <summary> /// Find out if squares on the way can be ignored. /// There are two consecutive conditions: /// 1) all active squares on the way move opposite direction; /// 2) initial obstacle that the square adjacent to was destroyed and /// all squares on the way are adjacent to the same initial obstacle; /// /// If the first case is false so the second one should be false as well to /// return negative result otherwise there is positive result. /// </summary> private bool CanIgnoreObstacles(IChainLink <IMainFieldSquare, Direction> item) { var direction = item.Value.State.Direction; var obstacle = item.Value.State.Obstacle; var nextItem = item.Next(direction.Value); var initialObstacle = _initialObstacles[item.Value.State.Obstacle]; var obstaclesIgnored = !this[initialObstacle.X, initialObstacle.Y].Value.State.IsActive; while (nextItem != null) { var square = nextItem.Value; if (square.State.IsActive) { obstaclesIgnored = obstaclesIgnored && square.State.Obstacle == obstacle; if ((square.State.Direction.Value != direction.Opposite) && !obstaclesIgnored) { return(false); } } nextItem = nextItem.Next(direction.Value); } return(true); }
public void AddSuccessor(IChainLink successor) { _allSuccessors[successor.Destination] = successor; _baggageDistributors[successor.Destination] = new Queue <IBaggage>(); Task.Run(() => { DistributeBaggage(successor.Destination); }); }
private void PassQueuedBaggage(IChainLink gate) { var queue = _gateQueues[gate]; if (!queue.Any() || gate.Status != NodeState.Free) { return; } gate.PassBaggage(queue.Dequeue()); }
public void AddNextLink(IChainLink <TSpecification, TResult> link) { if (_successor == null) { _successor = link; } else { _successor.AddNextLink(link); } }
private void Initialize() { _items = new IChainLink <T, Direction> [Width, Height]; for (uint x = 0; x < Width; x++) { for (uint y = 0; y < Height; y++) { _items[x, y] = new MatrixChainLink(Matrix[x, y]); } } ChainLinks(); }
public bool ExecuteStep() { bool nextLinkExists = Enumerator.MoveNext(); if (nextLinkExists) { IChainLink currentLink = Enumerator.Current; currentLink.Execute(Storage.Data); } //else //{ // Enumerator.Reset(); //} return(nextLinkExists); }
private void PassOrEnqueueBaggage(IChainLink gate, IBaggage bag) { bag.TransportationStartTime = TimerService.GetTicksSinceSimulationStart(); bag.TransporterId = "Queue AA"; if (gate.Status == NodeState.Free) { gate.PassBaggage(bag); } else { if (gate.OnStatusChangedToFree == null) { gate.OnStatusChangedToFree += () => { PassQueuedBaggage(gate); } } ; _gateQueues[gate].Enqueue(bag); } }
/// <summary> /// Set the next link in the chain. /// </summary> /// <param name="link"></param> void IChainLink <TRequest> .SetNext(IChainLink <TRequest> link) { _successor = link; }
public bool Equals(IChainLink <ICommit> other) { return(this == other); }
IChainFollowedBy <TRequest> IChainStartWith <TRequest> .StartWith(IChainLink <TRequest> link) { _first = link; _previous = link; return(this); }
IChainFollowedBy <TRequest> IChainFollowedBy <TRequest> .FollowedBy(IChainLink <TRequest> link) { _previous.SetNext(link); _previous = link; return(this); }
protected ChainLink(IChainLink <TSpecification, TResult> successor) { _successor = successor; }
public static IChainLink <TSpecification, TResult> Finally <TSpecification, TResult>(this IChainLink <TSpecification, TResult> chain, IChainLink <TSpecification, TResult> link) { chain.AddNextLink(link); return(chain); }
public void SetSuccessor(IChainLink nextLink) { NextLink = nextLink; }
public bool Equals(IChainLink <IVersion> other) { return(this == other); }
public void AddSuccessor(IChainLink chainLink) { _allSuccessors.Add(chainLink); }
public void LinkTo(Direction direction, IChainLink <T, Direction> link) { _links[direction] = link; }