removemine.cpp

Go to the documentation of this file.
00001 /*
00002      This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003      Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00004  
00005      This program is free software; you can redistribute it and/or modify
00006      it under the terms of the GNU General Public License as published by
00007      the Free Software Foundation; either version 2 of the License, or
00008      (at your option) any later version.
00009  
00010      This program is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013      GNU General Public License for more details.
00014  
00015      You should have received a copy of the GNU General Public License
00016      along with this program; see the file COPYING. If not, write to the 
00017      Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00018      Boston, MA  02111-1307  USA
00019 */
00020 
00021 
00022 #include "removemine.h"
00023 #include "action-registry.h"
00024 
00025 #include "../vehicle.h"
00026 #include "../gamemap.h"
00027      
00028 RemoveMine::RemoveMine( GameMap* gamemap, const MapCoordinate& position, int mineID )
00029    : GameAction( gamemap ), pos(position), layer(-1), mineBuffer(NULL)
00030 {
00031    this->mineID = mineID;
00032 }
00033       
00034       
00035 ASCString RemoveMine::getDescription() const
00036 {
00037    ASCString res = "Remove mine at " + pos.toString(false) + " with id " + ASCString::toString(mineID);
00038    return  res;
00039 }
00040       
00041       
00042 void RemoveMine::readData ( tnstream& stream ) 
00043 {
00044    int version = stream.readInt();
00045    if ( version != 1 )
00046       throw tinvalidversion ( "RemoveMine", 1, version );
00047    
00048    mineID = stream.readInt();
00049    layer = stream.readInt();
00050    pos.read( stream );
00051    
00052    if ( stream.readInt() ) {
00053       mineBuffer = new MemoryStreamStorage();
00054       mineBuffer->readfromstream( &stream );  
00055    } else
00056       mineBuffer = NULL;
00057    
00058 };
00059       
00060       
00061 void RemoveMine::writeData ( tnstream& stream ) const
00062 {
00063    stream.writeInt( 1 );
00064    stream.writeInt( mineID );
00065    stream.writeInt( layer );
00066    pos.write( stream );
00067    
00068    if ( mineBuffer ) {
00069       stream.writeInt( 1 );
00070       mineBuffer->writetostream( &stream );
00071    } else
00072       stream.writeInt( 0 );
00073    
00074 };
00075 
00076 
00077 GameActionID RemoveMine::getID() const
00078 {
00079    return ActionRegistry::RemoveMine;
00080 }
00081 
00082 ActionResult RemoveMine::runAction( const Context& context )
00083 {
00084    layer = 0;
00085    MapField* fld = getMap()->getField(pos);
00086    if ( !fld )
00087       return ActionResult( 21002, pos );
00088    
00089    if ( mineID > 0 ) {
00090       for ( MapField::MineContainer::iterator i = fld->mines.begin(); i != fld->mines.end(); ++i ) {
00091          if ( i->identifier == mineID ) {
00092             mineBuffer = new MemoryStreamStorage();
00093             MemoryStream memstream( mineBuffer, tnstream::writing );
00094             i->write( memstream );
00095             
00096             fld->mines.erase( i );
00097             return ActionResult(0);
00098          }
00099          ++layer;
00100       }
00101    } else {
00102       layer = fld->mines.size();
00103       mineBuffer = new MemoryStreamStorage();
00104       MemoryStream memstream( mineBuffer, tnstream::writing );
00105       for ( MapField::MineContainer::iterator i = fld->mines.begin(); i != fld->mines.end(); ++i ) 
00106          i->write( memstream );
00107       fld->mines.clear();
00108       return ActionResult(0);
00109    }
00110       
00111   return ActionResult( 21401 );
00112 }
00113 
00114 
00115 ActionResult RemoveMine::undoAction( const Context& context )
00116 {
00117    MapField* fld = getMap()->getField(pos);
00118    if ( !fld )
00119       return ActionResult( 21002, pos );
00120    
00121    if ( layer < 0 )
00122       throw ActionResult( 21401 );
00123    
00124    if ( mineID > 0 ) {
00125       MapField::MineContainer::iterator i = fld->mines.begin();
00126       
00127       int l = layer;
00128       while ( l-- )
00129          ++i;
00130       
00131       MemoryStream memstream( mineBuffer, tnstream::reading );
00132       fld->mines.insert( i, Mine::newFromStream( memstream ));
00133    } else {
00134       MemoryStream memstream( mineBuffer, tnstream::reading );
00135       for ( int i = 0; i < layer; ++i )
00136          fld->mines.push_back( Mine::newFromStream( memstream ));
00137    }
00138    return ActionResult(0);
00139 }
00140 
00141 ActionResult RemoveMine::verify()
00142 {
00143    return ActionResult(0);
00144 }
00145 
00146 RemoveMine::~RemoveMine()
00147 {
00148    delete mineBuffer;  
00149 }
00150 
00151 
00152 namespace {
00153    const bool r1 = registerAction<RemoveMine> ( ActionRegistry::RemoveMine );
00154 }
00155 

Generated on Mon May 21 01:26:36 2012 for Advanced Strategic Command by  doxygen 1.5.1