Esempio n. 1
0
        private bool ValidateEndState(FluidRouteState state, HashSet <RoutingCoordinate> destinations)
        {
            if (state.Depth != Depth.None)
            {
                return(false);
            }

            if (state.Building.Building.Name != "pipe")
            {
                return(false);
            }

            bool found = false;

            BuildingRotation[] rotations = new BuildingRotation[] { BuildingRotation.North, BuildingRotation.East, BuildingRotation.South, BuildingRotation.West };
            foreach (var rotation in rotations)
            {
                if (destinations.Where((d) => d.Position == state.Position + rotation.ToVector()).Any())
                {
                    found = true;
                }
            }

            if (!found)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        public Searchspace Route(ItemAmount item, Searchspace space, IEnumerable <RoutingCoordinate> sources, IEnumerable <RoutingCoordinate> destinations)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (destinations == null)
            {
                throw new ArgumentNullException("destinations");
            }

            AStar <FluidRouteState> star = new AStar <FluidRouteState>();

            star.StateGenerator    = (s) => s.NextStates(Grader.CostForBuilding, PipeToGround, Pipe);
            star.EndStateValidator = ValidateEndState;

            foreach (var dest in destinations)
            {
                star.AddDestination(new RoutingCoordinate(dest.Position, dest.State, dest.Rotation.Invert()));
            }

            foreach (var source in sources)
            {
                var startBuilding = new VirtualFlowStep(item, PipeToGround, source.Position, source.Rotation.Invert());
                var startState    = new FluidRouteState(startBuilding, 0, source.Position, space, Depth.None, source.Rotation, true);
                star.AddState(startState);
            }

            while (!star.Step())
            {
            }

            return(star.EndState.Space);
        }