src/vehiclelist.cpp
branchNewGRF_ports
changeset 10991 d8811e327d12
equal deleted inserted replaced
10731:67db0d431d5e 10991:d8811e327d12
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file vehicle.cpp Base implementations of all vehicles. */
       
     4 
       
     5 #include "stdafx.h"
       
     6 #include "openttd.h"
       
     7 #include "vehicle_type.h"
       
     8 #include "vehicle_func.h"
       
     9 #include "vehicle_base.h"
       
    10 #include "vehicle_gui.h"
       
    11 #include "core/alloc_func.hpp"
       
    12 #include "train.h"
       
    13 #include "vehiclelist.h"
       
    14 
       
    15 /**
       
    16  * Generate a list of vehicles inside a depot.
       
    17  * @param type    Type of vehicle
       
    18  * @param tile    The tile the depot is located on
       
    19  * @param engines Pointer to list to add vehicles to
       
    20  * @param wagons  Pointer to list to add wagons to (can be NULL)
       
    21  */
       
    22 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons)
       
    23 {
       
    24 	engines->Clear();
       
    25 	if (wagons != NULL && wagons != engines) wagons->Clear();
       
    26 
       
    27 	const Vehicle *v;
       
    28 	FOR_ALL_VEHICLES(v) {
       
    29 		/* General tests for all vehicle types */
       
    30 		if (v->type != type) continue;
       
    31 		if (v->tile != tile) continue;
       
    32 
       
    33 		switch (type) {
       
    34 			case VEH_TRAIN:
       
    35 				if (v->u.rail.track != TRACK_BIT_DEPOT) continue;
       
    36 				if (wagons != NULL && IsFreeWagon(v)) {
       
    37 					*wagons->Append() = v;
       
    38 					continue;
       
    39 				}
       
    40 				break;
       
    41 
       
    42 			default:
       
    43 				if (!v->IsInDepot()) continue;
       
    44 				break;
       
    45 		}
       
    46 
       
    47 		if (!v->IsPrimaryVehicle()) continue;
       
    48 
       
    49 		*engines->Append() = v;
       
    50 	}
       
    51 
       
    52 	/* Ensure the lists are not wasting too much space. If the lists are fresh
       
    53 	 * (i.e. built within a command) then this will actually do nothing. */
       
    54 	engines->Compact();
       
    55 	if (wagons != NULL && wagons != engines) wagons->Compact();
       
    56 }
       
    57 
       
    58 /**
       
    59  * Generate a list of vehicles based on window type.
       
    60  * @param list        Pointer to list to add vehicles to
       
    61  * @param type        Type of vehicle
       
    62  * @param owner       Player to generate list for
       
    63  * @param index       This parameter has different meanings depending on window_type
       
    64  *    <ul>
       
    65  *      <li>VLW_STATION_LIST:  index of station to generate a list for</li>
       
    66  *      <li>VLW_SHARED_ORDERS: index of order to generate a list for<li>
       
    67  *      <li>VLW_STANDARD: not used<li>
       
    68  *      <li>VLW_DEPOT_LIST: TileIndex of the depot/hangar to make the list for</li>
       
    69  *      <li>VLW_GROUP_LIST: index of group to generate a list for</li>
       
    70  *    </ul>
       
    71  * @param window_type The type of window the list is for, using the VLW_ flags in vehicle_gui.h
       
    72  */
       
    73 void GenerateVehicleSortList(VehicleList *list, VehicleType type, PlayerID owner, uint32 index, uint16 window_type)
       
    74 {
       
    75 	list->Clear();
       
    76 
       
    77 	const Vehicle *v;
       
    78 
       
    79 	switch (window_type) {
       
    80 		case VLW_STATION_LIST:
       
    81 			FOR_ALL_VEHICLES(v) {
       
    82 				if (v->type == type && v->IsPrimaryVehicle()) {
       
    83 					const Order *order;
       
    84 
       
    85 					FOR_VEHICLE_ORDERS(v, order) {
       
    86 						if (order->IsType(OT_GOTO_STATION) && order->GetDestination() == index) {
       
    87 							*list->Append() = v;
       
    88 							break;
       
    89 						}
       
    90 					}
       
    91 				}
       
    92 			}
       
    93 			break;
       
    94 
       
    95 		case VLW_SHARED_ORDERS:
       
    96 			FOR_ALL_VEHICLES(v) {
       
    97 				/* Find a vehicle with the order in question */
       
    98 				if (v->orders != NULL && v->orders->index == index) {
       
    99 					/* Add all vehicles from this vehicle's shared order list */
       
   100 					for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) {
       
   101 						*list->Append() = v;
       
   102 					}
       
   103 					break;
       
   104 				}
       
   105 			}
       
   106 			break;
       
   107 
       
   108 		case VLW_STANDARD:
       
   109 			FOR_ALL_VEHICLES(v) {
       
   110 				if (v->type == type && v->owner == owner && v->IsPrimaryVehicle()) {
       
   111 					*list->Append() = v;
       
   112 				}
       
   113 			}
       
   114 			break;
       
   115 
       
   116 		case VLW_DEPOT_LIST:
       
   117 			FOR_ALL_VEHICLES(v) {
       
   118 				if (v->type == type && v->IsPrimaryVehicle()) {
       
   119 					const Order *order;
       
   120 
       
   121 					FOR_VEHICLE_ORDERS(v, order) {
       
   122 						if (order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == index) {
       
   123 							*list->Append() = v;
       
   124 							break;
       
   125 						}
       
   126 					}
       
   127 				}
       
   128 			}
       
   129 			break;
       
   130 
       
   131 		case VLW_GROUP_LIST:
       
   132 			FOR_ALL_VEHICLES(v) {
       
   133 				if (v->type == type && v->IsPrimaryVehicle() &&
       
   134 						v->owner == owner && v->group_id == index) {
       
   135 					*list->Append() = v;
       
   136 				}
       
   137 			}
       
   138 			break;
       
   139 
       
   140 		default: NOT_REACHED(); break;
       
   141 	}
       
   142 
       
   143 	list->Compact();
       
   144 }