Skip to content

Commit

Permalink
TrafficDirector: put correct drivers into special cars
Browse files Browse the repository at this point in the history
Police cars are driven by cops.
Ambulances by medics.
Firetrucks by firefigters.

Signed-off-by: David Heidelberg <[email protected]>
  • Loading branch information
okias committed Dec 11, 2024
1 parent f10a5a8 commit 9a385be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
30 changes: 24 additions & 6 deletions rwengine/src/ai/TrafficDirector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include <algorithm>
#include <cmath>
#include <cstdint>

#include <glm/glm.hpp>
#include <glm/gtx/norm.hpp>
#include <glm/gtx/quaternion.hpp>
#include <glm/gtx/transform.hpp>
#include <string>

#include "ai/AIGraph.hpp"
#include "ai/AIGraphNode.hpp"
Expand Down Expand Up @@ -93,6 +93,19 @@ void TrafficDirector::setDensity(ai::NodeType type, float density) {
}
}

uint16_t TrafficDirector::assignDriver(const std::string &vehiclename) {
enum ped_types { COP = 1, MEDIC = 5, FIREMAN = 6 };
if (vehiclename == "POLICAR") {
return COP;
} else if (vehiclename == "AMBULAN") {
return MEDIC;
} else if (vehiclename == "FIRETRUK") {
return FIREMAN;
} else {
return 0;
}
}

std::vector<GameObject*> TrafficDirector::populateNearby(
const ViewCamera& camera, float radius, int maxSpawn) {

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

// Hardcoded cop Pedestrian
std::vector<uint16_t> peds = {1};
uint16_t pedId;

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

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

// Spawn a pedestrian and put it into the vehicle
const auto pedId =
peds.at(world->getRandomNumber(0u, peds.size() - 1));
CharacterObject* character = world->createPedestrian(pedId, vehicle->getPosition());
pedId = TrafficDirector::assignDriver(
vehicle->getVehicle()->vehiclename_);
if (pedId == 0) {
// not an special car, random person in
pedId = peds.at(world->getRandomNumber(0u, peds.size() - 1));
}
CharacterObject* character =
world->createPedestrian(pedId, vehicle->getPosition());
character->setLifetime(GameObject::TrafficLifetime);
character->setCurrentVehicle(vehicle, 0);
character->controller->setGoal(CharacterController::TrafficDriver);
Expand Down
9 changes: 8 additions & 1 deletion rwengine/src/ai/TrafficDirector.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef _RWENGINE_TRAFFICDIRECTOR_HPP_
#define _RWENGINE_TRAFFICDIRECTOR_HPP_

#include <vector>
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>

class GameWorld;
class GameObject;
Expand All @@ -24,6 +26,11 @@ class TrafficDirector {

void setDensity(NodeType type, float density);

/**
* Put the right pedestrian inside the car
*/
uint16_t assignDriver(const std::string &vehiclename);

/**
* Creates new traffic at available locations.
* @param camera The camera to spawn around
Expand Down

0 comments on commit 9a385be

Please sign in to comment.