Add Passengers

PHOTO EMBED

Sat Dec 11 2021 14:07:16 GMT+0000 (UTC)

Saved by @vincentleoooo #javascript

function addDynamicAgents() {
  if (Math.random() < probArrival) {
    // If the random generator decides to add passengers.
    let station = 2;
    // Attempts to find the next valid station.
    // Could have done a recursive function, I guess.
    let newState = getKeyByValue(positions, 2);
    if (newState == undefined) {
      newState = getKeyByValue(positions, 3);
      station = 3;
      if (newState == undefined) {
        newState = getKeyByValue(positions, 4);
        station = 4;
      }
    }
    
    // Queue state for the queueing lanes
    let queueState = newState + "Queue";
    
    // Finding which queue lane to choose
    let chosenQueue = 0;

    for (let i in objects[queueState]) {
      if (i !== "0") {
        if (
          Number(objects[queueState][i].stack) <
          Number(objects[queueState][chosenQueue].stack)
          // Not actually technically stacks, it is just for the joke below.
        ) {
          chosenQueue = Number(i);
        }
      }
    }

    let stackOverflow = 0; // This is the joke. I know it is lame.

    if (Number(objects[queueState][chosenQueue].stack) > 50) {
      stackOverflow = Number(objects[queueState][chosenQueue].stack) - 50;
    }

    let covid = false;

    if (Math.random() < probCovid) {
      covid = true;
    }

    let newPassenger = {
      id: 1,
      row: height / 2,
      col: 0,
      state: newState,
      queueState: queueState,
      chosenQueue: chosenQueue,
      targetRow: Number(objects[queueState][chosenQueue].row) + 3,
      targetCol: Number(objects[queueState][chosenQueue].col) - stackOverflow,
      station: station,
      timeWaited: 0,
      covid: covid,
      timeTaken: 0,
    };
    
    objects.passengers.push(newPassenger);
    let newStack = Number(objects[queueState][chosenQueue].stack) + 1;
    objects[queueState][chosenQueue].stack = newStack;
    enteredPassengers += 1;
  }
}
content_copyCOPY