OpenTTD
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 <http://www.gnu.org/licenses/>.
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/random_func.hpp"
24 #include "newgrf_generic.h"
25 
26 #include "table/strings.h"
27 #include "table/tree_land.h"
28 #include "table/clear_land.h"
29 
30 #include "safeguards.h"
31 
37 enum TreePlacer {
41 };
42 
48 };
49 
52 
53 static const uint16 DEFAULT_TREE_STEPS = 1000;
54 static const uint16 DEFAULT_RAINFOREST_TREE_STEPS = 15000;
55 static const uint16 EDITOR_TREE_DIV = 5;
56 
65 static bool CanPlantTreesOnTile(TileIndex tile, bool allow_desert)
66 {
67  switch (GetTileType(tile)) {
68  case MP_WATER:
69  return !IsBridgeAbove(tile) && IsCoast(tile) && !IsSlopeWithOneCornerRaised(GetTileSlope(tile));
70 
71  case MP_CLEAR:
72  return !IsBridgeAbove(tile) && !IsClearGround(tile, CLEAR_FIELDS) && GetRawClearGround(tile) != CLEAR_ROCKS &&
73  (allow_desert || !IsClearGround(tile, CLEAR_DESERT));
74 
75  default: return false;
76  }
77 }
78 
90 static void PlantTreesOnTile(TileIndex tile, TreeType treetype, uint count, uint growth)
91 {
92  assert(treetype != TREE_INVALID);
93  assert(CanPlantTreesOnTile(tile, true));
94 
95  TreeGround ground;
96  uint density = 3;
97 
98  switch (GetTileType(tile)) {
99  case MP_WATER:
100  ground = TREE_GROUND_SHORE;
101  break;
102 
103  case MP_CLEAR:
104  switch (GetClearGround(tile)) {
105  case CLEAR_GRASS: ground = TREE_GROUND_GRASS; break;
106  case CLEAR_ROUGH: ground = TREE_GROUND_ROUGH; break;
108  default: ground = TREE_GROUND_SNOW_DESERT; break;
109  }
110  if (GetClearGround(tile) != CLEAR_ROUGH) density = GetClearDensity(tile);
111  break;
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 {
133  case LT_TEMPERATE:
134  return (TreeType)(seed * TREE_COUNT_TEMPERATE / 256 + TREE_TEMPERATE);
135 
136  case LT_ARCTIC:
137  return (TreeType)(seed * TREE_COUNT_SUB_ARCTIC / 256 + TREE_SUB_ARCTIC);
138 
139  case LT_TROPIC:
140  switch (GetTropicZone(tile)) {
142  case TROPICZONE_DESERT: return (TreeType)((seed > 12) ? TREE_INVALID : TREE_CACTUS);
143  default: return (TreeType)(seed * TREE_COUNT_RAINFOREST / 256 + TREE_RAINFOREST);
144  }
145 
146  default:
147  return (TreeType)(seed * TREE_COUNT_TOYLAND / 256 + TREE_TOYLAND);
148  }
149 }
150 
160 static void PlaceTree(TileIndex tile, uint32 r)
161 {
162  TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));
163 
164  if (tree != TREE_INVALID) {
165  PlantTreesOnTile(tile, tree, GB(r, 22, 2), min(GB(r, 16, 3), 6));
166 
167  /* Rerandomize ground, if neither snow nor shore */
168  TreeGround ground = GetTreeGround(tile);
169  if (ground != TREE_GROUND_SNOW_DESERT && ground != TREE_GROUND_ROUGH_SNOW && ground != TREE_GROUND_SHORE) {
170  SetTreeGroundDensity(tile, (TreeGround)GB(r, 28, 1), 3);
171  }
172 
173  /* Set the counter to a random start value */
174  SetTreeCounter(tile, (TreeGround)GB(r, 24, 4));
175  }
176 }
177 
184 static void PlaceTreeGroups(uint num_groups)
185 {
186  do {
187  TileIndex center_tile = RandomTile();
188 
189  for (uint i = 0; i < DEFAULT_TREE_STEPS; i++) {
190  uint32 r = Random();
191  int x = GB(r, 0, 5) - 16;
192  int y = GB(r, 8, 5) - 16;
193  uint dist = abs(x) + abs(y);
194  TileIndex cur_tile = TileAddWrap(center_tile, x, y);
195 
197 
198  if (cur_tile != INVALID_TILE && dist <= 13 && CanPlantTreesOnTile(cur_tile, true)) {
199  PlaceTree(cur_tile, r);
200  }
201  }
202 
203  } while (--num_groups);
204 }
205 
215 static void PlaceTreeAtSameHeight(TileIndex tile, int height)
216 {
217  for (uint i = 0; i < DEFAULT_TREE_STEPS; i++) {
218  uint32 r = Random();
219  int x = GB(r, 0, 5) - 16;
220  int y = GB(r, 8, 5) - 16;
221  TileIndex cur_tile = TileAddWrap(tile, x, y);
222  if (cur_tile == INVALID_TILE) continue;
223 
224  /* Keep in range of the existing tree */
225  if (abs(x) + abs(y) > 16) continue;
226 
227  /* Clear tile, no farm-tiles or rocks */
228  if (!CanPlantTreesOnTile(cur_tile, true)) continue;
229 
230  /* Not too much height difference */
231  if (Delta(GetTileZ(cur_tile), height) > 2) continue;
232 
233  /* Place one tree and quit */
234  PlaceTree(cur_tile, r);
235  break;
236  }
237 }
238 
245 {
246  int i, j, ht;
247 
249  if (_game_mode == GM_EDITOR) i /= EDITOR_TREE_DIV;
250  do {
251  uint32 r = Random();
252  TileIndex tile = RandomTileSeed(r);
253 
255 
256  if (CanPlantTreesOnTile(tile, true)) {
257  PlaceTree(tile, r);
259 
260  /* Place a number of trees based on the tile height.
261  * This gives a cool effect of multiple trees close together.
262  * It is almost real life ;) */
263  ht = GetTileZ(tile);
264  /* The higher we get, the more trees we plant */
265  j = GetTileZ(tile) * 2;
266  /* Above snowline more trees! */
267  if (_settings_game.game_creation.landscape == LT_ARCTIC && ht > GetSnowLine()) j *= 3;
268  while (j--) {
269  PlaceTreeAtSameHeight(tile, ht);
270  }
271  }
272  } while (--i);
273 
274  /* place extra trees at rainforest area */
275  if (_settings_game.game_creation.landscape == LT_TROPIC) {
277  if (_game_mode == GM_EDITOR) i /= EDITOR_TREE_DIV;
278 
279  do {
280  uint32 r = Random();
281  TileIndex tile = RandomTileSeed(r);
282 
284 
285  if (GetTropicZone(tile) == TROPICZONE_RAINFOREST && CanPlantTreesOnTile(tile, false)) {
286  PlaceTree(tile, r);
287  }
288  } while (--i);
289  }
290 }
291 
299 {
300  uint i, total;
301 
303 
305  case TP_ORIGINAL: i = _settings_game.game_creation.landscape == LT_ARCTIC ? 15 : 6; break;
306  case TP_IMPROVED: i = _settings_game.game_creation.landscape == LT_ARCTIC ? 4 : 2; break;
307  default: NOT_REACHED();
308  }
309 
312  total *= i;
313  uint num_groups = (_settings_game.game_creation.landscape != LT_TOYLAND) ? ScaleByMapSize(GB(Random(), 0, 5) + 25) : 0;
314  total += num_groups * DEFAULT_TREE_STEPS;
316 
317  if (num_groups != 0) PlaceTreeGroups(num_groups);
318 
319  for (; i != 0; i--) {
321  }
322 }
323 
333 CommandCost CmdPlantTree(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
334 {
337  const byte tree_to_plant = GB(p1, 0, 8); // We cannot use Extract as min and max are climate specific.
338 
339  if (p2 >= MapSize()) return CMD_ERROR;
340  /* Check the tree type within the current climate */
341  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;
342 
343  Company *c = (_game_mode != GM_EDITOR) ? Company::GetIfValid(_current_company) : nullptr;
344  int limit = (c == nullptr ? INT32_MAX : GB(c->tree_limit, 16, 16));
345 
346  TileArea ta(tile, p2);
347  TILE_AREA_LOOP(tile, ta) {
348  switch (GetTileType(tile)) {
349  case MP_TREES:
350  /* no more space for trees? */
351  if (_game_mode != GM_EDITOR && GetTreeCount(tile) == 4) {
352  msg = STR_ERROR_TREE_ALREADY_HERE;
353  continue;
354  }
355 
356  /* Test tree limit. */
357  if (--limit < 1) {
358  msg = STR_ERROR_TREE_PLANT_LIMIT_REACHED;
359  break;
360  }
361 
362  if (flags & DC_EXEC) {
363  AddTreeCount(tile, 1);
364  MarkTileDirtyByTile(tile);
365  if (c != nullptr) c->tree_limit -= 1 << 16;
366  }
367  /* 2x as expensive to add more trees to an existing tile */
368  cost.AddCost(_price[PR_BUILD_TREES] * 2);
369  break;
370 
371  case MP_WATER:
372  if (!IsCoast(tile) || IsSlopeWithOneCornerRaised(GetTileSlope(tile))) {
373  msg = STR_ERROR_CAN_T_BUILD_ON_WATER;
374  continue;
375  }
376  FALLTHROUGH;
377 
378  case MP_CLEAR: {
379  if (IsBridgeAbove(tile)) {
380  msg = STR_ERROR_SITE_UNSUITABLE;
381  continue;
382  }
383 
384  TreeType treetype = (TreeType)tree_to_plant;
385  /* Be a bit picky about which trees go where. */
386  if (_settings_game.game_creation.landscape == LT_TROPIC && treetype != TREE_INVALID && (
387  /* No cacti outside the desert */
388  (treetype == TREE_CACTUS && GetTropicZone(tile) != TROPICZONE_DESERT) ||
389  /* No rain forest trees outside the rain forest, except in the editor mode where it makes those tiles rain forest tile */
390  (IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS) && GetTropicZone(tile) != TROPICZONE_RAINFOREST && _game_mode != GM_EDITOR) ||
391  /* And no subtropical trees in the desert/rain forest */
393  msg = STR_ERROR_TREE_WRONG_TERRAIN_FOR_TREE_TYPE;
394  continue;
395  }
396 
397  /* Test tree limit. */
398  if (--limit < 1) {
399  msg = STR_ERROR_TREE_PLANT_LIMIT_REACHED;
400  break;
401  }
402 
403  if (IsTileType(tile, MP_CLEAR)) {
404  /* Remove fields or rocks. Note that the ground will get barrened */
405  switch (GetRawClearGround(tile)) {
406  case CLEAR_FIELDS:
407  case CLEAR_ROCKS: {
408  CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
409  if (ret.Failed()) return ret;
410  cost.AddCost(ret);
411  break;
412  }
413 
414  default: break;
415  }
416  }
417 
418  if (_game_mode != GM_EDITOR && Company::IsValidID(_current_company)) {
420  if (t != nullptr) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM, flags);
421  }
422 
423  if (flags & DC_EXEC) {
424  if (treetype == TREE_INVALID) {
425  treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
426  if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
427  }
428 
429  /* Plant full grown trees in scenario editor */
430  PlantTreesOnTile(tile, treetype, 0, _game_mode == GM_EDITOR ? 3 : 0);
431  MarkTileDirtyByTile(tile);
432  if (c != nullptr) c->tree_limit -= 1 << 16;
433 
434  /* When planting rainforest-trees, set tropiczone to rainforest in editor. */
435  if (_game_mode == GM_EDITOR && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS)) {
437  }
438  }
439  cost.AddCost(_price[PR_BUILD_TREES]);
440  break;
441  }
442 
443  default:
444  msg = STR_ERROR_SITE_UNSUITABLE;
445  break;
446  }
447 
448  /* Tree limit used up? No need to check more. */
449  if (limit < 0) break;
450  }
451 
452  if (cost.GetCost() == 0) {
453  return_cmd_error(msg);
454  } else {
455  return cost;
456  }
457 }
458 
460  byte x, y;
461 };
462 
463 static void DrawTile_Trees(TileInfo *ti)
464 {
465  switch (GetTreeGround(ti->tile)) {
466  case TREE_GROUND_SHORE: DrawShoreTile(ti->tileh); break;
467  case TREE_GROUND_GRASS: DrawClearLandTile(ti, GetTreeDensity(ti->tile)); break;
468  case TREE_GROUND_ROUGH: DrawHillyLandTile(ti); break;
469  default: DrawGroundSprite(_clear_land_sprites_snow_desert[GetTreeDensity(ti->tile)] + SlopeToSpriteOffset(ti->tileh), PAL_NONE); break;
470  }
471 
472  /* Do not draw trees when the invisible trees setting is set */
473  if (IsInvisibilitySet(TO_TREES)) return;
474 
475  uint tmp = CountBits(ti->tile + ti->x + ti->y);
476  uint index = GB(tmp, 0, 2) + (GetTreeType(ti->tile) << 2);
477 
478  /* different tree styles above one of the grounds */
480  GetTreeDensity(ti->tile) >= 2 &&
481  IsInsideMM(index, TREE_SUB_ARCTIC << 2, TREE_RAINFOREST << 2)) {
482  index += 164 - (TREE_SUB_ARCTIC << 2);
483  }
484 
485  assert(index < lengthof(_tree_layout_sprite));
486 
487  const PalSpriteID *s = _tree_layout_sprite[index];
488  const TreePos *d = _tree_layout_xy[GB(tmp, 2, 2)];
489 
490  /* combine trees into one sprite object */
492 
493  TreeListEnt te[4];
494 
495  /* put the trees to draw in a list */
496  uint trees = GetTreeCount(ti->tile);
497 
498  for (uint i = 0; i < trees; i++) {
499  SpriteID sprite = s[0].sprite + (i == trees - 1 ? GetTreeGrowth(ti->tile) : 3);
500  PaletteID pal = s[0].pal;
501 
502  te[i].sprite = sprite;
503  te[i].pal = pal;
504  te[i].x = d->x;
505  te[i].y = d->y;
506  s++;
507  d++;
508  }
509 
510  /* draw them in a sorted way */
511  int z = ti->z + GetSlopeMaxPixelZ(ti->tileh) / 2;
512 
513  for (; trees > 0; trees--) {
514  uint min = te[0].x + te[0].y;
515  uint mi = 0;
516 
517  for (uint i = 1; i < trees; i++) {
518  if ((uint)(te[i].x + te[i].y) < min) {
519  min = te[i].x + te[i].y;
520  mi = i;
521  }
522  }
523 
524  AddSortableSpriteToDraw(te[mi].sprite, te[mi].pal, ti->x + te[mi].x, ti->y + te[mi].y, 16 - te[mi].x, 16 - te[mi].y, 0x30, z, IsTransparencySet(TO_TREES), -te[mi].x, -te[mi].y);
525 
526  /* replace the removed one with the last one */
527  te[mi] = te[trees - 1];
528  }
529 
531 }
532 
533 
534 static int GetSlopePixelZ_Trees(TileIndex tile, uint x, uint y)
535 {
536  int z;
537  Slope tileh = GetTilePixelSlope(tile, &z);
538 
539  return z + GetPartialPixelZ(x & 0xF, y & 0xF, tileh);
540 }
541 
542 static Foundation GetFoundation_Trees(TileIndex tile, Slope tileh)
543 {
544  return FOUNDATION_NONE;
545 }
546 
547 static CommandCost ClearTile_Trees(TileIndex tile, DoCommandFlag flags)
548 {
549  uint num;
550 
553  if (t != nullptr) ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM, flags);
554  }
555 
556  num = GetTreeCount(tile);
557  if (IsInsideMM(GetTreeType(tile), TREE_RAINFOREST, TREE_CACTUS)) num *= 4;
558 
559  if (flags & DC_EXEC) DoClearSquare(tile);
560 
561  return CommandCost(EXPENSES_CONSTRUCTION, num * _price[PR_CLEAR_TREES]);
562 }
563 
564 static void GetTileDesc_Trees(TileIndex tile, TileDesc *td)
565 {
566  TreeType tt = GetTreeType(tile);
567 
569  td->str = STR_LAI_TREE_NAME_RAINFOREST;
570  } else {
571  td->str = tt == TREE_CACTUS ? STR_LAI_TREE_NAME_CACTUS_PLANTS : STR_LAI_TREE_NAME_TREES;
572  }
573 
574  td->owner[0] = GetTileOwner(tile);
575 }
576 
577 static void TileLoopTreesDesert(TileIndex tile)
578 {
579  switch (GetTropicZone(tile)) {
580  case TROPICZONE_DESERT:
581  if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) {
583  MarkTileDirtyByTile(tile);
584  }
585  break;
586 
587  case TROPICZONE_RAINFOREST: {
588  static const SoundFx forest_sounds[] = {
589  SND_42_LOON_BIRD,
590  SND_43_LION,
591  SND_44_MONKEYS,
592  SND_48_DISTANT_BIRD
593  };
594  uint32 r = Random();
595 
596  if (Chance16I(1, 200, r) && _settings_client.sound.ambient) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
597  break;
598  }
599 
600  default: break;
601  }
602 }
603 
604 static void TileLoopTreesAlps(TileIndex tile)
605 {
606  int k = GetTileZ(tile) - GetSnowLine() + 1;
607 
608  if (k < 0) {
609  switch (GetTreeGround(tile)) {
612  default: return;
613  }
614  } else {
615  uint density = min<uint>(k, 3);
616 
619  SetTreeGroundDensity(tile, tg, density);
620  } else if (GetTreeDensity(tile) != density) {
621  SetTreeGroundDensity(tile, GetTreeGround(tile), density);
622  } else {
623  if (GetTreeDensity(tile) == 3) {
624  uint32 r = Random();
625  if (Chance16I(1, 200, r) && _settings_client.sound.ambient) {
626  SndPlayTileFx((r & 0x80000000) ? SND_39_HEAVY_WIND : SND_34_WIND, tile);
627  }
628  }
629  return;
630  }
631  }
632  MarkTileDirtyByTile(tile);
633 }
634 
635 static void TileLoop_Trees(TileIndex tile)
636 {
637  if (GetTreeGround(tile) == TREE_GROUND_SHORE) {
638  TileLoop_Water(tile);
639  } else {
641  case LT_TROPIC: TileLoopTreesDesert(tile); break;
642  case LT_ARCTIC: TileLoopTreesAlps(tile); break;
643  }
644  }
645 
646  AmbientSoundEffect(tile);
647 
648  uint treeCounter = GetTreeCounter(tile);
649 
650  /* Handle growth of grass (under trees/on MP_TREES tiles) at every 8th processings, like it's done for grass on MP_CLEAR tiles. */
651  if ((treeCounter & 7) == 7 && GetTreeGround(tile) == TREE_GROUND_GRASS) {
652  uint density = GetTreeDensity(tile);
653  if (density < 3) {
654  SetTreeGroundDensity(tile, TREE_GROUND_GRASS, density + 1);
655  MarkTileDirtyByTile(tile);
656  }
657  }
658  if (GetTreeCounter(tile) < 15) {
659  AddTreeCounter(tile, 1);
660  return;
661  }
662  SetTreeCounter(tile, 0);
663 
664  switch (GetTreeGrowth(tile)) {
665  case 3: // regular sized tree
666  if (_settings_game.game_creation.landscape == LT_TROPIC &&
667  GetTreeType(tile) != TREE_CACTUS &&
668  GetTropicZone(tile) == TROPICZONE_DESERT) {
669  AddTreeGrowth(tile, 1);
670  } else {
671  switch (GB(Random(), 0, 3)) {
672  case 0: // start destructing
673  AddTreeGrowth(tile, 1);
674  break;
675 
676  case 1: // add a tree
677  if (GetTreeCount(tile) < 4) {
678  AddTreeCount(tile, 1);
679  SetTreeGrowth(tile, 0);
680  break;
681  }
682  FALLTHROUGH;
683 
684  case 2: { // add a neighbouring tree
685  /* Don't plant extra trees if that's not allowed. */
689  break;
690  }
691 
692  TreeType treetype = GetTreeType(tile);
693 
694  tile += TileOffsByDir((Direction)(Random() & 7));
695 
696  /* Cacti don't spread */
697  if (!CanPlantTreesOnTile(tile, false)) return;
698 
699  /* Don't plant trees, if ground was freshly cleared */
700  if (IsTileType(tile, MP_CLEAR) && GetClearGround(tile) == CLEAR_GRASS && GetClearDensity(tile) != 3) return;
701 
702  PlantTreesOnTile(tile, treetype, 0, 0);
703 
704  break;
705  }
706 
707  default:
708  return;
709  }
710  }
711  break;
712 
713  case 6: // final stage of tree destruction
714  if (GetTreeCount(tile) > 1) {
715  /* more than one tree, delete it */
716  AddTreeCount(tile, -1);
717  SetTreeGrowth(tile, 3);
718  } else {
719  /* just one tree, change type into MP_CLEAR */
720  switch (GetTreeGround(tile)) {
721  case TREE_GROUND_SHORE: MakeShore(tile); break;
722  case TREE_GROUND_GRASS: MakeClear(tile, CLEAR_GRASS, GetTreeDensity(tile)); break;
723  case TREE_GROUND_ROUGH: MakeClear(tile, CLEAR_ROUGH, 3); break;
724  case TREE_GROUND_ROUGH_SNOW: {
725  uint density = GetTreeDensity(tile);
726  MakeClear(tile, CLEAR_ROUGH, 3);
727  MakeSnow(tile, density);
728  break;
729  }
730  default: // snow or desert
731  if (_settings_game.game_creation.landscape == LT_TROPIC) {
732  MakeClear(tile, CLEAR_DESERT, GetTreeDensity(tile));
733  } else {
734  uint density = GetTreeDensity(tile);
735  MakeClear(tile, CLEAR_GRASS, 3);
736  MakeSnow(tile, density);
737  }
738  break;
739  }
740  }
741  break;
742 
743  default:
744  AddTreeGrowth(tile, 1);
745  break;
746  }
747 
748  MarkTileDirtyByTile(tile);
749 }
750 
751 void OnTick_Trees()
752 {
753  /* Don't place trees if that's not allowed */
755 
756  uint32 r;
757  TileIndex tile;
758  TreeType tree;
759 
760  /* place a tree at a random rainforest spot */
761  if (_settings_game.game_creation.landscape == LT_TROPIC &&
762  (r = Random(), tile = RandomTileSeed(r), GetTropicZone(tile) == TROPICZONE_RAINFOREST) &&
763  CanPlantTreesOnTile(tile, false) &&
764  (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
765  PlantTreesOnTile(tile, tree, 0, 0);
766  }
767 
768  /* byte underflow */
770 
771  /* place a tree at a random spot */
772  r = Random();
773  tile = RandomTileSeed(r);
774  if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
775  PlantTreesOnTile(tile, tree, 0, 0);
776  }
777 }
778 
779 static TrackStatus GetTileTrackStatus_Trees(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
780 {
781  return 0;
782 }
783 
784 static void ChangeTileOwner_Trees(TileIndex tile, Owner old_owner, Owner new_owner)
785 {
786  /* not used */
787 }
788 
789 void InitializeTrees()
790 {
791  _trees_tick_ctr = 0;
792 }
793 
794 static CommandCost TerraformTile_Trees(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
795 {
796  return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
797 }
798 
799 
800 extern const TileTypeProcs _tile_type_trees_procs = {
801  DrawTile_Trees, // draw_tile_proc
802  GetSlopePixelZ_Trees, // get_slope_z_proc
803  ClearTile_Trees, // clear_tile_proc
804  nullptr, // add_accepted_cargo_proc
805  GetTileDesc_Trees, // get_tile_desc_proc
806  GetTileTrackStatus_Trees, // get_tile_track_status_proc
807  nullptr, // click_tile_proc
808  nullptr, // animate_tile_proc
809  TileLoop_Trees, // tile_loop_proc
810  ChangeTileOwner_Trees, // change_tile_owner_proc
811  nullptr, // add_produced_cargo_proc
812  nullptr, // vehicle_enter_tile_proc
813  GetFoundation_Trees, // get_foundation_proc
814  TerraformTile_Trees, // terraform_tile_proc
815 };
static const uint TREE_COUNT_TEMPERATE
number of tree types on a temperate map.
Definition: tree_map.h:41
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
Owner
Enum for all companies/owners.
Definition: company_type.h:18
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
#define RandomTile()
Get a valid random tile.
Definition: map_func.h:435
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:79
Generate trees.
Definition: genworld.h:75
Definition of stuff that is very close to a company, like the company struct itself.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:302
static const uint TREE_COUNT_SUB_TROPICAL
number of tree types for the &#39;sub-tropic part&#39; of a sub-tropic map.
Definition: tree_map.h:44
void GenerateTrees()
Place new trees.
Definition: tree_cmd.cpp:298
static TropicZone GetTropicZone(TileIndex tile)
Get the tropic zone.
Definition: tile_map.h:238
Rainforest tile.
Definition: tile_type.h:72
static void AddTreeCounter(TileIndex t, int a)
Add a value on the tick counter of a tree-tile.
Definition: tree_map.h:241
Normal tropiczone.
Definition: tile_type.h:70
Tile information, used while rendering the tile.
Definition: tile_cmd.h:42
static const uint TREE_COUNT_TOYLAND
number of tree types on a toyland map.
Definition: tree_map.h:45
static void SetTreeCounter(TileIndex t, uint c)
Set the tick counter for a tree-tile.
Definition: tree_map.h:256
void TileLoop_Water(TileIndex tile)
Let a water tile floods its diagonal adjoining tiles called from tunnelbridge_cmd, and by TileLoop_Industry() and TileLoop_Track()
Definition: water_cmd.cpp:1206
void PlaceTreesRandomly()
Place some trees randomly.
Definition: tree_cmd.cpp:244
byte landscape
the landscape we&#39;re currently in
static bool Chance16I(const uint a, const uint b, const uint32 r)
Checks if a given randomize-number is below a given probability.
Tile is desert.
Definition: tile_type.h:71
EconomySettings economy
settings to change the economy
ExtraTreePlacement
Where to place trees while in-game?
Definition: tree_cmd.cpp:44
uint GetPartialPixelZ(int x, int y, Slope corners)
Determines height at given coordinate of a slope.
Definition: landscape.cpp:215
static uint GetTreeCounter(TileIndex t)
Get the tick counter of a tree tile.
Definition: tree_map.h:226
static void MakeShore(TileIndex t)
Helper function to make a coast tile.
Definition: water_map.h:375
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
Slope tileh
Slope of the tile.
Definition: tile_cmd.h:45
static uint ScaleByMapSize(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:122
A snow tile that is rough underneath.
Definition: tree_map.h:57
byte tree_placer
the tree placer algorithm
static void SetTreeGroundDensity(TileIndex t, TreeGround g, uint d)
Set the density and ground type of a tile with trees.
Definition: tree_map.h:130
static void MakeTree(TileIndex t, TreeType type, uint count, uint growth, TreeGround ground, uint density)
Make a tree-tile.
Definition: tree_map.h:274
Combination of a palette sprite and a &#39;real&#39; sprite.
Definition: gfx_type.h:22
A &#39;improved&#39; algorithm.
Definition: tree_cmd.cpp:40
Tile description for the &#39;land area information&#39; tool.
Definition: tile_cmd.h:51
demolish a tile
Definition: command_type.h:180
bool ambient
Play ambient, industry and town sounds.
static const uint TREE_COUNT_RAINFOREST
number of tree types for the &#39;rainforest part&#39; of a sub-tropic map.
Definition: tree_map.h:43
TreePlacer
List of tree placer algorithm.
Definition: tree_cmd.cpp:37
Other expenses.
Definition: economy_type.h:161
Map accessors for tree tiles.
byte dist_local_authority
distance for town local authority, default 20
Functions related to world/map generation.
Money GetCost() const
The costs as made up to this moment.
Definition: command_type.h:82
static TreeType GetTreeType(TileIndex t)
Returns the treetype of a tile.
Definition: tree_map.h:73
Construction costs.
Definition: economy_type.h:149
Common return value for all commands.
Definition: command_type.h:23
void StartSpriteCombine()
Starts a block of sprites, which are "combined" into a single bounding box.
Definition: viewport.cpp:757
static bool IsClearGround(TileIndex t, ClearGround ct)
Set the type of clear tile.
Definition: clear_map.h:71
some rough tile
Definition: tree_map.h:54
static void AmbientSoundEffect(TileIndex tile)
Play an ambient sound effect for an empty tile.
int z
Height.
Definition: tile_cmd.h:47
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Definition: command_type.h:62
Owner owner[4]
Name of the owner(s)
Definition: tile_cmd.h:53
An invalid tree.
Definition: tree_map.h:32
static 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:248
static uint GetTreeGrowth(TileIndex t)
Returns the tree growth status.
Definition: tree_map.h:181
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:121
Functions related to (drawing on) viewports.
Pseudo random number generator.
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:59
void EndSpriteCombine()
Terminates a block of sprites started by StartSpriteCombine.
Definition: viewport.cpp:767
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:264
static bool IsBridgeAbove(TileIndex t)
checks if a bridge is set above the ground of this tile
Definition: bridge_map.h:45
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:3488
static void SetTreeGrowth(TileIndex t, uint g)
Sets the tree growth status of a tile.
Definition: tree_map.h:212
uint x
X position of the tile in unit coordinates.
Definition: tile_cmd.h:43
Foundation
Enumeration for Foundations.
Definition: slope_type.h:93
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
TileIndex tile
Tile index.
Definition: tile_cmd.h:46
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
Definition: command.cpp:441
static bool IsCoast(TileIndex t)
Is it a coast tile?
Definition: water_map.h:195
SoundSettings sound
sound effect settings
Sprites to use and how to display them for tree tiles.
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
No tree placer algorithm.
Definition: tree_cmd.cpp:38
DoCommandFlag
List of flags for a command.
Definition: command_type.h:342
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
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:78
#define TILE_AREA_LOOP(var, ta)
A loop which iterates over the tiles of a TileArea.
Definition of base types and functions in a cross-platform compatible way.
static const uint16 DEFAULT_RAINFOREST_TREE_STEPS
Default number of attempts for placing extra trees at rainforest in tropic.
Definition: tree_cmd.cpp:54
A number of safeguards to prevent using unsafe methods.
static void PlaceTreeAtSameHeight(TileIndex tile, int height)
Place a tree at the same height as an existing tree.
Definition: tree_cmd.cpp:215
Direction
Defines the 8 directions on the map.
static void PlaceTreeGroups(uint num_groups)
Creates a number of tree groups.
Definition: tree_cmd.cpp:184
Water tile.
Definition: tile_type.h:47
uint y
Y position of the tile in unit coordinates.
Definition: tile_cmd.h:44
Place trees on no tiles.
Definition: tree_cmd.cpp:45
static Slope GetTilePixelSlope(TileIndex tile, int *h)
Return the slope of a given tile.
Definition: tile_map.h:280
static bool IsSlopeWithOneCornerRaised(Slope s)
Tests if a specific slope has exactly one corner raised.
Definition: slope_func.h:88
Represents the covered area of e.g.
Definition: tilearea_type.h:16
Tables with sprites for clear land and fences.
static uint GetTreeDensity(TileIndex t)
Returns the &#39;density&#39; of a tile with trees.
Definition: tree_map.h:113
tree on a sub_arctic landscape
Definition: tree_map.h:27
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:1938
static void PlantTreesOnTile(TileIndex tile, TreeType treetype, uint count, uint growth)
Creates a tree tile Ground type and density is preserved.
Definition: tree_cmd.cpp:90
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
The original algorithm.
Definition: tree_cmd.cpp:39
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
static ClearGround GetClearGround(TileIndex t)
Get the type of clear tile.
Definition: clear_map.h:59
Functions related to sound.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
a desert or snow tile, depend on landscape
Definition: tree_map.h:55
bool Failed() const
Did this command fail?
Definition: command_type.h:159
a cactus for the &#39;desert part&#39; on a sub-tropical map
Definition: tree_map.h:29
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition: viewport.cpp:659
uint32 tree_limit
Amount of trees we can (still) plant (times 65536).
Definition: company_base.h:87
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:33
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:114
Functions related to clear (MP_CLEAR) land.
static uint GetClearDensity(TileIndex t)
Get the density of a non-field clear tile.
Definition: clear_map.h:83
static TileIndex RandomTileSeed(uint32 r)
Get a random tile out of a given seed.
Definition: map_func.h:424
execute the given command
Definition: command_type.h:344
temperate tree
Definition: tree_map.h:26
static int GetSlopeMaxPixelZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height) ...
Definition: slope_func.h:173
static void AddTreeCount(TileIndex t, int c)
Add a amount to the tree-count value of a tile with trees.
Definition: tree_map.h:166
Functions related to companies.
Tile got trees.
Definition: tile_type.h:45
static void PlaceTree(TileIndex tile, uint32 r)
Make a random tree tile of the given tile.
Definition: tree_cmd.cpp:160
static uint MapSize()
Get the size of the map.
Definition: map_func.h:92
Functions related to generic callbacks.
Place trees on all tiles.
Definition: tree_cmd.cpp:47
Place trees only on rainforest tiles.
Definition: tree_cmd.cpp:46
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
Set of callback functions for performing tile operations of a given tile type.
Definition: tile_cmd.h:145
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
Map accessors for &#39;clear&#39; tiles.
static TreeGround GetTreeGround(TileIndex t)
Returns the groundtype for tree tiles.
Definition: tree_map.h:88
static uint GetTreeCount(TileIndex t)
Returns the number of trees on a tile.
Definition: tree_map.h:149
static void MakeSnow(TileIndex t, uint density=0)
Make a snow tile.
Definition: clear_map.h:300
static void MakeClear(TileIndex t, ClearGround g, uint density)
Make a clear tile.
Definition: clear_map.h:259
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:81
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:94
TransportType
Available types of transport.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static ClearGround GetRawClearGround(TileIndex t)
Get the type of clear tile but never return CLEAR_SNOW.
Definition: clear_map.h:47
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
0-3
Definition: clear_map.h:24
Town data structure.
Definition: town.h:53
tree on a toyland map
Definition: tree_map.h:31
Functions related to OTTD&#39;s landscape.
static TileIndexDiff TileOffsByDir(Direction dir)
Convert a Direction to a TileIndexDiff.
Definition: map_func.h:355
static uint CountBits(T value)
Counts the number of set bits in a variable.
Functions related to commands.
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:45
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:575
TreeType
List of tree types along all landscape types.
Definition: tree_map.h:25
byte GetSnowLine()
Get the current snow line, either variable or static.
Definition: landscape.cpp:644
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
static void AddTreeGrowth(TileIndex t, int a)
Add a value to the tree growth status.
Definition: tree_map.h:196
static uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition: slope_func.h:415
ConstructionSettings construction
construction of things in-game
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
trees
Definition: transparency.h:24
normal grass
Definition: tree_map.h:53
StringID str
Description of the tile.
Definition: tile_cmd.h:52
DiagDirection
Enumeration for diagonal directions.
byte _trees_tick_ctr
Determines when to consider building more trees.
Definition: tree_cmd.cpp:51
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
Base of the town class.
GameCreationSettings game_creation
settings used during the creation of a game (map)
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition: tile_type.h:41
tree on a sub-tropical map, non-rainforest, non-desert
Definition: tree_map.h:30
TreeGround
Enumeration for ground types of tiles with trees.
Definition: tree_map.h:52
static bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren&#39;t in the game menu (there&#39;s never transpar...
Definition: transparency.h:59
static const uint TREE_COUNT_SUB_ARCTIC
number of tree types on a sub arctic map.
Definition: tree_map.h:42
void SetGeneratingWorldProgress(GenWorldProgress cls, uint total)
Set the total of a stage of the world generation.
tree on the &#39;green part&#39; on a sub-tropical map
Definition: tree_map.h:28
Functions related to water (management)
SpriteID sprite
The &#39;real&#39; sprite.
Definition: gfx_type.h:23
CommandCost CmdPlantTree(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Plant a tree.
Definition: tree_cmd.cpp:333
void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
Increases the current stage of the world generation with one.
static T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition: math_func.hpp:230
static const uint16 EDITOR_TREE_DIV
Game editor tree generation divisor factor.
Definition: tree_cmd.cpp:55
SoundFx
Sound effects from baseset.
Definition: sound_type.h:37
static bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren&#39;t in the game menu (there&#39;s never transpar...
Definition: transparency.h:48
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:65
static void SetTropicZone(TileIndex tile, TropicZone type)
Set the tropic zone.
Definition: tile_map.h:225
void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags)
Changes town rating of the current company.
Definition: town_cmd.cpp:3567
uint8 extra_tree_placement
(dis)allow building extra trees in-game
static const uint16 DEFAULT_TREE_STEPS
Default number of attempts for placing trees.
Definition: tree_cmd.cpp:53
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition: gfx_type.h:24