1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 11:22:45 +01:00

clang-format files in rwengine/src/ai

This commit is contained in:
Daniel Evans 2016-09-09 21:13:18 +01:00
parent 9aa3af6703
commit d5e853d23f
11 changed files with 1018 additions and 1051 deletions

View File

@ -1,116 +1,116 @@
#include "ai/AIGraph.hpp"
#include <objects/GameObject.hpp>
#include <ai/AIGraphNode.hpp>
#include <glm/gtx/norm.hpp>
#include <iostream>
#include <objects/GameObject.hpp>
AIGraph::~AIGraph()
{
for( auto n : nodes ) {
delete n;
}
AIGraph::~AIGraph() {
for (auto n : nodes) {
delete n;
}
}
void AIGraph::createPathNodes(const glm::vec3& position, const glm::quat& rotation, PathData& path)
{
size_t startIndex = nodes.size();
std::vector<AIGraphNode*> pathNodes;
pathNodes.reserve(path.nodes.size());
void AIGraph::createPathNodes(const glm::vec3& position,
const glm::quat& rotation, PathData& path) {
size_t startIndex = nodes.size();
std::vector<AIGraphNode*> pathNodes;
pathNodes.reserve(path.nodes.size());
for( size_t n = 0; n < path.nodes.size(); ++n ) {
auto& node = path.nodes[n];
AIGraphNode* ainode = nullptr;
glm::vec3 nodePosition = position + (rotation * node.position);
for (size_t n = 0; n < path.nodes.size(); ++n) {
auto& node = path.nodes[n];
AIGraphNode* ainode = nullptr;
glm::vec3 nodePosition = position + (rotation * node.position);
if( node.type == PathNode::EXTERNAL ) {
for( size_t rn = 0; rn < externalNodes.size(); ++rn ) {
auto& realNode = externalNodes[rn];
auto d = glm::distance2(realNode->position, nodePosition);
if( d < 1.f ) {
pathNodes.push_back(realNode);
ainode = realNode;
break;
}
}
}
if( ainode == nullptr ) {
ainode = new AIGraphNode;
ainode->type = (path.type == PathData::PATH_PED ? AIGraphNode::Pedestrian : AIGraphNode::Vehicle);
ainode->nextIndex = node.next >= 0 ? startIndex + node.next : -1;
ainode->flags = AIGraphNode::None;
ainode->size = node.size;
ainode->other_thing = node.other_thing;
ainode->other_thing2 = node.other_thing2;
ainode->position = nodePosition;
ainode->external = node.type == PathNode::EXTERNAL;
ainode->disabled = false;
if (node.type == PathNode::EXTERNAL) {
for (size_t rn = 0; rn < externalNodes.size(); ++rn) {
auto& realNode = externalNodes[rn];
auto d = glm::distance2(realNode->position, nodePosition);
if (d < 1.f) {
pathNodes.push_back(realNode);
ainode = realNode;
break;
}
}
}
pathNodes.push_back(ainode);
nodes.push_back(ainode);
if (ainode == nullptr) {
ainode = new AIGraphNode;
ainode->type =
(path.type == PathData::PATH_PED ? AIGraphNode::Pedestrian
: AIGraphNode::Vehicle);
ainode->nextIndex = node.next >= 0 ? startIndex + node.next : -1;
ainode->flags = AIGraphNode::None;
ainode->size = node.size;
ainode->other_thing = node.other_thing;
ainode->other_thing2 = node.other_thing2;
ainode->position = nodePosition;
ainode->external = node.type == PathNode::EXTERNAL;
ainode->disabled = false;
if( ainode->external )
{
externalNodes.push_back(ainode);
// Determine which grid cell this node falls into
float lowerCoord = -(WORLD_GRID_SIZE)/2.f;
auto gridrel = glm::vec2(ainode->position) - glm::vec2(lowerCoord, lowerCoord);
auto gridcoord = glm::floor(gridrel / glm::vec2(WORLD_CELL_SIZE));
if( gridcoord.x < 0 || gridcoord.y < 0 || gridcoord.x >= WORLD_GRID_WIDTH || gridcoord.y >= WORLD_GRID_WIDTH )
{
std::cout << "Warning: Node outside of grid at coord " << gridcoord.x << " " << gridcoord.y << std::endl;
}
auto index = (gridcoord.x * WORLD_GRID_WIDTH) + gridcoord.y;
gridNodes[index].push_back(ainode);
}
}
}
pathNodes.push_back(ainode);
nodes.push_back(ainode);
for(size_t pn = 0; pn < path.nodes.size(); ++pn) {
if(path.nodes[pn].next >= 0 && (unsigned) path.nodes[pn].next < pathNodes.size()) {
auto node = pathNodes[pn];
auto next = pathNodes[path.nodes[pn].next];
node->connections.push_back(next);
next->connections.push_back(node);
}
}
if (ainode->external) {
externalNodes.push_back(ainode);
// Determine which grid cell this node falls into
float lowerCoord = -(WORLD_GRID_SIZE) / 2.f;
auto gridrel = glm::vec2(ainode->position) -
glm::vec2(lowerCoord, lowerCoord);
auto gridcoord =
glm::floor(gridrel / glm::vec2(WORLD_CELL_SIZE));
if (gridcoord.x < 0 || gridcoord.y < 0 ||
gridcoord.x >= WORLD_GRID_WIDTH ||
gridcoord.y >= WORLD_GRID_WIDTH) {
std::cout << "Warning: Node outside of grid at coord "
<< gridcoord.x << " " << gridcoord.y << std::endl;
}
auto index = (gridcoord.x * WORLD_GRID_WIDTH) + gridcoord.y;
gridNodes[index].push_back(ainode);
}
}
}
for (size_t pn = 0; pn < path.nodes.size(); ++pn) {
if (path.nodes[pn].next >= 0 &&
(unsigned)path.nodes[pn].next < pathNodes.size()) {
auto node = pathNodes[pn];
auto next = pathNodes[path.nodes[pn].next];
node->connections.push_back(next);
next->connections.push_back(node);
}
}
}
glm::ivec2 worldToGrid(const glm::vec2& world)
{
static const float lowerCoord = -(WORLD_GRID_SIZE)/2.f;
return glm::ivec2((world - glm::vec2(lowerCoord)) / glm::vec2(WORLD_CELL_SIZE));
glm::ivec2 worldToGrid(const glm::vec2& world) {
static const float lowerCoord = -(WORLD_GRID_SIZE) / 2.f;
return glm::ivec2((world - glm::vec2(lowerCoord)) /
glm::vec2(WORLD_CELL_SIZE));
}
void AIGraph::gatherExternalNodesNear(const glm::vec3& center, const float radius, std::vector< AIGraphNode* >& nodes)
{
// the bounds end up covering more than might fit
auto planecoords = glm::vec2(center);
auto minWorld = planecoords - glm::vec2(radius);
auto maxWorld = planecoords + glm::vec2(radius);
auto minGrid = worldToGrid(minWorld);
auto maxGrid = worldToGrid(maxWorld);
void AIGraph::gatherExternalNodesNear(const glm::vec3& center,
const float radius,
std::vector<AIGraphNode*>& nodes) {
// the bounds end up covering more than might fit
auto planecoords = glm::vec2(center);
auto minWorld = planecoords - glm::vec2(radius);
auto maxWorld = planecoords + glm::vec2(radius);
auto minGrid = worldToGrid(minWorld);
auto maxGrid = worldToGrid(maxWorld);
for( int x = minGrid.x; x <= maxGrid.x; ++x )
{
for( int y = minGrid.y; y <= maxGrid.y; ++y )
{
int i = (x * WORLD_GRID_WIDTH) + y;
if( i < 0 || i >= (int)gridNodes.size() )
{
continue;
}
auto& external = gridNodes[i];
for ( AIGraphNode* node : external )
{
if ( glm::distance2( center, node->position ) < radius*radius )
{
nodes.push_back( node );
}
}
}
}
for (int x = minGrid.x; x <= maxGrid.x; ++x) {
for (int y = minGrid.y; y <= maxGrid.y; ++y) {
int i = (x * WORLD_GRID_WIDTH) + y;
if (i < 0 || i >= (int)gridNodes.size()) {
continue;
}
auto& external = gridNodes[i];
for (AIGraphNode* node : external) {
if (glm::distance2(center, node->position) < radius * radius) {
nodes.push_back(node);
}
}
}
}
}

View File

@ -1,37 +1,37 @@
#pragma once
#ifndef _AIGRAPH_HPP_
#define _AIGRAPH_HPP_
#include <vector>
#include <glm/gtc/quaternion.hpp>
#include <data/PathData.hpp>
#include <array>
#include <data/PathData.hpp>
#include <glm/gtc/quaternion.hpp>
#include <rw/types.hpp>
#include <vector>
struct AIGraphNode;
class AIGraph
{
class AIGraph {
public:
~AIGraph();
~AIGraph();
std::vector<AIGraphNode*> nodes;
std::vector<AIGraphNode*> nodes;
/**
* List of external nodes, which are links between each
* Instance's paths and where new pedestrians and vehicles
* are spawned
*/
std::vector<AIGraphNode*> externalNodes;
/**
* List of external nodes, which are links between each
* Instance's paths and where new pedestrians and vehicles
* are spawned
*/
std::vector<AIGraphNode*> externalNodes;
/**
* Stores the external AI Grid Nodes organised by world grid cell
*/
std::array<std::vector<AIGraphNode*>, WORLD_GRID_CELLS> gridNodes;
/**
* Stores the external AI Grid Nodes organised by world grid cell
*/
std::array<std::vector<AIGraphNode*>,WORLD_GRID_CELLS> gridNodes;
void createPathNodes(const glm::vec3& position, const glm::quat& rotation,
PathData& path);
void createPathNodes(const glm::vec3& position, const glm::quat& rotation, PathData& path);
void gatherExternalNodesNear(const glm::vec3& center, const float radius, std::vector<AIGraphNode*>& nodes);
void gatherExternalNodesNear(const glm::vec3& center, const float radius,
std::vector<AIGraphNode*>& nodes);
};
#endif

View File

@ -1,35 +1,32 @@
#pragma once
#ifndef _AIGRAPHNODE_HPP_
#define _AIGRAPHNODE_HPP_
#include <glm/glm.hpp>
#include <cstdint>
#include <glm/glm.hpp>
#include <vector>
struct AIGraphNode
{
enum NodeType {
Vehicle,
Pedestrian
};
struct AIGraphNode {
enum NodeType { Vehicle, Pedestrian };
enum {
None = 0,
CrossesRoad = 1 /// No documentation for other flags yet, but this is mentioned.
CrossesRoad =
1 /// No documentation for other flags yet, but this is mentioned.
};
NodeType type;
glm::vec3 position;
float size;
int other_thing;
int other_thing2;
bool external;
uint8_t flags;
float size;
int other_thing;
int other_thing2;
bool external;
uint8_t flags;
int32_t nextIndex;
bool disabled;
std::vector<AIGraphNode*> connections;
bool disabled;
std::vector<AIGraphNode*> connections;
};
#endif

View File

@ -1,7 +1,7 @@
#include <btBulletDynamicsCommon.h>
#include <ai/CharacterController.hpp>
#include <objects/CharacterObject.hpp>
#include <objects/VehicleObject.hpp>
#include <btBulletDynamicsCommon.h>
#include <data/Model.hpp>
#include <engine/Animator.hpp>
@ -10,504 +10,477 @@
constexpr float kCloseDoorIdleTime = 2.f;
CharacterController::CharacterController(CharacterObject* character)
: character(character)
, _currentActivity(nullptr)
, _nextActivity(nullptr)
, m_closeDoorTimer(0.f)
, currentGoal(None)
, leader(nullptr)
, targetNode(nullptr)
{
character->controller = this;
CharacterController::CharacterController(CharacterObject *character)
: character(character)
, _currentActivity(nullptr)
, _nextActivity(nullptr)
, m_closeDoorTimer(0.f)
, currentGoal(None)
, leader(nullptr)
, targetNode(nullptr) {
character->controller = this;
}
bool CharacterController::updateActivity()
{
if( _currentActivity && character->isAlive() ) {
return _currentActivity->update(character, this);
}
bool CharacterController::updateActivity() {
if (_currentActivity && character->isAlive()) {
return _currentActivity->update(character, this);
}
return false;
return false;
}
void CharacterController::setActivity(CharacterController::Activity* activity)
{
if( _currentActivity ) delete _currentActivity;
_currentActivity = activity;
void CharacterController::setActivity(CharacterController::Activity *activity) {
if (_currentActivity) delete _currentActivity;
_currentActivity = activity;
}
void CharacterController::skipActivity()
{
// Some activities can't be cancelled, such as the final phase of entering a vehicle
// or jumping.
if (getCurrentActivity() != nullptr &&
getCurrentActivity()->canSkip(character, this))
setActivity(nullptr);
void CharacterController::skipActivity() {
// Some activities can't be cancelled, such as the final phase of entering a
// vehicle
// or jumping.
if (getCurrentActivity() != nullptr &&
getCurrentActivity()->canSkip(character, this))
setActivity(nullptr);
}
void CharacterController::setNextActivity(CharacterController::Activity* activity)
{
if( _currentActivity == nullptr ) {
setActivity(activity);
_nextActivity = nullptr;
}
else {
if(_nextActivity) delete _nextActivity;
_nextActivity = activity;
}
void CharacterController::setNextActivity(
CharacterController::Activity *activity) {
if (_currentActivity == nullptr) {
setActivity(activity);
_nextActivity = nullptr;
} else {
if (_nextActivity) delete _nextActivity;
_nextActivity = activity;
}
}
bool CharacterController::isCurrentActivity(const std::string& activity) const
{
if (getCurrentActivity() == nullptr) return false;
return getCurrentActivity()->name() == activity;
bool CharacterController::isCurrentActivity(const std::string &activity) const {
if (getCurrentActivity() == nullptr) return false;
return getCurrentActivity()->name() == activity;
}
void CharacterController::update(float dt)
{
if( character->getCurrentVehicle() ) {
// Nevermind, the player is in a vehicle.
void CharacterController::update(float dt) {
if (character->getCurrentVehicle()) {
// Nevermind, the player is in a vehicle.
auto& d = character->getMovement();
auto &d = character->getMovement();
if( character->getCurrentSeat() == 0 )
{
character->getCurrentVehicle()->setSteeringAngle(d.y);
if( std::abs(d.x) > 0.01f )
{
character->getCurrentVehicle()->setHandbraking(false);
}
character->getCurrentVehicle()->setThrottle(d.x);
}
if (character->getCurrentSeat() == 0) {
character->getCurrentVehicle()->setSteeringAngle(d.y);
if( _currentActivity == nullptr ) {
// If character is idle in vehicle, try to close the door.
auto v = character->getCurrentVehicle();
auto entryDoor = v->getSeatEntryDoor(character->getCurrentSeat());
if (std::abs(d.x) > 0.01f) {
character->getCurrentVehicle()->setHandbraking(false);
}
character->getCurrentVehicle()->setThrottle(d.x);
}
if (entryDoor && entryDoor->constraint) {
if (glm::length( d ) <= 0.1f) {
if (m_closeDoorTimer >= kCloseDoorIdleTime) {
character->getCurrentVehicle()->setPartTarget(entryDoor, true, entryDoor->closedAngle);
}
m_closeDoorTimer += dt;
}
else {
m_closeDoorTimer = 0.f;
}
}
}
}
else
{
m_closeDoorTimer = 0.f;
}
if (_currentActivity == nullptr) {
// If character is idle in vehicle, try to close the door.
auto v = character->getCurrentVehicle();
auto entryDoor = v->getSeatEntryDoor(character->getCurrentSeat());
if( updateActivity() ) {
character->activityFinished();
if( _currentActivity ) {
delete _currentActivity;
_currentActivity = nullptr;
}
if( _nextActivity ) {
setActivity( _nextActivity );
_nextActivity = nullptr;
}
}
if (entryDoor && entryDoor->constraint) {
if (glm::length(d) <= 0.1f) {
if (m_closeDoorTimer >= kCloseDoorIdleTime) {
character->getCurrentVehicle()->setPartTarget(
entryDoor, true, entryDoor->closedAngle);
}
m_closeDoorTimer += dt;
} else {
m_closeDoorTimer = 0.f;
}
}
}
} else {
m_closeDoorTimer = 0.f;
}
if (updateActivity()) {
character->activityFinished();
if (_currentActivity) {
delete _currentActivity;
_currentActivity = nullptr;
}
if (_nextActivity) {
setActivity(_nextActivity);
_nextActivity = nullptr;
}
}
}
CharacterObject *CharacterController::getCharacter() const
{
return character;
CharacterObject *CharacterController::getCharacter() const {
return character;
}
void CharacterController::setMoveDirection(const glm::vec3 &movement)
{
character->setMovement(movement);
void CharacterController::setMoveDirection(const glm::vec3 &movement) {
character->setMovement(movement);
}
void CharacterController::setLookDirection(const glm::vec2 &look)
{
character->setLook(look);
void CharacterController::setLookDirection(const glm::vec2 &look) {
character->setLook(look);
}
void CharacterController::setRunning(bool run)
{
character->setRunning(run);
void CharacterController::setRunning(bool run) {
character->setRunning(run);
}
bool Activities::GoTo::update(CharacterObject *character,
CharacterController *controller) {
/* TODO: Use the ai nodes to navigate to the position */
auto cpos = character->getPosition();
glm::vec3 targetDirection = target - cpos;
bool Activities::GoTo::update(CharacterObject *character, CharacterController *controller)
{
/* TODO: Use the ai nodes to navigate to the position */
auto cpos = character->getPosition();
glm::vec3 targetDirection = target - cpos;
// Ignore vertical axis for the sake of simplicity.
if (glm::length(glm::vec2(targetDirection)) < 0.1f) {
character->setPosition(glm::vec3(glm::vec2(target), cpos.z));
controller->setMoveDirection({0.f, 0.f, 0.f});
character->controller->setRunning(false);
return true;
}
// Ignore vertical axis for the sake of simplicity.
if( glm::length(glm::vec2(targetDirection)) < 0.1f ) {
character->setPosition(glm::vec3(glm::vec2(target), cpos.z));
controller->setMoveDirection({0.f, 0.f, 0.f});
character->controller->setRunning(false);
return true;
}
float hdg =
atan2(targetDirection.y, targetDirection.x) - glm::half_pi<float>();
character->setHeading(glm::degrees(hdg));
float hdg = atan2(targetDirection.y, targetDirection.x) - glm::half_pi<float>();
character->setHeading(glm::degrees(hdg));
controller->setMoveDirection({1.f, 0.f, 0.f});
controller->setRunning(sprint);
controller->setMoveDirection({1.f, 0.f, 0.f});
controller->setRunning(sprint);
return false;
return false;
}
bool Activities::Jump::update(CharacterObject* character, CharacterController* controller)
{
RW_UNUSED(controller);
if (character->physCharacter == nullptr) return true;
bool Activities::Jump::update(CharacterObject *character,
CharacterController *controller) {
RW_UNUSED(controller);
if (character->physCharacter == nullptr) return true;
if( !jumped )
{
character->jump();
jumped = true;
}
else
{
if (character->physCharacter->canJump()) {
return true;
}
}
return false;
if (!jumped) {
character->jump();
jumped = true;
} else {
if (character->physCharacter->canJump()) {
return true;
}
}
return false;
}
bool Activities::EnterVehicle::canSkip(CharacterObject *character, CharacterController *) const
{
// If we're already inside the vehicle, it can't helped.
return character->getCurrentVehicle() == nullptr;
bool Activities::EnterVehicle::canSkip(CharacterObject *character,
CharacterController *) const {
// If we're already inside the vehicle, it can't helped.
return character->getCurrentVehicle() == nullptr;
}
bool Activities::EnterVehicle::update(CharacterObject *character, CharacterController *controller)
{
constexpr float kSprintToEnterDistance = 5.f;
constexpr float kGiveUpDistance = 100.f;
bool Activities::EnterVehicle::update(CharacterObject *character,
CharacterController *controller) {
constexpr float kSprintToEnterDistance = 5.f;
constexpr float kGiveUpDistance = 100.f;
RW_UNUSED(controller);
RW_UNUSED(controller);
// Boats don't have any kind of entry animation unless you're onboard.
if( vehicle->vehicle->type == VehicleData::BOAT ) {
character->enterVehicle(vehicle, seat);
return true;
}
if( seat == ANY_SEAT )
{
// Determine which seat to take.
float nearest = std::numeric_limits<float>::max();
for(unsigned int s = 1; s < vehicle->info->seats.size(); ++s)
{
auto entry = vehicle->getSeatEntryPositionWorld(s);
float dist = glm::distance(entry, character->getPosition());
if( dist < nearest )
{
seat = s;
nearest = dist;
}
}
}
auto entryDoor = vehicle->getSeatEntryDoor(seat);
auto entryPos = vehicle->getSeatEntryPositionWorld(seat);
auto entryPosLocal = vehicle->getSeatEntryPosition(seat);
// Boats don't have any kind of entry animation unless you're onboard.
if (vehicle->vehicle->type == VehicleData::BOAT) {
character->enterVehicle(vehicle, seat);
return true;
}
auto anm_open = character->animations.car_open_lhs;
auto anm_enter = character->animations.car_getin_lhs;
auto anm_pullout = character->animations.car_pullout_lhs;
if (seat == ANY_SEAT) {
// Determine which seat to take.
float nearest = std::numeric_limits<float>::max();
for (unsigned int s = 1; s < vehicle->info->seats.size(); ++s) {
auto entry = vehicle->getSeatEntryPositionWorld(s);
float dist = glm::distance(entry, character->getPosition());
if (dist < nearest) {
seat = s;
nearest = dist;
}
}
}
if ( entryPosLocal.x > 0.f )
{
anm_open = character->animations.car_open_rhs;
anm_enter = character->animations.car_getin_rhs;
anm_pullout = character->animations.car_pullout_rhs;
}
auto entryDoor = vehicle->getSeatEntryDoor(seat);
auto entryPos = vehicle->getSeatEntryPositionWorld(seat);
auto entryPosLocal = vehicle->getSeatEntryPosition(seat);
// If there's someone in this seat already, we may have to ask them to leave.
auto currentOccupant= static_cast<CharacterObject*>(vehicle->getOccupant(seat));
auto anm_open = character->animations.car_open_lhs;
auto anm_enter = character->animations.car_getin_lhs;
auto anm_pullout = character->animations.car_pullout_lhs;
bool tryToEnter = false;
if( entering ) {
if( character->animator->getAnimation(AnimIndexAction) == anm_open ) {
if( character->animator->isCompleted(AnimIndexAction) ) {
tryToEnter = true;
}
else if( entryDoor && character->animator->getAnimationTime(AnimIndexAction) >= 0.5f )
{
vehicle->setPartTarget(entryDoor, true, entryDoor->openAngle);
}
else {
//character->setPosition(vehicle->getSeatEntryPosition(seat));
character->rotation = vehicle->getRotation();
}
}
else if (character->animator->getAnimation(AnimIndexAction) == anm_pullout) {
if (character->animator->isCompleted(AnimIndexAction)) {
tryToEnter = true;
}
}
else if( character->animator->getAnimation(AnimIndexAction) == anm_enter ) {
if( character->animator->isCompleted(AnimIndexAction) ) {
// VehicleGetIn is over, finish activity
return true;
}
}
}
else {
glm::vec3 targetDirection = entryPos - character->getPosition();
targetDirection.z = 0.f;
if (entryPosLocal.x > 0.f) {
anm_open = character->animations.car_open_rhs;
anm_enter = character->animations.car_getin_rhs;
anm_pullout = character->animations.car_pullout_rhs;
}
float targetDistance = glm::length(targetDirection);
// If there's someone in this seat already, we may have to ask them to
// leave.
auto currentOccupant =
static_cast<CharacterObject *>(vehicle->getOccupant(seat));
if( targetDistance <= 0.4f ) {
entering = true;
// Warp character to vehicle orientation
character->controller->setMoveDirection({0.f, 0.f, 0.f});
character->controller->setRunning(false);
character->setHeading(
glm::degrees(glm::roll(vehicle->getRotation())));
// Determine if the door open animation should be skipped.
if( entryDoor == nullptr || (entryDoor->constraint != nullptr && glm::abs(entryDoor->constraint->getHingeAngle()) >= 0.6f ) )
{
tryToEnter = true;
}
else
{
character->playActivityAnimation(anm_open, false, true);
}
}
else if (targetDistance > kGiveUpDistance) {
return true;
}
else {
if( targetDistance > kSprintToEnterDistance ) {
character->controller->setRunning(true);
}
character->setHeading(
glm::degrees(atan2(targetDirection.y, targetDirection.x) - glm::half_pi<float>()));
character->controller->setMoveDirection({1.f, 0.f, 0.f});
}
}
bool tryToEnter = false;
if (tryToEnter) {
if (currentOccupant != nullptr && currentOccupant != character) {
// Play the pullout animation and tell the other character to get out.
character->playActivityAnimation(anm_pullout, false, true);
currentOccupant->controller->setNextActivity(new Activities::ExitVehicle(true));
}
else {
character->playActivityAnimation(anm_enter, false, true);
character->enterVehicle(vehicle, seat);
}
}
return false;
if (entering) {
if (character->animator->getAnimation(AnimIndexAction) == anm_open) {
if (character->animator->isCompleted(AnimIndexAction)) {
tryToEnter = true;
} else if (entryDoor &&
character->animator->getAnimationTime(AnimIndexAction) >=
0.5f) {
vehicle->setPartTarget(entryDoor, true, entryDoor->openAngle);
} else {
// character->setPosition(vehicle->getSeatEntryPosition(seat));
character->rotation = vehicle->getRotation();
}
} else if (character->animator->getAnimation(AnimIndexAction) ==
anm_pullout) {
if (character->animator->isCompleted(AnimIndexAction)) {
tryToEnter = true;
}
} else if (character->animator->getAnimation(AnimIndexAction) ==
anm_enter) {
if (character->animator->isCompleted(AnimIndexAction)) {
// VehicleGetIn is over, finish activity
return true;
}
}
} else {
glm::vec3 targetDirection = entryPos - character->getPosition();
targetDirection.z = 0.f;
float targetDistance = glm::length(targetDirection);
if (targetDistance <= 0.4f) {
entering = true;
// Warp character to vehicle orientation
character->controller->setMoveDirection({0.f, 0.f, 0.f});
character->controller->setRunning(false);
character->setHeading(
glm::degrees(glm::roll(vehicle->getRotation())));
// Determine if the door open animation should be skipped.
if (entryDoor == nullptr ||
(entryDoor->constraint != nullptr &&
glm::abs(entryDoor->constraint->getHingeAngle()) >= 0.6f)) {
tryToEnter = true;
} else {
character->playActivityAnimation(anm_open, false, true);
}
} else if (targetDistance > kGiveUpDistance) {
return true;
} else {
if (targetDistance > kSprintToEnterDistance) {
character->controller->setRunning(true);
}
character->setHeading(
glm::degrees(atan2(targetDirection.y, targetDirection.x) -
glm::half_pi<float>()));
character->controller->setMoveDirection({1.f, 0.f, 0.f});
}
}
if (tryToEnter) {
if (currentOccupant != nullptr && currentOccupant != character) {
// Play the pullout animation and tell the other character to get
// out.
character->playActivityAnimation(anm_pullout, false, true);
currentOccupant->controller->setNextActivity(
new Activities::ExitVehicle(true));
} else {
character->playActivityAnimation(anm_enter, false, true);
character->enterVehicle(vehicle, seat);
}
}
return false;
}
bool Activities::ExitVehicle::update(CharacterObject *character,
CharacterController *controller) {
RW_UNUSED(controller);
bool Activities::ExitVehicle::update(CharacterObject *character, CharacterController *controller)
{
RW_UNUSED(controller);
if (jacked) {
auto anm_jacked_lhs = character->animations.car_jacked_lhs;
auto anm_jacked_rhs = character->animations.car_jacked_lhs;
auto anm_current = character->animator->getAnimation(AnimIndexAction);
if (jacked) {
auto anm_jacked_lhs = character->animations.car_jacked_lhs;
auto anm_jacked_rhs = character->animations.car_jacked_lhs;
auto anm_current = character->animator->getAnimation(AnimIndexAction);
if (anm_current == anm_jacked_lhs || anm_current == anm_jacked_rhs) {
if (character->animator->isCompleted(AnimIndexAction)) {
return true;
}
} else {
if (character->getCurrentVehicle() == nullptr) return true;
if (anm_current == anm_jacked_lhs || anm_current == anm_jacked_rhs) {
if (character->animator->isCompleted(AnimIndexAction)) {
return true;
}
}
else {
if (character->getCurrentVehicle() == nullptr) return true;
auto vehicle = character->getCurrentVehicle();
auto seat = character->getCurrentSeat();
auto door = vehicle->getSeatEntryDoor(seat);
auto exitPos = vehicle->getSeatEntryPositionWorld(seat);
auto exitPosLocal = vehicle->getSeatEntryPosition(seat);
auto vehicle = character->getCurrentVehicle();
auto seat = character->getCurrentSeat();
auto door = vehicle->getSeatEntryDoor(seat);
auto exitPos = vehicle->getSeatEntryPositionWorld(seat);
auto exitPosLocal = vehicle->getSeatEntryPosition(seat);
character->rotation = vehicle->getRotation();
character->rotation = vehicle->getRotation();
// Exit the vehicle immediatley
character->enterVehicle(nullptr, seat);
character->setPosition(exitPos);
// Exit the vehicle immediatley
character->enterVehicle(nullptr, seat);
character->setPosition(exitPos);
if (exitPosLocal.x > 0.f) {
character->playActivityAnimation(anm_jacked_rhs, false, true);
} else {
character->playActivityAnimation(anm_jacked_lhs, false, true);
}
// No need to open the door, it should already be open.
}
return false;
}
if (exitPosLocal.x > 0.f) {
character->playActivityAnimation(anm_jacked_rhs, false, true);
}
else {
character->playActivityAnimation(anm_jacked_lhs, false, true);
}
// No need to open the door, it should already be open.
}
return false;
}
if (character->getCurrentVehicle() == nullptr) return true;
if( character->getCurrentVehicle() == nullptr ) return true;
auto vehicle = character->getCurrentVehicle();
auto seat = character->getCurrentSeat();
auto door = vehicle->getSeatEntryDoor(seat);
auto exitPos = vehicle->getSeatEntryPositionWorld(seat);
auto exitPosLocal = vehicle->getSeatEntryPosition(seat);
auto vehicle = character->getCurrentVehicle();
auto seat = character->getCurrentSeat();
auto door = vehicle->getSeatEntryDoor(seat);
auto exitPos = vehicle->getSeatEntryPositionWorld(seat);
auto exitPosLocal = vehicle->getSeatEntryPosition(seat);
auto anm_exit = character->animations.car_getout_lhs;
auto anm_exit = character->animations.car_getout_lhs;
if (exitPosLocal.x > 0.f) {
anm_exit = character->animations.car_getout_rhs;
}
if( exitPosLocal.x > 0.f )
{
anm_exit = character->animations.car_getout_rhs;
}
if (vehicle->vehicle->type == VehicleData::BOAT) {
auto ppos = character->getPosition();
character->enterVehicle(nullptr, seat);
character->setPosition(ppos);
return true;
}
if( vehicle->vehicle->type == VehicleData::BOAT ) {
auto ppos = character->getPosition();
character->enterVehicle(nullptr, seat);
character->setPosition(ppos);
return true;
}
bool isDriver = vehicle->isOccupantDriver(character->getCurrentSeat());
bool isDriver = vehicle->isOccupantDriver(character->getCurrentSeat());
// If the vehicle is going too fast, slow down
if (isDriver) {
if (!vehicle->canOccupantExit()) {
vehicle->setBraking(1.f);
return false;
}
}
// If the vehicle is going too fast, slow down
if (isDriver)
{
if (!vehicle->canOccupantExit())
{
vehicle->setBraking(1.f);
return false;
}
}
if (character->animator->getAnimation(AnimIndexAction) == anm_exit) {
if (character->animator->isCompleted(AnimIndexAction)) {
character->enterVehicle(nullptr, seat);
character->setPosition(exitPos);
if( character->animator->getAnimation(AnimIndexAction) == anm_exit ) {
if( character->animator->isCompleted(AnimIndexAction) ) {
if (isDriver) {
// Apply the handbrake
vehicle->setHandbraking(true);
vehicle->setThrottle(0.f);
}
character->enterVehicle(nullptr, seat);
character->setPosition(exitPos);
return true;
}
} else {
character->playActivityAnimation(anm_exit, false, true);
if (door) {
vehicle->setPartTarget(door, true, door->openAngle);
}
}
if (isDriver)
{
// Apply the handbrake
vehicle->setHandbraking(true);
vehicle->setThrottle(0.f);
}
return true;
}
}
else {
character->playActivityAnimation(anm_exit, false, true);
if( door )
{
vehicle->setPartTarget(door, true, door->openAngle);
}
}
return false;
return false;
}
#include <engine/GameWorld.hpp>
#include <engine/GameData.hpp>
#include <data/Model.hpp>
bool Activities::ShootWeapon::update(CharacterObject *character, CharacterController *controller)
{
RW_UNUSED(controller);
#include <engine/GameData.hpp>
#include <engine/GameWorld.hpp>
bool Activities::ShootWeapon::update(CharacterObject *character,
CharacterController *controller) {
RW_UNUSED(controller);
auto& wepdata = _item->getWeaponData();
auto &wepdata = _item->getWeaponData();
// Instant hit weapons loop their anim
// Thrown projectiles have lob / throw.
// Instant hit weapons loop their anim
// Thrown projectiles have lob / throw.
// Update player direction
character->setRotation(glm::angleAxis(character->getLook().x, glm::vec3{0.f, 0.f, 1.f}));
// Update player direction
character->setRotation(
glm::angleAxis(character->getLook().x, glm::vec3{0.f, 0.f, 1.f}));
RW_CHECK(wepdata->inventorySlot < maxInventorySlots, "Inventory slot out of bounds");
auto& itemState = character->getCurrentState().weapons[wepdata->inventorySlot];
if (itemState.bulletsClip == 0 && itemState.bulletsTotal > 0) {
itemState.bulletsClip += std::min(int(itemState.bulletsTotal), wepdata->clipSize);
itemState.bulletsTotal -= itemState.bulletsClip;
}
bool hasammo = itemState.bulletsClip > 0;
RW_CHECK(wepdata->inventorySlot < maxInventorySlots,
"Inventory slot out of bounds");
auto &itemState =
character->getCurrentState().weapons[wepdata->inventorySlot];
if (itemState.bulletsClip == 0 && itemState.bulletsTotal > 0) {
itemState.bulletsClip +=
std::min(int(itemState.bulletsTotal), wepdata->clipSize);
itemState.bulletsTotal -= itemState.bulletsClip;
}
bool hasammo = itemState.bulletsClip > 0;
if( wepdata->fireType == WeaponData::INSTANT_HIT ) {
if( _item->isFiring(character) && hasammo ) {
if (wepdata->fireType == WeaponData::INSTANT_HIT) {
if (_item->isFiring(character) && hasammo) {
auto shootanim =
character->engine->data->animations[wepdata->animation1];
if (shootanim) {
if (character->animator->getAnimation(AnimIndexAction) !=
shootanim) {
character->playActivityAnimation(shootanim, false, false);
}
auto shootanim = character->engine->data->animations[wepdata->animation1];
if( shootanim ) {
if( character->animator->getAnimation(AnimIndexAction) != shootanim ) {
character->playActivityAnimation(shootanim, false, false);
}
auto loopstart = wepdata->animLoopStart / 100.f;
auto loopend = wepdata->animLoopEnd / 100.f;
auto firetime = wepdata->animFirePoint / 100.f;
auto loopstart = wepdata->animLoopStart / 100.f;
auto loopend = wepdata->animLoopEnd / 100.f;
auto firetime = wepdata->animFirePoint / 100.f;
auto currID =
character->animator->getAnimationTime(AnimIndexAction);
auto currID = character->animator->getAnimationTime(AnimIndexAction);
if (currID >= firetime && !_fired) {
itemState.bulletsClip--;
_item->fire(character);
_fired = true;
}
if (currID > loopend) {
character->animator->setAnimationTime(AnimIndexAction,
loopstart);
_fired = false;
}
}
} else {
if (character->animator->isCompleted(AnimIndexAction)) {
return true;
}
}
}
/// @todo Use Thrown flag instead of project (RPG isn't thrown eg.)
else if (wepdata->fireType == WeaponData::PROJECTILE && hasammo) {
auto shootanim =
character->engine->data->animations[wepdata->animation1];
auto throwanim =
character->engine->data->animations[wepdata->animation2];
if( currID >= firetime && ! _fired ) {
itemState.bulletsClip --;
_item->fire(character);
_fired = true;
}
if( currID > loopend ) {
character->animator->setAnimationTime( AnimIndexAction, loopstart );
_fired = false;
}
}
}
else {
if( character->animator->isCompleted(AnimIndexAction) ) {
return true;
}
}
}
/// @todo Use Thrown flag instead of project (RPG isn't thrown eg.)
else if( wepdata->fireType == WeaponData::PROJECTILE && hasammo ) {
auto shootanim = character->engine->data->animations[wepdata->animation1];
auto throwanim = character->engine->data->animations[wepdata->animation2];
if (character->animator->getAnimation(AnimIndexAction) == shootanim) {
if (character->animator->isCompleted(AnimIndexAction)) {
character->playActivityAnimation(throwanim, false, false);
}
} else if (character->animator->getAnimation(AnimIndexAction) ==
throwanim) {
auto firetime = wepdata->animCrouchFirePoint / 100.f;
auto currID =
character->animator->getAnimationTime(AnimIndexAction);
if( character->animator->getAnimation(AnimIndexAction) == shootanim ) {
if( character->animator->isCompleted(AnimIndexAction) ) {
character->playActivityAnimation(throwanim, false, false);
}
}
else if( character->animator->getAnimation(AnimIndexAction) == throwanim ) {
auto firetime = wepdata->animCrouchFirePoint / 100.f;
auto currID = character->animator->getAnimationTime(AnimIndexAction);
if (currID >= firetime && !_fired) {
itemState.bulletsClip--;
_item->fire(character);
_fired = true;
}
if (character->animator->isCompleted(AnimIndexAction)) {
return true;
}
} else {
character->playActivityAnimation(throwanim, false, true);
}
} else if (wepdata->fireType == WeaponData::MELEE) {
RW_CHECK(wepdata->fireType != WeaponData::MELEE,
"Melee attacks not implemented");
return true;
} else {
RW_ERROR("Unrecognized fireType: " << wepdata->fireType);
return true;
}
if( currID >= firetime && !_fired ) {
itemState.bulletsClip --;
_item->fire(character);
_fired = true;
}
if( character->animator->isCompleted(AnimIndexAction) ) {
return true;
}
}
else {
character->playActivityAnimation(throwanim, false, true);
}
}
else if( wepdata->fireType == WeaponData::MELEE ) {
RW_CHECK(wepdata->fireType != WeaponData::MELEE, "Melee attacks not implemented");
return true;
}
else
{
RW_ERROR("Unrecognized fireType: " << wepdata->fireType);
return true;
}
return false;
return false;
}

View File

@ -11,127 +11,141 @@ class VehicleObject;
/**
* @class CharacterController
* Character Controller Interface, translates high-level behaviours into low level actions.
* Character Controller Interface, translates high-level behaviours into low
* level actions.
*/
class CharacterController
{
class CharacterController {
public:
/**
* @brief The Activity struct interface
*/
struct Activity {
virtual ~Activity() {
}
/**
* @brief The Activity struct interface
*/
struct Activity {
virtual std::string name() const = 0;
virtual ~Activity() {}
/**
* @brief canSkip
* @return true if the activity can be skipped.
*/
virtual bool canSkip(CharacterObject*, CharacterController*) const {
return false;
}
virtual std::string name() const = 0;
virtual bool update(CharacterObject* character,
CharacterController* controller) = 0;
};
/**
* @brief canSkip
* @return true if the activity can be skipped.
*/
virtual bool canSkip(CharacterObject*, CharacterController*) const { return false; }
virtual bool update(CharacterObject* character, CharacterController* controller) = 0;
};
/**
* Available AI goals.
*/
enum Goal
{
/**
* No goal, will idle or execute external Activities.
*/
None,
/**
* Keep close to leader character
*/
FollowLeader,
/**
* Wander randomly around the map
*/
TrafficWander
};
/**
* Available AI goals.
*/
enum Goal {
/**
* No goal, will idle or execute external Activities.
*/
None,
/**
* Keep close to leader character
*/
FollowLeader,
/**
* Wander randomly around the map
*/
TrafficWander
};
protected:
/**
* The character being controlled.
*/
CharacterObject* character;
/**
* The character being controlled.
*/
CharacterObject* character;
Activity* _currentActivity;
Activity* _nextActivity;
Activity* _currentActivity;
Activity* _nextActivity;
bool updateActivity();
void setActivity(Activity* activity);
bool updateActivity();
void setActivity(Activity* activity);
float m_closeDoorTimer;
// Goal related variables
Goal currentGoal;
CharacterObject* leader;
AIGraphNode* targetNode;
float m_closeDoorTimer;
// Goal related variables
Goal currentGoal;
CharacterObject* leader;
AIGraphNode* targetNode;
public:
CharacterController(CharacterObject* character);
CharacterController(CharacterObject* character);
virtual ~CharacterController() { }
virtual ~CharacterController() {
}
Activity* getCurrentActivity() const { return _currentActivity; }
Activity* getCurrentActivity() const {
return _currentActivity;
}
Activity* getNextActivity() const { return _nextActivity; }
Activity* getNextActivity() const {
return _nextActivity;
}
/**
* @brief skipActivity Cancel the current activity immediatley, if possible.
*/
void skipActivity();
/**
* @brief skipActivity Cancel the current activity immediatley, if possible.
*/
void skipActivity();
/**
* @brief setNextActivity Sets the next Activity with a parameter.
* @param activity
* @param position
*/
void setNextActivity( Activity* activity );
/**
* @brief setNextActivity Sets the next Activity with a parameter.
* @param activity
* @param position
*/
void setNextActivity(Activity* activity);
/**
* @brief IsCurrentActivity
* @param activity Name of activity to check for
* @return if the given activity is the current activity
*/
bool isCurrentActivity(const std::string& activity) const;
/**
* @brief update Updates the controller.
* @param dt
*/
virtual void update(float dt);
/**
* @brief IsCurrentActivity
* @param activity Name of activity to check for
* @return if the given activity is the current activity
*/
bool isCurrentActivity(const std::string& activity) const;
virtual glm::vec3 getTargetPosition() = 0;
/**
* @brief update Updates the controller.
* @param dt
*/
virtual void update(float dt);
/**
* @brief
* @return Returns the Character Object
*/
CharacterObject* getCharacter() const;
virtual glm::vec3 getTargetPosition() = 0;
void setMoveDirection(const glm::vec3& movement);
void setLookDirection(const glm::vec2& look);
/**
* @brief
* @return Returns the Character Object
*/
CharacterObject* getCharacter() const;
void setRunning(bool run);
void setGoal(Goal goal) { currentGoal = goal; }
Goal getGoal() const { return currentGoal; }
void setTargetCharacter(CharacterObject* c) { leader = c; }
CharacterObject* getTargetCharacter() const { return leader; }
void setMoveDirection(const glm::vec3& movement);
void setLookDirection(const glm::vec2& look);
void setRunning(bool run);
void setGoal(Goal goal) {
currentGoal = goal;
}
Goal getGoal() const {
return currentGoal;
}
void setTargetCharacter(CharacterObject* c) {
leader = c;
}
CharacterObject* getTargetCharacter() const {
return leader;
}
};
#define DECL_ACTIVITY( activity_name ) \
static constexpr auto ActivityName = #activity_name; \
std::string name() const { return ActivityName; }
#define DECL_ACTIVITY(activity_name) \
static constexpr auto ActivityName = #activity_name; \
std::string name() const { \
return ActivityName; \
}
// TODO: Refactor this with an ugly macro to reduce code dup.
class WeaponItem;
@ -142,74 +156,78 @@ class WeaponItem;
* @todo Move into ControllerActivities.hpp or equivelant
*/
namespace Activities {
struct GoTo : public CharacterController::Activity {
DECL_ACTIVITY( GoTo )
struct GoTo : public CharacterController::Activity {
DECL_ACTIVITY(GoTo)
glm::vec3 target;
bool sprint;
glm::vec3 target;
bool sprint;
GoTo( const glm::vec3& target, bool _sprint = false )
: target( target ), sprint(_sprint) {}
GoTo(const glm::vec3& target, bool _sprint = false)
: target(target), sprint(_sprint) {
}
bool update(CharacterObject* character, CharacterController* controller);
bool update(CharacterObject* character, CharacterController* controller);
bool canSkip(CharacterObject *, CharacterController *) const { return true; }
};
struct Jump : public CharacterController::Activity
{
DECL_ACTIVITY( Jump )
bool jumped;
Jump() : jumped(false) {}
bool update(CharacterObject* character, CharacterController* controller);
};
bool canSkip(CharacterObject*, CharacterController*) const {
return true;
}
};
struct EnterVehicle : public CharacterController::Activity {
DECL_ACTIVITY( EnterVehicle )
struct Jump : public CharacterController::Activity {
DECL_ACTIVITY(Jump)
VehicleObject* vehicle;
int seat;
enum {
ANY_SEAT = -1 // Magic number for any seat but the driver's.
};
bool jumped;
bool entering;
Jump() : jumped(false) {
}
EnterVehicle( VehicleObject* vehicle, int seat = 0 )
: vehicle( vehicle ), seat( seat ), entering(false) {}
bool update(CharacterObject* character, CharacterController* controller);
};
bool canSkip(CharacterObject* character, CharacterController*) const override;
struct EnterVehicle : public CharacterController::Activity {
DECL_ACTIVITY(EnterVehicle)
bool update(CharacterObject *character, CharacterController *controller);
};
VehicleObject* vehicle;
int seat;
struct ExitVehicle : public CharacterController::Activity {
DECL_ACTIVITY( ExitVehicle )
enum {
ANY_SEAT = -1 // Magic number for any seat but the driver's.
};
const bool jacked;
bool entering;
ExitVehicle(bool jacked_ = false)
: jacked(jacked_)
{}
EnterVehicle(VehicleObject* vehicle, int seat = 0)
: vehicle(vehicle), seat(seat), entering(false) {
}
bool update(CharacterObject *character, CharacterController *controller);
};
bool canSkip(CharacterObject* character,
CharacterController*) const override;
struct ShootWeapon : public CharacterController::Activity {
DECL_ACTIVITY( ShootWeapon )
bool update(CharacterObject* character, CharacterController* controller);
};
WeaponItem* _item;
bool _fired;
struct ExitVehicle : public CharacterController::Activity {
DECL_ACTIVITY(ExitVehicle)
ShootWeapon( WeaponItem* item )
: _item(item), _fired(false) {}
const bool jacked;
bool update(CharacterObject *character, CharacterController *controller);
};
ExitVehicle(bool jacked_ = false) : jacked(jacked_) {
}
bool update(CharacterObject* character, CharacterController* controller);
};
struct ShootWeapon : public CharacterController::Activity {
DECL_ACTIVITY(ShootWeapon)
WeaponItem* _item;
bool _fired;
ShootWeapon(WeaponItem* item) : _item(item), _fired(false) {
}
bool update(CharacterObject* character, CharacterController* controller);
};
}
#endif

View File

@ -1,101 +1,88 @@
#include <ai/DefaultAIController.hpp>
#include <objects/CharacterObject.hpp>
#include <engine/GameWorld.hpp>
#include <objects/CharacterObject.hpp>
glm::vec3 DefaultAIController::getTargetPosition()
{
/*if(targetNode) {
if(lastNode && character->getCurrentVehicle()) {
auto nDir = glm::normalize(targetNode->position - lastNode->position);
auto right = glm::cross(nDir, glm::vec3(0.f, 0.f, 1.f));
return targetNode->position + right * 2.2f;
}
return targetNode->position;
}*/
return glm::vec3();
glm::vec3 DefaultAIController::getTargetPosition() {
/*if(targetNode) {
if(lastNode && character->getCurrentVehicle()) {
auto nDir = glm::normalize(targetNode->position -
lastNode->position);
auto right = glm::cross(nDir, glm::vec3(0.f, 0.f, 1.f));
return targetNode->position + right * 2.2f;
}
return targetNode->position;
}*/
return glm::vec3();
}
const float followRadius = 5.f;
void DefaultAIController::update(float dt)
{
switch(currentGoal)
{
case FollowLeader:
{
if( ! leader ) break;
if( getCharacter()->getCurrentVehicle() )
{
if( leader->getCurrentVehicle() != getCharacter()->getCurrentVehicle() )
{
skipActivity();
setNextActivity(new Activities::ExitVehicle);
}
// else we're already in the right spot.
}
else
{
if( leader->getCurrentVehicle() )
{
setNextActivity(new Activities::EnterVehicle(leader->getCurrentVehicle(), 1));
}
else
{
glm::vec3 dir = leader->getPosition() - getCharacter()->getPosition();
if( glm::length(dir) > followRadius )
{
if( glm::distance(gotoPos, leader->getPosition()) > followRadius )
{
gotoPos = leader->getPosition() + ( glm::normalize(-dir) * followRadius * 0.7f );
skipActivity();
setNextActivity(new Activities::GoTo(gotoPos));
}
}
}
}
}
break;
case TrafficWander:
{
if( targetNode )
{
auto targetDistance = glm::vec2(character->getPosition() - targetNode->position);
if( glm::length(targetDistance) <= 0.1f )
{
// Assign the next target node
auto lastTarget = targetNode;
std::random_device rd;
std::default_random_engine re(rd());
std::uniform_int_distribution<> d(0, lastTarget->connections.size()-1);
targetNode = lastTarget->connections.at(d(re));
setNextActivity(new Activities::GoTo(targetNode->position));
}
else if ( getCurrentActivity() == nullptr )
{
setNextActivity(new Activities::GoTo(targetNode->position));
}
}
else
{
// We need to pick an initial node
auto& graph = getCharacter()->engine->aigraph;
AIGraphNode* node = nullptr;
float mindist = std::numeric_limits<float>::max();
for( auto n : graph.nodes )
{
float d = glm::distance( n->position, getCharacter()->getPosition() );
if( d < mindist )
{
node = n;
mindist = d;
}
}
targetNode = node;
}
}
break;
default: break;
}
CharacterController::update(dt);
void DefaultAIController::update(float dt) {
switch (currentGoal) {
case FollowLeader: {
if (!leader) break;
if (getCharacter()->getCurrentVehicle()) {
if (leader->getCurrentVehicle() !=
getCharacter()->getCurrentVehicle()) {
skipActivity();
setNextActivity(new Activities::ExitVehicle);
}
// else we're already in the right spot.
} else {
if (leader->getCurrentVehicle()) {
setNextActivity(new Activities::EnterVehicle(
leader->getCurrentVehicle(), 1));
} else {
glm::vec3 dir =
leader->getPosition() - getCharacter()->getPosition();
if (glm::length(dir) > followRadius) {
if (glm::distance(gotoPos, leader->getPosition()) >
followRadius) {
gotoPos =
leader->getPosition() +
(glm::normalize(-dir) * followRadius * 0.7f);
skipActivity();
setNextActivity(new Activities::GoTo(gotoPos));
}
}
}
}
} break;
case TrafficWander: {
if (targetNode) {
auto targetDistance =
glm::vec2(character->getPosition() - targetNode->position);
if (glm::length(targetDistance) <= 0.1f) {
// Assign the next target node
auto lastTarget = targetNode;
std::random_device rd;
std::default_random_engine re(rd());
std::uniform_int_distribution<> d(
0, lastTarget->connections.size() - 1);
targetNode = lastTarget->connections.at(d(re));
setNextActivity(new Activities::GoTo(targetNode->position));
} else if (getCurrentActivity() == nullptr) {
setNextActivity(new Activities::GoTo(targetNode->position));
}
} else {
// We need to pick an initial node
auto& graph = getCharacter()->engine->aigraph;
AIGraphNode* node = nullptr;
float mindist = std::numeric_limits<float>::max();
for (auto n : graph.nodes) {
float d = glm::distance(n->position,
getCharacter()->getPosition());
if (d < mindist) {
node = n;
mindist = d;
}
}
targetNode = node;
}
} break;
default:
break;
}
CharacterController::update(dt);
}

View File

@ -5,16 +5,16 @@
#include <random>
struct AIGraphNode;
class DefaultAIController : public CharacterController
{
glm::vec3 gotoPos;
public:
DefaultAIController(CharacterObject* character)
: CharacterController(character) {}
class DefaultAIController : public CharacterController {
glm::vec3 gotoPos;
public:
DefaultAIController(CharacterObject* character)
: CharacterController(character) {
}
glm::vec3 getTargetPosition();
glm::vec3 getTargetPosition();
virtual void update(float dt);
};

View File

@ -1,81 +1,74 @@
#include <ai/PlayerController.hpp>
#include <objects/CharacterObject.hpp>
#include <objects/VehicleObject.hpp>
#include <engine/Animator.hpp>
#include <engine/GameWorld.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <engine/Animator.hpp>
#include <objects/CharacterObject.hpp>
#include <objects/VehicleObject.hpp>
PlayerController::PlayerController(CharacterObject* character)
: CharacterController(character), lastRotation(glm::vec3(0.f, 0.f, 0.f)), _enabled(true)
{
: CharacterController(character)
, lastRotation(glm::vec3(0.f, 0.f, 0.f))
, _enabled(true) {
}
void PlayerController::setInputEnabled(bool enabled)
{
_enabled = enabled;
void PlayerController::setInputEnabled(bool enabled) {
_enabled = enabled;
}
bool PlayerController::isInputEnabled() const
{
return _enabled;
bool PlayerController::isInputEnabled() const {
return _enabled;
}
void PlayerController::updateCameraDirection(const glm::quat& rot)
{
cameraRotation = rot;
void PlayerController::updateCameraDirection(const glm::quat& rot) {
cameraRotation = rot;
}
void PlayerController::updateMovementDirection(const glm::vec3& dir, const glm::vec3 &rawdirection)
{
if( _currentActivity == nullptr ) {
direction = dir;
setMoveDirection(rawdirection);
}
void PlayerController::updateMovementDirection(const glm::vec3& dir,
const glm::vec3& rawdirection) {
if (_currentActivity == nullptr) {
direction = dir;
setMoveDirection(rawdirection);
}
}
void PlayerController::exitVehicle()
{
if(character->getCurrentVehicle()) {
setNextActivity(new Activities::ExitVehicle());
}
void PlayerController::exitVehicle() {
if (character->getCurrentVehicle()) {
setNextActivity(new Activities::ExitVehicle());
}
}
void PlayerController::enterNearestVehicle()
{
if(! character->getCurrentVehicle()) {
auto world = character->engine;
VehicleObject* nearest = nullptr; float d = 10.f;
void PlayerController::enterNearestVehicle() {
if (!character->getCurrentVehicle()) {
auto world = character->engine;
VehicleObject* nearest = nullptr;
float d = 10.f;
for( auto& p : world->vehiclePool.objects ) {
auto object = p.second;
float vd = glm::length( character->getPosition() - object->getPosition());
if( vd < d ) {
d = vd;
nearest = static_cast<VehicleObject*>(object);
}
}
for (auto& p : world->vehiclePool.objects) {
auto object = p.second;
float vd =
glm::length(character->getPosition() - object->getPosition());
if (vd < d) {
d = vd;
nearest = static_cast<VehicleObject*>(object);
}
}
if( nearest ) {
setNextActivity(new Activities::EnterVehicle(nearest, 0));
}
}
if (nearest) {
setNextActivity(new Activities::EnterVehicle(nearest, 0));
}
}
}
void PlayerController::update(float dt)
{
CharacterController::update(dt);
void PlayerController::update(float dt) {
CharacterController::update(dt);
}
glm::vec3 PlayerController::getTargetPosition()
{
return direction;
glm::vec3 PlayerController::getTargetPosition() {
return direction;
}
void PlayerController::jump()
{
if(! character->isInWater() ) {
setNextActivity(new Activities::Jump());
}
void PlayerController::jump() {
if (!character->isInWater()) {
setNextActivity(new Activities::Jump());
}
}

View File

@ -3,45 +3,46 @@
#define _PLAYERCONTROLLER_HPP_
#include <ai/CharacterController.hpp>
class PlayerController : public CharacterController
{
glm::quat cameraRotation;
glm::vec3 direction;
class PlayerController : public CharacterController {
glm::quat cameraRotation;
glm::quat lastRotation;
glm::vec3 direction;
glm::quat lastRotation;
bool _enabled;
bool _enabled;
public:
PlayerController(CharacterObject* character);
PlayerController(CharacterObject* character);
/**
* @brief Enables and disables player input.
* @todo actually implement input being disabled.
*/
void setInputEnabled(bool enabled);
bool isInputEnabled() const;
void updateCameraDirection(const glm::quat& rot);
void updateMovementDirection(const glm::vec3& pos,
const glm::vec3& rawdirection);
void exitVehicle();
void enterNearestVehicle();
/**
* @brief Enables and disables player input.
* @todo actually implement input being disabled.
*/
void setInputEnabled(bool enabled);
bool isInputEnabled() const;
void updateCameraDirection(const glm::quat& rot);
void updateMovementDirection(const glm::vec3& pos, const glm::vec3& rawdirection);
void exitVehicle();
void enterNearestVehicle();
virtual void update(float dt);
virtual glm::vec3 getTargetPosition();
void jump();
/**
* returns 0 (only one player supported)
*/
int getScriptObjectID() const { return 0; }
virtual glm::vec3 getTargetPosition();
void jump();
/**
* returns 0 (only one player supported)
*/
int getScriptObjectID() const {
return 0;
}
};
#endif

View File

@ -1,172 +1,169 @@
#include "ai/TrafficDirector.hpp"
#include <ai/AIGraphNode.hpp>
#include <ai/CharacterController.hpp>
#include <engine/GameWorld.hpp>
#include <core/Logger.hpp>
#include <engine/GameState.hpp>
#include <objects/GameObject.hpp>
#include <engine/GameWorld.hpp>
#include <objects/CharacterObject.hpp>
#include <objects/GameObject.hpp>
#include <objects/VehicleObject.hpp>
#include <render/ViewCamera.hpp>
#include <core/Logger.hpp>
#include <glm/gtx/string_cast.hpp>
#include <glm/gtx/norm.hpp>
#include <glm/gtx/string_cast.hpp>
#ifdef RW_WINDOWS
#include <rw_mingw.hpp>
#endif
TrafficDirector::TrafficDirector(AIGraph* g, GameWorld* w)
: graph( g ), world( w ), pedDensity(1.f), carDensity(1.f),
maximumPedestrians(20), maximumCars(10)
{
: graph(g)
, world(w)
, pedDensity(1.f)
, carDensity(1.f)
, maximumPedestrians(20)
, maximumCars(10) {
}
std::vector< AIGraphNode* > TrafficDirector::findAvailableNodes(AIGraphNode::NodeType type, const ViewCamera& camera, float radius)
{
std::vector<AIGraphNode*> available;
available.reserve(20);
std::vector<AIGraphNode*> TrafficDirector::findAvailableNodes(
AIGraphNode::NodeType type, const ViewCamera& camera, float radius) {
std::vector<AIGraphNode*> available;
available.reserve(20);
graph->gatherExternalNodesNear(camera.position, radius, available);
graph->gatherExternalNodesNear(camera.position, radius, available);
float density = type == AIGraphNode::Vehicle ? carDensity : pedDensity;
float minDist = (10.f / density) * (10.f / density);
float halfRadius2 = std::pow(radius / 2.f, 2.f);
float density = type == AIGraphNode::Vehicle ? carDensity : pedDensity;
float minDist = (10.f / density) * (10.f / density);
float halfRadius2 = std::pow(radius / 2.f, 2.f);
// Check if any of the nearby nodes are blocked by a pedestrian standing on it
// or because it's inside the view frustum
for (auto it = available.begin(); it != available.end();) {
bool blocked = false;
float dist2 = glm::distance2(camera.position, (*it)->position);
// Check if any of the nearby nodes are blocked by a pedestrian standing on
// it
// or because it's inside the view frustum
for (auto it = available.begin(); it != available.end();) {
bool blocked = false;
float dist2 = glm::distance2(camera.position, (*it)->position);
for (auto& obj : world->pedestrianPool.objects) {
if (glm::distance2( (*it)->position, obj.second->getPosition() ) <= minDist) {
blocked = true;
break;
}
}
for (auto& obj : world->pedestrianPool.objects) {
if (glm::distance2((*it)->position, obj.second->getPosition()) <=
minDist) {
blocked = true;
break;
}
}
// Check that we're not going to spawn something right where the player is looking
if (dist2 <= halfRadius2 && camera.frustum.intersects((*it)->position, 1.f)) {
blocked = true;
}
if (blocked) {
it = available.erase( it );
}
else {
it++;
}
}
// Check that we're not going to spawn something right where the player
// is looking
if (dist2 <= halfRadius2 &&
camera.frustum.intersects((*it)->position, 1.f)) {
blocked = true;
}
return available;
if (blocked) {
it = available.erase(it);
} else {
it++;
}
}
return available;
}
void TrafficDirector::setDensity(AIGraphNode::NodeType type, float density)
{
switch ( type )
{
case AIGraphNode::Vehicle:
carDensity = density;
break;
case AIGraphNode::Pedestrian:
pedDensity = density;
break;
}
void TrafficDirector::setDensity(AIGraphNode::NodeType type, float density) {
switch (type) {
case AIGraphNode::Vehicle:
carDensity = density;
break;
case AIGraphNode::Pedestrian:
pedDensity = density;
break;
}
}
std::vector<GameObject*> TrafficDirector::populateNearby(const ViewCamera& camera, float radius, int maxSpawn)
{
int availablePeds = maximumPedestrians - world->pedestrianPool.objects.size();
std::vector<GameObject*> TrafficDirector::populateNearby(
const ViewCamera& camera, float radius, int maxSpawn) {
int availablePeds =
maximumPedestrians - world->pedestrianPool.objects.size();
std::vector<GameObject*> created;
std::vector<GameObject*> created;
/// @todo Check how "in player view" should be determined.
/// @todo Check how "in player view" should be determined.
// Don't check the frustum for things more than 1/2 of the radius away
// so that things will spawn as you drive towards them
float halfRadius2 = std::pow(radius/ 2.f, 2.f);
// Don't check the frustum for things more than 1/2 of the radius away
// so that things will spawn as you drive towards them
float halfRadius2 = std::pow(radius / 2.f, 2.f);
// Spawn vehicles at vehicle generators
auto camera2D = glm::vec2(camera.position);
for (auto& gen : world->state->vehicleGenerators) {
/// @todo verify how vehicle generator proximity is determined
auto gen2D = glm::vec2(gen.position);
float dist2 = glm::distance2(camera2D, gen2D);
if (dist2 < radius * radius) {
auto position = gen.position;
// Check that the on-ground position is not in view
if (gen.position.z < -90.f) {
position = world->getGroundAtPosition(position);
}
// Spawn vehicles at vehicle generators
auto camera2D = glm::vec2(camera.position);
for (auto& gen : world->state->vehicleGenerators) {
/// @todo verify how vehicle generator proximity is determined
auto gen2D = glm::vec2(gen.position);
float dist2 = glm::distance2(camera2D, gen2D);
if (dist2 < radius * radius) {
auto position = gen.position;
// Check that the on-ground position is not in view
if (gen.position.z < -90.f) {
position = world->getGroundAtPosition(position);
}
if (dist2 <= halfRadius2 &&
camera.frustum.intersects(position, 1.f)) {
if (!gen.alwaysSpawn) {
// Don't spawn in the view frustum unless we're forced to
continue;
}
}
auto spawned = world->tryToSpawnVehicle(gen);
if (spawned) {
created.push_back(spawned);
}
}
}
if (dist2 <= halfRadius2 &&
camera.frustum.intersects(position, 1.f)) {
if (!gen.alwaysSpawn) {
// Don't spawn in the view frustum unless we're forced to
continue;
}
}
auto spawned = world->tryToSpawnVehicle(gen);
if (spawned) {
created.push_back(spawned);
}
}
}
auto type = AIGraphNode::Pedestrian;
auto available = findAvailableNodes(type, camera, radius);
if( availablePeds <= 0 )
{
// We have already reached the limit of spawned traffic
return { };
}
/// Hardcoded cop Pedestrian
std::vector<uint16_t> validPeds = { 1 };
validPeds.insert(validPeds.end(), {20, 11, 19, 5});
std::random_device rd;
std::default_random_engine re(rd());
std::uniform_int_distribution<> d(0, validPeds.size()-1);
auto type = AIGraphNode::Pedestrian;
auto available = findAvailableNodes(type, camera, radius);
int counter = availablePeds;
// maxSpawn can be -1 for "as many as possible"
if( maxSpawn > -1 )
{
counter = std::min( availablePeds, maxSpawn );
}
if (availablePeds <= 0) {
// We have already reached the limit of spawned traffic
return {};
}
for ( AIGraphNode* spawn : available )
{
if( spawn->type != AIGraphNode::Pedestrian )
{
continue;
}
if ( counter > -1 )
{
if ( counter == 0 )
{
break;
}
counter --;
}
/// Hardcoded cop Pedestrian
std::vector<uint16_t> validPeds = {1};
validPeds.insert(validPeds.end(), {20, 11, 19, 5});
std::random_device rd;
std::default_random_engine re(rd());
std::uniform_int_distribution<> d(0, validPeds.size() - 1);
// Spawn a pedestrian from the available pool
auto ped = world->createPedestrian(validPeds[d(re)], spawn->position + glm::vec3( 0.f, 0.f, 1.f ) );
ped->setLifetime(GameObject::TrafficLifetime);
ped->controller->setGoal(CharacterController::TrafficWander);
created.push_back( ped );
}
// Find places it's legal to spawn things
return created;
int counter = availablePeds;
// maxSpawn can be -1 for "as many as possible"
if (maxSpawn > -1) {
counter = std::min(availablePeds, maxSpawn);
}
for (AIGraphNode* spawn : available) {
if (spawn->type != AIGraphNode::Pedestrian) {
continue;
}
if (counter > -1) {
if (counter == 0) {
break;
}
counter--;
}
// Spawn a pedestrian from the available pool
auto ped = world->createPedestrian(
validPeds[d(re)], spawn->position + glm::vec3(0.f, 0.f, 1.f));
ped->setLifetime(GameObject::TrafficLifetime);
ped->controller->setGoal(CharacterController::TrafficWander);
created.push_back(ped);
}
// Find places it's legal to spawn things
return created;
}
void TrafficDirector::setPopulationLimits(int maxPeds, int maxCars)
{
maximumPedestrians = maxPeds;
maximumCars = maxCars;
void TrafficDirector::setPopulationLimits(int maxPeds, int maxCars) {
maximumPedestrians = maxPeds;
maximumCars = maxCars;
}

View File

@ -11,34 +11,35 @@ class GameObject;
class GameWorld;
class ViewCamera;
class TrafficDirector
{
class TrafficDirector {
public:
TrafficDirector(AIGraph* graph, GameWorld* world);
std::vector< AIGraphNode* > findAvailableNodes(AIGraphNode::NodeType type, const ViewCamera& camera, float radius);
void setDensity(AIGraphNode::NodeType type, float density);
TrafficDirector(AIGraph* graph, GameWorld* world);
/**
* Creates new traffic at available locations.
* @param camera The camera to spawn around
* @param radius the maximum distance to spawn in
* @param max The maximum number of traffic to create.
*/
std::vector<GameObject*> populateNearby( const ViewCamera& camera, float radius, int maxSpawn = -1 );
std::vector<AIGraphNode*> findAvailableNodes(AIGraphNode::NodeType type,
const ViewCamera& camera,
float radius);
void setDensity(AIGraphNode::NodeType type, float density);
/**
* Creates new traffic at available locations.
* @param camera The camera to spawn around
* @param radius the maximum distance to spawn in
* @param max The maximum number of traffic to create.
*/
std::vector<GameObject*> populateNearby(const ViewCamera& camera,
float radius, int maxSpawn = -1);
/**
* Sets the maximum number of pedestrians and cars in the traffic system
*/
void setPopulationLimits(int maxPeds, int maxCars);
/**
* Sets the maximum number of pedestrians and cars in the traffic system
*/
void setPopulationLimits(int maxPeds, int maxCars);
private:
AIGraph* graph;
GameWorld* world;
float pedDensity;
float carDensity;
int maximumPedestrians;
int maximumCars;
AIGraph* graph;
GameWorld* world;
float pedDensity;
float carDensity;
int maximumPedestrians;
int maximumCars;
};