summaryrefslogtreecommitdiff
path: root/src/uqm/commglue.c
blob: a7b514ccbfdbd99d7010a0904d718eaef57ec40c (plain)
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
412
413
414
415
416
417
418
419
420
421
//Copyright Paul Reiche, Fred Ford. 1992-2002

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "commglue.h"

#include "battle.h"
		// For instantVictory
#include "races.h"

#include <stdarg.h>
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
#include "libs/log.h"

static int NPCNumberPhrase (int number, const char *fmt, UNICODE **ptrack);

// The CallbackFunction is queued and executes synchronously
// on the Starcon2Main thread
void
NPCPhrase_cb (int index, CallbackFunction cb)
{
	UNICODE *pStr, buf[400];
	void *pClip, *pTimeStamp;

	switch (index)
	{
		case GLOBAL_PLAYER_NAME:
			pStr = GLOBAL_SIS (CommanderName);
			pClip = 0;
			pTimeStamp = 0;
			break;
		case GLOBAL_SHIP_NAME:
			pStr = GLOBAL_SIS (ShipName);
			pClip = 0;
			pTimeStamp = 0;
			break;
		case 0:
		{
			return;
		}
		default:
			if (index < 0)
			{	// One of the alliance name variants
				COUNT i;
				STRING S;

				index -= GLOBAL_ALLIANCE_NAME;

				i = GET_GAME_STATE (NEW_ALLIANCE_NAME);
				S = SetAbsStringTableIndex (CommData.ConversationPhrases, (index - 1) + i);
				strcpy (buf, (UNICODE *)GetStringAddress (S));
				if (i == 3)
					strcat (buf, GLOBAL_SIS (CommanderName));

				pStr = buf;
				pClip = 0;
				pTimeStamp = 0;
			}
			else
			{
				pStr = (UNICODE *)GetStringAddress (
						SetAbsStringTableIndex (CommData.ConversationPhrases, index - 1)
						);
				pClip = GetStringSoundClip (
						SetAbsStringTableIndex (CommData.ConversationPhrases, index - 1)
						);
				pTimeStamp = GetStringTimeStamp (
						SetAbsStringTableIndex (CommData.ConversationPhrases, index - 1)
						);
			}
			break;
	}

	SpliceTrack (pClip, pStr, pTimeStamp, cb);
}

// Special case variant: prevents page breaks.
void
NPCPhrase_splice (int index)
{
	UNICODE *pStr;
	void *pClip;

	assert (index >= 0);
	if (index == 0)
		return;

	pStr = (UNICODE *)GetStringAddress (
			SetAbsStringTableIndex (CommData.ConversationPhrases, index - 1));
	pClip = GetStringSoundClip (
			SetAbsStringTableIndex (CommData.ConversationPhrases, index - 1));

	if (!pClip)
	{	// Just appending some text
		SpliceTrack (NULL, pStr, NULL, NULL);
	}
	else
	{	// Splicing in some voice
		UNICODE *tracks[] = {NULL, NULL};

		tracks[0] = pClip;
		SpliceMultiTrack (tracks, pStr);
	}
}

void
NPCNumber (int number, const char *fmt)
{
	UNICODE buf[32];

	if (!fmt)
		fmt = "%d";

	if (CommData.AlienNumberSpeech)
	{
		NPCNumberPhrase (number, fmt, NULL);
		return;
	}
	
	// just splice in the subtitle text
	snprintf (buf, sizeof buf, fmt, number);
	SpliceTrack (NULL, buf, NULL, NULL);
}

