File: MetalMap.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (141 lines) | stat: -rw-r--r-- 2,998 bytes parent folder | download | duplicates (3)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "MetalMap.h"
#include "System/SpringMath.h"
#include "System/EventHandler.h"

#include <cstring>


CR_BIND(CMetalMap, )

CR_REG_METADATA(CMetalMap,(
	CR_MEMBER(metalScale),
	CR_MEMBER(sizeX),
	CR_MEMBER(sizeZ),

	CR_IGNORED(texturePalette),
	CR_MEMBER(distributionMap),
	CR_MEMBER(extractionMap)
))


CMetalMap metalMap;


#ifndef NO_METALMAP
void CMetalMap::Init(const unsigned char* map, int _sizeX, int _sizeZ, float _metalScale)
{
	metalScale = _metalScale;
	sizeX = _sizeX;
	sizeZ = _sizeZ;

	extractionMap.clear();
	extractionMap.resize(sizeX * sizeZ, 0.0f);
	distributionMap.clear();
	distributionMap.resize(sizeX * sizeZ, 0);

	if (map != nullptr) {
		memcpy(&distributionMap[0], map, sizeX * sizeZ);
	} else {
		metalScale = 1.0f;
	}

	for (int a = 0; a < 256; ++a) {
		texturePalette[a * 3 + 0] = a;
		texturePalette[a * 3 + 1] = std::min(255, a * 2      );
		texturePalette[a * 3 + 2] = std::max(  0, a * 2 - 255);
	}
}



float CMetalMap::GetMetalAmount(int x1, int z1, int x2, int z2) const
{
	x1 = Clamp(x1, 0, sizeX - 1);
	x2 = Clamp(x2, 0, sizeX - 1);
	z1 = Clamp(z1, 0, sizeZ - 1);
	z2 = Clamp(z2, 0, sizeZ - 1);

	float metal = 0.0f;

	for (int z = z1; z < z2; z++) {
		for (int x = x1; x < x2; x++) {
			metal += GetMetalAmount(x, z);
		}
	}

	return metal;
}


float CMetalMap::GetMetalAmount(int x, int z) const
{
	x = Clamp(x, 0, sizeX - 1);
	z = Clamp(z, 0, sizeZ - 1);

	return distributionMap[(z * sizeX) + x] * metalScale;
}


void CMetalMap::SetMetalAmount(int x, int z, float m)
{
	x = Clamp(x, 0, sizeX - 1);
	z = Clamp(z, 0, sizeZ - 1);

	distributionMap[(z * sizeX) + x] = (metalScale == 0.0f) ? 0 : Clamp((int)(m / metalScale), 0, 255);

	eventHandler.MetalMapChanged(x, z);
}


float CMetalMap::RequestExtraction(int x, int z, float toDepth)
{
	x = Clamp(x, 0, sizeX - 1);
	z = Clamp(z, 0, sizeZ - 1);

	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)
{
	x = Clamp(x, 0, sizeX - 1);
	z = Clamp(z, 0, sizeZ - 1);

	extractionMap[(z * sizeX) + x] -= depth;
}


int CMetalMap::GetMetalExtraction(int x, int z) const
{
	x = Clamp(x, 0, sizeX - 1);
	z = Clamp(z, 0, sizeZ - 1);

	return extractionMap[(z * sizeX) + x];
}


#else


void CMetalMap::Init(const unsigned char* map, int _sizeX, int _sizeZ, float _metalScale) {}

float CMetalMap::GetMetalAmount(int x1, int z1, int x2, int z2) const { return 0.0f; }
float CMetalMap::GetMetalAmount(int x, int z) const { return 0.0f; }

void CMetalMap::SetMetalAmount(int x, int z, float m) {}
float CMetalMap::RequestExtraction(int x, int z, float toDepth) { return 0.0f; }
void CMetalMap::RemoveExtraction(int x, int z, float depth) {}
int CMetalMap::GetMetalExtraction(int x, int z) const { return 0; }
#endif