File: team_colors.cpp

package info (click to toggle)
freespace2 25.0.0~rc11%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 47,232 kB
  • sloc: cpp: 657,500; ansic: 22,305; sh: 293; python: 200; makefile: 198; xml: 181
file content (132 lines) | stat: -rw-r--r-- 3,631 bytes parent folder | download
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
//
//

#include "team_colors.h"
#include "globalincs/alphacolors.h"
#include "scripting/api/objs/color.h"

namespace scripting::api {

//**********HANDLE: TeamColor
ADE_OBJ(l_TeamColor, int, "teamcolor", "Team color handle");

ADE_FUNC(__tostring, l_TeamColor, nullptr, "Team color name", "string", "Team color name, or an empty string if handle is invalid")
{
	int idx;
	if (!ade_get_args(L, "o", l_TeamColor.Get(&idx)))
		return ade_set_error(L, "s", "");

	if (!SCP_vector_inbounds(Team_Names, idx))
		return ade_set_error(L, "s", "");

	return ade_set_args(L, "s", Team_Names[idx].c_str());
}

ADE_FUNC(__eq, l_TeamColor, "teamcolor, teamcolor", "Checks if the two team colors are equal", "boolean", "true if equal, false otherwise")
{
	int idx1, idx2;
	if (!ade_get_args(L, "oo", l_TeamColor.Get(&idx1), l_TeamColor.Get(&idx2)))
		return ade_set_error(L, "b", false);

	if (!SCP_vector_inbounds(Team_Names, idx1))
		return ade_set_error(L, "b", false);

	if (!SCP_vector_inbounds(Team_Names, idx2))
		return ade_set_error(L, "b", false);

	return ade_set_args(L, "b", idx1 == idx2);
}

ADE_VIRTVAR(Name, l_TeamColor, nullptr, "The team color name", "string", "Team color name, or empty string if handle is invalid")
{
	int idx;
	if (!ade_get_args(L, "o", l_TeamColor.Get(&idx)))
		return ade_set_error(L, "s", "");

	if (!SCP_vector_inbounds(Team_Names, idx))
		return ade_set_error(L, "s", "");

	const auto& it = Team_Colors.find(Team_Names[idx]);
	if (it == Team_Colors.end()) {
		return ade_set_error(L, "s", "");
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "Setting Team Color Name is not supported");
	}

	return ade_set_args(L, "s", Team_Names[idx].c_str());
}

ADE_VIRTVAR(BaseColor, l_TeamColor, nullptr, "Team color base color", "color", "Team color base color, or nil if handle is invalid")
{
	int idx;
	if (!ade_get_args(L, "o", l_TeamColor.Get(&idx)))
		return ADE_RETURN_NIL;

	if (!SCP_vector_inbounds(Team_Names, idx))
		return ADE_RETURN_NIL;

	const auto& it = Team_Colors.find(Team_Names[idx]);
	if (it == Team_Colors.end()) {
		return ADE_RETURN_NIL;
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "Setting Team Color Base is not supported");
	}

	const auto& color_values = it->second.base;

	color cur;

	gr_init_alphacolor(&cur, static_cast<int>(color_values.r), static_cast<int>(color_values.g), static_cast<int>(color_values.b), 255);

	return ade_set_args(L, "o", l_Color.Set(cur));
}

ADE_VIRTVAR(StripeColor, l_TeamColor, nullptr, "Team color stripe color", "color", "Team color stripe color, or nil if handle is invalid")
{
	int idx;
	if (!ade_get_args(L, "o", l_TeamColor.Get(&idx)))
		return ADE_RETURN_NIL;

	if (!SCP_vector_inbounds(Team_Names, idx))
		return ADE_RETURN_NIL;

	const auto& it = Team_Colors.find(Team_Names[idx]);
	if (it == Team_Colors.end()) {
		return ADE_RETURN_NIL;
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "Setting Team Color Stripe is not supported");
	}

	const auto& color_values = it->second.stripe;

	color cur;

	gr_init_alphacolor(&cur, static_cast<int>(color_values.r), static_cast<int>(color_values.g), static_cast<int>(color_values.b), 255);

	return ade_set_args(L, "o", l_Color.Set(cur));
}

ADE_FUNC(isValid, l_TeamColor, nullptr, "Detects whether handle is valid", "boolean", "true if valid, false if handle is invalid, nil if a syntax/type error occurs")
{
	int idx;
	if (!ade_get_args(L, "o", l_TeamColor.Get(&idx)))
		return ADE_RETURN_NIL;

	if (!SCP_vector_inbounds(Team_Names, idx))
		return ADE_RETURN_FALSE;

	const auto& it = Team_Colors.find(Team_Names[idx]);
	if (it == Team_Colors.end()) {
		return ADE_RETURN_FALSE;
	}

	return ADE_RETURN_TRUE;
}

}