2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * pr.c: Use to do output to a player
30 * Known contributors to this file:
31 * Dave Pare, 1986, 1989
32 * Steve McClure, 1998-2000
35 * The pr routine historically arranged for nonbuffered i/o
36 * because stdio didn't used to automatically flush stdout before
37 * it read something from stdin. Now pr() prepends an "output id"
38 * in front of each line of text, informing the user interface
39 * what sort of item it is seeing; prompt, noecho prompt,
40 * more input data, etc.
56 #include "prototypes.h"
58 static void outid(struct player *pl, int n);
64 struct natstr *np = getnatp(player->cnum);
69 (void)vsprintf(buf, format, ap);
71 if (np->nat_flags & NF_UTF8)
72 upr_player(player, C_DATA, buf);
74 pr_player(player, C_DATA, buf);
78 uprnf(char *buf /* buf is message text */)
80 struct natstr *np = getnatp(player->cnum);
83 * Translate to ASCII if the client is not in UTF mode
85 if (!(np->nat_flags & NF_UTF8))
88 pr_player(player, C_DATA, buf);
93 pr_id(struct player *p, int id, s_char *format, ...)
99 io_puts(p->iop, "\n");
102 va_start(ap, format);
103 (void)vsprintf(buf, format, ap);
105 pr_player(p, id, buf);
109 pr_flash(struct player *pl, char *format
110 /* format is message text */, ...)
112 struct natstr *np = getnatp(pl->cnum);
113 char buf[4096]; /* buf is message text */
116 if (pl->state != PS_PLAYING)
118 va_start(ap, format);
119 (void)vsprintf(buf, format, ap);
122 * Translate to ASCII if the client is not in UTF mode
124 if (!(np->nat_flags & NF_UTF8))
126 pr_player(pl, C_FLASH, buf);
127 io_output(pl->iop, IO_NOWAIT);
131 pr_inform(struct player *pl, s_char *format, ...)
136 if (pl->state != PS_PLAYING)
138 va_start(ap, format);
139 (void)vsprintf(buf, format, ap);
141 pr_player(pl, C_INFORM, buf);
142 io_output(pl->iop, IO_NOWAIT);
146 pr_wall(s_char *format, ...)
152 va_start(ap, format);
153 (void)vsprintf(buf, format, ap);
155 for (p = player_next(0); p; p = player_next(p)) {
156 if (p->state != PS_PLAYING)
158 pr_player(p, C_FLASH, buf);
159 io_output(p->iop, IO_NOWAIT);
164 pr_player(struct player *pl, int id, s_char *buf)
171 while (*bp != '\0') {
172 if (pl->curid != -1 && pl->curid != id) {
173 io_puts(pl->iop, "\n");
178 p = strchr(bp, '\n');
181 if (pl->command && (pl->command->c_flags & C_MOD))
182 io_write(pl->iop, bp, len, IO_NOWAIT);
184 io_write(pl->iop, bp, len, IO_WAIT);
188 len = io_puts(pl->iop, bp);
195 upr_player(struct player *pl, int id, char *buf
196 /* buf is message text */)
198 register char *bp; /* bp is message text */
199 register int standout = 0;
200 char printbuf[2]; /* bp is message text */
206 while (*bp != '\0') {
207 if (pl->curid != -1 && pl->curid != id) {
208 io_puts(pl->iop, "\n");
214 if (*bp < 0) { /* looking for standout bit 0x80 */
217 io_puts(pl->iop, printbuf);
224 io_puts(pl->iop, printbuf);
229 if (pl->command && (pl->command->c_flags & C_MOD))
230 io_write(pl->iop, bp, 1, IO_NOWAIT);
232 io_write(pl->iop, bp, 1, IO_WAIT);
236 io_puts(pl->iop, printbuf);
243 * highlighted characters have hex 80 or'ed in
244 * with them to designate their highlightedness
247 pr_hilite(s_char *buf)
253 p = (s_char *)malloc(strlen(buf) + 1);
255 for (bp = p; 0 != (c = *bp); bp++)
263 * output hex code + space
266 outid(struct player *pl, int n)
272 logerror("outid: %d not valid code\n", n);
282 io_puts(pl->iop, buf);
287 prredir(s_char *redir)
289 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
295 pr_id(player, C_EXECUTE, "%s\n", file);
299 prprompt(int min, int btu)
301 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
307 pr_id(player, C_INIT, "%d\n", vers);
311 prmptrd(char *prompt, char *str, int size)
316 pr_id(player, C_FLUSH, "%s\n", prompt);
317 if ((r = recvclient(str, size)) < 0)
319 time(&player->curup);
322 for(cp = str; 0 != *cp; ++cp) {
323 if ((*cp >= 0x0 && *cp < 0x20 && *cp != '\t') ||
324 *cp == 0x7f || *cp & 0x80)
331 uprmptrd(char *prompt, char *str /* str is message text */, int size)
334 char *cp; /* cp is message text */
335 struct natstr *np = getnatp(player->cnum);
337 pr_id(player, C_FLUSH, "%s\n", prompt);
338 if ((r = recvclient(str, size)) < 0)
340 time(&player->curup);
344 for(cp = (unsigned char *)str; 0 != *cp; ++cp) {
345 if ((*cp >= 0x0 && *cp < 0x20 && *cp != '\t') ||
348 else if (!(np->nat_flags & NF_UTF8) && (*cp & 0x80))
364 * print x,y formatting as country
367 prxy(s_char *format, coord x, coord y, natid country)
372 np = getnatp(country);
373 sprintf(buf, format, xrel(np, x), yrel(np, y));
379 PR(int cn, s_char *format, ...)
381 /* XXX should really do this on a per-nation basis */
382 static s_char longline[MAXNOC][512];
387 va_start(ap, format);
388 (void)vsprintf(buf, format, ap);
390 newline = strrchr(buf, '\n') ? 1 : 0;
391 strcat(longline[cn], buf);
393 if (update_pending || (cn && cn != player->cnum))
394 typed_wu(0, cn, longline[cn], TEL_BULLETIN);
396 pr_player(player, C_DATA, longline[cn]);
397 longline[cn][0] = '\0';
413 struct natstr *np = getnatp(player->cnum);
415 if (np->nat_flags & NF_BEEP)
420 mpr(int cn, s_char *format, ...)
425 va_start(ap, format);
426 (void)vsprintf(buf, format, ap);
429 if (update_pending || cn != player->cnum)
430 typed_wu(0, cn, buf, TEL_BULLETIN);
432 pr_player(player, C_DATA, buf);
437 prtoascii(char *buf /* buf is message text */)
439 char *pbuf; /* pbuf is message text */
441 for(pbuf = buf; *pbuf != 0; pbuf++)
442 if ((*pbuf & 0xc0) == 0xc0)
444 else if (*pbuf & 0x80) {
445 memmove(pbuf,pbuf+1,strlen(pbuf)-1);