Merge 1.9.0-beta2
This commit is contained in:
279
src/viewport.cpp
279
src/viewport.cpp
@@ -1,4 +1,4 @@
|
||||
/* $Id: viewport.cpp 27893 2017-08-13 18:38:42Z frosch $ */
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
@@ -58,7 +58,7 @@
|
||||
*
|
||||
*
|
||||
* Rows are horizontal sections of the viewport, also half a tile wide.
|
||||
* This time the nothern most tile on the map defines 0 and
|
||||
* This time the northern most tile on the map defines 0 and
|
||||
* everything south of that has a positive number.
|
||||
*/
|
||||
|
||||
@@ -87,6 +87,10 @@
|
||||
#include "linkgraph/linkgraph_gui.h"
|
||||
#include "viewport_sprite_sorter.h"
|
||||
#include "tunnelbridge_map.h"
|
||||
#include "company_base.h"
|
||||
#include "command_func.h"
|
||||
#include "network/network_func.h"
|
||||
#include "framerate_type.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
@@ -432,65 +436,27 @@ ViewPort *IsPtInWindowViewport(const Window *w, int x, int y)
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate screen coordinate in a viewport to a tile coordinate
|
||||
* Translate screen coordinate in a viewport to underlying tile coordinate.
|
||||
*
|
||||
* Returns exact point of the map that is visible in the given place
|
||||
* of the viewport (3D perspective), height of tiles and foundations matter.
|
||||
*
|
||||
* @param vp Viewport that contains the (\a x, \a y) screen coordinate
|
||||
* @param x Screen x coordinate
|
||||
* @param y Screen y coordinate
|
||||
* @param clamp_to_map Clamp the coordinate outside of the map to the closest tile within the map.
|
||||
* @return Tile coordinate
|
||||
* @param x Screen x coordinate, distance in pixels from the left edge of viewport frame
|
||||
* @param y Screen y coordinate, distance in pixels from the top edge of viewport frame
|
||||
* @param clamp_to_map Clamp the coordinate outside of the map to the closest, non-void tile within the map
|
||||
* @return Tile coordinate or (-1, -1) if given x or y is not within viewport frame
|
||||
*/
|
||||
Point TranslateXYToTileCoord(const ViewPort *vp, int x, int y, bool clamp_to_map)
|
||||
{
|
||||
Point pt;
|
||||
int a, b;
|
||||
int z;
|
||||
|
||||
if ( (uint)(x -= vp->left) >= (uint)vp->width ||
|
||||
(uint)(y -= vp->top) >= (uint)vp->height) {
|
||||
Point pt = {-1, -1};
|
||||
return pt;
|
||||
if (!IsInsideBS(x, vp->left, vp->width) || !IsInsideBS(y, vp->top, vp->height)) {
|
||||
Point pt = { -1, -1 };
|
||||
return pt;
|
||||
}
|
||||
|
||||
x = (ScaleByZoom(x, vp->zoom) + vp->virtual_left) >> (2 + ZOOM_LVL_SHIFT);
|
||||
y = (ScaleByZoom(y, vp->zoom) + vp->virtual_top) >> (1 + ZOOM_LVL_SHIFT);
|
||||
|
||||
a = y - x;
|
||||
b = y + x;
|
||||
|
||||
if (clamp_to_map) {
|
||||
/* Bring the coordinates near to a valid range. This is mostly due to the
|
||||
* tiles on the north side of the map possibly being drawn too high due to
|
||||
* the extra height levels. So at the top we allow a number of extra tiles.
|
||||
* This number is based on the tile height and pixels. */
|
||||
int extra_tiles = CeilDiv(_settings_game.construction.max_heightlevel * TILE_HEIGHT, TILE_PIXELS);
|
||||
a = Clamp(a, -extra_tiles * TILE_SIZE, MapMaxX() * TILE_SIZE - 1);
|
||||
b = Clamp(b, -extra_tiles * TILE_SIZE, 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 = GetSlopePixelZ(Clamp(a + max(z, 4) - 4, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + max(z, 4) - 4, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
|
||||
for (int malus = 3; malus > 0; malus--) z = GetSlopePixelZ(Clamp(a + max(z, malus) - malus, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + max(z, malus) - malus, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
|
||||
for (int i = 0; i < 5; i++) z = GetSlopePixelZ(Clamp(a + z, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + z, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
|
||||
|
||||
if (clamp_to_map) {
|
||||
pt.x = Clamp(a + z, min_coord, MapMaxX() * TILE_SIZE - 1);
|
||||
pt.y = Clamp(b + z, min_coord, MapMaxY() * TILE_SIZE - 1);
|
||||
} else {
|
||||
pt.x = a + z;
|
||||
pt.y = b + z;
|
||||
}
|
||||
|
||||
return pt;
|
||||
return InverseRemapCoords2(
|
||||
ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left,
|
||||
ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top, clamp_to_map);
|
||||
}
|
||||
|
||||
/* When used for zooming, check area below current coordinates (x,y)
|
||||
@@ -1699,6 +1665,8 @@ static inline void ViewportDraw(const ViewPort *vp, int left, int top, int right
|
||||
*/
|
||||
void Window::DrawViewport() const
|
||||
{
|
||||
PerformanceAccumulator framerate(PFE_DRAWWORLD);
|
||||
|
||||
DrawPixelInfo *dpi = _cur_dpi;
|
||||
|
||||
dpi->left += this->left;
|
||||
@@ -1711,121 +1679,33 @@ void Window::DrawViewport() const
|
||||
}
|
||||
|
||||
/**
|
||||
* Continue criteria for the SearchMapEdge function.
|
||||
* @param iter Value to check.
|
||||
* @param iter_limit Maximum value for the iter
|
||||
* @param sy Screen y coordinate calculated for the tile at hand
|
||||
* @param sy_limit Limit to the screen y coordinate
|
||||
* @return True when we should continue searching.
|
||||
* Ensure that a given viewport has a valid scroll position.
|
||||
*
|
||||
* There must be a visible piece of the map in the center of the viewport.
|
||||
* If there isn't, the viewport will be scrolled to nearest such location.
|
||||
*
|
||||
* @param vp The viewport.
|
||||
* @param[in,out] scroll_x Viewport X scroll.
|
||||
* @param[in,out] scroll_y Viewport Y scroll.
|
||||
*/
|
||||
typedef bool ContinueMapEdgeSearch(int iter, int iter_limit, int sy, int sy_limit);
|
||||
|
||||
/** Continue criteria for searching a no-longer-visible tile in negative direction, starting at some tile. */
|
||||
static inline bool ContinueLowerMapEdgeSearch(int iter, int iter_limit, int sy, int sy_limit) { return iter > 0 && sy > sy_limit; }
|
||||
/** Continue criteria for searching a no-longer-visible tile in positive direction, starting at some tile. */
|
||||
static inline bool ContinueUpperMapEdgeSearch(int iter, int iter_limit, int sy, int sy_limit) { return iter < iter_limit && sy < sy_limit; }
|
||||
|
||||
/**
|
||||
* Searches, starting at the given tile, by applying the given offset to iter, for a no longer visible tile.
|
||||
* The whole sense of this function is keeping the to-be-written code small, thus it is a little bit abstracted
|
||||
* so the same function can be used for both the X and Y locations. As such a reference to one of the elements
|
||||
* in curr_tile was needed.
|
||||
* @param curr_tile A tile
|
||||
* @param iter Reference to either the X or Y of curr_tile.
|
||||
* @param iter_limit Upper search limit for the iter value.
|
||||
* @param offset Search in steps of this size
|
||||
* @param sy_limit Search limit to be passed to the criteria
|
||||
* @param continue_criteria Search as long as this criteria is true
|
||||
* @return The final value of iter.
|
||||
*/
|
||||
static int SearchMapEdge(Point &curr_tile, int &iter, int iter_limit, int offset, int sy_limit, ContinueMapEdgeSearch continue_criteria)
|
||||
static inline void ClampViewportToMap(const ViewPort *vp, int *scroll_x, int *scroll_y)
|
||||
{
|
||||
int sy;
|
||||
do {
|
||||
iter = Clamp(iter + offset, 0, iter_limit);
|
||||
sy = GetViewportY(curr_tile);
|
||||
} while (continue_criteria(iter, iter_limit, sy, sy_limit));
|
||||
/* Centre of the viewport is hot spot. */
|
||||
Point pt = {
|
||||
*scroll_x + vp->virtual_width / 2,
|
||||
*scroll_y + vp->virtual_height / 2
|
||||
};
|
||||
|
||||
return iter;
|
||||
}
|
||||
/* Find nearest tile that is within borders of the map. */
|
||||
bool clamped;
|
||||
pt = InverseRemapCoords2(pt.x, pt.y, true, &clamped);
|
||||
|
||||
/**
|
||||
* Determine the clamping of either the X or Y coordinate to the map.
|
||||
* @param curr_tile A tile
|
||||
* @param iter Reference to either the X or Y of curr_tile.
|
||||
* @param iter_limit Upper search limit for the iter value.
|
||||
* @param start Start value for the iteration.
|
||||
* @param other_ref Reference to the opposite axis in curr_tile than of iter.
|
||||
* @param other_value Start value for of the opposite axis
|
||||
* @param vp_value Value of the viewport location in the opposite axis as for iter.
|
||||
* @param other_limit Limit for the other value, so if iter is X, then other_limit is for Y.
|
||||
* @param vp_top Top of the viewport.
|
||||
* @param vp_bottom Bottom of the viewport.
|
||||
* @return Clamped version of vp_value.
|
||||
*/
|
||||
static inline int ClampXYToMap(Point &curr_tile, int &iter, int iter_limit, int start, int &other_ref, int other_value, int vp_value, int other_limit, int vp_top, int vp_bottom)
|
||||
{
|
||||
bool upper_edge = other_value < _settings_game.construction.max_heightlevel / 4;
|
||||
|
||||
/*
|
||||
* First get an estimate of the tiles relevant for us at that edge. Relevant in the sense
|
||||
* "at least close to the visible area". Thus, we don't look at exactly each tile, inspecting
|
||||
* e.g. every tenth should be enough. After all, the desired screen limit is set such that
|
||||
* the bordermost tiles are painted in the middle of the screen when one hits the limit,
|
||||
* i.e. it is no harm if there is some small error in that calculation
|
||||
*/
|
||||
|
||||
other_ref = upper_edge ? 0 : other_limit;
|
||||
iter = start;
|
||||
int min_iter = SearchMapEdge(curr_tile, iter, iter_limit, upper_edge ? -10 : +10, vp_top, upper_edge ? ContinueLowerMapEdgeSearch : ContinueUpperMapEdgeSearch);
|
||||
iter = start;
|
||||
int max_iter = SearchMapEdge(curr_tile, iter, iter_limit, upper_edge ? +10 : -10, vp_bottom, upper_edge ? ContinueUpperMapEdgeSearch : ContinueLowerMapEdgeSearch);
|
||||
|
||||
max_iter = min(max_iter + _settings_game.construction.max_heightlevel / 4, iter_limit);
|
||||
min_iter = min(min_iter, max_iter);
|
||||
|
||||
/* Now, calculate the highest heightlevel of these tiles. Again just as an estimate. */
|
||||
int max_heightlevel_at_edge = 0;
|
||||
for (iter = min_iter; iter <= max_iter; iter += 10) {
|
||||
max_heightlevel_at_edge = max(max_heightlevel_at_edge, (int)TileHeight(TileXY(curr_tile.x, curr_tile.y)));
|
||||
if (clamped) {
|
||||
/* Convert back to viewport coordinates and remove centering. */
|
||||
pt = RemapCoords2(pt.x, pt.y);
|
||||
*scroll_x = pt.x - vp->virtual_width / 2;
|
||||
*scroll_y = pt.y - vp->virtual_height / 2;
|
||||
}
|
||||
|
||||
/* Based on that heightlevel, calculate the limit. For the upper edge a tile with height zero would
|
||||
* get a limit of zero, on the other side it depends on the number of tiles along the axis. */
|
||||
return upper_edge ?
|
||||
max(vp_value, -max_heightlevel_at_edge * (int)(TILE_HEIGHT * 2 * ZOOM_LVL_BASE)) :
|
||||
min(vp_value, (other_limit * TILE_SIZE * 4 - max_heightlevel_at_edge * TILE_HEIGHT * 2) * ZOOM_LVL_BASE);
|
||||
}
|
||||
|
||||
static inline void ClampViewportToMap(const ViewPort *vp, int &x, int &y)
|
||||
{
|
||||
int original_y = 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;
|
||||
|
||||
/* Find out which tile corresponds to (vx,vy) if one assumes height zero. The cast is necessary to prevent C++ from
|
||||
* converting the result to an uint, which gives an overflow instead of a negative result... */
|
||||
int tx = vx / (int)(TILE_SIZE * 4 * ZOOM_LVL_BASE);
|
||||
int ty = vy / (int)(TILE_SIZE * 4 * ZOOM_LVL_BASE);
|
||||
|
||||
Point curr_tile;
|
||||
vx = ClampXYToMap(curr_tile, curr_tile.y, MapMaxY(), ty, curr_tile.x, tx, vx, MapMaxX(), original_y, original_y + vp->virtual_height);
|
||||
vy = ClampXYToMap(curr_tile, curr_tile.x, MapMaxX(), tx, curr_tile.y, ty, vy, MapMaxY(), original_y, original_y + vp->virtual_height);
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1845,7 +1725,7 @@ void UpdateViewportPosition(Window *w)
|
||||
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);
|
||||
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;
|
||||
@@ -1855,8 +1735,8 @@ void UpdateViewportPosition(Window *w)
|
||||
if (_settings_client.gui.smooth_scroll) {
|
||||
int max_scroll = ScaleByMapSize1D(512 * ZOOM_LVL_BASE);
|
||||
/* 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);
|
||||
w->viewport->scrollpos_x += Clamp(DivAwayFromZero(delta_x, 4), -max_scroll, max_scroll);
|
||||
w->viewport->scrollpos_y += Clamp(DivAwayFromZero(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;
|
||||
@@ -1865,7 +1745,7 @@ void UpdateViewportPosition(Window *w)
|
||||
w->viewport->scrollpos_y == w->viewport->dest_scrollpos_y);
|
||||
}
|
||||
|
||||
ClampViewportToMap(vp, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
|
||||
ClampViewportToMap(vp, &w->viewport->scrollpos_x, &w->viewport->scrollpos_y);
|
||||
|
||||
SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
|
||||
if (update_overlay) RebuildViewportOverlay(w);
|
||||
@@ -1947,11 +1827,12 @@ void ConstrainAllViewportsZoom()
|
||||
* Mark a tile given by its index dirty for repaint.
|
||||
* @param tile The tile to mark dirty.
|
||||
* @param bridge_level_offset Height of bridge on tile to also mark dirty. (Height level relative to north corner.)
|
||||
* @param tile_height_override Height of the tile (#TileHeight).
|
||||
* @ingroup dirty
|
||||
*/
|
||||
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset)
|
||||
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
|
||||
{
|
||||
Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, TilePixelHeight(tile));
|
||||
Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, tile_height_override * TILE_HEIGHT);
|
||||
MarkAllViewportsDirty(
|
||||
pt.x - MAX_TILE_EXTENT_LEFT,
|
||||
pt.y - MAX_TILE_EXTENT_TOP - ZOOM_LVL_BASE * TILE_HEIGHT * bridge_level_offset,
|
||||
@@ -1959,22 +1840,6 @@ void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset)
|
||||
pt.y + MAX_TILE_EXTENT_BOTTOM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a (virtual) tile outside the map dirty for repaint.
|
||||
* @param x Tile X position.
|
||||
* @param y Tile Y position.
|
||||
* @ingroup dirty
|
||||
*/
|
||||
void MarkTileDirtyByTileOutsideMap(int x, int y)
|
||||
{
|
||||
Point pt = RemapCoords(x * TILE_SIZE, y * TILE_SIZE, TilePixelHeightOutsideMap(x, y));
|
||||
MarkAllViewportsDirty(
|
||||
pt.x - MAX_TILE_EXTENT_LEFT,
|
||||
pt.y, // no buildings outside of map
|
||||
pt.x + MAX_TILE_EXTENT_RIGHT,
|
||||
pt.y + MAX_TILE_EXTENT_BOTTOM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the selected tiles as dirty.
|
||||
*
|
||||
@@ -2552,7 +2417,6 @@ void UpdateTileSelection()
|
||||
break;
|
||||
default:
|
||||
NOT_REACHED();
|
||||
break;
|
||||
}
|
||||
_thd.new_pos.x = x1 & ~TILE_UNIT_MASK;
|
||||
_thd.new_pos.y = y1 & ~TILE_UNIT_MASK;
|
||||
@@ -2769,8 +2633,8 @@ static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_t
|
||||
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]));
|
||||
FALLTHROUGH;
|
||||
}
|
||||
FALLTHROUGH;
|
||||
|
||||
case HT_POINT:
|
||||
h0 = TileHeight(start_tile);
|
||||
@@ -3670,6 +3534,43 @@ void InitializeSpriteSorter()
|
||||
assert(_vp_sprite_sorter != NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scroll players main viewport.
|
||||
* @param tile tile to center viewport on
|
||||
* @param flags type of operation
|
||||
* @param p1 ViewportScrollTarget of scroll target
|
||||
* @param p2 company or client id depending on the target
|
||||
* @param text unused
|
||||
* @return the cost of this operation or an error
|
||||
*/
|
||||
CommandCost CmdScrollViewport(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
||||
{
|
||||
if (_current_company != OWNER_DEITY) return CMD_ERROR;
|
||||
ViewportScrollTarget target = (ViewportScrollTarget)p1;
|
||||
switch (target) {
|
||||
case VST_EVERYONE:
|
||||
break;
|
||||
case VST_COMPANY:
|
||||
if (_local_company != (CompanyID)p2) return CommandCost();
|
||||
break;
|
||||
case VST_CLIENT:
|
||||
#ifdef ENABLE_NETWORK
|
||||
if (_network_own_client_id != (ClientID)p2) return CommandCost();
|
||||
break;
|
||||
#else
|
||||
return CommandCost();
|
||||
#endif
|
||||
default:
|
||||
return CMD_ERROR;
|
||||
}
|
||||
|
||||
if (flags & DC_EXEC) {
|
||||
ResetObjectToPlace();
|
||||
ScrollMainWindowToTile(tile);
|
||||
}
|
||||
return CommandCost();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a rail snapping point based on a spot where a rail tracks segment
|
||||
* has been placed.
|
||||
|
||||
Reference in New Issue
Block a user