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 $
11 void Error(const char *, const char *) __dead2
;
13 static const char *const files
[] = { /* all files to create */
25 const char *monsterfile
= "monsters.asc";
28 * FUNCTION: setup files for Phantasia 3.3.2
30 * GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
32 * GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
35 * This program tries to verify the parameters specified in
38 * Create all necessary files. Note that nothing needs to be
40 * Also, the monster binary data base is created here.
44 main(int argc
, char *argv
[])
46 const char *const *filename
; /* for pointing to file names */
47 int fd
; /* file descriptor */
48 FILE *fp
; /* for opening files */
49 struct stat fbuf
; /* for getting files statistics */
52 while ((ch
= getopt(argc
, argv
, "m:")) != -1)
67 umask(0117); /* only owner can read/write created files */
70 /* try to create data files */
72 /* create each file */
73 while (*filename
!= NULL
) {
74 /* file exists; remove it */
75 if (stat(*filename
, &fbuf
) == 0) {
76 /* do not reset character file if it already exists */
77 if (!strcmp(*filename
, _SPATH_PEOPLE
)) {
82 if (unlink(*filename
) < 0)
83 Error("Cannot unlink %s.\n", *filename
);
87 if ((fd
= creat(*filename
, 0666)) < 0)
88 Error("Cannot create %s.\n", *filename
);
91 close(fd
); /* close newly created file */
93 ++filename
; /* process next file */
96 /* put holy grail info into energy void file */
97 Enrgyvoid
.ev_active
= TRUE
;
98 Enrgyvoid
.ev_x
= ROLL(-1.0e6
, 2.0e6
);
99 Enrgyvoid
.ev_y
= ROLL(-1.0e6
, 2.0e6
);
100 if ((fp
= fopen(_SPATH_VOID
, "w")) == NULL
)
101 Error("Cannot update %s.\n", _SPATH_VOID
);
103 fwrite(&Enrgyvoid
, SZ_VOIDSTRUCT
, 1, fp
);
107 /* create binary monster data base */
108 if ((Monstfp
= fopen(_SPATH_MONST
, "w")) == NULL
)
109 Error("Cannot update %s.\n", _SPATH_MONST
);
110 else if ((fp
= fopen(monsterfile
, "r")) == NULL
) {
112 Error("cannot open %s to create monster database.\n", "monsters.asc");
114 Curmonster
.m_o_strength
=
115 Curmonster
.m_o_speed
=
116 Curmonster
.m_maxspeed
=
117 Curmonster
.m_o_energy
=
119 Curmonster
.m_skirmish
= 0.0;
121 /* read in text file, convert to binary */
122 while (fgets(Databuf
, SZ_DATABUF
, fp
) != NULL
) {
123 sscanf(&Databuf
[24], "%lf%lf%lf%lf%lf%d%d%lf",
124 &Curmonster
.m_strength
, &Curmonster
.m_brains
,
125 &Curmonster
.m_speed
, &Curmonster
.m_energy
,
126 &Curmonster
.m_experience
, &Curmonster
.m_treasuretype
,
127 &Curmonster
.m_type
, &Curmonster
.m_flock
);
129 strcpy(Curmonster
.m_name
, Databuf
);
130 fwrite((char *) &Curmonster
, SZ_MONSTERSTRUCT
, 1, Monstfp
);
141 * FUNCTION: print an error message, and exit
144 * char *str - format string for printf()
145 * char *file - file which caused error
147 * GLOBAL INPUTS: _iob[]
150 * Print an error message, then exit.
154 Error(const char *str
, const char *file
)
156 fprintf(stderr
, "Error: ");
157 fprintf(stderr
, str
, file
);