Knock SERGEEV out of setup.
[super-star-trek.git] / io.c
1 #ifdef SERGEEV
2 #define _GNU_SOURCE
3 #endif /* SERGEEV */
4 #include <stdio.h>
5 #ifdef SERGEEV
6 #include <unistd.h>
7 #endif /* SERGEEV */
8 #include <termios.h>
9 #include <curses.h>
10 #include <signal.h>
11 #include <ctype.h>
12 #include <stdarg.h>
13 #ifdef SERGEEV
14 #include <conio.h>
15 #endif /* SERGEEV */
16 #ifdef MSDOS
17 #include <dos.h>
18 #endif
19 #include <time.h>
20
21 #ifdef SERGEEV
22 #include "sstlinux.h"
23 #endif /* SERGEEV */
24 #include "sst.h"
25
26 #ifndef SERGEEV
27 static int linecount;   /* for paging */
28 #endif /* SERGEEV */
29 static int screenheight = 24, screenwidth = 80;
30 #ifndef SERGEEV
31 static int curses = FALSE;
32 #else /* SERGEEV */
33 static int curses = TRUE;
34 #endif /* SERGEEV */
35
36 static void outro(int sig) {
37 /* wrap up, either normally or due to signal */
38     if (curses) {
39         clear();
40         (void)refresh();
41         (void)resetterm();
42         //(void)echo();
43         (void)endwin();
44     }
45 }
46
47 static void fastexit(int sig) {
48     outro(sig);
49     putchar('\n');
50     exit(0);
51 }
52
53 void iostart(int usecurses) {
54         (void) signal(SIGINT, fastexit);
55         (void) signal(SIGINT, fastexit);
56 #ifdef SIGIOT
57         (void) signal(SIGIOT,fastexit);         /* for assert(3) */
58 #endif /* SIGIOT */
59         if(signal(SIGQUIT,SIG_IGN) != SIG_IGN)
60             (void)signal(SIGQUIT,fastexit);
61
62         if ((curses = usecurses)) {
63                 (void)initscr();
64 #ifdef KEY_MIN
65                 keypad(stdscr, TRUE);
66 #endif /* KEY_MIN */
67                 (void)saveterm();
68                 (void)nonl();
69                 (void)cbreak();
70                 //(void)noecho();
71                 scrollok(stdscr, TRUE);
72                 getmaxyx(stdscr, screenheight, screenwidth);
73         } else {
74                 char *LINES = getenv("LINES");
75                 if (LINES)
76                     screenheight = atoi(LINES);
77         }
78 }
79
80 void ioend(void) {
81     outro(0);
82 }
83
84 void clearscreen(void) {
85         /* Somehow we need to clear the screen */
86 #ifdef __BORLANDC__
87         extern void clrscr(void);
88         clrscr();
89 #else
90         if (curses) {
91             wclear(stdscr);
92             wrefresh(stdscr);
93         } else {
94                 // proutn("\033[2J"); /* Hope for an ANSI display */
95                 /* much more in that old-TTY spirit to just throw linefeeds */
96                 int i;
97                 for (i = 0; i < screenheight; i++)
98                     putchar('\n');
99         }
100 #endif
101 }
102
103 void waitfor(void) {
104 /* wait for user action -- OK to do nothing if on a TTY */
105 #ifdef SERGEEV
106         getche();
107 #endif /* SERGEEV */
108 }
109
110 void pause_game(int i) {
111         char *prompt;
112 #ifndef SERGEEV
113         char buf[BUFSIZ];
114 #else /* SERGEEV */
115         drawmaps(0);
116         setwnd(5);
117 #endif /* SERGEEV */
118         if (i==1) {
119                 if (skill > 2)
120                         prompt = "[ANOUNCEMENT ARRIVING...]";
121                 else
122                         prompt = "[IMPORTANT ANNOUNCEMENT ARRIVING -- PRESS ENTER TO CONTINUE]";
123         }
124         else {
125                 if (skill > 2)
126                         prompt = "[CONTINUE?]";
127                 else
128                         prompt = "[PRESS ENTER TO CONTINUE]";
129
130         }
131 #ifndef SERGEEV
132         if (curses) {
133             waddch(stdscr, '\n');
134                 waddstr(stdscr, prompt);
135                 wgetnstr(stdscr, buf, sizeof(buf));
136                 wclear(stdscr);
137                 wrefresh(stdscr);
138         } else {
139                 putchar('\n');
140                 proutn(prompt);
141                 fgets(buf, sizeof(buf), stdin);
142                 if (i != 0) {
143                         clearscreen();
144                 }
145                 linecount = 0;
146         }
147 #else /* SERGEEV */
148         proutn(prompt);
149         getche();
150         clrscr();
151         setwnd(4);
152         clrscr();
153 #endif /* SERGEEV */
154 }
155
156
157 void skip(int i) {
158 #ifndef SERGEEV
159     while (i-- > 0) {
160         if (curses) {
161             int y, x;
162             getyx(stdscr, y, x);
163             if (y == screenheight-1)
164                 pause_game(0);
165             else
166                 waddch(stdscr, '\n');
167         } else {
168             linecount++;
169             if (linecount >= screenheight)
170                 pause_game(0);
171             else
172                 putchar('\n');
173         }
174 #else /* SERGEEV */
175         while (i-- > 0) proutn("\n\r");
176 }
177
178 static void vproutn(char *fmt, va_list ap) {
179     char *s, *p;
180     vasprintf(&s, fmt, ap);
181     p=s;
182     if ((curwnd==4)&&(wherey()==wnds[curwnd].wndbottom-wnds[curwnd].wndtop)){
183        if (strchr(s,'\n')) {
184           p=strchr(s,'\n');
185           p[0]=0;
186           cprintf("%s",s);
187           p++;
188           pause_game(0);
189        }
190 #endif /* SERGEEV */
191     }
192 #ifdef SERGEEV
193     if ((curwnd==4)&&(wherey()>wnds[curwnd].wndbottom-wnds[curwnd].wndtop+1))
194        cprintf("\r");
195 //        setwnd(curwnd);
196     if (strchr(s,'\n') || strchr(s,'\r')) clreol();
197     cprintf("%s",p);
198     free(s);
199 #endif /* SERGEEV */
200 }
201
202 void proutn(char *fmt, ...) {
203     va_list ap;
204     va_start(ap, fmt);
205 #ifndef SERGEEV
206     if (curses) {
207         vw_printw(stdscr, fmt, ap);
208         wrefresh(stdscr);
209     } else
210         vprintf(fmt, ap);
211 #else /* SERGEEV */
212     vproutn(fmt, ap);
213 #endif /* SERGEEV */
214     va_end(ap);
215 }
216
217 void prout(char *fmt, ...) {
218     va_list ap;
219     va_start(ap, fmt);
220 #ifndef SERGEEV
221     if (curses) {
222         vw_printw(stdscr, fmt, ap);
223         wrefresh(stdscr);
224     } else
225         vprintf(fmt, ap);
226 #else /* SERGEEV */
227     vproutn(fmt, ap);
228 #endif /* SERGEEV */
229     va_end(ap);
230     skip(1);
231 }
232
233 void proutc(char *line) {
234     line[strlen(line)-1] = '\0';
235 #ifndef SERGEEV
236     if (curses)
237         waddstr(stdscr, line);
238     else
239         fputs(line, stdout);
240 #else /* SERGEEV */
241     cputs(line);
242 #endif /* SERGEEV */
243     skip(1);
244 }
245
246 #ifdef SERGEEV
247 static void prchr(char *s){
248      char str[2];
249      strncpy(str,s,1);
250      str[1]=0;
251      proutn(str);
252 }
253
254 static void vprouts(char *fmt, va_list ap) {
255     char *s, *p;
256     vasprintf(&s, fmt, ap);
257     p=s;
258     while (*p) {
259         prchr(p++);
260         delay(30);
261     }
262     free(s);
263 }
264
265 #endif /* SERGEEV */
266 void prouts(char *fmt, ...) {
267 #ifndef SERGEEV
268         clock_t endTime;
269         char *s, buf[BUFSIZ];
270         /* print slowly! */
271         va_list ap;
272         va_start(ap, fmt);
273         vsprintf(buf, fmt, ap);
274         va_end(ap);
275         skip(1);
276         for (s = buf; *s; s++) {
277                 endTime = clock() + CLOCKS_PER_SEC*0.05;
278                 while (clock() < endTime) continue;
279                 if (curses) {
280                     waddch(stdscr, *s);
281                     wrefresh(stdscr);
282                 }
283                 else {
284                     putchar(*s);
285                     fflush(stdout);
286                 }
287         }
288 #else /* SERGEEV */
289     va_list ap;
290     va_start(ap, fmt);
291     vprouts(fmt, ap);
292     va_end(ap);
293 #endif /* SERGEEV */
294 }
295
296 void warble(void)
297 /* sound and visual effects for teleportation */
298 {
299 #ifdef SERGEEV
300     int posx, posy;
301     posx=wherex();
302     posy=wherey();
303     drawmaps(1);
304     setwnd(4);
305     gotoxy(posx,posy);
306     sound(50);
307     delay(1000);
308     nosound();
309 #else
310     prouts(" . . . . . ");
311 #endif /* SERGEEV */
312 }
313
314 void setpassword(void) {
315 #ifndef SERGEEV
316         while (TRUE) {
317                 scan();
318                 strcpy(game.passwd, citem);
319                 chew();
320                 if (*game.passwd != 0) break;
321                 proutn("Please type in a secret password-");
322         }
323 #else
324         int i;
325         for(i=0;i<3;i++) game.passwd[i]=(char)(97+(int)(Rand()*25));
326         game.passwd[3]=0;
327 #endif /* SERGEEV */
328 }
329
330 void getline(char *line, int max) {
331     if (curses) {
332 #ifndef SERGEEV
333         wgetnstr(stdscr, line, max);
334         wrefresh(stdscr);
335 #else /* SERGEEV */
336         line[0]=max-1;
337         cgets(line);
338         memmove(line,&line[2],max-3);
339 #endif /* SERGEEV */
340     } else {
341         fgets(line, max, stdin);
342         line[strlen(line)-1] = '\0';
343     }
344 }
345
346 void commandhook(char *cmd, int before) {
347 }