2 * setup.c - set up all files for Phantasia
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 $
12 /* functions which we need to know about */
14 extern double drandom(void);
16 void Error(const char *, const char *);
18 static const char *files[] = { /* all files to create */
30 const char *monsterfile = "monsters.asc";
33 * FUNCTION: setup files for Phantasia 3.3.2
35 * GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
37 * GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
40 * This program tries to verify the parameters specified in
43 * Create all necessary files. Note that nothing needs to be
45 * Also, the monster binary data base is created here.
49 main(int argc, char *argv[])
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 */
57 while ((ch = getopt(argc, argv, "m:")) != -1)
72 umask(0117); /* only owner can read/write created files */
75 /* try to create data files */
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)) {
87 if (unlink(*filename) < 0)
88 Error("Cannot unlink %s.\n", *filename);
92 if ((fd = creat(*filename, 0666)) < 0)
93 Error("Cannot create %s.\n", *filename);
96 close(fd); /* close newly created file */
98 ++filename; /* process next file */
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);
108 fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
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) {
117 Error("cannot open %s to create monster database.\n", "monsters.asc");
119 Curmonster.m_o_strength =
120 Curmonster.m_o_speed =
121 Curmonster.m_maxspeed =
122 Curmonster.m_o_energy =
124 Curmonster.m_skirmish = 0.0;
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);
134 strcpy(Curmonster.m_name, Databuf);
135 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
146 * FUNCTION: print an error message, and exit
149 * char *str - format string for printf()
150 * char *file - file which caused error
152 * GLOBAL INPUTS: _iob[]
155 * Print an error message, then exit.
159 Error(const char *str, const char *file)
161 fprintf(stderr, "Error: ");
162 fprintf(stderr, str, file);