Advanced Strategic Command
unitcounting.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 #include <pgimage.h>
19 #include <pgtooltiphelp.h>
20 #include "unitcounting.h"
21 #include "vehicletypeselector.h"
22 #include "../vehicle.h"
23 #include "../containerbase.h"
24 #include "../gamemap.h"
25 #include "../unitset.h"
26 #include "fileselector.h"
27 
28 
29 
30 class VehicleCounterFactory: public SelectionItemFactory, public sigc::trackable {
31 public:
32  typedef vector<const VehicleType*> Container;
33 protected:
34  Container::iterator it;
36 
37  typedef map<const VehicleType*,int> Counter;
40 
41  void calcCargoSummary( const ContainerBase* cb, Counter& summary )
42  {
43  for ( ContainerBase::Cargo::const_iterator i = cb->getCargo().begin(); i != cb->getCargo().end(); ++i )
44  if ( *i ) {
45  calcCargoSummary( *i, summary );
46  if ( (*i)->getOwner() == cb->getMap()->actplayer )
47  summary[ (*i)->typ] += 1;
48  }
49  }
50 
51 
52 public:
54  VehicleCounterFactory( const ContainerBase* container );
55 
57 
58  void restart();
59  SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos );
60  void itemSelected( const SelectionWidget* widget, bool mouse ) {}
61 };
62 
64 {
65  for ( int y = 0; y < actmap->ysize; ++y )
66  for ( int x = 0; x < actmap->xsize; ++x ) {
67  MapField* fld = actmap->getField(x,y);
68  if ( fld ) {
69  if ( fld->vehicle ) {
71  if ( fld->vehicle->getOwner() == actmap->actplayer )
72  counter[ fld->vehicle->typ] += 1;
73  }
74  if ( fld->building && (fld->bdt & getTerrainBitType(cbbuildingentry)).any() )
76  }
77  }
78 
79  for ( Counter::iterator i = counter.begin(); i != counter.end(); ++i )
80  items.push_back( i->first );
81 
82  sort( items.begin(), items.end(), vehicleComp );
83  restart();
84 };
85 
86 
87 VehicleCounterFactory::VehicleCounterFactory( const ContainerBase* container ) : gamemap( container->getMap() )
88 {
89  calcCargoSummary( container, counter );
90  for ( Counter::iterator i = counter.begin(); i != counter.end(); ++i )
91  items.push_back( i->first );
92 
93  sort( items.begin(), items.end(), vehicleComp );
94  restart();
95 }
96 
97 
98 
100 {
101  it = items.begin();
102 };
103 
104 
105 SelectionWidget* VehicleCounterFactory::spawnNextItem( PG_Widget* parent, const PG_Point& pos )
106 {
107  if ( it != items.end() ) {
108  const VehicleType* v = *(it++);
109  return new VehicleTypeCountWidget( parent, pos, parent->Width() - 15, v, gamemap->getCurrentPlayer(), counter[v] );
110  } else
111  return NULL;
112 };
113 
115 {
116  ASCString t;
117  for ( Counter::const_iterator i = counter.begin(); i != counter.end(); ++i ) {
118  const VehicleType* v = i->first;
119  t += v->getName() + "\t" + ASCString::toString(i->second) + "\n";
120  }
121  return t;
122 }
123 
124 
125 /*
126 void VehicleCounterFactory::itemSelected( const SelectionWidget* widget, bool mouse )
127 {
128  if ( !widget )
129  return;
130 
131  const VehicleTypeBaseWidget* fw = dynamic_cast<const VehicleTypeBaseWidget*>(widget);
132  assert( fw );
133 }
134 */
135 
136 #include "fieldmarker.h"
137 
138 struct CursorDistSorter : public binary_function<const MapCoordinate&, const MapCoordinate&, bool> {
139  CursorDistSorter( const MapCoordinate& cursorPos) : cursor( cursorPos ) {};
140  bool operator()(const MapCoordinate& x, const MapCoordinate& y) {
141  return beeline(x,cursor) < beeline(y,cursor);
142  }
143 private:
144  MapCoordinate cursor;
145 };
146 
147 
148 void showAllUnitPositions( const VehicleType* vt, GameMap* gamemap )
149 {
150  Player& player = gamemap->getPlayer( gamemap->actplayer );
151 
152  SelectFromMap::CoordinateList coordinates;
153 
154  for ( Player::VehicleList::iterator i = player.vehicleList.begin(); i != player.vehicleList.end(); ++i ) {
155  if ( (*i)->typ == vt )
156  if ( find ( coordinates.begin(), coordinates.end(),(*i)->getPosition() ) == coordinates.end() )
157  coordinates.push_back ( (*i)->getPosition() );
158  }
159 
160  sort( coordinates.begin(), coordinates.end(), CursorDistSorter( gamemap->getCursor()) );
161 
162  SelectFromMap sfm( coordinates, gamemap, false, true );
163  sfm.Show();
164  sfm.RunModal();
165 }
166 
168 private:
169  GameMap* gamemap;
170  VehicleCounterFactory* factory;
171 
172  virtual void itemSelected( const SelectionWidget* sw) {
173  const VehicleTypeCountWidget* vtcw = dynamic_cast<const VehicleTypeCountWidget*>(sw);
174  assert( vtcw );
175 
176  if ( gamemap ) {
177  Hide();
178  showAllUnitPositions( vtcw->getVehicletype(), gamemap );
179  Show();
180  }
181  };
182 
183 
184  void saveText()
185  {
186  ASCString name = selectFile( "*.txt", false );
187  if ( !name.empty() ) {
189  s.writeString( factory->toString(), true );
190  }
191  }
192 
193  bool eventKeyDown(const SDL_KeyboardEvent* key) {
194  int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
195  if ( mod & KMOD_CTRL )
196  if ( key->keysym.sym == SDLK_s )
197  saveText();
198 
199  return ItemSelectorWindow::eventKeyDown( key );
200  }
201 
202 public:
203  UnitSummaryWindow ( PG_Widget *parent, const PG_Rect &r , const ASCString& title, VehicleCounterFactory* itemFactory, GameMap* actmap ) : ItemSelectorWindow( parent, r, title, itemFactory ), gamemap( actmap ), factory( itemFactory ) {};
204 };
205 
206 
208 {
209  UnitSummaryWindow isw( NULL, PG_Rect( -1, -1, 500, 700 ), "cargo summary", new VehicleCounterFactory( cb ), NULL );
210  isw.Show();
211  isw.RunModal();
212 }
213 
214 
216 {
217  UnitSummaryWindow isw( NULL, PG_Rect( -1, -1, 500, 700 ), "unit summary", new VehicleCounterFactory( actmap ), actmap );
218  isw.Show();
219  isw.RunModal();
220 }
Player & getCurrentPlayer()
Definition: gamemap.h:268
CursorDistSorter(const MapCoordinate &cursorPos)
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
MapCoordinate & getCursor()
Definition: gamemap.cpp:933
int xsize
the size of the map
Definition: gamemap.h:201
Vehicle * vehicle
Definition: mapfield.h:89
int getOwner() const
returns the number of the player this vehicle/building belongs to
bool eventKeyDown(const SDL_KeyboardEvent *key)
vector< const VehicleType * > Container
bool vehicleComp(const VehicleType *v1, const VehicleType *v2)
Definition: unitset.cpp:234
ContainerBase * getContainer()
returns a pointer to the ContainerBase of the field or NULL if there is none
Definition: mapfield.cpp:331
ASCString getName() const
a single field of the map
Definition: mapfield.h:26
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
bool operator()(const MapCoordinate &x, const MapCoordinate &y)
VehicleCounterFactory(GameMap *actmap)
int beeline(const Vehicle *a, const Vehicle *b)
returns the distance between the units a and b
virtual void writeString(const string &pc, bool binary=true)
writes the C++ String pc to the stream.
Definition: basestrm.cpp:545
void calcCargoSummary(const ContainerBase *cb, Counter &summary)
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
void Show(bool fade=false)
TerrainBits getTerrainBitType(TerrainBitTypes tbt)
Coordinate on the twodimensional map.
Definition: typen.h:202
SelectionWidget * spawnNextItem(PG_Widget *parent, const PG_Point &pos)
void showUnitSummary(GameMap *actmap)
signed char actplayer
the player who is currently making his moves (may be human or AI)
Definition: gamemap.h:232
void itemSelected(const SelectionWidget *widget, bool mouse)
int ysize
Definition: gamemap.h:201
TerrainBits bdt
the terraintype properties. They determine which units can move over the field. This variable is reca...
Definition: mapfield.h:161
ASCString selectFile(const ASCString &ext, bool load, bool overwriteMessage)
const VehicleType * getVehicletype() const
void showUnitCargoSummary(ContainerBase *cb)
vector< MapCoordinate > CoordinateList
Definition: fieldmarker.h:45
const VehicleType * typ
Definition: vehicle.h:83
Building * building
Definition: mapfield.h:102
GameMap * actmap
Definition: spfst.cpp:64
UnitSummaryWindow(PG_Widget *parent, const PG_Rect &r, const ASCString &title, VehicleCounterFactory *itemFactory, GameMap *actmap)
GameMap * getMap() const
Container::iterator it
map< const VehicleType *, int > Counter
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
void showAllUnitPositions(const VehicleType *vt, GameMap *gamemap)
const Cargo & getCargo() const
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
MapField * getField(int x, int y)
Definition: gamemap.h:465