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