Skip to content

Good Ai(cannot complete all levels)

00001H edited this page Mar 28, 2022 · 1 revision
{
    init: function(elevators, floors){
        var floorBtnPressed = [];
        for(var x=0;x<floors.length;x++){
            floorBtnPressed.push(false);
        }
        for(var es=0;es<elevators.length;es++){
            let elevator = elevators[es];
            elevator.on("floor_button_pressed", function(floorNum) { elevator.goToFloor(floorNum);} );
            elevator.on("passing_floor", function(floorNum, direction) {if(floorBtnPressed[floorNum]&&(elevator.loadFactor<0.6))elevator.stop();});
            elevator.onidle_ = function(){
                {
                    for(var qflor = 0;qflor<floorBtnPressed.length;qflor++){
                        if(floorBtnPressed[qflor]){
                            elevator.goToFloor(qflor);
                            floorBtnPressed[qflor]=false;
                            return;
                        }
                    }
                }
            }
            elevator.on("idle",elevator.onidle_);
        }
        for(var es=0;es<floors.length;es++){
            let floor = floors[es];
            floor._btnpress = function(){
                floorBtnPressed[floor.floorNum()]=true;
                for(var qpt=0;qpt<elevators.length;qpt++){
                    var elev = elevators[qpt];
                    if(elev.destinationQueue.length==0)elev.onidle_();
                }
            }
            floor.on("up_button_pressed",floor._btnpress);
            floor.on("down_button_pressed",floor._btnpress);
        }
    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
    }
}
Clone this wiki locally