]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - games/phantasia/setup.c
This commit was generated by cvs2svn to compensate for changes in r69159,
[FreeBSD/FreeBSD.git] / games / phantasia / setup.c
1 /*
2  * setup.c - set up all files for Phantasia
3  *
4  * $FreeBSD$
5  */
6 #include "include.h"
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 /*\f*/
11 /************************************************************************
12 /
13 / FUNCTION NAME: main()
14 /
15 / FUNCTION: setup files for Phantasia 3.3.2
16 /
17 / AUTHOR: E. A. Estes, 12/4/85
18 /
19 / ARGUMENTS: none
20 /
21 / RETURN VALUE: none
22 /
23 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
24 /       fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
25 /       unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
26 /
27 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
28 /
29 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
30 /
31 / DESCRIPTION:
32 /
33 /       This program tries to verify the parameters specified in
34 /       the Makefile.
35 /
36 /       Create all necessary files.  Note that nothing needs to be
37 /       put in these files.
38 /       Also, the monster binary data base is created here.
39 /
40 *************************************************************************/
41
42 void Error();
43
44 static char *files[] = {                /* all files to create */
45         _SPATH_MONST,
46         _SPATH_PEOPLE,
47         _SPATH_MESS,
48         _SPATH_LASTDEAD,
49         _SPATH_MOTD,
50         _SPATH_GOLD,
51         _SPATH_VOID,
52         _SPATH_SCORE,
53         NULL,
54 };
55
56 char *monsterfile="monsters.asc";
57
58 int
59 main(argc, argv)
60         int argc;
61         char *argv[];
62 {
63         char    **filename;     /* for pointing to file names */
64         register int    fd;             /* file descriptor */
65         FILE    *fp;                    /* for opening files */
66         struct stat     fbuf;           /* for getting files statistics */
67         int ch;
68
69         while ((ch = getopt(argc, argv, "m:")) != -1)
70                 switch(ch) {
71                 case 'm':
72                         monsterfile = optarg;
73                         break;
74                 case '?':
75                 default:
76                         break;
77                 }
78         argc -= optind;
79         argv += optind;
80
81     srandomdev();
82
83 #if 0
84     umask(0117);                /* only owner can read/write created files */
85 #endif
86
87     /* try to create data files */
88     filename = &files[0];
89     while (*filename != NULL)
90         /* create each file */
91         {
92         if (stat(*filename, &fbuf) == 0)
93             /* file exists; remove it */
94             {
95             if (!strcmp(*filename, _SPATH_PEOPLE))
96                 /* do not reset character file if it already exists */
97                 {
98                 ++filename;
99                 continue;
100                 }
101
102             if (unlink(*filename) < 0)
103                 Error("Cannot unlink %s.\n", *filename);
104                 /*NOTREACHED*/
105             }
106
107         if ((fd = creat(*filename, 0666)) < 0)
108             Error("Cannot create %s.\n", *filename);
109             /*NOTREACHED*/
110
111         close(fd);                      /* close newly created file */
112
113         ++filename;                     /* process next file */
114         }
115
116     /* put holy grail info into energy void file */
117     Enrgyvoid.ev_active = TRUE;
118     Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
119     Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
120     if ((fp = fopen(_SPATH_VOID, "w")) == NULL)
121         Error("Cannot update %s.\n", _SPATH_VOID);
122     else
123         {
124         fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
125         fclose(fp);
126         }
127
128     /* create binary monster data base */
129     if ((Monstfp = fopen(_SPATH_MONST, "w")) == NULL)
130         Error("Cannot update %s.\n", _SPATH_MONST);
131     else
132         {
133         if ((fp = fopen(monsterfile, "r")) == NULL)
134             {
135             fclose(Monstfp);
136             Error("cannot open %s to create monster database.\n", "monsters.asc");
137             }
138         else
139             {
140             Curmonster.m_o_strength =
141             Curmonster.m_o_speed =
142             Curmonster.m_maxspeed =
143             Curmonster.m_o_energy =
144             Curmonster.m_melee =
145             Curmonster.m_skirmish = 0.0;
146
147             while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
148                 /* read in text file, convert to binary */
149                 {
150                 sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
151                     &Curmonster.m_strength, &Curmonster.m_brains,
152                     &Curmonster.m_speed, &Curmonster.m_energy,
153                     &Curmonster.m_experience, &Curmonster.m_treasuretype,
154                     &Curmonster.m_type, &Curmonster.m_flock);
155                 Databuf[24] = '\0';
156                 strcpy(Curmonster.m_name, Databuf);
157                 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
158                 }
159             fclose(fp);
160             fclose(Monstfp);
161             }
162         }
163
164 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
165     /* write to motd file */
166     printf("One line 'motd' ? ");
167     if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
168         Databuf[0] = '\0';
169     if ((fp = fopen(_SPATH_MOTD, "w")) == NULL)
170         Error("Cannot update %s.\n", _SPATH_MOTD);
171     else
172         {
173         fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
174         fclose(fp);
175         }
176
177     /* report compile-time options */
178     printf("Compiled options:\n\n");
179     printf("Phantasia destination directory:  %s\n", _SPATH_PHANTDIR);
180     printf("Wizard: root UID: 0\n");
181
182 #ifdef BSD41
183     printf("Compiled for BSD 4.1\n");
184 #endif
185
186 #ifdef BSD42
187     printf("Compiled for BSD 4.2\n");
188 #endif
189
190 #ifdef SYS3
191     printf("Compiled for System III\n");
192 #endif
193
194 #ifdef SYS5
195     printf("Compiled for System V\n");
196 #endif
197 #endif
198
199     exit(0);
200     /*NOTREACHED*/
201 }
202 /*\f*/
203 /************************************************************************
204 /
205 / FUNCTION NAME: Error()
206 /
207 / FUNCTION: print an error message, and exit
208 /
209 / AUTHOR: E. A. Estes, 12/4/85
210 /
211 / ARGUMENTS:
212 /       char *str - format string for printf()
213 /       char *file - file which caused error
214 /
215 / RETURN VALUE: none
216 /
217 / MODULES CALLED: exit(), perror(), fprintf()
218 /
219 / GLOBAL INPUTS: _iob[]
220 /
221 / GLOBAL OUTPUTS: none
222 /
223 / DESCRIPTION:
224 /       Print an error message, then exit.
225 /
226 *************************************************************************/
227
228 void
229 Error(str, file)
230 char    *str, *file;
231 {
232     fprintf(stderr, "Error: ");
233     fprintf(stderr, str, file);
234     perror(file);
235     exit(1);
236     /*NOTREACHED*/
237 }
238 /*\f*/
239 /************************************************************************
240 /
241 / FUNCTION NAME: drandom()
242 /
243 / FUNCTION: return a random number
244 /
245 / AUTHOR: E. A. Estes, 2/7/86
246 /
247 / ARGUMENTS: none
248 /
249 / RETURN VALUE: none
250 /
251 / MODULES CALLED: random()
252 /
253 / GLOBAL INPUTS: none
254 /
255 / GLOBAL OUTPUTS: none
256 /
257 / DESCRIPTION:
258 /
259 *************************************************************************/
260
261 double
262 drandom()
263 {
264     if (sizeof(int) != 2)
265         return((double) (random() & 0x7fff) / 32768.0);
266     else
267         return((double) random() / 32768.0);
268 }