00001 /* 00002 $Id: mapsquare.h,v 1.13 2003/02/23 23:14:34 ksterker Exp $ 00003 00004 Copyright (C) 2001 Alexandre Courbot 00005 Part of the Adonthell Project http://adonthell.linuxgames.com 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License. 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY. 00011 00012 See the COPYING file for more details. 00013 */ 00014 00015 00016 /** 00017 * @file mapsquare.h 00018 * @author Alexandre Courbot <alexandrecourbot@linuxgames.com> 00019 * 00020 * @brief Declares the mapsquare and mapsquare_area classes. 00021 * 00022 * 00023 */ 00024 00025 00026 #ifndef MAPSQUARE_H_ 00027 #define MAPSQUARE_H_ 00028 00029 #include <list> 00030 00031 #include "mapsquare_walkable.h" 00032 00033 class mapobject; 00034 class mapcharacter; 00035 00036 /** 00037 * Contains informations about the position of an object on a map. 00038 * 00039 * Objects of this class has no reason to exist outside of a mapsquare. 00040 * You'll NEVER want to manipulate this class directly - only mapsquare, 00041 * mapsquare_area and landmap will. 00042 */ 00043 class mapsquare_tile 00044 { 00045 public: 00046 /** 00047 * Default constructor. 00048 */ 00049 mapsquare_tile (); 00050 00051 /** 00052 * Destructor. 00053 */ 00054 ~mapsquare_tile (); 00055 00056 #ifndef SWIG 00057 /** 00058 * Compare the location on the landsubmap of two mapsquare_tiles. 00059 * A mapsquare_tile is < to another if it's Y position is < to the other 00060 * one's or if it's Y position == the other one's and it's X position is 00061 * < to the other one's. 00062 * 00063 * @attention Not available from Python. 00064 * 00065 * @sa operator <= () 00066 * @sa operator == () 00067 */ 00068 bool operator < (const mapsquare_tile & mt) 00069 { 00070 return (mt.y > y || (mt.y == y && mt.x > x)); 00071 } 00072 00073 /** 00074 * Compare the location on the landsubmap of two mapsquare_tiles. 00075 * A mapsquare_tile is <= to another if it's Y position is < to the other 00076 * one's or if it's Y position == the other one's and it's X position is 00077 * <= to the other one's. 00078 * 00079 * @attention Not available from Python. 00080 * 00081 * @sa operator < () 00082 * @sa operator == () 00083 */ 00084 bool operator <= (const mapsquare_tile & mt) 00085 { 00086 return (mt.y > y || (mt.y == y && mt.x >= x)); 00087 } 00088 00089 /** 00090 * Compare the location on the landsubmap of two mapsquare_tiles. 00091 * A mapsquare_tile is == to another if their X and Y position are 00092 * equal. 00093 * 00094 * @attention Not available from Python. 00095 * 00096 * @sa operator < () 00097 * @sa operator <= () 00098 */ 00099 bool operator == (const mapsquare_tile & mt) 00100 { 00101 return (mt.y == y && mt.x == x); 00102 } 00103 #endif 00104 00105 00106 private: 00107 /// Pointer to the object here. 00108 mapobject * mapobj; 00109 00110 /// Is this mapsquare_tile a base square? 00111 bool is_base; 00112 00113 #ifndef SWIG 00114 /// Iterator to the base tile of this mapsquare_tile. 00115 list <mapsquare_tile>::iterator base_tile; 00116 #endif 00117 00118 /// x and y positions. 00119 u_int16 x, y; 00120 00121 #ifndef SWIG 00122 friend class mapsquare; 00123 friend class mapsquare_area; 00124 friend class landmap; 00125 friend class mapview; 00126 #endif 00127 }; 00128 00129 00130 /** 00131 * Contains informations about the position of a character on a map. 00132 * 00133 * Objects of this class has no reason to exist outside of a mapsquare. 00134 * You'll NEVER want to manipulate this class directly - only mapsquare, 00135 * mapsquare_area and landmap will. 00136 */ 00137 class mapsquare_char 00138 { 00139 public: 00140 /** 00141 * Default constructor. 00142 */ 00143 mapsquare_char (); 00144 00145 /** 00146 * Destructor. 00147 */ 00148 ~mapsquare_char (); 00149 00150 #ifndef SWIG 00151 /** 00152 * Compare the location on the landsubmap of two mapsquare_chars. 00153 * A mapsquare_char is < to another if it's Y position is < to the other 00154 * one's or if it's Y position == the other one's and it's X position is 00155 * < to the other one's. 00156 * @sa operator <= () 00157 * @sa operator == () 00158 */ 00159 bool operator < (const mapsquare_char & mt) 00160 { 00161 return (mt.y > y || (mt.y == y && mt.x > x)); 00162 } 00163 00164 /** 00165 * Compare the location on the landsubmap of two mapsquare_chars. 00166 * A mapsquare_char is <= to another if it's Y position is < to the other 00167 * one's or if it's Y position == the other one's and it's X position is 00168 * <= to the other one's. 00169 * @sa operator < () 00170 * @sa operator == () 00171 */ 00172 bool operator <= (const mapsquare_char & mt) 00173 { 00174 return (mt.y > y || (mt.y == y && mt.x >= x)); 00175 } 00176 00177 /** 00178 * Compare the location on the landsubmap of two mapsquare_chars. 00179 * A mapsquare_char is == to another if their X and Y position are 00180 * equal. 00181 * @sa operator < () 00182 * @sa operator <= () 00183 */ 00184 bool operator == (const mapsquare_char & mt) 00185 { 00186 return (mt.y == y && mt.x == x); 00187 } 00188 #endif 00189 00190 private: 00191 /// Pointer to the mapcharacter concerned by this mapchar_square. 00192 mapcharacter *mchar; 00193 00194 /// Is it the base tile? 00195 bool is_base; 00196 00197 /// Is this mapsquare_tile walkable? 00198 bool walkable; 00199 00200 #ifndef SWIG 00201 /// Iterator to the base tile of this mapsquare_char. 00202 list <mapsquare_char>::iterator base_tile; 00203 #endif 00204 00205 /// x and y positions. 00206 u_int16 x, y; 00207 00208 #ifndef SWIG 00209 friend class mapcharacter; 00210 friend class mapsquare; 00211 friend class landmap; 00212 friend class mapview; 00213 #endif 00214 }; 00215 00216 00217 /** 00218 * Base unit of a landsubmap, where you can place mapobjects or mapcharacters. 00219 * A landsubmap is a 2 dimensionnal array of mapsquares. When a mapobject is 00220 * placed on a landsubmap, it belongs to one or several mapsquares. A mapsquare 00221 * is made of a list of mapsquare_tiles, containing informations about the objects 00222 * that are on it, and a list of mapsquare_char, which informs about the mapcharacters 00223 * here. This make it possible to have several mapobjects 00224 * and mapcharacters on the same mapsquare. 00225 * 00226 * These two lists are sorted by the position of the object or mapcharacter's base square 00227 * on the map. This make it fast to iterate through the lists during drawing, as we always 00228 * want to iterate the list in this order. 00229 */ 00230 class mapsquare : public mapsquare_walkable 00231 { 00232 public: 00233 /** 00234 * Default constructor. 00235 * 00236 */ 00237 mapsquare (); 00238 00239 #ifndef SWIG 00240 /** 00241 * Copy constructor. 00242 * 00243 */ 00244 mapsquare (const mapsquare& src); 00245 #endif 00246 00247 /** 00248 * Destructor. 00249 * 00250 */ 00251 ~mapsquare (); 00252 00253 /** 00254 * Returns the X position of this mapsquare. 00255 * 00256 * 00257 * @return X position of this mapsquare. 00258 */ 00259 u_int16 x () 00260 { 00261 return x_; 00262 } 00263 00264 /** 00265 * Returns the Y position of this mapsquare. 00266 * 00267 * 00268 * @return Y position of this mapsquare. 00269 */ 00270 u_int16 y () 00271 { 00272 return y_; 00273 } 00274 00275 /** 00276 * Returns whether the mapsquare is free for a character to go on or not. 00277 * It only checks if a mapcharacter is already here. It doesn't deal with the 00278 * walkable problem. 00279 * @return 00280 * - false if the mapsquare isn't free. 00281 * - true if the mapsquare is free. 00282 */ 00283 bool is_free (); 00284 00285 /** 00286 * Return a pointer to the mapcharacter that occupies this mapsquare. 00287 * 00288 * @return pointer to the mapcharacter that occupies the mapsquare, NULL if none. 00289 */ 00290 mapcharacter *whoshere (); 00291 00292 /** 00293 * @name Pathfinding data members. 00294 * 00295 * These members are here to allow faster and more efficient 00296 * pathfinding. Though they can as well be used for something else, 00297 * but their value isn't guaranteed to stay constant. It is safe 00298 * to modify them however, so they are public and uninitialised. 00299 * 00300 */ 00301 //@{ 00302 00303 /** 00304 * Distance from the source square. 00305 * 00306 */ 00307 u_int16 g; 00308 00309 /** 00310 * Estimated distance to the goal square. 00311 * 00312 */ 00313 u_int16 h; 00314 00315 /** 00316 * Sum of g + h. 00317 * 00318 */ 00319 u_int16 f; 00320 00321 /** 00322 * Parent square for the path 00323 * 00324 */ 00325 mapsquare * parent; 00326 00327 /** 00328 * If == false, then this square will never be considered 00329 * as walkable by pathfinding functions. 00330 * 00331 */ 00332 bool can_use_for_pathfinding; 00333 00334 //@} 00335 00336 private: 00337 #ifndef SWIG 00338 /// List of mapsquare_tiles. 00339 list <mapsquare_tile> tiles; 00340 00341 /// Iterator to where the base tiles begin. 00342 /// This serves as an "accelerator" for mapview::draw () which 00343 /// can go directly to the base tiles of a squares with this 00344 /// iterator. 00345 list <mapsquare_tile>::iterator base_begin; 00346 00347 /// List of mapsquare_chars. 00348 list <mapsquare_char> mapchars; 00349 00350 /// Coordinates of the square. 00351 u_int16 x_, y_; 00352 00353 friend class mapcharacter; 00354 friend class mapsquare_area; 00355 friend class landmap; 00356 friend class mapview; 00357 #endif 00358 }; // mapsquare 00359 00360 /** 00361 * Area of mapsquares, for use with landmap. 00362 * 00363 * This class has no reason to exist is not belonging 00364 * to a landmap. You'll NEVER use this class directly - 00365 * anyway you can't do anything usefull with it alone. 00366 * 00367 */ 00368 class mapsquare_area 00369 { 00370 public: 00371 /** 00372 * Default constructor. 00373 * 00374 */ 00375 mapsquare_area (); 00376 00377 /** 00378 * Destructor. 00379 * 00380 */ 00381 ~mapsquare_area (); 00382 00383 /** 00384 * Totally clears the area. 00385 * 00386 */ 00387 void clear (); 00388 00389 /** 00390 * @name Area settings 00391 * 00392 */ 00393 //@{ 00394 00395 /** 00396 * Returns the length of the area. 00397 * 00398 * @return length (in number of squares) of the area. 00399 * 00400 */ 00401 u_int16 area_length () const 00402 { 00403 return area.size (); 00404 } 00405 00406 /** 00407 * Returns the height of the area. 00408 * 00409 * @return height (in number of squares) of the area. 00410 * 00411 */ 00412 u_int16 area_height () const 00413 { 00414 if (area.size ()) return area[0].size (); 00415 else return 0; 00416 } 00417 00418 /** 00419 * Returns a pointer to a desired square. 00420 * 00421 * @param x X position of the square to get. 00422 * @param y Y position of the square to get. 00423 * 00424 * @return pointer to the (x,y) square. 00425 */ 00426 mapsquare * get_square (u_int16 x, u_int16 y) const 00427 { 00428 return &(area[x][y]); 00429 } 00430 00431 /** 00432 * Resize the area. 00433 * 00434 * @param nl new length (in number of squares) of the area. 00435 * @param nh new height (in number of squares) of the area. 00436 */ 00437 void resize_area (u_int16 nl, u_int16 nh); 00438 00439 //@} 00440 00441 private: 00442 /** 00443 * Forbids value passing. 00444 * 00445 */ 00446 mapsquare_area (const mapsquare_area& src); 00447 00448 #ifndef SWIG 00449 /** 00450 * Forbids mapsquare_area copy. 00451 * 00452 */ 00453 mapsquare_area & operator = (const mapsquare_area & mo); 00454 #endif 00455 00456 /** 00457 * Place a mapobject on the submap. 00458 * 00459 * @param px X position of the base square of the object. 00460 * @param py Y position of the base square of the object. 00461 * @param mobj pointer to the mapobject to remove. 00462 */ 00463 s_int8 put_mapobject (u_int16 px, u_int16 py, mapobject * mobj); 00464 00465 /** 00466 * Remove a mapobject from the submap. 00467 * 00468 * @param px X position of the base square of the object. 00469 * @param py Y position of the base square of the object. 00470 * @param mobj pointer to the mapobject to remove. 00471 */ 00472 void remove_mapobject (u_int16 px, u_int16 py, mapobject * mobj); 00473 00474 /** 00475 * 2 dimensionnal array of mapsquares - the actual map 00476 * 00477 */ 00478 #ifndef SWIG 00479 mutable vector <vector<mapsquare> > area; 00480 00481 friend class mapcharacter; 00482 friend class mapview; 00483 friend class landmap; 00484 #endif 00485 }; 00486 00487 #endif