From cd0e022172005d5a6a463848cdc711df1dda648f Mon Sep 17 00:00:00 2001 From: peter1138 Date: Wed, 27 Jun 2007 20:53:25 +0000 Subject: (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type. --- src/misc_cmd.cpp | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'src/misc_cmd.cpp') diff --git a/src/misc_cmd.cpp b/src/misc_cmd.cpp index 5ad8ffc24..3a0448f84 100644 --- a/src/misc_cmd.cpp +++ b/src/misc_cmd.cpp @@ -17,6 +17,7 @@ #include "variables.h" #include "livery.h" #include "player_face.h" +#include "strings.h" /** Change the player's face. * @param tile unused @@ -194,6 +195,20 @@ CommandCost CmdDecreaseLoan(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) return CommandCost(); } +static bool IsUniqueCompanyName(const char *name) +{ + const Player *p; + char buf[512]; + + FOR_ALL_PLAYERS(p) { + SetDParam(0, p->index); + GetString(buf, STR_COMPANY_NAME, lastof(buf)); + if (strcmp(buf, name) == 0) return false; + } + + return true; +} + /** Change the name of the company. * @param tile unused * @param flags operation to perform @@ -205,9 +220,11 @@ CommandCost CmdChangeCompanyName(TileIndex tile, uint32 flags, uint32 p1, uint32 StringID str; Player *p; - if (_cmd_text[0] == '\0') return CMD_ERROR; + if (StrEmpty(_cmd_text)) return CMD_ERROR; + + if (!IsUniqueCompanyName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); - str = AllocateNameUnique(_cmd_text, 4); + str = AllocateName(_cmd_text, 4); if (str == 0) return CMD_ERROR; if (flags & DC_EXEC) { @@ -222,6 +239,20 @@ CommandCost CmdChangeCompanyName(TileIndex tile, uint32 flags, uint32 p1, uint32 return CommandCost(); } +static bool IsUniquePresidentName(const char *name) +{ + const Player *p; + char buf[512]; + + FOR_ALL_PLAYERS(p) { + SetDParam(0, p->index); + GetString(buf, STR_PLAYER_NAME, lastof(buf)); + if (strcmp(buf, name) == 0) return false; + } + + return true; +} + /** Change the name of the president. * @param tile unused * @param flags operation to perform @@ -233,9 +264,11 @@ CommandCost CmdChangePresidentName(TileIndex tile, uint32 flags, uint32 p1, uint StringID str; Player *p; - if (_cmd_text[0] == '\0') return CMD_ERROR; + if (StrEmpty(_cmd_text)) return CMD_ERROR; + + if (!IsUniquePresidentName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); - str = AllocateNameUnique(_cmd_text, 4); + str = AllocateName(_cmd_text, 4); if (str == 0) return CMD_ERROR; if (flags & DC_EXEC) { -- cgit v1.2.3-54-g00ecf