00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "powergenerationswitchcommand.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 "changeunitproperty.h"
00032
00033
00034 bool PowerGenerationSwitchCommand :: avail ( const Vehicle* unit, bool newState )
00035 {
00036 if ( !unit )
00037 return false;
00038
00039 if ( unit->getOwner() == unit->getMap()->actplayer &&
00040 ( unit->typ->hasFunction( ContainerBaseType::MatterConverter )))
00041 if ( unit->getGeneratorStatus() != newState )
00042 return true;
00043
00044 return false;
00045 }
00046
00047
00048
00049 PowerGenerationSwitchCommand :: PowerGenerationSwitchCommand ( Vehicle* container )
00050 : UnitCommand ( container )
00051 {
00052 }
00053
00054 void PowerGenerationSwitchCommand :: setNewState( bool enabled )
00055 {
00056 newState = enabled;
00057 setState( SetUp );
00058 }
00059
00060
00061
00062
00063 ActionResult PowerGenerationSwitchCommand::go ( const Context& context )
00064 {
00065 if ( getState() != SetUp )
00066 return ActionResult(22000);
00067
00068 Vehicle* unit = getUnit();
00069 if ( !avail( unit, newState ))
00070 return ActionResult( 22300 );
00071
00072 auto_ptr<ChangeUnitProperty> propChange ( new ChangeUnitProperty( getUnit(), ChangeUnitProperty::PowerGeneration, newState ));
00073 ActionResult res = propChange->execute( context );
00074
00075 if ( res.successful() )
00076 propChange.release();
00077
00078 return res;
00079 }
00080
00081
00082
00083 static const int PowerGenerationSwitchCommandVersion = 1;
00084
00085 void PowerGenerationSwitchCommand :: readData ( tnstream& stream )
00086 {
00087 UnitCommand::readData( stream );
00088 int version = stream.readInt();
00089 if ( version > PowerGenerationSwitchCommandVersion )
00090 throw tinvalidversion ( "PowerGenerationSwitchCommand", PowerGenerationSwitchCommandVersion, version );
00091 newState = stream.readInt();
00092 }
00093
00094 void PowerGenerationSwitchCommand :: writeData ( tnstream& stream ) const
00095 {
00096 UnitCommand::writeData( stream );
00097 stream.writeInt( PowerGenerationSwitchCommandVersion );
00098 stream.writeInt( newState );
00099 }
00100
00101
00102 ASCString PowerGenerationSwitchCommand :: getCommandString() const
00103 {
00104 ASCString c;
00105 c.format("unitPowerGenerationEnable ( map, %d, %d )", getUnitID(), newState );
00106 return c;
00107
00108 }
00109
00110 GameActionID PowerGenerationSwitchCommand::getID() const
00111 {
00112 return ActionRegistry::PowerGenerationSwitchCommand;
00113 }
00114
00115 ASCString PowerGenerationSwitchCommand::getDescription() const
00116 {
00117 ASCString s = newState ? "Enable " : "Disable ";
00118 s += "power generation ";
00119
00120 if ( getMap()->getUnit( getUnitID() ))
00121 s += " for " + getMap()->getUnit( getUnitID() )->getName();
00122
00123 return s;
00124 }
00125
00126 namespace
00127 {
00128 const bool r1 = registerAction<PowerGenerationSwitchCommand> ( ActionRegistry::PowerGenerationSwitchCommand );
00129 }
00130