diff --git a/src/citymania/cm_console_cmds.cpp b/src/citymania/cm_console_cmds.cpp index 8414c50a3e..ceacda37d2 100644 --- a/src/citymania/cm_console_cmds.cpp +++ b/src/citymania/cm_console_cmds.cpp @@ -4,11 +4,18 @@ #include "cm_export.hpp" +#include "../command_func.h" #include "../console_func.h" #include "../console_type.h" +#include "../fileio_type.h" +#include "../map_type.h" +#include "../map_func.h" +#include "../tree_map.h" #include "../safeguards.h" +bool ReadHeightMap(DetailedFileType dft, const char *filename, uint *x, uint *y, byte **map); + namespace citymania { static void IConsoleHelp(const char *str) @@ -28,4 +35,65 @@ bool ConExport(byte argc, char *argv[]) { return true; } +bool ConTreeMap(byte argc, char *argv[]) { + if (argc == 0) { + IConsoleHelp("Loads heighmap-like file and plants trees according to it, values 0-256 ore scaled to 0-4 trees."); + IConsoleHelp("Usage: 'cmtreemap '"); + IConsoleHelp("Default lookup patch is in scenario/heightmap in your openttd directory"); + return true; + } + + if (argc != 2) return false; + + std::string filename = argv[1]; + + if (_game_mode != GM_EDITOR) { + IConsolePrintF(CC_ERROR, "This command is only available in scenario editor."); + return true; + } + + + if (filename.size() < 4) { + IConsolePrintF(CC_ERROR, "Unknown treemap extension should be .bmp or .png."); + return true; + } + + auto ext = filename.substr(filename.length() - 4, 4); + DetailedFileType dft; + if (ext == ".bmp") dft = DFT_HEIGHTMAP_BMP; +#ifdef WITH_PNG + else if (ext == ".png") dft = DFT_HEIGHTMAP_PNG; +#endif + else { +#ifdef WITH_PNG + IConsolePrintF(CC_ERROR, "Unknown treemap extension %s, should be .bmp or .png.", ext.c_str()); +#else + IConsolePrintF(CC_ERROR, "Unknown treemap extension %s, should be .bmp (game was compiled without PNG support).", ext.c_str()); +#endif + return true; + } + + uint x, y; + byte *map = nullptr; + + if (!ReadHeightMap(dft, filename.c_str(), &x, &y, &map)) { + free(map); + return true; + } + + for (TileIndex tile = 0; tile < MapSize(); tile++) { + auto mx = x * TileX(tile) / MapSizeX(); + auto my = y * TileY(tile) / MapSizeY(); + auto t = map[mx + my * x]; + auto tree_count = min(t / 51, 4); + // auto tree_growth = (uint)(t % 51) * 7 / 50; + for (auto i = 0; i < tree_count; i++) { + DoCommand(tile, TREE_INVALID, tile, DC_EXEC, CMD_PLANT_TREE); + } + } + + free(map); + return true; +} + } // namespace citymania diff --git a/src/citymania/cm_console_cmds.hpp b/src/citymania/cm_console_cmds.hpp index b01e03fd6e..8ca2af7db5 100644 --- a/src/citymania/cm_console_cmds.hpp +++ b/src/citymania/cm_console_cmds.hpp @@ -4,6 +4,7 @@ namespace citymania { bool ConExport(byte argc, char *argv[]); +bool ConTreeMap(byte argc, char *argv[]); } // namespace citymania diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 4121945965..44d761ed75 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -2225,4 +2225,5 @@ void IConsoleStdLibRegister() IConsoleCmdRegister("newgrf_profile", ConNewGRFProfile, ConHookNewGRFDeveloperTool); IConsoleCmdRegister("cmexport", citymania::ConExport); + IConsoleCmdRegister("cmtreemap", citymania::ConTreeMap, ConHookNoNetwork); } diff --git a/src/heightmap.cpp b/src/heightmap.cpp index fab93c9802..ef95dc9770 100644 --- a/src/heightmap.cpp +++ b/src/heightmap.cpp @@ -450,7 +450,7 @@ void FixSlopes() * @param[in,out] map If not \c nullptr, destination to store the loaded block of image data. * @return Whether loading was successful. */ -static bool ReadHeightMap(DetailedFileType dft, const char *filename, uint *x, uint *y, byte **map) +bool ReadHeightMap(DetailedFileType dft, const char *filename, uint *x, uint *y, byte **map) { switch (dft) { default: