00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "removeobject.h"
00023 #include "action-registry.h"
00024
00025 #include "../vehicle.h"
00026 #include "../gamemap.h"
00027 #include "../viewcalculation.h"
00028
00029 RemoveObject::RemoveObject( GameMap* gamemap, const MapCoordinate& position, int objectID )
00030 : GameAction( gamemap ), pos(position), objectBuffer(NULL)
00031 {
00032 this->objectID = objectID;
00033 additionalObjectCount = 0;
00034 }
00035
00036
00037 ASCString RemoveObject::getDescription() const
00038 {
00039 ASCString res = "Remove object with id " + ASCString::toString(objectID) + " at " + pos.toString(false);
00040 return res;
00041 }
00042
00043
00044 static const int removeObjectStreamVersion = 2;
00045
00046 void RemoveObject::readData ( tnstream& stream )
00047 {
00048 int version = stream.readInt();
00049 if ( version < 1 || version > removeObjectStreamVersion )
00050 throw tinvalidversion ( "RemoveObject", removeObjectStreamVersion, version );
00051
00052 objectID = stream.readInt();
00053 pos.read( stream );
00054
00055 if ( stream.readInt() ) {
00056 objectBuffer = new MemoryStreamStorage();
00057 objectBuffer->readfromstream( &stream );
00058 } else
00059 objectBuffer = NULL;
00060
00061 if ( version >= 2 )
00062 additionalObjectCount = stream.readInt();
00063 else
00064 additionalObjectCount = 0;
00065 };
00066
00067
00068 void RemoveObject::writeData ( tnstream& stream ) const
00069 {
00070 stream.writeInt( removeObjectStreamVersion );
00071 stream.writeInt( objectID );
00072 pos.write( stream );
00073
00074 if ( objectBuffer ) {
00075 stream.writeInt( 1 );
00076 objectBuffer->writetostream( &stream );
00077 } else
00078 stream.writeInt( 0 );
00079
00080 stream.writeInt( additionalObjectCount );
00081 };
00082
00083
00084 GameActionID RemoveObject::getID() const
00085 {
00086 return ActionRegistry::RemoveObject;
00087 }
00088
00089
00090 class ObjectRemovalStrategy2 : public MapField::ObjectRemovalStrategy {
00091 tnstream& buffer;
00092 int counter;
00093 public:
00094 ObjectRemovalStrategy2( tnstream& objectBuffer )
00095 : buffer( objectBuffer ), counter( 0 )
00096 {}
00097
00098 virtual void removeObject( MapField* fld, const ObjectType* objectType ) {
00099 for ( MapField::ObjectContainer::iterator o = fld->objects.begin(); o != fld->objects.end(); ) {
00100 if ( o->typ == objectType ) {
00101 buffer.writeInt( o->typ->id );
00102 o->write( buffer );
00103 ++counter;
00104 o = fld->objects.erase( o );
00105 } else
00106 ++o ;
00107 }
00108 };
00109
00110 int getCounter()
00111 {
00112 return counter;
00113 }
00114
00115 };
00116
00117
00118 ActionResult RemoveObject::runAction( const Context& context )
00119 {
00120 MapField* fld = getMap()->getField(pos);
00121 if ( !fld )
00122 return ActionResult( 21002, pos );
00123
00124 ObjectType* ot = getMap()->getobjecttype_byid( objectID );
00125 if ( !ot )
00126 return ActionResult( 21501 );
00127
00128 Object* o = fld->checkForObject( ot );
00129 if ( !o )
00130 return ActionResult( 21502 );
00131
00132 objectBuffer = new MemoryStreamStorage();
00133 MemoryStream memstream( objectBuffer, tnstream::writing );
00134 o->write( memstream );
00135
00136 ObjectRemovalStrategy2 removalStrat( memstream );
00137
00138 bool result = fld->removeObject( ot, true, &removalStrat );
00139 additionalObjectCount = removalStrat.getCounter();
00140
00141 if ( result )
00142 return ActionResult(0);
00143 else
00144 return ActionResult(21508);
00145 }
00146
00147
00148 ActionResult RemoveObject::undoAction( const Context& context )
00149 {
00150 MapField* fld = getMap()->getField(pos);
00151 if ( !fld )
00152 return ActionResult( 21002, pos );
00153
00154
00155 ObjectType* ot = getMap()->getobjecttype_byid( objectID );
00156 if ( !ot )
00157 return ActionResult( 21501 );
00158
00159 fld->addobject( ot, -1, true );
00160 Object* o = fld->checkForObject( ot );
00161 if ( !o )
00162 return ActionResult( 21502 );
00163
00164
00165
00166 MemoryStream memstream( objectBuffer, tnstream::reading );
00167 o->read( memstream );
00168
00169 for ( int i = 0; i < additionalObjectCount; ++i ) {
00170 int id = memstream.readInt();
00171
00172 ObjectType* aot = getMap()->getobjecttype_byid( id );
00173 if ( !aot )
00174 return ActionResult( 21501 );
00175
00176 fld->addobject( aot, -1, true );
00177 Object* o = fld->checkForObject( aot );
00178 if ( !o )
00179 return ActionResult( 21502 );
00180
00181 o->read( memstream );
00182 }
00183
00184 return ActionResult(0);
00185 }
00186
00187 ActionResult RemoveObject::verify()
00188 {
00189 return ActionResult(0);
00190 }
00191
00192 RemoveObject::~RemoveObject()
00193 {
00194 delete objectBuffer;
00195 }
00196
00197
00198 namespace {
00199 const bool r1 = registerAction<RemoveObject> ( ActionRegistry::RemoveObject );
00200 }
00201