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