Merge pull request #23083 from AaronVanGeffen/refactor-utils

Start moving functions out of Util.h
This commit is contained in:
Aaron van Geffen authored and GitHub committed 2024-10-29 21:23:20 +01:00
commit d8a5643611
60 files changed
+409 -344

No files matched your search

+3 -2
View File
@@ -12,6 +12,7 @@
#include "Window.h"
#include <algorithm>
#include <openrct2/core/Numerics.hpp>
#include <openrct2/world/Map.h>
#include <openrct2/world/tile_element/EntranceElement.h>
#include <openrct2/world/tile_element/PathElement.h>
@@ -150,7 +151,7 @@ namespace OpenRCT2::Ui
uint32_t directions = (*tileElement)->AsEntrance()->GetDirections();
if (directions & 0x0F)
{
int32_t bx = UtilBitScanForward(directions);
int32_t bx = Numerics::bitScanForward(directions);
bx += (*tileElement)->AsEntrance()->GetDirection();
bx &= 3;
if (direction != nullptr)
@@ -167,7 +168,7 @@ namespace OpenRCT2::Ui
uint32_t directions = (*tileElement)->AsEntrance()->GetDirections();
if (directions & 0x0F)
{
int32_t bx = (*tileElement)->GetDirectionWithOffset(UtilBitScanForward(directions));
int32_t bx = (*tileElement)->GetDirectionWithOffset(Numerics::bitScanForward(directions));
if (direction != nullptr)
*direction = bx;
return info.Loc;
+1
View File
@@ -11,6 +11,7 @@
#include <openrct2/ride/RideTypes.h>
#include <openrct2/ride/Track.h>
#include <openrct2/util/Util.h>
namespace OpenRCT2
{
@@ -890,7 +890,7 @@ namespace OpenRCT2::Ui::Windows
if (numSourcesActive == 1)
{
widgets[WIDX_FILTER_DROPDOWN].text = STR_OBJECT_SELECTION_ONLY_STRINGID;
auto firstActiveSource = UtilBitScanForward(sources);
auto firstActiveSource = Numerics::bitScanForward(sources);
ft.Add<StringId>(kSourceStringIds[firstActiveSource]);
}
else
+1 -1
View File
@@ -18,6 +18,7 @@
#include <openrct2/audio/audio.h>
#include <openrct2/core/File.h>
#include <openrct2/core/Path.hpp>
#include <openrct2/core/UnitConversion.h>
#include <openrct2/localisation/Formatter.h>
#include <openrct2/object/ObjectManager.h>
#include <openrct2/ride/RideConstruction.h>
@@ -25,7 +26,6 @@
#include <openrct2/ride/TrackDesign.h>
#include <openrct2/ride/TrackDesignRepository.h>
#include <openrct2/sprites.h>
#include <openrct2/util/Util.h>
#include <string>
#include <vector>
+1 -1
View File
@@ -16,13 +16,13 @@
#include <openrct2/Input.h>
#include <openrct2/config/Config.h>
#include <openrct2/core/FileSystem.hpp>
#include <openrct2/core/UnitConversion.h>
#include <openrct2/drawing/Drawing.h>
#include <openrct2/localisation/Formatter.h>
#include <openrct2/object/ObjectManager.h>
#include <openrct2/object/TerrainEdgeObject.h>
#include <openrct2/object/TerrainSurfaceObject.h>
#include <openrct2/sprites.h>
#include <openrct2/util/Util.h>
#include <openrct2/windows/Intent.h>
#include <openrct2/world/Map.h>
#include <openrct2/world/MapGen.h>
+1 -1
View File
@@ -24,6 +24,7 @@
#include <openrct2/actions/ParkSetEntranceFeeAction.h>
#include <openrct2/actions/ParkSetNameAction.h>
#include <openrct2/config/Config.h>
#include <openrct2/core/UnitConversion.h>
#include <openrct2/localisation/Currency.h>
#include <openrct2/localisation/Formatting.h>
#include <openrct2/management/Award.h>
@@ -31,7 +32,6 @@
#include <openrct2/ride/RideData.h>
#include <openrct2/scenario/Scenario.h>
#include <openrct2/sprites.h>
#include <openrct2/util/Util.h>
#include <openrct2/world/Park.h>
namespace OpenRCT2::Ui::Windows
+1
View File
@@ -38,6 +38,7 @@
#include <openrct2/audio/audio.h>
#include <openrct2/config/Config.h>
#include <openrct2/core/String.hpp>
#include <openrct2/core/UnitConversion.h>
#include <openrct2/entity/EntityList.h>
#include <openrct2/entity/Staff.h>
#include <openrct2/localisation/Currency.h>
+1 -1
View File
@@ -717,7 +717,7 @@ namespace OpenRCT2::Ui::Windows
}
else
{
int32_t firstPage = UtilBitScanForward(showPages);
int32_t firstPage = Numerics::bitScanForward(showPages);
if (firstPage != -1)
{
selected_tab = firstPage;
+1 -1
View File
@@ -883,7 +883,7 @@ namespace OpenRCT2::Ui::Windows
}
uint32_t staffOrders = staff->StaffOrders;
for (auto index = UtilBitScanForward(staffOrders); index != -1; index = UtilBitScanForward(staffOrders))
for (auto index = Numerics::bitScanForward(staffOrders); index != -1; index = Numerics::bitScanForward(staffOrders))
{
staffOrders &= ~(1 << index);
SetCheckboxValue(WIDX_CHECKBOX_1 + index, true);
+1
View File
@@ -16,6 +16,7 @@
#include <openrct2/audio/audio.h>
#include <openrct2/config/Config.h>
#include <openrct2/core/String.hpp>
#include <openrct2/core/UnitConversion.h>
#include <openrct2/drawing/IDrawingEngine.h>
#include <openrct2/localisation/Formatting.h>
#include <openrct2/ride/RideConstruction.h>
-1
View File
@@ -16,7 +16,6 @@
#include "network/network.h"
#include "ride/Ride.h"
#include "scenario/Scenario.h"
#include "util/Util.h"
#include "world/Climate.h"
#include "world/Footpath.h"
#include "world/Map.h"
-1
View File
@@ -22,7 +22,6 @@
#include "object/ObjectLimits.h"
#include "object/ObjectManager.h"
#include "object/ObjectRepository.h"
#include "util/Util.h"
#include <cmath>
#include <cstring>
-1
View File
@@ -74,7 +74,6 @@
#include "scripting/ScriptEngine.h"
#include "ui/UiContext.h"
#include "ui/WindowManager.h"
#include "util/Util.h"
#include "world/Park.h"
#include <chrono>
@@ -12,7 +12,6 @@
#include "../Context.h"
#include "../Diagnostic.h"
#include "../management/Finance.h"
#include "../util/Util.h"
#include "../windows/Intent.h"
#include "../world/Banner.h"
#include "../world/tile_element/BannerElement.h"
@@ -10,7 +10,6 @@
#include "NetworkModifyGroupAction.h"
#include "../network/network.h"
#include "../util/Util.h"
using namespace OpenRCT2;
@@ -14,7 +14,6 @@
#include "../entity/EntityRegistry.h"
#include "../entity/Peep.h"
#include "../network/network.h"
#include "../util/Util.h"
using namespace OpenRCT2;
@@ -16,7 +16,6 @@
#include "../interface/Window.h"
#include "../management/Finance.h"
#include "../scenario/Scenario.h"
#include "../util/Util.h"
#include "../world/Park.h"
using namespace OpenRCT2;
+2 -2
View File
@@ -640,8 +640,8 @@ GameActions::Result TrackPlaceAction::Execute() const
{
if (!(GetFlags() & GAME_COMMAND_FLAG_GHOST) && !gameState.Cheats.DisableClearanceChecks)
{
for (int32_t chosenDirection = UtilBitScanForward(availableDirections); chosenDirection != -1;
chosenDirection = UtilBitScanForward(availableDirections))
for (int32_t chosenDirection = Numerics::bitScanForward(availableDirections); chosenDirection != -1;
chosenDirection = Numerics::bitScanForward(availableDirections))
{
availableDirections &= ~(1 << chosenDirection);
CoordsXY tempLoc{ mapLoc.x, mapLoc.y };
@@ -16,7 +16,6 @@
#include "../ride/Track.h"
#include "../ride/TrackData.h"
#include "../ride/TrackDesign.h"
#include "../util/Util.h"
#include "../world/Footpath.h"
#include "../world/MapAnimation.h"
#include "../world/tile_element/SurfaceElement.h"
+176
View File
@@ -0,0 +1,176 @@
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "Compression.h"
#include "../Diagnostic.h"
#include "zlib.h"
#include <cassert>
#include <stdexcept>
#include <string>
namespace OpenRCT2::Compression
{
constexpr size_t CHUNK = 128 * 1024;
// Compress the source to gzip-compatible stream, write to dest.
// Mainly used for compressing the crashdumps
bool gzipCompress(FILE* source, FILE* dest)
{
if (source == nullptr || dest == nullptr)
{
return false;
}
int ret, flush;
size_t have;
z_stream strm{};
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
int windowBits = 15;
int GZIP_ENCODING = 16;
ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, windowBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
{
LOG_ERROR("Failed to initialise stream");
return false;
}
do
{
strm.avail_in = uInt(fread(in, 1, CHUNK, source));
if (ferror(source))
{
deflateEnd(&strm);
LOG_ERROR("Failed to read data from source");
return false;
}
flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = in;
do
{
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, flush);
if (ret == Z_STREAM_ERROR)
{
LOG_ERROR("Failed to compress data");
return false;
}
have = CHUNK - strm.avail_out;
if (fwrite(out, 1, have, dest) != have || ferror(dest))
{
deflateEnd(&strm);
LOG_ERROR("Failed to write data to destination");
return false;
}
} while (strm.avail_out == 0);
} while (flush != Z_FINISH);
deflateEnd(&strm);
return true;
}
std::vector<uint8_t> gzip(const void* data, const size_t dataLen)
{
assert(data != nullptr);
std::vector<uint8_t> output;
z_stream strm{};
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
{
const auto ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
{
throw std::runtime_error("deflateInit2 failed with error " + std::to_string(ret));
}
}
int flush = 0;
const auto* src = static_cast<const Bytef*>(data);
size_t srcRemaining = dataLen;
do
{
const auto nextBlockSize = std::min(srcRemaining, CHUNK);
srcRemaining -= nextBlockSize;
flush = srcRemaining == 0 ? Z_FINISH : Z_NO_FLUSH;
strm.avail_in = static_cast<uInt>(nextBlockSize);
strm.next_in = const_cast<Bytef*>(src);
do
{
output.resize(output.size() + nextBlockSize);
strm.avail_out = static_cast<uInt>(nextBlockSize);
strm.next_out = &output[output.size() - nextBlockSize];
const auto ret = deflate(&strm, flush);
if (ret == Z_STREAM_ERROR)
{
throw std::runtime_error("deflate failed with error " + std::to_string(ret));
}
output.resize(output.size() - strm.avail_out);
} while (strm.avail_out == 0);
src += nextBlockSize;
} while (flush != Z_FINISH);
deflateEnd(&strm);
return output;
}
std::vector<uint8_t> ungzip(const void* data, const size_t dataLen)
{
assert(data != nullptr);
std::vector<uint8_t> output;
z_stream strm{};
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
{
const auto ret = inflateInit2(&strm, 15 | 16);
if (ret != Z_OK)
{
throw std::runtime_error("inflateInit2 failed with error " + std::to_string(ret));
}
}
int flush = 0;
const auto* src = static_cast<const Bytef*>(data);
size_t srcRemaining = dataLen;
do
{
const auto nextBlockSize = std::min(srcRemaining, CHUNK);
srcRemaining -= nextBlockSize;
flush = srcRemaining == 0 ? Z_FINISH : Z_NO_FLUSH;
strm.avail_in = static_cast<uInt>(nextBlockSize);
strm.next_in = const_cast<Bytef*>(src);
do
{
output.resize(output.size() + nextBlockSize);
strm.avail_out = static_cast<uInt>(nextBlockSize);
strm.next_out = &output[output.size() - nextBlockSize];
const auto ret = inflate(&strm, flush);
if (ret == Z_STREAM_ERROR)
{
throw std::runtime_error("deflate failed with error " + std::to_string(ret));
}
output.resize(output.size() - strm.avail_out);
} while (strm.avail_out == 0);
src += nextBlockSize;
} while (flush != Z_FINISH);
inflateEnd(&strm);
return output;
}
} // namespace OpenRCT2::Compression
+21
View File
@@ -0,0 +1,21 @@
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
#include <cstdint>
#include <cstdio>
#include <vector>
namespace OpenRCT2::Compression
{
bool gzipCompress(FILE* source, FILE* dest);
std::vector<uint8_t> gzip(const void* data, const size_t dataLen);
std::vector<uint8_t> ungzip(const void* data, const size_t dataLen);
} // namespace OpenRCT2::Compression
+48
View File
@@ -14,8 +14,56 @@
#include <limits>
#include <type_traits>
#ifdef _MSC_VER
# include <intrin.h>
#endif
namespace OpenRCT2::Numerics
{
inline int32_t bitScanForward(uint32_t source)
{
#if defined(_MSC_VER) && (_MSC_VER >= 1400) // Visual Studio 2005
unsigned long i;
uint8_t success = _BitScanForward(&i, source);
return success != 0 ? i : -1;
#elif defined(__GNUC__)
int32_t success = __builtin_ffs(source);
return success - 1;
#else
# pragma message("Falling back to iterative bitscan forward, consider using intrinsics")
// This is a low-hanging optimisation boost, check if your compiler offers
// any intrinsic.
// cf. https://github.com/OpenRCT2/OpenRCT2/pull/2093
for (int32_t i = 0; i < 32; i++)
if (source & (1u << i))
return i;
return -1;
#endif
}
inline int64_t bitScanForward(uint64_t source)
{
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && defined(_M_X64) // Visual Studio 2005
unsigned long i;
uint8_t success = _BitScanForward64(&i, source);
return success != 0 ? i : -1;
#elif defined(__GNUC__)
int32_t success = __builtin_ffsll(source);
return success - 1;
#else
# pragma message("Falling back to iterative bitscan forward, consider using intrinsics")
// This is a low-hanging optimisation boost, check if your compiler offers
// any intrinsic.
// cf. https://github.com/OpenRCT2/OpenRCT2/pull/2093
for (int32_t i = 0; i < 64; i++)
if (source & (1uLL << i))
return i;
return -1;
#endif
}
/**
* Bitwise left rotate
* @tparam _UIntType unsigned integral type
+4 -3
View File
@@ -9,7 +9,7 @@
#pragma once
#include "../util/Util.h"
#include "../core/Compression.h"
#include "../world/Location.hpp"
#include "Crypt.h"
#include "FileStream.h"
@@ -19,6 +19,7 @@
#include <algorithm>
#include <array>
#include <cstdint>
#include <optional>
#include <stack>
#include <type_traits>
#include <vector>
@@ -99,7 +100,7 @@ namespace OpenRCT2
// Uncompress
if (_header.Compression == COMPRESSION_GZIP)
{
auto uncompressedData = Ungzip(_buffer.GetData(), _buffer.GetLength());
auto uncompressedData = Compression::ungzip(_buffer.GetData(), _buffer.GetLength());
if (_header.UncompressedSize != uncompressedData.size())
{
// Warning?
@@ -135,7 +136,7 @@ namespace OpenRCT2
std::optional<std::vector<uint8_t>> compressedBytes;
if (_header.Compression == COMPRESSION_GZIP)
{
compressedBytes = Gzip(uncompressedData, uncompressedSize);
compressedBytes = Compression::gzip(uncompressedData, uncompressedSize);
if (compressedBytes)
{
_header.CompressedSize = compressedBytes->size();
+78
View File
@@ -0,0 +1,78 @@
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "UnitConversion.h"
namespace OpenRCT2
{
int32_t SquaredMetresToSquaredFeet(int32_t squaredMetres)
{
// 1 metre squared = 10.7639104 feet squared
// RCT2 approximates as 11
return squaredMetres * 11;
}
int32_t MetresToFeet(int32_t metres)
{
// 1 metre = 3.2808399 feet
// RCT2 approximates as 3.28125
return (metres * 840) / 256;
}
int32_t FeetToMetres(int32_t feet)
{
return feet * 256 / 840;
}
int32_t MphToKmph(int32_t mph)
{
// 1 mph = 1.60934 kmph
// RCT2 approximates as 1.609375
return (mph * 1648) >> 10;
}
int32_t MphToDmps(int32_t mph)
{
// 1 mph = 4.4704 decimeters/s
return (mph * 73243) >> 14;
}
int32_t BaseZToMetres(int16_t baseZ)
{
return (baseZ / 2 - 7) * 1.5;
}
uint8_t MetresToBaseZ(int16_t metres)
{
return ((metres / 1.5) + 7) * 2;
}
int32_t HeightUnitsToMetres(int32_t heightUnit)
{
// 1 unit = 0.75 metres
return (heightUnit * 3) >> 2;
}
int32_t ToHumanReadableSpeed(int32_t baseSpeed)
{
// Divide this value by 29127 to get the human-readable max speed
// (in RCT2, display_speed = (max_speed * 9) >> 18)
return (baseSpeed * 9) >> 18;
}
uint16_t ToHumanReadableAirTime(uint16_t airTime)
{
return airTime * 3;
}
int32_t ToHumanReadableRideLength(int32_t rideLength)
{
return rideLength >> 16;
}
} // namespace OpenRCT2
+27
View File
@@ -0,0 +1,27 @@
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
#include <cstdint>
namespace OpenRCT2
{
int32_t SquaredMetresToSquaredFeet(int32_t squaredMetres);
int32_t MetresToFeet(int32_t metres);
int32_t FeetToMetres(int32_t feet);
int32_t MphToKmph(int32_t mph);
int32_t MphToDmps(int32_t mph);
int32_t BaseZToMetres(int16_t baseZ);
uint8_t MetresToBaseZ(int16_t metres);
int32_t HeightUnitsToMetres(int32_t heightUnit);
int32_t ToHumanReadableSpeed(int32_t baseSpeed);
uint16_t ToHumanReadableAirTime(uint16_t airTime);
int32_t ToHumanReadableRideLength(int32_t rideLength);
} // namespace OpenRCT2
-1
View File
@@ -16,7 +16,6 @@
#include "../paint/Paint.h"
#include "../profiling/Profiling.h"
#include "../scenario/Scenario.h"
#include "../util/Util.h"
#include "../world/tile_element/TrackElement.h"
#include "EntityRegistry.h"
+3 -3
View File
@@ -820,7 +820,7 @@ void Guest::UpdateConsumptionMotives()
if (TimeToConsume == 0)
{
int32_t chosen_food = UtilBitScanForward(GetFoodOrDrinkFlags());
int32_t chosen_food = Numerics::bitScanForward(GetFoodOrDrinkFlags());
if (chosen_food != -1)
{
ShopItem food = ShopItem(chosen_food);
@@ -1513,7 +1513,7 @@ bool Guest::DecideAndBuyItem(Ride& ride, const ShopItem shopItem, money64 price)
const auto& shopItemDescriptor = GetShopItemDescriptor(shopItem);
if (shopItemDescriptor.IsFoodOrDrink())
{
int32_t food = UtilBitScanForward(GetFoodOrDrinkFlags());
int32_t food = Numerics::bitScanForward(GetFoodOrDrinkFlags());
if (food != -1)
{
InsertNewThought(PeepThoughtType::HaventFinished, static_cast<ShopItem>(food));
@@ -5371,7 +5371,7 @@ void Guest::UpdateWalking()
if ((!GetNextIsSurface()) && (static_cast<uint32_t>(Id.ToUnderlying() & 0x1FF) == (currentTicks & 0x1FF))
&& ((0xFFFF & ScenarioRand()) <= 4096))
{
int32_t container = UtilBitScanForward(GetEmptyContainerFlags());
int32_t container = Numerics::bitScanForward(GetEmptyContainerFlags());
auto litterType = Litter::Type::Vomit;
if (container != -1)
+1 -1
View File
@@ -14,11 +14,11 @@
#include "../localisation/StringIdType.h"
#include "../ride/RideTypes.h"
#include "../ride/Station.h"
#include "../util/Util.h"
#include "../world/Location.hpp"
#include <array>
#include <optional>
#include <string_view>
constexpr uint8_t kPeepMinEnergy = 32;
constexpr uint8_t kPeepMaxEnergy = 128;
+2 -2
View File
@@ -683,7 +683,7 @@ Direction Staff::MechanicDirectionPath(uint8_t validDirections, PathElement* pat
pathDirections |= (1 << DirectionReverse(PeepDirection));
}
Direction direction = UtilBitScanForward(pathDirections);
Direction direction = Numerics::bitScanForward(pathDirections);
pathDirections &= ~(1 << direction);
if (pathDirections == 0)
{
@@ -801,7 +801,7 @@ Direction Staff::DirectionPath(uint8_t validDirections, PathElement* pathElement
pathDirections |= (1u << DirectionReverse(PeepDirection));
}
Direction direction = UtilBitScanForward(pathDirections);
Direction direction = Numerics::bitScanForward(pathDirections);
// If this is the only direction they can go, then go
if (pathDirections == (1u << direction))
{
+1
View File
@@ -9,6 +9,7 @@
#pragma once
#include "../util/Util.h"
#include "../world/Map.h"
#include "Peep.h"
+4
View File
@@ -186,6 +186,7 @@
<ClInclude Include="core\CircularBuffer.h" />
<ClInclude Include="core\CodepointView.hpp" />
<ClInclude Include="core\Collections.hpp" />
<ClInclude Include="core\Compression.h" />
<ClInclude Include="core\Console.hpp" />
<ClInclude Include="core\Crypt.h" />
<ClInclude Include="core\DataSerialiser.h" />
@@ -231,6 +232,7 @@
<ClInclude Include="core\Timer.hpp" />
<ClInclude Include="core\UTF8.h" />
<ClInclude Include="core\UnicodeChar.h" />
<ClInclude Include="core\UnitConversion.h" />
<ClInclude Include="core\Zip.h" />
<ClInclude Include="core\ZipStream.hpp" />
<ClInclude Include="Date.h" />
@@ -745,6 +747,7 @@
<ClCompile Include="config\IniWriter.cpp" />
<ClCompile Include="Context.cpp" />
<ClCompile Include="core\ChecksumStream.cpp" />
<ClCompile Include="core\Compression.cpp" />
<ClCompile Include="core\Console.cpp" />
<ClCompile Include="core\Crypt.CNG.cpp" />
<ClCompile Include="core\Crypt.OpenRCT2.cpp" />
@@ -769,6 +772,7 @@
<ClCompile Include="core\StringBuilder.cpp" />
<ClCompile Include="core\StringReader.cpp" />
<ClCompile Include="core\UTF8.cpp" />
<ClCompile Include="core\UnitConversion.cpp" />
<ClCompile Include="core\Zip.cpp" />
<ClCompile Include="core\ZipAndroid.cpp" />
<ClCompile Include="Date.cpp" />
+1 -1
View File
@@ -13,9 +13,9 @@
#include "../Diagnostic.h"
#include "../config/Config.h"
#include "../core/String.hpp"
#include "../core/UnitConversion.h"
#include "../object/ObjectManager.h"
#include "../object/PeepNamesObject.h"
#include "../util/Util.h"
#include "Currency.h"
#include "FormatCodes.h"
#include "Formatter.h"
-1
View File
@@ -26,7 +26,6 @@
#include "../profiling/Profiling.h"
#include "../ride/Ride.h"
#include "../ride/Vehicle.h"
#include "../util/Util.h"
#include "../windows/Intent.h"
#include "../world/Location.hpp"
@@ -23,7 +23,6 @@
# include "../localisation/Localisation.Date.h"
# include "../management/Finance.h"
# include "../platform/Platform.h"
# include "../util/Util.h"
# include "../world/Map.h"
# include "../world/Park.h"
# include "Socket.h"
-1
View File
@@ -12,7 +12,6 @@
#include "../core/JsonFwd.hpp"
#include "../core/StringTypes.h"
#include "../drawing/ImageIndexType.h"
#include "../util/Util.h"
#include "ImageTable.h"
#include "ObjectAsset.h"
#include "ObjectTypes.h"
+2 -1
View File
@@ -17,6 +17,7 @@
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
#include "../core/Memory.hpp"
#include "../core/Numerics.hpp"
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../entity/Yaw.hpp"
@@ -112,7 +113,7 @@ static constexpr SpritePrecision PrecisionFromNumFrames(uint32_t numRotationFram
if (numRotationFrames == 0)
return SpritePrecision::None;
else
return static_cast<SpritePrecision>(UtilBitScanForward(numRotationFrames) + 1);
return static_cast<SpritePrecision>(Numerics::bitScanForward(numRotationFrames) + 1);
}
static void RideObjectUpdateRideType(RideObjectEntry& rideEntry)
+1
View File
@@ -16,6 +16,7 @@
#include "../core/String.hpp"
#include "../localisation/LocalisationService.h"
#include "../rct12/CSStringConverter.h"
#include "../util/Util.h"
#include "Object.h"
using namespace OpenRCT2;
@@ -9,6 +9,7 @@
#pragma once
#include "../core/Money.hpp"
#include "Object.h"
struct CoordsXY;
+7 -8
View File
@@ -19,7 +19,6 @@
#include "../ride/Station.h"
#include "../ride/Track.h"
#include "../scenario/Scenario.h"
#include "../util/Util.h"
#include "../world/Entrance.h"
#include "../world/Footpath.h"
#include "../world/tile_element/BannerElement.h"
@@ -602,7 +601,7 @@ namespace OpenRCT2::PathFinding
uint32_t edges = path->GetEdges();
int32_t testEdge = UtilBitScanForward(edges);
int32_t testEdge = Numerics::bitScanForward(edges);
if (testEdge == -1)
return false;
@@ -627,7 +626,7 @@ namespace OpenRCT2::PathFinding
break;
}
edges &= ~(1 << testEdge);
} while ((testEdge = UtilBitScanForward(edges)) != -1);
} while ((testEdge = Numerics::bitScanForward(edges)) != -1);
return isThinJunction;
}
@@ -1008,7 +1007,7 @@ namespace OpenRCT2::PathFinding
/* Remove the reverse edge (i.e. the edge back to the previous map element.) */
edges &= ~(1 << DirectionReverse(testEdge));
int32_t nextTestEdge = UtilBitScanForward(edges);
int32_t nextTestEdge = Numerics::bitScanForward(edges);
/* If there are no other edges the current search ends here.
* Continue to the next map element without updating the parameters (best result so far). */
@@ -1205,7 +1204,7 @@ namespace OpenRCT2::PathFinding
LogPathfinding(
&peep, "Returned to %d,%d,%d; Steps: %u; edge: %d; Score: %d", loc.x >> 5, loc.y >> 5, loc.z, numSteps,
nextTestEdge, *endScore);
} while ((nextTestEdge = UtilBitScanForward(edges)) != -1);
} while ((nextTestEdge = Numerics::bitScanForward(edges)) != -1);
} while (!(tileElement++)->IsLastForTile());
@@ -1377,7 +1376,7 @@ namespace OpenRCT2::PathFinding
if (edges == 0)
return INVALID_DIRECTION;
int32_t chosenEdge = UtilBitScanForward(edges);
int32_t chosenEdge = Numerics::bitScanForward(edges);
// Peep has multiple edges still to try.
if (edges & ~(1 << chosenEdge))
@@ -1398,7 +1397,7 @@ namespace OpenRCT2::PathFinding
* or for different edges with equal value, the edge with the
* least steps (best_sub). */
int32_t numEdges = std::popcount(edges);
for (int32_t testEdge = chosenEdge; testEdge != -1; testEdge = UtilBitScanForward(edges))
for (int32_t testEdge = chosenEdge; testEdge != -1; testEdge = Numerics::bitScanForward(edges))
{
edges &= ~(1 << testEdge);
uint8_t height = loc.z;
@@ -1945,7 +1944,7 @@ namespace OpenRCT2::PathFinding
edges &= ~(1 << direction);
}
direction = UtilBitScanForward(edges);
direction = Numerics::bitScanForward(edges);
// IF only one edge to choose from
if ((edges & ~(1 << direction)) == 0)
{
+1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "../entity/Peep.h"
#include "../util/Util.h"
#include <span>
+2 -2
View File
@@ -31,6 +31,7 @@
# include "../PlatformEnvironment.h"
# include "../Version.h"
# include "../config/Config.h"
# include "../core/Compression.h"
# include "../core/Console.hpp"
# include "../core/Guard.hpp"
# include "../core/Path.hpp"
@@ -42,7 +43,6 @@
# include "../park/ParkFile.h"
# include "../scenario/Scenario.h"
# include "../util/SawyerCoding.h"
# include "../util/Util.h"
# include "Platform.h"
# define WSZ(x) L"" x
@@ -139,7 +139,7 @@ static bool OnCrash(
FILE* input = _wfopen(dumpFilePath, L"rb");
FILE* dest = _wfopen(dumpFilePathGZIP, L"wb");
if (UtilGzipCompress(input, dest))
if (Compression::gzipCompress(input, dest))
{
// TODO: enable upload of gzip-compressed dumps once supported on
// backtrace.io (uncomment the line below). For now leave compression
-1
View File
@@ -16,7 +16,6 @@
# include "../core/Memory.hpp"
# include "../core/Path.hpp"
# include "../core/String.hpp"
# include "../util/Util.h"
# include <cerrno>
# include <clocale>
-1
View File
@@ -12,7 +12,6 @@
#include "../audio/audio.h"
#include "../entity/EntityList.h"
#include "../rct12/RCT12.h"
#include "../util/Util.h"
#include "../world/tile_element/TileElement.h"
#include "../world/tile_element/TrackElement.h"
#include "Ride.h"
+2 -2
View File
@@ -1481,7 +1481,7 @@ static int32_t RideGetNewBreakdownProblem(const Ride& ride)
int32_t breakdownProblem;
while (problemBits != 0)
{
breakdownProblem = UtilBitScanForward(problemBits);
breakdownProblem = Numerics::bitScanForward(problemBits);
problemBits &= ~(1 << breakdownProblem);
totalProbability += _breakdownProblemProbabilities[breakdownProblem];
}
@@ -1495,7 +1495,7 @@ static int32_t RideGetNewBreakdownProblem(const Ride& ride)
problemBits = availableBreakdownProblems;
do
{
breakdownProblem = UtilBitScanForward(problemBits);
breakdownProblem = Numerics::bitScanForward(problemBits);
problemBits &= ~(1 << breakdownProblem);
randomProbability -= _breakdownProblemProbabilities[breakdownProblem];
} while (randomProbability >= 0);
-1
View File
@@ -28,7 +28,6 @@
#include "../network/network.h"
#include "../ui/UiContext.h"
#include "../ui/WindowManager.h"
#include "../util/Util.h"
#include "../windows/Intent.h"
#include "../world/Banner.h"
#include "../world/Climate.h"
+1
View File
@@ -13,6 +13,7 @@
#include "../Context.h"
#include "../GameState.h"
#include "../OpenRCT2.h"
#include "../core/UnitConversion.h"
#include "../interface/Window.h"
#include "../localisation/Localisation.Date.h"
#include "../profiling/Profiling.h"
+1 -1
View File
@@ -11,9 +11,9 @@
#include "../core/Money.hpp"
#include "../entity/Litter.h"
#include "../util/Util.h"
struct Ride;
enum class PeepThoughtType : uint8_t;
enum class ShopItem : uint8_t
-1
View File
@@ -22,7 +22,6 @@
#include "../rct1/RCT1.h"
#include "../ride/RideColour.h"
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
#include "../world/Footpath.h"
#include "../world/Map.h"
#include "../world/MapAnimation.h"
+1
View File
@@ -10,6 +10,7 @@
#include "TrackData.h"
#include "../localisation/StringIds.h"
#include "../util/Util.h"
#include "Track.h"
#include "TrackPaint.h"
+3 -1
View File
@@ -35,6 +35,7 @@
#include "../core/File.h"
#include "../core/Numerics.hpp"
#include "../core/String.hpp"
#include "../core/UnitConversion.h"
#include "../drawing/X8DrawingEngine.h"
#include "../interface/Viewport.h"
#include "../localisation/StringIds.h"
@@ -888,7 +889,8 @@ static void TrackDesignMirrorMaze(TrackDesign& td)
uint32_t mazeEntry = maze.mazeEntry;
uint16_t newEntry = 0;
for (uint8_t position = UtilBitScanForward(mazeEntry); position != 0xFF; position = UtilBitScanForward(mazeEntry))
for (uint8_t position = Numerics::bitScanForward(mazeEntry); position != 0xFF;
position = Numerics::bitScanForward(mazeEntry))
{
mazeEntry &= ~(1 << position);
newEntry |= (1 << maze_segment_mirror_map[position]);
@@ -21,7 +21,6 @@
#include "../localisation/LocalisationService.h"
#include "../object/ObjectRepository.h"
#include "../ride/RideData.h"
#include "../util/Util.h"
#include "TrackDesign.h"
#include <memory>
-1
View File
@@ -21,7 +21,6 @@
#include "../object/ObjectManager.h"
#include "../rct2/RCT2.h"
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
#include "../windows/Intent.h"
#include "../world/Footpath.h"
#include "../world/Scenery.h"
@@ -9,6 +9,7 @@
#pragma once
#include "../util/Util.h"
#include "Track.h"
#include <cstdint>
+1
View File
@@ -24,6 +24,7 @@
#include "../core/Guard.hpp"
#include "../core/Path.hpp"
#include "../core/Random.hpp"
#include "../core/UnitConversion.h"
#include "../entity/Duck.h"
#include "../entity/Guest.h"
#include "../entity/Staff.h"
@@ -12,6 +12,7 @@
# include "ScRide.hpp"
# include "../../../Context.h"
# include "../../../core/UnitConversion.h"
# include "../../../ride/Ride.h"
# include "../../../ride/RideData.h"
# include "../../Duktape.hpp"
-222
View File
@@ -16,7 +16,6 @@
#include "../interface/Window.h"
#include "../platform/Platform.h"
#include "../scenes/title/TitleScene.h"
#include "zlib.h"
#include <algorithm>
#include <cassert>
@@ -25,71 +24,6 @@
#include <ctime>
#include <random>
int32_t SquaredMetresToSquaredFeet(int32_t squaredMetres)
{
// 1 metre squared = 10.7639104 feet squared
// RCT2 approximates as 11
return squaredMetres * 11;
}
int32_t MetresToFeet(int32_t metres)
{
// 1 metre = 3.2808399 feet
// RCT2 approximates as 3.28125
return (metres * 840) / 256;
}
int32_t FeetToMetres(int32_t feet)
{
return feet * 256 / 840;
}
int32_t MphToKmph(int32_t mph)
{
// 1 mph = 1.60934 kmph
// RCT2 approximates as 1.609375
return (mph * 1648) >> 10;
}
int32_t MphToDmps(int32_t mph)
{
// 1 mph = 4.4704 decimeters/s
return (mph * 73243) >> 14;
}
int32_t BaseZToMetres(int16_t baseZ)
{
return (baseZ / 2 - 7) * 1.5;
}
uint8_t MetresToBaseZ(int16_t metres)
{
return ((metres / 1.5) + 7) * 2;
}
int32_t HeightUnitsToMetres(int32_t heightUnit)
{
// 1 unit = 0.75 metres
return (heightUnit * 3) >> 2;
}
int32_t ToHumanReadableSpeed(int32_t baseSpeed)
{
// Divide this value by 29127 to get the human-readable max speed
// (in RCT2, display_speed = (max_speed * 9) >> 18)
return (baseSpeed * 9) >> 18;
}
uint16_t ToHumanReadableAirTime(uint16_t airTime)
{
return airTime * 3;
}
int32_t ToHumanReadableRideLength(int32_t rideLength)
{
return rideLength >> 16;
}
/* Case insensitive logical compare */
// Example:
// - Guest 10
@@ -225,162 +159,6 @@ float UtilRandNormalDistributed()
return _distributor(_prng);
}
constexpr size_t CHUNK = 128 * 1024;
// Compress the source to gzip-compatible stream, write to dest.
// Mainly used for compressing the crashdumps
bool UtilGzipCompress(FILE* source, FILE* dest)
{
if (source == nullptr || dest == nullptr)
{
return false;
}
int ret, flush;
size_t have;
z_stream strm{};
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
int windowBits = 15;
int GZIP_ENCODING = 16;
ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, windowBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
{
LOG_ERROR("Failed to initialise stream");
return false;
}
do
{
strm.avail_in = uInt(fread(in, 1, CHUNK, source));
if (ferror(source))
{
deflateEnd(&strm);
LOG_ERROR("Failed to read data from source");
return false;
}
flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = in;
do
{
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, flush);
if (ret == Z_STREAM_ERROR)
{
LOG_ERROR("Failed to compress data");
return false;
}
have = CHUNK - strm.avail_out;
if (fwrite(out, 1, have, dest) != have || ferror(dest))
{
deflateEnd(&strm);
LOG_ERROR("Failed to write data to destination");
return false;
}
} while (strm.avail_out == 0);
} while (flush != Z_FINISH);
deflateEnd(&strm);
return true;
}
std::vector<uint8_t> Gzip(const void* data, const size_t dataLen)
{
assert(data != nullptr);
std::vector<uint8_t> output;
z_stream strm{};
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
{
const auto ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
{
throw std::runtime_error("deflateInit2 failed with error " + std::to_string(ret));
}
}
int flush = 0;
const auto* src = static_cast<const Bytef*>(data);
size_t srcRemaining = dataLen;
do
{
const auto nextBlockSize = std::min(srcRemaining, CHUNK);
srcRemaining -= nextBlockSize;
flush = srcRemaining == 0 ? Z_FINISH : Z_NO_FLUSH;
strm.avail_in = static_cast<uInt>(nextBlockSize);
strm.next_in = const_cast<Bytef*>(src);
do
{
output.resize(output.size() + nextBlockSize);
strm.avail_out = static_cast<uInt>(nextBlockSize);
strm.next_out = &output[output.size() - nextBlockSize];
const auto ret = deflate(&strm, flush);
if (ret == Z_STREAM_ERROR)
{
throw std::runtime_error("deflate failed with error " + std::to_string(ret));
}
output.resize(output.size() - strm.avail_out);
} while (strm.avail_out == 0);
src += nextBlockSize;
} while (flush != Z_FINISH);
deflateEnd(&strm);
return output;
}
std::vector<uint8_t> Ungzip(const void* data, const size_t dataLen)
{
assert(data != nullptr);
std::vector<uint8_t> output;
z_stream strm{};
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
{
const auto ret = inflateInit2(&strm, 15 | 16);
if (ret != Z_OK)
{
throw std::runtime_error("inflateInit2 failed with error " + std::to_string(ret));
}
}
int flush = 0;
const auto* src = static_cast<const Bytef*>(data);
size_t srcRemaining = dataLen;
do
{
const auto nextBlockSize = std::min(srcRemaining, CHUNK);
srcRemaining -= nextBlockSize;
flush = srcRemaining == 0 ? Z_FINISH : Z_NO_FLUSH;
strm.avail_in = static_cast<uInt>(nextBlockSize);
strm.next_in = const_cast<Bytef*>(src);
do
{
output.resize(output.size() + nextBlockSize);
strm.avail_out = static_cast<uInt>(nextBlockSize);
strm.next_out = &output[output.size() - nextBlockSize];
const auto ret = inflate(&strm, flush);
if (ret == Z_STREAM_ERROR)
{
throw std::runtime_error("deflate failed with error " + std::to_string(ret));
}
output.resize(output.size() - strm.avail_out);
} while (strm.avail_out == 0);
src += nextBlockSize;
} while (flush != Z_FINISH);
inflateEnd(&strm);
return output;
}
uint8_t Lerp(uint8_t a, uint8_t b, float t)
{
if (t <= 0)
-64
View File
@@ -19,66 +19,6 @@
#include <type_traits>
#include <vector>
#ifdef _MSC_VER
# include <intrin.h>
#endif
int32_t SquaredMetresToSquaredFeet(int32_t squaredMetres);
int32_t MetresToFeet(int32_t metres);
int32_t FeetToMetres(int32_t feet);
int32_t MphToKmph(int32_t mph);
int32_t MphToDmps(int32_t mph);
int32_t BaseZToMetres(int16_t baseZ);
uint8_t MetresToBaseZ(int16_t metres);
int32_t HeightUnitsToMetres(int32_t heightUnit);
int32_t ToHumanReadableSpeed(int32_t baseSpeed);
uint16_t ToHumanReadableAirTime(uint16_t airTime);
int32_t ToHumanReadableRideLength(int32_t rideLength);
inline int32_t UtilBitScanForward(uint32_t source)
{
#if defined(_MSC_VER) && (_MSC_VER >= 1400) // Visual Studio 2005
unsigned long i;
uint8_t success = _BitScanForward(&i, source);
return success != 0 ? i : -1;
#elif defined(__GNUC__)
int32_t success = __builtin_ffs(source);
return success - 1;
#else
# pragma message("Falling back to iterative bitscan forward, consider using intrinsics")
// This is a low-hanging optimisation boost, check if your compiler offers
// any intrinsic.
// cf. https://github.com/OpenRCT2/OpenRCT2/pull/2093
for (int32_t i = 0; i < 32; i++)
if (source & (1u << i))
return i;
return -1;
#endif
}
inline int32_t UtilBitScanForward(uint64_t source)
{
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && defined(_M_X64) // Visual Studio 2005
unsigned long i;
uint8_t success = _BitScanForward64(&i, source);
return success != 0 ? i : -1;
#elif defined(__GNUC__)
int32_t success = __builtin_ffsll(source);
return success - 1;
#else
# pragma message("Falling back to iterative bitscan forward, consider using intrinsics")
// This is a low-hanging optimisation boost, check if your compiler offers
// any intrinsic.
// cf. https://github.com/OpenRCT2/OpenRCT2/pull/2093
for (int32_t i = 0; i < 64; i++)
if (source & (1uLL << i))
return i;
return -1;
#endif
}
int32_t StrLogicalCmp(char const* a, char const* b);
char* SafeStrCpy(char* destination, const char* source, size_t num);
char* SafeStrCat(char* destination, const char* source, size_t size);
@@ -86,10 +26,6 @@ char* SafeStrCat(char* destination, const char* source, size_t size);
uint32_t UtilRand();
float UtilRandNormalDistributed();
bool UtilGzipCompress(FILE* source, FILE* dest);
std::vector<uint8_t> Gzip(const void* data, const size_t dataLen);
std::vector<uint8_t> Ungzip(const void* data, const size_t dataLen);
template<typename T>
constexpr T AddClamp(T value, T valueToAdd)
{
+3 -2
View File
@@ -9,8 +9,7 @@
#pragma once
#include "../drawing/Drawing.h"
#include "../util/Util.h"
#include <cstdint>
enum class ClimateType : uint8_t
{
@@ -78,6 +77,8 @@ void ClimateUpdateSound();
void ClimateStopWeatherSound();
void ClimateForceWeather(WeatherType weather);
enum class FilterPaletteID : int32_t;
bool ClimateIsRaining();
bool ClimateIsSnowing();
bool ClimateIsSnowingHeavily();
+1 -2
View File
@@ -32,7 +32,6 @@
#include "../ride/RideData.h"
#include "../ride/Track.h"
#include "../ride/TrackData.h"
#include "../util/Util.h"
#include "Location.hpp"
#include "Map.h"
#include "MapAnimation.h"
@@ -1020,7 +1019,7 @@ static void FootpathFixOwnership(const CoordsXY& mapPos)
static bool GetNextDirection(uint32_t edges, int32_t* direction)
{
int32_t index = UtilBitScanForward(edges);
int32_t index = Numerics::bitScanForward(edges);
if (index == -1)
return false;
-1
View File
@@ -24,7 +24,6 @@
#include "../object/TerrainEdgeObject.h"
#include "../object/TerrainSurfaceObject.h"
#include "../platform/Platform.h"
#include "../util/Util.h"
#include "../world/tile_element/Slope.h"
#include "../world/tile_element/SmallSceneryElement.h"
#include "../world/tile_element/SurfaceElement.h"