File: species_defs.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (411 lines) | stat: -rw-r--r-- 12,724 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Species_Defs.CPP
 * Extended Species Support for FS2 Open
 *
 * You may not sell or otherwise commercially exploit the source or things you
 * create based on the source.
 *
 */


#include "cfile/cfile.h"
#include "def_files/def_files.h"
#include "iff_defs/iff_defs.h"
#include "localization/localize.h"
#include "parse/parselo.h"
#include "ship/shipfx.h"
#include "species_defs/species_defs.h"

extern int parse_warp_params(const WarpParams *inherit_from, WarpDirection direction, const char *info_type_name, const char *info_name, bool set_supercap_warp_physics = false);

SCP_vector<species_info> Species_info;

//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

// This function parses the data from the species_defs.tbl
// Names only - actual loading is done elsewhere

void parse_thrust_anims(species_info *species, bool no_create)
{
	if (!no_create)
	{
		required_string("$ThrustAnims:");

		generic_anim_init(&species->thruster_info.flames.normal, NULL);
		generic_anim_init(&species->thruster_info.flames.afterburn, NULL);
		generic_bitmap_init(&species->thruster_secondary_glow_info.normal, NULL);
		generic_bitmap_init(&species->thruster_secondary_glow_info.afterburn, NULL);
		generic_bitmap_init(&species->thruster_tertiary_glow_info.normal, NULL);
		generic_bitmap_init(&species->thruster_tertiary_glow_info.afterburn, NULL);
		generic_bitmap_init(&species->thruster_distortion_info.normal, NULL);
		generic_bitmap_init(&species->thruster_distortion_info.afterburn, NULL);
	}
	else if (!optional_string("$ThrustAnims:"))
	{
		return;
	}

	// favor new style
	if (no_create)
	{
		if (optional_string("+Pri_Normal:") || optional_string("+Normal:"))
			stuff_string(species->thruster_info.flames.normal.filename, F_NAME, MAX_FILENAME_LEN);
	}
	else
	{
		if (!optional_string("+Pri_Normal:"))
			required_string("+Normal:");

		stuff_string(species->thruster_info.flames.normal.filename, F_NAME, MAX_FILENAME_LEN);
	}

	// if no primary thruster anim is wanted then clear it
	if ( !VALID_FNAME(species->thruster_info.flames.normal.filename) )
		generic_anim_init(&species->thruster_info.flames.normal, NULL);

	// and again
	if (no_create)
	{
		if (optional_string("+Pri_Afterburn:") || optional_string("+Afterburn:"))
			stuff_string(species->thruster_info.flames.afterburn.filename, F_NAME, MAX_FILENAME_LEN);
	}
	else
	{
		if (!optional_string("+Pri_Afterburn:"))
			required_string("+Afterburn:");

		stuff_string(species->thruster_info.flames.afterburn.filename, F_NAME, MAX_FILENAME_LEN);
	}

	// if no primary thruster anim is wanted then clear it
	if ( !VALID_FNAME(species->thruster_info.flames.afterburn.filename) )
		generic_anim_init(&species->thruster_info.flames.afterburn, NULL);

	// extra thruster stuff, bah
	if (optional_string("+Sec_Normal:"))
		stuff_string(species->thruster_secondary_glow_info.normal.filename, F_NAME, MAX_FILENAME_LEN);

	// etc.
	if (optional_string("+Sec_Afterburn:"))
		stuff_string(species->thruster_secondary_glow_info.afterburn.filename, F_NAME, MAX_FILENAME_LEN);

	// etc.
	if (optional_string("+Ter_Normal:"))
		stuff_string(species->thruster_tertiary_glow_info.normal.filename, F_NAME, MAX_FILENAME_LEN);

	// etc.
	if (optional_string("+Ter_Afterburn:"))
		stuff_string(species->thruster_tertiary_glow_info.afterburn.filename, F_NAME, MAX_FILENAME_LEN);
	
	// etc.
	if (optional_string("+Dist_Normal:"))
		stuff_string(species->thruster_distortion_info.normal.filename, F_NAME, MAX_FILENAME_LEN);

	// etc.
	if (optional_string("+Dist_Afterburn:"))
		stuff_string(species->thruster_distortion_info.afterburn.filename, F_NAME, MAX_FILENAME_LEN);
}

