Skip to content

Commit 9a385be

Browse files
committed
TrafficDirector: put correct drivers into special cars
Police cars are driven by cops. Ambulances by medics. Firetrucks by firefigters. Signed-off-by: David Heidelberg <[email protected]>
1 parent f10a5a8 commit 9a385be

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

rwengine/src/ai/TrafficDirector.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#include <algorithm>
44
#include <cmath>
55
#include <cstdint>
6-
76
#include <glm/glm.hpp>
87
#include <glm/gtx/norm.hpp>
98
#include <glm/gtx/quaternion.hpp>
109
#include <glm/gtx/transform.hpp>
10+
#include <string>
1111

1212
#include "ai/AIGraph.hpp"
1313
#include "ai/AIGraphNode.hpp"
@@ -93,6 +93,19 @@ void TrafficDirector::setDensity(ai::NodeType type, float density) {
9393
}
9494
}
9595

96+
uint16_t TrafficDirector::assignDriver(const std::string &vehiclename) {
97+
enum ped_types { COP = 1, MEDIC = 5, FIREMAN = 6 };
98+
if (vehiclename == "POLICAR") {
99+
return COP;
100+
} else if (vehiclename == "AMBULAN") {
101+
return MEDIC;
102+
} else if (vehiclename == "FIRETRUK") {
103+
return FIREMAN;
104+
} else {
105+
return 0;
106+
}
107+
}
108+
96109
std::vector<GameObject*> TrafficDirector::populateNearby(
97110
const ViewCamera& camera, float radius, int maxSpawn) {
98111

@@ -133,6 +146,7 @@ std::vector<GameObject*> TrafficDirector::populateNearby(
133146

134147
// Hardcoded cop Pedestrian
135148
std::vector<uint16_t> peds = {1};
149+
uint16_t pedId;
136150

137151
// Determine which zone the viewpoint is in
138152
auto zone = world->data->findZoneAt(camera.position);
@@ -170,8 +184,7 @@ std::vector<GameObject*> TrafficDirector::populateNearby(
170184
counter--;
171185

172186
// Spawn a pedestrian from the available pool
173-
const auto pedId =
174-
peds.at(world->getRandomNumber(0u, peds.size() - 1));
187+
pedId = peds.at(world->getRandomNumber(0u, peds.size() - 1));
175188
auto ped = world->createPedestrian(pedId, spawn->position);
176189
ped->applyOffset();
177190
ped->setLifetime(GameObject::TrafficLifetime);
@@ -244,9 +257,14 @@ std::vector<GameObject*> TrafficDirector::populateNearby(
244257
vehicle->setHandbraking(false);
245258

246259
// Spawn a pedestrian and put it into the vehicle
247-
const auto pedId =
248-
peds.at(world->getRandomNumber(0u, peds.size() - 1));
249-
CharacterObject* character = world->createPedestrian(pedId, vehicle->getPosition());
260+
pedId = TrafficDirector::assignDriver(
261+
vehicle->getVehicle()->vehiclename_);
262+
if (pedId == 0) {
263+
// not an special car, random person in
264+
pedId = peds.at(world->getRandomNumber(0u, peds.size() - 1));
265+
}
266+
CharacterObject* character =
267+
world->createPedestrian(pedId, vehicle->getPosition());
250268
character->setLifetime(GameObject::TrafficLifetime);
251269
character->setCurrentVehicle(vehicle, 0);
252270
character->controller->setGoal(CharacterController::TrafficDriver);

rwengine/src/ai/TrafficDirector.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#ifndef _RWENGINE_TRAFFICDIRECTOR_HPP_
22
#define _RWENGINE_TRAFFICDIRECTOR_HPP_
33

4-
#include <vector>
54
#include <cstddef>
5+
#include <cstdint>
6+
#include <string>
7+
#include <vector>
68

79
class GameWorld;
810
class GameObject;
@@ -24,6 +26,11 @@ class TrafficDirector {
2426

2527
void setDensity(NodeType type, float density);
2628

29+
/**
30+
* Put the right pedestrian inside the car
31+
*/
32+
uint16_t assignDriver(const std::string &vehiclename);
33+
2734
/**
2835
* Creates new traffic at available locations.
2936
* @param camera The camera to spawn around

0 commit comments

Comments
 (0)