destructunitcommand.cpp

Go to the documentation of this file.
00001 /*
00002      This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003      Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00004 
00005      This program is free software; you can redistribute it and/or modify
00006      it under the terms of the GNU General Public License as published by
00007      the Free Software Foundation; either version 2 of the License, or
00008      (at your option) any later version.
00009 
00010      This program is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013      GNU General Public License for more details.
00014 
00015      You should have received a copy of the GNU General Public License
00016      along with this program; see the file COPYING. If not, write to the
00017      Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00018      Boston, MA  02111-1307  USA
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 

Generated on Mon May 21 01:26:30 2012 for Advanced Strategic Command by  doxygen 1.5.1