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