Merge branch 'ai_travel_destination' into 'master'

Ignore player when checking whether AiTravel destination is occupied by other actor (#6495)

See merge request OpenMW/openmw!1767
pull/3226/head
psi29a 3 years ago
commit 36436383a5

@ -10,6 +10,7 @@
#include <components/esm3/cellid.hpp>
#include <components/misc/rng.hpp>
#include <components/misc/span.hpp>
#include <osg/Timer>
@ -658,7 +659,7 @@ namespace MWBase
virtual bool hasCollisionWithDoor(const MWWorld::ConstPtr& door, const osg::Vec3f& position, const osg::Vec3f& destination) const = 0;
virtual bool isAreaOccupiedByOtherActor(const osg::Vec3f& position, const float radius,
const MWWorld::ConstPtr& ignore, std::vector<MWWorld::Ptr>* occupyingActors = nullptr) const = 0;
const Misc::Span<const MWWorld::ConstPtr>& ignore, std::vector<MWWorld::Ptr>* occupyingActors = nullptr) const = 0;
virtual void reportStats(unsigned int frameNumber, osg::Stats& stats) const = 0;

@ -83,7 +83,7 @@ namespace MWMechanics
if (mDestinationCheck.update(duration) == Misc::TimerStatus::Elapsed)
{
std::vector<MWWorld::Ptr> occupyingActors;
if (isAreaOccupiedByOtherActor(actor, targetPos, &occupyingActors))
if (isAreaOccupiedByOtherActor(actor, targetPos, true, &occupyingActors))
{
const float actorRadius = getActorRadius(actor);
const float distanceToTarget = distance(actorPos, targetPos);

@ -1,5 +1,7 @@
#include "obstacle.hpp"
#include <array>
#include <components/sceneutil/positionattitudetransform.hpp>
#include "../mwworld/class.hpp"
@ -74,13 +76,19 @@ namespace MWMechanics
return MWWorld::Ptr(); // none found
}
bool isAreaOccupiedByOtherActor(const MWWorld::ConstPtr& actor, const osg::Vec3f& destination,
bool isAreaOccupiedByOtherActor(const MWWorld::ConstPtr& actor, const osg::Vec3f& destination, bool ignorePlayer,
std::vector<MWWorld::Ptr>* occupyingActors)
{
const auto world = MWBase::Environment::get().getWorld();
const osg::Vec3f halfExtents = world->getPathfindingHalfExtents(actor);
const auto maxHalfExtent = std::max(halfExtents.x(), std::max(halfExtents.y(), halfExtents.z()));
return world->isAreaOccupiedByOtherActor(destination, 2 * maxHalfExtent, actor, occupyingActors);
if (ignorePlayer)
{
const std::array ignore {actor, world->getPlayerConstPtr()};
return world->isAreaOccupiedByOtherActor(destination, 2 * maxHalfExtent, ignore, occupyingActors);
}
const std::array ignore {actor};
return world->isAreaOccupiedByOtherActor(destination, 2 * maxHalfExtent, ignore, occupyingActors);
}
ObstacleCheck::ObstacleCheck()

@ -24,7 +24,7 @@ namespace MWMechanics
/** \return Pointer to the door, or empty pointer if none exists **/
const MWWorld::Ptr getNearbyDoor(const MWWorld::Ptr& actor, float minDist);
bool isAreaOccupiedByOtherActor(const MWWorld::ConstPtr& actor, const osg::Vec3f& destination,
bool isAreaOccupiedByOtherActor(const MWWorld::ConstPtr& actor, const osg::Vec3f& destination, bool ignorePlayer = false,
std::vector<MWWorld::Ptr>* occupyingActors = nullptr);
class ObstacleCheck

@ -22,15 +22,15 @@ namespace MWPhysics
return nearest.distance(position) < radius;
}
template <class OnCollision>
template <class Ignore, class OnCollision>
class HasSphereCollisionCallback final : public btBroadphaseAabbCallback
{
public:
HasSphereCollisionCallback(const btVector3& position, const btScalar radius, btCollisionObject* object,
const int mask, const int group, OnCollision* onCollision)
HasSphereCollisionCallback(const btVector3& position, const btScalar radius, const int mask, const int group,
const Ignore& ignore, OnCollision* onCollision)
: mPosition(position),
mRadius(radius),
mCollisionObject(object),
mIgnore(ignore),
mCollisionFilterMask(mask),
mCollisionFilterGroup(group),
mOnCollision(onCollision)
@ -42,7 +42,7 @@ namespace MWPhysics
if (mResult && mOnCollision == nullptr)
return false;
const auto collisionObject = static_cast<btCollisionObject*>(proxy->m_clientObject);
if (collisionObject == mCollisionObject
if (mIgnore(collisionObject)
|| !needsCollision(*proxy)
|| !testAabbAgainstSphere(proxy->m_aabbMin, proxy->m_aabbMax, mPosition, mRadius))
return true;
@ -63,7 +63,7 @@ namespace MWPhysics
private:
btVector3 mPosition;
btScalar mRadius;
btCollisionObject* mCollisionObject;
Ignore mIgnore;
int mCollisionFilterMask;
int mCollisionFilterGroup;
OnCollision* mOnCollision;

@ -2,7 +2,11 @@
#include <LinearMath/btIDebugDraw.h>
#include <LinearMath/btVector3.h>
#include <memory>
#include <algorithm>
#include <vector>
#include <osg/Group>
#include <osg/Stats>
#include <osg/Timer>
@ -884,12 +888,19 @@ namespace MWPhysics
}
bool PhysicsSystem::isAreaOccupiedByOtherActor(const osg::Vec3f& position, const float radius,
const MWWorld::ConstPtr& ignore, std::vector<MWWorld::Ptr>* occupyingActors) const
{
btCollisionObject* object = nullptr;
const auto it = mActors.find(ignore.mRef);
if (it != mActors.end())
object = it->second->getCollisionObject();
const Misc::Span<const MWWorld::ConstPtr>& ignore, std::vector<MWWorld::Ptr>* occupyingActors) const
{
std::vector<const btCollisionObject*> ignoredObjects;
ignoredObjects.reserve(ignore.size());
for (const auto& v : ignore)
if (const auto it = mActors.find(v.mRef); it != mActors.end())
ignoredObjects.push_back(it->second->getCollisionObject());
std::sort(ignoredObjects.begin(), ignoredObjects.end());
ignoredObjects.erase(std::unique(ignoredObjects.begin(), ignoredObjects.end()), ignoredObjects.end());
const auto ignoreFilter = [&] (const btCollisionObject* v)
{
return std::binary_search(ignoredObjects.begin(), ignoredObjects.end(), v);
};
const auto bulletPosition = Misc::Convert::toBullet(position);
const auto aabbMin = bulletPosition - btVector3(radius, radius, radius);
const auto aabbMax = bulletPosition + btVector3(radius, radius, radius);
@ -897,7 +908,7 @@ namespace MWPhysics
const int group = 0xff;
if (occupyingActors == nullptr)
{
HasSphereCollisionCallback callback(bulletPosition, radius, object, mask, group,
HasSphereCollisionCallback callback(bulletPosition, radius, mask, group, ignoreFilter,
static_cast<void (*)(const btCollisionObject*)>(nullptr));
mTaskScheduler->aabbTest(aabbMin, aabbMax, callback);
return callback.getResult();
@ -907,7 +918,7 @@ namespace MWPhysics
if (PtrHolder* holder = static_cast<PtrHolder*>(object->getUserPointer()))
occupyingActors->push_back(holder->getPtr());
};
HasSphereCollisionCallback callback(bulletPosition, radius, object, mask, group, &onCollision);
HasSphereCollisionCallback callback(bulletPosition, radius, mask, group, ignoreFilter, &onCollision);
mTaskScheduler->aabbTest(aabbMin, aabbMax, callback);
return callback.getResult();
}

@ -16,6 +16,8 @@
#include <osg/ref_ptr>
#include <osg/Timer>
#include <components/misc/span.hpp>
#include "../mwworld/ptr.hpp"
#include "collisiontype.hpp"
@ -277,7 +279,7 @@ namespace MWPhysics
}
bool isAreaOccupiedByOtherActor(const osg::Vec3f& position, const float radius,
const MWWorld::ConstPtr& ignore, std::vector<MWWorld::Ptr>* occupyingActors) const;
const Misc::Span<const MWWorld::ConstPtr>& ignore, std::vector<MWWorld::Ptr>* occupyingActors) const;
void reportStats(unsigned int frameNumber, osg::Stats& stats) const;
void reportCollision(const btVector3& position, const btVector3& normal);

@ -3997,7 +3997,7 @@ namespace MWWorld
}
bool World::isAreaOccupiedByOtherActor(const osg::Vec3f& position, const float radius,
const MWWorld::ConstPtr& ignore, std::vector<MWWorld::Ptr>* occupyingActors) const
const Misc::Span<const MWWorld::ConstPtr>& ignore, std::vector<MWWorld::Ptr>* occupyingActors) const
{
return mPhysics->isAreaOccupiedByOtherActor(position, radius, ignore, occupyingActors);
}

