|
|
@ -1,4 +1,6 @@
|
|
|
|
### v0.1
|
|
|
|
### v0.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
It's not really optimal, as elevator builds it's destination queue in plain format without thinking about it's current movement direction.
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
```js
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -10,47 +12,48 @@
|
|
|
|
const MAX_FLOOR = floors.length - 1;
|
|
|
|
const MAX_FLOOR = floors.length - 1;
|
|
|
|
|
|
|
|
|
|
|
|
var elev_state = [];
|
|
|
|
var elev_state = [];
|
|
|
|
|
|
|
|
|
|
|
|
console.clear();
|
|
|
|
console.clear();
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize elevator states
|
|
|
|
// Initialize elevator states
|
|
|
|
if (elev_state == []) {
|
|
|
|
if (elev_state == []) {
|
|
|
|
for (var i = 0; i < elevators.length; i++) {
|
|
|
|
for (var i = 0; i < elevators.length; i++) {
|
|
|
|
elev_state.push(DIR_STOP);
|
|
|
|
elev_state.push(DIR_STOP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// HELPER functions
|
|
|
|
// HELPER functions
|
|
|
|
// Command elevator to move to floor
|
|
|
|
// Command elevator to move to floor
|
|
|
|
sendElevator = function(floor, dir) {
|
|
|
|
sendElevator = function(floor, dir) {
|
|
|
|
// debug
|
|
|
|
// debug
|
|
|
|
for (var i = 0; i < elevators.length; i++) {
|
|
|
|
for (var i = 0; i < elevators.length; i++) {
|
|
|
|
var elevator = elevators[i];
|
|
|
|
var elevator = elevators[i];
|
|
|
|
|
|
|
|
|
|
|
|
//console.log("Elevator " + elev + " pressed floors:");
|
|
|
|
//console.log("Elevator " + elev + " pressed floors:");
|
|
|
|
//console.log(elevator.getPressedFloors());
|
|
|
|
//console.log(elevator.getPressedFloors());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// At first trying to find closest empty elevator and send it
|
|
|
|
// At first trying to find closest empty elevator and send it
|
|
|
|
if (!getFreeElevator(floor)) {
|
|
|
|
if (!getFreeElevator(floor)) {
|
|
|
|
// We didn't found free elevator
|
|
|
|
// We didn't found free elevator
|
|
|
|
var less_loaded = getLessLoaded()[0];
|
|
|
|
var less_loaded = getLessLoaded()[0];
|
|
|
|
less_loaded.destinationQueue.push(floor);
|
|
|
|
if (!less_loaded.destinationQueue.includes(floor)) {
|
|
|
|
//less_loaded.destinationQueue = superSort(less_loaded);
|
|
|
|
less_loaded.destinationQueue.push(floor);
|
|
|
|
less_loaded.checkDestinationQueue();
|
|
|
|
//less_loaded.destinationQueue = superSort(less_loaded);
|
|
|
|
|
|
|
|
less_loaded.checkDestinationQueue();
|
|
|
|
// debug
|
|
|
|
// debug
|
|
|
|
console.log("Added floor " + floor + " to less loaded elevator");
|
|
|
|
console.log("Added floor " + floor + " to less loaded elevator");
|
|
|
|
console.log("Elevator queue: ");
|
|
|
|
console.log("Elevator queue: ");
|
|
|
|
console.log(less_loaded.destinationQueue);
|
|
|
|
console.log(less_loaded.destinationQueue);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get distance of elevator from floor
|
|
|
|
// Get distance of elevator from floor
|
|
|
|
getDistance = function(elevator, floor) {
|
|
|
|
getDistance = function(elevator, floor) {
|
|
|
|
return Math.abs(elevator.currentFloor() - floor);
|
|
|
|
return Math.abs(elevator.currentFloor() - floor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Found free elevator closer to required floor and send it there
|
|
|
|
// Found free elevator closer to required floor and send it there
|
|
|
|
getFreeElevator = function(floorRequired = 0) {
|
|
|
|
getFreeElevator = function(floorRequired = 0) {
|
|
|
|
// closest empty elevator
|
|
|
|
// closest empty elevator
|
|
|
@ -66,7 +69,7 @@
|
|
|
|
// Found closer elevator from those who had 0 load
|
|
|
|
// Found closer elevator from those who had 0 load
|
|
|
|
for (var elev = 0; elev < zero.length; elev++) {
|
|
|
|
for (var elev = 0; elev < zero.length; elev++) {
|
|
|
|
var elevator = zero[elev];
|
|
|
|
var elevator = zero[elev];
|
|
|
|
|
|
|
|
|
|
|
|
if (best == null) {
|
|
|
|
if (best == null) {
|
|
|
|
best = elevator;
|
|
|
|
best = elevator;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -77,20 +80,20 @@
|
|
|
|
if (best != null) {
|
|
|
|
if (best != null) {
|
|
|
|
// debug
|
|
|
|
// debug
|
|
|
|
console.log("Empty elevator moving to floor " + floorRequired);
|
|
|
|
console.log("Empty elevator moving to floor " + floorRequired);
|
|
|
|
|
|
|
|
|
|
|
|
// If we found one command to move to requested floor
|
|
|
|
// If we found one command to move to requested floor
|
|
|
|
best.goToFloor(floorRequired);
|
|
|
|
best.goToFloor(floorRequired);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Super sort
|
|
|
|
// Super sort
|
|
|
|
superSort = function(elevator) {
|
|
|
|
superSort = function(elevator) {
|
|
|
|
var queue = elevator.destinationQueue;
|
|
|
|
var queue = elevator.destinationQueue;
|
|
|
|
var currentFloor = elevator.currentFloor;
|
|
|
|
var currentFloor = elevator.currentFloor;
|
|
|
|
queue.sort();
|
|
|
|
queue.sort();
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < queue.length; i++) {
|
|
|
|
for (var i = 0; i < queue.length; i++) {
|
|
|
|
if (queue[i] > currentFloor) {
|
|
|
|
if (queue[i] > currentFloor) {
|
|
|
|
var idx = i;
|
|
|
|
var idx = i;
|
|
|
@ -101,7 +104,7 @@
|
|
|
|
modQueue = modQueue.concat(queue.slice(0, idx));
|
|
|
|
modQueue = modQueue.concat(queue.slice(0, idx));
|
|
|
|
return modQueue;
|
|
|
|
return modQueue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get less loaded elevator
|
|
|
|
// Get less loaded elevator
|
|
|
|
getLessLoaded = function() {
|
|
|
|
getLessLoaded = function() {
|
|
|
|
return elevators.sort( (a,b) => (a.destinationQueue.length - b.destinationQueue.length) );
|
|
|
|
return elevators.sort( (a,b) => (a.destinationQueue.length - b.destinationQueue.length) );
|
|
|
@ -110,31 +113,35 @@
|
|
|
|
// ELEVATOR events
|
|
|
|
// ELEVATOR events
|
|
|
|
for (var elev = 0; elev < elevators.length; elev++) {
|
|
|
|
for (var elev = 0; elev < elevators.length; elev++) {
|
|
|
|
var elevator = elevators[elev];
|
|
|
|
var elevator = elevators[elev];
|
|
|
|
|
|
|
|
|
|
|
|
// Elevator is not doing anything
|
|
|
|
// Elevator is not doing anything
|
|
|
|
elevator.on("idle", function() {
|
|
|
|
elevator.on("idle", function() {
|
|
|
|
// When idle move to 0 floor
|
|
|
|
// When idle move to 0 floor
|
|
|
|
// elevator.goToFloor(0);
|
|
|
|
// elevator.goToFloor(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Passenger requested floor
|
|
|
|
// Passenger requested floor
|
|
|
|
elevator.on("floor_button_pressed", function(floor) {
|
|
|
|
elevator.on("floor_button_pressed", function(floor) {
|
|
|
|
// debug
|
|
|
|
// debug
|
|
|
|
//console.log("Elevator " + elev + " pressed floors:");
|
|
|
|
//console.log("Elevator " + elev + " pressed floors:");
|
|
|
|
//console.log(elevator.getPressedFloors());
|
|
|
|
//console.log(elevator.getPressedFloors());
|
|
|
|
|
|
|
|
|
|
|
|
elevator.goToFloor(floor);
|
|
|
|
//elevator.goToFloor(floor);
|
|
|
|
|
|
|
|
if (!elevator.destinationQueue.includes(floor)) {
|
|
|
|
|
|
|
|
elevator.destinationQueue.push(floor);
|
|
|
|
|
|
|
|
elevator.checkDestinationQueue();
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Stopped on a particulat floor
|
|
|
|
// Stopped on a particulat floor
|
|
|
|
elevator.on("stopped_at_floor", function(floorNum) {
|
|
|
|
elevator.on("stopped_at_floor", function(floorNum) {
|
|
|
|
// debug
|
|
|
|
// debug
|
|
|
|
// console.log("Elevator " + elev + " pressed floors:");
|
|
|
|
// console.log("Elevator " + elev + " pressed floors:");
|
|
|
|
// console.log(elevator.getPressedFloors());
|
|
|
|
// console.log(elevator.getPressedFloors());
|
|
|
|
console.log("Elevator stopped at floor: " + floorNum);
|
|
|
|
console.log("Elevator stopped at floor: " + floorNum);
|
|
|
|
|
|
|
|
|
|
|
|
elev_state[elev] = DIR_STOP;
|
|
|
|
elev_state[elev] = DIR_STOP;
|
|
|
|
|
|
|
|
|
|
|
|
// Change direction arrows lights
|
|
|
|
// Change direction arrows lights
|
|
|
|
var state_up, state_down = false;
|
|
|
|
var state_up, state_down = false;
|
|
|
|
switch (floorNum) {
|
|
|
|
switch (floorNum) {
|
|
|
@ -154,7 +161,7 @@
|
|
|
|
elevator.goingUpIndicator(state_up);
|
|
|
|
elevator.goingUpIndicator(state_up);
|
|
|
|
elevator.goingDownIndicator(state_down);
|
|
|
|
elevator.goingDownIndicator(state_down);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Passing floor
|
|
|
|
// Passing floor
|
|
|
|
elevator.on("passing_floor", function(floor, dir) {
|
|
|
|
elevator.on("passing_floor", function(floor, dir) {
|
|
|
|
switch (dir) {
|
|
|
|
switch (dir) {
|
|
|
@ -163,33 +170,33 @@
|
|
|
|
case "down": elev_state[elev] = DIR_DOWN;
|
|
|
|
case "down": elev_state[elev] = DIR_DOWN;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FLOOR events
|
|
|
|
// FLOOR events
|
|
|
|
for (var fl = 0; fl < floors.length; fl++) {
|
|
|
|
for (var fl = 0; fl < floors.length; fl++) {
|
|
|
|
var floor = floors[fl];
|
|
|
|
var floor = floors[fl];
|
|
|
|
|
|
|
|
|
|
|
|
// DOWN button pressed on a floor
|
|
|
|
// DOWN button pressed on a floor
|
|
|
|
floor.on("down_button_pressed", function() {
|
|
|
|
floor.on("down_button_pressed", function() {
|
|
|
|
// debug
|
|
|
|
// debug
|
|
|
|
console.log("Button DOWN pressed on a floor " + this.floorNum());
|
|
|
|
console.log("Button DOWN pressed on a floor " + this.floorNum());
|
|
|
|
|
|
|
|
|
|
|
|
sendElevator(this.floorNum(), DIR_DOWN);
|
|
|
|
sendElevator(this.floorNum(), DIR_DOWN);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// UP button pressed on a floor
|
|
|
|
// UP button pressed on a floor
|
|
|
|
floor.on("up_button_pressed", function() {
|
|
|
|
floor.on("up_button_pressed", function() {
|
|
|
|
// debug
|
|
|
|
// debug
|
|
|
|
console.log("Button UP pressed on a floor " + this.floorNum());
|
|
|
|
console.log("Button UP pressed on a floor " + this.floorNum());
|
|
|
|
|
|
|
|
|
|
|
|
sendElevator(this.floorNum(), DIR_UP);
|
|
|
|
sendElevator(this.floorNum(), DIR_UP);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
update: function(dt, elevators, floors) {
|
|
|
|
update: function(dt, elevators, floors) {
|
|
|
|
// We normally don't need to do anything here
|
|
|
|
// We normally don't need to do anything here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|