Changeset - r11695:8dbb94e1bfa0
[Not reviewed]
master
0 3 0
rubidium - 15 years ago 2009-04-18 21:10:36
rubidium@openttd.org
(svn r16086) -Codechange: magic numbers substitutes by enums and some type safety
3 files changed with 56 insertions and 58 deletions:
0 comments (0 inline, 0 general)
src/tile_type.h
Show inline comments
 
@@ -5,15 +5,16 @@
 
#ifndef TILE_TYPE_H
 
#define TILE_TYPE_H
 

	
 
#include "core/enum_type.hpp"
 

	
 
enum {
 
	TILE_SIZE   = 16,   ///< Tiles are 16x16 "units" in size
 
	TILE_PIXELS = 32,   ///< a tile is 32x32 pixels
 
	TILE_HEIGHT =  8,   ///< The standard height-difference between tiles on two levels is 8 (z-diff 8)
 
	TILE_SIZE      = 16,            ///< Tiles are 16x16 "units" in size
 
	TILE_UNIT_MASK = TILE_SIZE - 1, ///< for masking in/out the inner-tile units.
 
	TILE_PIXELS    = 32,            ///< a tile is 32x32 pixels
 
	TILE_HEIGHT    =  8,            ///< The standard height-difference between tiles on two levels is 8 (z-diff 8)
 

	
 
	MAX_TILE_HEIGHT     = 15,                    ///< Maximum allowed tile height
 
	MAX_SNOWLINE_HEIGHT = (MAX_TILE_HEIGHT - 2), ///< Maximum allowed snowline height
 
};
 

	
 

	
src/tilehighlight_type.h
Show inline comments
 
@@ -36,12 +36,13 @@ enum HighLightStyle {
 
	HT_DIR_X  = 0,    ///< X direction
 
	HT_DIR_Y  = 1,    ///< Y direction
 
	HT_DIR_HU = 2,    ///< horizontal upper
 
	HT_DIR_HL = 3,    ///< horizontal lower
 
	HT_DIR_VL = 4,    ///< vertical left
 
	HT_DIR_VR = 5,    ///< vertical right
 
	HT_DIR_END,       ///< end marker
 
	HT_DIR_MASK = 0x7 ///< masks the drag-direction
 
};
 
DECLARE_ENUM_AS_BIT_SET(HighLightStyle);
 

	
 

	
 
struct TileHighlightData {
 
@@ -56,15 +57,15 @@ struct TileHighlightData {
 

	
 
	Point selend, selstart;
 

	
 
	byte dirty;
 
	byte sizelimit;
 

	
 
	byte drawstyle;      // lower bits 0-3 are reserved for detailed highlight information information
 
	byte new_drawstyle;  // only used in UpdateTileSelection() to as a buffer to compare if there was a change between old and new
 
	byte next_drawstyle; // queued, but not yet drawn style
 
	HighLightStyle drawstyle;      // lower bits 0-3 are reserved for detailed highlight information information
 
	HighLightStyle new_drawstyle;  // only used in UpdateTileSelection() to as a buffer to compare if there was a change between old and new
 
	HighLightStyle next_drawstyle; // queued, but not yet drawn style
 

	
 
	ViewportHighlightMode place_mode;
 
	bool make_square_red;
 
	WindowClass window_class;
 
	WindowNumber window_number;
 

	
src/viewport.cpp
Show inline comments
 
@@ -899,21 +899,21 @@ static void DrawTileSelection(const Tile
 
					if (IsSteepSlope(ti->tileh)) z -= TILE_HEIGHT;
 
				}
 
			}
 
