Loading

Paste #puljoogty

  1. /**
  2.  * Builds a lock.
  3.  * @param tile Central tile of the lock.
  4.  * @param dir Uphill direction.
  5.  * @param flags Operation to perform.
  6.  * @return The cost in case of success, or an error code if it failed.
  7.  */
  8. static CommandCost DoBuildLock(TileIndex tile, DiagDirection dir, DoCommandFlag flags)
  9. {
  10.     CommandCost cost(EXPENSES_CONSTRUCTION);
  11.  
  12.     int delta = TileOffsByDiagDir(dir);
  13.     CommandCost ret = EnsureNoVehicleOnGround(tile);
  14.     if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile + delta);
  15.     if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile - delta);
  16.     if (ret.Failed()) return ret;
  17.  
  18.     /* middle tile */
  19.     WaterClass wc_middle = IsWaterTile(tile) ? GetWaterClass(tile) : WATER_CLASS_CANAL;
  20.  
  21.     if (!IsWaterTile(tile)) {
  22.         ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  23.         if (ret.Failed()) return ret;
  24.         cost.AddCost(ret);
  25.         cost.AddCost(_price[PR_BUILD_CANAL]);
  26.     }
  27.  
  28.     /* lower tile */
  29.     WaterClass wc_lower = IsWaterTile(tile - delta) ? GetWaterClass(tile - delta) : WATER_CLASS_CANAL;
  30.  
  31.     if (!IsWaterTile(tile - delta)) {
  32.         ret = DoCommand(tile - delta, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  33.         if (ret.Failed()) return ret;
  34.         cost.AddCost(ret);
  35.         cost.AddCost(_price[PR_BUILD_CANAL]);
  36.     }
  37.     if (!IsTileFlat(tile - delta)) {
  38.         return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
  39.     }
  40.  
  41.     /* upper tile */
  42.     WaterClass wc_upper = IsWaterTile(tile + delta) ? GetWaterClass(tile + delta) : WATER_CLASS_CANAL;
  43.  
  44.     if (!IsWaterTile(tile + delta)) {
  45.         ret = DoCommand(tile + delta, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  46.         if (ret.Failed()) return ret;
  47.         cost.AddCost(ret);
  48.         cost.AddCost(_price[PR_BUILD_CANAL]);
  49.     }
  50.     if (!IsTileFlat(tile + delta)) {
  51.         return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
  52.     }
  53.  
  54.     if (IsBridgeAbove(tile) || IsBridgeAbove(tile - delta) || IsBridgeAbove(tile + delta)) {
  55.         return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
  56.     }
  57.  
  58.     if (flags & DC_EXEC) {
  59.         /* Update company infrastructure counts. */
  60.         Company *c = Company::GetIfValid(_current_company);
  61.         if (c != NULL) {
  62.             /* Counts for the water. */
  63.             if (!IsWaterTile(tile - delta)) c->infrastructure.water++;
  64.             if (!IsWaterTile(tile)) c->infrastructure.water++;
  65.             if (!IsWaterTile(tile + delta)) c->infrastructure.water++;
  66.             /* Count for the lock itself. */
  67.             c->infrastructure.water += 3 * LOCK_DEPOT_TILE_FACTOR; // Lock is three tiles.
  68.             DirtyCompanyInfrastructureWindows(_current_company);
  69.         }
  70.  
  71.         MakeLock(tile, _current_company, dir, wc_lower, wc_upper, wc_middle);
  72.         MarkTileDirtyByTile(tile);
  73.         MarkTileDirtyByTile(tile - delta);
  74.         MarkTileDirtyByTile(tile + delta);
  75.         MarkCanalsAndRiversAroundDirty(tile - delta);
  76.         MarkCanalsAndRiversAroundDirty(tile + delta);
  77.     }
  78.     cost.AddCost(_price[PR_BUILD_LOCK]);
  79.  
  80.     return cost;
  81. }
  82.  
  83. /**
  84.  * Remove a lock.
  85.  * @param tile Central tile of the lock.
  86.  * @param flags Operation to perform.
  87.  * @return The cost in case of success, or an error code if it failed.
  88.  */
  89. static CommandCost RemoveLock(TileIndex tile, DoCommandFlag flags)
  90. {
  91.     if (GetTileOwner(tile) != OWNER_NONE) {
  92.         CommandCost ret = CheckTileOwnership(tile);
  93.         if (ret.Failed()) return ret;
  94.     }
  95.  
  96.     TileIndexDiff delta = TileOffsByDiagDir(GetLockDirection(tile));
  97.  
  98.     /* make sure no vehicle is on the tile. */
  99.     CommandCost ret = EnsureNoVehicleOnGround(tile);
  100.     if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile + delta);
  101.     if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile - delta);
  102.     if (ret.Failed()) return ret;
  103.  
  104.     if (flags & DC_EXEC) {
  105.         /* Remove middle part from company infrastructure count. */
  106.         Company *c = Company::GetIfValid(GetTileOwner(tile));
  107.         if (c != NULL) {
  108.             c->infrastructure.water -= 3 * LOCK_DEPOT_TILE_FACTOR; // three parts of the lock.
  109.             DirtyCompanyInfrastructureWindows(c->index);
  110.         }
  111.  
  112.         if (GetWaterClass(tile) == WATER_CLASS_RIVER) {
  113.             MakeRiver(tile, Random());
  114.         } else {
  115.             c->infrastructure.water--;
  116.             DoClearSquare(tile);
  117.         }
  118.         MakeWaterKeepingClass(tile + delta, GetTileOwner(tile + delta));
  119.         MakeWaterKeepingClass(tile - delta, GetTileOwner(tile - delta));
  120.         MarkCanalsAndRiversAroundDirty(tile);
  121.         MarkCanalsAndRiversAroundDirty(tile - delta);
  122.         MarkCanalsAndRiversAroundDirty(tile + delta);
  123.     }
  124.  
  125.     return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_LOCK]);
  126. }

Version history

Revision # Author Created at
psiivpgdr Anonymous 16 Feb 2015, 16:14:15 UTC Diff
piy9msgw1 Anonymous 16 Feb 2015, 15:23:05 UTC Diff
prugkdgjq Anonymous 16 Feb 2015, 13:57:28 UTC Diff
p0fosxr56 Anonymous 16 Feb 2015, 02:20:47 UTC Diff

Comments