void parse_thrust_glows(species_info *species, bool no_create)
{
	if (!no_create)
	{
		required_string("$ThrustGlows:");

		generic_anim_init(&species->thruster_info.glow.normal, NULL);
		generic_anim_init(&species->thruster_info.glow.afterburn, NULL);
	}
	else if (!optional_string("$ThrustGlows:"))
	{
		return;
	}

	if (no_create)
	{
		if (optional_string("+Normal:"))
			stuff_string(species->thruster_info.glow.normal.filename, F_NAME, MAX_FILENAME_LEN);
	}
	else
	{
		required_string("+Normal:");
		stuff_string(species->thruster_info.glow.normal.filename, F_NAME, MAX_FILENAME_LEN);
	}

	// if no glow is wanted then clear it
	if ( !VALID_FNAME(species->thruster_info.glow.normal.filename) )
		generic_anim_init(&species->thruster_info.glow.normal, NULL);

	if (no_create)
	{
		if (optional_string("+Afterburn:"))
			stuff_string(species->thruster_info.glow.afterburn.filename, F_NAME, MAX_FILENAME_LEN);
	}
	else
	{
		required_string("+Afterburn:");
		stuff_string(species->thruster_info.glow.afterburn.filename, F_NAME, MAX_FILENAME_LEN);
	}

	// if no glow is wanted then clear it
	if ( !VALID_FNAME(species->thruster_info.glow.afterburn.filename) )
		generic_anim_init(&species->thruster_info.glow.afterburn, NULL);
}

