| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- //
- // Copyright (c) 2008-2017 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../Precompiled.h"
- #include "../Core/Context.h"
- #include "../Core/Profiler.h"
- #include "../Graphics/DebugRenderer.h"
- #include "../IO/Log.h"
- #include "../IO/MemoryBuffer.h"
- #include "../Navigation/NavigationEvents.h"
- #include "../Navigation/NavigationMesh.h"
- #include "../Navigation/CrowdAgent.h"
- #include "../Scene/Node.h"
- #include "../Scene/Scene.h"
- // ATOMIC BEGIN
- #include <Detour/include/DetourCommon.h>
- #include <DetourCrowd/include/DetourCrowd.h>
- // ATOMIC END
- #include "../DebugNew.h"
- namespace Atomic
- {
- extern const char* NAVIGATION_CATEGORY;
- static const CrowdAgentRequestedTarget DEFAULT_AGENT_REQUEST_TARGET_TYPE = CA_REQUESTEDTARGET_NONE;
- static const float DEFAULT_AGENT_MAX_SPEED = 0.f;
- static const float DEFAULT_AGENT_MAX_ACCEL = 0.f;
- static const unsigned DEFAULT_AGENT_QUERY_FILTER_TYPE = 0;
- static const unsigned DEFAULT_AGENT_OBSTACLE_AVOIDANCE_TYPE = 0;
- static const NavigationQuality DEFAULT_AGENT_AVOIDANCE_QUALITY = NAVIGATIONQUALITY_HIGH;
- static const NavigationPushiness DEFAULT_AGENT_NAVIGATION_PUSHINESS = NAVIGATIONPUSHINESS_MEDIUM;
- static const unsigned SCOPE_NAVIGATION_QUALITY_PARAMS = 1;
- static const unsigned SCOPE_NAVIGATION_PUSHINESS_PARAMS = 2;
- static const unsigned SCOPE_BASE_PARAMS = M_MAX_UNSIGNED & ~SCOPE_NAVIGATION_QUALITY_PARAMS & ~SCOPE_NAVIGATION_PUSHINESS_PARAMS;
- static const char* crowdAgentRequestedTargetTypeNames[] = {
- "None",
- "Position",
- "Velocity",
- 0
- };
- static const char* crowdAgentAvoidanceQualityNames[] = {
- "Low",
- "Medium",
- "High",
- 0
- };
- static const char* crowdAgentPushinessNames[] = {
- "Low",
- "Medium",
- "High",
- "None",
- 0
- };
- CrowdAgent::CrowdAgent(Context* context) :
- Component(context),
- agentCrowdId_(-1),
- requestedTargetType_(DEFAULT_AGENT_REQUEST_TARGET_TYPE),
- updateNodePosition_(true),
- maxAccel_(DEFAULT_AGENT_MAX_ACCEL),
- maxSpeed_(DEFAULT_AGENT_MAX_SPEED),
- radius_(0.0f),
- height_(0.0f),
- queryFilterType_(DEFAULT_AGENT_QUERY_FILTER_TYPE),
- obstacleAvoidanceType_(DEFAULT_AGENT_OBSTACLE_AVOIDANCE_TYPE),
- navQuality_(DEFAULT_AGENT_AVOIDANCE_QUALITY),
- navPushiness_(DEFAULT_AGENT_NAVIGATION_PUSHINESS),
- previousTargetState_(CA_TARGET_NONE),
- previousAgentState_(CA_STATE_WALKING),
- ignoreTransformChanges_(false)
- {
- SubscribeToEvent(E_NAVIGATION_TILE_ADDED, ATOMIC_HANDLER(CrowdAgent, HandleNavigationTileAdded));
- }
- CrowdAgent::~CrowdAgent()
- {
- RemoveAgentFromCrowd();
- }
- void CrowdAgent::RegisterObject(Context* context)
- {
- context->RegisterFactory<CrowdAgent>(NAVIGATION_CATEGORY);
- ATOMIC_ATTRIBUTE("Target Position", Vector3, targetPosition_, Vector3::ZERO, AM_DEFAULT);
- ATOMIC_ATTRIBUTE("Target Velocity", Vector3, targetVelocity_, Vector3::ZERO, AM_DEFAULT);
- ATOMIC_ENUM_ATTRIBUTE("Requested Target Type", requestedTargetType_, crowdAgentRequestedTargetTypeNames,
- DEFAULT_AGENT_REQUEST_TARGET_TYPE, AM_DEFAULT);
- ATOMIC_ACCESSOR_ATTRIBUTE("Update Node Position", GetUpdateNodePosition, SetUpdateNodePosition, bool, true, AM_DEFAULT);
- ATOMIC_ATTRIBUTE("Max Accel", float, maxAccel_, DEFAULT_AGENT_MAX_ACCEL, AM_DEFAULT);
- ATOMIC_ATTRIBUTE("Max Speed", float, maxSpeed_, DEFAULT_AGENT_MAX_SPEED, AM_DEFAULT);
- ATOMIC_ATTRIBUTE("Radius", float, radius_, 0.0f, AM_DEFAULT);
- ATOMIC_ATTRIBUTE("Height", float, height_, 0.0f, AM_DEFAULT);
- ATOMIC_ATTRIBUTE("Query Filter Type", unsigned, queryFilterType_, DEFAULT_AGENT_QUERY_FILTER_TYPE, AM_DEFAULT);
- ATOMIC_ATTRIBUTE("Obstacle Avoidance Type", unsigned, obstacleAvoidanceType_, DEFAULT_AGENT_OBSTACLE_AVOIDANCE_TYPE, AM_DEFAULT);
- ATOMIC_ENUM_ATTRIBUTE("Navigation Pushiness", navPushiness_, crowdAgentPushinessNames, DEFAULT_AGENT_NAVIGATION_PUSHINESS, AM_DEFAULT);
- ATOMIC_ENUM_ATTRIBUTE("Navigation Quality", navQuality_, crowdAgentAvoidanceQualityNames, DEFAULT_AGENT_AVOIDANCE_QUALITY, AM_DEFAULT);
- }
- void CrowdAgent::ApplyAttributes()
- {
- // Values from Editor, saved-file, or network must be checked before applying
- maxAccel_ = Max(0.f, maxAccel_);
- maxSpeed_ = Max(0.f, maxSpeed_);
- radius_ = Max(0.f, radius_);
- height_ = Max(0.f, height_);
- queryFilterType_ = Min(queryFilterType_, (unsigned)DT_CROWD_MAX_QUERY_FILTER_TYPE - 1);
- obstacleAvoidanceType_ = Min(obstacleAvoidanceType_, (unsigned)DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS - 1);
- UpdateParameters();
- // Set or reset target after we have attributes applied to the agent's parameters.
- CrowdAgentRequestedTarget requestedTargetType = requestedTargetType_;
- if (CA_REQUESTEDTARGET_NONE != requestedTargetType_)
- {
- // Assign a dummy value such that the value check in the setter method passes
- requestedTargetType_ = CA_REQUESTEDTARGET_NONE;
- if (requestedTargetType == CA_REQUESTEDTARGET_POSITION)
- SetTargetPosition(targetPosition_);
- else
- SetTargetVelocity(targetVelocity_);
- }
- else
- {
- requestedTargetType_ = CA_REQUESTEDTARGET_POSITION;
- ResetTarget();
- }
- }
- void CrowdAgent::OnSetEnabled()
- {
- bool enabled = IsEnabledEffective();
- if (enabled && !IsInCrowd())
- AddAgentToCrowd();
- else if (!enabled && IsInCrowd())
- RemoveAgentFromCrowd();
- }
- void CrowdAgent::DrawDebugGeometry(bool depthTest)
- {
- Scene* scene = GetScene();
- if (scene)
- {
- DebugRenderer* debug = scene->GetComponent<DebugRenderer>();
- if (debug)
- DrawDebugGeometry(debug, depthTest);
- }
- }
- void CrowdAgent::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
- {
- if (node_)
- {
- const Vector3 pos = GetPosition();
- const Vector3 vel = GetActualVelocity();
- const Vector3 desiredVel = GetDesiredVelocity();
- const Vector3 agentHeightVec(0, height_, 0);
- debug->AddLine(pos + 0.5f * agentHeightVec, pos + vel + 0.5f * agentHeightVec, Color::GREEN, depthTest);
- debug->AddLine(pos + 0.25f * agentHeightVec, pos + desiredVel + 0.25f * agentHeightVec, Color::RED, depthTest);
- debug->AddCylinder(pos, radius_, height_, HasArrived() ? Color::GREEN : Color::WHITE, depthTest);
- }
- }
- void CrowdAgent::UpdateParameters(unsigned scope)
- {
- const dtCrowdAgent* agent = GetDetourCrowdAgent();
- if (agent)
- {
- dtCrowdAgentParams params = agent->params;
- if (scope & SCOPE_NAVIGATION_QUALITY_PARAMS)
- {
- switch (navQuality_)
- {
- case NAVIGATIONQUALITY_LOW:
- params.updateFlags = 0
- | DT_CROWD_OPTIMIZE_VIS
- | DT_CROWD_ANTICIPATE_TURNS;
- break;
- case NAVIGATIONQUALITY_MEDIUM:
- params.updateFlags = 0
- | DT_CROWD_OPTIMIZE_TOPO
- | DT_CROWD_OPTIMIZE_VIS
- | DT_CROWD_ANTICIPATE_TURNS
- | DT_CROWD_SEPARATION;
- break;
- case NAVIGATIONQUALITY_HIGH:
- params.updateFlags = 0
- // Path finding
- | DT_CROWD_OPTIMIZE_TOPO
- | DT_CROWD_OPTIMIZE_VIS
- // Steering
- | DT_CROWD_ANTICIPATE_TURNS
- | DT_CROWD_SEPARATION
- // Velocity planning
- | DT_CROWD_OBSTACLE_AVOIDANCE;
- break;
- }
- }
- if (scope & SCOPE_NAVIGATION_PUSHINESS_PARAMS)
- {
- switch (navPushiness_)
- {
- case NAVIGATIONPUSHINESS_LOW:
- params.separationWeight = 4.0f;
- params.collisionQueryRange = radius_ * 16.0f;
- break;
- case NAVIGATIONPUSHINESS_MEDIUM:
- params.separationWeight = 2.0f;
- params.collisionQueryRange = radius_ * 8.0f;
- break;
- case NAVIGATIONPUSHINESS_HIGH:
- params.separationWeight = 0.5f;
- params.collisionQueryRange = radius_ * 1.0f;
- break;
- case NAVIGATIONPUSHINESS_NONE:
- params.separationWeight = 0.0f;
- params.collisionQueryRange = radius_ * 1.0f;
- break;
- }
- }
- if (scope & SCOPE_BASE_PARAMS)
- {
- params.radius = radius_;
- params.height = height_;
- params.maxAcceleration = maxAccel_;
- params.maxSpeed = maxSpeed_;
- params.pathOptimizationRange = radius_ * 30.0f;
- params.queryFilterType = (unsigned char)queryFilterType_;
- params.obstacleAvoidanceType = (unsigned char)obstacleAvoidanceType_;
- }
- crowdManager_->GetCrowd()->updateAgentParameters(agentCrowdId_, ¶ms);
- }
- }
- int CrowdAgent::AddAgentToCrowd(bool force)
- {
- if (!node_ || !crowdManager_ || !crowdManager_->crowd_)
- return -1;
- if (force || !IsInCrowd())
- {
- ATOMIC_PROFILE(AddAgentToCrowd);
- agentCrowdId_ = crowdManager_->AddAgent(this, node_->GetWorldPosition());
- if (agentCrowdId_ == -1)
- return -1;
- ApplyAttributes();
- previousAgentState_ = GetAgentState();
- previousTargetState_ = GetTargetState();
- // Agent created, but initial state is invalid and needs to be addressed
- if (previousAgentState_ == CA_STATE_INVALID)
- {
- using namespace CrowdAgentFailure;
- VariantMap& map = GetEventDataMap();
- map[P_NODE] = GetNode();
- map[P_CROWD_AGENT] = this;
- map[P_CROWD_TARGET_STATE] = previousTargetState_;
- map[P_CROWD_AGENT_STATE] = previousAgentState_;
- map[P_POSITION] = GetPosition();
- map[P_VELOCITY] = GetActualVelocity();
- crowdManager_->SendEvent(E_CROWD_AGENT_FAILURE, map);
- Node* node = GetNode();
- if (node)
- node->SendEvent(E_CROWD_AGENT_NODE_FAILURE, map);
- // Reevaluate states as handling of event may have resulted in changes
- previousAgentState_ = GetAgentState();
- previousTargetState_ = GetTargetState();
- }
- // Save the initial position to prevent CrowdAgentReposition event being triggered unnecessarily
- previousPosition_ = GetPosition();
- }
- return agentCrowdId_;
- }
- void CrowdAgent::RemoveAgentFromCrowd()
- {
- if (IsInCrowd())
- {
- crowdManager_->RemoveAgent(this);
- agentCrowdId_ = -1;
- }
- }
- void CrowdAgent::SetTargetPosition(const Vector3& position)
- {
- if (position != targetPosition_ || CA_REQUESTEDTARGET_POSITION != requestedTargetType_)
- {
- targetPosition_ = position;
- requestedTargetType_ = CA_REQUESTEDTARGET_POSITION;
- MarkNetworkUpdate();
- if (!IsInCrowd())
- AddAgentToCrowd();
- if (IsInCrowd()) // Make sure the previous method call is successful
- {
- dtPolyRef nearestRef;
- Vector3 nearestPos = crowdManager_->FindNearestPoint(position, queryFilterType_, &nearestRef);
- crowdManager_->GetCrowd()->requestMoveTarget(agentCrowdId_, nearestRef, nearestPos.Data());
- }
- }
- }
- void CrowdAgent::SetTargetVelocity(const Vector3& velocity)
- {
- if (velocity != targetVelocity_ || CA_REQUESTEDTARGET_VELOCITY != requestedTargetType_)
- {
- targetVelocity_ = velocity;
- requestedTargetType_ = CA_REQUESTEDTARGET_VELOCITY;
- MarkNetworkUpdate();
- if (IsInCrowd())
- crowdManager_->GetCrowd()->requestMoveVelocity(agentCrowdId_, velocity.Data());
- }
- }
- void CrowdAgent::ResetTarget()
- {
- if (CA_REQUESTEDTARGET_NONE != requestedTargetType_)
- {
- requestedTargetType_ = CA_REQUESTEDTARGET_NONE;
- MarkNetworkUpdate();
- if (IsInCrowd())
- crowdManager_->GetCrowd()->resetMoveTarget(agentCrowdId_);
- }
- }
- void CrowdAgent::SetUpdateNodePosition(bool unodepos)
- {
- if (unodepos != updateNodePosition_)
- {
- updateNodePosition_ = unodepos;
- MarkNetworkUpdate();
- }
- }
- void CrowdAgent::SetMaxAccel(float maxAccel)
- {
- if (maxAccel != maxAccel_ && maxAccel >= 0.f)
- {
- maxAccel_ = maxAccel;
- UpdateParameters(SCOPE_BASE_PARAMS);
- MarkNetworkUpdate();
- }
- }
- void CrowdAgent::SetMaxSpeed(float maxSpeed)
- {
- if (maxSpeed != maxSpeed_ && maxSpeed >= 0.f)
- {
- maxSpeed_ = maxSpeed;
- UpdateParameters(SCOPE_BASE_PARAMS);
- MarkNetworkUpdate();
- }
- }
- void CrowdAgent::SetRadius(float radius)
- {
- if (radius != radius_ && radius > 0.f)
- {
- radius_ = radius;
- UpdateParameters(SCOPE_BASE_PARAMS | SCOPE_NAVIGATION_PUSHINESS_PARAMS);
- MarkNetworkUpdate();
- }
- }
- void CrowdAgent::SetHeight(float height)
- {
- if (height != height_ && height > 0.f)
- {
- height_ = height;
- UpdateParameters(SCOPE_BASE_PARAMS);
- MarkNetworkUpdate();
- }
- }
- void CrowdAgent::SetQueryFilterType(unsigned queryFilterType)
- {
- if (queryFilterType != queryFilterType_)
- {
- if (queryFilterType >= DT_CROWD_MAX_QUERY_FILTER_TYPE)
- {
- ATOMIC_LOGERRORF("The specified filter type index (%d) exceeds the maximum allowed value (%d)", queryFilterType,
- DT_CROWD_MAX_QUERY_FILTER_TYPE);
- return;
- }
- queryFilterType_ = queryFilterType;
- UpdateParameters(SCOPE_BASE_PARAMS);
- MarkNetworkUpdate();
- }
- }
- void CrowdAgent::SetObstacleAvoidanceType(unsigned obstacleAvoidanceType)
- {
- if (obstacleAvoidanceType != obstacleAvoidanceType_)
- {
- if (obstacleAvoidanceType >= DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS)
- {
- ATOMIC_LOGERRORF("The specified obstacle avoidance type index (%d) exceeds the maximum allowed value (%d)",
- obstacleAvoidanceType, DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS);
- return;
- }
- obstacleAvoidanceType_ = obstacleAvoidanceType;
- UpdateParameters(SCOPE_BASE_PARAMS);
- MarkNetworkUpdate();
- }
- }
- void CrowdAgent::SetNavigationQuality(NavigationQuality val)
- {
- if (val != navQuality_)
- {
- navQuality_ = val;
- UpdateParameters(SCOPE_NAVIGATION_QUALITY_PARAMS);
- MarkNetworkUpdate();
- }
- }
- void CrowdAgent::SetNavigationPushiness(NavigationPushiness val)
- {
- if (val != navPushiness_)
- {
- navPushiness_ = val;
- UpdateParameters(SCOPE_NAVIGATION_PUSHINESS_PARAMS);
- MarkNetworkUpdate();
- }
- }
- Vector3 CrowdAgent::GetPosition() const
- {
- const dtCrowdAgent* agent = GetDetourCrowdAgent();
- return agent ? Vector3(agent->npos) : node_->GetWorldPosition();
- }
- Vector3 CrowdAgent::GetDesiredVelocity() const
- {
- const dtCrowdAgent* agent = GetDetourCrowdAgent();
- return agent ? Vector3(agent->dvel) : Vector3::ZERO;
- }
- Vector3 CrowdAgent::GetActualVelocity() const
- {
- const dtCrowdAgent* agent = GetDetourCrowdAgent();
- return agent ? Vector3(agent->vel) : Vector3::ZERO;
- }
- CrowdAgentState CrowdAgent::GetAgentState() const
- {
- const dtCrowdAgent* agent = GetDetourCrowdAgent();
- return agent ? (CrowdAgentState)agent->state : CA_STATE_INVALID;
- }
- CrowdAgentTargetState CrowdAgent::GetTargetState() const
- {
- const dtCrowdAgent* agent = GetDetourCrowdAgent();
- return agent ? (CrowdAgentTargetState)agent->targetState : CA_TARGET_NONE;
- }
- bool CrowdAgent::HasArrived() const
- {
- // Is the agent at or near the end of its path and within its own radius of the goal?
- const dtCrowdAgent* agent = GetDetourCrowdAgent();
- return agent && (!agent->ncorners || (agent->cornerFlags[agent->ncorners - 1] & DT_STRAIGHTPATH_END &&
- dtVdist2D(agent->npos, &agent->cornerVerts[(agent->ncorners - 1) * 3]) <=
- agent->params.radius));
- }
- bool CrowdAgent::IsInCrowd() const
- {
- return crowdManager_ && agentCrowdId_ != -1;
- }
- void CrowdAgent::OnCrowdUpdate(dtCrowdAgent* ag, float dt)
- {
- assert (ag);
- if (node_)
- {
- // Use pointer to self to check for destruction after sending events
- WeakPtr<CrowdAgent> self(this);
- Vector3 newPos(ag->npos);
- Vector3 newVel(ag->vel);
- // Notify parent node of the reposition
- if (newPos != previousPosition_)
- {
- previousPosition_ = newPos;
- if (updateNodePosition_)
- {
- ignoreTransformChanges_ = true;
- node_->SetWorldPosition(newPos);
- ignoreTransformChanges_ = false;
- }
- using namespace CrowdAgentReposition;
- VariantMap& map = GetEventDataMap();
- map[P_NODE] = node_;
- map[P_CROWD_AGENT] = this;
- map[P_POSITION] = newPos;
- map[P_VELOCITY] = newVel;
- map[P_ARRIVED] = HasArrived();
- map[P_TIMESTEP] = dt;
- crowdManager_->SendEvent(E_CROWD_AGENT_REPOSITION, map);
- if (self.Expired())
- return;
- node_->SendEvent(E_CROWD_AGENT_NODE_REPOSITION, map);
- if (self.Expired())
- return;
- }
- // Send a notification event if we've reached the destination
- CrowdAgentTargetState newTargetState = GetTargetState();
- CrowdAgentState newAgentState = GetAgentState();
- if (newAgentState != previousAgentState_ || newTargetState != previousTargetState_)
- {
- using namespace CrowdAgentStateChanged;
- VariantMap& map = GetEventDataMap();
- map[P_NODE] = node_;
- map[P_CROWD_AGENT] = this;
- map[P_CROWD_TARGET_STATE] = newTargetState;
- map[P_CROWD_AGENT_STATE] = newAgentState;
- map[P_POSITION] = newPos;
- map[P_VELOCITY] = newVel;
- crowdManager_->SendEvent(E_CROWD_AGENT_STATE_CHANGED, map);
- if (self.Expired())
- return;
- node_->SendEvent(E_CROWD_AGENT_NODE_STATE_CHANGED, map);
- if (self.Expired())
- return;
- // Send a failure event if either state is a failed status
- if (newAgentState == CA_STATE_INVALID || newTargetState == CA_TARGET_FAILED)
- {
- VariantMap& map = GetEventDataMap();
- map[P_NODE] = node_;
- map[P_CROWD_AGENT] = this;
- map[P_CROWD_TARGET_STATE] = newTargetState;
- map[P_CROWD_AGENT_STATE] = newAgentState;
- map[P_POSITION] = newPos;
- map[P_VELOCITY] = newVel;
- crowdManager_->SendEvent(E_CROWD_AGENT_FAILURE, map);
- if (self.Expired())
- return;
- node_->SendEvent(E_CROWD_AGENT_NODE_FAILURE, map);
- if (self.Expired())
- return;
- }
- // State may have been altered during the handling of the event
- previousAgentState_ = GetAgentState();
- previousTargetState_ = GetTargetState();
- }
- }
- }
- void CrowdAgent::OnNodeSet(Node* node)
- {
- if (node)
- node->AddListener(this);
- }
- void CrowdAgent::OnSceneSet(Scene* scene)
- {
- if (scene)
- {
- if (scene == node_)
- ATOMIC_LOGERROR(GetTypeName() + " should not be created to the root scene node");
- crowdManager_ = scene->GetOrCreateComponent<CrowdManager>();
- AddAgentToCrowd();
- }
- else
- RemoveAgentFromCrowd();
- }
- void CrowdAgent::OnMarkedDirty(Node* node)
- {
- if (!ignoreTransformChanges_ && IsEnabledEffective())
- {
- dtCrowdAgent* agent = const_cast<dtCrowdAgent*>(GetDetourCrowdAgent());
- if (agent)
- {
- Vector3& agentPos = reinterpret_cast<Vector3&>(agent->npos);
- Vector3 nodePos = node->GetWorldPosition();
- // Only reset position / state if actually changed
- if (nodePos != agentPos)
- {
- agentPos = nodePos;
- // If the node has been externally altered, provide the opportunity for DetourCrowd to reevaluate the crowd agent
- if (agent->state == CA_STATE_INVALID)
- agent->state = CA_STATE_WALKING;
- }
- }
- }
- }
- const dtCrowdAgent* CrowdAgent::GetDetourCrowdAgent() const
- {
- return IsInCrowd() ? crowdManager_->GetDetourCrowdAgent(agentCrowdId_) : 0;
- }
- void CrowdAgent::HandleNavigationTileAdded(StringHash eventType, VariantMap& eventData)
- {
- if (!crowdManager_)
- return;
- NavigationMesh* mesh = static_cast<NavigationMesh*>(eventData[NavigationTileAdded::P_MESH].GetPtr());
- if (crowdManager_->GetNavigationMesh() != mesh)
- return;
- const IntVector2 tile = eventData[NavigationTileRemoved::P_TILE].GetIntVector2();
- const IntVector2 agentTile = mesh->GetTileIndex(node_->GetWorldPosition());
- const BoundingBox boundingBox = mesh->GetTileBoudningBox(agentTile);
- if (tile == agentTile && IsInCrowd())
- {
- RemoveAgentFromCrowd();
- AddAgentToCrowd();
- }
- }
- }
|