spawnmine.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 "spawnmine.h"
00023 #include "action-registry.h"
00024 
00025 #include "../vehicle.h"
00026 #include "../gamemap.h"
00027      
00028 SpawnMine::SpawnMine( GameMap* gamemap, const MapCoordinate& position, MineTypes mineType, int owner, int strength )
00029    : GameAction( gamemap ), pos(position)
00030 {
00031    this->type = mineType;
00032    this->owner = owner;
00033    this->strength = strength;
00034    stackPosition = -1;
00035    mapNetworkIdCounterAfter = -1;
00036    mapNetworkIdCounterBefore = -1;
00037 }
00038       
00039       
00040 ASCString SpawnMine::getDescription() const
00041 {
00042    return "Spawn mine at position " + pos.toString();
00043 }
00044       
00045 static const int mineDataVersion = 2;      
00046       
00047 void SpawnMine::readData ( tnstream& stream ) 
00048 {
00049    int version = stream.readInt();
00050    if ( version < 1 || version > mineDataVersion )
00051       throw tinvalidversion ( "SpawnMine", mineDataVersion, version );
00052    
00053    type = (MineTypes) stream.readInt();
00054    pos.read( stream );
00055    owner = stream.readInt();
00056    strength = stream.readInt();
00057    stackPosition = stream.readInt();
00058    if ( version >= 2 ) {
00059       mapNetworkIdCounterBefore = stream.readInt();
00060       mapNetworkIdCounterAfter = stream.readInt();
00061    }
00062 };
00063       
00064       
00065 void SpawnMine::writeData ( tnstream& stream ) const
00066 {
00067    stream.writeInt( mineDataVersion );
00068    stream.writeInt( type );
00069    pos.write( stream );
00070    stream.writeInt( owner );
00071    stream.writeInt( strength ); 
00072    stream.writeInt( stackPosition );
00073    stream.writeInt( mapNetworkIdCounterBefore );
00074    stream.writeInt( mapNetworkIdCounterAfter );
00075 };
00076 
00077 
00078 GameActionID SpawnMine::getID() const
00079 {
00080    return ActionRegistry::SpawnMine;
00081 }
00082 
00083 ActionResult SpawnMine::runAction( const Context& context )
00084 {
00085    MapField* fld = getMap()->getField(pos);
00086    if ( !fld )
00087       return ActionResult( 21002, pos );
00088    
00089    mapNetworkIdCounterBefore = getMap()->idManager.unitnetworkid;
00090    
00091    bool result = fld->putmine( owner, type, strength );
00092    
00093    mapNetworkIdCounterAfter = getMap()->idManager.unitnetworkid;
00094    
00095    
00096    if ( result ) {
00097       stackPosition = fld->mines.size();
00098       return ActionResult(0);
00099    } else
00100       return ActionResult(21600);
00101 }
00102 
00103 
00104 ActionResult SpawnMine::undoAction( const Context& context )
00105 {
00106    MapField* fld = getMap()->getField(pos);
00107    if ( !fld )
00108       return ActionResult( 21002, pos );
00109    
00110    if ( fld->mines.size() != stackPosition )
00111       return ActionResult(21601);
00112    
00113    if ( getMap()->idManager.unitnetworkid != mapNetworkIdCounterAfter )
00114       return ActionResult( 21605 );
00115    
00116    getMap()->idManager.unitnetworkid = mapNetworkIdCounterBefore;
00117    
00118    fld->mines.pop_back();
00119    
00120    
00121    
00122    return ActionResult(0);
00123 }
00124 
00125 ActionResult SpawnMine::verify()
00126 {
00127    if ( getMap()->idManager.unitnetworkid != mapNetworkIdCounterAfter )
00128       return ActionResult( 21605 );
00129 
00130    return ActionResult(0);
00131 }
00132 
00133 
00134 namespace {
00135    const bool r1 = registerAction<SpawnMine> ( ActionRegistry::SpawnMine );
00136 }
00137 

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