changeset 16078:aef87b55e3b5 draft

(svn r20771) -Add: concept of vehicle list identifiers to identify a vehicle list instead of a string of parameters
author rubidium <rubidium@openttd.org>
date Wed, 08 Sep 2010 21:28:50 +0000
parents f1fffbd6c714
children f961c962db5d
files src/group_gui.cpp src/vehicle_cmd.cpp src/vehicle_gui.cpp src/vehicle_gui_base.h src/vehiclelist.cpp src/vehiclelist.h
diffstat 6 files changed, 69 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/src/group_gui.cpp
+++ b/src/group_gui.cpp
@@ -190,7 +190,8 @@
 		this->vehicles.SetListing(*this->sorting);
 		this->vehicles.ForceRebuild();
 		this->vehicles.NeedResort();
-		this->BuildVehicleList(owner, this->group_sel, IsAllGroupID(this->group_sel) ? VLW_STANDARD : VLW_GROUP_LIST);
+		VehicleListIdentifier vli(IsAllGroupID(this->group_sel) ? VL_STANDARD : VL_GROUP_LIST, this->vehicle_type, owner, this->group_sel);
+		this->BuildVehicleList(vli);
 		this->SortVehicleList();
 
 		this->groups.ForceRebuild();
@@ -302,7 +303,8 @@
 
 		/* If we select the all vehicles, this->list will contain all vehicles of the owner
 		 * else this->list will contain all vehicles which belong to the selected group */
-		this->BuildVehicleList(owner, this->group_sel, IsAllGroupID(this->group_sel) ? VLW_STANDARD : VLW_GROUP_LIST);
+		VehicleListIdentifier vli(IsAllGroupID(this->group_sel) ? VL_STANDARD : VL_GROUP_LIST, this->vehicle_type, owner, this->group_sel);
+		this->BuildVehicleList(vli);
 		this->SortVehicleList();
 
 		this->BuildGroupList(owner);
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -456,7 +456,8 @@
 		uint32 id = GB(p2, 16, 16);
 		uint16 window_type = p2 & VLW_MASK;
 
