1 #include "ladder.h"
2 #include <unistd.h>
3 #include <sys/time.h>
4 
5 
6 
7 int lads, score, level, scrno;
8 int speed = 0;
9 int boni[DIMSCRN];
10 
unmerge(char * s)11 char *unmerge(char *s)
12 {
13     static char t[128];
14     char *d = t;
15     int i;
16 
17     for( ; *s; s++ )
18         if( *s & 0200 )
19             for( i = 210 - (unsigned char)*s; i; i-- )
20                 *d++ = ' ';
21         else
22             *d++ = *s;
23     *d = '\0';
24     return t;
25 }
26 
getcmd(int row,int col)27 static int getcmd(int row, int col)
28 {
29     static char *funny[2] =
30     {
31         "You eat quiche!",
32         "Come on, we don't have all day!"
33     };
34     struct timeval tv;
35     fd_set fdset;
36 
37     for( ;; )
38     {
39         mvaddstr(row,col,"Enter one of the above: ");
40         refresh();
41         FD_ZERO(&fdset);
42         FD_SET(0,&fdset);
43         tv.tv_sec = 10;
44         tv.tv_usec = 0;
45         if( select(1,&fdset,(fd_set *)0,(fd_set *)0,&tv) )
46             break;
47         mvaddstr(row + 2,col,funny[rand() % DIM(funny)]);
48         refresh();
49         tv.tv_sec = 0;
50         tv.tv_usec = 500000;
51         select(0,(fd_set *)0,(fd_set *)0,(fd_set *)0,&tv);
52         move(row + 2,col);
53         clrtoeol();
54     }
55     return getch();
56 }
57 
menu(void)58 static char menu(void)
59 {
60 #define LM  2
61 #define RM0 33
62 #define RM1 40
63 
64     static char *text[] =
65     {
66         "LL\275dd\313dd",
67         "LL\275dd\313dd\274tm",
68         "LL\311aaaa\315ddddd\316ddddd\316eeee\317rrrrrrr",
69         "LL\312aa\320aa\317dd\320dd\317dd\320dd\317ee\320ee\320rr\316rr",
70         "LL\312aa\320aa\317dd\320dd\317dd\320dd\317eeeeee\320rr",
71         "LL\312aa\320aa\317dd\320dd\317dd\320dd\317ee\314rr",
72         "LLLLLLLL\317aaa\321aa\317ddd\321dd\317ddd\321dd\317eeee\317rr"
73     };
74     int r;
75 
76     for( r = 0; r < DIM(text); r++ )
77         mvaddstr(r + 1,11,unmerge(text[r]));
78     r += 3;
79     mvaddstr(r,LM,
80         "(c) in 1982, 1983: Yahoo Software, cloned by Andreas Burmester.");
81     r += 2;
82     mvaddstr(r,LM,"Version:    n/a");
83     mvaddstr(r,RM0,"Up = k|8  Down = j|2  Left = h|4  Right = l|6");
84     r++;
85     mvprintw(r,LM,"Terminal:   %s",getenv("TERM"));
86     mvaddstr(r,RM0,"Jump = Space   Stop = Other");
87     r++;
88     mvprintw(r,LM,"Play Speed: %d",speed + 1);
89     r++;
90     prt_score(r,RM1);
91     r++;
92     mvaddstr(r++,LM,"P = Play game");
93     mvaddstr(r++,LM,"L = Change level of difficulty");
94     mvaddstr(r++,LM,"I = Instructions");
95     mvaddstr(r++,LM,"E = Exit Ladder");
96     r++;
97     refresh();
98     return getcmd(r,LM);
99 
100 #undef LM
101 #undef RM0
102 #undef RM1
103 }
104 
105 
instructions(void)106 static void instructions(void)
107 {
108     static char *text[] =
109     {
110         "You are a Lad trapped in a maze.  Your mission is to explore the",
111         "dark corridors never before seen by human eyes and find hidden",
112         "treasures and riches.","",
113         "You control Lad by typing the direction buttons and jumping by",
114         "typing SPACE.  But beware of the falling rocks called Der rocks.",
115         "You must find and grasp the treasure (shown as $) BEFORE the",
116         "bonus time runs out.","",
117         "A new Lad will be awarded for every 10,000 points.",
118         "Extra points are awarded for touching the gold",
119         "statues (shown as &).  You will receive the bonus time points",
120         "that are left when you have finished the level.",
121         "Remember, there is more than one way to skin a cat. (Chum)",
122         "Type an ESCape to pause the game.","",
123         "Good luck Lad.","","",
124         "Type RETURN to return to main menu: "
125     };
126     int r;
127 
128     clear();
129     for( r = 0; r < DIM(text); r++ )
130         mvaddstr(r + 2,4,text[r]);
131     refresh();
132     nodelay(stdscr,FALSE);
133     getch();
134     nodelay(stdscr,TRUE);
135 }
136 
play(void)137 static void play(void)
138 {
139     int hi_scrno;
140 
141     memcpy(boni,st_boni,sizeof(st_boni));
142     lads = 5;
143     score = 0;
144     scrno = 0;
145     hi_scrno = 1;
146     clear();
147     for( level = 0; ; level++ )
148     {
149         if( lplay() == DEAD )
150             break;
151         boni[scrno] -= 2;
152         if( ++scrno > hi_scrno )
153         {
154             if( hi_scrno != DIMSCRN - 1)
155                 hi_scrno++;
156             scrno = 0;
157         }
158     }
159     upd_score();
160 }
161 
main(void)162 int main(void)
163 {
164     if( (int)initscr() == ERR )
165     {
166         fputs("Curses initialization failed.\n",stderr);
167         return EXIT_FAILURE;
168     }
169     if( LINES < 24 || COLS < 80 )
170     {
171         addstr("Unsufficient Screen Dimensions.");
172         mexit0();
173     }
174     cbreak();
175     noecho();
176     nodelay(stdscr,TRUE);
177     leaveok(stdscr,TRUE);
178     /* keypad(stdscr,TRUE); */
179     typeahead(-1);
180     signal(SIGINT,mexit1);
181     signal(SIGQUIT,mexit1);
182     srand(getpid());
183 
184     clear();
185     for( ;; )
186         switch( toupper(menu()) )
187         {
188             case 'P':
189                 play();
190                 clear();
191                 break;
192             case 'I':
193                 instructions();
194                 clear();
195                 break;
196             case 'L':
197                 if( ++speed == HISPEED )
198                     speed = 0;
199                 break;
200             case 'R'-'@':
201             case 'L'-'@':
202             case KEY_CLEAR:
203                 wrefresh(curscr);
204                 break;
205             case 'E':
206                 mexit1();
207             default:
208                 flash();
209         }
210     mexit1();
211 }
212 
mexit0()213 void mexit0()
214 {
215     refresh();
216     noraw();
217     echo();
218     endwin();
219     exit(EXIT_SUCCESS);
220 }
221 
mexit1()222 void mexit1()
223 {
224     move(23,0);
225     mexit0();
226 }
227