nrelease - fix/improve livecd
[dragonfly.git] / games / phantasia / setup.c
CommitLineData
984263bc
MD
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 */
e9634912 6/*#include "include.h"*/
7#include <sys/param.h>
984263bc
MD
8#include <sys/types.h>
9#include <sys/stat.h>
e9634912 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>
984263bc 17#include <unistd.h>
313fa7d1 18
e9634912 19#include "include.h"
20
4f91f2b6 21void Error(const char *, const char *) __dead2;
313fa7d1 22
4f91f2b6 23static const char *const files[] = { /* all files to create */
313fa7d1
SW
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
6693db17
SW
35const 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 */
984263bc 52
984263bc 53int
6693db17 54main(int argc, char *argv[])
984263bc 55{
4f91f2b6 56 const char *const *filename; /* for pointing to file names */
6693db17 57 int fd; /* file descriptor */
984263bc
MD
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
6693db17 74 srandomdev();
984263bc
MD
75
76#if 0
6693db17 77 umask(0117); /* only owner can read/write created files */
984263bc
MD
78#endif
79
6693db17
SW
80 /* try to create data files */
81 filename = &files[0];
984263bc 82 /* create each file */
6693db17
SW
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 */
984263bc
MD
95 }
96
6693db17
SW
97 if ((fd = creat(*filename, 0666)) < 0)
98 Error("Cannot create %s.\n", *filename);
99 /* NOTREACHED */
984263bc 100
6693db17 101 close(fd); /* close newly created file */
984263bc 102
6693db17 103 ++filename; /* process next file */
984263bc
MD
104 }
105
6693db17
SW
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);
984263bc
MD
115 }
116
6693db17
SW
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
984263bc 131 /* read in text file, convert to binary */
6693db17
SW
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);
984263bc 141 }
6693db17
SW
142 fclose(fp);
143 fclose(Monstfp);
984263bc
MD
144 }
145
6693db17
SW
146 exit(0);
147 /* NOTREACHED */
984263bc 148}
6693db17
SW
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 */
984263bc
MD
162
163void
313fa7d1 164Error(const char *str, const char *file)
984263bc 165{
6693db17
SW
166 fprintf(stderr, "Error: ");
167 fprintf(stderr, str, file);
168 perror(file);
169 exit(1);
170 /* NOTREACHED */
984263bc 171}