Advanced Strategic Command
setresourceprocessingratecommand.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 
23 
24 #include "../vehicle.h"
25 #include "../mapfield.h"
26 #include "../gamemap.h"
27 #include "../viewcalculation.h"
28 #include "../spfst.h"
29 #include "../mapdisplayinterface.h"
30 #include "action-registry.h"
31 #include "../itemrepository.h"
32 #include "../containercontrols.h"
33 #include "consumeresource.h"
34 #include "servicecommand.h"
35 #include "convertcontainer.h"
36 
38 {
39  if ( !container )
40  return false;
41 
44 }
45 
46 
47 
48 
49 SetResourceProcessingRateCommand :: SetResourceProcessingRateCommand ( ContainerBase* container, int rate )
50  : ContainerCommand ( container ), newRate(rate)
51 {
52  setState( SetUp );
53 }
54 
55 
57 {
58  Resources res;
60  for ( int r = 0; r < Resources::count; r++ )
61  res.resource(r) = c->maxplus.resource(r) * newRate/100;
62 
63  return res;
64 }
65 
66 
68 {
69  if ( getState() != SetUp )
70  return ActionResult(22000);
71 
73  if ( !avail( c ))
74  return ActionResult(22850);
75 
76  if ( newRate < 0 || newRate > 100 )
77  return ActionResult(22851);
78 
79  oldRate = c->plus;
80  c->plus = getNewPlus();
81 
82  setState( Finished );
83 
84  return ActionResult(0);
85 }
86 
88 {
89  getContainer()->plus = oldRate;
90  return ContainerCommand::undoAction( context );
91 }
92 
93 
95 
97 {
99  int version = stream.readInt();
101  throw tinvalidversion ( "SetResourceProcessingRateCommand", SetResourceProcessingRateCommandVersion, version );
102  newRate = stream.readInt();
103  oldRate.read( stream );
104 }
105 
107 {
108  ContainerCommand::writeData( stream );
110  stream.writeInt( newRate );
111  oldRate.write( stream );
112 }
113 
114 
116 {
117  ASCString c;
118  c.format("setResourceProcessingRate ( map, %d, %d )", getContainerID(), newRate );
119  return c;
120 
121 }
122 
124 {
126 }
127 
129 {
130  ASCString s = "Set Resource processing rate of ";
131 
132  if ( getContainer(true) ) {
133  s += getContainer()->getName();
134  } else {
135  s += " container with ID " + ASCString::toString( getContainerID() );
136  }
137 
138  s += " to " + ASCString::toString( newRate ) + "%";
139  return s;
140 }
141 
142 namespace
143 {
144  const bool r1 = registerAction<SetResourceProcessingRateCommand> ( ActionRegistry::SetResourceProcessingRateCommand );
145 }
146 
static bool avail(const ContainerBase *factory)
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
ActionResult undoAction(const Context &context)
int GameActionID
Definition: action.h:35
the command is totally done
Definition: command.h:120
virtual ASCString getName() const =0
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
ActionResult go(const Context &context)
bool hasFunction(ContainerFunctions function) const
State getState() const
Definition: command.h:125
void read(tnstream &stream)
Definition: typen.cpp:195
The interface for all kinds of IO stream.
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
static const int SetResourceProcessingRateCommandVersion
int getContainerID() const
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void write(tnstream &stream) const
Definition: typen.cpp:201
Resources maxplus
the maximum amount of Resources that the building can produce each turn in the ASC resource mode ; se...
virtual ActionResult undoAction(const Context &context)
Definition: command.cpp:60
void setState(State state)
Definition: command.cpp:44
Resources plus
the Resources that are produced each turn
void writeData(tnstream &stream) const
int & resource(int type)
Definition: typen.h:105
ContainerBase * getContainer(bool dontThrow=false)
static const int count
Definition: typen.h:103
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
void readData(tnstream &stream)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
Resources are basically the currency of ASC.
Definition: typen.h:97