Advanced Strategic Command
removeproductionlinecommand.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 ( !factory )
40  return false;
41 
44 }
45 
46 
48 {
50 }
51 
52 
53 
54 RemoveProductionLineCommand :: RemoveProductionLineCommand ( ContainerBase* container )
55  : ContainerCommand ( container ), vehicleTypeId(-1)
56 {
57 
58 }
59 
60 
61 
63 {
64  if ( vehicleType ) {
65  vehicleTypeId = vehicleType->id;
66  setState( SetUp );
67  }
68 }
69 
70 
72 {
73  if ( getState() != SetUp )
74  return ActionResult(22000);
75 
76  if ( !avail( getContainer() ))
77  return ActionResult(22800);
78 
79  const VehicleType* vt = NULL;
81  for ( ContainerBase::Production::const_iterator i = production.begin(); i != production.end(); ++i )
82  if ( (*i)->id == vehicleTypeId )
83  vt = *i;
84 
85  if ( !vt )
86  return ActionResult(22900);
87 
88  Resources needed = resourcesNeeded( getContainer()->baseType, vt );
89  Resources avail = getContainer()->getResource( needed, true );
90  if ( avail < needed )
91  return ActionResult(22901);
92 
93  auto_ptr<ConsumeResource> cr ( new ConsumeResource( getContainer(), needed ));
94  ActionResult res = cr->execute(context);
95 
96  if ( !res.successful() ) {
97  setState( Failed );
98  return res;
99  }
100 
102 
103  cr.release();
104  setState( Finished );
105 
106  return res;
107 }
108 
110 {
111  VehicleType* vt = vehicleTypeRepository.getObject_byID( vehicleTypeId );
112  if ( !vt )
113  return ActionResult(22902);
114 
116 
117  return ContainerCommand::undoAction( context );
118 }
119 
120 
122 
124 {
125  ContainerCommand::readData( stream );
126  int version = stream.readInt();
127  if ( version > RemoveProductionLineCommandVersion )
128  throw tinvalidversion ( "RemoveProductionLineCommand", RemoveProductionLineCommandVersion, version );
129  vehicleTypeId = stream.readInt();
130 }
131 
133 {
134  ContainerCommand::writeData( stream );
136  stream.writeInt( vehicleTypeId );
137 }
138 
139 
141 {
142  ASCString c;
143  c.format("removeProductionLine ( map, %d, %d )", getContainerID(), vehicleTypeId );
144  return c;
145 
146 }
147 
149 {
151 }
152 
154 {
155  ASCString s = "Remove production line of type ";
156 
157  VehicleType* vt = vehicleTypeRepository.getObject_byID( vehicleTypeId );
158  if ( !vt )
159  s += ASCString::toString(vehicleTypeId);
160  else
161  s += vt->getName();
162 
163  if ( getContainer(true) ) {
164  s += " from " + getContainer()->getName();
165  }
166 
167  return s;
168 }
169 
170 namespace
171 {
172  const bool r1 = registerAction<RemoveProductionLineCommand> ( ActionRegistry::RemoveProductionLineCommand );
173 }
174 
static const int RemoveProductionLineCommandVersion
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
int GameActionID
Definition: action.h:35
ActionResult go(const Context &context)
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
ResourceMatrix productionEfficiency
bool hasFunction(ContainerFunctions function) const
State getState() const
Definition: command.h:125
ASCString getName() const
The interface for all kinds of IO stream.
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
const double productionLineRemovalCostFactor
Definition: typen.h:545
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
const Production & getProduction() const
static bool avail(const ContainerBase *factory)
int getContainerID() const
void writeData(tnstream &stream) const
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
virtual int getResource(int amount, int resourcetype, bool queryonly, int scope=1, int player=-1)=0
ItemRepositoryLoader< VehicleType > vehicleTypeRepository("vehicletype")
virtual ActionResult undoAction(const Context &context)
Definition: command.cpp:60
void setState(State state)
Definition: command.cpp:44
void addProductionLine(const VehicleType *type)
bool successful() const
void writeData(tnstream &stream) const
ContainerBase * getContainer(bool dontThrow=false)
ActionResult undoAction(const Context &context)
vector< const VehicleType * > Production
Definition: containerbase.h:87
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
static Resources resourcesNeeded(const ContainerBaseType *factory, const VehicleType *veh)
void readData(tnstream &stream)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
void deleteProductionLine(const VehicleType *type)
Resources are basically the currency of ASC.
Definition: typen.h:97
void setRemoval(const VehicleType *vehicleType)