Merge branch 'master' into jgrpp
# Conflicts: # projects/openttd_vs140.vcxproj.filters # projects/openttd_vs141.vcxproj.filters # projects/openttd_vs142.vcxproj.filters # src/base_consist.h # src/company_base.h # src/newgrf_config.cpp # src/newgrf_config.h # src/openttd.cpp # src/saveload/saveload.cpp # src/saveload/saveload.h # src/saveload/station_sl.cpp # src/settings.cpp # src/signs_base.h # src/string.cpp # src/string_func.h # src/table/misc_settings.ini # src/table/settings.h.preamble # src/town_cmd.cpp # src/vehicle.cpp # src/vehicle_cmd.cpp # src/video/cocoa/cocoa_v.mm # src/video/null_v.cpp
This commit is contained in:
@@ -33,6 +33,8 @@
|
||||
#include "core/random_func.hpp"
|
||||
#include "tbtr_template_vehicle.h"
|
||||
#include "tbtr_template_vehicle_func.h"
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#include "table/strings.h"
|
||||
|
||||
@@ -785,7 +787,7 @@ CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32
|
||||
static bool IsUniqueVehicleName(const char *name)
|
||||
{
|
||||
for (const Vehicle *v : Vehicle::Iterate()) {
|
||||
if (v->name != nullptr && strcmp(v->name, name) == 0) return false;
|
||||
if (!v->name.empty() && v->name == name) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -798,42 +800,48 @@ static bool IsUniqueVehicleName(const char *name)
|
||||
*/
|
||||
static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
|
||||
{
|
||||
char buf[256];
|
||||
std::string buf;
|
||||
|
||||
/* Find the position of the first digit in the last group of digits. */
|
||||
size_t number_position;
|
||||
for (number_position = strlen(src->name); number_position > 0; number_position--) {
|
||||
for (number_position = src->name.length(); number_position > 0; number_position--) {
|
||||
/* The design of UTF-8 lets this work simply without having to check
|
||||
* for UTF-8 sequences. */
|
||||
if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
|
||||
}
|
||||
|
||||
/* Format buffer and determine starting number. */
|
||||
int num;
|
||||
long num;
|
||||
byte padding = 0;
|
||||
if (number_position == strlen(src->name)) {
|
||||
if (number_position == src->name.length()) {
|
||||
/* No digit at the end, so start at number 2. */
|
||||
strecpy(buf, src->name, lastof(buf));
|
||||
strecat(buf, " ", lastof(buf));
|
||||
number_position = strlen(buf);
|
||||
buf = src->name;
|
||||
buf += " ";
|
||||
number_position = buf.length();
|
||||
num = 2;
|
||||
} else {
|
||||
/* Found digits, parse them and start at the next number. */
|
||||
strecpy(buf, src->name, lastof(buf));
|
||||
buf[number_position] = '\0';
|
||||
char *endptr;
|
||||
num = strtol(&src->name[number_position], &endptr, 10) + 1;
|
||||
padding = endptr - &src->name[number_position];
|
||||
buf = src->name.substr(0, number_position);
|
||||
|
||||
auto num_str = src->name.substr(number_position);
|
||||
padding = (byte)num_str.length();
|
||||
|
||||
std::istringstream iss(num_str);
|
||||
iss >> num;
|
||||
num++;
|
||||
}
|
||||
|
||||
/* Check if this name is already taken. */
|
||||
for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
|
||||
std::ostringstream oss;
|
||||
|
||||
/* Attach the number to the temporary name. */
|
||||
seprintf(&buf[number_position], lastof(buf), "%0*d", padding, num);
|
||||
oss << buf << std::setw(padding) << std::setfill('0') << std::internal << num;
|
||||
|
||||
/* Check the name is unique. */
|
||||
if (IsUniqueVehicleName(buf)) {
|
||||
dst->name = stredup(buf);
|
||||
auto new_name = oss.str();
|
||||
if (IsUniqueVehicleName(new_name.c_str())) {
|
||||
dst->name = new_name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1484,7 +1492,7 @@ CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
|
||||
DoCommand(0, w_front->index | (p2 & 1 ? CO_SHARE : CO_COPY) << 30, v_front->index, flags, CMD_CLONE_ORDER);
|
||||
|
||||
/* Now clone the vehicle's name, if it has one. */
|
||||
if (v_front->name != nullptr) CloneVehicleName(v_front, w_front);
|
||||
if (!v_front->name.empty()) CloneVehicleName(v_front, w_front);
|
||||
}
|
||||
|
||||
/* Since we can't estimate the cost of cloning a vehicle accurately we must
|
||||
@@ -1611,8 +1619,11 @@ CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
|
||||
}
|
||||
|
||||
if (flags & DC_EXEC) {
|
||||
free(v->name);
|
||||
v->name = reset ? nullptr : stredup(text);
|
||||
if (reset) {
|
||||
v->name.clear();
|
||||
} else {
|
||||
v->name = text;
|
||||
}
|
||||
InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 1);
|
||||
InvalidateWindowClassesData(WC_DEPARTURES_BOARD, 0);
|
||||
MarkWholeScreenDirty();
|
||||
|
Reference in New Issue
Block a user