tiles.h

Go to the documentation of this file.
00001 /*
00002  *  tiles.h - Tile coords.
00003  *
00004  *  Copyright (C) 2000-2001  The Exult Team
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #ifndef INCL_TILES
00022 #define INCL_TILES  1
00023 
00024 #include "exult_constants.h"
00025 
00026 /*
00027  *  A 3D tile coordinate:
00028  */
00029 class Tile_coord
00030   {
00031   static short neighbors[16]; // Neighboring tiles in each dir.
00032 public:
00033   short tx, ty, tz;   // Coords. within world. tz=lift.
00034   Tile_coord(int x, int y, int z) : tx(x), ty(y), tz(z)
00035     {  }
00036   Tile_coord(): tx(0),ty(0),tz(0) { }
00037   int operator==(Tile_coord t2)
00038     { return t2.tx == tx && t2.ty == ty && t2.tz == tz; }
00039   int operator!=(Tile_coord t2)
00040     { return !(*this == t2); }
00041   int distance(Tile_coord t2) // Distance to another tile?
00042     {     // Handle wrapping round the world.
00043     int dy = (t2.ty - ty + c_num_tiles)%c_num_tiles;
00044     int dx = (t2.tx - tx + c_num_tiles)%c_num_tiles;
00045     if (dy >= c_num_tiles/2)// World-wrapping.
00046       dy = c_num_tiles - dy;
00047     if (dx >= c_num_tiles/2)
00048       dx = c_num_tiles - dx;
00049           // Take larger abs. value.
00050     return (dy > dx ? dy : dx);
00051     }
00052           // Get neighbor in given dir (0-7).
00053   inline Tile_coord get_neighbor(int dir)
00054     { return Tile_coord(
00055       (tx + neighbors[2*dir] + c_num_tiles)%c_num_tiles,
00056       (ty + neighbors[2*dir + 1] + c_num_tiles)%c_num_tiles,
00057                  tz); }
00058   static bool gte(int t1, int t2) // Ret. t1 >= t2 with wrapping.
00059     {
00060     int diff = t1 - t2;
00061     return diff >= 0 ? (diff < c_num_tiles/2) :
00062             diff < -c_num_tiles/2;
00063     }
00064           // Ret. (to - from) with wrapping.
00065   static int delta(int from, int to)
00066     {
00067     int diff = to - from;
00068     return diff >= c_num_tiles/2 ? (diff - c_num_tiles) :
00069       (diff <= -c_num_tiles/2 ? (diff + c_num_tiles) :
00070                 diff);
00071     }
00072   };
00073           // Add two coords.
00074 inline Tile_coord operator+(Tile_coord a, Tile_coord b)
00075   { return Tile_coord(a.tx + b.tx, a.ty + b.ty, a.tz + b.tz); }
00076 
00077 #endif

Generated on Mon Jul 9 14:42:50 2007 for ExultEngine by  doxygen 1.5.1