xref: /csrg-svn/games/hack/hack.options.c (revision 41252)
1*41252Sbostic /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2*41252Sbostic /* hack.options.c - version 1.0.3 */
3*41252Sbostic 
4*41252Sbostic #include "config.h"
5*41252Sbostic #include "hack.h"
6*41252Sbostic extern char *eos();
7*41252Sbostic 
initoptions()8*41252Sbostic initoptions()
9*41252Sbostic {
10*41252Sbostic 	register char *opts;
11*41252Sbostic 	extern char *getenv();
12*41252Sbostic 
13*41252Sbostic 	flags.time = flags.nonews = flags.notombstone = flags.end_own =
14*41252Sbostic 	flags.standout = flags.nonull = FALSE;
15*41252Sbostic 	flags.no_rest_on_space = TRUE;
16*41252Sbostic 	flags.invlet_constant = TRUE;
17*41252Sbostic 	flags.end_top = 5;
18*41252Sbostic 	flags.end_around = 4;
19*41252Sbostic 	flags.female = FALSE;			/* players are usually male */
20*41252Sbostic 
21*41252Sbostic 	if(opts = getenv("HACKOPTIONS"))
22*41252Sbostic 		parseoptions(opts,TRUE);
23*41252Sbostic }
24*41252Sbostic 
parseoptions(opts,from_env)25*41252Sbostic parseoptions(opts, from_env)
26*41252Sbostic register char *opts;
27*41252Sbostic boolean from_env;
28*41252Sbostic {
29*41252Sbostic 	register char *op,*op2;
30*41252Sbostic 	unsigned num;
31*41252Sbostic 	boolean negated;
32*41252Sbostic 
33*41252Sbostic 	if(op = index(opts, ',')) {
34*41252Sbostic 		*op++ = 0;
35*41252Sbostic 		parseoptions(op, from_env);
36*41252Sbostic 	}
37*41252Sbostic 	if(op = index(opts, ' ')) {
38*41252Sbostic 		op2 = op;
39*41252Sbostic 		while(*op++)
40*41252Sbostic 			if(*op != ' ') *op2++ = *op;
41*41252Sbostic 	}
42*41252Sbostic 	if(!*opts) return;
43*41252Sbostic 	negated = FALSE;
44*41252Sbostic 	while((*opts == '!') || !strncmp(opts, "no", 2)) {
45*41252Sbostic 		if(*opts == '!') opts++; else opts += 2;
46*41252Sbostic 		negated = !negated;
47*41252Sbostic 	}
48*41252Sbostic 
49*41252Sbostic 	if(!strncmp(opts,"standout",8)) {
50*41252Sbostic 		flags.standout = !negated;
51*41252Sbostic 		return;
52*41252Sbostic 	}
53*41252Sbostic 
54*41252Sbostic 	if(!strncmp(opts,"null",3)) {
55*41252Sbostic 		flags.nonull = negated;
56*41252Sbostic 		return;
57*41252Sbostic 	}
58*41252Sbostic 
59*41252Sbostic 	if(!strncmp(opts,"tombstone",4)) {
60*41252Sbostic 		flags.notombstone = negated;
61*41252Sbostic 		return;
62*41252Sbostic 	}
63*41252Sbostic 
64*41252Sbostic 	if(!strncmp(opts,"news",4)) {
65*41252Sbostic 		flags.nonews = negated;
66*41252Sbostic 		return;
67*41252Sbostic 	}
68*41252Sbostic 
69*41252Sbostic 	if(!strncmp(opts,"time",4)) {
70*41252Sbostic 		flags.time = !negated;
71*41252Sbostic 		flags.botl = 1;
72*41252Sbostic 		return;
73*41252Sbostic 	}
74*41252Sbostic 
75*41252Sbostic 	if(!strncmp(opts,"restonspace",4)) {
76*41252Sbostic 		flags.no_rest_on_space = negated;
77*41252Sbostic 		return;
78*41252Sbostic 	}
79*41252Sbostic 
80*41252Sbostic 	if(!strncmp(opts,"fixinv",4)) {
81*41252Sbostic 		if(from_env)
82*41252Sbostic 			flags.invlet_constant = !negated;
83*41252Sbostic 		else
84*41252Sbostic 			pline("The fixinvlet option must be in HACKOPTIONS.");
85*41252Sbostic 		return;
86*41252Sbostic 	}
87*41252Sbostic 
88*41252Sbostic 	if(!strncmp(opts,"male",4)) {
89*41252Sbostic 		flags.female = negated;
90*41252Sbostic 		return;
91*41252Sbostic 	}
92*41252Sbostic 	if(!strncmp(opts,"female",6)) {
93*41252Sbostic 		flags.female = !negated;
94*41252Sbostic 		return;
95*41252Sbostic 	}
96*41252Sbostic 
97*41252Sbostic 	/* name:string */
98*41252Sbostic 	if(!strncmp(opts,"name",4)) {
99*41252Sbostic 		extern char plname[PL_NSIZ];
100*41252Sbostic 		if(!from_env) {
101*41252Sbostic 		  pline("The playername can be set only from HACKOPTIONS.");
102*41252Sbostic 		  return;
103*41252Sbostic 		}
104*41252Sbostic 		op = index(opts,':');
105*41252Sbostic 		if(!op) goto bad;
106*41252Sbostic 		(void) strncpy(plname, op+1, sizeof(plname)-1);
107*41252Sbostic 		return;
108*41252Sbostic 	}
109*41252Sbostic 
110*41252Sbostic 	/* endgame:5t[op] 5a[round] o[wn] */
111*41252Sbostic 	if(!strncmp(opts,"endgame",3)) {
112*41252Sbostic 		op = index(opts,':');
113*41252Sbostic 		if(!op) goto bad;
114*41252Sbostic 		op++;
115*41252Sbostic 		while(*op) {
116*41252Sbostic 			num = 1;
117*41252Sbostic 			if(digit(*op)) {
118*41252Sbostic 				num = atoi(op);
119*41252Sbostic 				while(digit(*op)) op++;
120*41252Sbostic 			} else
121*41252Sbostic 			if(*op == '!') {
122*41252Sbostic 				negated = !negated;
123*41252Sbostic 				op++;
124*41252Sbostic 			}
125*41252Sbostic 			switch(*op) {
126*41252Sbostic 			case 't':
127*41252Sbostic 				flags.end_top = num;
128*41252Sbostic 				break;
129*41252Sbostic 			case 'a':
130*41252Sbostic 				flags.end_around = num;
131*41252Sbostic 				break;
132*41252Sbostic 			case 'o':
133*41252Sbostic 				flags.end_own = !negated;
134*41252Sbostic 				break;
135*41252Sbostic 			default:
136*41252Sbostic 				goto bad;
137*41252Sbostic 			}
138*41252Sbostic 			while(letter(*++op)) ;
139*41252Sbostic 			if(*op == '/') op++;
140*41252Sbostic 		}
141*41252Sbostic 		return;
142*41252Sbostic 	}
143*41252Sbostic bad:
144*41252Sbostic 	if(!from_env) {
145*41252Sbostic 		if(!strncmp(opts, "help", 4)) {
146*41252Sbostic 			pline("%s%s%s",
147*41252Sbostic "To set options use `HACKOPTIONS=\"<options>\"' in your environment, or ",
148*41252Sbostic "give the command 'o' followed by the line `<options>' while playing. ",
149*41252Sbostic "Here <options> is a list of <option>s separated by commas." );
150*41252Sbostic 			pline("%s%s%s",
151*41252Sbostic "Simple (boolean) options are rest_on_space, news, time, ",
152*41252Sbostic "null, tombstone, (fe)male. ",
153*41252Sbostic "These can be negated by prefixing them with '!' or \"no\"." );
154*41252Sbostic 			pline("%s",
155*41252Sbostic "A string option is name, as in HACKOPTIONS=\"name:Merlin-W\"." );
156*41252Sbostic 			pline("%s%s%s",
157*41252Sbostic "A compound option is endgame; it is followed by a description of what ",
158*41252Sbostic "parts of the scorelist you want to see. You might for example say: ",
159*41252Sbostic "`endgame:own scores/5 top scores/4 around my score'." );
160*41252Sbostic 			return;
161*41252Sbostic 		}
162*41252Sbostic 		pline("Bad option: %s.", opts);
163*41252Sbostic 		pline("Type `o help<cr>' for help.");
164*41252Sbostic 		return;
165*41252Sbostic 	}
166*41252Sbostic 	puts("Bad syntax in HACKOPTIONS.");
167*41252Sbostic 	puts("Use for example:");
168*41252Sbostic 	puts(
169*41252Sbostic "HACKOPTIONS=\"!restonspace,notombstone,endgame:own/5 topscorers/4 around me\""
170*41252Sbostic 	);
171*41252Sbostic 	getret();
172*41252Sbostic }
173*41252Sbostic 
doset()174*41252Sbostic doset()
175*41252Sbostic {
176*41252Sbostic 	char buf[BUFSZ];
177*41252Sbostic 
178*41252Sbostic 	pline("What options do you want to set? ");
179*41252Sbostic 	getlin(buf);
180*41252Sbostic 	if(!buf[0] || buf[0] == '\033') {
181*41252Sbostic 	    (void) strcpy(buf,"HACKOPTIONS=");
182*41252Sbostic 	    (void) strcat(buf, flags.female ? "female," : "male,");
183*41252Sbostic 	    if(flags.standout) (void) strcat(buf,"standout,");
184*41252Sbostic 	    if(flags.nonull) (void) strcat(buf,"nonull,");
185*41252Sbostic 	    if(flags.nonews) (void) strcat(buf,"nonews,");
186*41252Sbostic 	    if(flags.time) (void) strcat(buf,"time,");
187*41252Sbostic 	    if(flags.notombstone) (void) strcat(buf,"notombstone,");
188*41252Sbostic 	    if(flags.no_rest_on_space)
189*41252Sbostic 		(void) strcat(buf,"!rest_on_space,");
190*41252Sbostic 	    if(flags.end_top != 5 || flags.end_around != 4 || flags.end_own){
191*41252Sbostic 		(void) sprintf(eos(buf), "endgame: %u topscores/%u around me",
192*41252Sbostic 			flags.end_top, flags.end_around);
193*41252Sbostic 		if(flags.end_own) (void) strcat(buf, "/own scores");
194*41252Sbostic 	    } else {
195*41252Sbostic 		register char *eop = eos(buf);
196*41252Sbostic 		if(*--eop == ',') *eop = 0;
197*41252Sbostic 	    }
198*41252Sbostic 	    pline(buf);
199*41252Sbostic 	} else
200*41252Sbostic 	    parseoptions(buf, FALSE);
201*41252Sbostic 
202*41252Sbostic 	return(0);
203*41252Sbostic }
204