00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef itemrepositoryH
00019 #define itemrepositoryH
00020
00021 #include <vector>
00022 #include <sigc++/sigc++.h>
00023 #include "ascstring.h"
00024 #include "typen.h"
00025 #include "terraintype.h"
00026 #include "vehicletype.h"
00027 #include "objecttype.h"
00028 #include "buildingtype.h"
00029 #include "research.h"
00030 #include "textfile_evaluation.h"
00031 #include "objects.h"
00032 #include "package.h"
00033
00038 class TextFileDataLoader {
00039 public:
00040 virtual void readTextFiles( PropertyReadingContainer& prc, const ASCString& fileName, const ASCString& location ) = 0;
00041 virtual void read ( tnstream& stream ) = 0;
00042 virtual void write ( tnstream& stream ) = 0;
00043 virtual ASCString getTypeName() = 0;
00044 virtual void postChecks() {};
00045 virtual ~TextFileDataLoader() {};
00046 };
00047
00048
00050 extern void registerDataLoader( TextFileDataLoader* dataLoader );
00051
00053 extern void registerDataLoader( TextFileDataLoader& dataLoader );
00054
00055
00056 template<class T>
00057 class ItemRepository {
00058 protected:
00059 ASCString typeName;
00060
00061 typedef vector<T*> ItemContainerType;
00062 ItemContainerType container;
00063 typedef map<int,T*> ObjectMap;
00064 ObjectMap hash;
00065
00066 void add( T* obj );
00067
00068 map<int,int> idTranslation;
00069
00070 class RegisterID {
00071 ItemRepository<T>& repository;
00072 T* object;
00073 public:
00074 RegisterID( ItemRepository<T>& parent, T* obj ) : repository ( parent ), object(obj) {};
00075 void operator() (int id);
00076 };
00077
00078 friend class RegisterID;
00079
00080
00081 public:
00082 ItemRepository( const ASCString& typeName_ ) : typeName( typeName_ ) {};
00083
00084 T* getObject_byPos( int pos ) const { return container[pos]; };
00085
00086 T* getObject_byID( int id ) {
00087 typename ObjectMap::iterator i = hash.find( id );
00088 if ( i != hash.end() )
00089 return i->second;
00090
00091 map<int,int>::iterator j = idTranslation.find( id );
00092 if ( j != idTranslation.end())
00093 return getObject_byID( j->second );
00094
00095 return NULL;
00096 };
00097
00098 size_t getNum() const { return container.size(); };
00099
00100 vector<T*>& getVector() { return container; };
00101 virtual ~ItemRepository() {
00102 for ( typename ItemContainerType::iterator i = container.begin(); i != container.end(); ++i )
00103 delete *i;
00104 };
00105
00106
00107 void addIdTranslation( int from, int to );
00108 ASCString getTypeName() { return typeName; };
00109 };
00110
00111 template<class T>
00112 class ItemRepositoryLoader: public ItemRepository<T>, public TextFileDataLoader {
00113
00114 public:
00115 ItemRepositoryLoader( const ASCString& typeName_ ) : ItemRepository<T>( typeName_ ) {};
00116 void readTextFiles( PropertyReadingContainer& prc, const ASCString& fileName, const ASCString& location );
00117 void read( tnstream& stream );
00118 void write( tnstream& stream );
00119 ASCString getTypeName() { return ItemRepository<T>::getTypeName(); };
00120 };
00121
00122
00123
00124 class MineTypeRepository : public ItemRepository<MineType> {
00125 public:
00126 MineTypeRepository();
00127 };
00128
00129 extern MineTypeRepository mineTypeRepository;
00130
00131
00132 extern SigC::Signal0<void> dataLoaderTicker;
00133
00134
00135 extern ItemRepositoryLoader<VehicleType> vehicleTypeRepository;
00136 extern ItemRepositoryLoader<TerrainType> terrainTypeRepository;
00137 extern ItemRepositoryLoader<ObjectType> objectTypeRepository;
00138 extern ItemRepositoryLoader<BuildingType> buildingTypeRepository;
00139 extern ItemRepositoryLoader<Technology> technologyRepository;
00140
00141 extern void loadAllData( bool useCache = true );
00142
00143 typedef deallocating_vector<TechAdapter*> TechAdapterContainer;
00144 extern TechAdapterContainer techAdapterContainer;
00145
00146
00147
00148 class ItemFiltrationSystem {
00149 public:
00150 typedef enum { Building, Vehicle, Object, Terrain, Technology } Category;
00151
00152 class ItemFilter {
00153 public:
00154 typedef vector<IntRange> IntRangeArray;
00155 private:
00156 IntRangeArray buildings;
00157 IntRangeArray objects;
00158 IntRangeArray units;
00159 IntRangeArray terrain;
00160 IntRangeArray technologies;
00161 bool isContained (IntRangeArray& arr, int id );
00162 bool active;
00163 public:
00164 ItemFilter() { active = false; };
00165 ItemFilter( const ASCString& _name, const IntRangeArray& unitsetIDs, bool _active );
00166 ASCString name;
00167 bool isActive() { return active; };
00168 void setActive( bool _active ) { active = _active; };
00169 void runTextIO ( PropertyContainer& pc );
00170 void read ( tnstream& stream ) ;
00171 void write ( tnstream& stream ) ;
00172 bool isContained ( ItemFiltrationSystem::Category cat, int id );
00173 };
00174 static deallocating_vector<ItemFilter*> itemFilters;
00175
00176 class DataLoader : public TextFileDataLoader {
00177 public:
00178 void readTextFiles( PropertyReadingContainer& prc, const ASCString& fileName, const ASCString& location );
00179 void read ( tnstream& stream ) ;
00180 void write ( tnstream& stream ) ;
00181 ASCString getTypeName() { return "itemfilter"; };
00182 };
00183
00184
00185 static bool isFiltered ( Category cat, int id );
00186 static bool isFiltered( const VehicleType* item );
00187 static bool isFiltered( const BuildingType* item );
00188 static bool isFiltered( const ObjectType* item );
00189 static bool isFiltered( const TerrainType* item );
00190 static bool isFiltered( const MineType* item );
00191
00192 };
00193
00194 #endif