Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

action.h

00001 // $Id: action_8h-source.html,v 1.2 2002/06/20 17:09:53 mkrohn5 Exp $
00002 
00003 // Action Header File
00004 // Written by: Alberto Barsella
00005 
00006 // Copyright (C) 1999-2001, Alberto Barsella <Alberto.Barsella@univ-lille1.fr>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 1, or (at your option)
00011 // any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 #ifndef __TACTION_INCLUDED
00019 #define __TACTION_INCLUDED
00020 
00021 #include "common/global.h"
00022 #include "common/plist.h"
00023 #include "gamelogic/property.h"
00024 #include "common/datapackage.h"
00025 
00026 #include <string>
00027 #include <vector>
00028 using namespace std;
00029 
00030 class TActionObject;
00031 class TAction;
00032 class TActionSequencer;
00033 class TDataPackageList;
00034 
00035 
00036 
00037 
00038 //----------------------------------------  Some evil defines  ----------------------------------------
00039 // this makes writing actions a lot easier
00040 
00041 #define ActionClass(a)         TAction_##a
00042 #define ACString(a)            "TAction_"#a
00043 #define ActionType(a)          ACTION_##a
00044 #define ActionProto(a)         Action##a##Proto
00045 
00046 #define DefineNewAction(a, b)  ActionClass(a) ActionProto(a)(b, &ActionType(a)); \
00047                                TActionType ActionType(a); \
00048                                ActionClass(a)::ActionClass(a)(const string& name, TActionType* type) : TAction(name, type) \
00049                                { initialize(); }
00050 
00051 #define DeclareNewAction(a) extern TActionType ActionType(a); \
00052                             class ActionClass(a) : public TAction { \
00053                               public: \
00054                                 ActionClass(a)(const string& name, TActionType* type); \
00055                                 ~ActionClass(a)() { uninitialize(); } \
00056                                 bool execute(); \
00057                                 bool loadAttributes(TDataPackageList* in); \
00058                                 bool saveAttributes(TDataPackageList* out) const; \
00059                               private: \
00060                                 ActionClass(a)(const TActionType type, const string* tn) : TAction(type, tn) { initialize(); } \
00061                                 void initialize(); \
00062                                 void uninitialize(); \
00063                                 TAction* _new() const { return new ActionClass(a)(Type, TypeName); }
00064 
00065 #define EndDeclareAction    }
00066 
00067 #define Unsupported(type)   Fatal( #type, "unsupported method", "an unsupported method was called for action "#type, -1)
00068 
00069 #define Member(a)  ActionClass(a)
00070 
00071 typedef int2 TActionType;
00072 
00073 
00074 
00075 
00076 
00125 class TAction : public TLinkable {
00126   public:
00127     // this is only used at prototype creation
00128     // all creation of actions should pass through Create
00129     TAction(const string& name, TActionType* type);
00130 
00131     // destructor
00132     virtual ~TAction();
00133 
00137     virtual bool load(TDataPackageList* in);
00141     virtual bool loadAttributes(TDataPackageList* in);
00142     virtual bool save(TDataPackageList* out) const;
00143     virtual bool saveAttributes( TDataPackageList* out) const;
00144 
00145     void markAsExecuted();
00146     void objectValidationIsNeeded();
00147     virtual bool validateObjects();
00148     virtual bool checkActionConflicts();
00149     virtual bool resolveActionConflicts();
00150 
00151     virtual bool canUndo() const;
00152 
00156     virtual bool execute() = 0;
00162     void setActor( TActionObject* actor );
00168     void setCreator( TRace* creator );
00169 
00170     // create a new action of the specified type
00171 static TAction* create(const string& actname);
00172 
00173     // verify type of an action
00174     bool is(const TActionType type) const;
00175     virtual bool isPlayerAction() const;
00176     const TActionType getActionType() const;
00177     const string& getActionName() const;
00178 
00179     // overloading of TType methods
00180     int2 getType() const;
00181     const string& getTypeName() const;
00182     TRace* getCreator() const;
00183 
00184   protected:
00185     TAction(const TActionType type, const string* tn); // only invoked by _new
00186     virtual TAction* _new() const = 0;
00187 
00188     TActionType Type;
00189     const string* TypeName;
00190 
00191     TLink<TRace> fCreator;
00192     TLink<TActionObject> fActor;
00193     list<TLink<TActionObject>*> Objects;
00194     list<TLink<TActionObject>*> Observers;
00195 
00196   private:
00197 static TAction* protoManager(TAction* act, const string& actname);
00198 
00199     bool Executed;
00200     bool ObjectsAreValid;
00201     bool IsPrototype;
00202 
00203     // global action type counter
00204 static uint4 iNumActionTypes;
00205 };
00206 
00207 
00208 ostream& operator<<(ostream& os, const TAction& Act);
00209 
00210 
00211 
00221 class TActionObject : public TProperty
00222 {
00223     friend class TAction;
00224 
00225   public:
00226     TActionObject();
00227     virtual ~TActionObject();
00228 
00229     virtual bool canPerformAction(const TAction& action);
00230     virtual bool canBeObjectInAction(const TAction& action);
00231     virtual bool canObserveAction(const TAction& action);
00232 
00241     virtual bool load( TDataPackageList* in );
00250     virtual bool save( TDataPackageList* out ) const;
00251 
00252     // overloading of TType methods done by derived classes
00253 
00254   private:
00255 };
00256 
00257 
00258 
00268 class TActionSequencer {
00269   public:
00270     TActionSequencer(const bool client);
00271     ~TActionSequencer();
00272 
00278     bool isInClientMode() const;
00279     void clear();
00280     bool addAction(TAction* action);
00281     bool runSequence();
00282     bool saveLists(ofstream& out) const;
00283 
00284   private:
00285     bool ClientMode;
00286 
00287     typedef map<TObjectID, TPList<TAction> > tActionTable;
00288 
00289     tActionTable ActionLists;
00290 };
00291 
00292 
00293 #endif        // __TACTION_INCLUDED

Generated on Thu Jun 20 18:13:15 2002 for Stellar Legacy by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001