1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-02-28 12:09:46 +00:00

Merge pull request #2936 from akortunov/refactoring

Use more C++11 in the physics code
This commit is contained in:
Alexei Dobrohotov 2020-06-26 19:44:16 +03:00 committed by GitHub
commit c94d6336b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 44 deletions

View file

@ -84,12 +84,12 @@ namespace MWPhysics
assert (mShapeInstance->getCollisionShape()->isCompound()); assert (mShapeInstance->getCollisionShape()->isCompound());
btCompoundShape* compound = static_cast<btCompoundShape*>(mShapeInstance->getCollisionShape()); btCompoundShape* compound = static_cast<btCompoundShape*>(mShapeInstance->getCollisionShape());
for (std::map<int, int>::const_iterator it = mShapeInstance->mAnimatedShapes.begin(); it != mShapeInstance->mAnimatedShapes.end(); ++it) for (const auto& shape : mShapeInstance->mAnimatedShapes)
{ {
int recIndex = it->first; int recIndex = shape.first;
int shapeIndex = it->second; int shapeIndex = shape.second;
std::map<int, osg::NodePath>::iterator nodePathFound = mRecIndexToNodePath.find(recIndex); auto nodePathFound = mRecIndexToNodePath.find(recIndex);
if (nodePathFound == mRecIndexToNodePath.end()) if (nodePathFound == mRecIndexToNodePath.end())
{ {
NifOsg::FindGroupByRecIndex visitor(recIndex); NifOsg::FindGroupByRecIndex visitor(recIndex);
@ -104,7 +104,7 @@ namespace MWPhysics
} }
osg::NodePath nodePath = visitor.mFoundPath; osg::NodePath nodePath = visitor.mFoundPath;
nodePath.erase(nodePath.begin()); nodePath.erase(nodePath.begin());
nodePathFound = mRecIndexToNodePath.insert(std::make_pair(recIndex, nodePath)).first; nodePathFound = mRecIndexToNodePath.emplace(recIndex, nodePath).first;
} }
osg::NodePath& nodePath = nodePathFound->second; osg::NodePath& nodePath = nodePathFound->second;

View file

