File: missionhotkey.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (205 lines) | stat: -rw-r--r-- 4,338 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "missionhotkey.h"
#include "enums.h"

#include "mission/missionhotkey.h"
#include "playerman/player.h"

namespace scripting {
namespace api {

hotkey_h::hotkey_h() : line(-1) {}
hotkey_h::hotkey_h(int l_line) : line(l_line) {}

hotkey_line* hotkey_h::getLine() const
{
	if (!isValid())
		return nullptr;

	return &Hotkey_lines[line];
}

int hotkey_h::getIndex() const
{
	return line;
}

bool hotkey_h::isValid() const
{
	return line >= 0 && line < MAX_LINES;
}

//**********HANDLE: help section
ADE_OBJ(l_Hotkey, hotkey_h, "hotkey_ship", "Hotkey handle");

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

	return ade_set_args(L, "b", current.isValid());
}

ADE_VIRTVAR(Text, l_Hotkey, nullptr, "The text of this hotkey line", "string", "The text")
{
	hotkey_h current;
	if (!ade_get_args(L, "o", l_Hotkey.Get(&current))) {
		return ADE_RETURN_NIL;
	}
	if (!current.isValid()) {
		return ade_set_error(L, "s", "");
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read only.");
	}

	return ade_set_args(L, "s", current.getLine()->label);
}

ADE_VIRTVAR(Type,
	l_Hotkey,
	nullptr,
	"The type of this hotkey line: HOTKEY_LINE_NONE, HOTKEY_LINE_HEADING, HOTKEY_LINE_WING, HOTKEY_LINE_SHIP, or HOTKEY_LINE_SUBSHIP.",
	"enumeration",
	"The type")
{
	hotkey_h current;
	lua_enum eh_idx = ENUM_INVALID;

	if (!ade_get_args(L, "o", l_Hotkey.Get(&current))) {
		return ADE_RETURN_NIL;
	}
	if (!current.isValid()) {
		return ade_set_error(L, "o", l_Enum.Set(enum_h(eh_idx)));
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read only.");
	}

	switch (current.getLine()->type) {
		case HotkeyLineType::HEADING:
			eh_idx = LE_HOTKEY_LINE_HEADING;
			break;
		case HotkeyLineType::WING:
			eh_idx = LE_HOTKEY_LINE_WING;
			break;
		case HotkeyLineType::SHIP:
			eh_idx = LE_HOTKEY_LINE_SHIP;
			break;
		case HotkeyLineType::SUBSHIP:
			eh_idx = LE_HOTKEY_LINE_SUBSHIP;
			break;
		case HotkeyLineType::NONE:
		default:
			eh_idx = LE_HOTKEY_LINE_NONE;
			break;
	}

	return ade_set_args(L, "o", l_Enum.Set(enum_h(eh_idx)));
}

ADE_VIRTVAR(Keys,
	l_Hotkey,
	nullptr,
	"Gets a table of hotkeys set to the ship in the order from F5 - F12",
	"{ number => boolean ... }",
	"The hotkeys table")
{
	hotkey_h current;
	if (!ade_get_args(L, "o", l_Hotkey.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	auto table = luacpp::LuaTable::create(L);
	if (!current.isValid()) {
		return ade_set_error(L, "t", &table);
	}

	int hotkeys;
	if (current.getLine()->type == HotkeyLineType::WING)
		hotkeys = get_wing_hotkeys(current.getIndex()); // for wings
	else
		hotkeys = get_ship_hotkeys(current.getIndex()); // for everything else (there's mastercard)

	for (int i = 0; i < MAX_KEYED_TARGETS; i++) {
		bool key_active = hotkeys & (1 << i);
		table.addValue(i + 1, key_active); // translate to Lua index
	}

	return ade_set_args(L, "t", &table);
}

ADE_FUNC(addHotkey,
	l_Hotkey,
	"number Key",
	"Adds a hotkey to the to the ship in the list. 1-8 correspond to F5-F12. Returns nothing.",
	nullptr,
	nullptr)
{
	SCP_UNUSED(L);

	hotkey_h current;
	int key;
	if (!ade_get_args(L, "oi", l_Hotkey.Get(&current), &key)) {
		return ADE_RETURN_NIL;
	}
	if (!current.isValid()) {
		return ADE_RETURN_NIL;
	}
	key--;

	add_hotkey(key, current.getIndex());

	return ADE_RETURN_NIL;
}

ADE_FUNC(removeHotkey,
	l_Hotkey,
	"number Key",
	"Removes a hotkey from the ship in the list. 1-8 correspond to F5-F12. Returns nothing.",
	nullptr,
	nullptr)
{
	SCP_UNUSED(L);

	hotkey_h current;
	int key;
	if (!ade_get_args(L, "oi", l_Hotkey.Get(&current), &key)) {
		return ADE_RETURN_NIL;
	}
	if (!current.isValid()) {
		return ADE_RETURN_NIL;
	}
	key--;

	remove_hotkey(key, current.getIndex());

	return ADE_RETURN_NIL;
}

ADE_FUNC(clearHotkeys,
	l_Hotkey,
	nullptr,
	"Clears all hotkeys from the ship in the list. Returns nothing.",
	nullptr,
	nullptr)
{
	SCP_UNUSED(L);

	hotkey_h current;
	if (!ade_get_args(L, "o", l_Hotkey.Get(&current))) {
		return ADE_RETURN_NIL;
	}
	if (!current.isValid()) {
		return ADE_RETURN_NIL;
	}

	clear_hotkeys(current.getIndex());

	return ADE_RETURN_NIL;
}

} // namespace api
} // namespace scripting