Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gameeventsystem.h

Go to the documentation of this file.
00001 
00005 /*
00006     This file is part of Advanced Strategic Command; http://www.asc-hq.de
00007     Copyright (C) 1994-1999  Martin Bickel  and  Marc Schellenberger
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU General Public License for more details.
00018 
00019     You should have received a copy of the GNU General Public License
00020     along with this program; see the file COPYING. If not, write to the
00021     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00022     Boston, MA  02111-1307  USA
00023 */
00024 
00025 
00026 #ifndef gameeventsystemH
00027 #define gameeventsystemH
00028 
00029 #include <sigc++/sigc++.h>
00030 
00031 #include "typen.h"
00032 #include "libs/loki/Singleton.h"
00033 #include "factory.h"
00034 
00035 #include "mapdisplayinterface.h"
00036 
00037 class MapDisplayInterface;
00038 
00039 enum  EventConnections { cconnection_destroy = 1,
00040                          cconnection_conquer = 2,
00041                          cconnection_lose  = 4,
00042                          cconnection_seen = 8,
00043                          cconnection_areaentered_anyunit = 16,
00044                          cconnection_areaentered_specificunit = 32 };
00045 
00046 
00047 extern bool  checkevents( MapDisplayInterface* md );
00048 
00049 extern void  checktimedevents( MapDisplayInterface* md );
00050 
00051 extern void eventReady();
00052 
00053 typedef int EventTriggerID;
00054 typedef int EventActionID;
00055 
00056 class Event;
00057 
00058 class EventTrigger {
00059       EventTriggerID triggerID;
00060    public:
00061       enum State { unfulfilled, fulfilled, finally_fulfilled, finally_failed };
00062    protected:
00063       EventTrigger ( EventTriggerID id ) : triggerID ( id ), gamemap(NULL), event(NULL), stateCache(unfulfilled), triggerFinal( false ), invert(false) {};
00064       virtual State getState( int player ) = 0;
00065       GameMap* gamemap;
00066       Event* event;
00067 
00068       bool isFulfilled();
00069    private:
00070       State stateCache;
00071       bool triggerFinal;
00072    public:
00073       bool invert;
00075       State state( int player );
00076 
00077       virtual void readData ( tnstream& stream ) = 0;
00078       virtual void writeData ( tnstream& stream ) = 0;
00079       
00080       virtual ASCString getDetailledName() const = 0;
00081       virtual ASCString getTypeName() const = 0;
00082       virtual void setup() = 0;
00083       virtual void arm() {};
00084       void setMap( GameMap* gamemap_ ) { gamemap = gamemap_; };
00085       void setEvent( Event* ev ) { event = ev; };
00086       EventTriggerID getTriggerID() { return triggerID; };
00087 
00088       virtual ~EventTrigger(){};
00089 
00090       // friend Event* readOldEvent( pnstream stream );
00091 };
00092 
00093 class EventAction {
00094       EventActionID actionID;
00095    protected:
00096       GameMap* gamemap;
00097       EventAction( EventActionID id ) : actionID ( id ), gamemap(NULL) {};
00098    public:
00099 
00100       virtual void readData ( tnstream& stream ) = 0;
00101       virtual void writeData ( tnstream& stream ) = 0;
00102       virtual ASCString getName() const = 0;
00103 
00104       virtual void execute( MapDisplayInterface* md ) = 0;
00105       virtual void setup() = 0;
00106       void setMap( GameMap* gamemap_ ) { gamemap = gamemap_; };
00107       EventActionID getActionID() { return actionID; };
00108       virtual ~EventAction() {};
00109 };
00110 
00111 class Event {
00112       GameMap& gamemap;
00113 
00114       void clear();
00115    public:
00116       Event( GameMap& map_ );
00117       const GameMap* getMap() const { return &gamemap; };
00118 
00119       enum Status { Untriggered, Triggered, Timed, Executed } status;
00120 
00121       typedef vector<EventTrigger*> Trigger;
00122       Trigger trigger;
00123 
00124       int id;
00125       int playerBitmap; 
00126       ASCString  description;
00127       GameTime   triggerTime;
00128       struct Delayedexecution {
00129          Delayedexecution():turn(0),move(0){};
00130          int turn;
00131          int move;   // negative values allowed !!
00132       } delayedexecution;
00133 
00135       int reArmNum;
00136 
00137       EventAction* action;
00138       enum TriggerConnection { AND, OR } triggerConnection;
00139 
00140       void check( MapDisplayInterface* md );
00141       void execute( MapDisplayInterface* md );
00142       void spawnAction( EventActionID eai );
00143       EventTrigger* spawnTrigger( EventTriggerID eti );
00144 
00145       Event& operator= ( const Event& ev  );
00146 
00147       void read ( tnstream& stream );
00148       void write ( tnstream& stream );
00149 
00150       void arm();
00151 
00152       SigC::Signal0<void> executed;
00153 
00154       virtual ~Event();
00155 };
00156 
00157 class VariableLocker {
00158    bool& var;
00159    public:
00160       VariableLocker( bool& variable ) : var ( variable ) { var = true; };
00161       ~VariableLocker() { var = false; };
00162 };
00163 
00164 
00165 template < class AbstractProduct, typename IdentifierType, typename ObjectCreatorCallBack = AbstractProduct*(*)(), typename NameType = ASCString >
00166 class FactoryWithNames : protected Factory<AbstractProduct, IdentifierType>
00167 {
00168    private:
00169       typedef std::map<NameType, IdentifierType> NameMap;
00170       NameMap names;
00171    public:
00172       vector<NameType> getNames(){
00173          vector<NameType> nameList;
00174          
00175          for ( typename NameMap::iterator i = names.begin(); i != names.end(); ++i )
00176             nameList.push_back( i->first );
00177          return nameList;
00178       }
00179 
00180       IdentifierType getID( const ASCString& name )
00181       {
00182          return names[name];
00183       }
00184 
00185       bool registerClass( IdentifierType id, typename FactoryWithNames<AbstractProduct, IdentifierType, typename FactoryWithNames::ObjectCreatorCallBack, NameType>::ObjectCreatorCallBack createFn, Loki::Functor<NameType, TYPELIST_1(const IdentifierType&)> nameProvider )
00186       {
00187          return registerClass( id, createFn, nameProvider(id) );
00188       }
00189 
00190       bool registerClass( IdentifierType id, typename FactoryWithNames<AbstractProduct, IdentifierType, typename FactoryWithNames::ObjectCreatorCallBack, NameType>::ObjectCreatorCallBack createFn, NameType name )
00191       {
00192          if ( Factory<AbstractProduct, IdentifierType>::registerClass ( id, createFn )) {
00193             names[name] = id;
00194             return true;
00195          } else
00196             return false;
00197       }
00198 
00199       AbstractProduct* createObject( IdentifierType id ) {
00200          return Factory<AbstractProduct, IdentifierType>::createObject( id );
00201       }
00202 
00203 };
00204 
00205 typedef Loki::SingletonHolder< FactoryWithNames< EventTrigger, EventTriggerID > > triggerFactory;
00206 typedef Loki::SingletonHolder< FactoryWithNames< EventAction , EventActionID  > > actionFactory;
00207 
00208 
00209 
00210 // }; // namespace GameEvents
00211 
00212 #endif

Generated on Tue Jun 24 01:27:42 2008 for Advanced Strategic Command by  doxygen 1.4.2