src/autoreplace_cmd.cpp
changeset 8258 9fa31acb07bc
parent 8254 1496654ca5e7
child 8264 b1e85998c7d3
equal deleted inserted replaced
8257:3151fdbc73f9 8258:9fa31acb07bc
   132 	const Player *p = GetPlayer(old_v->owner);
   132 	const Player *p = GetPlayer(old_v->owner);
   133 	EngineID new_engine_type;
   133 	EngineID new_engine_type;
   134 	const UnitID cached_unitnumber = old_v->unitnumber;
   134 	const UnitID cached_unitnumber = old_v->unitnumber;
   135 	bool new_front = false;
   135 	bool new_front = false;
   136 	Vehicle *new_v = NULL;
   136 	Vehicle *new_v = NULL;
   137 	char vehicle_name[32];
   137 	char *vehicle_name = NULL;
   138 	CargoID replacement_cargo_type;
   138 	CargoID replacement_cargo_type;
   139 
   139 
   140 	/* If the vehicle belongs to a group, check if the group is protected from the global autoreplace.
   140 	/* If the vehicle belongs to a group, check if the group is protected from the global autoreplace.
   141 	 *  If not, chek if an global auto replacement is defined */
   141 	 *  If not, chek if an global auto replacement is defined */
   142 	new_engine_type = (IsValidGroupID(old_v->group_id) && GetGroup(old_v->group_id)->replace_protection) ?
   142 	new_engine_type = (IsValidGroupID(old_v->group_id) && GetGroup(old_v->group_id)->replace_protection) ?
   238 		}
   238 		}
   239 		/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
   239 		/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
   240 		MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v);
   240 		MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v);
   241 
   241 
   242 		// Get the name of the old vehicle if it has a custom name.
   242 		// Get the name of the old vehicle if it has a custom name.
   243 		if (!IsCustomName(old_v->string_id)) {
   243 		if (old_v->name != NULL) vehicle_name = strdup(old_v->name);
   244 			vehicle_name[0] = '\0';
       
   245 		} else {
       
   246 			SetDParam(0, old_v->index);
       
   247 			GetString(vehicle_name, STR_VEHICLE_NAME, lastof(vehicle_name));
       
   248 		}
       
   249 	} else { // flags & DC_EXEC not set
   244 	} else { // flags & DC_EXEC not set
   250 		CommandCost tmp_move;
   245 		CommandCost tmp_move;
   251 
   246 
   252 		if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) {
   247 		if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) {
   253 			Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts
   248 			Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts
   283 		/* now we assign the old unitnumber to the new vehicle */
   278 		/* now we assign the old unitnumber to the new vehicle */
   284 		new_v->unitnumber = cached_unitnumber;
   279 		new_v->unitnumber = cached_unitnumber;
   285 	}
   280 	}
   286 
   281 
   287 	/* Transfer the name of the old vehicle */
   282 	/* Transfer the name of the old vehicle */
   288 	if ((flags & DC_EXEC) && vehicle_name[0] != '\0') {
   283 	if ((flags & DC_EXEC) && vehicle_name != NULL) {
   289 		_cmd_text = vehicle_name;
   284 		_cmd_text = vehicle_name;
   290 		DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE);
   285 		DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE);
       
   286 		free(vehicle_name);
   291 	}
   287 	}
   292 
   288 
   293 	return cost;
   289 	return cost;
   294 }
   290 }
   295 
   291