Advanced Strategic Command
removeobject.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 "removeobject.h"
23 #include "action-registry.h"
24 
25 #include "../vehicle.h"
26 #include "../gamemap.h"
27 #include "../viewcalculation.h"
28 
29 RemoveObject::RemoveObject( GameMap* gamemap, const MapCoordinate& position, int objectID )
30  : GameAction( gamemap ), pos(position), objectBuffer(NULL)
31 {
32  this->objectID = objectID;
33  additionalObjectCount = 0;
34 }
35 
36 
38 {
39  ASCString res = "Remove object with id " + ASCString::toString(objectID) + " at " + pos.toString(false);
40  return res;
41 }
42 
43 
44 static const int removeObjectStreamVersion = 2;
45 
47 {
48  int version = stream.readInt();
49  if ( version < 1 || version > removeObjectStreamVersion )
50  throw tinvalidversion ( "RemoveObject", removeObjectStreamVersion, version );
51 
52  objectID = stream.readInt();
53  pos.read( stream );
54 
55  if ( stream.readInt() ) {
56  objectBuffer = new MemoryStreamStorage();
57  objectBuffer->readfromstream( &stream );
58  } else
59  objectBuffer = NULL;
60 
61  if ( version >= 2 )
62  additionalObjectCount = stream.readInt();
63  else
64  additionalObjectCount = 0;
65 };
66 
67 
68 void RemoveObject::writeData ( tnstream& stream ) const
69 {
71  stream.writeInt( objectID );
72  pos.write( stream );
73 
74  if ( objectBuffer ) {
75  stream.writeInt( 1 );
76  objectBuffer->writetostream( &stream );
77  } else
78  stream.writeInt( 0 );
79 
80  stream.writeInt( additionalObjectCount );
81 };
82 
83 
85 {
87 }
88 
89 
91  tnstream& buffer;
92  int counter;
93  public:
94  ObjectRemovalStrategy2( tnstream& objectBuffer )
95  : buffer( objectBuffer ), counter( 0 )
96  {}
97 
98  virtual void removeObject( MapField* fld, const ObjectType* objectType ) {
99  for ( MapField::ObjectContainer::iterator o = fld->objects.begin(); o != fld->objects.end(); ) {
100  if ( o->typ == objectType ) {
101  buffer.writeInt( o->typ->id );
102  o->write( buffer );
103  ++counter;
104  o = fld->objects.erase( o );
105  } else
106  ++o ;
107  }
108  };
109 
110  int getCounter()
111  {
112  return counter;
113  }
114 
115 };
116 
117 
119 {
120  MapField* fld = getMap()->getField(pos);
121  if ( !fld )
122  return ActionResult( 21002, pos );
123 
124  ObjectType* ot = getMap()->getobjecttype_byid( objectID );
125  if ( !ot )
126  return ActionResult( 21501 );
127 
128  Object* o = fld->checkForObject( ot );
129  if ( !o )
130  return ActionResult( 21502 );
131 
132  objectBuffer = new MemoryStreamStorage();
133  MemoryStream memstream( objectBuffer, tnstream::writing );
134  o->write( memstream );
135 
136  ObjectRemovalStrategy2 removalStrat( memstream );
137 
138  bool result = fld->removeObject( ot, true, &removalStrat );
139  additionalObjectCount = removalStrat.getCounter();
140 
141  if ( result )
142  return ActionResult(0);
143  else
144  return ActionResult(21508);
145 }
146 
147 
149 {
150  MapField* fld = getMap()->getField(pos);
151  if ( !fld )
152  return ActionResult( 21002, pos );
153 
154 
155  ObjectType* ot = getMap()->getobjecttype_byid( objectID );
156  if ( !ot )
157  return ActionResult( 21501 );
158 
159  fld->addobject( ot, -1, true );
160  Object* o = fld->checkForObject( ot );
161  if ( !o )
162  return ActionResult( 21502 );
163 
164  //computeview( getMap() );
165 
166  MemoryStream memstream( objectBuffer, tnstream::reading );
167  o->read( memstream );
168 
169  for ( int i = 0; i < additionalObjectCount; ++i ) {
170  int id = memstream.readInt();
171 
172  ObjectType* aot = getMap()->getobjecttype_byid( id );
173  if ( !aot )
174  return ActionResult( 21501 );
175 
176  fld->addobject( aot, -1, true );
177  Object* o = fld->checkForObject( aot );
178  if ( !o )
179  return ActionResult( 21502 );
180 
181  o->read( memstream );
182  }
183 
184  return ActionResult(0);
185 }
186 
188 {
189  return ActionResult(0);
190 }
191 
193 {
194  delete objectBuffer;
195 }
196 
197 
198 namespace {
199  const bool r1 = registerAction<RemoveObject> ( ActionRegistry::RemoveObject );
200 }
201 
virtual ActionResult runAction(const Context &context)
An in-memory storage of streamed data.
Definition: basestrm.h:172
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
ASCString getDescription() const
void read(tnstream &stream)
Definition: objects.cpp:130
void read(tnstream &stream)
Definition: typen.h:213
GameMap * getMap()
Definition: action.h:92
virtual void writeData(tnstream &stream) const
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
Interface for removing objects from a field when it turns out that they can no longer exist...
Definition: mapfield.h:127
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
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
virtual void removeObject(MapField *fld, const ObjectType *objectType)
void write(tnstream &stream) const
Definition: typen.h:212
void write(tnstream &stream)
Definition: objects.cpp:121
An object that can be placed on fields. Roads, pipelines and ditches are examples of objects...
Definition: objecttype.h:30
Coordinate on the twodimensional map.
Definition: typen.h:202
static const int removeObjectStreamVersion
Reads data from or writes data to a MemoryStreamStorage This allows a completely volatile storage of ...
Definition: basestrm.h:204
bool addobject(const ObjectType *obj, int dir=-1, bool force=false, ObjectRemovalStrategy *objectRemovalStrategy=NULL)
add an object to the field
Definition: mapfield.cpp:212
virtual ActionResult undoAction(const Context &context)
virtual ActionResult verify()
void readfromstream(tnstream *stream)
read persisted contents from another stream
Definition: basestrm.cpp:2316
an instance of an object type (ObjectType) on the map
Definition: objects.h:46
A GameAction is an modification of something on a map.
Definition: action.h:45
virtual void readData(tnstream &stream)
ObjectType * getobjecttype_byid(int id)
Definition: gamemap.cpp:1783
Object * checkForObject(const ObjectType *o)
checks if there are objects from the given type on the field and returns them
Definition: mapfield.cpp:648
bool removeObject(const ObjectType *obj, bool force=false, ObjectRemovalStrategy *objectRemovalStrategy=NULL)
removes all objects of the given type from the field
Definition: mapfield.cpp:256
ObjectRemovalStrategy2(tnstream &objectBuffer)
virtual GameActionID getID() const
ObjectContainer objects
Definition: mapfield.h:124
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