MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / BuildLandLegend

Function BuildLandLegend

src/smallmap_gui.cpp:290–336  ·  view source on GitHub ↗

* (Re)build the colour tables for the legends. */

Source from the content-addressed store, hash-verified

288 * (Re)build the colour tables for the legends.
289 */
290void BuildLandLegend()
291{
292 /* The smallmap window has never been initialized, so no need to change the legend. */
293 if (_heightmap_schemes[0].height_colours.empty()) return;
294
295 /*
296 * The general idea of this function is to fill the legend with an appropriate evenly spaced
297 * selection of height levels. All entries with STR_TINY_BLACK_HEIGHT are reserved for this.
298 * At the moment there are twelve of these.
299 *
300 * The table below defines up to which height level a particular delta in the legend should be
301 * used. One could opt for just dividing the maximum height and use that as delta, but that
302 * creates many "ugly" legend labels, e.g. once every 950 meter. As a result, this table will
303 * reduce the number of deltas to 7: every 100m, 200m, 300m, 500m, 750m, 1000m and 1250m. The
304 * deltas are closer together at the lower numbers because going from 12 entries to just 4, as
305 * would happen when replacing 200m and 300m by 250m, would mean the legend would be short and
306 * that might not be considered appropriate.
307 *
308 * The current method yields at least 7 legend entries and at most 12. It can be increased to
309 * 8 by adding a 150m and 400m option, but especially 150m creates ugly heights.
310 *
311 * It tries to evenly space the legend items over the two columns that are there for the legend.
312 */
313
314 /* Table for delta; if max_height is less than the first column, use the second column as value. */
315 uint deltas[][2] = { { 24, 2 }, { 48, 4 }, { 72, 6 }, { 120, 10 }, { 180, 15 }, { 240, 20 }, { MAX_TILE_HEIGHT + 1, 25 }};
316 uint i = 0;
317 for (; _settings_game.construction.map_height_limit >= deltas[i][0]; i++) {
318 /* Nothing to do here. */
319 }
320 uint delta = deltas[i][1];
321
322 int total_entries = (_settings_game.construction.map_height_limit / delta) + 1;
323 int rows = CeilDiv(total_entries, 2);
324 int j = 0;
325
326 for (i = 0; i < lengthof(_legend_land_contours) - 1 && j < total_entries; i++) {
327 if (_legend_land_contours[i].legend != STR_TINY_BLACK_HEIGHT) continue;
328
329 _legend_land_contours[i].col_break = j % rows == 0;
330 _legend_land_contours[i].end = false;
331 _legend_land_contours[i].height = j * delta;
332 _legend_land_contours[i].colour = PixelColour{static_cast<uint8_t>(_heightmap_schemes[_settings_client.gui.smallmap_land_colour].height_colours[_legend_land_contours[i].height])};
333 j++;
334 }
335 _legend_land_contours[i].end = true;
336}
337
338/**
339 * Completes the array for the owned property legend.

Callers 2

RedrawSmallmapFunction · 0.85

Calls 2

CeilDivFunction · 0.85
emptyMethod · 0.45

Tested by

no test coverage detected