vehicle_gui.c
changeset 588 1b60458bdc29
parent 579 08ce4c50bd32
child 594 81c0d7edfb17
equal deleted inserted replaced
587:2fa4963eb92c 588:1b60458bdc29
     1 #include "stdafx.h"
     1 #include "stdafx.h"
     2 #include "ttd.h"
     2 #include "ttd.h"
     3 #include "table/strings.h"
     3 #include "table/strings.h"
     4 #include "vehicle.h"
     4 #include "vehicle.h"
       
     5 #include "window.h"
     5 
     6 
     6 VehicleSortListingTypeFunctions * const _vehicle_sorter[] = {
     7 VehicleSortListingTypeFunctions * const _vehicle_sorter[] = {
     7 	&VehicleUnsortedSorter,
     8 	&VehicleUnsortedSorter,
     8 	&VehicleNumberSorter,
     9 	&VehicleNumberSorter,
     9 	&VehicleNameSorter,
    10 	&VehicleNameSorter,
    26 	STR_SORT_BY_RELIABILITY,
    27 	STR_SORT_BY_RELIABILITY,
    27 	STR_SORT_BY_MAX_SPEED,
    28 	STR_SORT_BY_MAX_SPEED,
    28 	INVALID_STRING_ID
    29 	INVALID_STRING_ID
    29 };
    30 };
    30 
    31 
       
    32 void RebuildVehicleLists(void)
       
    33 {
       
    34 	Window *w;
       
    35 
       
    36 	for (w = _windows; w != _last_window; ++w)
       
    37 		switch (w->window_class) {
       
    38 			case WC_TRAINS_LIST:
       
    39 			case WC_ROADVEH_LIST:
       
    40 			case WC_SHIPS_LIST:
       
    41 			case WC_AIRCRAFT_LIST:
       
    42 				WP(w, vehiclelist_d).flags |= VL_REBUILD;
       
    43 				SetWindowDirty(w);
       
    44 				break;
       
    45 
       
    46 			default:
       
    47 				break;
       
    48 		}
       
    49 }
       
    50 
       
    51 void ResortVehicleLists(void)
       
    52 {
       
    53 	Window *w;
       
    54 
       
    55 	for (w = _windows; w != _last_window; ++w)
       
    56 		switch (w->window_class) {
       
    57 			case WC_TRAINS_LIST:
       
    58 			case WC_ROADVEH_LIST:
       
    59 			case WC_SHIPS_LIST:
       
    60 			case WC_AIRCRAFT_LIST:
       
    61 				WP(w, vehiclelist_d).flags |= VL_RESORT;
       
    62 				SetWindowDirty(w);
       
    63 				break;
       
    64 
       
    65 			default:
       
    66 				break;
       
    67 		}
       
    68 }
       
    69 
       
    70 void BuildVehicleList(vehiclelist_d *vl, int type, int owner, int station)
       
    71 {
       
    72 	SortStruct sort_list[NUM_NORMAL_VEHICLES];
       
    73 	int subtype = (type != VEH_Aircraft) ? 0 : 2;
       
    74 	int n = 0;
       
    75 	int i;
       
    76 
       
    77 	if (!(vl->flags & VL_REBUILD)) return;
       
    78 
       
    79 	DEBUG(misc, 1) ("Building vehicle list for player %d station %d...",
       
    80 		owner, station);	
       
    81 
       
    82 	if (station != -1) {
       
    83 		const Vehicle *v;
       
    84 		FOR_ALL_VEHICLES(v) {
       
    85 			if (v->type == type && v->subtype <= subtype) {
       
    86 				const Order *ord;
       
    87 				for (ord = v->schedule_ptr; ord->type != OT_NOTHING; ++ord)
       
    88 					if (ord->type == OT_GOTO_STATION && ord->station == station) {
       
    89 						sort_list[n].index = v - _vehicles;
       
    90 						sort_list[n].owner = v->owner;
       
    91 						++n;
       
    92 						break;
       
    93 					}
       
    94 			}
       
    95 		}
       
    96 	} else {
       
    97 		const Vehicle *v;
       
    98 		FOR_ALL_VEHICLES(v) {
       
    99 			if (v->type == type && v->subtype <= subtype && v->owner == owner) {
       
   100 				sort_list[n].index = v - _vehicles;
       
   101 				sort_list[n].owner = v->owner;
       
   102 				++n;
       
   103 			}
       
   104 		}
       
   105 	}
       
   106 
       
   107 	vl->sort_list = realloc(vl->sort_list, n * sizeof(vl->sort_list[0])); /* XXX unchecked malloc */
       
   108 	vl->list_length = n;
       
   109 
       
   110 	for (i = 0; i < n; ++i)
       
   111 		vl->sort_list[i] = sort_list[i];
       
   112 
       
   113 	vl->flags &= ~VL_REBUILD;
       
   114 	vl->flags |= VL_RESORT;
       
   115 }
       
   116 
       
   117 void SortVehicleList(vehiclelist_d *vl)
       
   118 {
       
   119 	if (!(vl->flags & VL_RESORT)) return;
       
   120 
       
   121 	_internal_sort_order = vl->flags & VL_DESC;
       
   122 	_internal_name_sorter_id = STR_SV_TRAIN_NAME;
       
   123 	_last_vehicle_idx = 0; // used for "cache" in namesorting
       
   124 	qsort(vl->sort_list, vl->list_length, sizeof(vl->sort_list[0]),
       
   125 		_vehicle_sorter[vl->sort_type]);
       
   126 
       
   127 	vl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS;
       
   128 	vl->flags &= ~VL_RESORT;
       
   129 }
       
   130 
       
   131 
    31 /* General Vehicle GUI based procedures that are independent of vehicle types */
   132 /* General Vehicle GUI based procedures that are independent of vehicle types */
    32 void InitializeVehiclesGuiList()
   133 void InitializeVehiclesGuiList()
    33 {
   134 {
    34 	memset(_train_sort_dirty, true, sizeof(_train_sort_dirty));
       
    35 	memset(_aircraft_sort_dirty, true, sizeof(_aircraft_sort_dirty));
       
    36 	memset(_ship_sort_dirty, true, sizeof(_ship_sort_dirty));
       
    37 	memset(_road_sort_dirty, true, sizeof(_road_sort_dirty));
       
    38 	memset(_vehicle_sort_dirty, true, sizeof(_vehicle_sort_dirty));
       
    39 }
   135 }
    40 
   136 
    41 // draw the vehicle profit button in the vehicle list window.
   137 // draw the vehicle profit button in the vehicle list window.
    42 void DrawVehicleProfitButton(Vehicle *v, int x, int y)
   138 void DrawVehicleProfitButton(Vehicle *v, int x, int y)
    43 {
   139 {