1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-10 21:40:15 +00:00

Merge branch 'cleanup_aistate' into 'master'

Cleanup MWMechanics::AiState

See merge request OpenMW/openmw!2141
This commit is contained in:
psi29a 2022-07-17 09:13:04 +00:00
commit 06db2446b1
10 changed files with 65 additions and 70 deletions

View File

@ -2,6 +2,7 @@
#define GAME_MWMECHANICS_AICOMBAT_H
#include "typedaipackage.hpp"
#include "aitemporarybase.hpp"
#include "../mwworld/cellstore.hpp" // for Doors

View File

@ -2,6 +2,7 @@
#define GAME_MWMECHANICS_AIFOLLOW_H
#include "typedaipackage.hpp"
#include "aitemporarybase.hpp"
#include <string>
#include <string_view>

View File

@ -7,9 +7,10 @@
#include "pathfinding.hpp"
#include "obstacle.hpp"
#include "aistate.hpp"
#include "aipackagetypeid.hpp"
#include "aitimer.hpp"
#include "aistatefwd.hpp"
#include "../mwworld/ptr.hpp"
namespace ESM

View File

@ -419,9 +419,9 @@ void AiSequence::stack (const AiPackage& package, const MWWorld::Ptr& actor, boo
// Make sure that temporary storage is empty
if (cancelOther)
{
mAiState.moveIn(new AiCombatStorage());
mAiState.moveIn(new AiFollowStorage());
mAiState.moveIn(new AiWanderStorage());
mAiState.moveIn(std::make_unique<AiCombatStorage>());
mAiState.moveIn(std::make_unique<AiFollowStorage>());
mAiState.moveIn(std::make_unique<AiWanderStorage>());
}
}

View File

@ -28,10 +28,6 @@ namespace MWMechanics
class AiPackage;
class CharacterController;
template< class Base > class DerivedClassStorage;
struct AiTemporaryBase;
typedef DerivedClassStorage<AiTemporaryBase> AiState;
using AiPackages = std::vector<std::shared_ptr<AiPackage>>;
/// \brief Sequence of AI-packages for a single actor

View File

@ -1,7 +1,10 @@
#ifndef AISTATE_H
#define AISTATE_H
#include <typeinfo>
#include "aistatefwd.hpp"
#include "aitemporarybase.hpp"
#include <memory>
namespace MWMechanics
{
@ -15,24 +18,20 @@ namespace MWMechanics
class DerivedClassStorage
{
private:
Base* mStorage;
//if needed you have to provide a clone member function
DerivedClassStorage( const DerivedClassStorage& other );
DerivedClassStorage& operator=( const DerivedClassStorage& );
std::unique_ptr<Base> mStorage;
public:
/// \brief returns reference to stored object or deletes it and creates a fitting
template< class Derived >
Derived& get()
{
Derived* result = dynamic_cast<Derived*>(mStorage);
Derived* result = dynamic_cast<Derived*>(mStorage.get());
if(!result)
if (result == nullptr)
{
if(mStorage)
delete mStorage;
mStorage = result = new Derived();
auto storage = std::make_unique<Derived>();
result = storage.get();
mStorage = std::move(storage);
}
//return a reference to the (new allocated) object
@ -42,7 +41,7 @@ namespace MWMechanics
template< class Derived >
void copy(DerivedClassStorage& destination) const
{
Derived* result = dynamic_cast<Derived*>(mStorage);
Derived* result = dynamic_cast<Derived*>(mStorage.get());
if (result != nullptr)
destination.store<Derived>(*result);
}
@ -50,53 +49,16 @@ namespace MWMechanics
template< class Derived >
void store( const Derived& payload )
{
if(mStorage)
delete mStorage;
mStorage = new Derived(payload);
mStorage = std::make_unique<Derived>(payload);
}
/// \brief takes ownership of the passed object
template< class Derived >
void moveIn( Derived* p )
template <class Derived>
void moveIn(std::unique_ptr<Derived>&& storage)
{
if(mStorage)
delete mStorage;
mStorage = p;
}
bool empty() const
{
return mStorage == nullptr;
}
const std::type_info& getType() const
{
return typeid(mStorage);
}
DerivedClassStorage():mStorage(nullptr){}
~DerivedClassStorage()
{
if(mStorage)
delete mStorage;
mStorage = std::move(storage);
}
};
/// \brief base class for the temporary storage of AiPackages.
/**
* Each AI package with temporary values needs a AiPackageStorage class
* which is derived from AiTemporaryBase. The Actor holds a container
* AiState where one of these storages can be stored at a time.
* The execute(...) member function takes this container as an argument.
* */
struct AiTemporaryBase
{
virtual ~AiTemporaryBase(){}
};
/// \brief Container for AI package status.
typedef DerivedClassStorage<AiTemporaryBase> AiState;
}
#endif // AISTATE_H

View File

@ -0,0 +1,15 @@
#ifndef OPENMW_MWMECHANICS_AISTATEFWD_H
#define OPENMW_MWMECHANICS_AISTATEFWD_H
namespace MWMechanics
{
template <class Base>
class DerivedClassStorage;
struct AiTemporaryBase;
/// \brief Container for AI package status.
using AiState = DerivedClassStorage<AiTemporaryBase>;
}
#endif

View File

@ -0,0 +1,19 @@
#ifndef OPENMW_MWMECHANICS_AISTATE_H
#define OPENMW_MWMECHANICS_AISTATE_H
namespace MWMechanics
{
/// \brief base class for the temporary storage of AiPackages.
/**
* Each AI package with temporary values needs a AiPackageStorage class
* which is derived from AiTemporaryBase. The Actor holds a container
* AiState where one of these storages can be stored at a time.
* The execute(...) member function takes this container as an argument.
* */
struct AiTemporaryBase
{
virtual ~AiTemporaryBase() = default;
};
}
#endif

View File

@ -767,7 +767,7 @@ namespace MWMechanics
ToWorldCoordinates(dest, actor.getCell()->getCell());
state.moveIn(new AiWanderStorage());
state.moveIn(std::make_unique<AiWanderStorage>());
osg::Vec3f pos(static_cast<float>(dest.mX), static_cast<float>(dest.mY), static_cast<float>(dest.mZ));
MWBase::Environment::get().getWorld()->moveObject(actor, pos);

View File

@ -7,7 +7,7 @@
#include "pathfinding.hpp"
#include "obstacle.hpp"
#include "aistate.hpp"
#include "aitemporarybase.hpp"
#include "aitimer.hpp"
namespace ESM