00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "destructunitcommand.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 "destructcontainer.h"
00036 #include "../soundList.h"
00037 #include "viewregistration.h"
00038
00039
00040 bool DestructUnitCommand :: avail ( const ContainerBase* unit )
00041 {
00042 if ( !unit )
00043 return false;
00044
00045 if ( unit->getMap()->getField( unit->getPosition() )->getContainer() == unit )
00046 return unit->baseType->hasFunction( ContainerBaseType::ManualSelfDestruct ) ;
00047
00048 return false;
00049 }
00050
00051
00052
00053 DestructUnitCommand :: DestructUnitCommand ( ContainerBase* container )
00054 : ContainerCommand ( container )
00055 {
00056 if ( avail( container ))
00057 setState( SetUp );
00058 }
00059
00060
00061
00062
00063
00064 ActionResult DestructUnitCommand::go ( const Context& context )
00065 {
00066 if ( getState() != SetUp )
00067 return ActionResult(22000);
00068
00069 ContainerBase* container = getContainer();
00070 if ( !avail( container ))
00071 return ActionResult( 22101 );
00072
00073 if ( context.display ) {
00074 Vehicle* v = dynamic_cast<Vehicle*>(container);
00075 if ( v )
00076 SoundList::getInstance().playSound( SoundList::unitExplodes , 0, false, v->typ->killSoundLabel );
00077 else
00078 SoundList::getInstance().playSound( SoundList::buildingCollapses );
00079 }
00080
00081 MapCoordinate pos = container->getPosition();
00082 int viewdist = container->baseType->view;
00083
00084
00085 auto_ptr<DestructContainer> destructor ( new DestructContainer( container ));
00086 ActionResult res = destructor->execute( context );
00087 if ( res.successful() )
00088 destructor.release();
00089
00090 evaluateviewcalculation( getMap(), pos, viewdist, 0, false, &context );
00091
00092 if ( context.display )
00093 context.display->repaintDisplay();
00094
00095 return res;
00096 }
00097
00098
00099
00100 static const int DestructUnitCommandVersion = 1;
00101
00102 void DestructUnitCommand :: readData ( tnstream& stream )
00103 {
00104 ContainerCommand::readData( stream );
00105 int version = stream.readInt();
00106 if ( version > DestructUnitCommandVersion )
00107 throw tinvalidversion ( "DestructUnitCommand", DestructUnitCommandVersion, version );
00108 }
00109
00110 void DestructUnitCommand :: writeData ( tnstream& stream ) const
00111 {
00112 ContainerCommand::writeData( stream );
00113 stream.writeInt( DestructUnitCommandVersion );
00114 }
00115
00116
00117 ASCString DestructUnitCommand :: getCommandString() const
00118 {
00119 ASCString c;
00120 c.format("selfDestruct ( map, %d )", getContainerID() );
00121 return c;
00122 }
00123
00124 GameActionID DestructUnitCommand::getID() const
00125 {
00126 return ActionRegistry::DestructUnitCommand;
00127 }
00128
00129 ASCString DestructUnitCommand::getDescription() const
00130 {
00131 ASCString s = "Destruct ";
00132
00133 if ( getContainer(true) ) {
00134 s += " by " + getContainer()->getName();
00135 } else
00136 s += ASCString::toString( getContainerID() );
00137
00138 return s;
00139 }
00140
00141 namespace
00142 {
00143 const bool r1 = registerAction<DestructUnitCommand> ( ActionRegistry::DestructUnitCommand );
00144 }
00145