phantasia(6): Separate hostprog.
[dragonfly.git] / games / phantasia / setup.c
1 /*
2  * setup.c - set up all files for Phantasia
3  *
4  * $FreeBSD: src/games/phantasia/setup.c,v 1.11 1999/11/16 02:57:34 billf Exp $
5  */
6 /*#include "include.h"*/
7 #include <sys/param.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <math.h>
12 #include <setjmp.h>
13 #include <signal.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 #include "include.h"
20
21 void Error(const char *, const char *) __dead2;
22
23 static const char *const files[] = {            /* all files to create */
24         _SPATH_MONST,
25         _SPATH_PEOPLE,
26         _SPATH_MESS,
27         _SPATH_LASTDEAD,
28         _SPATH_MOTD,
29         _SPATH_GOLD,
30         _SPATH_VOID,
31         _SPATH_SCORE,
32         NULL,
33 };
34
35 const char *monsterfile = "monsters.asc";
36
37 /*
38  * FUNCTION: setup files for Phantasia 3.3.2
39  *
40  * GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
41  *
42  * GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
43  *
44  * DESCRIPTION:
45  *      This program tries to verify the parameters specified in
46  *      the Makefile.
47  *
48  *      Create all necessary files.  Note that nothing needs to be
49  *      put in these files.
50  *      Also, the monster binary data base is created here.
51  */
52
53 int
54 main(int argc, char *argv[])
55 {
56         const char *const *filename;    /* for pointing to file names */
57         int     fd;                     /* file descriptor */
58         FILE    *fp;                    /* for opening files */
59         struct stat     fbuf;           /* for getting files statistics */
60         int ch;
61
62         while ((ch = getopt(argc, argv, "m:")) != -1)
63                 switch(ch) {
64                 case 'm':
65                         monsterfile = optarg;
66                         break;
67                 case '?':
68                 default:
69                         break;
70                 }
71         argc -= optind;
72         argv += optind;
73
74         srandomdev();
75
76 #if 0
77         umask(0117);            /* only owner can read/write created files */
78 #endif
79
80         /* try to create data files */
81         filename = &files[0];
82         /* create each file */
83         while (*filename != NULL) {
84                 /* file exists; remove it */
85                 if (stat(*filename, &fbuf) == 0) {
86                         /* do not reset character file if it already exists */
87                         if (!strcmp(*filename, _SPATH_PEOPLE)) {
88                                 ++filename;
89                                 continue;
90                         }
91
92                         if (unlink(*filename) < 0)
93                                 Error("Cannot unlink %s.\n", *filename);
94                         /* NOTREACHED */
95                 }
96
97                 if ((fd = creat(*filename, 0666)) < 0)
98                         Error("Cannot create %s.\n", *filename);
99                 /* NOTREACHED */
100
101                 close(fd);                      /* close newly created file */
102
103                 ++filename;                     /* process next file */
104         }
105
106         /* put holy grail info into energy void file */
107         Enrgyvoid.ev_active = TRUE;
108         Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
109         Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
110         if ((fp = fopen(_SPATH_VOID, "w")) == NULL)
111                 Error("Cannot update %s.\n", _SPATH_VOID);
112         else {
113                 fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
114                 fclose(fp);
115         }
116
117         /* create binary monster data base */
118         if ((Monstfp = fopen(_SPATH_MONST, "w")) == NULL)
119                 Error("Cannot update %s.\n", _SPATH_MONST);
120         else if ((fp = fopen(monsterfile, "r")) == NULL) {
121                 fclose(Monstfp);
122                 Error("cannot open %s to create monster database.\n", "monsters.asc");
123         } else {
124                 Curmonster.m_o_strength =
125                     Curmonster.m_o_speed =
126                     Curmonster.m_maxspeed =
127                     Curmonster.m_o_energy =
128                     Curmonster.m_melee =
129                     Curmonster.m_skirmish = 0.0;
130
131                 /* read in text file, convert to binary */
132                 while (fgets(Databuf, SZ_DATABUF, fp) != NULL) {
133                         sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
134                             &Curmonster.m_strength, &Curmonster.m_brains,
135                             &Curmonster.m_speed, &Curmonster.m_energy,
136                             &Curmonster.m_experience, &Curmonster.m_treasuretype,
137                             &Curmonster.m_type, &Curmonster.m_flock);
138                         Databuf[24] = '\0';
139                         strcpy(Curmonster.m_name, Databuf);
140                         fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
141                 }
142                 fclose(fp);
143                 fclose(Monstfp);
144         }
145
146         exit(0);
147         /* NOTREACHED */
148 }
149
150 /*
151  * FUNCTION: print an error message, and exit
152  *
153  * ARGUMENTS:
154  *      char *str - format string for printf()
155  *      char *file - file which caused error
156  *
157  * GLOBAL INPUTS: _iob[]
158  *
159  * DESCRIPTION:
160  *      Print an error message, then exit.
161  */
162
163 void
164 Error(const char *str, const char *file)
165 {
166         fprintf(stderr, "Error: ");
167         fprintf(stderr, str, file);
168         perror(file);
169         exit(1);
170         /* NOTREACHED */
171 }