src/autoreplace_gui.cpp
branchNewGRF_ports
changeset 6719 4cc327ad39d5
parent 6585 7da94b26498a
child 6720 35756db7e577
--- a/src/autoreplace_gui.cpp	Tue Mar 27 23:27:27 2007 +0000
+++ b/src/autoreplace_gui.cpp	Sat Jun 02 19:59:29 2007 +0000
@@ -14,6 +14,7 @@
 #include "variables.h"
 #include "vehicle_gui.h"
 #include "newgrf_engine.h"
+#include "group.h"
 
 
 static RailType _railtype_selected_in_replace_gui;
@@ -62,7 +63,7 @@
 /** When an engine is made buildable or is removed from being buildable, add/remove it from the build/autoreplace lists
  * @param type The type of engine
  */
-void AddRemoveEngineFromAutoreplaceAndBuildWindows(byte type)
+void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type)
 {
 	_rebuild_left_list = false; // left list is only for the vehicles the player owns and is not related to being buildable
 	_rebuild_right_list = true;
@@ -150,12 +151,18 @@
 		if (type == VEH_TRAIN && !GenerateReplaceRailList(e, draw_left, WP(w, replaceveh_d).wagon_btnstate)) continue; // special rules for trains
 
 		if (draw_left) {
+			const GroupID selected_group = WP(w, replaceveh_d).sel_group;
+			const uint num_engines = IsDefaultGroupID(selected_group) ? p->num_engines[e] : GetGroup(selected_group)->num_engines[e];
+
 			/* Skip drawing the engines we don't have any of and haven't set for replacement */
-			if (p->num_engines[e] == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), e) == INVALID_ENGINE) continue;
+			if (num_engines == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), e, selected_group) == INVALID_ENGINE) continue;
 		} else {
 			/* This is for engines we can replace to and they should depend on what we selected to replace from */
 			if (!IsEngineBuildable(e, type, _local_player)) continue; // we need to be able to build the engine
 			if (!EnginesGotCargoInCommon(e, WP(w, replaceveh_d).sel_engine[0])) continue; // the engines needs to be able to carry the same cargo
+
+			/* Road vehicles can't be replaced by trams and vice-versa */
+			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;
 			if (e == WP(w, replaceveh_d).sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew)
 		}
 
@@ -202,7 +209,7 @@
 }
 
 
-void DrawEngineList(byte type, int x, int y, const EngineList eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count);
+void DrawEngineList(VehicleType type, int x, int y, const EngineList eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group);
 
 static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
 {
@@ -231,6 +238,7 @@
 
 			Player *p = GetPlayer(_local_player);
 			EngineID selected_id[2];
+			const GroupID selected_group = WP(w,replaceveh_d).sel_group;
 
 			selected_id[0] = WP(w, replaceveh_d).sel_engine[0];
 			selected_id[1] = WP(w, replaceveh_d).sel_engine[1];
@@ -242,15 +250,15 @@
 			SetWindowWidgetDisabledState(w, 4,
 										 selected_id[0] == INVALID_ENGINE ||
 										 selected_id[1] == INVALID_ENGINE ||
-										 EngineReplacementForPlayer(p, selected_id[1]) != INVALID_ENGINE ||
-										 EngineReplacementForPlayer(p, selected_id[0]) == selected_id[1]);
+										 EngineReplacementForPlayer(p, selected_id[1], selected_group) != INVALID_ENGINE ||
+										 EngineReplacementForPlayer(p, selected_id[0], selected_group) == selected_id[1]);
 
 			/* Disable the "Stop Replacing" button if:
 			 *   The left list (existing vehicle) is empty
 			 *   or The selected vehicle has no replacement set up */
 			SetWindowWidgetDisabledState(w, 6,
 										 selected_id[0] == INVALID_ENGINE ||
-										 !EngineHasReplacementForPlayer(p, selected_id[0]));
+										 !EngineHasReplacementForPlayer(p, selected_id[0], selected_group));
 
 			/* now the actual drawing of the window itself takes place */
 			SetDParam(0, _vehicle_type_names[w->window_number]);
