# tilearea_type.h

Go to the documentation of this file.

    00001 /* $Id: tilearea_type.h 26125 2013-11-26 14:47:45Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #ifndef TILEAREA_TYPE_H
    00013 #define TILEAREA_TYPE_H
    00014
    00015 #include "map_func.h"
    00016
    00018 struct TileArea {
    00019   TileIndex tile;
    00020   uint16 w;
    00021   uint16 h;
    00022
    00029   TileArea(TileIndex tile = INVALID_TILE, uint8 w = 0, uint8 h = 0) : tile(tile), w(w), h(h) {}
    00030
    00031   TileArea(TileIndex start, TileIndex end);
    00032
    00033
    00034   void Add(TileIndex to_add);
    00035
    00039   void Clear()
    00040   {
    00041     this->tile = INVALID_TILE;
    00042     this->w    = 0;
    00043     this->h    = 0;
    00044   }
    00045
    00046   bool Intersects(const TileArea &ta) const;
    00047
    00048   bool Contains(TileIndex tile) const;
    00049
    00050   void ClampToMap();
    00051
    00056   TileIndex GetCenterTile() const
    00057   {
    00058     return TILE_ADDXY(this->tile, this->w / 2, this->h / 2);
    00059   }
    00060 };
    00061
    00063 class TileIterator {
    00064 protected:
    00065   TileIndex tile;
    00066
    00071   TileIterator(TileIndex tile) : tile(tile)
    00072   {
    00073   }
    00074
    00075 public:
    00077   virtual ~TileIterator()
    00078   {
    00079   }
    00080
    00085   inline operator TileIndex () const
    00086   {
    00087     return this->tile;
    00088   }
    00089
    00093   virtual TileIterator& operator ++() = 0;
    00094
    00098   virtual TileIterator *Clone() const = 0;
    00099 };
    00100
    00102 class OrthogonalTileIterator : public TileIterator {
    00103 private:
    00104   int w;
    00105   int x;
    00106   int y;
    00107
    00108 public:
    00113   OrthogonalTileIterator(const TileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
    00114   {
    00115   }
    00116
    00120   inline TileIterator& operator ++()
    00121   {
    00122     assert(this->tile != INVALID_TILE);
    00123
    00124     if (--this->x > 0) {
    00125       this->tile++;
    00126     } else if (--this->y > 0) {
    00127       this->x = this->w;
    00128       this->tile += TileDiffXY(1, 1) - this->w;
    00129     } else {
    00130       this->tile = INVALID_TILE;
    00131     }
    00132     return *this;
    00133   }
    00134
    00135   virtual TileIterator *Clone() const
    00136   {
    00137     return new OrthogonalTileIterator(*this);
    00138   }
    00139 };
    00140
    00142 class DiagonalTileIterator : public TileIterator {
    00143 private:
    00144   uint base_x;
    00145   uint base_y;
    00146   int a_cur;
    00147   int b_cur;
    00148   int a_max;
    00149   int b_max;
    00150
    00151 public:
    00152   DiagonalTileIterator(TileIndex begin, TileIndex end);
    00153
    00154   TileIterator& operator ++();
    00155
    00156   virtual TileIterator *Clone() const
    00157   {
    00158     return new DiagonalTileIterator(*this);
    00159   }
    00160 };
    00161
    00168 #define TILE_AREA_LOOP(var, ta) for (OrthogonalTileIterator var(ta); var != INVALID_TILE; ++var)
    00169
    00170 #endif /* TILEAREA_TYPE_H */