tree_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: tree_cmd.cpp 14464 2008-10-14 18:38:51Z rubidium $ */
00002 
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "bridge_map.h"
00008 #include "clear_map.h"
00009 #include "tile_cmd.h"
00010 #include "landscape.h"
00011 #include "tree_map.h"
00012 #include "viewport_func.h"
00013 #include "command_func.h"
00014 #include "economy_func.h"
00015 #include "town.h"
00016 #include "variables.h"
00017 #include "genworld.h"
00018 #include "transparency.h"
00019 #include "functions.h"
00020 #include "company_func.h"
00021 #include "sound_func.h"
00022 #include "settings_type.h"
00023 #include "water_map.h"
00024 #include "water.h"
00025 #include "landscape_type.h"
00026 #include "company_base.h"
00027 
00028 #include "table/strings.h"
00029 #include "table/sprites.h"
00030 #include "table/tree_land.h"
00031 
00037 enum TreePlacer {
00038   TP_NONE,     
00039   TP_ORIGINAL, 
00040   TP_IMPROVED, 
00041 };
00042 
00051 static bool CanPlantTreesOnTile(TileIndex tile, bool allow_desert)
00052 {
00053   switch (GetTileType(tile)) {
00054     case MP_WATER:
00055       return !IsBridgeAbove(tile) && IsCoast(tile) && !IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL));
00056 
00057     case MP_CLEAR:
00058       return !IsBridgeAbove(tile) && !IsClearGround(tile, CLEAR_FIELDS) && !IsClearGround(tile, CLEAR_ROCKS) &&
00059              (allow_desert || !IsClearGround(tile, CLEAR_DESERT));
00060 
00061     default: return false;
00062   }
00063 }
00064 
00076 static void PlantTreesOnTile(TileIndex tile, TreeType treetype, uint count, uint growth)
00077 {
00078   assert(treetype != TREE_INVALID);
00079   assert(CanPlantTreesOnTile(tile, true));
00080 
00081   TreeGround ground;
00082   uint density = 3;
00083 
00084   switch (GetTileType(tile)) {
00085     case MP_WATER:
00086       ground = TREE_GROUND_SHORE;
00087       break;
00088 
00089     case MP_CLEAR:
00090       switch (GetClearGround(tile)) {
00091         case CLEAR_GRASS:  ground = TREE_GROUND_GRASS;       density = GetClearDensity(tile); break;
00092         case CLEAR_ROUGH:  ground = TREE_GROUND_ROUGH;                                        break;
00093         default:           ground = TREE_GROUND_SNOW_DESERT; density = GetClearDensity(tile); break;
00094       }
00095       break;
00096 
00097     default: NOT_REACHED();
00098   }
00099 
00100   MakeTree(tile, treetype, count, growth, ground, density);
00101 }
00102 
00114 static TreeType GetRandomTreeType(TileIndex tile, uint seed)
00115 {
00116   switch (_settings_game.game_creation.landscape) {
00117     case LT_TEMPERATE:
00118       return (TreeType)(seed * TREE_COUNT_TEMPERATE / 256 + TREE_TEMPERATE);
00119 
00120     case LT_ARCTIC:
00121       return (TreeType)(seed * TREE_COUNT_SUB_ARCTIC / 256 + TREE_SUB_ARCTIC);
00122 
00123     case LT_TROPIC:
00124       switch (GetTropicZone(tile)) {
00125         case TROPICZONE_NORMAL:  return (TreeType)(seed * TREE_COUNT_SUB_TROPICAL / 256 + TREE_SUB_TROPICAL);
00126         case TROPICZONE_DESERT:  return (TreeType)((seed > 12) ? TREE_INVALID : TREE_CACTUS);
00127         default:                 return (TreeType)(seed * TREE_COUNT_RAINFOREST / 256 + TREE_RAINFOREST);
00128       }
00129 
00130     default:
00131       return (TreeType)(seed * TREE_COUNT_TOYLAND / 256 + TREE_TOYLAND);
00132   }
00133 }
00134 
00144 static void PlaceTree(TileIndex tile, uint32 r)
00145 {
00146   TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));
00147 
00148   if (tree != TREE_INVALID) {
00149     PlantTreesOnTile(tile, tree, GB(r, 22, 2), min(GB(r, 16, 3), 6));
00150 
00151     /* Rerandomize ground, if neither snow nor shore */
00152     TreeGround ground = GetTreeGround(tile);
00153     if (ground != TREE_GROUND_SNOW_DESERT && ground != TREE_GROUND_SHORE) {
00154       SetTreeGroundDensity(tile, (TreeGround)GB(r, 28, 1), 3);
00155     }
00156 
00157     /* Set the counter to a random start value */
00158     SetTreeCounter(tile, (TreeGround)GB(r, 24, 4));
00159   }
00160 }
00161 
00171 static void DoPlaceMoreTrees(TileIndex tile)
00172 {
00173   uint i;
00174 
00175   for (i = 0; i < 1000; i++) {
00176     uint32 r = Random();
00177     int x = GB(r, 0, 5) - 16;
00178     int y = GB(r, 8, 5) - 16;
00179     uint dist = abs(x) + abs(y);
00180     TileIndex cur_tile = TILE_MASK(tile + TileDiffXY(x, y));
00181 
00182     if (dist <= 13 && CanPlantTreesOnTile(cur_tile, true)) {
00183       PlaceTree(cur_tile, r);
00184     }
00185   }
00186 }
00187 
00193 static void PlaceMoreTrees()
00194 {
00195   uint i = ScaleByMapSize(GB(Random(), 0, 5) + 25);
00196   do {
00197     DoPlaceMoreTrees(RandomTile());
00198   } while (--i);
00199 }
00200 
00210 static void PlaceTreeAtSameHeight(TileIndex tile, uint height)
00211 {
00212   uint i;
00213 
00214   for (i = 0; i < 1000; i++) {
00215     uint32 r = Random();
00216     int x = GB(r, 0, 5) - 16;
00217     int y = GB(r, 8, 5) - 16;
00218     TileIndex cur_tile = TILE_MASK(tile + TileDiffXY(x, y));
00219 
00220     /* Keep in range of the existing tree */
00221     if (abs(x) + abs(y) > 16) continue;
00222 
00223     /* Clear tile, no farm-tiles or rocks */
00224     if (!CanPlantTreesOnTile(cur_tile, true)) continue;
00225 
00226     /* Not too much height difference */
00227     if (Delta(GetTileZ(cur_tile), height) > 2) continue;
00228 
00229     /* Place one tree and quit */
00230     PlaceTree(cur_tile, r);
00231     break;
00232   }
00233 }
00234 
00240 void PlaceTreesRandomly()
00241 {
00242   uint i, j, ht;
00243 
00244   i = ScaleByMapSize(1000);
00245   do {
00246     uint32 r = Random();
00247     TileIndex tile = RandomTileSeed(r);
00248 
00249     IncreaseGeneratingWorldProgress(GWP_TREE);
00250 
00251     if (CanPlantTreesOnTile(tile, true)) {
00252       PlaceTree(tile, r);
00253       if (_settings_game.game_creation.tree_placer != TP_IMPROVED) continue;
00254 
00255       /* Place a number of trees based on the tile height.
00256        *  This gives a cool effect of multiple trees close together.
00257        *  It is almost real life ;) */
00258       ht = GetTileZ(tile);
00259       /* The higher we get, the more trees we plant */
00260       j = GetTileZ(tile) / TILE_HEIGHT * 2;
00261       while (j--) {
00262         /* Above snowline more trees! */
00263         if (_settings_game.game_creation.landscape == LT_ARCTIC && ht > GetSnowLine()) {
00264           PlaceTreeAtSameHeight(tile, ht);
00265           PlaceTreeAtSameHeight(tile, ht);
00266         };
00267 
00268         PlaceTreeAtSameHeight(tile, ht);
00269       }
00270     }
00271   } while (--i);
00272 
00273   /* place extra trees at rainforest area */
00274   if (_settings_game.game_creation.landscape == LT_TROPIC) {
00275     i = ScaleByMapSize(15000);
00276 
00277     do {
00278       uint32 r = Random();
00279       TileIndex tile = RandomTileSeed(r);
00280 
00281       IncreaseGeneratingWorldProgress(GWP_TREE);
00282 
00283       if (GetTropicZone(tile) == TROPICZONE_RAINFOREST && CanPlantTreesOnTile(tile, false)) {
00284         PlaceTree(tile, r);
00285       }
00286     } while (--i);
00287   }
00288 }
00289 
00296 void GenerateTrees()
00297 {
00298   uint i, total;
00299 
00300   if (_settings_game.game_creation.tree_placer == TP_NONE) return;
00301 
00302   if (_settings_game.game_creation.landscape != LT_TOYLAND) PlaceMoreTrees();
00303 
00304   switch (_settings_game.game_creation.tree_placer) {
00305     case TP_ORIGINAL: i = _settings_game.game_creation.landscape == LT_ARCTIC ? 15 : 6; break;
00306     case TP_IMPROVED: i = _settings_game.game_creation.landscape == LT_ARCTIC ?  4 : 2; break;
00307     default: NOT_REACHED(); return;
00308   }
00309 
00310   total = ScaleByMapSize(1000);
00311   if (_settings_game.game_creation.landscape == LT_TROPIC) total += ScaleByMapSize(15000);
00312   total *= i;
00313   SetGeneratingWorldProgress(GWP_TREE, total);
00314 
00315   for (; i != 0; i--) {
00316     PlaceTreesRandomly();
00317   }
00318 }
00319 
00326 CommandCost CmdPlantTree(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
00327 {
00328   StringID msg = INVALID_STRING_ID;
00329   CommandCost cost(EXPENSES_OTHER);
00330   int ex;
00331   int ey;
00332   int sx, sy, x, y;
00333 
00334   if (p2 >= MapSize()) return CMD_ERROR;
00335   /* Check the tree type. It can be random or some valid value within the current climate */
00336   if (p1 != UINT_MAX && p1 - _tree_base_by_landscape[_settings_game.game_creation.landscape] >= _tree_count_by_landscape[_settings_game.game_creation.landscape]) return CMD_ERROR;
00337 
00338   // make sure sx,sy are smaller than ex,ey
00339   ex = TileX(tile);
00340   ey = TileY(tile);
00341   sx = TileX(p2);
00342   sy = TileY(p2);
00343   if (ex < sx) Swap(ex, sx);
00344   if (ey < sy) Swap(ey, sy);
00345 
00346   for (x = sx; x <= ex; x++) {
00347     for (y = sy; y <= ey; y++) {
00348       TileIndex tile = TileXY(x, y);
00349 
00350       switch (GetTileType(tile)) {
00351         case MP_TREES:
00352           /* no more space for trees? */
00353           if (_game_mode != GM_EDITOR && GetTreeCount(tile) == 3) {
00354             msg = STR_2803_TREE_ALREADY_HERE;
00355             continue;
00356           }
00357 
00358           if (flags & DC_EXEC) {
00359             AddTreeCount(tile, 1);
00360             MarkTileDirtyByTile(tile);
00361           }
00362           /* 2x as expensive to add more trees to an existing tile */
00363           cost.AddCost(_price.build_trees * 2);
00364           break;
00365 
00366         case MP_WATER:
00367           if (!IsCoast(tile) || IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL))) {
00368             msg = STR_3807_CAN_T_BUILD_ON_WATER;
00369             continue;
00370           }
00371         /* FALL THROUGH */
00372         case MP_CLEAR:
00373           if (IsBridgeAbove(tile)) {
00374             msg = STR_2804_SITE_UNSUITABLE;
00375             continue;
00376           }
00377 
00378           if (IsTileType(tile, MP_CLEAR)) {
00379             /* Remove fields or rocks. Note that the ground will get barrened */
00380             switch (GetClearGround(tile)) {
00381               case CLEAR_FIELDS:
00382               case CLEAR_ROCKS: {
00383                 CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00384                 if (CmdFailed(ret)) return ret;
00385                 cost.AddCost(ret);
00386                 break;
00387               }
00388 
00389               default: break;
00390             }
00391           }
00392 
00393           if (_game_mode != GM_EDITOR && IsValidCompanyID(_current_company)) {
00394             Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
00395             if (t != NULL) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM);
00396           }
00397 
00398           if (flags & DC_EXEC) {
00399             TreeType treetype;
00400 
00401             treetype = (TreeType)p1;
00402             if (treetype == TREE_INVALID) {
00403               treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
00404               if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
00405             }
00406 
00407             /* Plant full grown trees in scenario editor */
00408             PlantTreesOnTile(tile, treetype, 0, _game_mode == GM_EDITOR ? 3 : 0);
00409             MarkTileDirtyByTile(tile);
00410 
00411             /* When planting rainforest-trees, set tropiczone to rainforest in editor. */
00412             if (_game_mode == GM_EDITOR && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS))
00413               SetTropicZone(tile, TROPICZONE_RAINFOREST);
00414           }
00415           cost.AddCost(_price.build_trees);
00416           break;
00417 
00418         default:
00419           msg = STR_2804_SITE_UNSUITABLE;
00420           break;
00421       }
00422     }
00423   }
00424 
00425   if (cost.GetCost() == 0) {
00426     return_cmd_error(msg);
00427   } else {
00428     return cost;
00429   }
00430 }
00431 
00432 struct TreeListEnt {
00433   SpriteID image;
00434   SpriteID pal;
00435   byte x, y;
00436 };
00437 
00438 static void DrawTile_Trees(TileInfo *ti)
00439 {
00440   switch (GetTreeGround(ti->tile)) {
00441     case TREE_GROUND_SHORE: DrawShoreTile(ti->tileh); break;
00442     case TREE_GROUND_GRASS: DrawClearLandTile(ti, GetTreeDensity(ti->tile)); break;
00443     case TREE_GROUND_ROUGH: DrawHillyLandTile(ti); break;
00444     default: DrawGroundSprite(_tree_sprites_1[GetTreeDensity(ti->tile)] + _tileh_to_sprite[ti->tileh], PAL_NONE); break;
00445   }
00446 
00447   DrawClearLandFence(ti);
00448 
00449   /* Do not draw trees when the invisible trees patch and transparency tree are set */
00450   if (IsInvisibilitySet(TO_TREES)) return;
00451 
00452   uint16 tmp = ti->x;
00453 
00454   tmp = ROR(tmp, 2);
00455   tmp -= ti->y;
00456   tmp = ROR(tmp, 3);
00457   tmp -= ti->x;
00458   tmp = ROR(tmp, 1);
00459   tmp += ti->y;
00460 
00461   uint index = GB(tmp, 6, 2) + (GetTreeType(ti->tile) << 2);
00462 
00463   /* different tree styles above one of the grounds */
00464   if (GetTreeGround(ti->tile) == TREE_GROUND_SNOW_DESERT &&
00465       GetTreeDensity(ti->tile) >= 2 &&
00466       IsInsideMM(index, TREE_SUB_ARCTIC << 2, TREE_RAINFOREST << 2)) {
00467     index += 164 - (TREE_SUB_ARCTIC << 2);
00468   }
00469 
00470   assert(index < lengthof(_tree_layout_sprite));
00471 
00472   const PalSpriteID *s = _tree_layout_sprite[index];
00473   const TreePos *d = _tree_layout_xy[GB(tmp, 4, 2)];
00474 
00475   /* combine trees into one sprite object */
00476   StartSpriteCombine();
00477 
00478   TreeListEnt te[4];
00479 
00480   /* put the trees to draw in a list */
00481   uint trees = GetTreeCount(ti->tile) + 1;
00482 
00483   for (uint i = 0; i < trees; i++) {
00484     SpriteID image = s[0].sprite + (i == trees - 1 ? GetTreeGrowth(ti->tile) : 3);
00485     SpriteID pal = s[0].pal;
00486 
00487     te[i].image = image;
00488     te[i].pal   = pal;
00489     te[i].x = d->x;
00490     te[i].y = d->y;
00491     s++;
00492     d++;
00493   }
00494 
00495   /* draw them in a sorted way */
00496   byte z = ti->z + GetSlopeMaxZ(ti->tileh) / 2;
00497 
00498   for (; trees > 0; trees--) {
00499     uint min = te[0].x + te[0].y;
00500     uint mi = 0;
00501 
00502     for (uint i = 1; i < trees; i++) {
00503       if ((uint)(te[i].x + te[i].y) < min) {
00504         min = te[i].x + te[i].y;
00505         mi = i;
00506       }
00507     }
00508 
00509     AddSortableSpriteToDraw(te[mi].image, 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);
00510 
00511     /* replace the removed one with the last one */
00512     te[mi] = te[trees - 1];
00513   }
00514 
00515   EndSpriteCombine();
00516 }
00517 
00518 
00519 static uint GetSlopeZ_Trees(TileIndex tile, uint x, uint y)
00520 {
00521   uint z;
00522   Slope tileh = GetTileSlope(tile, &z);
00523 
00524   return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
00525 }
00526 
00527 static Foundation GetFoundation_Trees(TileIndex tile, Slope tileh)
00528 {
00529   return FOUNDATION_NONE;
00530 }
00531 
00532 static CommandCost ClearTile_Trees(TileIndex tile, byte flags)
00533 {
00534   uint num;
00535 
00536   if (IsValidCompanyID(_current_company)) {
00537     Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
00538     if (t != NULL) ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM);
00539   }
00540 
00541   num = GetTreeCount(tile) + 1;
00542   if (IsInsideMM(GetTreeType(tile), TREE_RAINFOREST, TREE_CACTUS)) num *= 4;
00543 
00544   if (flags & DC_EXEC) DoClearSquare(tile);
00545 
00546   return CommandCost(EXPENSES_CONSTRUCTION, num * _price.remove_trees);
00547 }
00548 
00549 static void GetAcceptedCargo_Trees(TileIndex tile, AcceptedCargo ac)
00550 {
00551   /* not used */
00552 }
00553 
00554 static void GetTileDesc_Trees(TileIndex tile, TileDesc *td)
00555 {
00556   TreeType tt = GetTreeType(tile);
00557 
00558   if (IsInsideMM(tt, TREE_RAINFOREST, TREE_CACTUS)) {
00559     td->str = STR_280F_RAINFOREST;
00560   } else {
00561     td->str = tt == TREE_CACTUS ? STR_2810_CACTUS_PLANTS : STR_280E_TREES;
00562   }
00563 
00564   td->owner[0] = GetTileOwner(tile);
00565 }
00566 
00567 static void AnimateTile_Trees(TileIndex tile)
00568 {
00569   /* not used */
00570 }
00571 
00572 static void TileLoopTreesDesert(TileIndex tile)
00573 {
00574   switch (GetTropicZone(tile)) {
00575     case TROPICZONE_DESERT:
00576       if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) {
00577         SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, 3);
00578         MarkTileDirtyByTile(tile);
00579       }
00580       break;
00581 
00582     case TROPICZONE_RAINFOREST: {
00583       static const SoundFx forest_sounds[] = {
00584         SND_42_LOON_BIRD,
00585         SND_43_LION,
00586         SND_44_MONKEYS,
00587         SND_48_DISTANT_BIRD
00588       };
00589       uint32 r = Random();
00590 
00591       if (Chance16I(1, 200, r)) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
00592       break;
00593     }
00594 
00595     default: break;
00596   }
00597 }
00598 
00599 static void TileLoopTreesAlps(TileIndex tile)
00600 {
00601   int k = GetTileZ(tile) - GetSnowLine() + TILE_HEIGHT;
00602 
00603   if (k < 0) {
00604     if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) return;
00605     SetTreeGroundDensity(tile, TREE_GROUND_GRASS, 3);
00606   } else {
00607     uint density = min((uint)k / TILE_HEIGHT, 3);
00608 
00609     if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT ||
00610         GetTreeDensity(tile) != density) {
00611       SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, density);
00612     } else {
00613       if (GetTreeDensity(tile) == 3) {
00614         uint32 r = Random();
00615         if (Chance16I(1, 200, r)) {
00616           SndPlayTileFx((r & 0x80000000) ? SND_39_HEAVY_WIND : SND_34_WIND, tile);
00617         }
00618       }
00619       return;
00620     }
00621   }
00622   MarkTileDirtyByTile(tile);
00623 }
00624 
00625 static void TileLoop_Trees(TileIndex tile)
00626 {
00627   if (GetTreeGround(tile) == TREE_GROUND_SHORE) {
00628     TileLoop_Water(tile);
00629   } else {
00630     switch (_settings_game.game_creation.landscape) {
00631       case LT_TROPIC: TileLoopTreesDesert(tile); break;
00632       case LT_ARCTIC: TileLoopTreesAlps(tile);   break;
00633     }
00634   }
00635 
00636   TileLoopClearHelper(tile);
00637 
00638   uint treeCounter = GetTreeCounter(tile);
00639 
00640   /* Handle growth of grass at every 8th processings, like it's done for grass */
00641   if ((treeCounter & 7) == 7 && GetTreeGround(tile) == TREE_GROUND_GRASS) {
00642     uint density = GetTreeDensity(tile);
00643     if (density < 3) {
00644       SetTreeGroundDensity(tile, TREE_GROUND_GRASS, density + 1);
00645       MarkTileDirtyByTile(tile);
00646     }
00647   }
00648   if (GetTreeCounter(tile) < 15) {
00649     AddTreeCounter(tile, 1);
00650     return;
00651   }
00652   SetTreeCounter(tile, 0);
00653 
00654   switch (GetTreeGrowth(tile)) {
00655     case 3: /* regular sized tree */
00656       if (_settings_game.game_creation.landscape == LT_TROPIC &&
00657           GetTreeType(tile) != TREE_CACTUS &&
00658           GetTropicZone(tile) == TROPICZONE_DESERT) {
00659         AddTreeGrowth(tile, 1);
00660       } else {
00661         switch (GB(Random(), 0, 3)) {
00662           case 0: /* start destructing */
00663             AddTreeGrowth(tile, 1);
00664             break;
00665 
00666           case 1: /* add a tree */
00667             if (GetTreeCount(tile) < 3) {
00668               AddTreeCount(tile, 1);
00669               SetTreeGrowth(tile, 0);
00670               break;
00671             }
00672             /* FALL THROUGH */
00673 
00674           case 2: { /* add a neighbouring tree */
00675             TreeType treetype = GetTreeType(tile);
00676 
00677             tile += TileOffsByDir((Direction)(Random() & 7));
00678 
00679             /* Cacti don't spread */
00680             if (!CanPlantTreesOnTile(tile, false)) return;
00681 
00682             /* Don't plant trees, if ground was freshly cleared */
00683             if (IsTileType(tile, MP_CLEAR) && GetClearGround(tile) == CLEAR_GRASS && GetClearDensity(tile) != 3) return;
00684 
00685             PlantTreesOnTile(tile, treetype, 0, 0);
00686 
00687             break;
00688           }
00689 
00690           default:
00691             return;
00692         }
00693       }
00694       break;
00695 
00696     case 6: /* final stage of tree destruction */
00697       if (GetTreeCount(tile) > 0) {
00698         /* more than one tree, delete it */
00699         AddTreeCount(tile, -1);
00700         SetTreeGrowth(tile, 3);
00701       } else {
00702         /* just one tree, change type into MP_CLEAR */
00703         switch (GetTreeGround(tile)) {
00704           case TREE_GROUND_SHORE: MakeShore(tile); break;
00705           case TREE_GROUND_GRASS: MakeClear(tile, CLEAR_GRASS, GetTreeDensity(tile)); break;
00706           case TREE_GROUND_ROUGH: MakeClear(tile, CLEAR_ROUGH, 3); break;
00707           default: // snow or desert
00708             MakeClear(tile, _settings_game.game_creation.landscape == LT_TROPIC ? CLEAR_DESERT : CLEAR_SNOW, GetTreeDensity(tile));
00709             break;
00710         }
00711       }
00712       break;
00713 
00714     default:
00715       AddTreeGrowth(tile, 1);
00716       break;
00717   }
00718 
00719   MarkTileDirtyByTile(tile);
00720 }
00721 
00722 void OnTick_Trees()
00723 {
00724   uint32 r;
00725   TileIndex tile;
00726   TreeType tree;
00727 
00728   /* place a tree at a random rainforest spot */
00729   if (_settings_game.game_creation.landscape == LT_TROPIC &&
00730       (r = Random(), tile = RandomTileSeed(r), GetTropicZone(tile) == TROPICZONE_RAINFOREST) &&
00731       CanPlantTreesOnTile(tile, false) &&
00732       (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
00733     PlantTreesOnTile(tile, tree, 0, 0);
00734   }
00735 
00736   /* byte underflow */
00737   if (--_trees_tick_ctr != 0) return;
00738 
00739   /* place a tree at a random spot */
00740   r = Random();
00741   tile = TILE_MASK(r);
00742   if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
00743     PlantTreesOnTile(tile, tree, 0, 0);
00744   }
00745 }
00746 
00747 static void ClickTile_Trees(TileIndex tile)
00748 {
00749   /* not used */
00750 }
00751 
00752 static TrackStatus GetTileTrackStatus_Trees(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00753 {
00754   return 0;
00755 }
00756 
00757 static void ChangeTileOwner_Trees(TileIndex tile, Owner old_owner, Owner new_owner)
00758 {
00759   /* not used */
00760 }
00761 
00762 void InitializeTrees()
00763 {
00764   _trees_tick_ctr = 0;
00765 }
00766 
00767 static CommandCost TerraformTile_Trees(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
00768 {
00769   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00770 }
00771 
00772 
00773 extern const TileTypeProcs _tile_type_trees_procs = {
00774   DrawTile_Trees,           /* draw_tile_proc */
00775   GetSlopeZ_Trees,          /* get_slope_z_proc */
00776   ClearTile_Trees,          /* clear_tile_proc */
00777   GetAcceptedCargo_Trees,   /* get_accepted_cargo_proc */
00778   GetTileDesc_Trees,        /* get_tile_desc_proc */
00779   GetTileTrackStatus_Trees, /* get_tile_track_status_proc */
00780   ClickTile_Trees,          /* click_tile_proc */
00781   AnimateTile_Trees,        /* animate_tile_proc */
00782   TileLoop_Trees,           /* tile_loop_clear */
00783   ChangeTileOwner_Trees,    /* change_tile_owner_clear */
00784   NULL,                     /* get_produced_cargo_proc */
00785   NULL,                     /* vehicle_enter_tile_proc */
00786   GetFoundation_Trees,      /* get_foundation_proc */
00787   TerraformTile_Trees,      /* terraform_tile_proc */
00788 };

Generated on Wed Nov 19 19:01:42 2008 for openttd by  doxygen 1.5.6