static int
NPCNumberPhrase (int number, const char *fmt, UNICODE **ptrack)
{
#define MAX_NUMBER_TRACKS 20
	NUMBER_SPEECH speech = CommData.AlienNumberSpeech;
	COUNT i;
	int queued = 0;
	int toplevel = 0;
	UNICODE *TrackNames[MAX_NUMBER_TRACKS];
	UNICODE numbuf[60];
	const SPEECH_DIGIT* dig = NULL;

	if (!speech)
		return 0;

	if (!ptrack)
	{
		toplevel = 1;
		if (!fmt)
			fmt = "%d";
		sprintf (numbuf, fmt, number);
		ptrack = TrackNames;
	}

	for (i = 0; i < speech->NumDigits; ++i)
	{
		int quot;

		dig = speech->Digits + i;
		quot = number / dig->Divider;
	
		if (quot == 0)
			continue;
		quot -= dig->Subtrahend;
		if (quot < 0)
			continue;

		if (dig->StrDigits)
		{
			COUNT index;

			assert (quot < 10);
			index = dig->StrDigits[quot];
			if (index == 0)
				continue;
			index -= 1;

			*ptrack++ = GetStringSoundClip (SetAbsStringTableIndex (
					CommData.ConversationPhrases, index
					));
			queued++;
		}
		else
		{
			int ctracks = NPCNumberPhrase (quot, NULL, ptrack);
			ptrack += ctracks;
			queued += ctracks;
		}

		if (dig->Names != 0)
		{
			SPEECH_DIGITNAME* name;

			for (name = dig->Names; name->Divider; ++name)
			{
				if (number % name->Divider <= name->MaxRemainder)
				{
					*ptrack++ = GetStringSoundClip (
							SetAbsStringTableIndex (
							CommData.ConversationPhrases, name->StrIndex - 1));
					queued++;
					break;
				}
			}
		}
		else if (dig->CommonNameIndex != 0)
		{
			*ptrack++ = GetStringSoundClip (SetAbsStringTableIndex (
					CommData.ConversationPhrases, dig->CommonNameIndex - 1));
			queued++;
		}

		number %= dig->Divider;
	}

	if (toplevel)
	{
		if (queued == 0)
		{	// nothing queued, say "zero"
			assert (number == 0);
			*ptrack++ = GetStringSoundClip (SetAbsStringTableIndex (
					CommData.ConversationPhrases, dig->StrDigits[number] - 1));
		}
		*ptrack++ = NULL; // term
		
		SpliceMultiTrack (TrackNames, numbuf);
	}
	
	return queued;
}

void
GetAllianceName (UNICODE *buf, RESPONSE_REF name_1)
{
	COUNT i;
	STRING S;

	i = GET_GAME_STATE (NEW_ALLIANCE_NAME);
	S = SetAbsStringTableIndex (CommData.ConversationPhrases, (name_1 - 1) + i);
	// XXX: this should someday be changed so that the function takes
	//   the buffer size as an argument
	strcpy (buf, (UNICODE *)GetStringAddress (S));
	if (i == 3)
	{
		strcat (buf, GLOBAL_SIS (CommanderName));
		strcat (buf, (UNICODE *)GetStringAddress (SetRelStringTableIndex (S, 1)));
	}
}

void
construct_response (UNICODE *buf, int R /* promoted from RESPONSE_REF */, ...)
{
	UNICODE *buf_start = buf;
	UNICODE *name;
	va_list vlist;
	
	va_start (vlist, R);
	
	do
	{
		COUNT len;
		STRING S;
		
		S = SetAbsStringTableIndex (CommData.ConversationPhrases, R - 1);
		
		strcpy (buf, (UNICODE *)GetStringAddress (S));
		
		len = strlen (buf);
		
		buf += len;
		
		name = va_arg (vlist, UNICODE *);
		
		if (name)
		{
			len = strlen (name);
			strcpy (buf, name);
			buf += len;
			
			/*
			if ((R = va_arg (vlist, RESPONSE_REF)) == (RESPONSE_REF)-1)
				name = 0;
			*/
					
			R = va_arg(vlist, int);
			if (R == ((RESPONSE_REF) -1))
				name = 0;
		}
	} while (name);
	va_end (vlist);
	
	*buf = '\0';

	// XXX: this should someday be changed so that the function takes
	//   the buffer size as an argument
	if ((buf_start == shared_phrase_buf) &&
			(buf > shared_phrase_buf + sizeof (shared_phrase_buf)))
	{
		log_add (log_Fatal, "Error: shared_phrase_buf size exceeded,"
				" please increase!\n");
		exit (EXIT_FAILURE);
	}
}

