powergenerationswitchcommand.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 "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 

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