BWAPI
SPAR/AIModule/SparAIModule/DecisionMaking/Layer1Reactive/FSM/Behaviors/EngageEnemies/GroupAttack.h
Go to the documentation of this file.
00001 #pragma once
00002 #include "EngageEnemies.h"
00003 #include "../../BehaviorState.h"
00004 #include "../../Conditions/IsAttacking.h"
00005 #include "../../Conditions/ReachedDestination.h"
00006 #include "../../../../../PersistentUnitGroup.h"
00007 #include "UnitAttackFSM.h"
00008 #include <BWAPI.h>
00009 #include <hash_map>
00010 
00011 //class UnitAttackFSM;
00012 
00013 //TODO: Needs to be cleaned!  Units now select their own target, so select target is no longer useful... but it's what allows the machine to switch from engaging to moving...
00014 //maybe this machine is not required anymore... only individual FSMs?
00015 //Observer logic should be put in another file, this class is getting messy
00016 
00017 class GroupAttack : public EngageEnemies<GroupAttack>
00018 {
00019 protected:
00020   class GoToLocation;
00021   class Engaging;
00022   class Terminate;
00023 
00024 protected:
00025   struct BehaviorWrapper
00026   {
00027     IUnitAttackFSM* behavior;
00028     Behavior::OnTerminate::SubscriberID subscriberId;
00029   };
00030 
00031   // GoToLocation state /////////////////////////////////////////////////////////
00032   class GoToLocation : public BehaviorState<GroupAttack, GoToLocation>
00033   {
00034   public:
00035     GoToLocation(GroupAttack& behaviour, Location* m_location);
00036     void onEnterImpl();
00037 
00038     void onExitImpl() {}
00039 
00040     void unitAdded(BWAPI::Unit* unit);
00041 
00042     void onTimer();
00043 
00044     typedef SCHEDULER_EVENT(GoToLocation, onTimer) TimerEvent;
00045 
00046     typedef boost::tuple<
00047           TimerEvent&
00048           > Events;
00049                 Events m_events;
00050     
00051                 typedef boost::tuple<
00052           Transition<GoToLocation, GroupIsAttacking, NoTransitionAction, Engaging>&,
00053           Transition<GoToLocation, GroupReachedDestinationLocation<PersistentUnitGroup>, NoTransitionAction, Terminate>&
00054                   > Transitions;
00055                 Transitions m_transitions;
00056 
00057   
00058   private:
00059     Location* m_location;
00060     int time;
00061 
00062     TimerEvent m_isAttacking;
00063                 Transition<GoToLocation, GroupIsAttacking, NoTransitionAction, Engaging> m_transitionToEngaging;
00064     Transition<GoToLocation, GroupReachedDestinationLocation<PersistentUnitGroup>, NoTransitionAction, Terminate> m_transitionToTerminate;
00065   };
00066 
00067   // Engaging state ////////////////////////////////////////////////////////////
00068   class Engaging : public BehaviorState<GroupAttack, Engaging>
00069   {
00070   public:
00071     Engaging(GroupAttack& behavior);
00072     void onEnterImpl();
00073     void onExitImpl() {}
00074     
00075     void onTimer();
00076 
00077     void unitAdded(BWAPI::Unit* unit);
00078 
00079     void onSubBehaviorTerminated(void* data);
00080 
00081   private:
00082     bool selectTarget();
00083 
00084     typedef SCHEDULER_EVENT(Engaging, onTimer) TimerEvent;
00085     TimerEvent m_enemySighted;
00086 
00087     EVENT_HANDLER(Engaging, onSubBehaviorTerminated) m_onSubBehaviorTerminated;
00088     Transition<Engaging, NoCondition, NoTransitionAction, GoToLocation> m_transitionToGoTo;
00089 
00090   public:
00091     typedef boost::tuple<
00092       TimerEvent&
00093     > Events;
00094     const Events m_events;
00095 
00096     typedef boost::tuple<
00097           Transition<Engaging, NoCondition, NoTransitionAction, GoToLocation>& 
00098       > Transitions;
00099     const Transitions m_transitions;
00100   };
00101 
00102   class Terminate : public BehaviorState<GroupAttack, Terminate>
00103   {
00104   public:
00105     Terminate(GroupAttack& behavior);
00106     void onEnterImpl() ;
00107     void onExitImpl() {}
00108 
00109     typedef boost::tuple<> Events;
00110           Events m_events;
00111     
00112                 typedef boost::tuple<> Transitions;
00113                 Transitions m_transitions;
00114   };
00115 
00116 public:
00117   GroupAttack(const Process* parent, unsigned int initialPriority, const PersistentUnitGroup* group, Location* location)
00118     : EngageEnemies(parent, initialPriority, group)
00119     , IEngageEnemies(parent, initialPriority, group)
00120     , IGroupBehavior(parent, initialPriority, group)
00121     , IBehavior(parent, initialPriority)
00122     , IFSM(parent)
00123     , Process(parent)
00124     , m_engagingState(*this)
00125     , m_goToState(*this, location)
00126     , m_terminateState(*this)
00127     , m_states(m_goToState, m_engagingState)
00128     , m_location(location)
00129     , m_observerGroup("GroupAttack observers")
00130   {
00131   }
00132 
00133   virtual std::string getName() const
00134   {
00135     return "GroupAttack (" + getGroup()->getName() + ")";
00136   }
00137 
00138   virtual void onUnitAddedToGroup(void* data, BWAPI::Unit* unit);
00139   virtual void onUnitRemovedFromGroup(void* data, BWAPI::Unit* unit);
00140   
00141   typedef boost::tuple<
00142     GoToLocation&,
00143     Engaging&
00144   > States;
00145   const States m_states;
00146 
00147 protected:
00148   virtual void executeImpl()
00149   {
00150     EngageEnemies::executeImpl();
00151     m_goToState.onEnter();
00152   }
00153 
00154   // Member variables //////////////////////////////////////////////////////////
00155   Engaging m_engagingState;
00156   GoToLocation m_goToState;
00157   Terminate m_terminateState;
00158   stdext::hash_map<BWAPI::Unit*, BehaviorWrapper> m_subBehaviors;
00159   //BWAPI::Position m_location;
00160   Location* m_location;
00161   PersistentUnitGroup m_observerGroup;
00162 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines