1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-05 19:31:04 +00:00

Various random code cleanups

This commit is contained in:
sfan5 2025-03-01 11:53:37 +01:00
parent 358658fa34
commit 7892541383
73 changed files with 216 additions and 285 deletions

View file

@ -194,7 +194,7 @@ bool VectorAreaStore::removeArea(u32 id)
if (it == areas_map.end())
return false;
Area *a = &it->second;
for (std::vector<Area *>::iterator v_it = m_areas.begin();
for (auto v_it = m_areas.begin();
v_it != m_areas.end(); ++v_it) {
if (*v_it == a) {
m_areas.erase(v_it);
@ -259,7 +259,7 @@ bool SpatialAreaStore::insertArea(Area *a)
bool SpatialAreaStore::removeArea(u32 id)
{
std::map<u32, Area>::iterator itr = areas_map.find(id);
auto itr = areas_map.find(id);
if (itr != areas_map.end()) {
Area *a = &itr->second;
bool result = m_tree->deleteData(get_spatial_region(a->minedge,

View file

@ -162,7 +162,7 @@ private:
{
u32 id = in.getIdentifier();
std::map<u32, Area>::iterator itr = m_store->areas_map.find(id);
auto itr = m_store->areas_map.find(id);
assert(itr != m_store->areas_map.end());
m_result->push_back(&itr->second);
}

View file

@ -7,7 +7,6 @@
#include "log.h"
#include "constants.h" // BS, MAP_BLOCKSIZE
#include "noise.h" // PseudoRandom, PcgRandom
#include "threading/mutex_auto_lock.h"
#include <cstring>
#include <cmath>
@ -73,15 +72,14 @@ u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed)
h *= m;
}
const unsigned char *data2 = (const unsigned char *)data;
switch (len & 7) {
case 7: h ^= (u64)data2[6] << 48; [[fallthrough]];
case 6: h ^= (u64)data2[5] << 40; [[fallthrough]];
case 5: h ^= (u64)data2[4] << 32; [[fallthrough]];
case 4: h ^= (u64)data2[3] << 24; [[fallthrough]];
case 3: h ^= (u64)data2[2] << 16; [[fallthrough]];
case 2: h ^= (u64)data2[1] << 8; [[fallthrough]];
case 1: h ^= (u64)data2[0];
case 7: h ^= (u64)data[6] << 48; [[fallthrough]];
case 6: h ^= (u64)data[5] << 40; [[fallthrough]];
case 5: h ^= (u64)data[4] << 32; [[fallthrough]];
case 4: h ^= (u64)data[3] << 24; [[fallthrough]];
case 3: h ^= (u64)data[2] << 16; [[fallthrough]];
case 2: h ^= (u64)data[1] << 8; [[fallthrough]];
case 1: h ^= (u64)data[0];
h *= m;
}
@ -105,11 +103,7 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
// Block center position
v3f blockpos(
((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
);
v3f blockpos = v3f::from(blockpos_nodes + MAP_BLOCKSIZE / 2) * BS;
// Block position relative to camera
v3f blockpos_relative = blockpos - camera_pos;

View file

@ -15,14 +15,16 @@
#include <cmath>
#include <algorithm>
#define rangelim(d, min, max) ((d) < (min) ? (min) : ((d) > (max) ? (max) : (d)))
#define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x))
// The naive swap performs better than the xor version
#define SWAP(t, x, y) do { \
t temp = x; \
x = y; \
y = temp; \
} while (0)
// Like std::clamp but allows mismatched types
template <typename T, typename T2, typename T3>
inline constexpr T rangelim(const T &d, const T2 &min, const T3 &max)
{
if (d < (T)min)
return (T)min;
if (d > (T)max)
return (T)max;
return d;
}
// Maximum radius of a block. The magic number is
// sqrt(3.0) / 2.0 in literal form.
@ -113,23 +115,24 @@ inline bool isInArea(v3s16 p, v3s16 d)
);
}
inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2)
{
if (p1.X > p2.X)
SWAP(s16, p1.X, p2.X);
std::swap(p1.X, p2.X);
if (p1.Y > p2.Y)
SWAP(s16, p1.Y, p2.Y);
std::swap(p1.Y, p2.Y);
if (p1.Z > p2.Z)
SWAP(s16, p1.Z, p2.Z);
std::swap(p1.Z, p2.Z);
}
inline v3s16 componentwise_min(const v3s16 &a, const v3s16 &b)
{
return v3s16(MYMIN(a.X, b.X), MYMIN(a.Y, b.Y), MYMIN(a.Z, b.Z));
return v3s16(std::min(a.X, b.X), std::min(a.Y, b.Y), std::min(a.Z, b.Z));
}
inline v3s16 componentwise_max(const v3s16 &a, const v3s16 &b)
{
return v3s16(MYMAX(a.X, b.X), MYMAX(a.Y, b.Y), MYMAX(a.Z, b.Z));
return v3s16(std::max(a.X, b.X), std::max(a.Y, b.Y), std::max(a.Z, b.Z));
}
/// @brief Describes a grid with given step, oirginating at (0,0,0)
@ -290,7 +293,8 @@ inline s32 myround(f32 f)
return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f));
}
inline constexpr f32 sqr(f32 f)
template <typename T>
inline constexpr T sqr(T f)
{
return f * f;
}
@ -322,23 +326,15 @@ inline v3s16 doubleToInt(v3d p, double d)
*/
inline v3f intToFloat(v3s16 p, f32 d)
{
return v3f(
(f32)p.X * d,
(f32)p.Y * d,
(f32)p.Z * d
);
return v3f::from(p) * d;
}
// Random helper. Usually d=BS
inline aabb3f getNodeBox(v3s16 p, float d)
{
return aabb3f(
(float)p.X * d - 0.5f * d,
(float)p.Y * d - 0.5f * d,
(float)p.Z * d - 0.5f * d,
(float)p.X * d + 0.5f * d,
(float)p.Y * d + 0.5f * d,
(float)p.Z * d + 0.5f * d
v3f::from(p) * d - 0.5f * d,
v3f::from(p) * d + 0.5f * d
);
}
@ -410,14 +406,15 @@ inline float cycle_shift(float value, float by = 0, float max = 1)
return value + by;
}
inline bool is_power_of_two(u32 n)
constexpr inline bool is_power_of_two(u32 n)
{
return n != 0 && (n & (n - 1)) == 0;
}
// Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
// Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
inline u32 npot2(u32 orig) {
constexpr inline u32 npot2(u32 orig)
{
orig--;
orig |= orig >> 1;
orig |= orig >> 2;

View file

@ -139,16 +139,6 @@ std::string wide_to_utf8(std::wstring_view input)
return out;
}
void wide_add_codepoint(std::wstring &result, char32_t codepoint)
{
if ((0xD800 <= codepoint && codepoint <= 0xDFFF) || (0x10FFFF < codepoint)) {
// Invalid codepoint, replace with unicode replacement character
result.push_back(0xFFFD);
return;
}
result.push_back(codepoint);
}
#else // _WIN32
std::wstring utf8_to_wide(std::string_view input)
@ -175,32 +165,25 @@ std::string wide_to_utf8(std::wstring_view input)
return out;
}
#endif // _WIN32
void wide_add_codepoint(std::wstring &result, char32_t codepoint)
{
if (codepoint < 0x10000) {
if (0xD800 <= codepoint && codepoint <= 0xDFFF) {
// Invalid codepoint, part of a surrogate pair
// Replace with unicode replacement character
result.push_back(0xFFFD);
return;
}
result.push_back((wchar_t) codepoint);
return;
}
codepoint -= 0x10000;
if (codepoint >= 0x100000) {
// original codepoint was above 0x10FFFF, so invalid
// replace with unicode replacement character
if ((0xD800 <= codepoint && codepoint <= 0xDFFF) || codepoint > 0x10FFFF) {
// Invalid codepoint, replace with unicode replacement character
result.push_back(0xFFFD);
return;
}
result.push_back((wchar_t) ((codepoint >> 10) | 0xD800));
result.push_back((wchar_t) ((codepoint & 0x3FF) | 0xDC00));
if constexpr (sizeof(wchar_t) == 2) { // Surrogate encoding needed?
if (codepoint > 0xffff) {
result.push_back((wchar_t) ((codepoint >> 10) | 0xD800));
result.push_back((wchar_t) ((codepoint & 0x3FF) | 0xDC00));
return;
}
}
result.push_back((wchar_t) codepoint);
}
#endif // _WIN32
std::string urlencode(std::string_view str)
{
// Encodes reserved URI characters by a percent sign

View file

@ -92,22 +92,19 @@ public:
void add(const Key &key, Caller caller, CallerData callerdata,
ResultQueue<Key, T, Caller, CallerData> *dest)
{
typename std::deque<GetRequest<Key, T, Caller, CallerData> >::iterator i;
typename std::list<CallerInfo<Caller, CallerData, Key, T> >::iterator j;
{
MutexAutoLock lock(m_queue.getMutex());
/*
If the caller is already on the list, only update CallerData
*/
for (i = m_queue.getQueue().begin(); i != m_queue.getQueue().end(); ++i) {
GetRequest<Key, T, Caller, CallerData> &request = *i;
for (auto i = m_queue.getQueue().begin(); i != m_queue.getQueue().end(); ++i) {
auto &request = *i;
if (request.key != key)
continue;
for (j = request.callers.begin(); j != request.callers.end(); ++j) {
CallerInfo<Caller, CallerData, Key, T> &ca = *j;
for (auto j = request.callers.begin(); j != request.callers.end(); ++j) {
auto &ca = *j;
if (ca.caller == caller) {
ca.data = callerdata;
return;
@ -150,10 +147,9 @@ public:
void pushResult(GetRequest<Key, T, Caller, CallerData> req, T res)
{
for (typename std::list<CallerInfo<Caller, CallerData, Key, T> >::iterator
i = req.callers.begin();
for (auto i = req.callers.begin();
i != req.callers.end(); ++i) {
CallerInfo<Caller, CallerData, Key, T> &ca = *i;
auto &ca = *i;
GetResult<Key,T,Caller,CallerData> result;