00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "transfercontrolcommand.h"
00023
00024 #include "../vehicle.h"
00025 #include "../mapfield.h"
00026 #include "../gamemap.h"
00027 #include "../viewcalculation.h"
00028 #include "../spfst.h"
00029 #include "../mapdisplayinterface.h"
00030 #include "action-registry.h"
00031 #include "../itemrepository.h"
00032 #include "../containercontrols.h"
00033 #include "consumeresource.h"
00034 #include "servicecommand.h"
00035 #include "convertcontainer.h"
00036
00037 bool TransferControlCommand :: avail ( const ContainerBase* item )
00038 {
00039 if ( !item )
00040 return false;
00041
00042 if ( item->getMap()->getgameparameter( cgp_disableUnitTransfer ))
00043 return false;
00044
00045 Receivers rec = getReceivers( item->getMap(), item->getOwner(), item->getCarrier() );
00046
00047 return rec.size() > 0 ;
00048 }
00049
00050
00051 TransferControlCommand::Receivers TransferControlCommand::getReceivers( GameMap* map, int currentPlayer, bool isInCarrier )
00052 {
00053 Receivers rec;
00054 if ( !map )
00055 return rec;
00056
00057 for ( int p = 0; p < map->getPlayerCount(); ++p )
00058 if ( p != currentPlayer )
00059 if ( map->player[p].exist() ) {
00060 if ( isInCarrier ) {
00061 if ( map->player[p].diplomacy.getState( currentPlayer ) >= ALLIANCE )
00062 rec.push_back( &map->player[p] );
00063 } else {
00064 if ( map->player[p].diplomacy.getState( currentPlayer ) >= PEACE )
00065 rec.push_back( &map->player[p] );
00066 }
00067 }
00068
00069 return rec;
00070 }
00071
00072
00073 TransferControlCommand::Receivers TransferControlCommand::getReceivers()
00074 {
00075 return getReceivers( getContainer()->getMap(), getContainer()->getOwner(), getContainer()->getCarrier() );
00076 }
00077
00078
00079 TransferControlCommand :: TransferControlCommand ( ContainerBase* container )
00080 : ContainerCommand ( container ), receivingPlayer(-1)
00081 {
00082
00083 }
00084
00085
00086
00087
00088
00089 ActionResult TransferControlCommand::go ( const Context& context )
00090 {
00091 if ( getState() != SetUp )
00092 return ActionResult(22000);
00093
00094 if ( !avail( getContainer() ))
00095 return ActionResult(22800);
00096
00097 bool found = false;
00098 Receivers rec = getReceivers();
00099 for ( Receivers::const_iterator i = rec.begin(); i != rec.end(); ++i )
00100 if ( (*i)->getPosition() == receivingPlayer )
00101 found = true;
00102
00103 if ( !found )
00104 return ActionResult(22801);
00105
00106 auto_ptr<ConvertContainer> cc ( new ConvertContainer( getContainer(), receivingPlayer ));
00107 ActionResult res = cc->execute(context);
00108
00109 if ( res.successful() ) {
00110 cc.release();
00111 setState( Finished );
00112
00113 computeview( getMap(), 0, false, &context );
00114
00115 if ( context.display )
00116 context.display->repaintDisplay();
00117 } else
00118 setState( Failed );
00119
00120 return res;
00121 }
00122
00123
00124
00125 static const int TransferControlCommandVersion = 1;
00126
00127 void TransferControlCommand :: readData ( tnstream& stream )
00128 {
00129 ContainerCommand::readData( stream );
00130 int version = stream.readInt();
00131 if ( version > TransferControlCommandVersion )
00132 throw tinvalidversion ( "TransferControlCommand", TransferControlCommandVersion, version );
00133 receivingPlayer = stream.readInt();
00134 }
00135
00136 void TransferControlCommand :: writeData ( tnstream& stream ) const
00137 {
00138 ContainerCommand::writeData( stream );
00139 stream.writeInt( TransferControlCommandVersion );
00140 stream.writeInt( receivingPlayer );
00141 }
00142
00143 void TransferControlCommand :: setReceiver( const Player* receiver )
00144 {
00145 if ( receiver ) {
00146 receivingPlayer = receiver->getPosition();
00147 setState( SetUp );
00148 }
00149 }
00150
00151
00152 ASCString TransferControlCommand :: getCommandString() const
00153 {
00154 ASCString c;
00155 c.format("transferControl ( map, %d, %d )", getContainerID(), receivingPlayer );
00156 return c;
00157
00158 }
00159
00160 GameActionID TransferControlCommand::getID() const
00161 {
00162 return ActionRegistry::TransferControlCommand;
00163 }
00164
00165 ASCString TransferControlCommand::getDescription() const
00166 {
00167 ASCString s = "Transfer Control";
00168
00169 if ( getContainer(true) ) {
00170 s += " of " + getContainer()->getName();
00171 }
00172
00173 s += "to " + getMap()->getPlayer( receivingPlayer ).getName();
00174 return s;
00175 }
00176
00177 namespace
00178 {
00179 const bool r1 = registerAction<TransferControlCommand> ( ActionRegistry::TransferControlCommand );
00180 }
00181