			DrawSelectionSprite(_cur_dpi->zoom <= ZOOM_LVL_DETAIL ? SPR_DOT : SPR_DOT_SMALL, PAL_NONE, ti, z, foundation_part);
 
		} else if (_thd.drawstyle & HT_RAIL /* && _thd.place_mode == VHM_RAIL*/) {
 
			/* autorail highlight piece under cursor */
 
			uint type = _thd.drawstyle & 0xF;
 
			assert(type <= 5);
 
			HighLightStyle type = _thd.drawstyle & HT_DIR_MASK;
 
			assert(type < HT_DIR_END);
 
			DrawAutorailSelection(ti, _autorail_type[type][0]);
 
		} else if (IsPartOfAutoLine(ti->x, ti->y)) {
 
			/* autorail highlighting long line */
 
			int dir = _thd.drawstyle & ~0xF0;
 
			HighLightStyle dir = _thd.drawstyle & HT_DIR_MASK;
 
			uint side;
 

	
 
			if (dir < 2) {
 
			if (dir == HT_DIR_X || dir == HT_DIR_Y) {
 
				side = 0;
 
			} else {
 
				TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
 
				side = Delta(Delta(TileX(start), TileX(ti->tile)), Delta(TileY(start), TileY(ti->tile)));
 
			}
 

	
 
@@ -939,14 +939,14 @@ static void ViewportAddLandscape()
 
	TileInfo ti;
 
	bool direction;
 

	
 
	_cur_ti = &ti;
 

	
 
	/* Transform into tile coordinates and round to closest full tile */
 
	x = ((_vd.dpi.top >> 1) - (_vd.dpi.left >> 2)) & ~0xF;
 
	y = ((_vd.dpi.top >> 1) + (_vd.dpi.left >> 2) - 0x10) & ~0xF;
 
	x = ((_vd.dpi.top >> 1) - (_vd.dpi.left >> 2)) & ~TILE_UNIT_MASK;
 
	y = ((_vd.dpi.top >> 1) + (_vd.dpi.left >> 2) - TILE_SIZE) & ~TILE_UNIT_MASK;
 

	
 
	/* determine size of area */
 
	{
 
		Point pt = RemapCoords(x, y, 241);
 
		width = (_vd.dpi.left + _vd.dpi.width - pt.x + 95) >> 6;
 
		height = (_vd.dpi.top + _vd.dpi.height - pt.y) >> 5 << 1;
 
@@ -2062,14 +2062,14 @@ void PlaceObject()
 

	
 
	if (_thd.place_mode == VHM_POINT) {
 
		pt.x += 8;
 
		pt.y += 8;
 
	}
 

	
 
	_tile_fract_coords.x = pt.x & 0xF;
 
	_tile_fract_coords.y = pt.y & 0xF;
 
	_tile_fract_coords.x = pt.x & TILE_UNIT_MASK;
 
	_tile_fract_coords.y = pt.y & TILE_UNIT_MASK;
 

	
 
	w = GetCallbackWnd();
 
	if (w != NULL) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y));
 
}
 

	
 

	
 
@@ -2129,13 +2129,13 @@ void SetTileSelectBigSize(int ox, int oy
 
	_thd.new_outersize.y = sy * TILE_SIZE;
 
}
 

	
 
/** returns the best autorail highlight type from map coordinates */
 
static HighLightStyle GetAutorailHT(int x, int y)
 
{
 
	return HT_RAIL | _autorail_piece[x & 0xF][y & 0xF];
 
	return HT_RAIL | _autorail_piece[x & TILE_UNIT_MASK][y & TILE_UNIT_MASK];
 
}
 

	
 
/**
 
 * Updates tile highlighting for all cases.
 
 * Uses _thd.selstart and _thd.selend and _thd.place_mode (set elsewhere) to determine _thd.pos and _thd.size
 
 * Also drawstyle is determined. Uses _thd.new.* as a buffer and calls SetSelectionTilesDirty() twice,
 
@@ -2144,22 +2144,22 @@ static HighLightStyle GetAutorailHT(int 
 
 */
 
void UpdateTileSelection()
 
