Advanced Strategic Command
buildproductionlinecommand.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 {
49 
50  vector<const VehicleType*> list;
51 
52  ContainerBase* container = getContainer();
53 
54  if ( !avail( container ))
55  return list;
56 
57  for ( int i = 0; i < vehicleTypeRepository.getNum(); ++i ) {
58  VehicleType* veh = getMap()->getvehicletype_bypos ( i );
59  if ( veh ) {
60  bool found = find( container->getProduction().begin(), container->getProduction().end(), veh ) != container->getProduction().end();
61  if ( container->baseType->vehicleFit ( veh ) && !found )
63  if ( veh->techDependency.available ( container->getMap()->getCurrentPlayer().research ))
64  if ( container->baseType->vehicleCategoriesProduceable & (1 << veh->movemalustyp))
65  list.push_back( veh );
66  }
67  }
68  return list;
69 }
70 
72 {
74 }
75 
76 
77 
78 BuildProductionLineCommand :: BuildProductionLineCommand ( ContainerBase* container )
79  : ContainerCommand ( container ), vehicleTypeId(-1)
80 {
81 
82 }
83 
84 
85 
87 {
88  if ( vehicleType ) {
89  vehicleTypeId = vehicleType->id;
90  setState( SetUp );
91  }
92 }
93 
94 
96 {
97  if ( getState() != SetUp )
98  return ActionResult(22000);
99 
100  if ( !avail( getContainer() ))
101  return ActionResult(22800);
102 
103  const VehicleType* vt = NULL;
104  vector<const VehicleType*> types = productionLinesBuyable();
105  for ( vector<const VehicleType*>::iterator i = types.begin(); i != types.end(); ++i )
106  if ( (*i)->id == vehicleTypeId )
107  vt = *i;
108 
109  if ( !vt )
110  return ActionResult(22900);
111 
112 
113  Resources needed = resourcesNeeded( getContainer()->baseType, vt );
114  Resources avail = getContainer()->getResource( needed, true );
115  if ( avail < needed )
116  return ActionResult(22901);
117 
118  auto_ptr<ConsumeResource> cr ( new ConsumeResource( getContainer(), needed ));
119  ActionResult res = cr->execute(context);
120 
121  if ( !res.successful() ) {
122  setState( Failed );
123  return res;
124  }
125 
127 
128  cr.release();
129  setState( Finished );
130 
131  return res;
132 }
133 
135 {
136  VehicleType* vt = vehicleTypeRepository.getObject_byID( vehicleTypeId );
137  if ( !vt )
138  return ActionResult(22902);
139 
141 
142  return ContainerCommand::undoAction( context );
143 }
144 
145 
147 
149 {
150  ContainerCommand::readData( stream );
151  int version = stream.readInt();
152  if ( version > BuildProductionLineCommandVersion )
153  throw tinvalidversion ( "BuildProductionLineCommand", BuildProductionLineCommandVersion, version );
154  vehicleTypeId = stream.readInt();
155 }
156 
158 {
159  ContainerCommand::writeData( stream );
161  stream.writeInt( vehicleTypeId );
162 }
163 
164 
166 {
167  ASCString c;
168  c.format("buildProductionLine ( map, %d, %d )", getContainerID(), vehicleTypeId );
169  return c;
170 
171 }
172 
174 {
176 }
177 
179 {
180  ASCString s = "Add production line for type ";
181 
182  VehicleType* vt = vehicleTypeRepository.getObject_byID( vehicleTypeId );
183  if ( !vt )
184  s += ASCString::toString(vehicleTypeId);
185  else
186  s += vt->getName();
187 
188  if ( getContainer(true) ) {
189  s += " to " + getContainer()->getName();
190  }
191 
192  return s;
193 }
194 
195 namespace
196 {
197  const bool r1 = registerAction<BuildProductionLineCommand> ( ActionRegistry::BuildProductionLineCommand );
198 }
199 
const double productionLineConstructionCostFactor
Definition: typen.h:544
Player & getCurrentPlayer()
Definition: gamemap.h:268
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
the command is totally done
Definition: command.h:120
virtual ASCString getName() const =0
TechAdapterDependency techDependency
static Resources resourcesNeeded(const ContainerBaseType *factory, const VehicleType *veh)
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
ActionResult undoAction(const Context &context)
bool hasFunction(ContainerFunctions function) const
static bool avail(const ContainerBase *factory)
static const int BuildProductionLineCommandVersion
State getState() const
Definition: command.h:125
bool vehicleFit(const VehicleType *type) const
can units of the given type be moved into this buildtype? This is a prerequisite - but not the only r...
GameMap * getMap()
Definition: action.h:92
ASCString getName() const
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
const Production & getProduction() const
int getContainerID() 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
int vehicleUnloadable(const VehicleType *vehicleType, int carrierHeight=-1) const
checks the unloading of a unit type
virtual int getResource(int amount, int resourcetype, bool queryonly, int scope=1, int player=-1)=0
ItemRepositoryLoader< VehicleType > vehicleTypeRepository("vehicletype")
int vehicleCategoriesProduceable
bitmapped: vehicle categories that can be produced in the container
int movemalustyp
The category of the unit. Original used only to distinguish only between different movement costs for...
Definition: vehicletype.h:206
virtual ActionResult undoAction(const Context &context)
Definition: command.cpp:60
void setState(State state)
Definition: command.cpp:44
void addProductionLine(const VehicleType *type)
VehicleType * getvehicletype_bypos(int pos)
Definition: gamemap.cpp:1831
void writeData(tnstream &stream) const
bool successful() const
void writeData(tnstream &stream) const
ActionResult go(const Context &context)
Research research
the status of the scientific research
Definition: player.h:142
ContainerBase * getContainer(bool dontThrow=false)
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
GameMap * getMap() const
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
bool available(const Research &research) const
Definition: research.cpp:412
vector< const VehicleType * > productionLinesBuyable()
void setProduction(const VehicleType *vehicleType)