Junction.cpp 2.96 KB
Newer Older
Martin Kröning's avatar
Martin Kröning committed
1 2 3 4 5 6 7
#include "Junction.hpp"
#include "Car.hpp"
#include "Vehicle.hpp"
#include <memory>
#include <random>
#include <typeinfo>
#include <utility>
8
class MapAppAPIWrapper;
Martin Kröning's avatar
Martin Kröning committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

void Junction::connect(const std::weak_ptr<Junction> &junctionA,
                       const std::weak_ptr<Junction> &junctionB,
                       const std::string &laneAToBName,
                       const std::string &laneBToAName, const double length,
                       const double speedLimit, const bool noPassing) {
  Lane laneAToB(laneAToBName, laneBToAName, length, speedLimit, noPassing,
                junctionB);
  Lane laneBToA(laneBToAName, laneAToBName, length, speedLimit, noPassing,
                junctionA);
  junctionA.lock()->outboundLanes.push_back(std::move(laneAToB));
  junctionB.lock()->outboundLanes.push_back(std::move(laneBToA));
}

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Junction::Junction(Junction &&other) noexcept = default;
Junction &Junction::operator=(Junction &&other) noexcept = default;

Junction::Junction(std::string name, const double fuelAmount)
    : Named(std::move(name)), fuelAmount(fuelAmount) {}

void Junction::processVehicles(const double time) {
  for (auto &&lane : outboundLanes) {
    lane.processVehicles(time);
  }
}

36
void Junction::drawVehicles(MapAppAPIWrapper &mapApp) const {
Martin Kröning's avatar
Martin Kröning committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  for (auto &&weg : outboundLanes) {
    weg.drawVehicles(mapApp);
  }
}

void Junction::createCar(std::string name, const double topSpeed,
                         const double fuelConsumption,
                         const double fuelCapacity, const double timeOfStart,
                         const double time) {
  randomOutboundLane().createCar(std::move(name), topSpeed, fuelConsumption,
                                 fuelCapacity, timeOfStart, time);
}

void Junction::createBicycle(std::string name, const double topSpeed,
                             const double timeOfStart, const double time) {
  randomOutboundLane().createBicycle(std::move(name), topSpeed, timeOfStart,
                                     time);
}

void Junction::acceptVehicle(std::unique_ptr<Vehicle> vehicle,
                             const std::string &prohibitedLaneName) {
  fillUp(*vehicle);
  vehicle->resetDistanceOnLane();
  randomOutboundLaneExcept(prohibitedLaneName)
      .acceptVehicle(std::move(vehicle));
}

std::mt19937 Junction::gen([]() {
  std::random_device rd;
  return rd();
}());

void Junction::fillUp(Vehicle &fahrzeug) {
  try {
    auto &car = dynamic_cast<Car &>(fahrzeug);
    fuelAmount -= car.refuel(fuelAmount);
  } catch (const std::bad_cast &e) {
  }
}

Lane &Junction::randomOutboundLane() {
  std::uniform_int_distribution<> dis(0, outboundLanes.size() - 1);
  return outboundLanes.at(dis(gen));
}

Lane &Junction::randomOutboundLaneExcept(const std::string &laneName) {
  auto &randomLane = randomOutboundLane();
  return randomLane.getName() != laneName || outboundLanes.size() <= 1
             ? randomLane
             : randomOutboundLaneExcept(laneName);
}