Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

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          tfield* 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 void showAllUnitPositions( const Vehicletype* vt, GameMap* gamemap ) 
00139 {
00140    Player& player = gamemap->getPlayer( gamemap->actplayer );
00141    
00142    SelectFromMap::CoordinateList coordinates;
00143    
00144    for ( Player::VehicleList::iterator i = player.vehicleList.begin(); i != player.vehicleList.end(); ++i ) {
00145       if ( (*i)->typ == vt )
00146          if ( find ( coordinates.begin(), coordinates.end(),(*i)->getPosition() ) == coordinates.end() ) 
00147             coordinates.push_back ( (*i)->getPosition() );
00148    }
00149    
00150    SelectFromMap sfm( coordinates, gamemap, false, true );
00151    sfm.Show();
00152    sfm.RunModal();
00153 }
00154 
00155 class UnitSummaryWindow : public ItemSelectorWindow {
00156    private:
00157       GameMap* gamemap;
00158       VehicleCounterFactory* factory;
00159       
00160       virtual void itemSelected( const SelectionWidget* sw) {
00161          const VehicleTypeCountWidget* vtcw = dynamic_cast<const VehicleTypeCountWidget*>(sw);
00162          assert( vtcw );
00163          
00164          if ( gamemap ) {
00165             Hide();
00166             showAllUnitPositions( vtcw->getVehicletype(), gamemap );
00167             Show();
00168          }
00169       };
00170 
00171 
00172       void saveText()
00173       {
00174          ASCString name = selectFile( "*.txt", false );
00175          if ( !name.empty() ) {
00176             tn_file_buf_stream s( name, tnstream::writing );
00177             s.writeString( factory->toString(), true );
00178          }
00179       }
00180 
00181       bool eventKeyDown(const SDL_KeyboardEvent* key) {
00182          int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
00183          if ( mod & KMOD_CTRL ) 
00184             if ( key->keysym.sym == SDLK_s )
00185                saveText();
00186 
00187          return ItemSelectorWindow::eventKeyDown( key );
00188       }
00189 
00190    public:
00191       UnitSummaryWindow ( PG_Widget *parent, const PG_Rect &r , const ASCString& title, VehicleCounterFactory* itemFactory, GameMap* actmap ) : ItemSelectorWindow( parent, r, title, itemFactory ), gamemap( actmap ), factory( itemFactory ) {};
00192 };      
00193       
00194 
00195 void showUnitCargoSummary( ContainerBase* cb )
00196 {
00197    UnitSummaryWindow isw( NULL, PG_Rect( -1, -1, 500, 700 ),  "cargo summary", new VehicleCounterFactory( cb ), NULL );
00198    isw.Show();
00199    isw.RunModal();
00200 }
00201 
00202 
00203 void showUnitSummary( GameMap* actmap )
00204 {
00205    UnitSummaryWindow isw( NULL, PG_Rect( -1, -1, 500, 700 ),  "unit summary", new VehicleCounterFactory( actmap ), actmap );
00206    isw.Show();
00207    isw.RunModal();
00208 }

Generated on Tue Jun 24 01:27:53 2008 for Advanced Strategic Command by  doxygen 1.4.2