unitcounting.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           cargodialog.cpp  -  description
00003                              -------------------
00004     begin                : Tue Oct 24 2000
00005     copyright            : (C) 2000 by Martin Bickel
00006     email                : bickel@asc-hq.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <pgimage.h>
00019 #include <pgtooltiphelp.h>
00020 #include "unitcounting.h"
00021 #include "vehicletypeselector.h"
00022 #include "../vehicle.h"
00023 #include "../containerbase.h"
00024 #include "../gamemap.h"
00025 #include "../unitset.h"
00026 #include "fileselector.h"
00027 
00028 
00029 
00030 class VehicleCounterFactory: public SelectionItemFactory, public SigC::Object  {
00031 public:
00032     typedef vector<const VehicleType*> Container;
00033 protected:
00034     Container::iterator it;
00035     Container items;
00036 
00037     typedef map<const VehicleType*,int> Counter;
00038     Counter counter;
00039     GameMap* gamemap;
00040 
00041     void calcCargoSummary( const ContainerBase* cb, Counter& summary )
00042     {
00043         for ( ContainerBase::Cargo::const_iterator i = cb->getCargo().begin(); i != cb->getCargo().end(); ++i )
00044             if ( *i ) {
00045                 calcCargoSummary( *i, summary );
00046                 if ( (*i)->getOwner() == cb->getMap()->actplayer )
00047                     summary[ (*i)->typ] += 1;
00048             }
00049     }
00050 
00051 
00052 public:
00053     VehicleCounterFactory( GameMap* actmap );
00054     VehicleCounterFactory( const ContainerBase* container );
00055 
00056     ASCString toString();
00057 
00058     void restart();
00059     SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos );
00060     void itemSelected( const SelectionWidget* widget, bool mouse ) {}
00061 };
00062 
00063 VehicleCounterFactory :: VehicleCounterFactory( GameMap* actmap ) : gamemap ( actmap )
00064 {
00065     for ( int y = 0; y < actmap->ysize; ++y )
00066         for ( int x = 0; x < actmap->xsize; ++x ) {
00067             MapField* fld = actmap->getField(x,y);
00068             if ( fld ) {
00069                 if ( fld->vehicle ) {
00070                     calcCargoSummary( fld->vehicle, counter );
00071                     if ( fld->vehicle->getOwner() == actmap->actplayer )
00072                         counter[ fld->vehicle->typ] += 1;
00073                 }
00074                 if ( fld->building && (fld->bdt & getTerrainBitType(cbbuildingentry)).any() )
00075                     calcCargoSummary( fld->getContainer(), counter );
00076             }
00077         }
00078 
00079     for ( Counter::iterator i = counter.begin(); i != counter.end(); ++i )
00080         items.push_back( i->first );
00081 
00082     sort( items.begin(), items.end(), vehicleComp );
00083     restart();
00084 };
00085 
00086 
00087 VehicleCounterFactory::VehicleCounterFactory( const ContainerBase* container ) : gamemap( container->getMap() )
00088 {
00089     calcCargoSummary( container, counter );
00090     for ( Counter::iterator i = counter.begin(); i != counter.end(); ++i )
00091         items.push_back( i->first );
00092 
00093     sort( items.begin(), items.end(), vehicleComp );
00094     restart();
00095 }
00096 
00097 
00098 
00099 void VehicleCounterFactory::restart()
00100 {
00101     it = items.begin();
00102 };
00103 
00104 
00105 SelectionWidget* VehicleCounterFactory::spawnNextItem( PG_Widget* parent, const PG_Point& pos )
00106 {
00107     if ( it != items.end() ) {
00108         const VehicleType* v = *(it++);
00109         return new VehicleTypeCountWidget( parent, pos, parent->Width() - 15, v, gamemap->getCurrentPlayer(), counter[v] );
00110     } else
00111         return NULL;
00112 };
00113 
00114 ASCString VehicleCounterFactory::toString()
00115 {
00116     ASCString t;
00117     for ( Counter::const_iterator i = counter.begin(); i != counter.end(); ++i ) {
00118         const VehicleType* v = i->first;
00119         t += v->getName() + "\t" + ASCString::toString(i->second) + "\n";
00120     }
00121     return t;
00122 }
00123 
00124 
00125 /*
00126 void VehicleCounterFactory::itemSelected( const SelectionWidget* widget, bool mouse )
00127 {
00128    if ( !widget )
00129       return;
00130 
00131    const VehicleTypeBaseWidget* fw = dynamic_cast<const VehicleTypeBaseWidget*>(widget);
00132    assert( fw );
00133 }
00134 */
00135 
00136 #include "fieldmarker.h"
00137 
00138 struct CursorDistSorter : public binary_function<const MapCoordinate&, const MapCoordinate&, bool> {
00139     CursorDistSorter( const MapCoordinate& cursorPos) : cursor( cursorPos ) {};
00140     bool operator()(const MapCoordinate& x, const MapCoordinate& y) {
00141         return beeline(x,cursor) < beeline(y,cursor);
00142     }
00143 private:
00144     MapCoordinate cursor;
00145 };
00146 
00147 
00148 void showAllUnitPositions( const VehicleType* vt, GameMap* gamemap )
00149 {
00150     Player& player = gamemap->getPlayer( gamemap->actplayer );
00151 
00152     SelectFromMap::CoordinateList coordinates;
00153 
00154     for ( Player::VehicleList::iterator i = player.vehicleList.begin(); i != player.vehicleList.end(); ++i ) {
00155         if ( (*i)->typ == vt )
00156             if ( find ( coordinates.begin(), coordinates.end(),(*i)->getPosition() ) == coordinates.end() )
00157                 coordinates.push_back ( (*i)->getPosition() );
00158     }
00159 
00160     sort( coordinates.begin(), coordinates.end(), CursorDistSorter( gamemap->getCursor()) );
00161 
00162     SelectFromMap sfm( coordinates, gamemap, false, true );
00163     sfm.Show();
00164     sfm.RunModal();
00165 }
00166 
00167 class UnitSummaryWindow : public ItemSelectorWindow {
00168 private:
00169     GameMap* gamemap;
00170     VehicleCounterFactory* factory;
00171 
00172     virtual void itemSelected( const SelectionWidget* sw) {
00173         const VehicleTypeCountWidget* vtcw = dynamic_cast<const VehicleTypeCountWidget*>(sw);
00174         assert( vtcw );
00175 
00176         if ( gamemap ) {
00177             Hide();
00178             showAllUnitPositions( vtcw->getVehicletype(), gamemap );
00179             Show();
00180         }
00181     };
00182 
00183 
00184     void saveText()
00185     {
00186         ASCString name = selectFile( "*.txt", false );
00187         if ( !name.empty() ) {
00188             tn_file_buf_stream s( name, tnstream::writing );
00189             s.writeString( factory->toString(), true );
00190         }
00191     }
00192 
00193     bool eventKeyDown(const SDL_KeyboardEvent* key) {
00194         int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
00195         if ( mod & KMOD_CTRL )
00196             if ( key->keysym.sym == SDLK_s )
00197                 saveText();
00198 
00199         return ItemSelectorWindow::eventKeyDown( key );
00200     }
00201 
00202 public:
00203     UnitSummaryWindow ( PG_Widget *parent, const PG_Rect &r , const ASCString& title, VehicleCounterFactory* itemFactory, GameMap* actmap ) : ItemSelectorWindow( parent, r, title, itemFactory ), gamemap( actmap ), factory( itemFactory ) {};
00204 };
00205 
00206 
00207 void showUnitCargoSummary( ContainerBase* cb )
00208 {
00209     UnitSummaryWindow isw( NULL, PG_Rect( -1, -1, 500, 700 ),  "cargo summary", new VehicleCounterFactory( cb ), NULL );
00210     isw.Show();
00211     isw.RunModal();
00212 }
00213 
00214 
00215 void showUnitSummary( GameMap* actmap )
00216 {
00217     UnitSummaryWindow isw( NULL, PG_Rect( -1, -1, 500, 700 ),  "unit summary", new VehicleCounterFactory( actmap ), actmap );
00218     isw.Show();
00219     isw.RunModal();
00220 }

Generated on Mon May 21 01:26:38 2012 for Advanced Strategic Command by  doxygen 1.5.1