the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 165 lines 4.1 kB view raw
1#include "stdafx.h" 2 3#include "Biome.h" 4#include "BiomeSource.h" 5#include "BiomeCache.h" 6#include "System.h" 7 8BiomeCache::Block::Block(int x, int z, BiomeCache *parent) 9{ 10// temps = floatArray(ZONE_SIZE * ZONE_SIZE, false); // MGH - added "no clear" flag to arrayWithLength 11// downfall = floatArray(ZONE_SIZE * ZONE_SIZE, false); 12// biomes = BiomeArray(ZONE_SIZE * ZONE_SIZE, false); 13 biomeIndices = byteArray(ZONE_SIZE * ZONE_SIZE, false); 14 15 lastUse = 0; 16 this->x = x; 17 this->z = z; 18// parent->source->getTemperatureBlock(temps, x << ZONE_SIZE_BITS, z << ZONE_SIZE_BITS, ZONE_SIZE, ZONE_SIZE); 19// parent->source->getDownfallBlock(downfall, x << ZONE_SIZE_BITS, z << ZONE_SIZE_BITS, ZONE_SIZE, ZONE_SIZE); 20// parent->source->getBiomeBlock(biomes, x << ZONE_SIZE_BITS, z << ZONE_SIZE_BITS, ZONE_SIZE, ZONE_SIZE, false); 21 parent->source->getBiomeIndexBlock(biomeIndices, x << ZONE_SIZE_BITS, z << ZONE_SIZE_BITS, ZONE_SIZE, ZONE_SIZE, false); 22 23} 24 25BiomeCache::Block::~Block() 26{ 27// delete [] temps.data; 28// delete [] downfall.data; 29// delete [] biomes.data; 30 delete [] biomeIndices.data; 31} 32 33Biome *BiomeCache::Block::getBiome(int x, int z) 34{ 35// return biomes[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)]; 36 37 int biomeIndex = biomeIndices[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)]; 38 return Biome::biomes[biomeIndex]; 39} 40 41float BiomeCache::Block::getTemperature(int x, int z) 42{ 43// return temps[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)]; 44 45 int biomeIndex = biomeIndices[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)]; 46 return Biome::biomes[biomeIndex]->getTemperature(); 47 48} 49 50float BiomeCache::Block::getDownfall(int x, int z) 51{ 52// return downfall[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)]; 53 54 int biomeIndex = biomeIndices[(x & ZONE_SIZE_MASK) | ((z & ZONE_SIZE_MASK) << ZONE_SIZE_BITS)]; 55 return Biome::biomes[biomeIndex]->getDownfall(); 56 57} 58 59BiomeCache::BiomeCache(BiomeSource *source) 60{ 61 // 4J Initialisors 62 lastUpdateTime = 0; 63 64 this->source = source; 65 66 InitializeCriticalSection(&m_CS); 67 68} 69 70BiomeCache::~BiomeCache() 71{ 72 // 4J Stu - Delete source? 73 // delete source; 74 75 for(AUTO_VAR(it, all.begin()); it != all.end(); ++it) 76 { 77 delete (*it); 78 } 79 DeleteCriticalSection(&m_CS); 80} 81 82 83BiomeCache::Block *BiomeCache::getBlockAt(int x, int z) 84{ 85 EnterCriticalSection(&m_CS); 86 x >>= ZONE_SIZE_BITS; 87 z >>= ZONE_SIZE_BITS; 88 __int64 slot = (((__int64) x) & 0xffffffffl) | ((((__int64) z) & 0xffffffffl) << 32l); 89 AUTO_VAR(it, cached.find(slot)); 90 Block *block = NULL; 91 if (it == cached.end()) 92 { 93 MemSect(48); 94 block = new Block(x, z, this); 95 cached[slot] = block; 96 all.push_back(block); 97 MemSect(0); 98 } 99 else 100 { 101 block = it->second; 102 } 103 block->lastUse = app.getAppTime(); 104 LeaveCriticalSection(&m_CS); 105 return block; 106} 107 108 109Biome *BiomeCache::getBiome(int x, int z) 110{ 111 return getBlockAt(x, z)->getBiome(x, z); 112} 113 114float BiomeCache::getTemperature(int x, int z) 115{ 116 return getBlockAt(x, z)->getTemperature(x, z); 117} 118 119float BiomeCache::getDownfall(int x, int z) 120{ 121 return getBlockAt(x, z)->getDownfall(x, z); 122} 123 124void BiomeCache::update() 125{ 126 EnterCriticalSection(&m_CS); 127 __int64 now = app.getAppTime(); 128 __int64 utime = now - lastUpdateTime; 129 if (utime > DECAY_TIME / 4 || utime < 0) 130 { 131 lastUpdateTime = now; 132 133 for (AUTO_VAR(it, all.begin()); it != all.end();) 134 { 135 Block *block = *it; 136 __int64 time = now - block->lastUse; 137 if (time > DECAY_TIME || time < 0) 138 { 139 it = all.erase(it); 140 __int64 slot = (((__int64) block->x) & 0xffffffffl) | ((((__int64) block->z) & 0xffffffffl) << 32l); 141 cached.erase(slot); 142 delete block; 143 } 144 else 145 { 146 ++it; 147 } 148 } 149 } 150 LeaveCriticalSection(&m_CS); 151} 152 153BiomeArray BiomeCache::getBiomeBlockAt(int x, int z) 154{ 155 byteArray indices = getBlockAt(x, z)->biomeIndices; 156 BiomeArray biomes(indices.length); 157 for(int i=0;i<indices.length;i++) 158 biomes[i] = Biome::biomes[indices[i]]; 159 return biomes; 160} 161 162byteArray BiomeCache::getBiomeIndexBlockAt(int x, int z) 163{ 164 return getBlockAt(x, z)->biomeIndices; 165}