# depot_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "command_func.h"
    00014 #include "depot_base.h"
    00015 #include "company_func.h"
    00016 #include "string_func.h"
    00017 #include "town.h"
    00018 #include "vehicle_gui.h"
    00019 #include "vehiclelist.h"
    00020 #include "window_func.h"
    00021
    00022 #include "table/strings.h"
    00023
    00029 static bool IsUniqueDepotName(const char *name)
    00030 {
    00031   const Depot *d;
    00032
    00033   FOR_ALL_DEPOTS(d) {
    00034     if (d->name != NULL && strcmp(d->name, name) == 0) return false;
    00035   }
    00036
    00037   return true;
    00038 }
    00039
    00049 CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00050 {
    00051   Depot *d = Depot::GetIfValid(p1);
    00052   if (d == NULL) return CMD_ERROR;
    00053
    00054   CommandCost ret = CheckTileOwnership(d->xy);
    00055   if (ret.Failed()) return ret;
    00056
    00057   bool reset = StrEmpty(text);
    00058
    00059   if (!reset) {
    00060     if (Utf8StringLength(text) >= MAX_LENGTH_DEPOT_NAME_CHARS) return CMD_ERROR;
    00061     if (!IsUniqueDepotName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
    00062   }
    00063
    00064   if (flags & DC_EXEC) {
    00065     free(d->name);
    00066
    00067     if (reset) {
    00068       d->name = NULL;
    00069       MakeDefaultName(d);
    00070     } else {
    00071       d->name = strdup(text);
    00072     }
    00073
    00074     /* Update the orders and depot */
    00075     SetWindowClassesDirty(WC_VEHICLE_ORDERS);
    00076     SetWindowDirty(WC_VEHICLE_DEPOT, d->xy);
    00077
    00078     /* Update the depot list */
    00079     VehicleType vt;
    00080     switch (GetTileType(d->xy)) {
    00081       default: NOT_REACHED();
    00082       case MP_RAILWAY: vt = VEH_TRAIN; break;
    00083       case MP_ROAD:    vt = VEH_ROAD;  break;
    00084       case MP_WATER:   vt = VEH_SHIP;  break;
    00085     }
    00086     SetWindowDirty(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_DEPOT_LIST, vt, GetTileOwner(d->xy), d->index).Pack());
    00087   }
    00088   return CommandCost();
    00089 }