Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrafficDirector: put correct drivers into special cars #755

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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