BWAPI
SPAR/AIModule/SparAIModule/SparAIContainer.h
Go to the documentation of this file.
00001 #pragma once
00002 #include "../Utils/Utils.h"
00003 #include "Utils/Process.h"
00004 #include <Config.h>
00005 #include <iostream>
00006 #include <BWAPI.h>
00007 #include <BWTA.h>
00008 
00009 class SparAIContainer;
00010 extern SparAIContainer* Spar;
00011 
00012 class PerceptualState;
00013 class UnitManager;
00014 class Scheduler;
00015 class Timer;
00016 
00017 class SparAIContainer : public BWAPI::AIModule, public Process
00018 {
00019 public:
00020   SparAIContainer(Scheduler* scheduler,
00021                   PerceptualState* perceptualState,
00022                   UnitManager* unitManager,
00023                   Timer* timer,
00024                   Config* configuration)
00025     : Process(NULL)
00026     , m_schedulerPtr(scheduler)
00027     , m_perceptualStatePtr(perceptualState)
00028     , m_unitManagerPtr(unitManager)
00029     , m_timerPtr(timer)
00030     , m_configuration(configuration)
00031   {
00032     Spar = this;
00033   }
00034   ~SparAIContainer()
00035   {
00036     assert(m_zombieComponents.empty());
00037   }
00038 
00039   PerceptualState& getPerceptualState() { return *m_perceptualStatePtr; }
00040   UnitManager& getUnitManager() { return *m_unitManagerPtr; }
00041   Scheduler& getScheduler() { return *m_schedulerPtr; }
00042   Timer& getTimer() { return *m_timerPtr; }
00043   Config& getConfiguration() { return *m_configuration; }
00044 
00045   virtual void onStart()
00046   {
00047     Process::execute();
00048   }
00049 
00050   virtual void onFrame()
00051   {
00052     deleteZombieComponents();
00053   }
00054 
00055   virtual void onEnd(bool isWinner)
00056   {
00057     unused(isWinner);
00058 
00059     Process::terminate(Success);
00060     deleteZombieComponents();
00061   }
00062 
00067   template <class ComponentType>
00068   void addZombieComponent(const ComponentType* component)
00069   {
00070     // This static_assert is supposed to prevent people from zombifying processes manually; 
00071     // use Process::zombifyOnTerminate() or ProcessContainer/ProcessContainerList instead.
00072     static_assert<!boost::is_convertible<const ComponentType*,const Process*>::value || boost::is_same<ComponentType,Process>::value> use_process_zombify_on_terminate_or_process_container_to_zombify_processes;
00073     unused(use_process_zombify_on_terminate_or_process_container_to_zombify_processes);
00074     assert(std::find(m_zombieComponents.begin(), m_zombieComponents.end(), component) == m_zombieComponents.end());
00075 
00076     if (component != NULL)
00077       m_zombieComponents.push_back(component);
00078   }
00079 
00080 protected:
00081   virtual void output(std::ostream& out) const
00082   {
00083     out << "SPAR agent";
00084   }
00085 
00090   void deleteZombieComponents()
00091   {
00092     for (std::list<const Component*>::const_iterator it = m_zombieComponents.begin();
00093          it != m_zombieComponents.end();
00094          ++it)
00095     {
00096       delete *it;
00097     }
00098     m_zombieComponents.clear();
00099   }
00100 
00101 private:
00102   // Keep m_scheduler BEFORE m_perceptualState (else deleting locations will fail on Component::verifyPendingTasks())
00103   Scheduler* m_schedulerPtr;
00104   PerceptualState* m_perceptualStatePtr;
00105   UnitManager* m_unitManagerPtr;
00106   Timer* m_timerPtr;
00107   Config* m_configuration;
00108 
00112   // Optimization: chain the UnitData to each other?
00113   std::list<const Component*> m_zombieComponents;
00114 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines