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);
68 (void)vsprintf(buf, format, ap);
70 if (player->flags & PF_UTF8)
71 upr_player(player, C_DATA, buf);
73 pr_player(player, C_DATA, buf);
77 uprnf(char *buf /* buf is message text */)
80 * Translate to ASCII if the client is not in UTF mode
82 if (!(player->flags & PF_UTF8))
85 pr_player(player, C_DATA, buf);
90 pr_id(struct player *p, int id, s_char *format, ...)
96 io_puts(p->iop, "\n");
100 (void)vsprintf(buf, format, ap);
102 pr_player(p, id, buf);
106 pr_flash(struct player *pl, char *format
107 /* format is message text */, ...)
109 char buf[4096]; /* buf is message text */
112 if (pl->state != PS_PLAYING)
114 va_start(ap, format);
115 (void)vsprintf(buf, format, ap);
118 * Translate to ASCII if the client is not in UTF mode
120 if (!(pl->flags & PF_UTF8))
122 pr_player(pl, C_FLASH, buf);
123 io_output(pl->iop, IO_NOWAIT);
127 pr_inform(struct player *pl, s_char *format, ...)
132 if (pl->state != PS_PLAYING)
134 va_start(ap, format);
135 (void)vsprintf(buf, format, ap);
137 pr_player(pl, C_INFORM, buf);
138 io_output(pl->iop, IO_NOWAIT);
142 pr_wall(s_char *format, ...)
148 va_start(ap, format);
149 (void)vsprintf(buf, format, ap);
151 for (p = player_next(0); p; p = player_next(p)) {
152 if (p->state != PS_PLAYING)
154 pr_player(p, C_FLASH, buf);
155 io_output(p->iop, IO_NOWAIT);
160 pr_player(struct player *pl, int id, s_char *buf)
167 while (*bp != '\0') {
168 if (pl->curid != -1 && pl->curid != id) {
169 io_puts(pl->iop, "\n");
174 p = strchr(bp, '\n');
177 if (pl->command && (pl->command->c_flags & C_MOD))
178 io_write(pl->iop, bp, len, IO_NOWAIT);
180 io_write(pl->iop, bp, len, IO_WAIT);
184 len = io_puts(pl->iop, bp);
191 upr_player(struct player *pl, int id, char *buf
192 /* buf is message text */)
194 register char *bp; /* bp is message text */
195 register int standout = 0;
196 char printbuf[2]; /* bp is message text */
203 while ((ch = *bp++)) {
204 if (pl->curid != -1 && pl->curid != id) {
205 io_puts(pl->iop, "\n");
214 io_puts(pl->iop, printbuf);
221 io_puts(pl->iop, printbuf);
226 if (pl->command && (pl->command->c_flags & C_MOD))
227 io_write(pl->iop, &ch, 1, IO_NOWAIT);
229 io_write(pl->iop, &ch, 1, IO_WAIT);
233 io_puts(pl->iop, printbuf);
239 * highlighted characters have hex 80 or'ed in
240 * with them to designate their highlightedness
243 pr_hilite(s_char *buf)
249 p = malloc(strlen(buf) + 1);
251 for (bp = p; 0 != (c = *bp); bp++)
259 * output hex code + space
262 outid(struct player *pl, int n)
268 logerror("outid: %d not valid code\n", n);
278 io_puts(pl->iop, buf);
283 prredir(s_char *redir)
285 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
291 pr_id(player, C_EXECUTE, "%s\n", file);
295 prprompt(int min, int btu)
297 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
301 prmptrd(char *prompt, char *str, int size)
306 pr_id(player, C_FLUSH, "%s\n", prompt);
307 if ((r = recvclient(str, size)) < 0)
309 time(&player->curup);
312 for(cp = str; 0 != *cp; ++cp) {
313 if ((*cp >= 0x0 && *cp < 0x20 && *cp != '\t') ||
314 *cp == 0x7f || *cp & 0x80)
321 uprmptrd(char *prompt, char *str /* str is message text */, int size)
324 char *cp; /* cp is message text */
326 pr_id(player, C_FLUSH, "%s\n", prompt);
327 if ((r = recvclient(str, size)) < 0)
329 time(&player->curup);
333 for(cp = str; 0 != *cp; ++cp) {
334 if ((*cp >= 0x0 && *cp < 0x20 && *cp != '\t') ||
337 else if (!(player->flags & PF_UTF8) && (*cp & 0x80))
353 * print x,y formatting as country
356 prxy(s_char *format, coord x, coord y, natid country)
361 np = getnatp(country);
362 sprintf(buf, format, xrel(np, x), yrel(np, y));
368 PR(int cn, s_char *format, ...)
370 /* XXX should really do this on a per-nation basis */
371 static s_char longline[MAXNOC][512];
376 va_start(ap, format);
377 (void)vsprintf(buf, format, ap);
379 newline = strrchr(buf, '\n') ? 1 : 0;
380 strcat(longline[cn], buf);
382 if (update_pending || (cn && cn != player->cnum))
383 typed_wu(0, cn, longline[cn], TEL_BULLETIN);
385 pr_player(player, C_DATA, longline[cn]);
386 longline[cn][0] = '\0';
402 struct natstr *np = getnatp(player->cnum);
404 if (np->nat_flags & NF_BEEP)
409 mpr(int cn, s_char *format, ...)
414 va_start(ap, format);
415 (void)vsprintf(buf, format, ap);
418 if (update_pending || cn != player->cnum)
419 typed_wu(0, cn, buf, TEL_BULLETIN);
421 pr_player(player, C_DATA, buf);
426 prtoascii(char *buf /* buf is message text */)
428 char *pbuf; /* pbuf is message text */
430 for(pbuf = buf; *pbuf != 0; pbuf++)
431 if ((*pbuf & 0xc0) == 0xc0)
433 else if (*pbuf & 0x80) {
434 memmove(pbuf,pbuf+1,strlen(pbuf)-1);
440 * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
441 * If S doesn't have that many characters, return its length instead.
444 ufindpfx(char *s, int n)
450 if ((s[i++] & 0xc0) == 0xc0)
451 while ((s[i] & 0xc0) == 0x80)