@ -736,7 +736,7 @@ namespace MWWorld
bool hasCollisionWithDoor(const MWWorld::ConstPtr& door, const osg::Vec3f& position, const osg::Vec3f& destination) const override;
bool isAreaOccupiedByOtherActor(const osg::Vec3f& position, const float radius,
const MWWorld::ConstPtr& ignore, std::vector<MWWorld::Ptr>* occupyingActors) const override;
const Misc::Span<const MWWorld::ConstPtr>& ignore, std::vector<MWWorld::Ptr>* occupyingActors) const override;
void reportStats(unsigned int frameNumber, osg::Stats& stats) const override;

@ -0,0 +1,36 @@
#ifndef OPENMW_COMPONENTS_MISC_SPAN_H
#define OPENMW_COMPONENTS_MISC_SPAN_H
#include <cstddef>
namespace Misc
{
template <class T>
class Span
{
public:
constexpr Span() = default;
constexpr Span(T* pointer, std::size_t size)
: mPointer(pointer)
, mSize(size)
{}
template <class Range>
constexpr Span(Range& range)
: Span(range.data(), range.size())
{}
constexpr T* begin() const { return mPointer; }
constexpr T* end() const { return mPointer + mSize; }
constexpr std::size_t size() const { return mSize; }
private:
T* mPointer = nullptr;
std::size_t mSize = 0;
};
}
#endif
Loading…
Cancel
Save