xref: /openbsd/games/adventure/wizard.c (revision d485f761)
1 /*	$OpenBSD: wizard.c,v 1.8 2001/03/22 22:31:47 pjanzen Exp $	*/
2 /*	$NetBSD: wizard.c,v 1.3 1995/04/24 12:21:41 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * The game adventure was originally written in Fortran by Will Crowther
9  * and Don Woods.  It was later translated to C and enhanced by Jim
10  * Gillogly.  This code is derived from software contributed to Berkeley
11  * by Jim Gillogly at The Rand Corporation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)wizard.c	8.1 (Berkeley) 6/2/93";
45 #else
46 static char rcsid[] = "$OpenBSD: wizard.c,v 1.8 2001/03/22 22:31:47 pjanzen Exp $";
47 #endif
48 #endif /* not lint */
49 
50 /*	Re-coding of advent in C: privileged operations			*/
51 
52 #include <sys/param.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <stdlib.h>
56 #include <time.h>
57 #include "hdr.h"
58 #include "extern.h"
59 
60 #if 0
61 void
62 datime(d, t)
63 	int    *d, *t;
64 {
65 	time_t  tvec;
66 	struct tm *tptr;
67 
68 	time(&tvec);
69 	tptr = localtime(&tvec);
70 	/* day since 1977  (mod leap)   */
71 	*d = tptr->tm_yday + 365 * (tptr->tm_year - 77);
72 	/* bug: this will overflow in the year 2066 AD                  */
73 	/* it will be attributed to Wm the C's millenial celebration    */
74 	/* and minutes since midnite */
75 	*t = tptr->tm_hour * 60 + tptr->tm_min;
76 }				/* pretty painless              */
77 #endif
78 
79 char    magic[6];
80 
81 void
82 poof()
83 {
84 	strcpy(magic, DECR(d,w,a,r,f));
85 	latncy = 45;
86 }
87 
88 int
89 Start()
90 {
91 	time_t  t, delay;
92 
93 	time(&t);
94 	delay = (t - savet) / 60;	/* Minutes	*/
95 	saved = -1;
96 
97 	if (delay >= latncy)
98 		return (FALSE);
99 	printf("This adventure was suspended a mere %d minute%s ago.",
100 		delay, delay == 1 ? "" : "s");
101 	if (delay <= latncy / 3) {
102 		mspeak(2);
103 		exit(0);
104 	}
105 	mspeak(8);
106 	if (!wizard()) {
107 		mspeak(9);
108 		exit(0);
109 	}
110 	return (FALSE);
111 }
112 
113 int
114 wizard()		/* not as complex as advent/10 (for now)	*/
115 {
116 	char   *word, *x;
117 
118 	if (!yesm(16, 0, 7))
119 		return (FALSE);
120 	mspeak(17);
121 	getin(&word, &x);
122 	if (!weq(word, magic)) {
123 		mspeak(20);
124 		return (FALSE);
125 	}
126 	mspeak(19);
127 	return (TRUE);
128 }
129 
130 void
131 ciao()
132 {
133 	char   *c;
134 	char    fname[MAXPATHLEN];
135 
136 	printf("What would you like to call the saved version?\n");
137 	for (c = fname; c - fname < MAXPATHLEN; c++)
138 		if ((*c = getchar()) == '\n' || *c == EOF)
139 			break;
140 	if (c - fname == MAXPATHLEN) {
141 		c--;
142 		FLUSHLINE;
143 	}
144 	*c = '\0';
145 	if (save(fname) != 0)
146 		return;		/* Save failed */
147 	printf("To resume, say \"adventure %s\".\n", fname);
148 	printf("\"With these rooms I might now have been familiarly acquainted.\"\n");
149 	exit(0);
150 }
151 
152 
153 int
154 ran(range)
155 	int     range;
156 {
157 	long    i;
158 
159 	i = random() % range;
160 	return (i);
161 }
162