866 |
866 |
867 // used to get a sorted list of the vehicles |
867 // used to get a sorted list of the vehicles |
868 static SortStruct _aircraft_sort[NUM_NORMAL_VEHICLES]; |
868 static SortStruct _aircraft_sort[NUM_NORMAL_VEHICLES]; |
869 static uint16 _num_aircraft_sort[MAX_PLAYERS]; |
869 static uint16 _num_aircraft_sort[MAX_PLAYERS]; |
870 |
870 |
|
871 static void GlobalSortAircraftList() |
|
872 { |
|
873 const Vehicle *v; |
|
874 uint16 *i; |
|
875 uint32 n = 0; |
|
876 |
|
877 // reset #-of aircraft to 0 because ++ is used for value-assignment |
|
878 for (i = _num_aircraft_sort; i != endof(_num_aircraft_sort); i++) {*i = 0;} |
|
879 |
|
880 FOR_ALL_VEHICLES(v) { |
|
881 if(v->type == VEH_Aircraft && v->subtype <= 2) { |
|
882 _aircraft_sort[n].index = v->index; |
|
883 _aircraft_sort[n++].owner = v->owner; |
|
884 _num_aircraft_sort[v->owner]++; // add number of aircraft of player |
|
885 } |
|
886 } |
|
887 |
|
888 // create cumulative aircraft-ownership |
|
889 // aircraft are stored as a cummulative index, eg 25, 41, 43. This means |
|
890 // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 |
|
891 for (i = &_num_aircraft_sort[1]; i != endof(_num_aircraft_sort); i++) {*i += *(i-1);} |
|
892 |
|
893 qsort(_aircraft_sort, n, sizeof(_aircraft_sort[0]), GeneralOwnerSorter); // sort by owner |
|
894 |
|
895 // since indexes are messed up after adding/removing a station, mark all lists dirty |
|
896 memset(_aircraft_sort_dirty, true, sizeof(_aircraft_sort_dirty)); |
|
897 _vehicle_sort_dirty[VEHAIRCRAFT] = false; |
|
898 |
|
899 DEBUG(misc, 1) ("Resorting global aircraft list..."); |
|
900 } |
|
901 |
871 static void MakeSortedAircraftList(byte owner) |
902 static void MakeSortedAircraftList(byte owner) |
872 { |
903 { |
873 SortStruct *firstelement; |
904 SortStruct *firstelement; |
874 Vehicle *v; |
|
875 uint32 n = 0; |
905 uint32 n = 0; |
876 uint16 *i; |
|
877 |
|
878 if (_vehicle_sort_dirty[VEHAIRCRAFT]) { // only resort the whole array if vehicles have been added/removed |
|
879 // reset to 0 just to be sure |
|
880 for (i = _num_aircraft_sort; i != endof(_num_aircraft_sort); i++) {*i = 0;} |
|
881 |
|
882 FOR_ALL_VEHICLES(v) { |
|
883 if(v->type == VEH_Aircraft && v->subtype <= 2) { |
|
884 _aircraft_sort[n].index = v->index; |
|
885 _aircraft_sort[n++].owner = v->owner; |
|
886 _num_aircraft_sort[v->owner]++; // add number of aircraft of player |
|
887 } |
|
888 } |
|
889 |
|
890 // create cumulative aircraft-ownage |
|
891 // aircraft are stored as a cummulative index, eg 25, 41, 43. This means |
|
892 // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 |
|
893 for (i = &_num_aircraft_sort[1]; i != endof(_num_aircraft_sort); i++) {*i += *(i-1);} |
|
894 |
|
895 // sort by owner, then only subsort the requested owner-vehicles |
|
896 qsort(_aircraft_sort, n, sizeof(_aircraft_sort[0]), GeneralOwnerSorter); |
|
897 |
|
898 _last_vehicle_idx = 0; // used for "cache" in namesorting |
|
899 _vehicle_sort_dirty[VEHAIRCRAFT] = false; |
|
900 } |
|
901 |
906 |
902 if (owner == 0) { // first element starts at 0th element and has n elements as described above |
907 if (owner == 0) { // first element starts at 0th element and has n elements as described above |
903 firstelement = &_aircraft_sort[0]; |
908 firstelement = &_aircraft_sort[0]; |
904 n = _num_aircraft_sort[0]; |
909 n = _num_aircraft_sort[0]; |
905 } else { // nth element starts at the end of the previous one, and has n elements as described above |
910 } else { // nth element starts at the end of the previous one, and has n elements as described above |
907 n = _num_aircraft_sort[owner] - _num_aircraft_sort[owner-1]; |
912 n = _num_aircraft_sort[owner] - _num_aircraft_sort[owner-1]; |
908 } |
913 } |
909 |
914 |
910 _internal_sort_order = _aircraft_sort_order[owner]; |
915 _internal_sort_order = _aircraft_sort_order[owner]; |
911 _internal_name_sorter_id = STR_SV_AIRCRAFT_NAME; |
916 _internal_name_sorter_id = STR_SV_AIRCRAFT_NAME; |
|
917 _last_vehicle_idx = 0; // used for "cache" in namesorting |
912 qsort(firstelement, n, sizeof(_aircraft_sort[0]), _vehicle_sorter[_aircraft_sort_type[owner]]); |
918 qsort(firstelement, n, sizeof(_aircraft_sort[0]), _vehicle_sorter[_aircraft_sort_type[owner]]); |
|
919 |
|
920 _aircraft_sort_dirty[owner] = false; |
913 |
921 |
914 DEBUG(misc, 1) ("Resorting Aircraft list player %d...", owner+1); |
922 DEBUG(misc, 1) ("Resorting Aircraft list player %d...", owner+1); |
915 } |
923 } |
916 |
924 |
917 static void PlayerAircraftWndProc(Window *w, WindowEvent *e) |
925 static void PlayerAircraftWndProc(Window *w, WindowEvent *e) |
922 const byte window_number = (byte)w->window_number; |
930 const byte window_number = (byte)w->window_number; |
923 |
931 |
924 if (_aircraft_sort_type[window_number] == SORT_BY_UNSORTED) // disable 'Sort By' tooltip on Unsorted sorting criteria |
932 if (_aircraft_sort_type[window_number] == SORT_BY_UNSORTED) // disable 'Sort By' tooltip on Unsorted sorting criteria |
925 w->disabled_state |= (1 << 2); |
933 w->disabled_state |= (1 << 2); |
926 |
934 |
927 if (_aircraft_sort_dirty[window_number] || _vehicle_sort_dirty[VEHAIRCRAFT]) { |
935 // resort shipps window if roadvehicles have been added/removed |
928 _aircraft_sort_dirty[window_number] = false; |
936 if (_vehicle_sort_dirty[VEHAIRCRAFT]) |
|
937 GlobalSortAircraftList(); |
|
938 |
|
939 if (_aircraft_sort_dirty[window_number]) { |
929 MakeSortedAircraftList(window_number); |
940 MakeSortedAircraftList(window_number); |
930 /* reset sorting timeout */ |
941 /* reset sorting timeout */ |
931 w->custom[0] = DAY_TICKS; |
942 w->custom[0] = DAY_TICKS; |
932 w->custom[1] = PERIODIC_RESORT_DAYS; |
943 w->custom[1] = PERIODIC_RESORT_DAYS; |
933 } |
944 } |