@@ -261,14 +269,14 @@
 
 				/* set wagon/engine button */
 				SetDParam(2, WP(w, replaceveh_d).wagon_btnstate ? STR_ENGINES : STR_WAGONS);
+
+				/* sets the colour of that art thing */
+				w->widget[13].color = _player_colors[_local_player];
+				w->widget[16].color = _player_colors[_local_player];
 			}
 
-			w->widget[13].color = _player_colors[_local_player]; // sets the colour of that art thing
-			w->widget[16].color = _player_colors[_local_player]; // sets the colour of that art thing
-
 			DrawWindowWidgets(w);
 
-
 			if (w->window_number == VEH_TRAIN) {
 				/* Draw the selected railtype in the pulldown menu */
 				RailType railtype = _railtype_selected_in_replace_gui;
@@ -277,10 +285,10 @@
 
 			/* sets up the string for the vehicle that is being replaced to */
 			if (selected_id[0] != INVALID_ENGINE) {
-				if (!EngineHasReplacementForPlayer(p, selected_id[0])) {
+				if (!EngineHasReplacementForPlayer(p, selected_id[0], selected_group)) {
 					SetDParam(0, STR_NOT_REPLACING);
 				} else {
-					SetDParam(0, GetCustomEngineName(EngineReplacementForPlayer(p, selected_id[0])));
+					SetDParam(0, GetCustomEngineName(EngineReplacementForPlayer(p, selected_id[0], selected_group)));
 				}
 			} else {
 				SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
@@ -296,7 +304,7 @@
 				EngineID end    = min((i == 0 ? w->vscroll.cap : w->vscroll2.cap) + start, EngList_Count(&list));
 
 				/* Do the actual drawing */
-				DrawEngineList(w->window_number, x, 15, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0);
+				DrawEngineList((VehicleType)w->window_number, x, 15, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0, selected_group);
 
 				/* Also draw the details if an engine is selected */
 				if (WP(w, replaceveh_d).sel_engine[i] != INVALID_ENGINE) {
@@ -328,12 +336,12 @@
 				case 4: { /* Start replacing */
 					EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
 					EngineID veh_to = WP(w, replaceveh_d).sel_engine[1];
-					DoCommandP(0, 3, veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE);
+					DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16) , veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE);
 				} break;
 
 				case 6: { /* Stop replacing */
 					EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
-					DoCommandP(0, 3, veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE);
+					DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16), veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE);
 				} break;
 
 				case 7:
@@ -480,7 +488,7 @@
 };
 
 
-void ShowReplaceVehicleWindow(byte vehicletype)
+void ShowReplaceVehicleWindow(VehicleType vehicletype)
 {
 	Window *w;
 
@@ -509,4 +517,37 @@
 
 	w->caption_color = _local_player;
 	w->vscroll2.cap = w->vscroll.cap;   // these two are always the same
+	WP(w, replaceveh_d).sel_group = DEFAULT_GROUP;
+ }
+
+void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype)
+{
+	Window *w;
+
+	DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype);
+
+	switch (vehicletype) {
+		default: NOT_REACHED();
+		case VEH_TRAIN:
+			w = AllocateWindowDescFront(&_replace_rail_vehicle_desc, vehicletype);
+			w->vscroll.cap  = 8;
+			w->resize.step_height = 14;
+			WP(w, replaceveh_d).wagon_btnstate = true;
+			break;
+		case VEH_ROAD:
+			w = AllocateWindowDescFront(&_replace_road_vehicle_desc, vehicletype);
+			w->vscroll.cap  = 8;
+			w->resize.step_height = 14;
+			break;
+		case VEH_SHIP:
+		case VEH_AIRCRAFT:
+			w = AllocateWindowDescFront(&_replace_ship_aircraft_vehicle_desc, vehicletype);
+			w->vscroll.cap  = 4;
+			w->resize.step_height = 24;
+			break;
+	}
+
+	w->caption_color = _local_player;
+	WP(w, replaceveh_d).sel_group = id_g;
+	w->vscroll2.cap = w->vscroll.cap;   // these two are always the same
 }