mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-02-10 03:39:55 +00:00
Merge branch 'cleanup_aistate' into 'master'
Cleanup MWMechanics::AiState See merge request OpenMW/openmw!2141
This commit is contained in:
commit
06db2446b1
@ -2,6 +2,7 @@
|
||||
#define GAME_MWMECHANICS_AICOMBAT_H
|
||||
|
||||
#include "typedaipackage.hpp"
|
||||
#include "aitemporarybase.hpp"
|
||||
|
||||
#include "../mwworld/cellstore.hpp" // for Doors
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define GAME_MWMECHANICS_AIFOLLOW_H
|
||||
|
||||
#include "typedaipackage.hpp"
|
||||
#include "aitemporarybase.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
@ -4,12 +4,13 @@
|
||||
#include <memory>
|
||||
|
||||
#include <components/detournavigator/areatype.hpp>
|
||||
|
||||
|
||||
#include "pathfinding.hpp"
|
||||
#include "obstacle.hpp"
|
||||
#include "aistate.hpp"
|
||||
#include "aipackagetypeid.hpp"
|
||||
#include "aitimer.hpp"
|
||||
#include "aistatefwd.hpp"
|
||||
|
||||
#include "../mwworld/ptr.hpp"
|
||||
|
||||
namespace ESM
|
||||
|
@ -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>());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,10 +27,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>>;
|
||||
|
||||
|
@ -1,7 +1,10 @@
|
||||
#ifndef AISTATE_H
|
||||
#define AISTATE_H
|
||||
|
||||
#include <typeinfo>
|
||||
#include "aistatefwd.hpp"
|
||||
#include "aitemporarybase.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace MWMechanics
|
||||
{
|
||||
@ -13,28 +16,24 @@ namespace MWMechanics
|
||||
*/
|
||||
template< class Base >
|
||||
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);
|
||||
|
||||
if(!result)
|
||||
Derived* result = dynamic_cast<Derived*>(mStorage.get());
|
||||
|
||||
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
|
||||
return *result;
|
||||
}
|
||||
@ -42,61 +41,24 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
|
15
apps/openmw/mwmechanics/aistatefwd.hpp
Normal file
15
apps/openmw/mwmechanics/aistatefwd.hpp
Normal 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
|
19
apps/openmw/mwmechanics/aitemporarybase.hpp
Normal file
19
apps/openmw/mwmechanics/aitemporarybase.hpp
Normal 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
|
@ -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);
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
#include "pathfinding.hpp"
|
||||
#include "obstacle.hpp"
|
||||
#include "aistate.hpp"
|
||||
#include "aitemporarybase.hpp"
|
||||
#include "aitimer.hpp"
|
||||
|
||||
namespace ESM
|
||||
|
Loading…
x
Reference in New Issue
Block a user