diff --git a/bin/ai/regression/tst_regression/result.txt b/bin/ai/regression/tst_regression/result.txt index 58736c68b8..18ae14cb7a 100644 --- a/bin/ai/regression/tst_regression/result.txt +++ b/bin/ai/regression/tst_regression/result.txt @@ -8534,7 +8534,7 @@ ERROR: IsEnd() is invalid as Begin() is never called 19693 => 8 --TileList_IndustryProducing-- - Count(): 92 + Count(): 90 Location ListDump: 46919 => 1 46918 => 1 @@ -8626,8 +8626,6 @@ ERROR: IsEnd() is invalid as Begin() is never called 44353 => 1 44352 => 1 44351 => 1 - 46920 => 0 - 46911 => 0 --TileList_StationType-- Count(): 4 diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp index 40f8d8ef2b..c00d97b91e 100644 --- a/src/autoreplace_gui.cpp +++ b/src/autoreplace_gui.cpp @@ -32,11 +32,9 @@ void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group); -static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b) +static bool EngineNumberSorter(const EngineID &a, const EngineID &b) { - int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position; - - return r; + return Engine::Get(a)->list_position < Engine::Get(b)->list_position; } /** diff --git a/src/bitmap_type.h b/src/bitmap_type.h index 11a4190938..3c9c4afbdf 100644 --- a/src/bitmap_type.h +++ b/src/bitmap_type.h @@ -34,6 +34,14 @@ public: this->h = 0; } + BitmapTileArea(const TileArea &ta) + { + this->tile = ta.tile; + this->w = ta.w; + this->h = ta.h; + this->data.resize(Index(this->w, this->h)); + } + /** * Reset and clear the BitmapTileArea. */ diff --git a/src/bridge_gui.cpp b/src/bridge_gui.cpp index f1aad6adcd..da513470b2 100644 --- a/src/bridge_gui.cpp +++ b/src/bridge_gui.cpp @@ -96,21 +96,21 @@ private: Scrollbar *vscroll; /** Sort the bridges by their index */ - static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b) + static bool BridgeIndexSorter(const BuildBridgeData &a, const BuildBridgeData &b) { - return a->index - b->index; + return a.index < b.index; } /** Sort the bridges by their price */ - static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b) + static bool BridgePriceSorter(const BuildBridgeData &a, const BuildBridgeData &b) { - return a->cost - b->cost; + return a.cost < b.cost; } /** Sort the bridges by their maximum speed */ - static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b) + static bool BridgeSpeedSorter(const BuildBridgeData &a, const BuildBridgeData &b) { - return a->spec->speed - b->spec->speed; + return a.spec->speed < b.spec->speed; } void BuildBridge(uint8 i) diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index 5cb8ed4095..2c1eda6ab4 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -100,57 +100,54 @@ static CargoID _engine_sort_last_cargo_criteria[] = {CF_ANY, CF_ANY, CF_ANY, CF_ /** * Determines order of engines by engineID - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b) +static bool EngineNumberSorter(const EngineID &a, const EngineID &b) { - int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position; + int r = Engine::Get(a)->list_position - Engine::Get(b)->list_position; - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of engines by introduction date - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL EngineIntroDateSorter(const EngineID *a, const EngineID *b) +static bool EngineIntroDateSorter(const EngineID &a, const EngineID &b) { - const int va = Engine::Get(*a)->intro_date; - const int vb = Engine::Get(*b)->intro_date; + const int va = Engine::Get(a)->intro_date; + const int vb = Engine::Get(b)->intro_date; const int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of engines by name - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b) +static bool EngineNameSorter(const EngineID &a, const EngineID &b) { static EngineID last_engine[2] = { INVALID_ENGINE, INVALID_ENGINE }; static char last_name[2][64] = { "\0", "\0" }; - const EngineID va = *a; - const EngineID vb = *b; - - if (va != last_engine[0]) { - last_engine[0] = va; - SetDParam(0, va); + if (a != last_engine[0]) { + last_engine[0] = a; + SetDParam(0, a); GetString(last_name[0], STR_ENGINE_NAME, lastof(last_name[0])); } - if (vb != last_engine[1]) { - last_engine[1] = vb; - SetDParam(0, vb); + if (b != last_engine[1]) { + last_engine[1] = b; + SetDParam(0, b); GetString(last_name[1], STR_ENGINE_NAME, lastof(last_name[1])); } @@ -158,121 +155,121 @@ static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b) /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of engines by reliability - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL EngineReliabilitySorter(const EngineID *a, const EngineID *b) +static bool EngineReliabilitySorter(const EngineID &a, const EngineID &b) { - const int va = Engine::Get(*a)->reliability; - const int vb = Engine::Get(*b)->reliability; + const int va = Engine::Get(a)->reliability; + const int vb = Engine::Get(b)->reliability; const int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of engines by purchase cost - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL EngineCostSorter(const EngineID *a, const EngineID *b) +static bool EngineCostSorter(const EngineID &a, const EngineID &b) { - Money va = Engine::Get(*a)->GetCost(); - Money vb = Engine::Get(*b)->GetCost(); + Money va = Engine::Get(a)->GetCost(); + Money vb = Engine::Get(b)->GetCost(); int r = ClampToI32(va - vb); /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of engines by speed - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL EngineSpeedSorter(const EngineID *a, const EngineID *b) +static bool EngineSpeedSorter(const EngineID &a, const EngineID &b) { - int va = Engine::Get(*a)->GetDisplayMaxSpeed(); - int vb = Engine::Get(*b)->GetDisplayMaxSpeed(); + int va = Engine::Get(a)->GetDisplayMaxSpeed(); + int vb = Engine::Get(b)->GetDisplayMaxSpeed(); int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of engines by power - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL EnginePowerSorter(const EngineID *a, const EngineID *b) +static bool EnginePowerSorter(const EngineID &a, const EngineID &b) { - int va = Engine::Get(*a)->GetPower(); - int vb = Engine::Get(*b)->GetPower(); + int va = Engine::Get(a)->GetPower(); + int vb = Engine::Get(b)->GetPower(); int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of engines by tractive effort - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL EngineTractiveEffortSorter(const EngineID *a, const EngineID *b) +static bool EngineTractiveEffortSorter(const EngineID &a, const EngineID &b) { - int va = Engine::Get(*a)->GetDisplayMaxTractiveEffort(); - int vb = Engine::Get(*b)->GetDisplayMaxTractiveEffort(); + int va = Engine::Get(a)->GetDisplayMaxTractiveEffort(); + int vb = Engine::Get(b)->GetDisplayMaxTractiveEffort(); int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of engines by running costs - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL EngineRunningCostSorter(const EngineID *a, const EngineID *b) +static bool EngineRunningCostSorter(const EngineID &a, const EngineID &b) { - Money va = Engine::Get(*a)->GetRunningCost(); - Money vb = Engine::Get(*b)->GetRunningCost(); + Money va = Engine::Get(a)->GetRunningCost(); + Money vb = Engine::Get(b)->GetRunningCost(); int r = ClampToI32(va - vb); /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of engines by running costs - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL EnginePowerVsRunningCostSorter(const EngineID *a, const EngineID *b) +static bool EnginePowerVsRunningCostSorter(const EngineID &a, const EngineID &b) { - const Engine *e_a = Engine::Get(*a); - const Engine *e_b = Engine::Get(*b); + const Engine *e_a = Engine::Get(a); + const Engine *e_b = Engine::Get(b); /* Here we are using a few tricks to get the right sort. * We want power/running cost, but since we usually got higher running cost than power and we store the result in an int, @@ -286,79 +283,79 @@ static int CDECL EnginePowerVsRunningCostSorter(const EngineID *a, const EngineI /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /* Train sorting functions */ /** * Determines order of train engines by capacity - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b) +static bool TrainEngineCapacitySorter(const EngineID &a, const EngineID &b) { - const RailVehicleInfo *rvi_a = RailVehInfo(*a); - const RailVehicleInfo *rvi_b = RailVehInfo(*b); + const RailVehicleInfo *rvi_a = RailVehInfo(a); + const RailVehicleInfo *rvi_b = RailVehInfo(b); - int va = GetTotalCapacityOfArticulatedParts(*a) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); - int vb = GetTotalCapacityOfArticulatedParts(*b) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); + int va = GetTotalCapacityOfArticulatedParts(a) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); + int vb = GetTotalCapacityOfArticulatedParts(b) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of train engines by engine / wagon - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL TrainEnginesThenWagonsSorter(const EngineID *a, const EngineID *b) +static bool TrainEnginesThenWagonsSorter(const EngineID &a, const EngineID &b) { - int val_a = (RailVehInfo(*a)->railveh_type == RAILVEH_WAGON ? 1 : 0); - int val_b = (RailVehInfo(*b)->railveh_type == RAILVEH_WAGON ? 1 : 0); + int val_a = (RailVehInfo(a)->railveh_type == RAILVEH_WAGON ? 1 : 0); + int val_b = (RailVehInfo(b)->railveh_type == RAILVEH_WAGON ? 1 : 0); int r = val_a - val_b; /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /* Road vehicle sorting functions */ /** * Determines order of road vehicles by capacity - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL RoadVehEngineCapacitySorter(const EngineID *a, const EngineID *b) +static bool RoadVehEngineCapacitySorter(const EngineID &a, const EngineID &b) { - int va = GetTotalCapacityOfArticulatedParts(*a); - int vb = GetTotalCapacityOfArticulatedParts(*b); + int va = GetTotalCapacityOfArticulatedParts(a); + int vb = GetTotalCapacityOfArticulatedParts(b); int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /* Ship vehicle sorting functions */ /** * Determines order of ships by capacity - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b) +static bool ShipEngineCapacitySorter(const EngineID &a, const EngineID &b) { - const Engine *e_a = Engine::Get(*a); - const Engine *e_b = Engine::Get(*b); + const Engine *e_a = Engine::Get(a); + const Engine *e_b = Engine::Get(b); int va = e_a->GetDisplayDefaultCapacity(); int vb = e_b->GetDisplayDefaultCapacity(); @@ -366,21 +363,21 @@ static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b) /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /* Aircraft sorting functions */ /** * Determines order of aircraft by cargo - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b) +static bool AircraftEngineCargoSorter(const EngineID &a, const EngineID &b) { - const Engine *e_a = Engine::Get(*a); - const Engine *e_b = Engine::Get(*b); + const Engine *e_a = Engine::Get(a); + const Engine *e_b = Engine::Get(b); uint16 mail_a, mail_b; int va = e_a->GetDisplayDefaultCapacity(&mail_a); @@ -396,25 +393,25 @@ static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b) return EngineNumberSorter(a, b); } } - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** * Determines order of aircraft by range. - * @param *a first engine to compare. - * @param *b second engine to compare. - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal. + * @param a first engine to compare + * @param b second engine to compare + * @return for descending order: returns true if a < b. Vice versa for ascending order */ -static int CDECL AircraftRangeSorter(const EngineID *a, const EngineID *b) +static bool AircraftRangeSorter(const EngineID &a, const EngineID &b) { - uint16 r_a = Engine::Get(*a)->GetRange(); - uint16 r_b = Engine::Get(*b)->GetRange(); + uint16 r_a = Engine::Get(a)->GetRange(); + uint16 r_b = Engine::Get(b)->GetRange(); int r = r_a - r_b; /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return _engine_sort_direction ? r > 0 : r < 0; } /** Sort functions for the vehicle sort criteria, for each vehicle type. */ diff --git a/src/company_gui.cpp b/src/company_gui.cpp index 36091298ff..14987c5250 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -620,26 +620,26 @@ private: ShowDropDownList(this, std::move(list), sel, widget); } - static int CDECL GroupNameSorter(const Group * const *a, const Group * const *b) + static bool GroupNameSorter(const Group * const &a, const Group * const &b) { static const Group *last_group[2] = { nullptr, nullptr }; static char last_name[2][64] = { "", "" }; - if (*a != last_group[0]) { - last_group[0] = *a; - SetDParam(0, (*a)->index); + if (a != last_group[0]) { + last_group[0] = a; + SetDParam(0, a->index); GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0])); } - if (*b != last_group[1]) { - last_group[1] = *b; - SetDParam(0, (*b)->index); + if (b != last_group[1]) { + last_group[1] = b; + SetDParam(0, b->index); GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1])); } int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting). - if (r == 0) return (*a)->index - (*b)->index; - return r; + if (r == 0) return a->index < b->index; + return r < 0; } void AddChildren(GUIGroupList *source, GroupID parent, int indent) diff --git a/src/core/alloc_type.hpp b/src/core/alloc_type.hpp index 45e7b0b43a..3d27df03cb 100644 --- a/src/core/alloc_type.hpp +++ b/src/core/alloc_type.hpp @@ -14,49 +14,6 @@ #include "alloc_func.hpp" -/** - * A small 'wrapper' for allocations that can be done on most OSes on the - * stack, but are just too large to fit in the stack on devices with a small - * stack such as the NDS. - * So when it is possible a stack allocation is made, otherwise a heap - * allocation is made and this is freed once the struct goes out of scope. - * @param T the type to make the allocation for - * @param length the amount of items to allocate - */ -template -struct SmallStackSafeStackAlloc { - /** Storing the data on the stack */ - T data[length]; - - /** - * Gets a pointer to the data stored in this wrapper. - * @return the pointer. - */ - inline operator T *() - { - return data; - } - - /** - * Gets a pointer to the data stored in this wrapper. - * @return the pointer. - */ - inline T *operator -> () - { - return data; - } - - /** - * Gets a pointer to the last data element stored in this wrapper. - * @note needed because endof does not work properly for pointers. - * @return the 'endof' pointer. - */ - inline T *EndOf() - { - return endof(data); - } -}; - /** * A reusable buffer that can be used for places that temporary allocate * a bit of memory and do that very often, or for places where static @@ -160,50 +117,9 @@ public: inline void operator delete[](void *ptr) { free(ptr); } }; -/** - * A smart pointer class that free()'s the pointer on destruction. - * @tparam T Storage type. - */ -template -class AutoFreePtr +struct FreeDeleter { - T *ptr = nullptr; ///< Stored pointer. - -public: - AutoFreePtr(T *ptr) : ptr(ptr) {} - ~AutoFreePtr() { free(this->ptr); } - - /** - * Take ownership of a new pointer and free the old one if needed. - * @param ptr NEw pointer. - */ - inline void Assign(T *ptr) - { - free(this->ptr); - this->ptr = ptr; - } - - /** Dereference pointer. */ - inline T *operator ->() { return this->ptr; } - /** Dereference pointer. */ - inline const T *operator ->() const { return this->ptr; } - - /** Cast to underlaying regular pointer. */ - inline operator T *() { return this->ptr; } - /** Cast to underlaying regular pointer. */ - inline operator const T *() const { return this->ptr; } - - AutoFreePtr(AutoFreePtr &&other) noexcept - { - *this = std::move(other); - } - - AutoFreePtr& operator=(AutoFreePtr &&other) noexcept - { - this->Assign(other.ptr); - other.ptr = nullptr; - return *this; - } + void operator()(const void* ptr) { free(ptr); } }; #endif /* ALLOC_TYPE_HPP */ diff --git a/src/core/smallmap_type.hpp b/src/core/smallmap_type.hpp index 0fd6d0a07c..d24fc4742d 100644 --- a/src/core/smallmap_type.hpp +++ b/src/core/smallmap_type.hpp @@ -164,16 +164,6 @@ struct SmallMap : std::vector > { n.first = key; return n.second; } - - inline void SortByKey() - { - QSortT(std::vector::data(), std::vector::size(), KeySorter); - } - - static int CDECL KeySorter(const Pair *a, const Pair *b) - { - return a->first - b->first; - } }; #endif /* SMALLMAP_TYPE_HPP */ diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index 15d63cc880..2ecad90115 100644 --- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -325,11 +325,8 @@ void DrawVehicleEngine(int left, int right, int preferred_x, int y, EngineID eng */ void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare) { - size_t size = el->size(); - /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems) - * generally, do not sort if there are less than 2 items */ - if (size < 2) return; - QSortT(el->data(), size, compare); + if (el->size() < 2) return; + std::sort(el->begin(), el->end(), compare); } /** @@ -344,6 +341,6 @@ void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, ui if (num_items < 2) return; assert(begin < el->size()); assert(begin + num_items <= el->size()); - QSortT(el->data() + begin, num_items, compare); + std::sort(el->begin() + begin, el->begin() + begin + num_items, compare); } diff --git a/src/engine_gui.h b/src/engine_gui.h index fc0b7ad7d9..e95cc13e4d 100644 --- a/src/engine_gui.h +++ b/src/engine_gui.h @@ -19,7 +19,7 @@ typedef GUIList GUIEngineList; -typedef int CDECL EngList_SortTypeFunction(const EngineID*, const EngineID*); ///< argument type for #EngList_Sort. +typedef bool EngList_SortTypeFunction(const EngineID&, const EngineID&); ///< argument type for #EngList_Sort. void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare); void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items); diff --git a/src/fios.cpp b/src/fios.cpp index 008f552e6b..76a2e7430f 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -47,21 +47,20 @@ extern void GetOldSaveGameName(const char *file, char *title, const char *last); /** * Compare two FiosItem's. Used with sort when sorting the file list. - * @param da A pointer to the first FiosItem to compare. - * @param db A pointer to the second FiosItem to compare. - * @return -1, 0 or 1, depending on how the two items should be sorted. + * @param other The FiosItem to compare to. + * @return for ascending order: returns true if da < db. Vice versa for descending order. */ -int CDECL CompareFiosItems(const FiosItem *da, const FiosItem *db) +bool FiosItem::operator< (const FiosItem &other) const { - int r = 0; + bool r = false; - if ((_savegame_sort_order & SORT_BY_NAME) == 0 && da->mtime != db->mtime) { - r = da->mtime < db->mtime ? -1 : 1; + if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) { + r = (*this).mtime < other.mtime; } else { - r = strcasecmp(da->title, db->title); + r = strcasecmp((*this).title, other.title) < 0; } - if (_savegame_sort_order & SORT_DESCENDING) r = -r; + if (_savegame_sort_order & SORT_DESCENDING) r = !r; return r; } @@ -380,7 +379,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c { SortingBits order = _savegame_sort_order; _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; - QSortT(file_list.files.data(), file_list.files.size(), CompareFiosItems); + std::sort(file_list.files.begin(), file_list.files.end()); _savegame_sort_order = order; } @@ -395,7 +394,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c scanner.Scan(nullptr, subdir, true, true); } - QSortT(file_list.Get(sort_start), file_list.Length() - sort_start, CompareFiosItems); + std::sort(file_list.files.begin() + sort_start, file_list.files.end()); /* Show drives */ FiosGetDrives(file_list); diff --git a/src/fios.h b/src/fios.h index 77bc6c1474..e22e14c0b5 100644 --- a/src/fios.h +++ b/src/fios.h @@ -109,6 +109,7 @@ struct FiosItem { uint64 mtime; char title[64]; char name[MAX_PATH]; + bool operator< (const FiosItem &other) const; }; /** List of file information. */ @@ -229,6 +230,4 @@ void FiosMakeSavegameName(char *buf, const char *name, const char *last); FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last); -int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b); - #endif /* FIOS_H */ diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 88eb1a1526..fe4fa469fd 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -260,8 +260,7 @@ static void SortSaveGameList(FileList &file_list) } } - size_t s_amount = file_list.Length() - sort_start - sort_end; - QSortT(file_list.Get(sort_start), s_amount, CompareFiosItems); + std::sort(file_list.files.begin() + sort_start, file_list.files.end() - sort_end); } void SaveGameConfirmationCallback(Window *w, bool confirmed); diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp index 5dc565dfe3..8981e2e792 100644 --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -1148,9 +1148,9 @@ private: } /** Sort the company league by performance history */ - static int CDECL PerformanceSorter(const Company * const *c1, const Company * const *c2) + static bool PerformanceSorter(const Company * const &c1, const Company * const &c2) { - return (*c2)->old_economy[0].performance_history - (*c1)->old_economy[0].performance_history; + return c2->old_economy[0].performance_history < c1->old_economy[0].performance_history; } public: diff --git a/src/group_gui.cpp b/src/group_gui.cpp index e836754280..ce2812979a 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -105,26 +105,26 @@ static const NWidgetPart _nested_group_widgets[] = { }; /** Sort the groups by their name */ -int CDECL GroupNameSorter(const Group * const *a, const Group * const *b) +bool GroupNameSorter(const Group * const &a, const Group * const &b) { static const Group *last_group[2] = { nullptr, nullptr }; static char last_name[2][64] = { "", "" }; - if (*a != last_group[0]) { - last_group[0] = *a; - SetDParam(0, (*a)->index); + if (a != last_group[0]) { + last_group[0] = a; + SetDParam(0, a->index); GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0])); } - if (*b != last_group[1]) { - last_group[1] = *b; - SetDParam(0, (*b)->index); + if (b != last_group[1]) { + last_group[1] = b; + SetDParam(0, b->index); GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1])); } int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting). - if (r == 0) return (*a)->index - (*b)->index; - return r; + if (r == 0) return a->index < b->index; + return r < 0; } class VehicleGroupWindow : public BaseVehicleListWindow { diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 534d01ba5d..b41888feaa 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -157,8 +157,7 @@ Industry::~Industry() } if (GetIndustrySpec(this->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) { - TileArea ta(this->location.tile - TileDiffXY(min(TileX(this->location.tile), 21), min(TileY(this->location.tile), 21)), 42, 42); - ta.ClampToMap(); + TileArea ta = TileArea(this->location.tile, 0, 0).Expand(21); /* Remove the farmland and convert it to regular tiles over time. */ TILE_AREA_LOOP(tile_cur, ta) { @@ -1541,6 +1540,8 @@ static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, /* Check that all tiles in area and surrounding are clear * this determines that there are no obstructing items */ + /* TileArea::Expand is not used here as we need to abort + * instead of clamping if the bounds cannot expanded. */ TileArea ta(tile + TileDiffXY(-_settings_game.construction.industry_platform, -_settings_game.construction.industry_platform), max_x + 2 + 2 * _settings_game.construction.industry_platform, max_y + 2 + 2 * _settings_game.construction.industry_platform); @@ -1601,9 +1602,7 @@ static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int t /* On a large map with many industries, it may be faster to check an area. */ static const int dmax = 14; if (Industry::GetNumItems() > (size_t) (dmax * dmax * 2)) { - const int tx = TileX(tile); - const int ty = TileY(tile); - TileArea tile_area = TileArea(TileXY(max(0, tx - dmax), max(0, ty - dmax)), TileXY(min(MapMaxX(), tx + dmax), min(MapMaxY(), ty + dmax))); + TileArea tile_area = TileArea(tile, 1, 1).Expand(dmax); TILE_AREA_LOOP(atile, tile_area) { if (GetTileType(atile) == MP_INDUSTRY) { const Industry *i2 = Industry::GetByTile(atile); diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 218c3de085..3485d7eb11 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1252,52 +1252,52 @@ protected: } /** Sort industries by name */ - static int CDECL IndustryNameSorter(const Industry * const *a, const Industry * const *b) + static bool IndustryNameSorter(const Industry * const &a, const Industry * const &b) { static char buf_cache[96]; static char buf[96]; - SetDParam(0, (*a)->index); + SetDParam(0, a->index); GetString(buf, STR_INDUSTRY_NAME, lastof(buf)); - if (*b != last_industry) { - last_industry = *b; - SetDParam(0, (*b)->index); + if (b != last_industry) { + last_industry = b; + SetDParam(0, b->index); GetString(buf_cache, STR_INDUSTRY_NAME, lastof(buf_cache)); } - return strnatcmp(buf, buf_cache); // Sort by name (natural sorting). + return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting). } /** Sort industries by type and name */ - static int CDECL IndustryTypeSorter(const Industry * const *a, const Industry * const *b) + static bool IndustryTypeSorter(const Industry * const &a, const Industry * const &b) { int it_a = 0; - while (it_a != NUM_INDUSTRYTYPES && (*a)->type != _sorted_industry_types[it_a]) it_a++; + while (it_a != NUM_INDUSTRYTYPES && a->type != _sorted_industry_types[it_a]) it_a++; int it_b = 0; - while (it_b != NUM_INDUSTRYTYPES && (*b)->type != _sorted_industry_types[it_b]) it_b++; + while (it_b != NUM_INDUSTRYTYPES && b->type != _sorted_industry_types[it_b]) it_b++; int r = it_a - it_b; - return (r == 0) ? IndustryNameSorter(a, b) : r; + return (r == 0) ? IndustryNameSorter(a, b) : r < 0; } /** Sort industries by production and name */ - static int CDECL IndustryProductionSorter(const Industry * const *a, const Industry * const *b) + static bool IndustryProductionSorter(const Industry * const &a, const Industry * const &b) { uint prod_a = 0, prod_b = 0; - for (uint i = 0; i < lengthof((*a)->produced_cargo); i++) { - if ((*a)->produced_cargo[i] != CT_INVALID) prod_a += (*a)->last_month_production[i]; - if ((*b)->produced_cargo[i] != CT_INVALID) prod_b += (*b)->last_month_production[i]; + for (uint i = 0; i < lengthof(a->produced_cargo); i++) { + if (a->produced_cargo[i] != CT_INVALID) prod_a += a->last_month_production[i]; + if (b->produced_cargo[i] != CT_INVALID) prod_b += b->last_month_production[i]; } int r = prod_a - prod_b; - return (r == 0) ? IndustryTypeSorter(a, b) : r; + return (r == 0) ? IndustryTypeSorter(a, b) : r < 0; } /** Sort industries by transported cargo and name */ - static int CDECL IndustryTransportedCargoSorter(const Industry * const *a, const Industry * const *b) + static bool IndustryTransportedCargoSorter(const Industry * const &a, const Industry * const &b) { - int r = GetCargoTransportedSortValue(*a) - GetCargoTransportedSortValue(*b); - return (r == 0) ? IndustryNameSorter(a, b) : r; + int r = GetCargoTransportedSortValue(a) - GetCargoTransportedSortValue(b); + return (r == 0) ? IndustryNameSorter(a, b) : r < 0; } /** diff --git a/src/lang/luxembourgish.txt b/src/lang/luxembourgish.txt index 7f43100f59..5c88d7f662 100644 --- a/src/lang/luxembourgish.txt +++ b/src/lang/luxembourgish.txt @@ -3492,6 +3492,7 @@ STR_BUY_VEHICLE_ROAD_VEHICLE_BUY_VEHICLE_TOOLTIP :{BLACK}Keeft da STR_BUY_VEHICLE_SHIP_BUY_VEHICLE_TOOLTIP :{BLACK}Keeft dat ugewielte Schëff. Shift+Klick weist ongeféier Käschten ouni Kaf STR_BUY_VEHICLE_AIRCRAFT_BUY_VEHICLE_TOOLTIP :{BLACK}Keeft den ungewielte Fliger. Shift+Klick weist ongeféier Käschten ouni Kaf +STR_BUY_VEHICLE_TRAIN_BUY_REFIT_VEHICLE_TOOLTIP :{BLACK}Gewielten Zuch/Waggon kafen an ëmbauen. Shift+Klick weist ongeféier Käschten ouni Kaf STR_BUY_VEHICLE_AIRCRAFT_BUY_REFIT_VEHICLE_TOOLTIP :{BLACK}Gewielte Fliger kafen an ëmbauen. Shift+Klick weist ongeféier Käschten ouni Kaf STR_BUY_VEHICLE_TRAIN_RENAME_BUTTON :{BLACK}Ëmbenennen diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index f3eb597a67..875a5b77c6 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -405,28 +405,28 @@ class NetworkContentListWindow : public Window, ContentCallback { } /** Sort content by name. */ - static int CDECL NameSorter(const ContentInfo * const *a, const ContentInfo * const *b) + static bool NameSorter(const ContentInfo * const &a, const ContentInfo * const &b) { - return strnatcmp((*a)->name, (*b)->name, true); // Sort by name (natural sorting). + return strnatcmp(a->name, b->name, true) < 0; // Sort by name (natural sorting). } /** Sort content by type. */ - static int CDECL TypeSorter(const ContentInfo * const *a, const ContentInfo * const *b) + static bool TypeSorter(const ContentInfo * const &a, const ContentInfo * const &b) { int r = 0; - if ((*a)->type != (*b)->type) { - r = strnatcmp(content_type_strs[(*a)->type], content_type_strs[(*b)->type]); + if (a->type != b->type) { + r = strnatcmp(content_type_strs[a->type], content_type_strs[b->type]); } - if (r == 0) r = NameSorter(a, b); - return r; + if (r == 0) return NameSorter(a, b); + return r < 0; } /** Sort content by state. */ - static int CDECL StateSorter(const ContentInfo * const *a, const ContentInfo * const *b) + static bool StateSorter(const ContentInfo * const &a, const ContentInfo * const &b) { - int r = (*a)->state - (*b)->state; - if (r == 0) r = TypeSorter(a, b); - return r; + int r = a->state - b->state; + if (r == 0) return TypeSorter(a, b); + return r < 0; } /** Sort the content list */ diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 1d152259a7..73dd1cd8db 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -279,10 +279,10 @@ protected: } /** Sort servers by name. */ - static int CDECL NGameNameSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool NGameNameSorter(NetworkGameList * const &a, NetworkGameList * const &b) { - int r = strnatcmp((*a)->info.server_name, (*b)->info.server_name, true); // Sort by name (natural sorting). - return r == 0 ? (*a)->address.CompareTo((*b)->address) : r; + int r = strnatcmp(a->info.server_name, b->info.server_name, true); // Sort by name (natural sorting). + return r == 0 ? a->address.CompareTo(b->address) < 0: r < 0; } /** @@ -290,60 +290,60 @@ protected: * server. If the two servers have the same amount, the one with the * higher maximum is preferred. */ - static int CDECL NGameClientSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool NGameClientSorter(NetworkGameList * const &a, NetworkGameList * const &b) { /* Reverse as per default we are interested in most-clients first */ - int r = (*a)->info.clients_on - (*b)->info.clients_on; + int r = a->info.clients_on - b->info.clients_on; - if (r == 0) r = (*a)->info.clients_max - (*b)->info.clients_max; - if (r == 0) r = NGameNameSorter(a, b); + if (r == 0) r = a->info.clients_max - b->info.clients_max; + if (r == 0) return NGameNameSorter(a, b); - return r; + return r < 0; } /** Sort servers by map size */ - static int CDECL NGameMapSizeSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool NGameMapSizeSorter(NetworkGameList * const &a, NetworkGameList * const &b) { /* Sort by the area of the map. */ - int r = ((*a)->info.map_height) * ((*a)->info.map_width) - ((*b)->info.map_height) * ((*b)->info.map_width); + int r = (a->info.map_height) * (a->info.map_width) - (b->info.map_height) * (b->info.map_width); - if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width; - return (r != 0) ? r : NGameClientSorter(a, b); + if (r == 0) r = a->info.map_width - b->info.map_width; + return (r != 0) ? r < 0 : NGameClientSorter(a, b); } /** Sort servers by current date */ - static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool NGameDateSorter(NetworkGameList * const &a, NetworkGameList * const &b) { - int r = (*a)->info.game_date - (*b)->info.game_date; - return (r != 0) ? r : NGameClientSorter(a, b); + int r = a->info.game_date - b->info.game_date; + return (r != 0) ? r < 0 : NGameClientSorter(a, b); } /** Sort servers by the number of days the game is running */ - static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool NGameYearsSorter(NetworkGameList * const &a, NetworkGameList * const &b) { - int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date; - return (r != 0) ? r : NGameDateSorter(a, b); + int r = a->info.game_date - a->info.start_date - b->info.game_date + b->info.start_date; + return (r != 0) ? r < 0: NGameDateSorter(a, b); } /** * Sort servers by joinability. If both servers are the * same, prefer the non-passworded server first. */ - static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool NGameAllowedSorter(NetworkGameList * const &a, NetworkGameList * const &b) { /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */ - int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision); + int r = StrEmpty(a->info.server_revision) - StrEmpty(b->info.server_revision); /* Reverse default as we are interested in version-compatible clients first */ - if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible; + if (r == 0) r = b->info.version_compatible - a->info.version_compatible; /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */ - if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible; + if (r == 0) r = b->info.compatible - a->info.compatible; /* Passworded servers should be below unpassworded servers */ - if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password; + if (r == 0) r = a->info.use_password - b->info.use_password; /* Finally sort on the number of clients of the server */ - if (r == 0) r = -NGameClientSorter(a, b); + if (r == 0) return NGameClientSorter(a, b); - return r; + return r < 0; } /** Sort the server list */ diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 10518918c8..5446229cec 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -9506,27 +9506,27 @@ static void DecodeSpecialSprite(byte *buf, uint num, GrfLoadingStage stage) * is not in memory and scanning the file every time would be too expensive. * In other stages we skip action 0x10 since it's already dealt with. */ static const SpecialSpriteHandler handlers[][GLS_END] = { - /* 0x00 */ { nullptr, SafeChangeInfo, nullptr, nullptr, ReserveChangeInfo, FeatureChangeInfo, }, - /* 0x01 */ { SkipAct1, SkipAct1, SkipAct1, SkipAct1, SkipAct1, NewSpriteSet, }, - /* 0x02 */ { nullptr, nullptr, nullptr, nullptr, nullptr, NewSpriteGroup, }, - /* 0x03 */ { nullptr, GRFUnsafe, nullptr, nullptr, nullptr, FeatureMapSpriteGroup, }, - /* 0x04 */ { nullptr, nullptr, nullptr, nullptr, nullptr, FeatureNewName, }, - /* 0x05 */ { SkipAct5, SkipAct5, SkipAct5, SkipAct5, SkipAct5, GraphicsNew, }, - /* 0x06 */ { nullptr, nullptr, nullptr, CfgApply, CfgApply, CfgApply, }, - /* 0x07 */ { nullptr, nullptr, nullptr, nullptr, SkipIf, SkipIf, }, - /* 0x08 */ { ScanInfo, nullptr, nullptr, GRFInfo, GRFInfo, GRFInfo, }, - /* 0x09 */ { nullptr, nullptr, nullptr, SkipIf, SkipIf, SkipIf, }, - /* 0x0A */ { SkipActA, SkipActA, SkipActA, SkipActA, SkipActA, SpriteReplace, }, - /* 0x0B */ { nullptr, nullptr, nullptr, GRFLoadError, GRFLoadError, GRFLoadError, }, - /* 0x0C */ { nullptr, nullptr, nullptr, GRFComment, nullptr, GRFComment, }, - /* 0x0D */ { nullptr, SafeParamSet, nullptr, ParamSet, ParamSet, ParamSet, }, - /* 0x0E */ { nullptr, SafeGRFInhibit, nullptr, GRFInhibit, GRFInhibit, GRFInhibit, }, - /* 0x0F */ { nullptr, GRFUnsafe, nullptr, FeatureTownName, nullptr, nullptr, }, - /* 0x10 */ { nullptr, nullptr, DefineGotoLabel, nullptr, nullptr, nullptr, }, - /* 0x11 */ { SkipAct11,GRFUnsafe, SkipAct11, GRFSound, SkipAct11, GRFSound, }, - /* 0x12 */ { SkipAct12, SkipAct12, SkipAct12, SkipAct12, SkipAct12, LoadFontGlyph, }, - /* 0x13 */ { nullptr, nullptr, nullptr, nullptr, nullptr, TranslateGRFStrings, }, - /* 0x14 */ { StaticGRFInfo, nullptr, nullptr, Act14FeatureTest, nullptr, nullptr, }, + /* 0x00 */ { nullptr, SafeChangeInfo, nullptr, nullptr, ReserveChangeInfo, FeatureChangeInfo, }, + /* 0x01 */ { SkipAct1, SkipAct1, SkipAct1, SkipAct1, SkipAct1, NewSpriteSet, }, + /* 0x02 */ { nullptr, nullptr, nullptr, nullptr, nullptr, NewSpriteGroup, }, + /* 0x03 */ { nullptr, GRFUnsafe, nullptr, nullptr, nullptr, FeatureMapSpriteGroup, }, + /* 0x04 */ { nullptr, nullptr, nullptr, nullptr, nullptr, FeatureNewName, }, + /* 0x05 */ { SkipAct5, SkipAct5, SkipAct5, SkipAct5, SkipAct5, GraphicsNew, }, + /* 0x06 */ { nullptr, nullptr, nullptr, CfgApply, CfgApply, CfgApply, }, + /* 0x07 */ { nullptr, nullptr, nullptr, nullptr, SkipIf, SkipIf, }, + /* 0x08 */ { ScanInfo, nullptr, nullptr, GRFInfo, GRFInfo, GRFInfo, }, + /* 0x09 */ { nullptr, nullptr, nullptr, SkipIf, SkipIf, SkipIf, }, + /* 0x0A */ { SkipActA, SkipActA, SkipActA, SkipActA, SkipActA, SpriteReplace, }, + /* 0x0B */ { nullptr, nullptr, nullptr, GRFLoadError, GRFLoadError, GRFLoadError, }, + /* 0x0C */ { nullptr, nullptr, nullptr, GRFComment, nullptr, GRFComment, }, + /* 0x0D */ { nullptr, SafeParamSet, nullptr, ParamSet, ParamSet, ParamSet, }, + /* 0x0E */ { nullptr, SafeGRFInhibit, nullptr, GRFInhibit, GRFInhibit, GRFInhibit, }, + /* 0x0F */ { nullptr, GRFUnsafe, nullptr, FeatureTownName, nullptr, nullptr, }, + /* 0x10 */ { nullptr, nullptr, DefineGotoLabel, nullptr, nullptr, nullptr, }, + /* 0x11 */ { SkipAct11, GRFUnsafe, SkipAct11, GRFSound, SkipAct11, GRFSound, }, + /* 0x12 */ { SkipAct12, SkipAct12, SkipAct12, SkipAct12, SkipAct12, LoadFontGlyph, }, + /* 0x13 */ { nullptr, nullptr, nullptr, nullptr, nullptr, TranslateGRFStrings, }, + /* 0x14 */ { StaticGRFInfo, nullptr, nullptr, Act14FeatureTest,nullptr, nullptr, }, }; GRFLocation location(_cur.grfconfig->ident.grfid, _cur.nfo_line); diff --git a/src/newgrf.h b/src/newgrf.h index 528f9b8602..9684597522 100644 --- a/src/newgrf.h +++ b/src/newgrf.h @@ -226,7 +226,7 @@ struct GRFFile : ZeroedMemoryAllocator { GRFFilePropertyRemapSet action0_property_remaps[GSF_END]; Action5TypeRemapSet action5_type_remaps; - std::vector> remap_unknown_property_names; + std::vector> remap_unknown_property_names; uint32 param[0x80]; uint param_end; ///< one more than the highest set parameter @@ -277,7 +277,7 @@ struct GRFLoadedFeatures { uint64 used_liveries; ///< Bitmask of #LiveryScheme used by the defined engines. bool has_newhouses; ///< Set if there are any newhouses loaded. bool has_newindustries; ///< Set if there are any newindustries loaded. - ShoreReplacement shore; ///< It which way shore sprites were replaced. + ShoreReplacement shore; ///< In which way shore sprites were replaced. }; /** diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 4c0ed98cb8..e997138f4d 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -1211,19 +1211,19 @@ void AlterVehicleListOrder(EngineID engine, uint target) * @param b right side * @return comparison result */ -static int CDECL EnginePreSort(const EngineID *a, const EngineID *b) +static bool EnginePreSort(const EngineID &a, const EngineID &b) { - const EngineIDMapping &id_a = _engine_mngr.at(*a); - const EngineIDMapping &id_b = _engine_mngr.at(*b); + const EngineIDMapping &id_a = _engine_mngr.at(a); + const EngineIDMapping &id_b = _engine_mngr.at(b); /* 1. Sort by engine type */ - if (id_a.type != id_b.type) return (int)id_a.type - (int)id_b.type; + if (id_a.type != id_b.type) return (int)id_a.type < (int)id_b.type; /* 2. Sort by scope-GRFID */ - if (id_a.grfid != id_b.grfid) return id_a.grfid < id_b.grfid ? -1 : 1; + if (id_a.grfid != id_b.grfid) return id_a.grfid < id_b.grfid; /* 3. Sort by local ID */ - return (int)id_a.internal_id - (int)id_b.internal_id; + return (int)id_a.internal_id < (int)id_b.internal_id; } /** @@ -1237,7 +1237,7 @@ void CommitVehicleListOrderChanges() FOR_ALL_ENGINES(e) { ordering.push_back(e->index); } - QSortT(ordering.data(), ordering.size(), EnginePreSort); + std::sort(ordering.begin(), ordering.end(), EnginePreSort); /* Apply Insertion-Sort operations */ for (const ListOrderChange &it : _list_order_changes) { diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index e8df599eeb..dd4cfd104a 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -1429,15 +1429,15 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { private: /** Sort grfs by name. */ - static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b) + static bool NameSorter(const GRFConfig * const &a, const GRFConfig * const &b) { - int i = strnatcmp((*a)->GetName(), (*b)->GetName(), true); // Sort by name (natural sorting). - if (i != 0) return i; + int i = strnatcmp(a->GetName(), b->GetName(), true); // Sort by name (natural sorting). + if (i != 0) return i < 0; - i = (*a)->version - (*b)->version; - if (i != 0) return i; + i = a->version - b->version; + if (i != 0) return i < 0; - return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum)); + return memcmp(a->ident.md5sum, b->ident.md5sum, lengthof(b->ident.md5sum)) < 0; } /** Filter grfs by tags/name */ diff --git a/src/order_gui.cpp b/src/order_gui.cpp index 3d8fcf5e78..10564a79ef 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -993,7 +993,7 @@ static Order GetOrderCmdFromTile(const Vehicle *v, TileIndex tile) case VEH_SHIP: facil = FACIL_DOCK; break; case VEH_TRAIN: facil = FACIL_TRAIN; break; case VEH_AIRCRAFT: facil = FACIL_AIRPORT; break; - case VEH_ROAD: facil = RoadVehicle::From(v)->IsBus() ? FACIL_BUS_STOP : FACIL_TRUCK_STOP; break; + case VEH_ROAD: facil = FACIL_BUS_STOP | FACIL_TRUCK_STOP; break; default: NOT_REACHED(); } if (st->facilities & facil) { diff --git a/src/saveload/economy_sl.cpp b/src/saveload/economy_sl.cpp index c43be14b93..0efca3939f 100644 --- a/src/saveload/economy_sl.cpp +++ b/src/saveload/economy_sl.cpp @@ -102,7 +102,7 @@ static void Ptrs_CAPY() extern const ChunkHandler _economy_chunk_handlers[] = { { 'CAPY', Save_CAPY, Load_CAPY, Ptrs_CAPY, nullptr, CH_ARRAY}, - { 'PRIC', nullptr, Load_PRIC, nullptr, nullptr, CH_RIFF }, - { 'CAPR', nullptr, Load_CAPR, nullptr, nullptr, CH_RIFF }, - { 'ECMY', Save_ECMY, Load_ECMY, nullptr, nullptr, CH_RIFF | CH_LAST}, + { 'PRIC', nullptr, Load_PRIC, nullptr, nullptr, CH_RIFF }, + { 'CAPR', nullptr, Load_CAPR, nullptr, nullptr, CH_RIFF }, + { 'ECMY', Save_ECMY, Load_ECMY, nullptr, nullptr, CH_RIFF | CH_LAST}, }; diff --git a/src/saveload/engine_sl.cpp b/src/saveload/engine_sl.cpp index 0fe190a0ff..f4c017bb16 100644 --- a/src/saveload/engine_sl.cpp +++ b/src/saveload/engine_sl.cpp @@ -199,5 +199,5 @@ static void Load_EIDS() extern const ChunkHandler _engine_chunk_handlers[] = { { 'EIDS', Save_EIDS, Load_EIDS, nullptr, nullptr, CH_ARRAY }, { 'ENGN', Save_ENGN, Load_ENGN, nullptr, nullptr, CH_ARRAY }, - { 'ENGS', nullptr, Load_ENGS, nullptr, nullptr, CH_RIFF | CH_LAST }, + { 'ENGS', nullptr, Load_ENGS, nullptr, nullptr, CH_RIFF | CH_LAST }, }; diff --git a/src/saveload/industry_sl.cpp b/src/saveload/industry_sl.cpp index 391e5fbc3c..34fa14542f 100644 --- a/src/saveload/industry_sl.cpp +++ b/src/saveload/industry_sl.cpp @@ -184,8 +184,8 @@ static void Load_ITBL() extern const ChunkHandler _industry_chunk_handlers[] = { { 'INDY', Save_INDY, Load_INDY, Ptrs_INDY, nullptr, CH_ARRAY}, - { 'IIDS', Save_IIDS, Load_IIDS, nullptr, nullptr, CH_ARRAY}, - { 'TIDS', Save_TIDS, Load_TIDS, nullptr, nullptr, CH_ARRAY}, - { 'IBLD', LoadSave_IBLD, LoadSave_IBLD, nullptr, nullptr, CH_RIFF}, - { 'ITBL', Save_ITBL, Load_ITBL, nullptr, nullptr, CH_ARRAY | CH_LAST}, + { 'IIDS', Save_IIDS, Load_IIDS, nullptr, nullptr, CH_ARRAY}, + { 'TIDS', Save_TIDS, Load_TIDS, nullptr, nullptr, CH_ARRAY}, + { 'IBLD', LoadSave_IBLD, LoadSave_IBLD, nullptr, nullptr, CH_RIFF}, + { 'ITBL', Save_ITBL, Load_ITBL, nullptr, nullptr, CH_ARRAY | CH_LAST}, }; diff --git a/src/saveload/linkgraph_sl.cpp b/src/saveload/linkgraph_sl.cpp index 3fd1b93e76..71c4b4db3c 100644 --- a/src/saveload/linkgraph_sl.cpp +++ b/src/saveload/linkgraph_sl.cpp @@ -301,7 +301,7 @@ static void Ptrs_LGRS() } extern const ChunkHandler _linkgraph_chunk_handlers[] = { - { 'LGRP', Save_LGRP, Load_LGRP, nullptr, nullptr, CH_ARRAY }, - { 'LGRJ', Save_LGRJ, Load_LGRJ, nullptr, nullptr, CH_ARRAY }, + { 'LGRP', Save_LGRP, Load_LGRP, nullptr, nullptr, CH_ARRAY }, + { 'LGRJ', Save_LGRJ, Load_LGRJ, nullptr, nullptr, CH_ARRAY }, { 'LGRS', Save_LGRS, Load_LGRS, Ptrs_LGRS, nullptr, CH_LAST } }; diff --git a/src/saveload/map_sl.cpp b/src/saveload/map_sl.cpp index e78129f3eb..a994874865 100644 --- a/src/saveload/map_sl.cpp +++ b/src/saveload/map_sl.cpp @@ -15,6 +15,7 @@ #include "../core/endian_func.hpp" #include "../core/endian_type.hpp" #include "../fios.h" +#include #include "saveload.h" #include "saveload_buffer.h" @@ -57,44 +58,44 @@ static const uint MAP_SL_BUF_SIZE = 4096; static void Load_MAPT() { - SmallStackSafeStackAlloc buf; + std::array buf; TileIndex size = MapSize(); for (TileIndex i = 0; i != size;) { - SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8); + SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].type = buf[j]; } } static void Load_MAPH() { - SmallStackSafeStackAlloc buf; + std::array buf; TileIndex size = MapSize(); for (TileIndex i = 0; i != size;) { - SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8); + SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].height = buf[j]; } } static void Load_MAP1() { - SmallStackSafeStackAlloc buf; + std::array buf; TileIndex size = MapSize(); for (TileIndex i = 0; i != size;) { - SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8); + SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m1 = buf[j]; } } static void Load_MAP2() { - SmallStackSafeStackAlloc buf; + std::array buf; TileIndex size = MapSize(); for (TileIndex i = 0; i != size;) { - SlArray(buf, MAP_SL_BUF_SIZE, + SlArray(buf.data(), MAP_SL_BUF_SIZE, /* In those versions the m2 was 8 bits */ IsSavegameVersionBefore(SLV_5) ? SLE_FILE_U8 | SLE_VAR_U16 : SLE_UINT16 ); @@ -104,46 +105,46 @@ static void Load_MAP2() static void Load_MAP3() { - SmallStackSafeStackAlloc buf; + std::array buf; TileIndex size = MapSize(); for (TileIndex i = 0; i != size;) { - SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8); + SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m3 = buf[j]; } } static void Load_MAP4() { - SmallStackSafeStackAlloc buf; + std::array buf; TileIndex size = MapSize(); for (TileIndex i = 0; i != size;) { - SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8); + SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m4 = buf[j]; } } static void Load_MAP5() { - SmallStackSafeStackAlloc buf; + std::array buf; TileIndex size = MapSize(); for (TileIndex i = 0; i != size;) { - SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8); + SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m5 = buf[j]; } } static void Load_MAP6() { - SmallStackSafeStackAlloc buf; + std::array buf; TileIndex size = MapSize(); if (IsSavegameVersionBefore(SLV_42)) { for (TileIndex i = 0; i != size;) { /* 1024, otherwise we overflow on 64x64 maps! */ - SlArray(buf, 1024, SLE_UINT8); + SlArray(buf.data(), 1024, SLE_UINT8); for (uint j = 0; j != 1024; j++) { _me[i++].m6 = GB(buf[j], 0, 2); _me[i++].m6 = GB(buf[j], 2, 2); @@ -153,7 +154,7 @@ static void Load_MAP6() } } else { for (TileIndex i = 0; i != size;) { - SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8); + SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _me[i++].m6 = buf[j]; } } @@ -161,22 +162,22 @@ static void Load_MAP6() static void Load_MAP7() { - SmallStackSafeStackAlloc buf; + std::array buf; TileIndex size = MapSize(); for (TileIndex i = 0; i != size;) { - SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8); + SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _me[i++].m7 = buf[j]; } } static void Load_MAP8() { - SmallStackSafeStackAlloc buf; + std::array buf; TileIndex size = MapSize(); for (TileIndex i = 0; i != size;) { - SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT16); + SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT16); for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _me[i++].m8 = buf[j]; } } @@ -278,5 +279,5 @@ extern const ChunkHandler _map_chunk_handlers[] = { { 'MAPE', nullptr, Load_MAP6, nullptr, nullptr, CH_RIFF }, { 'MAP7', nullptr, Load_MAP7, nullptr, nullptr, CH_RIFF }, { 'MAP8', nullptr, Load_MAP8, nullptr, nullptr, CH_RIFF }, - { 'WMAP', Save_WMAP, Load_WMAP, nullptr, nullptr, CH_RIFF | CH_LAST }, + { 'WMAP', Save_WMAP, Load_WMAP, nullptr, nullptr, CH_RIFF | CH_LAST }, }; diff --git a/src/saveload/misc_sl.cpp b/src/saveload/misc_sl.cpp index 14efd17fb5..130d149a1b 100644 --- a/src/saveload/misc_sl.cpp +++ b/src/saveload/misc_sl.cpp @@ -155,5 +155,5 @@ static void SaveLoad_VIEW() extern const ChunkHandler _misc_chunk_handlers[] = { { 'DATE', SaveLoad_DATE, SaveLoad_DATE, nullptr, Check_DATE, CH_RIFF}, - { 'VIEW', SaveLoad_VIEW, SaveLoad_VIEW, nullptr, nullptr, CH_RIFF | CH_LAST}, + { 'VIEW', SaveLoad_VIEW, SaveLoad_VIEW, nullptr, nullptr, CH_RIFF | CH_LAST}, }; diff --git a/src/saveload/object_sl.cpp b/src/saveload/object_sl.cpp index 2ec9966649..7d7f2fde9e 100644 --- a/src/saveload/object_sl.cpp +++ b/src/saveload/object_sl.cpp @@ -74,6 +74,6 @@ static void Load_OBID() } extern const ChunkHandler _object_chunk_handlers[] = { - { 'OBID', Save_OBID, Load_OBID, nullptr, nullptr, CH_ARRAY }, + { 'OBID', Save_OBID, Load_OBID, nullptr, nullptr, CH_ARRAY }, { 'OBJS', Save_OBJS, Load_OBJS, Ptrs_OBJS, nullptr, CH_ARRAY | CH_LAST}, }; diff --git a/src/saveload/oldloader.h b/src/saveload/oldloader.h index a2953196b5..12d8a348e5 100644 --- a/src/saveload/oldloader.h +++ b/src/saveload/oldloader.h @@ -125,12 +125,12 @@ static inline uint32 ReadUint32(LoadgameState *ls) * - OCL_CHUNK: load another proc to load a part of the savegame, 'amount' times * - OCL_ASSERT: to check if we are really at the place we expect to be.. because old savegames are too binary to be sure ;) */ -#define OCL_SVAR(type, base, offset) { type, 1, nullptr, (uint)cpp_offsetof(base, offset), nullptr } +#define OCL_SVAR(type, base, offset) { type, 1, nullptr, (uint)cpp_offsetof(base, offset), nullptr } #define OCL_VAR(type, amount, pointer) { type, amount, pointer, 0, nullptr } -#define OCL_END() { OC_END, 0, nullptr, 0, nullptr } -#define OCL_CNULL(type, amount) { OC_NULL | type, amount, nullptr, 0, nullptr } -#define OCL_CCHUNK(type, amount, proc) { OC_CHUNK | type, amount, nullptr, 0, proc } -#define OCL_ASSERT(type, size) { OC_ASSERT | type, 1, nullptr, size, nullptr } +#define OCL_END() { OC_END, 0, nullptr, 0, nullptr } +#define OCL_CNULL(type, amount) { OC_NULL | type, amount, nullptr, 0, nullptr } +#define OCL_CCHUNK(type, amount, proc) { OC_CHUNK | type, amount, nullptr, 0, proc } +#define OCL_ASSERT(type, size) { OC_ASSERT | type, 1, nullptr, size, nullptr } #define OCL_NULL(amount) OCL_CNULL((OldChunkType)0, amount) #define OCL_CHUNK(amount, proc) OCL_CCHUNK((OldChunkType)0, amount, proc) diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 23043a89cf..2b53eb3fc4 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -31,6 +31,7 @@ #include "../core/smallvec_type.hpp" #include "saveload_internal.h" #include "oldloader.h" +#include #include "table/strings.h" #include "../table/engines.h" @@ -1753,8 +1754,8 @@ bool LoadTTDMain(LoadgameState *ls) _read_ttdpatch_flags = false; /* Load the biggest chunk */ - SmallStackSafeStackAlloc map3; - _old_map3 = map3.data; + std::array map3; + _old_map3 = map3.data(); _old_vehicle_names = nullptr; try { if (!LoadChunk(ls, nullptr, main_chunk)) { @@ -1796,10 +1797,10 @@ bool LoadTTOMain(LoadgameState *ls) _read_ttdpatch_flags = false; - SmallStackSafeStackAlloc engines; // we don't want to call Engine constructor here - _old_engines = (Engine *)engines.data; - SmallStackSafeStackAlloc vehnames; - _old_vehicle_names = vehnames.data; + std::array engines; // we don't want to call Engine constructor here + _old_engines = (Engine *)engines.data(); + std::array vehnames; + _old_vehicle_names = vehnames.data(); /* Load the biggest chunk */ if (!LoadChunk(ls, nullptr, main_chunk)) { diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index f8e42d85c0..e8929fc4b4 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2562,7 +2562,7 @@ static const SaveLoadFormat _saveload_formats[] = { /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */ {"lzo", TO_BE32X('OTTD'), CreateLoadFilter, CreateSaveFilter, 0, 0, 0}, #else - {"lzo", TO_BE32X('OTTD'), nullptr, nullptr, 0, 0, 0}, + {"lzo", TO_BE32X('OTTD'), nullptr, nullptr, 0, 0, 0}, #endif /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */ {"none", TO_BE32X('OTTN'), CreateLoadFilter, CreateSaveFilter, 0, 0, 0}, @@ -2572,7 +2572,7 @@ static const SaveLoadFormat _saveload_formats[] = { * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */ {"zlib", TO_BE32X('OTTZ'), CreateLoadFilter, CreateSaveFilter, 0, 6, 9}, #else - {"zlib", TO_BE32X('OTTZ'), nullptr, nullptr, 0, 0, 0}, + {"zlib", TO_BE32X('OTTZ'), nullptr, nullptr, 0, 0, 0}, #endif #if defined(WITH_LIBLZMA) /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves. @@ -2582,7 +2582,7 @@ static const SaveLoadFormat _saveload_formats[] = { * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */ {"lzma", TO_BE32X('OTTX'), CreateLoadFilter, CreateSaveFilter, 0, 2, 9}, #else - {"lzma", TO_BE32X('OTTX'), nullptr, nullptr, 0, 0, 0}, + {"lzma", TO_BE32X('OTTX'), nullptr, nullptr, 0, 0, 0}, #endif }; diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp index f05bcdec62..74acaf94ea 100644 --- a/src/saveload/station_sl.cpp +++ b/src/saveload/station_sl.cpp @@ -711,7 +711,7 @@ static void Ptrs_DOCK() } extern const ChunkHandler _station_chunk_handlers[] = { - { 'STNS', nullptr, Load_STNS, Ptrs_STNS, nullptr, CH_ARRAY }, + { 'STNS', nullptr, Load_STNS, Ptrs_STNS, nullptr, CH_ARRAY }, { 'STNN', Save_STNN, Load_STNN, Ptrs_STNN, nullptr, CH_ARRAY }, { 'ROAD', Save_ROADSTOP, Load_ROADSTOP, Ptrs_ROADSTOP, nullptr, CH_ARRAY}, { 'DOCK', Save_DOCK, Load_DOCK, Ptrs_DOCK, nullptr, CH_ARRAY | CH_LAST}, diff --git a/src/saveload/story_sl.cpp b/src/saveload/story_sl.cpp index 8cca9aed73..11aba1347a 100644 --- a/src/saveload/story_sl.cpp +++ b/src/saveload/story_sl.cpp @@ -103,5 +103,5 @@ static void Load_STORY_PAGE() extern const ChunkHandler _story_page_chunk_handlers[] = { { 'STPE', Save_STORY_PAGE_ELEMENT, Load_STORY_PAGE_ELEMENT, nullptr, nullptr, CH_ARRAY}, - { 'STPA', Save_STORY_PAGE, Load_STORY_PAGE, nullptr, nullptr, CH_ARRAY | CH_LAST}, + { 'STPA', Save_STORY_PAGE, Load_STORY_PAGE, nullptr, nullptr, CH_ARRAY | CH_LAST}, }; diff --git a/src/saveload/town_sl.cpp b/src/saveload/town_sl.cpp index 3633c072ac..87d033c869 100644 --- a/src/saveload/town_sl.cpp +++ b/src/saveload/town_sl.cpp @@ -341,6 +341,6 @@ static void Ptrs_TOWN() /** Chunk handler for towns. */ extern const ChunkHandler _town_chunk_handlers[] = { - { 'HIDS', Save_HIDS, Load_HIDS, nullptr, nullptr, CH_ARRAY }, + { 'HIDS', Save_HIDS, Load_HIDS, nullptr, nullptr, CH_ARRAY }, { 'CITY', Save_TOWN, Load_TOWN, Ptrs_TOWN, nullptr, CH_ARRAY | CH_LAST}, }; diff --git a/src/script/api/script_tilelist.cpp b/src/script/api/script_tilelist.cpp index 6c4e7abec9..1441278814 100644 --- a/src/script/api/script_tilelist.cpp +++ b/src/script/api/script_tilelist.cpp @@ -49,6 +49,32 @@ void ScriptTileList::RemoveTile(TileIndex tile) this->RemoveItem(tile); } +/** + * Helper to get list of tiles that will cover an industry's production or acceptance. + * @param i Industry in question + * @param radius Catchment radius to test + * @param bta BitmapTileArea to fill + */ +static void FillIndustryCatchment(const Industry *i, int radius, BitmapTileArea &bta) +{ + TILE_AREA_LOOP(cur_tile, i->location) { + if (!::IsTileType(cur_tile, MP_INDUSTRY) || ::GetIndustryIndex(cur_tile) != i->index) continue; + + int tx = TileX(cur_tile); + int ty = TileY(cur_tile); + for (int y = -radius; y <= radius; y++) { + if (ty + y < 0 || ty + y > (int)MapMaxY()) continue; + for (int x = -radius; x <= radius; x++) { + if (tx + x < 0 || tx + x > (int)MapMaxX()) continue; + TileIndex tile = TileXY(tx + x, ty + y); + if (!IsValidTile(tile)) continue; + if (::IsTileType(tile, MP_INDUSTRY) && ::GetIndustryIndex(tile) == i->index) continue; + bta.SetTile(tile); + } + } + } +} + ScriptTileList_IndustryAccepting::ScriptTileList_IndustryAccepting(IndustryID industry_id, int radius) { if (!ScriptIndustry::IsValidIndustry(industry_id) || radius <= 0) return; @@ -66,12 +92,11 @@ ScriptTileList_IndustryAccepting::ScriptTileList_IndustryAccepting(IndustryID in if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED + _settings_game.station.catchment_increase; - TileArea ta(i->location.tile - ::TileDiffXY(radius, radius), i->location.w + radius * 2, i->location.h + radius * 2); - TILE_AREA_LOOP(cur_tile, ta) { - if (!::IsValidTile(cur_tile)) continue; - /* Exclude all tiles that belong to this industry */ - if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue; + BitmapTileArea bta(TileArea(i->location).Expand(radius)); + FillIndustryCatchment(i, radius, bta); + BitmapTileIterator it(bta); + for (TileIndex cur_tile = it; cur_tile != INVALID_TILE; cur_tile = ++it) { /* Only add the tile if it accepts the cargo (sometimes just 1 tile of an * industry triggers the acceptance). */ CargoArray acceptance = ::GetAcceptanceAroundTiles(cur_tile, 1, 1, radius); @@ -102,12 +127,11 @@ ScriptTileList_IndustryProducing::ScriptTileList_IndustryProducing(IndustryID in if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED + _settings_game.station.catchment_increase; - TileArea ta(i->location.tile - ::TileDiffXY(radius, radius), i->location.w + radius * 2, i->location.h + radius * 2); - TILE_AREA_LOOP(cur_tile, ta) { - if (!::IsValidTile(cur_tile)) continue; - /* Exclude all tiles that belong to this industry */ - if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue; + BitmapTileArea bta(TileArea(i->location).Expand(radius)); + FillIndustryCatchment(i, radius, bta); + BitmapTileIterator it(bta); + for (TileIndex cur_tile = it; cur_tile != INVALID_TILE; cur_tile = ++it) { this->AddTile(cur_tile); } } diff --git a/src/signs_gui.cpp b/src/signs_gui.cpp index a06898af7f..d346ec185b 100644 --- a/src/signs_gui.cpp +++ b/src/signs_gui.cpp @@ -72,21 +72,21 @@ struct SignList { } /** Sort signs by their name */ - static int CDECL SignNameSorter(const Sign * const *a, const Sign * const *b) + static bool SignNameSorter(const Sign * const &a, const Sign * const &b) { /* Signs are very very rarely using the default text, but there can also be * a lot of them. Therefore a worthwhile performance gain can be made by * directly comparing Sign::name instead of going through the string * system for each comparison. */ - const char *a_name = (*a)->name; - const char *b_name = (*b)->name; + const char *a_name = a->name; + const char *b_name = b->name; if (a_name == nullptr) a_name = SignList::default_name; if (b_name == nullptr) b_name = SignList::default_name; int r = strnatcmp(a_name, b_name); // Sort by name (natural sorting). - return r != 0 ? r : ((*a)->index - (*b)->index); + return r != 0 ? r < 0 : (a->index < b->index); } void SortSignsList() diff --git a/src/sortlist_type.h b/src/sortlist_type.h index 75730c08d7..8174995cab 100644 --- a/src/sortlist_type.h +++ b/src/sortlist_type.h @@ -49,8 +49,8 @@ struct Filtering { template class GUIList : public std::vector { public: - typedef int CDECL SortFunction(const T*, const T*); ///< Signature of sort function. - typedef bool CDECL FilterFunction(const T*, F); ///< Signature of filter function. + typedef bool SortFunction(const T&, const T&); ///< Signature of sort function. + typedef bool CDECL FilterFunction(const T*, F); ///< Signature of filter function. protected: SortFunction * const *sort_func_list; ///< the sort criteria functions @@ -270,11 +270,11 @@ public: if (this->flags & VL_FIRST_SORT) { CLRBITS(this->flags, VL_FIRST_SORT); - QSortT(std::vector::data(), std::vector::size(), compare, desc); + std::sort(std::vector::begin(), std::vector::end(), [&](const T &a, const T &b) { return desc ? compare(b, a) : compare(a, b); }); return true; } - GSortT(std::vector::data(), std::vector::size(), compare, desc); + std::sort(std::vector::begin(), std::vector::end(), [&](const T &a, const T &b) { return desc ? compare(b, a) : compare(a, b); }); return true; } diff --git a/src/spriteloader/grf.cpp b/src/spriteloader/grf.cpp index a07ab2a041..f65fb61f34 100644 --- a/src/spriteloader/grf.cpp +++ b/src/spriteloader/grf.cpp @@ -69,8 +69,8 @@ static bool WarnCorruptSprite(uint file_slot, size_t file_pos, int line) */ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint file_slot, size_t file_pos, SpriteType sprite_type, int64 num, byte type, ZoomLevel zoom_lvl, byte colour_fmt, byte container_format) { - AutoFreePtr dest_orig(MallocT(num)); - byte *dest = dest_orig; + std::unique_ptr dest_orig(new byte[num]); + byte *dest = dest_orig.get(); const int64 dest_size = num; /* Read the file, which has some kind of compression */ @@ -89,7 +89,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint file_slot, size_t fil } else { /* Copy bytes from earlier in the sprite */ const uint data_offset = ((code & 7) << 8) | FioReadByte(); - if (dest - data_offset < dest_orig) return WarnCorruptSprite(file_slot, file_pos, __LINE__); + if (dest - data_offset < dest_orig.get()) return WarnCorruptSprite(file_slot, file_pos, __LINE__); int size = -(code >> 3); num -= size; if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__); @@ -123,10 +123,10 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint file_slot, size_t fil } /* Go to that row */ - dest = dest_orig + offset; + dest = dest_orig.get() + offset; do { - if (dest + (container_format >= 2 && sprite->width > 256 ? 4 : 2) > dest_orig + dest_size) { + if (dest + (container_format >= 2 && sprite->width > 256 ? 4 : 2) > dest_orig.get() + dest_size) { return WarnCorruptSprite(file_slot, file_pos, __LINE__); } @@ -152,7 +152,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint file_slot, size_t fil data = &sprite->data[y * sprite->width + skip]; - if (skip + length > sprite->width || dest + length * bpp > dest_orig + dest_size) { + if (skip + length > sprite->width || dest + length * bpp > dest_orig.get() + dest_size) { return WarnCorruptSprite(file_slot, file_pos, __LINE__); } @@ -188,7 +188,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint file_slot, size_t fil warning_level = 6; } - dest = dest_orig; + dest = dest_orig.get(); for (int i = 0; i < sprite->width * sprite->height; i++) { byte *pixel = &dest[i * bpp]; diff --git a/src/station.cpp b/src/station.cpp index aebbfb203b..d4ec11f9f1 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -472,7 +472,7 @@ void Station::RecomputeCatchment() if (r == CA_NONE) continue; /* This tile sub-loop doesn't need to test any tiles, they are simply added to the catchment set. */ - TileArea ta2(TileXY(max(TileX(tile) - r, 0), max(TileY(tile) - r, 0)), TileXY(min(TileX(tile) + r, MapMaxX()), min(TileY(tile) + r, MapMaxY()))); + TileArea ta2 = TileArea(tile, 1, 1).Expand(r); TILE_AREA_LOOP(tile2, ta2) this->catchment_tiles.SetTile(tile2); } diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 65080d4ae7..d95f0c6b9a 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -103,9 +103,7 @@ bool IsHangar(TileIndex t) template CommandCost GetStationAround(TileArea ta, StationID closest_station, CompanyID company, T **st) { - ta.tile -= TileDiffXY(1, 1); - ta.w += 2; - ta.h += 2; + ta.Expand(1); /* check around to see if there are any stations there owned by the company */ TILE_AREA_LOOP(tile_cur, ta) { @@ -498,24 +496,8 @@ CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad) { CargoArray produced; - int x = TileX(tile); - int y = TileY(tile); - - /* expand the region by rad tiles on each side - * while making sure that we remain inside the board. */ - int x2 = min(x + w + rad, MapSizeX()); - int x1 = max(x - rad, 0); - - int y2 = min(y + h + rad, MapSizeY()); - int y1 = max(y - rad, 0); - - assert(x1 < x2); - assert(y1 < y2); - assert(w > 0); - assert(h > 0); - btree::btree_set industries; - TileArea ta(TileXY(x1, y1), TileXY(x2 - 1, y2 - 1)); + TileArea ta = TileArea(tile, w, h).Expand(rad); /* Loop over all tiles to get the produced cargo of * everything except industries */ @@ -555,30 +537,13 @@ CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad, Cargo CargoArray acceptance; if (always_accepted != nullptr) *always_accepted = 0; - int x = TileX(tile); - int y = TileY(tile); + TileArea ta = TileArea(tile, w, h).Expand(rad); - /* expand the region by rad tiles on each side - * while making sure that we remain inside the board. */ - int x2 = min(x + w + rad, MapSizeX()); - int y2 = min(y + h + rad, MapSizeY()); - int x1 = max(x - rad, 0); - int y1 = max(y - rad, 0); + TILE_AREA_LOOP(tile, ta) { + /* Ignore industry if it has a neutral station. */ + if (!_settings_game.station.serve_neutral_industries && IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile)->neutral_station != nullptr) continue; - assert(x1 < x2); - assert(y1 < y2); - assert(w > 0); - assert(h > 0); - - for (int yc = y1; yc != y2; yc++) { - for (int xc = x1; xc != x2; xc++) { - TileIndex tile = TileXY(xc, yc); - - /* Ignore industry if it has a neutral station. */ - if (!_settings_game.station.serve_neutral_industries && IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile)->neutral_station != nullptr) continue; - - AddAcceptedCargo(tile, acceptance, always_accepted); - } + AddAcceptedCargo(tile, acceptance, always_accepted); } return acceptance; @@ -4083,8 +4048,6 @@ void FindStationsAroundTiles(const TileArea &location, StationList * const stati } /* Not using, or don't have a nearby stations list, so we need to scan. */ - uint x = TileX(location.tile); - uint y = TileY(location.tile); btree::btree_set seen_stations; @@ -4092,7 +4055,7 @@ void FindStationsAroundTiles(const TileArea &location, StationList * const stati * to find the possible nearby stations. */ uint max_c = _settings_game.station.modified_catchment ? MAX_CATCHMENT : CA_UNMODIFIED; max_c += _settings_game.station.catchment_increase; - TileArea ta(TileXY(max(0, x - max_c), max(0, y - max_c)), TileXY(min(MapMaxX(), x + location.w + max_c), min(MapMaxY(), y + location.h + max_c))); + TileArea ta = TileArea(location).Expand(max_c); TILE_AREA_LOOP(tile, ta) { if (IsTileType(tile, MP_STATION)) seen_stations.insert(GetStationIndex(tile)); } diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 07b96a9af7..a4ec6cfc98 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -211,85 +211,85 @@ protected: } /** Sort stations by their name */ - static int CDECL StationNameSorter(const Station * const *a, const Station * const *b) + static bool StationNameSorter(const Station * const &a, const Station * const &b) { static char buf_cache[64]; char buf[64]; - SetDParam(0, (*a)->index); + SetDParam(0, a->index); GetString(buf, STR_STATION_NAME, lastof(buf)); - if (*b != last_station) { - last_station = *b; - SetDParam(0, (*b)->index); + if (b != last_station) { + last_station = b; + SetDParam(0, b->index); GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache)); } int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting). - if (r == 0) return (*a)->index - (*b)->index; - return r; + if (r == 0) return a->index < b->index; + return r < 0; } /** Sort stations by their type */ - static int CDECL StationTypeSorter(const Station * const *a, const Station * const *b) + static bool StationTypeSorter(const Station * const &a, const Station * const &b) { - return (*a)->facilities - (*b)->facilities; + return a->facilities < b->facilities; } /** Sort stations by their waiting cargo */ - static int CDECL StationWaitingTotalSorter(const Station * const *a, const Station * const *b) + static bool StationWaitingTotalSorter(const Station * const &a, const Station * const &b) { int diff = 0; CargoID j; FOR_EACH_SET_CARGO_ID(j, cargo_filter) { - diff += (*a)->goods[j].cargo.TotalCount() - (*b)->goods[j].cargo.TotalCount(); + diff += a->goods[j].cargo.TotalCount() - b->goods[j].cargo.TotalCount(); } - return diff; + return diff < 0; } /** Sort stations by their available waiting cargo */ - static int CDECL StationWaitingAvailableSorter(const Station * const *a, const Station * const *b) + static bool StationWaitingAvailableSorter(const Station * const &a, const Station * const &b) { int diff = 0; CargoID j; FOR_EACH_SET_CARGO_ID(j, cargo_filter) { - diff += (*a)->goods[j].cargo.AvailableCount() - (*b)->goods[j].cargo.AvailableCount(); + diff += a->goods[j].cargo.AvailableCount() - b->goods[j].cargo.AvailableCount(); } - return diff; + return diff < 0; } /** Sort stations by their rating */ - static int CDECL StationRatingMaxSorter(const Station * const *a, const Station * const *b) + static bool StationRatingMaxSorter(const Station * const &a, const Station * const &b) { byte maxr1 = 0; byte maxr2 = 0; CargoID j; FOR_EACH_SET_CARGO_ID(j, cargo_filter) { - if ((*a)->goods[j].HasRating()) maxr1 = max(maxr1, (*a)->goods[j].rating); - if ((*b)->goods[j].HasRating()) maxr2 = max(maxr2, (*b)->goods[j].rating); + if (a->goods[j].HasRating()) maxr1 = max(maxr1, a->goods[j].rating); + if (b->goods[j].HasRating()) maxr2 = max(maxr2, b->goods[j].rating); } - return maxr1 - maxr2; + return maxr1 < maxr2; } /** Sort stations by their rating */ - static int CDECL StationRatingMinSorter(const Station * const *a, const Station * const *b) + static bool StationRatingMinSorter(const Station * const &a, const Station * const &b) { byte minr1 = 255; byte minr2 = 255; for (CargoID j = 0; j < NUM_CARGO; j++) { if (!HasBit(cargo_filter, j)) continue; - if ((*a)->goods[j].HasRating()) minr1 = min(minr1, (*a)->goods[j].rating); - if ((*b)->goods[j].HasRating()) minr2 = min(minr2, (*b)->goods[j].rating); + if (a->goods[j].HasRating()) minr1 = min(minr1, a->goods[j].rating); + if (b->goods[j].HasRating()) minr2 = min(minr2, b->goods[j].rating); } - return -(minr1 - minr2); + return minr1 > minr2; } /** Sort the stations list */ diff --git a/src/story_gui.cpp b/src/story_gui.cpp index 944e7bd509..5ce77cc63b 100644 --- a/src/story_gui.cpp +++ b/src/story_gui.cpp @@ -69,9 +69,9 @@ protected: } /** Sort story pages by order value. */ - static int CDECL PageOrderSorter(const StoryPage * const *a, const StoryPage * const *b) + static bool PageOrderSorter(const StoryPage * const &a, const StoryPage * const &b) { - return (*a)->sort_value - (*b)->sort_value; + return a->sort_value < b->sort_value; } /** (Re)Build story page element list. */ @@ -98,9 +98,9 @@ protected: } /** Sort story page elements by order value. */ - static int CDECL PageElementOrderSorter(const StoryPageElement * const *a, const StoryPageElement * const *b) + static bool PageElementOrderSorter(const StoryPageElement * const &a, const StoryPageElement * const &b) { - return (*a)->sort_value - (*b)->sort_value; + return a->sort_value < b->sort_value; } /* diff --git a/src/strgen/strgen.cpp b/src/strgen/strgen.cpp index a4c9da08e7..c33e884af0 100644 --- a/src/strgen/strgen.cpp +++ b/src/strgen/strgen.cpp @@ -418,7 +418,7 @@ static const OptionData _opts[] = { GETOPT_NOVAL( 't', "--todo"), GETOPT_NOVAL( 'w', "--warning"), GETOPT_NOVAL( 'h', "--help"), - GETOPT_GENERAL('h', '?', nullptr, ODF_NO_VALUE), + GETOPT_GENERAL('h', '?', nullptr, ODF_NO_VALUE), GETOPT_VALUE( 's', "--source_dir"), GETOPT_VALUE( 'd', "--dest_dir"), GETOPT_END(), diff --git a/src/table/townname.h b/src/table/townname.h index f09a7d4f48..2c90dc239b 100644 --- a/src/table/townname.h +++ b/src/table/townname.h @@ -1995,7 +1995,7 @@ static const char * const _name_romanian_real[] = { "Motru", "N\xC4\x83s\xC4\x83ud", "N\xC4\x83vodari", - "Odobe\xC8x99ti", + "Odobe\xC8\x99ti", "Olteni\xC8\x9B""a", "One\xC8\x99ti", "Oradea", diff --git a/src/tbtr_template_gui_main.cpp b/src/tbtr_template_gui_main.cpp index a8803b94e3..ce0aa50b56 100644 --- a/src/tbtr_template_gui_main.cpp +++ b/src/tbtr_template_gui_main.cpp @@ -559,29 +559,6 @@ public: } } - /** Sort the groups by their name */ - static int CDECL GroupNameSorter(const Group * const *a, const Group * const *b) - { - static const Group *last_group[2] = { nullptr, nullptr }; - static char last_name[2][64] = { "", "" }; - - if (*a != last_group[0]) { - last_group[0] = *a; - SetDParam(0, (*a)->index); - GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0])); - } - - if (*b != last_group[1]) { - last_group[1] = *b; - SetDParam(0, (*b)->index); - GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1])); - } - - int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting). - if (r == 0) return (*a)->index - (*b)->index; - return r; - } - void BuildGroupList(Owner owner) { if (!this->groups.NeedRebuild()) return; @@ -599,6 +576,7 @@ public: } list.ForceResort(); + extern bool GroupNameSorter(const Group * const &a, const Group * const &b); list.Sort(&GroupNameSorter); AddParents(&list, INVALID_GROUP, 0); diff --git a/src/tilearea.cpp b/src/tilearea.cpp index ec3b9aafbb..33850c7203 100644 --- a/src/tilearea.cpp +++ b/src/tilearea.cpp @@ -117,6 +117,27 @@ bool OrthogonalTileArea::Contains(TileIndex tile) const return IsInsideBS(tile_x, left, this->w) && IsInsideBS(tile_y, top, this->h); } +/** + * Expand a tile area by rad tiles in each direction, keeping within map bounds. + * @param rad Number of tiles to expand + * @return The OrthogonalTileArea. + */ +OrthogonalTileArea &OrthogonalTileArea::Expand(int rad) +{ + int x = TileX(this->tile); + int y = TileY(this->tile); + + int sx = max(x - rad, 0); + int sy = max(y - rad, 0); + int ex = min(x + this->w + rad, MapSizeX()); + int ey = min(y + this->h + rad, MapSizeY()); + + this->tile = TileXY(sx, sy); + this->w = ex - sx; + this->h = ey - sy; + return *this; +} + /** * Clamp the tile area to map borders. */ diff --git a/src/tilearea_type.h b/src/tilearea_type.h index ba3a08f1ad..d970acf443 100644 --- a/src/tilearea_type.h +++ b/src/tilearea_type.h @@ -50,6 +50,8 @@ struct OrthogonalTileArea { bool Contains(TileIndex tile) const; + OrthogonalTileArea &Expand(int rad); + void ClampToMap(); /** diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index db0bbdbb05..ebd7940dc9 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -342,15 +342,12 @@ CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1, * Order vehicles based on their timetable. The vehicles will be sorted in order * they would reach the first station. * - * @param ap First Vehicle pointer. - * @param bp Second Vehicle pointer. + * @param a First Vehicle pointer. + * @param b Second Vehicle pointer. * @return Comparison value. */ -static int CDECL VehicleTimetableSorter(Vehicle * const *ap, Vehicle * const *bp) +static bool VehicleTimetableSorter(Vehicle * const &a, Vehicle * const &b) { - const Vehicle *a = *ap; - const Vehicle *b = *bp; - VehicleOrderID a_order = a->cur_real_order_index; VehicleOrderID b_order = b->cur_real_order_index; int j = (int)b_order - (int)a_order; @@ -368,15 +365,15 @@ static int CDECL VehicleTimetableSorter(Vehicle * const *ap, Vehicle * const *bp /* First check the order index that accounted for loading, then just the raw one. */ int i = (int)b_order - (int)a_order; - if (i != 0) return i; - if (j != 0) return j; + if (i != 0) return i < 0; + if (j != 0) return j < 0; /* Look at the time we spent in this order; the higher, the closer to its destination. */ i = b->current_order_time - a->current_order_time; - if (i != 0) return i; + if (i != 0) return i < 0; /* If all else is equal, use some unique index to sort it the same way. */ - return b->unitnumber - a->unitnumber; + return b->unitnumber < a->unitnumber; } /** @@ -421,7 +418,7 @@ CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1, int num_vehs = (uint)vehs.size(); if (num_vehs >= 2) { - QSortT(vehs.data(), vehs.size(), &VehicleTimetableSorter); + std::sort(vehs.begin(), vehs.end(), &VehicleTimetableSorter); } int idx = vehs.begin() - std::find(vehs.begin(), vehs.end(), v); diff --git a/src/town_gui.cpp b/src/town_gui.cpp index acefdbbba8..cd96d43828 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -674,54 +674,55 @@ private: } /** Sort by town name */ - static int CDECL TownNameSorter(const Town * const *a, const Town * const *b) + static bool TownNameSorter(const Town * const &a, const Town * const &b) { static char buf_cache[64]; - const Town *ta = *a; - const Town *tb = *b; char buf[64]; - SetDParam(0, ta->index); + SetDParam(0, a->index); GetString(buf, STR_TOWN_NAME, lastof(buf)); /* If 'b' is the same town as in the last round, use the cached value * We do this to speed stuff up ('b' is called with the same value a lot of * times after each other) */ - if (tb != last_town) { - last_town = tb; - SetDParam(0, tb->index); + if (b != last_town) { + last_town = b; + SetDParam(0, b->index); GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache)); } - return strnatcmp(buf, buf_cache); // Sort by name (natural sorting). + return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting). } /** Sort by population (default descending, as big towns are of the most interest). */ - static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b) + static bool TownPopulationSorter(const Town * const &a, const Town * const &b) { - uint32 a_population = (*a)->cache.population; - uint32 b_population = (*b)->cache.population; + uint32 a_population = a->cache.population; + uint32 b_population = b->cache.population; if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b); - return (a_population < b_population) ? -1 : 1; + return a_population < b_population; } /** Sort by town rating */ - static int CDECL TownRatingSorter(const Town * const *a, const Town * const *b) + static bool TownRatingSorter(const Town * const &a, const Town * const &b) { - int before = TownDirectoryWindow::last_sorting.order ? 1 : -1; // Value to get 'a' before 'b'. + bool before = !TownDirectoryWindow::last_sorting.order; // Value to get 'a' before 'b'. /* Towns without rating are always after towns with rating. */ - if (HasBit((*a)->have_ratings, _local_company)) { - if (HasBit((*b)->have_ratings, _local_company)) { - int16 a_rating = (*a)->ratings[_local_company]; - int16 b_rating = (*b)->ratings[_local_company]; + if (HasBit(a->have_ratings, _local_company)) { + if (HasBit(b->have_ratings, _local_company)) { + int16 a_rating = a->ratings[_local_company]; + int16 b_rating = b->ratings[_local_company]; if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b); - return (a_rating < b_rating) ? -1 : 1; + return a_rating < b_rating; } return before; } - if (HasBit((*b)->have_ratings, _local_company)) return -before; - return -before * TownDirectoryWindow::TownNameSorter(a, b); // Sort unrated towns always on ascending town name. + if (HasBit(b->have_ratings, _local_company)) return !before; + + /* Sort unrated towns always on ascending town name. */ + if (before) return TownDirectoryWindow::TownNameSorter(a, b); + return !TownDirectoryWindow::TownNameSorter(a, b); } public: diff --git a/src/tracerestrict_gui.cpp b/src/tracerestrict_gui.cpp index 40e76d2976..00900c17f5 100644 --- a/src/tracerestrict_gui.cpp +++ b/src/tracerestrict_gui.cpp @@ -474,7 +474,7 @@ static const TraceRestrictDropDownListSet *GetSortedCargoTypeDropDownListSet() static DropDownList GetGroupDropDownList(Owner owner, GroupID group_id, int &selected) { typedef GUIList GUIGroupList; - extern int CDECL GroupNameSorter(const Group * const *a, const Group * const *b); + extern bool GroupNameSorter(const Group * const &a, const Group * const &b); GUIGroupList list; @@ -506,11 +506,11 @@ static DropDownList GetGroupDropDownList(Owner owner, GroupID group_id, int &sel } /** Sort slots by their name */ -static int CDECL SlotNameSorter(const TraceRestrictSlot * const *a, const TraceRestrictSlot * const *b) +static bool CDECL SlotNameSorter(const TraceRestrictSlot * const &a, const TraceRestrictSlot * const &b) { - int r = strnatcmp((*a)->name.c_str(), (*b)->name.c_str()); // Sort by name (natural sorting). - if (r == 0) return (*a)->index - (*b)->index; - return r; + int r = strnatcmp(a->name.c_str(), b->name.c_str()); // Sort by name (natural sorting). + if (r == 0) return a->index < b->index; + return r < 0; } /** diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index b416139889..f7309e0772 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -384,7 +384,7 @@ void BaseVehicleListWindow::SortVehicleList() void DepotSortList(VehicleList *list) { if (list->size() < 2) return; - QSortT(list->data(), list->size(), &VehicleNumberSorter); + std::sort(list->begin(), list->end(), &VehicleNumberSorter); } /** draw the vehicle profit button in the vehicle list window. */ @@ -1338,69 +1338,69 @@ StringID GetCargoSubtypeText(const Vehicle *v) } /** Sort vehicles by their number */ -static int CDECL VehicleNumberSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleNumberSorter(const Vehicle * const &a, const Vehicle * const &b) { - return (*a)->unitnumber - (*b)->unitnumber; + return a->unitnumber < b->unitnumber; } /** Sort vehicles by their name */ -static int CDECL VehicleNameSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleNameSorter(const Vehicle * const &a, const Vehicle * const &b) { static char last_name[2][64]; - if (*a != _last_vehicle[0]) { - _last_vehicle[0] = *a; - SetDParam(0, (*a)->index); + if (a != _last_vehicle[0]) { + _last_vehicle[0] = a; + SetDParam(0, a->index); GetString(last_name[0], STR_VEHICLE_NAME, lastof(last_name[0])); } - if (*b != _last_vehicle[1]) { - _last_vehicle[1] = *b; - SetDParam(0, (*b)->index); + if (b != _last_vehicle[1]) { + _last_vehicle[1] = b; + SetDParam(0, b->index); GetString(last_name[1], STR_VEHICLE_NAME, lastof(last_name[1])); } int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting). - return (r != 0) ? r : VehicleNumberSorter(a, b); + return (r != 0) ? r < 0: VehicleNumberSorter(a, b); } /** Sort vehicles by their age */ -static int CDECL VehicleAgeSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleAgeSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->age - (*b)->age; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->age - b->age; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by this year profit */ -static int CDECL VehicleProfitThisYearSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleProfitThisYearSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = ClampToI32((*a)->GetDisplayProfitThisYear() - (*b)->GetDisplayProfitThisYear()); - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = ClampToI32(a->GetDisplayProfitThisYear() - b->GetDisplayProfitThisYear()); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by last year profit */ -static int CDECL VehicleProfitLastYearSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleProfitLastYearSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = ClampToI32((*a)->GetDisplayProfitLastYear() - (*b)->GetDisplayProfitLastYear()); - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = ClampToI32(a->GetDisplayProfitLastYear() - b->GetDisplayProfitLastYear()); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by lifetime profit */ -static int CDECL VehicleProfitLifetimeSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleProfitLifetimeSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = ClampToI32((*a)->GetDisplayProfitLifetime() - (*b)->GetDisplayProfitLifetime()); - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = ClampToI32(a->GetDisplayProfitLifetime() - b->GetDisplayProfitLifetime()); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their cargo */ -static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleCargoSorter(const Vehicle * const &a, const Vehicle * const &b) { const Vehicle *v; CargoArray diff; /* Append the cargo of the connected waggons */ - for (v = *a; v != nullptr; v = v->Next()) diff[v->cargo_type] += v->cargo_cap; - for (v = *b; v != nullptr; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap; + for (v = a; v != nullptr; v = v->Next()) diff[v->cargo_type] += v->cargo_cap; + for (v = b; v != nullptr; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap; int r = 0; for (CargoID i = 0; i < NUM_CARGO; i++) { @@ -1408,69 +1408,69 @@ static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * co if (r != 0) break; } - return (r != 0) ? r : VehicleNumberSorter(a, b); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their reliability */ -static int CDECL VehicleReliabilitySorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleReliabilitySorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->reliability - (*b)->reliability; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->reliability - b->reliability; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their max speed */ -static int CDECL VehicleMaxSpeedSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleMaxSpeedSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->vcache.cached_max_speed - (*b)->vcache.cached_max_speed; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->vcache.cached_max_speed - b->vcache.cached_max_speed; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by model */ -static int CDECL VehicleModelSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleModelSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->engine_type - (*b)->engine_type; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->engine_type - b->engine_type; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their value */ -static int CDECL VehicleValueSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleValueSorter(const Vehicle * const &a, const Vehicle * const &b) { const Vehicle *u; Money diff = 0; - for (u = *a; u != nullptr; u = u->Next()) diff += u->value; - for (u = *b; u != nullptr; u = u->Next()) diff -= u->value; + for (u = a; u != nullptr; u = u->Next()) diff += u->value; + for (u = b; u != nullptr; u = u->Next()) diff -= u->value; int r = ClampToI32(diff); - return (r != 0) ? r : VehicleNumberSorter(a, b); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their length */ -static int CDECL VehicleLengthSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleLengthSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->GetGroundVehicleCache()->cached_total_length - (*b)->GetGroundVehicleCache()->cached_total_length; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->GetGroundVehicleCache()->cached_total_length - b->GetGroundVehicleCache()->cached_total_length; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by the time they can still live */ -static int CDECL VehicleTimeToLiveSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleTimeToLiveSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = ClampToI32(((*a)->max_age - (*a)->age) - ((*b)->max_age - (*b)->age)); - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = ClampToI32((a->max_age - a->age) - (b->max_age - b->age)); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by the timetable delay */ -static int CDECL VehicleTimetableDelaySorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleTimetableDelaySorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->lateness_counter - (*b)->lateness_counter; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->lateness_counter - b->lateness_counter; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by the average order occupancy */ -static int CDECL VehicleAverageOrderOccupancySorter(const Vehicle * const *a, const Vehicle * const *b) +static bool VehicleAverageOrderOccupancySorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->GetOrderOccupancyAverage() - (*b)->GetOrderOccupancyAverage(); - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->GetOrderOccupancyAverage() - b->GetOrderOccupancyAverage(); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } void InitializeGUI() diff --git a/src/waypoint_cmd.cpp b/src/waypoint_cmd.cpp index a4e6aa355e..957b31ca34 100644 --- a/src/waypoint_cmd.cpp +++ b/src/waypoint_cmd.cpp @@ -213,7 +213,7 @@ CommandCost CmdBuildRailWaypoint(TileIndex start_tile, DoCommandFlag flags, uint } Waypoint *wp = nullptr; - TileArea new_location(TileArea(start_tile, width, height)); + TileArea new_location(start_tile, width, height); CommandCost ret = FindJoiningWaypoint(est, station_to_join, adjacent, new_location, &wp); if (ret.Failed()) return ret; diff --git a/src/window.cpp b/src/window.cpp index 8052ef555a..0a70bfdd5f 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -157,10 +157,10 @@ void WindowDesc::LoadFromConfig() /** * Sort WindowDesc by ini_key. */ -static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b) +static bool DescSorter(WindowDesc* const &a, WindowDesc* const &b) { - if ((*a)->ini_key != nullptr && (*b)->ini_key != nullptr) return strcmp((*a)->ini_key, (*b)->ini_key); - return ((*b)->ini_key != nullptr ? 1 : 0) - ((*a)->ini_key != nullptr ? 1 : 0); + if (a->ini_key != nullptr && b->ini_key != nullptr) return strcmp(a->ini_key, b->ini_key) < 0; + return a->ini_key != nullptr; } /** @@ -169,7 +169,7 @@ static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b) void WindowDesc::SaveToConfig() { /* Sort the stuff to get a nice ini file on first write */ - QSortT(_window_descs->data(), _window_descs->size(), DescSorter); + std::sort(_window_descs->begin(), _window_descs->end(), DescSorter); IniFile *ini = new IniFile(); ini->LoadFromDisk(_windows_file, NO_DIRECTORY);