Advanced Strategic Command
action.h
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 
22 #ifndef actionH
23 #define actionH
24 
25 
26 #include "../typen.h"
27 #include "../ascstring.h"
28 #include "../basestrm.h"
29 #include "../util/factorywithnames.h"
30 
31 #include "context.h"
32 #include "actionresult.h"
33 
34 class Vehicle;
35 class GameMap;
36 
37 
38 typedef int GameActionID;
39 
45 class GameAction {
46 
48  Children children;
49 
50  GameMap* gamemap;
51 
52  int sequenceNumber;
53 
54  ActionResult undoChildren( const Context& context );
55 
56  protected:
57 
58  void deleteChildren();
59 
60  GameAction( GameMap* map );
61 
63  void addChild( GameAction* action );
64 
65  virtual ActionResult runAction( const Context& context ) = 0;
66  virtual ActionResult undoAction( const Context& context ) = 0;
67 
74  virtual ActionResult preCheck() {return ActionResult(0);};
75 
80  virtual ActionResult postCheck() {return ActionResult(0);};
81 
85  virtual bool undoOrderChildFirst() const { return true; };
86 
87  virtual void readData ( tnstream& stream ) = 0;
88  virtual void writeData ( tnstream& stream ) const = 0;
89 
90  virtual GameActionID getID() const = 0;
91 
92  GameMap* getMap() { return gamemap; };
93  const GameMap* getMap() const { return gamemap; } ;
94 
95  public:
96  ActionResult execute( const Context& context );
97  ActionResult undo( const Context& context );
98 
99  void read ( tnstream& stream );
100  void write ( tnstream& stream ) const;
101  void write ( tnstream& stream, bool persistChildren ) const;
102 
103  virtual ASCString getDescription() const = 0;
104 
105  virtual ~GameAction() {};
106 
107  static GameAction* readFromStream( tnstream& stream, GameMap* map );
108 };
109 
110 
111 typedef Loki::SingletonHolder< Factory1< GameAction , GameActionID, GameMap* > > gameActionFactory;
112 
113 template<class Derived>
115 {
116  return new Derived( map );
117 }
118 
119 template <typename ActionType >
121 {
122  return gameActionFactory::Instance().registerClass( id, GameActionCreator<ActionType> );
123 }
124 
125 
126 #endif
127 
virtual ActionResult postCheck()
postCheck is called after an action has been redone and should check that the state of the map is exa...
Definition: action.h:80
virtual bool undoOrderChildFirst() const
determines the order in which the child actions are undone.
Definition: action.h:85
virtual ActionResult undoAction(const Context &context)=0
int GameActionID
Definition: action.h:35
static GameAction * readFromStream(tnstream &stream, GameMap *map)
Definition: action.cpp:150
virtual ActionResult runAction(const Context &context)=0
virtual ASCString getDescription() const =0
virtual GameActionID getID() const =0
GameMap * getMap()
Definition: action.h:92
The interface for all kinds of IO stream.
GameAction * GameActionCreator(GameMap *map)
Definition: action.h:114
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ActionResult undo(const Context &context)
Definition: action.cpp:77
bool registerAction(GameActionID id)
Definition: action.h:120
ActionResult execute(const Context &context)
Definition: action.cpp:41
Loki::SingletonHolder< Factory1< GameAction, GameActionID, GameMap * > > gameActionFactory
Definition: action.h:111
void write(tnstream &stream) const
Definition: action.cpp:145
virtual ActionResult preCheck()
preCheck is called before an action is redone and should check the preconditions for the action...
Definition: action.h:74
void deleteChildren()
Definition: action.cpp:159
void addChild(GameAction *action)
adds a child action. Ownership of the child action is passed to THIS
Definition: action.cpp:36
virtual ~GameAction()
Definition: action.h:105
const GameMap * getMap() const
Definition: action.h:93
GameAction(GameMap *map)
Definition: action.cpp:28
A GameAction is an modification of something on a map.
Definition: action.h:45
void read(tnstream &stream)
Definition: action.cpp:110
virtual void writeData(tnstream &stream) const =0
virtual void readData(tnstream &stream)=0
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182