src/autoreplace_gui.cpp
changeset 9889 1aaf71e958cc
parent 9751 9e9c726170cc
child 10000 57463dc267a0
equal deleted inserted replaced
9888:90d3e362b95c 9889:1aaf71e958cc
    21 #include "widgets/dropdown_func.h"
    21 #include "widgets/dropdown_func.h"
    22 #include "engine_func.h"
    22 #include "engine_func.h"
    23 #include "engine_base.h"
    23 #include "engine_base.h"
    24 #include "window_gui.h"
    24 #include "window_gui.h"
    25 #include "engine_gui.h"
    25 #include "engine_gui.h"
    26 #include "articulated_vehicles.h"
       
    27 
    26 
    28 #include "table/sprites.h"
    27 #include "table/sprites.h"
    29 #include "table/strings.h"
    28 #include "table/strings.h"
    30 
    29 
    31 void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group);
    30 void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group);
    95  */
    94  */
    96 void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type)
    95 void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type)
    97 {
    96 {
    98 	InvalidateWindowData(WC_REPLACE_VEHICLE, type, false); // Update the autoreplace window
    97 	InvalidateWindowData(WC_REPLACE_VEHICLE, type, false); // Update the autoreplace window
    99 	InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well
    98 	InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well
   100 }
       
   101 
       
   102 /** Figure out if two engines got at least one type of cargo in common (refitting if needed)
       
   103  * @param engine_a one of the EngineIDs
       
   104  * @param engine_b the other EngineID
       
   105  * @param type the type of the engines
       
   106  * @return true if they can both carry the same type of cargo (or at least one of them got no capacity at all)
       
   107  */
       
   108 static bool EnginesGotCargoInCommon(EngineID engine_a, EngineID engine_b, VehicleType type)
       
   109 {
       
   110 	uint32 available_cargos_a = GetUnionOfArticulatedRefitMasks(engine_a, type, true);
       
   111 	uint32 available_cargos_b = GetUnionOfArticulatedRefitMasks(engine_b, type, true);
       
   112 	return (available_cargos_a == 0 || available_cargos_b == 0 || (available_cargos_a & available_cargos_b) != 0);
       
   113 }
    99 }
   114 
   100 
   115 /**
   101 /**
   116  * Window for the autoreplacing of vehicles.
   102  * Window for the autoreplacing of vehicles.
   117  */
   103  */
   141 		if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false;
   127 		if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false;
   142 
   128 
   143 		if (draw_left && show_engines) {
   129 		if (draw_left && show_engines) {
   144 			/* Ensure that the railtype is specific to the selected one */
   130 			/* Ensure that the railtype is specific to the selected one */
   145 			if (rvi->railtype != this->sel_railtype) return false;
   131 			if (rvi->railtype != this->sel_railtype) return false;
   146 		} else {
       
   147 			/* Ensure that it's a compatible railtype to the selected one (like electric <-> diesel)
       
   148 			 * The vehicle do not have to have power on the railtype in question, only able to drive (pulled if needed) */
       
   149 			if (!IsCompatibleRail(rvi->railtype, this->sel_railtype)) return false;
       
   150 		}
   132 		}
   151 		return true;
   133 		return true;
   152 	}
   134 	}
   153 
   135 
   154 
   136 
   175 				const uint num_engines = GetGroupNumEngines(_local_player, selected_group, eid);
   157 				const uint num_engines = GetGroupNumEngines(_local_player, selected_group, eid);
   176 
   158 
   177 				/* Skip drawing the engines we don't have any of and haven't set for replacement */
   159 				/* Skip drawing the engines we don't have any of and haven't set for replacement */
   178 				if (num_engines == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), eid, selected_group) == INVALID_ENGINE) continue;
   160 				if (num_engines == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), eid, selected_group) == INVALID_ENGINE) continue;
   179 			} else {
   161 			} else {
   180 				/* This is for engines we can replace to and they should depend on what we selected to replace from */
   162 				if (!CheckAutoreplaceValidity(this->sel_engine[0], eid, _local_player)) continue;
   181 				if (!IsEngineBuildable(eid, type, _local_player)) continue; // we need to be able to build the engine
       
   182 				if (!EnginesGotCargoInCommon(eid, this->sel_engine[0], type)) continue; // the engines needs to be able to carry the same cargo
       
   183 
       
   184 				/* Road vehicles can't be replaced by trams and vice-versa */
       
   185 				if (type == VEH_ROAD && HasBit(EngInfo(this->sel_engine[0])->misc_flags, EF_ROAD_TRAM) != HasBit(e->info.misc_flags, EF_ROAD_TRAM)) continue;
       
   186 				if (eid == this->sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew)
       
   187 			}
   163 			}
   188 
   164 
   189 			*list->Append() = eid;
   165 			*list->Append() = eid;
   190 			if (eid == this->sel_engine[i]) selected_engine = eid; // The selected engine is still in the list
   166 			if (eid == this->sel_engine[i]) selected_engine = eid; // The selected engine is still in the list
   191 		}
   167 		}