xref: /dragonfly/games/phantasia/setup.c (revision 0bb9290e)
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  * $DragonFly: src/games/phantasia/setup.c,v 1.3 2005/05/31 00:06:26 swildner Exp $
6  */
7 #include "include.h"
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 
12 /* functions which we need to know about */
13 /* phantglobs.c */
14 extern	double	drandom(void);
15 
16 void	Error(const char *, const char *);
17 
18 static const char *files[] = {		/* all files to create */
19 	_SPATH_MONST,
20 	_SPATH_PEOPLE,
21 	_SPATH_MESS,
22 	_SPATH_LASTDEAD,
23 	_SPATH_MOTD,
24 	_SPATH_GOLD,
25 	_SPATH_VOID,
26 	_SPATH_SCORE,
27 	NULL,
28 };
29 
30 const char *monsterfile="monsters.asc";
31 
32 /**/
33 /************************************************************************
34 /
35 / FUNCTION NAME: main()
36 /
37 / FUNCTION: setup files for Phantasia 3.3.2
38 /
39 / AUTHOR: E. A. Estes, 12/4/85
40 /
41 / ARGUMENTS: none
42 /
43 / RETURN VALUE: none
44 /
45 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
46 /	fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
47 /	unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
48 /
49 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
50 /
51 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
52 /
53 / DESCRIPTION:
54 /
55 /	This program tries to verify the parameters specified in
56 /	the Makefile.
57 /
58 /	Create all necessary files.  Note that nothing needs to be
59 /	put in these files.
60 /	Also, the monster binary data base is created here.
61 /
62 *************************************************************************/
63 
64 int
65 main(int argc, char **argv)
66 {
67 	const	char	**filename;	/* for pointing to file names */
68 	int	fd;		/* file descriptor */
69 	FILE	*fp;			/* for opening files */
70 	struct stat	fbuf;		/* for getting files statistics */
71 	int ch;
72 
73 	while ((ch = getopt(argc, argv, "m:")) != -1)
74 		switch(ch) {
75 		case 'm':
76 			monsterfile = optarg;
77 			break;
78 		case '?':
79 		default:
80 			break;
81 		}
82 	argc -= optind;
83 	argv += optind;
84 
85     srandomdev();
86 
87 #if 0
88     umask(0117);		/* only owner can read/write created files */
89 #endif
90 
91     /* try to create data files */
92     filename = &files[0];
93     while (*filename != NULL)
94 	/* create each file */
95 	{
96 	if (stat(*filename, &fbuf) == 0)
97 	    /* file exists; remove it */
98 	    {
99 	    if (!strcmp(*filename, _SPATH_PEOPLE))
100 		/* do not reset character file if it already exists */
101 		{
102 		++filename;
103 		continue;
104 		}
105 
106 	    if (unlink(*filename) < 0)
107 		Error("Cannot unlink %s.\n", *filename);
108 		/*NOTREACHED*/
109 	    }
110 
111 	if ((fd = creat(*filename, 0666)) < 0)
112 	    Error("Cannot create %s.\n", *filename);
113 	    /*NOTREACHED*/
114 
115 	close(fd);			/* close newly created file */
116 
117 	++filename;			/* process next file */
118 	}
119 
120     /* put holy grail info into energy void file */
121     Enrgyvoid.ev_active = TRUE;
122     Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
123     Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
124     if ((fp = fopen(_SPATH_VOID, "w")) == NULL)
125 	Error("Cannot update %s.\n", _SPATH_VOID);
126     else
127 	{
128 	fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
129 	fclose(fp);
130 	}
131 
132     /* create binary monster data base */
133     if ((Monstfp = fopen(_SPATH_MONST, "w")) == NULL)
134 	Error("Cannot update %s.\n", _SPATH_MONST);
135     else
136 	{
137 	if ((fp = fopen(monsterfile, "r")) == NULL)
138 	    {
139 	    fclose(Monstfp);
140 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
141 	    }
142 	else
143 	    {
144 	    Curmonster.m_o_strength =
145 	    Curmonster.m_o_speed =
146 	    Curmonster.m_maxspeed =
147 	    Curmonster.m_o_energy =
148 	    Curmonster.m_melee =
149 	    Curmonster.m_skirmish = 0.0;
150 
151 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
152 		/* read in text file, convert to binary */
153 		{
154 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
155 		    &Curmonster.m_strength, &Curmonster.m_brains,
156 		    &Curmonster.m_speed, &Curmonster.m_energy,
157 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
158 		    &Curmonster.m_type, &Curmonster.m_flock);
159 		Databuf[24] = '\0';
160 		strcpy(Curmonster.m_name, Databuf);
161 		fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
162 		}
163 	    fclose(fp);
164 	    fclose(Monstfp);
165 	    }
166 	}
167 
168 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
169     /* write to motd file */
170     printf("One line 'motd' ? ");
171     if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
172 	Databuf[0] = '\0';
173     if ((fp = fopen(_SPATH_MOTD, "w")) == NULL)
174 	Error("Cannot update %s.\n", _SPATH_MOTD);
175     else
176 	{
177 	fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
178 	fclose(fp);
179 	}
180 
181     /* report compile-time options */
182     printf("Compiled options:\n\n");
183     printf("Phantasia destination directory:  %s\n", _SPATH_PHANTDIR);
184     printf("Wizard: root UID: 0\n");
185 
186 #ifdef BSD41
187     printf("Compiled for BSD 4.1\n");
188 #endif
189 
190 #ifdef BSD42
191     printf("Compiled for BSD 4.2\n");
192 #endif
193 
194 #ifdef SYS3
195     printf("Compiled for System III\n");
196 #endif
197 
198 #ifdef SYS5
199     printf("Compiled for System V\n");
200 #endif
201 #endif
202 
203     exit(0);
204     /*NOTREACHED*/
205 }
206 /**/
207 /************************************************************************
208 /
209 / FUNCTION NAME: Error()
210 /
211 / FUNCTION: print an error message, and exit
212 /
213 / AUTHOR: E. A. Estes, 12/4/85
214 /
215 / ARGUMENTS:
216 /	char *str - format string for printf()
217 /	char *file - file which caused error
218 /
219 / RETURN VALUE: none
220 /
221 / MODULES CALLED: exit(), perror(), fprintf()
222 /
223 / GLOBAL INPUTS: _iob[]
224 /
225 / GLOBAL OUTPUTS: none
226 /
227 / DESCRIPTION:
228 /	Print an error message, then exit.
229 /
230 *************************************************************************/
231 
232 void
233 Error(const char *str, const char *file)
234 {
235     fprintf(stderr, "Error: ");
236     fprintf(stderr, str, file);
237     perror(file);
238     exit(1);
239     /*NOTREACHED*/
240 }
241