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 $
6 /*#include "include.h"*/
21 void Error(const char *, const char *) __dead2;
23 static const char *const files[] = { /* all files to create */
35 const char *monsterfile = "monsters.asc";
38 * FUNCTION: setup files for Phantasia 3.3.2
40 * GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
42 * GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
45 * This program tries to verify the parameters specified in
48 * Create all necessary files. Note that nothing needs to be
50 * Also, the monster binary data base is created here.
54 main(int argc, char *argv[])
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 */
62 while ((ch = getopt(argc, argv, "m:")) != -1)
77 umask(0117); /* only owner can read/write created files */
80 /* try to create data files */
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)) {
92 if (unlink(*filename) < 0)
93 Error("Cannot unlink %s.\n", *filename);
97 if ((fd = creat(*filename, 0666)) < 0)
98 Error("Cannot create %s.\n", *filename);
101 close(fd); /* close newly created file */
103 ++filename; /* process next file */
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);
113 fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
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) {
122 Error("cannot open %s to create monster database.\n", "monsters.asc");
124 Curmonster.m_o_strength =
125 Curmonster.m_o_speed =
126 Curmonster.m_maxspeed =
127 Curmonster.m_o_energy =
129 Curmonster.m_skirmish = 0.0;
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);
139 strcpy(Curmonster.m_name, Databuf);
140 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
151 * FUNCTION: print an error message, and exit
154 * char *str - format string for printf()
155 * char *file - file which caused error
157 * GLOBAL INPUTS: _iob[]
160 * Print an error message, then exit.
164 Error(const char *str, const char *file)
166 fprintf(stderr, "Error: ");
167 fprintf(stderr, str, file);