src/autoreplace_cmd.cpp
branchcpp_gui
changeset 6308 646711c5feaa
parent 6298 c30fe89622df
child 9911 0b8b245a2391
equal deleted inserted replaced
6307:f40e88cff863 6308:646711c5feaa
    85  *    CT_NO_REFIT is returned if no refit is needed
    85  *    CT_NO_REFIT is returned if no refit is needed
    86  *    CT_INVALID is returned when both old and new vehicle got cargo capacity and refitting the new one to the old one's cargo type isn't possible
    86  *    CT_INVALID is returned when both old and new vehicle got cargo capacity and refitting the new one to the old one's cargo type isn't possible
    87  */
    87  */
    88 static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type)
    88 static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type)
    89 {
    89 {
    90 	bool new_cargo_capacity = true;
    90 	CargoID new_cargo_type = GetEngineCargoType(engine_type);
    91 	CargoID new_cargo_type = CT_INVALID;
    91 
    92 
    92 	if (new_cargo_type == CT_INVALID) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity
    93 	switch (v->type) {
       
    94 		case VEH_TRAIN:
       
    95 			new_cargo_capacity = (RailVehInfo(engine_type)->capacity > 0);
       
    96 			new_cargo_type     = RailVehInfo(engine_type)->cargo_type;
       
    97 			break;
       
    98 
       
    99 		case VEH_ROAD:
       
   100 			new_cargo_capacity = (RoadVehInfo(engine_type)->capacity > 0);
       
   101 			new_cargo_type     = RoadVehInfo(engine_type)->cargo_type;
       
   102 			break;
       
   103 		case VEH_SHIP:
       
   104 			new_cargo_capacity = (ShipVehInfo(engine_type)->capacity > 0);
       
   105 			new_cargo_type     = ShipVehInfo(engine_type)->cargo_type;
       
   106 			break;
       
   107 
       
   108 		case VEH_AIRCRAFT:
       
   109 			/* all aircraft starts as passenger planes with cargo capacity
       
   110 			 * new_cargo_capacity is always true for aircraft, which is the init value. No need to set it here */
       
   111 			new_cargo_type     = CT_PASSENGERS;
       
   112 			break;
       
   113 
       
   114 		default: NOT_REACHED(); break;
       
   115 	}
       
   116 
       
   117 	if (!new_cargo_capacity) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity
       
   118 
    93 
   119 	if (v->cargo_type == new_cargo_type || CanRefitTo(engine_type, v->cargo_type)) {
    94 	if (v->cargo_type == new_cargo_type || CanRefitTo(engine_type, v->cargo_type)) {
   120 		if (VerifyAutoreplaceRefitForOrders(v, engine_type)) {
    95 		if (VerifyAutoreplaceRefitForOrders(v, engine_type)) {
   121 			return v->cargo_type == new_cargo_type ? (CargoID)CT_NO_REFIT : v->cargo_type;
    96 			return v->cargo_type == new_cargo_type ? (CargoID)CT_NO_REFIT : v->cargo_type;
   122 		} else {
    97 		} else {
   135 	do {
   110 	do {
   136 		if (v->cargo_cap == 0) continue;
   111 		if (v->cargo_cap == 0) continue;
   137 		/* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
   112 		/* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
   138 		if (v->cargo_type == new_cargo_type) return CT_NO_REFIT;
   113 		if (v->cargo_type == new_cargo_type) return CT_NO_REFIT;
   139 		if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type;
   114 		if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type;
   140 	} while ((v=v->next) != NULL);
   115 	} while ((v = v->next) != NULL);
   141 	return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
   116 	return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
   142 }
   117 }
   143 
   118 
   144 /* Replaces a vehicle (used to be called autorenew)
   119 /* Replaces a vehicle (used to be called autorenew)
   145  * This function is only called from MaybeReplaceVehicle()
   120  * This function is only called from MaybeReplaceVehicle()