Skip to content

Instantly share code, notes, and snippets.

@nobonobo
Last active November 25, 2022 11:30
Show Gist options
  • Save nobonobo/e8b51c3163c27a5cc6589e868e2ec6b0 to your computer and use it in GitHub Desktop.
Save nobonobo/e8b51c3163c27a5cc6589e868e2ec6b0 to your computer and use it in GitHub Desktop.
{
init: (elevators, floors) => {
floors.time = 0;
floors.waiting = new Array(floors.length).fill(0);
floors.forEach((floor, n) => {
let floorNum = n
floors[floorNum].on("down_button_pressed", () => {
floors.waiting[floorNum]++;
});
floors[floorNum].on("up_button_pressed", () => {
floors.waiting[floorNum]++;
});
});
elevators.forEach((elevator, i)=>{
elevator.waittimes = new Array(floors.length).fill(0)
elevator.on("floor_button_pressed", (fn) => {
elevator.waittimes[fn] = floors.time;
})
elevator.on("stopped_at_floor", (fn)=>{
floors.waiting[fn] = 0;
elevator.waittimes[fn] = 0;
});
});
},
update: (dt, elevators, floors) => {
floors.time += dt;
elevators.forEach((elevator, i) => {
if(elevator.destinationQueue.length>0) return;
console.log(floors.time);
let targets = new Array(floors.length).fill(0);
let pressed = elevator.getPressedFloors();
let goto = new Array(floors.length).fill(0);
let factor1 = elevator.maxPassengerCount()*elevator.loadFactor();
let factor2 = elevator.maxPassengerCount()*(1-elevator.loadFactor());
pressed.forEach(n=>goto[n]++);
let num = floors.length;
pressed.forEach(n=>targets[n]+=factor1);
elevator.waittimes.forEach((t,idx) => {
if (t>0) {
targets[idx] += (floors.time-t)/5;
}
});
let waiting2 = [...floors.waiting];
elevators.forEach(e=>{
e.destinationQueue.forEach(n=>{
if(waiting2[n]>0 && e.loadFactor()<0.5) waiting2[n]--;
})
})
waiting2.forEach((n, idx)=>targets[idx]+=n*factor2);
targets.forEach((n, idx) => {
targets[idx] += num/(num+Math.abs(n - elevator.currentFloor()));
})
var next = -1;
var max = -1;
targets.forEach((n,idx)=>{
if (max<n) {
max=n
next = idx
}
});
if (next>=0) {
elevator.goToFloor(next);
console.log(i+":", floors.waiting, goto, targets, next);
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment