Loading

Paste #p0fosxr56

  1. static CommandCost DoBuildLock(TileIndex tile, DiagDirection dir, DoCommandFlag flags)
  2. {
  3.     CommandCost cost(EXPENSES_CONSTRUCTION);
  4.  
  5.     int delta = TileOffsByDiagDir(dir);
  6.     CommandCost ret = EnsureNoVehicleOnGround(tile);
  7.     if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile + delta);
  8.     if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile - delta);
  9.     if (ret.Failed()) return ret;
  10.  
  11.     /* middle tile */
  12.     WaterClass wc_middle = IsWaterTile(tile) ? GetWaterClass(tile) : WATER_CLASS_CANAL;
  13.     ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  14.     if (ret.Failed()) return ret;
  15.     cost.AddCost(ret);
  16.  
  17.     /* lower tile */
  18.     WaterClass wc_lower = IsWaterTile(tile - delta) ? GetWaterClass(tile - delta) : WATER_CLASS_CANAL;
  19.  
  20.     if (!IsWaterTile(tile - delta)) {
  21.         ret = DoCommand(tile - delta, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  22.         if (ret.Failed()) return ret;
  23.         cost.AddCost(ret);
  24.         cost.AddCost(_price[PR_BUILD_CANAL]);
  25.     }
  26.     if (!IsTileFlat(tile - delta)) {
  27.         return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
  28.     }
  29.  
  30.     /* upper tile */
  31.     WaterClass wc_upper = IsWaterTile(tile + delta) ? GetWaterClass(tile + delta) : WATER_CLASS_CANAL;
  32.  
  33.     if (!IsWaterTile(tile + delta)) {
  34.         ret = DoCommand(tile + delta, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  35.         if (ret.Failed()) return ret;
  36.         cost.AddCost(ret);
  37.         cost.AddCost(_price[PR_BUILD_CANAL]);
  38.     }
  39.     if (!IsTileFlat(tile + delta)) {
  40.         return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
  41.     }
  42.  
  43.     if (IsBridgeAbove(tile) || IsBridgeAbove(tile - delta) || IsBridgeAbove(tile + delta)) {
  44.         return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
  45.     }
  46.  
  47.     if (flags & DC_EXEC) {
  48.         /* Update company infrastructure counts. */
  49.         Company *c = Company::GetIfValid(_current_company);
  50.         if (c != NULL) {
  51.             /* Counts for the water. */
  52.             if (!IsWaterTile(tile - delta)) c->infrastructure.water++;
  53.             if (!IsWaterTile(tile + delta)) c->infrastructure.water++;
  54.             /* Count for the lock itself. */
  55.             c->infrastructure.water += 3 * LOCK_DEPOT_TILE_FACTOR; // Lock is three tiles.
  56.             DirtyCompanyInfrastructureWindows(_current_company);
  57.         }
  58.  
  59.         MakeLock(tile, _current_company, dir, wc_lower, wc_upper, wc_middle);
  60.         MarkTileDirtyByTile(tile);
  61.         MarkTileDirtyByTile(tile - delta);
  62.         MarkTileDirtyByTile(tile + delta);
  63.         MarkCanalsAndRiversAroundDirty(tile - delta);
  64.         MarkCanalsAndRiversAroundDirty(tile + delta);
  65.     }
  66.     cost.AddCost(_price[PR_BUILD_LOCK]);
  67.  
  68.     return cost;
  69. }

Comments