Heavy-haul railways (HHRs) pose significant challenges due to their substantial traction weight, extended train length, and complex operational environments. Heavy-haul trains (HHTs), equipped with traditional pneumatic control braking systems, must adopt cycle braking strategies on long downhill slopes. The varying traction masses of HHTs on these railways lead to diverse maneuvering characteristics, presenting challenges for drivers and dispatchers in unforeseen circumstances. To enhance transportation efficiency and mitigate operational complexities, a trajectory optimization method is formulated for determining the optimal trajectory of HHTs with different traction masses under complex conditions, including long downhill slopes, temporary speed limit sections, and regular sections. It considers the dynamics of train traction, braking, and coasting at each phase, optimizing objectives such as train operation efficiency, energy consumption, and pneumatic braking times. A linear weight search algorithm ensures punctuality, and the model is linearized into a mixed-integer linear programming (MILP) form using segmented and stepwise functions to align with operational realities. Simulation experiments utilizing real data and various HHT configurations validate the efficacy of the proposed approach against alternative methods. This method offers precise trajectory optimization under complex conditions, providing valuable guidance for dispatchers and drivers in the heavy-haul railway sector.