00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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