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 "setresourceprocessingratecommand.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 SetResourceProcessingRateCommand :: avail ( const ContainerBase* container ) 00038 { 00039 if ( !container ) 00040 return false; 00041 00042 return container->baseType->hasFunction( ContainerBaseType::MatterConverter ) 00043 || container->baseType->hasFunction( ContainerBaseType::MiningStation ) ; 00044 } 00045 00046 00047 00048 00049 SetResourceProcessingRateCommand :: SetResourceProcessingRateCommand ( ContainerBase* container, int rate ) 00050 : ContainerCommand ( container ), newRate(rate) 00051 { 00052 setState( SetUp ); 00053 } 00054 00055 00056 Resources SetResourceProcessingRateCommand::getNewPlus() 00057 { 00058 Resources res; 00059 ContainerBase* c = getContainer(); 00060 for ( int r = 0; r < Resources::count; r++ ) 00061 res.resource(r) = c->maxplus.resource(r) * newRate/100; 00062 00063 return res; 00064 } 00065 00066 00067 ActionResult SetResourceProcessingRateCommand::go ( const Context& context ) 00068 { 00069 if ( getState() != SetUp ) 00070 return ActionResult(22000); 00071 00072 ContainerBase* c = getContainer(); 00073 if ( !avail( c )) 00074 return ActionResult(22850); 00075 00076 if ( newRate < 0 || newRate > 100 ) 00077 return ActionResult(22851); 00078 00079 oldRate = c->plus; 00080 c->plus = getNewPlus(); 00081 00082 setState( Finished ); 00083 00084 return ActionResult(0); 00085 } 00086 00087 ActionResult SetResourceProcessingRateCommand::undoAction( const Context& context ) 00088 { 00089 getContainer()->plus = oldRate; 00090 return ContainerCommand::undoAction( context ); 00091 } 00092 00093 00094 static const int SetResourceProcessingRateCommandVersion = 1; 00095 00096 void SetResourceProcessingRateCommand :: readData ( tnstream& stream ) 00097 { 00098 ContainerCommand::readData( stream ); 00099 int version = stream.readInt(); 00100 if ( version > SetResourceProcessingRateCommandVersion ) 00101 throw tinvalidversion ( "SetResourceProcessingRateCommand", SetResourceProcessingRateCommandVersion, version ); 00102 newRate = stream.readInt(); 00103 oldRate.read( stream ); 00104 } 00105 00106 void SetResourceProcessingRateCommand :: writeData ( tnstream& stream ) const 00107 { 00108 ContainerCommand::writeData( stream ); 00109 stream.writeInt( SetResourceProcessingRateCommandVersion ); 00110 stream.writeInt( newRate ); 00111 oldRate.write( stream ); 00112 } 00113 00114 00115 ASCString SetResourceProcessingRateCommand :: getCommandString() const 00116 { 00117 ASCString c; 00118 c.format("setResourceProcessingRate ( map, %d, %d )", getContainerID(), newRate ); 00119 return c; 00120 00121 } 00122 00123 GameActionID SetResourceProcessingRateCommand::getID() const 00124 { 00125 return ActionRegistry::SetResourceProcessingRateCommand; 00126 } 00127 00128 ASCString SetResourceProcessingRateCommand::getDescription() const 00129 { 00130 ASCString s = "Set Resource processing rate of "; 00131 00132 if ( getContainer(true) ) { 00133 s += getContainer()->getName(); 00134 } else { 00135 s += " container with ID " + ASCString::toString( getContainerID() ); 00136 } 00137 00138 s += " to " + ASCString::toString( newRate ) + "%"; 00139 return s; 00140 } 00141 00142 namespace 00143 { 00144 const bool r1 = registerAction<SetResourceProcessingRateCommand> ( ActionRegistry::SetResourceProcessingRateCommand ); 00145 } 00146
1.5.1