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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#include "stdafx.h"
#include "Biome.h"
#include "BiomeSource.h"
#include "BiomeCache.h"
#include "System.h"
BiomeCache::Block::Block(int x, int z, BiomeCache *parent)
{
// temps = floatArray(ZONE_SIZE * ZONE_SIZE, false); // MGH - added "no clear" flag to arrayWithLength
// downfall = floatArray(ZONE_SIZE * ZONE_SIZE, false);
// biomes = BiomeArray(ZONE_SIZE * ZONE_SIZE, false);
biomeIndices = byteArray(static_cast<unsigned int>(ZONE_SIZE * ZONE_SIZE), false);
lastUse = 0;
this->x = x;
this->z = z;
// parent->source->getTemperatureBlock(temps, x << ZONE_SIZE_BITS, z << ZONE_SIZE_BITS, ZONE_SIZE, ZONE_SIZE);
// parent->source->getDownfallBlock(downfall, x << ZONE_SIZE_BITS, z << ZONE_SIZE_BITS, ZONE_SIZE, ZONE_SIZE);
// parent->source->getBiomeBlock(biomes, x << ZONE_SIZE_BITS, z << ZONE_SIZE_BITS, ZONE_SIZE, ZONE_SIZE, false);
parent->source->getBiomeIndexBlock(biomeIndices, x << ZONE_SIZE_BITS, z << ZONE_SIZE_BITS, ZONE_SIZE, ZONE_SIZE, false);
}
BiomeCache::Block::~Block()
{
// delete [] temps.data;
// delete [] downfall.data;
// delete [] biomes.data;
delete [] biomeIndices.data;
}
Biome *BiomeCache::Block::getBiome(int x, int z)
{
// return biomes[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)];
const int biomeIndex = biomeIndices[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)];
return Biome::biomes[biomeIndex];
}
float BiomeCache::Block::getTemperature(int x, int z)
{
// return temps[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)];
const int biomeIndex = biomeIndices[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)];
return Biome::biomes[biomeIndex]->getTemperature();
}
float BiomeCache::Block::getDownfall(int x, int z)
{
// return downfall[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)];
const int biomeIndex = biomeIndices[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)];
return Biome::biomes[biomeIndex]->getDownfall();
}
BiomeCache::BiomeCache(BiomeSource *source)
{
// 4J Initialisors
lastUpdateTime = 0;
this->source = source;
InitializeCriticalSection(&m_CS);
}
BiomeCache::~BiomeCache()
{
// 4J Stu - Delete source?
// delete source;
for(const auto& it : all )
{
delete it;
}
DeleteCriticalSection(&m_CS);
}
BiomeCache::Block *BiomeCache::getBlockAt(int x, int z)
{
EnterCriticalSection(&m_CS);
x >>= ZONE_SIZE_BITS;
z >>= ZONE_SIZE_BITS;
const int64_t slot = (static_cast<int64_t>(x) & 0xffffffffl) | ((static_cast<int64_t>(z) & 0xffffffffl) << 32l);
const auto it = cached.find(slot);
Block *block = nullptr;
if (it == cached.end())
{
MemSect(48);
block = new Block(x, z, this);
cached[slot] = block;
all.push_back(block);
MemSect(0);
}
else
{
block = it->second;
}
block->lastUse = app.getAppTime();
LeaveCriticalSection(&m_CS);
return block;
}
Biome *BiomeCache::getBiome(int x, int z)
{
return getBlockAt(x, z)->getBiome(x, z);
}
float BiomeCache::getTemperature(int x, int z)
{
return getBlockAt(x, z)->getTemperature(x, z);
}
float BiomeCache::getDownfall(int x, int z)
{
return getBlockAt(x, z)->getDownfall(x, z);
}
void BiomeCache::update()
{
EnterCriticalSection(&m_CS);
const int64_t now = app.getAppTime();
const int64_t utime = now - lastUpdateTime;
if (utime > DECAY_TIME / 4 || utime < 0)
{
lastUpdateTime = now;
for (auto it = all.begin(); it != all.end();)
{
const Block *block = *it;
const int64_t time = now - block->lastUse;
if (time > DECAY_TIME || time < 0)
{
it = all.erase(it);
int64_t slot = (static_cast<int64_t>(block->x) & 0xffffffffl) | ((static_cast<int64_t>(block->z) & 0xffffffffl) << 32l);
cached.erase(slot);
delete block;
}
else
{
++it;
}
}
}
LeaveCriticalSection(&m_CS);
}
BiomeArray BiomeCache::getBiomeBlockAt(int x, int z)
{
byteArray indices = getBlockAt(x, z)->biomeIndices;
BiomeArray biomes(indices.length);
for(int i=0;i<indices.length;i++)
biomes[i] = Biome::biomes[indices[i]];
return biomes;
}
byteArray BiomeCache::getBiomeIndexBlockAt(int x, int z)
{
return getBlockAt(x, z)->biomeIndices;
}
|