void parse_species_tbl(const char *filename)
{
	char species_name[NAME_LENGTH];

	try
	{
		if (filename == NULL)
			read_file_text_from_default(defaults_get_file("species_defs.tbl"));
		else
			read_file_text(filename, CF_TYPE_TABLES);

		reset_parse();


		// start parsing
		required_string("#SPECIES DEFS");

		// no longer required: counted automatically
		if (optional_string("$NumSpecies:"))
		{
			int temp;
			stuff_int(&temp);
		}

		// begin reading data
		while (required_string_either("#END", "$Species_Name:"))
		{
			bool no_create = false;
			species_info *species, new_species;

			species = &new_species;

			// Start Species - Get its name
			required_string("$Species_Name:");
			stuff_string(species_name, F_NAME, NAME_LENGTH);

			if (optional_string("+nocreate"))
			{
				no_create = true;

				int i = species_info_lookup(species_name);
				if (i >= 0)
					species = &Species_info[i];
			}
			else
			{
				strcpy_s(species->species_name, species_name);
			}

			// Goober5000 - IFF
			if (optional_string("$Default IFF:"))
			{
				bool iff_found = false;
				char temp_name[NAME_LENGTH];
				stuff_string(temp_name, F_NAME, NAME_LENGTH);

				// search for it in iffs
				for (int iLoop = 0; iLoop < (int)Iff_info.size(); iLoop++)
				{
					if (!stricmp(Iff_info[iLoop].iff_name, temp_name))
					{
						species->default_iff = iLoop;
						iff_found = true;
					}
				}

				if (!iff_found)
				{
					species->default_iff = 0;
					Warning(LOCATION, "Species %s default IFF %s not found in iff_defs.tbl!  Defaulting to %s.\n", species->species_name, temp_name, Iff_info[species->default_iff].iff_name);
				}
			}
			else if (!no_create)
			{
				// we have no idea which it could be, so default to 0
				species->default_iff = 0;

				// let them know
				Warning(LOCATION, "$Default IFF not specified for species %s in species_defs.tbl!  Defaulting to %s.\n", species->species_name, Iff_info[species->default_iff].iff_name);
			}

			// Goober5000 - FRED color
			if (optional_string("$FRED Color:") || optional_string("$FRED Colour:"))
			{
				stuff_int_list(species->fred_color.a1d, 3, RAW_INTEGER_TYPE);
			}
			else if (!no_create)
			{
				// set defaults to Volition's originals
				if (!stricmp(species->species_name, "Terran"))
				{
					species->fred_color.rgb.r = 0;
					species->fred_color.rgb.g = 0;
					species->fred_color.rgb.b = 192;
				}
				else if (!stricmp(species->species_name, "Vasudan"))
				{
					species->fred_color.rgb.r = 0;
					species->fred_color.rgb.g = 128;
					species->fred_color.rgb.b = 0;
				}
				else if (!stricmp(species->species_name, "Shivan"))
				{
					species->fred_color.rgb.r = 192;
					species->fred_color.rgb.g = 0;
					species->fred_color.rgb.b = 0;
				}
				else if (!stricmp(species->species_name, "Ancients") || !stricmp(species->species_name, "Ancient"))
				{
					species->fred_color.rgb.r = 192;
					species->fred_color.rgb.g = 0;
					species->fred_color.rgb.b = 192;
				}
				else
				{
					species->fred_color.rgb.r = 0;
					species->fred_color.rgb.g = 0;
					species->fred_color.rgb.b = 0;
				}

				// let them know
				Warning(LOCATION, "$FRED Color not specified for species %s in species_defs.tbl!  Defaulting to (%d, %d, %d).\n", species->species_name, species->fred_color.rgb.r, species->fred_color.rgb.g, species->fred_color.rgb.b);
			}

			// stuff
			optional_string("$MiscAnims:");

			// Get its Debris Texture
			if ((!no_create && required_string("+Debris_Texture:")) || optional_string("+Debris_Texture:"))
			{
				generic_bitmap_init(&species->debris_texture, NULL);
				stuff_string(species->debris_texture.filename, F_NAME, MAX_FILENAME_LEN);
			}


			// Shield Hit Animation
			if (optional_string("+Shield_Hit_ani:")) // Shouldn't be required -- LPine
			{
				generic_anim_init(&species->shield_anim, NULL);
				stuff_string(species->shield_anim.filename, F_NAME, MAX_FILENAME_LEN);
			}
			else if (!no_create)
			{
				species->shield_anim.filename[0] = '\0';
				species->shield_anim.first_frame = -1; // Landmine to trip up anyone who does end up using this
			}


			// Thruster Anims
			parse_thrust_anims(species, no_create);

			// Thruster Glow Anims
			parse_thrust_glows(species, no_create);


			// Goober5000 - AWACS multiplier
			if (optional_string("$AwacsMultiplier:"))
			{
				stuff_float(&species->awacs_multiplier);
			}
			else if (!no_create)
			{
				// set defaults to Volition's originals
				if (!stricmp(species->species_name, "Vasudan"))
					species->awacs_multiplier = 1.25f;
				else if (!stricmp(species->species_name, "Shivan"))
					species->awacs_multiplier = 1.50f;
				else
					species->awacs_multiplier = 1.0f;

				// let them know
				Warning(LOCATION, "$AwacsMultiplier not specified for species %s in species_defs.tbl!  Defaulting to %.2f.\n", species->species_name, species->awacs_multiplier);
			}

			// Goober5000 - countermeasure type
			// (we won't be able to resolve it until after we've parsed the weapons table)
			if (optional_string("$Countermeasure type:"))
				stuff_string(species->cmeasure_name, F_NAME, NAME_LENGTH);

			// ditto for support ships - naomimyselfandi
			if (optional_string("$Support ship:"))
				stuff_string(species->support_ship_name, F_NAME, NAME_LENGTH);

			if (optional_string("$Borrows Briefing Icons from:")) {
				char temp_name[NAME_LENGTH];
				stuff_string(temp_name, F_NAME, NAME_LENGTH);
				int idx = species_info_lookup(temp_name);
				if (idx >= 0)
					species->borrows_bii_index_species = idx;
				else {
					Warning(LOCATION, "Species %s for '$Borrows Briefing Icons from' in Species %s is either invalid or not yet parsed."
									  "The Species doing the borrowing must be defined after the Species it is borrowing from\n", temp_name, species->species_name);
				}
			}

			if (optional_string("$Borrows Flyby Sounds from:")) {
				char temp_name[NAME_LENGTH];
				stuff_string(temp_name, F_NAME, NAME_LENGTH);
				int idx = species_info_lookup(temp_name);
				if (idx >= 0) {
					species->borrows_flyby_sounds_species = idx;
				} else {
					Warning(LOCATION, "Species %s for '$Borrows Flyby Sounds from' in Species %s is either invalid or not yet parsed."
									  "The Species doing the borrowing must be defined after the Species it is borrowing from\n", temp_name, species->species_name);
				}
			}

			// get species parameters for warpin and warpout
			// Note: if the index is not -1, we must have already assigned warp parameters, probably because we are now
			// parsing a TBM.  In that case, inherit from ourselves.
			species->warpin_params_index = parse_warp_params(species->warpin_params_index >= 0 ? &Warp_params[species->warpin_params_index] : nullptr, WarpDirection::WARP_IN, "Species", species->species_name);
			species->warpout_params_index = parse_warp_params(species->warpout_params_index >= 0 ? &Warp_params[species->warpout_params_index] : nullptr, WarpDirection::WARP_OUT, "Species", species->species_name);

			// don't add new entry if this is just a modified one
			if (!no_create)
				Species_info.push_back(new_species);
		}

		required_string("#END");
	}
	catch (const parse::ParseException& e)
	{
		mprintf(("TABLES: Unable to parse '%s'!  Error message = %s.\n", (filename) ? filename : NOX("<default species_defs.tbl>"), e.what()));
		return;
	}
}

int Species_initted = 0;

void species_init()
{
	if (Species_initted)
		return;

	Species_info.clear();


	if (cf_exists_full("species_defs.tbl", CF_TYPE_TABLES))
		parse_species_tbl("species_defs.tbl");
	else
		parse_species_tbl(NULL);

	parse_modular_table("*-sdf.tbm", parse_species_tbl);


	Species_initted = 1;
}

int species_info_lookup(const char *species_name)
{
	for (int i = 0; i < static_cast<int>(Species_info.size()); i++)
	{
		if (!stricmp(Species_info[i].species_name, species_name))
			return i;
	}

	return -1;
}