Files @ r26712:3eea27cceb09
Branch filter:

Location: cpp/openttd-patchpack/source/src/depot_cmd.cpp

translators
Update: Translations from eints
swedish: 9 changes by joeax910
norwegian (bokmal): 7 changes by buzzCraft
chinese (traditional): 48 changes by wpi3
galician: 98 changes by pvillaverde
vietnamese: 13 changes by myquartz
czech: 42 changes by vladoschreiner, 40 changes by PatrikSamuelTauchim, 19 changes by adamek0202, 3 changes by LubosKolouch
chinese (simplified): 52 changes by HansKaffee
luxembourgish: 148 changes by phreeze83
hungarian: 50 changes by PstasDev, 23 changes by baliball
german: 69 changes by Wuzzy2, 4 changes by Luensche
romanian: 3 changes by kneekoo
ukrainian: 45 changes by StepanIvasyn
catalan: 12 changes by J0anJosep
turkish: 9 changes by Anceph
french: 1 change by Athozus
portuguese (brazilian): 9 changes by ericandradex
/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file depot_cmd.cpp %Command Handling for depots. */

#include "stdafx.h"
#include "command_func.h"
#include "depot_base.h"
#include "company_func.h"
#include "string_func.h"
#include "town.h"
#include "vehicle_gui.h"
#include "vehiclelist.h"
#include "window_func.h"
#include "depot_cmd.h"

#include "table/strings.h"

#include "safeguards.h"

/**
 * Check whether the given name is globally unique amongst depots.
 * @param name The name to check.
 * @return True if there is no depot with the given name.
 */
static bool IsUniqueDepotName(const std::string &name)
{
	for (const Depot *d : Depot::Iterate()) {
		if (!d->name.empty() && d->name == name) return false;
	}

	return true;
}

/**
 * Rename a depot.
 * @param flags type of operation
 * @param depot_id id of depot
 * @param text the new name or an empty string when resetting to the default
 * @return the cost of this operation or an error
 */
CommandCost CmdRenameDepot(DoCommandFlag flags, DepotID depot_id, const std::string &text)
{
	Depot *d = Depot::GetIfValid(depot_id);
	if (d == nullptr) return CMD_ERROR;

	CommandCost ret = CheckTileOwnership(d->xy);
	if (ret.Failed()) return ret;

	bool reset = text.empty();

	if (!reset) {
		if (Utf8StringLength(text) >= MAX_LENGTH_DEPOT_NAME_CHARS) return CMD_ERROR;
		if (!IsUniqueDepotName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
	}

	if (flags & DC_EXEC) {
		if (reset) {
			d->name.clear();
			MakeDefaultName(d);
		} else {
			d->name = text;
		}

		/* Update the orders and depot */
		SetWindowClassesDirty(WC_VEHICLE_ORDERS);
		SetWindowDirty(WC_VEHICLE_DEPOT, d->xy);

		/* Update the depot list */
		VehicleType vt = GetDepotVehicleType(d->xy);
		SetWindowDirty(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_DEPOT_LIST, vt, GetTileOwner(d->xy), d->index).Pack());
	}
	return CommandCost();
}