xref: /dragonfly/games/battlestar/command6.c (revision abf903a5)
1 /*-
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)com6.c	8.1 (Berkeley) 5/31/93
30  * $FreeBSD: src/games/battlestar/com6.c,v 1.9.2.1 2001/03/05 11:45:35 kris Exp $
31  * $DragonFly: src/games/battlestar/com6.c,v 1.4 2006/08/08 16:47:20 pavalos Exp $
32  */
33 
34 #include <signal.h>
35 #include <sys/time.h>
36 #include "externs.h"
37 #include "pathnames.h"
38 
39 static FILE *score_fp;
40 static void post(unsigned int ch);
41 
42 int
43 launch(void)
44 {
45 	if (testbit(location[position].objects, VIPER) && !notes[CANTLAUNCH]) {
46 		if (fuel > 4) {
47 			clearbit(location[position].objects, VIPER);
48 			position = location[position].up;
49 			notes[LAUNCHED] = 1;
50 			gtime++;
51 			fuel -= 4;
52 			puts("You climb into the viper and prepare for launch.");
53 			puts("With a touch of your thumb the turbo engines ignite, thrusting you back into\nyour seat.");
54 			return (1);
55 		} else
56 			puts("Not enough fuel to launch.");
57 	} else
58 		puts("Can't launch.");
59 	return (0);
60 }
61 
62 int
63 land(void)
64 {
65 	if (notes[LAUNCHED] && testbit(location[position].objects, LAND) &&
66 	    location[position].down) {
67 		notes[LAUNCHED] = 0;
68 		position = location[position].down;
69 		setbit(location[position].objects, VIPER);
70 		fuel -= 2;
71 		gtime++;
72 		puts("You are down.");
73 		return (1);
74 	} else
75 		puts("You can't land here.");
76 	return (0);
77 }
78 
79 void
80 die(int sig __unused)    /* endgame */
81 {
82 	printf("bye.\nYour rating was %s.\n", rate());
83 	post(' ');
84 	exit(0);
85 }
86 
87 void
88 live(void)
89 {
90 	puts("\nYou win!");
91 	post('!');
92 	exit(0);
93 }
94 
95 void
96 open_score_file(void)
97 {
98 	if ((score_fp = fopen(_PATH_SCORE, "a")) == NULL)
99 		perror(_PATH_SCORE);
100 }
101 
102 static void
103 post(unsigned int ch)
104 {
105 	struct timeval tv;
106 	char *date;
107 	time_t tvsec;
108 	int s;
109 
110 	if (score_fp == NULL)
111 		return;
112 
113 	s = sigblock(sigmask(SIGINT));
114 
115 	gettimeofday(&tv, NULL);        /* can't call time */
116 	tvsec = (time_t)tv.tv_sec;
117 	date = ctime(&tvsec);
118 	date[24] = '\0';
119 
120 	fprintf(score_fp, "%s  %8s  %c%20s", date, uname, ch, rate());
121 	if (wiz)
122 		fprintf(score_fp, "   wizard\n");
123 	else if (tempwiz)
124 		fprintf(score_fp, "   WIZARD!\n");
125 	else
126 		fprintf(score_fp, "\n");
127 
128 	sigsetmask(s);
129 }
130 
131 const char *
132 rate(void)
133 {
134 	int score;
135 
136 	score = max(max(pleasure, power), ego);
137 	if (score == pleasure) {
138 		if (score < 5)
139 			return ("novice");
140 		else if (score < 20)
141 			return ("junior voyeur");
142 		else if (score < 35)
143 			return ("Don Juan");
144 		else
145 			return ("Marquis De Sade");
146 	} else if (score == power) {
147 		if (score < 5)
148 			return ("serf");
149 		else if (score < 8)
150 			return ("Samurai");
151 		else if (score < 13)
152 			return ("Klingon");
153 		else if (score < 22)
154 			return ("Darth Vader");
155 		else
156 			return ("Sauron the Great");
157 	} else {
158 		if (score < 5)
159 			return ("Polyanna");
160 		else if (score < 10)
161 			return ("philanthropist");
162 		else if (score < 20)
163 			return ("Tattoo");
164 		else
165 			return ("Mr. Roarke");
166 	}
167 }
168 
169 int
170 drive(void)
171 {
172 	if (testbit(location[position].objects, CAR)) {
173 		puts("You hop in the car and turn the key.  There is a perceptible grating noise,");
174 		puts("and an explosion knocks you unconscious...");
175 		clearbit(location[position].objects, CAR);
176 		setbit(location[position].objects, CRASH);
177 		injuries[5] = injuries[6] = injuries[7] = injuries[8] = 1;
178 		gtime += 15;
179 		zzz();
180 		return (0);
181 	} else
182 		puts("There is nothing to drive here.");
183 	return (-1);
184 }
185 
186 int
187 ride(void)
188 {
189 	if (testbit(location[position].objects, HORSE)) {
190 		puts("You climb onto the stallion and kick it in the guts.  The stupid steed launches");
191 		puts("forward through bush and fern.  You are thrown and the horse gallups off.");
192 		clearbit(location[position].objects, HORSE);
193 		while (!(position = rnd(NUMOFROOMS + 1)) || !OUTSIDE ||
194 		    !beenthere[position] || location[position].flyhere)
195 			; /* nothing */
196 		setbit(location[position].objects, HORSE);
197 		if (location[position].north)
198 			position = location[position].north;
199 		else if (location[position].south)
200 			position = location[position].south;
201 		else if (location[position].east)
202 			position = location[position].east;
203 		else
204 			position = location[position].west;
205 		return (0);
206 	} else
207 		puts("There is no horse here.");
208 	return (-1);
209 }
210 
211 void
212 light(void)     /* synonyms = {strike, smoke} */
213 {               /* for matches, cigars */
214 	if (testbit(inven, MATCHES) && matchcount) {
215 		puts("Your match splutters to life.");
216 		gtime++;
217 		matchlight = 1;
218 		matchcount--;
219 		if (position == 217) {
220 			puts("The whole bungalow explodes with an intense blast.");
221 			die(0);
222 		}
223 	} else
224 		puts("You're out of matches.");
225 }
226