OpenTTD
viewport.cpp
Go to the documentation of this file.
1 /* $Id: viewport.cpp 27020 2014-10-15 18:31:37Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
71 #include "stdafx.h"
72 #include "landscape.h"
73 #include "viewport_func.h"
74 #include "station_base.h"
75 #include "waypoint_base.h"
76 #include "town.h"
77 #include "signs_base.h"
78 #include "signs_func.h"
79 #include "vehicle_base.h"
80 #include "vehicle_gui.h"
81 #include "blitter/factory.hpp"
82 #include "strings_func.h"
83 #include "zoom_func.h"
84 #include "vehicle_func.h"
85 #include "company_func.h"
86 #include "waypoint_func.h"
87 #include "window_func.h"
88 #include "tilehighlight_func.h"
89 #include "window_gui.h"
91 #include "viewport_sprite_sorter.h"
92 #include "bridge_map.h"
93 
94 #include <map>
95 
96 #include "table/strings.h"
97 #include "table/palettes.h"
98 
99 #include "safeguards.h"
100 
101 Point _tile_fract_coords;
102 
104  StringID string;
105  Colours colour;
106  int32 x;
107  int32 y;
108  uint64 params[2];
109  uint16 width;
110 };
111 
113  SpriteID image;
114  PaletteID pal;
115  const SubSprite *sub;
116  int32 x;
117  int32 y;
118 };
119 
121  SpriteID image;
122  PaletteID pal;
123  const SubSprite *sub;
124  int32 x;
125  int32 y;
126  int next;
127 };
128 
134  FOUNDATION_PART_END
135 };
136 
145 };
146 
151 
154  DrawPixelInfo dpi;
155 
156  StringSpriteToDrawVector string_sprites_to_draw;
157  TileSpriteToDrawVector tile_sprites_to_draw;
158  ParentSpriteToDrawVector parent_sprites_to_draw;
160  ChildScreenSpriteToDrawVector child_screen_sprites_to_draw;
161 
162  int *last_child;
163 
165 
166  int foundation[FOUNDATION_PART_END];
168  int *last_foundation_child[FOUNDATION_PART_END];
169  Point foundation_offset[FOUNDATION_PART_END];
170 };
171 
172 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom);
173 
174 static ViewportDrawer _vd;
175 
176 TileHighlightData _thd;
177 static TileInfo *_cur_ti;
178 bool _draw_bounding_boxes = false;
179 bool _draw_dirty_blocks = false;
180 uint _dirty_block_colour = 0;
181 static VpSpriteSorter _vp_sprite_sorter = NULL;
182 
183 static Point MapXYZToViewport(const ViewPort *vp, int x, int y, int z)
184 {
185  Point p = RemapCoords(x, y, z);
186  p.x -= vp->virtual_width / 2;
187  p.y -= vp->virtual_height / 2;
188  return p;
189 }
190 
191 void DeleteWindowViewport(Window *w)
192 {
193  if (w->viewport == NULL) return;
194 
195  delete w->viewport->overlay;
196  free(w->viewport);
197  w->viewport = NULL;
198 }
199 
212 void InitializeWindowViewport(Window *w, int x, int y,
213  int width, int height, uint32 follow_flags, ZoomLevel zoom)
214 {
215  assert(w->viewport == NULL);
216 
217  ViewportData *vp = CallocT<ViewportData>(1);
218 
219  vp->left = x + w->left;
220  vp->top = y + w->top;
221  vp->width = width;
222  vp->height = height;
223 
225 
226  vp->virtual_width = ScaleByZoom(width, zoom);
227  vp->virtual_height = ScaleByZoom(height, zoom);
228 
229  Point pt;
230 
231  if (follow_flags & 0x80000000) {
232  const Vehicle *veh;
233 
234  vp->follow_vehicle = (VehicleID)(follow_flags & 0xFFFFF);
235  veh = Vehicle::Get(vp->follow_vehicle);
236  pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
237  } else {
238  uint x = TileX(follow_flags) * TILE_SIZE;
239  uint y = TileY(follow_flags) * TILE_SIZE;
240 
242  pt = MapXYZToViewport(vp, x, y, GetSlopePixelZ(x, y));
243  }
244 
245  vp->scrollpos_x = pt.x;
246  vp->scrollpos_y = pt.y;
247  vp->dest_scrollpos_x = pt.x;
248  vp->dest_scrollpos_y = pt.y;
249 
250  vp->overlay = NULL;
251 
252  w->viewport = vp;
253  vp->virtual_left = 0; // pt.x;
254  vp->virtual_top = 0; // pt.y;
255 }
256 
257 static Point _vp_move_offs;
258 
259 static void DoSetViewportPosition(const Window *w, int left, int top, int width, int height)
260 {
262  if (left + width > w->left &&
263  w->left + w->width > left &&
264  top + height > w->top &&
265  w->top + w->height > top) {
266 
267  if (left < w->left) {
268  DoSetViewportPosition(w, left, top, w->left - left, height);
269  DoSetViewportPosition(w, left + (w->left - left), top, width - (w->left - left), height);
270  return;
271  }
272 
273  if (left + width > w->left + w->width) {
274  DoSetViewportPosition(w, left, top, (w->left + w->width - left), height);
275  DoSetViewportPosition(w, left + (w->left + w->width - left), top, width - (w->left + w->width - left), height);
276  return;
277  }
278 
279  if (top < w->top) {
280  DoSetViewportPosition(w, left, top, width, (w->top - top));
281  DoSetViewportPosition(w, left, top + (w->top - top), width, height - (w->top - top));
282  return;
283  }
284 
285  if (top + height > w->top + w->height) {
286  DoSetViewportPosition(w, left, top, width, (w->top + w->height - top));
287  DoSetViewportPosition(w, left, top + (w->top + w->height - top), width, height - (w->top + w->height - top));
288  return;
289  }
290 
291  return;
292  }
293  }
294 
295  {
296  int xo = _vp_move_offs.x;
297  int yo = _vp_move_offs.y;
298 
299  if (abs(xo) >= width || abs(yo) >= height) {
300  /* fully_outside */
301  RedrawScreenRect(left, top, left + width, top + height);
302  return;
303  }
304 
305  GfxScroll(left, top, width, height, xo, yo);
306 
307  if (xo > 0) {
308  RedrawScreenRect(left, top, xo + left, top + height);
309  left += xo;
310  width -= xo;
311  } else if (xo < 0) {
312  RedrawScreenRect(left + width + xo, top, left + width, top + height);
313  width += xo;
314  }
315 
316  if (yo > 0) {
317  RedrawScreenRect(left, top, width + left, top + yo);
318  } else if (yo < 0) {
319  RedrawScreenRect(left, top + height + yo, width + left, top + height);
320  }
321  }
322 }
323 
324 static void SetViewportPosition(Window *w, int x, int y)
325 {
326  ViewPort *vp = w->viewport;
327  int old_left = vp->virtual_left;
328  int old_top = vp->virtual_top;
329  int i;
330  int left, top, width, height;
331 
332  vp->virtual_left = x;
333  vp->virtual_top = y;
334 
335  /* Viewport is bound to its left top corner, so it must be rounded down (UnScaleByZoomLower)
336  * else glitch described in FS#1412 will happen (offset by 1 pixel with zoom level > NORMAL)
337  */
338  old_left = UnScaleByZoomLower(old_left, vp->zoom);
339  old_top = UnScaleByZoomLower(old_top, vp->zoom);
340  x = UnScaleByZoomLower(x, vp->zoom);
341  y = UnScaleByZoomLower(y, vp->zoom);
342 
343  old_left -= x;
344  old_top -= y;
345 
346  if (old_top == 0 && old_left == 0) return;
347 
348  _vp_move_offs.x = old_left;
349  _vp_move_offs.y = old_top;
350 
351  left = vp->left;
352  top = vp->top;
353  width = vp->width;
354  height = vp->height;
355 
356  if (left < 0) {
357  width += left;
358  left = 0;
359  }
360 
361  i = left + width - _screen.width;
362  if (i >= 0) width -= i;
363 
364  if (width > 0) {
365  if (top < 0) {
366  height += top;
367  top = 0;
368  }
369 
370  i = top + height - _screen.height;
371  if (i >= 0) height -= i;
372 
373  if (height > 0) DoSetViewportPosition(w->z_front, left, top, width, height);
374  }
375 }
376 
385 ViewPort *IsPtInWindowViewport(const Window *w, int x, int y)
386 {
387  ViewPort *vp = w->viewport;
388 
389  if (vp != NULL &&
390  IsInsideMM(x, vp->left, vp->left + vp->width) &&
391  IsInsideMM(y, vp->top, vp->top + vp->height))
392  return vp;
393 
394  return NULL;
395 }
396 
404 static Point TranslateXYToTileCoord(const ViewPort *vp, int x, int y)
405 {
406  Point pt;
407  int a, b;
408  int z;
409 
410  if ( (uint)(x -= vp->left) >= (uint)vp->width ||
411  (uint)(y -= vp->top) >= (uint)vp->height) {
412  Point pt = {-1, -1};
413  return pt;
414  }
415 
416  x = (ScaleByZoom(x, vp->zoom) + vp->virtual_left) >> (2 + ZOOM_LVL_SHIFT);
417  y = (ScaleByZoom(y, vp->zoom) + vp->virtual_top) >> (1 + ZOOM_LVL_SHIFT);
418 
419  a = y - x;
420  b = y + x;
421 
422  /* Bring the coordinates near to a valid range. This is mostly due to the
423  * tiles on the north side of the map possibly being drawn too high due to
424  * the extra height levels. So at the top we allow a number of extra tiles.
425  * This number is based on the tile height and pixels. */
427  a = Clamp(a, -extra_tiles * TILE_SIZE, MapMaxX() * TILE_SIZE - 1);
428  b = Clamp(b, -extra_tiles * TILE_SIZE, MapMaxY() * TILE_SIZE - 1);
429 
430  /* (a, b) is the X/Y-world coordinate that belongs to (x,y) if the landscape would be completely flat on height 0.
431  * Now find the Z-world coordinate by fix point iteration.
432  * This is a bit tricky because the tile height is non-continuous at foundations.
433  * The clicked point should be approached from the back, otherwise there are regions that are not clickable.
434  * (FOUNDATION_HALFTILE_LOWER on SLOPE_STEEP_S hides north halftile completely)
435  * So give it a z-malus of 4 in the first iterations.
436  */
437  z = 0;
438 
439  int min_coord = _settings_game.construction.freeform_edges ? TILE_SIZE : 0;
440 
441  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;
442  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;
443  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;
444 
445  pt.x = Clamp(a + z, min_coord, MapMaxX() * TILE_SIZE - 1);
446  pt.y = Clamp(b + z, min_coord, MapMaxY() * TILE_SIZE - 1);
447 
448  return pt;
449 }
450 
451 /* When used for zooming, check area below current coordinates (x,y)
452  * and return the tile of the zoomed out/in position (zoom_x, zoom_y)
453  * when you just want the tile, make x = zoom_x and y = zoom_y */
454 static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y)
455 {
456  Window *w;
457  ViewPort *vp;
458  Point pt;
459 
460  if ( (w = FindWindowFromPt(x, y)) != NULL &&
461  (vp = IsPtInWindowViewport(w, x, y)) != NULL)
462  return TranslateXYToTileCoord(vp, zoom_x, zoom_y);
463 
464  pt.y = pt.x = -1;
465  return pt;
466 }
467 
468 Point GetTileBelowCursor()
469 {
470  return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y);
471 }
472 
473 
474 Point GetTileZoomCenterWindow(bool in, Window * w)
475 {
476  int x, y;
477  ViewPort *vp = w->viewport;
478 
479  if (in) {
480  x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2);
481  y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2);
482  } else {
483  x = vp->width - (_cursor.pos.x - vp->left);
484  y = vp->height - (_cursor.pos.y - vp->top);
485  }
486  /* Get the tile below the cursor and center on the zoomed-out center */
487  return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top);
488 }
489 
498 void HandleZoomMessage(Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out)
499 {
500  w->SetWidgetDisabledState(widget_zoom_in, vp->zoom <= _settings_client.gui.zoom_min);
501  w->SetWidgetDirty(widget_zoom_in);
502 
503  w->SetWidgetDisabledState(widget_zoom_out, vp->zoom >= _settings_client.gui.zoom_max);
504  w->SetWidgetDirty(widget_zoom_out);
505 }
506 
519 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)
520 {
521  assert((image & SPRITE_MASK) < MAX_SPRITES);
522 
523  TileSpriteToDraw *ts = _vd.tile_sprites_to_draw.Append();
524  ts->image = image;
525  ts->pal = pal;
526  ts->sub = sub;
527  Point pt = RemapCoords(x, y, z);
528  ts->x = pt.x + extra_offs_x;
529  ts->y = pt.y + extra_offs_y;
530 }
531 
544 static void AddChildSpriteToFoundation(SpriteID image, PaletteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y)
545 {
546  assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END));
547  assert(_vd.foundation[foundation_part] != -1);
548  Point offs = _vd.foundation_offset[foundation_part];
549 
550  /* Change the active ChildSprite list to the one of the foundation */
551  int *old_child = _vd.last_child;
552  _vd.last_child = _vd.last_foundation_child[foundation_part];
553 
554  AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub, false);
555 
556  /* Switch back to last ChildSprite list */
557  _vd.last_child = old_child;
558 }
559 
573 void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
574 {
575  /* Switch to first foundation part, if no foundation was drawn */
577 
578  if (_vd.foundation[_vd.foundation_part] != -1) {
579  Point pt = RemapCoords(x, y, z);
580  AddChildSpriteToFoundation(image, pal, sub, _vd.foundation_part, pt.x + extra_offs_x * ZOOM_LVL_BASE, pt.y + extra_offs_y * ZOOM_LVL_BASE);
581  } else {
582  AddTileSpriteToDraw(image, pal, _cur_ti->x + x, _cur_ti->y + y, _cur_ti->z + z, sub, extra_offs_x * ZOOM_LVL_BASE, extra_offs_y * ZOOM_LVL_BASE);
583  }
584 }
585 
596 void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
597 {
598  DrawGroundSpriteAt(image, pal, 0, 0, 0, sub, extra_offs_x, extra_offs_y);
599 }
600 
608 void OffsetGroundSprite(int x, int y)
609 {
610  /* Switch to next foundation part */
611  switch (_vd.foundation_part) {
614  break;
617  break;
618  default: NOT_REACHED();
619  }
620 
621  /* _vd.last_child == NULL if foundation sprite was clipped by the viewport bounds */
622  if (_vd.last_child != NULL) _vd.foundation[_vd.foundation_part] = _vd.parent_sprites_to_draw.Length() - 1;
623 
624  _vd.foundation_offset[_vd.foundation_part].x = x * ZOOM_LVL_BASE;
625  _vd.foundation_offset[_vd.foundation_part].y = y * ZOOM_LVL_BASE;
626  _vd.last_foundation_child[_vd.foundation_part] = _vd.last_child;
627 }
628 
640 static void AddCombinedSprite(SpriteID image, PaletteID pal, int x, int y, int z, const SubSprite *sub)
641 {
642  Point pt = RemapCoords(x, y, z);
643  const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
644 
645  if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width ||
646  pt.x + spr->x_offs + spr->width <= _vd.dpi.left ||
647  pt.y + spr->y_offs >= _vd.dpi.top + _vd.dpi.height ||
648  pt.y + spr->y_offs + spr->height <= _vd.dpi.top)
649  return;
650 
651  const ParentSpriteToDraw *pstd = _vd.parent_sprites_to_draw.End() - 1;
652  AddChildSpriteScreen(image, pal, pt.x - pstd->left, pt.y - pstd->top, false, sub, false);
653 }
654 
680 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)
681 {
682  int32 left, right, top, bottom;
683 
684  assert((image & SPRITE_MASK) < MAX_SPRITES);
685 
686  /* make the sprites transparent with the right palette */
687  if (transparent) {
690  }
691 
693  AddCombinedSprite(image, pal, x, y, z, sub);
694  return;
695  }
696 
697  _vd.last_child = NULL;
698 
699  Point pt = RemapCoords(x, y, z);
700  int tmp_left, tmp_top, tmp_x = pt.x, tmp_y = pt.y;
701 
702  /* Compute screen extents of sprite */
703  if (image == SPR_EMPTY_BOUNDING_BOX) {
704  left = tmp_left = RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x;
705  right = RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1;
706  top = tmp_top = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y;
707  bottom = RemapCoords(x + w , y + h , z + bb_offset_z).y + 1;
708  } else {
709  const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
710  left = tmp_left = (pt.x += spr->x_offs);
711  right = (pt.x + spr->width );
712  top = tmp_top = (pt.y += spr->y_offs);
713  bottom = (pt.y + spr->height);
714  }
715 
716  if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) {
717  /* Compute maximal extents of sprite and its bounding box */
718  left = min(left , RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x);
719  right = max(right , RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1);
720  top = min(top , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y);
721  bottom = max(bottom, RemapCoords(x + w , y + h , z + bb_offset_z).y + 1);
722  }
723 
724  /* Do not add the sprite to the viewport, if it is outside */
725  if (left >= _vd.dpi.left + _vd.dpi.width ||
726  right <= _vd.dpi.left ||
727  top >= _vd.dpi.top + _vd.dpi.height ||
728  bottom <= _vd.dpi.top) {
729  return;
730  }
731 
732  ParentSpriteToDraw *ps = _vd.parent_sprites_to_draw.Append();
733  ps->x = tmp_x;
734  ps->y = tmp_y;
735 
736  ps->left = tmp_left;
737  ps->top = tmp_top;
738 
739  ps->image = image;
740  ps->pal = pal;
741  ps->sub = sub;
742  ps->xmin = x + bb_offset_x;
743  ps->xmax = x + max(bb_offset_x, w) - 1;
744 
745  ps->ymin = y + bb_offset_y;
746  ps->ymax = y + max(bb_offset_y, h) - 1;
747 
748  ps->zmin = z + bb_offset_z;
749  ps->zmax = z + max(bb_offset_z, dz) - 1;
750 
751  ps->comparison_done = false;
752  ps->first_child = -1;
753 
754  _vd.last_child = &ps->first_child;
755 
757 }
758 
778 {
779  assert(_vd.combine_sprites == SPRITE_COMBINE_NONE);
781 }
782 
788 {
789  assert(_vd.combine_sprites != SPRITE_COMBINE_NONE);
791 }
792 
802 static bool IsInRangeInclusive(int begin, int end, int check)
803 {
804  if (begin > end) Swap(begin, end);
805  return begin <= check && check <= end;
806 }
807 
814 bool IsInsideRotatedRectangle(int x, int y)
815 {
816  int dist_a = (_thd.size.x + _thd.size.y); // Rotated coordinate system for selected rectangle.
817  int dist_b = (_thd.size.x - _thd.size.y); // We don't have to divide by 2. It's all relative!
818  int a = ((x - _thd.pos.x) + (y - _thd.pos.y)); // Rotated coordinate system for the point under scrutiny.
819  int b = ((x - _thd.pos.x) - (y - _thd.pos.y));
820 
821  /* Check if a and b are between 0 and dist_a or dist_b respectively. */
822  return IsInRangeInclusive(dist_a, 0, a) && IsInRangeInclusive(dist_b, 0, b);
823 }
824 
835 void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale)
836 {
837  assert((image & SPRITE_MASK) < MAX_SPRITES);
838 
839  /* If the ParentSprite was clipped by the viewport bounds, do not draw the ChildSprites either */
840  if (_vd.last_child == NULL) return;
841 
842  /* make the sprites transparent with the right palette */
843  if (transparent) {
846  }
847 
848  *_vd.last_child = _vd.child_screen_sprites_to_draw.Length();
849 
850  ChildScreenSpriteToDraw *cs = _vd.child_screen_sprites_to_draw.Append();
851  cs->image = image;
852  cs->pal = pal;
853  cs->sub = sub;
854  cs->x = scale ? x * ZOOM_LVL_BASE : x;
855  cs->y = scale ? y * ZOOM_LVL_BASE : y;
856  cs->next = -1;
857 
858  /* Append the sprite to the active ChildSprite list.
859  * If the active ParentSprite is a foundation, update last_foundation_child as well.
860  * Note: ChildSprites of foundations are NOT sequential in the vector, as selection sprites are added at last. */
861  if (_vd.last_foundation_child[0] == _vd.last_child) _vd.last_foundation_child[0] = &cs->next;
862  if (_vd.last_foundation_child[1] == _vd.last_child) _vd.last_foundation_child[1] = &cs->next;
863  _vd.last_child = &cs->next;
864 }
865 
866 static void AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2, Colours colour, uint16 width)
867 {
868  assert(width != 0);
869  StringSpriteToDraw *ss = _vd.string_sprites_to_draw.Append();
870  ss->string = string;
871  ss->x = x;
872  ss->y = y;
873  ss->params[0] = params_1;
874  ss->params[1] = params_2;
875  ss->width = width;
876  ss->colour = colour;
877 }
878 
879 
891 static void DrawSelectionSprite(SpriteID image, PaletteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part)
892 {
893  /* FIXME: This is not totally valid for some autorail highlights that extend over the edges of the tile. */
894  if (_vd.foundation[foundation_part] == -1) {
895  /* draw on real ground */
896  AddTileSpriteToDraw(image, pal, ti->x, ti->y, ti->z + z_offset);
897  } else {
898  /* draw on top of foundation */
899  AddChildSpriteToFoundation(image, pal, NULL, foundation_part, 0, -z_offset * ZOOM_LVL_BASE);
900  }
901 }
902 
909 static void DrawTileSelectionRect(const TileInfo *ti, PaletteID pal)
910 {
911  if (!IsValidTile(ti->tile)) return;
912 
913  SpriteID sel;
914  if (IsHalftileSlope(ti->tileh)) {
915  Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
916  SpriteID sel2 = SPR_HALFTILE_SELECTION_FLAT + halftile_corner;
918 
919  Corner opposite_corner = OppositeCorner(halftile_corner);
920  if (IsSteepSlope(ti->tileh)) {
921  sel = SPR_HALFTILE_SELECTION_DOWN;
922  } else {
923  sel = ((ti->tileh & SlopeWithOneCornerRaised(opposite_corner)) != 0 ? SPR_HALFTILE_SELECTION_UP : SPR_HALFTILE_SELECTION_FLAT);
924  }
925  sel += opposite_corner;
926  } else {
927  sel = SPR_SELECT_TILE + SlopeToSpriteOffset(ti->tileh);
928  }
930 }
931 
932 static bool IsPartOfAutoLine(int px, int py)
933 {
934  px -= _thd.selstart.x;
935  py -= _thd.selstart.y;
936 
937  if ((_thd.drawstyle & HT_DRAG_MASK) != HT_LINE) return false;
938 
939  switch (_thd.drawstyle & HT_DIR_MASK) {
940  case HT_DIR_X: return py == 0; // x direction
941  case HT_DIR_Y: return px == 0; // y direction
942  case HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper
943  case HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower
944  case HT_DIR_VL: return px == py || px == py + 16; // vertical left
945  case HT_DIR_VR: return px == py || px == py - 16; // vertical right
946  default:
947  NOT_REACHED();
948  }
949 }
950 
951 /* [direction][side] */
952 static const HighLightStyle _autorail_type[6][2] = {
953  { HT_DIR_X, HT_DIR_X },
954  { HT_DIR_Y, HT_DIR_Y },
955  { HT_DIR_HU, HT_DIR_HL },
956  { HT_DIR_HL, HT_DIR_HU },
957  { HT_DIR_VL, HT_DIR_VR },
958  { HT_DIR_VR, HT_DIR_VL }
959 };
960 
961 #include "table/autorail.h"
962 
969 static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type)
970 {
971  SpriteID image;
972  PaletteID pal;
973  int offset;
974 
975  FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
976  Slope autorail_tileh = RemoveHalftileSlope(ti->tileh);
977  if (IsHalftileSlope(ti->tileh)) {
978  static const uint _lower_rail[4] = { 5U, 2U, 4U, 3U };
979  Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
980  if (autorail_type != _lower_rail[halftile_corner]) {
981  foundation_part = FOUNDATION_PART_HALFTILE;
982  /* Here we draw the highlights of the "three-corners-raised"-slope. That looks ok to me. */
983  autorail_tileh = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
984  }
985  }
986 
987  offset = _AutorailTilehSprite[autorail_tileh][autorail_type];
988  if (offset >= 0) {
989  image = SPR_AUTORAIL_BASE + offset;
990  pal = PAL_NONE;
991  } else {
992  image = SPR_AUTORAIL_BASE - offset;
993  pal = PALETTE_SEL_TILE_RED;
994  }
995 
996  DrawSelectionSprite(image, _thd.make_square_red ? PALETTE_SEL_TILE_RED : pal, ti, 7, foundation_part);
997 }
998 
1003 static void DrawTileSelection(const TileInfo *ti)
1004 {
1005  /* Draw a red error square? */
1006  bool is_redsq = _thd.redsq == ti->tile;
1008 
1009  /* No tile selection active? */
1010  if ((_thd.drawstyle & HT_DRAG_MASK) == HT_NONE) return;
1011 
1012  if (_thd.diagonal) { // We're drawing a 45 degrees rotated (diagonal) rectangle
1013  if (IsInsideRotatedRectangle((int)ti->x, (int)ti->y)) goto draw_inner;
1014  return;
1015  }
1016 
1017  /* Inside the inner area? */
1018  if (IsInsideBS(ti->x, _thd.pos.x, _thd.size.x) &&
1019  IsInsideBS(ti->y, _thd.pos.y, _thd.size.y)) {
1020 draw_inner:
1021  if (_thd.drawstyle & HT_RECT) {
1022  if (!is_redsq) DrawTileSelectionRect(ti, _thd.make_square_red ? PALETTE_SEL_TILE_RED : PAL_NONE);
1023  } else if (_thd.drawstyle & HT_POINT) {
1024  /* Figure out the Z coordinate for the single dot. */
1025  int z = 0;
1026  FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
1027  if (ti->tileh & SLOPE_N) {
1028  z += TILE_HEIGHT;
1030  }
1031  if (IsHalftileSlope(ti->tileh)) {
1032  Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
1033  if ((halftile_corner == CORNER_W) || (halftile_corner == CORNER_E)) z += TILE_HEIGHT;
1034  if (halftile_corner != CORNER_S) {
1035  foundation_part = FOUNDATION_PART_HALFTILE;
1036  if (IsSteepSlope(ti->tileh)) z -= TILE_HEIGHT;
1037  }
1038  }
1039  DrawSelectionSprite(_cur_dpi->zoom <= ZOOM_LVL_DETAIL ? SPR_DOT : SPR_DOT_SMALL, PAL_NONE, ti, z, foundation_part);
1040  } else if (_thd.drawstyle & HT_RAIL) {
1041  /* autorail highlight piece under cursor */
1042  HighLightStyle type = _thd.drawstyle & HT_DIR_MASK;
1043  assert(type < HT_DIR_END);
1044  DrawAutorailSelection(ti, _autorail_type[type][0]);
1045  } else if (IsPartOfAutoLine(ti->x, ti->y)) {
1046  /* autorail highlighting long line */
1047  HighLightStyle dir = _thd.drawstyle & HT_DIR_MASK;
1048  uint side;
1049 
1050  if (dir == HT_DIR_X || dir == HT_DIR_Y) {
1051  side = 0;
1052  } else {
1053  TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
1054  side = Delta(Delta(TileX(start), TileX(ti->tile)), Delta(TileY(start), TileY(ti->tile)));
1055  }
1056 
1057  DrawAutorailSelection(ti, _autorail_type[dir][side]);
1058  }
1059  return;
1060  }
1061 
1062  /* Check if it's inside the outer area? */
1063  if (!is_redsq && _thd.outersize.x > 0 &&
1064  IsInsideBS(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) &&
1065  IsInsideBS(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) {
1066  /* Draw a blue rect. */
1068  return;
1069  }
1070 }
1071 
1080 static inline Point GetTileCoordFromScreenCoord(int x, int y)
1081 {
1082  /* First convert from the screen coordinate system (where the width of tiles
1083  * is twice their height) to the tile coordinate system. That means, turn
1084  * around by 45 degrees and make the tiles quadratic. */
1085  Point tile_coord = InverseRemapCoords(x, y);
1086 
1087  /* Scale from a 16x16-grid to a 1x1-grid as returned by TileX/TileY. */
1088  tile_coord.x /= (int)TILE_SIZE;
1089  tile_coord.y /= (int)TILE_SIZE;
1090 
1091  return tile_coord;
1092 }
1093 
1111 {
1112  Point tile_coord = GetTileCoordFromScreenCoord(x, y);
1113 
1114  /* Expand area to be painted in order to avoid situations
1115  * where south or east of the to be painted point in dpi are tiles
1116  * which will not be painted. */
1117  tile_coord.y--;
1118 
1119  return tile_coord;
1120 }
1121 
1139 {
1140  Point tile_coord = GetTileCoordFromScreenCoord(x, y);
1141 
1142  /* Expand area to be painted to southeast in order to avoid situations
1143  * where north or east of the given to be painted point in dpi are
1144  * tiles which will not be repainted. */
1145  tile_coord.y++;
1146 
1147  return tile_coord;
1148 }
1149 
1156 static int GetViewportY(Point tile)
1157 {
1158  return (tile.y * TILE_SIZE + tile.x * TILE_SIZE - GetTileMaxPixelZOutsideMap(tile.x, tile.y)) << ZOOM_LVL_SHIFT;
1159 }
1160 
1168 static int GetTileColumnFromTileCoord(Point tile_coord)
1169 {
1170  return tile_coord.y - tile_coord.x;
1171 }
1172 
1181 {
1182  Point northern_end;
1183 
1184  if (tile.x < tile.y) {
1185  northern_end.x = 0;
1186  northern_end.y = tile.y - tile.x;
1187  } else {
1188  northern_end.x = tile.x - tile.y;
1189  northern_end.y = 0;
1190  }
1191 
1192  return northern_end;
1193 }
1194 
1205 {
1206  Point distance_to_end;
1207  distance_to_end.x = (int)MapMaxX() - tile.x;
1208  distance_to_end.y = (int)MapMaxY() - tile.y;
1209 
1210  Point southern_end;
1211  if (distance_to_end.x < distance_to_end.y) {
1212  int number_of_steps = min(limit, distance_to_end.x);
1213  southern_end.x = tile.x + number_of_steps;
1214  southern_end.y = tile.y + number_of_steps;
1215  } else {
1216  int number_of_steps = min(limit, distance_to_end.y);
1217  southern_end.x = tile.x + number_of_steps;
1218  southern_end.y = tile.y + number_of_steps;
1219  }
1220 
1221  return southern_end;
1222 }
1223 
1232 {
1233  return GetSouthernEndOfColumnWithLimit(tile, UINT32_MAX);
1234 }
1235 
1243 static Point GetMiddleTile(Point upper_tile, Point lower_tile)
1244 {
1245  Point middle_tile;
1246  middle_tile.x = (lower_tile.x + upper_tile.x) / 2;
1247  middle_tile.y = (lower_tile.y + upper_tile.y) / 2;
1248  return middle_tile;
1249 }
1250 
1281 int GetRowAtTile(int viewport_y, Point tile, bool bridge_correct)
1282 {
1283  Point northern_tile = GetNorthernEndOfColumn(tile);
1284  Point southern_tile = GetSouthernEndOfColumn(tile);
1285 
1286  int northern_tile_viewport_y = GetViewportY(northern_tile);
1287  int southern_tile_viewport_y = GetViewportY(southern_tile);
1288 
1289  if (northern_tile_viewport_y >= viewport_y) {
1290  /* We are north of the map, search tile by tile with direction north. */
1291  while (northern_tile_viewport_y >= viewport_y) {
1292  northern_tile.x--;
1293  northern_tile.y--;
1294  northern_tile_viewport_y = GetViewportY(northern_tile);
1295  }
1296  return northern_tile.x + northern_tile.y;
1297  }
1298 
1299  if (southern_tile_viewport_y <= viewport_y) {
1300  /* We are south of the map, search tile by tile with direction south. */
1301  while (southern_tile_viewport_y <= viewport_y) {
1302  southern_tile.x++;
1303  southern_tile.y++;
1304  southern_tile_viewport_y = GetViewportY(southern_tile);
1305  }
1306  return southern_tile.x + southern_tile.y;
1307  }
1308 
1309  /*
1310  * We are inside the map. The searched tile is at most
1311  * <maximum heightlevel / 4> tiles south of the given tile (as one tile
1312  * painted on the screen needs as much vertical space as painting a tile
1313  * by 4 heightlevels ascended). Add one to avoid rounding errors to the
1314  * wrong side.
1315  *
1316  * Invariant in the code below: The searched tile shown at viewport_y
1317  * always is between upper_tile and lower_tile.
1318  */
1319  Point upper_tile = tile;
1321  int middle_bound;
1322 
1323  do {
1324  Point middle_tile = GetMiddleTile(upper_tile, lower_tile);
1325  middle_bound = GetViewportY(middle_tile);
1326 
1327  if (middle_bound >= viewport_y) {
1328  /* The tile shown at viewport_y is somewhere in the upper half of
1329  * the currently observed section. */
1330  lower_tile = middle_tile;
1331  } else {
1332  /* The tile shown at viewport_y is somewhere in the lower half of
1333  * the currently observed section. */
1334  upper_tile = middle_tile;
1335  }
1336  }
1337  while (lower_tile.y - upper_tile.y > 1);
1338 
1339  /* Now our interval has length 1, so only contains two tiles, and we take the upper one.
1340  * However, there is one problem left: Tiles being located southwards, containing a high bridge.
1341  * They may, though not high enough in terms of landscape, intersect the drawing area with parts
1342  * of the bridge.
1343  * Luckily, there is a guaranteed upper bound for bridge height, thus we know how far we have to
1344  * search southwards whether such a bridge exists.
1345  */
1346  int correction_step = 0;
1347  if (bridge_correct) {
1348  /* Calculate, how many tiles below upper_tile, a worst case bridge intersecting upper_tile in
1349  * terms of painting can be located. Lets inspect that formula in detail:
1350  * ... - 5: The magic constant near the beginning of ViewportAddLandscape accounts for 5 harmless heightlevels a bridge can have. Thus subtract them.
1351  * ... / 2: Four heightlevels account for one tile height. On the other hand, if landscape ascends from upper_tile southwards, this can account for
1352  * as many additional heightlevels as we step southwards. In combination: A division by two gains the number of tiles to step southwards.
1353  * ... + 1: Avoid rounding errors, and fall back to the safe side.
1354  */
1355  int worst_case_steps_southwards = max(0, ((int)_settings_game.construction.max_bridge_height - 5) / 2 + 1);
1356  for (int n = 0; n < worst_case_steps_southwards; n++) {
1357  TileIndex potential_bridge_tile = TileXY(upper_tile.x + n, upper_tile.y + n);
1358  if (IsValidTile(potential_bridge_tile) && IsBridgeAbove(potential_bridge_tile)) {
1359  /* There is a bridge. */
1360  TileIndex bridge_start = GetNorthernBridgeEnd(potential_bridge_tile);
1361  int bridge_height = GetBridgeHeight(bridge_start);
1362  int upper_tile_height = GetTileZ(TileXY(upper_tile.x, upper_tile.y));
1363 
1364  /* Start at the bridge level, descend by the number of heightlevels equivalent to our steps southwards (in worst case), subtract the harmless
1365  * bridge heightlevels, and compare whether we are still above the height of the upper_tile. If yes, we need to paint that tile, to avoid glitches.
1366  */
1367  if (bridge_height - 2 * n - 1 > upper_tile_height) {
1368  correction_step = n;
1369  }
1370  }
1371  }
1372  }
1373 
1374  /* The biggest recorded correction_step defines, which tile we actually return. */
1375  upper_tile.x += correction_step;
1376  upper_tile.y += correction_step;
1377 
1378  /* Returns its row. */
1379  return upper_tile.x + upper_tile.y;
1380 }
1381 
1397 static Point GetBottomTileOfColumn(Point upper_tile, Point lower_right_tile)
1398 {
1399  int upper_row = upper_tile.x + upper_tile.y;
1400  int lower_row = lower_right_tile.x + lower_right_tile.y;
1401 
1402  assert(upper_row <= lower_row);
1403 
1404  int number_of_rows = lower_row - upper_row;
1405 
1406  if (number_of_rows % 2 != 0) {
1407  /* Avoid 0.5 being rounded down to zero; painting too much is better than
1408  * painting too little. */
1409  number_of_rows++;
1410  }
1411 
1412  Point bottom_tile;
1413  bottom_tile.x = upper_tile.x + number_of_rows / 2;
1414  bottom_tile.y = upper_tile.y + number_of_rows / 2;
1415 
1416  int bottom_row = bottom_tile.x + bottom_tile.y;
1417 
1418  assert(bottom_row >= lower_row);
1419 
1420  return bottom_tile;
1421 }
1422 
1427 {
1428  assert(_vd.dpi.top <= _vd.dpi.top + _vd.dpi.height);
1429  assert(_vd.dpi.left <= _vd.dpi.left + _vd.dpi.width);
1430 
1431  /* The upper and lower edge of the viewport part to paint. Add some number
1432  * of pixels to the lower end in order to ensure that we also take tiles
1433  * south of the given area, but with high buildings intersecting the area.
1434  * Subtract some pixels from the upper end in order to avoid glitches at the
1435  * upper end of the top be painted area. */
1436  int viewport_top = _vd.dpi.top - 16;
1437  int viewport_bottom = _vd.dpi.top + _vd.dpi.height + 116;
1438 
1439  /* First get the position of the tile at the upper left / lower right edge,
1440  * for now ignoring the height. (i.e. assuming height zero.) */
1441  Point upper_left_tile = GetMinTileCoordsIgnoringHeight(_vd.dpi.left, viewport_top);
1442  Point lower_right_tile = GetMaxTileCoordsIgnoringHeight(_vd.dpi.left + _vd.dpi.width, viewport_bottom);
1443 
1444  /* Calculate the bounding columns. We won't need to draw anything
1445  * left / right of them. */
1446  int left_column = GetTileColumnFromTileCoord(upper_left_tile);
1447  /* Correction to avoid glitches when approaching the left edge of the map. */
1448  left_column--;
1449  int right_column = GetTileColumnFromTileCoord(lower_right_tile);
1450  right_column++;
1451 
1452  /* For each column, calculate the top and the bottom row. These are the
1453  * bounding rows for that specific column. */
1454  int *top_row = AllocaM(int, right_column - left_column + 1); // Pre-allocate memory for visual studio/express to be able to compile.
1455  int *bottom_row = AllocaM(int, right_column - left_column + 1); // Pre-allocate memory for visual studio/express to be able to compile.
1456  int min_top_row = MapMaxX() + MapMaxY();
1457  int max_bottom_row = 0;
1458  Point top_tile_of_column = upper_left_tile;
1459 
1460  /* And now for each column, determine the top and the bottom row we must paint. */
1461  bool south_east_direction = false;
1462  for (int x = left_column; x <= right_column; x++) {
1463  Point bottom_tile_of_column = GetBottomTileOfColumn(top_tile_of_column, lower_right_tile);
1464 
1465  /* And then actually find out the top and the bottom row. Note that
1466  * top_tile_of_column and bottom_tile_of_column may be outside the map here.
1467  * This possibility is needed, otherwise we couldn't paint the black area
1468  * outside the map (and in particular the edge of map) properly.
1469  * Subtract three / add one to avoid glitches. */
1470  top_row[x - left_column] = GetRowAtTile(viewport_top, top_tile_of_column, false);
1471 
1472  top_row[x - left_column] -= 3;
1473  bottom_row[x - left_column] = GetRowAtTile(viewport_bottom, bottom_tile_of_column, true);
1474  bottom_row[x - left_column]++;
1475 
1476  /* We never paint things in rows < min_top_row or > max_bottom_row. */
1477  min_top_row = min(min_top_row, top_row[x - left_column]);
1478  max_bottom_row = max(max_bottom_row, bottom_row[x - left_column]);
1479 
1480  /* Go to next column in the east. */
1481  if (south_east_direction) {
1482  top_tile_of_column.y++;
1483  } else {
1484  top_tile_of_column.x--;
1485  }
1486 
1487  /* Switch between directions southeast and northeast. */
1488  south_east_direction = !south_east_direction;
1489  }
1490 
1491  for (int row = min_top_row; row <= max_bottom_row; row++) {
1492  for (int column = left_column; column <= right_column; column++) {
1493  /* For each column, we only paint the interval top_row .. bottom_row.
1494  * Due to the division by two below, even and odd values of row + column map to
1495  * the same (x,y) combinations. Thus, we only paint one of them. */
1496  if (((row + column) % 2 == 0) &&
1497  (top_row[column - left_column] <= row) &&
1498  (row <= bottom_row[column - left_column])) {
1499  TileType tile_type;
1500  TileInfo tile_info;
1501  _cur_ti = &tile_info;
1502 
1503  /* column = y - x; row = x + y; now solve the equation system
1504  * for x and y. */
1505  int x = (row - column) / 2;
1506  int y = (row + column) / 2;
1507  tile_info.x = x;
1508  tile_info.y = y;
1509 
1510  /* For some strange reason, those fields inside tile_info are uints. However,
1511  * in the old code their copies in an int variable where compared against zero. */
1512  if (0 < x && x < (int)MapMaxX() && 0 < y && y < (int)MapMaxY()) {
1513  /* We are inside the map => paint landscape. */
1514  tile_info.tile = TileXY(tile_info.x, tile_info.y);
1515  tile_info.tileh = GetTilePixelSlope(tile_info.tile, &tile_info.z);
1516  tile_type = GetTileType(tile_info.tile);
1517  } else {
1518  /* We are outside the map => paint black. */
1519  tile_info.tile = INVALID_TILE;
1520  tile_info.tileh = GetTilePixelSlopeOutsideMap(tile_info.x, tile_info.y, &tile_info.z);
1521  tile_type = MP_VOID;
1522  }
1523 
1524  /* Scale to 16x16 tiles, needed for the drawing procedures called below. */
1525  tile_info.x *= TILE_SIZE;
1526  tile_info.y *= TILE_SIZE;
1527 
1529  _vd.foundation[0] = -1;
1530  _vd.foundation[1] = -1;
1531  _vd.last_foundation_child[0] = NULL;
1532  _vd.last_foundation_child[1] = NULL;
1533 
1534  _tile_type_procs[tile_type]->draw_tile_proc(&tile_info);
1535  DrawTileSelection(&tile_info);
1536  }
1537  }
1538  }
1539 }
1540 
1551 void ViewportAddString(const DrawPixelInfo *dpi, ZoomLevel small_from, const ViewportSign *sign, StringID string_normal, StringID string_small, StringID string_small_shadow, uint64 params_1, uint64 params_2, Colours colour)
1552 {
1553  bool small = dpi->zoom >= small_from;
1554 
1555  int left = dpi->left;
1556  int top = dpi->top;
1557  int right = left + dpi->width;
1558  int bottom = top + dpi->height;
1559 
1560  int sign_height = ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM, dpi->zoom);
1561  int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, dpi->zoom);
1562 
1563  if (bottom < sign->top ||
1564  top > sign->top + sign_height ||
1565  right < sign->center - sign_half_width ||
1566  left > sign->center + sign_half_width) {
1567  return;
1568  }
1569 
1570  if (!small) {
1571  AddStringToDraw(sign->center - sign_half_width, sign->top, string_normal, params_1, params_2, colour, sign->width_normal);
1572  } else {
1573  int shadow_offset = 0;
1574  if (string_small_shadow != STR_NULL) {
1575  shadow_offset = 4;
1576  AddStringToDraw(sign->center - sign_half_width + shadow_offset, sign->top, string_small_shadow, params_1, params_2, INVALID_COLOUR, sign->width_small);
1577  }
1578  AddStringToDraw(sign->center - sign_half_width, sign->top - shadow_offset, string_small, params_1, params_2,
1579  colour, sign->width_small | 0x8000);
1580  }
1581 }
1582 
1583 static void ViewportAddTownNames(DrawPixelInfo *dpi)
1584 {
1585  if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES) || _game_mode == GM_MENU) return;
1586 
1587  const Town *t;
1588  FOR_ALL_TOWNS(t) {
1590  _settings_client.gui.population_in_label ? STR_VIEWPORT_TOWN_POP : STR_VIEWPORT_TOWN,
1591  STR_VIEWPORT_TOWN_TINY_WHITE, STR_VIEWPORT_TOWN_TINY_BLACK,
1592  t->index, t->cache.population);
1593  }
1594 }
1595 
1596 
1597 static void ViewportAddStationNames(DrawPixelInfo *dpi)
1598 {
1599  if (!(HasBit(_display_opt, DO_SHOW_STATION_NAMES) || HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)) || _game_mode == GM_MENU) return;
1600 
1601  const BaseStation *st;
1602  FOR_ALL_BASE_STATIONS(st) {
1603  /* Check whether the base station is a station or a waypoint */
1604  bool is_station = Station::IsExpected(st);
1605 
1606  /* Don't draw if the display options are disabled */
1607  if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
1608 
1609  /* Don't draw if station is owned by another company and competitor station names are hidden. Stations owned by none are never ignored. */
1610  if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != st->owner && st->owner != OWNER_NONE) continue;
1611 
1613  is_station ? STR_VIEWPORT_STATION : STR_VIEWPORT_WAYPOINT,
1614  (is_station ? STR_VIEWPORT_STATION : STR_VIEWPORT_WAYPOINT) + 1, STR_NULL,
1615  st->index, st->facilities, (st->owner == OWNER_NONE || !st->IsInUse()) ? COLOUR_GREY : _company_colours[st->owner]);
1616  }
1617 }
1618 
1619 
1620 static void ViewportAddSigns(DrawPixelInfo *dpi)
1621 {
1622  /* Signs are turned off or are invisible */
1624 
1625  const Sign *si;
1626  FOR_ALL_SIGNS(si) {
1627  /* Don't draw if sign is owned by another company and competitor signs should be hidden.
1628  * Note: It is intentional that also signs owned by OWNER_NONE are hidden. Bankrupt
1629  * companies can leave OWNER_NONE signs after them. */
1630  if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != si->owner && si->owner != OWNER_DEITY) continue;
1631 
1632  ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &si->sign,
1633  STR_WHITE_SIGN,
1634  (IsTransparencySet(TO_SIGNS) || si->owner == OWNER_DEITY) ? STR_VIEWPORT_SIGN_SMALL_WHITE : STR_VIEWPORT_SIGN_SMALL_BLACK, STR_NULL,
1635  si->index, 0, (si->owner == OWNER_NONE) ? COLOUR_GREY : (si->owner == OWNER_DEITY ? INVALID_COLOUR : _company_colours[si->owner]));
1636  }
1637 }
1638 
1645 void ViewportSign::UpdatePosition(int center, int top, StringID str)
1646 {
1647  if (this->width_normal != 0) this->MarkDirty();
1648 
1649  this->top = top;
1650 
1651  char buffer[DRAW_STRING_BUFFER];
1652 
1653  GetString(buffer, str, lastof(buffer));
1654  this->width_normal = VPSM_LEFT + Align(GetStringBoundingBox(buffer).width, 2) + VPSM_RIGHT;
1655  this->center = center;
1656 
1657  /* zoomed out version */
1658  this->width_small = VPSM_LEFT + Align(GetStringBoundingBox(buffer, FS_SMALL).width, 2) + VPSM_RIGHT;
1659 
1660  this->MarkDirty();
1661 }
1662 
1670 {
1671  Rect zoomlevels[ZOOM_LVL_COUNT];
1672 
1673  for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
1674  /* FIXME: This doesn't switch to width_small when appropriate. */
1675  zoomlevels[zoom].left = this->center - ScaleByZoom(this->width_normal / 2 + 1, zoom);
1676  zoomlevels[zoom].top = this->top - ScaleByZoom(1, zoom);
1677  zoomlevels[zoom].right = this->center + ScaleByZoom(this->width_normal / 2 + 1, zoom);
1678  zoomlevels[zoom].bottom = this->top + ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM + 1, zoom);
1679  }
1680 
1681  Window *w;
1682  FOR_ALL_WINDOWS_FROM_BACK(w) {
1683  ViewPort *vp = w->viewport;
1684  if (vp != NULL && vp->zoom <= maxzoom) {
1685  assert(vp->width != 0);
1686  Rect &zl = zoomlevels[vp->zoom];
1687  MarkViewportDirty(vp, zl.left, zl.top, zl.right, zl.bottom);
1688  }
1689  }
1690 }
1691 
1692 static void ViewportDrawTileSprites(const TileSpriteToDrawVector *tstdv)
1693 {
1694  const TileSpriteToDraw *tsend = tstdv->End();
1695  for (const TileSpriteToDraw *ts = tstdv->Begin(); ts != tsend; ++ts) {
1696  DrawSpriteViewport(ts->image, ts->pal, ts->x, ts->y, ts->sub);
1697  }
1698 }
1699 
1702 {
1703  return true;
1704 }
1705 
1708 {
1709  ParentSpriteToDraw **psdvend = psdv->End();
1710  ParentSpriteToDraw **psd = psdv->Begin();
1711  while (psd != psdvend) {
1712  ParentSpriteToDraw *ps = *psd;
1713 
1714  if (ps->comparison_done) {
1715  psd++;
1716  continue;
1717  }
1718 
1719  ps->comparison_done = true;
1720 
1721  for (ParentSpriteToDraw **psd2 = psd + 1; psd2 != psdvend; psd2++) {
1722  ParentSpriteToDraw *ps2 = *psd2;
1723 
1724  if (ps2->comparison_done) continue;
1725 
1726  /* Decide which comparator to use, based on whether the bounding
1727  * boxes overlap
1728  */
1729  if (ps->xmax >= ps2->xmin && ps->xmin <= ps2->xmax && // overlap in X?
1730  ps->ymax >= ps2->ymin && ps->ymin <= ps2->ymax && // overlap in Y?
1731  ps->zmax >= ps2->zmin && ps->zmin <= ps2->zmax) { // overlap in Z?
1732  /* Use X+Y+Z as the sorting order, so sprites closer to the bottom of
1733  * the screen and with higher Z elevation, are drawn in front.
1734  * Here X,Y,Z are the coordinates of the "center of mass" of the sprite,
1735  * i.e. X=(left+right)/2, etc.
1736  * However, since we only care about order, don't actually divide / 2
1737  */
1738  if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <=
1739  ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) {
1740  continue;
1741  }
1742  } else {
1743  /* We only change the order, if it is definite.
1744  * I.e. every single order of X, Y, Z says ps2 is behind ps or they overlap.
1745  * That is: If one partial order says ps behind ps2, do not change the order.
1746  */
1747  if (ps->xmax < ps2->xmin ||
1748  ps->ymax < ps2->ymin ||
1749  ps->zmax < ps2->zmin) {
1750  continue;
1751  }
1752  }
1753 
1754  /* Move ps2 in front of ps */
1755  ParentSpriteToDraw *temp = ps2;
1756  for (ParentSpriteToDraw **psd3 = psd2; psd3 > psd; psd3--) {
1757  *psd3 = *(psd3 - 1);
1758  }
1759  *psd = temp;
1760  }
1761  }
1762 }
1763 
1764 static void ViewportDrawParentSprites(const ParentSpriteToSortVector *psd, const ChildScreenSpriteToDrawVector *csstdv)
1765 {
1766  const ParentSpriteToDraw * const *psd_end = psd->End();
1767  for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
1768  const ParentSpriteToDraw *ps = *it;
1769  if (ps->image != SPR_EMPTY_BOUNDING_BOX) DrawSpriteViewport(ps->image, ps->pal, ps->x, ps->y, ps->sub);
1770 
1771  int child_idx = ps->first_child;
1772  while (child_idx >= 0) {
1773  const ChildScreenSpriteToDraw *cs = csstdv->Get(child_idx);
1774  child_idx = cs->next;
1775  DrawSpriteViewport(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub);
1776  }
1777  }
1778 }
1779 
1785 {
1786  const ParentSpriteToDraw * const *psd_end = psd->End();
1787  for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
1788  const ParentSpriteToDraw *ps = *it;
1789  Point pt1 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmax + 1); // top front corner
1790  Point pt2 = RemapCoords(ps->xmin , ps->ymax + 1, ps->zmax + 1); // top left corner
1791  Point pt3 = RemapCoords(ps->xmax + 1, ps->ymin , ps->zmax + 1); // top right corner
1792  Point pt4 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmin ); // bottom front corner
1793 
1794  DrawBox( pt1.x, pt1.y,
1795  pt2.x - pt1.x, pt2.y - pt1.y,
1796  pt3.x - pt1.x, pt3.y - pt1.y,
1797  pt4.x - pt1.x, pt4.y - pt1.y);
1798  }
1799 }
1800 
1805 {
1807  const DrawPixelInfo *dpi = _cur_dpi;
1808  void *dst;
1809  int right = UnScaleByZoom(dpi->width, dpi->zoom);
1810  int bottom = UnScaleByZoom(dpi->height, dpi->zoom);
1811 
1812  int colour = _string_colourmap[_dirty_block_colour & 0xF];
1813 
1814  dst = dpi->dst_ptr;
1815 
1816  byte bo = UnScaleByZoom(dpi->left + dpi->top, dpi->zoom) & 1;
1817  do {
1818  for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
1819  dst = blitter->MoveTo(dst, 0, 1);
1820  } while (--bottom > 0);
1821 }
1822 
1823 static void ViewportDrawStrings(ZoomLevel zoom, const StringSpriteToDrawVector *sstdv)
1824 {
1825  const StringSpriteToDraw *ssend = sstdv->End();
1826  for (const StringSpriteToDraw *ss = sstdv->Begin(); ss != ssend; ++ss) {
1827  TextColour colour = TC_BLACK;
1828  bool small = HasBit(ss->width, 15);
1829  int w = GB(ss->width, 0, 15);
1830  int x = UnScaleByZoom(ss->x, zoom);
1831  int y = UnScaleByZoom(ss->y, zoom);
1832  int h = VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM;
1833 
1834  SetDParam(0, ss->params[0]);
1835  SetDParam(1, ss->params[1]);
1836 
1837  if (ss->colour != INVALID_COLOUR) {
1838  /* Do not draw signs nor station names if they are set invisible */
1839  if (IsInvisibilitySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) continue;
1840 
1841  if (IsTransparencySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) {
1842  /* Don't draw the rectangle.
1843  * Real colours need the TC_IS_PALETTE_COLOUR flag.
1844  * Otherwise colours from _string_colourmap are assumed. */
1845  colour = (TextColour)_colour_gradient[ss->colour][6] | TC_IS_PALETTE_COLOUR;
1846  } else {
1847  /* Draw the rectangle if 'transparent station signs' is off,
1848  * or if we are drawing a general text sign (STR_WHITE_SIGN). */
1849  DrawFrameRect(
1850  x, y, x + w, y + h, ss->colour,
1852  );
1853  }
1854  }
1855 
1856  DrawString(x + VPSM_LEFT, x + w - 1 - VPSM_RIGHT, y + VPSM_TOP, ss->string, colour, SA_HOR_CENTER);
1857  }
1858 }
1859 
1860 void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom)
1861 {
1862  DrawPixelInfo *old_dpi = _cur_dpi;
1863  _cur_dpi = &_vd.dpi;
1864 
1865  _vd.dpi.zoom = vp->zoom;
1866  int mask = ScaleByZoom(-1, vp->zoom);
1867 
1869 
1870  _vd.dpi.width = (right - left) & mask;
1871  _vd.dpi.height = (bottom - top) & mask;
1872  _vd.dpi.left = left & mask;
1873  _vd.dpi.top = top & mask;
1874  _vd.dpi.pitch = old_dpi->pitch;
1875  _vd.last_child = NULL;
1876 
1877  int x = UnScaleByZoom(_vd.dpi.left - (vp->virtual_left & mask), vp->zoom) + vp->left;
1878  int y = UnScaleByZoom(_vd.dpi.top - (vp->virtual_top & mask), vp->zoom) + vp->top;
1879 
1880  _vd.dpi.dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(old_dpi->dst_ptr, x - old_dpi->left, y - old_dpi->top);
1881 
1883  ViewportAddVehicles(&_vd.dpi);
1884 
1885  ViewportAddTownNames(&_vd.dpi);
1886  ViewportAddStationNames(&_vd.dpi);
1887  ViewportAddSigns(&_vd.dpi);
1888 
1889  DrawTextEffects(&_vd.dpi);
1890 
1891  if (_vd.tile_sprites_to_draw.Length() != 0) ViewportDrawTileSprites(&_vd.tile_sprites_to_draw);
1892 
1893  ParentSpriteToDraw *psd_end = _vd.parent_sprites_to_draw.End();
1894  for (ParentSpriteToDraw *it = _vd.parent_sprites_to_draw.Begin(); it != psd_end; it++) {
1895  *_vd.parent_sprites_to_sort.Append() = it;
1896  }
1897 
1898  _vp_sprite_sorter(&_vd.parent_sprites_to_sort);
1899  ViewportDrawParentSprites(&_vd.parent_sprites_to_sort, &_vd.child_screen_sprites_to_draw);
1900 
1901  if (_draw_bounding_boxes) ViewportDrawBoundingBoxes(&_vd.parent_sprites_to_sort);
1902  if (_draw_dirty_blocks) ViewportDrawDirtyBlocks();
1903 
1904  DrawPixelInfo dp = _vd.dpi;
1905  ZoomLevel zoom = _vd.dpi.zoom;
1906  dp.zoom = ZOOM_LVL_NORMAL;
1907  dp.width = UnScaleByZoom(dp.width, zoom);
1908  dp.height = UnScaleByZoom(dp.height, zoom);
1909  _cur_dpi = &dp;
1910 
1911  if (vp->overlay != NULL && vp->overlay->GetCargoMask() != 0 && vp->overlay->GetCompanyMask() != 0) {
1912  /* translate to window coordinates */
1913  dp.left = x;
1914  dp.top = y;
1915  vp->overlay->Draw(&dp);
1916  }
1917 
1918  if (_vd.string_sprites_to_draw.Length() != 0) {
1919  /* translate to world coordinates */
1920  dp.left = UnScaleByZoom(_vd.dpi.left, zoom);
1921  dp.top = UnScaleByZoom(_vd.dpi.top, zoom);
1922  ViewportDrawStrings(zoom, &_vd.string_sprites_to_draw);
1923  }
1924 
1925  _cur_dpi = old_dpi;
1926 
1927  _vd.string_sprites_to_draw.Clear();
1928  _vd.tile_sprites_to_draw.Clear();
1929  _vd.parent_sprites_to_draw.Clear();
1931  _vd.child_screen_sprites_to_draw.Clear();
1932 }
1933 
1938 static void ViewportDrawChk(const ViewPort *vp, int left, int top, int right, int bottom)
1939 {
1940  if (ScaleByZoom(bottom - top, vp->zoom) * ScaleByZoom(right - left, vp->zoom) > 180000 * ZOOM_LVL_BASE * ZOOM_LVL_BASE) {
1941  if ((bottom - top) > (right - left)) {
1942  int t = (top + bottom) >> 1;
1943  ViewportDrawChk(vp, left, top, right, t);
1944  ViewportDrawChk(vp, left, t, right, bottom);
1945  } else {
1946  int t = (left + right) >> 1;
1947  ViewportDrawChk(vp, left, top, t, bottom);
1948  ViewportDrawChk(vp, t, top, right, bottom);
1949  }
1950  } else {
1951  ViewportDoDraw(vp,
1952  ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
1953  ScaleByZoom(top - vp->top, vp->zoom) + vp->virtual_top,
1954  ScaleByZoom(right - vp->left, vp->zoom) + vp->virtual_left,
1955  ScaleByZoom(bottom - vp->top, vp->zoom) + vp->virtual_top
1956  );
1957  }
1958 }
1959 
1960 static inline void ViewportDraw(const ViewPort *vp, int left, int top, int right, int bottom)
1961 {
1962  if (right <= vp->left || bottom <= vp->top) return;
1963 
1964  if (left >= vp->left + vp->width) return;
1965 
1966  if (left < vp->left) left = vp->left;
1967  if (right > vp->left + vp->width) right = vp->left + vp->width;
1968 
1969  if (top >= vp->top + vp->height) return;
1970 
1971  if (top < vp->top) top = vp->top;
1972  if (bottom > vp->top + vp->height) bottom = vp->top + vp->height;
1973 
1974  ViewportDrawChk(vp, left, top, right, bottom);
1975 }
1976 
1981 {
1982  DrawPixelInfo *dpi = _cur_dpi;
1983 
1984  dpi->left += this->left;
1985  dpi->top += this->top;
1986 
1987  ViewportDraw(this->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height);
1988 
1989  dpi->left -= this->left;
1990  dpi->top -= this->top;
1991 }
1992 
2001 typedef bool ContinueMapEdgeSearch(int iter, int iter_limit, int sy, int sy_limit);
2002 
2004 static inline bool ContinueLowerMapEdgeSearch(int iter, int iter_limit, int sy, int sy_limit) { return iter > 0 && sy > sy_limit; }
2006 static inline bool ContinueUpperMapEdgeSearch(int iter, int iter_limit, int sy, int sy_limit) { return iter < iter_limit && sy < sy_limit; }
2007 
2021 static int SearchMapEdge(Point &curr_tile, int &iter, int iter_limit, int offset, int sy_limit, ContinueMapEdgeSearch continue_criteria)
2022 {
2023  int sy;
2024  do {
2025  iter = Clamp(iter + offset, 0, iter_limit);
2026  sy = GetViewportY(curr_tile);
2027  } while (continue_criteria(iter, iter_limit, sy, sy_limit));
2028 
2029  return iter;
2030 }
2031 
2046 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)
2047 {
2048  bool upper_edge = other_value < _settings_game.construction.max_heightlevel / 4;
2049 
2050  /*
2051  * First get an estimate of the tiles relevant for us at that edge. Relevant in the sense
2052  * "at least close to the visible area". Thus, we don't look at exactly each tile, inspecting
2053  * e.g. every tenth should be enough. After all, the desired screen limit is set such that
2054  * the bordermost tiles are painted in the middle of the screen when one hits the limit,
2055  * i.e. it is no harm if there is some small error in that calculation
2056  */
2057 
2058  other_ref = upper_edge ? 0 : other_limit;
2059  iter = start;
2060  int min_iter = SearchMapEdge(curr_tile, iter, iter_limit, upper_edge ? -10 : +10, vp_top, upper_edge ? ContinueLowerMapEdgeSearch : ContinueUpperMapEdgeSearch);
2061  iter = start;
2062  int max_iter = SearchMapEdge(curr_tile, iter, iter_limit, upper_edge ? +10 : -10, vp_bottom, upper_edge ? ContinueUpperMapEdgeSearch : ContinueLowerMapEdgeSearch);
2063 
2064  max_iter = min(max_iter + _settings_game.construction.max_heightlevel / 4, iter_limit);
2065  min_iter = min(min_iter, max_iter);
2066 
2067  /* Now, calculate the highest heightlevel of these tiles. Again just as an estimate. */
2068  int max_heightlevel_at_edge = 0;
2069  for (iter = min_iter; iter <= max_iter; iter += 10) {
2070  max_heightlevel_at_edge = max(max_heightlevel_at_edge, (int)TileHeight(TileXY(curr_tile.x, curr_tile.y)));
2071  }
2072 
2073  /* Based on that heightlevel, calculate the limit. For the upper edge a tile with height zero would
2074  * get a limit of zero, on the other side it depends on the number of tiles along the axis. */
2075  return upper_edge ?
2076  max(vp_value, -max_heightlevel_at_edge * (int)(TILE_HEIGHT * 2 * ZOOM_LVL_BASE)) :
2077  min(vp_value, (other_limit * TILE_SIZE * 4 - max_heightlevel_at_edge * TILE_HEIGHT * 2) * ZOOM_LVL_BASE);
2078 }
2079 
2080 static inline void ClampViewportToMap(const ViewPort *vp, int &x, int &y)
2081 {
2082  int original_y = y;
2083 
2084  /* Centre of the viewport is hot spot */
2085  x += vp->virtual_width / 2;
2086  y += vp->virtual_height / 2;
2087 
2088  /* Convert viewport coordinates to map coordinates
2089  * Calculation is scaled by 4 to avoid rounding errors */
2090  int vx = -x + y * 2;
2091  int vy = x + y * 2;
2092 
2093  /* Find out which tile corresponds to (vx,vy) if one assumes height zero. The cast is necessary to prevent C++ from
2094  * converting the result to an uint, which gives an overflow instead of a negative result... */
2095  int tx = vx / (int)(TILE_SIZE * 4 * ZOOM_LVL_BASE);
2096  int ty = vy / (int)(TILE_SIZE * 4 * ZOOM_LVL_BASE);
2097 
2098  Point curr_tile;
2099  vx = ClampXYToMap(curr_tile, curr_tile.y, MapMaxY(), ty, curr_tile.x, tx, vx, MapMaxX(), original_y, original_y + vp->virtual_height);
2100  vy = ClampXYToMap(curr_tile, curr_tile.x, MapMaxX(), tx, curr_tile.y, ty, vy, MapMaxY(), original_y, original_y + vp->virtual_height);
2101 
2102  /* Convert map coordinates to viewport coordinates */
2103  x = (-vx + vy) / 2;
2104  y = ( vx + vy) / 4;
2105 
2106  /* Remove centering */
2107  x -= vp->virtual_width / 2;
2108  y -= vp->virtual_height / 2;
2109 }
2110 
2116 {
2117  const ViewPort *vp = w->viewport;
2118 
2120  const Vehicle *veh = Vehicle::Get(w->viewport->follow_vehicle);
2121  Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
2122 
2123  w->viewport->scrollpos_x = pt.x;
2124  w->viewport->scrollpos_y = pt.y;
2125  SetViewportPosition(w, pt.x, pt.y);
2126  } else {
2127  /* Ensure the destination location is within the map */
2128  ClampViewportToMap(vp, w->viewport->dest_scrollpos_x, w->viewport->dest_scrollpos_y);
2129 
2130  int delta_x = w->viewport->dest_scrollpos_x - w->viewport->scrollpos_x;
2131  int delta_y = w->viewport->dest_scrollpos_y - w->viewport->scrollpos_y;
2132 
2133  bool update_overlay = false;
2134  if (delta_x != 0 || delta_y != 0) {
2136  int max_scroll = ScaleByMapSize1D(512 * ZOOM_LVL_BASE);
2137  /* Not at our desired position yet... */
2138  w->viewport->scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll);
2139  w->viewport->scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll);
2140  } else {
2143  }
2144  update_overlay = (w->viewport->scrollpos_x == w->viewport->dest_scrollpos_x &&
2146  }
2147 
2148  ClampViewportToMap(vp, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
2149 
2150  SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
2151  if (update_overlay) RebuildViewportOverlay(w);
2152  }
2153 }
2154 
2164 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom)
2165 {
2166  right -= vp->virtual_left;
2167  if (right <= 0) return;
2168 
2169  bottom -= vp->virtual_top;
2170  if (bottom <= 0) return;
2171 
2172  left = max(0, left - vp->virtual_left);
2173 
2174  if (left >= vp->virtual_width) return;
2175 
2176  top = max(0, top - vp->virtual_top);
2177 
2178  if (top >= vp->virtual_height) return;
2179 
2181  UnScaleByZoomLower(left, vp->zoom) + vp->left,
2182  UnScaleByZoomLower(top, vp->zoom) + vp->top,
2183  UnScaleByZoom(right, vp->zoom) + vp->left + 1,
2184  UnScaleByZoom(bottom, vp->zoom) + vp->top + 1
2185  );
2186 }
2187 
2196 void MarkAllViewportsDirty(int left, int top, int right, int bottom)
2197 {
2198  Window *w;
2199  FOR_ALL_WINDOWS_FROM_BACK(w) {
2200  ViewPort *vp = w->viewport;
2201  if (vp != NULL) {
2202  assert(vp->width != 0);
2203  MarkViewportDirty(vp, left, top, right, bottom);
2204  }
2205  }
2206 }
2207 
2208 void ConstrainAllViewportsZoom()
2209 {
2210  Window *w;
2211  FOR_ALL_WINDOWS_FROM_FRONT(w) {
2212  if (w->viewport == NULL) continue;
2213 
2215  if (zoom != w->viewport->zoom) {
2216  while (w->viewport->zoom < zoom) DoZoomInOutWindow(ZOOM_OUT, w);
2217  while (w->viewport->zoom > zoom) DoZoomInOutWindow(ZOOM_IN, w);
2218  }
2219  }
2220 }
2221 
2228 {
2229  Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTilePixelZ(tile));
2231  pt.x - 31 * ZOOM_LVL_BASE,
2232  pt.y - 122 * ZOOM_LVL_BASE,
2233  pt.x - 31 * ZOOM_LVL_BASE + 67 * ZOOM_LVL_BASE,
2234  pt.y - 122 * ZOOM_LVL_BASE + 154 * ZOOM_LVL_BASE
2235  );
2236 }
2237 
2238 void MarkTileDirtyByTileOutsideMap(int x, int y)
2239 {
2240  Point pt = RemapCoords(x * TILE_SIZE, y * TILE_SIZE, GetTilePixelZOutsideMap(x, y));
2241  /* Since tiles painted outside the map don't contain buildings, trees, etc.,
2242  * this reduced area for repainting should suffice. If not, adjust the offsets
2243  * used below. */
2245  pt.x - TILE_SIZE + 1,
2246  pt.y,
2247  pt.x + TILE_SIZE - 1,
2248  pt.y + TILE_SIZE + TILE_HEIGHT - 1
2249  );
2250 }
2251 
2260 {
2261  int x_size = _thd.size.x;
2262  int y_size = _thd.size.y;
2263 
2264  if (!_thd.diagonal) { // Selecting in a straight rectangle (or a single square)
2265  int x_start = _thd.pos.x;
2266  int y_start = _thd.pos.y;
2267 
2268  if (_thd.outersize.x != 0) {
2269  x_size += _thd.outersize.x;
2270  x_start += _thd.offs.x;
2271  y_size += _thd.outersize.y;
2272  y_start += _thd.offs.y;
2273  }
2274 
2275  x_size -= TILE_SIZE;
2276  y_size -= TILE_SIZE;
2277 
2278  assert(x_size >= 0);
2279  assert(y_size >= 0);
2280 
2281  int x_end = Clamp(x_start + x_size, 0, MapSizeX() * TILE_SIZE - TILE_SIZE);
2282  int y_end = Clamp(y_start + y_size, 0, MapSizeY() * TILE_SIZE - TILE_SIZE);
2283 
2284  x_start = Clamp(x_start, 0, MapSizeX() * TILE_SIZE - TILE_SIZE);
2285  y_start = Clamp(y_start, 0, MapSizeY() * TILE_SIZE - TILE_SIZE);
2286 
2287  /* make sure everything is multiple of TILE_SIZE */
2288  assert((x_end | y_end | x_start | y_start) % TILE_SIZE == 0);
2289 
2290  /* How it works:
2291  * Suppose we have to mark dirty rectangle of 3x4 tiles:
2292  * x
2293  * xxx
2294  * xxxxx
2295  * xxxxx
2296  * xxx
2297  * x
2298  * This algorithm marks dirty columns of tiles, so it is done in 3+4-1 steps:
2299  * 1) x 2) x
2300  * xxx Oxx
2301  * Oxxxx xOxxx
2302  * xxxxx Oxxxx
2303  * xxx xxx
2304  * x x
2305  * And so forth...
2306  */
2307 
2308  int top_x = x_end; // coordinates of top dirty tile
2309  int top_y = y_start;
2310  int bot_x = top_x; // coordinates of bottom dirty tile
2311  int bot_y = top_y;
2312 
2313  do {
2314  /* topmost dirty point */
2315  TileIndex top_tile = TileVirtXY(top_x, top_y);
2316  Point top = RemapCoords(top_x, top_y, GetTileMaxPixelZ(top_tile));
2317 
2318  /* bottommost point */
2319  TileIndex bottom_tile = TileVirtXY(bot_x, bot_y);
2320  Point bot = RemapCoords(bot_x + TILE_SIZE, bot_y + TILE_SIZE, GetTilePixelZ(bottom_tile)); // bottommost point
2321 
2322  /* the 'x' coordinate of 'top' and 'bot' is the same (and always in the same distance from tile middle),
2323  * tile height/slope affects only the 'y' on-screen coordinate! */
2324 
2325  int l = top.x - (TILE_PIXELS - 2) * ZOOM_LVL_BASE; // 'x' coordinate of left side of dirty rectangle
2326  int t = top.y; // 'y' coordinate of top side -//-
2327  int r = top.x + (TILE_PIXELS - 2) * ZOOM_LVL_BASE; // right side of dirty rectangle
2328  int b = bot.y; // bottom -//-
2329 
2330  static const int OVERLAY_WIDTH = 4 * ZOOM_LVL_BASE; // part of selection sprites is drawn outside the selected area
2331 
2332  /* For halftile foundations on SLOPE_STEEP_S the sprite extents some more towards the top */
2333  MarkAllViewportsDirty(l - OVERLAY_WIDTH, t - OVERLAY_WIDTH - TILE_HEIGHT * ZOOM_LVL_BASE, r + OVERLAY_WIDTH, b + OVERLAY_WIDTH * ZOOM_LVL_BASE);
2334 
2335  /* haven't we reached the topmost tile yet? */
2336  if (top_x != x_start) {
2337  top_x -= TILE_SIZE;
2338  } else {
2339  top_y += TILE_SIZE;
2340  }
2341 
2342  /* the way the bottom tile changes is different when we reach the bottommost tile */
2343  if (bot_y != y_end) {
2344  bot_y += TILE_SIZE;
2345  } else {
2346  bot_x -= TILE_SIZE;
2347  }
2348  } while (bot_x >= top_x);
2349  } else { // Selecting in a 45 degrees rotated (diagonal) rectangle.
2350  /* a_size, b_size describe a rectangle with rotated coordinates */
2351  int a_size = x_size + y_size, b_size = x_size - y_size;
2352 
2353  int interval_a = a_size < 0 ? -(int)TILE_SIZE : (int)TILE_SIZE;
2354  int interval_b = b_size < 0 ? -(int)TILE_SIZE : (int)TILE_SIZE;
2355 
2356  for (int a = -interval_a; a != a_size + interval_a; a += interval_a) {
2357  for (int b = -interval_b; b != b_size + interval_b; b += interval_b) {
2358  uint x = (_thd.pos.x + (a + b) / 2) / TILE_SIZE;
2359  uint y = (_thd.pos.y + (a - b) / 2) / TILE_SIZE;
2360 
2361  if (x < MapMaxX() && y < MapMaxY()) {
2362  MarkTileDirtyByTile(TileXY(x, y));
2363  }
2364  }
2365  }
2366  }
2367 }
2368 
2369 
2370 void SetSelectionRed(bool b)
2371 {
2372  _thd.make_square_red = b;
2374 }
2375 
2384 static bool CheckClickOnViewportSign(const ViewPort *vp, int x, int y, const ViewportSign *sign)
2385 {
2386  bool small = (vp->zoom >= ZOOM_LVL_OUT_16X);
2387  int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, vp->zoom);
2388  int sign_height = ScaleByZoom(VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM, vp->zoom);
2389 
2390  x = ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left;
2391  y = ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top;
2392 
2393  return y >= sign->top && y < sign->top + sign_height &&
2394  x >= sign->center - sign_half_width && x < sign->center + sign_half_width;
2395 }
2396 
2397 static bool CheckClickOnTown(const ViewPort *vp, int x, int y)
2398 {
2399  if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES)) return false;
2400 
2401  const Town *t;
2402  FOR_ALL_TOWNS(t) {
2403  if (CheckClickOnViewportSign(vp, x, y, &t->cache.sign)) {
2404  ShowTownViewWindow(t->index);
2405  return true;
2406  }
2407  }
2408 
2409  return false;
2410 }
2411 
2412 static bool CheckClickOnStation(const ViewPort *vp, int x, int y)
2413 {
2414  if (!(HasBit(_display_opt, DO_SHOW_STATION_NAMES) || HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)) || IsInvisibilitySet(TO_SIGNS)) return false;
2415 
2416  const BaseStation *st;
2417  FOR_ALL_BASE_STATIONS(st) {
2418  /* Check whether the base station is a station or a waypoint */
2419  bool is_station = Station::IsExpected(st);
2420 
2421  /* Don't check if the display options are disabled */
2422  if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
2423 
2424  /* Don't check if competitor signs are not shown and the sign isn't owned by the local company */
2425  if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != st->owner && st->owner != OWNER_NONE) continue;
2426 
2427  if (CheckClickOnViewportSign(vp, x, y, &st->sign)) {
2428  if (is_station) {
2430  } else {
2432  }
2433  return true;
2434  }
2435  }
2436 
2437  return false;
2438 }
2439 
2440 
2441 static bool CheckClickOnSign(const ViewPort *vp, int x, int y)
2442 {
2443  /* Signs are turned off, or they are transparent and invisibility is ON, or company is a spectator */
2445 
2446  const Sign *si;
2447  FOR_ALL_SIGNS(si) {
2448  /* If competitor signs are hidden, don't check signs that aren't owned by local company */
2449  if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != si->owner && si->owner != OWNER_DEITY) continue;
2450  if (si->owner == OWNER_DEITY && _game_mode != GM_EDITOR) continue;
2451 
2452  if (CheckClickOnViewportSign(vp, x, y, &si->sign)) {
2453  HandleClickOnSign(si);
2454  return true;
2455  }
2456  }
2457 
2458  return false;
2459 }
2460 
2461 
2462 static bool CheckClickOnLandscape(const ViewPort *vp, int x, int y)
2463 {
2464  Point pt = TranslateXYToTileCoord(vp, x, y);
2465 
2466  if (pt.x != -1) return ClickTile(TileVirtXY(pt.x, pt.y));
2467  return true;
2468 }
2469 
2470 static void PlaceObject()
2471 {
2472  Point pt;
2473  Window *w;
2474 
2475  pt = GetTileBelowCursor();
2476  if (pt.x == -1) return;
2477 
2478  if ((_thd.place_mode & HT_DRAG_MASK) == HT_POINT) {
2479  pt.x += TILE_SIZE / 2;
2480  pt.y += TILE_SIZE / 2;
2481  }
2482 
2483  _tile_fract_coords.x = pt.x & TILE_UNIT_MASK;
2484  _tile_fract_coords.y = pt.y & TILE_UNIT_MASK;
2485 
2486  w = _thd.GetCallbackWnd();
2487  if (w != NULL) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y));
2488 }
2489 
2490 
2491 bool HandleViewportClicked(const ViewPort *vp, int x, int y)
2492 {
2493  const Vehicle *v = CheckClickOnVehicle(vp, x, y);
2494 
2495  if (_thd.place_mode & HT_VEHICLE) {
2496  if (v != NULL && VehicleClicked(v)) return true;
2497  }
2498 
2499  /* Vehicle placement mode already handled above. */
2500  if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
2501  PlaceObject();
2502  return true;
2503  }
2504 
2505  if (CheckClickOnTown(vp, x, y)) return true;
2506  if (CheckClickOnStation(vp, x, y)) return true;
2507  if (CheckClickOnSign(vp, x, y)) return true;
2508  bool result = CheckClickOnLandscape(vp, x, y);
2509 
2510  if (v != NULL) {
2511  DEBUG(misc, 2, "Vehicle %d (index %d) at %p", v->unitnumber, v->index, v);
2513  v = v->First();
2514  if (_ctrl_pressed && v->owner == _local_company) {
2515  StartStopVehicle(v, true);
2516  } else {
2518  }
2519  }
2520  return true;
2521  }
2522  return result;
2523 }
2524 
2525 void RebuildViewportOverlay(Window *w)
2526 {
2527  if (w->viewport->overlay != NULL &&
2528  w->viewport->overlay->GetCompanyMask() != 0 &&
2529  w->viewport->overlay->GetCargoMask() != 0) {
2530  w->viewport->overlay->RebuildCache();
2531  w->SetDirty();
2532  }
2533 }
2534 
2544 bool ScrollWindowTo(int x, int y, int z, Window *w, bool instant)
2545 {
2546  /* The slope cannot be acquired outside of the map, so make sure we are always within the map. */
2547  if (z == -1) {
2548  if ( x >= 0 && x <= (int)MapSizeX() * (int)TILE_SIZE - 1
2549  && y >= 0 && y <= (int)MapSizeY() * (int)TILE_SIZE - 1) {
2550  z = GetSlopePixelZ(x, y);
2551  } else {
2553  }
2554  }
2555 
2556  Point pt = MapXYZToViewport(w->viewport, x, y, z);
2558 
2559  if (w->viewport->dest_scrollpos_x == pt.x && w->viewport->dest_scrollpos_y == pt.y) return false;
2560 
2561  if (instant) {
2562  w->viewport->scrollpos_x = pt.x;
2563  w->viewport->scrollpos_y = pt.y;
2564  RebuildViewportOverlay(w);
2565  }
2566 
2567  w->viewport->dest_scrollpos_x = pt.x;
2568  w->viewport->dest_scrollpos_y = pt.y;
2569  return true;
2570 }
2571 
2579 bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
2580 {
2581  return ScrollWindowTo(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, -1, w, instant);
2582 }
2583 
2590 bool ScrollMainWindowToTile(TileIndex tile, bool instant)
2591 {
2592  return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, -1, instant);
2593 }
2594 
2600 {
2601  TileIndex old;
2602 
2603  old = _thd.redsq;
2604  _thd.redsq = tile;
2605 
2606  if (tile != old) {
2607  if (tile != INVALID_TILE) MarkTileDirtyByTile(tile);
2608  if (old != INVALID_TILE) MarkTileDirtyByTile(old);
2609  }
2610 }
2611 
2617 void SetTileSelectSize(int w, int h)
2618 {
2619  _thd.new_size.x = w * TILE_SIZE;
2620  _thd.new_size.y = h * TILE_SIZE;
2621  _thd.new_outersize.x = 0;
2622  _thd.new_outersize.y = 0;
2623 }
2624 
2625 void SetTileSelectBigSize(int ox, int oy, int sx, int sy)
2626 {
2627  _thd.offs.x = ox * TILE_SIZE;
2628  _thd.offs.y = oy * TILE_SIZE;
2629  _thd.new_outersize.x = sx * TILE_SIZE;
2630  _thd.new_outersize.y = sy * TILE_SIZE;
2631 }
2632 
2634 static HighLightStyle GetAutorailHT(int x, int y)
2635 {
2636  return HT_RAIL | _autorail_piece[x & TILE_UNIT_MASK][y & TILE_UNIT_MASK];
2637 }
2638 
2643 {
2644  this->pos.x = 0;
2645  this->pos.y = 0;
2646  this->new_pos.x = 0;
2647  this->new_pos.y = 0;
2648 }
2649 
2655 {
2656  return (this->place_mode & HT_DIAGONAL) != 0 && _ctrl_pressed && _left_button_down;
2657 }
2658 
2664 {
2665  return FindWindowById(this->window_class, this->window_number);
2666 }
2667 
2668 
2669 
2678 {
2679  int x1;
2680  int y1;
2681 
2682  HighLightStyle new_drawstyle = HT_NONE;
2683  bool new_diagonal = false;
2684 
2685  if ((_thd.place_mode & HT_DRAG_MASK) == HT_SPECIAL) {
2686  x1 = _thd.selend.x;
2687  y1 = _thd.selend.y;
2688  if (x1 != -1) {
2689  int x2 = _thd.selstart.x & ~TILE_UNIT_MASK;
2690  int y2 = _thd.selstart.y & ~TILE_UNIT_MASK;
2691  x1 &= ~TILE_UNIT_MASK;
2692  y1 &= ~TILE_UNIT_MASK;
2693 
2694  if (_thd.IsDraggingDiagonal()) {
2695  new_diagonal = true;
2696  } else {
2697  if (x1 >= x2) Swap(x1, x2);
2698  if (y1 >= y2) Swap(y1, y2);
2699  }
2700  _thd.new_pos.x = x1;
2701  _thd.new_pos.y = y1;
2702  _thd.new_size.x = x2 - x1;
2703  _thd.new_size.y = y2 - y1;
2704  if (!new_diagonal) {
2705  _thd.new_size.x += TILE_SIZE;
2706  _thd.new_size.y += TILE_SIZE;
2707  }
2708  new_drawstyle = _thd.next_drawstyle;
2709  }
2710  } else if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
2711  Point pt = GetTileBelowCursor();
2712  x1 = pt.x;
2713  y1 = pt.y;
2714  if (x1 != -1) {
2715  switch (_thd.place_mode & HT_DRAG_MASK) {
2716  case HT_RECT:
2717  new_drawstyle = HT_RECT;
2718  break;
2719  case HT_POINT:
2720  new_drawstyle = HT_POINT;
2721  x1 += TILE_SIZE / 2;
2722  y1 += TILE_SIZE / 2;
2723  break;
2724  case HT_RAIL:
2725  /* Draw one highlighted tile in any direction */
2726  new_drawstyle = GetAutorailHT(pt.x, pt.y);
2727  break;
2728  case HT_LINE:
2729  switch (_thd.place_mode & HT_DIR_MASK) {
2730  case HT_DIR_X: new_drawstyle = HT_LINE | HT_DIR_X; break;
2731  case HT_DIR_Y: new_drawstyle = HT_LINE | HT_DIR_Y; break;
2732 
2733  case HT_DIR_HU:
2734  case HT_DIR_HL:
2735  new_drawstyle = (pt.x & TILE_UNIT_MASK) + (pt.y & TILE_UNIT_MASK) <= TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
2736  break;
2737 
2738  case HT_DIR_VL:
2739  case HT_DIR_VR:
2740  new_drawstyle = (pt.x & TILE_UNIT_MASK) > (pt.y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2741  break;
2742 
2743  default: NOT_REACHED();
2744  }
2745  _thd.selstart.x = x1 & ~TILE_UNIT_MASK;
2746  _thd.selstart.y = y1 & ~TILE_UNIT_MASK;
2747  break;
2748  default:
2749  NOT_REACHED();
2750  break;
2751  }
2752  _thd.new_pos.x = x1 & ~TILE_UNIT_MASK;
2753  _thd.new_pos.y = y1 & ~TILE_UNIT_MASK;
2754  }
2755  }
2756 
2757  /* redraw selection */
2758  if (_thd.drawstyle != new_drawstyle ||
2759  _thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y ||
2760  _thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y ||
2761  _thd.outersize.x != _thd.new_outersize.x ||
2762  _thd.outersize.y != _thd.new_outersize.y ||
2763  _thd.diagonal != new_diagonal) {
2764  /* Clear the old tile selection? */
2766 
2767  _thd.drawstyle = new_drawstyle;
2768  _thd.pos = _thd.new_pos;
2769  _thd.size = _thd.new_size;
2770  _thd.outersize = _thd.new_outersize;
2771  _thd.diagonal = new_diagonal;
2772  _thd.dirty = 0xff;
2773 
2774  /* Draw the new tile selection? */
2775  if ((new_drawstyle & HT_DRAG_MASK) != HT_NONE) SetSelectionTilesDirty();
2776  }
2777 }
2778 
2786 static inline void ShowMeasurementTooltips(StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_cond = TCC_LEFT_CLICK)
2787 {
2788  if (!_settings_client.gui.measure_tooltip) return;
2789  GuiShowTooltips(_thd.GetCallbackWnd(), str, paramcount, params, close_cond);
2790 }
2791 
2794 {
2795  _thd.select_method = method;
2796  _thd.select_proc = process;
2797  _thd.selend.x = TileX(tile) * TILE_SIZE;
2798  _thd.selstart.x = TileX(tile) * TILE_SIZE;
2799  _thd.selend.y = TileY(tile) * TILE_SIZE;
2800  _thd.selstart.y = TileY(tile) * TILE_SIZE;
2801 
2802  /* Needed so several things (road, autoroad, bridges, ...) are placed correctly.
2803  * In effect, placement starts from the centre of a tile
2804  */
2805  if (method == VPM_X_OR_Y || method == VPM_FIX_X || method == VPM_FIX_Y) {
2806  _thd.selend.x += TILE_SIZE / 2;
2807  _thd.selend.y += TILE_SIZE / 2;
2808  _thd.selstart.x += TILE_SIZE / 2;
2809  _thd.selstart.y += TILE_SIZE / 2;
2810  }
2811 
2812  HighLightStyle others = _thd.place_mode & ~(HT_DRAG_MASK | HT_DIR_MASK);
2813  if ((_thd.place_mode & HT_DRAG_MASK) == HT_RECT) {
2814  _thd.place_mode = HT_SPECIAL | others;
2815  _thd.next_drawstyle = HT_RECT | others;
2816  } else if (_thd.place_mode & (HT_RAIL | HT_LINE)) {
2817  _thd.place_mode = HT_SPECIAL | others;
2818  _thd.next_drawstyle = _thd.drawstyle | others;
2819  } else {
2820  _thd.place_mode = HT_SPECIAL | others;
2821  _thd.next_drawstyle = HT_POINT | others;
2822  }
2824 }
2825 
2826 void VpSetPlaceSizingLimit(int limit)
2827 {
2828  _thd.sizelimit = limit;
2829 }
2830 
2837 {
2838  uint64 distance = DistanceManhattan(from, to) + 1;
2839 
2840  _thd.selend.x = TileX(to) * TILE_SIZE;
2841  _thd.selend.y = TileY(to) * TILE_SIZE;
2842  _thd.selstart.x = TileX(from) * TILE_SIZE;
2843  _thd.selstart.y = TileY(from) * TILE_SIZE;
2844  _thd.next_drawstyle = HT_RECT;
2845 
2846  /* show measurement only if there is any length to speak of */
2847  if (distance > 1) ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1, &distance, TCC_HOVER);
2848 }
2849 
2850 static void VpStartPreSizing()
2851 {
2852  _thd.selend.x = -1;
2854 }
2855 
2861 {
2862  int fxpy = _tile_fract_coords.x + _tile_fract_coords.y;
2863  int sxpy = (_thd.selend.x & TILE_UNIT_MASK) + (_thd.selend.y & TILE_UNIT_MASK);
2864  int fxmy = _tile_fract_coords.x - _tile_fract_coords.y;
2865  int sxmy = (_thd.selend.x & TILE_UNIT_MASK) - (_thd.selend.y & TILE_UNIT_MASK);
2866 
2867  switch (mode) {
2868  default: NOT_REACHED();
2869  case 0: // end piece is lower right
2870  if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
2871  if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
2872  return HT_DIR_Y;
2873 
2874  case 1:
2875  if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
2876  if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
2877  return HT_DIR_Y;
2878 
2879  case 2:
2880  if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
2881  if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
2882  return HT_DIR_X;
2883 
2884  case 3:
2885  if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
2886  if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
2887  return HT_DIR_X;
2888  }
2889 }
2890 
2904 static bool SwapDirection(HighLightStyle style, TileIndex start_tile, TileIndex end_tile)
2905 {
2906  uint start_x = TileX(start_tile);
2907  uint start_y = TileY(start_tile);
2908  uint end_x = TileX(end_tile);
2909  uint end_y = TileY(end_tile);
2910 
2911  switch (style & HT_DRAG_MASK) {
2912  case HT_RAIL:
2913  case HT_LINE: return (end_x > start_x || (end_x == start_x && end_y > start_y));
2914 
2915  case HT_RECT:
2916  case HT_POINT: return (end_x != start_x && end_y < start_y);
2917  default: NOT_REACHED();
2918  }
2919 
2920  return false;
2921 }
2922 
2938 static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile)
2939 {
2940  bool swap = SwapDirection(style, start_tile, end_tile);
2941  uint h0, h1; // Start height and end height.
2942 
2943  if (start_tile == end_tile) return 0;
2944  if (swap) Swap(start_tile, end_tile);
2945 
2946  switch (style & HT_DRAG_MASK) {
2947  case HT_RECT: {
2948  static const TileIndexDiffC heightdiff_area_by_dir[] = {
2949  /* Start */ {1, 0}, /* Dragging east */ {0, 0}, // Dragging south
2950  /* End */ {0, 1}, /* Dragging east */ {1, 1} // Dragging south
2951  };
2952 
2953  /* In the case of an area we can determine whether we were dragging south or
2954  * east by checking the X-coordinates of the tiles */
2955  byte style_t = (byte)(TileX(end_tile) > TileX(start_tile));
2956  start_tile = TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_area_by_dir[style_t]));
2957  end_tile = TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_area_by_dir[2 + style_t]));
2958  /* FALL THROUGH */
2959  }
2960 
2961  case HT_POINT:
2962  h0 = TileHeight(start_tile);
2963  h1 = TileHeight(end_tile);
2964  break;
2965  default: { // All other types, this is mostly only line/autorail
2966  static const HighLightStyle flip_style_direction[] = {
2968  };
2969  static const TileIndexDiffC heightdiff_line_by_dir[] = {
2970  /* Start */ {1, 0}, {1, 1}, /* HT_DIR_X */ {0, 1}, {1, 1}, // HT_DIR_Y
2971  /* Start */ {1, 0}, {0, 0}, /* HT_DIR_HU */ {1, 0}, {1, 1}, // HT_DIR_HL
2972  /* Start */ {1, 0}, {1, 1}, /* HT_DIR_VL */ {0, 1}, {1, 1}, // HT_DIR_VR
2973 
2974  /* Start */ {0, 1}, {0, 0}, /* HT_DIR_X */ {1, 0}, {0, 0}, // HT_DIR_Y
2975  /* End */ {0, 1}, {0, 0}, /* HT_DIR_HU */ {1, 1}, {0, 1}, // HT_DIR_HL
2976  /* End */ {1, 0}, {0, 0}, /* HT_DIR_VL */ {0, 0}, {0, 1}, // HT_DIR_VR
2977  };
2978 
2979  distance %= 2; // we're only interested if the distance is even or uneven
2980  style &= HT_DIR_MASK;
2981 
2982  /* To handle autorail, we do some magic to be able to use a lookup table.
2983  * Firstly if we drag the other way around, we switch start&end, and if needed
2984  * also flip the drag-position. Eg if it was on the left, and the distance is even
2985  * that means the end, which is now the start is on the right */
2986  if (swap && distance == 0) style = flip_style_direction[style];
2987 
2988  /* Use lookup table for start-tile based on HighLightStyle direction */
2989  byte style_t = style * 2;
2990  assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
2991  h0 = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t])));
2992  uint ht = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t + 1])));
2993  h0 = max(h0, ht);
2994 
2995  /* Use lookup table for end-tile based on HighLightStyle direction
2996  * flip around side (lower/upper, left/right) based on distance */
2997  if (distance == 0) style_t = flip_style_direction[style] * 2;
2998  assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
2999  h1 = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t])));
3000  ht = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t + 1])));
3001  h1 = max(h1, ht);
3002  break;
3003  }
3004  }
3005 
3006  if (swap) Swap(h0, h1);
3007  return (int)(h1 - h0) * TILE_HEIGHT_STEP;
3008 }
3009 
3010 static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF};
3011 
3018 static void CheckUnderflow(int &test, int &other, int mult)
3019 {
3020  if (test >= 0) return;
3021 
3022  other += mult * test;
3023  test = 0;
3024 }
3025 
3033 static void CheckOverflow(int &test, int &other, int max, int mult)
3034 {
3035  if (test <= max) return;
3036 
3037  other += mult * (test - max);
3038  test = max;
3039 }
3040 
3042 static void CalcRaildirsDrawstyle(int x, int y, int method)
3043 {
3044  HighLightStyle b;
3045 
3046  int dx = _thd.selstart.x - (_thd.selend.x & ~TILE_UNIT_MASK);
3047  int dy = _thd.selstart.y - (_thd.selend.y & ~TILE_UNIT_MASK);
3048  uint w = abs(dx) + TILE_SIZE;
3049  uint h = abs(dy) + TILE_SIZE;
3050 
3051  if (method & ~(VPM_RAILDIRS | VPM_SIGNALDIRS)) {
3052  /* We 'force' a selection direction; first four rail buttons. */
3053  method &= ~(VPM_RAILDIRS | VPM_SIGNALDIRS);
3054  int raw_dx = _thd.selstart.x - _thd.selend.x;
3055  int raw_dy = _thd.selstart.y - _thd.selend.y;
3056  switch (method) {
3057  case VPM_FIX_X:
3058  b = HT_LINE | HT_DIR_Y;
3059  x = _thd.selstart.x;
3060  break;
3061 
3062  case VPM_FIX_Y:
3063  b = HT_LINE | HT_DIR_X;
3064  y = _thd.selstart.y;
3065  break;
3066 
3067  case VPM_FIX_HORIZONTAL:
3068  if (dx == -dy) {
3069  /* We are on a straight horizontal line. Determine the 'rail'
3070  * to build based the sub tile location. */
3072  } else {
3073  /* We are not on a straight line. Determine the rail to build
3074  * based on whether we are above or below it. */
3075  b = dx + dy >= (int)TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
3076 
3077  /* Calculate where a horizontal line through the start point and
3078  * a vertical line from the selected end point intersect and
3079  * use that point as the end point. */
3080  int offset = (raw_dx - raw_dy) / 2;
3081  x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
3082  y = _thd.selstart.y + (offset & ~TILE_UNIT_MASK);
3083 
3084  /* 'Build' the last half rail tile if needed */
3085  if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
3086  if (dx + dy >= (int)TILE_SIZE) {
3087  x += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
3088  } else {
3089  y += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
3090  }
3091  }
3092 
3093  /* Make sure we do not overflow the map! */
3094  CheckUnderflow(x, y, 1);
3095  CheckUnderflow(y, x, 1);
3096  CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, 1);
3097  CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, 1);
3098  assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
3099  }
3100  break;
3101 
3102  case VPM_FIX_VERTICAL:
3103  if (dx == dy) {
3104  /* We are on a straight vertical line. Determine the 'rail'
3105  * to build based the sub tile location. */
3106  b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
3107  } else {
3108  /* We are not on a straight line. Determine the rail to build
3109  * based on whether we are left or right from it. */
3110  b = dx < dy ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
3111 
3112  /* Calculate where a vertical line through the start point and
3113  * a horizontal line from the selected end point intersect and
3114  * use that point as the end point. */
3115  int offset = (raw_dx + raw_dy + (int)TILE_SIZE) / 2;
3116  x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
3117  y = _thd.selstart.y - (offset & ~TILE_UNIT_MASK);
3118 
3119  /* 'Build' the last half rail tile if needed */
3120  if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
3121  if (dx - dy < 0) {
3122  y += (dx > dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
3123  } else {
3124  x += (dx < dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
3125  }
3126  }
3127 
3128  /* Make sure we do not overflow the map! */
3129  CheckUnderflow(x, y, -1);
3130  CheckUnderflow(y, x, -1);
3131  CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, -1);
3132  CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, -1);
3133  assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
3134  }
3135  break;
3136 
3137  default:
3138  NOT_REACHED();
3139  }
3140  } else if (TileVirtXY(_thd.selstart.x, _thd.selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile
3141  if (method & VPM_RAILDIRS) {
3142  b = GetAutorailHT(x, y);
3143  } else { // rect for autosignals on one tile
3144  b = HT_RECT;
3145  }
3146  } else if (h == TILE_SIZE) { // Is this in X direction?
3147  if (dx == (int)TILE_SIZE) { // 2x1 special handling
3148  b = (Check2x1AutoRail(3)) | HT_LINE;
3149  } else if (dx == -(int)TILE_SIZE) {
3150  b = (Check2x1AutoRail(2)) | HT_LINE;
3151  } else {
3152  b = HT_LINE | HT_DIR_X;
3153  }
3154  y = _thd.selstart.y;
3155  } else if (w == TILE_SIZE) { // Or Y direction?
3156  if (dy == (int)TILE_SIZE) { // 2x1 special handling
3157  b = (Check2x1AutoRail(1)) | HT_LINE;
3158  } else if (dy == -(int)TILE_SIZE) { // 2x1 other direction
3159  b = (Check2x1AutoRail(0)) | HT_LINE;
3160  } else {
3161  b = HT_LINE | HT_DIR_Y;
3162  }
3163  x = _thd.selstart.x;
3164  } else if (w > h * 2) { // still count as x dir?
3165  b = HT_LINE | HT_DIR_X;
3166  y = _thd.selstart.y;
3167  } else if (h > w * 2) { // still count as y dir?
3168  b = HT_LINE | HT_DIR_Y;
3169  x = _thd.selstart.x;
3170  } else { // complicated direction
3171  int d = w - h;
3172  _thd.selend.x = _thd.selend.x & ~TILE_UNIT_MASK;
3173  _thd.selend.y = _thd.selend.y & ~TILE_UNIT_MASK;
3174 
3175  /* four cases. */
3176  if (x > _thd.selstart.x) {
3177  if (y > _thd.selstart.y) {
3178  /* south */
3179  if (d == 0) {
3180  b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
3181  } else if (d >= 0) {
3182  x = _thd.selstart.x + h;
3183  b = HT_LINE | HT_DIR_VL;
3184  } else {
3185  y = _thd.selstart.y + w;
3186  b = HT_LINE | HT_DIR_VR;
3187  }
3188  } else {
3189  /* west */
3190  if (d == 0) {
3192  } else if (d >= 0) {
3193  x = _thd.selstart.x + h;
3194  b = HT_LINE | HT_DIR_HL;
3195  } else {
3196  y = _thd.selstart.y - w;
3197  b = HT_LINE | HT_DIR_HU;
3198  }
3199  }
3200  } else {
3201  if (y > _thd.selstart.y) {
3202  /* east */
3203  if (d == 0) {
3205  } else if (d >= 0) {
3206  x = _thd.selstart.x - h;
3207  b = HT_LINE | HT_DIR_HU;
3208  } else {
3209  y = _thd.selstart.y + w;
3210  b = HT_LINE | HT_DIR_HL;
3211  }
3212  } else {
3213  /* north */
3214  if (d == 0) {
3215  b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
3216  } else if (d >= 0) {
3217  x = _thd.selstart.x - h;
3218  b = HT_LINE | HT_DIR_VR;
3219  } else {
3220  y = _thd.selstart.y - w;
3221  b = HT_LINE | HT_DIR_VL;
3222  }
3223  }
3224  }
3225  }
3226 
3228  TileIndex t0 = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
3229  TileIndex t1 = TileVirtXY(x, y);
3230  uint distance = DistanceManhattan(t0, t1) + 1;
3231  byte index = 0;
3232  uint64 params[2];
3233 
3234  if (distance != 1) {
3235  int heightdiff = CalcHeightdiff(b, distance, t0, t1);
3236  /* If we are showing a tooltip for horizontal or vertical drags,
3237  * 2 tiles have a length of 1. To bias towards the ceiling we add
3238  * one before division. It feels more natural to count 3 lengths as 2 */
3239  if ((b & HT_DIR_MASK) != HT_DIR_X && (b & HT_DIR_MASK) != HT_DIR_Y) {
3240  distance = CeilDiv(distance, 2);
3241  }
3242 
3243  params[index++] = distance;
3244  if (heightdiff != 0) params[index++] = heightdiff;
3245  }
3246 
3247  ShowMeasurementTooltips(measure_strings_length[index], index, params);
3248  }
3249 
3250  _thd.selend.x = x;
3251  _thd.selend.y = y;
3252  _thd.next_drawstyle = b;
3253 }
3254 
3263 {
3264  int sx, sy;
3265  HighLightStyle style;
3266 
3267  if (x == -1) {
3268  _thd.selend.x = -1;
3269  return;
3270  }
3271 
3272  /* Special handling of drag in any (8-way) direction */
3273  if (method & (VPM_RAILDIRS | VPM_SIGNALDIRS)) {
3274  _thd.selend.x = x;
3275  _thd.selend.y = y;
3276  CalcRaildirsDrawstyle(x, y, method);
3277  return;
3278  }
3279 
3280  /* Needed so level-land is placed correctly */
3281  if ((_thd.next_drawstyle & HT_DRAG_MASK) == HT_POINT) {
3282  x += TILE_SIZE / 2;
3283  y += TILE_SIZE / 2;
3284  }
3285 
3286  sx = _thd.selstart.x;
3287  sy = _thd.selstart.y;
3288 
3289  int limit = 0;
3290 
3291  switch (method) {
3292  case VPM_X_OR_Y: // drag in X or Y direction
3293  if (abs(sy - y) < abs(sx - x)) {
3294  y = sy;
3295  style = HT_DIR_X;
3296  } else {
3297  x = sx;
3298  style = HT_DIR_Y;
3299  }
3300  goto calc_heightdiff_single_direction;
3301 
3302  case VPM_X_LIMITED: // Drag in X direction (limited size).
3303  limit = (_thd.sizelimit - 1) * TILE_SIZE;
3304  /* FALL THROUGH */
3305 
3306  case VPM_FIX_X: // drag in Y direction
3307  x = sx;
3308  style = HT_DIR_Y;
3309  goto calc_heightdiff_single_direction;
3310 
3311  case VPM_Y_LIMITED: // Drag in Y direction (limited size).
3312  limit = (_thd.sizelimit - 1) * TILE_SIZE;
3313  /* FALL THROUGH */
3314 
3315  case VPM_FIX_Y: // drag in X direction
3316  y = sy;
3317  style = HT_DIR_X;
3318 
3319 calc_heightdiff_single_direction:;
3320  if (limit > 0) {
3321  x = sx + Clamp(x - sx, -limit, limit);
3322  y = sy + Clamp(y - sy, -limit, limit);
3323  }
3325  TileIndex t0 = TileVirtXY(sx, sy);
3326  TileIndex t1 = TileVirtXY(x, y);
3327  uint distance = DistanceManhattan(t0, t1) + 1;
3328  byte index = 0;
3329  uint64 params[2];
3330 
3331  if (distance != 1) {
3332  /* With current code passing a HT_LINE style to calculate the height
3333  * difference is enough. However if/when a point-tool is created
3334  * with this method, function should be called with new_style (below)
3335  * instead of HT_LINE | style case HT_POINT is handled specially
3336  * new_style := (_thd.next_drawstyle & HT_RECT) ? HT_LINE | style : _thd.next_drawstyle; */
3337  int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1);
3338 
3339  params[index++] = distance;
3340  if (heightdiff != 0) params[index++] = heightdiff;
3341  }
3342 
3343  ShowMeasurementTooltips(measure_strings_length[index], index, params);
3344  }
3345  break;
3346 
3347  case VPM_X_AND_Y_LIMITED: // Drag an X by Y constrained rect area.
3348  limit = (_thd.sizelimit - 1) * TILE_SIZE;
3349  x = sx + Clamp(x - sx, -limit, limit);
3350  y = sy + Clamp(y - sy, -limit, limit);
3351  /* FALL THROUGH */
3352 
3353  case VPM_X_AND_Y: // drag an X by Y area
3355  static const StringID measure_strings_area[] = {
3356  STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF
3357  };
3358 
3359  TileIndex t0 = TileVirtXY(sx, sy);
3360  TileIndex t1 = TileVirtXY(x, y);
3361  uint dx = Delta(TileX(t0), TileX(t1)) + 1;
3362  uint dy = Delta(TileY(t0), TileY(t1)) + 1;
3363  byte index = 0;
3364  uint64 params[3];
3365 
3366  /* If dragging an area (eg dynamite tool) and it is actually a single
3367  * row/column, change the type to 'line' to get proper calculation for height */
3368  style = (HighLightStyle)_thd.next_drawstyle;
3369  if (_thd.IsDraggingDiagonal()) {
3370  /* Determine the "area" of the diagonal dragged selection.
3371  * We assume the area is the number of tiles along the X
3372  * edge and the number of tiles along the Y edge. However,
3373  * multiplying these two numbers does not give the exact
3374  * number of tiles; basically we are counting the black
3375  * squares on a chess board and ignore the white ones to
3376  * make the tile counts at the edges match up. There is no
3377  * other way to make a proper count though.
3378  *
3379  * First convert to the rotated coordinate system. */
3380  int dist_x = TileX(t0) - TileX(t1);
3381  int dist_y = TileY(t0) - TileY(t1);
3382  int a_max = dist_x + dist_y;
3383  int b_max = dist_y - dist_x;
3384 
3385  /* Now determine the size along the edge, but due to the
3386  * chess board principle this counts double. */
3387  a_max = abs(a_max + (a_max > 0 ? 2 : -2)) / 2;
3388  b_max = abs(b_max + (b_max > 0 ? 2 : -2)) / 2;
3389 
3390  /* We get a 1x1 on normal 2x1 rectangles, due to it being
3391  * a seen as two sides. As the result for actual building
3392  * will be the same as non-diagonal dragging revert to that
3393  * behaviour to give it a more normally looking size. */
3394  if (a_max != 1 || b_max != 1) {
3395  dx = a_max;
3396  dy = b_max;
3397  }
3398  } else if (style & HT_RECT) {
3399  if (dx == 1) {
3400  style = HT_LINE | HT_DIR_Y;
3401  } else if (dy == 1) {
3402  style = HT_LINE | HT_DIR_X;
3403  }
3404  }
3405 
3406  if (dx != 1 || dy != 1) {
3407  int heightdiff = CalcHeightdiff(style, 0, t0, t1);
3408 
3409  params[index++] = dx - (style & HT_POINT ? 1 : 0);
3410  params[index++] = dy - (style & HT_POINT ? 1 : 0);
3411  if (heightdiff != 0) params[index++] = heightdiff;
3412  }
3413 
3414  ShowMeasurementTooltips(measure_strings_area[index], index, params);
3415  }
3416  break;
3417 
3418  default: NOT_REACHED();
3419  }
3420 
3421  _thd.selend.x = x;
3422  _thd.selend.y = y;
3423 }
3424 
3430 {
3432 
3433  /* stop drag mode if the window has been closed */
3434  Window *w = _thd.GetCallbackWnd();
3435  if (w == NULL) {
3436  ResetObjectToPlace();
3437  return ES_HANDLED;
3438  }
3439 
3440  /* while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) */
3441  if (_left_button_down) {
3442  w->OnPlaceDrag(_thd.select_method, _thd.select_proc, GetTileBelowCursor());
3443  return ES_HANDLED;
3444  }
3445 
3446  /* mouse button released..
3447  * keep the selected tool, but reset it to the original mode. */
3449  HighLightStyle others = _thd.place_mode & ~(HT_DRAG_MASK | HT_DIR_MASK);
3450  if ((_thd.next_drawstyle & HT_DRAG_MASK) == HT_RECT) {
3451  _thd.place_mode = HT_RECT | others;
3452  } else if (_thd.select_method & VPM_SIGNALDIRS) {
3453  _thd.place_mode = HT_RECT | others;
3454  } else if (_thd.select_method & VPM_RAILDIRS) {
3455  _thd.place_mode = (_thd.select_method & ~VPM_RAILDIRS) ? _thd.next_drawstyle : (HT_RAIL | others);
3456  } else {
3457  _thd.place_mode = HT_POINT | others;
3458  }
3459  SetTileSelectSize(1, 1);
3460 
3461  w->OnPlaceMouseUp(_thd.select_method, _thd.select_proc, _thd.selend, TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y));
3462 
3463  return ES_HANDLED;
3464 }
3465 
3466 void SetObjectToPlaceWnd(CursorID icon, PaletteID pal, HighLightStyle mode, Window *w)
3467 {
3468  SetObjectToPlace(icon, pal, mode, w->window_class, w->window_number);
3469 }
3470 
3471 #include "table/animcursors.h"
3472 
3473 void SetObjectToPlace(CursorID icon, PaletteID pal, HighLightStyle mode, WindowClass window_class, WindowNumber window_num)
3474 {
3475  if (_thd.window_class != WC_INVALID) {
3476  /* Undo clicking on button and drag & drop */
3477  Window *w = _thd.GetCallbackWnd();
3478  /* Call the abort function, but set the window class to something
3479  * that will never be used to avoid infinite loops. Setting it to
3480  * the 'next' window class must not be done because recursion into
3481  * this function might in some cases reset the newly set object to
3482  * place or not properly reset the original selection. */
3483  _thd.window_class = WC_INVALID;
3484  if (w != NULL) w->OnPlaceObjectAbort();
3485  }
3486 
3487  /* Mark the old selection dirty, in case the selection shape or colour changes */
3489 
3490  SetTileSelectSize(1, 1);
3491 
3492  _thd.make_square_red = false;
3493 
3494  if (mode == HT_DRAG) { // HT_DRAG is for dragdropping trains in the depot window
3495  mode = HT_NONE;
3497  } else {
3499  }
3500 
3501  _thd.place_mode = mode;
3502  _thd.window_class = window_class;
3503  _thd.window_number = window_num;
3504 
3505  if ((mode & HT_DRAG_MASK) == HT_SPECIAL) { // special tools, like tunnels or docks start with presizing mode
3506  VpStartPreSizing();
3507  }
3508 
3509  if ((icon & ANIMCURSOR_FLAG) != 0) {
3510  SetAnimatedMouseCursor(_animcursors[icon & ~ANIMCURSOR_FLAG]);
3511  } else {
3512  SetMouseCursor(icon, pal);
3513  }
3514 
3515 }
3516 
3517 void ResetObjectToPlace()
3518 {
3519  SetObjectToPlace(SPR_CURSOR_MOUSE, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
3520 }
3521 
3522 Point GetViewportStationMiddle(const ViewPort *vp, const Station *st)
3523 {
3524  int x = TileX(st->xy) * TILE_SIZE;
3525  int y = TileY(st->xy) * TILE_SIZE;
3526  int z = GetSlopePixelZ(Clamp(x, 0, MapSizeX() * TILE_SIZE - 1), Clamp(y, 0, MapSizeY() * TILE_SIZE - 1));
3527 
3528  Point p = RemapCoords(x, y, z);
3529  p.x = UnScaleByZoom(p.x - vp->virtual_left, vp->zoom) + vp->left;
3530  p.y = UnScaleByZoom(p.y - vp->virtual_top, vp->zoom) + vp->top;
3531  return p;
3532 }
3533 
3538 };
3539 
3542 #ifdef WITH_SSE
3543  { &ViewportSortParentSpritesSSE41Checker, &ViewportSortParentSpritesSSE41 },
3544 #endif
3546 };
3547 
3550 {
3551  for (uint i = 0; i < lengthof(_vp_sprite_sorters); i++) {
3552  if (_vp_sprite_sorters[i].fct_checker()) {
3553  _vp_sprite_sorter = _vp_sprite_sorters[i].fct_sorter;
3554  break;
3555  }
3556  }
3557  assert(_vp_sprite_sorter != NULL);
3558 }