magwo/elevatorsaga

Passengers do not enter into waiting elevators

Opened this issue · 3 comments

Vort commented

This problem is detected well with challenge # 14. Here we have 5 elevators.
At the beginning of the game, first and second elevators goes up, third, fourth and fifth stays on floor # 0.
Despite this, people at zero floor do not see 3 available elevators and continue to wait until more elevators becomes available.
Version: 1.6.5
Screenshot:
elevatorsaga_bug
Here is my solution.
It barely use events, maybe this scenario needs more support.

{
    init: function(elevators, floors) {
        for (var i = 0; i < elevators.length; i++) {
            elevators[i].index = i;
            elevators[i].intention = 1;
            elevators[i].moveTo = function(floor, floors) {
                for (var i = 0; i < floors.length; i++)
                {
                    if (floors[i].elUp == this.index)
                        floors[i].elUp = -1;
                    if (floors[i].elDown == this.index)
                        floors[i].elDown = -1;
                }
                if (this.goingUpIndicator())
                    floors[floor].elUp = this.index;
                if (this.goingDownIndicator())
                    floors[floor].elDown = this.index;
                this.destinationQueue = [floor];
                this.checkDestinationQueue();
            }
            elevators[i].elf = function(floors) {
                var curFloor = this.currentFloor();
                if (this.destinationQueue.length == 0)
                {
                    if (this.goingUpIndicator())
                    {
                        floors[curFloor].reqUp = false;
                        floors[curFloor].elUp = -1;
                    }
                    if (this.goingDownIndicator())
                    {
                        floors[curFloor].reqDown = false;
                        floors[curFloor].elDown = -1;
                    }
                }
                var nextFloor = floors.length;
                var prFloors = this.getPressedFloors();
                this.goingUpIndicator(true);
                this.goingDownIndicator(true);
                if (this.intention == 1)
                {
                    var availFloors = new Set();
                    if (this.loadFactor() < 0.5)
                        for (var i = curFloor; i < floors.length; i++)
                            if (floors[i].reqUp && ((floors[i].elUp == this.index) || (floors[i].elUp == -1)))
                                availFloors.add(i);
                    for (var i = 0; i < prFloors.length; i++)
                        if (prFloors[i] >= curFloor)
                            availFloors.add(prFloors[i]);
                    if (availFloors.size == 0)
                    {
                        for (var i = curFloor; i < floors.length; i++)
                            if (floors[i].reqDown && ((floors[i].elDown == this.index) || (floors[i].elDown == -1)))
                                availFloors.add(i);
                        if (availFloors.size != 0)
                            nextFloor = Math.max.apply(Math, [...availFloors]);
                    }
                    else
                        nextFloor = Math.min.apply(Math, [...availFloors]);
                    if (availFloors.size > 1)
                    {
                        this.goingUpIndicator(true);
                        this.goingDownIndicator(false);
                    }
                    if (nextFloor == floors.length)
                        this.intention = -1
                }
                if (this.intention == -1)
                {
                    nextFloor = -1;
                    var availFloors = new Set();
                    if (this.loadFactor() < 0.5)
                        for (var i = 0; i <= curFloor; i++)
                            if (floors[i].reqDown && ((floors[i].elDown == this.index) || (floors[i].elDown == -1)))
                                availFloors.add(i);
                    for (var i = 0; i < prFloors.length; i++)
                        if (prFloors[i] <= curFloor)
                            availFloors.add(prFloors[i]);
                    if (availFloors.size == 0)
                    {
                        for (var i = 0; i <= curFloor; i++)
                            if (floors[i].reqUp && ((floors[i].elUp == this.index) || (floors[i].elUp == -1)))
                                availFloors.add(i);
                        if (availFloors.size != 0)
                            nextFloor = Math.min.apply(Math, [...availFloors]);
                    }
                    else
                        nextFloor = Math.max.apply(Math, [...availFloors]);
                    if (availFloors.size > 1)
                    {
                        this.goingUpIndicator(false);
                        this.goingDownIndicator(true);
                    }
                    if (nextFloor == -1)
                        this.intention = 1;
                }
                if (nextFloor != -1)
                    this.moveTo(nextFloor, floors);
            }
        }
        for (var i = 0; i < floors.length; i++) {
            floors[i].reqUp = false;
            floors[i].reqDown = false;
            floors[i].elUp = -1;
            floors[i].elDown = -1;
            floors[i].on("up_button_pressed", function() {
                this.reqUp = true;
            });
            floors[i].on("down_button_pressed", function() {
                this.reqDown = true;
            });
        }
    },
    update: function(dt, elevators, floors) {
        for (var i = 0; i < elevators.length; i++)
            elevators[i].elf(floors);
    }
}

I've ran into the same problem. It seemed to happen when I started messing around with up/down indicators. My elevators in the middle which indicate both up and down won't accept passengers. In my code I set the indicators to false than true.

I found that I needed to set them both true in elevator.on("stopped_at_floor") . It appeared to be too late to do it in elevator.on("idle") after arriving at the floor.

a different workaround seems to be to goToFloor(elevator.currentFloor()) if you changed the indicators in the idle event.