src/autoreplace_gui.cpp
changeset 9065 cb8e7bdc2782
parent 9027 b28d0b8809ad
child 9070 dd0121143eba
equal deleted inserted replaced
9064:ec8e45f6198b 9065:cb8e7bdc2782
   189  * @param w Window, that contains the list
   189  * @param w Window, that contains the list
   190  * @param draw_left true if generating the left list, otherwise false
   190  * @param draw_left true if generating the left list, otherwise false
   191  */
   191  */
   192 static void GenerateReplaceVehList(Window *w, bool draw_left)
   192 static void GenerateReplaceVehList(Window *w, bool draw_left)
   193 {
   193 {
   194 	EngineID e;
       
   195 	EngineID selected_engine = INVALID_ENGINE;
   194 	EngineID selected_engine = INVALID_ENGINE;
   196 	VehicleType type = (VehicleType)w->window_number;
   195 	VehicleType type = (VehicleType)w->window_number;
   197 	byte i = draw_left ? 0 : 1;
   196 	byte i = draw_left ? 0 : 1;
   198 
   197 
   199 	EngineList *list = &WP(w, replaceveh_d).list[i];
   198 	EngineList *list = &WP(w, replaceveh_d).list[i];
   200 	EngList_RemoveAll(list);
   199 	EngList_RemoveAll(list);
   201 
   200 
   202 	FOR_ALL_ENGINEIDS_OF_TYPE(e, type) {
   201 	EngineID eid;
   203 		if (type == VEH_TRAIN && !GenerateReplaceRailList(e, draw_left, WP(w, replaceveh_d).wagon_btnstate)) continue; // special rules for trains
   202 	FOR_ALL_ENGINEIDS_OF_TYPE(eid, type) {
       
   203 		if (type == VEH_TRAIN && !GenerateReplaceRailList(eid, draw_left, WP(w, replaceveh_d).wagon_btnstate)) continue; // special rules for trains
   204 
   204 
   205 		if (draw_left) {
   205 		if (draw_left) {
   206 			const GroupID selected_group = WP(w, replaceveh_d).sel_group;
   206 			const GroupID selected_group = WP(w, replaceveh_d).sel_group;
   207 			const uint num_engines = GetGroupNumEngines(_local_player, selected_group, e);
   207 			const uint num_engines = GetGroupNumEngines(_local_player, selected_group, eid);
   208 
   208 
   209 			/* Skip drawing the engines we don't have any of and haven't set for replacement */
   209 			/* Skip drawing the engines we don't have any of and haven't set for replacement */
   210 			if (num_engines == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), e, selected_group) == INVALID_ENGINE) continue;
   210 			if (num_engines == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), eid, selected_group) == INVALID_ENGINE) continue;
   211 		} else {
   211 		} else {
   212 			/* This is for engines we can replace to and they should depend on what we selected to replace from */
   212 			/* This is for engines we can replace to and they should depend on what we selected to replace from */
   213 			if (!IsEngineBuildable(e, type, _local_player)) continue; // we need to be able to build the engine
   213 			if (!IsEngineBuildable(eid, type, _local_player)) continue; // we need to be able to build the engine
   214 			if (!EnginesGotCargoInCommon(e, WP(w, replaceveh_d).sel_engine[0])) continue; // the engines needs to be able to carry the same cargo
   214 			if (!EnginesGotCargoInCommon(eid, WP(w, replaceveh_d).sel_engine[0])) continue; // the engines needs to be able to carry the same cargo
   215 
   215 
   216 			/* Road vehicles can't be replaced by trams and vice-versa */
   216 			/* Road vehicles can't be replaced by trams and vice-versa */
   217 			if (type == VEH_ROAD && HasBit(EngInfo(WP(w, replaceveh_d).sel_engine[0])->misc_flags, EF_ROAD_TRAM) != HasBit(EngInfo(e)->misc_flags, EF_ROAD_TRAM)) continue;
   217 			if (type == VEH_ROAD && HasBit(EngInfo(WP(w, replaceveh_d).sel_engine[0])->misc_flags, EF_ROAD_TRAM) != HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM)) continue;
   218 			if (e == WP(w, replaceveh_d).sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew)
   218 			if (eid == WP(w, replaceveh_d).sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew)
   219 		}
   219 		}
   220 
   220 
   221 		EngList_Add(list, e);
   221 		EngList_Add(list, eid);
   222 		if (e == WP(w, replaceveh_d).sel_engine[i]) selected_engine = e; // The selected engine is still in the list
   222 		if (eid == WP(w, replaceveh_d).sel_engine[i]) selected_engine = eid; // The selected engine is still in the list
   223 	}
   223 	}
   224 	WP(w, replaceveh_d).sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
   224 	WP(w, replaceveh_d).sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
   225 	if (type == VEH_TRAIN) EngList_Sort(list, &TrainEngineNumberSorter);
   225 	if (type == VEH_TRAIN) EngList_Sort(list, &TrainEngineNumberSorter);
   226 }
   226 }
   227 
   227