xref: /netbsd/games/adventure/wizard.c (revision abca1099)
1*abca1099Srillig /*	$NetBSD: wizard.c,v 1.17 2021/05/02 12:50:43 rillig Exp $	*/
243b95251Scgd 
3107c0a28Sjtc /*-
4107c0a28Sjtc  * Copyright (c) 1991, 1993
5107c0a28Sjtc  *	The Regents of the University of California.  All rights reserved.
6107c0a28Sjtc  *
7107c0a28Sjtc  * The game adventure was originally written in Fortran by Will Crowther
8107c0a28Sjtc  * and Don Woods.  It was later translated to C and enhanced by Jim
9107c0a28Sjtc  * Gillogly.  This code is derived from software contributed to Berkeley
10107c0a28Sjtc  * by Jim Gillogly at The Rand Corporation.
11107c0a28Sjtc  *
12107c0a28Sjtc  * Redistribution and use in source and binary forms, with or without
13107c0a28Sjtc  * modification, are permitted provided that the following conditions
14107c0a28Sjtc  * are met:
15107c0a28Sjtc  * 1. Redistributions of source code must retain the above copyright
16107c0a28Sjtc  *    notice, this list of conditions and the following disclaimer.
17107c0a28Sjtc  * 2. Redistributions in binary form must reproduce the above copyright
18107c0a28Sjtc  *    notice, this list of conditions and the following disclaimer in the
19107c0a28Sjtc  *    documentation and/or other materials provided with the distribution.
20e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
21107c0a28Sjtc  *    may be used to endorse or promote products derived from this software
22107c0a28Sjtc  *    without specific prior written permission.
23107c0a28Sjtc  *
24107c0a28Sjtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25107c0a28Sjtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26107c0a28Sjtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27107c0a28Sjtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28107c0a28Sjtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29107c0a28Sjtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30107c0a28Sjtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31107c0a28Sjtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32107c0a28Sjtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33107c0a28Sjtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34107c0a28Sjtc  * SUCH DAMAGE.
35107c0a28Sjtc  */
36107c0a28Sjtc 
37690bee10Schristos #include <sys/cdefs.h>
38107c0a28Sjtc #ifndef lint
3943b95251Scgd #if 0
40107c0a28Sjtc static char sccsid[] = "@(#)wizard.c	8.1 (Berkeley) 6/2/93";
4143b95251Scgd #else
42*abca1099Srillig __RCSID("$NetBSD: wizard.c,v 1.17 2021/05/02 12:50:43 rillig Exp $");
4343b95251Scgd #endif
44107c0a28Sjtc #endif				/* not lint */
45107c0a28Sjtc 
46107c0a28Sjtc /*      Re-coding of advent in C: privileged operations                 */
47107c0a28Sjtc 
48690bee10Schristos #include <stdio.h>
49c4816c32Scgd #include <string.h>
50690bee10Schristos #include <stdlib.h>
51690bee10Schristos #include <time.h>
52690bee10Schristos #include "hdr.h"
53690bee10Schristos #include "extern.h"
54107c0a28Sjtc 
55c62bf84cSdholland static int wizard(void);
56c62bf84cSdholland 
57690bee10Schristos void
datime(int * d,int * t)58243d04efSjmc datime(int *d, int *t)
5984f80fd6Slukem {
6084f80fd6Slukem 	time_t  tvec;
61690bee10Schristos 	struct tm *tptr;
62107c0a28Sjtc 
63690bee10Schristos 	time(&tvec);
64690bee10Schristos 	tptr = localtime(&tvec);
65690bee10Schristos 	/* day since 1977  (mod leap)   */
66c81bbf32Shubertf 	*d = (tptr->tm_yday + 365 * (tptr->tm_year - 77)
67c81bbf32Shubertf              + (tptr->tm_year - 77) / 4 - (tptr->tm_year - 1) / 100
68c81bbf32Shubertf              + (tptr->tm_year + 299) / 400);
69c81bbf32Shubertf 	/* bug: this will overflow in the year 2066 AD (with 16 bit int) */
70107c0a28Sjtc 	/* it will be attributed to Wm the C's millenial celebration    */
71690bee10Schristos 	/* and minutes since midnite */
72690bee10Schristos 	*t = tptr->tm_hour * 60 + tptr->tm_min;
73107c0a28Sjtc }				/* pretty painless              */
74107c0a28Sjtc 
75107c0a28Sjtc 
76c62bf84cSdholland static char magic[6];
77107c0a28Sjtc 
78690bee10Schristos void
poof(void)79243d04efSjmc poof(void)
80107c0a28Sjtc {
81690bee10Schristos 	strcpy(magic, DECR('d', 'w', 'a', 'r', 'f'));
82aa19a9d0Sdholland 	latency = 45;
83107c0a28Sjtc }
84107c0a28Sjtc 
85690bee10Schristos int
Start(void)86243d04efSjmc Start(void)
8784f80fd6Slukem {
8884f80fd6Slukem 	int     d, t, delay;
89107c0a28Sjtc 
90107c0a28Sjtc 	datime(&d, &t);
91adf74b1eShubertf 	delay = (d - saveday) * 1440 + (t - savet);	/* good for about a
9284f80fd6Slukem 							 * month     */
93107c0a28Sjtc 
94aa19a9d0Sdholland 	if (delay >= latency) {
9584f80fd6Slukem 		saved = -1;
96107c0a28Sjtc 		return (FALSE);
97107c0a28Sjtc 	}
98107c0a28Sjtc 	printf("This adventure was suspended a mere %d minute%s ago.",
99107c0a28Sjtc 	    delay, delay == 1 ? "" : "s");
100aa19a9d0Sdholland 	if (delay <= latency / 3) {
10184f80fd6Slukem 		mspeak(2);
102107c0a28Sjtc 		exit(0);
103107c0a28Sjtc 	}
104107c0a28Sjtc 	mspeak(8);
10584f80fd6Slukem 	if (!wizard()) {
10684f80fd6Slukem 		mspeak(9);
107107c0a28Sjtc 		exit(0);
108107c0a28Sjtc 	}
109107c0a28Sjtc 	saved = -1;
110107c0a28Sjtc 	return (FALSE);
111107c0a28Sjtc }
112107c0a28Sjtc 
113243d04efSjmc /* not as complex as advent/10 (for now)        */
114c62bf84cSdholland static int
wizard(void)115243d04efSjmc wizard(void)
116243d04efSjmc {
11784f80fd6Slukem 	char   *word, *x;
11884f80fd6Slukem 	if (!yesm(16, 0, 7))
11984f80fd6Slukem 		return (FALSE);
120107c0a28Sjtc 	mspeak(17);
121107c0a28Sjtc 	getin(&word, &x);
12284f80fd6Slukem 	if (!weq(word, magic)) {
12384f80fd6Slukem 		mspeak(20);
124107c0a28Sjtc 		return (FALSE);
125107c0a28Sjtc 	}
126107c0a28Sjtc 	mspeak(19);
127107c0a28Sjtc 	return (TRUE);
128107c0a28Sjtc }
129107c0a28Sjtc 
130690bee10Schristos void
ciao(void)131243d04efSjmc ciao(void)
13284f80fd6Slukem {
133690bee10Schristos 	char fname[80];
134349050b8Sdholland 	size_t pos;
135107c0a28Sjtc 
136107c0a28Sjtc 	printf("What would you like to call the saved version?\n");
13708ab0f32Shubertf 	/* XXX - should use fgetln to avoid arbitrary limit */
138f0543574Sdholland 	for (pos = 0; pos < sizeof(fname) - 1; pos++) {
13908ab0f32Shubertf 		int ch;
14008ab0f32Shubertf 		ch = getchar();
14108ab0f32Shubertf 		if (ch == '\n' || ch == EOF)
14284f80fd6Slukem 			break;
143349050b8Sdholland 		fname[pos] = ch;
14408ab0f32Shubertf 	}
145349050b8Sdholland 	fname[pos] = '\0';
14684f80fd6Slukem 	if (save(fname) != 0)
14784f80fd6Slukem 		return;		/* Save failed */
148107c0a28Sjtc 	printf("To resume, say \"adventure %s\".\n", fname);
149243d04efSjmc 	printf("\"With these rooms I might now have been familiarly ");
150243d04efSjmc 	printf("acquainted.\"\n");
151107c0a28Sjtc 	exit(0);
152107c0a28Sjtc }
153107c0a28Sjtc 
154107c0a28Sjtc 
155690bee10Schristos int
ran(int range)156243d04efSjmc ran(int range)
157107c0a28Sjtc {
158690bee10Schristos 	long    i;
159107c0a28Sjtc 
160107c0a28Sjtc 	i = rand() % range;
161107c0a28Sjtc 	return (i);
162107c0a28Sjtc }
163