removeproductionlinecommand.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 "removeproductionlinecommand.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 "convertcontainer.h"
00036 
00037 bool RemoveProductionLineCommand :: avail ( const ContainerBase* factory )
00038 {
00039    if ( !factory  )
00040       return false;
00041    
00042    return factory->baseType->hasFunction( ContainerBaseType::InternalVehicleProduction ) 
00043       && !factory->baseType->hasFunction( ContainerBaseType::NoProductionCustomization ) ;
00044 }
00045 
00046 
00047 Resources RemoveProductionLineCommand :: resourcesNeeded( const ContainerBaseType* factory, const VehicleType* veh )
00048 {
00049    return factory->productionEfficiency * veh->productionCost * productionLineRemovalCostFactor;
00050 }
00051 
00052 
00053 
00054 RemoveProductionLineCommand :: RemoveProductionLineCommand ( ContainerBase* container )
00055    : ContainerCommand ( container ), vehicleTypeId(-1)
00056 {
00057 
00058 }
00059 
00060 
00061 
00062 void RemoveProductionLineCommand::setRemoval( const VehicleType* vehicleType )
00063 {
00064    if ( vehicleType ) {
00065       vehicleTypeId = vehicleType->id;
00066       setState( SetUp );
00067    }
00068 }
00069 
00070 
00071 ActionResult RemoveProductionLineCommand::go ( const Context& context )
00072 {
00073    if ( getState() != SetUp )
00074       return ActionResult(22000);
00075 
00076    if ( !avail( getContainer() ))
00077       return ActionResult(22800);
00078    
00079    const VehicleType* vt = NULL;
00080    const ContainerBase::Production production = getContainer()->getProduction();
00081    for ( ContainerBase::Production::const_iterator i = production.begin(); i != production.end(); ++i )
00082       if ( (*i)->id == vehicleTypeId )
00083          vt = *i;
00084    
00085    if ( !vt )
00086       return ActionResult(22900);
00087    
00088    Resources needed = resourcesNeeded( getContainer()->baseType,  vt );
00089    Resources avail = getContainer()->getResource( needed, true );
00090    if ( avail < needed  )
00091       return ActionResult(22901);
00092    
00093    auto_ptr<ConsumeResource> cr ( new ConsumeResource( getContainer(), needed ));
00094    ActionResult res = cr->execute(context);
00095    
00096    if ( !res.successful() ) {
00097       setState( Failed );
00098       return res;
00099    }
00100    
00101    getContainer()->deleteProductionLine( vt );
00102    
00103    cr.release();
00104    setState( Finished );
00105 
00106    return res;
00107 }
00108 
00109 ActionResult RemoveProductionLineCommand::undoAction( const Context& context )
00110 {
00111    VehicleType* vt = vehicleTypeRepository.getObject_byID( vehicleTypeId );
00112    if ( !vt )
00113       return ActionResult(22902);
00114          
00115    getContainer()->addProductionLine( vt );
00116    
00117    return ContainerCommand::undoAction( context );
00118 }
00119 
00120 
00121 static const int RemoveProductionLineCommandVersion = 1;
00122 
00123 void RemoveProductionLineCommand :: readData ( tnstream& stream )
00124 {
00125    ContainerCommand::readData( stream );
00126    int version = stream.readInt();
00127    if ( version > RemoveProductionLineCommandVersion )
00128       throw tinvalidversion ( "RemoveProductionLineCommand", RemoveProductionLineCommandVersion, version );
00129    vehicleTypeId = stream.readInt();
00130 }
00131 
00132 void RemoveProductionLineCommand :: writeData ( tnstream& stream ) const
00133 {
00134    ContainerCommand::writeData( stream );
00135    stream.writeInt( RemoveProductionLineCommandVersion );
00136    stream.writeInt( vehicleTypeId );
00137 }
00138 
00139 
00140 ASCString RemoveProductionLineCommand :: getCommandString() const
00141 {
00142    ASCString c;
00143    c.format("removeProductionLine ( map, %d, %d )", getContainerID(), vehicleTypeId );
00144    return c;
00145 
00146 }
00147 
00148 GameActionID RemoveProductionLineCommand::getID() const
00149 {
00150    return ActionRegistry::RemoveProductionLineCommand;
00151 }
00152 
00153 ASCString RemoveProductionLineCommand::getDescription() const
00154 {
00155    ASCString s = "Remove production line of type ";
00156    
00157    VehicleType* vt = vehicleTypeRepository.getObject_byID( vehicleTypeId );
00158    if ( !vt )
00159       s += ASCString::toString(vehicleTypeId);
00160    else
00161       s += vt->getName();
00162    
00163    if ( getContainer(true) ) {
00164       s += " from " + getContainer()->getName();
00165    }
00166    
00167    return s;
00168 }
00169 
00170 namespace
00171 {
00172    const bool r1 = registerAction<RemoveProductionLineCommand> ( ActionRegistry::RemoveProductionLineCommand );
00173 }
00174 

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