-		if (!GenerateVehicleSortList(&list, vehicle_type, _current_company, id, window_type)) return CMD_ERROR;
+		VehicleListIdentifier vli((VehicleListType)(window_type >> 8), vehicle_type, _current_company, id);
+		if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
 	} else {
 		/* Get the list of vehicles in the depot */
 		BuildDepotVehicleList(vehicle_type, tile, &list, NULL);
@@ -806,25 +807,22 @@
 
 /**
  * Send all vehicles of type to depots
- * @param type type of vehicle
- * @param flags the flags used for DoCommand()
+ * @param flags   the flags used for DoCommand()
  * @param service should the vehicles only get service in the depots
- * @param owner owner of the vehicles to send
- * @param vlw_flag tells what kind of list requested the goto depot
- * @param id general purpose id whoms meaning is given by @c vlw_flag; e.g. StationID for station lists
+ * @param vli     identifier of the vehicle list
  * @return 0 for success and CMD_ERROR if no vehicle is able to go to depot
  */
-CommandCost SendAllVehiclesToDepot(VehicleType type, DoCommandFlag flags, bool service, Owner owner, uint16 vlw_flag, uint32 id)
+CommandCost SendAllVehiclesToDepot(DoCommandFlag flags, bool service, const VehicleListIdentifier &vli)
 {
 	VehicleList list;
 
-	if (!GenerateVehicleSortList(&list, type, owner, id, vlw_flag)) return CMD_ERROR;
+	if (!GenerateVehicleSortList(&list, vli)) return CMD_ERROR;
 
 	/* Send all the vehicles to a depot */
 	bool had_success = false;
 	for (uint i = 0; i < list.Length(); i++) {
 		const Vehicle *v = list[i];
-		CommandCost ret = DoCommand(v->tile, v->index | (service ? DEPOT_SERVICE : 0U) | DEPOT_DONT_CANCEL, 0, flags, GetCmdSendToDepot(type));
+		CommandCost ret = DoCommand(v->tile, v->index | (service ? DEPOT_SERVICE : 0U) | DEPOT_DONT_CANCEL, 0, flags, GetCmdSendToDepot(vli.vtype));
 
 		if (ret.Succeeded()) {
 			had_success = true;
@@ -859,7 +857,8 @@
 	if (p1 & DEPOT_MASS_SEND) {
 		/* Mass goto depot requested */
 		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
-		return SendAllVehiclesToDepot((VehicleType)GB(p2, 11, 2), flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), GB(p1, 0, 20));
+		VehicleListIdentifier vli((VehicleListType)((p2 & VLW_MASK) >> 8), (VehicleType)GB(p2, 11, 2), _current_company, GB(p1, 0, 20));
+		return SendAllVehiclesToDepot(flags, (p2 & DEPOT_SERVICE) != 0, vli);
 	}
 
 	Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -99,13 +99,13 @@
 	STR_VEHICLE_LIST_SEND_AIRCRAFT_TO_HANGAR
 };
 
-void BaseVehicleListWindow::BuildVehicleList(Owner owner, uint16 index, uint16 window_type)
+void BaseVehicleListWindow::BuildVehicleList(const VehicleListIdentifier &vli)
 {
 	if (!this->vehicles.NeedRebuild()) return;
 
-	DEBUG(misc, 3, "Building vehicle list for company %d at station %d", owner, index);
+	DEBUG(misc, 3, "Building vehicle list for company %d at station %d", vli.company, vli.index);
 
-	GenerateVehicleSortList(&this->vehicles, this->vehicle_type, owner, index, window_type);
+	GenerateVehicleSortList(&this->vehicles, vli);
 
 	uint unitnumber = 0;
 	for (const Vehicle **v = this->vehicles.Begin(); v != this->vehicles.End(); v++) {
@@ -1108,7 +1108,8 @@
 		this->vehicles.SetListing(*this->sorting);
 		this->vehicles.ForceRebuild();
 		this->vehicles.NeedResort();
-		this->BuildVehicleList(company, GB(window_number, 16, 16), window_type);
+		VehicleListIdentifier vli((VehicleListType)(window_type >> 8), this->vehicle_type, owner, GB(window_number, 16, 16));
+		this->BuildVehicleList(vli);
 		this->SortVehicleList();
 
 		/* Set up the window widgets */
@@ -1222,7 +1223,8 @@
 	{
 		const uint16 window_type = this->window_number & VLW_MASK;
 
-		this->BuildVehicleList(this->owner, GB(this->window_number, 16, 16), window_type);
+		VehicleListIdentifier vli((VehicleListType)(window_type >> 8), this->vehicle_type, this->owner, GB(this->window_number, 16, 16));
+		this->BuildVehicleList(vli);
 		this->SortVehicleList();
 
 		if (this->vehicles.Length() == 0 && this->IsWidgetLowered(VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN)) {
--- a/src/vehicle_gui_base.h
+++ b/src/vehicle_gui_base.h
@@ -13,6 +13,7 @@
 #define VEHICLE_GUI_BASE_H
 
 #include "sortlist_type.h"
+#include "vehiclelist.h"
 #include "window_gui.h"
 #include "widgets/dropdown_type.h"
 
@@ -44,7 +45,7 @@
 
 	void DrawVehicleListItems(VehicleID selected_vehicle, int line_height, const Rect &r) const;
 	void SortVehicleList();
-	void BuildVehicleList(Owner owner, uint16 index, uint16 window_type);
+	void BuildVehicleList(const VehicleListIdentifier &identifier);
 	Dimension GetActionDropdownSize(bool show_autoreplace, bool show_group);
 	DropDownList *BuildActionDropdownList(bool show_autoreplace, bool show_group);
 };
--- a/src/vehiclelist.cpp
+++ b/src/vehiclelist.cpp
@@ -63,35 +63,25 @@
 
 /**
  * Generate a list of vehicles based on window type.
- * @param list        Pointer to list to add vehicles to
- * @param type        Type of vehicle
- * @param owner       Company to generate list for
- * @param index       This parameter has different meanings depending on window_type
- *    <ul>
- *      <li>VLW_STATION_LIST:  index of station/waypoint to generate a list for</li>
- *      <li>VLW_SHARED_ORDERS: index of order to generate a list for<li>
- *      <li>VLW_STANDARD: not used<li>
- *      <li>VLW_DEPOT_LIST: TileIndex of the depot/hangar to make the list for</li>
- *      <li>VLW_GROUP_LIST: index of group to generate a list for</li>
- *    </ul>
- * @param window_type The type of window the list is for, using the VLW_ flags in vehicle_gui.h
+ * @param list Pointer to list to add vehicles to
+ * @param vli  The identifier of this vehicle list.
  * @return false if invalid list is requested
  */
-bool GenerateVehicleSortList(VehicleList *list, VehicleType type, Owner owner, uint32 index, uint16 window_type)
+bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
 {
 	list->Clear();
 
 	const Vehicle *v;
 
-	switch (window_type) {
-		case VLW_STATION_LIST:
+	switch (vli.type) {
+		case VL_STATION_LIST:
 			FOR_ALL_VEHICLES(v) {
-				if (v->type == type && v->IsPrimaryVehicle()) {
+				if (v->type == vli.type && v->IsPrimaryVehicle()) {
 					const Order *order;
 
 					FOR_VEHICLE_ORDERS(v, order) {
 						if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT))
-								&& order->GetDestination() == index) {
+								&& order->GetDestination() == vli.index) {
 							*list->Append() = v;
 							break;
 						}
@@ -100,31 +90,31 @@
 			}
 			break;
 
-		case VLW_SHARED_ORDERS:
+		case VL_SHARED_ORDERS:
 			/* Add all vehicles from this vehicle's shared order list */
-			v = Vehicle::GetIfValid(index);
-			if (v == NULL || v->type != type || !v->IsPrimaryVehicle()) return false;
+			v = Vehicle::GetIfValid(vli.index);
+			if (v == NULL || v->type != vli.type || !v->IsPrimaryVehicle()) return false;
 
 			for (; v != NULL; v = v->NextShared()) {
 				*list->Append() = v;
 			}
 			break;
 
-		case VLW_STANDARD:
+		case VL_STANDARD:
 			FOR_ALL_VEHICLES(v) {
-				if (v->type == type && v->owner == owner && v->IsPrimaryVehicle()) {
+				if (v->type == vli.type && v->owner == vli.company && v->IsPrimaryVehicle()) {
 					*list->Append() = v;
 				}
 			}
 			break;
 
-		case VLW_DEPOT_LIST:
+		case VL_DEPOT_LIST:
 			FOR_ALL_VEHICLES(v) {
-				if (v->type == type && v->IsPrimaryVehicle()) {
+				if (v->type == vli.type && v->IsPrimaryVehicle()) {
 					const Order *order;
 
 					FOR_VEHICLE_ORDERS(v, order) {
-						if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == index) {
+						if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index) {
 							*list->Append() = v;
 							break;
 						}
@@ -133,10 +123,10 @@
 			}
 			break;
 
-		case VLW_GROUP_LIST:
+		case VL_GROUP_LIST:
 			FOR_ALL_VEHICLES(v) {
-				if (v->type == type && v->IsPrimaryVehicle() &&
-						v->owner == owner && v->group_id == index) {
+				if (v->type == vli.type && v->IsPrimaryVehicle() &&
+						v->owner == vli.company && v->group_id == vli.index) {
 					*list->Append() = v;
 				}
 			}
--- a/src/vehiclelist.h
+++ b/src/vehiclelist.h
@@ -17,9 +17,37 @@
 #include "company_type.h"
 #include "tile_type.h"
 
+/** Vehicle List type flags */
+enum VehicleListType {
+	VL_STANDARD,
+	VL_SHARED_ORDERS,
+	VL_STATION_LIST,
+	VL_DEPOT_LIST,
+	VL_GROUP_LIST,
+	VLT_END
+};
+
+/** The information about a vehicle list. */
+struct VehicleListIdentifier {
+	VehicleListType type; ///< The type of vehicle list.
+	VehicleType vtype;    ///< The vehicle type associated with this list.
+	CompanyID company;    ///< The company associated with this list.
+	uint32 index;         ///< A vehicle list type specific index.
+
+	/**
+	 * Create a simple vehicle list.
+	 * @param type    List type.
+	 * @param vtype   Vehicle type associated with this list.
+	 * @param company Company associated with this list.
+	 * @param index   Optional type specific index.
+	 */
+	VehicleListIdentifier(VehicleListType type, VehicleType vtype, CompanyID company, uint index = 0) :
+		type(type), vtype(vtype), company(company), index(index) {}
+};
+
 typedef SmallVector<const Vehicle *, 32> VehicleList;
 
-bool GenerateVehicleSortList(VehicleList *list, VehicleType type, Owner owner, uint32 index, uint16 window_type);
+bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &identifier);
 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engine_list, VehicleList *wagon_list, bool individual_wagons = false);
 
 #endif /* VEHICLELIST_H */