Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
Revert common::
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamYuan committed May 9, 2023
1 parent a2d9205 commit a0adb42
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
21 changes: 11 additions & 10 deletions client/include/client/Chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class Chunk : public std::enable_shared_from_this<Chunk> {
using Block = block::Block;

public:
static constexpr uint32_t kSize = kChunkSize;
static constexpr uint32_t kSize = common::kChunkSize;

template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
static inline constexpr void Index2XYZ(uint32_t idx, T *xyz) {
return ChunkIndex2XYZ(idx, xyz);
return common::ChunkIndex2XYZ(idx, xyz);
}
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
static inline constexpr uint32_t XYZ2Index(T x, T y, T z) {
return ChunkXYZ2Index(x, y, z);
return common::ChunkXYZ2Index(x, y, z);
}
template <typename T>
static inline constexpr typename std::enable_if<std::is_signed<T>::value && std::is_integral<T>::value, bool>::type
Expand All @@ -46,20 +46,20 @@ class Chunk : public std::enable_shared_from_this<Chunk> {
template <typename T>
static inline constexpr typename std::enable_if<std::is_integral<T>::value, uint32_t>::type
CmpXYZ2NeighbourIndex(T cmp_x, T cmp_y, T cmp_z) {
return ::CmpXYZ2NeighbourIndex(cmp_x, cmp_y, cmp_z);
return common::CmpXYZ2NeighbourIndex(cmp_x, cmp_y, cmp_z);
}
template <typename T>
static inline constexpr typename std::enable_if<std::is_integral<T>::value, uint32_t>::type
GetBlockNeighbourIndex(T x, T y, T z) {
return GetBlockChunkNeighbourIndex(x, y, z);
return common::GetBlockChunkNeighbourIndex(x, y, z);
}
template <typename T>
static inline constexpr typename std::enable_if<std::is_signed<T>::value && std::is_integral<T>::value, void>::type
NeighbourIndex2CmpXYZ(uint32_t idx, T *cmp_xyz) {
return ::NeighbourIndex2CmpXYZ(idx, cmp_xyz);
return common::NeighbourIndex2CmpXYZ(idx, cmp_xyz);
}

inline const ChunkPos3 &GetPosition() const { return m_position; }
inline const common::ChunkPos3 &GetPosition() const { return m_position; }

// Block Getter and Setter
inline const Block *GetBlockData() const { return m_blocks; }
Expand Down Expand Up @@ -131,12 +131,13 @@ class Chunk : public std::enable_shared_from_this<Chunk> {
}
inline void PushLight(uint64_t version, const Light *light_buffer) {
m_light_sync.Done(version, [this, light_buffer]() {
std::copy(light_buffer, light_buffer + kChunkSize * kChunkSize * kChunkSize, m_lights);
std::copy(light_buffer, light_buffer + common::kChunkSize * common::kChunkSize * common::kChunkSize,
m_lights);
});
}

// Creation
static inline std::shared_ptr<Chunk> Create(const std::weak_ptr<World> &world, const ChunkPos3 &position) {
static inline std::shared_ptr<Chunk> Create(const std::weak_ptr<World> &world, const common::ChunkPos3 &position) {
auto ret = std::make_shared<Chunk>();
ret->m_position = position;
ret->m_world_weak_ptr = world;
Expand All @@ -153,7 +154,7 @@ class Chunk : public std::enable_shared_from_this<Chunk> {
Block m_blocks[kSize * kSize * kSize];
Light m_lights[kSize * kSize * kSize];

ChunkPos3 m_position{};
common::ChunkPos3 m_position{};

std::weak_ptr<Chunk> m_neighbour_weak_ptrs[26];
std::weak_ptr<World> m_world_weak_ptr;
Expand Down
3 changes: 2 additions & 1 deletion client/include/client/ChunkMesher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class ChunkMesher final : public ChunkWorkerS26Base {
LightLvl light_lvl;
};
static_assert(sizeof(LightEntry) == 4);
inline static thread_local Light m_light_buffer[(kChunkSize + 30) * (kChunkSize + 30) * (kChunkSize + 30)]{};
inline static thread_local Light
m_light_buffer[(common::kChunkSize + 30) * (common::kChunkSize + 30) * (common::kChunkSize + 30)]{};
static thread_local std::queue<LightEntry> m_light_queue;

struct AO4 { // compressed ambient occlusion data for 4 vertices (a face)
Expand Down
8 changes: 4 additions & 4 deletions client/include/client/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class World : public std::enable_shared_from_this<World> {
friend class ENetClient;

// Chunks
std::unordered_map<ChunkPos3, std::shared_ptr<Chunk>> m_chunks;
std::unordered_map<common::ChunkPos3, std::shared_ptr<Chunk>> m_chunks;

// Work Queue
std::shared_ptr<common::WorkPool> m_work_pool_ptr;
Expand All @@ -51,9 +51,9 @@ class World : public std::enable_shared_from_this<World> {

inline const auto &GetWorkPoolPtr() const { return m_work_pool_ptr; }

std::shared_ptr<Chunk> FindChunk(const ChunkPos3 &position) const;
std::shared_ptr<Chunk> PushChunk(const ChunkPos3 &position);
void EraseChunk(const ChunkPos3 &position) { m_chunks.erase(position); }
std::shared_ptr<Chunk> FindChunk(const common::ChunkPos3 &position) const;
std::shared_ptr<Chunk> PushChunk(const common::ChunkPos3 &position);
void EraseChunk(const common::ChunkPos3 &position) { m_chunks.erase(position); }

void Update(const glm::vec3 &position);
};
Expand Down
9 changes: 8 additions & 1 deletion client/src/ChunkLighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

template <typename T, typename = std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>>>
static constexpr uint32_t chunk_xyz_extended14_to_index(T x, T y, T z) {
constexpr auto kChunkSize = common::kChunkSize;

bool x_inside = 0 <= x && x < kChunkSize, y_inside = 0 <= y && y < kChunkSize, z_inside = 0 <= z && z < kChunkSize;
uint32_t bits = x_inside | (y_inside << 1u) | (z_inside << 2u);
if (bits == 7u)
return ChunkXYZ2Index(x, y, z);
return common::ChunkXYZ2Index(x, y, z);
constexpr uint32_t kOffsets[8] = {
kChunkSize * kChunkSize * kChunkSize,
kChunkSize * kChunkSize * kChunkSize + 28 * 28 * 28,
Expand Down Expand Up @@ -39,6 +41,8 @@ static constexpr uint32_t chunk_xyz_extended14_to_index(T x, T y, T z) {

template <typename T, typename = std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>>>
static constexpr bool light_interfere(T x, T y, T z, LightLvl lvl) {
constexpr auto kChunkSize = common::kChunkSize;

if (lvl <= 1)
return false;
uint32_t dist = 0;
Expand All @@ -57,6 +61,7 @@ struct LightEntry {
};
class LightQueue {
private:
inline static constexpr auto kChunkSize = common::kChunkSize;
LightEntry m_entries[(kChunkSize + 28) * (kChunkSize + 28) * (kChunkSize + 28)]{};
LightEntry *m_back = m_entries, *m_top = m_entries;

Expand Down Expand Up @@ -89,6 +94,8 @@ void ChunkLighter::Run() {
if (!lock())
return;

constexpr auto kChunkSize = common::kChunkSize;

thread_local static Light light_buffer[(kChunkSize + 28) * (kChunkSize + 28) * (kChunkSize + 28)];
thread_local static LightQueue sunlight_queue, torchlight_queue;

Expand Down
4 changes: 2 additions & 2 deletions client/src/WorldLoadingList.inl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <common/Position.hpp>
#include <client/Config.hpp>

constexpr ChunkPos3 kWorldLoadingList[] = {
constexpr common::ChunkPos3 kWorldLoadingList[] = {
{0, 0, 0}, {0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, 1, 1},
{0, 1, -1}, {0, -1, 0}, {0, -1, 1}, {0, -1, -1}, {1, 0, 0},
{1, 0, 1}, {1, 0, -1}, {1, 1, 0}, {1, 1, 1}, {1, 1, -1},
Expand Down Expand Up @@ -8291,5 +8291,5 @@ constexpr ChunkPos3 kWorldLoadingList[] = {
{20, 7, 0}, {20, 7, -2}, {20, 7, 1}, {20, 7, 2}, {21, 0, 0},
{21, 0, 1}, {21, 0, -1}, {21, 1, 0}, {21, 1, 1}, {21, 1, -1},
{21, -1, 0}, {21, -1, 1}, {21, -1, -1}, };
constexpr const ChunkPos3 *kWorldLoadingRadiusEnd[kWorldMaxLoadRadius + 1] = {
constexpr const common::ChunkPos3 *kWorldLoadingRadiusEnd[kWorldMaxLoadRadius + 1] = {
kWorldLoadingList + 27, kWorldLoadingList + 81, kWorldLoadingList + 179, kWorldLoadingList + 389, kWorldLoadingList + 667, kWorldLoadingList + 1141, kWorldLoadingList + 1767, kWorldLoadingList + 2501, kWorldLoadingList + 3503, kWorldLoadingList + 4825, kWorldLoadingList + 6331, kWorldLoadingList + 8121, kWorldLoadingList + 10107, kWorldLoadingList + 12677, kWorldLoadingList + 15547, kWorldLoadingList + 18781, kWorldLoadingList + 22263, kWorldLoadingList + 26313, kWorldLoadingList + 30911, kWorldLoadingList + 35921, kWorldLoadingList + 41443, };
4 changes: 4 additions & 0 deletions common/include/common/Position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "Size.hpp"

namespace common {

using ChunkPos1 = int16_t;
using ChunkPos2 = glm::vec<2, ChunkPos1>;
using ChunkPos3 = glm::vec<3, ChunkPos1>;
Expand Down Expand Up @@ -62,4 +64,6 @@ NeighbourIndex2CmpXYZ(uint32_t idx, T *cmp_xyz) {
cmp_xyz[0] = kRevLookUp[idx / 3u];
}

} // namespace common

#endif
5 changes: 5 additions & 0 deletions common/include/common/Size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#define HYPERCRAFT_COMMON_SIZE_HPP

#include <cinttypes>

namespace common {

constexpr uint32_t kChunkSize = 32u;

}

#endif

0 comments on commit a0adb42

Please sign in to comment.