static void ChangeTileOwner_Water(TileIndex tile, Owner old_owner, Owner new_owner)
{
if (!IsTileOwner(tile, old_owner)) return;
bool is_lock_middle = IsLock(tile) && GetLockPart(tile) == LOCK_PART_MIDDLE;
/* No need to dirty company windows here, we'll redraw the whole screen anyway. */
if (is_lock_middle) Company::Get(old_owner)->infrastructure.water -= 3 * LOCK_DEPOT_TILE_FACTOR; // Lock has three parts.
if (GetWaterClass(tile) == WATER_CLASS_CANAL && is_lock_middle) {
Company::Get(old_owner)->infrastructure.water--;
}
if (new_owner != INVALID_OWNER) {
if (is_lock_middle) Company::Get(new_owner)->infrastructure.water += 3 * LOCK_DEPOT_TILE_FACTOR; // Lock has three parts.
if (GetWaterClass(tile) == WATER_CLASS_CANAL && is_lock_middle) {
Company::Get(new_owner)->infrastructure.water++;
}
/* Only subtract from the old owner here if the new owner is valid,
* otherwise we clear ship depots and canal water below. */
if (GetWaterClass(tile) == WATER_CLASS_CANAL && !is_lock_middle) {
Company::Get(old_owner)->infrastructure.water--;
Company::Get(new_owner)->infrastructure.water++;
}
if (IsShipDepot(tile)) {
Company::Get(old_owner)->infrastructure.water -= LOCK_DEPOT_TILE_FACTOR;
Company::Get(new_owner)->infrastructure.water += LOCK_DEPOT_TILE_FACTOR;
}
SetTileOwner(tile, new_owner);
return;
}
/* Remove depot */
if (IsShipDepot(tile)) DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
/* Set owner of canals and locks ... and also canal under dock there was before.
* Check if the new owner after removing depot isn't OWNER_WATER. */
if (IsTileOwner(tile, old_owner)) {
if (GetWaterClass(tile) == WATER_CLASS_CANAL && !is_lock_middle) Company::Get(old_owner)->infrastructure.water--;
if (GetWaterClass(tile) == WATER_CLASS_CANAL && is_lock_middle) {
Company::Get(old_owner)->infrastructure.water--;
}
SetTileOwner(tile, OWNER_NONE);
}
}