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

void Junction::connect(const std::weak_ptr<Junction> &junctionA,
                       const std::weak_ptr<Junction> &junctionB,
12
13
14
                       std::string laneAToBName, std::string laneBToAName,
                       const double length, const double speedLimit,
                       const bool noPassing) {
Martin Kröning's avatar
Martin Kröning committed
15
16
  Lane laneAToB(laneAToBName, laneBToAName, length, speedLimit, noPassing,
                junctionB);
17
  Lane laneBToA(std::move(laneBToAName), std::move(laneAToBName), length, speedLimit, noPassing,
Martin Kröning's avatar
Martin Kröning committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
                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
  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,
57
                             std::string_view prohibitedLaneName) {
Martin Kröning's avatar
Martin Kröning committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  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));
}

82
Lane &Junction::randomOutboundLaneExcept(std::string_view laneName) {
Martin Kröning's avatar
Martin Kröning committed
83
84
85
86
87
  auto &randomLane = randomOutboundLane();
  return randomLane.getName() != laneName || outboundLanes.size() <= 1
             ? randomLane
             : randomOutboundLaneExcept(laneName);
}