File: tables.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (344 lines) | stat: -rw-r--r-- 9,932 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
//

#include "tables.h"

#include "scripting/api/objs/decaldefinition.h"
#include "scripting/api/objs/fireballclass.h"
#include "scripting/api/objs/intelentry.h"
#include "scripting/api/objs/shipclass.h"
#include "scripting/api/objs/shiptype.h"
#include "scripting/api/objs/weaponclass.h"
#include "scripting/api/objs/wingformation.h"

#include "decals/decals.h"
#include "fireball/fireballs.h"
#include "menuui/techmenu.h"
#include "mission/missionmessage.h"
#include "ship/ship.h"
#include "weapon/weapon.h"


extern bool Ships_inited;
extern bool Weapons_inited;
extern bool Intel_inited;


namespace scripting {
namespace api {

//**********LIBRARY: Tables
ADE_LIB(l_Tables, "Tables", "tb", "Tables library");

//*****SUBLIBRARY: Tables/ShipClasses
ADE_LIB_DERIV(l_Tables_ShipClasses, "ShipClasses", NULL, NULL, l_Tables);
ADE_INDEXER(l_Tables_ShipClasses, "number/string IndexOrName", "Array of ship classes", "shipclass", "Ship class handle, or invalid handle if index is invalid")
{
	if(!Ships_inited)
		return ade_set_error(L, "o", l_Shipclass.Set(-1));

	const char* name;
	if(!ade_get_args(L, "*s", &name))
		return ade_set_error(L, "o", l_Shipclass.Set(-1));

	int idx = ship_info_lookup(name);

	if(idx < 0) {
		try {
			idx = std::stoi(name);
			idx--; // Lua->FS2
		} catch (const std::exception&) {
			// Not a number
			return ade_set_error(L, "o", l_Shipclass.Set(-1));
		}

		if (idx < 0 || idx >= ship_info_size()) {
			return ade_set_error(L, "o", l_Shipclass.Set(-1));
		}
	}

	return ade_set_args(L, "o", l_Shipclass.Set(idx));
}

ADE_FUNC(__len, l_Tables_ShipClasses, NULL, "Number of ship classes", "number", "Number of ship classes, or 0 if ship classes haven't been loaded yet")
{
	if(!Ships_inited)
		return ade_set_args(L, "i", 0);	//No ships loaded...should be 0

	return ade_set_args(L, "i", Ship_info.size());
}

//*****SUBLIBRARY: Tables/ShipTypes
ADE_LIB_DERIV(l_Tables_ShipTypes, "ShipTypes", nullptr, nullptr, l_Tables);
ADE_INDEXER(l_Tables_ShipTypes, "number/string IndexOrName", "Array of ship types", "shiptype", "Ship type handle, or invalid handle if index is invalid")
{
	if (!Ships_inited)
		return ade_set_error(L, "o", l_Shiptype.Set(-1));

	const char* name;
	if (!ade_get_args(L, "*s", &name))
		return ade_set_error(L, "o", l_Shiptype.Set(-1));

	int idx = ship_type_name_lookup(name);

	if (idx < 0) {
		try {
			idx = std::stoi(name);
			idx--; // Lua->FS2
		}
		catch (const std::exception&) {
			// Not a number
			return ade_set_error(L, "o", l_Shiptype.Set(-1));
		}

		if (idx < 0 || idx >= (int)Ship_types.size()) {
			return ade_set_error(L, "o", l_Shiptype.Set(-1));
		}
	}

	return ade_set_args(L, "o", l_Shiptype.Set(idx));
}

ADE_FUNC(__len, l_Tables_ShipTypes, nullptr, "Number of ship types", "number", "Number of ship types, or 0 if ship types haven't been loaded yet")
{
	if (!Ships_inited)
		return ade_set_args(L, "i", 0);	//No ship types loaded...should be 0

	return ade_set_args(L, "i", Ship_types.size());
}

//*****SUBLIBRARY: Tables/WeaponClasses
ADE_LIB_DERIV(l_Tables_WeaponClasses, "WeaponClasses", NULL, NULL, l_Tables);

ADE_INDEXER(l_Tables_WeaponClasses, "number/string IndexOrWeaponName", "Array of weapon classes", "weaponclass", "Weapon class handle, or invalid handle if index is invalid")
{
	if(!Weapons_inited)
		return ade_set_error(L, "o", l_Weaponclass.Set(-1));

	const char* name;
	if(!ade_get_args(L, "*s", &name))
		return 0;

	int idx = weapon_info_lookup(name);

	if(idx < 0) {
		idx = atoi(name);

		// atoi is good enough here, 0 is invalid anyway
		if (idx > 0)
		{
			idx--; // Lua --> C/C++
		}
		else
		{
			return ade_set_args(L, "o", l_Weaponclass.Set(-1));
		}
	}

	return ade_set_args(L, "o", l_Weaponclass.Set(idx));
}

ADE_FUNC(__len, l_Tables_WeaponClasses, NULL, "Number of weapon classes", "number", "Number of weapon classes, or 0 if weapon classes haven't been loaded yet")
{
	if(!Weapons_inited)
		return ade_set_args(L, "i", 0);

	return ade_set_args(L, "i", weapon_info_size());
}

//*****SUBLIBRARY: Tables/IntelEntries
ADE_LIB_DERIV(l_Tables_IntelEntries, "IntelEntries", nullptr, nullptr, l_Tables);
ADE_INDEXER(l_Tables_IntelEntries, "number/string IndexOrName", "Array of intel entries", "intel_entry", "Intel entry handle, or invalid handle if index is invalid")
{
	if(!Intel_inited)
		return ade_set_error(L, "o", l_Intelentry.Set(-1));

	const char* name;
	if(!ade_get_args(L, "*s", &name))
		return ade_set_error(L, "o", l_Intelentry.Set(-1));

	int idx = intel_info_lookup(name);

	if(idx < 0) {
		try {
			idx = std::stoi(name);
			idx--; // Lua->FS2
		} catch (const std::exception&) {
			// Not a number
			return ade_set_error(L, "o", l_Intelentry.Set(-1));
		}

		if (idx < 0 || idx >= intel_info_size()) {
			return ade_set_error(L, "o", l_Intelentry.Set(-1));
		}
	}

	return ade_set_args(L, "o", l_Intelentry.Set(idx));
}

ADE_FUNC(__len, l_Tables_IntelEntries, nullptr, "Number of intel entries", "number", "Number of intel entries, or 0 if intel entries haven't been loaded yet")
{
	if(!Intel_inited)
		return ade_set_args(L, "i", 0);	//No intel loaded...should be 0

	return ade_set_args(L, "i", intel_info_size());
}

//*****SUBLIBRARY: Tables/FireballClasses
ADE_LIB_DERIV(l_Tables_FireballClasses, "FireballClasses", NULL, NULL, l_Tables);

ADE_INDEXER(l_Tables_FireballClasses, "number/string IndexOrFireballUniqueID", "Array of fireball classes", "fireballclass", "Fireball class handle, or invalid handle if index is invalid")
{
	if (!fireballs_inited)
		return ade_set_error(L, "o", l_Fireballclass.Set(-1));

	const char* name;
	if (!ade_get_args(L, "*s", &name))
		return 0;

	int idx = fireball_info_lookup(name);

	if (idx < 0) {
		idx = atoi(name);

		// atoi is good enough here, 0 is invalid anyway
		if (idx > 0)
		{
			idx--; // Lua --> C/C++
		}
		else
		{
			return ade_set_args(L, "o", l_Fireballclass.Set(-1));
		}
	}

	return ade_set_args(L, "o", l_Fireballclass.Set(idx));
}

ADE_FUNC(__len, l_Tables_FireballClasses, NULL, "Number of fireball classes", "number", "Number of fireball classes, or 0 if fireball classes haven't been loaded yet")
{
	if (!fireballs_inited)
		return ade_set_args(L, "i", 0);

	return ade_set_args(L, "i", Num_fireball_types);
}

//*****SUBLIBRARY: Tables/SimulatedSpeechOverrides
ADE_LIB_DERIV(l_Tables_SimulatedSpeechOverrides, "SimulatedSpeechOverrides", NULL, NULL, l_Tables);

ADE_INDEXER(l_Tables_SimulatedSpeechOverrides, "number Index", nullptr, "string", "Truncated filenames of simulated speech overrides or empty string if index is out of range.")
{
	int idx = -1;
	if (!ade_get_args(L, "*i", &idx)) {
		return ade_set_error(L, "s", "");
	}
	if (idx > 0 && (size_t) idx <= Generic_message_filenames.size()) {
		idx--; //Convert from Lua to C, as lua indices start from 1, not 0
		return ade_set_args(L, "s", Generic_message_filenames[idx]);
	}

	return ade_set_error(L, "s", "");
}

ADE_FUNC(__len, l_Tables_SimulatedSpeechOverrides, nullptr, "Number of simulated speech overrides", "number", "Number of simulated speech overrides")
{
	return ade_set_args(L, "i", Generic_message_filenames.size());
}

//*****SUBLIBRARY: Tables/DecalDefinitions
ADE_LIB_DERIV(l_Tables_DecalDefinitions, "DecalDefinitions", nullptr, nullptr, l_Tables);

ADE_INDEXER(l_Tables_DecalDefinitions, "number/string IndexOrDecalName", "Array of decal definitions", "decaldefinition", "Decal definition handle, or invalid handle if name is invalid")
{
	const char* name;
	if (!ade_get_args(L, "*s", &name))
		return 0;

	int idx = decals::findDecalDefinition(name);

	if (idx < 0)
	{
		idx = atoi(name);

		// atoi is good enough here, 0 is invalid anyway
		if (idx > 0)
		{
			idx--; // Lua --> C/C++
		}
		else
		{
			return ade_set_args(L, "o", l_DecalDefinitionclass.Set(-1));
		}
	}

	return ade_set_args(L, "o", l_DecalDefinitionclass.Set(idx));
}

ADE_FUNC(__len, l_Tables_DecalDefinitions, nullptr, "Number of decal definitions", "number", "Number of decal definitions, or 0 if decal definitions haven't been loaded yet")
{
	return ade_set_args(L, "i", static_cast<int>(decals::DecalDefinitions.size()));
}

ADE_FUNC(isDecalSystemActive, l_Tables, nullptr, "Returns whether the decal system is able to work on the current machine", "boolean", "true if active, false if inactive")
{
	return ade_set_args(L, "b", decals::Decal_system_active);
}

ADE_VIRTVAR(DecalOptionActive, l_Tables, "boolean", "Gets or sets whether the decal option is active (note, decals will only work if the decal system is able to work on the current machine)", "boolean", "true if active, false if inactive")
{
	bool choice = decals::Decal_option_active;

	if (ADE_SETTING_VAR && ade_get_args(L, "*b", &choice)) {
		decals::Decal_option_active = choice;
	}

	return ade_set_args(L, "b", choice);
}

//*****SUBLIBRARY: Tables/WingFormations
ADE_LIB_DERIV(l_Tables_WingFormations, "WingFormations", nullptr, nullptr, l_Tables);

ADE_INDEXER(l_Tables_WingFormations, "number/string IndexOrName", "Array of wing formations", "wingformation", "Wing formation handle, or invalid handle if name is invalid")
{
	const char* name;
	if (!ade_get_args(L, "*s", &name))
		return 0;

	// default is always the first formation
	if (!stricmp(name, "Default"))
		return ade_set_args(L, "o", l_WingFormation.Set(0));

	// look up by name
	int idx = wing_formation_lookup(name);

	// if found, add 1 since "Default" is always first
	if (idx >= 0)
	{
		idx++;
	}
	// look up by number
	else
	{
		// atoi is good enough here, 0 is invalid anyway
		idx = atoi(name);
		if (idx > 0)
		{
			idx--; // Lua --> C/C++
		}
		else
		{
			return ade_set_args(L, "o", l_WingFormation.Set(-1));
		}
	}

	return ade_set_args(L, "o", l_WingFormation.Set(idx));
}

ADE_FUNC(__len, l_Tables_WingFormations, nullptr, "Number of wing formations", "number", "Number of wing formations")
{
	// add 1 since "Default" is always first
	return ade_set_args(L, "i", static_cast<int>(Wing_formations.size()) + 1);
}

}
}