{
 
	int x1;
 
	int y1;
 

	
 
	_thd.new_drawstyle = 0;
 
	_thd.new_drawstyle = HT_NONE;
 

	
 
	if (_thd.place_mode == VHM_SPECIAL) {
 
		x1 = _thd.selend.x;
 
		y1 = _thd.selend.y;
 
		if (x1 != -1) {
 
			int x2 = _thd.selstart.x & ~0xF;
 
			int y2 = _thd.selstart.y & ~0xF;
 
			x1 &= ~0xF;
 
			y1 &= ~0xF;
 
			int x2 = _thd.selstart.x & ~TILE_UNIT_MASK;
 
			int y2 = _thd.selstart.y & ~TILE_UNIT_MASK;
 
			x1 &= ~TILE_UNIT_MASK;
 
			y1 &= ~TILE_UNIT_MASK;
 

	
 
			if (x1 >= x2) Swap(x1, x2);
 
			if (y1 >= y2) Swap(y1, y2);
 
			_thd.new_pos.x = x1;
 
			_thd.new_pos.y = y1;
 
			_thd.new_size.x = x2 - x1 + TILE_SIZE;
 
@@ -2174,24 +2174,24 @@ void UpdateTileSelection()
 
			switch (_thd.place_mode) {
 
				case VHM_RECT:
 
					_thd.new_drawstyle = HT_RECT;
 
					break;
 
				case VHM_POINT:
 
					_thd.new_drawstyle = HT_POINT;
 
					x1 += 8;
 
					y1 += 8;
 
					x1 += TILE_SIZE / 2;
 
					y1 += TILE_SIZE / 2;
 
					break;
 
				case VHM_RAIL:
 
					_thd.new_drawstyle = GetAutorailHT(pt.x, pt.y); // draw one highlighted tile
 
					break;
 
				default:
 
					NOT_REACHED();
 
					break;
 
			}
 
			_thd.new_pos.x = x1 & ~0xF;
 
			_thd.new_pos.y = y1 & ~0xF;
 
			_thd.new_pos.x = x1 & ~TILE_UNIT_MASK;
 
			_thd.new_pos.y = y1 & ~TILE_UNIT_MASK;
 
		}
 
	}
 

	
 
	/* redraw selection */
 
	if (_thd.drawstyle != _thd.new_drawstyle ||
 
			_thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y ||
 
@@ -2287,36 +2287,36 @@ static void VpStartPreSizing()
 

	
 
/** returns information about the 2x1 piece to be build.
 
 * The lower bits (0-3) are the track type. */
 
static HighLightStyle Check2x1AutoRail(int mode)
 
{
 
	int fxpy = _tile_fract_coords.x + _tile_fract_coords.y;
 
	int sxpy = (_thd.selend.x & 0xF) + (_thd.selend.y & 0xF);
 
	int sxpy = (_thd.selend.x & TILE_UNIT_MASK) + (_thd.selend.y & TILE_UNIT_MASK);
 
	int fxmy = _tile_fract_coords.x - _tile_fract_coords.y;
 
	int sxmy = (_thd.selend.x & 0xF) - (_thd.selend.y & 0xF);
 
	int sxmy = (_thd.selend.x & TILE_UNIT_MASK) - (_thd.selend.y & TILE_UNIT_MASK);
 

	
 
	switch (mode) {
 
		default: NOT_REACHED();
 
		case 0: // end piece is lower right
 
			if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_HL; }
 
			if (fxmy < -3 && sxmy > 3) {/* DoRailroadTrack(0); */return HT_DIR_VR; }
 
			if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
 
			if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
 
			return HT_DIR_Y;
 

	
 
		case 1:
 
			if (fxmy > 3 && sxmy < -3) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_VL; }
 
			if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return HT_DIR_HU; }
 
			if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
 
			if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
 
			return HT_DIR_Y;
 

	
 
		case 2:
 
			if (fxmy > 3 && sxmy < -3) { /*DoRailroadTrack(3);*/ return HT_DIR_VL; }
 
			if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_HL; }
 
			if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
 
			if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
 
			return HT_DIR_X;
 

	
 
		case 3:
 
			if (fxmy < -3 && sxmy > 3) { /*SwapSelection(); DoRailroadTrack(3);*/ return HT_DIR_VR; }
 
			if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return HT_DIR_HU; }
 
			if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
 
			if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
 
			return HT_DIR_X;
 
	}
 
}
 

	
 
/** Check if the direction of start and end tile should be swapped based on
 
 * the dragging-style. Default directions are:
 
@@ -2437,38 +2437,37 @@ static int CalcHeightdiff(HighLightStyle
 
static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF};
 

	
 
/** while dragging */
 
static void CalcRaildirsDrawstyle(TileHighlightData *thd, int x, int y, int method)
 
