xref: /csrg-svn/games/phantasia/setup.c (revision 56081)
131409Sbostic /*
231409Sbostic  * setup.c - set up all files for Phantasia
331409Sbostic  */
431409Sbostic #include "include.h"
531409Sbostic #include <sys/types.h>
631409Sbostic #include <sys/stat.h>
7*56081Sbostic #include <stdlib.h>
831409Sbostic /**/
931409Sbostic /************************************************************************
1031409Sbostic /
1131409Sbostic / FUNCTION NAME: main()
1231409Sbostic /
1331409Sbostic / FUNCTION: setup files for Phantasia 3.3.2
1431409Sbostic /
1531409Sbostic / AUTHOR: E. A. Estes, 12/4/85
1631409Sbostic /
1731409Sbostic / ARGUMENTS: none
1831409Sbostic /
1931409Sbostic / RETURN VALUE: none
2031409Sbostic /
2131409Sbostic / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
2231409Sbostic /	fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
2331409Sbostic /	unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
2431409Sbostic /
2540279Sbostic / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
2631409Sbostic /
2731409Sbostic / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
2831409Sbostic /
2931409Sbostic / DESCRIPTION:
3031409Sbostic /
3131409Sbostic /	This program tries to verify the parameters specified in
3231409Sbostic /	the Makefile.
3331409Sbostic /
3431409Sbostic /	Create all necessary files.  Note that nothing needs to be
3531409Sbostic /	put in these files.
3631409Sbostic /	Also, the monster binary data base is created here.
3731409Sbostic /
3831409Sbostic /************************************************************************/
3931409Sbostic 
40*56081Sbostic static char *files[] = {		/* all files to create */
4140279Sbostic 	_PATH_MONST,
4240279Sbostic 	_PATH_PEOPLE,
4340279Sbostic 	_PATH_MESS,
4440279Sbostic 	_PATH_LASTDEAD,
4540279Sbostic 	_PATH_MOTD,
4640279Sbostic 	_PATH_GOLD,
4740279Sbostic 	_PATH_VOID,
4840279Sbostic 	_PATH_SCORE,
49*56081Sbostic 	NULL,
50*56081Sbostic };
5131409Sbostic 
52*56081Sbostic char *monsterfile="monsters.asc";
53*56081Sbostic 
54*56081Sbostic int
55*56081Sbostic main(argc, argv)
56*56081Sbostic 	int argc;
57*56081Sbostic 	char *argv[];
58*56081Sbostic {
59*56081Sbostic 	register char	**filename;	/* for pointing to file names */
60*56081Sbostic 	register int	fd;		/* file descriptor */
61*56081Sbostic 	FILE	*fp;			/* for opening files */
62*56081Sbostic 	struct stat	fbuf;		/* for getting files statistics */
63*56081Sbostic 	int ch;
64*56081Sbostic 
65*56081Sbostic 	while ((ch = getopt(argc, argv, "m:")) != EOF)
66*56081Sbostic 		switch(ch) {
67*56081Sbostic 		case 'm':
68*56081Sbostic 			monsterfile = optarg;
69*56081Sbostic 			break;
70*56081Sbostic 		case '?':
71*56081Sbostic 		default:
72*56081Sbostic 			break;
73*56081Sbostic 		}
74*56081Sbostic 	argc -= optind;
75*56081Sbostic 	argv += optind;
76*56081Sbostic 
7731409Sbostic     srandom((unsigned) time((long *) NULL));	/* prime random numbers */
7831409Sbostic 
7931409Sbostic     umask(0117);		/* only owner can read/write created files */
8031409Sbostic 
8131409Sbostic     /* try to create data files */
8231409Sbostic     filename = &files[0];
8331409Sbostic     while (*filename != NULL)
8431409Sbostic 	/* create each file */
8531409Sbostic 	{
8631409Sbostic 	if (stat(*filename, &fbuf) == 0)
8731409Sbostic 	    /* file exists; remove it */
8831409Sbostic 	    {
8940279Sbostic 	    if (!strcmp(*filename, _PATH_PEOPLE))
9031409Sbostic 		/* do not reset character file if it already exists */
9131409Sbostic 		{
9231409Sbostic 		++filename;
9331409Sbostic 		continue;
9431409Sbostic 		}
9531409Sbostic 
9631409Sbostic 	    if (unlink(*filename) < 0)
9731409Sbostic 		Error("Cannot unlink %s.\n", *filename);
9831409Sbostic 		/*NOTREACHED*/
9931409Sbostic 	    }
10031409Sbostic 
10131409Sbostic 	if ((fd = creat(*filename, 0660)) < 0)
10231409Sbostic 	    Error("Cannot create %s.\n", *filename);
10331409Sbostic 	    /*NOTREACHED*/
10431409Sbostic 
10531409Sbostic 	close(fd);			/* close newly created file */
10631409Sbostic 
10731409Sbostic 	++filename;			/* process next file */
10831409Sbostic 	}
10931409Sbostic 
11031409Sbostic     /* put holy grail info into energy void file */
11131409Sbostic     Enrgyvoid.ev_active = TRUE;
11231409Sbostic     Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
11331409Sbostic     Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
11440279Sbostic     if ((fp = fopen(_PATH_VOID, "w")) == NULL)
11540279Sbostic 	Error("Cannot update %s.\n", _PATH_VOID);
11631409Sbostic     else
11731409Sbostic 	{
11831409Sbostic 	fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
11931409Sbostic 	fclose(fp);
12031409Sbostic 	}
12131409Sbostic 
12231409Sbostic     /* create binary monster data base */
12340279Sbostic     if ((Monstfp = fopen(_PATH_MONST, "w")) == NULL)
12440279Sbostic 	Error("Cannot update %s.\n", _PATH_MONST);
12531409Sbostic     else
12631409Sbostic 	{
127*56081Sbostic 	if ((fp = fopen(monsterfile, "r")) == NULL)
12831409Sbostic 	    {
12931409Sbostic 	    fclose(Monstfp);
13031409Sbostic 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
13131409Sbostic 	    }
13231409Sbostic 	else
13331409Sbostic 	    {
13431409Sbostic 	    Curmonster.m_o_strength =
13531409Sbostic 	    Curmonster.m_o_speed =
13631409Sbostic 	    Curmonster.m_maxspeed =
13731409Sbostic 	    Curmonster.m_o_energy =
13831409Sbostic 	    Curmonster.m_melee =
13931409Sbostic 	    Curmonster.m_skirmish = 0.0;
14031409Sbostic 
14131409Sbostic 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
14231409Sbostic 		/* read in text file, convert to binary */
14331409Sbostic 		{
14434613Sbostic 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
14531409Sbostic 		    &Curmonster.m_strength, &Curmonster.m_brains,
14631409Sbostic 		    &Curmonster.m_speed, &Curmonster.m_energy,
14731409Sbostic 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
14831409Sbostic 		    &Curmonster.m_type, &Curmonster.m_flock);
14931409Sbostic 		Databuf[24] = '\0';
15031409Sbostic 		strcpy(Curmonster.m_name, Databuf);
15131409Sbostic 		fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
15231409Sbostic 		}
15331409Sbostic 	    fclose(fp);
15431409Sbostic 	    fclose(Monstfp);
15531409Sbostic 	    }
15631409Sbostic 	}
15731409Sbostic 
15837055Sbostic #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
15931409Sbostic     /* write to motd file */
16031409Sbostic     printf("One line 'motd' ? ");
16131409Sbostic     if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
16231409Sbostic 	Databuf[0] = '\0';
16340279Sbostic     if ((fp = fopen(_PATH_MOTD, "w")) == NULL)
16440279Sbostic 	Error("Cannot update %s.\n", _PATH_MOTD);
16531409Sbostic     else
16631409Sbostic 	{
16731409Sbostic 	fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
16831409Sbostic 	fclose(fp);
16931409Sbostic 	}
17031409Sbostic 
17131409Sbostic     /* report compile-time options */
17231409Sbostic     printf("Compiled options:\n\n");
17340279Sbostic     printf("Phantasia destination directory:  %s\n", _PATH_PHANTDIR);
17441317Sbostic     printf("Wizard: root UID: 0\n");
17531409Sbostic 
17631409Sbostic #ifdef BSD41
17731409Sbostic     printf("Compiled for BSD 4.1\n");
17831409Sbostic #endif
17931409Sbostic 
18031409Sbostic #ifdef BSD42
18131409Sbostic     printf("Compiled for BSD 4.2\n");
18231409Sbostic #endif
18331409Sbostic 
18431409Sbostic #ifdef SYS3
18531409Sbostic     printf("Compiled for System III\n");
18631409Sbostic #endif
18731409Sbostic 
18831409Sbostic #ifdef SYS5
18931409Sbostic     printf("Compiled for System V\n");
19031409Sbostic #endif
19137055Sbostic #endif
19231409Sbostic 
19331409Sbostic     exit(0);
19431409Sbostic     /*NOTREACHED*/
19531409Sbostic }
19631409Sbostic /**/
19731409Sbostic /************************************************************************
19831409Sbostic /
19931409Sbostic / FUNCTION NAME: Error()
20031409Sbostic /
20131409Sbostic / FUNCTION: print an error message, and exit
20231409Sbostic /
20331409Sbostic / AUTHOR: E. A. Estes, 12/4/85
20431409Sbostic /
20531409Sbostic / ARGUMENTS:
20631409Sbostic /	char *str - format string for printf()
20731409Sbostic /	char *file - file which caused error
20831409Sbostic /
20931409Sbostic / RETURN VALUE: none
21031409Sbostic /
21131409Sbostic / MODULES CALLED: exit(), perror(), fprintf()
21231409Sbostic /
21331409Sbostic / GLOBAL INPUTS: _iob[]
21431409Sbostic /
21531409Sbostic / GLOBAL OUTPUTS: none
21631409Sbostic /
21731409Sbostic / DESCRIPTION:
21831409Sbostic /	Print an error message, then exit.
21931409Sbostic /
22031409Sbostic /************************************************************************/
22131409Sbostic 
22231409Sbostic Error(str, file)
22331409Sbostic char	*str, *file;
22431409Sbostic {
22531409Sbostic     fprintf(stderr, "Error: ");
22631409Sbostic     fprintf(stderr, str, file);
22731409Sbostic     perror(file);
22831409Sbostic     exit(1);
22931409Sbostic     /*NOTREACHED*/
23031409Sbostic }
23131409Sbostic /**/
23231409Sbostic /************************************************************************
23331409Sbostic /
23431409Sbostic / FUNCTION NAME: drandom()
23531409Sbostic /
23631409Sbostic / FUNCTION: return a random number
23731409Sbostic /
23831409Sbostic / AUTHOR: E. A. Estes, 2/7/86
23931409Sbostic /
24031409Sbostic / ARGUMENTS: none
24131409Sbostic /
24231409Sbostic / RETURN VALUE: none
24331409Sbostic /
24431409Sbostic / MODULES CALLED: random()
24531409Sbostic /
24631409Sbostic / GLOBAL INPUTS: none
24731409Sbostic /
24831409Sbostic / GLOBAL OUTPUTS: none
24931409Sbostic /
25031409Sbostic / DESCRIPTION:
25131409Sbostic /
25231409Sbostic /************************************************************************/
25331409Sbostic 
25431409Sbostic double
25531409Sbostic drandom()
25631409Sbostic {
25731409Sbostic     if (sizeof(int) != 2)
25831409Sbostic 	return((double) (random() & 0x7fff) / 32768.0);
25931409Sbostic     else
26031409Sbostic 	return((double) random() / 32768.0);
26131409Sbostic }
262