Preparing for release.
[super-star-trek.git] / c-version / src / io.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <wchar.h>
4
5 #include "config.h"
6 #include "sst.h"
7 #include "sstlinux.h"
8
9 static int rows, linecount;     /* for paging */
10
11 WINDOW *curwnd;
12 WINDOW *fullscreen_window;
13 WINDOW *srscan_window;
14 WINDOW *report_window;
15 WINDOW *status_window;
16 WINDOW *lrscan_window;
17 WINDOW *message_window;
18 WINDOW *prompt_window;
19
20 static void outro(void)
21 /* wrap up, either normally or due to signal */
22 {
23     if (game.options & OPTION_CURSES) {
24         //clear();
25         //curs_set(1);
26         //refresh();
27         //resetterm();
28         //echo();
29         endwin();
30         putchar('\n');
31     }
32     if (logfp)
33         fclose(logfp);
34 }
35
36 void iostart(void) 
37 {
38     setlocale(LC_ALL, "");
39     bindtextdomain(PACKAGE, LOCALEDIR);
40     textdomain(PACKAGE);
41
42     if (atexit(outro)){
43         fprintf(stderr,"Unable to register outro(), exiting...\n");
44         exit(1);
45     }
46     if (!(game.options & OPTION_CURSES)) {
47         char *ln_env = getenv("LINES");
48         rows = ln_env ? atoi(ln_env) : 25;
49     } else {
50         initscr();
51 #ifdef KEY_MIN
52         keypad(stdscr, TRUE);
53 #endif /* KEY_MIN */
54         saveterm();
55         nonl();
56         cbreak();
57 #ifdef A_COLOR
58         {
59             start_color();
60             init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
61             init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
62             init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
63             init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
64             init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
65             init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
66             init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
67             init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
68         }
69 #endif /* A_COLOR */
70         //noecho();
71         fullscreen_window = stdscr;
72         srscan_window     = newwin(12, 25, 0,       0);
73         report_window     = newwin(11, 0,  1,       25);
74         status_window     = newwin(10, 0,  1,       39);
75         lrscan_window     = newwin(5,  0,  0,       64); 
76         message_window    = newwin(0,  0,  12,      0);
77         prompt_window     = newwin(1,  0,  LINES-2, 0); 
78         scrollok(message_window, TRUE);
79         setwnd(fullscreen_window);
80         textcolor(DEFAULT);
81     }
82 }
83
84
85 void waitfor(void)
86 /* wait for user action -- OK to do nothing if on a TTY */
87 {
88     if (game.options & OPTION_CURSES)
89         wgetch(prompt_window);
90 }
91
92 void announce(void)
93 {
94     skip(1);
95     prouts(_("[ANNOUNCEMENT ARRIVING...]"));
96     skip(1);
97 }
98
99 static void pause_game(void)
100 {
101         char *prompt;
102         char buf[BUFSIZ];
103         if (game.skill > SKILL_FAIR)
104             prompt = _("[CONTINUE?]");
105         else
106             prompt = _("[PRESS ENTER TO CONTINUE]");
107
108         if (game.options & OPTION_CURSES) {
109             drawmaps(0);
110             setwnd(prompt_window);
111             wclear(prompt_window);
112             waddstr(prompt_window, prompt);
113             wgetnstr(prompt_window, buf, sizeof(buf));
114             wclear(prompt_window);
115             wrefresh(prompt_window);
116             setwnd(message_window);
117         } else {
118             int j;
119             putchar('\n');
120             proutn(prompt);
121             fgets(buf, sizeof(buf), stdin);
122             for (j = 0; j < rows; j++)
123                 putchar('\n');
124             linecount = 0;
125         }
126 }
127
128
129 void skip(int i) 
130 {
131     while (i-- > 0) {
132         if (game.options & OPTION_CURSES) {
133             if (curwnd == message_window && getcury(curwnd) >= getmaxy(curwnd) - 3) {
134                 pause_game();
135                 clrscr();
136             } else {
137                 proutn("\n");
138             }
139         } else {
140             linecount++;
141             if (linecount >= rows)
142                 pause_game();
143             else
144                 putchar('\n');
145         }
146     }
147 }
148
149 static void vproutn(const char *fmt, va_list ap) 
150 {
151     if (game.options & OPTION_CURSES) {
152         vwprintw(curwnd, fmt, ap);
153         wrefresh(curwnd);
154     }
155     else
156         vprintf(fmt, ap);
157 }
158
159 void proutn(const char *fmt, ...) 
160 {
161     va_list ap;
162     va_start(ap, fmt);
163     vproutn(fmt, ap);
164     va_end(ap);
165 }
166
167 void prout(const char *fmt, ...) 
168 {
169     va_list ap;
170     va_start(ap, fmt);
171     vproutn(fmt, ap);
172     va_end(ap);
173     skip(1);
174 }
175
176 void prouts(const char *fmt, ...) 
177 /* print slowly! */
178 {
179     char buf[BUFSIZ];
180     wchar_t *s, mbuf[BUFSIZ];
181     va_list ap;
182     va_start(ap, fmt);
183     vsprintf(buf, fmt, ap);
184     va_end(ap);
185     mbstowcs(mbuf, buf, BUFSIZ);
186     for (s = mbuf; *s; s++) {
187         /* HOW to convince ncurses to use wchar_t?? */
188         /* WHY putwchar() doesn't work?? */
189         /* OK then, convert back to mbs... */
190         char c[MB_CUR_MAX*2];
191         int n;
192         n = wctomb(c, *s);
193         c[n] = 0;
194         delay(30);
195         proutn(c);
196         if (game.options & OPTION_CURSES)
197             wrefresh(curwnd);
198         else
199             fflush(stdout);
200     }
201     delay(300);
202 }
203
204 void cgetline(char *line, int max)
205 {
206     if (game.options & OPTION_CURSES) {
207         wgetnstr(curwnd, line, max);
208         strcat(line, "\n");
209         wrefresh(curwnd);
210     } else {
211         if (replayfp && !feof(replayfp))
212             (void)fgets(line, max, replayfp);
213         else
214             (void)fgets(line, max, stdin);
215     }
216     if (logfp)
217         (void)fputs(line, logfp);
218     line[strlen(line)-1] = '\0';
219 }
220
221 void setwnd(WINDOW *wnd)
222 /* change windows -- OK for this to be a no-op in tty mode */
223 {
224     if (game.options & OPTION_CURSES) {
225      curwnd=wnd;
226      curs_set(wnd == fullscreen_window || wnd == message_window || wnd == prompt_window);
227     }
228 }
229
230 void clreol(void)
231 /* clear to end of line -- can be a no-op in tty mode */
232 {
233    if (game.options & OPTION_CURSES) {
234        wclrtoeol(curwnd);
235        wrefresh(curwnd);
236    }
237 }
238
239 void clrscr(void)
240 /* clear screen -- can be a no-op in tty mode */
241 {
242    if (game.options & OPTION_CURSES) {
243        wclear(curwnd);
244        wmove(curwnd,0,0);
245        wrefresh(curwnd);
246    }
247    linecount = 0;
248 }
249
250 void textcolor(int color)
251 {
252 #ifdef A_COLOR
253     if (game.options & OPTION_CURSES) {
254         switch(color) {
255         case DEFAULT: 
256             wattrset(curwnd, 0);
257             break;
258         case BLACK: 
259             wattron(curwnd, COLOR_PAIR(COLOR_BLACK));
260             break;
261         case BLUE: 
262             wattron(curwnd, COLOR_PAIR(COLOR_BLUE));
263             break;
264         case GREEN: 
265             wattron(curwnd, COLOR_PAIR(COLOR_GREEN));
266             break;
267         case CYAN: 
268             wattron(curwnd, COLOR_PAIR(COLOR_CYAN));
269             break;
270         case RED: 
271             wattron(curwnd, COLOR_PAIR(COLOR_RED));
272             break;
273         case MAGENTA: 
274             wattron(curwnd, COLOR_PAIR(COLOR_MAGENTA));
275             break;
276         case BROWN: 
277             wattron(curwnd, COLOR_PAIR(COLOR_YELLOW));
278             break;
279         case LIGHTGRAY: 
280             wattron(curwnd, COLOR_PAIR(COLOR_WHITE));
281             break;
282         case DARKGRAY: 
283             wattron(curwnd, COLOR_PAIR(COLOR_BLACK) | A_BOLD);
284             break;
285         case LIGHTBLUE: 
286             wattron(curwnd, COLOR_PAIR(COLOR_BLUE) | A_BOLD);
287             break;
288         case LIGHTGREEN: 
289             wattron(curwnd, COLOR_PAIR(COLOR_GREEN) | A_BOLD);
290             break;
291         case LIGHTCYAN: 
292             wattron(curwnd, COLOR_PAIR(COLOR_CYAN) | A_BOLD);
293             break;
294         case LIGHTRED: 
295             wattron(curwnd, COLOR_PAIR(COLOR_RED) | A_BOLD);
296             break;
297         case LIGHTMAGENTA: 
298             wattron(curwnd, COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
299             break;
300         case YELLOW: 
301             wattron(curwnd, COLOR_PAIR(COLOR_YELLOW) | A_BOLD);
302             break;
303         case WHITE:
304             wattron(curwnd, COLOR_PAIR(COLOR_WHITE) | A_BOLD);
305             break;
306         }
307     }
308 #endif /* A_COLOR */
309 }
310
311 void highvideo(void)
312 {
313     if (game.options & OPTION_CURSES) {
314         wattron(curwnd, A_REVERSE);
315     }
316 }
317  
318 void commandhook(char *cmd, bool before) {
319 }
320
321 /*
322  * Things past this point have policy implications.
323  */
324
325 void drawmaps(int mode)
326 /* hook to be called after moving to redraw maps */
327 {
328     if (game.options & OPTION_CURSES) {
329         if (mode == 1)
330             sensor();
331         setwnd(srscan_window);
332         wmove(curwnd, 0, 0);
333         srscan();
334         if (mode != 2) {
335             setwnd(status_window);
336             wclear(status_window);
337             wmove(status_window, 0, 0);
338             setwnd(report_window);
339             wclear(report_window);
340             wmove(report_window, 0, 0);
341             status(0);
342             setwnd(lrscan_window);
343             wclear(lrscan_window);
344             wmove(lrscan_window, 0, 0);
345             lrscan();
346         }
347     }
348 }
349
350 static void put_srscan_sym(coord w, char sym)
351 {
352     wmove(srscan_window, w.x+1, w.y*2+2);
353     waddch(srscan_window, sym);
354     wrefresh(srscan_window);
355 }
356
357 void boom(coord w)
358 /* enemy fall down, go boom */ 
359 {
360     if (game.options & OPTION_CURSES) {
361         drawmaps(2);
362         setwnd(srscan_window);
363         wattron(srscan_window, A_REVERSE);
364         put_srscan_sym(w, game.quad[w.x][w.y]);
365         sound(500);
366         delay(1000);
367         nosound();
368         wattroff(srscan_window, A_REVERSE);
369         put_srscan_sym(w, game.quad[w.x][w.y]);
370         delay(500);
371         setwnd(message_window);
372     }
373
374
375 void warble(void)
376 /* sound and visual effects for teleportation */
377 {
378     if (game.options & OPTION_CURSES) {
379         drawmaps(2);
380         setwnd(message_window);
381         sound(50);
382     }
383     prouts("     . . . . .     ");
384     if (game.options & OPTION_CURSES) {
385         delay(1000);
386         nosound();
387     }
388 }
389
390 void tracktorpedo(coord w, int l, int i, int n, int iquad)
391 /* torpedo-track animation */
392 {
393     if (!(game.options & OPTION_CURSES)) {
394         if (l == 1) {
395             if (n != 1) {
396                 skip(1);
397                 proutn(_("Track for torpedo number %d-  "), i);
398             }
399             else {
400                 skip(1);
401                 proutn(_("Torpedo track- "));
402             }
403         } else if (l==4 || l==9) 
404             skip(1);
405         proutn("%d - %d   ", w.x, w.y);
406     } else {
407         if (!damaged(DSRSENS) || game.condition==docked) {
408             if (i != 1 && l == 1) {
409                 drawmaps(2);
410                 delay(400);
411             }
412             if ((iquad==IHDOT)||(iquad==IHBLANK)){
413                 put_srscan_sym(w, '+');
414                 sound(l*10);
415                 delay(100);
416                 nosound();
417                 put_srscan_sym(w, iquad);
418             }
419             else {
420                 wattron(curwnd, A_REVERSE);
421                 put_srscan_sym(w, iquad);
422                 sound(500);
423                 delay(1000);
424                 nosound();
425                 wattroff(curwnd, A_REVERSE);
426                 put_srscan_sym(w, iquad);
427             }
428         } else {
429             proutn("%d - %d   ", w.x, w.y);
430         }
431     }
432 }
433
434 void makechart(void) 
435 {
436     if (game.options & OPTION_CURSES) {
437         setwnd(message_window);
438         wclear(message_window);
439     }
440     chart();
441     if (game.options & OPTION_TTY) {
442         skip(1);
443     }
444 }
445
446 void prstat(const char *txt, const char *fmt, ...)
447 {
448 #define NSYM 14
449     int i;
450     va_list args;
451     proutn(txt);
452     if (game.options & OPTION_CURSES) {
453         skip(1);
454     } else  {
455         for (i = mblen(txt, strlen(txt)); i < NSYM; i++)
456             proutn(" ");
457     }
458     if (game.options & OPTION_CURSES)
459         setwnd(status_window);
460     va_start(args, fmt);
461     vproutn(fmt, args);
462     va_end(args);
463     skip(1);
464     if (game.options & OPTION_CURSES)
465         setwnd(report_window);
466 }