Advanced Strategic Command
removemine.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 "removemine.h"
23 #include "action-registry.h"
24 
25 #include "../vehicle.h"
26 #include "../gamemap.h"
27 
28 RemoveMine::RemoveMine( GameMap* gamemap, const MapCoordinate& position, int mineID )
29  : GameAction( gamemap ), pos(position), layer(-1), mineBuffer(NULL)
30 {
31  this->mineID = mineID;
32 }
33 
34 
36 {
37  ASCString res = "Remove mine at " + pos.toString(false) + " with id " + ASCString::toString(mineID);
38  return res;
39 }
40 
41 
42 void RemoveMine::readData ( tnstream& stream )
43 {
44  int version = stream.readInt();
45  if ( version != 1 )
46  throw tinvalidversion ( "RemoveMine", 1, version );
47 
48  mineID = stream.readInt();
49  layer = stream.readInt();
50  pos.read( stream );
51 
52  if ( stream.readInt() ) {
53  mineBuffer = new MemoryStreamStorage();
54  mineBuffer->readfromstream( &stream );
55  } else
56  mineBuffer = NULL;
57 
58 };
59 
60 
61 void RemoveMine::writeData ( tnstream& stream ) const
62 {
63  stream.writeInt( 1 );
64  stream.writeInt( mineID );
65  stream.writeInt( layer );
66  pos.write( stream );
67 
68  if ( mineBuffer ) {
69  stream.writeInt( 1 );
70  mineBuffer->writetostream( &stream );
71  } else
72  stream.writeInt( 0 );
73 
74 };
75 
76 
78 {
80 }
81 
83 {
84  layer = 0;
85  MapField* fld = getMap()->getField(pos);
86  if ( !fld )
87  return ActionResult( 21002, pos );
88 
89  if ( mineID > 0 ) {
90  for ( MapField::MineContainer::iterator i = fld->mines.begin(); i != fld->mines.end(); ++i ) {
91  if ( i->identifier == mineID ) {
92  mineBuffer = new MemoryStreamStorage();
93  MemoryStream memstream( mineBuffer, tnstream::writing );
94  i->write( memstream );
95 
96  fld->mines.erase( i );
97  return ActionResult(0);
98  }
99  ++layer;
100  }
101  } else {
102  layer = fld->mines.size();
103  mineBuffer = new MemoryStreamStorage();
104  MemoryStream memstream( mineBuffer, tnstream::writing );
105  for ( MapField::MineContainer::iterator i = fld->mines.begin(); i != fld->mines.end(); ++i )
106  i->write( memstream );
107  fld->mines.clear();
108  return ActionResult(0);
109  }
110 
111  return ActionResult( 21401 );
112 }
113 
114 
116 {
117  MapField* fld = getMap()->getField(pos);
118  if ( !fld )
119  return ActionResult( 21002, pos );
120 
121  if ( layer < 0 )
122  throw ActionResult( 21401 );
123 
124  if ( mineID > 0 ) {
125  MapField::MineContainer::iterator i = fld->mines.begin();
126 
127  int l = layer;
128  while ( l-- )
129  ++i;
130 
131  MemoryStream memstream( mineBuffer, tnstream::reading );
132  fld->mines.insert( i, Mine::newFromStream( memstream ));
133  } else {
134  MemoryStream memstream( mineBuffer, tnstream::reading );
135  for ( int i = 0; i < layer; ++i )
136  fld->mines.push_back( Mine::newFromStream( memstream ));
137  }
138  return ActionResult(0);
139 }
140 
142 {
143  return ActionResult(0);
144 }
145 
147 {
148  delete mineBuffer;
149 }
150 
151 
152 namespace {
153  const bool r1 = registerAction<RemoveMine> ( ActionRegistry::RemoveMine );
154 }
155 
An in-memory storage of streamed data.
Definition: basestrm.h:172
ASCString getDescription() const
Definition: removemine.cpp:35
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
int GameActionID
Definition: action.h:35
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
ASCString toString(bool coordinates=false) const
Definition: typen.cpp:304
The interface for all kinds of IO stream.
a single field of the map
Definition: mapfield.h:26
virtual ActionResult undoAction(const Context &context)
Definition: removemine.cpp:115
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
void writetostream(tnstream *stream)
persist the contents in another stream
Definition: basestrm.cpp:2303
virtual ActionResult verify()
Definition: removemine.cpp:141
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void write(tnstream &stream) const
Definition: typen.h:212
Coordinate on the twodimensional map.
Definition: typen.h:202
Reads data from or writes data to a MemoryStreamStorage This allows a completely volatile storage of ...
Definition: basestrm.h:204
virtual void writeData(tnstream &stream) const
Definition: removemine.cpp:61
MineContainer mines
Definition: mapfield.h:117
void readfromstream(tnstream *stream)
read persisted contents from another stream
Definition: basestrm.cpp:2316
virtual void readData(tnstream &stream)
Definition: removemine.cpp:42
A GameAction is an modification of something on a map.
Definition: action.h:45
virtual ActionResult runAction(const Context &context)
Definition: removemine.cpp:82
virtual GameActionID getID() const
Definition: removemine.cpp:77
static Mine newFromStream(tnstream &stream)
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