summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2009-03-05 17:37:56 +0000
committerpeter1138 <peter1138@openttd.org>2009-03-05 17:37:56 +0000
commit1bde2d97b5fd6b320518d9a2aa8697de5902c707 (patch)
tree9d5e98ec1c1de0ca64477638c9db4d0d4d78cdae /src
parent05fe2d8d2114be0a5ded14827fd092cb79408bd6 (diff)
downloadopenttd-1bde2d97b5fd6b320518d9a2aa8697de5902c707.tar.xz
(svn r15621) -Feature: When cloning a vehicle with a custom name, add and/or increment a number at the end of name and assign it to the new vehicle (for emma)
Diffstat (limited to 'src')
-rw-r--r--src/vehicle.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index 471f5b8c9..2b33a73e8 100644
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -1171,6 +1171,64 @@ CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32
return cost;
}
+/** Clone the custom name of a vehicle, adding or incrementing a number.
+ * @param src Source vehicle, with a custom name.
+ * @param dst Destination vehicle.
+ */
+static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
+{
+ char buf[256];
+
+ /* Find the position of the first digit in the last group of digits. */
+ int number_position;
+ for (number_position = strlen(src->name); 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;
+ if (number_position == strlen(src->name)) {
+ /* No digit at the end, so start at number 2. */
+ strecpy(buf, src->name, lastof(buf));
+ strecat(buf, " ", lastof(buf));
+ number_position = strlen(buf);
+ num = 2;
+ } else {
+ /* Found digits, parse them and start at the next number. */
+ strecpy(buf, src->name, lastof(buf));
+ buf[number_position] = '\0';
+ num = strtol(&src->name[number_position], NULL, 10) + 1;
+ }
+
+ /* Check if this name is already taken. */
+ for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
+ /* Attach the number to the temporary name. */
+ seprintf(&buf[number_position], lastof(buf), "%d", num);
+ bool dup = false;
+
+ /* Check name against all other vehicles for this company. */
+ const Vehicle *v;
+ FOR_ALL_VEHICLES(v) {
+ if (v->owner == src->owner && v->name != NULL) {
+ if (strcmp(buf, v->name) == 0) {
+ dup = true;
+ break;
+ }
+ }
+ }
+
+ if (!dup) {
+ /* Name is not a duplicate, so assign it. */
+ dst->name = strdup(buf);
+ break;
+ }
+ }
+
+ /* All done. If we didn't find a name, it'll just use its default. */
+}
+
/** Clone a vehicle. If it is a train, it will clone all the cars too
* @param tile tile of the depot where the cloned vehicle is build
* @param flags type of operation
@@ -1322,6 +1380,9 @@ CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
* cargo types might not match (passenger vs non-passenger)
*/
DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
+
+ /* Now clone the vehicle's name, if it has one. */
+ if (v_front->name != NULL) CloneVehicleName(v_front, w_front);
}
/* Since we can't estimate the cost of cloning a vehicle accurately we must