magwo / elevatorsaga

The elevator programming game!

Home Page:http://play.elevatorsaga.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling elevator.stop() triggers many incorrect "passing_floor" events

andresi opened this issue · comments

Using the following code:

{
    init: function(elevators, floors) {
        var elevator = elevators[0];
        elevator.goToFloor(2);
        elevator.on("passing_floor", function(floorNum, direction) {
            console.log("Passing floor "+floorNum);
            elevator.stop();
        });
    },
    update: function(dt, elevators, floors) {}
}

You would expect to see only Passing floor 1 printed, and then nothing else.
The actual result is sometimes it prints:

Passing floor 1
Passing floor 2
Passing floor 1

And sometimes even:

Passing floor 1
Passing floor 2
Passing floor 1
Passing floor 2
Passing floor 1

You can try this in challenge 1

Nice find, pretty serious bug. Will take a look.

It's kind of sporadic, but I managed to make a test that consistently fails.
https://github.com/magwo/elevatorsaga/blob/floorpassing/test/tests.js#L302

It seems that if you overshoot some certain "magic" amount or something, the bug manifests.

The code that generates the passing_floor events is not very clean. I'd like to make it more simple and obviously correct.

The reason it is complicated, I think, is because sometimes elevators overshoot slightly when stopping at a floor, causing it to pass the floor and then go back down. This is a tiny amount and that should not trigger the passing_floor event again, which complicates the code.

Might have to completely rewrite the code that generates these events.

Now rewritten and kind of fixed. You can still sometimes get multiple events for the same floor when stopping very close to the floor, due to slightly bouncy control system. But all the events should be expected and arguably correct.

I'm still seeing this behavior and it does not seem correct. When the elevator is moving from floor 2 to floor 0, on the first "passing_floor" (1) event I direct it to goToFloor(1). This results in several new "passing_floor" (0) events.

Floor 1 has not yet been passed, and floor 0 is not being passed (it is scheduled to be visited after the visit to floor 1).

I should add, the number of redundant passing_floor events is dependent on the timescale. At 1x, there are about 20 extraneous passing_floor(0) events. At 2x, there are about 9 events; at 55x there are 1 or 2.

You are correct. Will investigate.

Code to reproduce:

{
    init: function(elevators, floors) {
        var elevator = elevators[0]; // Let's use the first elevator

        // Whenever the elevator is idle (has no more queued destinations) ...
        elevator.on("idle", function() {
            // let's go to all the floors (or did we forget one?)
            elevator.goToFloor(2);
            elevator.goToFloor(0);
        });

        elevator.on("passing_floor", function(floorNum, direction) {
            console.log("passing floor", floorNum, direction);

            if(floorNum === 1 && direction === "down") {
                elevator.stop();
                elevator.goToFloor(1);
            }
        });
    },
        update: function(dt, elevators, floors) {
            // We normally don't need to do anything here
        }
}