repairbuildingcommand.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 "repairbuildingcommand.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 "changecontainerproperty.h"
00033 #include "consumeresource.h"
00034 
00035 
00036       
00037 bool RepairBuildingCommand :: avail ( const Building* building )
00038 {
00039    if ( !building )
00040       return false;
00041    
00042    if ( !building->damage )
00043       return false;
00044    else
00045       return building->repairableDamage() > 0;
00046    
00047 }
00048 
00049 
00050 RepairBuildingCommand :: RepairBuildingCommand ( Building* building )
00051    : ContainerCommand ( building )
00052 {
00053    if ( avail( building ))
00054       setState( SetUp );
00055 }
00056 
00057 RepairBuildingCommand::RepairData RepairBuildingCommand::getCost()
00058 {
00059    RepairData data;
00060    data.newDamage = getContainer()->getMaxRepair ( getContainer(), 0, data.cost  );
00061    data.damageDelta = getContainer()->damage - data.newDamage;
00062    return data;   
00063 }
00064 
00065 
00066 
00067 
00068 ActionResult RepairBuildingCommand::go ( const Context& context )
00069 {
00070    if ( getState() != SetUp )
00071       return ActionResult(22000);
00072 
00073    ContainerBase* servicer = getContainer();
00074    Building* building = dynamic_cast<Building*>(servicer);
00075    if ( !building )
00076       return ActionResult( 22700 );
00077    
00078    if ( !avail( building ))
00079       return ActionResult( 22700 );
00080    
00081    
00082    RepairData data = getCost();
00083    
00084    auto_ptr<ChangeContainerProperty> propChange ( new ChangeContainerProperty( building, ChangeContainerProperty::Damage, data.newDamage ));
00085    ActionResult res = propChange->execute( context );
00086    if ( res.successful() )
00087       propChange.release();
00088    else
00089       return res;
00090    
00091    auto_ptr<ChangeContainerProperty> propChange2 ( new ChangeContainerProperty( building, ChangeContainerProperty::RepairedThisTurn, data.damageDelta, false ));
00092    res = propChange2->execute( context );
00093    if ( res.successful() )
00094       propChange2.release();
00095    else
00096       return res;
00097    
00098    
00099    auto_ptr<ConsumeResource> resource ( new ConsumeResource( getContainer(), data.cost ));
00100    res = resource->execute( context );
00101    if ( res.successful() )
00102       resource.release();
00103    
00104    return res;
00105 }
00106 
00107 
00108 
00109 static const int RepairBuildingCommandVersion = 1;
00110 
00111 void RepairBuildingCommand :: readData ( tnstream& stream )
00112 {
00113    ContainerCommand::readData( stream );
00114    int version = stream.readInt();
00115    if ( version > RepairBuildingCommandVersion )
00116       throw tinvalidversion ( "RepairBuildingCommand", RepairBuildingCommandVersion, version );
00117 }
00118 
00119 void RepairBuildingCommand :: writeData ( tnstream& stream ) const
00120 {
00121    ContainerCommand::writeData( stream );
00122    stream.writeInt( RepairBuildingCommandVersion );
00123 }
00124 
00125 
00126 ASCString RepairBuildingCommand :: getCommandString() const
00127 {
00128    ASCString c;
00129    c.format("repairBuilding ( map, %d )", getContainerID() );
00130    return c;
00131 
00132 }
00133 
00134 GameActionID RepairBuildingCommand::getID() const
00135 {
00136    return ActionRegistry::RepairBuildingCommand;
00137 }
00138 
00139 ASCString RepairBuildingCommand::getDescription() const
00140 {
00141    ASCString s = "Repair building ";
00142    if ( getContainer(true))
00143       s += getContainer( true )->getName() + " at ";
00144    s += ASCString::toString( getContainerID() );
00145    
00146    return s;
00147 }
00148 
00149 namespace
00150 {
00151    const bool r1 = registerAction<RepairBuildingCommand> ( ActionRegistry::RepairBuildingCommand );
00152 }
00153 

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