changeset 4465:cd2b09e54303 draft

(svn r6249) -Fix: fixed assert when pressing goto depot in an empty list (forgot to disable the button in this condition) -Code cleanup r6246: simplified SendAllVehiclesToDepot() and moved an { in PlayerVehWndProc()
author bjarni <bjarni@openttd.org>
date Wed, 30 Aug 2006 23:01:45 +0000
parents 5e4d40ff21a7
children e1a370ed33ee
files vehicle.c vehicle_gui.c
diffstat 2 files changed, 39 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/vehicle.c
+++ b/vehicle.c
@@ -1908,31 +1908,22 @@
 int32 SendAllVehiclesToDepot(byte type, uint32 flags, bool service, PlayerID owner)
 {
 	const uint subtype = (type != VEH_Aircraft) ? Train_Front : 2;
-	if (flags & DC_EXEC) {
+	const Vehicle *v;
+
 	/* Send all the vehicles to a depot */
-		const Vehicle *v;
-		FOR_ALL_VEHICLES(v) {
-			if (v->type == type && v->owner == owner && (
-				(type == VEH_Train && IsFrontEngine(v)) ||
-				(type != VEH_Train && v->subtype <= subtype))) {
-				DoCommand(v->tile, v->index, service, flags, CMD_SEND_TO_DEPOT(type));
-			}
+	FOR_ALL_VEHICLES(v) {
+		if (v->type == type && v->owner == owner && (
+			(type == VEH_Train && IsFrontEngine(v)) ||
+			(type != VEH_Train && v->subtype <= subtype))) {
+			/* Return 0 if DC_EXEC is not set and a DoCommand() returns 0 (valid goto depot command) */
+			/* In this case we know that at least one vehicle can be send to a depot and we will issue the command */
+			/* Since we will issue the command nomatter how many vehicles more than one it's valid for, we skip checking the rest */
+			/* When DC_EXEC is set, we need to run this loop for all vehicles nomatter return values from each vehicle */
+			if (!DoCommand(v->tile, v->index, service, flags, CMD_SEND_TO_DEPOT(type)) && !(flags & DC_EXEC)) return 0;
 		}
-	} else {
-	/* See if we can find a vehicle to send to a depot */
-		const Vehicle *v;
-		FOR_ALL_VEHICLES(v) {
-			if (v->type == type && v->owner == owner && (
-				(type == VEH_Train && IsFrontEngine(v)) ||
-				(type != VEH_Train && v->subtype <= subtype))) {
-				/* We found one vehicle to send to a depot. No need to search for more. The command is valid */
-				if (!DoCommand(v->tile, v->index, service, flags, CMD_SEND_TO_DEPOT(type))) return 0;
-			}
-		}
-
-		return CMD_ERROR;
 	}
-	return 0;
+
+	return (flags & DC_EXEC) ? 0 : CMD_ERROR;
 }
 
 
--- a/vehicle_gui.c
+++ b/vehicle_gui.c
@@ -1268,6 +1268,7 @@
 					SetDParam(0, p->name_1);
 					SetDParam(1, p->name_2);
 					SetDParam(2, w->vscroll.count);
+					if (vl->list_length == 0) SETBIT(w->disabled_state, 9);
 					if (vehicle_type == VEH_Aircraft) {
 						w->widget[9].unkA = STR_SEND_TO_HANGARS;
 						w->widget[9].tooltips = STR_SEND_TO_HANGARS_TIP;
@@ -1404,35 +1405,34 @@
 					}
 				} break;
 
-				case 9: /* Left button */
+				case 9: { /* Left button */
+					uint16 window_type = w->window_number & VLW_FLAGS;
 					if (GB(w->window_number, 0, 8) /* OwnerID */ != _local_player) break;
-					{
-						uint16 window_type = w->window_number & VLW_FLAGS;
-						switch (window_type) {
-							case VLW_STANDARD:
-							case VLW_SHARED_ORDERS: {
-								/* Send to depot */
-								const Vehicle *v;
-								assert(vl->list_length != 0);
-								v = vl->sort_list[0];
-								DoCommandP(v->tile, v->index, window_type | _ctrl_pressed ? 3 : 2, NULL, CMD_SEND_TO_DEPOT(vehicle_type));
-								break;
-							}
+					switch (window_type) {
+						case VLW_STANDARD:
+						case VLW_SHARED_ORDERS: {
+							/* Send to depot */
+							const Vehicle *v;
+							assert(vl->list_length != 0);
+							v = vl->sort_list[0];
+							DoCommandP(v->tile, v->index, window_type | _ctrl_pressed ? 3 : 2, NULL, CMD_SEND_TO_DEPOT(vehicle_type));
+							break;
+						}
 
-							case VLW_STATION_LIST:
-								/* Build new Vehicle */
-								switch (vehicle_type) {
-									case VEH_Train:	   ShowBuildTrainWindow(0);    break;
-									case VEH_Road:     ShowBuildRoadVehWindow(0);  break;
-									case VEH_Ship:     ShowBuildShipWindow(0);     break;
-									case VEH_Aircraft: ShowBuildAircraftWindow(0); break;
-									default: NOT_REACHED(); break;
-								}
-								break;
-							default: NOT_REACHED(); break;
-						}
-						break;
+						case VLW_STATION_LIST:
+							/* Build new Vehicle */
+							switch (vehicle_type) {
+								case VEH_Train:	   ShowBuildTrainWindow(0);    break;
+								case VEH_Road:     ShowBuildRoadVehWindow(0);  break;
+								case VEH_Ship:     ShowBuildShipWindow(0);     break;
+								case VEH_Aircraft: ShowBuildAircraftWindow(0); break;
+								default: NOT_REACHED(); break;
+							}
+							break;
+						default: NOT_REACHED(); break;
 					}
+					break;
+				}
 
 				case 10: /* Right button */
 					ShowReplaceVehicleWindow(vehicle_type);