diff options
-rw-r--r-- | aircraft_gui.c | 59 | ||||
-rw-r--r-- | roadveh_gui.c | 61 | ||||
-rw-r--r-- | ship_gui.c | 61 | ||||
-rw-r--r-- | station.h | 3 | ||||
-rw-r--r-- | station_cmd.c | 18 | ||||
-rw-r--r-- | station_gui.c | 39 | ||||
-rw-r--r-- | train_gui.c | 59 | ||||
-rw-r--r-- | vehicle_gui.c | 46 |
8 files changed, 197 insertions, 149 deletions
diff --git a/aircraft_gui.c b/aircraft_gui.c index ce438ab5a..371ef4f10 100644 --- a/aircraft_gui.c +++ b/aircraft_gui.c @@ -868,36 +868,41 @@ static void DrawSmallSchedule(Vehicle *v, int x, int y) { static SortStruct _aircraft_sort[NUM_NORMAL_VEHICLES]; static uint16 _num_aircraft_sort[MAX_PLAYERS]; -static void MakeSortedAircraftList(byte owner) +static void GlobalSortAircraftList() { - SortStruct *firstelement; - Vehicle *v; - uint32 n = 0; + const Vehicle *v; uint16 *i; + uint32 n = 0; - if (_vehicle_sort_dirty[VEHAIRCRAFT]) { // only resort the whole array if vehicles have been added/removed - // reset to 0 just to be sure - for (i = _num_aircraft_sort; i != endof(_num_aircraft_sort); i++) {*i = 0;} + // reset #-of aircraft to 0 because ++ is used for value-assignment + for (i = _num_aircraft_sort; i != endof(_num_aircraft_sort); i++) {*i = 0;} - FOR_ALL_VEHICLES(v) { - if(v->type == VEH_Aircraft && v->subtype <= 2) { - _aircraft_sort[n].index = v->index; - _aircraft_sort[n++].owner = v->owner; - _num_aircraft_sort[v->owner]++; // add number of aircraft of player - } + FOR_ALL_VEHICLES(v) { + if(v->type == VEH_Aircraft && v->subtype <= 2) { + _aircraft_sort[n].index = v->index; + _aircraft_sort[n++].owner = v->owner; + _num_aircraft_sort[v->owner]++; // add number of aircraft of player } + } - // create cumulative aircraft-ownage - // aircraft are stored as a cummulative index, eg 25, 41, 43. This means - // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 - for (i = &_num_aircraft_sort[1]; i != endof(_num_aircraft_sort); i++) {*i += *(i-1);} + // create cumulative aircraft-ownership + // aircraft are stored as a cummulative index, eg 25, 41, 43. This means + // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 + for (i = &_num_aircraft_sort[1]; i != endof(_num_aircraft_sort); i++) {*i += *(i-1);} - // sort by owner, then only subsort the requested owner-vehicles - qsort(_aircraft_sort, n, sizeof(_aircraft_sort[0]), GeneralOwnerSorter); + qsort(_aircraft_sort, n, sizeof(_aircraft_sort[0]), GeneralOwnerSorter); // sort by owner - _last_vehicle_idx = 0; // used for "cache" in namesorting - _vehicle_sort_dirty[VEHAIRCRAFT] = false; - } + // since indexes are messed up after adding/removing a station, mark all lists dirty + memset(_aircraft_sort_dirty, true, sizeof(_aircraft_sort_dirty)); + _vehicle_sort_dirty[VEHAIRCRAFT] = false; + + DEBUG(misc, 1) ("Resorting global aircraft list..."); +} + +static void MakeSortedAircraftList(byte owner) +{ + SortStruct *firstelement; + uint32 n = 0; if (owner == 0) { // first element starts at 0th element and has n elements as described above firstelement = &_aircraft_sort[0]; @@ -909,8 +914,11 @@ static void MakeSortedAircraftList(byte owner) _internal_sort_order = _aircraft_sort_order[owner]; _internal_name_sorter_id = STR_SV_AIRCRAFT_NAME; + _last_vehicle_idx = 0; // used for "cache" in namesorting qsort(firstelement, n, sizeof(_aircraft_sort[0]), _vehicle_sorter[_aircraft_sort_type[owner]]); + _aircraft_sort_dirty[owner] = false; + DEBUG(misc, 1) ("Resorting Aircraft list player %d...", owner+1); } @@ -924,8 +932,11 @@ static void PlayerAircraftWndProc(Window *w, WindowEvent *e) if (_aircraft_sort_type[window_number] == SORT_BY_UNSORTED) // disable 'Sort By' tooltip on Unsorted sorting criteria w->disabled_state |= (1 << 2); - if (_aircraft_sort_dirty[window_number] || _vehicle_sort_dirty[VEHAIRCRAFT]) { - _aircraft_sort_dirty[window_number] = false; + // resort shipps window if roadvehicles have been added/removed + if (_vehicle_sort_dirty[VEHAIRCRAFT]) + GlobalSortAircraftList(); + + if (_aircraft_sort_dirty[window_number]) { MakeSortedAircraftList(window_number); /* reset sorting timeout */ w->custom[0] = DAY_TICKS; diff --git a/roadveh_gui.c b/roadveh_gui.c index 3ac65a3e3..7451746b5 100644 --- a/roadveh_gui.c +++ b/roadveh_gui.c @@ -707,36 +707,41 @@ void ShowRoadDepotWindow(uint tile) static SortStruct _road_sort[NUM_NORMAL_VEHICLES]; static uint16 _num_road_sort[MAX_PLAYERS]; -static void MakeSortedRoadList(byte owner) +static void GlobalSortRoadVehList() { - SortStruct *firstelement; - Vehicle *v; - uint32 n = 0; + const Vehicle *v; uint16 *i; + uint32 n = 0; - if (_vehicle_sort_dirty[VEHROAD]) { // only resort the whole array if vehicles have been added/removed - // reset to 0 just to be sure - for (i = _num_road_sort; i != endof(_num_road_sort); i++) {*i = 0;} + // reset #-of roadvehicles to 0 because ++ is used for value-assignment + for (i = _num_road_sort; i != endof(_num_road_sort); i++) {*i = 0;} - FOR_ALL_VEHICLES(v) { - if(v->type == VEH_Road) { - _road_sort[n].index = v->index; - _road_sort[n++].owner = v->owner; - _num_road_sort[v->owner]++; // add number of roads of player - } + FOR_ALL_VEHICLES(v) { + if(v->type == VEH_Road) { + _road_sort[n].index = v->index; + _road_sort[n++].owner = v->owner; + _num_road_sort[v->owner]++; // add number of roadvehicless of player } + } - // create cumulative road-ownage - // roads are stored as a cummulative index, eg 25, 41, 43. This means - // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 - for (i = &_num_road_sort[1]; i != endof(_num_road_sort); i++) {*i += *(i-1);} + // create cumulative roadvehicle-ownership + // roads are stored as a cummulative index, eg 25, 41, 43. This means + // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 + for (i = &_num_road_sort[1]; i != endof(_num_road_sort); i++) {*i += *(i-1);} - // sort by owner, then only subsort the requested owner-vehicles - qsort(_road_sort, n, sizeof(_road_sort[0]), GeneralOwnerSorter); + qsort(_road_sort, n, sizeof(_road_sort[0]), GeneralOwnerSorter); // sort by owner - _last_vehicle_idx = 0; // used for "cache" in namesorting - _vehicle_sort_dirty[VEHROAD] = false; - } + // since indexes are messed up after adding/removing a station, mark all lists dirty + memset(_road_sort_dirty, true, sizeof(_road_sort_dirty)); + _vehicle_sort_dirty[VEHROAD] = false; + + DEBUG(misc, 1) ("Resorting global roadvehicles list..."); +} + +static void MakeSortedRoadVehList(byte owner) +{ + SortStruct *firstelement; + uint32 n = 0; if (owner == 0) { // first element starts at 0th element and has n elements as described above firstelement = &_road_sort[0]; @@ -748,8 +753,11 @@ static void MakeSortedRoadList(byte owner) _internal_sort_order = _road_sort_order[owner]; _internal_name_sorter_id = STR_SV_ROADVEH_NAME; + _last_vehicle_idx = 0; // used for "cache" in namesorting qsort(firstelement, n, sizeof(_road_sort[0]), _vehicle_sorter[_road_sort_type[owner]]); + _road_sort_dirty[owner] = false; + DEBUG(misc, 1) ("Resorting Roadvehicles list player %d...", owner+1); } @@ -763,9 +771,12 @@ static void PlayerRoadVehWndProc(Window *w, WindowEvent *e) if (_road_sort_type[window_number] == SORT_BY_UNSORTED) // disable 'Sort By' tooltip on Unsorted sorting criteria w->disabled_state |= (1 << 2); - if (_road_sort_dirty[window_number] || _vehicle_sort_dirty[VEHROAD]) { - _road_sort_dirty[window_number] = false; - MakeSortedRoadList(window_number); + // resort roadvehicles window if roadvehicles have been added/removed + if (_vehicle_sort_dirty[VEHROAD]) + GlobalSortRoadVehList(); + + if (_road_sort_dirty[window_number]) { + MakeSortedRoadVehList(window_number); /* reset sorting timeout */ w->custom[0] = DAY_TICKS; w->custom[1] = PERIODIC_RESORT_DAYS; diff --git a/ship_gui.c b/ship_gui.c index 6862f3024..23a796d0d 100644 --- a/ship_gui.c +++ b/ship_gui.c @@ -871,36 +871,41 @@ static void DrawSmallShipSchedule(Vehicle *v, int x, int y) { static SortStruct _ship_sort[NUM_NORMAL_VEHICLES]; static uint16 _num_ship_sort[MAX_PLAYERS]; -static void MakeSortedShiptList(byte owner) +static void GlobalSortShipList() { - SortStruct *firstelement; - Vehicle *v; - uint32 n = 0; + const Vehicle *v; uint16 *i; + uint32 n = 0; - if (_vehicle_sort_dirty[VEHSHIP]) { // only resort the whole array if vehicles have been added/removed - // reset to 0 just to be sure - for (i = _num_ship_sort; i != endof(_num_ship_sort); i++) {*i = 0;} + // reset #-of ships to 0 because ++ is used for value-assignment + for (i = _num_ship_sort; i != endof(_num_ship_sort); i++) {*i = 0;} - FOR_ALL_VEHICLES(v) { - if(v->type == VEH_Ship) { - _ship_sort[n].index = v->index; - _ship_sort[n++].owner = v->owner; - _num_ship_sort[v->owner]++; // add number of trains of player - } + FOR_ALL_VEHICLES(v) { + if(v->type == VEH_Ship) { + _ship_sort[n].index = v->index; + _ship_sort[n++].owner = v->owner; + _num_ship_sort[v->owner]++; // add number of ships of player } + } - // create cumulative ship-ownage - // ships are stored as a cummulative index, eg 25, 41, 43. This means - // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 - for (i = &_num_ship_sort[1]; i != endof(_num_ship_sort); i++) {*i += *(i-1);} + // create cumulative ship-ownership + // ships are stored as a cummulative index, eg 25, 41, 43. This means + // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 + for (i = &_num_ship_sort[1]; i != endof(_num_ship_sort); i++) {*i += *(i-1);} - // sort by owner, then only subsort the requested owner-vehicles - qsort(_ship_sort, n, sizeof(_ship_sort[0]), GeneralOwnerSorter); + qsort(_ship_sort, n, sizeof(_ship_sort[0]), GeneralOwnerSorter); // sort by owner - _last_vehicle_idx = 0; // used for "cache" in namesorting - _vehicle_sort_dirty[VEHSHIP] = false; - } + // since indexes are messed up after adding/removing a station, mark all lists dirty + memset(_ship_sort_dirty, true, sizeof(_ship_sort_dirty)); + _vehicle_sort_dirty[VEHSHIP] = false; + + DEBUG(misc, 1) ("Resorting global ships list..."); +} + +static void MakeSortedShipList(byte owner) +{ + SortStruct *firstelement; + uint32 n = 0; if (owner == 0) { // first element starts at 0th element and has n elements as described above firstelement = &_ship_sort[0]; @@ -912,8 +917,11 @@ static void MakeSortedShiptList(byte owner) _internal_sort_order = _ship_sort_order[owner]; _internal_name_sorter_id = STR_SV_SHIP_NAME; + _last_vehicle_idx = 0; // used for "cache" in namesorting qsort(firstelement, n, sizeof(_ship_sort[0]), _vehicle_sorter[_ship_sort_type[owner]]); + _ship_sort_dirty[owner] = false; + DEBUG(misc, 1) ("Resorting Ships list player %d...", owner+1); } @@ -927,9 +935,12 @@ static void PlayerShipsWndProc(Window *w, WindowEvent *e) if (_ship_sort_type[window_number] == SORT_BY_UNSORTED) // disable 'Sort By' tooltip on Unsorted sorting criteria w->disabled_state |= (1 << 2); - if (_ship_sort_dirty[window_number] || _vehicle_sort_dirty[VEHSHIP]) { - _ship_sort_dirty[window_number] = false; - MakeSortedShiptList(window_number); + // resort shipps window if roadvehicles have been added/removed + if (_vehicle_sort_dirty[VEHSHIP]) + GlobalSortShipList(); + + if (_ship_sort_dirty[window_number]) { + MakeSortedShipList(window_number); /* reset sorting timeout */ w->custom[0] = DAY_TICKS; w->custom[1] = PERIODIC_RESORT_DAYS; @@ -79,7 +79,8 @@ void ShowStationViewWindow(int station); void UpdateAllStationVirtCoord(); VARDEF Station _stations[250]; -VARDEF bool _station_sort_dirty; +VARDEF bool _station_sort_dirty[MAX_PLAYERS]; +VARDEF bool _global_station_sort_dirty; #define DEREF_STATION(i) (&_stations[i]) #define FOR_ALL_STATIONS(st) for(st=_stations; st != endof(_stations); st++) diff --git a/station_cmd.c b/station_cmd.c index 9f56d7836..921c3bf42 100644 --- a/station_cmd.c +++ b/station_cmd.c @@ -313,7 +313,8 @@ static void StationInitialize(Station *st, TileIndex tile) ge->last_speed = 0; ge->last_age = 0xFF; } - _station_sort_dirty = true; + + _global_station_sort_dirty = true; // build a new station } // Update the virtual coords needed to draw the station sign. @@ -2026,7 +2027,7 @@ static void DeleteStation(Station *st) DeleteName(st->string_id); MarkStationDirty(st); - _station_sort_dirty = true; + _global_station_sort_dirty = true; // delete station, remove sign InvalidateWindowClasses(WC_STATION_LIST); index = st->index; @@ -2247,7 +2248,7 @@ int32 CmdRenameStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) st->string_id = str; UpdateStationVirtCoord(st); DeleteName(old_str); - _station_sort_dirty = true; + _station_sort_dirty[st->owner] = true; // rename a station MarkWholeScreenDirty(); } else { DeleteName(str); @@ -2426,7 +2427,7 @@ static void ChangeTileOwner_Station(uint tile, byte old_player, byte new_player) Station *st = DEREF_STATION(_map2[tile]); _map_owner[tile] = new_player; st->owner = new_player; - _station_sort_dirty = true; + _global_station_sort_dirty = true; // transfer ownership of station to another player InvalidateWindowClasses(WC_STATION_LIST); } else { DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); @@ -2476,11 +2477,16 @@ static int32 ClearTile_Station(uint tile, byte flags) { void InitializeStations() { int i; + memset(_stations, 0, sizeof(_stations)); - for(i=0;i!=lengthof(_stations); i++) + for(i = 0; i != lengthof(_stations); i++) _stations[i].index=i; + _station_tick_ctr = 0; - _station_sort_dirty = true; // set stations to be sorted on first load + + // set stations to be sorted on load of savegame + memset(_station_sort_dirty, true, sizeof(_station_sort_dirty)); + _global_station_sort_dirty = true; // load of savegame } diff --git a/station_gui.c b/station_gui.c index b1c553a30..c0fca5411 100644 --- a/station_gui.c +++ b/station_gui.c @@ -73,13 +73,13 @@ static int CDECL StationNameSorter(const void *a, const void *b) return strcmp(buf1, _bufcache); // sort by name } -static void MakeSortedStationList(byte owner) +static void GlobalSortStationList() { - SortStruct *firstelement; - Station *st; + const Station *st; uint32 n = 0; uint16 *i; - // reset to 0 just to be sure + + // reset #-of stations to 0 because ++ is used for value-assignment for (i = _num_station_sort; i != endof(_num_station_sort); i++) {*i = 0;} FOR_ALL_STATIONS(st) { @@ -90,15 +90,24 @@ static void MakeSortedStationList(byte owner) } } - // create cumulative station-ownage + // create cumulative station-ownership // stations are stored as a cummulative index, eg 25, 41, 43. This means // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 for (i = &_num_station_sort[1]; i != endof(_num_station_sort); i++) {*i += *(i-1);} - _last_station_idx = 0; // used for "cache" + qsort(_station_sort, n, sizeof(_station_sort[0]), GeneralOwnerSorter); // sort by owner - // sort by owner, then only subsort the requested owner-vehicles - qsort(_station_sort, n, sizeof(_station_sort[0]), GeneralOwnerSorter); + // since indexes are messed up after adding/removing a station, mark all lists dirty + memset(_station_sort_dirty, true, sizeof(_station_sort_dirty)); + _global_station_sort_dirty = false; + + DEBUG(misc, 1) ("Resorting global station list..."); +} + +static void MakeSortedStationList(byte owner) +{ + SortStruct *firstelement; + uint32 n = 0; if (owner == 0) { // first element starts at 0th element and has n elements as described above firstelement = &_station_sort[0]; @@ -108,9 +117,12 @@ static void MakeSortedStationList(byte owner) n = _num_station_sort[owner] - _num_station_sort[owner-1]; } - qsort(firstelement, n, sizeof(_station_sort[0]), StationNameSorter); + _last_station_idx = 0; // used for "cache" in namesorting + qsort(firstelement, n, sizeof(_station_sort[0]), StationNameSorter); // sort by name + + _station_sort_dirty[owner] = false; - DEBUG(misc, 1) ("Resorting Stations list..."); + DEBUG(misc, 1) ("Resorting Stations list player %d...", owner+1); } static void PlayerStationsWndProc(Window *w, WindowEvent *e) @@ -120,8 +132,11 @@ static void PlayerStationsWndProc(Window *w, WindowEvent *e) uint32 i; const byte window_number = (byte)w->window_number; - if (_station_sort_dirty) { - _station_sort_dirty = false; + // resort station window if stations have been added/removed + if (_global_station_sort_dirty) + GlobalSortStationList(); + + if (_station_sort_dirty[window_number]) { // resort in case of a station rename. MakeSortedStationList(window_number); } diff --git a/train_gui.c b/train_gui.c index e51d20dd0..f960ba91d 100644 --- a/train_gui.c +++ b/train_gui.c @@ -1140,36 +1140,41 @@ void ShowTrainDetailsWindow(Vehicle *v) static SortStruct _train_sort[NUM_NORMAL_VEHICLES]; static uint16 _num_train_sort[MAX_PLAYERS]; -static void MakeSortedTrainList(byte owner) +static void GlobalSortTrainList() { - SortStruct *firstelement; - Vehicle *v; - uint32 n = 0; + const Vehicle *v; uint16 *i; + uint32 n = 0; - if (_vehicle_sort_dirty[VEHTRAIN]) { // only resort the whole array if vehicles have been added/removed - // reset to 0 just to be sure - for (i = _num_train_sort; i != endof(_num_train_sort); i++) {*i = 0;} + // reset #-of trains to 0 because ++ is used for value-assignment + for (i = _num_train_sort; i != endof(_num_train_sort); i++) {*i = 0;} - FOR_ALL_VEHICLES(v) { - if(v->type == VEH_Train && v->subtype == 0) { - _train_sort[n].index = v->index; - _train_sort[n++].owner = v->owner; - _num_train_sort[v->owner]++; // add number of trains of player - } + FOR_ALL_VEHICLES(v) { + if(v->type == VEH_Train && v->subtype == 0) { + _train_sort[n].index = v->index; + _train_sort[n++].owner = v->owner; + _num_train_sort[v->owner]++; // add number of trains of player } + } - // create cumulative train-ownage - // trains are stored as a cummulative index, eg 25, 41, 43. This means - // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 - for (i = &_num_train_sort[1]; i != endof(_num_train_sort); i++) {*i += *(i-1);} + // create cumulative train-ownership + // trains are stored as a cummulative index, eg 25, 41, 43. This means + // Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 + for (i = &_num_train_sort[1]; i != endof(_num_train_sort); i++) {*i += *(i-1);} - // sort by owner, then only subsort the requested owner-vehicles - qsort(_train_sort, n, sizeof(_train_sort[0]), GeneralOwnerSorter); + qsort(_train_sort, n, sizeof(_train_sort[0]), GeneralOwnerSorter); // sort by owner - _last_vehicle_idx = 0; // used for "cache" in namesorting - _vehicle_sort_dirty[VEHTRAIN] = false; - } + // since indexes are messed up after adding/removing a station, mark all lists dirty + memset(_train_sort_dirty, true, sizeof(_train_sort_dirty)); + _vehicle_sort_dirty[VEHTRAIN] = false; + + DEBUG(misc, 1) ("Resorting global trains list..."); +} + +static void MakeSortedTrainList(byte owner) +{ + SortStruct *firstelement; + uint32 n = 0; if (owner == 0) { // first element starts at 0th element and has n elements as described above firstelement = &_train_sort[0]; @@ -1181,8 +1186,11 @@ static void MakeSortedTrainList(byte owner) _internal_sort_order = _train_sort_order[owner]; _internal_name_sorter_id = STR_SV_TRAIN_NAME; + _last_vehicle_idx = 0; // used for "cache" in namesorting qsort(firstelement, n, sizeof(_train_sort[0]), _vehicle_sorter[_train_sort_type[owner]]); + _train_sort_dirty[owner] = false; + DEBUG(misc, 1) ("Resorting Trains list player %d...", owner+1); } @@ -1196,8 +1204,11 @@ static void PlayerTrainsWndProc(Window *w, WindowEvent *e) if (_train_sort_type[window_number] == SORT_BY_UNSORTED) // disable 'Sort By' tooltip on Unsorted sorting criteria w->disabled_state |= (1 << 2); - if (_train_sort_dirty[window_number] || _vehicle_sort_dirty[VEHTRAIN]) { - _train_sort_dirty[window_number] = false; + // resort trains window if roadvehicles have been added/removed + if (_vehicle_sort_dirty[VEHTRAIN]) + GlobalSortTrainList(); + + if (_train_sort_dirty[window_number]) { MakeSortedTrainList(window_number); /* reset sorting timeout */ w->custom[0] = DAY_TICKS; diff --git a/vehicle_gui.c b/vehicle_gui.c index ed5fc1666..5d0536d76 100644 --- a/vehicle_gui.c +++ b/vehicle_gui.c @@ -6,23 +6,11 @@ /* General Vehicle GUI based procedures that are independent of vehicle types */ void InitializeVehiclesGuiList() { - bool *i; - for (i = _train_sort_dirty; i != endof(_train_sort_dirty); i++) - *i = true; - - for (i = _aircraft_sort_dirty; i != endof(_aircraft_sort_dirty); i++) - *i = true; - - for (i = _ship_sort_dirty; i != endof(_ship_sort_dirty); i++) - *i = true; - - for (i = _road_sort_dirty; i != endof(_road_sort_dirty); i++) - *i = true; - - for (i = _vehicle_sort_dirty; i != endof(_vehicle_sort_dirty); i++) - *i = true; - - //memset(_train_sort_dirty, true, sizeof(_train_sort_dirty)); + memset(_train_sort_dirty, true, sizeof(_train_sort_dirty)); + memset(_aircraft_sort_dirty, true, sizeof(_aircraft_sort_dirty)); + memset(_ship_sort_dirty, true, sizeof(_ship_sort_dirty)); + memset(_road_sort_dirty, true, sizeof(_road_sort_dirty)); + memset(_vehicle_sort_dirty, true, sizeof(_vehicle_sort_dirty)); } // draw the vehicle profit button in the vehicle list window. @@ -59,15 +47,15 @@ int CDECL VehicleUnsortedSorter(const void *a, const void *b) return DEREF_VEHICLE((*(const SortStruct*)a).index)->index - DEREF_VEHICLE((*(const SortStruct*)b).index)->index; } +// if the sorting criteria had the same value, sort vehicle by unitnumber +#define VEHICLEUNITNUMBERSORTER(r, a, b) {if (r == 0) {r = a->unitnumber - b->unitnumber;}} + int CDECL VehicleNumberSorter(const void *a, const void *b) { const Vehicle *va = DEREF_VEHICLE((*(const SortStruct*)a).index); const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index); int r = va->unitnumber - vb->unitnumber; - if (r == 0) // if the sorting criteria had the same value, sort by unitnumber - r = va->unitnumber - vb->unitnumber; - return (_internal_sort_order & 1) ? -r : r; } @@ -97,8 +85,7 @@ int CDECL VehicleNameSorter(const void *a, const void *b) r = strcmp(buf1, _bufcache); // sort by name - if (r == 0) // if the sorting criteria had the same value, sort by unitnumber - r = va->unitnumber - vb->unitnumber; + VEHICLEUNITNUMBERSORTER(r, va, vb); return (_internal_sort_order & 1) ? -r : r; } @@ -109,8 +96,7 @@ int CDECL VehicleAgeSorter(const void *a, const void *b) const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index); int r = va->age - vb->age; - if (r == 0) // if the sorting criteria had the same value, sort by unitnumber - r = va->unitnumber - vb->unitnumber; + VEHICLEUNITNUMBERSORTER(r, va, vb); return (_internal_sort_order & 1) ? -r : r; } @@ -121,8 +107,7 @@ int CDECL VehicleProfitThisYearSorter(const void *a, const void *b) const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index); int r = va->profit_this_year - vb->profit_this_year; - if (r == 0) // if the sorting criteria had the same value, sort by unitnumber - r = va->unitnumber - vb->unitnumber; + VEHICLEUNITNUMBERSORTER(r, va, vb); return (_internal_sort_order & 1) ? -r : r; } @@ -133,8 +118,7 @@ int CDECL VehicleProfitLastYearSorter(const void *a, const void *b) const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index); int r = va->profit_last_year - vb->profit_last_year; - if (r == 0) // if the sorting criteria had the same value, sort by unitnumber - r = va->unitnumber - vb->unitnumber; + VEHICLEUNITNUMBERSORTER(r, va, vb); return (_internal_sort_order & 1) ? -r : r; } @@ -175,8 +159,7 @@ int CDECL VehicleReliabilitySorter(const void *a, const void *b) const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index); int r = va->reliability - vb->reliability; - if (r == 0) // if the sorting criteria had the same value, sort by unitnumber - r = va->unitnumber - vb->unitnumber; + VEHICLEUNITNUMBERSORTER(r, va, vb); return (_internal_sort_order & 1) ? -r : r; } @@ -187,8 +170,7 @@ int CDECL VehicleMaxSpeedSorter(const void *a, const void *b) const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index); int r = va->max_speed - vb->max_speed; - if (r == 0) // if the sorting criteria had the same value, sort by unitnumber - r = va->unitnumber - vb->unitnumber; + VEHICLEUNITNUMBERSORTER(r, va, vb); return (_internal_sort_order & 1) ? -r : r; } |