00001 /* 00002 This file is part of Advanced Strategic Command; http://www.asc-hq.de 00003 Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; see the file COPYING. If not, write to the 00017 Free Software Foundation, Inc., 59 Temple Place, Suite 330, 00018 Boston, MA 02111-1307 USA 00019 */ 00020 00021 00022 #ifndef actionH 00023 #define actionH 00024 00025 00026 #include "../typen.h" 00027 #include "../ascstring.h" 00028 #include "../basestrm.h" 00029 #include "../util/factorywithnames.h" 00030 00031 #include "context.h" 00032 #include "actionresult.h" 00033 00034 class Vehicle; 00035 class GameMap; 00036 00037 00038 typedef int GameActionID; 00039 00045 class GameAction { 00046 00047 typedef deallocating_vector<GameAction*> Children; 00048 Children children; 00049 00050 GameMap* gamemap; 00051 00052 int sequenceNumber; 00053 00054 ActionResult undoChildren( const Context& context ); 00055 00056 protected: 00057 00058 void deleteChildren(); 00059 00060 GameAction( GameMap* map ); 00061 00063 void addChild( GameAction* action ); 00064 00065 virtual ActionResult runAction( const Context& context ) = 0; 00066 virtual ActionResult undoAction( const Context& context ) = 0; 00067 00074 virtual ActionResult preCheck() {return ActionResult(0);}; 00075 00080 virtual ActionResult postCheck() {return ActionResult(0);}; 00081 00085 virtual bool undoOrderChildFirst() const { return true; }; 00086 00087 virtual void readData ( tnstream& stream ) = 0; 00088 virtual void writeData ( tnstream& stream ) const = 0; 00089 00090 virtual GameActionID getID() const = 0; 00091 00092 GameMap* getMap() { return gamemap; }; 00093 const GameMap* getMap() const { return gamemap; } ; 00094 00095 public: 00096 ActionResult execute( const Context& context ); 00097 ActionResult undo( const Context& context ); 00098 00099 void read ( tnstream& stream ); 00100 void write ( tnstream& stream ) const; 00101 void write ( tnstream& stream, bool persistChildren ) const; 00102 00103 virtual ASCString getDescription() const = 0; 00104 00105 virtual ~GameAction() {}; 00106 00107 static GameAction* readFromStream( tnstream& stream, GameMap* map ); 00108 }; 00109 00110 00111 typedef Loki::SingletonHolder< Factory1< GameAction , GameActionID, GameMap* > > gameActionFactory; 00112 00113 template<class Derived> 00114 GameAction* GameActionCreator( GameMap* map ) 00115 { 00116 return new Derived( map ); 00117 } 00118 00119 template <typename ActionType > 00120 bool registerAction( GameActionID id ) 00121 { 00122 return gameActionFactory::Instance().registerClass( id, GameActionCreator<ActionType> ); 00123 } 00124 00125 00126 #endif 00127
1.5.1