convertcontainer.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 "convertcontainer.h"
00023 #include "destructcontainer.h"
00024 #include "action-registry.h"
00025 
00026 #include "../vehicle.h"
00027 
00028 ConvertContainer::ConvertContainer( ContainerBase* container, int newPlayer )
00029    : ContainerAction( container ), newOwner(newPlayer), previousOwner(-1)
00030 {
00031 }
00032       
00033 ASCString ConvertContainer::getDescription() const
00034 {
00035    ASCString res = "Change owner of " + getContainer()->getName();
00036    res += " to " + ASCString::toString(newOwner);
00037    return  res;
00038 }
00039       
00040       
00041 void ConvertContainer::readData ( tnstream& stream ) 
00042 {
00043    ContainerAction::readData( stream );
00044    int version = stream.readInt();
00045    if ( version != 1 )
00046       throw tinvalidversion ( "ConvertContainer", 1, version );
00047    
00048    newOwner = stream.readInt();
00049    previousOwner = stream.readInt();
00050 };
00051       
00052       
00053 void ConvertContainer::writeData ( tnstream& stream ) const
00054 {
00055    ContainerAction::writeData( stream );
00056    stream.writeInt( 1 );
00057    stream.writeInt( newOwner );
00058    stream.writeInt( previousOwner );
00059 };
00060 
00061 
00062 GameActionID ConvertContainer::getID() const
00063 {
00064    return ActionRegistry::ConvertContainer;
00065 }
00066 
00067 
00068 ActionResult ConvertContainer::runAction( const Context& context )
00069 {
00070    ContainerBase* c = getContainer();
00071    previousOwner = c->getOwner();
00072    
00073    if ( c->baseType->hasFunction( ContainerBaseType::SelfDestructOnConquer  ) ) {
00074       (new DestructContainer(c))->execute(context);
00075       return ActionResult(0);
00076    }
00077 
00078    c->registerForNewOwner( newOwner );
00079    
00080    for ( ContainerBase::Cargo::iterator i = c->cargo.begin(); i != c->cargo.end(); ++i )
00081       if ( *i ) 
00082          (new ConvertContainer( *i, newOwner ))->execute(context);
00083    
00084    
00085    c->conquered();
00086    ContainerBase::anyContainerConquered(c);
00087    
00088    return ActionResult(0);
00089 }
00090 
00091 
00092 ActionResult ConvertContainer::undoAction( const Context& context )
00093 {
00094    getContainer()->registerForNewOwner( previousOwner );
00095    return ActionResult(0);
00096 }
00097 
00098 ActionResult ConvertContainer::preCheck()
00099 {
00100    if ( getContainer()->getOwner() != previousOwner  )
00101       throw ActionResult( 21204, getContainer(), "owner" );
00102    
00103    return ActionResult(0);
00104 }
00105 
00106 ActionResult ConvertContainer::postCheck()
00107 {
00108    if ( getContainer()->getOwner() != newOwner  )
00109       throw ActionResult( 21204, getContainer(), "owner" );
00110    
00111    return ActionResult(0);
00112 }
00113 
00114 
00115 namespace {
00116    const bool r1 = registerAction<ConvertContainer> ( ActionRegistry::ConvertContainer );
00117 }

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