initial commit, lordsawar source, slightly modified
[lordsawar] / src / armysetlist.h
1 // Copyright (C) 2001, 2002, 2003 Michael Bartl
2 // Copyright (C) 2003, 2004, 2005 Ulf Lorenz
3 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
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 3 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 Library 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; if not, write to the Free Software
17 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
18 //  02110-1301, USA.
19
20 #ifndef ARMYSETLIST_H
21 #define ARMYSETLIST_H
22
23 #include <gtkmm.h>
24 #include <string>
25 #include <map>
26 #include <vector>
27 #include <sigc++/trackable.h>
28
29 #include "xmlhelper.h"
30 #include "armyproto.h"
31 #include "armyset.h"
32
33 class Tar_Helper;
34
35 //! A list of all Armyset objects available to the game.
36 /** 
37  * This class contains a list of all armyset objects available to the game. 
38  * Each armyset has a size, a name and a list of armies. The armysetlist 
39  * shields all these from the rest of the program.  Armysets are most often
40  * referenced by their id.
41  *
42  * The Armysetlist is populated with Armyset objects that are loaded from the 
43  * army/ directory.
44  *
45  * Since several classes access this class, it is implemented as a singleton.
46  */
47
48 class Armysetlist : public std::list<Armyset*>, public sigc::trackable
49 {
50     public:
51         //! Return the singleton instance of this class.
52         static Armysetlist* getInstance();
53
54         //! Explicitly delete the singleton instance of this class
55         static void deleteInstance();
56
57         //! Returns an army prototype from a given armyset.
58         /** 
59          * @param id       The Id of the armyset.
60          * @param index    The index of the army within the set.
61          *                 This value becomes the Army object's type.
62          *
63          * @return The requested army or 0 on error.
64          */
65         ArmyProto* getArmy(guint32 id, guint32 index) const;
66
67         //! Returns an army prototype of a scout from a given armyset.
68         /** 
69          * @param id       The Id of the armyset.
70          *
71          * @return The requested scout or 0 on error.
72          */
73         ArmyProto* getScout(guint32 id) const;
74
75         //! Get the unshaded ship image for the given Armyset.
76         PixMask* getShipPic (guint32 id);
77
78         //! Get the ship mask picture for the given Armyset.
79         PixMask* getShipMask (guint32 id);
80
81         //! Get the unshaded planted standard picture for the given Armyset.
82         PixMask* getStandardPic (guint32 id);
83
84         //! Get the bag of oitems picture for the given Armyset.
85         PixMask* getBagPic (guint32 id);
86
87         //! Get the planted standard mask for the given Armyset.
88         PixMask* getStandardMask (guint32 id);
89         guint32 getTileSize(guint32 id);
90
91         //! Returns the size of a specific armyset.
92         /** 
93          * @param id       The id of the armyset to get the size of.
94          *
95          * @return The number of Army prototype objects in the Armyset.
96          *         Returns 0 on error. 
97          */
98         guint32 getSize(guint32 id) const;
99
100         //! Return the name of a given armyset.
101         /** 
102          * @param id       The id of the armyset to get the name of.
103          *
104          * @return The name of the Armyset or an empty string on error.
105          */
106         std::string getName(guint32 id) const;
107
108         //! Returns the different tilesizes present in the armysetlist.
109         void getSizes(std::list<guint32> &sizes);
110
111         //! Returns the names of all Armyset objects available to the game.
112         std::list<std::string> getNames();
113
114         //! Returns the names of armysets that have the given tile size.
115         std::list<std::string> getNames(guint32 tilesize);
116
117         /** Returns the Id of a specific armyset by name
118           * 
119           * @param armyset       the name of the armyset
120           * @param tilesize      the height and width of tiles in the armyset.
121           *
122           * @return the id of the armyset (0 on error)
123           */
124         guint32 getArmysetId(std::string armyset, guint32 tilesize);
125         Armyset *getArmyset(guint32 id);
126
127         //! Return the Armyset object by the name of the subdir.
128         /**
129          * @param dir  The directory where the Armyset resides on disk.
130          *             This value does not contain any slashes, and is
131          *             presumed to be found inside the army/ directory.
132          */
133         Armyset *getArmyset(std::string dir);
134
135         //! Return the name of the subdirectory for a given armyset.
136         /** 
137          * @param name          The name of the Armyset to get the subdir of.
138          * @param tilesize      The size of the Armyset to get the subdir of.
139          *
140          * @return The name of the directory that holds the cityset.  See 
141          *         Armyset::d_dir for more information about the nature of 
142          *         the return value.
143          */
144         std::string getArmysetDir(std::string name, guint32 tilesize);
145
146
147         //! Return an unused armyset number.
148         static int getNextAvailableId(int after = 0);
149
150         void add(Armyset *arymset);
151
152         void instantiateImages();
153         void uninstantiateImages();
154         
155
156         Armyset *import(Tar_Helper *t, std::string f, bool &broken);
157     private:
158         bool addToPersonalCollection(Armyset *armyset, std::string &new_subdir, guint32 &new_id);
159         //! Default Constructor.  Loads all armyset objects it can find.
160         /**
161          * The army/ directory is scanned for armyset directories.
162          */
163         Armysetlist();
164         
165         //! Destructor.
166         ~Armysetlist();
167
168         //! Loads a specific armyset.
169         /**
170          * Load the armyset from an armyset configuration file and add it to 
171          * this list of armysets.
172          *
173          * @param name  The subdirectory name that the Armyset resides in.
174          *
175          * @return the Armyset.  NULL otherwise.
176          */
177         Armyset* loadArmyset(std::string name);
178           
179         //! Loads a bunch of armysets.
180         /**
181          * Load a list of armysets from the system armyset directory, or the
182          * user's personal collection.
183          */
184         void loadArmysets(std::list<std::string> armysets);
185         
186         typedef std::map<guint32, std::vector<ArmyProto*> > ArmyPrototypeMap;
187         typedef std::map<guint32, std::string> NameMap;
188         typedef std::map<std::string, guint32> IdMap;
189         typedef std::map<std::string, Armyset*> ArmysetMap;
190         typedef std::map<guint32, Armyset*> ArmysetIdMap;
191         
192         //! A map that provides Army objects by their index.
193         ArmyPrototypeMap d_armies;
194
195         //! A map that provides Armyset::d_name by supplying a Armyset::d_id.
196         NameMap d_names;
197
198         //! A map that provides Armyset:d_id by supplying a Armyset::d_name.
199         IdMap d_ids;
200
201         //! A map that provides an Armyset when supplying a subdirectory name.
202         ArmysetMap d_armysets;
203
204         //! A map that provides an Armyset when supplying a subdirectory name.
205         ArmysetIdMap d_armysetids;
206
207         //! A static pointer for the singleton instance.
208         static Armysetlist* s_instance;
209 };
210
211 #endif // ARMYSETLIST_H
212