@ -95,21 +95,21 @@ namespace MWPhysics
if (mWaterCollisionObject.get()) if (mWaterCollisionObject.get())
mCollisionWorld->removeCollisionObject(mWaterCollisionObject.get()); mCollisionWorld->removeCollisionObject(mWaterCollisionObject.get());
for (HeightFieldMap::iterator it = mHeightFields.begin(); it != mHeightFields.end(); ++it) for (auto& heightField : mHeightFields)
{ {
mCollisionWorld->removeCollisionObject(it->second->getCollisionObject()); mCollisionWorld->removeCollisionObject(heightField.second->getCollisionObject());
delete it->second; delete heightField.second;
} }
for (ObjectMap::iterator it = mObjects.begin(); it != mObjects.end(); ++it) for (auto& object : mObjects)
{ {
mCollisionWorld->removeCollisionObject(it->second->getCollisionObject()); mCollisionWorld->removeCollisionObject(object.second->getCollisionObject());
delete it->second; delete object.second;
} }
for (ActorMap::iterator it = mActors.begin(); it != mActors.end(); ++it) for (auto& actor : mActors)
{ {
delete it->second; delete actor.second;
} }
delete mCollisionWorld; delete mCollisionWorld;
@ -476,7 +476,7 @@ namespace MWPhysics
return; return;
Object *obj = new Object(ptr, shapeInstance); Object *obj = new Object(ptr, shapeInstance);
mObjects.insert(std::make_pair(ptr, obj)); mObjects.emplace(ptr, obj);
if (obj->isAnimated()) if (obj->isAnimated())
mAnimatedObjects.insert(obj); mAnimatedObjects.insert(obj);
@ -518,10 +518,10 @@ namespace MWPhysics
map.erase(found); map.erase(found);
} }
for (CollisionMap::iterator it = map.begin(); it != map.end(); ++it) for (auto& collision : map)
{ {
if (it->second == old) if (collision.second == old)
it->second = updated; collision.second = updated;
} }
} }
@ -533,7 +533,7 @@ namespace MWPhysics
Object* obj = found->second; Object* obj = found->second;
obj->updatePtr(updated); obj->updatePtr(updated);
mObjects.erase(found); mObjects.erase(found);
mObjects.insert(std::make_pair(updated, obj)); mObjects.emplace(updated, obj);
} }
ActorMap::iterator foundActor = mActors.find(old); ActorMap::iterator foundActor = mActors.find(old);
@ -542,7 +542,7 @@ namespace MWPhysics
Actor* actor = foundActor->second; Actor* actor = foundActor->second;
actor->updatePtr(updated); actor->updatePtr(updated);
mActors.erase(foundActor); mActors.erase(foundActor);
mActors.insert(std::make_pair(updated, actor)); mActors.emplace(updated, actor);
} }
updateCollisionMapPtr(mStandingCollisions, old, updated); updateCollisionMapPtr(mStandingCollisions, old, updated);
@ -630,7 +630,8 @@ namespace MWPhysics
} }
} }
void PhysicsSystem::addActor (const MWWorld::Ptr& ptr, const std::string& mesh) { void PhysicsSystem::addActor (const MWWorld::Ptr& ptr, const std::string& mesh)
{
osg::ref_ptr<const Resource::BulletShape> shape = mShapeManager->getShape(mesh); osg::ref_ptr<const Resource::BulletShape> shape = mShapeManager->getShape(mesh);
if (!shape) if (!shape)
return; return;
@ -646,7 +647,7 @@ namespace MWPhysics
} }
Actor* actor = new Actor(ptr, shape, mCollisionWorld); Actor* actor = new Actor(ptr, shape, mCollisionWorld);
mActors.insert(std::make_pair(ptr, actor)); mActors.emplace(ptr, actor);
} }
bool PhysicsSystem::toggleCollisionMode() bool PhysicsSystem::toggleCollisionMode()
@ -666,17 +667,16 @@ namespace MWPhysics
void PhysicsSystem::queueObjectMovement(const MWWorld::Ptr &ptr, const osg::Vec3f &movement) void PhysicsSystem::queueObjectMovement(const MWWorld::Ptr &ptr, const osg::Vec3f &movement)
{ {
PtrVelocityList::iterator iter = mMovementQueue.begin(); for(auto& movementItem : mMovementQueue)
for(;iter != mMovementQueue.end();++iter)
{ {
if(iter->first == ptr) if (movementItem.first == ptr)
{ {
iter->second = movement; movementItem.second = movement;
return; return;
} }
} }
mMovementQueue.push_back(std::make_pair(ptr, movement)); mMovementQueue.emplace_back(ptr, movement);
} }
void PhysicsSystem::clearQueuedMovement() void PhysicsSystem::clearQueuedMovement()
@ -705,27 +705,26 @@ namespace MWPhysics
const MWWorld::Ptr player = MWMechanics::getPlayer(); const MWWorld::Ptr player = MWMechanics::getPlayer();
const MWBase::World *world = MWBase::Environment::get().getWorld(); const MWBase::World *world = MWBase::Environment::get().getWorld();
PtrVelocityList::iterator iter = mMovementQueue.begin(); for(auto& movementItem : mMovementQueue)
for(;iter != mMovementQueue.end();++iter)
{ {
ActorMap::iterator foundActor = mActors.find(iter->first); ActorMap::iterator foundActor = mActors.find(movementItem.first);
if (foundActor == mActors.end()) // actor was already removed from the scene if (foundActor == mActors.end()) // actor was already removed from the scene
continue; continue;
Actor* physicActor = foundActor->second; Actor* physicActor = foundActor->second;
float waterlevel = -std::numeric_limits<float>::max(); float waterlevel = -std::numeric_limits<float>::max();
const MWWorld::CellStore *cell = iter->first.getCell(); const MWWorld::CellStore *cell = movementItem.first.getCell();
if(cell->getCell()->hasWater()) if(cell->getCell()->hasWater())
waterlevel = cell->getWaterLevel(); waterlevel = cell->getWaterLevel();
const MWMechanics::MagicEffects& effects = iter->first.getClass().getCreatureStats(iter->first).getMagicEffects(); const MWMechanics::MagicEffects& effects = movementItem.first.getClass().getCreatureStats(movementItem.first).getMagicEffects();
bool waterCollision = false; bool waterCollision = false;
if (cell->getCell()->hasWater() && effects.get(ESM::MagicEffect::WaterWalking).getMagnitude()) if (cell->getCell()->hasWater() && effects.get(ESM::MagicEffect::WaterWalking).getMagnitude())
{ {
if (!world->isUnderwater(iter->first.getCell(), osg::Vec3f(iter->first.getRefData().getPosition().asVec3()))) if (!world->isUnderwater(movementItem.first.getCell(), osg::Vec3f(movementItem.first.getRefData().getPosition().asVec3())))
waterCollision = true; waterCollision = true;
else if (physicActor->getCollisionMode() && canMoveToWaterSurface(iter->first, waterlevel)) else if (physicActor->getCollisionMode() && canMoveToWaterSurface(movementItem.first, waterlevel))
{ {
const osg::Vec3f actorPosition = physicActor->getPosition(); const osg::Vec3f actorPosition = physicActor->getPosition();
physicActor->setPosition(osg::Vec3f(actorPosition.x(), actorPosition.y(), waterlevel)); physicActor->setPosition(osg::Vec3f(actorPosition.x(), actorPosition.y(), waterlevel));
@ -737,8 +736,8 @@ namespace MWPhysics
// Slow fall reduces fall speed by a factor of (effect magnitude / 200) // Slow fall reduces fall speed by a factor of (effect magnitude / 200)
float slowFall = 1.f - std::max(0.f, std::min(1.f, effects.get(ESM::MagicEffect::SlowFall).getMagnitude() * 0.005f)); float slowFall = 1.f - std::max(0.f, std::min(1.f, effects.get(ESM::MagicEffect::SlowFall).getMagnitude() * 0.005f));
bool flying = world->isFlying(iter->first); bool flying = world->isFlying(movementItem.first);
bool swimming = world->isSwimming(iter->first); bool swimming = world->isSwimming(movementItem.first);
bool wasOnGround = physicActor->getOnGround(); bool wasOnGround = physicActor->getOnGround();
osg::Vec3f position = physicActor->getPosition(); osg::Vec3f position = physicActor->getPosition();
@ -746,7 +745,7 @@ namespace MWPhysics
bool positionChanged = false; bool positionChanged = false;
for (int i=0; i<numSteps; ++i) for (int i=0; i<numSteps; ++i)
{ {
position = MovementSolver::move(position, physicActor->getPtr(), physicActor, iter->second, mPhysicsDt, position = MovementSolver::move(position, physicActor->getPtr(), physicActor, movementItem.second, mPhysicsDt,
flying, waterlevel, slowFall, mCollisionWorld, mStandingCollisions); flying, waterlevel, slowFall, mCollisionWorld, mStandingCollisions);
if (position != physicActor->getPosition()) if (position != physicActor->getPosition())
positionChanged = true; positionChanged = true;
@ -760,14 +759,14 @@ namespace MWPhysics
float heightDiff = position.z() - oldHeight; float heightDiff = position.z() - oldHeight;
MWMechanics::CreatureStats& stats = iter->first.getClass().getCreatureStats(iter->first); MWMechanics::CreatureStats& stats = movementItem.first.getClass().getCreatureStats(movementItem.first);
bool isStillOnGround = (numSteps > 0 && wasOnGround && physicActor->getOnGround()); bool isStillOnGround = (numSteps > 0 && wasOnGround && physicActor->getOnGround());
if (isStillOnGround || flying || swimming || slowFall < 1) if (isStillOnGround || flying || swimming || slowFall < 1)
stats.land(iter->first == player && (flying || swimming)); stats.land(movementItem.first == player && (flying || swimming));
else if (heightDiff < 0) else if (heightDiff < 0)
stats.addToFallHeight(-heightDiff); stats.addToFallHeight(-heightDiff);
mMovementResults.push_back(std::make_pair(iter->first, interpolated)); mMovementResults.emplace_back(movementItem.first, interpolated);
} }
mMovementQueue.clear(); mMovementQueue.clear();
@ -801,9 +800,9 @@ namespace MWPhysics
bool PhysicsSystem::isActorStandingOn(const MWWorld::Ptr &actor, const MWWorld::ConstPtr &object) const bool PhysicsSystem::isActorStandingOn(const MWWorld::Ptr &actor, const MWWorld::ConstPtr &object) const
{ {
for (CollisionMap::const_iterator it = mStandingCollisions.begin(); it != mStandingCollisions.end(); ++it) for (const auto& standingActor : mStandingCollisions)
{ {
if (it->first == actor && it->second == object) if (standingActor.first == actor && standingActor.second == object)
return true; return true;
} }
return false; return false;
@ -811,10 +810,10 @@ namespace MWPhysics
void PhysicsSystem::getActorsStandingOn(const MWWorld::ConstPtr &object, std::vector<MWWorld::Ptr> &out) const void PhysicsSystem::getActorsStandingOn(const MWWorld::ConstPtr &object, std::vector<MWWorld::Ptr> &out) const
{ {
for (CollisionMap::const_iterator it = mStandingCollisions.begin(); it != mStandingCollisions.end(); ++it) for (const auto& standingActor : mStandingCollisions)
{ {
if (it->second == object) if (standingActor.second == object)
out.push_back(it->first); out.push_back(standingActor.first);
} }
} }