Fix #9521: Don't load at just removed docks that were part of a multi-dock station...
[openttd-github.git] / src / terraform_gui.cpp
blobf710cecc5c823d3139b6d66adbf4856ed29f286d
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file terraform_gui.cpp GUI related to terraforming the map. */
10 #include "stdafx.h"
11 #include "core/backup_type.hpp"
12 #include "clear_map.h"
13 #include "company_func.h"
14 #include "company_base.h"
15 #include "gui.h"
16 #include "window_gui.h"
17 #include "window_func.h"
18 #include "viewport_func.h"
19 #include "command_func.h"
20 #include "signs_func.h"
21 #include "sound_func.h"
22 #include "base_station_base.h"
23 #include "textbuf_gui.h"
24 #include "genworld.h"
25 #include "tree_map.h"
26 #include "landscape_type.h"
27 #include "tilehighlight_func.h"
28 #include "strings_func.h"
29 #include "newgrf_object.h"
30 #include "object.h"
31 #include "hotkeys.h"
32 #include "engine_base.h"
33 #include "terraform_gui.h"
34 #include "zoom_func.h"
36 #include "widgets/terraform_widget.h"
38 #include "table/strings.h"
40 #include "safeguards.h"
42 void CcTerraform(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
44 if (result.Succeeded()) {
45 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
46 } else {
47 extern TileIndex _terraform_err_tile;
48 SetRedErrorSquare(_terraform_err_tile);
53 /** Scenario editor command that generates desert areas */
54 static void GenerateDesertArea(TileIndex end, TileIndex start)
56 if (_game_mode != GM_EDITOR) return;
58 Backup<bool> old_generating_world(_generating_world, true, FILE_LINE);
60 TileArea ta(start, end);
61 for (TileIndex tile : ta) {
62 SetTropicZone(tile, (_ctrl_pressed) ? TROPICZONE_NORMAL : TROPICZONE_DESERT);
63 DoCommandP(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
64 MarkTileDirtyByTile(tile);
66 old_generating_world.Restore();
67 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
70 /** Scenario editor command that generates rocky areas */
71 static void GenerateRockyArea(TileIndex end, TileIndex start)
73 if (_game_mode != GM_EDITOR) return;
75 bool success = false;
76 TileArea ta(start, end);
78 for (TileIndex tile : ta) {
79 switch (GetTileType(tile)) {
80 case MP_TREES:
81 if (GetTreeGround(tile) == TREE_GROUND_SHORE) continue;
82 FALLTHROUGH;
84 case MP_CLEAR:
85 MakeClear(tile, CLEAR_ROCKS, 3);
86 break;
88 default:
89 continue;
91 MarkTileDirtyByTile(tile);
92 success = true;
95 if (success && _settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, end);
98 /**
99 * A central place to handle all X_AND_Y dragged GUI functions.
100 * @param proc Procedure related to the dragging
101 * @param start_tile Begin of the dragging
102 * @param end_tile End of the dragging
103 * @return Returns true if the action was found and handled, and false otherwise. This
104 * allows for additional implements that are more local. For example X_Y drag
105 * of convertrail which belongs in rail_gui.cpp and not terraform_gui.cpp
107 bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc, TileIndex start_tile, TileIndex end_tile)
109 if (!_settings_game.construction.freeform_edges) {
110 /* When end_tile is MP_VOID, the error tile will not be visible to the
111 * user. This happens when terraforming at the southern border. */
112 if (TileX(end_tile) == MapMaxX()) end_tile += TileDiffXY(-1, 0);
113 if (TileY(end_tile) == MapMaxY()) end_tile += TileDiffXY(0, -1);
116 switch (proc) {
117 case DDSP_DEMOLISH_AREA:
118 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 1 : 0, CMD_CLEAR_AREA | CMD_MSG(STR_ERROR_CAN_T_CLEAR_THIS_AREA), CcPlaySound_EXPLOSION);
119 break;
120 case DDSP_RAISE_AND_LEVEL_AREA:
121 DoCommandP(end_tile, start_tile, LM_RAISE << 1 | (_ctrl_pressed ? 1 : 0), CMD_LEVEL_LAND | CMD_MSG(STR_ERROR_CAN_T_RAISE_LAND_HERE), CcTerraform);
122 break;
123 case DDSP_LOWER_AND_LEVEL_AREA:
124 DoCommandP(end_tile, start_tile, LM_LOWER << 1 | (_ctrl_pressed ? 1 : 0), CMD_LEVEL_LAND | CMD_MSG(STR_ERROR_CAN_T_LOWER_LAND_HERE), CcTerraform);
125 break;
126 case DDSP_LEVEL_AREA:
127 DoCommandP(end_tile, start_tile, LM_LEVEL << 1 | (_ctrl_pressed ? 1 : 0), CMD_LEVEL_LAND | CMD_MSG(STR_ERROR_CAN_T_LEVEL_LAND_HERE), CcTerraform);
128 break;
129 case DDSP_CREATE_ROCKS:
130 GenerateRockyArea(end_tile, start_tile);
131 break;
132 case DDSP_CREATE_DESERT:
133 GenerateDesertArea(end_tile, start_tile);
134 break;
135 default:
136 return false;
139 return true;
143 * Start a drag for demolishing an area.
144 * @param tile Position of one corner.
146 void PlaceProc_DemolishArea(TileIndex tile)
148 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_DEMOLISH_AREA);
151 /** Terra form toolbar managing class. */
152 struct TerraformToolbarWindow : Window {
153 int last_user_action; ///< Last started user action.
155 TerraformToolbarWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
157 /* This is needed as we like to have the tree available on OnInit. */
158 this->CreateNestedTree();
159 this->FinishInitNested(window_number);
160 this->last_user_action = WIDGET_LIST_END;
163 ~TerraformToolbarWindow()
167 void OnInit() override
169 /* Don't show the place object button when there are no objects to place. */
170 NWidgetStacked *show_object = this->GetWidget<NWidgetStacked>(WID_TT_SHOW_PLACE_OBJECT);
171 show_object->SetDisplayedPlane(ObjectClass::GetUIClassCount() != 0 ? 0 : SZSP_NONE);
174 void OnClick(Point pt, int widget, int click_count) override
176 if (widget < WID_TT_BUTTONS_START) return;
178 switch (widget) {
179 case WID_TT_LOWER_LAND: // Lower land button
180 HandlePlacePushButton(this, WID_TT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT | HT_DIAGONAL);
181 this->last_user_action = widget;
182 break;
184 case WID_TT_RAISE_LAND: // Raise land button
185 HandlePlacePushButton(this, WID_TT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT | HT_DIAGONAL);
186 this->last_user_action = widget;
187 break;
189 case WID_TT_LEVEL_LAND: // Level land button
190 HandlePlacePushButton(this, WID_TT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
191 this->last_user_action = widget;
192 break;
194 case WID_TT_DEMOLISH: // Demolish aka dynamite button
195 HandlePlacePushButton(this, WID_TT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
196 this->last_user_action = widget;
197 break;
199 case WID_TT_BUY_LAND: // Buy land button
200 HandlePlacePushButton(this, WID_TT_BUY_LAND, SPR_CURSOR_BUY_LAND, HT_RECT);
201 this->last_user_action = widget;
202 break;
204 case WID_TT_PLANT_TREES: // Plant trees button
205 ShowBuildTreesToolbar();
206 break;
208 case WID_TT_PLACE_SIGN: // Place sign button
209 HandlePlacePushButton(this, WID_TT_PLACE_SIGN, SPR_CURSOR_SIGN, HT_RECT);
210 this->last_user_action = widget;
211 break;
213 case WID_TT_PLACE_OBJECT: // Place object button
214 ShowBuildObjectPicker();
215 break;
217 default: NOT_REACHED();
221 void OnPlaceObject(Point pt, TileIndex tile) override
223 switch (this->last_user_action) {
224 case WID_TT_LOWER_LAND: // Lower land button
225 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LOWER_AND_LEVEL_AREA);
226 break;
228 case WID_TT_RAISE_LAND: // Raise land button
229 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
230 break;
232 case WID_TT_LEVEL_LAND: // Level land button
233 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
234 break;
236 case WID_TT_DEMOLISH: // Demolish aka dynamite button
237 PlaceProc_DemolishArea(tile);
238 break;
240 case WID_TT_BUY_LAND: // Buy land button
241 DoCommandP(tile, OBJECT_OWNED_LAND, 0, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_PURCHASE_THIS_LAND), CcPlaySound_CONSTRUCTION_RAIL);
242 break;
244 case WID_TT_PLACE_SIGN: // Place sign button
245 PlaceProc_Sign(tile);
246 break;
248 default: NOT_REACHED();
252 void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
254 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
257 Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
259 Point pt = GetToolbarAlignedWindowPosition(sm_width);
260 pt.y += sm_height;
261 return pt;
264 void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
266 if (pt.x != -1) {
267 switch (select_proc) {
268 default: NOT_REACHED();
269 case DDSP_DEMOLISH_AREA:
270 case DDSP_RAISE_AND_LEVEL_AREA:
271 case DDSP_LOWER_AND_LEVEL_AREA:
272 case DDSP_LEVEL_AREA:
273 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
274 break;
279 void OnPlaceObjectAbort() override
281 this->RaiseButtons();
284 static HotkeyList hotkeys;
288 * Handler for global hotkeys of the TerraformToolbarWindow.
289 * @param hotkey Hotkey
290 * @return ES_HANDLED if hotkey was accepted.
292 static EventState TerraformToolbarGlobalHotkeys(int hotkey)
294 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
295 Window *w = ShowTerraformToolbar(nullptr);
296 if (w == nullptr) return ES_NOT_HANDLED;
297 return w->OnHotkey(hotkey);
300 static Hotkey terraform_hotkeys[] = {
301 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_TT_LOWER_LAND),
302 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_TT_RAISE_LAND),
303 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_TT_LEVEL_LAND),
304 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_TT_DEMOLISH),
305 Hotkey('U', "buyland", WID_TT_BUY_LAND),
306 Hotkey('I', "trees", WID_TT_PLANT_TREES),
307 Hotkey('O', "placesign", WID_TT_PLACE_SIGN),
308 Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT),
309 HOTKEY_LIST_END
311 HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys, TerraformToolbarGlobalHotkeys);
313 static const NWidgetPart _nested_terraform_widgets[] = {
314 NWidget(NWID_HORIZONTAL),
315 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
316 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_LANDSCAPING_TOOLBAR, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
317 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
318 EndContainer(),
319 NWidget(NWID_HORIZONTAL),
320 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOWER_LAND), SetMinimalSize(22, 22),
321 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
322 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_RAISE_LAND), SetMinimalSize(22, 22),
323 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
324 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LEVEL_LAND), SetMinimalSize(22, 22),
325 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
327 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
329 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_DEMOLISH), SetMinimalSize(22, 22),
330 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
331 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUY_LAND), SetMinimalSize(22, 22),
332 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND),
333 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLANT_TREES), SetMinimalSize(22, 22),
334 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_SCENEDIT_TOOLBAR_PLANT_TREES),
335 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_SIGN), SetMinimalSize(22, 22),
336 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_SCENEDIT_TOOLBAR_PLACE_SIGN),
337 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_TT_SHOW_PLACE_OBJECT),
338 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_OBJECT), SetMinimalSize(22, 22),
339 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
340 EndContainer(),
341 EndContainer(),
344 static WindowDesc _terraform_desc(
345 WDP_MANUAL, "toolbar_landscape", 0, 0,
346 WC_SCEN_LAND_GEN, WC_NONE,
347 WDF_CONSTRUCTION,
348 _nested_terraform_widgets, lengthof(_nested_terraform_widgets),
349 &TerraformToolbarWindow::hotkeys
353 * Show the toolbar for terraforming in the game.
354 * @param link The toolbar we might want to link to.
355 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
357 Window *ShowTerraformToolbar(Window *link)
359 if (!Company::IsValidID(_local_company)) return nullptr;
361 Window *w;
362 if (link == nullptr) {
363 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
364 return w;
367 /* Delete the terraform toolbar to place it again. */
368 CloseWindowById(WC_SCEN_LAND_GEN, 0, true);
369 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
370 /* Align the terraform toolbar under the main toolbar. */
371 w->top -= w->height;
372 w->SetDirty();
373 /* Put the linked toolbar to the left / right of it. */
374 link->left = w->left + (_current_text_dir == TD_RTL ? w->width : -link->width);
375 link->top = w->top;
376 link->SetDirty();
378 return w;
381 static byte _terraform_size = 1;
384 * Raise/Lower a bigger chunk of land at the same time in the editor. When
385 * raising get the lowest point, when lowering the highest point, and set all
386 * tiles in the selection to that height.
387 * @todo : Incorporate into game itself to allow for ingame raising/lowering of
388 * larger chunks at the same time OR remove altogether, as we have 'level land' ?
389 * @param tile The top-left tile where the terraforming will start
390 * @param mode 1 for raising, 0 for lowering land
392 static void CommonRaiseLowerBigLand(TileIndex tile, int mode)
394 if (_terraform_size == 1) {
395 StringID msg =
396 mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
398 DoCommandP(tile, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND | CMD_MSG(msg), CcTerraform);
399 } else {
400 assert(_terraform_size != 0);
401 TileArea ta(tile, _terraform_size, _terraform_size);
402 ta.ClampToMap();
404 if (ta.w == 0 || ta.h == 0) return;
406 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
408 uint h;
409 if (mode != 0) {
410 /* Raise land */
411 h = MAX_TILE_HEIGHT;
412 for (TileIndex tile2 : ta) {
413 h = std::min(h, TileHeight(tile2));
415 } else {
416 /* Lower land */
417 h = 0;
418 for (TileIndex tile2 : ta) {
419 h = std::max(h, TileHeight(tile2));
423 for (TileIndex tile2 : ta) {
424 if (TileHeight(tile2) == h) {
425 DoCommandP(tile2, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND);
431 static const int8 _multi_terraform_coords[][2] = {
432 { 0, -2},
433 { 4, 0}, { -4, 0}, { 0, 2},
434 { -8, 2}, { -4, 4}, { 0, 6}, { 4, 4}, { 8, 2},
435 {-12, 0}, { -8, -2}, { -4, -4}, { 0, -6}, { 4, -4}, { 8, -2}, { 12, 0},
436 {-16, 2}, {-12, 4}, { -8, 6}, { -4, 8}, { 0, 10}, { 4, 8}, { 8, 6}, { 12, 4}, { 16, 2},
437 {-20, 0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, { 0,-10}, { 4, -8}, { 8, -6}, { 12, -4}, { 16, -2}, { 20, 0},
438 {-24, 2}, {-20, 4}, {-16, 6}, {-12, 8}, { -8, 10}, { -4, 12}, { 0, 14}, { 4, 12}, { 8, 10}, { 12, 8}, { 16, 6}, { 20, 4}, { 24, 2},
439 {-28, 0}, {-24, -2}, {-20, -4}, {-16, -6}, {-12, -8}, { -8,-10}, { -4,-12}, { 0,-14}, { 4,-12}, { 8,-10}, { 12, -8}, { 16, -6}, { 20, -4}, { 24, -2}, { 28, 0},
442 static const NWidgetPart _nested_scen_edit_land_gen_widgets[] = {
443 NWidget(NWID_HORIZONTAL),
444 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
445 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
446 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
447 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
448 EndContainer(),
449 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
450 NWidget(NWID_HORIZONTAL), SetPadding(2, 2, 7, 2),
451 NWidget(NWID_SPACER), SetFill(1, 0),
452 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DEMOLISH), SetMinimalSize(22, 22),
453 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
454 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LOWER_LAND), SetMinimalSize(22, 22),
455 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
456 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_RAISE_LAND), SetMinimalSize(22, 22),
457 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
458 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LEVEL_LAND), SetMinimalSize(22, 22),
459 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
460 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_ROCKS), SetMinimalSize(22, 22),
461 SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE),
462 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_ETT_SHOW_PLACE_DESERT),
463 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_DESERT), SetMinimalSize(22, 22),
464 SetFill(0, 1), SetDataTip(SPR_IMG_DESERT, STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA),
465 EndContainer(),
466 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_ETT_PLACE_OBJECT), SetMinimalSize(23, 22),
467 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
468 NWidget(NWID_SPACER), SetFill(1, 0),
469 EndContainer(),
470 NWidget(NWID_HORIZONTAL),
471 NWidget(NWID_SPACER), SetFill(1, 0),
472 NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, WID_ETT_DOTS), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY, STR_NULL),
473 NWidget(NWID_SPACER), SetFill(1, 0),
474 NWidget(NWID_VERTICAL),
475 NWidget(NWID_SPACER), SetFill(0, 1),
476 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_INCREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA),
477 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
478 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DECREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA),
479 NWidget(NWID_SPACER), SetFill(0, 1),
480 EndContainer(),
481 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
482 EndContainer(),
483 NWidget(NWID_SPACER), SetMinimalSize(0, 6),
484 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_NEW_SCENARIO), SetMinimalSize(160, 12),
485 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetPadding(0, 2, 0, 2),
486 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_RESET_LANDSCAPE), SetMinimalSize(160, 12),
487 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP), SetPadding(1, 2, 2, 2),
488 EndContainer(),
492 * Callback function for the scenario editor 'reset landscape' confirmation window
493 * @param w Window unused
494 * @param confirmed boolean value, true when yes was clicked, false otherwise
496 static void ResetLandscapeConfirmationCallback(Window *w, bool confirmed)
498 if (confirmed) {
499 /* Set generating_world to true to get instant-green grass after removing
500 * company property. */
501 Backup<bool> old_generating_world(_generating_world, true, FILE_LINE);
503 /* Delete all companies */
504 for (Company *c : Company::Iterate()) {
505 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
506 delete c;
509 old_generating_world.Restore();
511 /* Delete all station signs */
512 for (BaseStation *st : BaseStation::Iterate()) {
513 /* There can be buoys, remove them */
514 if (IsBuoyTile(st->xy)) DoCommand(st->xy, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
515 if (!st->IsInUse()) delete st;
518 /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
519 EngineOverrideManager::ResetToCurrentNewGRFConfig();
521 MarkWholeScreenDirty();
525 /** Landscape generation window handler in the scenario editor. */
526 struct ScenarioEditorLandscapeGenerationWindow : Window {
527 int last_user_action; ///< Last started user action.
529 ScenarioEditorLandscapeGenerationWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
531 this->CreateNestedTree();
532 NWidgetStacked *show_desert = this->GetWidget<NWidgetStacked>(WID_ETT_SHOW_PLACE_DESERT);
533 show_desert->SetDisplayedPlane(_settings_game.game_creation.landscape == LT_TROPIC ? 0 : SZSP_NONE);
534 this->FinishInitNested(window_number);
535 this->last_user_action = WIDGET_LIST_END;
538 void OnPaint() override
540 this->DrawWidgets();
542 if (this->IsWidgetLowered(WID_ETT_LOWER_LAND) || this->IsWidgetLowered(WID_ETT_RAISE_LAND)) { // change area-size if raise/lower corner is selected
543 SetTileSelectSize(_terraform_size, _terraform_size);
547 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
549 if (widget != WID_ETT_DOTS) return;
551 size->width = std::max<uint>(size->width, ScaleGUITrad(59));
552 size->height = std::max<uint>(size->height, ScaleGUITrad(31));
555 void DrawWidget(const Rect &r, int widget) const override
557 if (widget != WID_ETT_DOTS) return;
559 int center_x = RoundDivSU(r.left + r.right, 2);
560 int center_y = RoundDivSU(r.top + r.bottom, 2);
562 int n = _terraform_size * _terraform_size;
563 const int8 *coords = &_multi_terraform_coords[0][0];
565 assert(n != 0);
566 do {
567 DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + ScaleGUITrad(coords[0]), center_y + ScaleGUITrad(coords[1]));
568 coords += 2;
569 } while (--n);
572 void OnClick(Point pt, int widget, int click_count) override
574 if (widget < WID_ETT_BUTTONS_START) return;
576 switch (widget) {
577 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
578 HandlePlacePushButton(this, WID_ETT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
579 this->last_user_action = widget;
580 break;
582 case WID_ETT_LOWER_LAND: // Lower land button
583 HandlePlacePushButton(this, WID_ETT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT);
584 this->last_user_action = widget;
585 break;
587 case WID_ETT_RAISE_LAND: // Raise land button
588 HandlePlacePushButton(this, WID_ETT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT);
589 this->last_user_action = widget;
590 break;
592 case WID_ETT_LEVEL_LAND: // Level land button
593 HandlePlacePushButton(this, WID_ETT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
594 this->last_user_action = widget;
595 break;
597 case WID_ETT_PLACE_ROCKS: // Place rocks button
598 HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT);
599 this->last_user_action = widget;
600 break;
602 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
603 HandlePlacePushButton(this, WID_ETT_PLACE_DESERT, SPR_CURSOR_DESERT, HT_RECT);
604 this->last_user_action = widget;
605 break;
607 case WID_ETT_PLACE_OBJECT: // Place transmitter button
608 ShowBuildObjectPicker();
609 break;
611 case WID_ETT_INCREASE_SIZE:
612 case WID_ETT_DECREASE_SIZE: { // Increase/Decrease terraform size
613 int size = (widget == WID_ETT_INCREASE_SIZE) ? 1 : -1;
614 this->HandleButtonClick(widget);
615 size += _terraform_size;
617 if (!IsInsideMM(size, 1, 8 + 1)) return;
618 _terraform_size = size;
620 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
621 this->SetDirty();
622 break;
625 case WID_ETT_NEW_SCENARIO: // gen random land
626 this->HandleButtonClick(widget);
627 ShowCreateScenario();
628 break;
630 case WID_ETT_RESET_LANDSCAPE: // Reset landscape
631 ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, nullptr, ResetLandscapeConfirmationCallback);
632 break;
634 default: NOT_REACHED();
638 void OnTimeout() override
640 for (uint i = WID_ETT_START; i < this->nested_array_size; i++) {
641 if (i == WID_ETT_BUTTONS_START) i = WID_ETT_BUTTONS_END; // skip the buttons
642 if (this->IsWidgetLowered(i)) {
643 this->RaiseWidget(i);
644 this->SetWidgetDirty(i);
649 void OnPlaceObject(Point pt, TileIndex tile) override
651 switch (this->last_user_action) {
652 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
653 PlaceProc_DemolishArea(tile);
654 break;
656 case WID_ETT_LOWER_LAND: // Lower land button
657 CommonRaiseLowerBigLand(tile, 0);
658 break;
660 case WID_ETT_RAISE_LAND: // Raise land button
661 CommonRaiseLowerBigLand(tile, 1);
662 break;
664 case WID_ETT_LEVEL_LAND: // Level land button
665 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
666 break;
668 case WID_ETT_PLACE_ROCKS: // Place rocks button
669 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
670 break;
672 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
673 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
674 break;
676 default: NOT_REACHED();
680 void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
682 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
685 void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
687 if (pt.x != -1) {
688 switch (select_proc) {
689 default: NOT_REACHED();
690 case DDSP_CREATE_ROCKS:
691 case DDSP_CREATE_DESERT:
692 case DDSP_RAISE_AND_LEVEL_AREA:
693 case DDSP_LOWER_AND_LEVEL_AREA:
694 case DDSP_LEVEL_AREA:
695 case DDSP_DEMOLISH_AREA:
696 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
697 break;
702 void OnPlaceObjectAbort() override
704 this->RaiseButtons();
705 this->SetDirty();
708 static HotkeyList hotkeys;
712 * Handler for global hotkeys of the ScenarioEditorLandscapeGenerationWindow.
713 * @param hotkey Hotkey
714 * @return ES_HANDLED if hotkey was accepted.
716 static EventState TerraformToolbarEditorGlobalHotkeys(int hotkey)
718 if (_game_mode != GM_EDITOR) return ES_NOT_HANDLED;
719 Window *w = ShowEditorTerraformToolbar();
720 if (w == nullptr) return ES_NOT_HANDLED;
721 return w->OnHotkey(hotkey);
724 static Hotkey terraform_editor_hotkeys[] = {
725 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
726 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
727 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_ETT_RAISE_LAND),
728 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_ETT_LEVEL_LAND),
729 Hotkey('R', "rocky", WID_ETT_PLACE_ROCKS),
730 Hotkey('T', "desert", WID_ETT_PLACE_DESERT),
731 Hotkey('O', "object", WID_ETT_PLACE_OBJECT),
732 HOTKEY_LIST_END
735 HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys);
737 static WindowDesc _scen_edit_land_gen_desc(
738 WDP_AUTO, "toolbar_landscape_scen", 0, 0,
739 WC_SCEN_LAND_GEN, WC_NONE,
740 WDF_CONSTRUCTION,
741 _nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets),
742 &ScenarioEditorLandscapeGenerationWindow::hotkeys
746 * Show the toolbar for terraforming in the scenario editor.
747 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
749 Window *ShowEditorTerraformToolbar()
751 return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);