diff --git a/src/tree_cmd.cpp b/src/tree_cmd.cpp index 3c127e0ae3..3a96ddc3aa 100644 --- a/src/tree_cmd.cpp +++ b/src/tree_cmd.cpp @@ -336,7 +336,7 @@ void GenerateTrees() /** Plant a tree. * @param tile start tile of area-drag of tree plantation * @param flags type of operation - * @param p1 tree type, -1 means random. + * @param p1 tree type, TREE_INVALID means random. * @param p2 end tile of area-drag * @param text unused * @return the cost of this operation or an error @@ -345,10 +345,11 @@ CommandCost CmdPlantTree(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 { StringID msg = INVALID_STRING_ID; CommandCost cost(EXPENSES_OTHER); + const byte tree_to_plant = GB(p1, 0, 8); // We cannot use Extract as min and max are climate specific. if (p2 >= MapSize()) return CMD_ERROR; - /* Check the tree type. It can be random or some valid value within the current climate */ - if (p1 != UINT_MAX && p1 - _tree_base_by_landscape[_settings_game.game_creation.landscape] >= _tree_count_by_landscape[_settings_game.game_creation.landscape]) return CMD_ERROR; + /* Check the tree type within the current climate */ + if (tree_to_plant != TREE_INVALID && !IsInsideBS(tree_to_plant, _tree_base_by_landscape[_settings_game.game_creation.landscape], _tree_count_by_landscape[_settings_game.game_creation.landscape])) return CMD_ERROR; TileArea ta(tile, p2); TILE_AREA_LOOP(tile, ta) { @@ -401,9 +402,7 @@ CommandCost CmdPlantTree(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 } if (flags & DC_EXEC) { - TreeType treetype; - - treetype = (TreeType)p1; + TreeType treetype = (TreeType)tree_to_plant; if (treetype == TREE_INVALID) { treetype = GetRandomTreeType(tile, GB(Random(), 24, 8)); if (treetype == TREE_INVALID) treetype = TREE_CACTUS; diff --git a/src/tree_gui.cpp b/src/tree_gui.cpp index 7d8a04c6db..0c69e5008d 100644 --- a/src/tree_gui.cpp +++ b/src/tree_gui.cpp @@ -18,6 +18,7 @@ #include "company_base.h" #include "command_func.h" #include "sound_func.h" +#include "tree_map.h" #include "table/sprites.h" #include "table/strings.h" @@ -50,7 +51,7 @@ class BuildTreesWindow : public Window { uint16 base; ///< Base tree number used for drawing the window. uint16 count; ///< Number of different trees available. - uint tree_to_plant; ///< Tree number to plant, \c UINT_MAX for a random tree. + TreeType tree_to_plant; ///< Tree number to plant, \c TREE_INVALID for a random tree. public: BuildTreesWindow(const WindowDesc *desc, WindowNumber window_number) : Window() @@ -105,13 +106,13 @@ public: if (widget - BTW_TYPE_11 >= this->count) break; if (HandlePlacePushButton(this, widget, SPR_CURSOR_TREE, HT_RECT, NULL)) { - this->tree_to_plant = this->base + widget - BTW_TYPE_11; + this->tree_to_plant = (TreeType)(this->base + widget - BTW_TYPE_11); } break; case BTW_TYPE_RANDOM: // tree of random type. if (HandlePlacePushButton(this, BTW_TYPE_RANDOM, SPR_CURSOR_TREE, HT_RECT, NULL)) { - this->tree_to_plant = UINT_MAX; + this->tree_to_plant = TREE_INVALID; } break; diff --git a/src/tree_map.h b/src/tree_map.h index 18966da825..a7282cadca 100644 --- a/src/tree_map.h +++ b/src/tree_map.h @@ -22,17 +22,15 @@ * offsets from the grfs files. These points to the start of * the tree list for a landscape. See the TREE_COUNT_* enumerations * for the amount of different trees for a specific landscape. - * - * @note TREE_INVALID may be 0xFF according to the coding style, not -1 (Progman) */ enum TreeType { - TREE_INVALID = -1, ///< An invalid tree TREE_TEMPERATE = 0x00, ///< temperate tree TREE_SUB_ARCTIC = 0x0C, ///< tree on a sub_arctic landscape TREE_RAINFOREST = 0x14, ///< tree on the 'green part' on a sub-tropical map TREE_CACTUS = 0x1B, ///< a catus for the 'desert part' on a sub-tropical map TREE_SUB_TROPICAL = 0x1C, ///< tree on a sub-tropical map, non-rainforest, non-desert TREE_TOYLAND = 0x20, ///< tree on a toyland map + TREE_INVALID = 0xFF, ///< An invalid tree }; /**