Advanced Strategic Command
productionanalysis.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  cargodialog.cpp - description
3  -------------------
4  begin : Tue Oct 24 2000
5  copyright : (C) 2000 by Martin Bickel
6  email : bickel@asc-hq.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 #include "../containerbasetype.h"
20 #include "../vehicletype.h"
21 #include "../player.h"
22 #include "../gamemap.h"
23 #include "../itemrepository.h"
24 
25 #include "productionanalysis.h"
26 #include "vehicletypeselector.h"
27 #include "../widgets/textrenderer.h"
28 
30  private:
31  const VehicleType* selectedVehicleType;
32 
33  virtual void itemSelected( const SelectionWidget* sw) {
34  const VehicleTypeBaseWidget* vtcw = dynamic_cast<const VehicleTypeBaseWidget*>(sw);
35  assert( vtcw );
36  selectedVehicleType = vtcw->getVehicletype ();
37  if ( selectedVehicleType )
38  QuitModal();
39  };
40  public:
41  AvailableUnitWindow ( PG_Widget *parent, const PG_Rect &r , const ASCString& title, SelectionItemFactory* itemFactory ) : ItemSelectorWindow( parent, r, title, itemFactory ), selectedVehicleType(NULL) {};
42  const VehicleType* getSelected() { return selectedVehicleType; };
43 };
44 
45 
46 bool factoryProductionInstanceExists( const ContainerBaseType* potentialFactory, const VehicleType* vt, GameMap* gamemap )
47 {
48  Player& p = gamemap->getCurrentPlayer();
49  for ( Player::BuildingList::const_iterator b = p.buildingList.begin(); b != p.buildingList.end(); ++b )
50  if ( (*b)->typ == potentialFactory )
51  if ( (*b)->hasProductionLine(vt))
52  return true;
53 
54  for ( Player::VehicleList::const_iterator v = p.vehicleList.begin(); v != p.vehicleList.end(); ++v )
55  if ( (*v)->typ == potentialFactory )
56  if ( (*v)->hasProductionLine(vt))
57  return true;
58 
59  return false;
60 
61 }
62 
63 int evaluateProduction( const ContainerBaseType* potentialFactory, const VehicleType* vt, GameMap* gamemap )
64 {
65  if ( potentialFactory->vehicleFit( vt ) && potentialFactory->hasFunction( ContainerBaseType::InternalVehicleProduction)) {
66  if ( potentialFactory->vehicleCategoriesProduceable & (1 << vt->movemalustyp))
67  for ( int h = 0; h < 8; ++h )
68  if ( potentialFactory->height & (1<<h))
69  if ( potentialFactory->vehicleUnloadable(vt,h) || potentialFactory->hasFunction( ContainerBaseType::ProduceNonLeavableUnits ))
70  if ( !potentialFactory->hasFunction( ContainerBaseType::NoProductionCustomization) || factoryProductionInstanceExists( potentialFactory, vt, gamemap ))
71  return 1;
72  }
73 
74  return 0;
75 }
76 
77 int evaluateProduction( const VehicleType* potentialFactory, const VehicleType* vt, GameMap* gamemap )
78 {
79  const ContainerBaseType* cbt = potentialFactory;
80  int res = evaluateProduction( cbt, vt, gamemap );
81 
82  // if ( potentialFactory->hasFunction( ContainerBaseType::ExternalVehicleProduction))
83  for ( vector<IntRange>::const_iterator i = potentialFactory->vehiclesBuildable.begin(); i != potentialFactory->vehiclesBuildable.end(); ++i )
84  if( vt->id >= i->from && vt->id <= i->to )
85  return res | 2;
86  return res;
87 }
88 
89 
90 ASCString getProductionString( const ContainerBaseType* potentialFactory, const VehicleType* vt, GameMap* gamemap )
91 {
92  ASCString s;
93  int res = evaluateProduction( potentialFactory, vt, gamemap);
94  if ( res ) {
95  s = potentialFactory->getName() + " (" + ASCString::toString(potentialFactory->id) + ") ";
96  if ( res & 1 )
97  s += " internally";
98  if ( res & 2 )
99  s += " externally";
100  s += "\n";
101  }
102  return s;
103 }
104 
105 
106 
107 ASCString getInstances( const ContainerBaseType* evaluatedFactory, const VehicleType* unitsToProduce, GameMap* gamemap, bool lineAvail )
108 {
109  ASCString instances;
110  if ( lineAvail ) {
111  int count = 0;
112  ASCString units = evaluatedFactory->getName() + ": ";
113 
114  for ( Player::VehicleList::const_iterator j = gamemap->getCurrentPlayer().vehicleList.begin(); j != gamemap->getCurrentPlayer().vehicleList.end(); ++j )
115  if ( (*j)->typ == evaluatedFactory )
116  for ( Vehicle::Production::const_iterator i = (*j)->getProduction().begin(); i != (*j)->getProduction().end(); ++i)
117  if ( *i == unitsToProduce ) {
118  units += (*j)->getPosition().toString();
119  ++count;
120  }
121 
122  if ( count )
123  instances += units + "\n";
124 
125  }
126  {
127  int count = 0;
128  ASCString units = evaluatedFactory->getName() + ": ";
129 
130  for ( Player::BuildingList::const_iterator j = gamemap->getCurrentPlayer().buildingList.begin(); j != gamemap->getCurrentPlayer().buildingList.end(); ++j )
131  if ( (*j)->typ == evaluatedFactory ) {
132  if ( lineAvail ) {
133  for ( ContainerBase::Production::const_iterator k = (*j)->getProduction().begin(); k != (*j)->getProduction().end(); ++k )
134  if ( *k == unitsToProduce ) {
135  units += (*j)->getPosition().toString();
136  ++count;
137  }
138  } else {
139  if ( unitsToProduce->techDependency.available( gamemap->getCurrentPlayer().research ) ) {
140  bool found = false;
141  for ( ContainerBase::Production::const_iterator k = (*j)->getProduction().begin(); k != (*j)->getProduction().end(); ++k )
142  if ( *k == unitsToProduce )
143  found = true;
144 
145  if ( !found ) {
146  units += (*j)->getPosition().toString();
147  ++count;
148  }
149  }
150  }
151  }
152  if ( count )
153  instances += units + "\n";
154  }
155 
156  return instances;
157 }
158 
159 template<typename T>
160 void checkType( T* t, const VehicleType* evaluatedUnitType, ASCString& instances, ASCString& lineAddable, ASCString& types, GameMap* gamemap, bool checkResearch )
161 {
162  if ( evaluateProduction( t, evaluatedUnitType, gamemap)) {
163  instances += getInstances( t, evaluatedUnitType, gamemap, true );
164  lineAddable += getInstances( t, evaluatedUnitType, gamemap, false );
165  if ( !checkResearch || t->techDependency.available( gamemap->getCurrentPlayer().research ))
166  types += getProductionString( t, evaluatedUnitType, gamemap );
167  }
168 }
169 
170 bool vehicleOwned( const VehicleType* vt, GameMap* gamemap )
171 {
172  for ( Player::VehicleList::const_iterator j = gamemap->getCurrentPlayer().vehicleList.begin(); j != gamemap->getCurrentPlayer().vehicleList.end(); ++j )
173  if ( (*j)->typ == vt )
174  return true;
175  return false;
176 }
177 
178 
179 void unitProductionAnalysis( GameMap* gamemap, bool checkResearch )
180 {
182 
183  for ( int i = 0; i < vehicleTypeRepository.getNum(); ++i ) {
184  VehicleType* p = vehicleTypeRepository.getObject_byPos(i);
185  if ( p ) {
186  if ( vehicleOwned( p, gamemap ) || !checkResearch || p->techDependency.available( gamemap->getCurrentPlayer().research ) )
187  c.push_back(p);
188  }
189  }
190 
191  AvailableUnitWindow auw( NULL, PG_Rect( -1, -1, 400, 400), "Select Vehicle Type", new VehicleTypeSelectionItemFactory(c, gamemap->getCurrentPlayer() ));
192  auw.Show();
193  auw.RunModal();
194  auw.Hide();
195 
196 
197  if ( auw.getSelected() ) {
198  ASCString types, instances, lineAddable;
199 
200  for ( int i = 0; i < vehicleTypeRepository.getNum(); ++i )
201  checkType( vehicleTypeRepository.getObject_byPos(i), auw.getSelected(), instances, lineAddable, types, gamemap, checkResearch );
202 
203  for ( int i = 0; i < buildingTypeRepository.getNum(); ++i )
204  checkType( buildingTypeRepository.getObject_byPos(i), auw.getSelected(), instances, lineAddable, types, gamemap, checkResearch );
205 
206  if ( types.empty() )
207  types = "-none-\n";
208 
209  if ( instances.empty() )
210  instances = "-nowhere-\n";
211 
212  if ( lineAddable.empty() )
213  lineAddable = "-nowhere-\n";
214 
215  types = "\n#fontsize=18#Vehicle and Building types#fontsize=10#\n(provided that the unit has been researched)\n#fontsize=12#" + types;
216  instances = "\n#fontsize=18#Production available #fontsize=12#\n" + instances;
217  lineAddable = "\n#fontsize=18#Production can be added #fontsize=12#\n" + lineAddable;
218 
219  ViewFormattedText vft("Unit Production Analysis", instances + lineAddable + types, PG_Rect( -1, -1, 500, 400 ));
220  vft.Show();
221  vft.RunModal();
222  }
223 }
224 
Player & getCurrentPlayer()
Definition: gamemap.h:268
vector< IntRange > vehiclesBuildable
the ids of units this unit can construct
Definition: vehicletype.h:230
TechAdapterDependency techDependency
ASCString getInstances(const ContainerBaseType *evaluatedFactory, const VehicleType *unitsToProduce, GameMap *gamemap, bool lineAvail)
bool vehicleOwned(const VehicleType *vt, GameMap *gamemap)
bool hasFunction(ContainerFunctions function) const
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...
ItemRepositoryLoader< BuildingType > buildingTypeRepository("buildingtype")
const VehicleType * getSelected()
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
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
VehicleList vehicleList
a list of all units
Definition: player.h:135
ItemRepositoryLoader< VehicleType > vehicleTypeRepository("vehicletype")
int vehicleCategoriesProduceable
bitmapped: vehicle categories that can be produced in the container
vector< const VehicleType * > Container
int vehicleUnloadable(const VehicleType *vehicleType, int carrierHeight) const
checks the unloading of a unit type
int movemalustyp
The category of the unit. Original used only to distinguish only between different movement costs for...
Definition: vehicletype.h:206
const VehicleType * getVehicletype() const
int height
the levels of height which this unit can enter
BuildingList buildingList
a list of all units
Definition: player.h:139
Research research
the status of the scientific research
Definition: player.h:142
bool factoryProductionInstanceExists(const ContainerBaseType *potentialFactory, const VehicleType *vt, GameMap *gamemap)
ASCString getProductionString(const ContainerBaseType *potentialFactory, const VehicleType *vt, GameMap *gamemap)
ASCString getName() const
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
bool available(const Research &research) const
Definition: research.cpp:412
void checkType(T *t, const VehicleType *evaluatedUnitType, ASCString &instances, ASCString &lineAddable, ASCString &types, GameMap *gamemap, bool checkResearch)
AvailableUnitWindow(PG_Widget *parent, const PG_Rect &r, const ASCString &title, SelectionItemFactory *itemFactory)
int evaluateProduction(const ContainerBaseType *potentialFactory, const VehicleType *vt, GameMap *gamemap)
void unitProductionAnalysis(GameMap *gamemap, bool checkResearch)
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182