reactionfireswitchcommand.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 "reactionfireswitchcommand.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 #include "changeunitmovement.h"
00033 
00034 
00035 bool ReactionFireSwitchCommand :: avail ( const Vehicle* unit, bool newState )
00036 {
00037    if ( !unit )
00038       return false;
00039 
00040    if ( newState == true ) {
00041       if ( unit->getMap()->getField( unit->getPosition() )->getContainer() == unit ) 
00042          if ( !unit->baseType->hasFunction(ContainerBaseType::NoReactionfire ))
00043             if ( unit->reactionfire.getStatus() == Vehicle::ReactionFire::off )
00044                if ( unit->weapexist() )
00045                   for ( int i = 0; i < unit->typ->weapons.count; ++i )
00046                      if ( unit->typ->weapons.weapon[i].offensive() && unit->typ->weapons.weapon[i].reactionFireShots )
00047                         return true;
00048    } else {
00049       if ( unit->reactionfire.getStatus() != Vehicle::ReactionFire::off )
00050          return true;         
00051    }
00052    
00053    return false;
00054 }
00055 
00056 
00057 
00058 ReactionFireSwitchCommand :: ReactionFireSwitchCommand ( Vehicle* container )
00059    : UnitCommand ( container )
00060 {
00061 }
00062 
00063 void ReactionFireSwitchCommand :: setNewState( bool enabled )
00064 {
00065    newRFstate = enabled;
00066    setState( SetUp );  
00067 }
00068 
00069 
00070  
00071 
00072 ActionResult ReactionFireSwitchCommand::go ( const Context& context )
00073 {
00074    if ( getState() != SetUp )
00075       return ActionResult(22000);
00076 
00077    Vehicle* unit = getUnit();
00078    if ( !avail( unit, newRFstate ))
00079       return ActionResult( 22300 );
00080    
00081    
00082    if ( newRFstate == true ) {
00083          
00084       auto_ptr<ChangeUnitProperty> propChange ( new ChangeUnitProperty( getUnit(), ChangeUnitProperty::ReactionFire, (int) Vehicle::ReactionFire::init2 ));
00085       ActionResult res = propChange->execute( context );
00086       
00087       if ( res.successful() ) {
00088          propChange.release();
00089          if ( context.display )
00090             context.display->repaintDisplay();
00091       }
00092       
00093       return res;
00094    } else {
00095       if (     unit->reactionfire.getStatus() != Vehicle::ReactionFire::init1a 
00096             && unit->reactionfire.getStatus() != Vehicle::ReactionFire::init2 
00097             && !unit->typ->hasFunction(ContainerBaseType::MoveWithReactionFire)  ) {
00098          
00099          auto_ptr<ChangeUnitMovement> propChange ( new ChangeUnitMovement( getUnit(), 0 ));
00100          ActionResult res = propChange->execute( context );
00101       
00102          if ( res.successful() ) 
00103             propChange.release();
00104          
00105       }
00106       
00107       auto_ptr<ChangeUnitProperty> propChange ( new ChangeUnitProperty( getUnit(), ChangeUnitProperty::ReactionFire, (int) Vehicle::ReactionFire::off ));
00108       ActionResult res = propChange->execute( context );
00109       
00110       if ( res.successful() ) {
00111          propChange.release();
00112          if ( context.display )
00113             context.display->repaintDisplay();
00114       }
00115       
00116       return res;
00117    }
00118 }
00119 
00120 
00121 
00122 static const int ReactionFireSwitchCommandVersion = 1;
00123 
00124 void ReactionFireSwitchCommand :: readData ( tnstream& stream )
00125 {
00126    UnitCommand::readData( stream );
00127    int version = stream.readInt();
00128    if ( version > ReactionFireSwitchCommandVersion )
00129       throw tinvalidversion ( "ReactionFireSwitchCommand", ReactionFireSwitchCommandVersion, version );
00130    newRFstate = stream.readInt();
00131 }
00132 
00133 void ReactionFireSwitchCommand :: writeData ( tnstream& stream ) const
00134 {
00135    UnitCommand::writeData( stream );
00136    stream.writeInt( ReactionFireSwitchCommandVersion );
00137    stream.writeInt( newRFstate );
00138 }
00139 
00140 
00141 ASCString ReactionFireSwitchCommand :: getCommandString() const
00142 {
00143    ASCString c;
00144    c.format("unitReactionFireEnable ( map, %d, %d )", getUnitID(), newRFstate );
00145    return c;
00146 
00147 }
00148 
00149 GameActionID ReactionFireSwitchCommand::getID() const
00150 {
00151    return ActionRegistry::ReactionFireSwitchCommand;
00152 }
00153 
00154 ASCString ReactionFireSwitchCommand::getDescription() const
00155 {
00156    ASCString s = newRFstate ? "Enable " : "Disable ";
00157    s += "reaction fire ";
00158    
00159    if ( getMap()->getUnit( getUnitID() ))
00160       s += " for " + getMap()->getUnit( getUnitID() )->getName();
00161    
00162    return s;
00163 }
00164 
00165 namespace
00166 {
00167    const bool r1 = registerAction<ReactionFireSwitchCommand> ( ActionRegistry::ReactionFireSwitchCommand );
00168 }
00169 

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