-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene_levels.cpp
More file actions
129 lines (109 loc) · 2.53 KB
/
Copy pathscene_levels.cpp
File metadata and controls
129 lines (109 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "template.h"
#include "Noise.h"
#include "scene.h"
#include "Edit.h"
Level* Scene::AddLevel(std::string name)
{
m_levels.emplace_back(name);
return GetLevelByIndexSafe(m_levels.size() - 1);
}
void Tmpl8::Scene::ApplyEdits(const std::vector<std::unique_ptr<Edit>>& edits)
{
#pragma omp parallel for
for (int x = 0; x < WORLDSIZE; x++)
{
for (int y = 0; y < WORLDSIZE; y++)
{
for (int z = 0; z < WORLDSIZE; z++)
{
const int3 worldPos = int3(x, y, z);
const float3 relPos = make_float3(worldPos) / float(WORLDSIZE);
Material::ID_T mat = GetVoxelAt(x, y, z).m_materialIndex;
for (const auto& edit : edits)
{
mat = edit->Evaluate(worldPos, relPos, mat);
}
Set(x, y, z, Voxel{ mat });
}
}
}
BuildChunks();
}
Level* Scene::GetLevelByIndexSafe(size_t index)
{
if (index < 0 || index >= m_levels.size())
{
return nullptr;
}
return &m_levels[index];
}
Level* Scene::GetCurrentLevelRW()
{
static Level* cached = nullptr;
static size_t cachedID = 0;
const size_t index = GetSettingsR().m_currentLevelIndex;
if (cached == nullptr || cachedID != index)
{
cachedID = index;
cached = &m_levels[index];
}
return cached;
}
const Level* const Scene::GetCurrentLevelR() const
{
static const Level* cached = nullptr;
static size_t cachedID = 0;
const size_t index = GetSettingsR().m_currentLevelIndex;
if (cached == nullptr || cachedID != index)
{
cachedID = index;
cached = &m_levels[index];
}
return cached;
}
size_t Scene::GetCurrentLevelIndex() const
{
return GetSettingsR().m_currentLevelIndex;
}
size_t Scene::GetLevelsCount() const
{
return m_levels.size();
}
void Scene::TryLoadLevelSafe(size_t index)
{
if (index < 0 || index >= m_levels.size())
{
return;
}
UnloadCurrentLevel();
GetSettingsRW().m_currentLevelIndex = index;
Level* level = GetCurrentLevelRW();
auto output = Level::Outputs{ *this, level };
level->Load(Level::Inputs{}, output);
m_LevelMaterialManager = &level->m_materialManager;
m_hasSpheres_cache = level->m_hasSpheres;
BuildChunks();
}
void Scene::ReloadCurrentLevel()
{
TryLoadLevelSafe(GetCurrentLevelIndex());
}
void Tmpl8::Scene::LoadRandomLevel()
{
size_t length = GetLevelsCount();
if (length == 0)
{
return;
}
size_t index = Noise::SimpleWhite::GetNext<size_t>(Noise::Config().MinMax(0.0f, static_cast<float>(length)));
if (index >= length)
{
index = length - 1;
}
TryLoadLevelSafe(index);
}
void Scene::UnloadCurrentLevel()
{
m_LevelMaterialManager = nullptr;
memset(grid, 0, sizeof(grid));
}