# terraform_gui.cpp

Go to the documentation of this file.

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