00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 }