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