00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "cargomovecommand.h"
00023
00024 #include "../vehicle.h"
00025 #include "../gamemap.h"
00026 #include "../mapdisplayinterface.h"
00027 #include "action-registry.h"
00028 #include "../containercontrols.h"
00029 #include "unitfieldregistration.h"
00030
00031
00032 bool CargoMoveCommand :: moveOutAvail( const Vehicle* movingUnit )
00033 {
00034 if ( !movingUnit )
00035 return false;
00036
00037 ContainerBase* carr = movingUnit ->getCarrier();
00038 if ( carr ) {
00039 ContainerBase* carr2 = carr->getCarrier();
00040 if ( carr2 )
00041 return carr2->vehicleFit( movingUnit );
00042 }
00043 return false;
00044
00045 }
00046
00047 bool CargoMoveCommand :: moveInAvail( const Vehicle* movingUnit , Vehicle* newCarrier )
00048 {
00049 if ( movingUnit && newCarrier )
00050 return newCarrier->vehicleFit( movingUnit );
00051 else
00052 return false;
00053 }
00054
00055
00056 vector<Vehicle*> CargoMoveCommand :: getTargetCarriers()
00057 {
00058 vector<Vehicle*> targets;
00059
00060 ContainerBase* container = getUnit()->getCarrier();
00061 if ( !container )
00062 return targets;
00063
00064 for ( ContainerBase::Cargo::const_iterator i = container->getCargo().begin(); i != container->getCargo().end(); ++i )
00065 if ( *i != getUnit() && *i )
00066 if ( moveInAvail ( getUnit(), *i ))
00067 targets.push_back( *i );
00068
00069 return targets;
00070 }
00071
00072
00073 CargoMoveCommand :: CargoMoveCommand ( Vehicle* unit )
00074 : UnitCommand ( unit ), mode( undefined ), targetCarrier(-1)
00075 {
00076
00077 }
00078
00079
00080
00081
00082
00083 ActionResult CargoMoveCommand::go ( const Context& context )
00084 {
00085 if ( getState() != SetUp )
00086 return ActionResult(22000);
00087
00088 if( mode == undefined )
00089 return ActionResult(21900);
00090
00091 if ( mode == moveOutwards ) {
00092 if ( !moveOutAvail( getUnit() ) )
00093 return ActionResult( 21901 );
00094
00095 Vehicle* targetContainer1 = dynamic_cast<Vehicle*>(getUnit()->getCarrier());
00096 if ( !targetContainer1 )
00097 return ActionResult( 21902 );
00098
00099 ContainerBase* targetContainer2 = targetContainer1->getCarrier();
00100 if ( !targetContainer2 )
00101 return ActionResult( 21902 );
00102
00103 ActionResult res = (new UnitFieldRegistration( getUnit(), getUnit()->getPosition(), UnitFieldRegistration::UnRegisterFromCarrier, getUnit()->getCarrier() ))->execute ( context );
00104 if ( !res.successful() )
00105 return res;
00106
00107 res = (new UnitFieldRegistration( getUnit(), getUnit()->getPosition(), UnitFieldRegistration::RegisterInCarrier, targetContainer2 ))->execute ( context );
00108 return res;
00109
00110 } else {
00111 Vehicle* targetContainer = getMap()->getUnit( targetCarrier );
00112 if ( !targetContainer )
00113 return ActionResult( 21902 );
00114
00115 if ( !moveInAvail( getUnit(), targetContainer ) )
00116 return ActionResult( 21901 );
00117
00118 ActionResult res = (new UnitFieldRegistration( getUnit(), getUnit()->getPosition(), UnitFieldRegistration::UnRegisterFromCarrier, getUnit()->getCarrier() ))->execute ( context );
00119 if ( !res.successful() )
00120 return res;
00121
00122 res = (new UnitFieldRegistration( getUnit(), getUnit()->getPosition(), UnitFieldRegistration::RegisterInCarrier, targetContainer ))->execute ( context );
00123 return res;
00124 }
00125
00126 }
00127
00128 void CargoMoveCommand :: setMode( Mode mode )
00129 {
00130 this->mode = mode;
00131 if ( mode == moveOutwards || targetCarrier > 0 )
00132 setState( SetUp );
00133 };
00134
00135
00136 static const int CargoMoveCommandVersion = 1;
00137
00138 void CargoMoveCommand :: readData ( tnstream& stream )
00139 {
00140 UnitCommand::readData( stream );
00141 int version = stream.readInt();
00142 if ( version > CargoMoveCommandVersion )
00143 throw tinvalidversion ( "CargoMoveCommand", CargoMoveCommandVersion, version );
00144 targetCarrier = stream.readInt();
00145 mode = (Mode) stream.readInt();
00146 }
00147
00148 void CargoMoveCommand :: writeData ( tnstream& stream ) const
00149 {
00150 UnitCommand::writeData( stream );
00151 stream.writeInt( CargoMoveCommandVersion );
00152 stream.writeInt( targetCarrier );
00153 stream.writeInt( mode );
00154 }
00155
00156
00157 void CargoMoveCommand :: setTargetCarrier( Vehicle* targetCarrier )
00158 {
00159 if ( targetCarrier ) {
00160 this->targetCarrier = targetCarrier->networkid;
00161 if ( mode == moveInwards )
00162 setState( SetUp );
00163 } else
00164 this->targetCarrier = -1;
00165 }
00166
00167
00168 ASCString CargoMoveCommand :: getCommandString() const
00169 {
00170 if ( mode == moveOutwards ) {
00171 ASCString c;
00172 c.format("cargoUnitMove ( map, %d, -1 )", getUnitID() );
00173 return c;
00174 }
00175 if ( mode == moveInwards ) {
00176 ASCString c;
00177 c.format("cargoUnitMove ( map, %d, %d )", getUnitID(), targetCarrier );
00178 return c;
00179 }
00180 return "";
00181 }
00182
00183 GameActionID CargoMoveCommand::getID() const
00184 {
00185 return ActionRegistry::CargoMoveCommand;
00186 }
00187
00188 ASCString CargoMoveCommand::getDescription() const
00189 {
00190 ASCString s = "Move ";
00191 if ( getUnit() )
00192 s += getUnit()->getName() ;
00193 else
00194 s += "unit nwid " + ASCString::toString(getUnitID() );
00195
00196 if ( mode == moveOutwards )
00197 s += " into outer carrier ";
00198 else
00199 s += " into inner carrier with ID " + ASCString::toString( targetCarrier );
00200
00201 if ( getUnit() )
00202 s += " at " + getUnit()->getPosition().toString();
00203
00204 return s;
00205 }
00206
00207 namespace
00208 {
00209 const bool r1 = registerAction<CargoMoveCommand> ( ActionRegistry::CargoMoveCommand );
00210 }
00211