1 /* File m.c */
2 /***************************************************************************
3 * Copyright 2003 - Steven Shipway <steve@cheshire.demon.co.uk> *
4 * Put "nospam" in subject to avoid spam filter *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA *
19 * 02111-1307, USA. *
20 ***************************************************************************/
21
22
23 #include <stdlib.h>
24 #include <sys/time.h>
25 #include "wand_head.h"
26
27 #define SCROLLING 0
28 #define PAGED 1
29
30 /**********************************
31 * function declarations *
32 ***********************************/
33 extern char *playscreen();
34 extern int rscreen();
35 extern int savescore();
36 extern void map();
37 extern void check_legality();
38
39 /***********************************
40 * Global Variables *
41 ************************************/
42 int debug_disp = 0;
43 int no_passwords = 0;
44 int maxscreens;
45 char screen[NOOFROWS][ROWLEN+1];
46 int edit_mode = 0;
47 int saved_game = 0;
48 char *edit_screen = (char *) 0;
49 char *edit_memory = (char *) 0;
50 char *memory_end = (char *) 0;
51 char screen_name[61] ;
52 int record_file = -1;
53
54 /**/
55 /***********************************
56 * readline *
57 ************************************/
readline(int fd,char * ptr,int max)58 int readline(int fd,char *ptr,int max)
59 {
60 int count=0;
61
62 while(read(fd,ptr,1)==1)
63 {
64 if(++count==max)
65 break;
66 if(*ptr=='\n')
67 break;
68 ptr++;
69 }
70 *ptr='\0';
71 return count;
72 }
73
74 /**************************************************
75 * Show Credits *
76 * Break this function into 2 discrete functions *
77 * One for scrolling and one for paged Credits *
78 **************************************************/
show_credits(opt)79 void show_credits(opt)
80 int opt;
81 {
82 int maxlines, linecount;
83 FILE *fp;
84 char ch, buffer[100];
85 int inp ,nul;
86 struct timeval tv;
87
88 sprintf(buffer,"%s/credits",SCREENPATH);
89 if((fp = fopen(buffer,"r")) == NULL) {
90 printf("Sorry - credits unavailable!\n");
91 exit(1);
92 }
93 if (opt == PAGED)
94 {
95 initscr();
96 CBON;
97 noecho();
98 maxlines = tgetnum("li") - 3;
99 }
100 linecount = 0;
101 nul = 0;
102 tv.tv_sec = 0;
103 tv.tv_usec = 500000L; /* half second between scrolls */
104 while( fgets(buffer,100,fp) ) {
105 addstr(buffer);
106 if(opt == PAGED) {
107 if(++linecount == maxlines) {
108 move(maxlines + 2,0);
109 addstr("-- More --");
110 refresh();
111 ch = getch();
112 if( ch == 'q' )
113 break;
114 move(maxlines + 2,0);
115 addstr(" ");
116 refresh();
117 clear();
118 linecount = 0;
119 }
120 } else { /* opt == SCROLLING */
121 inp = 1;
122 printf("%s",buffer);
123 select(1,&inp,&nul,&nul,&tv);
124 if(inp) {
125 read(0,&ch,1);
126 if(ch == 'q')
127 break;
128 }
129 }
130 }
131 if(opt == PAGED ) { /* Marina's changes - stdout blocked under curses */
132 CBOFF;
133 echo();
134 move(maxlines+2,0);
135 refresh();
136 endwin();
137 }
138 }
139
140 /**/
141 /***************************************************************
142 * get_name *
143 * Placed in a new separate function. Is asking really needed ? *
144 * Call function before initializing curses. Marina *
145 ****************************************************************/
get_name(void)146 char *get_name(void)
147 {
148 char *name;
149 char *endchar;
150
151 if((name = (char *)getenv("NEWNAME")) == NULL)
152 if((name = (char *)getenv("NAME")) == NULL)
153 if((name = (char *)getenv("FULLNAME")) == NULL)
154 if((name = (char *)getenv("USER")) == NULL)
155 if((name = (char *)getenv("LOGNAME")) == NULL)
156 #define ASKNAME /* Marina */
157 #ifdef ASKNAME /* M001 */
158 {
159 name = (char *)malloc(80);
160 if (name == NULL) {
161 printf("malloc error\n"); /* Replace with Err* */
162 exit(1);
163 }
164 printf("Name? "); fflush(stdout);
165 fgets(name,80,stdin); /* get rid of gets Marina*/
166 endchar=strchr(name,'\0');
167 endchar='\0';
168 if (name[0] == '\0')
169 name = "noname";
170 }
171 #else
172 name = "noname";
173 #endif
174 return(name);
175 }
176
177 /**********************************************
178 * get_keys *
179 * Get movement keys for navigating screen *
180 ***********************************************/
get_keys()181 char *get_keys()
182 {
183 char *keys = NULL;
184 if( ! keys ) {
185 if((keys = (char *)getenv("NEWKEYS")) == NULL)
186 {
187 keys = (char *)malloc(5);
188 strcpy(keys,"kjhl");
189 }
190 }
191 return(keys);
192 }
193
194 /**/
195 /***************
196 * Why here ? *
197 ****************/
198 extern int optind,opterr;
199 extern char *optarg;
200
201 /***************************************
202 * Main Program -- Comment by Marina *
203 ****************************************/
main(int argc,char ** argv)204 main(int argc,char **argv)
205 {
206 char (*frow)[ROWLEN+1] = screen;
207 long score = 0;
208 int fp,
209 num = 1,
210 bell = 0,
211 maxmoves = 0,
212 x,y;
213 char howdead[25],
214 buffer[100],
215 *name,
216 *keys,
217 *dead,ch;
218 char c;
219
220 while(( c = getopt(argc,argv,"01k:et:r:fmCcvsi")) != -1 )
221 {
222 switch(c)
223 {
224 case '0': bell = 0; break;
225 case '1': bell = 1; break;
226 case 'k': keys = optarg; break;
227 case 'i':
228 printf("\nWANDERER Copyright (C) 1988 S Shipway. Version %s.\n\n",VERSION);
229 printf("Screens in %s.\n",SCREENPATH);
230 printf("Hiscore File %s.\n",HISCOREPATH);
231 printf("Looking for Dictionary in %s.\n",DICTIONARY);
232 printf("Lockfile for Scorefile %s.\n",LOCKFILE);
233 exit(0);
234 break;
235 case 'm': erase_scores(); /* Need noecho() before rec passwd Marina */
236 exit(0);
237 case 'c': show_credits(PAGED);
238 exit(0);
239 case 'C': show_credits(SCROLLING);
240 exit(0);
241 case 's':
242 savescore("-",-1,-1,"-"); /* savescore bug ! */
243 exit(0);
244 case 'f': debug_disp = 1; break; /* Full screen mode */
245 case 'v': printf("\nWANDERER Copyright (C) 1988 S Shipway. Version %s.\n\n",VERSION);
246 exit (0);
247 case 'r':
248 if((record_file = open(optarg,O_WRONLY|O_CREAT,0600))==-1) {
249 printf("Cannot open file %s for recording.\n",optarg);
250 exit(-1);
251 }
252 break;
253 case 'e': edit_mode = 1;
254 no_passwords = 1;
255 memory_end = edit_memory = (char *)malloc(EMSIZE * sizeof(char));
256 break;
257 case 't': edit_screen = optarg;
258 debug_disp = edit_mode = 1;
259 rscreen(0,&maxmoves);
260 initscr();
261 map(frow);
262 check_legality();
263 move(21,0); refresh();
264 endwin();
265 exit(0);
266 default: fprintf(stderr,"Usage: %s [ -e | -m | -C | -c | -s | [ -r file ] [ -f ] | -t file ] [ -0 | -1 ] [ -i ] [ -k keys ] [ file ]\n",argv[0]);
267 exit(1);
268 }
269 }
270
271 if( optind < argc ) edit_screen = argv[optind];
272
273 /* check for passwords - if file no_pws is in screen dir no pws! */
274 sprintf(buffer,"%s/no_pws",SCREENPATH);
275 if((fp = open(buffer,O_RDONLY)) != -1) {
276 close(fp);
277 no_passwords = 1;
278 }
279
280 /* count available screens */
281 for(maxscreens = 0;;maxscreens++) {
282 sprintf(buffer,"%s/screen.%d",SCREENPATH,(maxscreens+1));
283 if((fp = open(buffer, O_RDONLY)) == -1 )
284 break;
285 close(fp);
286 }
287
288 name=get_name(); /* M */
289 keys=get_keys(); /* M */
290 initscr();
291
292 /* MAIN PROGRAM HERE */
293
294 CBON; noecho();
295
296 if(!edit_mode) {
297 for (;;num++) {
298 if (rscreen(num,&maxmoves))
299 {
300 strcpy(howdead,"a non-existant screen");
301 break;
302 }
303 dead = playscreen(&num,&score,&bell,maxmoves,keys);
304 if ((dead != NULL) && (*dead == '~'))
305 {
306 num = (int)(dead[1]) - 1;
307 dead = NULL;
308 }
309 if (dead != NULL)
310 {
311 strcpy(howdead,dead);
312 break;
313 }
314 }
315 }
316 else
317 {
318 if(rscreen(num,&maxmoves))
319 {
320 for(x=0;x<ROWLEN;x++)
321 for(y=0;y<NOOFROWS;y++)
322 screen[y][x] = ' ';
323 }
324 editscreen(num,&score,&bell,maxmoves,keys);
325 }
326 /* END OF MAIN PROGRAM */
327
328 /****************************
329 * SAVE ROUTINES FOR SCORES *
330 *****************************/
331 if(!edit_mode)
332 {
333 getch();
334 echo();
335 refresh();
336 endwin();
337 printf("%s killed by %s with a score of %d on level %d.\n",
338 name,howdead,score,num);
339 printf("\nWANDERER (C)1988 S. Shipway\n");
340 if((savescore(howdead,score,num,name) == 0)&&(score != 0))
341 printf("\nWARNING: %s : score not saved!\n",argv[0]);
342 }
343 else
344 { /* Still need to clean up screen, yes ? Marina */
345
346 echo();
347 refresh();
348 endwin();
349 }
350 if(record_file != -1)/* Wouldn't it be better !to leave file open?*/
351 close(record_file);
352 }
353