aboutsummaryrefslogtreecommitdiff
path: root/src/parsec/con_say.cpp
blob: cc1289582a55cb0ca32fe9cfe4eba7128d2ff49f (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
/*
 * PARSEC - Say/Talk Commands
 *
 * $Author: uberlinuxguy $ - $Date: 2004/09/15 12:25:23 $
 *
 * Orginally written by:
 *   Copyright (c) Markus Hadwiger     <msh@parsec.org>   1996-1998
 *
 *  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
 */ 

// C library
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// compilation flags/debug support
#include "config.h"
#include "debug.h"

// general definitions
#include "general.h"
#include "objstruc.h"

// global externals
#include "globals.h"

// subsystem headers
#include "net_defs.h"

// local module header
#include "con_say.h"

// proprietary module headers
#include "con_arg.h"
#include "con_aux.h"
#include "con_main.h"


// textmacros (up to ten textmacros can be assigned and sent) -----------------
//
char text_macros[][ MAX_TEXTMACRO_LEN + 1 ] = {

	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	""
};

// generic string paste area
#define PASTE_STR_LEN 255
static char paste_str[ PASTE_STR_LEN + 1 ];

// string constants
static char text_not_sent[] 	= "text could not be sent.";
static char say_hint[]			= "use say? where ? is [0..9].";
static char set_hint[]			= "use set? where ? is [0..9].";
static char undef_macro[]		= "macro is undefined.";
static char you_said[]			= "you said: ";


// exec say macro (send preset string to all remote players) ------------------
//
PRIVATE
int ExecSayMacro( char *cstr )
{
	ASSERT( cstr != NULL );
	ASSERT( isdigit( *cstr ) );

	// display syntax if chars behind "say{digit}"
	if ( strtok( cstr + 1, " " ) != NULL ) {
		CON_AddLine( say_hint );
		return FALSE;
	}

	// retrieve macro text
	cstr = text_macros[ *cstr - '0' ];
	if ( *cstr == 0 ) {
		CON_AddLine( undef_macro );
		return FALSE;
	}

	// try to send text
	if ( !NetConnected || !NET_RmEvSendText( cstr ) ) {
		CON_AddLine( text_not_sent );
		return !NetConnected;
	}

	if ( con_in_talk_mode ) {

		// show text in talk area
		ShowSentText( LocalPlayerName, cstr, TALK_PUBLIC );
		CON_DisableLineFeed();

	} else {

		// show text normally
		ASSERT( ( strlen( you_said ) + strlen( cstr ) ) <= PASTE_STR_LEN );
		strcpy( paste_str, you_said );
		strcat( paste_str, cstr );
		CON_AddLine( paste_str );
	}

	return TRUE;
}


// exec say command (send arbitrary string to all remote players) -------------
//
int Cmd_SayText( char *cstr )
{
	//NOTE:
	// if the text is defined by a macro (SAY0, SAY1, ...)
	// it will be displayed in the console of the local
	// player so he will know what he said.
	// ("you said: ...")
	//
	// if the text is typed in directly, then it will
	// not be displayed since it is part of the command
	// (which is still visible) anyway.
	//
	// in talk mode everything will be displayed in the
	// talk area, however.

	ASSERT( cstr != NULL );

	// if digit immediately after command try macro
	if ( isdigit( *cstr ) ) {
		ExecSayMacro( cstr );
		return TRUE;
	}

	//NOTE:
	// in talkmode say without an argument is
	// allowed and an empty line will be sent.

	const char *scan = GetStringBehindCommand( cstr, con_in_talk_mode );

	// command invalid or argument missing?
	if ( scan == NULL )
		return TRUE;

	// try to send text
	if ( !NetConnected || !NET_RmEvSendText( scan ) ) {
		CON_AddLine( text_not_sent );
		return TRUE;
	}

	// show text in talk area
	if ( con_in_talk_mode ) {
		ShowSentText( LocalPlayerName, scan, TALK_PUBLIC );
		CON_DisableLineFeed();
	}

	return TRUE;
}


// exec set macro (set text macro) --------------------------------------------
//
int Cmd_SetTextMacro( char *cstr )
{
	ASSERT( cstr != NULL );

	// recognize only if digit behind "set"
	if ( !isdigit( *cstr ) )
		return FALSE;

	int indx   = *cstr - '0';
	const char *scan = GetStringBehindCommand( cstr + 1, FALSE );

	if ( scan == NULL )
		return TRUE;

	strncpy( text_macros[ indx ], scan, MAX_TEXTMACRO_LEN );
	text_macros[ indx ][ MAX_TEXTMACRO_LEN ] = 0;

	return TRUE;
}


// show text sent by remote player in console ---------------------------------
//
void ShowSentText( const char *name, const char *text, int mode )
{
	ASSERT( name != NULL );
	ASSERT( text != NULL );
	ASSERT( ( mode >= 0 ) && ( mode < NUM_TALK_MODES ) );

	//NOTE:
	// in talkmode the output of say strings cannot
	// be disabled.

	//NOTE:
	// if mode == TALK_STATUS, the name parameter of ShowSentText()
	// is ignored, but it should not be NULL, because of the ASSERT.

	if ( !AUX_DISABLE_CONSOLE_SAY_OUTPUT || con_in_talk_mode ) {

		// save current cursor pos and input line
		int curs_pos = cursor_x;
		strcpy( paste_str, con_lines[ con_bottom ] );

		if ( con_in_talk_mode ) {

			// print sent text in line where separator currently is
			int pline = ( con_talk_line - 1 ) & NUM_CONSOLE_LINES_MASK;

			const char *br_l = "";
			const char *br_r = br_l;

			// determine caller brackets
			if ( mode == TALK_PUBLIC ) {
				br_l = "<";
				br_r = "> ";
			} else if ( mode == TALK_PRIVATE ) {
				br_l = "*";
				br_r = "* ";
			} else {
				name = br_l;
			}

			// print name of talker and sent text
			strcpy( con_lines[ pline ], br_l );
			strcat( con_lines[ pline ], name );
			strcat( con_lines[ pline ], br_r );
			strcat( con_lines[ pline ], text );

			// scroll down lines to preserve
			if ( con_talk_line < con_bottom ) {

				int numl = con_bottom - con_talk_line;
				memmove( con_lines[ con_talk_line + 1 ],
						 con_lines[ con_talk_line ],
						 numl * ( MAX_CONSOLE_LINE_LENGTH + 1 ) );

			} else if ( con_talk_line > con_bottom ) {

				// with wraparound handling

				int numl = NUM_CONSOLE_LINES - con_talk_line - 1;
				memmove( con_lines[ 1 ], con_lines[ 0 ],
						 con_bottom * ( MAX_CONSOLE_LINE_LENGTH + 1 ) );
				memcpy( con_lines[ 0 ], con_lines[ NUM_CONSOLE_LINES - 1 ],
						MAX_CONSOLE_LINE_LENGTH + 1 );
				if ( numl > 0 )
					memmove( con_lines[ con_talk_line + 1 ],
							 con_lines[ con_talk_line ],
							 numl * ( MAX_CONSOLE_LINE_LENGTH + 1 ) );
			}

			// new separator
			CreateSeparatorLine( con_talk_line );
			CON_EnableLineFeed();

			// this actually increases here
			con_talk_line = ( con_talk_line + 1 ) & NUM_CONSOLE_LINES_MASK;

		} else {

			// output two lines (name and text)
			EraseConLine( con_bottom );
			strcpy( con_lines[ con_bottom ], name );
			strcat( con_lines[ con_bottom ], " says:" );
			CON_AddLine( text );
		}

		// restore cursor pos and input line
		CON_AddLine( paste_str );
		cursor_x = curs_pos;
	}
}