Adonthell  0.4
mapsquare.h
Go to the documentation of this file.
1 /*
2  $Id: mapsquare.h,v 1.13 2003/02/23 23:14:34 ksterker Exp $
3 
4  Copyright (C) 2001 Alexandre Courbot
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 /**
17  * @file mapsquare.h
18  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
19  *
20  * @brief Declares the mapsquare and mapsquare_area classes.
21  *
22  *
23  */
24 
25 
26 #ifndef MAPSQUARE_H_
27 #define MAPSQUARE_H_
28 
29 #include <list>
30 
31 #include "mapsquare_walkable.h"
32 
33 class mapobject;
34 class mapcharacter;
35 
36 /**
37  * Contains informations about the position of an object on a map.
38  *
39  * Objects of this class has no reason to exist outside of a mapsquare.
40  * You'll NEVER want to manipulate this class directly - only mapsquare,
41  * mapsquare_area and landmap will.
42  */
44 {
45 public:
46  /**
47  * Default constructor.
48  */
49  mapsquare_tile ();
50 
51  /**
52  * Destructor.
53  */
54  ~mapsquare_tile ();
55 
56 #ifndef SWIG
57  /**
58  * Compare the location on the landsubmap of two mapsquare_tiles.
59  * A mapsquare_tile is < to another if it's Y position is < to the other
60  * one's or if it's Y position == the other one's and it's X position is
61  * < to the other one's.
62  *
63  * @attention Not available from Python.
64  *
65  * @sa operator <= ()
66  * @sa operator == ()
67  */
68  bool operator < (const mapsquare_tile & mt)
69  {
70  return (mt.y > y || (mt.y == y && mt.x > x));
71  }
72 
73  /**
74  * Compare the location on the landsubmap of two mapsquare_tiles.
75  * A mapsquare_tile is <= to another if it's Y position is < to the other
76  * one's or if it's Y position == the other one's and it's X position is
77  * <= to the other one's.
78  *
79  * @attention Not available from Python.
80  *
81  * @sa operator < ()
82  * @sa operator == ()
83  */
84  bool operator <= (const mapsquare_tile & mt)
85  {
86  return (mt.y > y || (mt.y == y && mt.x >= x));
87  }
88 
89  /**
90  * Compare the location on the landsubmap of two mapsquare_tiles.
91  * A mapsquare_tile is == to another if their X and Y position are
92  * equal.
93  *
94  * @attention Not available from Python.
95  *
96  * @sa operator < ()
97  * @sa operator <= ()
98  */
99  bool operator == (const mapsquare_tile & mt)
100  {
101  return (mt.y == y && mt.x == x);
102  }
103 #endif
104 
105 
106 private:
107  /// Pointer to the object here.
108  mapobject * mapobj;
109 
110  /// Is this mapsquare_tile a base square?
111  bool is_base;
112 
113 #ifndef SWIG
114  /// Iterator to the base tile of this mapsquare_tile.
115  list <mapsquare_tile>::iterator base_tile;
116 #endif
117 
118  /// x and y positions.
119  u_int16 x, y;
120 
121 #ifndef SWIG
122  friend class mapsquare;
123  friend class mapsquare_area;
124  friend class landmap;
125  friend class mapview;
126 #endif
127 };
128 
129 
130 /**
131  * Contains informations about the position of a character on a map.
132  *
133  * Objects of this class has no reason to exist outside of a mapsquare.
134  * You'll NEVER want to manipulate this class directly - only mapsquare,
135  * mapsquare_area and landmap will.
136  */
138 {
139 public:
140  /**
141  * Default constructor.
142  */
143  mapsquare_char ();
144 
145  /**
146  * Destructor.
147  */
148  ~mapsquare_char ();
149 
150 #ifndef SWIG
151  /**
152  * Compare the location on the landsubmap of two mapsquare_chars.
153  * A mapsquare_char is < to another if it's Y position is < to the other
154  * one's or if it's Y position == the other one's and it's X position is
155  * < to the other one's.
156  * @sa operator <= ()
157  * @sa operator == ()
158  */
159  bool operator < (const mapsquare_char & mt)
160  {
161  return (mt.y > y || (mt.y == y && mt.x > x));
162  }
163 
164  /**
165  * Compare the location on the landsubmap of two mapsquare_chars.
166  * A mapsquare_char is <= to another if it's Y position is < to the other
167  * one's or if it's Y position == the other one's and it's X position is
168  * <= to the other one's.
169  * @sa operator < ()
170  * @sa operator == ()
171  */
172  bool operator <= (const mapsquare_char & mt)
173  {
174  return (mt.y > y || (mt.y == y && mt.x >= x));
175  }
176 
177  /**
178  * Compare the location on the landsubmap of two mapsquare_chars.
179  * A mapsquare_char is == to another if their X and Y position are
180  * equal.
181  * @sa operator < ()
182  * @sa operator <= ()
183  */
184  bool operator == (const mapsquare_char & mt)
185  {
186  return (mt.y == y && mt.x == x);
187  }
188 #endif
189 
190 private:
191  /// Pointer to the mapcharacter concerned by this mapchar_square.
192  mapcharacter *mchar;
193 
194  /// Is it the base tile?
195  bool is_base;
196 
197  /// Is this mapsquare_tile walkable?
198  bool walkable;
199 
200 #ifndef SWIG
201  /// Iterator to the base tile of this mapsquare_char.
202  list <mapsquare_char>::iterator base_tile;
203 #endif
204 
205  /// x and y positions.
206  u_int16 x, y;
207 
208 #ifndef SWIG
209  friend class mapcharacter;
210  friend class mapsquare;
211  friend class landmap;
212  friend class mapview;
213 #endif
214 };
215 
216 
217 /**
218  * Base unit of a landsubmap, where you can place mapobjects or mapcharacters.
219  * A landsubmap is a 2 dimensionnal array of mapsquares. When a mapobject is
220  * placed on a landsubmap, it belongs to one or several mapsquares. A mapsquare
221  * is made of a list of mapsquare_tiles, containing informations about the objects
222  * that are on it, and a list of mapsquare_char, which informs about the mapcharacters
223  * here. This make it possible to have several mapobjects
224  * and mapcharacters on the same mapsquare.
225  *
226  * These two lists are sorted by the position of the object or mapcharacter's base square
227  * on the map. This make it fast to iterate through the lists during drawing, as we always
228  * want to iterate the list in this order.
229  */
231 {
232 public:
233  /**
234  * Default constructor.
235  *
236  */
237  mapsquare ();
238 
239 #ifndef SWIG
240  /**
241  * Copy constructor.
242  *
243  */
244  mapsquare (const mapsquare& src);
245 #endif
246 
247  /**
248  * Destructor.
249  *
250  */
251  ~mapsquare ();
252 
253  /**
254  * Returns the X position of this mapsquare.
255  *
256  *
257  * @return X position of this mapsquare.
258  */
260  {
261  return x_;
262  }
263 
264  /**
265  * Returns the Y position of this mapsquare.
266  *
267  *
268  * @return Y position of this mapsquare.
269  */
271  {
272  return y_;
273  }
274 
275  /**
276  * Returns whether the mapsquare is free for a character to go on or not.
277  * It only checks if a mapcharacter is already here. It doesn't deal with the
278  * walkable problem.
279  * @return
280  * - false if the mapsquare isn't free.
281  * - true if the mapsquare is free.
282  */
283  bool is_free ();
284 
285  /**
286  * Return a pointer to the mapcharacter that occupies this mapsquare.
287  *
288  * @return pointer to the mapcharacter that occupies the mapsquare, NULL if none.
289  */
290  mapcharacter *whoshere ();
291 
292  /**
293  * @name Pathfinding data members.
294  *
295  * These members are here to allow faster and more efficient
296  * pathfinding. Though they can as well be used for something else,
297  * but their value isn't guaranteed to stay constant. It is safe
298  * to modify them however, so they are public and uninitialised.
299  *
300  */
301  //@{
302 
303  /**
304  * Distance from the source square.
305  *
306  */
308 
309  /**
310  * Estimated distance to the goal square.
311  *
312  */
314 
315  /**
316  * Sum of g + h.
317  *
318  */
320 
321  /**
322  * Parent square for the path
323  *
324  */
326 
327  /**
328  * If == false, then this square will never be considered
329  * as walkable by pathfinding functions.
330  *
331  */
333 
334  //@}
335 
336 private:
337 #ifndef SWIG
338  /// List of mapsquare_tiles.
339  list <mapsquare_tile> tiles;
340 
341  /// Iterator to where the base tiles begin.
342  /// This serves as an "accelerator" for mapview::draw () which
343  /// can go directly to the base tiles of a squares with this
344  /// iterator.
345  list <mapsquare_tile>::iterator base_begin;
346 
347  /// List of mapsquare_chars.
348  list <mapsquare_char> mapchars;
349 
350  /// Coordinates of the square.
351  u_int16 x_, y_;
352 
353  friend class mapcharacter;
354  friend class mapsquare_area;
355  friend class landmap;
356  friend class mapview;
357 #endif
358 }; // mapsquare
359 
360 /**
361  * Area of mapsquares, for use with landmap.
362  *
363  * This class has no reason to exist is not belonging
364  * to a landmap. You'll NEVER use this class directly -
365  * anyway you can't do anything usefull with it alone.
366  *
367  */
369 {
370 public:
371  /**
372  * Default constructor.
373  *
374  */
375  mapsquare_area ();
376 
377  /**
378  * Destructor.
379  *
380  */
381  ~mapsquare_area ();
382 
383  /**
384  * Totally clears the area.
385  *
386  */
387  void clear ();
388 
389  /**
390  * @name Area settings
391  *
392  */
393  //@{
394 
395  /**
396  * Returns the length of the area.
397  *
398  * @return length (in number of squares) of the area.
399  *
400  */
402  {
403  return area.size ();
404  }
405 
406  /**
407  * Returns the height of the area.
408  *
409  * @return height (in number of squares) of the area.
410  *
411  */
413  {
414  if (area.size ()) return area[0].size ();
415  else return 0;
416  }
417 
418  /**
419  * Returns a pointer to a desired square.
420  *
421  * @param x X position of the square to get.
422  * @param y Y position of the square to get.
423  *
424  * @return pointer to the (x,y) square.
425  */
427  {
428  return &(area[x][y]);
429  }
430 
431  /**
432  * Resize the area.
433  *
434  * @param nl new length (in number of squares) of the area.
435  * @param nh new height (in number of squares) of the area.
436  */
437  void resize_area (u_int16 nl, u_int16 nh);
438 
439  //@}
440 
441 private:
442  /**
443  * Forbids value passing.
444  *
445  */
446  mapsquare_area (const mapsquare_area& src);
447 
448 #ifndef SWIG
449  /**
450  * Forbids mapsquare_area copy.
451  *
452  */
453  mapsquare_area & operator = (const mapsquare_area & mo);
454 #endif
455 
456  /**
457  * Place a mapobject on the submap.
458  *
459  * @param px X position of the base square of the object.
460  * @param py Y position of the base square of the object.
461  * @param mobj pointer to the mapobject to remove.
462  */
463  s_int8 put_mapobject (u_int16 px, u_int16 py, mapobject * mobj);
464 
465  /**
466  * Remove a mapobject from the submap.
467  *
468  * @param px X position of the base square of the object.
469  * @param py Y position of the base square of the object.
470  * @param mobj pointer to the mapobject to remove.
471  */
472  void remove_mapobject (u_int16 px, u_int16 py, mapobject * mobj);
473 
474  /**
475  * 2 dimensionnal array of mapsquares - the actual map
476  *
477  */
478 #ifndef SWIG
479  mutable vector <vector<mapsquare> > area;
480 
481  friend class mapcharacter;
482  friend class mapview;
483  friend class landmap;
484 #endif
485 };
486 
487 #endif
u_int16 x()
Returns the X position of this mapsquare.
Definition: mapsquare.h:259
#define u_int16
16 bits long unsigned integer
Definition: types.h:32
mapsquare * parent
Parent square for the path.
Definition: mapsquare.h:325
u_int16 f
Sum of g + h.
Definition: mapsquare.h:319
mapsquare * get_square(u_int16 x, u_int16 y) const
Returns a pointer to a desired square.
Definition: mapsquare.h:426
u_int16 area_height() const
Returns the height of the area.
Definition: mapsquare.h:412
Contains informations about the position of an object on a map.
Definition: mapsquare.h:43
bool can_use_for_pathfinding
If == false, then this square will never be considered as walkable by pathfinding functions...
Definition: mapsquare.h:332
Declares the mapsquare_walkable and mapsquare_walkable_area classes.
u_int16 area_length() const
Returns the length of the area.
Definition: mapsquare.h:401
Contains informations about the position of a character on a map.
Definition: mapsquare.h:137
~mapsquare_tile()
Destructor.
Definition: mapsquare.cc:36
u_int16 g
Distance from the source square.
Definition: mapsquare.h:307
u_int16 y()
Returns the Y position of this mapsquare.
Definition: mapsquare.h:270
Allows you to display a landmap on a specified area of a surface.
Definition: mapview.h:44
Contains information about the walkability of a mapsquare.
Base unit of a landsubmap, where you can place mapobjects or mapcharacters.
Definition: mapsquare.h:230
bool operator==(const mapsquare_tile &mt)
Compare the location on the landsubmap of two mapsquare_tiles.
Definition: mapsquare.h:99
Map where the world takes place.
Definition: landmap.h:52
u_int16 h
Estimated distance to the goal square.
Definition: mapsquare.h:313
Representation of characters on a landmap.
Definition: mapcharacter.h:135
bool operator<=(const mapsquare_tile &mt)
Compare the location on the landsubmap of two mapsquare_tiles.
Definition: mapsquare.h:84
bool operator<(const mapsquare_tile &mt)
Compare the location on the landsubmap of two mapsquare_tiles.
Definition: mapsquare.h:68
#define s_int8
8 bits long signed integer
Definition: types.h:38
mapsquare_tile()
Default constructor.
Definition: mapsquare.cc:30
Objects that can be placed on a landmap.
Definition: mapobject.h:46
Area of mapsquares, for use with landmap.
Definition: mapsquare.h:368