24 #include "core/MASignal.hpp" 25 #include "types/MALeg.hpp" 26 #include "sound/MASoundBase.hpp" 28 #include <boost/shared_ptr.hpp> 29 #include <boost/unordered_map.hpp> 42 #define MABEHAVIOR_CREATE(_behavior, _master, ...) \ 44 MABEHAVIOR_CREATE_S(_behavior, _master, "", __VA_ARGS__) 49 #define MABEHAVIOR_CREATE_S(_behavior, _master, _name_suffix, ...) \ 51 MABehavior* _Temp = _master; \ 52 MABehavior* _Master = static_cast<MABehavior*>(_Temp); \ 54 if (!MABehavior::Exists(MCGetClassName<_behavior>(nullptr, _name_suffix)) && \ 55 !(_Master != nullptr && \ 56 (_Master->GetState() == MABehavior::Failed || \ 57 _Master->GetState() == MABehavior::Finished))) \ 59 _behavior* Behavior = new _behavior(__VA_ARGS__); \ 61 if (_Master != nullptr) \ 62 _Master->AddSlave(*Behavior); \ 67 #define MABEHAVIOR_GET(_behavior) \ 68 MABEHAVIOR_GET_S(_behavior, "") 70 #define MABEHAVIOR_GET_S(_behavior, _name_suffix) \ 71 dynamic_cast<_behavior*>(MABehavior::GetBehavior(MCGetClassName<_behavior>(nullptr, _name_suffix))); 74 #define MABEHAVIOR_EXISTS(_behavior) \ 75 MABEHAVIOR_EXISTS_S(_behavior, "") 77 #define MABEHAVIOR_EXISTS_S(_behavior, _name_suffix) \ 78 MABehavior::Exists(MCGetClassName<_behavior>(nullptr, _name_suffix)) 81 #define MABEHAVIOR_DELETE(_behavior) \ 82 MABEHAVIOR_DELETE_S(_behavior, "") 84 #define MABEHAVIOR_DELETE_S(_behavior, _name_suffix) \ 86 MABehavior* Behavior = MABehavior::GetBehavior(MCGetClassName<_behavior>(nullptr, _name_suffix)); \ 90 Behavior->MarkDirty(); \ 95 #define MABEHAVIOR_IS_NORMAL(_behavior) \ 96 MABEHAVIOR_IS_NORMAL_S(_behavior, "") 98 #define MABEHAVIOR_IS_NORMAL_S(_behavior, _name_suffix) \ 99 (MABEHAVIOR_EXISTS_S(_behavior, _name_suffix) ? \ 100 MABehavior::GetBehavior(MCGetClassName<_behavior>(nullptr, _name_suffix))->GetState() == MABehavior::Normal : \ 104 #define MABEHAVIOR_IS_ACTIVATED(_behavior) \ 105 MABEHAVIOR_IS_ACTIVATED_S(_behavior, "") 107 #define MABEHAVIOR_IS_ACTIVATED_S(_behavior, _name_suffix) \ 108 (MABEHAVIOR_EXISTS_S(_behavior, _name_suffix) ? \ 109 MABehavior::GetBehavior(MCGetClassName<_behavior>(nullptr, _name_suffix))->GetState() == MABehavior::Activated : \ 113 #define MABEHAVIOR_IS_FINISHED(_behavior) \ 114 MABEHAVIOR_IS_FINISHED_S(_behavior, "") 116 #define MABEHAVIOR_IS_FINISHED_S(_behavior, _name_suffix) \ 117 (MABEHAVIOR_EXISTS_S(_behavior, _name_suffix) ? \ 118 MABehavior::GetBehavior(MCGetClassName<_behavior>(nullptr, _name_suffix))->GetState() == MABehavior::Finished : \ 122 #define MABEHAVIOR_IS_FAILED(_behavior) \ 123 MABEHAVIOR_IS_FAILED_ADNVANCED(_behavior, "") 125 #define MABEHAVIOR_IS_FAILED_ADNVANCED(_behavior, _name_suffix) \ 126 (MABEHAVIOR_EXISTS_S(_behavior, _name_suffix) ? \ 127 MABehavior::GetBehavior(MCGetClassName<_behavior>(nullptr, _name_suffix))->GetState() == MABehavior::Failed : \ 133 typedef std::vector<boost::shared_ptr<MABehavior> > BehaviorList;
136 typedef std::vector<MABehavior*> BehaviorPtrList;
139 typedef boost::unordered_map<std::string, float> DesiredConnectionMap;
142 typedef boost::unordered_map<MABehavior*, float> ConnectionMap;
145 typedef boost::unordered_map<std::string, int> IncomingActivationMap;
148 typedef boost::unordered_multimap<MAController*, std::string> ControllerTransitionMap;
151 typedef boost::unordered_map<MAIntervalNum<int>*,
float> EmotionWeightMap;
165 template <typename T>
166 friend
void boost::checked_delete(T*);
194 MABehavior(
const std::string& type_name,
bool dynamic =
true);
204 static const MA::BehaviorList& GetBehaviors();
213 static const MA::BehaviorList& GetNewBehaviors();
222 static void SetVerbose(
bool new_state);
231 static bool IsVerbose();
240 static void ClearBehaviorStateEvents();
251 static MABehavior* GetBehavior(
const std::string& type_name);
266 static bool Exists(
const std::string& type_name,
bool can_be_dying =
false);
281 static bool Exists(
const MABehavior* behavior,
bool can_be_dying =
false);
301 static bool IsDying(
const std::string& type_name);
321 static bool IsDying(
const MABehavior* behavior);
332 static void CalculateActivations();
339 static void GarbageCollection();
349 static void Cleanup();
356 static void MergeNewBehaviors();
363 static void DumpBehaviors();
372 static bool IsMergeNeeded();
444 bool HasSlave()
const;
453 void ResetSelfActivation();
460 void SetMaxSelfActivation();
469 void SetSelfActivationDuration(
const int new_duration);
478 int GetSelfActivationDuration()
const;
490 void SetLifetimeExpirationTime(
const unsigned int expiration_time);
499 void SetActivatedStateExpirationTime(
const unsigned int expiration_time);
508 int GetElapsedActivatedStateTime()
const;
520 int GetActivationLevel();
529 bool HasActivatedStateExpired()
const;
548 void SetState(StateType new_state);
565 void StartTransition(
MAController& controller,
const std::string& transition_name);
575 void RegisterActiveTransition(
MAController& controller,
const std::string& transition_name);
586 int PlaySound(
const std::string& name);
597 int PlaySound(MA::SoundBaseSPtr sound);
635 bool AreActiveTransitionsFinished();
644 bool AnyActiveTransitionFailed()
const;
651 void StopActiveTransitions();
658 void ClearActiveTransitions();
667 bool AreSoundPlaybacksFinished();
674 void ClearSoundPlaybacks();
683 virtual bool IsFinished();
692 virtual bool IsFailed();
699 virtual void ActivatingActions();
706 virtual void ActivatedStateUpdate();
713 virtual void FailingActions();
720 virtual void FinishingActions();
727 void SendOutgoingActivations();
734 void ClearIncomingActivations();
743 void AddIncomingActivation(
int value);
778 MAIntervalNum<int> SelfActivation;
813 template <
typename T>
817 !(master !=
nullptr &&
820 T* Behavior =
new T();
MC::StringList QueuedSounds
Queued sounds.
static bool Exists(const std::string &type_name, bool can_be_dying=false)
Check if a behavior exists.
MA_SLOT_CLASS typedef MASignal< MABehavior & > BehaviorDestroyedSignalType
Behavior destroyed signal type.
boost::shared_ptr< BehaviorDestroyedSignalType > BehaviorDestroyedSignal
Behavior destruction signal.
MA::DesiredConnectionMap DesiredConnections
Desired connection map to other behaviors.
std::vector< MA::SoundBaseSPtr > QueuedInMemorySounds
Queued in-memory sounds.
MABehavior * Master
Master behavior of the subnetwork where the behavior belongs to.
MA::EmotionWeightMap EmotionWeights
Emotion weight map.
Base class for the controllers.
#define MC_DISABLE_COPY(class_name)
Helper macro to disable the copy constructor and assignment operator of a class (object copying) ...
int IncomingActivationLevel
Incoming activation level.
MABehavior * MACreateBehavior(MABehavior *master)
Create a behavior.
MA::BehaviorPtrList SlaveBehaviors
The list of the slave (child) behaviors.
StateType GetState() const
Get the behavior state.
boost::shared_ptr< MCTimer > LifetimeTimer
Lifetime duration timer.
StateType State
The current behavior state.
int SoundPlaybackID
Active sound playback during the activated state.
int ActivatedStateStartTimestamp
Activated state starting timestamp.
bool IsDynamic() const
Check if the behavior is dynamic.
MA::ConnectionMap Connections
Connection map to other behaviors.
MA::ControllerTransitionMap ActiveTransitions
LED or motion transitions during the activated state.
bool Dynamic
Whether the behavior is dynamic (it is destroyed after in failed or finished state) ...
Simple timer class with microsecond precision.
boost::shared_ptr< MCTimer > ActivatedStateTimer
Active state duration timer.
const std::string & GetTypeName() const
Get the behavior type name.
void AddSlave(MABehavior &slave)
Add a slave behavior.
std::string TypeName
A unique behavior type name.