3be175d79a6f948cff112c4da259f73f39f212ff
[cs356-p1-elevator.git] / elevator.cpp
1 #include "elevator.hpp"
2
3 Elevator::Elevator ()
4         : door_(CLOSED)
5         , direction_(IDLE)
6         , current_position_()
7         , stops_()
8         , ELEVATOR_STEP(0.1)
9 {
10         // Intentionally Left Empty
11 }
12
13 bool Elevator::currently_at_stop () const
14 {
15         StopList::const_iterator it;
16         Stop current(current_position_, direction_);
17
18         for (it = stops_.begin (); it != stops_.end (); it++)
19                 if (*it == current)
20                         return true;
21
22         return false;
23 }
24
25 void Elevator::stop_at (Stop &stop)
26 {
27         StopList::iterator it;
28
29         /* If this is an "ALL" Stop, it supercedes all others */
30         if (stop.getDirection() == ALL)
31         {
32                 stops_.remove (stop);
33                 stops_.push_back (stop);
34                 return;
35         }
36
37         /* Check if this stop already exists. If so, just leave */
38         for (it = stops_.begin(); it != stops_.end(); it++)
39                 if (*it == stop)
40                         return;
41
42         /* The stop did not already exist, so add it */
43         stops_.push_back (stop);
44 }
45
46 float Elevator::distance_from (Position &pos) const
47 {
48         if (current_position_ > pos)
49                 return current_position_ - pos;
50
51         return pos - current_position_;
52 }
53
54 void Elevator::move ()
55 {
56         static int wait = 0;
57
58         /* Wait around if we need to.
59          *
60          * This is an artificial delay to make the elevators work in a
61          * much more natural fashion. This allows some delay to leave the
62          * doors open, or for the user to press floor selection buttons
63          * before the elevator starts moving
64          */
65         if (wait > 0)
66         {
67                 --wait;
68                 return;
69         }
70
71         /* close the door if it is open. This is a requirement to move */
72         if (door_ != CLOSED)
73         {
74                 wait = 10;
75                 close_door ();
76                 return;
77         }
78
79         if (currently_at_stop ())
80         {
81                 wait = 10;                                                                      // delay around for 10 steps
82                 stops_.remove (Stop(current_position_, direction_));
83                 open_door ();                                                           // call into the GUI to open the door
84                 return;
85         }
86
87         /* Calculate the number of Stops above and below our
88          * current position */
89         StopList::const_iterator it;
90         Stop current = Stop(current_position_, direction_);
91         int stops_above = 0;
92         int stops_below = 0;
93
94         for (it = stops_.begin(); it != stops_.begin(); it++)
95         {
96                 if (current < *it)
97                         ++stops_above;
98
99                 if (current > *it)
100                         ++stops_below;
101         }
102
103         /* Check if we need to change direction */
104         if (direction_ == UP && stops_above == 0 && stops_below > 0)
105                 direction_ = DOWN;
106
107         if (direction_ == DOWN && stops_below == 0 && stops_above > 0)
108                 direction_ = UP;
109
110         if (stops_above == 0 && stops_below == 0)
111                 direction_ = IDLE;
112
113
114         /* Move in the correct direction */
115         switch (direction_)
116         {
117                 case IDLE:
118                         break;
119                 case UP:
120                         current_position_ += ELEVATOR_STEP;
121                         break;
122                 case DOWN:
123                         current_position_ -= ELEVATOR_STEP;
124                         break;
125                 default:
126                         std::cout << __FILE__ << ":" << __LINE__ << " Unhandled Elevator Position" << std::endl;
127                         break;
128         }
129
130         /* Call the GUI with our updated position */
131         update_position ();
132 }
133
134 bool Elevator::is_idle () const
135 {
136         return direction_ == IDLE;
137 }
138
139 void Elevator::update_position () const
140 {
141         std::cout << "Updating the GUI with our position" << std::endl;
142 }
143
144 void Elevator::open_door () const
145 {
146         std::cout << "Opening Door" << std::endl;
147 }
148
149 void Elevator::close_door () const
150 {
151         std::cout << "Closing Door" << std::endl;
152 }
153
154
155
156
157
158 /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */