File diff r15609:02b794721f9c → r15610:623a23fb6560
src/viewport.cpp
Show inline comments
 
/* $Id$ */
 

	
 
/*
 
 * 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/>.
 
 */
 

	
 
/** @file viewport.cpp Handling of all viewports.
 
/**
 
 * @file viewport.cpp Handling of all viewports.
 
 *
 
 * \verbatim
 
 * The in-game coordinate system looks like this *
 
 *                                               *
 
 *                    ^ Z                        *
 
 *                    |                          *
 
 *                    |                          *
 
 *                    |                          *
 
 *                    |                          *
 
 *                 /     \                       *
 
 *              /           \                    *
 
 *           /                 \                 *
 
 *        /                       \              *
 
 *   X <                             > Y         *
 
 * \endverbatim
 
 */
 

	
 
#include "stdafx.h"
 
#include "landscape.h"
 
#include "viewport_func.h"
 
#include "station_base.h"
 
#include "waypoint_base.h"
 
#include "town.h"
 
#include "signs_base.h"
 
#include "signs_func.h"
 
#include "vehicle_base.h"
 
#include "vehicle_gui.h"
 
#include "blitter/factory.hpp"
 
#include "strings_func.h"
 
#include "zoom_func.h"
 
#include "vehicle_func.h"
 
#include "company_func.h"
 
#include "waypoint_func.h"
 
#include "window_func.h"
 
#include "tilehighlight_func.h"
 
#include "window_gui.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
 

	
 
PlaceProc *_place_proc;
 
Point _tile_fract_coords;
 

	
 
struct StringSpriteToDraw {
 
	StringID string;
 
	Colours colour;
 
	int32 x;
 
	int32 y;
 
	uint64 params[2];
 
	uint16 width;
 
};
 

	
 
struct TileSpriteToDraw {
 
	SpriteID image;
 
	PaletteID pal;
 
	const SubSprite *sub;           ///< only draw a rectangular part of the sprite
 
	int32 x;                        ///< screen X coordinate of sprite
 
	int32 y;                        ///< screen Y coordinate of sprite
 
};
 

	
 
struct ChildScreenSpriteToDraw {
 
	SpriteID image;
 
	PaletteID pal;
 
	const SubSprite *sub;           ///< only draw a rectangular part of the sprite
 
	int32 x;
 
	int32 y;
 
	int next;                       ///< next child to draw (-1 at the end)
 
};
 

	
 
/** Parent sprite that should be drawn */
 
struct ParentSpriteToDraw {
 
	SpriteID image;                 ///< sprite to draw
 
	PaletteID pal;                  ///< palette to use
 
	const SubSprite *sub;           ///< only draw a rectangular part of the sprite
 

	
 
	int32 x;                        ///< screen X coordinate of sprite
 
	int32 y;                        ///< screen Y coordinate of sprite
 

	
 
	int32 left;                     ///< minimal screen X coordinate of sprite (= x + sprite->x_offs), reference point for child sprites
 
	int32 top;                      ///< minimal screen Y coordinate of sprite (= y + sprite->y_offs), reference point for child sprites
 

	
 
	int32 xmin;                     ///< minimal world X coordinate of bounding box
 
	int32 xmax;                     ///< maximal world X coordinate of bounding box
 
	int32 ymin;                     ///< minimal world Y coordinate of bounding box
 
	int32 ymax;                     ///< maximal world Y coordinate of bounding box
 
	int zmin;                       ///< minimal world Z coordinate of bounding box
 
	int zmax;                       ///< maximal world Z coordinate of bounding box
 

	
 
	int first_child;                ///< the first child to draw.
 
	bool comparison_done;           ///< Used during sprite sorting: true if sprite has been compared with all other sprites
 
};
 

	
 
/** Enumeration of multi-part foundations */
 
enum FoundationPart {
 
	FOUNDATION_PART_NONE     = 0xFF,  ///< Neither foundation nor groundsprite drawn yet.
 
	FOUNDATION_PART_NORMAL   = 0,     ///< First part (normal foundation or no foundation)
 
	FOUNDATION_PART_HALFTILE = 1,     ///< Second part (halftile foundation)
 
	FOUNDATION_PART_END
 
};
 

	
 
/** Mode of "sprite combining"
 
/**
 
 * Mode of "sprite combining"
 
 * @see StartSpriteCombine
 
 */
 
enum SpriteCombineMode {
 
	SPRITE_COMBINE_NONE,     ///< Every #AddSortableSpriteToDraw start its own bounding box
 
	SPRITE_COMBINE_PENDING,  ///< %Sprite combining will start with the next unclipped sprite.
 
	SPRITE_COMBINE_ACTIVE,   ///< %Sprite combining is active. #AddSortableSpriteToDraw outputs child sprites.
 
};
 

	
 
typedef SmallVector<TileSpriteToDraw, 64> TileSpriteToDrawVector;
 
typedef SmallVector<StringSpriteToDraw, 4> StringSpriteToDrawVector;
 
typedef SmallVector<ParentSpriteToDraw, 64> ParentSpriteToDrawVector;
 
typedef SmallVector<ParentSpriteToDraw*, 64> ParentSpriteToSortVector;
 
typedef SmallVector<ChildScreenSpriteToDraw, 16> ChildScreenSpriteToDrawVector;
 

	
 
/** Data structure storing rendering information */
 
struct ViewportDrawer {
 
	DrawPixelInfo dpi;
 

	
 
	StringSpriteToDrawVector string_sprites_to_draw;
 
	TileSpriteToDrawVector tile_sprites_to_draw;
 
	ParentSpriteToDrawVector parent_sprites_to_draw;
 
	ParentSpriteToSortVector parent_sprites_to_sort; ///< Parent sprite pointer array used for sorting
 
	ChildScreenSpriteToDrawVector child_screen_sprites_to_draw;
 

	
 
	int *last_child;
 

	
 
	SpriteCombineMode combine_sprites;               ///< Current mode of "sprite combining". @see StartSpriteCombine
 

	
 
	int foundation[FOUNDATION_PART_END];             ///< Foundation sprites (index into parent_sprites_to_draw).
 
	FoundationPart foundation_part;                  ///< Currently active foundation for ground sprite drawing.
 
	int *last_foundation_child[FOUNDATION_PART_END]; ///< Tail of ChildSprite list of the foundations. (index into child_screen_sprites_to_draw)
 
	Point foundation_offset[FOUNDATION_PART_END];    ///< Pixel offset for ground sprites on the foundations.
 
};
 

	
 
static ViewportDrawer _vd;
 

	
 
TileHighlightData _thd;
 
static TileInfo *_cur_ti;
 
bool _draw_bounding_boxes = false;
 

	
 
static Point MapXYZToViewport(const ViewPort *vp, int x, int y, int z)
 
{
 
	Point p = RemapCoords(x, y, z);
 
	p.x -= vp->virtual_width / 2;
 
	p.y -= vp->virtual_height / 2;
 
	return p;
 
}
 

	
 
void DeleteWindowViewport(Window *w)
 
{
 
	free(w->viewport);
 
	w->viewport = NULL;
 
}
 

	
 
/**
 
 * Initialize viewport of the window for use.
 
 * @param w Window to use/display the viewport in
 
 * @param x Offset of left edge of viewport with respect to left edge window \a w
 
 * @param y Offset of top edge of viewport with respect to top edge window \a w
 
 * @param width Width of the viewport
 
 * @param height Height of the viewport
 
 * @param follow_flags Flags controlling the viewport.
 
 *        - If bit 31 is set, the lower 16 bits are the vehicle that the viewport should follow.
 
 *        - If bit 31 is clear, it is a tile position.
 
 * @param zoom Zoomlevel to display
 
 */
 
void InitializeWindowViewport(Window *w, int x, int y,
 
	int width, int height, uint32 follow_flags, ZoomLevel zoom)
 
{
 
	assert(w->viewport == NULL);
 

	
 
	ViewportData *vp = CallocT<ViewportData>(1);
 

	
 
	vp->left = x + w->left;
 
	vp->top = y + w->top;
 
	vp->width = width;
 
	vp->height = height;
 

	
 
	vp->zoom = zoom;
 

	
 
	vp->virtual_width = ScaleByZoom(width, zoom);
 
	vp->virtual_height = ScaleByZoom(height, zoom);
 

	
 
	Point pt;
 

	
 
	if (follow_flags & 0x80000000) {
 
		const Vehicle *veh;
 

	
 
		vp->follow_vehicle = (VehicleID)(follow_flags & 0xFFFF);
 
		veh = Vehicle::Get(vp->follow_vehicle);
 
		pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
 
	} else {
 
		uint x = TileX(follow_flags) * TILE_SIZE;
 
		uint y = TileY(follow_flags) * TILE_SIZE;
 

	
 
		vp->follow_vehicle = INVALID_VEHICLE;
 
		pt = MapXYZToViewport(vp, x, y, GetSlopeZ(x, y));
 
	}
 

	
 
	vp->scrollpos_x = pt.x;
 
	vp->scrollpos_y = pt.y;
 
	vp->dest_scrollpos_x = pt.x;
 
	vp->dest_scrollpos_y = pt.y;
 

	
 
	w->viewport = vp;
 
	vp->virtual_left = 0;//pt.x;
 
	vp->virtual_top = 0;//pt.y;
 
}
 

	
 
static Point _vp_move_offs;
 

	
 
static void DoSetViewportPosition(const Window *w, int left, int top, int width, int height)
 
{
 
	FOR_ALL_WINDOWS_FROM_BACK_FROM(w, w) {
 
		if (left + width > w->left &&
 
				w->left + w->width > left &&
 
				top + height > w->top &&
 
				w->top + w->height > top) {
 

	
 
			if (left < w->left) {
 
				DoSetViewportPosition(w, left, top, w->left - left, height);
 
				DoSetViewportPosition(w, left + (w->left - left), top, width - (w->left - left), height);
 
				return;
 
			}
 

	
 
			if (left + width > w->left + w->width) {
 
				DoSetViewportPosition(w, left, top, (w->left + w->width - left), height);
 
				DoSetViewportPosition(w, left + (w->left + w->width - left), top, width - (w->left + w->width - left), height);
 
				return;
 
			}
 

	
 
			if (top < w->top) {
 
				DoSetViewportPosition(w, left, top, width, (w->top - top));
 
				DoSetViewportPosition(w, left, top + (w->top - top), width, height - (w->top - top));
 
				return;
 
			}
 

	
 
			if (top + height > w->top + w->height) {
 
				DoSetViewportPosition(w, left, top, width, (w->top + w->height - top));
 
				DoSetViewportPosition(w, left, top + (w->top + w->height - top), width, height - (w->top + w->height - top));
 
				return;
 
			}
 

	
 
			return;
 
		}
 
	}
 

	
 
	{
 
		int xo = _vp_move_offs.x;
 
		int yo = _vp_move_offs.y;
 

	
 
		if (abs(xo) >= width || abs(yo) >= height) {
 
			/* fully_outside */
 
			RedrawScreenRect(left, top, left + width, top + height);
 
			return;
 
		}
 

	
 
		GfxScroll(left, top, width, height, xo, yo);
 

	
 
		if (xo > 0) {
 
			RedrawScreenRect(left, top, xo + left, top + height);
 
			left += xo;
 
			width -= xo;
 
		} else if (xo < 0) {
 
			RedrawScreenRect(left + width + xo, top, left + width, top + height);
 
			width += xo;
 
		}
 

	
 
		if (yo > 0) {
 
			RedrawScreenRect(left, top, width + left, top + yo);
 
		} else if (yo < 0) {
 
			RedrawScreenRect(left, top + height + yo, width + left, top + height);
 
		}
 
	}
 
}
 

	
 
static void SetViewportPosition(Window *w, int x, int y)
 
{
 
	ViewPort *vp = w->viewport;
 
	int old_left = vp->virtual_left;
 
	int old_top = vp->virtual_top;
 
	int i;
 
	int left, top, width, height;
 

	
 
	vp->virtual_left = x;
 
	vp->virtual_top = y;
 

	
 
	/* Viewport is bound to its left top corner, so it must be rounded down (UnScaleByZoomLower)
 
	 * else glitch described in FS#1412 will happen (offset by 1 pixel with zoom level > NORMAL)
 
	 */
 
	old_left = UnScaleByZoomLower(old_left, vp->zoom);
 
	old_top = UnScaleByZoomLower(old_top, vp->zoom);
 
	x = UnScaleByZoomLower(x, vp->zoom);
 
	y = UnScaleByZoomLower(y, vp->zoom);
 

	
 
	old_left -= x;
 
	old_top -= y;
 

	
 
	if (old_top == 0 && old_left == 0) return;
 

	
 
	_vp_move_offs.x = old_left;
 
	_vp_move_offs.y = old_top;
 

	
 
	left = vp->left;
 
	top = vp->top;
 
	width = vp->width;
 
	height = vp->height;
 

	
 
	if (left < 0) {
 
		width += left;
 
		left = 0;
 
	}
 

	
 
	i = left + width - _screen.width;
 
	if (i >= 0) width -= i;
 

	
 
	if (width > 0) {
 
		if (top < 0) {
 
			height += top;
 
			top = 0;
 
		}
 

	
 
		i = top + height - _screen.height;
 
		if (i >= 0) height -= i;
 

	
 
		if (height > 0) DoSetViewportPosition(w->z_front, left, top, width, height);
 
	}
 
}
 

	
 
/**
 
 * Is a xy position inside the viewport of the window?
 
 * @param w Window to examine its viewport
 
 * @param x X coordinate of the xy position
 
 * @param y Y coordinate of the xy position
 
 * @return Pointer to the viewport if the xy position is in the viewport of the window,
 
 *         otherwise \c NULL is returned.
 
 */
 
ViewPort *IsPtInWindowViewport(const Window *w, int x, int y)
 
{
 
	ViewPort *vp = w->viewport;
 

	
 
	if (vp != NULL &&
 
			IsInsideMM(x, vp->left, vp->left + vp->width) &&
 
			IsInsideMM(y, vp->top, vp->top + vp->height))
 
		return vp;
 

	
 
	return NULL;
 
}
 

	
 
/**
 
 * Translate screen coordinate in a viewport to a tile coordinate
 
 * @param vp  Viewport that contains the (\a x, \a y) screen coordinate
 
 * @param x   Screen x coordinate
 
 * @param y   Screen y coordinate
 
 * @return Tile coordinate */
 
static Point TranslateXYToTileCoord(const ViewPort *vp, int x, int y)
 
{
 
	Point pt;
 
	int a, b;
 
	uint z;
 

	
 
	if ( (uint)(x -= vp->left) >= (uint)vp->width ||
 
				(uint)(y -= vp->top) >= (uint)vp->height) {
 
				Point pt = {-1, -1};
 
				return pt;
 
	}
 

	
 
	x = (ScaleByZoom(x, vp->zoom) + vp->virtual_left) >> 2;
 
	y = (ScaleByZoom(y, vp->zoom) + vp->virtual_top) >> 1;
 

	
 
	a = y - x;
 
	b = y + x;
 

	
 
	/* we need to move variables in to the valid range, as the
 
	 * GetTileZoomCenterWindow() function can call here with invalid x and/or y,
 
	 * when the user tries to zoom out along the sides of the map */
 
	a = Clamp(a, -4 * (int)TILE_SIZE, (int)(MapMaxX() * TILE_SIZE) - 1);
 
	b = Clamp(b, -4 * (int)TILE_SIZE, (int)(MapMaxY() * TILE_SIZE) - 1);
 

	
 
	/* (a, b) is the X/Y-world coordinate that belongs to (x,y) if the landscape would be completely flat on height 0.
 
	 * Now find the Z-world coordinate by fix point iteration.
 
	 * This is a bit tricky because the tile height is non-continuous at foundations.
 
	 * The clicked point should be approached from the back, otherwise there are regions that are not clickable.
 
	 * (FOUNDATION_HALFTILE_LOWER on SLOPE_STEEP_S hides north halftile completely)
 
	 * So give it a z-malus of 4 in the first iterations.
 
	 */
 
	z = 0;
 

	
 
	int min_coord = _settings_game.construction.freeform_edges ? TILE_SIZE : 0;
 

	
 
	for (int i = 0; i < 5; i++) z = GetSlopeZ(Clamp(a + (int)max(z, 4u) - 4, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + (int)max(z, 4u) - 4, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
 
	for (uint malus = 3; malus > 0; malus--) z = GetSlopeZ(Clamp(a + (int)max(z, malus) - (int)malus, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + (int)max(z, malus) - (int)malus, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
 
	for (int i = 0; i < 5; i++) z = GetSlopeZ(Clamp(a + (int)z, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + (int)z, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
 

	
 
	pt.x = Clamp(a + (int)z, min_coord, MapMaxX() * TILE_SIZE - 1);
 
	pt.y = Clamp(b + (int)z, min_coord, MapMaxY() * TILE_SIZE - 1);
 

	
 
	return pt;
 
}
 

	
 
/* When used for zooming, check area below current coordinates (x,y)
 
 * and return the tile of the zoomed out/in position (zoom_x, zoom_y)
 
 * when you just want the tile, make x = zoom_x and y = zoom_y */
 
static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y)
 
{
 
	Window *w;
 
	ViewPort *vp;
 
	Point pt;
 

	
 
	if ( (w = FindWindowFromPt(x, y)) != NULL &&
 
			 (vp = IsPtInWindowViewport(w, x, y)) != NULL)
 
				return TranslateXYToTileCoord(vp, zoom_x, zoom_y);
 

	
 
	pt.y = pt.x = -1;
 
	return pt;
 
}
 

	
 
Point GetTileBelowCursor()
 
{
 
	return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y);
 
}
 

	
 

	
 
Point GetTileZoomCenterWindow(bool in, Window * w)
 
{
 
	int x, y;
 
	ViewPort *vp = w->viewport;
 

	
 
	if (in) {
 
		x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2);
 
		y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2);
 
	} else {
 
		x = vp->width - (_cursor.pos.x - vp->left);
 
		y = vp->height - (_cursor.pos.y - vp->top);
 
	}
 
	/* Get the tile below the cursor and center on the zoomed-out center */
 
	return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top);
 
}
 

	
 
/** Update the status of the zoom-buttons according to the zoom-level
 
/**
 
 * Update the status of the zoom-buttons according to the zoom-level
 
 * of the viewport. This will update their status and invalidate accordingly
 
 * @param w Window pointer to the window that has the zoom buttons
 
 * @param vp pointer to the viewport whose zoom-level the buttons represent
 
 * @param widget_zoom_in widget index for window with zoom-in button
 
 * @param widget_zoom_out widget index for window with zoom-out button */
 
void HandleZoomMessage(Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out)
 
{
 
	w->SetWidgetDisabledState(widget_zoom_in, vp->zoom == ZOOM_LVL_MIN);
 
	w->SetWidgetDirty(widget_zoom_in);
 

	
 
	w->SetWidgetDisabledState(widget_zoom_out, vp->zoom == ZOOM_LVL_MAX);
 
	w->SetWidgetDirty(widget_zoom_out);
 
}
 

	
 
/**
 
 * Schedules a tile sprite for drawing.
 
 *
 
 * @param image the image to draw.
 
 * @param pal the provided palette.
 
 * @param x position x (world coordinates) of the sprite.
 
 * @param y position y (world coordinates) of the sprite.
 
 * @param z position z (world coordinates) of the sprite.
 
 * @param sub Only draw a part of the sprite.
 
 * @param extra_offs_x Pixel X offset for the sprite position.
 
 * @param extra_offs_y Pixel Y offset for the sprite position.
 
 */
 
static void AddTileSpriteToDraw(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub = NULL, int extra_offs_x = 0, int extra_offs_y = 0)
 
{
 
	assert((image & SPRITE_MASK) < MAX_SPRITES);
 

	
 
	TileSpriteToDraw *ts = _vd.tile_sprites_to_draw.Append();
 
	ts->image = image;
 
	ts->pal = pal;
 
	ts->sub = sub;
 
	Point pt = RemapCoords(x, y, z);
 
	ts->x = pt.x + extra_offs_x;
 
	ts->y = pt.y + extra_offs_y;
 
}
 

	
 
/**
 
 * Adds a child sprite to the active foundation.
 
 *
 
 * The pixel offset of the sprite relative to the ParentSprite is the sum of the offset passed to OffsetGroundSprite() and extra_offs_?.
 
 *
 
 * @param image the image to draw.
 
 * @param pal the provided palette.
 
 * @param sub Only draw a part of the sprite.
 
 * @param foundation_part Foundation part.
 
 * @param extra_offs_x Pixel X offset for the sprite position.
 
 * @param extra_offs_y Pixel Y offset for the sprite position.
 
 */
 
static void AddChildSpriteToFoundation(SpriteID image, PaletteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y)
 
{
 
	assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END));
 
	assert(_vd.foundation[foundation_part] != -1);
 
	Point offs = _vd.foundation_offset[foundation_part];
 

	
 
	/* Change the active ChildSprite list to the one of the foundation */
 
	int *old_child = _vd.last_child;
 
	_vd.last_child = _vd.last_foundation_child[foundation_part];
 

	
 
	AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub);
 

	
 
	/* Switch back to last ChildSprite list */
 
	_vd.last_child = old_child;
 
}
 

	
 
/**
 
 * Draws a ground sprite at a specific world-coordinate relative to the current tile.
 
 * If the current tile is drawn on top of a foundation the sprite is added as child sprite to the "foundation"-ParentSprite.
 
 *
 
 * @param image the image to draw.
 
 * @param pal the provided palette.
 
 * @param x position x (world coordinates) of the sprite relative to current tile.
 
 * @param y position y (world coordinates) of the sprite relative to current tile.
 
 * @param z position z (world coordinates) of the sprite relative to current tile.
 
 * @param sub Only draw a part of the sprite.
 
 * @param extra_offs_x Pixel X offset for the sprite position.
 
 * @param extra_offs_y Pixel Y offset for the sprite position.
 
 */
 
void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
 
{
 
	/* Switch to first foundation part, if no foundation was drawn */
 
	if (_vd.foundation_part == FOUNDATION_PART_NONE) _vd.foundation_part = FOUNDATION_PART_NORMAL;
 

	
 
	if (_vd.foundation[_vd.foundation_part] != -1) {
 
		Point pt = RemapCoords(x, y, z);
 
		AddChildSpriteToFoundation(image, pal, sub, _vd.foundation_part, pt.x + extra_offs_x, pt.y + extra_offs_y);
 
	} else {
 
		AddTileSpriteToDraw(image, pal, _cur_ti->x + x, _cur_ti->y + y, _cur_ti->z + z, sub, extra_offs_x, extra_offs_y);
 
	}
 
}
 

	
 
/**
 
 * Draws a ground sprite for the current tile.
 
 * If the current tile is drawn on top of a foundation the sprite is added as child sprite to the "foundation"-ParentSprite.
 
 *
 
 * @param image the image to draw.
 
 * @param pal the provided palette.
 
 * @param sub Only draw a part of the sprite.
 
 * @param extra_offs_x Pixel X offset for the sprite position.
 
 * @param extra_offs_y Pixel Y offset for the sprite position.
 
 */
 
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
 
{
 
	DrawGroundSpriteAt(image, pal, 0, 0, 0, sub, extra_offs_x, extra_offs_y);
 
}
 

	
 
/**
 
 * Called when a foundation has been drawn for the current tile.
 
 * Successive ground sprites for the current tile will be drawn as child sprites of the "foundation"-ParentSprite, not as TileSprites.
 
 *
 
 * @param x sprite x-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite.
 
 * @param y sprite y-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite.
 
 */
 
void OffsetGroundSprite(int x, int y)
 
{
 
	/* Switch to next foundation part */
 
	switch (_vd.foundation_part) {
 
		case FOUNDATION_PART_NONE:
 
			_vd.foundation_part = FOUNDATION_PART_NORMAL;
 
			break;
 
		case FOUNDATION_PART_NORMAL:
 
			_vd.foundation_part = FOUNDATION_PART_HALFTILE;
 
			break;
 
		default: NOT_REACHED();
 
	}
 

	
 
	/* _vd.last_child == NULL if foundation sprite was clipped by the viewport bounds */
 
	if (_vd.last_child != NULL) _vd.foundation[_vd.foundation_part] = _vd.parent_sprites_to_draw.Length() - 1;
 

	
 
	_vd.foundation_offset[_vd.foundation_part].x = x;
 
	_vd.foundation_offset[_vd.foundation_part].y = y;
 
	_vd.last_foundation_child[_vd.foundation_part] = _vd.last_child;
 
}
 

	
 
/**
 
 * Adds a child sprite to a parent sprite.
 
 * In contrast to "AddChildSpriteScreen()" the sprite position is in world coordinates
 
 *
 
 * @param image the image to draw.
 
 * @param pal the provided palette.
 
 * @param x position x of the sprite.
 
 * @param y position y of the sprite.
 
 * @param z position z of the sprite.
 
 * @param sub Only draw a part of the sprite.
 
 */
 
static void AddCombinedSprite(SpriteID image, PaletteID pal, int x, int y, byte z, const SubSprite *sub)
 
{
 
	Point pt = RemapCoords(x, y, z);
 
	const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
 

	
 
	if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width ||
 
			pt.x + spr->x_offs + spr->width <= _vd.dpi.left ||
 
			pt.y + spr->y_offs >= _vd.dpi.top + _vd.dpi.height ||
 
			pt.y + spr->y_offs + spr->height <= _vd.dpi.top)
 
		return;
 

	
 
	const ParentSpriteToDraw *pstd = _vd.parent_sprites_to_draw.End() - 1;
 
	AddChildSpriteScreen(image, pal, pt.x - pstd->left, pt.y - pstd->top, false, sub);
 
}
 

	
 
/** Draw a (transparent) sprite at given coordinates with a given bounding box.
 
/**
 
 * Draw a (transparent) sprite at given coordinates with a given bounding box.
 
 * The bounding box extends from (x + bb_offset_x, y + bb_offset_y, z + bb_offset_z) to (x + w - 1, y + h - 1, z + dz - 1), both corners included.
 
 * Bounding boxes with bb_offset_x == w or bb_offset_y == h or bb_offset_z == dz are allowed and produce thin slices.
 
 *
 
 * @note Bounding boxes are normally specified with bb_offset_x = bb_offset_y = bb_offset_z = 0. The extent of the bounding box in negative direction is
 
 *       defined by the sprite offset in the grf file.
 
 *       However if modifying the sprite offsets is not suitable (e.g. when using existing graphics), the bounding box can be tuned by bb_offset.
 
 *
 
 * @pre w >= bb_offset_x, h >= bb_offset_y, dz >= bb_offset_z. Else w, h or dz are ignored.
 
 *
 
 * @param image the image to combine and draw,
 
 * @param pal the provided palette,
 
 * @param x position X (world) of the sprite,
 
 * @param y position Y (world) of the sprite,
 
 * @param w bounding box extent towards positive X (world),
 
 * @param h bounding box extent towards positive Y (world),
 
 * @param dz bounding box extent towards positive Z (world),
 
 * @param z position Z (world) of the sprite,
 
 * @param transparent if true, switch the palette between the provided palette and the transparent palette,
 
 * @param bb_offset_x bounding box extent towards negative X (world),
 
 * @param bb_offset_y bounding box extent towards negative Y (world),
 
 * @param bb_offset_z bounding box extent towards negative Z (world)
 
 * @param sub Only draw a part of the sprite.
 
 */
 
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
 
{
 
	int32 left, right, top, bottom;
 

	
 
	assert((image & SPRITE_MASK) < MAX_SPRITES);
 

	
 
	/* make the sprites transparent with the right palette */
 
	if (transparent) {
 
		SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
 
		pal = PALETTE_TO_TRANSPARENT;
 
	}
 

	
 
	if (_vd.combine_sprites == SPRITE_COMBINE_ACTIVE) {
 
		AddCombinedSprite(image, pal, x, y, z, sub);
 
		return;
 
	}
 

	
 
	_vd.last_child = NULL;
 

	
 
	Point pt = RemapCoords(x, y, z);
 
	int tmp_left, tmp_top, tmp_x = pt.x, tmp_y = pt.y;
 

	
 
	/* Compute screen extents of sprite */
 
	if (image == SPR_EMPTY_BOUNDING_BOX) {
 
		left = tmp_left = RemapCoords(x + w          , y + bb_offset_y, z + bb_offset_z).x;
 
		right           = RemapCoords(x + bb_offset_x, y + h          , z + bb_offset_z).x + 1;
 
		top  = tmp_top  = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz         ).y;
 
		bottom          = RemapCoords(x + w          , y + h          , z + bb_offset_z).y + 1;
 
	} else {
 
		const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
 
		left = tmp_left = (pt.x += spr->x_offs);
 
		right           = (pt.x +  spr->width );
 
		top  = tmp_top  = (pt.y += spr->y_offs);
 
		bottom          = (pt.y +  spr->height);
 
	}
 

	
 
	if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) {
 
		/* Compute maximal extents of sprite and its bounding box */
 
		left   = min(left  , RemapCoords(x + w          , y + bb_offset_y, z + bb_offset_z).x);
 
		right  = max(right , RemapCoords(x + bb_offset_x, y + h          , z + bb_offset_z).x + 1);
 
		top    = min(top   , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz         ).y);
 
		bottom = max(bottom, RemapCoords(x + w          , y + h          , z + bb_offset_z).y + 1);
 
	}
 

	
 
	/* Do not add the sprite to the viewport, if it is outside */
 
	if (left   >= _vd.dpi.left + _vd.dpi.width ||
 
	    right  <= _vd.dpi.left                 ||
 
	    top    >= _vd.dpi.top + _vd.dpi.height ||
 
	    bottom <= _vd.dpi.top) {
 
		return;
 
	}
 

	
 
	ParentSpriteToDraw *ps = _vd.parent_sprites_to_draw.Append();
 
	ps->x = tmp_x;
 
	ps->y = tmp_y;
 

	
 
	ps->left = tmp_left;
 
	ps->top  = tmp_top;
 

	
 
	ps->image = image;
 
	ps->pal = pal;
 
	ps->sub = sub;
 
	ps->xmin = x + bb_offset_x;
 
	ps->xmax = x + max(bb_offset_x, w) - 1;
 

	
 
	ps->ymin = y + bb_offset_y;
 
	ps->ymax = y + max(bb_offset_y, h) - 1;
 

	
 
	ps->zmin = z + bb_offset_z;
 
	ps->zmax = z + max(bb_offset_z, dz) - 1;
 

	
 
	ps->comparison_done = false;
 
	ps->first_child = -1;
 

	
 
	_vd.last_child = &ps->first_child;
 

	
 
	if (_vd.combine_sprites == SPRITE_COMBINE_PENDING) _vd.combine_sprites = SPRITE_COMBINE_ACTIVE;
 
}
 

	
 
/**
 
 * Starts a block of sprites, which are "combined" into a single bounding box.
 
 *
 
 * Subsequent calls to #AddSortableSpriteToDraw will be drawn into the same bounding box.
 
 * That is: The first sprite that is not clipped by the viewport defines the bounding box, and
 
 * the following sprites will be child sprites to that one.
 
 *
 
 * That implies:
 
 *  - The drawing order is definite. No other sprites will be sorted between those of the block.
 
 *  - You have to provide a valid bounding box for all sprites,
 
 *    as you won't know which one is the first non-clipped one.
 
 *    Preferable you use the same bounding box for all.
 
 *  - You cannot use #AddChildSpriteScreen inside the block, as its result will be indefinite.
 
 *
 
 * The block is terminated by #EndSpriteCombine.
 
 *
 
 * You cannot nest "combined" blocks.
 
 */
 
void StartSpriteCombine()
 
{
 
	assert(_vd.combine_sprites == SPRITE_COMBINE_NONE);
 
	_vd.combine_sprites = SPRITE_COMBINE_PENDING;
 
}
 

	
 
/**
 
 * Terminates a block of sprites started by #StartSpriteCombine.
 
 * Take a look there for details.
 
 */
 
void EndSpriteCombine()
 
{
 
	assert(_vd.combine_sprites != SPRITE_COMBINE_NONE);
 
	_vd.combine_sprites = SPRITE_COMBINE_NONE;
 
}
 

	
 
/**
 
 * Add a child sprite to a parent sprite.
 
 *
 
 * @param image the image to draw.
 
 * @param pal the provided palette.
 
 * @param x sprite x-offset (screen coordinates) relative to parent sprite.
 
 * @param y sprite y-offset (screen coordinates) relative to parent sprite.
 
 * @param transparent if true, switch the palette between the provided palette and the transparent palette,
 
 * @param sub Only draw a part of the sprite.
 
 */
 
void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub)
 
{
 
	assert((image & SPRITE_MASK) < MAX_SPRITES);
 

	
 
	/* If the ParentSprite was clipped by the viewport bounds, do not draw the ChildSprites either */
 
	if (_vd.last_child == NULL) return;
 

	
 
	/* make the sprites transparent with the right palette */
 
	if (transparent) {
 
		SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
 
		pal = PALETTE_TO_TRANSPARENT;
 
	}
 

	
 
	*_vd.last_child = _vd.child_screen_sprites_to_draw.Length();
 

	
 
	ChildScreenSpriteToDraw *cs = _vd.child_screen_sprites_to_draw.Append();
 
	cs->image = image;
 
	cs->pal = pal;
 
	cs->sub = sub;
 
	cs->x = x;
 
	cs->y = y;
 
	cs->next = -1;
 

	
 
	/* Append the sprite to the active ChildSprite list.
 
	 * If the active ParentSprite is a foundation, update last_foundation_child as well.
 
	 * Note: ChildSprites of foundations are NOT sequential in the vector, as selection sprites are added at last. */
 
	if (_vd.last_foundation_child[0] == _vd.last_child) _vd.last_foundation_child[0] = &cs->next;
 
	if (_vd.last_foundation_child[1] == _vd.last_child) _vd.last_foundation_child[1] = &cs->next;
 
	_vd.last_child = &cs->next;
 
}
 

	
 
static void AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2, Colours colour, uint16 width)
 
{
 
	assert(width != 0);
 
	StringSpriteToDraw *ss = _vd.string_sprites_to_draw.Append();
 
	ss->string = string;
 
	ss->x = x;
 
	ss->y = y;
 
	ss->params[0] = params_1;
 
	ss->params[1] = params_2;
 
	ss->width = width;
 
	ss->colour = colour;
 
}
 

	
 

	
 
/**
 
@@ -1231,385 +1235,386 @@ static void ViewportSortParentSprites(Pa
 
		}
 

	
 
		ps->comparison_done = true;
 

	
 
		for (ParentSpriteToDraw **psd2 = psd + 1; psd2 != psdvend; psd2++) {
 
			ParentSpriteToDraw *ps2 = *psd2;
 

	
 
			if (ps2->comparison_done) continue;
 

	
 
			/* Decide which comparator to use, based on whether the bounding
 
			 * boxes overlap
 
			 */
 
			if (ps->xmax >= ps2->xmin && ps->xmin <= ps2->xmax && // overlap in X?
 
					ps->ymax >= ps2->ymin && ps->ymin <= ps2->ymax && // overlap in Y?
 
					ps->zmax >= ps2->zmin && ps->zmin <= ps2->zmax) { // overlap in Z?
 
				/* Use X+Y+Z as the sorting order, so sprites closer to the bottom of
 
				 * the screen and with higher Z elevation, are drawn in front.
 
				 * Here X,Y,Z are the coordinates of the "center of mass" of the sprite,
 
				 * i.e. X=(left+right)/2, etc.
 
				 * However, since we only care about order, don't actually divide / 2
 
				 */
 
				if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <=
 
						ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) {
 
					continue;
 
				}
 
			} else {
 
				/* We only change the order, if it is definite.
 
				 * I.e. every single order of X, Y, Z says ps2 is behind ps or they overlap.
 
				 * That is: If one partial order says ps behind ps2, do not change the order.
 
				 */
 
				if (ps->xmax < ps2->xmin ||
 
						ps->ymax < ps2->ymin ||
 
						ps->zmax < ps2->zmin) {
 
					continue;
 
				}
 
			}
 

	
 
			/* Move ps2 in front of ps */
 
			ParentSpriteToDraw *temp = ps2;
 
			for (ParentSpriteToDraw **psd3 = psd2; psd3 > psd; psd3--) {
 
				*psd3 = *(psd3 - 1);
 
			}
 
			*psd = temp;
 
		}
 
	}
 
}
 

	
 
static void ViewportDrawParentSprites(const ParentSpriteToSortVector *psd, const ChildScreenSpriteToDrawVector *csstdv)
 
{
 
	const ParentSpriteToDraw * const *psd_end = psd->End();
 
	for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
 
		const ParentSpriteToDraw *ps = *it;
 
		if (ps->image != SPR_EMPTY_BOUNDING_BOX) DrawSprite(ps->image, ps->pal, ps->x, ps->y, ps->sub);
 

	
 
		int child_idx = ps->first_child;
 
		while (child_idx >= 0) {
 
			const ChildScreenSpriteToDraw *cs = csstdv->Get(child_idx);
 
			child_idx = cs->next;
 
			DrawSprite(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub);
 
		}
 
	}
 
}
 

	
 
/**
 
 * Draws the bounding boxes of all ParentSprites
 
 * @param psd Array of ParentSprites
 
 */
 
static void ViewportDrawBoundingBoxes(const ParentSpriteToSortVector *psd)
 
{
 
	const ParentSpriteToDraw * const *psd_end = psd->End();
 
	for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
 
		const ParentSpriteToDraw *ps = *it;
 
		Point pt1 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmax + 1); // top front corner
 
		Point pt2 = RemapCoords(ps->xmin    , ps->ymax + 1, ps->zmax + 1); // top left corner
 
		Point pt3 = RemapCoords(ps->xmax + 1, ps->ymin    , ps->zmax + 1); // top right corner
 
		Point pt4 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmin    ); // bottom front corner
 

	
 
		DrawBox(        pt1.x,         pt1.y,
 
		        pt2.x - pt1.x, pt2.y - pt1.y,
 
		        pt3.x - pt1.x, pt3.y - pt1.y,
 
		        pt4.x - pt1.x, pt4.y - pt1.y);
 
	}
 
}
 

	
 
static void ViewportDrawStrings(DrawPixelInfo *dpi, const StringSpriteToDrawVector *sstdv)
 
{
 
	DrawPixelInfo dp;
 
	ZoomLevel zoom;
 

	
 
	_cur_dpi = &dp;
 
	dp = *dpi;
 

	
 
	zoom = dp.zoom;
 
	dp.zoom = ZOOM_LVL_NORMAL;
 

	
 
	dp.left   = UnScaleByZoom(dp.left,   zoom);
 
	dp.top    = UnScaleByZoom(dp.top,    zoom);
 
	dp.width  = UnScaleByZoom(dp.width,  zoom);
 
	dp.height = UnScaleByZoom(dp.height, zoom);
 

	
 
	const StringSpriteToDraw *ssend = sstdv->End();
 
	for (const StringSpriteToDraw *ss = sstdv->Begin(); ss != ssend; ++ss) {
 
		TextColour colour = TC_BLACK;
 
		bool small = HasBit(ss->width, 15);
 
		int w = GB(ss->width, 0, 15);
 
		int x = UnScaleByZoom(ss->x, zoom);
 
		int y = UnScaleByZoom(ss->y, zoom);
 
		int h = VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM;
 

	
 
		SetDParam(0, ss->params[0]);
 
		SetDParam(1, ss->params[1]);
 

	
 
		if (ss->colour != INVALID_COLOUR) {
 
			/* Do not draw signs nor station names if they are set invisible */
 
			if (IsInvisibilitySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) continue;
 

	
 
			/* if we didn't draw a rectangle, or if transparant building is on,
 
			 * draw the text in the colour the rectangle would have */
 
			if (IsTransparencySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) {
 
				/* Real colours need the IS_PALETTE_COLOUR flag
 
				 * otherwise colours from _string_colourmap are assumed. */
 
				colour = (TextColour)_colour_gradient[ss->colour][6] | IS_PALETTE_COLOUR;
 
			}
 

	
 
			/* Draw the rectangle if 'tranparent station signs' is off,
 
			 * or if we are drawing a general text sign (STR_WHITE_SIGN) */
 
			if (!IsTransparencySet(TO_SIGNS) || ss->string == STR_WHITE_SIGN) {
 
				DrawFrameRect(
 
					x, y, x + w, y + h, ss->colour,
 
					IsTransparencySet(TO_SIGNS) ? FR_TRANSPARENT : FR_NONE
 
				);
 
			}
 
		}
 

	
 
		DrawString(x + VPSM_LEFT, x + w - 1 - VPSM_RIGHT, y + VPSM_TOP, ss->string, colour, SA_HOR_CENTER);
 
	}
 
}
 

	
 
void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom)
 
{
 
	DrawPixelInfo *old_dpi = _cur_dpi;
 
	_cur_dpi = &_vd.dpi;
 

	
 
	_vd.dpi.zoom = vp->zoom;
 
	int mask = ScaleByZoom(-1, vp->zoom);
 

	
 
	_vd.combine_sprites = SPRITE_COMBINE_NONE;
 

	
 
	_vd.dpi.width = (right - left) & mask;
 
	_vd.dpi.height = (bottom - top) & mask;
 
	_vd.dpi.left = left & mask;
 
	_vd.dpi.top = top & mask;
 
	_vd.dpi.pitch = old_dpi->pitch;
 
	_vd.last_child = NULL;
 

	
 
	int x = UnScaleByZoom(_vd.dpi.left - (vp->virtual_left & mask), vp->zoom) + vp->left;
 
	int y = UnScaleByZoom(_vd.dpi.top - (vp->virtual_top & mask), vp->zoom) + vp->top;
 

	
 
	_vd.dpi.dst_ptr = BlitterFactoryBase::GetCurrentBlitter()->MoveTo(old_dpi->dst_ptr, x - old_dpi->left, y - old_dpi->top);
 

	
 
	ViewportAddLandscape();
 
	ViewportAddVehicles(&_vd.dpi);
 

	
 
	ViewportAddTownNames(&_vd.dpi);
 
	ViewportAddStationNames(&_vd.dpi);
 
	ViewportAddSigns(&_vd.dpi);
 

	
 
	DrawTextEffects(&_vd.dpi);
 

	
 
	if (_vd.tile_sprites_to_draw.Length() != 0) ViewportDrawTileSprites(&_vd.tile_sprites_to_draw);
 

	
 
	ParentSpriteToDraw *psd_end = _vd.parent_sprites_to_draw.End();
 
	for (ParentSpriteToDraw *it = _vd.parent_sprites_to_draw.Begin(); it != psd_end; it++) {
 
		*_vd.parent_sprites_to_sort.Append() = it;
 
	}
 

	
 
	ViewportSortParentSprites(&_vd.parent_sprites_to_sort);
 
	ViewportDrawParentSprites(&_vd.parent_sprites_to_sort, &_vd.child_screen_sprites_to_draw);
 

	
 
	if (_draw_bounding_boxes) ViewportDrawBoundingBoxes(&_vd.parent_sprites_to_sort);
 

	
 
	if (_vd.string_sprites_to_draw.Length() != 0) ViewportDrawStrings(&_vd.dpi, &_vd.string_sprites_to_draw);
 

	
 
	_cur_dpi = old_dpi;
 

	
 
	_vd.string_sprites_to_draw.Clear();
 
	_vd.tile_sprites_to_draw.Clear();
 
	_vd.parent_sprites_to_draw.Clear();
 
	_vd.parent_sprites_to_sort.Clear();
 
	_vd.child_screen_sprites_to_draw.Clear();
 
}
 

	
 
/** Make sure we don't draw a too big area at a time.
 
/**
 
 * Make sure we don't draw a too big area at a time.
 
 * If we do, the sprite memory will overflow. */
 
static void ViewportDrawChk(const ViewPort *vp, int left, int top, int right, int bottom)
 
{
 
	if (ScaleByZoom(bottom - top, vp->zoom) * ScaleByZoom(right - left, vp->zoom) > 180000) {
 
		if ((bottom - top) > (right - left)) {
 
			int t = (top + bottom) >> 1;
 
			ViewportDrawChk(vp, left, top, right, t);
 
			ViewportDrawChk(vp, left, t, right, bottom);
 
		} else {
 
			int t = (left + right) >> 1;
 
			ViewportDrawChk(vp, left, top, t, bottom);
 
			ViewportDrawChk(vp, t, top, right, bottom);
 
		}
 
	} else {
 
		ViewportDoDraw(vp,
 
			ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
 
			ScaleByZoom(top - vp->top, vp->zoom) + vp->virtual_top,
 
			ScaleByZoom(right - vp->left, vp->zoom) + vp->virtual_left,
 
			ScaleByZoom(bottom - vp->top, vp->zoom) + vp->virtual_top
 
		);
 
	}
 
}
 

	
 
static inline void ViewportDraw(const ViewPort *vp, int left, int top, int right, int bottom)
 
{
 
	if (right <= vp->left || bottom <= vp->top) return;
 

	
 
	if (left >= vp->left + vp->width) return;
 

	
 
	if (left < vp->left) left = vp->left;
 
	if (right > vp->left + vp->width) right = vp->left + vp->width;
 

	
 
	if (top >= vp->top + vp->height) return;
 

	
 
	if (top < vp->top) top = vp->top;
 
	if (bottom > vp->top + vp->height) bottom = vp->top + vp->height;
 

	
 
	ViewportDrawChk(vp, left, top, right, bottom);
 
}
 

	
 
/**
 
 * Draw the viewport of this window.
 
 */
 
void Window::DrawViewport() const
 
{
 
	DrawPixelInfo *dpi = _cur_dpi;
 

	
 
	dpi->left += this->left;
 
	dpi->top += this->top;
 

	
 
	ViewportDraw(this->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height);
 

	
 
	dpi->left -= this->left;
 
	dpi->top -= this->top;
 
}
 

	
 
static inline void ClampViewportToMap(const ViewPort *vp, int &x, int &y)
 
{
 
	/* Centre of the viewport is hot spot */
 
	x += vp->virtual_width / 2;
 
	y += vp->virtual_height / 2;
 

	
 
	/* Convert viewport coordinates to map coordinates
 
	 * Calculation is scaled by 4 to avoid rounding errors */
 
	int vx = -x + y * 2;
 
	int vy =  x + y * 2;
 

	
 
	/* clamp to size of map */
 
	vx = Clamp(vx, 0, MapMaxX() * TILE_SIZE * 4);
 
	vy = Clamp(vy, 0, MapMaxY() * TILE_SIZE * 4);
 

	
 
	/* Convert map coordinates to viewport coordinates */
 
	x = (-vx + vy) / 2;
 
	y = ( vx + vy) / 4;
 

	
 
	/* Remove centering */
 
	x -= vp->virtual_width / 2;
 
	y -= vp->virtual_height / 2;
 
}
 

	
 
/**
 
 * Update the viewport position being displayed.
 
 * @param w %Window owning the viewport.
 
 */
 
void UpdateViewportPosition(Window *w)
 
{
 
	const ViewPort *vp = w->viewport;
 

	
 
	if (w->viewport->follow_vehicle != INVALID_VEHICLE) {
 
		const Vehicle *veh = Vehicle::Get(w->viewport->follow_vehicle);
 
		Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
 

	
 
		w->viewport->scrollpos_x = pt.x;
 
		w->viewport->scrollpos_y = pt.y;
 
		SetViewportPosition(w, pt.x, pt.y);
 
	} else {
 
		/* Ensure the destination location is within the map */
 
		ClampViewportToMap(vp, w->viewport->dest_scrollpos_x, w->viewport->dest_scrollpos_y);
 

	
 
		int delta_x = w->viewport->dest_scrollpos_x - w->viewport->scrollpos_x;
 
		int delta_y = w->viewport->dest_scrollpos_y - w->viewport->scrollpos_y;
 

	
 
		if (delta_x != 0 || delta_y != 0) {
 
			if (_settings_client.gui.smooth_scroll) {
 
				int max_scroll = ScaleByMapSize1D(512);
 
				/* Not at our desired position yet... */
 
				w->viewport->scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll);
 
				w->viewport->scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll);
 
			} else {
 
				w->viewport->scrollpos_x = w->viewport->dest_scrollpos_x;
 
				w->viewport->scrollpos_y = w->viewport->dest_scrollpos_y;
 
			}
 
		}
 

	
 
		ClampViewportToMap(vp, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
 

	
 
		SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
 
	}
 
}
 

	
 
/**
 
 * Marks a viewport as dirty for repaint if it displays (a part of) the area the needs to be repainted.
 
 * @param vp     The viewport to mark as dirty
 
 * @param left   Left edge of area to repaint
 
 * @param top    Top edge of area to repaint
 
 * @param right  Right edge of area to repaint
 
 * @param bottom Bottom edge of area to repaint
 
 * @ingroup dirty
 
 */
 
static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom)
 
{
 
	right -= vp->virtual_left;
 
	if (right <= 0) return;
 

	
 
	bottom -= vp->virtual_top;
 
	if (bottom <= 0) return;
 

	
 
	left = max(0, left - vp->virtual_left);
 

	
 
	if (left >= vp->virtual_width) return;
 

	
 
	top = max(0, top - vp->virtual_top);
 

	
 
	if (top >= vp->virtual_height) return;
 

	
 
	SetDirtyBlocks(
 
		UnScaleByZoomLower(left, vp->zoom) + vp->left,
 
		UnScaleByZoomLower(top, vp->zoom) + vp->top,
 
		UnScaleByZoom(right, vp->zoom) + vp->left + 1,
 
		UnScaleByZoom(bottom, vp->zoom) + vp->top + 1
 
	);
 
}
 

	
 
/**
 
 * Mark all viewports that display an area as dirty (in need of repaint).
 
 * @param left   Left edge of area to repaint
 
 * @param top    Top edge of area to repaint
 
 * @param right  Right edge of area to repaint
 
 * @param bottom Bottom edge of area to repaint
 
 * @ingroup dirty
 
 */
 
void MarkAllViewportsDirty(int left, int top, int right, int bottom)
 
{
 
	Window *w;
 
	FOR_ALL_WINDOWS_FROM_BACK(w) {
 
		ViewPort *vp = w->viewport;
 
		if (vp != NULL) {
 
			assert(vp->width != 0);
 
			MarkViewportDirty(vp, left, top, right, bottom);
 
		}
 
	}
 
}
 

	
 
void MarkTileDirtyByTile(TileIndex tile)
 
{
 
	Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTileZ(tile));
 
	MarkAllViewportsDirty(
 
		pt.x - 31,
 
		pt.y - 122,
 
		pt.x - 31 + 67,
 
		pt.y - 122 + 154
 
	);
 
}
 

	
 
/**
 
 * Marks the selected tiles as dirty.
 
 *
 
 * This function marks the selected tiles as dirty for repaint
 
 *
 
 * @ingroup dirty
 
 */
 
static void SetSelectionTilesDirty()
 
@@ -1670,691 +1675,697 @@ static void SetSelectionTilesDirty()
 
		Point bot = RemapCoords2(bot_x + TILE_SIZE - 1, bot_y + TILE_SIZE - 1); // bottommost point
 

	
 
		/* the 'x' coordinate of 'top' and 'bot' is the same (and always in the same distance from tile middle),
 
		 * tile height/slope affects only the 'y' on-screen coordinate! */
 

	
 
		int l = top.x - (TILE_PIXELS - 2); // 'x' coordinate of left side of dirty rectangle
 
		int t = top.y;                     // 'y' coordinate of top side -//-
 
		int r = top.x + (TILE_PIXELS - 2); // right side of dirty rectangle
 
		int b = bot.y;                     // bottom -//-
 

	
 
		static const int OVERLAY_WIDTH = 4; // part of selection sprites is drawn outside the selected area
 

	
 
		/* For halftile foundations on SLOPE_STEEP_S the sprite extents some more towards the top */
 
		MarkAllViewportsDirty(l - OVERLAY_WIDTH, t - OVERLAY_WIDTH - TILE_HEIGHT, r + OVERLAY_WIDTH, b + OVERLAY_WIDTH);
 

	
 
		/* haven't we reached the topmost tile yet? */
 
		if (top_x != x_start) {
 
			top_x -= TILE_SIZE;
 
		} else {
 
			top_y += TILE_SIZE;
 
		}
 

	
 
		/* the way the bottom tile changes is different when we reach the bottommost tile */
 
		if (bot_y != y_end) {
 
			bot_y += TILE_SIZE;
 
		} else {
 
			bot_x -= TILE_SIZE;
 
		}
 
	} while (bot_x >= top_x);
 
}
 

	
 

	
 
void SetSelectionRed(bool b)
 
{
 
	_thd.make_square_red = b;
 
	SetSelectionTilesDirty();
 
}
 

	
 
/**
 
 * Test whether a sign is below the mouse
 
 * @param vp the clicked viewport
 
 * @param x X position of click
 
 * @param y Y position of click
 
 * @param sign the sign to check
 
 * @return true if the sign was hit
 
 */
 
static bool CheckClickOnViewportSign(const ViewPort *vp, int x, int y, const ViewportSign *sign)
 
{
 
	bool small = (vp->zoom >= ZOOM_LVL_OUT_4X);
 
	int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, vp->zoom);
 
	int sign_height = ScaleByZoom(VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM, vp->zoom);
 

	
 
	x = ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left;
 
	y = ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top;
 

	
 
	return
 
			y >= sign->top &&
 
			y <  sign->top + sign_height &&
 
			x >= sign->center - sign_half_width &&
 
			x <  sign->center + sign_half_width;
 
}
 

	
 
static bool CheckClickOnTown(const ViewPort *vp, int x, int y)
 
{
 
	if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES)) return false;
 

	
 
	const Town *t;
 
	FOR_ALL_TOWNS(t) {
 
		if (CheckClickOnViewportSign(vp, x, y, &t->sign)) {
 
			ShowTownViewWindow(t->index);
 
			return true;
 
		}
 
	}
 

	
 
	return false;
 
}
 

	
 
static bool CheckClickOnStation(const ViewPort *vp, int x, int y)
 
{
 
	if (!(HasBit(_display_opt, DO_SHOW_STATION_NAMES) || HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)) || IsInvisibilitySet(TO_SIGNS)) return false;
 

	
 
	const BaseStation *st;
 
	FOR_ALL_BASE_STATIONS(st) {
 
		/* Check whether the base station is a station or a waypoint */
 
		bool is_station = Station::IsExpected(st);
 

	
 
		/* Don't check if the display options are disabled */
 
		if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
 

	
 
		if (CheckClickOnViewportSign(vp, x, y, &st->sign)) {
 
			if (is_station) {
 
				ShowStationViewWindow(st->index);
 
			} else {
 
				ShowWaypointWindow(Waypoint::From(st));
 
			}
 
			return true;
 
		}
 
	}
 

	
 
	return false;
 
}
 

	
 

	
 
static bool CheckClickOnSign(const ViewPort *vp, int x, int y)
 
{
 
	/* Signs are turned off, or they are transparent and invisibility is ON, or company is a spectator */
 
	if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS) || _local_company == COMPANY_SPECTATOR) return false;
 

	
 
	const Sign *si;
 
	FOR_ALL_SIGNS(si) {
 
		if (CheckClickOnViewportSign(vp, x, y, &si->sign)) {
 
			HandleClickOnSign(si);
 
			return true;
 
		}
 
	}
 

	
 
	return false;
 
}
 

	
 

	
 
static bool CheckClickOnLandscape(const ViewPort *vp, int x, int y)
 
{
 
	Point pt = TranslateXYToTileCoord(vp, x, y);
 

	
 
	if (pt.x != -1) return ClickTile(TileVirtXY(pt.x, pt.y));
 
	return true;
 
}
 

	
 

	
 
bool HandleViewportClicked(const ViewPort *vp, int x, int y)
 
{
 
	const Vehicle *v;
 

	
 
	if (CheckClickOnTown(vp, x, y)) return true;
 
	if (CheckClickOnStation(vp, x, y)) return true;
 
	if (CheckClickOnSign(vp, x, y)) return true;
 
	CheckClickOnLandscape(vp, x, y);
 

	
 
	v = CheckClickOnVehicle(vp, x, y);
 
	if (v != NULL) {
 
		DEBUG(misc, 2, "Vehicle %d (index %d) at %p", v->unitnumber, v->index, v);
 
		if (IsCompanyBuildableVehicleType(v)) {
 
			v = v->First();
 
			if (_ctrl_pressed && v->owner == _local_company) {
 
				StartStopVehicle(v, true);
 
			} else {
 
				ShowVehicleViewWindow(v);
 
			}
 
		}
 
		return true;
 
	}
 
	return CheckClickOnLandscape(vp, x, y);
 
}
 

	
 
Vehicle *CheckMouseOverVehicle()
 
{
 
	const Window *w;
 
	const ViewPort *vp;
 

	
 
	int x = _cursor.pos.x;
 
	int y = _cursor.pos.y;
 

	
 
	w = FindWindowFromPt(x, y);
 
	if (w == NULL) return NULL;
 

	
 
	vp = IsPtInWindowViewport(w, x, y);
 
	return (vp != NULL) ? CheckClickOnVehicle(vp, x, y) : NULL;
 
}
 

	
 

	
 

	
 
void PlaceObject()
 
{
 
	Point pt;
 
	Window *w;
 

	
 
	pt = GetTileBelowCursor();
 
	if (pt.x == -1) return;
 

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

	
 
	_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));
 
}
 

	
 

	
 
