コード例 #1
0
        // This method is called by ElevatorController when an elevator arrives at a floor.
        public void ElevatorArrived(Elevator elev, Elevator.Direction elevDir)
        {
            lock (m_riders)
            {
                // First unload riders whose destination is this floor.
                List <Rider> unloadedRiders = elev.UnloadRiders();

                // Then load riders who are going the same direction as elevator.
                for (int i = m_riders.Count - 1; i >= 0; i--)
                {
                    Rider rider = m_riders[i];
                    Elevator.Direction riderDir = (rider.m_destFloor > m_floor) ? Elevator.Direction.Up : Elevator.Direction.Down;
                    if (riderDir == elevDir && rider.m_destFloor != m_floor)
                    {
                        elev.LoadRider(rider);
                        m_riders.RemoveAt(i);
                    }
                }

                // Add unloaded riders back to floor.
                for (int i = 0; i < unloadedRiders.Count; i++)
                {
                    AddRider(unloadedRiders[i]);
                }
            }
        }
コード例 #2
0
 void HandleElevatorEvent(Elevator elev, Elevator.State state, int floor, Elevator.Direction dir)
 {
     if (state == Elevator.State.VisitingFloor)
     {
         Floors[floor].ElevatorArrived(elev, dir);
     }
 }
コード例 #3
0
        int FindBestElevator(int floor, Elevator.Direction dir)
        {
            // This method chooses which elevator to route a particular request to.  It first finds the closest
            // idle elevator.  Then it finds the closest one moving in the proper direction (and above/below as necessary).
            // It then selects whichever of these is closest.  If none are found, the method returns -1.

            bool goingUp = dir == Elevator.Direction.Up;
            int  closestIdleIdx = -1, closestIdleDist = int.MaxValue;
            int  closestMovingIdx = -1, closestMovingDist = int.MaxValue;

            for (int i = 0; i < Elevators.Length; i++)
            {
                Elevators[i].GetState(out Elevator.State elevState, out Elevator.Direction elevDir, out int elevFloor);
                int dist = Math.Abs(floor - elevFloor);

                if (elevState == Elevator.State.Idle)
                {
                    if (dist < closestIdleDist)
                    {
                        closestIdleIdx  = i;
                        closestIdleDist = dist;
                    }
                }
                else if ((elevDir == dir) && (goingUp ? (elevFloor <= floor) : (elevFloor >= floor)))
                {
                    if (dist < closestMovingDist)
                    {
                        closestMovingIdx  = i;
                        closestMovingDist = dist;
                    }
                }
            }

            int bestIdx;

            if (closestIdleIdx == -1 && closestMovingIdx == -1)                         // If no closest idle nor moving, return none found
            {
                bestIdx = -1;
            }
            else if (closestMovingIdx == -1)                                                            // If no closest moving, use closest idle
            {
                bestIdx = closestIdleIdx;
            }
            else if (closestIdleIdx == -1)                                                              // If no closest idle, use closest moving
            {
                bestIdx = closestMovingIdx;
            }
            else
            {
                bestIdx = closestMovingDist < closestIdleDist ? closestMovingIdx : closestIdleIdx;                      // If both, choose closer one
            }
            return(bestIdx);
        }
コード例 #4
0
        static void Main(string[] args)
        {
            // This code reads input commands from a text file and passes them along to an
            // elevator controller object.
            string path = args.Length > 0 ? args[0] : "..\\..\\ElevatorInput.txt";

            string[] lines = File.ReadAllLines(path);

            ElevatorController ctrl = new ElevatorController();

            for (int i = 0; i < lines.Length; i++)
            {
                string[] cmd = lines[i].ToLower().Split(" ".ToCharArray());

                try
                {
                    if (cmd[0] == "init")                                               // init {numElevs} {numFloors}				- Initializes the ElevatorController
                    {
                        ctrl.Start(int.Parse(cmd[1]), int.Parse(cmd[2]));
                    }
                    else if (cmd[0] == "sleep")                                         // sleep {numSecs}							- sleeps this thread for numSecs
                    {
                        Thread.Sleep(int.Parse(cmd[1]) * 1000);
                    }
                    else if (cmd[0] == "rider")                                         // rider {name} {startFloor} {destFloor}	- submits a request for a rider
                    {
                        Rider rider = new Rider()
                        {
                            m_name = cmd[1], m_destFloor = int.Parse(cmd[3])
                        };
                        int startFloor = int.Parse(cmd[2]);

                        ctrl.Floors[startFloor].AddRider(rider);
                        Elevator.Direction dir = rider.m_destFloor > startFloor ? Elevator.Direction.Up : Elevator.Direction.Down;
                        ctrl.RequestElevator(startFloor, dir);
                    }
                    else if (cmd[0] == "quit")                                          // quit the app
                    {
                        break;
                    }
                    else
                    {
                        // Ignore all other input (like comments)
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0:mm:ss} - Exception on line {1}: {2}", DateTime.Now, i, ex);
                }
            }

            ctrl.Stop();
        }
コード例 #5
0
        private Elevator GetFarthestElevatorInDirection(Elevator elevator, Elevator.Direction direction)
        {
            var currentElevator = elevator;

            Elevator nextElevator;

            while ((nextElevator = currentElevator.GetElevatorInDirection(direction)) != null)
            {
                currentElevator = nextElevator;
            }

            return(currentElevator);
        }
コード例 #6
0
    public void Server_RaiseLowerFloor(RPCMessage msg)
    {
        if (!CanMove())
        {
            return;
        }
        Elevator.Direction direction = (Elevator.Direction)msg.read.Int32();
        bool flag = msg.read.Bit();

        if (Interface.CallHook("OnElevatorButtonPress", this, msg.player, direction, flag) == null)
        {
            SetFlag((direction == Elevator.Direction.Up) ? Flags.Reserved1 : Flags.Reserved2, true);
            owner.Server_RaiseLowerElevator(direction, flag);
            Invoke(ClearDirection, 0.7f);
            if (liftButtonPressedEffect.isValid)
            {
                Effect.server.Run(liftButtonPressedEffect.resourcePath, base.transform.position, Vector3.up);
            }
        }
    }
コード例 #7
0
 public FloorRequest(int floor, Elevator.Direction dir)
 {
     m_floor = floor; m_dir = dir;
 }
コード例 #8
0
 public void RequestElevator(int floor, Elevator.Direction dir)
 {
     lock (m_requests)
         m_requests.Enqueue(new FloorRequest(floor, dir));
 }
コード例 #9
0
ファイル: ElevatorTask.cs プロジェクト: andreyGladin/Elevator
 public ElevatorTask(string type, int floor = 0, Elevator.Direction dir = Elevator.Direction.Stop)
 {
     TaskType        = type;
     TargetFloor     = floor;
     TargetDirection = dir;
 }