File: adjustment.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (179 lines) | stat: -rw-r--r-- 4,099 bytes parent folder | download | duplicates (2)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// clang-format on
#include "globalincs/adjustment.h"

#include "globalincs/pstypes.h"

//*************************************************
//			adjustment funcs
//*************************************************

/**
 * @brief Processes an input according to the user's configuration and returns the result.
 */
float adjustment::handle(float input) const
{
	if (only_positive && input < 0.0f)
		input = base;
	if (has_multiplier)
		input *= multipier;
	// handling adjust after multiplier makes it possible to multiply by 0 and still adjust.
	if (has_adjust)
		input += adjust;
	if (has_minimum)
		input = MAX(input, minimum);
	if (has_maximum)
		input = MIN(input, maximum);
	return input;
}

void adjustment::reset()
{
	base = 1.0f;
	has_adjust = false;
	has_minimum = false;
	has_maximum = false;
	has_multiplier = false;
	only_positive = true;
}

void adjustment::set_adjust(float in)
{
	has_adjust = true;
	adjust = in;
}

void adjustment::set_multiplier(float in)
{
	has_multiplier = true;
	multipier = in;
}

void adjustment::stack_multiplier(float in)
{
	if (has_multiplier) {
		multipier *= in;
	} else {
		multipier = in;
		has_multiplier = true;
	}
}

void adjustment::set_maximum(float in)
{
	has_maximum = true;
	maximum = in;
}

void adjustment::set_minimum(float in)
{
	has_minimum = true;
	minimum = in;
}

void adjustment::stack_minimum(float in)
{
	if (has_minimum) {
		minimum = MAX(minimum, in);
	} else {
		minimum = in;
		has_minimum = true;
	}
}
bool adjustment::read_adjust(float* out) const
{
	*out = adjust;
	return has_adjust;
}
bool adjustment::read_multiplier(float* out) const
{
	*out = multipier;
	return has_multiplier;
}
/**
 * @brief for use during the parsing of a light profile to attempt to read in an LPV
 *
 * @param filename for error reporting primairly
 * @param valuename Text of the field
 * @param profile_name The name of the profile being parsed, for error reporting
 * @param value_target The profile object parsing into
 * @param required If false it is an error for this to not find a value.
 * @return true if a succesful parse, false otherwise
 */
bool adjustment::parse(const char* filename,
	const char* valuename,
	const SCP_string& profile_name,
	adjustment* value_target,
	bool required)
{
	if (optional_string(valuename)) {
		bool parsed = true;
		int parses = 0;
		while (parsed) {
			parsed = false;
			if (parse_optional_float_into("+default:", &value_target->base)) {
				parsed = true;
				parses++;
			}
			if (parse_optional_float_into("+maximum:", &value_target->maximum)) {
				value_target->has_maximum = true;
				parsed = true;
				parses++;
			}
			if (parse_optional_float_into("+minimum:", &value_target->minimum)) {
				value_target->has_minimum = true;
				parsed = true;
				parses++;
			}
			if (parse_optional_float_into("+multiplier:", &value_target->multipier)) {
				value_target->has_multiplier = true;
				parsed = true;
				parses++;
			}
			if (parse_optional_float_into("+adjust:", &value_target->adjust)) {
				value_target->has_adjust = true;
				parsed = true;
				parses++;
			}
		}
		if (parses == 0) {
			Warning(LOCATION,
				"Adjustment config '%s' in file '%s' profile '%s' parsed but set no properties, possible "
				"malformed table.",
				valuename,
				filename,
				profile_name.c_str());
		} else if (parses > 5) {
			Warning(LOCATION,
				"Adjustment config '%s' in file '%s' section '%s' parsed too many properties, possible "
				"malformed table.",
				valuename,
				filename,
				profile_name.c_str());
		}
		return true;

	} else if (required) {
		Error(LOCATION,
			"Expected adjustment config value '%s' in file '%s' section '%s' not found",
			valuename,
			filename,
			profile_name.c_str());
	}
	return false;
}

adjustment::adjustment(const adjustment& rhs){
	*this = rhs;
}

adjustment& adjustment::operator= (const adjustment& rhs){
	has_adjust = rhs.has_adjust;
	adjust = rhs.adjust;
	has_multiplier = rhs.has_multiplier;
	multipier = rhs.multipier;
	has_minimum = rhs.has_minimum;
	minimum = rhs.minimum;
	has_maximum = rhs.has_maximum;
	maximum = rhs.maximum;
	return *this;
}