void
setSegue (Segue segue)
{
	switch (segue)
	{
		case Segue_peace:
			SET_GAME_STATE (BATTLE_SEGUE, 0);
			break;
		case Segue_hostile:
			SET_GAME_STATE (BATTLE_SEGUE, 1);
			break;
		case Segue_victory:
			instantVictory = TRUE;
			SET_GAME_STATE (BATTLE_SEGUE, 1);
			break;
		case Segue_defeat:
			SET_GAME_STATE (BATTLE_SEGUE, 0);
			GLOBAL_SIS(CrewEnlisted) = (COUNT)~0;
			GLOBAL(CurrentActivity) |= CHECK_RESTART;
			break;
	}
}

Segue
getSegue (void)
{
	if (GET_GAME_STATE(BATTLE_SEGUE) == 0) {
		if (GLOBAL_SIS(CrewEnlisted) == (COUNT)~0 &&
				(GLOBAL(CurrentActivity) & CHECK_RESTART)) {
			return Segue_defeat;
		} else {
			return Segue_peace;
		}
	} else /* GET_GAME_STATE(BATTLE_SEGUE) == 1) */ {
		if (instantVictory) {
			return Segue_victory;
		} else {
			return Segue_hostile;
		}
	}
}

LOCDATA*
init_race (CONVERSATION comm_id)
{
	switch (comm_id)
	{
		case ARILOU_CONVERSATION:
			return init_arilou_comm ();
		case BLACKURQ_CONVERSATION:
			return init_blackurq_comm ();
		case CHMMR_CONVERSATION:
			return init_chmmr_comm ();
		case COMMANDER_CONVERSATION:
			if (!GET_GAME_STATE (STARBASE_AVAILABLE))
				return init_commander_comm ();
			else
				return init_starbase_comm ();
		case DRUUGE_CONVERSATION:
			return init_druuge_comm ();
		case ILWRATH_CONVERSATION:
			return init_ilwrath_comm ();
		case MELNORME_CONVERSATION:
			return init_melnorme_comm ();
		case MYCON_CONVERSATION:
			return init_mycon_comm ();
		case ORZ_CONVERSATION:
			return init_orz_comm ();
		case PKUNK_CONVERSATION:
			return init_pkunk_comm ();
		case SHOFIXTI_CONVERSATION:
			return init_shofixti_comm ();
		case SLYLANDRO_CONVERSATION:
			return init_slyland_comm ();
		case SLYLANDRO_HOME_CONVERSATION:
			return init_slylandro_comm ();
		case SPATHI_CONVERSATION:
			if (!(GET_GAME_STATE (GLOBAL_FLAGS_AND_DATA) & (1 << 7)))
				return init_spathi_comm ();
			else
				return init_spahome_comm ();
		case SUPOX_CONVERSATION:
			return init_supox_comm ();
		case SYREEN_CONVERSATION:
			return init_syreen_comm ();
		case TALKING_PET_CONVERSATION:
			return init_talkpet_comm ();
		case THRADD_CONVERSATION:
			return init_thradd_comm ();
		case UMGAH_CONVERSATION:
			return init_umgah_comm ();
		case URQUAN_CONVERSATION:
			return init_urquan_comm ();
		case UTWIG_CONVERSATION:
			return init_utwig_comm ();
		case VUX_CONVERSATION:
			return init_vux_comm ();
		case YEHAT_REBEL_CONVERSATION:
			return init_rebel_yehat_comm ();
		case YEHAT_CONVERSATION:
			return init_yehat_comm ();
		case ZOQFOTPIK_CONVERSATION:
			return init_zoqfot_comm ();
		default:
			return init_chmmr_comm ();
	}
}