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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "System/mmgr.h"
#include "MetalMap.h"
#include "ReadMap.h"
#include "System/Config/ConfigHandler.h"
#include "System/myMath.h"
CONFIG(bool, MetalMapPalette).defaultValue(false);
CR_BIND(CMetalMap,(NULL, 0, 0, 0.0f));
CR_REG_METADATA(CMetalMap,(
CR_MEMBER(extractionMap)
));
CMetalMap::CMetalMap(const unsigned char* map, int _sizeX, int _sizeZ, float _metalScale)
: metalScale(_metalScale)
, sizeX(_sizeX)
, sizeZ(_sizeZ)
{
extractionMap.resize(sizeX * sizeZ, 0.0f);
metalMap.resize(sizeX * sizeZ, 0);
if (map != NULL) {
memcpy(&metalMap[0], map, sizeX * sizeZ);
} else {
metalScale = 1.0f;
}
if (configHandler->GetBool("MetalMapPalette")) {
/* Swap the green and blue channels. making metal go
black -> blue -> cyan,
rather than the usual black -> green -> cyan. */
for (int a = 0; a < 256; ++a) {
metalPal[a * 3 + 0] = a;
metalPal[a * 3 + 1] = std::max(0, a * 2 - 255);
metalPal[a * 3 + 2] = std::min(255, a * 2);
}
} else {
for (int a = 0; a < 256; ++a) {
metalPal[a * 3 + 0] = a;
metalPal[a * 3 + 1] = std::min(255, a * 2);
metalPal[a * 3 + 2] = std::max(0, a * 2 - 255);
}
}
}
CMetalMap::~CMetalMap()
{
}
static inline void ClampInt(int& var, int min, int maxPlusOne)
{
if (var < min) {
var = min;
} else if (var >= maxPlusOne) {
var = maxPlusOne - 1;
}
}
float CMetalMap::GetMetalAmount(int x1, int z1, int x2, int z2)
{
ClampInt(x1, 0, sizeX);
ClampInt(x2, 0, sizeX);
ClampInt(z1, 0, sizeZ);
ClampInt(z2, 0, sizeZ);
float metal = 0.0f;
int x, z;
for (x = x1; x < x2; x++) {
for (z = z1; z < z2; z++) {
metal += metalMap[(z * sizeX) + x];
}
}
return metal * metalScale;
}
float CMetalMap::GetMetalAmount(int x, int z)
{
ClampInt(x, 0, sizeX);
ClampInt(z, 0, sizeZ);
return metalMap[(z * sizeX) + x] * metalScale;
}
void CMetalMap::SetMetalAmount(int x, int z, float m)
{
ClampInt(x, 0, sizeX);
ClampInt(z, 0, sizeZ);
metalMap[(z * sizeX) + x] = (metalScale == 0.0f) ? 0 : Clamp((int)(m / metalScale), 0, 255);
}
float CMetalMap::RequestExtraction(int x, int z, float toDepth)
{
ClampInt(x, 0, sizeX);
ClampInt(z, 0, sizeZ);
const float current = extractionMap[(z * sizeX) + x];
if (toDepth <= current) {
return 0.0f;
}
const float available = toDepth - current;
extractionMap[(z * sizeX) + x] = toDepth;
return available;
}
void CMetalMap::RemoveExtraction(int x, int z, float depth)
{
ClampInt(x, 0, sizeX);
ClampInt(z, 0, sizeZ);
extractionMap[(z * sizeX) + x] -= depth;
}
int CMetalMap::GetMetalExtraction(int x, int z)
{
ClampInt(x, 0, sizeX);
ClampInt(z, 0, sizeZ);
return extractionMap[(z * sizeX) + x];
}
|