2012-11-15 21:22:44 +00:00
# include "aiactivate.hpp"
2014-02-05 15:12:50 +00:00
2014-06-12 21:27:04 +00:00
# include <components/esm/aisequence.hpp>
2014-02-05 15:12:50 +00:00
# include "../mwbase/world.hpp"
# include "../mwbase/environment.hpp"
2014-02-23 19:11:05 +00:00
2014-02-05 15:12:50 +00:00
# include "../mwworld/class.hpp"
2016-06-17 14:07:16 +00:00
# include "creaturestats.hpp"
2014-02-17 09:50:10 +00:00
# include "steering.hpp"
2014-02-23 19:11:05 +00:00
# include "movement.hpp"
2014-02-05 15:12:50 +00:00
2016-06-17 11:50:46 +00:00
namespace MWMechanics
2014-01-07 00:12:37 +00:00
{
2016-06-17 11:50:46 +00:00
AiActivate : : AiActivate ( const std : : string & objectId )
: mObjectId ( objectId )
{
}
2014-06-12 21:27:04 +00:00
2016-06-17 11:50:46 +00:00
AiActivate * MWMechanics : : AiActivate : : clone ( ) const
{
return new AiActivate ( * this ) ;
}
2014-07-28 15:28:00 +00:00
2016-08-14 16:02:13 +00:00
bool AiActivate : : execute ( const MWWorld : : Ptr & actor , CharacterController & characterController , AiState & state , float duration )
2016-06-17 11:50:46 +00:00
{
2016-08-14 16:02:13 +00:00
ESM : : Position pos = actor . getRefData ( ) . getPosition ( ) ; //position of the actor
2016-06-17 11:50:46 +00:00
const MWWorld : : Ptr target = MWBase : : Environment : : get ( ) . getWorld ( ) - > searchPtr ( mObjectId , false ) ; //The target to follow
2014-07-28 15:28:00 +00:00
2016-06-17 11:50:46 +00:00
actor . getClass ( ) . getCreatureStats ( actor ) . setDrawState ( DrawState_Nothing ) ;
2014-06-12 21:27:04 +00:00
2016-08-14 16:02:13 +00:00
if ( target = = MWWorld : : Ptr ( ) | |
! target . getRefData ( ) . getCount ( ) | | ! target . getRefData ( ) . isEnabled ( ) // Really we should be checking whether the target is currently registered
2016-06-17 11:50:46 +00:00
// with the MechanicsManager
2016-08-14 16:02:13 +00:00
)
return true ; //Target doesn't exist
2014-06-12 21:27:04 +00:00
2016-08-14 16:02:13 +00:00
//Set the target desition from the actor
2016-06-17 11:50:46 +00:00
ESM : : Pathgrid : : Point dest = target . getRefData ( ) . getPosition ( ) . pos ;
2014-06-12 22:01:29 +00:00
2016-08-14 16:02:13 +00:00
if ( distance ( dest , pos . pos [ 0 ] , pos . pos [ 1 ] , pos . pos [ 2 ] ) < MWBase : : Environment : : get ( ) . getWorld ( ) - > getMaxActivationDistance ( ) ) { //Stop when you get in activation range
actor . getClass ( ) . getMovementSettings ( actor ) . mPosition [ 1 ] = 0 ;
2016-06-17 11:50:46 +00:00
MWWorld : : Ptr target = MWBase : : Environment : : get ( ) . getWorld ( ) - > getPtr ( mObjectId , false ) ;
2016-08-14 16:02:13 +00:00
2016-06-17 11:50:46 +00:00
MWBase : : Environment : : get ( ) . getWorld ( ) - > activate ( target , actor ) ;
2014-06-12 21:27:04 +00:00
2016-06-17 11:50:46 +00:00
return true ;
}
2016-08-14 16:02:13 +00:00
else {
pathTo ( actor , dest , duration ) ; //Go to the destination
}
2014-01-07 00:12:37 +00:00
2016-06-17 11:50:46 +00:00
return false ;
}
2014-06-12 21:27:04 +00:00
2016-06-17 11:50:46 +00:00
int AiActivate : : getTypeId ( ) const
{
return TypeIdActivate ;
}
2014-06-12 21:27:04 +00:00
2016-06-17 11:50:46 +00:00
void AiActivate : : writeState ( ESM : : AiSequence : : AiSequence & sequence ) const
{
std : : auto_ptr < ESM : : AiSequence : : AiActivate > activate ( new ESM : : AiSequence : : AiActivate ( ) ) ;
activate - > mTargetId = mObjectId ;
2014-06-12 21:27:04 +00:00
2016-06-17 11:50:46 +00:00
ESM : : AiSequence : : AiPackageContainer package ;
package . mType = ESM : : AiSequence : : Ai_Activate ;
package . mPackage = activate . release ( ) ;
sequence . mPackages . push_back ( package ) ;
}
2014-06-12 21:27:04 +00:00
2016-06-17 11:50:46 +00:00
AiActivate : : AiActivate ( const ESM : : AiSequence : : AiActivate * activate )
: mObjectId ( activate - > mTargetId )
{
}
2014-06-12 21:27:04 +00:00
}