OpenTTD: src/viewport.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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