{
 
	HighLightStyle b;
 
	uint w, h;
 

	
 
	int dx = thd->selstart.x - (thd->selend.x & ~0xF);
 
	int dy = thd->selstart.y - (thd->selend.y & ~0xF);
 
	w = abs(dx) + 16;
 
	h = abs(dy) + 16;
 

	
 
	int dx = thd->selstart.x - (thd->selend.x & ~TILE_UNIT_MASK);
 
	int dy = thd->selstart.y - (thd->selend.y & ~TILE_UNIT_MASK);
 
	uint w = abs(dx) + TILE_SIZE;
 
	uint h = abs(dy) + TILE_SIZE;
 

	
 
	if (TileVirtXY(thd->selstart.x, thd->selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile
 
		if (method == VPM_RAILDIRS) {
 
			b = GetAutorailHT(x, y);
 
		} else { // rect for autosignals on one tile
 
			b = HT_RECT;
 
		}
 
	} else if (h == 16) { // Is this in X direction?
 
		if (dx == 16) { // 2x1 special handling
 
	} else if (h == TILE_SIZE) { // Is this in X direction?
 
		if (dx == TILE_SIZE) { // 2x1 special handling
 
			b = (Check2x1AutoRail(3)) | HT_LINE;
 
		} else if (dx == -16) {
 
		} else if (dx == -TILE_SIZE) {
 
			b = (Check2x1AutoRail(2)) | HT_LINE;
 
		} else {
 
			b = HT_LINE | HT_DIR_X;
 
		}
 
		y = thd->selstart.y;
 
	} else if (w == 16) { // Or Y direction?
 
		if (dy == 16) { // 2x1 special handling
 
	} else if (w == TILE_SIZE) { // Or Y direction?
 
		if (dy == TILE_SIZE) { // 2x1 special handling
 
			b = (Check2x1AutoRail(1)) | HT_LINE;
 
		} else if (dy == -16) { // 2x1 other direction
 
		} else if (dy == -TILE_SIZE) { // 2x1 other direction
 
			b = (Check2x1AutoRail(0)) | HT_LINE;
 
		} else {
 
			b = HT_LINE | HT_DIR_Y;
 
		}
 
		x = thd->selstart.x;
 
	} else if (w > h * 2) { // still count as x dir?
 
@@ -2476,66 +2475,63 @@ static void CalcRaildirsDrawstyle(TileHi
 
		y = thd->selstart.y;
 
	} else if (h > w * 2) { // still count as y dir?
 
		b = HT_LINE | HT_DIR_Y;
 
		x = thd->selstart.x;
 
	} else { // complicated direction
 
		int d = w - h;
 
		thd->selend.x = thd->selend.x & ~0xF;
 
		thd->selend.y = thd->selend.y & ~0xF;
 
		thd->selend.x = thd->selend.x & ~TILE_UNIT_MASK;
 
		thd->selend.y = thd->selend.y & ~TILE_UNIT_MASK;
 

	
 
		/* four cases. */
 
		if (x > thd->selstart.x) {
 
			if (y > thd->selstart.y) {
 
				/* south */
 
				if (d == 0) {
 
					b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
 
					b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
 
				} else if (d >= 0) {
 
					x = thd->selstart.x + h;
 
					b = HT_LINE | HT_DIR_VL;
 
					// return px == py || px == py + 16;
 
				} else {
 
					y = thd->selstart.y + w;
 
					b = HT_LINE | HT_DIR_VR;
 
				} // return px == py || px == py - 16;
 
				}
 
			} else {
 
				/* west */
 
				if (d == 0) {
 
					b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
 
					b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
 
				} else if (d >= 0) {
 
					x = thd->selstart.x + h;
 
					b = HT_LINE | HT_DIR_HL;
 
				} else {
 
					y = thd->selstart.y - w;
 
					b = HT_LINE | HT_DIR_HU;
 
				}
 
			}
 
		} else {
 
			if (y > thd->selstart.y) {
 
				/* east */
 
				if (d == 0) {
 
					b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
 
					b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
 
				} else if (d >= 0) {
 
					x = thd->selstart.x - h;
 
					b = HT_LINE | HT_DIR_HU;
 
					// return px == -py || px == -py - 16;
 
				} else {
 
					y = thd->selstart.y + w;
 
					b = HT_LINE | HT_DIR_HL;
 
				} // return px == -py || px == -py + 16;
 
				}
 
			} else {
 
				/* north */
 
				if (d == 0) {
 
					b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
 
					b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
 
				} else if (d >= 0) {
 
					x = thd->selstart.x - h;
 
					b = HT_LINE | HT_DIR_VR;
 
					// return px == py || px == py - 16;
 
				} else {
 
					y = thd->selstart.y - w;
 
					b = HT_LINE | HT_DIR_VL;
 
				} // return px == py || px == py + 16;
 
				}
 
			}
 
		}
 
	}
 

	
 
	if (_settings_client.gui.measure_tooltip) {
 
		TileIndex t0 = TileVirtXY(thd->selstart.x, thd->selstart.y);
0 comments (0 inline, 0 general)