I must admit that when i started to code an elevator system for a building sim, i thought it would be as easy as max an hour of work.
- Push the button, enter the elevator, exit on your floor.
And in theory, it is very easy, especially when you only have one user of the elevator, and just a couple of floors in your building.
But, this turned out to not be working so well when you have more elevators and a really tall building and 100+ employees to use the elevator.
Scenario
A worker with a high index and a late update (compared to all workers with index below), comes to the elevator and push the button. The elevator is already in use, and thus the worker has to wait.
This goes on almost forever, because workers with lower indexes, who’s also waiting for the elevator will be getting it first since they will always push the button before a worker with higher index, because they have their update methods earlier.
So, what needed to be done was a queue system that records which worker was there first, so that their “button push” will be dealt with in a first in, first out – manner.
But, if there is always the fifo-person to use and order the elevator to a certain floor, then he could be the only passenger at any given time. Ofcourse this is not really effecient, nor user friendly. And it wasnt they way i wanted it to work. More people are supposed to ride it at any given time.
But, this is also supposed to be effecient.
The scenario turned out like this.
- Push button, to order the elevator to my floor.
- The elevator cart is on my floor, was it because i pushed the button, or is it going to pass me?
- If it’s going to pass me, stop and enter the elevator ONLY IF
- There is a passenger slot free.
- The elevator is moving in the direction i want to go.
- The elevators current destination is further or equal to my destination. - If it’s because i pushed the button, enter the elevator.
- Wait until the elevator is on my destination floor.
- Exit.
And then there is the twist with draw order, when i have multiple elevator carts in the same elevator shaft, but thats another story! =)