2010-05-12 20:50:10 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/** @file depot_cmd.cpp %Command Handling for depots. */
|
2010-05-12 20:50:10 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "command_func.h"
|
|
|
|
#include "depot_base.h"
|
2011-02-07 22:08:11 +00:00
|
|
|
#include "company_func.h"
|
2010-05-12 20:50:10 +00:00
|
|
|
#include "string_func.h"
|
|
|
|
#include "town.h"
|
|
|
|
#include "vehicle_gui.h"
|
2010-09-08 21:37:13 +00:00
|
|
|
#include "vehiclelist.h"
|
2010-05-12 20:50:10 +00:00
|
|
|
#include "window_func.h"
|
|
|
|
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2011-05-02 17:42:12 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-05-29 14:07:42 +00:00
|
|
|
static bool IsUniqueDepotName(const std::string &name)
|
2010-05-12 20:50:10 +00:00
|
|
|
{
|
2019-12-15 16:54:05 +00:00
|
|
|
for (const Depot *d : Depot::Iterate()) {
|
2020-05-17 21:31:59 +00:00
|
|
|
if (!d->name.empty() && d->name == name) return false;
|
2010-05-12 20:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a depot.
|
|
|
|
* @param tile unused
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 id of depot
|
|
|
|
* @param p2 unused
|
|
|
|
* @param text the new name or an empty string when resetting to the default
|
|
|
|
* @return the cost of this operation or an error
|
|
|
|
*/
|
2021-05-29 14:09:25 +00:00
|
|
|
CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
|
2010-05-12 20:50:10 +00:00
|
|
|
{
|
|
|
|
Depot *d = Depot::GetIfValid(p1);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (d == nullptr) return CMD_ERROR;
|
2010-05-12 20:50:10 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckTileOwnership(d->xy);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
2021-05-29 14:09:25 +00:00
|
|
|
bool reset = text.empty();
|
2010-05-12 20:50:10 +00:00
|
|
|
|
|
|
|
if (!reset) {
|
2010-12-05 22:24:04 +00:00
|
|
|
if (Utf8StringLength(text) >= MAX_LENGTH_DEPOT_NAME_CHARS) return CMD_ERROR;
|
2010-05-12 20:50:10 +00:00
|
|
|
if (!IsUniqueDepotName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
if (reset) {
|
2020-05-17 21:31:59 +00:00
|
|
|
d->name.clear();
|
2010-05-12 20:50:10 +00:00
|
|
|
MakeDefaultName(d);
|
|
|
|
} else {
|
2020-05-17 21:31:59 +00:00
|
|
|
d->name = text;
|
2010-05-12 20:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the orders and depot */
|
|
|
|
SetWindowClassesDirty(WC_VEHICLE_ORDERS);
|
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, d->xy);
|
|
|
|
|
|
|
|
/* Update the depot list */
|
2013-10-13 20:11:05 +00:00
|
|
|
VehicleType vt = GetDepotVehicleType(d->xy);
|
2010-09-08 21:37:13 +00:00
|
|
|
SetWindowDirty(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_DEPOT_LIST, vt, GetTileOwner(d->xy), d->index).Pack());
|
2010-05-12 20:50:10 +00:00
|
|
|
}
|
|
|
|
return CommandCost();
|
|
|
|
}
|