Adonthell  0.4
landmap.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 1999/2000/2001 Alexandre Courbot
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell 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  Adonthell 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 Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 
21 /**
22  * @file landmap.h
23  * Declares the landmap class.
24  */
25 
26 
27 
28 #ifndef LANDMAP_H_
29 #define LANDMAP_H_
30 
31 #include "mapobject.h"
32 #include "mapcharacter.h"
33 
34 /**
35  * Subdirectory where maps are saved.
36  */
37 #define MAPS_DIR "maps/"
38 
39 class mapview;
40 
41 /**
42  * Map where the world takes place.
43  * This class handles everything that is needed for map display. More
44  * specifically, it includes:
45  * @li submaps (i.e mapsquare_area),
46  * @li mapobjects.
47  * @li pointers to mapcharacters,
48  * It can make a map and the characters that are on it "live", but isn't
49  * designed for display. See the mapview class for that.
50  *
51  * This class is responsible for the storage of it's submaps and mapobjects,
52  * but NOT mapcharacters. So be sure to delete () yourself your mapcharacters
53  * when you don't need them anymore.
54  *
55  */
56 class landmap : public event_list
57 {
58  public:
59  /**
60  * Default constructor.
61  *
62  */
63  landmap ();
64 
65  /**
66  * Destructor.
67  *
68  */
69  ~landmap ();
70 
71  /**
72  * Cleanup the map.
73  * Totally cleanup a map, that is deleting every
74  * mapobject/mapcharacter/landsubmap it contains, and reset it to
75  * a stable state (just like it has just been created).
76  */
77  void clear ();
78 
79  /**
80  * @name Map information
81  *
82  */
83  //@{
84 
85  /**
86  * Get the number of mapobjects that the map owns.
87  * @return number of mapobjects the map contains.
88  */
90  {
91  return mobj.size ();
92  }
93 
94  /**
95  * Get the number of landsubmaps that the map owns.
96  * @return number of landsubmaps the map contains.
97  */
99  {
100  return submap.size ();
101  }
102 
103  /**
104  * Get the number of mapcharacters that are on this map.
105  * @return number of mapcharacters on this map.
106  */
108  {
109  return mapchar.size ();
110  }
111 
112  /**
113  * Get the filename of the map, i.e the file from which
114  * it has been loaded (if any).
115  *
116  *
117  * @return filename of the map.
118  */
119  string filename () const
120  {
121  return filename_;
122  }
123 
124  //@}
125 
126 
127  /**
128  * @name Individual map elements manipulation
129  * Using these methods should be avoided as long as possible. They
130  * are here for completeness, but their use should be exceptionnal.
131  *
132  */
133 
134  //@{
135 
136  /**
137  * Returns a pointer to a mapcharacter on this landmap.
138  *
139  * @param pos index of the mapcharacter to return.
140  *
141  * @return pointer to the \e pos mapcharacter.
142  */
144  {
145  return mapchar[pos];
146  }
147 
148  /**
149  * Returns a pointer to a mapobject belonging to this landmap.
150  *
151  * @param pos index of the mapobject to return.
152  *
153  * @return pointer to the \e pos mapobject.
154  */
156  {
157  return mobj[pos];
158  }
159 
160  /**
161  * Returns a pointer to a submap belonging to this landmap.
162  *
163  * @param pos index of the submap to return.
164  *
165  * @return pointer to the \e pos submap.
166  */
168  {
169  return submap[pos];
170  }
171 
172  //@}
173 
174 
175  /**
176  * @name State updating
177  *
178  */
179 
180  //@{
181 
182  /**
183  * Update the entire map (mapcharacters, mapobjects, etc... of 1 cycle.
184  *
185  */
186  void update ();
187 
188  //@}
189 
190 
191  /**
192  * @name Loading/Saving methods.
193  *
194  */
195 
196  //@{
197 
198  /**
199  * Load a map from an opened file.
200  *
201  * @param file the file from which to load.
202  *
203  * @return
204  * @li 0 in case of success.
205  * @li -1 in case of failure.
206  */
207  s_int8 get (igzstream& file);
208 
209  /**
210  * Load a map from a filename.
211  *
212  * @param fname the filename from which to load.
213  *
214  * @return
215  * @li 0 in case of success.
216  * @li -1 in case of failure.
217  */
218  s_int8 load (string fname);
219 
220  /**
221  * Put a map into an opened file.
222  *
223  * @param file the file where to save.
224  *
225  * @return
226  * @li 0 in case of success.
227  * @li -1 in case of failure.
228  */
229  s_int8 put (ogzstream& file) const;
230 
231  /**
232  * Save a map into a file.
233  *
234  * @param fname the filename where to save.
235  *
236  * @return
237  * @li 0 in case of success.
238  * @li -1 in case of failure.
239  */
240  s_int8 save (string fname);
241 
242  //@}
243 
244 
245  /**
246  * @name State loading/saving methods.
247  *
248  */
249 
250  //@{
251 
252  /**
253  * Restore the landmap's state from an opened file.
254  *
255  * @param file the opened file from which to load the state.
256  *
257  * @return 0 in case of success, error code otherwise.
258  */
259  s_int8 get_state (igzstream& file);
260 
261  /**
262  * Saves the landmap's state into an opened file.
263  *
264  * @param file the opened file where to the state.
265  *
266  * @return 0 in case of success, error code otherwise.
267  */
268  s_int8 put_state (ogzstream& file) const;
269 
270  //@}
271 
272  /**
273  * @name Landmap modification
274  * Although it should be very rare to modify a landmap
275  * during gameplay, these methods are here to allow you
276  * to safely to it. Be aware that they check if each element
277  * is in a safe state, and modify them if necessary. Therefore,
278  * they are quite slow and should be used in exceptionnal situations.
279  *
280  * Note however that put_mapobject () and remove_mapobject () should
281  * be fast enough to allow real-time map modifications. But beware anyway.
282  *
283  */
284 
285  //@{
286 
287 
288  /**
289  * Put a mapobject on the map.
290  *
291  * @param smap index of the submap to put the object on.
292  * @param px X position to put the mapobject on
293  * @param py Y position to put the mapobject on.
294  * @param mobjnbr index of the mapobject to put.
295  *
296  * @return
297  * @li 0 in case of success.
298  * @li -1 in case of failure.
299  */
301  u_int16 mobjnbr);
302 
303  /**
304  * Remove a mapobject from the map.
305  *
306  * @param smap index of the submap to remove the object on.
307  * @param px X position of the mapobject.
308  * @param py Y position of the mapobject.
309  * @param mobjnbr index of the mapobject to remove.
310  */
311  void remove_mapobject (u_int16 smap, u_int16 px, u_int16 py,
312  u_int16 mobjnbr);
313 
314 
315  /**
316  * Inserts an empty landsubmap into the landmap.
317  *
318  * The landmap can then be accessed for resizing with
319  * get_submap ()
320  *
321  * @param pos the position where to insert the submap.
322  *
323  * @return
324  * @li 0 in case of success.
325  * @li -1 in case of error.
326  *
327  * @sa get_submap ()
328  */
330 
331  /**
332  * Remove a landsubmap from the landmap.
333  *
334  * @param pos the index of the submap to remove
335  *
336  * @return
337  * @li 0 in case of success.
338  * @li -1 in case of error.
339  */
341 
342  /**
343  * Adds a mapobject to a landmap.
344  *
345  * @param an the mapobject to insert.
346  * @param pos the position where to insert the mapobject.
347  * @param srcfile the name of the file where the mapobject come from.
348  *
349  * @return
350  * @li 0 in case of success.
351  * @li -1 in case of error.
352  */
354  string srcfile = "");
355 
356  /**
357  * Delete a mapobject from a landmap.
358  *
359  * @param pos the index of the mapobject to delete.
360  *
361  * @return
362  * @li 0 in case of success.
363  * @li -1 in case of failure.
364  */
366 
367  //@}
368 
369 private:
370  /**
371  * Forbids value passing.
372  *
373  */
374  landmap (const landmap& src);
375 
376 #ifndef SWIG
377  /**
378  * Forbids landmap copy.
379  *
380  */
381  landmap & operator = (const landmap & src);
382 #endif
383 
384  string filename_;
385  vector <mapcharacter *> mapchar;
386  vector <mapobject *> mobj;
387  vector <string> mobjsrc;
388  vector <mapsquare_area *> submap;
389 
390 #ifndef SWIG
391  friend class mapcharacter;
392  friend class mapview;
393 #endif
394 
395 };
396 
397 #endif
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
Declares the mapobject class.
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
s_int8 delete_mapobject(u_int16 pos)
Delete a mapobject from a landmap.
Definition: landmap.cc:281
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
mapcharacter * get_mapcharacter(u_int16 pos)
Returns a pointer to a mapcharacter on this landmap.
Definition: landmap.h:143
s_int8 put_state(ogzstream &file) const
Saves the landmap&#39;s state into an opened file.
Definition: landmap.cc:236
s_int8 put(ogzstream &file) const
Put a map into an opened file.
Definition: landmap.cc:148
landmap()
Default constructor.
Definition: landmap.cc:32
s_int8 insert_mapobject(mapobject *an, u_int16 pos, string srcfile="")
Adds a mapobject to a landmap.
Definition: landmap.cc:260
u_int16 nbr_of_mapobjects() const
Get the number of mapobjects that the map owns.
Definition: landmap.h:89
mapsquare_area * get_submap(u_int16 pos)
Returns a pointer to a submap belonging to this landmap.
Definition: landmap.h:167
s_int8 insert_submap(u_int16 pos)
Inserts an empty landsubmap into the landmap.
Definition: landmap.cc:321
Allows you to display a landmap on a specified area of a surface.
Definition: mapview.h:48
s_int8 save(string fname)
Save a map into a file.
Definition: landmap.cc:197
s_int8 put_mapobject(u_int16 smap, u_int16 px, u_int16 py, u_int16 mobjnbr)
Put a mapobject on the map.
Definition: landmap.cc:368
void update()
Update the entire map (mapcharacters, mapobjects, etc...
Definition: landmap.cc:69
s_int8 load(string fname)
Load a map from a filename.
Definition: landmap.cc:131
u_int16 nbr_of_mapcharacters() const
Get the number of mapcharacters that are on this map.
Definition: landmap.h:107
Map where the world takes place.
Definition: landmap.h:56
string filename() const
Get the filename of the map, i.e the file from which it has been loaded (if any). ...
Definition: landmap.h:119
Representation of characters on a landmap.
Definition: mapcharacter.h:139
void remove_mapobject(u_int16 smap, u_int16 px, u_int16 py, u_int16 mobjnbr)
Remove a mapobject from the map.
Definition: landmap.cc:374
~landmap()
Destructor.
Definition: landmap.cc:36
Declares the mapcharacter class.
void clear()
Cleanup the map.
Definition: landmap.cc:41
Base class for objects that want to register events.
Definition: event_list.h:56
s_int8 get_state(igzstream &file)
Restore the landmap&#39;s state from an opened file.
Definition: landmap.cc:211
s_int8 delete_submap(u_int16 pos)
Remove a landsubmap from the landmap.
Definition: landmap.cc:341
u_int16 nbr_of_submaps() const
Get the number of landsubmaps that the map owns.
Definition: landmap.h:98
#define s_int8
8 bits long signed integer
Definition: types.h:44
mapobject * get_mapobject(u_int16 pos)
Returns a pointer to a mapobject belonging to this landmap.
Definition: landmap.h:155
Objects that can be placed on a landmap.
Definition: mapobject.h:50
Area of mapsquares, for use with landmap.
Definition: mapsquare.h:372