# tilearea.cpp

Go to the documentation of this file.

    00001 /* $Id: tilearea.cpp 19041 2010-02-06 13:19:46Z alberth $ */
    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 #include "stdafx.h"
    00013
    00014 #include "tile_map.h"
    00015 #include "core/math_func.hpp"
    00016 #include "tilearea_type.h"
    00017
    00023 TileArea::TileArea(TileIndex start, TileIndex end)
    00024 {
    00025   uint sx = TileX(start);
    00026   uint sy = TileY(start);
    00027   uint ex = TileX(end);
    00028   uint ey = TileY(end);
    00029
    00030   if (sx > ex) Swap(sx, ex);
    00031   if (sy > ey) Swap(sy, ey);
    00032
    00033   this->tile = TileXY(sx, sy);
    00034   this->w    = ex - sx + 1;
    00035   this->h    = ey - sy + 1;
    00036 }
    00037
    00042 void TileArea::Add(TileIndex to_add)
    00043 {
    00044   if (this->tile == INVALID_TILE) {
    00045     this->tile = to_add;
    00046     this->w = 1;
    00047     this->h = 1;
    00048     return;
    00049   }
    00050
    00051   uint sx = TileX(this->tile);
    00052   uint sy = TileY(this->tile);
    00053   uint ex = sx + this->w - 1;
    00054   uint ey = sy + this->h - 1;
    00055
    00056   uint ax = TileX(to_add);
    00057   uint ay = TileY(to_add);
    00058
    00059   sx = min(ax, sx);
    00060   sy = min(ay, sy);
    00061   ex = max(ax, ex);
    00062   ey = max(ay, ey);
    00063
    00064   this->tile = TileXY(sx, sy);
    00065   this->w    = ex - sx + 1;
    00066   this->h    = ey - sy + 1;
    00067 }
    00068
    00074 bool TileArea::Intersects(const TileArea &ta) const
    00075 {
    00076   if (ta.w == 0 || this->w == 0) return false;
    00077
    00078   assert(ta.w != 0 && ta.h != 0 && this->w != 0 && this->h != 0);
    00079
    00080   uint left1   = TileX(this->tile);
    00081   uint top1    = TileY(this->tile);
    00082   uint right1  = left1 + this->w - 1;
    00083   uint bottom1 = top1  + this->h - 1;
    00084
    00085   uint left2   = TileX(ta.tile);
    00086   uint top2    = TileY(ta.tile);
    00087   uint right2  = left2 + ta.w - 1;
    00088   uint bottom2 = top2  + ta.h - 1;
    00089
    00090   return !(
    00091       left2   > right1  ||
    00092       right2  < left1   ||
    00093       top2    > bottom1 ||
    00094       bottom2 < top1
    00095     );
    00096 }
    00097
    00101 void TileArea::ClampToMap()
    00102 {
    00103   assert(this->tile < MapSize());
    00104   this->w = min(this->w, MapSizeX() - TileX(this->tile));
    00105   this->h = min(this->h, MapSizeY() - TileY(this->tile));
    00106 }
    00107