2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file map_func.h Functions related to maps. */
|
2007-03-03 04:04:22 +00:00
|
|
|
|
2007-12-26 11:45:43 +00:00
|
|
|
#ifndef MAP_FUNC_H
|
|
|
|
#define MAP_FUNC_H
|
2004-12-15 22:18:54 +00:00
|
|
|
|
2010-04-18 14:56:05 +00:00
|
|
|
#include "core/math_func.hpp"
|
2007-12-19 23:26:02 +00:00
|
|
|
#include "tile_type.h"
|
2007-12-26 11:45:43 +00:00
|
|
|
#include "map_type.h"
|
|
|
|
#include "direction_func.h"
|
2005-01-29 13:33:14 +00:00
|
|
|
|
2023-01-21 14:39:20 +00:00
|
|
|
/**
|
|
|
|
* Wrapper class to abstract away the way the tiles are stored. It is
|
|
|
|
* intended to be used to access the "map" data of a single tile.
|
|
|
|
*
|
|
|
|
* The wrapper is expected to be fully optimized away by the compiler, even
|
|
|
|
* with low optimization levels except when completely disabling it.
|
|
|
|
*/
|
|
|
|
class Tile {
|
|
|
|
private:
|
2023-01-21 16:32:28 +00:00
|
|
|
friend struct Map;
|
|
|
|
/**
|
|
|
|
* Data that is stored per tile. Also used TileExtended for this.
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the members.
|
|
|
|
*/
|
|
|
|
struct TileBase {
|
|
|
|
byte type; ///< The type (bits 4..7), bridges (2..3), rainforest/desert (0..1)
|
|
|
|
byte height; ///< The height of the northern corner.
|
|
|
|
uint16 m2; ///< Primarily used for indices to towns, industries and stations
|
|
|
|
byte m1; ///< Primarily used for ownership information
|
|
|
|
byte m3; ///< General purpose
|
|
|
|
byte m4; ///< General purpose
|
|
|
|
byte m5; ///< General purpose
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(TileBase) == 8);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data that is stored per tile. Also used TileBase for this.
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the members.
|
|
|
|
*/
|
|
|
|
struct TileExtended {
|
|
|
|
byte m6; ///< General purpose
|
|
|
|
byte m7; ///< Primarily used for newgrf support
|
|
|
|
uint16 m8; ///< General purpose
|
|
|
|
};
|
|
|
|
|
|
|
|
static TileBase *base_tiles; ///< Pointer to the tile-array.
|
|
|
|
static TileExtended *extended_tiles; ///< Pointer to the extended tile-array.
|
|
|
|
|
2023-01-21 14:39:20 +00:00
|
|
|
TileIndex tile; ///< The tile to access the map data for.
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create the tile wrapper for the given tile.
|
|
|
|
* @param tile The tile to access the map for.
|
|
|
|
*/
|
|
|
|
debug_inline Tile(TileIndex tile) : tile(tile) {}
|
|
|
|
|
2023-01-21 15:40:28 +00:00
|
|
|
/**
|
|
|
|
* Create the tile wrapper for the given tile.
|
|
|
|
* @param tile The tile to access the map for.
|
|
|
|
*/
|
|
|
|
Tile(uint tile) : tile(tile) {}
|
|
|
|
|
2023-01-21 14:39:20 +00:00
|
|
|
/**
|
|
|
|
* Implicit conversion to the TileIndex.
|
|
|
|
*/
|
|
|
|
debug_inline constexpr operator TileIndex() const { return tile; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implicit conversion to the uint for bounds checking.
|
|
|
|
*/
|
|
|
|
debug_inline constexpr operator uint() const { return tile; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type (bits 4..7), bridges (2..3), rainforest/desert (0..1)
|
|
|
|
*
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the data.
|
|
|
|
* @param tile The tile to get the data for.
|
|
|
|
* @return reference to the byte holding the data.
|
|
|
|
*/
|
|
|
|
debug_inline byte &type()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return base_tiles[tile].type;
|
2023-01-21 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The height of the northern corner
|
|
|
|
*
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the data.
|
|
|
|
* @param tile The tile to get the height for.
|
|
|
|
* @return reference to the byte holding the height.
|
|
|
|
*/
|
|
|
|
debug_inline byte &height()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return base_tiles[tile].height;
|
2023-01-21 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Primarily used for ownership information
|
|
|
|
*
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the data.
|
|
|
|
* @param tile The tile to get the data for.
|
|
|
|
* @return reference to the byte holding the data.
|
|
|
|
*/
|
|
|
|
debug_inline byte &m1()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return base_tiles[tile].m1;
|
2023-01-21 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Primarily used for indices to towns, industries and stations
|
|
|
|
*
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the data.
|
|
|
|
* @param tile The tile to get the data for.
|
|
|
|
* @return reference to the uint16 holding the data.
|
|
|
|
*/
|
|
|
|
debug_inline uint16 &m2()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return base_tiles[tile].m2;
|
2023-01-21 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* General purpose
|
|
|
|
*
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the data.
|
|
|
|
* @param tile The tile to get the data for.
|
|
|
|
* @return reference to the byte holding the data.
|
|
|
|
*/
|
|
|
|
debug_inline byte &m3()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return base_tiles[tile].m3;
|
2023-01-21 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* General purpose
|
|
|
|
*
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the data.
|
|
|
|
* @param tile The tile to get the data for.
|
|
|
|
* @return reference to the byte holding the data.
|
|
|
|
*/
|
|
|
|
debug_inline byte &m4()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return base_tiles[tile].m4;
|
2023-01-21 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* General purpose
|
|
|
|
*
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the data.
|
|
|
|
* @param tile The tile to get the data for.
|
|
|
|
* @return reference to the byte holding the data.
|
|
|
|
*/
|
|
|
|
debug_inline byte &m5()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return base_tiles[tile].m5;
|
2023-01-21 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* General purpose
|
|
|
|
*
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the data.
|
|
|
|
* @param tile The tile to get the data for.
|
|
|
|
* @return reference to the byte holding the data.
|
|
|
|
*/
|
|
|
|
debug_inline byte &m6()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return extended_tiles[tile].m6;
|
2023-01-21 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Primarily used for newgrf support
|
|
|
|
*
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the data.
|
|
|
|
* @param tile The tile to get the data for.
|
|
|
|
* @return reference to the byte holding the data.
|
|
|
|
*/
|
|
|
|
debug_inline byte &m7()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return extended_tiles[tile].m7;
|
2023-01-21 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* General purpose
|
|
|
|
*
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the data.
|
|
|
|
* @param tile The tile to get the data for.
|
|
|
|
* @return reference to the uint16 holding the data.
|
|
|
|
*/
|
|
|
|
debug_inline uint16 &m8()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return extended_tiles[tile].m8;
|
2023-01-21 14:39:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
2023-01-21 09:27:00 +00:00
|
|
|
* Size related data of the map.
|
2007-09-09 10:21:27 +00:00
|
|
|
*/
|
2023-01-21 09:27:00 +00:00
|
|
|
struct Map {
|
2023-01-21 11:14:28 +00:00
|
|
|
private:
|
2023-01-21 16:07:06 +00:00
|
|
|
/**
|
|
|
|
* Iterator to iterate all Tiles
|
|
|
|
*/
|
|
|
|
struct Iterator {
|
2023-01-21 15:40:28 +00:00
|
|
|
typedef Tile value_type;
|
|
|
|
typedef Tile *pointer;
|
|
|
|
typedef Tile &reference;
|
2023-01-21 16:07:06 +00:00
|
|
|
typedef size_t difference_type;
|
|
|
|
typedef std::forward_iterator_tag iterator_category;
|
|
|
|
|
|
|
|
explicit Iterator(TileIndex index) : index(index) {}
|
|
|
|
bool operator==(const Iterator &other) const { return this->index == other.index; }
|
|
|
|
bool operator!=(const Iterator &other) const { return !(*this == other); }
|
2023-01-21 15:40:28 +00:00
|
|
|
Tile operator*() const { return this->index; }
|
2023-01-21 16:07:06 +00:00
|
|
|
Iterator & operator++() { this->index++; return *this; }
|
|
|
|
private:
|
|
|
|
TileIndex index;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterable ensemble of all Tiles
|
|
|
|
*/
|
|
|
|
struct IterateWrapper {
|
|
|
|
Iterator begin() { return Iterator(0); }
|
|
|
|
Iterator end() { return Iterator(Map::Size()); }
|
|
|
|
bool empty() { return false; }
|
|
|
|
};
|
|
|
|
|
2023-01-21 11:14:28 +00:00
|
|
|
static uint log_x; ///< 2^_map_log_x == _map_size_x
|
|
|
|
static uint log_y; ///< 2^_map_log_y == _map_size_y
|
|
|
|
static uint size_x; ///< Size of the map along the X
|
|
|
|
static uint size_y; ///< Size of the map along the Y
|
|
|
|
static uint size; ///< The number of tiles on the map
|
|
|
|
static uint tile_mask; ///< _map_size - 1 (to mask the mapsize)
|
|
|
|
|
|
|
|
public:
|
2023-01-21 09:27:00 +00:00
|
|
|
static void Allocate(uint size_x, uint size_y);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logarithm of the map size along the X side.
|
|
|
|
* @note try to avoid using this one
|
|
|
|
* @return 2^"return value" == Map::SizeX()
|
|
|
|
*/
|
2023-01-24 21:50:53 +00:00
|
|
|
debug_inline static uint LogX()
|
2023-01-21 09:27:00 +00:00
|
|
|
{
|
2023-01-21 11:14:28 +00:00
|
|
|
return Map::log_x;
|
2023-01-21 09:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logarithm of the map size along the y side.
|
|
|
|
* @note try to avoid using this one
|
|
|
|
* @return 2^"return value" == Map::SizeY()
|
|
|
|
*/
|
|
|
|
static inline uint LogY()
|
|
|
|
{
|
2023-01-21 11:14:28 +00:00
|
|
|
return Map::log_y;
|
2023-01-21 09:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size of the map along the X
|
|
|
|
* @return the number of tiles along the X of the map
|
|
|
|
*/
|
2023-01-24 21:50:53 +00:00
|
|
|
debug_inline static uint SizeX()
|
2023-01-21 09:27:00 +00:00
|
|
|
{
|
2023-01-21 11:14:28 +00:00
|
|
|
return Map::size_x;
|
2023-01-21 09:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size of the map along the Y
|
|
|
|
* @return the number of tiles along the Y of the map
|
|
|
|
*/
|
|
|
|
static inline uint SizeY()
|
|
|
|
{
|
2023-01-21 11:14:28 +00:00
|
|
|
return Map::size_y;
|
2023-01-21 09:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size of the map
|
|
|
|
* @return the number of tiles of the map
|
|
|
|
*/
|
2023-01-24 21:50:53 +00:00
|
|
|
debug_inline static uint Size()
|
2023-01-21 09:27:00 +00:00
|
|
|
{
|
2023-01-21 11:14:28 +00:00
|
|
|
return Map::size;
|
2023-01-21 09:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the maximum X coordinate within the map, including MP_VOID
|
|
|
|
* @return the maximum X coordinate
|
|
|
|
*/
|
2023-01-24 21:50:53 +00:00
|
|
|
debug_inline static uint MaxX()
|
2023-01-21 09:27:00 +00:00
|
|
|
{
|
|
|
|
return Map::SizeX() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the maximum Y coordinate within the map, including MP_VOID
|
|
|
|
* @return the maximum Y coordinate
|
|
|
|
*/
|
|
|
|
static inline uint MaxY()
|
|
|
|
{
|
|
|
|
return Map::SizeY() - 1;
|
|
|
|
}
|
|
|
|
|
2023-01-21 10:05:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 'Wraps' the given "tile" so it is within the map.
|
|
|
|
* It does this by masking the 'high' bits of.
|
|
|
|
* @param tile the tile to 'wrap'
|
|
|
|
*/
|
|
|
|
static inline TileIndex WrapToMap(uint tile)
|
|
|
|
{
|
2023-01-21 11:14:28 +00:00
|
|
|
return tile & Map::tile_mask;
|
2023-01-21 10:05:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 09:27:00 +00:00
|
|
|
/**
|
|
|
|
* Scales the given value by the map size, where the given value is
|
|
|
|
* for a 256 by 256 map.
|
|
|
|
* @param n the value to scale
|
|
|
|
* @return the scaled size
|
|
|
|
*/
|
|
|
|
static inline uint ScaleBySize(uint n)
|
|
|
|
{
|
|
|
|
/* Subtract 12 from shift in order to prevent integer overflow
|
|
|
|
* for large values of n. It's safe since the min mapsize is 64x64. */
|
|
|
|
return CeilDiv(n << (Map::LogX() + Map::LogY() - 12), 1 << 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scales the given value by the maps circumference, where the given
|
|
|
|
* value is for a 256 by 256 map
|
|
|
|
* @param n the value to scale
|
|
|
|
* @return the scaled size
|
|
|
|
*/
|
|
|
|
static inline uint ScaleBySize1D(uint n)
|
|
|
|
{
|
|
|
|
/* Normal circumference for the X+Y is 256+256 = 1<<9
|
|
|
|
* Note, not actually taking the full circumference into account,
|
|
|
|
* just half of it. */
|
|
|
|
return CeilDiv((n << Map::LogX()) + (n << Map::LogY()), 1 << 9);
|
|
|
|
}
|
|
|
|
|
2023-01-21 11:14:28 +00:00
|
|
|
/**
|
|
|
|
* Check whether the map has been initialized, as to not try to save the map
|
|
|
|
* during crashlog when the map is not there yet.
|
|
|
|
* @return true when the map has been allocated/initialized.
|
|
|
|
*/
|
|
|
|
static bool IsInitialized()
|
|
|
|
{
|
2023-01-21 16:32:28 +00:00
|
|
|
return Tile::base_tiles != nullptr;
|
2023-01-21 11:14:28 +00:00
|
|
|
}
|
2023-01-21 16:07:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterable ensemble of all Tiles
|
|
|
|
* @return an iterable ensemble of all Tiles
|
|
|
|
*/
|
|
|
|
static IterateWrapper Iterate() { return IterateWrapper(); }
|
2023-01-21 11:14:28 +00:00
|
|
|
};
|
2005-01-28 15:31:04 +00:00
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
2022-04-17 17:00:00 +00:00
|
|
|
* An offset value between two tiles.
|
2007-09-09 10:21:27 +00:00
|
|
|
*
|
2013-01-08 22:46:42 +00:00
|
|
|
* This value is used for the difference between
|
2022-04-17 17:00:00 +00:00
|
|
|
* two tiles. It can be added to a TileIndex to get
|
|
|
|
* the resulting TileIndex of the start tile applied
|
2007-09-09 10:21:27 +00:00
|
|
|
* with this saved difference.
|
|
|
|
*
|
|
|
|
* @see TileDiffXY(int, int)
|
|
|
|
*/
|
2005-06-25 16:44:57 +00:00
|
|
|
typedef int32 TileIndexDiff;
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
|
|
|
* Returns the TileIndex of a coordinate.
|
|
|
|
*
|
|
|
|
* @param x The x coordinate of the tile
|
|
|
|
* @param y The y coordinate of the tile
|
|
|
|
* @return The TileIndex calculated by the coordinate
|
|
|
|
*/
|
2023-01-24 21:50:53 +00:00
|
|
|
debug_inline static TileIndex TileXY(uint x, uint y)
|
2005-06-25 16:44:57 +00:00
|
|
|
{
|
2023-01-21 09:43:03 +00:00
|
|
|
return (y << Map::LogX()) + x;
|
2005-06-25 16:44:57 +00:00
|
|
|
}
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
|
|
|
* Calculates an offset for the given coordinate(-offset).
|
|
|
|
*
|
2022-04-17 17:00:00 +00:00
|
|
|
* This function calculate an offset value which can be added to a
|
2007-09-09 10:21:27 +00:00
|
|
|
* #TileIndex. The coordinates can be negative.
|
|
|
|
*
|
|
|
|
* @param x The offset in x direction
|
|
|
|
* @param y The offset in y direction
|
|
|
|
* @return The resulting offset value of the given coordinate
|
|
|
|
* @see ToTileIndexDiff(TileIndexDiffC)
|
|
|
|
*/
|
2005-06-25 16:44:57 +00:00
|
|
|
static inline TileIndexDiff TileDiffXY(int x, int y)
|
|
|
|
{
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Multiplication gives much better optimization on MSVC than shifting.
|
|
|
|
* 0 << shift isn't optimized to 0 properly.
|
|
|
|
* Typically x and y are constants, and then this doesn't result
|
|
|
|
* in any actual multiplication in the assembly code.. */
|
2023-01-21 09:43:03 +00:00
|
|
|
return (y * Map::SizeX()) + x;
|
2005-06-25 16:44:57 +00:00
|
|
|
}
|
2005-02-06 22:36:08 +00:00
|
|
|
|
2011-05-02 17:42:12 +00:00
|
|
|
/**
|
|
|
|
* Get a tile from the virtual XY-coordinate.
|
|
|
|
* @param x The virtual x coordinate of the tile.
|
|
|
|
* @param y The virtual y coordinate of the tile.
|
|
|
|
* @return The TileIndex calculated by the coordinate.
|
|
|
|
*/
|
2023-01-24 21:50:53 +00:00
|
|
|
debug_inline static TileIndex TileVirtXY(uint x, uint y)
|
2005-06-25 06:15:43 +00:00
|
|
|
{
|
2023-01-21 09:43:03 +00:00
|
|
|
return (y >> 4 << Map::LogX()) + (x >> 4);
|
2005-06-25 06:15:43 +00:00
|
|
|
}
|
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* Get the X component of a tile
|
|
|
|
* @param tile the tile to get the X component of
|
|
|
|
* @return the X component
|
|
|
|
*/
|
2023-01-24 21:50:53 +00:00
|
|
|
debug_inline static uint TileX(TileIndex tile)
|
2005-01-07 17:02:43 +00:00
|
|
|
{
|
2023-01-21 09:43:03 +00:00
|
|
|
return tile.value & Map::MaxX();
|
2005-01-07 17:02:43 +00:00
|
|
|
}
|
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* Get the Y component of a tile
|
|
|
|
* @param tile the tile to get the Y component of
|
|
|
|
* @return the Y component
|
|
|
|
*/
|
2023-01-24 21:50:53 +00:00
|
|
|
debug_inline static uint TileY(TileIndex tile)
|
2005-01-07 17:02:43 +00:00
|
|
|
{
|
2023-01-21 09:43:03 +00:00
|
|
|
return tile.value >> Map::LogX();
|
2005-01-07 17:02:43 +00:00
|
|
|
}
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
2022-04-17 17:00:00 +00:00
|
|
|
* Return the offset between two tiles from a TileIndexDiffC struct.
|
2007-09-09 10:21:27 +00:00
|
|
|
*
|
|
|
|
* This function works like #TileDiffXY(int, int) and returns the
|
|
|
|
* difference between two tiles.
|
|
|
|
*
|
|
|
|
* @param tidc The coordinate of the offset as TileIndexDiffC
|
|
|
|
* @return The difference between two tiles.
|
|
|
|
* @see TileDiffXY(int, int)
|
|
|
|
*/
|
2005-01-06 11:39:00 +00:00
|
|
|
static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
|
|
|
|
{
|
2023-01-21 09:43:03 +00:00
|
|
|
return (tidc.y << Map::LogX()) + tidc.x;
|
2005-01-06 11:39:00 +00:00
|
|
|
}
|
2005-01-05 13:32:03 +00:00
|
|
|
|
2005-01-09 17:55:11 +00:00
|
|
|
|
|
|
|
#ifndef _DEBUG
|
2007-12-26 11:45:43 +00:00
|
|
|
/**
|
2022-04-17 17:00:00 +00:00
|
|
|
* Adds two tiles together.
|
2007-12-26 11:45:43 +00:00
|
|
|
*
|
|
|
|
* @param x One tile
|
2010-01-01 18:45:40 +00:00
|
|
|
* @param y Another tile to add
|
2007-12-26 11:45:43 +00:00
|
|
|
* @return The resulting tile(index)
|
|
|
|
*/
|
2020-07-02 21:36:10 +00:00
|
|
|
# define TILE_ADD(x, y) ((x) + (y))
|
2005-01-09 17:55:11 +00:00
|
|
|
#else
|
|
|
|
extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
|
|
|
|
const char *exp, const char *file, int line);
|
2020-07-02 21:36:10 +00:00
|
|
|
# define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
|
2005-01-09 17:55:11 +00:00
|
|
|
#endif
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
|
|
|
* Adds a given offset to a tile.
|
|
|
|
*
|
|
|
|
* @param tile The tile to add an offset on it
|
|
|
|
* @param x The x offset to add to the tile
|
|
|
|
* @param y The y offset to add to the tile
|
|
|
|
*/
|
2005-06-25 16:44:57 +00:00
|
|
|
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
|
2005-01-09 17:55:11 +00:00
|
|
|
|
2008-01-17 16:55:13 +00:00
|
|
|
TileIndex TileAddWrap(TileIndex tile, int addx, int addy);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
|
|
|
* Returns the TileIndexDiffC offset from a DiagDirection.
|
|
|
|
*
|
|
|
|
* @param dir The given direction
|
|
|
|
* @return The offset as TileIndexDiffC value
|
|
|
|
*/
|
2007-07-24 13:03:24 +00:00
|
|
|
static inline TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir)
|
|
|
|
{
|
|
|
|
extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END];
|
2006-09-06 01:56:01 +00:00
|
|
|
|
2007-07-24 13:03:24 +00:00
|
|
|
assert(IsValidDiagDirection(dir));
|
2006-09-06 01:56:01 +00:00
|
|
|
return _tileoffs_by_diagdir[dir];
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2007-12-26 11:45:43 +00:00
|
|
|
|
2008-01-22 17:48:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the TileIndexDiffC offset from a Direction.
|
|
|
|
*
|
|
|
|
* @param dir The given direction
|
|
|
|
* @return The offset as TileIndexDiffC value
|
|
|
|
*/
|
|
|
|
static inline TileIndexDiffC TileIndexDiffCByDir(Direction dir)
|
|
|
|
{
|
|
|
|
extern const TileIndexDiffC _tileoffs_by_dir[DIR_END];
|
|
|
|
|
|
|
|
assert(IsValidDirection(dir));
|
|
|
|
return _tileoffs_by_dir[dir];
|
|
|
|
}
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
|
|
|
* Add a TileIndexDiffC to a TileIndex and returns the new one.
|
|
|
|
*
|
|
|
|
* Returns tile + the diff given in diff. If the result tile would end up
|
2005-01-31 11:23:10 +00:00
|
|
|
* outside of the map, INVALID_TILE is returned instead.
|
2007-09-09 10:21:27 +00:00
|
|
|
*
|
|
|
|
* @param tile The base tile to add the offset on
|
|
|
|
* @param diff The offset to add on the tile
|
|
|
|
* @return The resulting TileIndex
|
2005-01-31 11:23:10 +00:00
|
|
|
*/
|
2007-07-24 13:03:24 +00:00
|
|
|
static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff)
|
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
int x = TileX(tile) + diff.x;
|
|
|
|
int y = TileY(tile) + diff.y;
|
2009-06-10 14:07:08 +00:00
|
|
|
/* Negative value will become big positive value after cast */
|
2023-01-21 09:43:03 +00:00
|
|
|
if ((uint)x >= Map::SizeX() || (uint)y >= Map::SizeY()) return INVALID_TILE;
|
2009-06-10 14:07:08 +00:00
|
|
|
return TileXY(x, y);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2005-01-09 17:55:11 +00:00
|
|
|
|
2007-05-04 16:27:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the diff between two tiles
|
|
|
|
*
|
|
|
|
* @param tile_a from tile
|
|
|
|
* @param tile_b to tile
|
|
|
|
* @return the difference between tila_a and tile_b
|
|
|
|
*/
|
|
|
|
static inline TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
|
|
|
|
{
|
|
|
|
TileIndexDiffC difference;
|
|
|
|
|
|
|
|
difference.x = TileX(tile_a) - TileX(tile_b);
|
|
|
|
difference.y = TileY(tile_a) - TileY(tile_b);
|
|
|
|
|
|
|
|
return difference;
|
|
|
|
}
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Functions to calculate distances */
|
|
|
|
uint DistanceManhattan(TileIndex, TileIndex); ///< also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
|
|
|
|
uint DistanceSquare(TileIndex, TileIndex); ///< euclidian- or L2-Norm squared
|
|
|
|
uint DistanceMax(TileIndex, TileIndex); ///< also known as L-Infinity-Norm
|
|
|
|
uint DistanceMaxPlusManhattan(TileIndex, TileIndex); ///< Max + Manhattan
|
|
|
|
uint DistanceFromEdge(TileIndex); ///< shortest distance from any edge of the map
|
2010-12-11 19:48:31 +00:00
|
|
|
uint DistanceFromEdgeDir(TileIndex, DiagDirection); ///< distance from the map edge in given direction
|
2005-01-31 07:23:15 +00:00
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
|
|
|
* Convert a DiagDirection to a TileIndexDiff
|
|
|
|
*
|
|
|
|
* @param dir The DiagDirection
|
|
|
|
* @return The resulting TileIndexDiff
|
|
|
|
* @see TileIndexDiffCByDiagDir
|
|
|
|
*/
|
2007-07-24 13:03:24 +00:00
|
|
|
static inline TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
|
2006-09-05 23:21:41 +00:00
|
|
|
{
|
2007-07-24 13:03:24 +00:00
|
|
|
extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END];
|
2006-09-05 23:21:41 +00:00
|
|
|
|
2007-07-24 13:03:24 +00:00
|
|
|
assert(IsValidDiagDirection(dir));
|
2006-09-05 23:21:41 +00:00
|
|
|
return ToTileIndexDiff(_tileoffs_by_diagdir[dir]);
|
|
|
|
}
|
2005-07-21 22:15:02 +00:00
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
|
|
|
* Convert a Direction to a TileIndexDiff.
|
|
|
|
*
|
|
|
|
* @param dir The direction to convert from
|
|
|
|
* @return The resulting TileIndexDiff
|
|
|
|
*/
|
2007-07-24 13:03:24 +00:00
|
|
|
static inline TileIndexDiff TileOffsByDir(Direction dir)
|
2005-01-05 13:32:03 +00:00
|
|
|
{
|
2007-07-24 13:03:24 +00:00
|
|
|
extern const TileIndexDiffC _tileoffs_by_dir[DIR_END];
|
2005-01-05 13:32:03 +00:00
|
|
|
|
2007-07-24 13:03:24 +00:00
|
|
|
assert(IsValidDirection(dir));
|
2005-01-06 11:39:00 +00:00
|
|
|
return ToTileIndexDiff(_tileoffs_by_dir[dir]);
|
2005-01-05 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 21:47:36 +00:00
|
|
|
/**
|
|
|
|
* Adds a Direction to a tile.
|
|
|
|
*
|
|
|
|
* @param tile The current tile
|
|
|
|
* @param dir The direction in which we want to step
|
|
|
|
* @return the moved tile
|
|
|
|
*/
|
|
|
|
static inline TileIndex TileAddByDir(TileIndex tile, Direction dir)
|
|
|
|
{
|
|
|
|
return TILE_ADD(tile, TileOffsByDir(dir));
|
|
|
|
}
|
|
|
|
|
2007-09-26 16:12:43 +00:00
|
|
|
/**
|
|
|
|
* Adds a DiagDir to a tile.
|
|
|
|
*
|
|
|
|
* @param tile The current tile
|
|
|
|
* @param dir The direction in which we want to step
|
|
|
|
* @return the moved tile
|
|
|
|
*/
|
|
|
|
static inline TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
|
|
|
|
{
|
|
|
|
return TILE_ADD(tile, TileOffsByDiagDir(dir));
|
|
|
|
}
|
|
|
|
|
2009-03-21 21:43:23 +00:00
|
|
|
/**
|
|
|
|
* Determines the DiagDirection to get from one tile to another.
|
|
|
|
* The tiles do not necessarily have to be adjacent.
|
|
|
|
* @param tile_from Origin tile
|
|
|
|
* @param tile_to Destination tile
|
|
|
|
* @return DiagDirection from tile_from towards tile_to, or INVALID_DIAGDIR if the tiles are not on an axis
|
|
|
|
*/
|
|
|
|
static inline DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
|
|
|
|
{
|
|
|
|
int dx = (int)TileX(tile_to) - (int)TileX(tile_from);
|
|
|
|
int dy = (int)TileY(tile_to) - (int)TileY(tile_from);
|
|
|
|
if (dx == 0) {
|
|
|
|
if (dy == 0) return INVALID_DIAGDIR;
|
|
|
|
return (dy < 0 ? DIAGDIR_NW : DIAGDIR_SE);
|
|
|
|
} else {
|
|
|
|
if (dy != 0) return INVALID_DIAGDIR;
|
|
|
|
return (dx < 0 ? DIAGDIR_NE : DIAGDIR_SW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/**
|
|
|
|
* A callback function type for searching tiles.
|
|
|
|
*
|
|
|
|
* @param tile The tile to test
|
2008-06-25 18:46:05 +00:00
|
|
|
* @param user_data additional data for the callback function to use
|
2007-09-09 10:21:27 +00:00
|
|
|
* @return A boolean value, depend on the definition of the function.
|
|
|
|
*/
|
2008-06-25 18:46:05 +00:00
|
|
|
typedef bool TestTileOnSearchProc(TileIndex tile, void *user_data);
|
2007-09-09 10:21:27 +00:00
|
|
|
|
2008-06-25 18:46:05 +00:00
|
|
|
bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data);
|
2009-01-05 17:48:54 +00:00
|
|
|
bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data);
|
|
|
|
|
2007-12-26 11:45:43 +00:00
|
|
|
/**
|
|
|
|
* Get a random tile out of a given seed.
|
|
|
|
* @param r the random 'seed'
|
|
|
|
* @return a valid tile
|
|
|
|
*/
|
|
|
|
static inline TileIndex RandomTileSeed(uint32 r)
|
|
|
|
{
|
2023-01-21 10:05:19 +00:00
|
|
|
return Map::WrapToMap(r);
|
2007-12-26 11:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a valid random tile.
|
|
|
|
* @note a define so 'random' gets inserted in the place where it is actually
|
|
|
|
* called, thus making the random traces more explicit.
|
|
|
|
* @return a valid tile
|
|
|
|
*/
|
|
|
|
#define RandomTile() RandomTileSeed(Random())
|
|
|
|
|
2009-04-24 22:27:21 +00:00
|
|
|
uint GetClosestWaterDistance(TileIndex tile, bool water);
|
|
|
|
|
2007-12-26 11:45:43 +00:00
|
|
|
#endif /* MAP_FUNC_H */
|