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