Exemple #1
0
        protected TransportRobot GetAdjacentTransporter()
        {
            int x = _location.X;
            int y = _location.Y;

            var proposedLocations = new List <Point>()
            {
                new Point(x - 1, y),
                new Point(x + 1, y),
                new Point(x, y - 1),
                new Point(x, y + 1),
            };

            foreach (Point location in proposedLocations)
            {
                Byte id = Warehouse.ValueAt(location);
                if (id > 9 && Warehouse._Transporters.ContainsKey(id))
                {
                    TransportRobot robot = (TransportRobot)Warehouse._Transporters[id];
                    if (robot._path.Count == 0 && robot._destination_point.Equals(_destination_point))
                    {
                        return(robot);
                    }
                }
            }

            return(null);
        }
Exemple #2
0
        private TransportRobot GetAdjacentTransporter()
        {
            foreach (Point location in _shipPoints)
            {
                Byte id = Warehouse.ValueAt(location);
                if (id > 9 && Warehouse._Transporters.ContainsKey(id))
                {
                    TransportRobot robot = (TransportRobot)Warehouse._Transporters[id];
                    if (robot._state == robot_state.ship &&
                        robot._loadedItems.Count > 0 &&
                        robot._path.Count == 0)
                    {
                        return(robot);
                    }
                }
            }

            return(null);
        }
Exemple #3
0
        public bool LeavePathForPicker()
        {
            int x = _destination_point.X;
            int y = _destination_point.Y;

            var proposedLocations = new List <Point>()
            {
                new Point(x - 1, y),
                new Point(x + 1, y),
                new Point(x, y - 1),
                new Point(x, y + 1),
            };

            foreach (Point newgoal in proposedLocations)
            {
                if (Warehouse.ValueAt(newgoal) == 0)
                {
                    Program.PrintLine(_id + "pick or slot reroute to " + newgoal);
                    _path = AStarPathfinding.FindPath(_location, newgoal, out _noPath);
                    return(true);
                }
            }
            return(false);
        }
Exemple #4
0
        protected virtual void MoveToNextTile()
        {
            SetLocation(_path.Pop());
            _actionString += " f";

            if (_path.Count > 0)
            {
                Byte robot_id = Warehouse.ValueAt(_path.Peek());
                if (robot_id == 0 || robot_id == 1) // No robot standing at this tile, road is clear
                {
                    return;
                }

                Robot another_robot = Warehouse._AllMovingRobots[robot_id];
                if (IsCollideWith(another_robot))
                {
                    Program.Print(_id + " is collide with " + robot_id + " at " + _path.Peek() + "\n");
                    if (another_robot._state == robot_state.slot || another_robot._state == robot_state.pick)
                    {
                        another_robot.AvoidToLeavePath();
                    }
                }
            }
        }
Exemple #5
0
        public override void GenerateAction(int sec)
        {
            if (sec == 0)
            {
                _actionString = _id.ToString(); // add id at sec 0
                if (_noPath)
                {
                    Program.Print("Refind path from " + _location + " to " + _destination_point + "\n");
                    _path = AStarPathfinding.FindPath(_location, _destination_point, out _noPath);
                }
            }

            if (_noPath)
            {
                _actionString += " n";
                return;
            }

            if (_state == robot_state.free) // no action
            {
                _actionString += " n";
            }
            else if (_path.Count > 0) // moving
            {
                if (Rotate() == true)
                {
                    return;
                }

                Byte robot_id = Warehouse.ValueAt(_path.Peek());
                if (robot_id == 0 || robot_id == 1) // No robot standing at this tile, road is clear
                {
                    MoveToNextTile();
                }
                else // new location is obstructed
                {
                    _actionString += " n";
                    Robot another_robot = Warehouse._AllMovingRobots[robot_id];
                    if (another_robot._path.Count == 0)// anther robot is stopping
                    {
                        Program.Print(_id + " is obstructed by " + robot_id + another_robot._state + " at " + _path.Peek() + "\n");
                        if (Warehouse._Transporters.ContainsKey(another_robot._id) && _destination_point.Equals(another_robot._destination_point))
                        {
                            TransportRobot robot = (TransportRobot)another_robot;
                            robot.LeavePathForPicker();
                            return;
                        }
                        if (another_robot._state == robot_state.slot || another_robot._state == robot_state.pick)
                        {
                            another_robot.AvoidToLeavePath();
                        }
                    }
                    else if (IsCollideWith(another_robot))
                    {
                        Program.Print(_id + "" + _location + "collide with " + robot_id + _path.Peek() + "\n");
                        if (another_robot._state == robot_state.slot || another_robot._state == robot_state.pick)
                        {
                            if (another_robot.AvoidToLeavePath() == false)
                            {
                                this.AvoidToLeavePath();
                            }
                        }
                        else
                        {
                            AvoidToLeavePath();
                        }
                    }
                }
            }
            else // we arrive at the destination
            {
                if (_state == robot_state.returning) // robot return to charging point
                {
                    if (Rotate(Direction.Up) == false) // rotate to upward position
                    {
                        _state         = robot_state.free;
                        _actionString += " n";
                    }
                }
                else if (_state == robot_state.pick && _order._quantity > 0)
                {
                    Pick(sec);
                }
                else if (_state == robot_state.slot && _order._quantity > 0)
                {
                    Slot(sec);
                }
            }
        }
