Loading

Revision differences

Old revision #prugkdgjqNew revision #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 */  
1static CommandCost DoBuildLock(TileIndex tile, DiagDirection dir, DoCommandFlag flags)  8static CommandCost DoBuildLock(TileIndex tile, DiagDirection dir, DoCommandFlag flags)  
2{  9{  
3    CommandCost cost(EXPENSES_CONSTRUCTION);  10    CommandCost cost(EXPENSES_CONSTRUCTION);  
  
10  17  
11    /* middle tile */  18    /* middle tile */  
12    WaterClass wc_middle = IsWaterTile(tile) ? GetWaterClass(tile) : WATER_CLASS_CANAL;  19    WaterClass wc_middle = IsWaterTile(tile) ? GetWaterClass(tile) : WATER_CLASS_CANAL;  
13     13
14    if (!IsWaterTile(tile)) {  21    if (!IsWaterTile(tile)) {  
15        ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);  22        ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);  
16        if (ret.Failed()) return ret;  23        if (ret.Failed()) return ret;  
17        cost.AddCost(ret);  24        cost.AddCost(ret);  
  25        cost.AddCost(_price[PR_BUILD_CANAL]);  
18    }  26    }  
19  27  
20    /* lower tile */  28    /* lower tile */  
  
53        if (c != NULL) {  61        if (c != NULL) {  
54            /* Counts for the water. */  62            /* Counts for the water. */  
55            if (!IsWaterTile(tile - delta)) c->infrastructure.water++;  63            if (!IsWaterTile(tile - delta)) c->infrastructure.water++;  
  64            if (!IsWaterTile(tile)) c->infrastructure.water++;  
56            if (!IsWaterTile(tile + delta)) c->infrastructure.water++;  65            if (!IsWaterTile(tile + delta)) c->infrastructure.water++;  
57            /* Count for the lock itself. */  66            /* Count for the lock itself. */  
58            c->infrastructure.water += 3 * LOCK_DEPOT_TILE_FACTOR; // Lock is three tiles.  67            c->infrastructure.water += 3 * LOCK_DEPOT_TILE_FACTOR; // Lock is three tiles.  
  
69    cost.AddCost(_price[PR_BUILD_LOCK]);  78    cost.AddCost(_price[PR_BUILD_LOCK]);  
70  79  
71    return cost;  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 */  
  89static 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]);  
72} 126}