OpenTTD Source: src/tree_cmd.cpp Source File

OpenTTD Source 15.0-RC2

tree_cmd.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "clear_map.h"
12#include "landscape.h"
13#include "tree_map.h"
14#include "viewport_func.h"
15#include "command_func.h"
16#include "town.h"
17#include "genworld.h"
18#include "clear_func.h"
19#include "company_func.h"
20#include "sound_func.h"
21#include "water.h"
22#include "company_base.h"
23#include "core/geometry_type.hpp"
24#include "core/random_func.hpp"
25#include "newgrf_generic.h"
26#include "timer/timer_game_tick.h"
27#include "tree_cmd.h"
28#include "landscape_cmd.h"
29
30#include "table/strings.h"
31#include "table/tree_land.h"
32#include "table/clear_land.h"
33
34#include "safeguards.h"
35
37 enum ExtraTreePlacement : uint8_t {
38 ETP_NO_SPREAD,
39 ETP_SPREAD_RAINFOREST,
40 ETP_SPREAD_ALL,
41 ETP_NO_GROWTH_NO_SPREAD,
42};
43
45uint8_t _trees_tick_ctr;
46
47 static const uint16_t DEFAULT_TREE_STEPS = 1000;
48 static const uint16_t DEFAULT_RAINFOREST_TREE_STEPS = 15000;
49 static const uint16_t EDITOR_TREE_DIV = 5;
50
59 static bool CanPlantTreesOnTile(TileIndex tile, bool allow_desert)
60{
61 switch (GetTileType(tile)) {
62 case MP_WATER:
63 return !IsBridgeAbove(tile) && IsCoast(tile) && !IsSlopeWithOneCornerRaised(GetTileSlope(tile));
64
65 case MP_CLEAR:
66 return !IsBridgeAbove(tile) && !IsClearGround(tile, CLEAR_FIELDS) && !IsClearGround(tile, CLEAR_ROCKS) &&
67 (allow_desert || !IsClearGround(tile, CLEAR_DESERT));
68
69 default: return false;
70 }
71}
72
84 static void PlantTreesOnTile(TileIndex tile, TreeType treetype, uint count, TreeGrowthStage growth)
85{
86 assert(treetype != TREE_INVALID);
87 assert(CanPlantTreesOnTile(tile, true));
88
89 TreeGround ground;
90 uint density = 3;
91
92 switch (GetTileType(tile)) {
93 case MP_WATER:
94 ground = TREE_GROUND_SHORE;
95 ClearNeighbourNonFloodingStates(tile);
96 break;
97
98 case MP_CLEAR: {
99 ClearGround clearground = GetClearGround(tile);
100 if (IsSnowTile(tile)) {
101 ground = clearground == CLEAR_ROUGH ? TREE_GROUND_ROUGH_SNOW : TREE_GROUND_SNOW_DESERT;
102 } else {
103 switch (clearground) {
104 case CLEAR_GRASS: ground = TREE_GROUND_GRASS; break;
105 case CLEAR_ROUGH: ground = TREE_GROUND_ROUGH; break;
106 default: ground = TREE_GROUND_SNOW_DESERT; break;
107 }
108 }
109 if (clearground != CLEAR_ROUGH) density = GetClearDensity(tile);
110 break;
111 }
112
113 default: NOT_REACHED();
114 }
115
116 MakeTree(tile, treetype, count, growth, ground, density);
117}
118
130 static TreeType GetRandomTreeType(TileIndex tile, uint seed)
131{
132 switch (_settings_game.game_creation.landscape) {
133 case LandscapeType::Temperate:
134 return static_cast<TreeType>(seed * TREE_COUNT_TEMPERATE / 256 + TREE_TEMPERATE);
135
136 case LandscapeType::Arctic:
137 return static_cast<TreeType>(seed * TREE_COUNT_SUB_ARCTIC / 256 + TREE_SUB_ARCTIC);
138
139 case LandscapeType::Tropic:
140 switch (GetTropicZone(tile)) {
141 case TROPICZONE_NORMAL: return static_cast<TreeType>(seed * TREE_COUNT_SUB_TROPICAL / 256 + TREE_SUB_TROPICAL);
142 case TROPICZONE_DESERT: return static_cast<TreeType>((seed > 12) ? TREE_INVALID : TREE_CACTUS);
143 default: return static_cast<TreeType>(seed * TREE_COUNT_RAINFOREST / 256 + TREE_RAINFOREST);
144 }
145
146 default:
147 return static_cast<TreeType>(seed * TREE_COUNT_TOYLAND / 256 + TREE_TOYLAND);
148 }
149}
150
161 void PlaceTree(TileIndex tile, uint32_t r, bool keep_density)
162{
163 TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));
164
165 if (tree != TREE_INVALID) {
166 PlantTreesOnTile(tile, tree, GB(r, 22, 2), static_cast<TreeGrowthStage>(std::min<uint8_t>(GB(r, 16, 3), 6)));
167 MarkTileDirtyByTile(tile);
168
169 /* Maybe keep the existing ground density.*/
170 if (keep_density) return;
171
172 /* Rerandomize ground, if neither snow nor shore */
173 TreeGround ground = GetTreeGround(tile);
174 if (ground != TREE_GROUND_SNOW_DESERT && ground != TREE_GROUND_ROUGH_SNOW && ground != TREE_GROUND_SHORE) {
175 SetTreeGroundDensity(tile, (TreeGround)GB(r, 28, 1), 3);
176 }
177 }
178}
179
180 struct BlobHarmonic {
181 int amplitude;
182 float phase;
183 int frequency;
184};
185
193 static void CreateStarShapedPolygon(int radius, std::span<const BlobHarmonic> harmonics, std::span<Point> shape)
194{
195 float theta = 0;
196 float step = (M_PI * 2) / std::size(shape);
197
198 /* Divide a circle into a number of equally spaced divisions. */
199 for (Point &vertex : shape) {
200
201 /* Add up the values of each harmonic at this segment.*/
202 float deviation = std::accumulate(std::begin(harmonics), std::end(harmonics), 0.f, [theta](float d, const BlobHarmonic &harmonic) -> float {
203 return d + sinf((theta + harmonic.phase) * harmonic.frequency) * harmonic.amplitude;
204 });
205
206 /* Smooth out changes. */
207 float adjusted_radius = (radius / 2.f) + (deviation / 2);
208
209 /* Add to the final polygon. */
210 vertex.x = cosf(theta) * adjusted_radius;
211 vertex.y = sinf(theta) * adjusted_radius;
212
213 /* Proceed to the next segment. */
214 theta += step;
215 }
216}
217
224 static void CreateRandomStarShapedPolygon(int radius, std::span<Point> shape)
225{
226 /* Valid values for the phase of blob harmonics are between 0 and Tau. we can get a value in the correct range
227 * from Random() by dividing the maximum possible value by the desired maximum, and then dividing the random
228 * value by the result. */
229 static constexpr float PHASE_DIVISOR = static_cast<float>(INT32_MAX / M_PI * 2);
230
231 /* These values are ones found in testing that result in suitable-looking polygons that did not self-intersect
232 * and fit within a square of radius * radius dimensions. */
233 std::initializer_list<BlobHarmonic> harmonics = {
234 {radius / 2, Random() / PHASE_DIVISOR, 1},
235 {radius / 4, Random() / PHASE_DIVISOR, 2},
236 {radius / 8, Random() / PHASE_DIVISOR, 3},
237 {radius / 16, Random() / PHASE_DIVISOR, 4},
238 };
239
240 CreateStarShapedPolygon(radius, harmonics, shape);
241}
242
252 static bool IsPointInTriangle(int x, int y, const Point &v1, const Point &v2, const Point &v3)
253{
254 const int s = ((v1.x - v3.x) * (y - v3.y)) - ((v1.y - v3.y) * (x - v3.x));
255 const int t = ((v2.x - v1.x) * (y - v1.y)) - ((v2.y - v1.y) * (x - v1.x));
256
257 if ((s < 0) != (t < 0) && s != 0 && t != 0) return false;
258
259 const int d = (v3.x - v2.x) * (y - v2.y) - (v3.y - v2.y) * (x - v2.x);
260 return (d < 0) == (s + t <= 0);
261}
262
271 static bool IsPointInStarShapedPolygon(int x, int y, std::span<Point> shape)
272{
273 for (auto it = std::begin(shape); it != std::end(shape); /* nothing */) {
274 const Point &v1 = *it;
275 ++it;
276 const Point &v2 = (it == std::end(shape)) ? shape.front() : *it;
277
278 if (IsPointInTriangle(x, y, v1, v2, {0, 0})) return true;
279 }
280
281 return false;
282}
283
290 static void PlaceTreeGroups(uint num_groups)
291{
292 static constexpr uint GROVE_SEGMENTS = 16;
293 static constexpr uint GROVE_RADIUS = 16;
294
295 /* Shape in which trees may be contained. Array is here to reduce allocations. */
296 std::array<Point, GROVE_SEGMENTS> grove;
297
298 do {
299 TileIndex center_tile = RandomTile();
300
301 CreateRandomStarShapedPolygon(GROVE_RADIUS, grove);
302
303 for (uint i = 0; i < DEFAULT_TREE_STEPS; i++) {
304 IncreaseGeneratingWorldProgress(GWP_TREE);
305
306 uint32_t r = Random();
307 int x = GB(r, 0, 5) - GROVE_RADIUS;
308 int y = GB(r, 8, 5) - GROVE_RADIUS;
309 TileIndex cur_tile = TileAddWrap(center_tile, x, y);
310
311 if (cur_tile == INVALID_TILE) continue;
312 if (!CanPlantTreesOnTile(cur_tile, true)) continue;
313 if (!IsPointInStarShapedPolygon(x, y, grove)) continue;
314
315 PlaceTree(cur_tile, r);
316 }
317
318 } while (--num_groups);
319}
320
330 static void PlaceTreeAtSameHeight(TileIndex tile, int height)
331{
332 for (uint i = 0; i < DEFAULT_TREE_STEPS; i++) {
333 uint32_t r = Random();
334 int x = GB(r, 0, 5) - 16;
335 int y = GB(r, 8, 5) - 16;
336 TileIndex cur_tile = TileAddWrap(tile, x, y);
337 if (cur_tile == INVALID_TILE) continue;
338
339 /* Keep in range of the existing tree */
340 if (abs(x) + abs(y) > 16) continue;
341
342 /* Clear tile, no farm-tiles or rocks */
343 if (!CanPlantTreesOnTile(cur_tile, true)) continue;
344
345 /* Not too much height difference */
346 if (Delta(GetTileZ(cur_tile), height) > 2) continue;
347
348 /* Place one tree and quit */
349 PlaceTree(cur_tile, r);
350 break;
351 }
352}
353
359 void PlaceTreesRandomly()
360{
361 int i, j, ht;
362 uint8_t max_height = _settings_game.construction.map_height_limit;
363
364 i = Map::ScaleBySize(DEFAULT_TREE_STEPS);
365 if (_game_mode == GM_EDITOR) i /= EDITOR_TREE_DIV;
366 do {
367 uint32_t r = Random();
368 TileIndex tile = RandomTileSeed(r);
369
370 IncreaseGeneratingWorldProgress(GWP_TREE);
371
372 if (CanPlantTreesOnTile(tile, true)) {
373 PlaceTree(tile, r);
374 if (_settings_game.game_creation.tree_placer != TP_IMPROVED) continue;
375
376 /* Place a number of trees based on the tile height.
377 * This gives a cool effect of multiple trees close together.
378 * It is almost real life ;) */
379 ht = GetTileZ(tile);
380 /* The higher we get, the more trees we plant */
381 j = GetTileZ(tile) * 2;
382 /* Above snowline more trees! */
383 if (_settings_game.game_creation.landscape == LandscapeType::Arctic && ht > GetSnowLine()) j *= 3;
384 /* Scale generation by maximum map height. */
385 if (max_height > MAP_HEIGHT_LIMIT_ORIGINAL) j = j * MAP_HEIGHT_LIMIT_ORIGINAL / max_height;
386 while (j--) {
387 PlaceTreeAtSameHeight(tile, ht);
388 }
389 }
390 } while (--i);
391
392 /* place extra trees at rainforest area */
393 if (_settings_game.game_creation.landscape == LandscapeType::Tropic) {
394 i = Map::ScaleBySize(DEFAULT_RAINFOREST_TREE_STEPS);
395 if (_game_mode == GM_EDITOR) i /= EDITOR_TREE_DIV;
396
397 do {
398 uint32_t r = Random();
399 TileIndex tile = RandomTileSeed(r);
400
401 IncreaseGeneratingWorldProgress(GWP_TREE);
402
403 if (GetTropicZone(tile) == TROPICZONE_RAINFOREST && CanPlantTreesOnTile(tile, false)) {
404 PlaceTree(tile, r);
405 }
406 } while (--i);
407 }
408}
409
422uint PlaceTreeGroupAroundTile(TileIndex tile, TreeType treetype, uint radius, uint count, bool set_zone)
423{
424 assert(_game_mode == GM_EDITOR); // Due to InteractiveRandom being used in this function
425 assert(treetype < TREE_TOYLAND + TREE_COUNT_TOYLAND);
426 const bool allow_desert = treetype == TREE_CACTUS;
427 uint planted = 0;
428
429 for (; count > 0; count--) {
430 /* Simple quasi-normal distribution with range [-radius; radius) */
431 auto mkcoord = [&]() -> int32_t {
432 const uint32_t rand = InteractiveRandom();
433 const int32_t dist = GB<int32_t>(rand, 0, 8) + GB<int32_t>(rand, 8, 8) + GB<int32_t>(rand, 16, 8) + GB<int32_t>(rand, 24, 8);
434 const int32_t scu = dist * radius / 512;
435 return scu - radius;
436 };
437 const int32_t xofs = mkcoord();
438 const int32_t yofs = mkcoord();
439 const TileIndex tile_to_plant = TileAddWrap(tile, xofs, yofs);
440 if (tile_to_plant != INVALID_TILE) {
441 if (IsTileType(tile_to_plant, MP_TREES) && GetTreeCount(tile_to_plant) < 4) {
442 AddTreeCount(tile_to_plant, 1);
443 SetTreeGrowth(tile_to_plant, TreeGrowthStage::Growing1);
444 MarkTileDirtyByTile(tile_to_plant, 0);
445 planted++;
446 } else if (CanPlantTreesOnTile(tile_to_plant, allow_desert)) {
447 PlantTreesOnTile(tile_to_plant, treetype, 0, TreeGrowthStage::Grown);
448 MarkTileDirtyByTile(tile_to_plant, 0);
449 planted++;
450 }
451 }
452 }
453
454 if (set_zone && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS)) {
455 for (TileIndex t : TileArea(tile).Expand(radius)) {
456 if (GetTileType(t) != MP_VOID && DistanceSquare(tile, t) < radius * radius) SetTropicZone(t, TROPICZONE_RAINFOREST);
457 }
458 }
459
460 return planted;
461}
462
469 void GenerateTrees()
470{
471 uint i, total;
472
473 if (_settings_game.game_creation.tree_placer == TP_NONE) return;
474
475 switch (_settings_game.game_creation.tree_placer) {
476 case TP_ORIGINAL: i = _settings_game.game_creation.landscape == LandscapeType::Arctic ? 15 : 6; break;
477 case TP_IMPROVED: i = _settings_game.game_creation.landscape == LandscapeType::Arctic ? 4 : 2; break;
478 default: NOT_REACHED();
479 }
480
481 total = Map::ScaleBySize(DEFAULT_TREE_STEPS);
482 if (_settings_game.game_creation.landscape == LandscapeType::Tropic) total += Map::ScaleBySize(DEFAULT_RAINFOREST_TREE_STEPS);
483 total *= i;
484 uint num_groups = (_settings_game.game_creation.landscape != LandscapeType::Toyland) ? Map::ScaleBySize(GB(Random(), 0, 5) + 25) : 0;
485 total += num_groups * DEFAULT_TREE_STEPS;
486 SetGeneratingWorldProgress(GWP_TREE, total);
487
488 if (num_groups != 0) PlaceTreeGroups(num_groups);
489
490 for (; i != 0; i--) {
491 PlaceTreesRandomly();
492 }
493}
494
504 CommandCost CmdPlantTree(DoCommandFlags flags, TileIndex tile, TileIndex start_tile, uint8_t tree_to_plant, bool diagonal)
505{
506 StringID msg = INVALID_STRING_ID;
507 CommandCost cost(EXPENSES_OTHER);
508
509 if (start_tile >= Map::Size()) return CMD_ERROR;
510 /* Check the tree type within the current climate */
511 if (tree_to_plant != TREE_INVALID && !IsInsideBS(tree_to_plant, _tree_base_by_landscape[to_underlying(_settings_game.game_creation.landscape)], _tree_count_by_landscape[to_underlying(_settings_game.game_creation.landscape)])) return CMD_ERROR;
512
513 Company *c = (_game_mode != GM_EDITOR) ? Company::GetIfValid(_current_company) : nullptr;
514 int limit = (c == nullptr ? INT32_MAX : GB(c->tree_limit, 16, 16));
515
516 std::unique_ptr<TileIterator> iter = TileIterator::Create(tile, start_tile, diagonal);
517 for (; *iter != INVALID_TILE; ++(*iter)) {
518 TileIndex current_tile = *iter;
519 switch (GetTileType(current_tile)) {
520 case MP_TREES:
521 /* no more space for trees? */
522 if (GetTreeCount(current_tile) == 4) {
523 msg = STR_ERROR_TREE_ALREADY_HERE;
524 continue;
525 }
526
527 /* Test tree limit. */
528 if (--limit < 1) {
529 msg = STR_ERROR_TREE_PLANT_LIMIT_REACHED;
530 break;
531 }
532
533 if (flags.Test(DoCommandFlag::Execute)) {
534 AddTreeCount(current_tile, 1);
535 MarkTileDirtyByTile(current_tile);
536 if (c != nullptr) c->tree_limit -= 1 << 16;
537 }
538 /* 2x as expensive to add more trees to an existing tile */
539 cost.AddCost(_price[PR_BUILD_TREES] * 2);
540 break;
541
542 case MP_WATER:
543 if (!IsCoast(current_tile) || IsSlopeWithOneCornerRaised(GetTileSlope(current_tile))) {
544 msg = STR_ERROR_CAN_T_BUILD_ON_WATER;
545 continue;
546 }
547 [[fallthrough]];
548
549 case MP_CLEAR: {
550 if (IsBridgeAbove(current_tile)) {
551 msg = STR_ERROR_SITE_UNSUITABLE;
552 continue;
553 }
554
555 TreeType treetype = (TreeType)tree_to_plant;
556 /* Be a bit picky about which trees go where. */
557 if (_settings_game.game_creation.landscape == LandscapeType::Tropic && treetype != TREE_INVALID && (
558 /* No cacti outside the desert */
559 (treetype == TREE_CACTUS && GetTropicZone(current_tile) != TROPICZONE_DESERT) ||
560 /* No rainforest trees outside the rainforest, except in the editor mode where it makes those tiles rainforest tile */
561 (IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS) && GetTropicZone(current_tile) != TROPICZONE_RAINFOREST && _game_mode != GM_EDITOR) ||
562 /* And no subtropical trees in the desert/rainforest */
563 (IsInsideMM(treetype, TREE_SUB_TROPICAL, TREE_TOYLAND) && GetTropicZone(current_tile) != TROPICZONE_NORMAL))) {
564 msg = STR_ERROR_TREE_WRONG_TERRAIN_FOR_TREE_TYPE;
565 continue;
566 }
567
568 /* Test tree limit. */
569 if (--limit < 1) {
570 msg = STR_ERROR_TREE_PLANT_LIMIT_REACHED;
571 break;
572 }
573
574 if (IsTileType(current_tile, MP_CLEAR)) {
575 /* Remove fields or rocks. Note that the ground will get barrened */
576 switch (GetClearGround(current_tile)) {
577 case CLEAR_FIELDS:
578 case CLEAR_ROCKS: {
579 CommandCost ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, current_tile);
580 if (ret.Failed()) return ret;
581 cost.AddCost(ret.GetCost());
582 break;
583 }
584
585 default: break;
586 }
587 }
588
589 if (_game_mode != GM_EDITOR && Company::IsValidID(_current_company)) {
590 Town *t = ClosestTownFromTile(current_tile, _settings_game.economy.dist_local_authority);
591 if (t != nullptr) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM, flags);
592 }
593
594 if (flags.Test(DoCommandFlag::Execute)) {
595 if (treetype == TREE_INVALID) {
596 treetype = GetRandomTreeType(current_tile, GB(Random(), 24, 8));
597 if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
598 }
599
600 /* Plant full grown trees in scenario editor */
601 PlantTreesOnTile(current_tile, treetype, 0, _game_mode == GM_EDITOR ? TreeGrowthStage::Grown : TreeGrowthStage::Growing1);
602 MarkTileDirtyByTile(current_tile);
603 if (c != nullptr) c->tree_limit -= 1 << 16;
604
605 /* When planting rainforest-trees, set tropiczone to rainforest in editor. */
606 if (_game_mode == GM_EDITOR && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS)) {
607 SetTropicZone(current_tile, TROPICZONE_RAINFOREST);
608 }
609 }
610 cost.AddCost(_price[PR_BUILD_TREES]);
611 break;
612 }
613
614 default:
615 msg = STR_ERROR_SITE_UNSUITABLE;
616 break;
617 }
618
619 /* Tree limit used up? No need to check more. */
620 if (limit < 0) break;
621 }
622
623 if (cost.GetCost() == 0) {
624 return CommandCost(msg);
625 } else {
626 return cost;
627 }
628}
629
630 struct TreeListEnt : PalSpriteID {
631 int8_t x, y;
632};
633
634static void DrawTile_Trees(TileInfo *ti)
635{
636 switch (GetTreeGround(ti->tile)) {
637 case TREE_GROUND_SHORE: DrawShoreTile(ti->tileh); break;
638 case TREE_GROUND_GRASS: DrawClearLandTile(ti, GetTreeDensity(ti->tile)); break;
639 case TREE_GROUND_ROUGH: DrawHillyLandTile(ti); break;
640 default: DrawGroundSprite(_clear_land_sprites_snow_desert[GetTreeDensity(ti->tile)] + SlopeToSpriteOffset(ti->tileh), PAL_NONE); break;
641 }
642
643 /* Do not draw trees when the invisible trees setting is set */
644 if (IsInvisibilitySet(TO_TREES)) return;
645
646 uint tmp = CountBits(ti->tile.base() + ti->x + ti->y);
647 uint index = GB(tmp, 0, 2) + (GetTreeType(ti->tile) << 2);
648
649 /* different tree styles above one of the grounds */
650 if ((GetTreeGround(ti->tile) == TREE_GROUND_SNOW_DESERT || GetTreeGround(ti->tile) == TREE_GROUND_ROUGH_SNOW) &&
651 GetTreeDensity(ti->tile) >= 2 &&
652 IsInsideMM(index, TREE_SUB_ARCTIC << 2, TREE_RAINFOREST << 2)) {
653 index += 164 - (TREE_SUB_ARCTIC << 2);
654 }
655
656 assert(index < lengthof(_tree_layout_sprite));
657
658 const PalSpriteID *s = _tree_layout_sprite[index];
659 const TreePos *d = _tree_layout_xy[GB(tmp, 2, 2)];
660
661 /* combine trees into one sprite object */
662 StartSpriteCombine();
663
664 TreeListEnt te[4];
665
666 /* put the trees to draw in a list */
667 uint trees = GetTreeCount(ti->tile);
668
669 for (uint i = 0; i < trees; i++) {
670 SpriteID sprite = s[0].sprite + (i == trees - 1 ? to_underlying(GetTreeGrowth(ti->tile)) : 3);
671 PaletteID pal = s[0].pal;
672
673 te[i].sprite = sprite;
674 te[i].pal = pal;
675 te[i].x = d->x;
676 te[i].y = d->y;
677 s++;
678 d++;
679 }
680
681 /* draw them in a sorted way */
682 int z = ti->z + GetSlopeMaxPixelZ(ti->tileh) / 2;
683
684 for (; trees > 0; trees--) {
685 uint min = te[0].x + te[0].y;
686 uint mi = 0;
687
688 for (uint i = 1; i < trees; i++) {
689 if ((uint)(te[i].x + te[i].y) < min) {
690 min = te[i].x + te[i].y;
691 mi = i;
692 }
693 }
694
695 SpriteBounds bounds{{}, {TILE_SIZE, TILE_SIZE, 48}, {te[mi].x, te[mi].y, 0}};
696 AddSortableSpriteToDraw(te[mi].sprite, te[mi].pal, ti->x, ti->y, z, bounds, IsTransparencySet(TO_TREES));
697
698 /* replace the removed one with the last one */
699 te[mi] = te[trees - 1];
700 }
701
702 EndSpriteCombine();
703}
704
705
706static int GetSlopePixelZ_Trees(TileIndex tile, uint x, uint y, bool)
707{
708 auto [tileh, z] = GetTilePixelSlope(tile);
709
710 return z + GetPartialPixelZ(x & 0xF, y & 0xF, tileh);
711}
712
713static Foundation GetFoundation_Trees(TileIndex, Slope)
714{
715 return FOUNDATION_NONE;
716}
717
718static CommandCost ClearTile_Trees(TileIndex tile, DoCommandFlags flags)
719{
720 uint num;
721
722 if (Company::IsValidID(_current_company)) {
723 Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
724 if (t != nullptr) ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM, flags);
725 }
726
727 num = GetTreeCount(tile);
728 if (IsInsideMM(GetTreeType(tile), TREE_RAINFOREST, TREE_CACTUS)) num *= 4;
729
730 if (flags.Test(DoCommandFlag::Execute)) DoClearSquare(tile);
731
732 return CommandCost(EXPENSES_CONSTRUCTION, num * _price[PR_CLEAR_TREES]);
733}
734
735static void GetTileDesc_Trees(TileIndex tile, TileDesc &td)
736{
737 TreeType tt = GetTreeType(tile);
738
739 if (IsInsideMM(tt, TREE_RAINFOREST, TREE_CACTUS)) {
740 td.str = STR_LAI_TREE_NAME_RAINFOREST;
741 } else {
742 td.str = tt == TREE_CACTUS ? STR_LAI_TREE_NAME_CACTUS_PLANTS : STR_LAI_TREE_NAME_TREES;
743 }
744
745 td.owner[0] = GetTileOwner(tile);
746}
747
748static void TileLoopTreesDesert(TileIndex tile)
749{
750 switch (GetTropicZone(tile)) {
751 case TROPICZONE_DESERT:
752 if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) {
753 SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, 3);
754 MarkTileDirtyByTile(tile);
755 }
756 break;
757
758 case TROPICZONE_RAINFOREST: {
759 static const SoundFx forest_sounds[] = {
760 SND_42_RAINFOREST_1,
761 SND_43_RAINFOREST_2,
762 SND_44_RAINFOREST_3,
763 SND_48_RAINFOREST_4
764 };
765 uint32_t r = Random();
766
767 if (Chance16I(1, 200, r) && _settings_client.sound.ambient) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
768 break;
769 }
770
771 default: break;
772 }
773}
774
775static void TileLoopTreesAlps(TileIndex tile)
776{
777 int k = GetTileZ(tile) - GetSnowLine() + 1;
778
779 if (k < 0) {
780 switch (GetTreeGround(tile)) {
781 case TREE_GROUND_SNOW_DESERT: SetTreeGroundDensity(tile, TREE_GROUND_GRASS, 3); break;
782 case TREE_GROUND_ROUGH_SNOW: SetTreeGroundDensity(tile, TREE_GROUND_ROUGH, 3); break;
783 default: return;
784 }
785 } else {
786 uint density = std::min<uint>(k, 3);
787
788 if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT && GetTreeGround(tile) != TREE_GROUND_ROUGH_SNOW) {
789 TreeGround tg = GetTreeGround(tile) == TREE_GROUND_ROUGH ? TREE_GROUND_ROUGH_SNOW : TREE_GROUND_SNOW_DESERT;
790 SetTreeGroundDensity(tile, tg, density);
791 } else if (GetTreeDensity(tile) != density) {
792 SetTreeGroundDensity(tile, GetTreeGround(tile), density);
793 } else {
794 if (GetTreeDensity(tile) == 3) {
795 uint32_t r = Random();
796 if (Chance16I(1, 200, r) && _settings_client.sound.ambient) {
797 SndPlayTileFx((r & 0x80000000) ? SND_39_ARCTIC_SNOW_2 : SND_34_ARCTIC_SNOW_1, tile);
798 }
799 }
800 return;
801 }
802 }
803 MarkTileDirtyByTile(tile);
804}
805
806static bool CanPlantExtraTrees(TileIndex tile)
807{
808 return ((_settings_game.game_creation.landscape == LandscapeType::Tropic && GetTropicZone(tile) == TROPICZONE_RAINFOREST) ?
809 (_settings_game.construction.extra_tree_placement == ETP_SPREAD_ALL || _settings_game.construction.extra_tree_placement == ETP_SPREAD_RAINFOREST) :
810 _settings_game.construction.extra_tree_placement == ETP_SPREAD_ALL);
811}
812
813static void TileLoop_Trees(TileIndex tile)
814{
815 if (GetTreeGround(tile) == TREE_GROUND_SHORE) {
816 TileLoop_Water(tile);
817 } else {
818 switch (_settings_game.game_creation.landscape) {
819 case LandscapeType::Tropic: TileLoopTreesDesert(tile); break;
820 case LandscapeType::Arctic: TileLoopTreesAlps(tile); break;
821 default: break;
822 }
823 }
824
825 AmbientSoundEffect(tile);
826
827 /* TimerGameTick::counter is incremented by 256 between each call, so ignore lower 8 bits.
828 * Also, we use a simple hash to spread the updates evenly over the map.
829 * 11 and 9 are just some co-prime numbers for better spread.
830 */
831 uint32_t cycle = 11 * TileX(tile) + 9 * TileY(tile) + (TimerGameTick::counter >> 8);
832
833 /* Handle growth of grass (under trees/on MP_TREES tiles) at every 8th processings, like it's done for grass on MP_CLEAR tiles. */
834 if ((cycle & 7) == 7 && GetTreeGround(tile) == TREE_GROUND_GRASS) {
835 uint density = GetTreeDensity(tile);
836 if (density < 3) {
837 SetTreeGroundDensity(tile, TREE_GROUND_GRASS, density + 1);
838 MarkTileDirtyByTile(tile);
839 }
840 }
841
842 if (_settings_game.construction.extra_tree_placement == ETP_NO_GROWTH_NO_SPREAD) return;
843
844 static const uint32_t TREE_UPDATE_FREQUENCY = 16; // How many tile updates happen for one tree update
845 if (cycle % TREE_UPDATE_FREQUENCY != TREE_UPDATE_FREQUENCY - 1) return;
846
847 switch (GetTreeGrowth(tile)) {
848 case TreeGrowthStage::Grown: // regular sized tree
849 if (_settings_game.game_creation.landscape == LandscapeType::Tropic &&
850 GetTreeType(tile) != TREE_CACTUS &&
851 GetTropicZone(tile) == TROPICZONE_DESERT) {
852 AddTreeGrowth(tile, 1);
853 } else {
854 switch (GB(Random(), 0, 3)) {
855 case 0: // start destructing
856 AddTreeGrowth(tile, 1);
857 break;
858
859 case 1: // add a tree
860 if (GetTreeCount(tile) < 4 && CanPlantExtraTrees(tile)) {
861 AddTreeCount(tile, 1);
862 SetTreeGrowth(tile, TreeGrowthStage::Growing1);
863 break;
864 }
865 [[fallthrough]];
866
867 case 2: { // add a neighbouring tree
868 if (!CanPlantExtraTrees(tile)) break;
869
870 TreeType treetype = GetTreeType(tile);
871
872 tile += TileOffsByDir(static_cast<Direction>(Random() % DIR_END));
873
874 /* Cacti don't spread */
875 if (!CanPlantTreesOnTile(tile, false)) return;
876
877 /* Don't plant trees, if ground was freshly cleared */
878 if (IsTileType(tile, MP_CLEAR) && GetClearGround(tile) == CLEAR_GRASS && GetClearDensity(tile) != 3) return;
879
880 PlantTreesOnTile(tile, treetype, 0, TreeGrowthStage::Growing1);
881
882 break;
883 }
884
885 default:
886 return;
887 }
888 }
889 break;
890
891 case TreeGrowthStage::Dead: // final stage of tree destruction
892 if (!CanPlantExtraTrees(tile)) {
893 /* if trees can't spread just plant a new one to prevent deforestation */
894 SetTreeGrowth(tile, TreeGrowthStage::Growing1);
895 } else if (GetTreeCount(tile) > 1) {
896 /* more than one tree, delete it */
897 AddTreeCount(tile, -1);
898 SetTreeGrowth(tile, TreeGrowthStage::Grown);
899 } else {
900 /* just one tree, change type into MP_CLEAR */
901 switch (GetTreeGround(tile)) {
902 case TREE_GROUND_SHORE: MakeShore(tile); break;
903 case TREE_GROUND_GRASS: MakeClear(tile, CLEAR_GRASS, GetTreeDensity(tile)); break;
904 case TREE_GROUND_ROUGH: MakeClear(tile, CLEAR_ROUGH, 3); break;
905 case TREE_GROUND_ROUGH_SNOW: {
906 uint density = GetTreeDensity(tile);
907 MakeClear(tile, CLEAR_ROUGH, 3);
908 MakeSnow(tile, density);
909 break;
910 }
911 default: // snow or desert
912 if (_settings_game.game_creation.landscape == LandscapeType::Tropic) {
913 MakeClear(tile, CLEAR_DESERT, GetTreeDensity(tile));
914 } else {
915 uint density = GetTreeDensity(tile);
916 MakeClear(tile, CLEAR_GRASS, 3);
917 MakeSnow(tile, density);
918 }
919 break;
920 }
921 }
922 break;
923
924 default:
925 AddTreeGrowth(tile, 1);
926 break;
927 }
928
929 MarkTileDirtyByTile(tile);
930}
931
938 bool DecrementTreeCounter()
939{
940 /* Ensure _trees_tick_ctr can be decremented past zero only once for the largest map size. */
941 static_assert(2 * (MAX_MAP_SIZE_BITS - MIN_MAP_SIZE_BITS) - 4 <= std::numeric_limits<uint8_t>::digits);
942
943 /* byte underflow */
944 uint8_t old_trees_tick_ctr = _trees_tick_ctr;
945 _trees_tick_ctr -= Map::ScaleBySize(1);
946 return old_trees_tick_ctr <= _trees_tick_ctr;
947}
948
953 static void PlantRandomTree(bool rainforest)
954{
955 uint32_t r = Random();
956 TileIndex tile = RandomTileSeed(r);
957
958 if (rainforest && GetTropicZone(tile) != TROPICZONE_RAINFOREST) return;
959 if (!CanPlantTreesOnTile(tile, false)) return;
960
961 TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));
962 if (tree == TREE_INVALID) return;
963
964 PlantTreesOnTile(tile, tree, 0, TreeGrowthStage::Growing1);
965}
966
967void OnTick_Trees()
968{
969 /* Don't spread trees if that's not allowed */
970 if (_settings_game.construction.extra_tree_placement == ETP_NO_SPREAD || _settings_game.construction.extra_tree_placement == ETP_NO_GROWTH_NO_SPREAD) return;
971
972 /* Skip some tree ticks for map sizes below 256 * 256. 64 * 64 is 16 times smaller, so
973 * this is the maximum number of ticks that are skipped. Number of ticks to skip is
974 * inversely proportional to map size, so that is handled to create a mask. */
975 int skip = Map::ScaleBySize(16);
976 if (skip < 16 && (TimerGameTick::counter & (16 / skip - 1)) != 0) return;
977
978 /* place a tree at a random rainforest spot */
979 if (_settings_game.game_creation.landscape == LandscapeType::Tropic) {
980 for (uint c = Map::ScaleBySize(1); c > 0; c--) {
981 PlantRandomTree(true);
982 }
983 }
984
985 if (!DecrementTreeCounter() || _settings_game.construction.extra_tree_placement == ETP_SPREAD_RAINFOREST) return;
986
987 /* place a tree at a random spot */
988 PlantRandomTree(false);
989}
990
991static TrackStatus GetTileTrackStatus_Trees(TileIndex, TransportType, uint, DiagDirection)
992{
993 return 0;
994}
995
996static void ChangeTileOwner_Trees(TileIndex, Owner, Owner)
997{
998 /* not used */
999}
1000
1001void InitializeTrees()
1002{
1003 _trees_tick_ctr = 0;
1004}
1005
1006static CommandCost TerraformTile_Trees(TileIndex tile, DoCommandFlags flags, int, Slope)
1007{
1008 return Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile);
1009}
1010
1011
1012extern const TileTypeProcs _tile_type_trees_procs = {
1013 DrawTile_Trees, // draw_tile_proc
1014 GetSlopePixelZ_Trees, // get_slope_z_proc
1015 ClearTile_Trees, // clear_tile_proc
1016 nullptr, // add_accepted_cargo_proc
1017 GetTileDesc_Trees, // get_tile_desc_proc
1018 GetTileTrackStatus_Trees, // get_tile_track_status_proc
1019 nullptr, // click_tile_proc
1020 nullptr, // animate_tile_proc
1021 TileLoop_Trees, // tile_loop_proc
1022 ChangeTileOwner_Trees, // change_tile_owner_proc
1023 nullptr, // add_produced_cargo_proc
1024 nullptr, // vehicle_enter_tile_proc
1025 GetFoundation_Trees, // get_foundation_proc
1026 TerraformTile_Trees, // terraform_tile_proc
1027 nullptr, // check_build_above_proc
1028};
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
CountBits
constexpr uint CountBits(T value)
Counts the number of set bits in a variable.
Definition bitmath_func.hpp:238
IsBridgeAbove
bool IsBridgeAbove(Tile t)
checks if a bridge is set above the ground of this tile
Definition bridge_map.h:45
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
CommandCost
Common return value for all commands.
Definition command_type.h:24
CommandCost::AddCost
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Definition command_type.h:98
CommandCost::GetCost
Money GetCost() const
The costs as made up to this moment.
Definition command_type.h:118
CommandCost::Failed
bool Failed() const
Did this command fail?
Definition command_type.h:177
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
TileIterator::Create
static std::unique_ptr< TileIterator > Create(TileIndex corner1, TileIndex corner2, bool diagonal)
Create either an OrthogonalTileIterator or DiagonalTileIterator given the diagonal parameter.
Definition tilearea.cpp:291
TimerGameTick::counter
static TickCounter counter
Monotonic counter, in ticks, since start of game.
Definition timer_game_tick.h:60
clear_func.h
Functions related to clear (MP_CLEAR) land.
clear_land.h
Tables with sprites for clear land and fences.
clear_map.h
Map accessors for 'clear' tiles.
IsClearGround
bool IsClearGround(Tile t, ClearGround ct)
Set the type of clear tile.
Definition clear_map.h:59
MakeSnow
void MakeSnow(Tile t, uint density=0)
Make a snow tile.
Definition clear_map.h:288
ClearGround
ClearGround
Ground types.
Definition clear_map.h:19
CLEAR_GRASS
@ CLEAR_GRASS
0-3
Definition clear_map.h:20
CLEAR_FIELDS
@ CLEAR_FIELDS
3
Definition clear_map.h:23
CLEAR_DESERT
@ CLEAR_DESERT
1,3
Definition clear_map.h:25
CLEAR_ROUGH
@ CLEAR_ROUGH
3
Definition clear_map.h:21
CLEAR_ROCKS
@ CLEAR_ROCKS
3
Definition clear_map.h:22
MakeClear
void MakeClear(Tile t, ClearGround g, uint density)
Make a clear tile.
Definition clear_map.h:247
GetClearGround
ClearGround GetClearGround(Tile t)
Get the type of clear tile.
Definition clear_map.h:47
IsSnowTile
bool IsSnowTile(Tile t)
Test if a tile is covered with snow.
Definition clear_map.h:35
GetClearDensity
uint GetClearDensity(Tile t)
Get the density of a non-field clear tile.
Definition clear_map.h:71
command_func.h
Functions related to commands.
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition command_func.h:28
DoCommandFlag::Execute
@ Execute
execute the given command
company_base.h
Definition of stuff that is very close to a company, like the company struct itself.
_current_company
CompanyID _current_company
Company currently doing an action.
Definition company_cmd.cpp:56
company_func.h
Functions related to companies.
Direction
Direction
Defines the 8 directions on the map.
Definition direction_type.h:24
DIR_END
@ DIR_END
Used to iterate.
Definition direction_type.h:34
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
EXPENSES_CONSTRUCTION
@ EXPENSES_CONSTRUCTION
Construction costs.
Definition economy_type.h:179
EXPENSES_OTHER
@ EXPENSES_OTHER
Other expenses.
Definition economy_type.h:191
to_underlying
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:17
genworld.h
Functions related to world/map generation.
IncreaseGeneratingWorldProgress
void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
Increases the current stage of the world generation with one.
Definition genworld_gui.cpp:1534
GWP_TREE
@ GWP_TREE
Generate trees.
Definition genworld.h:70
SetGeneratingWorldProgress
void SetGeneratingWorldProgress(GenWorldProgress cls, uint total)
Set the total of a stage of the world generation.
Definition genworld_gui.cpp:1520
MAP_HEIGHT_LIMIT_ORIGINAL
static constexpr uint MAP_HEIGHT_LIMIT_ORIGINAL
Original map height limit.
Definition genworld.h:52
geometry_type.hpp
All geometry types in OpenTTD.
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
PaletteID
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
GetSnowLine
uint8_t GetSnowLine()
Get the current snow line, either variable or static.
Definition landscape.cpp:605
MarkTileDirtyByTile
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition viewport.cpp:2128
GetPartialPixelZ
uint GetPartialPixelZ(int x, int y, Slope corners)
Determines height at given coordinate of a slope.
Definition landscape.cpp:231
landscape.h
Functions related to OTTD's landscape.
landscape_cmd.h
Command definitions related to landscape (slopes etc.).
BorderFlag::Random
@ Random
Randomise borders.
DistanceSquare
uint DistanceSquare(TileIndex t0, TileIndex t1)
Gets the 'Square' distance between the two given tiles.
Definition map.cpp:175
TileAddWrap
TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
This function checks if we add addx/addy to tile, if we do wrap around the edges.
Definition map.cpp:109
RandomTileSeed
TileIndex RandomTileSeed(uint32_t r)
Get a random tile out of a given seed.
Definition map_func.h:653
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:437
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:427
RandomTile
#define RandomTile()
Get a valid random tile.
Definition map_func.h:664
TileOffsByDir
TileIndexDiff TileOffsByDir(Direction dir)
Convert a Direction to a TileIndexDiff.
Definition map_func.h:596
MIN_MAP_SIZE_BITS
static const uint MIN_MAP_SIZE_BITS
Minimal and maximal map width and height.
Definition map_type.h:37
MAX_MAP_SIZE_BITS
static const uint MAX_MAP_SIZE_BITS
Maximal size of map is equal to 2 ^ MAX_MAP_SIZE_BITS.
Definition map_type.h:38
IsInsideBS
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
Definition math_func.hpp:252
IsInsideMM
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition math_func.hpp:267
abs
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
Delta
constexpr T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition math_func.hpp:234
newgrf_generic.h
Functions related to generic callbacks.
AmbientSoundEffect
void AmbientSoundEffect(TileIndex tile)
Play an ambient sound effect for an empty tile.
Definition newgrf_generic.h:54
random_func.hpp
Pseudo random number generator.
Chance16I
bool Chance16I(const uint32_t a, const uint32_t b, const uint32_t r)
Checks if a given randomize-number is below a given probability.
Definition random_func.hpp:118
safeguards.h
A number of safeguards to prevent using unsafe methods.
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
TP_IMPROVED
@ TP_IMPROVED
A 'improved' algorithm.
Definition settings_type.h:110
TP_NONE
@ TP_NONE
No tree placer algorithm.
Definition settings_type.h:108
TP_ORIGINAL
@ TP_ORIGINAL
The original algorithm.
Definition settings_type.h:109
IsSlopeWithOneCornerRaised
bool IsSlopeWithOneCornerRaised(Slope s)
Tests if a specific slope has exactly one corner raised.
Definition slope_func.h:88
SlopeToSpriteOffset
uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition slope_func.h:415
GetSlopeMaxPixelZ
static constexpr int GetSlopeMaxPixelZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height)
Definition slope_func.h:173
Slope
Slope
Enumeration for the slope-type.
Definition slope_type.h:48
Foundation
Foundation
Enumeration for Foundations.
Definition slope_type.h:93
FOUNDATION_NONE
@ FOUNDATION_NONE
The tile has no foundation, the slope remains unchanged.
Definition slope_type.h:94
sound_func.h
Functions related to sound.
SoundFx
SoundFx
Sound effects from baseset.
Definition sound_type.h:45
SND_39_ARCTIC_SNOW_2
@ SND_39_ARCTIC_SNOW_2
57 == 0x39 Tree ambient: arctic snow (2): heavy wind
Definition sound_type.h:104
SND_44_RAINFOREST_3
@ SND_44_RAINFOREST_3
68 == 0x44 Tree ambient: rainforest ambient (3): monkeys
Definition sound_type.h:115
SND_34_ARCTIC_SNOW_1
@ SND_34_ARCTIC_SNOW_1
52 == 0x34 Tree ambient: arctic snow (1): wind
Definition sound_type.h:99
SND_43_RAINFOREST_2
@ SND_43_RAINFOREST_2
67 == 0x43 Tree ambient: rainforest ambient (2): lion
Definition sound_type.h:114
SND_42_RAINFOREST_1
@ SND_42_RAINFOREST_1
66 == 0x42 Tree ambient: rainforest ambient (1): bird (1)
Definition sound_type.h:113
SND_48_RAINFOREST_4
@ SND_48_RAINFOREST_4
72 == 0x48 Tree ambient: rainforest ambient (4): bird (2)
Definition sound_type.h:119
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition strings_type.h:20
BlobHarmonic
Definition tree_cmd.cpp:180
ClientSettings::sound
SoundSettings sound
sound effect settings
Definition settings_type.h:653
CommandHelper
Definition command_func.h:83
CompanyProperties::tree_limit
uint32_t tree_limit
Amount of trees we can (still) plant (times 65536).
Definition company_base.h:107
Company
Definition company_base.h:127
ConstructionSettings::extra_tree_placement
uint8_t extra_tree_placement
(dis)allow building extra trees in-game
Definition settings_type.h:424
ConstructionSettings::map_height_limit
uint8_t map_height_limit
the maximum allowed heightlevel
Definition settings_type.h:410
Coord2D< int >
Coord2D::y
T y
Y coordinate.
Definition geometry_type.hpp:35
Coord2D::x
T x
X coordinate.
Definition geometry_type.hpp:34
Coord3D::x
T x
X coordinate.
Definition geometry_type.hpp:44
Coord3D::y
T y
Y coordinate.
Definition geometry_type.hpp:45
Coord3D::z
T z
Z coordinate.
Definition geometry_type.hpp:46
EconomySettings::dist_local_authority
uint8_t dist_local_authority
distance for town local authority, default 20
Definition settings_type.h:545
GameCreationSettings::tree_placer
uint8_t tree_placer
the tree placer algorithm
Definition settings_type.h:391
GameCreationSettings::landscape
LandscapeType landscape
the landscape we're currently in
Definition settings_type.h:395
GameSettings::economy
EconomySettings economy
settings to change the economy
Definition settings_type.h:642
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition settings_type.h:635
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition settings_type.h:634
Map::ScaleBySize
static uint ScaleBySize(uint n)
Scales the given value by the map size, where the given value is for a 256 by 256 map.
Definition map_func.h:340
Map::Size
static uint Size()
Get the size of the map.
Definition map_func.h:290
OrthogonalTileArea::Expand
OrthogonalTileArea & Expand(int rad)
Expand a tile area by rad tiles in each direction, keeping within map bounds.
Definition tilearea.cpp:123
PalSpriteID
Combination of a palette sprite and a 'real' sprite.
Definition gfx_type.h:22
PalSpriteID::sprite
SpriteID sprite
The 'real' sprite.
Definition gfx_type.h:23
PalSpriteID::pal
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition gfx_type.h:24
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition pool_type.hpp:376
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Definition pool_type.hpp:398
SoundSettings::ambient
bool ambient
Play ambient, industry and town sounds.
Definition settings_type.h:279
SpriteBounds
Definition sprite.h:18
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
TileDesc
Tile description for the 'land area information' tool.
Definition tile_cmd.h:36
TileDesc::str
StringID str
Description of the tile.
Definition tile_cmd.h:37
TileDesc::owner
std::array< Owner, 4 > owner
Name of the owner(s)
Definition tile_cmd.h:39
TileInfo
Tile information, used while rendering the tile.
Definition tile_cmd.h:30
TileInfo::tileh
Slope tileh
Slope of the tile.
Definition tile_cmd.h:31
TileInfo::tile
TileIndex tile
Tile index.
Definition tile_cmd.h:32
TileTypeProcs
Set of callback functions for performing tile operations of a given tile type.
Definition tile_cmd.h:152
Town
Town data structure.
Definition town.h:63
TreeListEnt
Definition tree_cmd.cpp:630
TreePos
Definition tree_land.h:16
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.cpp:116
IsTileType
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
GetTileOwner
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
GetTilePixelSlope
std::tuple< Slope, int > GetTilePixelSlope(TileIndex tile)
Return the slope of a given tile.
Definition tile_map.h:289
GetTropicZone
TropicZone GetTropicZone(Tile tile)
Get the tropic zone.
Definition tile_map.h:238
GetTileSlope
Slope GetTileSlope(TileIndex tile)
Return the slope of a given tile inside the map.
Definition tile_map.h:279
SetTropicZone
void SetTropicZone(Tile tile, TropicZone type)
Set the tropic zone.
Definition tile_map.h:225
GetTileType
static TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
TROPICZONE_RAINFOREST
@ TROPICZONE_RAINFOREST
Rainforest tile.
Definition tile_type.h:79
TROPICZONE_DESERT
@ TROPICZONE_DESERT
Tile is desert.
Definition tile_type.h:78
TROPICZONE_NORMAL
@ TROPICZONE_NORMAL
Normal tropiczone.
Definition tile_type.h:77
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
TILE_SIZE
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
MP_TREES
@ MP_TREES
Tile got trees.
Definition tile_type.h:52
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:48
MP_WATER
@ MP_WATER
Water tile.
Definition tile_type.h:54
MP_VOID
@ MP_VOID
Invisible tiles at the SW and SE border.
Definition tile_type.h:55
TileArea
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition tilearea_type.h:102
timer_game_tick.h
Definition of the tick-based game-timer.
town.h
Base of the town class.
ChangeTownRating
void ChangeTownRating(Town *t, int add, int max, DoCommandFlags flags)
Changes town rating of the current company.
Definition town_cmd.cpp:4031
ClosestTownFromTile
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
Definition town_cmd.cpp:3952
IsTransparencySet
bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren't in the game menu (there's never transpar...
Definition transparency.h:50
IsInvisibilitySet
bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren't in the game menu (there's never transpar...
Definition transparency.h:61
TO_TREES
@ TO_TREES
trees
Definition transparency.h:25
TransportType
TransportType
Available types of transport.
Definition transport_type.h:17
PlaceTree
void PlaceTree(TileIndex tile, uint32_t r, bool keep_density)
Make a random tree tile of the given tile.
Definition tree_cmd.cpp:161
PlaceTreeGroupAroundTile
uint PlaceTreeGroupAroundTile(TileIndex tile, TreeType treetype, uint radius, uint count, bool set_zone)
Place some trees in a radius around a tile.
Definition tree_cmd.cpp:422
PlantTreesOnTile
static void PlantTreesOnTile(TileIndex tile, TreeType treetype, uint count, TreeGrowthStage growth)
Creates a tree tile Ground type and density is preserved.
Definition tree_cmd.cpp:84
GetRandomTreeType
static TreeType GetRandomTreeType(TileIndex tile, uint seed)
Get a random TreeType for the given tile based on a given seed.
Definition tree_cmd.cpp:130
IsPointInTriangle
static bool IsPointInTriangle(int x, int y, const Point &v1, const Point &v2, const Point &v3)
Returns true if the given coordinates lie within a triangle.
Definition tree_cmd.cpp:252
GenerateTrees
void GenerateTrees()
Place new trees.
Definition tree_cmd.cpp:469
CreateStarShapedPolygon
static void CreateStarShapedPolygon(int radius, std::span< const BlobHarmonic > harmonics, std::span< Point > shape)
Creates a star-shaped polygon originating from (0, 0) as defined by the given harmonics.
Definition tree_cmd.cpp:193
EDITOR_TREE_DIV
static const uint16_t EDITOR_TREE_DIV
Game editor tree generation divisor factor.
Definition tree_cmd.cpp:49
CanPlantTreesOnTile
static bool CanPlantTreesOnTile(TileIndex tile, bool allow_desert)
Tests if a tile can be converted to MP_TREES This is true for clear ground without farms or rocks.
Definition tree_cmd.cpp:59
PlaceTreesRandomly
void PlaceTreesRandomly()
Place some trees randomly.
Definition tree_cmd.cpp:359
DEFAULT_TREE_STEPS
static const uint16_t DEFAULT_TREE_STEPS
Default number of attempts for placing trees.
Definition tree_cmd.cpp:47
DEFAULT_RAINFOREST_TREE_STEPS
static const uint16_t DEFAULT_RAINFOREST_TREE_STEPS
Default number of attempts for placing extra trees at rainforest in tropic.
Definition tree_cmd.cpp:48
IsPointInStarShapedPolygon
static bool IsPointInStarShapedPolygon(int x, int y, std::span< Point > shape)
Returns true if the given coordinates lie within a star shaped polygon.
Definition tree_cmd.cpp:271
PlantRandomTree
static void PlantRandomTree(bool rainforest)
Place a random tree on a random tile.
Definition tree_cmd.cpp:953
CmdPlantTree
CommandCost CmdPlantTree(DoCommandFlags flags, TileIndex tile, TileIndex start_tile, uint8_t tree_to_plant, bool diagonal)
Plant a tree.
Definition tree_cmd.cpp:504
PlaceTreeAtSameHeight
static void PlaceTreeAtSameHeight(TileIndex tile, int height)
Place a tree at the same height as an existing tree.
Definition tree_cmd.cpp:330
CreateRandomStarShapedPolygon
static void CreateRandomStarShapedPolygon(int radius, std::span< Point > shape)
Creates a random star-shaped polygon originating from (0, 0).
Definition tree_cmd.cpp:224
_trees_tick_ctr
uint8_t _trees_tick_ctr
Determines when to consider building more trees.
Definition tree_cmd.cpp:45
PlaceTreeGroups
static void PlaceTreeGroups(uint num_groups)
Creates a number of tree groups.
Definition tree_cmd.cpp:290
ExtraTreePlacement
ExtraTreePlacement
Where to place trees while in-game?
Definition tree_cmd.cpp:37
ETP_SPREAD_RAINFOREST
@ ETP_SPREAD_RAINFOREST
Grow trees on tiles that have them, only spread to new ones in rainforests.
Definition tree_cmd.cpp:39
ETP_NO_SPREAD
@ ETP_NO_SPREAD
Grow trees on tiles that have them but don't spread to new ones.
Definition tree_cmd.cpp:38
ETP_NO_GROWTH_NO_SPREAD
@ ETP_NO_GROWTH_NO_SPREAD
Don't grow trees and don't spread them at all.
Definition tree_cmd.cpp:41
ETP_SPREAD_ALL
@ ETP_SPREAD_ALL
Grow trees and spread them without restrictions.
Definition tree_cmd.cpp:40
DecrementTreeCounter
bool DecrementTreeCounter()
Decrement the tree tick counter.
Definition tree_cmd.cpp:938
tree_cmd.h
Command definitions related to tree tiles.
tree_land.h
Sprites to use and how to display them for tree tiles.
tree_map.h
Map accessors for tree tiles.
TREE_COUNT_RAINFOREST
static const uint TREE_COUNT_RAINFOREST
number of tree types for the 'rainforest part' of a sub-tropic map.
Definition tree_map.h:43
GetTreeCount
uint GetTreeCount(Tile t)
Returns the number of trees on a tile.
Definition tree_map.h:163
GetTreeGrowth
TreeGrowthStage GetTreeGrowth(Tile t)
Returns the tree growth stage.
Definition tree_map.h:195
TREE_COUNT_SUB_TROPICAL
static const uint TREE_COUNT_SUB_TROPICAL
number of tree types for the 'sub-tropic part' of a sub-tropic map.
Definition tree_map.h:44
TREE_COUNT_TOYLAND
static const uint TREE_COUNT_TOYLAND
number of tree types on a toyland map.
Definition tree_map.h:45
MakeTree
void MakeTree(Tile t, TreeType type, uint count, TreeGrowthStage growth, TreeGround ground, uint density)
Make a tree-tile.
Definition tree_map.h:244
SetTreeGrowth
void SetTreeGrowth(Tile t, TreeGrowthStage g)
Sets the tree growth stage of a tile.
Definition tree_map.h:226
GetTreeGround
TreeGround GetTreeGround(Tile t)
Returns the groundtype for tree tiles.
Definition tree_map.h:102
TreeType
TreeType
List of tree types along all landscape types.
Definition tree_map.h:25
TREE_RAINFOREST
@ TREE_RAINFOREST
tree on the 'green part' on a sub-tropical map
Definition tree_map.h:28
TREE_TOYLAND
@ TREE_TOYLAND
tree on a toyland map
Definition tree_map.h:31
TREE_SUB_ARCTIC
@ TREE_SUB_ARCTIC
tree on a sub_arctic landscape
Definition tree_map.h:27
TREE_SUB_TROPICAL
@ TREE_SUB_TROPICAL
tree on a sub-tropical map, non-rainforest, non-desert
Definition tree_map.h:30
TREE_TEMPERATE
@ TREE_TEMPERATE
temperate tree
Definition tree_map.h:26
TREE_CACTUS
@ TREE_CACTUS
a cactus for the 'desert part' on a sub-tropical map
Definition tree_map.h:29
TREE_INVALID
@ TREE_INVALID
An invalid tree.
Definition tree_map.h:32
AddTreeCount
void AddTreeCount(Tile t, int c)
Add a amount to the tree-count value of a tile with trees.
Definition tree_map.h:180
TreeGround
TreeGround
Enumeration for ground types of tiles with trees.
Definition tree_map.h:52
TREE_GROUND_GRASS
@ TREE_GROUND_GRASS
normal grass
Definition tree_map.h:53
TREE_GROUND_SHORE
@ TREE_GROUND_SHORE
shore
Definition tree_map.h:56
TREE_GROUND_ROUGH_SNOW
@ TREE_GROUND_ROUGH_SNOW
A snow tile that is rough underneath.
Definition tree_map.h:57
TREE_GROUND_SNOW_DESERT
@ TREE_GROUND_SNOW_DESERT
a desert or snow tile, depend on landscape
Definition tree_map.h:55
TREE_GROUND_ROUGH
@ TREE_GROUND_ROUGH
some rough tile
Definition tree_map.h:54
TREE_COUNT_SUB_ARCTIC
static const uint TREE_COUNT_SUB_ARCTIC
number of tree types on a sub arctic map.
Definition tree_map.h:42
GetTreeType
TreeType GetTreeType(Tile t)
Returns the treetype of a tile.
Definition tree_map.h:87
SetTreeGroundDensity
void SetTreeGroundDensity(Tile t, TreeGround g, uint d)
Set the density and ground type of a tile with trees.
Definition tree_map.h:144
AddTreeGrowth
void AddTreeGrowth(Tile t, int a)
Add a value to the tree growth stage.
Definition tree_map.h:210
GetTreeDensity
uint GetTreeDensity(Tile t)
Returns the 'density' of a tile with trees.
Definition tree_map.h:127
TreeGrowthStage
TreeGrowthStage
Enumeration for tree growth stages.
Definition tree_map.h:65
TreeGrowthStage::Grown
@ Grown
Fully grown tree.
TreeGrowthStage::Dead
@ Dead
Dead tree.
TreeGrowthStage::Growing1
@ Growing1
First stage of growth.
TREE_COUNT_TEMPERATE
static const uint TREE_COUNT_TEMPERATE
number of tree types on a temperate map.
Definition tree_map.h:41
StartSpriteCombine
void StartSpriteCombine()
Starts a block of sprites, which are "combined" into a single bounding box.
Definition viewport.cpp:764
AddSortableSpriteToDraw
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int z, const SpriteBounds &bounds, bool transparent, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition viewport.cpp:663
EndSpriteCombine
void EndSpriteCombine()
Terminates a block of sprites started by StartSpriteCombine.
Definition viewport.cpp:774
DrawGroundSprite
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite for the current tile.
Definition viewport.cpp:579
viewport_func.h
Functions related to (drawing on) viewports.
water.h
Functions related to water (management)
TileLoop_Water
void TileLoop_Water(TileIndex tile)
Let a water tile floods its diagonal adjoining tiles called from tunnelbridge_cmd,...
Definition water_cmd.cpp:1269
ClearNeighbourNonFloodingStates
void ClearNeighbourNonFloodingStates(TileIndex tile)
Clear non-flooding state of the tiles around a tile.
Definition water_cmd.cpp:97
MakeShore
void MakeShore(Tile t)
Helper function to make a coast tile.
Definition water_map.h:383
IsCoast
bool IsCoast(Tile t)
Is it a coast tile?
Definition water_map.h:203