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