diff options
-rw-r--r-- | src/bridge_gui.cpp | 62 | ||||
-rw-r--r-- | src/group_gui.cpp | 56 | ||||
-rw-r--r-- | src/industry_gui.cpp | 33 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 32 | ||||
-rw-r--r-- | src/sortlist_type.h | 6 | ||||
-rw-r--r-- | src/station_gui.cpp | 31 | ||||
-rw-r--r-- | src/vehicle.cpp | 29 | ||||
-rw-r--r-- | src/vehicle_func.h | 2 | ||||
-rw-r--r-- | src/vehicle_gui.cpp | 24 |
9 files changed, 91 insertions, 184 deletions
diff --git a/src/bridge_gui.cpp b/src/bridge_gui.cpp index 0ff0ac895..f4104fd57 100644 --- a/src/bridge_gui.cpp +++ b/src/bridge_gui.cpp @@ -120,7 +120,7 @@ private: void BuildBridge(uint8 i) { - DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->sort_list[i].index, + DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index, CcBuildBridge, CMD_BUILD_BRIDGE | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE)); } @@ -130,7 +130,7 @@ private: /* Skip sorting if resort bit is not set */ if (!(bridges->flags & VL_RESORT)) return; - qsort(this->bridges->sort_list, this->bridges->list_length, sizeof(this->bridges->sort_list[0]), _bridge_sorter[_bridge_sorting.criteria]); + qsort(this->bridges->Begin(), this->bridges->Length(), sizeof(this->bridges->Begin()), _bridge_sorter[_bridge_sorting.criteria]); /* Display the current sort variant */ this->widget[BBSW_DROPDOWN_CRITERIA].data = _bridge_sort_listing[this->bridges->sort_type]; @@ -155,7 +155,7 @@ public: this->widget[BBSW_CAPTION].data = (GB(this->type, 15, 2) == TRANSPORT_ROAD) ? STR_1803_SELECT_ROAD_BRIDGE : STR_100D_SELECT_RAIL_BRIDGE; this->resize.step_height = 22; - this->vscroll.count = bl->list_length; + this->vscroll.count = bl->Length(); if (this->last_size <= 4) { this->vscroll.cap = 4; @@ -171,7 +171,6 @@ public: ~BuildBridgeWindow() { - free(this->bridges->sort_list); delete bridges; } @@ -183,10 +182,10 @@ public: uint y = this->widget[BBSW_BRIDGE_LIST].top + 2; - for (int i = this->vscroll.pos; (i < (this->vscroll.cap + this->vscroll.pos)) && (i < this->bridges->list_length); i++) { - const BridgeSpec *b = this->bridges->sort_list[i].spec; + for (int i = this->vscroll.pos; (i < (this->vscroll.cap + this->vscroll.pos)) && (i < (int)this->bridges->Length()); i++) { + const BridgeSpec *b = this->bridges->Get(i)->spec; - SetDParam(2, this->bridges->sort_list[i].cost); + SetDParam(2, this->bridges->Get(i)->cost); SetDParam(1, b->speed * 10 / 16); SetDParam(0, b->material); @@ -200,7 +199,7 @@ public: virtual EventState OnKeyPress(uint16 key, uint16 keycode) { const uint8 i = keycode - '1'; - if (i < 9 && i < this->bridges->list_length) { + if (i < 9 && i < this->bridges->Length()) { /* Build the requested bridge */ this->BuildBridge(i); delete this; @@ -217,7 +216,7 @@ public: uint i = ((int)pt.y - this->widget[BBSW_BRIDGE_LIST].top) / this->resize.step_height; if (i < this->vscroll.cap) { i += this->vscroll.pos; - if (i < this->bridges->list_length) { + if (i < this->bridges->Length()) { this->BuildBridge(i); delete this; } @@ -254,7 +253,7 @@ public: { this->vscroll.cap += delta.y / (int)this->resize.step_height; this->widget[BBSW_BRIDGE_LIST].data = (this->vscroll.cap << 8) + 1; - SetVScrollCount(this, this->bridges->list_length); + SetVScrollCount(this, this->bridges->Length()); this->last_size = this->vscroll.cap; } @@ -286,35 +285,6 @@ static const WindowDesc _build_bridge_desc = { }; /** - * Add a buildable bridge to the list. - * If the list is empty a new one is created. - * - * @param bl The list which we want to manage - * @param item The item to add - * @return The pointer to the list - */ -static GUIBridgeList *PushBridgeList(GUIBridgeList *bl, BuildBridgeData item) -{ - if (bl == NULL) { - /* Create the list if needed */ - bl = new GUIBridgeList(); - bl->flags |= VL_RESORT; - if (_bridge_sorting.order) bl->flags |= VL_DESC; - bl->list_length = 1; - bl->sort_type = _bridge_sorting.criteria; - } else { - /* Resize the list */ - bl->list_length++; - } - - bl->sort_list = ReallocT(bl->sort_list, bl->list_length); - - bl->sort_list[bl->list_length - 1] = item; - - return bl; -} - -/** * Prepare the data for the build a bridge window. * If we can't build a bridge under the given conditions * show an error message. @@ -350,24 +320,26 @@ void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transpo /* total length of bridge */ const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2); + bl = new GUIBridgeList(); + /* loop for all bridgetypes */ for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) { if (CheckBridge_Stuff(brd_type, bridge_len)) { /* bridge is accepted, add to list */ - BuildBridgeData item; - item.index = brd_type; - item.spec = GetBridgeSpec(brd_type); + BuildBridgeData *item = bl->Append(); + item->index = brd_type; + item->spec = GetBridgeSpec(brd_type); /* Add to terraforming & bulldozing costs the cost of the * bridge itself (not computed with DC_QUERY_COST) */ - item.cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * item.spec->price) >> 8); - bl = PushBridgeList(bl, item); + item->cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * item->spec->price) >> 8); } } } - if (bl != NULL) { + if (bl != NULL && bl->Length() != 0) { new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl); } else { + if (bl != NULL) delete bl; ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE); } } diff --git a/src/group_gui.cpp b/src/group_gui.cpp index d40ef46d9..f3b3b161b 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -32,22 +32,16 @@ typedef GUIList<const Group*> GUIGroupList; static void BuildGroupList(GUIGroupList *gl, PlayerID owner, VehicleType vehicle_type) { - uint n = 0; - if (!(gl->flags & VL_REBUILD)) return; - const Group **list = MallocT<const Group*>(GetGroupArraySize()); + gl->Clear(); const Group *g; FOR_ALL_GROUPS(g) { - if (g->owner == owner && g->vehicle_type == vehicle_type) list[n++] = g; + if (g->owner == owner && g->vehicle_type != vehicle_type) *gl->Append() = g; } - gl->sort_list = ReallocT(gl->sort_list, n); - gl->list_length = n; - - for (uint i = 0; i < n; ++i) gl->sort_list[i] = list[i]; - free(list); + gl->Compact(); gl->flags &= ~VL_REBUILD; gl->flags |= VL_RESORT; @@ -87,7 +81,7 @@ static void SortGroupList(GUIGroupList *gl) { if (!(gl->flags & VL_RESORT)) return; - qsort((void*)gl->sort_list, gl->list_length, sizeof(gl->sort_list[0]), GroupNameSorter); + qsort((void*)gl->Begin(), gl->Length(), sizeof(gl->Begin()), GroupNameSorter); gl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; gl->flags &= ~VL_RESORT; @@ -223,12 +217,12 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break; } - this->vehicles.sort_list = NULL; + this->vehicles.Clear(); this->vehicles.sort_type = this->sorting->criteria; this->vehicles.flags = VL_REBUILD | (this->sorting->order ? VL_DESC : VL_NONE); this->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer - this->groups.sort_list = NULL; + this->groups.Clear(); this->groups.flags = VL_REBUILD | VL_NONE; this->groups.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer @@ -280,8 +274,6 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { ~VehicleGroupWindow() { - free((void*)this->vehicles.sort_list); - free((void*)this->groups.sort_list); } virtual void OnInvalidateData(int data) @@ -313,17 +305,17 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { BuildGroupList(&this->groups, owner, this->vehicle_type); SortGroupList(&this->groups); - SetVScrollCount(this, this->groups.list_length); - SetVScroll2Count(this, this->vehicles.list_length); + SetVScrollCount(this, this->groups.Length()); + SetVScroll2Count(this, this->vehicles.Length()); /* The drop down menu is out, *but* it may not be used, retract it. */ - if (this->vehicles.list_length == 0 && this->IsWidgetLowered(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN)) { + if (this->vehicles.Length() == 0 && this->IsWidgetLowered(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN)) { this->RaiseWidget(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN); HideDropDownMenu(this); } /* Disable all lists management button when the list is empty */ - this->SetWidgetsDisabledState(this->vehicles.list_length == 0 || _local_player != owner, + this->SetWidgetsDisabledState(this->vehicles.Length() == 0 || _local_player != owner, GRP_WIDGET_STOP_ALL, GRP_WIDGET_START_ALL, GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN, @@ -352,7 +344,7 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { We list all vehicles or ungrouped vehicles */ if (IsDefaultGroupID(this->group_sel) || IsAllGroupID(this->group_sel)) { SetDParam(0, owner); - SetDParam(1, this->vehicles.list_length); + SetDParam(1, this->vehicles.Length()); switch (this->vehicle_type) { case VEH_TRAIN: @@ -434,9 +426,9 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { DrawString(10, y1, str_no_group_veh, IsDefaultGroupID(this->group_sel) ? TC_WHITE : TC_BLACK); - max = min(this->vscroll.pos + this->vscroll.cap, this->groups.list_length); + max = min(this->vscroll.pos + this->vscroll.cap, this->groups.Length()); for (i = this->vscroll.pos ; i < max ; ++i) { - const Group *g = this->groups.sort_list[i]; + const Group *g = this->groups[i]; assert(g->owner == owner); @@ -454,9 +446,9 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { this->DrawSortButtonState(GRP_WIDGET_SORT_BY_ORDER, this->vehicles.flags & VL_DESC ? SBS_DOWN : SBS_UP); /* Draw Matrix Vehicle according to the vehicle list built before */ - max = min(this->vscroll2.pos + this->vscroll2.cap, this->vehicles.list_length); + max = min(this->vscroll2.pos + this->vscroll2.cap, this->vehicles.Length()); for (i = this->vscroll2.pos ; i < max ; ++i) { - const Vehicle* v = this->vehicles.sort_list[i]; + const Vehicle* v = this->vehicles[i]; assert(v->type == this->vehicle_type && v->owner == owner); @@ -521,9 +513,9 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { id_g += this->vscroll.pos; - if (id_g >= this->groups.list_length) return; + if (id_g >= this->groups.Length()) return; - this->group_sel = this->groups.sort_list[id_g]->index;; + this->group_sel = this->groups[id_g]->index;; this->vehicles.flags |= VL_REBUILD; this->SetDirty(); @@ -538,9 +530,9 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { id_v += this->vscroll2.pos; - if (id_v >= this->vehicles.list_length) return; // click out of list bound + if (id_v >= this->vehicles.Length()) return; // click out of list bound - v = this->vehicles.sort_list[id_v]; + v = this->vehicles[id_v]; this->vehicle_sel = v->index; @@ -628,9 +620,9 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { id_g += this->vscroll.pos; - if (id_g >= this->groups.list_length) return; + if (id_g >= this->groups.Length()) return; - DoCommandP(0, this->groups.sort_list[id_g]->index, vindex, NULL, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_GROUP_CAN_T_ADD_VEHICLE)); + DoCommandP(0, this->groups[id_g]->index, vindex, NULL, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_GROUP_CAN_T_ADD_VEHICLE)); break; } @@ -648,9 +640,9 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { id_v += this->vscroll2.pos; - if (id_v >= this->vehicles.list_length) return; // click out of list bound + if (id_v >= this->vehicles.Length()) return; // click out of list bound - v = this->vehicles.sort_list[id_v]; + v = this->vehicles[id_v]; if (vindex == v->index) { ShowVehicleViewWindow(v); @@ -693,7 +685,7 @@ struct VehicleGroupWindow : public Window, public VehicleListBase { break; case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN: - assert(this->vehicles.list_length != 0); + assert(this->vehicles.Length() != 0); switch (index) { case GALF_REPLACE: // Replace window diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 57c7b9b51..d8857f6e1 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -812,27 +812,21 @@ typedef GUIList<const Industry*> GUIIndustryList; */ static void BuildIndustriesList(GUIIndustryList *sl) { - uint n = 0; - const Industry *i; - if (!(sl->flags & VL_REBUILD)) return; - /* Create array for sorting */ - const Industry **industry_sort = MallocT<const Industry*>(GetMaxIndustryIndex() + 1); + sl->Clear(); DEBUG(misc, 3, "Building industry list"); - FOR_ALL_INDUSTRIES(i) industry_sort[n++] = i; - - free((void*)sl->sort_list); - sl->sort_list = MallocT<const Industry*>(n); - sl->list_length = n; + const Industry *i; + FOR_ALL_INDUSTRIES(i) { + *sl->Append() = i; + } - for (uint i = 0; i < n; ++i) sl->sort_list[i] = industry_sort[i]; + sl->Compact(); sl->flags &= ~VL_REBUILD; sl->flags |= VL_RESORT; - free((void*)industry_sort); } @@ -847,7 +841,7 @@ static void SortIndustriesList(GUIIndustryList *sl) _internal_sort_order = (sl->sort_type << 1) | (sl->flags & VL_DESC); _last_industry = NULL; // used for "cache" in namesorting - qsort((void*)sl->sort_list, sl->list_length, sizeof(sl->sort_list[0]), &GeneralIndustrySorter); + qsort((void*)sl->Begin(), sl->Length(), sizeof(sl->Begin()), &GeneralIndustrySorter); sl->flags &= ~VL_RESORT; } @@ -865,7 +859,6 @@ struct IndustryDirectoryWindow : public Window, public GUIIndustryList { this->resize.step_height = 10; this->FindWindowPlacementAndResize(desc); - this->sort_list = NULL; this->flags = VL_REBUILD; this->sort_type = industry_sort.criteria; if (industry_sort.order) this->flags |= VL_DESC; @@ -876,16 +869,16 @@ struct IndustryDirectoryWindow : public Window, public GUIIndustryList { BuildIndustriesList(this); SortIndustriesList(this); - SetVScrollCount(this, this->list_length); + SetVScrollCount(this, this->Length()); this->DrawWidgets(); this->DrawSortButtonState(IDW_SORTBYNAME + this->sort_type, this->flags & VL_DESC ? SBS_DOWN : SBS_UP); - int max = min(this->vscroll.pos + this->vscroll.cap, this->list_length); + int max = min(this->vscroll.pos + this->vscroll.cap, this->Length()); int y = 28; // start of the list-widget for (int n = this->vscroll.pos; n < max; ++n) { - const Industry* i = this->sort_list[n]; + const Industry* i = *this->Get(n); const IndustrySpec *indsp = GetIndustrySpec(i->type); byte p = 0; @@ -940,11 +933,11 @@ struct IndustryDirectoryWindow : public Window, public GUIIndustryList { if (!IsInsideMM(y, 0, this->vscroll.cap)) return; p = y + this->vscroll.pos; - if (p < this->list_length) { + if (p < this->Length()) { if (_ctrl_pressed) { - ShowExtraViewPortWindow(this->sort_list[p]->xy); + ShowExtraViewPortWindow((*this->Get(p))->xy); } else { - ScrollMainWindowToTile(this->sort_list[p]->xy); + ScrollMainWindowToTile((*this->Get(p))->xy); } } } break; diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 11e6e33b7..c3787e33d 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -190,7 +190,6 @@ struct NetworkGameWindow : public QueryStringBaseWindow { this->field = NGWW_PLAYER; this->server = NULL; - this->servers.sort_list = NULL; this->servers.flags = VL_REBUILD | (_ng_sorting.order ? VL_DESC : VL_NONE); this->servers.sort_type = _ng_sorting.criteria; @@ -199,7 +198,6 @@ struct NetworkGameWindow : public QueryStringBaseWindow { ~NetworkGameWindow() { - free(this->servers.sort_list); } /** @@ -208,23 +206,17 @@ struct NetworkGameWindow : public QueryStringBaseWindow { */ void BuildNetworkGameList() { - NetworkGameList *ngl_temp; - uint n = 0; - if (!(this->servers.flags & VL_REBUILD)) return; - /* Count the number of games in the list */ - for (ngl_temp = _network_game_list; ngl_temp != NULL; ngl_temp = ngl_temp->next) n++; - if (n == 0) return; - /* Create temporary array of games to use for listing */ - this->servers.sort_list = ReallocT(this->servers.sort_list, n); - this->servers.list_length = n; + this->servers.Clear(); - for (n = 0, ngl_temp = _network_game_list; ngl_temp != NULL; ngl_temp = ngl_temp->next) { - this->servers.sort_list[n++] = ngl_temp; + for (NetworkGameList *ngl = _network_game_list; ngl != NULL; ngl = ngl->next) { + *this->servers.Append() = ngl; } + this->servers.Compact(); + /* Force resort */ this->servers.flags &= ~VL_REBUILD; this->servers.flags |= VL_RESORT; @@ -242,17 +234,17 @@ struct NetworkGameWindow : public QueryStringBaseWindow { uint i; if (!(this->servers.flags & VL_RESORT)) return; - if (this->servers.list_length == 0) return; + if (this->servers.Length() == 0) return; _internal_sort_order = !!(this->servers.flags & VL_DESC); - qsort(this->servers.sort_list, this->servers.list_length, sizeof(this->servers.sort_list[0]), ngame_sorter[this->servers.sort_type]); + qsort(this->servers.Begin(), this->servers.Length(), sizeof(this->servers.Begin()), ngame_sorter[this->servers.sort_type]); /* After sorting ngl->sort_list contains the sorted items. Put these back * into the original list. Basically nothing has changed, we are only * shuffling the ->next pointers */ - _network_game_list = this->servers.sort_list[0]; - for (item = _network_game_list, i = 1; i != this->servers.list_length; i++) { - item->next = this->servers.sort_list[i]; + _network_game_list = this->servers[0]; + for (item = _network_game_list, i = 1; i != this->servers.Length(); i++) { + item->next = this->servers[i]; item = item->next; } item->next = NULL; @@ -300,7 +292,7 @@ struct NetworkGameWindow : public QueryStringBaseWindow { if (this->servers.flags & VL_REBUILD) { this->BuildNetworkGameList(); - SetVScrollCount(this, this->servers.list_length); + SetVScrollCount(this, this->servers.Length()); } if (this->servers.flags & VL_RESORT) this->SortNetworkGameList(); @@ -582,7 +574,7 @@ struct NetworkGameWindow : public QueryStringBaseWindow { this->widget[NGWW_MATRIX].data = (this->vscroll.cap << 8) + 1; - SetVScrollCount(this, this->servers.list_length); + SetVScrollCount(this, this->servers.Length()); int widget_width = this->widget[NGWW_FIND].right - this->widget[NGWW_FIND].left; int space = (this->width - 4 * widget_width - 25) / 3; diff --git a/src/sortlist_type.h b/src/sortlist_type.h index a3e428554..fc325754c 100644 --- a/src/sortlist_type.h +++ b/src/sortlist_type.h @@ -5,6 +5,8 @@ #ifndef SORTLIST_TYPE_H #define SORTLIST_TYPE_H +#include "misc/smallvec.h" + enum SortListFlags { VL_NONE = 0, ///< no sort VL_DESC = 1 << 0, ///< sort descending or ascending @@ -20,10 +22,8 @@ struct Listing { }; template <typename T> -struct GUIList { - T* sort_list; ///< The items to sort. +struct GUIList : public SmallVector<T, 32> { SortListFlags flags; ///< used to control sorting/resorting/etc. - uint16 list_length; ///< length of the list being sorted uint16 resort_timer; ///< resort list after a given amount of ticks if set byte sort_type; ///< what criteria to sort on }; diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 4086f9ad8..4583156ff 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -178,16 +178,13 @@ typedef GUIList<const Station*> GUIStationList; */ static void BuildStationsList(GUIStationList *sl, PlayerID owner, byte facilities, uint32 cargo_filter, bool include_empty) { - uint n = 0; - const Station *st; - if (!(sl->flags & VL_REBUILD)) return; - /* Create array for sorting */ - const Station **station_sort = MallocT<const Station*>(GetMaxStationIndex() + 1); + sl->Clear(); DEBUG(misc, 3, "Building station list for player %d", owner); + const Station *st; FOR_ALL_STATIONS(st) { if (st->owner == owner || (st->owner == OWNER_NONE && !st->IsBuoy() && HasStationInUse(st->index, owner))) { if (facilities & st->facilities) { //only stations with selected facilities @@ -196,28 +193,23 @@ static void BuildStationsList(GUIStationList *sl, PlayerID owner, byte facilitie if (!st->goods[j].cargo.Empty()) { num_waiting_cargo++; //count number of waiting cargo if (HasBit(cargo_filter, j)) { - station_sort[n++] = st; + *sl->Append() = st; break; } } } /* stations without waiting cargo */ if (num_waiting_cargo == 0 && include_empty) { - station_sort[n++] = st; + *sl->Append() = st; } } } } - free((void*)sl->sort_list); - sl->sort_list = MallocT<const Station*>(n); - sl->list_length = n; - - for (uint i = 0; i < n; ++i) sl->sort_list[i] = station_sort[i]; + sl->Compact(); sl->flags &= ~VL_REBUILD; sl->flags |= VL_RESORT; - free((void*)station_sort); } @@ -239,7 +231,7 @@ static void SortStationsList(GUIStationList *sl) _internal_sort_order = sl->flags & VL_DESC; _last_station = NULL; // used for "cache" in namesorting - qsort((void*)sl->sort_list, sl->list_length, sizeof(sl->sort_list[0]), _station_sorter[sl->sort_type]); + qsort((void*)sl->Begin(), sl->Length(), sizeof(sl->Begin()), _station_sorter[sl->sort_type]); sl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; sl->flags &= ~VL_RESORT; @@ -311,7 +303,6 @@ struct PlayerStationsWindow : public Window, public GUIStationList this->SetWidgetLoweredState(SLW_CARGOALL, _cargo_filter == _cargo_mask && include_empty); this->SetWidgetLoweredState(SLW_NOCARGOWAITING, include_empty); - this->sort_list = NULL; this->flags = VL_REBUILD; this->sort_type = station_sort.criteria; if (station_sort.order) this->flags |= VL_DESC; @@ -329,7 +320,7 @@ struct PlayerStationsWindow : public Window, public GUIStationList BuildStationsList(this, owner, facilities, _cargo_filter, include_empty); SortStationsList(this); - SetVScrollCount(this, this->list_length); + SetVScrollCount(this, this->Length()); /* draw widgets, with player's name in the caption */ SetDParam(0, owner); @@ -375,11 +366,11 @@ struct PlayerStationsWindow : public Window, public GUIStationList return; } - int max = min(this->vscroll.pos + this->vscroll.cap, this->list_length); + int max = min(this->vscroll.pos + this->vscroll.cap, this->Length()); y = 40; // start of the list-widget for (int i = this->vscroll.pos; i < max; ++i) { // do until max number of stations of owner - const Station *st = this->sort_list[i]; + const Station *st = *this->Get(i); int x; assert(st->xy != 0); @@ -413,9 +404,9 @@ struct PlayerStationsWindow : public Window, public GUIStationList id_v += this->vscroll.pos; - if (id_v >= this->list_length) return; // click out of list bound + if (id_v >= this->Length()) return; // click out of list bound - const Station *st = this->sort_list[id_v]; + const Station *st = *this->Get(id_v); /* do not check HasStationInUse - it is slow and may be invalid */ assert(st->owner == (PlayerID)this->window_number || (st->owner == OWNER_NONE && !st->IsBuoy())); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index f5bb940f7..54c379edd 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1335,35 +1335,6 @@ void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engine } /** - * @param sort_list list to store the list in. Either NULL or the length length_of_array tells - * @param length_of_array informs the length allocated for sort_list. This is not the same as the number of vehicles in the list. Needs to be 0 when sort_list is NULL - * @param type type of vehicle - * @param owner PlayerID of owner to generate a list for - * @param index This parameter has different meanings depending on window_type - * <ul> - * <li>VLW_STATION_LIST: index of station 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 tells what kind of window the list is for. Use the VLW flags in vehicle_gui.h - * @return the number of vehicles added to the list - */ -uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type) -{ - VehicleList list; - GenerateVehicleSortList(&list, type, owner, index, window_type); - - if (list.Length() > 0) { - *sort_list = ReallocT(*sort_list, list.Length()); - memcpy(*sort_list, list.Begin(), sizeof(list.Begin()) * list.Length()); - } - - return list.Length(); -} - -/** * Generate a list of vehicles based on window type. * @param list Pointer to list to add vehicles to * @param type Type of vehicle diff --git a/src/vehicle_func.h b/src/vehicle_func.h index 05daf0fb8..84870b59d 100644 --- a/src/vehicle_func.h +++ b/src/vehicle_func.h @@ -68,8 +68,6 @@ void TrainConsistChanged(Vehicle *v); void TrainPowerChanged(Vehicle *v); Money GetTrainRunningCost(const Vehicle *v); -/* Old style list kept for migration */ -uint GenerateVehicleSortList(const Vehicle*** sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type); void GenerateVehicleSortList(VehicleList *list, VehicleType type, PlayerID owner, uint32 index, uint16 window_type); void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engine_list, VehicleList *wagon_list); CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id); diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 2ddd3e68c..1a9a44b42 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -92,7 +92,7 @@ void BuildVehicleList(VehicleListBase *vl, PlayerID owner, uint16 index, uint16 DEBUG(misc, 3, "Building vehicle list for player %d at station %d", owner, index); - vl->vehicles.list_length = GenerateVehicleSortList(&vl->vehicles.sort_list, &vl->vehicles.list_length, vl->vehicle_type, owner, index, window_type); + GenerateVehicleSortList(&vl->vehicles, vl->vehicle_type, owner, index, window_type); vl->vehicles.flags &= ~VL_REBUILD; vl->vehicles.flags |= VL_RESORT; @@ -110,7 +110,7 @@ void SortVehicleList(VehicleListBase *vl) _last_vehicle[0] = _last_vehicle[1] = NULL; _internal_sort_order = (vl->vehicles.flags & VL_DESC) != 0; - qsort((void*)vl->vehicles.sort_list, vl->vehicles.list_length, sizeof(vl->vehicles.sort_list[0]), + qsort((void*)vl->vehicles.Begin(), vl->vehicles.Length(), sizeof(vl->vehicles.Begin()), _vehicle_sorter[vl->vehicles.sort_type]); vl->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; @@ -806,8 +806,7 @@ struct VehicleListWindow : public Window, public VehicleListBase { PlayerID player = (PlayerID)GB(this->window_number, 0, 8); this->vehicle_type = (VehicleType)GB(this->window_number, 11, 5); - this->vehicles.list_length = 0; - this->vehicles.sort_list = NULL; + this->vehicles.Clear(); this->caption_color = player; /* Hide the widgets that we will not use in this window @@ -925,7 +924,6 @@ struct VehicleListWindow : public Window, public VehicleListBase { ~VehicleListWindow() { - free((void*)this->vehicles.sort_list); } virtual void OnPaint() @@ -940,12 +938,12 @@ struct VehicleListWindow : public Window, public VehicleListBase { BuildVehicleList(this, owner, index, window_type); SortVehicleList(this); - SetVScrollCount(this, this->vehicles.list_length); + SetVScrollCount(this, this->vehicles.Length()); /* draw the widgets */ switch (window_type) { case VLW_SHARED_ORDERS: /* Shared Orders */ - if (this->vehicles.list_length == 0) { + if (this->vehicles.Length() == 0) { /* We can't open this window without vehicles using this order * and we should close the window when deleting the order */ NOT_REACHED(); @@ -981,7 +979,7 @@ struct VehicleListWindow : public Window, public VehicleListBase { default: NOT_REACHED(); break; } - this->SetWidgetsDisabledState(this->vehicles.list_length == 0, + this->SetWidgetsDisabledState(this->vehicles.Length() == 0, VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN, VLW_WIDGET_STOP_ALL, VLW_WIDGET_START_ALL, @@ -994,9 +992,9 @@ struct VehicleListWindow : public Window, public VehicleListBase { /* draw arrow pointing up/down for ascending/descending sorting */ this->DrawSortButtonState(VLW_WIDGET_SORT_ORDER, this->vehicles.flags & VL_DESC ? SBS_DOWN : SBS_UP); - max = min(this->vscroll.pos + this->vscroll.cap, this->vehicles.list_length); + max = min(this->vscroll.pos + this->vscroll.cap, this->vehicles.Length()); for (i = this->vscroll.pos; i < max; ++i) { - const Vehicle *v = this->vehicles.sort_list[i]; + const Vehicle *v = this->vehicles[i]; StringID str; SetDParam(0, v->GetDisplayProfitThisYear()); @@ -1049,9 +1047,9 @@ struct VehicleListWindow : public Window, public VehicleListBase { id_v += this->vscroll.pos; - if (id_v >= this->vehicles.list_length) return; // click out of list bound + if (id_v >= this->vehicles.Length()) return; // click out of list bound - v = this->vehicles.sort_list[id_v]; + v = this->vehicles[id_v]; ShowVehicleViewWindow(v); } break; @@ -1100,7 +1098,7 @@ struct VehicleListWindow : public Window, public VehicleListBase { } break; case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN: - assert(this->vehicles.list_length != 0); + assert(this->vehicles.Length() != 0); switch (index) { case 0: /* Replace window */ |