00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "internalammotransfercommand.h"
00023
00024 #include "../vehicle.h"
00025 #include "../mapfield.h"
00026 #include "../gamemap.h"
00027 #include "../viewcalculation.h"
00028 #include "../spfst.h"
00029 #include "changeunitmovement.h"
00030 #include "../mapdisplayinterface.h"
00031 #include "action-registry.h"
00032 #include "../reactionfire.h"
00033 #include "../soundList.h"
00034 #include "consumeammo.h"
00035 #include "servicecommand.h"
00036
00037 bool InternalAmmoTransferCommand :: avail ( const Vehicle* unit )
00038 {
00039 if ( unit ) {
00040 for( int i=0; i<unit->typ->weapons.count; i++ )
00041 {
00042 const SingleWeapon* weapon = unit->getWeapon( i );
00043 if( weapon->canRefuel() )
00044 return true;
00045
00046 for( int j=0; j<i; j++ )
00047 {
00048 const SingleWeapon* otherWeapon = unit->getWeapon( j );
00049 if( weapon->equals( otherWeapon ) )
00050 return true;
00051 }
00052 }
00053 return false;
00054 }
00055
00056 return false;
00057 }
00058
00059
00060 InternalAmmoTransferCommand :: InternalAmmoTransferCommand ( Vehicle* unit)
00061 : UnitCommand ( unit )
00062 {
00063
00064 }
00065
00066
00067
00068 bool InternalAmmoTransferCommand::checkConsistency()
00069 {
00070 map<int,int> amount;
00071
00072 Vehicle* v = getUnit();
00073 for ( int w = 0; w < v->typ->weapons.count; ++w ) {
00074 if ( ammoAmount.size() < w )
00075 return false;
00076
00077 int type = v->typ->weapons.weapon[w].getScalarWeaponType();
00078 amount[type] += v->ammo[w] - ammoAmount[w];
00079 }
00080
00081 for ( map<int,int>::iterator i = amount.begin(); i != amount.end(); ++i )
00082 if ( i->second != 0 )
00083 return false;
00084
00085 return true;
00086
00087 }
00088
00089
00090 bool InternalAmmoTransferCommand::setAmmounts( const vector<int>& ammo )
00091 {
00092 ammoAmount = ammo;
00093
00094 return checkConsistency();
00095 }
00096
00097
00098 ActionResult InternalAmmoTransferCommand::go ( const Context& context )
00099 {
00100 if ( getState() != SetUp )
00101 return ActionResult(22000);
00102
00103 Vehicle* unit = getUnit();
00104
00105 if ( !avail( unit ))
00106 return ActionResult( 22600 );
00107
00108 if ( !checkConsistency())
00109 return ActionResult( 23300 );
00110
00111 for ( int w = 0; w < unit->typ->weapons.count; ++w ) {
00112 if ( unit->ammo[w] != ammoAmount[w] ) {
00113 auto_ptr<ConsumeAmmo> cr ( new ConsumeAmmo( unit, unit->typ->weapons.weapon[w].getScalarWeaponType(), w, unit->ammo[w] - ammoAmount[w] ));
00114 ActionResult res = cr->execute( context );
00115 if ( !res.successful() )
00116 return res;
00117 else
00118 cr.release();
00119 }
00120 }
00121
00122 setState( Finished );
00123
00124 return ActionResult(0);
00125 }
00126
00127
00128
00129 static const int InternalAmmoTransferCommandVersion = 1;
00130
00131 void InternalAmmoTransferCommand :: readData ( tnstream& stream )
00132 {
00133 UnitCommand::readData( stream );
00134 int version = stream.readInt();
00135 if ( version > InternalAmmoTransferCommandVersion )
00136 throw tinvalidversion ( "InternalAmmoTransferCommand", InternalAmmoTransferCommandVersion, version );
00137
00138 readClassContainer( ammoAmount, stream );
00139 }
00140
00141 void InternalAmmoTransferCommand :: writeData ( tnstream& stream ) const
00142 {
00143 UnitCommand::writeData( stream );
00144 stream.writeInt( InternalAmmoTransferCommandVersion );
00145 writeClassContainer( ammoAmount, stream );
00146 }
00147
00148
00149 ASCString InternalAmmoTransferCommand :: getCommandString() const
00150 {
00151 ASCString c;
00152 c.format("InternalAmmoTransfer ( %d )", getUnitID() );
00153 return c;
00154
00155 }
00156
00157 GameActionID InternalAmmoTransferCommand::getID() const
00158 {
00159 return ActionRegistry::InternalAmmoTransferCommand;
00160 }
00161
00162 ASCString InternalAmmoTransferCommand::getDescription() const
00163 {
00164 ASCString s = "InternalAmmoTransfer ";
00165
00166 if ( getUnit())
00167 s += " for unit " + getUnit()->getName();
00168
00169 return s;
00170 }
00171
00172 namespace
00173 {
00174 const bool r1 = registerAction<InternalAmmoTransferCommand> ( ActionRegistry::InternalAmmoTransferCommand );
00175 }
00176