# clear_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: clear_cmd.cpp 16744 2009-07-04 21:06:17Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "clear_map.h"
    00008 #include "command_func.h"
    00009 #include "landscape.h"
    00010 #include "variables.h"
    00011 #include "genworld.h"
    00012 #include "industry.h"
    00013 #include "functions.h"
    00014 #include "economy_func.h"
    00015 #include "viewport_func.h"
    00016 #include "water.h"
    00017 #include "settings_type.h"
    00018
    00019 #include "table/strings.h"
    00020 #include "table/sprites.h"
    00021 #include "table/clear_land.h"
    00022
    00023 static CommandCost ClearTile_Clear(TileIndex tile, DoCommandFlag flags)
    00024 {
    00025   static const Money *clear_price_table[] = {
    00026     &_price.clear_grass,
    00027     &_price.clear_roughland,
    00028     &_price.clear_rocks,
    00029     &_price.clear_fields,
    00030     &_price.clear_roughland,
    00031     &_price.clear_roughland,
    00032   };
    00033   CommandCost price(EXPENSES_CONSTRUCTION);
    00034
    00035   if (!IsClearGround(tile, CLEAR_GRASS) || GetClearDensity(tile) != 0) {
    00036     price.AddCost(*clear_price_table[GetClearGround(tile)]);
    00037   }
    00038
    00039   if (flags & DC_EXEC) DoClearSquare(tile);
    00040
    00041   return price;
    00042 }
    00043
    00044 void DrawClearLandTile(const TileInfo *ti, byte set)
    00045 {
    00046   DrawGroundSprite(SPR_FLAT_BARE_LAND + _tileh_to_sprite[ti->tileh] + set * 19, PAL_NONE);
    00047 }
    00048
    00049 void DrawHillyLandTile(const TileInfo *ti)
    00050 {
    00051   if (ti->tileh != SLOPE_FLAT) {
    00052     DrawGroundSprite(SPR_FLAT_ROUGH_LAND + _tileh_to_sprite[ti->tileh], PAL_NONE);
    00053   } else {
    00054     DrawGroundSprite(_landscape_clear_sprites[GB(ti->x ^ ti->y, 4, 3)], PAL_NONE);
    00055   }
    00056 }
    00057
    00058 void DrawClearLandFence(const TileInfo *ti)
    00059 {
    00060   byte z = ti->z;
    00061
    00062   if (ti->tileh & SLOPE_S) {
    00063     z += TILE_HEIGHT;
    00064     if (ti->tileh == SLOPE_STEEP_S) z += TILE_HEIGHT;
    00065   }
    00066
    00067   if (GetFenceSW(ti->tile) != 0) {
    00068     DrawGroundSpriteAt(_clear_land_fence_sprites_1[GetFenceSW(ti->tile) - 1] + _fence_mod_by_tileh[ti->tileh], PAL_NONE, ti->x, ti->y, z);
    00069   }
    00070
    00071   if (GetFenceSE(ti->tile) != 0) {
    00072     DrawGroundSpriteAt(_clear_land_fence_sprites_1[GetFenceSE(ti->tile) - 1] + _fence_mod_by_tileh_2[ti->tileh], PAL_NONE, ti->x, ti->y, z);
    00073   }
    00074 }
    00075
    00076 static void DrawTile_Clear(TileInfo *ti)
    00077 {
    00078   switch (GetClearGround(ti->tile)) {
    00079     case CLEAR_GRASS:
    00080       DrawClearLandTile(ti, GetClearDensity(ti->tile));
    00081       break;
    00082
    00083     case CLEAR_ROUGH:
    00084       DrawHillyLandTile(ti);
    00085       break;
    00086
    00087     case CLEAR_ROCKS:
    00088       DrawGroundSprite(SPR_FLAT_ROCKY_LAND_1 + _tileh_to_sprite[ti->tileh], PAL_NONE);
    00089       break;
    00090
    00091     case CLEAR_FIELDS:
    00092       DrawGroundSprite(_clear_land_sprites_1[GetFieldType(ti->tile)] + _tileh_to_sprite[ti->tileh], PAL_NONE);
    00093       break;
    00094
    00095     case CLEAR_SNOW:
    00096       DrawGroundSprite(_clear_land_sprites_2[GetClearDensity(ti->tile)] + _tileh_to_sprite[ti->tileh], PAL_NONE);
    00097       break;
    00098
    00099     case CLEAR_DESERT:
    00100       DrawGroundSprite(_clear_land_sprites_3[GetClearDensity(ti->tile)] + _tileh_to_sprite[ti->tileh], PAL_NONE);
    00101       break;
    00102   }
    00103
    00104   DrawClearLandFence(ti);
    00105   DrawBridgeMiddle(ti);
    00106 }
    00107
    00108 static uint GetSlopeZ_Clear(TileIndex tile, uint x, uint y)
    00109 {
    00110   uint z;
    00111   Slope tileh = GetTileSlope(tile, &z);
    00112
    00113   return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
    00114 }
    00115
    00116 static Foundation GetFoundation_Clear(TileIndex tile, Slope tileh)
    00117 {
    00118   return FOUNDATION_NONE;
    00119 }
    00120
    00121 static void GetAcceptedCargo_Clear(TileIndex tile, AcceptedCargo ac)
    00122 {
    00123   /* unused */
    00124 }
    00125
    00126 void TileLoopClearHelper(TileIndex tile)
    00127 {
    00128   byte self;
    00129   byte neighbour;
    00130   TileIndex dirty = INVALID_TILE;
    00131
    00132   self = (IsTileType(tile, MP_CLEAR) && IsClearGround(tile, CLEAR_FIELDS));
    00133
    00134   neighbour = (IsTileType(TILE_ADDXY(tile, 1, 0), MP_CLEAR) && IsClearGround(TILE_ADDXY(tile, 1, 0), CLEAR_FIELDS));
    00135   if (GetFenceSW(tile) == 0) {
    00136     if (self != neighbour) {
    00137       SetFenceSW(tile, 3);
    00138       dirty = tile;
    00139     }
    00140   } else {
    00141     if (self == 0 && neighbour == 0) {
    00142       SetFenceSW(tile, 0);
    00143       dirty = tile;
    00144     }
    00145   }
    00146
    00147   neighbour = (IsTileType(TILE_ADDXY(tile, 0, 1), MP_CLEAR) && IsClearGround(TILE_ADDXY(tile, 0, 1), CLEAR_FIELDS));
    00148   if (GetFenceSE(tile) == 0) {
    00149     if (self != neighbour) {
    00150       SetFenceSE(tile, 3);
    00151       dirty = tile;
    00152     }
    00153   } else {
    00154     if (self == 0 && neighbour == 0) {
    00155       SetFenceSE(tile, 0);
    00156       dirty = tile;
    00157     }
    00158   }
    00159
    00160   if (dirty != INVALID_TILE) MarkTileDirtyByTile(dirty);
    00161 }
    00162
    00163
    00164 /* convert into snowy tiles */
    00165 static void TileLoopClearAlps(TileIndex tile)
    00166 {
    00167   int k = GetTileZ(tile) - GetSnowLine() + TILE_HEIGHT;
    00168
    00169   if (k < 0) { // well below the snow line
    00170     if (!IsClearGround(tile, CLEAR_SNOW)) return;
    00171     if (GetClearDensity(tile) == 0) SetClearGroundDensity(tile, CLEAR_GRASS, 3);
    00172   } else {
    00173     if (!IsClearGround(tile, CLEAR_SNOW)) {
    00174       SetClearGroundDensity(tile, CLEAR_SNOW, 0);
    00175     } else {
    00176       uint density = min((uint)k / TILE_HEIGHT, 3);
    00177
    00178       if (GetClearDensity(tile) < density) {
    00179         AddClearDensity(tile, 1);
    00180       } else if (GetClearDensity(tile) > density) {
    00181         AddClearDensity(tile, -1);
    00182       } else {
    00183         return;
    00184       }
    00185     }
    00186   }
    00187
    00188   MarkTileDirtyByTile(tile);
    00189 }
    00190
    00191 static void TileLoopClearDesert(TileIndex tile)
    00192 {
    00193   if (IsClearGround(tile, CLEAR_DESERT)) return;
    00194
    00195   if (GetTropicZone(tile) == TROPICZONE_DESERT) {
    00196     SetClearGroundDensity(tile, CLEAR_DESERT, 3);
    00197   } else {
    00198     if (GetTropicZone(tile + TileDiffXY( 1,  0)) != TROPICZONE_DESERT &&
    00199         GetTropicZone(tile + TileDiffXY(-1,  0)) != TROPICZONE_DESERT &&
    00200         GetTropicZone(tile + TileDiffXY( 0,  1)) != TROPICZONE_DESERT &&
    00201         GetTropicZone(tile + TileDiffXY( 0, -1)) != TROPICZONE_DESERT)
    00202       return;
    00203     SetClearGroundDensity(tile, CLEAR_DESERT, 1);
    00204   }
    00205
    00206   MarkTileDirtyByTile(tile);
    00207 }
    00208
    00209 static void TileLoop_Clear(TileIndex tile)
    00210 {
    00211   /* If the tile is at any edge flood it to prevent maps without water. */
    00212   if (_settings_game.construction.freeform_edges && DistanceFromEdge(tile) == 1) {
    00213     uint z;
    00214     Slope slope = GetTileSlope(tile, &z);
    00215     if (z == 0 && slope == SLOPE_FLAT) {
    00216       DoFloodTile(tile);
    00217       MarkTileDirtyByTile(tile);
    00218       return;
    00219     }
    00220   }
    00221   TileLoopClearHelper(tile);
    00222
    00223   switch (_settings_game.game_creation.landscape) {
    00224     case LT_TROPIC: TileLoopClearDesert(tile); break;
    00225     case LT_ARCTIC: TileLoopClearAlps(tile);   break;
    00226   }
    00227
    00228   switch (GetClearGround(tile)) {
    00229     case CLEAR_GRASS:
    00230       if (GetClearDensity(tile) == 3) return;
    00231
    00232       if (_game_mode != GM_EDITOR) {
    00233         if (GetClearCounter(tile) < 7) {
    00234           AddClearCounter(tile, 1);
    00235           return;
    00236         } else {
    00237           SetClearCounter(tile, 0);
    00238           AddClearDensity(tile, 1);
    00239         }
    00240       } else {
    00241         SetClearGroundDensity(tile, GB(Random(), 0, 8) > 21 ? CLEAR_GRASS : CLEAR_ROUGH, 3);
    00242       }
    00243       break;
    00244
    00245     case CLEAR_FIELDS: {
    00246       uint field_type;
    00247
    00248       if (_game_mode == GM_EDITOR) return;
    00249
    00250       if (GetClearCounter(tile) < 7) {
    00251         AddClearCounter(tile, 1);
    00252         return;
    00253       } else {
    00254         SetClearCounter(tile, 0);
    00255       }
    00256
    00257       if (GetIndustryIndexOfField(tile) == INVALID_INDUSTRY && GetFieldType(tile) >= 7) {
    00258         /* This farmfield is no longer farmfield, so make it grass again */
    00259         MakeClear(tile, CLEAR_GRASS, 2);
    00260       } else {
    00261         field_type = GetFieldType(tile);
    00262         field_type = (field_type < 8) ? field_type + 1 : 0;
    00263         SetFieldType(tile, field_type);
    00264       }
    00265       break;
    00266     }
    00267
    00268     default:
    00269       return;
    00270   }
    00271
    00272   MarkTileDirtyByTile(tile);
    00273 }
    00274
    00275 void GenerateClearTile()
    00276 {
    00277   uint i, gi;
    00278   TileIndex tile;
    00279
    00280   /* add rough tiles */
    00281   i = ScaleByMapSize(GB(Random(), 0, 10) + 0x400);
    00282   gi = ScaleByMapSize(GB(Random(), 0, 7) + 0x80);
    00283
    00284   SetGeneratingWorldProgress(GWP_ROUGH_ROCKY, gi + i);
    00285   do {
    00286     IncreaseGeneratingWorldProgress(GWP_ROUGH_ROCKY);
    00287     tile = RandomTile();
    00288     if (IsTileType(tile, MP_CLEAR) && !IsClearGround(tile, CLEAR_DESERT)) SetClearGroundDensity(tile, CLEAR_ROUGH, 3);
    00289   } while (--i);
    00290
    00291   /* add rocky tiles */
    00292   i = gi;
    00293   do {
    00294     uint32 r = Random();
    00295     tile = RandomTileSeed(r);
    00296
    00297     IncreaseGeneratingWorldProgress(GWP_ROUGH_ROCKY);
    00298     if (IsTileType(tile, MP_CLEAR) && !IsClearGround(tile, CLEAR_DESERT)) {
    00299       uint j = GB(r, 16, 4) + 5;
    00300       for (;;) {
    00301         TileIndex tile_new;
    00302
    00303         SetClearGroundDensity(tile, CLEAR_ROCKS, 3);
    00304         do {
    00305           if (--j == 0) goto get_out;
    00306           tile_new = tile + TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
    00307         } while (!IsTileType(tile_new, MP_CLEAR) || IsClearGround(tile_new, CLEAR_DESERT));
    00308         tile = tile_new;
    00309       }
    00310 get_out:;
    00311     }
    00312   } while (--i);
    00313 }
    00314
    00315 static TrackStatus GetTileTrackStatus_Clear(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    00316 {
    00317   return 0;
    00318 }
    00319
    00320 static const StringID _clear_land_str[] = {
    00321   STR_080D_GRASS,
    00322   STR_080B_ROUGH_LAND,
    00323   STR_080A_ROCKS,
    00324   STR_080E_FIELDS,
    00325   STR_080F_SNOW_COVERED_LAND,
    00326   STR_0810_DESERT
    00327 };
    00328
    00329 static void GetTileDesc_Clear(TileIndex tile, TileDesc *td)
    00330 {
    00331   if (IsClearGround(tile, CLEAR_GRASS) && GetClearDensity(tile) == 0) {
    00332     td->str = STR_080C_BARE_LAND;
    00333   } else {
    00334     td->str = _clear_land_str[GetClearGround(tile)];
    00335   }
    00336   td->owner[0] = GetTileOwner(tile);
    00337 }
    00338
    00339 static void ChangeTileOwner_Clear(TileIndex tile, Owner old_owner, Owner new_owner)
    00340 {
    00341   return;
    00342 }
    00343
    00344 void InitializeClearLand()
    00345 {
    00346   _settings_game.game_creation.snow_line = _settings_game.game_creation.snow_line_height * TILE_HEIGHT;
    00347 }
    00348
    00349 static CommandCost TerraformTile_Clear(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
    00350 {
    00351   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00352 }
    00353
    00354 extern const TileTypeProcs _tile_type_clear_procs = {
    00355   DrawTile_Clear,
    00356   GetSlopeZ_Clear,
    00357   ClearTile_Clear,
    00358   GetAcceptedCargo_Clear,
    00359   GetTileDesc_Clear,
    00360   GetTileTrackStatus_Clear,
    00361   NULL,
    00362   NULL,
    00363   TileLoop_Clear,
    00364   ChangeTileOwner_Clear,
    00365   NULL,
    00366   NULL,
    00367   GetFoundation_Clear,
    00368   TerraformTile_Clear,
    00369 };