*** vehicle.c 2006-06-05 14:59:24.000000000 +0200 --- vehicle.c 2006-06-05 15:00:35.000000000 +0200 *************** *** 30,35 **** --- 30,37 ---- #include "station_map.h" #include "water_map.h" + #include + #define INVALID_COORD (-0x8000) #define GEN_HASH(x,y) (((x & 0x1F80)>>7) + ((y & 0xFC0))) *************** *** 1567,1572 **** --- 1569,1580 ---- w_front = w; w->service_interval = v->service_interval; DoCommand(0, (v->index << 16) | w->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER); + + // if orders are shared, increment vehicle number + if (p2) { + IncrementVehicleNameOnClone(v, w, flags); + } + } w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop } *************** *** 1579,1584 **** --- 1587,1672 ---- return total_cost; } + /** Increment the cloned' vehicles name + * @param v the original vehicle's index + * @param w the clone's index + */ + void IncrementVehicleNameOnClone(Vehicle *v, Vehicle *w, uint32 flags) + { + int len = 32; + char vehicle_name[len]; + char vehicle_number[len]; + Vehicle *nvehicle; // the next vehicle in array to check + char nvehicle_name[len]; // the next vehicle's name + uint16 e; + uint16 pool = GetVehiclePoolSize(); + int i, j, f, n, largest; + int numbers[pool]; // the array of numbers our new number must fit into + + // Get the name of the old vehicle + if ((v->string_id & 0xF800) != 0x7800) { + vehicle_name[0] = '\0'; + } else { + GetName(v->string_id & 0x7FF, vehicle_name); + } + + // find out how far into the name the digits start, save the index + i = 0; + while (!isdigit(vehicle_name[i]) && (i < len)) { + i++; + } + + // use the index to read an integer from the vehicle name + n = atoi(&vehicle_name[i]); + if (n != 0) { + + // find a vacant number to assign the new vehicle, check all existing + // vehicles which have the same name + memset(numbers, 0, pool); + e = 0; + j = 0; + for (e = 0; e < pool; e++) { + // verify vehicle owner and type to match this vehicle + nvehicle = GetVehicle(e); + if (CheckOwnership(nvehicle->owner) && (v->type == nvehicle->type)) { + + // get string_id + if ((nvehicle->string_id & 0xF800) != 0x7800) { + nvehicle_name[0] = '\0'; + } else { + GetName(nvehicle->string_id & 0x7FF, nvehicle_name); + } + + // filter string_id's that are empty or non-equal + if (strncmp(nvehicle_name, vehicle_name, i) == 0) { + numbers[j] = atoi( &nvehicle_name[i]); + j++; + } + + } + } + + // resulting array 'numbers' has the numbers of vehicles we need to examine + largest = 0; + for (f = 0; f < j; f++) { + if (numbers[f] > largest) largest = numbers[f]; + } + + // assign number to vehicle + n = largest+1; + vehicle_name[i] = '\0'; + sprintf(vehicle_number, "%d", n); + strcat(vehicle_name, vehicle_number); + + // Set the name of the new vehicle + if ((flags & DC_EXEC) && vehicle_name[0] != '\0') { + _cmd_text = vehicle_name; + DoCommand(0, w->index, 0, DC_EXEC, CMD_NAME_VEHICLE); + } + } + + } + /* * move the cargo from one engine to another if possible */ *** vehicle.h 2006-06-05 14:59:24.000000000 +0200 --- vehicle.h 2006-06-05 14:59:52.000000000 +0200 *************** *** 297,302 **** --- 297,304 ---- void AgeVehicle(Vehicle *v); void VehicleEnteredDepotThisTick(Vehicle *v); + void IncrementVehicleNameOnClone(Vehicle *v, Vehicle *w, uint32 flags); + void BeginVehicleMove(Vehicle *v); void EndVehicleMove(Vehicle *v);