Add cmtreemap command to plant trees according to a heightmap

This commit is contained in:
dP
2020-07-21 20:34:18 +03:00
parent df12479252
commit 31d0548c1a
4 changed files with 71 additions and 1 deletions

View File

@@ -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 <file>'");
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

View File

@@ -4,6 +4,7 @@
namespace citymania {
bool ConExport(byte argc, char *argv[]);
bool ConTreeMap(byte argc, char *argv[]);
} // namespace citymania

View File

@@ -2225,4 +2225,5 @@ void IConsoleStdLibRegister()
IConsoleCmdRegister("newgrf_profile", ConNewGRFProfile, ConHookNewGRFDeveloperTool);
IConsoleCmdRegister("cmexport", citymania::ConExport);
IConsoleCmdRegister("cmtreemap", citymania::ConTreeMap, ConHookNoNetwork);
}

View File

@@ -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: