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