18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 17:46:48 +01:00
Files
maszyna/utilities/uuid.hpp
T
jerrrrycho d237996afd fix(scene): the uuid of a scene node no longer takes the random numbers of the simulation
Each scene node drew its uuid (which only the editor goes by) from Global.random_engine, the engine of the
run's seed, two numbers a node. A node added to a scenery, or made by the program before it, moved every
random number drawn after it: the pressure the brake valves are set to, the brake delays, the sound sets
and the rest. The memory cell __simulation.metar (2be85410c, merged with #468) did that to every run, so
the same seed gave a different train from 27.11 on, with no change in its physics. The uuids come from an
engine of their own now.
2026-11-29 02:06:40 +01:00

72 lines
2.4 KiB
C++

#pragma once
#include <array>
#include <random>
#include <sstream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cstdint>
#include <mutex>
#include "utilities/utilities.h"
#include "utilities/Globals.h"
class UID {
public:
std::array<uint8_t,16> bytes;
// the uuid of each scene node, which only the editor goes by. drawn from an engine of its own: from the one of the
// simulation (Global.random_engine) each node made took two of the numbers the run's seed gives, and a node added to
// the scenery, or made by the program before it (a memory cell of the scripting interface), changed every random
// number drawn after it, the physics of the vehicles among them
static UID random() {
static std::mt19937 engine(static_cast<std::mt19937::result_type>(true_random_seed())); // NOSONAR: an identifier, not anything security-relevant
static std::mutex mutex;
UID u;
std::scoped_lock lock(mutex);
uint64_t a = engine();
uint64_t b = engine();
for (int i = 0; i < 8; ++i) u.bytes[i] = uint8_t(a >> (i * 8) & 0xFF);
for (int i = 0; i < 8; ++i) u.bytes[8 + i] = uint8_t(b >> (i * 8) & 0xFF);
u.bytes[6] = u.bytes[6] & 0x0F | 0x40;
u.bytes[8] = u.bytes[8] & 0x3F | 0x80;
return u;
}
std::string to_string() const {
// NOTE: done by hand as the text is made for each model instance of a scenery, and a string stream took a few microseconds each
static constexpr char digits[] = "0123456789abcdef";
std::string text;
text.reserve(36);
// format 12-4-4-4-8, the one node names in scenery files were written with
for (int i = 0; i < 16; ++i) {
if (i == 6 || i == 8 || i == 10 || i == 12) text += '-';
text += digits[bytes[i] >> 4];
text += digits[bytes[i] & 0x0F];
}
return text;
}
static UID from_string(const std::string& str) {
std::istringstream is(str);
is >> std::hex;
UID u;
for (int i = 0; i < 16; ++i) {
int byte;
is >> byte;
u.bytes[i] = static_cast<uint8_t>(byte);
if (is.peek() == '-') is.get();
}
return u;
}
bool operator==(const UID &other) const noexcept {
return bytes == other.bytes;
}
bool operator!=(const UID &other) const noexcept {
return !(*this == other);
}
};