diff --git a/include/mcpp/util.h b/include/mcpp/util.h index d425676..191af2f 100644 --- a/include/mcpp/util.h +++ b/include/mcpp/util.h @@ -69,8 +69,8 @@ struct Coordinate { Coordinate operator-(const Coordinate& obj) const; /** - * @brief Implements hash algorithm for Coordinate object using XOR, bit - * shifts and prime multiplication + * @brief Implements hash algorithm for Coordinate object using non-negative + * mapping and weighted coordinate values. * * @param obj The Coordinate object to hash. * @return Hash of Coordinate object. diff --git a/src/util.cpp b/src/util.cpp index f516097..aa7c4e3 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -42,13 +42,16 @@ Coordinate Coordinate::operator-(const Coordinate& obj) const { } std::size_t Coordinate::operator()(const mcpp::Coordinate& obj) const { + // Minecraft coordinate bounds int lower = -3e7, upper = 3e7; size_t base = upper - lower + 1; - // Make x,y,z non negative - size_t nx = obj.x - lower, ny = obj.y - lower, nz = obj.z - lower; + // Convert coordinate attributes to non-negative values + size_t nx = obj.x - lower; + size_t ny = obj.y - lower; + size_t nz = obj.z - lower; - // Use overflow instead of modding + // Combine and weight coordinate values using the boundary range return nx * base * base + ny * base + nz; }