/** Scrolls the viewport in a window to a given location.
 
/**
 
 * Scrolls the viewport in a window to a given location.
 
 * @param x       Desired x location of the map to scroll to (world coordinate).
 
 * @param y       Desired y location of the map to scroll to (world coordinate).
 
 * @param z       Desired z location of the map to scroll to (world coordinate). Use \c -1 to scroll to the height of the map at the \a x, \a y location.
 
 * @param w       %Window containing the viewport.
 
 * @param instant Jump to the location instead of slowly moving to it.
 
 * @return Destination of the viewport was changed (to activate other actions when the viewport is already at the desired position).
 
 */
 
bool ScrollWindowTo(int x, int y, int z, Window *w, bool instant)
 
{
 
	/* The slope cannot be acquired outside of the map, so make sure we are always within the map. */
 
	if (z == -1) z = GetSlopeZ(Clamp(x, 0, MapSizeX() * TILE_SIZE - 1), Clamp(y, 0, MapSizeY() * TILE_SIZE - 1));
 

	
 
	Point pt = MapXYZToViewport(w->viewport, x, y, z);
 
	w->viewport->follow_vehicle = INVALID_VEHICLE;
 

	
 
	if (w->viewport->dest_scrollpos_x == pt.x && w->viewport->dest_scrollpos_y == pt.y) return false;
 

	
 
	if (instant) {
 
		w->viewport->scrollpos_x = pt.x;
 
		w->viewport->scrollpos_y = pt.y;
 
	}
 

	
 
	w->viewport->dest_scrollpos_x = pt.x;
 
	w->viewport->dest_scrollpos_y = pt.y;
 
	return true;
 
}
 

	
 
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
 
{
 
	return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, -1, instant);
 
}
 

	
 
void SetRedErrorSquare(TileIndex tile)
 
{
 
	TileIndex old;
 

	
 
	old = _thd.redsq;
 
	_thd.redsq = tile;
 

	
 
	if (tile != old) {
 
		if (tile != INVALID_TILE) MarkTileDirtyByTile(tile);
 
		if (old  != INVALID_TILE) MarkTileDirtyByTile(old);
 
	}
 
}
 

	
 
/** Highlight \a w by \a h tiles at the cursor.
 
/**
 
 * Highlight \a w by \a h tiles at the cursor.
 
 * @param w Width of the highlighted tiles rectangle.
 
 * @param h Height of the highlighted tiles rectangle.
 
 */
 
void SetTileSelectSize(int w, int h)
 
{
 
	_thd.new_size.x = w * TILE_SIZE;
 
	_thd.new_size.y = h * TILE_SIZE;
 
	_thd.new_outersize.x = 0;
 
	_thd.new_outersize.y = 0;
 
}
 

	
 
void SetTileSelectBigSize(int ox, int oy, int sx, int sy)
 
{
 
	_thd.offs.x = ox * TILE_SIZE;
 
	_thd.offs.y = oy * TILE_SIZE;
 
	_thd.new_outersize.x = sx * TILE_SIZE;
 
	_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 & 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,
 
 * Once for the old and once for the new selection.
 
 * _thd is TileHighlightData, found in viewport.h
 
 */
 
void UpdateTileSelection()
 
{
 
	int x1;
 
	int y1;
 

	
 
	_thd.new_drawstyle = HT_NONE;
 

	
 
	if (_thd.place_mode == HT_SPECIAL) {
 
		x1 = _thd.selend.x;
 
		y1 = _thd.selend.y;
 
		if (x1 != -1) {
 
			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;
 
			_thd.new_size.y = y2 - y1 + TILE_SIZE;
 
			_thd.new_drawstyle = _thd.next_drawstyle;
 
		}
 
	} else if (_thd.place_mode != HT_NONE) {
 
		Point pt = GetTileBelowCursor();
 
		x1 = pt.x;
 
		y1 = pt.y;
 
		if (x1 != -1) {
 
			switch (_thd.place_mode & HT_DRAG_MASK) {
 
				case HT_RECT:
 
					_thd.new_drawstyle = HT_RECT;
 
					break;
 
				case HT_POINT:
 
					_thd.new_drawstyle = HT_POINT;
 
					x1 += TILE_SIZE / 2;
 
					y1 += TILE_SIZE / 2;
 
					break;
 
				case HT_RAIL:
 
					/* Draw one highlighted tile in any direction */
 
					_thd.new_drawstyle = GetAutorailHT(pt.x, pt.y);
 
					break;
 
				case HT_LINE:
 
					switch (_thd.place_mode & HT_DIR_MASK) {
 
						case HT_DIR_X: _thd.new_drawstyle = HT_LINE | HT_DIR_X; break;
 
						case HT_DIR_Y: _thd.new_drawstyle = HT_LINE | HT_DIR_Y; break;
 

	
 
						case HT_DIR_HU:
 
						case HT_DIR_HL:
 
							_thd.new_drawstyle = (pt.x & TILE_UNIT_MASK) + (pt.y & TILE_UNIT_MASK) <= TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
 
							break;
 

	
 
						case HT_DIR_VL:
 
						case HT_DIR_VR:
 
							_thd.new_drawstyle = (pt.x & TILE_UNIT_MASK) > (pt.y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
 
							break;
 

	
 
						default: NOT_REACHED();
 
					}
 
					_thd.selstart.x = x1 & ~TILE_UNIT_MASK;
 
					_thd.selstart.y = y1 & ~TILE_UNIT_MASK;
 
					break;
 
				default:
 
					NOT_REACHED();
 
					break;
 
			}
 
			_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 ||
 
			_thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y ||
 
			_thd.outersize.x != _thd.new_outersize.x ||
 
			_thd.outersize.y != _thd.new_outersize.y) {
 
		/* clear the old selection? */
 
		if (_thd.drawstyle) SetSelectionTilesDirty();
 

	
 
		_thd.drawstyle = _thd.new_drawstyle;
 
		_thd.pos = _thd.new_pos;
 
		_thd.size = _thd.new_size;
 
		_thd.outersize = _thd.new_outersize;
 
		_thd.dirty = 0xff;
 

	
 
		/* draw the new selection? */
 
		if (_thd.new_drawstyle) SetSelectionTilesDirty();
 
	}
 
}
 

	
 
/** Displays the measurement tooltips when selecting multiple tiles
 
/**
 
 * Displays the measurement tooltips when selecting multiple tiles
 
 * @param str String to be displayed
 
 * @param paramcount number of params to deal with
 
 * @param params (optional) up to 5 pieces of additional information that may be added to a tooltip
 
 */
 
static inline void ShowMeasurementTooltips(StringID str, uint paramcount, const uint64 params[])
 
{
 
	if (!_settings_client.gui.measure_tooltip) return;
 
	GuiShowTooltips(str, paramcount, params, TCC_LEFT_CLICK);
 
}
 

	
 
/** highlighting tiles while only going over them with the mouse */
 
void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, ViewportDragDropSelectionProcess process)
 
{
 
	_thd.select_method = method;
 
	_thd.select_proc   = process;
 
	_thd.selend.x = TileX(tile) * TILE_SIZE;
 
	_thd.selstart.x = TileX(tile) * TILE_SIZE;
 
	_thd.selend.y = TileY(tile) * TILE_SIZE;
 
	_thd.selstart.y = TileY(tile) * TILE_SIZE;
 

	
 
	/* Needed so several things (road, autoroad, bridges, ...) are placed correctly.
 
	 * In effect, placement starts from the centre of a tile
 
	 */
 
	if (method == VPM_X_OR_Y || method == VPM_FIX_X || method == VPM_FIX_Y) {
 
		_thd.selend.x += TILE_SIZE / 2;
 
		_thd.selend.y += TILE_SIZE / 2;
 
		_thd.selstart.x += TILE_SIZE / 2;
 
		_thd.selstart.y += TILE_SIZE / 2;
 
	}
 

	
 
	if (_thd.place_mode == HT_RECT) {
 
		_thd.place_mode = HT_SPECIAL;
 
		_thd.next_drawstyle = HT_RECT;
 
	} else if (_thd.place_mode & (HT_RAIL | HT_LINE)) {
 
		_thd.place_mode = HT_SPECIAL;
 
		_thd.next_drawstyle = _thd.drawstyle;
 
	} else {
 
		_thd.place_mode = HT_SPECIAL;
 
		_thd.next_drawstyle = HT_POINT;
 
	}
 
	_special_mouse_mode = WSM_SIZING;
 
}
 

	
 
void VpSetPlaceSizingLimit(int limit)
 
{
 
	_thd.sizelimit = limit;
 
}
 

	
 
/**
 
 * Highlights all tiles between a set of two tiles. Used in dock and tunnel placement
 
 * @param from TileIndex of the first tile to highlight
 
 * @param to TileIndex of the last tile to highlight */
 
void VpSetPresizeRange(TileIndex from, TileIndex to)
 
{
 
	uint64 distance = DistanceManhattan(from, to) + 1;
 

	
 
	_thd.selend.x = TileX(to) * TILE_SIZE;
 
	_thd.selend.y = TileY(to) * TILE_SIZE;
 
	_thd.selstart.x = TileX(from) * TILE_SIZE;
 
	_thd.selstart.y = TileY(from) * TILE_SIZE;
 
	_thd.next_drawstyle = HT_RECT;
 

	
 
	/* show measurement only if there is any length to speak of */
 
	if (distance > 1) ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1, &distance);
 
}
 

	
 
static void VpStartPreSizing()
 
{
 
	_thd.selend.x = -1;
 
	_special_mouse_mode = WSM_PRESIZE;
 
}
 

	
 
/** returns information about the 2x1 piece to be build.
 
/**
 
 * 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 & TILE_UNIT_MASK) + (_thd.selend.y & TILE_UNIT_MASK);
 
	int fxmy = _tile_fract_coords.x - _tile_fract_coords.y;
 
	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) return HT_DIR_HL;
 
			if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
 
			return HT_DIR_Y;
 

	
 
		case 1:
 
			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) return HT_DIR_VL;
 
			if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
 
			return HT_DIR_X;
 

	
 
		case 3:
 
			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
 
/**
 
 * Check if the direction of start and end tile should be swapped based on
 
 * the dragging-style. Default directions are:
 
 * in the case of a line (HT_RAIL, HT_LINE):  DIR_NE, DIR_NW, DIR_N, DIR_E
 
 * in the case of a rect (HT_RECT, HT_POINT): DIR_S, DIR_E
 
 * For example dragging a rectangle area from south to north should be swapped to
 
 * north-south (DIR_S) to obtain the same results with less code. This is what
 
 * the return value signifies.
 
 * @param style HighLightStyle dragging style
 
 * @param start_tile start tile of drag
 
 * @param end_tile end tile of drag
 
 * @return boolean value which when true means start/end should be swapped */
 
static bool SwapDirection(HighLightStyle style, TileIndex start_tile, TileIndex end_tile)
 
{
 
	uint start_x = TileX(start_tile);
 
	uint start_y = TileY(start_tile);
 
	uint end_x = TileX(end_tile);
 
	uint end_y = TileY(end_tile);
 

	
 
	switch (style & HT_DRAG_MASK) {
 
		case HT_RAIL:
 
		case HT_LINE: return (end_x > start_x || (end_x == start_x && end_y > start_y));
 

	
 
		case HT_RECT:
 
		case HT_POINT: return (end_x != start_x && end_y < start_y);
 
		default: NOT_REACHED();
 
	}
 

	
 
	return false;
 
}
 

	
 
/** Calculates height difference between one tile and another.
 
/**
 
 * Calculates height difference between one tile and another.
 
 * Multiplies the result to suit the standard given by #TILE_HEIGHT_STEP.
 
 *
 
 * To correctly get the height difference we need the direction we are dragging
 
 * in, as well as with what kind of tool we are dragging. For example a horizontal
 
 * autorail tool that starts in bottom and ends at the top of a tile will need the
 
 * maximum of SW, S and SE, N corners respectively. This is handled by the lookup table below
 
 * See #_tileoffs_by_dir in map.cpp for the direction enums if you can't figure out the values yourself.
 
 * @param style      Highlighting style of the drag. This includes direction and style (autorail, rect, etc.)
 
 * @param distance   Number of tiles dragged, important for horizontal/vertical drags, ignored for others.
 
 * @param start_tile Start tile of the drag operation.
 
 * @param end_tile   End tile of the drag operation.
 
 * @return Height difference between two tiles. The tile measurement tool utilizes this value in its tooltip.
 
 */
 
static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile)
 
