depot_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: depot_cmd.cpp 22013 2011-02-07 22:08:11Z rubidium $ */
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 
00024 
00025 static bool IsUniqueDepotName(const char *name)
00026 {
00027   const Depot *d;
00028 
00029   FOR_ALL_DEPOTS(d) {
00030     if (d->name != NULL && strcmp(d->name, name) == 0) return false;
00031   }
00032 
00033   return true;
00034 }
00035 
00045 CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00046 {
00047   Depot *d = Depot::GetIfValid(p1);
00048   if (d == NULL) return CMD_ERROR;
00049 
00050   CommandCost ret = CheckTileOwnership(d->xy);
00051   if (ret.Failed()) return ret;
00052 
00053   bool reset = StrEmpty(text);
00054 
00055   if (!reset) {
00056     if (Utf8StringLength(text) >= MAX_LENGTH_DEPOT_NAME_CHARS) return CMD_ERROR;
00057     if (!IsUniqueDepotName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
00058   }
00059 
00060   if (flags & DC_EXEC) {
00061     free(d->name);
00062 
00063     if (reset) {
00064       d->name = NULL;
00065       MakeDefaultName(d);
00066     } else {
00067       d->name = strdup(text);
00068     }
00069 
00070     /* Update the orders and depot */
00071     SetWindowClassesDirty(WC_VEHICLE_ORDERS);
00072     SetWindowDirty(WC_VEHICLE_DEPOT, d->xy);
00073 
00074     /* Update the depot list */
00075     VehicleType vt;
00076     switch (GetTileType(d->xy)) {
00077       default: NOT_REACHED();
00078       case MP_RAILWAY: vt = VEH_TRAIN; break;
00079       case MP_ROAD:    vt = VEH_ROAD;  break;
00080       case MP_WATER:   vt = VEH_SHIP;  break;
00081     }
00082     SetWindowDirty(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_DEPOT_LIST, vt, GetTileOwner(d->xy), d->index).Pack());
00083   }
00084   return CommandCost();
00085 }

Generated on Sun May 15 19:20:07 2011 for OpenTTD by  doxygen 1.6.1