# terraform_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: terraform_gui.cpp 18966 2010-01-30 18:34:48Z frosch $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "clear_map.h"
    00014 #include "company_func.h"
    00015 #include "company_base.h"
    00016 #include "gui.h"
    00017 #include "window_gui.h"
    00018 #include "window_func.h"
    00019 #include "viewport_func.h"
    00020 #include "command_func.h"
    00021 #include "signs_func.h"
    00022 #include "variables.h"
    00023 #include "functions.h"
    00024 #include "sound_func.h"
    00025 #include "base_station_base.h"
    00026 #include "unmovable_map.h"
    00027 #include "textbuf_gui.h"
    00028 #include "genworld.h"
    00029 #include "tree_map.h"
    00030 #include "landscape_type.h"
    00031 #include "tilehighlight_func.h"
    00032 #include "strings_func.h"
    00033
    00034 #include "table/sprites.h"
    00035 #include "table/strings.h"
    00036
    00037 void CcTerraform(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00038 {
    00039   if (result.Succeeded()) {
    00040     SndPlayTileFx(SND_1F_SPLAT, tile);
    00041   } else {
    00042     extern TileIndex _terraform_err_tile;
    00043     SetRedErrorSquare(_terraform_err_tile);
    00044   }
    00045 }
    00046
    00047
    00049 static void GenerateDesertArea(TileIndex end, TileIndex start)
    00050 {
    00051   if (_game_mode != GM_EDITOR) return;
    00052
    00053   _generating_world = true;
    00054
    00055   TileArea ta(start, end);
    00056   TILE_AREA_LOOP(tile, ta) {
    00057     SetTropicZone(tile, (_ctrl_pressed) ? TROPICZONE_NORMAL : TROPICZONE_DESERT);
    00058     DoCommandP(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
    00059     MarkTileDirtyByTile(tile);
    00060   }
    00061   _generating_world = false;
    00062 }
    00063
    00065 static void GenerateRockyArea(TileIndex end, TileIndex start)
    00066 {
    00067   if (_game_mode != GM_EDITOR) return;
    00068
    00069   bool success = false;
    00070   TileArea ta(start, end);
    00071
    00072   TILE_AREA_LOOP(tile, ta) {
    00073     switch (GetTileType(tile)) {
    00074       case MP_TREES:
    00075         if (GetTreeGround(tile) == TREE_GROUND_SHORE) continue;
    00076       /* FALL THROUGH */
    00077       case MP_CLEAR:
    00078         MakeClear(tile, CLEAR_ROCKS, 3);
    00079         break;
    00080
    00081       default: continue;
    00082     }
    00083     MarkTileDirtyByTile(tile);
    00084     success = true;
    00085   }
    00086
    00087   if (success) SndPlayTileFx(SND_1F_SPLAT, end);
    00088 }
    00089
    00099 bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc, TileIndex start_tile, TileIndex end_tile)
    00100 {
    00101   if (!_settings_game.construction.freeform_edges) {
    00102     /* When end_tile is MP_VOID, the error tile will not be visible to the
    00103      * user. This happens when terraforming at the southern border. */
    00104     if (TileX(end_tile) == MapMaxX()) end_tile += TileDiffXY(-1, 0);
    00105     if (TileY(end_tile) == MapMaxY()) end_tile += TileDiffXY(0, -1);
    00106   }
    00107
    00108   switch (proc) {
    00109     case DDSP_DEMOLISH_AREA:
    00110       DoCommandP(end_tile, start_tile, 0, CMD_CLEAR_AREA | CMD_MSG(STR_ERROR_CAN_T_CLEAR_THIS_AREA), CcPlaySound10);
    00111       break;
    00112     case DDSP_RAISE_AND_LEVEL_AREA:
    00113       DoCommandP(end_tile, start_tile, 1, CMD_LEVEL_LAND | CMD_MSG(STR_ERROR_CAN_T_RAISE_LAND_HERE), CcTerraform);
    00114       break;
    00115     case DDSP_LOWER_AND_LEVEL_AREA:
    00116       DoCommandP(end_tile, start_tile, (uint32)-1, CMD_LEVEL_LAND | CMD_MSG(STR_ERROR_CAN_T_LOWER_LAND_HERE), CcTerraform);
    00117       break;
    00118     case DDSP_LEVEL_AREA:
    00119       DoCommandP(end_tile, start_tile, 0, CMD_LEVEL_LAND | CMD_MSG(STR_ERROR_CAN_T_LEVEL_LAND_HERE), CcTerraform);
    00120       break;
    00121     case DDSP_CREATE_ROCKS:
    00122       GenerateRockyArea(end_tile, start_tile);
    00123       break;
    00124     case DDSP_CREATE_DESERT:
    00125       GenerateDesertArea(end_tile, start_tile);
    00126       break;
    00127     default:
    00128       return false;
    00129   }
    00130
    00131   return true;
    00132 }
    00133
    00134 typedef void OnButtonClick(Window *w);
    00135
    00136 static const uint16 _terraform_keycodes[] = {
    00137   'Q',
    00138   'W',
    00139   'E',
    00140   'D',
    00141   'U',
    00142   'I',
    00143   'O',
    00144 };
    00145
    00146 static void PlaceProc_BuyLand(TileIndex tile)
    00147 {
    00148   DoCommandP(tile, 0, 0, CMD_PURCHASE_LAND_AREA | CMD_MSG(STR_ERROR_CAN_T_PURCHASE_THIS_LAND), CcPlaySound1E);
    00149 }
    00150
    00151 void PlaceProc_DemolishArea(TileIndex tile)
    00152 {
    00153   VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_DEMOLISH_AREA);
    00154 }
    00155
    00156 static void PlaceProc_RaiseLand(TileIndex tile)
    00157 {
    00158   VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
    00159 }
    00160
    00161 static void PlaceProc_LowerLand(TileIndex tile)
    00162 {
    00163   VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LOWER_AND_LEVEL_AREA);
    00164 }
    00165
    00166 static void PlaceProc_LevelLand(TileIndex tile)
    00167 {
    00168   VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
    00169 }
    00170
    00172 enum TerraformToolbarWidgets {
    00173   TTW_BUTTONS_START,
    00174   TTW_LOWER_LAND = TTW_BUTTONS_START,
    00175   TTW_RAISE_LAND,
    00176   TTW_LEVEL_LAND,
    00177   TTW_DEMOLISH,
    00178   TTW_BUY_LAND,
    00179   TTW_PLANT_TREES,
    00180   TTW_PLACE_SIGN,
    00181 };
    00182
    00183 static void TerraformClick_Lower(Window *w)
    00184 {
    00185   HandlePlacePushButton(w, TTW_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT, PlaceProc_LowerLand);
    00186 }
    00187
    00188 static void TerraformClick_Raise(Window *w)
    00189 {
    00190   HandlePlacePushButton(w, TTW_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT, PlaceProc_RaiseLand);
    00191 }
    00192
    00193 static void TerraformClick_Level(Window *w)
    00194 {
    00195   HandlePlacePushButton(w, TTW_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT, PlaceProc_LevelLand);
    00196 }
    00197
    00198 static void TerraformClick_Dynamite(Window *w)
    00199 {
    00200   HandlePlacePushButton(w, TTW_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT, PlaceProc_DemolishArea);
    00201 }
    00202
    00203 static void TerraformClick_BuyLand(Window *w)
    00204 {
    00205   HandlePlacePushButton(w, TTW_BUY_LAND, SPR_CURSOR_BUY_LAND, HT_RECT, PlaceProc_BuyLand);
    00206 }
    00207
    00208 static void TerraformClick_Trees(Window *w)
    00209 {
    00210   /* This button is NOT a place-push-button, so don't treat it as such */
    00211   ShowBuildTreesToolbar();
    00212 }
    00213
    00214 static void TerraformClick_PlaceSign(Window *w)
    00215 {
    00216   HandlePlacePushButton(w, TTW_PLACE_SIGN, SPR_CURSOR_SIGN, HT_RECT, PlaceProc_Sign);
    00217 }
    00218
    00219 static OnButtonClick * const _terraform_button_proc[] = {
    00220   TerraformClick_Lower,
    00221   TerraformClick_Raise,
    00222   TerraformClick_Level,
    00223   TerraformClick_Dynamite,
    00224   TerraformClick_BuyLand,
    00225   TerraformClick_Trees,
    00226   TerraformClick_PlaceSign,
    00227 };
    00228
    00229 struct TerraformToolbarWindow : Window {
    00230   TerraformToolbarWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00231   {
    00232     this->InitNested(desc, window_number);
    00233   }
    00234
    00235   ~TerraformToolbarWindow()
    00236   {
    00237   }
    00238
    00239   virtual void OnPaint()
    00240   {
    00241     this->DrawWidgets();
    00242   }
    00243
    00244   virtual void OnClick(Point pt, int widget, int click_count)
    00245   {
    00246     if (widget >= TTW_BUTTONS_START) _terraform_button_proc[widget - TTW_BUTTONS_START](this);
    00247   }
    00248
    00249   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00250   {
    00251     for (uint i = 0; i != lengthof(_terraform_keycodes); i++) {
    00252       if (keycode == _terraform_keycodes[i]) {
    00253         _terraform_button_proc[i](this);
    00254         return ES_HANDLED;
    00255       }
    00256     }
    00257     return ES_NOT_HANDLED;
    00258   }
    00259
    00260   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00261   {
    00262     _place_proc(tile);
    00263   }
    00264
    00265   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
    00266   {
    00267     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
    00268   }
    00269
    00270   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    00271   {
    00272     Point pt = GetToolbarAlignedWindowPosition(sm_width);
    00273     pt.y += sm_height;
    00274     return pt;
    00275   }
    00276
    00277   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
    00278   {
    00279     if (pt.x != -1) {
    00280       switch (select_proc) {
    00281         default: NOT_REACHED();
    00282         case DDSP_DEMOLISH_AREA:
    00283         case DDSP_RAISE_AND_LEVEL_AREA:
    00284         case DDSP_LOWER_AND_LEVEL_AREA:
    00285         case DDSP_LEVEL_AREA:
    00286           GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
    00287           break;
    00288       }
    00289     }
    00290   }
    00291
    00292   virtual void OnPlaceObjectAbort()
    00293   {
    00294     this->RaiseButtons();
    00295   }
    00296 };
    00297
    00298 static const NWidgetPart _nested_terraform_widgets[] = {
    00299   NWidget(NWID_HORIZONTAL),
    00300     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00301     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_LANDSCAPING_TOOLBAR, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00302     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00303   EndContainer(),
    00304   NWidget(NWID_HORIZONTAL),
    00305     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_LOWER_LAND), SetMinimalSize(22,22),
    00306                 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
    00307     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_RAISE_LAND), SetMinimalSize(22,22),
    00308                 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
    00309     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_LEVEL_LAND), SetMinimalSize(22,22),
    00310                 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
    00311
    00312     NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
    00313
    00314     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_DEMOLISH), SetMinimalSize(22,22),
    00315                 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
    00316     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_BUY_LAND), SetMinimalSize(22,22),
    00317                 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND),
    00318     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_PLANT_TREES), SetMinimalSize(22,22),
    00319                 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_SCENEDIT_TOOLBAR_PLANT_TREES),
    00320     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_PLACE_SIGN), SetMinimalSize(22,22),
    00321                 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_SCENEDIT_TOOLBAR_PLACE_SIGN),
    00322   EndContainer(),
    00323 };
    00324
    00325 static const WindowDesc _terraform_desc(
    00326   WDP_MANUAL, 0, 0,
    00327   WC_SCEN_LAND_GEN, WC_NONE,
    00328   WDF_CONSTRUCTION,
    00329   _nested_terraform_widgets, lengthof(_nested_terraform_widgets)
    00330 );
    00331
    00332 Window *ShowTerraformToolbar(Window *link)
    00333 {
    00334   if (!Company::IsValidID(_local_company)) return NULL;
    00335
    00336   Window *w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
    00337   if (link == NULL) return w;
    00338
    00339   if (w == NULL) {
    00340     w = FindWindowById(WC_SCEN_LAND_GEN, 0);
    00341     if (w == NULL) return NULL;
    00342   } else {
    00343     w->top -= w->height;
    00344     w->SetDirty();
    00345   }
    00346
    00347   /* Align the terraform toolbar under the main toolbar and put the linked
    00348    * toolbar to left/right of it */
    00349   link->left = w->left + (_dynlang.text_dir == TD_RTL ? w->width : -link->width);
    00350   link->top  = w->top;
    00351   link->SetDirty();
    00352
    00353   return w;
    00354 }
    00355
    00356 void ShowTerraformToolbarWithTool(uint16 key, uint16 keycode)
    00357 {
    00358   Window *w = FindWindowById(WC_SCEN_LAND_GEN, 0);
    00359
    00360   if (w == NULL) w = ShowTerraformToolbar(NULL);
    00361   if (w == NULL) return;
    00362
    00363   w->OnKeyPress(key, keycode);
    00364 }
    00365
    00366 static byte _terraform_size = 1;
    00367
    00377 static void CommonRaiseLowerBigLand(TileIndex tile, int mode)
    00378 {
    00379   int sizex, sizey;
    00380   uint h;
    00381
    00382   if (_terraform_size == 1) {
    00383     StringID msg =
    00384       mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
    00385
    00386     DoCommandP(tile, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND | CMD_MSG(msg), CcTerraform);
    00387   } else {
    00388     assert(_terraform_size != 0);
    00389     /* check out for map overflows */
    00390     sizex = min(MapSizeX() - TileX(tile), _terraform_size);
    00391     sizey = min(MapSizeY() - TileY(tile), _terraform_size);
    00392
    00393     if (sizex == 0 || sizey == 0) return;
    00394
    00395     SndPlayTileFx(SND_1F_SPLAT, tile);
    00396
    00397     if (mode != 0) {
    00398       /* Raise land */
    00399       h = 15; // XXX - max height
    00400       TILE_LOOP(tile2, sizex, sizey, tile) {
    00401         h = min(h, TileHeight(tile2));
    00402       }
    00403     } else {
    00404       /* Lower land */
    00405       h = 0;
    00406       TILE_LOOP(tile2, sizex, sizey, tile) {
    00407         h = max(h, TileHeight(tile2));
    00408       }
    00409     }
    00410
    00411     TILE_LOOP(tile2, sizex, sizey, tile) {
    00412       if (TileHeight(tile2) == h) {
    00413         DoCommandP(tile2, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND);
    00414       }
    00415     }
    00416   }
    00417 }
    00418
    00419 static void PlaceProc_RaiseBigLand(TileIndex tile)
    00420 {
    00421   CommonRaiseLowerBigLand(tile, 1);
    00422 }
    00423
    00424 static void PlaceProc_LowerBigLand(TileIndex tile)
    00425 {
    00426   CommonRaiseLowerBigLand(tile, 0);
    00427 }
    00428
    00429 static void PlaceProc_RockyArea(TileIndex tile)
    00430 {
    00431   VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
    00432 }
    00433
    00434 static void PlaceProc_LightHouse(TileIndex tile)
    00435 {
    00436   /* not flat || not(trees || clear without bridge above) */
    00437   if (GetTileSlope(tile, NULL) != SLOPE_FLAT || !(IsTileType(tile, MP_TREES) || (IsTileType(tile, MP_CLEAR) && !IsBridgeAbove(tile)))) {
    00438     return;
    00439   }
    00440
    00441   MakeLighthouse(tile);
    00442   MarkTileDirtyByTile(tile);
    00443   SndPlayTileFx(SND_1F_SPLAT, tile);
    00444 }
    00445
    00446 static void PlaceProc_Transmitter(TileIndex tile)
    00447 {
    00448   /* not flat || not(trees || clear without bridge above) */
    00449   if (GetTileSlope(tile, NULL) != SLOPE_FLAT || !(IsTileType(tile, MP_TREES) || (IsTileType(tile, MP_CLEAR) && !IsBridgeAbove(tile)))) {
    00450     return;
    00451   }
    00452
    00453   MakeTransmitter(tile);
    00454   MarkTileDirtyByTile(tile);
    00455   SndPlayTileFx(SND_1F_SPLAT, tile);
    00456 }
    00457
    00458 static void PlaceProc_DesertArea(TileIndex tile)
    00459 {
    00460   VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
    00461 }
    00462
    00463 static const int8 _multi_terraform_coords[][2] = {
    00464   {  0, -2},
    00465   {  4,  0}, { -4,  0}, {  0,  2},
    00466   { -8,  2}, { -4,  4}, {  0,  6}, {  4,  4}, {  8,  2},
    00467   {-12,  0}, { -8, -2}, { -4, -4}, {  0, -6}, {  4, -4}, {  8, -2}, { 12,  0},
    00468   {-16,  2}, {-12,  4}, { -8,  6}, { -4,  8}, {  0, 10}, {  4,  8}, {  8,  6}, { 12,  4}, { 16,  2},
    00469   {-20,  0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, {  0,-10}, {  4, -8}, {  8, -6}, { 12, -4}, { 16, -2}, { 20,  0},
    00470   {-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},
    00471   {-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},
    00472 };
    00473
    00475 enum EditorTerraformToolbarWidgets {
    00476   ETTW_START = 0,
    00477   ETTW_DOTS = ETTW_START,
    00478   ETTW_BUTTONS_START,
    00479   ETTW_DEMOLISH = ETTW_BUTTONS_START,
    00480   ETTW_LOWER_LAND,
    00481   ETTW_RAISE_LAND,
    00482   ETTW_LEVEL_LAND,
    00483   ETTW_PLACE_ROCKS,
    00484   ETTW_PLACE_DESERT_LIGHTHOUSE,
    00485   ETTW_PLACE_TRANSMITTER,
    00486   ETTW_BUTTONS_END,
    00487   ETTW_INCREASE_SIZE = ETTW_BUTTONS_END,
    00488   ETTW_DECREASE_SIZE,
    00489   ETTW_NEW_SCENARIO,
    00490   ETTW_RESET_LANDSCAPE,
    00491 };
    00492
    00493 static const NWidgetPart _nested_scen_edit_land_gen_widgets[] = {
    00494   NWidget(NWID_HORIZONTAL),
    00495     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00496     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00497     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
    00498     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00499   EndContainer(),
    00500   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    00501     NWidget(NWID_HORIZONTAL), SetPadding(2, 2, 7, 2),
    00502       NWidget(NWID_SPACER), SetFill(1, 0),
    00503       NWidget(WWT_IMGBTN, COLOUR_GREY, ETTW_DEMOLISH), SetMinimalSize(22, 22),
    00504                     SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
    00505       NWidget(WWT_IMGBTN, COLOUR_GREY, ETTW_LOWER_LAND), SetMinimalSize(22, 22),
    00506                     SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
    00507       NWidget(WWT_IMGBTN, COLOUR_GREY, ETTW_RAISE_LAND), SetMinimalSize(22, 22),
    00508                     SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
    00509       NWidget(WWT_IMGBTN, COLOUR_GREY, ETTW_LEVEL_LAND), SetMinimalSize(22, 22),
    00510                     SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
    00511       NWidget(WWT_IMGBTN, COLOUR_GREY, ETTW_PLACE_ROCKS), SetMinimalSize(22, 22),
    00512                     SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE),
    00513       NWidget(WWT_IMGBTN, COLOUR_GREY, ETTW_PLACE_DESERT_LIGHTHOUSE), SetMinimalSize(22, 22),
    00514                     SetFill(0, 1), SetDataTip(SPR_IMG_LIGHTHOUSE_DESERT, STR_NULL),
    00515       NWidget(WWT_IMGBTN, COLOUR_GREY, ETTW_PLACE_TRANSMITTER), SetMinimalSize(23, 22),
    00516                     SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_TERRAFORM_TOOLTIP_PLACE_TRANSMITTER),
    00517       NWidget(NWID_SPACER), SetFill(1, 0),
    00518     EndContainer(),
    00519     NWidget(NWID_HORIZONTAL),
    00520       NWidget(NWID_SPACER), SetFill(1, 0),
    00521       NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, ETTW_DOTS), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY, STR_NULL),
    00522       NWidget(NWID_SPACER), SetFill(1, 0),
    00523       NWidget(NWID_VERTICAL),
    00524         NWidget(NWID_SPACER), SetFill(0, 1),
    00525         NWidget(WWT_IMGBTN, COLOUR_GREY, ETTW_INCREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA),
    00526         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    00527         NWidget(WWT_IMGBTN, COLOUR_GREY, ETTW_DECREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA),
    00528         NWidget(NWID_SPACER), SetFill(0, 1),
    00529       EndContainer(),
    00530       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00531     EndContainer(),
    00532     NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    00533     NWidget(WWT_TEXTBTN, COLOUR_GREY, ETTW_NEW_SCENARIO), SetMinimalSize(160, 12),
    00534                 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetPadding(0, 2, 0, 2),
    00535     NWidget(WWT_TEXTBTN, COLOUR_GREY, ETTW_RESET_LANDSCAPE), SetMinimalSize(160, 12),
    00536                 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP), SetPadding(1, 2, 2, 2),
    00537   EndContainer(),
    00538 };
    00539
    00544 static void EditorTerraformClick_Dynamite(Window *w)
    00545 {
    00546   HandlePlacePushButton(w, ETTW_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT, PlaceProc_DemolishArea);
    00547 }
    00548
    00549 static void EditorTerraformClick_LowerBigLand(Window *w)
    00550 {
    00551   HandlePlacePushButton(w, ETTW_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT, PlaceProc_LowerBigLand);
    00552 }
    00553
    00554 static void EditorTerraformClick_RaiseBigLand(Window *w)
    00555 {
    00556   HandlePlacePushButton(w, ETTW_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT, PlaceProc_RaiseBigLand);
    00557 }
    00558
    00559 static void EditorTerraformClick_LevelLand(Window *w)
    00560 {
    00561   HandlePlacePushButton(w, ETTW_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT, PlaceProc_LevelLand);
    00562 }
    00563
    00564 static void EditorTerraformClick_RockyArea(Window *w)
    00565 {
    00566   HandlePlacePushButton(w, ETTW_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT, PlaceProc_RockyArea);
    00567 }
    00568
    00569 static void EditorTerraformClick_DesertLightHouse(Window *w)
    00570 {
    00571   HandlePlacePushButton(w, ETTW_PLACE_DESERT_LIGHTHOUSE, SPR_CURSOR_LIGHTHOUSE, HT_RECT, (_settings_game.game_creation.landscape == LT_TROPIC) ? PlaceProc_DesertArea : PlaceProc_LightHouse);
    00572 }
    00573
    00574 static void EditorTerraformClick_Transmitter(Window *w)
    00575 {
    00576   HandlePlacePushButton(w, ETTW_PLACE_TRANSMITTER, SPR_CURSOR_TRANSMITTER, HT_RECT, PlaceProc_Transmitter);
    00577 }
    00578
    00579 static const uint16 _editor_terraform_keycodes[] = {
    00580   'D',
    00581   'Q',
    00582   'W',
    00583   'E',
    00584   'R',
    00585   'T',
    00586   'Y'
    00587 };
    00588
    00589 typedef void OnButtonClick(Window *w);
    00590 static OnButtonClick * const _editor_terraform_button_proc[] = {
    00591   EditorTerraformClick_Dynamite,
    00592   EditorTerraformClick_LowerBigLand,
    00593   EditorTerraformClick_RaiseBigLand,
    00594   EditorTerraformClick_LevelLand,
    00595   EditorTerraformClick_RockyArea,
    00596   EditorTerraformClick_DesertLightHouse,
    00597   EditorTerraformClick_Transmitter
    00598 };
    00599
    00600
    00604 static void ResetLandscapeConfirmationCallback(Window *w, bool confirmed)
    00605 {
    00606   if (confirmed) {
    00607     /* Set generating_world to true to get instant-green grass after removing
    00608      * company property. */
    00609     _generating_world = true;
    00610
    00611     /* Delete all companies */
    00612     Company *c;
    00613     FOR_ALL_COMPANIES(c) {
    00614       ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
    00615       delete c;
    00616     }
    00617
    00618     _generating_world = false;
    00619
    00620     /* Delete all station signs */
    00621     BaseStation *st;
    00622     FOR_ALL_BASE_STATIONS(st) {
    00623       /* There can be buoys, remove them */
    00624       if (IsBuoyTile(st->xy)) DoCommand(st->xy, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
    00625       if (!st->IsInUse()) delete st;
    00626     }
    00627
    00628     MarkWholeScreenDirty();
    00629   }
    00630 }
    00631
    00632 struct ScenarioEditorLandscapeGenerationWindow : Window {
    00633   ScenarioEditorLandscapeGenerationWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00634   {
    00635     this->InitNested(desc, window_number);
    00636     this->GetWidget<NWidgetCore>(ETTW_PLACE_DESERT_LIGHTHOUSE)->tool_tip = (_settings_game.game_creation.landscape == LT_TROPIC) ? STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA : STR_TERRAFORM_TOOLTIP_PLACE_LIGHTHOUSE;
    00637   }
    00638
    00639   virtual void OnPaint()
    00640   {
    00641     this->DrawWidgets();
    00642
    00643     if (this->IsWidgetLowered(ETTW_LOWER_LAND) || this->IsWidgetLowered(ETTW_RAISE_LAND)) { // change area-size if raise/lower corner is selected
    00644       SetTileSelectSize(_terraform_size, _terraform_size);
    00645     }
    00646   }
    00647
    00648   virtual void DrawWidget(const Rect &r, int widget) const
    00649   {
    00650     if (widget != ETTW_DOTS) return;
    00651
    00652     int center_x = (r.left + r.right + 1) / 2;
    00653     int center_y = (r.top + r.bottom + 1) / 2;
    00654
    00655     int n = _terraform_size * _terraform_size;
    00656     const int8 *coords = &_multi_terraform_coords[0][0];
    00657
    00658     assert(n != 0);
    00659     do {
    00660       DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + coords[0], center_y + coords[1]);
    00661       coords += 2;
    00662     } while (--n);
    00663   }
    00664
    00665   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00666   {
    00667     for (uint i = 0; i != lengthof(_editor_terraform_keycodes); i++) {
    00668       if (keycode == _editor_terraform_keycodes[i]) {
    00669         _editor_terraform_button_proc[i](this);
    00670         return ES_HANDLED;
    00671       }
    00672     }
    00673     return ES_NOT_HANDLED;
    00674   }
    00675
    00676   virtual void OnClick(Point pt, int widget, int click_count)
    00677   {
    00678     if (IsInsideMM(widget, ETTW_BUTTONS_START, ETTW_BUTTONS_END)) {
    00679       _editor_terraform_button_proc[widget - ETTW_BUTTONS_START](this);
    00680     } else {
    00681       switch (widget) {
    00682         case ETTW_INCREASE_SIZE:
    00683         case ETTW_DECREASE_SIZE: { // Increase/Decrease terraform size
    00684           int size = (widget == ETTW_INCREASE_SIZE) ? 1 : -1;
    00685           this->HandleButtonClick(widget);
    00686           size += _terraform_size;
    00687
    00688           if (!IsInsideMM(size, 1, 8 + 1)) return;
    00689           _terraform_size = size;
    00690
    00691           SndPlayFx(SND_15_BEEP);
    00692           this->SetDirty();
    00693         } break;
    00694         case ETTW_NEW_SCENARIO: // gen random land
    00695           this->HandleButtonClick(widget);
    00696           ShowCreateScenario();
    00697           break;
    00698         case ETTW_RESET_LANDSCAPE: // Reset landscape
    00699           ShowQuery(
    00700             STR_QUERY_RESET_LANDSCAPE_CAPTION,
    00701             STR_RESET_LANDSCAPE_CONFIRMATION_TEXT,
    00702             NULL,
    00703             ResetLandscapeConfirmationCallback);
    00704           break;
    00705       }
    00706     }
    00707   }
    00708
    00709   virtual void OnTimeout()
    00710   {
    00711     for (uint i = ETTW_START; i < this->nested_array_size; i++) {
    00712       if (i == ETTW_BUTTONS_START) i = ETTW_BUTTONS_END; // skip the buttons
    00713       if (this->IsWidgetLowered(i)) {
    00714         this->RaiseWidget(i);
    00715         this->SetWidgetDirty(i);
    00716       }
    00717     }
    00718   }
    00719
    00720   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00721   {
    00722     _place_proc(tile);
    00723   }
    00724
    00725   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
    00726   {
    00727     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
    00728   }
    00729
    00730   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
    00731   {
    00732     if (pt.x != -1) {
    00733       switch (select_proc) {
    00734         default: NOT_REACHED();
    00735         case DDSP_CREATE_ROCKS:
    00736         case DDSP_CREATE_DESERT:
    00737         case DDSP_RAISE_AND_LEVEL_AREA:
    00738         case DDSP_LOWER_AND_LEVEL_AREA:
    00739         case DDSP_LEVEL_AREA:
    00740         case DDSP_DEMOLISH_AREA:
    00741           GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
    00742           break;
    00743       }
    00744     }
    00745   }
    00746
    00747   virtual void OnPlaceObjectAbort()
    00748   {
    00749     this->RaiseButtons();
    00750     this->SetDirty();
    00751   }
    00752 };
    00753
    00754 static const WindowDesc _scen_edit_land_gen_desc(
    00755   WDP_AUTO, 0, 0,
    00756   WC_SCEN_LAND_GEN, WC_NONE,
    00757   WDF_CONSTRUCTION,
    00758   _nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets)
    00759 );
    00760
    00761 Window *ShowEditorTerraformToolbar()
    00762 {
    00763   return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);
    00764 }
    00765
    00766 void ShowEditorTerraformToolbarWithTool(uint16 key, uint16 keycode)
    00767 {
    00768   Window *w = FindWindowById(WC_SCEN_LAND_GEN, 0);
    00769
    00770   if (w == NULL) w = ShowEditorTerraformToolbar();
    00771   if (w == NULL) return;
    00772
    00773   w->OnKeyPress(key, keycode);
    00774 }