1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-10-06 00:57:19 +02:00
openrw/framework2/GTADefaultAIController.cpp

58 lines
1.6 KiB
C++
Raw Normal View History

2013-08-11 22:42:54 +02:00
#include "renderwure/ai/GTADefaultAIController.hpp"
2013-09-09 01:18:36 +02:00
#include <renderwure/objects/GTACharacter.hpp>
2013-08-11 22:42:54 +02:00
#include <renderwure/engine/GTAEngine.hpp>
void GTADefaultAIController::update(float dt)
{
if( action == Wander ) {
if( targetNode == nullptr ) {
float d = std::numeric_limits< float >::max();
auto oldTarget = targetNode;
for( auto n = character->engine->ainodes.begin();
n != character->engine->ainodes.end();
++n ) {
2013-08-16 02:59:32 +02:00
if( (*n)->type != GTAAINode::Pedestrian ) continue;
float ld = glm::length( (*n)->position - character->position );
2013-08-15 19:11:36 +02:00
if( ld > 2.5f && ld < d ) {
2013-08-11 22:42:54 +02:00
d = ld;
2013-08-16 02:59:32 +02:00
targetNode = (*n);
2013-08-11 22:42:54 +02:00
}
}
if( targetNode == nullptr ) {
character->changeAction(GTACharacter::Idle);
}
else {
character->changeAction(GTACharacter::Walk);
2013-08-15 19:11:36 +02:00
// Choose a new random margin
std::uniform_real_distribution<float> dist(1.f, 2.5f);
nodeMargin = dist(character->engine->randomEngine);
2013-08-11 22:42:54 +02:00
}
}
2013-08-15 19:11:36 +02:00
else if( glm::length(targetNode->position - character->position) < nodeMargin ) {
2013-08-16 02:59:32 +02:00
if( targetNode->connections.size() > 0 ) {
std::uniform_int_distribution<int> dist(0, targetNode->connections.size()-1);
targetNode = character->engine->ainodes[dist(character->engine->randomEngine)];
2013-08-11 22:42:54 +02:00
}
else {
targetNode = nullptr;
}
}
}
}
glm::vec3 GTADefaultAIController::getTargetPosition()
{
return glm::vec3();
}
glm::quat GTADefaultAIController::getTargetRotation()
{
if( action == Wander && targetNode != nullptr ) {
auto d = targetNode->position - character->position;
float a = atan2( d.x, d.y );
return glm::quat( glm::vec3(0.f, 0.f, -a) );
}
return glm::quat();
}