Exemple #6
0
        public override void GenerateAction(int sec)
        {
            if (sec == 0)
            {
                _actionString = _id.ToString(); // add id at sec 0
                if (_noPath)
                {
                    Program.Print("Refind path from " + _location + " to " + _destination_point + "\n");
                    _path = AStarPathfinding.FindPath(_location, _destination_point, out _noPath);
                }
            }

            if (_noPath)
            {
                _actionString += " n";
                return;
            }

            // when picking, stop 1 tile before the pickup point
            if (_path.Count == 1 &&
                _path.Peek().Equals(_destination_point) &&
                (_state == robot_state.pick || _state == robot_state.slot))
            {
                _path.Pop();
            }

            if (_state == robot_state.free) // no action
            {
                _actionString += " n";
            }
            else if (_path.Count > 0) // moving
            {
                Byte robot_id = Warehouse.ValueAt(_path.Peek());
                if (robot_id == 0 || robot_id == 1) // No robot standing at this tile, road is clear
                {
                    if (Rotate() == true)
                    {
                        return;
                    }
                    MoveToNextTile();
                }
                else // new location is obstructed
                {
                    _actionString += " n";
                    Robot another_robot = Warehouse._AllMovingRobots[robot_id];
                    if (another_robot._path.Count == 0)
                    {
                        Program.Print(_id + " is obstructed by " + robot_id + another_robot._state + " at " + _path.Peek() + "\n");
                        if (another_robot._state == robot_state.slot || another_robot._state == robot_state.pick)
                        {
                            another_robot.AvoidToLeavePath();
                        }
                    }
                    else if (IsCollideWith(another_robot))
                    {
                        Program.Print(_id + "" + _location + "collide with " + robot_id + _path.Peek() + "\n");
                        if (another_robot._state == robot_state.slot || another_robot._state == robot_state.pick)
                        {
                            if (another_robot.AvoidToLeavePath() == false)
                            {
                                this.AvoidToLeavePath();
                            }
                        }
                        else
                        {
                            AvoidToLeavePath();
                        }
                    }
                }
            }
            else // we arrive at the destination
            {
                if (_state == robot_state.returning)
                {
                    if (Rotate(Direction.Up) == false)
                    {
                        _state         = robot_state.free;
                        _actionString += " n";
                    }
                }
                else
                {
                    _actionString += " n";
                }
            }
        }
Exemple #7
0
        public bool AvoidToLeavePath()
        {
            if (_isLoading == true || _isUnloading == true)
            {
                return(false);
            }

            Point LeftTile;
            Point RightTile;
            Point BackTile;
            Point FrontTile;

            switch (_direction)
            {
            case Direction.Up:
                LeftTile  = new Point(_location.X - 1, _location.Y);
                RightTile = new Point(_location.X + 1, _location.Y);
                BackTile  = new Point(_location.X, _location.Y + 1);
                FrontTile = new Point(_location.X, _location.Y - 1);
                break;

            case Direction.Down:
                LeftTile  = new Point(_location.X + 1, _location.Y);
                RightTile = new Point(_location.X - 1, _location.Y);
                BackTile  = new Point(_location.X, _location.Y - 1);
                FrontTile = new Point(_location.X, _location.Y + 1);
                break;

            case Direction.Left:
                LeftTile  = new Point(_location.X, _location.Y + 1);
                RightTile = new Point(_location.X, _location.Y - 1);
                BackTile  = new Point(_location.X + 1, _location.Y);
                FrontTile = new Point(_location.X - 1, _location.Y);
                break;

            case Direction.Right:
                LeftTile  = new Point(_location.X, _location.Y - 1);
                RightTile = new Point(_location.X, _location.Y + 1);
                BackTile  = new Point(_location.X - 1, _location.Y);
                FrontTile = new Point(_location.X + 1, _location.Y);
                break;

            default:
                return(false);
            }

            if (Warehouse.ValueAt(LeftTile) == 0)
            {
                _path.Push(_location);
                _path.Push(LeftTile);
                Program.PrintLine(_id + "Move to left side" + _location + LeftTile);
                return(true);
            }
            else if (Warehouse.ValueAt(RightTile) == 0)
            {
                _path.Push(_location);
                _path.Push(RightTile);
                Program.PrintLine(_id + "Move to right side" + _location + RightTile);
                return(true);
            }
            else if (Warehouse.ValueAt(BackTile) == 0)
            {
                _path.Push(_location);
                _path.Push(BackTile);
                Program.PrintLine(_id + "Move to back side" + _location + BackTile);
                return(true);
            }
            else if (Warehouse.ValueAt(FrontTile) == 0)
            {
                _path.Push(_location);
                _path.Push(FrontTile);
                Program.PrintLine(_id + "Move to front side" + _location + FrontTile);
                return(true);
            }
            Program.PrintLine(_id + "can not move");
            return(false);
        }