Advanced Strategic Command
changeview.cpp
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 #include "changeview.h"
23 #include "action-registry.h"
24 
25 #include "../gamemap.h"
26 
27 ChangeView::ChangeView( GameMap* gamemap, const ViewState& state )
28  : GameAction( gamemap )
29 {
30  newState = state;
31 }
32 
33 
35 {
36  return "Change view";
37 }
38 
39 
40 void ChangeView::readData ( tnstream& stream )
41 {
42  int version = stream.readInt();
43  if ( version != 1 )
44  throw tinvalidversion ( "ChangeView", 1, version );
45 
46  int count = stream.readInt();
47  for ( int i = 0; i< count; ++i ) {
48  MapCoordinate pos;
49  pos.read( stream );
50  newState[pos] = stream.readInt();
51  }
52 
53  count = stream.readInt();
54  for ( int i = 0; i< count; ++i ) {
55  MapCoordinate pos;
56  pos.read( stream );
57  oldState[pos] = stream.readInt();
58  }
59 
60 };
61 
62 
63 void ChangeView::writeData ( tnstream& stream ) const
64 {
65  stream.writeInt( 1 );
66  stream.writeInt( newState.size() );
67  for ( ViewState::const_iterator i = newState.begin(); i != newState.end(); ++i ) {
68  i->first.write( stream );
69  stream.writeInt( i->second );
70  }
71 
72  stream.writeInt( oldState.size() );
73  for ( ViewState::const_iterator i = oldState.begin(); i != oldState.end(); ++i ) {
74  i->first.write( stream );
75  stream.writeInt( i->second );
76  }
77 
78 };
79 
80 
82 {
84 }
85 
87 {
88  for ( ViewState::iterator i = newState.begin(); i != newState.end(); ++i ) {
89  MapField* fld = getMap()->getField( i->first );
90  oldState[i->first] = fld->visible;
91  fld->visible = newState[i->first];
92  }
93  return ActionResult(0);
94 }
95 
96 
98 {
99  for ( ViewState::iterator i = newState.begin(); i != newState.end(); ++i ) {
100  MapField* fld = getMap()->getField( i->first );
101  if ( fld->visible != newState[i->first] ) {
102  ASCString msg;
103  msg.format( "; expected: %x ; found %x" , newState[i->first], fld->visible );
104  throw ActionResult(21207, "Position is " + i->first.toString(true) + msg );
105  }
106  fld->visible = oldState[i->first];
107  }
108  return ActionResult(0);
109 }
110 
111 namespace {
112  const bool r1 = registerAction<ChangeView> ( ActionRegistry::ChangeView );
113 }
114 
ASCString getDescription() const
Definition: changeview.cpp:34
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
virtual GameActionID getID() const
Definition: changeview.cpp:81
int GameActionID
Definition: action.h:35
map< MapCoordinate, int > ViewState
Definition: changeview.h:32
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
void read(tnstream &stream)
Definition: typen.h:213
GameMap * getMap()
Definition: action.h:92
The interface for all kinds of IO stream.
a single field of the map
Definition: mapfield.h:26
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
Coordinate on the twodimensional map.
Definition: typen.h:202
Uint16 visible
can this field be seen be the player. Variable is bitmapped; two bits for each player. These two bits can have the states defined in VisibilityStates
Definition: mapfield.h:63
virtual void writeData(tnstream &stream) const
Definition: changeview.cpp:63
ChangeView(GameMap *gamemap, const ViewState &state)
Definition: changeview.cpp:27
virtual ActionResult runAction(const Context &context)
Definition: changeview.cpp:86
A GameAction is an modification of something on a map.
Definition: action.h:45
virtual ActionResult undoAction(const Context &context)
Definition: changeview.cpp:97
virtual void readData(tnstream &stream)
Definition: changeview.cpp:40
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
MapField * getField(int x, int y)
Definition: gamemap.h:465