{
 
	bool swap = SwapDirection(style, start_tile, end_tile);
 
	uint h0, h1; // Start height and end height.
 

	
 
	if (start_tile == end_tile) return 0;
 
	if (swap) Swap(start_tile, end_tile);
 

	
 
	switch (style & HT_DRAG_MASK) {
 
		case HT_RECT: {
 
			static const TileIndexDiffC heightdiff_area_by_dir[] = {
 
				/* Start */ {1, 0}, /* Dragging east */ {0, 0}, // Dragging south
 
				/* End   */ {0, 1}, /* Dragging east */ {1, 1}  // Dragging south
 
			};
 

	
 
			/* In the case of an area we can determine whether we were dragging south or
 
			 * east by checking the X-coordinates of the tiles */
 
			byte style_t = (byte)(TileX(end_tile) > TileX(start_tile));
 
			start_tile = TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_area_by_dir[style_t]));
 
			end_tile   = TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_area_by_dir[2 + style_t]));
 
		}
 
		/* FALL THROUGH */
 
		case HT_POINT:
 
			h0 = TileHeight(start_tile);
 
			h1 = TileHeight(end_tile);
 
			break;
 
		default: { // All other types, this is mostly only line/autorail
 
			static const HighLightStyle flip_style_direction[] = {
 
				HT_DIR_X, HT_DIR_Y, HT_DIR_HL, HT_DIR_HU, HT_DIR_VR, HT_DIR_VL
 
			};
 
			static const TileIndexDiffC heightdiff_line_by_dir[] = {
 
				/* Start */ {1, 0}, {1, 1}, /* HT_DIR_X  */ {0, 1}, {1, 1}, // HT_DIR_Y
 
				/* Start */ {1, 0}, {0, 0}, /* HT_DIR_HU */ {1, 0}, {1, 1}, // HT_DIR_HL
 
				/* Start */ {1, 0}, {1, 1}, /* HT_DIR_VL */ {0, 1}, {1, 1}, // HT_DIR_VR
 

	
 
				/* Start */ {0, 1}, {0, 0}, /* HT_DIR_X  */ {1, 0}, {0, 0}, // HT_DIR_Y
 
				/* End   */ {0, 1}, {0, 0}, /* HT_DIR_HU */ {1, 1}, {0, 1}, // HT_DIR_HL
 
				/* End   */ {1, 0}, {0, 0}, /* HT_DIR_VL */ {0, 0}, {0, 1}, // HT_DIR_VR
 
			};
 

	
 
			distance %= 2; // we're only interested if the distance is even or uneven
 
			style &= HT_DIR_MASK;
 

	
 
			/* To handle autorail, we do some magic to be able to use a lookup table.
 
			 * Firstly if we drag the other way around, we switch start&end, and if needed
 
			 * also flip the drag-position. Eg if it was on the left, and the distance is even
 
			 * that means the end, which is now the start is on the right */
 
			if (swap && distance == 0) style = flip_style_direction[style];
 

	
 
			/* Use lookup table for start-tile based on HighLightStyle direction */
 
			byte style_t = style * 2;
 
			assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
 
			h0 = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t])));
 
			uint ht = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t + 1])));
 
			h0 = max(h0, ht);
 

	
 
			/* Use lookup table for end-tile based on HighLightStyle direction
 
			 * flip around side (lower/upper, left/right) based on distance */
 
			if (distance == 0) style_t = flip_style_direction[style] * 2;
 
			assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
 
			h1 = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t])));
 
			ht = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t + 1])));
 
			h1 = max(h1, ht);
 
			break;
 
		}
 
	}
 

	
 
	if (swap) Swap(h0, h1);
 
	return (int)(h1 - h0) * TILE_HEIGHT_STEP;
 
}
 

	
 
static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF};
 

	
 
/**
 
 * Check for underflowing the map.
 
 * @param test  the variable to test for underflowing
 
 * @param other the other variable to update to keep the line
 
 * @param mult  the constant to multiply the difference by for \c other
 
 */
 
static void CheckUnderflow(int &test, int &other, int mult)
 
{
 
	if (test >= 0) return;
 

	
 
	other += mult * test;
 
	test = 0;
 
}
 

	
 
/**
 
 * Check for overflowing the map.
 
 * @param test  the variable to test for overflowing
 
 * @param other the other variable to update to keep the line
 
 * @param max   the maximum value for the \c test variable
 
 * @param mult  the constant to multiply the difference by for \c other
 
 */
 
static void CheckOverflow(int &test, int &other, int max, int mult)
 
{
 
	if (test <= max) return;
 

	
 
	other += mult * (test - max);
 
	test = max;
 
}
 

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

	
 
	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 (method & ~(VPM_RAILDIRS | VPM_SIGNALDIRS)) {
 
		/* We 'force' a selection direction; first four rail buttons. */
 
		method &= ~(VPM_RAILDIRS | VPM_SIGNALDIRS);
 
		int raw_dx = thd->selstart.x - thd->selend.x;
 
		int raw_dy = thd->selstart.y - thd->selend.y;
 
		switch (method) {
 
			case VPM_FIX_X:
 
				b = HT_LINE | HT_DIR_Y;
 
				x = thd->selstart.x;
 
				break;
 

	
 
			case VPM_FIX_Y:
 
				b = HT_LINE | HT_DIR_X;
 
				y = thd->selstart.y;
 
				break;
 

	
 
			case VPM_FIX_HORIZONTAL:
 
				if (dx == -dy) {
 
					/* We are on a straight horizontal line. Determine the 'rail'
 
					 * to build based the sub tile location. */
 
					b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
 
				} else {
 
					/* We are not on a straight line. Determine the rail to build
 
					 * based on whether we are above or below it. */
 
					b = dx + dy >= (int)TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
 

	
 
					/* Calculate where a horizontal line through the start point and
 
					 * a vertical line from the selected end point intersect and
 
					 * use that point as the end point. */
 
					int offset = (raw_dx - raw_dy) / 2;
 
					x = thd->selstart.x - (offset & ~TILE_UNIT_MASK);
 
					y = thd->selstart.y + (offset & ~TILE_UNIT_MASK);
 

	
 
					/* 'Build' the last half rail tile if needed */
 
					if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
 
						if (dx + dy >= (int)TILE_SIZE) {
 
							x += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
 
						} else {
 
							y += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
 
						}
 
					}
 

	
 
					/* Make sure we do not overflow the map! */
 
					CheckUnderflow(x, y, 1);
 
					CheckUnderflow(y, x, 1);
 
					CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, 1);
 
					CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, 1);
 
					assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
 
				}
 
				break;
 

	
 
			case VPM_FIX_VERTICAL:
 
				if (dx == dy) {
 
					/* We are on a straight vertical line. Determine the 'rail'
 
					 * to build based the sub tile location. */
 
					b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
 
				} else {
 
					/* We are not on a straight line. Determine the rail to build
 
					 * based on whether we are left or right from it. */
 
					b = dx < dy ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
 

	
 
					/* Calculate where a vertical line through the start point and
 
					 * a horizontal line from the selected end point intersect and
 
					 * use that point as the end point. */
 
					int offset = (raw_dx + raw_dy + (int)TILE_SIZE) / 2;
 
					x = thd->selstart.x - (offset & ~TILE_UNIT_MASK);
 
					y = thd->selstart.y - (offset & ~TILE_UNIT_MASK);