Advanced Strategic Command
packagemanager.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 #include "packagemanager.h"
22 #include "package.h"
23 #include "packagerepository.h"
24 
25 
26 static const int packageDataStreamVersion = 1;
27 
28 void PackageData::read ( tnstream& stream )
29 {
30  int v = stream.readInt();
31  if ( v != packageDataStreamVersion )
32  throw tinvalidversion ( "PackageData", packageDataStreamVersion, v );
33 
34  int size = stream.readInt();
35  for ( int i = 0; i < size; ++i ) {
36  ASCString s = stream.readString();
37  Package* p = new Package();
38  p->read( stream );
39  packages[s] = p;
40  }
41 }
42 
43 
44 void PackageData::write ( tnstream& stream ) const
45 {
47  stream.writeInt( packages.size() );
48  for ( Packages::const_iterator i = packages.begin(); i != packages.end(); ++i ) {
49  stream.writeString( i->first );
50  i->second->write( stream );
51  }
52 }
53 
54 
56 {
57  if( game == NULL || game->packageData == NULL )
58  return;
59 
60  for ( PackageData::Packages::const_iterator i = game->packageData->packages.begin(); i != game->packageData->packages.end(); ++i )
62 }
63 
64 void PackageManager::processContainer( const ContainerBase* container, std::set<ASCString>& archives )
65 {
66  archives.insert( container->baseType->archive );
67  for ( ContainerBase::Production::const_iterator i = container->getProduction().begin(); i != container->getProduction().end(); ++i )
68  if ( *i ) {
69  archives.insert( (*i)->archive );
70  }
71 }
72 
73 
75 {
76  if( game->packageData == NULL )
77  game->packageData = new PackageData();
78 
79  std::set<ASCString> archives;
80 
81  for ( int y = 0; y < game->ysize; ++y )
82  for ( int x = 0; x < game->xsize; ++x ) {
83  const MapField* fld = game->getField( x,y );
84  archives.insert( fld->typ->terraintype->archive );
85 
86  for ( MapField::ObjectContainer::const_iterator o = fld->objects.begin(); o != fld->objects.end(); ++o )
87  archives.insert( o->typ->archive );
88  }
89  for ( int p = 0; p <= 8; ++p ) {
90  const Player& pl = game->getPlayer( p );
91  for ( Player::VehicleList::const_iterator i = pl.vehicleList.begin(); i != pl.vehicleList.end(); ++i )
92  processContainer( *i , archives );
93 
94  for ( Player::BuildingList::const_iterator i = pl.buildingList.begin(); i != pl.buildingList.end(); ++i )
95  processContainer( *i , archives );
96  }
97 
98  game->packageData->packages.clear();
99  for ( PackageRepository::const_iterator i = packageRepository.begin(); i != packageRepository.end(); ++i ) {
100  if ( archives.find( (*i)->archive ) != archives.end() )
101  game->packageData->packages[ (*i)->name ] = *i;
102  }
103 
104 }
105 
TerrainType::Weather * typ
the terraintype of the field
Definition: mapfield.h:38
static const int packageDataStreamVersion
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
int xsize
the size of the map
Definition: gamemap.h:201
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
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
void checkPackageDependency(const Package *pack, const PackageData *packageData=NULL)
static void storeData(const GameMap *game)
void write(tnstream &stream) const
TerrainType * terraintype
pointer to the outer structure
Definition: terraintype.h:128
PackageRepository packageRepository
The interface for all kinds of IO stream.
a single field of the map
Definition: mapfield.h:26
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ASCString archive
Definition: typen.h:298
const Production & getProduction() const
virtual void writeString(const string &pc, bool binary=true)
writes the C++ String pc to the stream.
Definition: basestrm.cpp:545
void read(tnstream &stream)
static void checkGame(GameMap *game)
VehicleList vehicleList
a list of all units
Definition: player.h:135
Packages packages
int ysize
Definition: gamemap.h:201
virtual ASCString readString(bool includeCR=false)
Reads and returns a string.
Definition: basestrm.cpp:535
BuildingList buildingList
a list of all units
Definition: player.h:139
PackageData * packageData
Definition: gamemap.h:582
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
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 read(tnstream &stream)
Definition: package.cpp:28
ObjectContainer objects
Definition: mapfield.h:124
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